xref: /freebsd/contrib/llvm-project/llvm/include/llvm/Transforms/Utils/ModuleUtils.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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/STLFunctionalExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/IR/GlobalIFunc.h"
19 #include "llvm/Support/Alignment.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/MemoryBufferRef.h"
22 #include <utility> // for std::pair
23 
24 namespace llvm {
25 template <typename T> class SmallVectorImpl;
26 
27 template <typename T> class ArrayRef;
28 class Module;
29 class Function;
30 class FunctionCallee;
31 class GlobalIFunc;
32 class GlobalValue;
33 class Constant;
34 class ConstantStruct;
35 class Value;
36 class Type;
37 
38 /// Append F to the list of global ctors of module M with the given Priority.
39 /// This wraps the function in the appropriate structure and stores it along
40 /// side other global constructors. For details see
41 /// https://llvm.org/docs/LangRef.html#the-llvm-global-ctors-global-variable
42 LLVM_ABI void appendToGlobalCtors(Module &M, Function *F, int Priority,
43                                   Constant *Data = nullptr);
44 
45 /// Same as appendToGlobalCtors(), but for global dtors.
46 LLVM_ABI void appendToGlobalDtors(Module &M, Function *F, int Priority,
47                                   Constant *Data = nullptr);
48 
49 /// Apply 'Fn' to the list of global ctors of module M and replace contructor
50 /// record with the one returned by `Fn`. If `nullptr` was returned, the
51 /// corresponding constructor will be removed from the array. For details see
52 /// https://llvm.org/docs/LangRef.html#the-llvm-global-ctors-global-variable
53 using GlobalCtorTransformFn = llvm::function_ref<Constant *(Constant *)>;
54 LLVM_ABI void transformGlobalCtors(Module &M, const GlobalCtorTransformFn &Fn);
55 LLVM_ABI void transformGlobalDtors(Module &M, const GlobalCtorTransformFn &Fn);
56 
57 /// Sets the KCFI type for the function. Used for compiler-generated functions
58 /// that are indirectly called in instrumented code.
59 LLVM_ABI void setKCFIType(Module &M, Function &F, StringRef MangledType);
60 
61 LLVM_ABI FunctionCallee
62 declareSanitizerInitFunction(Module &M, StringRef InitName,
63                              ArrayRef<Type *> InitArgTypes, bool Weak = false);
64 
65 /// Creates sanitizer constructor function.
66 /// \return Returns pointer to constructor.
67 LLVM_ABI Function *createSanitizerCtor(Module &M, StringRef CtorName);
68 
69 /// Creates sanitizer constructor function, and calls sanitizer's init
70 /// function from it.
71 /// \return Returns pair of pointers to constructor, and init functions
72 /// respectively.
73 LLVM_ABI std::pair<Function *, FunctionCallee>
74 createSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName,
75                                     StringRef InitName,
76                                     ArrayRef<Type *> InitArgTypes,
77                                     ArrayRef<Value *> InitArgs,
78                                     StringRef VersionCheckName = StringRef(),
79                                     bool Weak = false);
80 
81 /// Creates sanitizer constructor function lazily. If a constructor and init
82 /// function already exist, this function returns it. Otherwise it calls \c
83 /// createSanitizerCtorAndInitFunctions. The FunctionsCreatedCallback is invoked
84 /// in that case, passing the new Ctor and Init function.
85 ///
86 /// \return Returns pair of pointers to constructor, and init functions
87 /// respectively.
88 LLVM_ABI std::pair<Function *, FunctionCallee>
89 getOrCreateSanitizerCtorAndInitFunctions(
90     Module &M, StringRef CtorName, StringRef InitName,
91     ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
92     function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
93     StringRef VersionCheckName = StringRef(), bool Weak = false);
94 
95 /// Rename all the anon globals in the module using a hash computed from
96 /// the list of public globals in the module.
97 LLVM_ABI bool nameUnamedGlobals(Module &M);
98 
99 /// Adds global values to the llvm.used list.
100 LLVM_ABI void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values);
101 
102 /// Adds global values to the llvm.compiler.used list.
103 LLVM_ABI void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values);
104 
105 /// Removes global values from the llvm.used and llvm.compiler.used arrays. \p
106 /// ShouldRemove should return true for any initializer field that should not be
107 /// included in the replacement global.
108 LLVM_ABI void removeFromUsedLists(Module &M,
109                                   function_ref<bool(Constant *)> ShouldRemove);
110 
111 /// Filter out potentially dead comdat functions where other entries keep the
112 /// entire comdat group alive.
113 ///
114 /// This is designed for cases where functions appear to become dead but remain
115 /// alive due to other live entries in their comdat group.
116 ///
117 /// The \p DeadComdatFunctions container should only have pointers to
118 /// `Function`s which are members of a comdat group and are believed to be
119 /// dead.
120 ///
121 /// After this routine finishes, the only remaining `Function`s in \p
122 /// DeadComdatFunctions are those where every member of the comdat is listed
123 /// and thus removing them is safe (provided *all* are removed).
124 LLVM_ABI void
125 filterDeadComdatFunctions(SmallVectorImpl<Function *> &DeadComdatFunctions);
126 
127 /// Produce a unique identifier for this module by taking the MD5 sum of
128 /// the names of the module's strong external symbols that are not comdat
129 /// members.
130 ///
131 /// This identifier is normally guaranteed to be unique, or the program would
132 /// fail to link due to multiply defined symbols.
133 ///
134 /// If the module has no strong external symbols (such a module may still have a
135 /// semantic effect if it performs global initialization), we cannot produce a
136 /// unique identifier for this module, so we return the empty string.
137 LLVM_ABI std::string getUniqueModuleId(Module *M);
138 
139 /// Embed the memory buffer \p Buf into the module \p M as a global using the
140 /// specified section name. Also provide a metadata entry to identify it in the
141 /// module using the same section name.
142 LLVM_ABI void embedBufferInModule(Module &M, MemoryBufferRef Buf,
143                                   StringRef SectionName,
144                                   Align Alignment = Align(1));
145 
146 /// Lower all calls to ifuncs by replacing uses with indirect calls loaded out
147 /// of a global table initialized in a global constructor. This will introduce
148 /// one constructor function and adds it to llvm.global_ctors. The constructor
149 /// will call the resolver function once for each ifunc.
150 ///
151 /// Leaves any unhandled constant initializer uses as-is.
152 ///
153 /// If \p IFuncsToLower is empty, all ifuncs in the module will be lowered.
154 /// If \p IFuncsToLower is non-empty, only the selected ifuncs will be lowered.
155 ///
156 /// The processed ifuncs without remaining users will be removed from the
157 /// module.
158 LLVM_ABI bool
159 lowerGlobalIFuncUsersAsGlobalCtor(Module &M,
160                                   ArrayRef<GlobalIFunc *> IFuncsToLower = {});
161 
162 } // End llvm namespace
163 
164 #endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
165