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