ModuleUtils.h revision 360784
1//===-- ModuleUtils.h - Functions to manipulate Modules ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This family of functions perform manipulations on Modules.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
14#define LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
15
16#include "llvm/ADT/SmallSet.h"
17#include "llvm/ADT/StringRef.h"
18#include <utility> // for std::pair
19
20namespace llvm {
21
22template <typename T> class ArrayRef;
23class Module;
24class Function;
25class FunctionCallee;
26class GlobalValue;
27class GlobalVariable;
28class Constant;
29class StringRef;
30class Value;
31class Type;
32
33/// Append F to the list of global ctors of module M with the given Priority.
34/// This wraps the function in the appropriate structure and stores it along
35/// side other global constructors. For details see
36/// http://llvm.org/docs/LangRef.html#intg_global_ctors
37void appendToGlobalCtors(Module &M, Function *F, int Priority,
38                         Constant *Data = nullptr);
39
40/// Same as appendToGlobalCtors(), but for global dtors.
41void appendToGlobalDtors(Module &M, Function *F, int Priority,
42                         Constant *Data = nullptr);
43
44FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName,
45                                            ArrayRef<Type *> InitArgTypes);
46
47/// Creates sanitizer constructor function, and calls sanitizer's init
48/// function from it.
49/// \return Returns pair of pointers to constructor, and init functions
50/// respectively.
51std::pair<Function *, FunctionCallee> createSanitizerCtorAndInitFunctions(
52    Module &M, StringRef CtorName, StringRef InitName,
53    ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
54    StringRef VersionCheckName = StringRef());
55
56/// Creates sanitizer constructor function lazily. If a constructor and init
57/// function already exist, this function returns it. Otherwise it calls \c
58/// createSanitizerCtorAndInitFunctions. The FunctionsCreatedCallback is invoked
59/// in that case, passing the new Ctor and Init function.
60///
61/// \return Returns pair of pointers to constructor, and init functions
62/// respectively.
63std::pair<Function *, FunctionCallee> getOrCreateSanitizerCtorAndInitFunctions(
64    Module &M, StringRef CtorName, StringRef InitName,
65    ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
66    function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
67    StringRef VersionCheckName = StringRef());
68
69// Creates and returns a sanitizer init function without argument if it doesn't
70// exist, and adds it to the global constructors list. Otherwise it returns the
71// existing function.
72Function *getOrCreateInitFunction(Module &M, StringRef Name);
73
74/// Rename all the anon globals in the module using a hash computed from
75/// the list of public globals in the module.
76bool nameUnamedGlobals(Module &M);
77
78/// Adds global values to the llvm.used list.
79void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values);
80
81/// Adds global values to the llvm.compiler.used list.
82void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values);
83
84/// Filter out potentially dead comdat functions where other entries keep the
85/// entire comdat group alive.
86///
87/// This is designed for cases where functions appear to become dead but remain
88/// alive due to other live entries in their comdat group.
89///
90/// The \p DeadComdatFunctions container should only have pointers to
91/// `Function`s which are members of a comdat group and are believed to be
92/// dead.
93///
94/// After this routine finishes, the only remaining `Function`s in \p
95/// DeadComdatFunctions are those where every member of the comdat is listed
96/// and thus removing them is safe (provided *all* are removed).
97void filterDeadComdatFunctions(
98    Module &M, SmallVectorImpl<Function *> &DeadComdatFunctions);
99
100/// Produce a unique identifier for this module by taking the MD5 sum of
101/// the names of the module's strong external symbols that are not comdat
102/// members.
103///
104/// This identifier is normally guaranteed to be unique, or the program would
105/// fail to link due to multiply defined symbols.
106///
107/// If the module has no strong external symbols (such a module may still have a
108/// semantic effect if it performs global initialization), we cannot produce a
109/// unique identifier for this module, so we return the empty string.
110std::string getUniqueModuleId(Module *M);
111
112class CallInst;
113namespace VFABI {
114/// Overwrite the Vector Function ABI variants attribute with the names provide
115/// in \p VariantMappings.
116void setVectorVariantNames(CallInst *CI,
117                           const SmallVector<std::string, 8> &VariantMappings);
118} // End VFABI namespace
119} // End llvm namespace
120
121#endif //  LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
122