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/StringRef.h" 17 #include "llvm/Support/Alignment.h" 18 #include "llvm/Support/MemoryBufferRef.h" 19 #include <utility> // for std::pair 20 21 namespace llvm { 22 template <typename T> class SmallVectorImpl; 23 24 template <typename T> class ArrayRef; 25 class Module; 26 class Function; 27 class FunctionCallee; 28 class GlobalValue; 29 class Constant; 30 class Value; 31 class 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 37 void appendToGlobalCtors(Module &M, Function *F, int Priority, 38 Constant *Data = nullptr); 39 40 /// Same as appendToGlobalCtors(), but for global dtors. 41 void appendToGlobalDtors(Module &M, Function *F, int Priority, 42 Constant *Data = nullptr); 43 44 FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName, 45 ArrayRef<Type *> InitArgTypes); 46 47 /// Creates sanitizer constructor function. 48 /// \return Returns pointer to constructor. 49 Function *createSanitizerCtor(Module &M, StringRef CtorName); 50 51 /// Creates sanitizer constructor function, and calls sanitizer's init 52 /// function from it. 53 /// \return Returns pair of pointers to constructor, and init functions 54 /// respectively. 55 std::pair<Function *, FunctionCallee> createSanitizerCtorAndInitFunctions( 56 Module &M, StringRef CtorName, StringRef InitName, 57 ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs, 58 StringRef VersionCheckName = StringRef()); 59 60 /// Creates sanitizer constructor function lazily. If a constructor and init 61 /// function already exist, this function returns it. Otherwise it calls \c 62 /// createSanitizerCtorAndInitFunctions. The FunctionsCreatedCallback is invoked 63 /// in that case, passing the new Ctor and Init function. 64 /// 65 /// \return Returns pair of pointers to constructor, and init functions 66 /// respectively. 67 std::pair<Function *, FunctionCallee> getOrCreateSanitizerCtorAndInitFunctions( 68 Module &M, StringRef CtorName, StringRef InitName, 69 ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs, 70 function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback, 71 StringRef VersionCheckName = StringRef()); 72 73 /// Rename all the anon globals in the module using a hash computed from 74 /// the list of public globals in the module. 75 bool nameUnamedGlobals(Module &M); 76 77 /// Adds global values to the llvm.used list. 78 void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values); 79 80 /// Adds global values to the llvm.compiler.used list. 81 void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values); 82 83 /// Filter out potentially dead comdat functions where other entries keep the 84 /// entire comdat group alive. 85 /// 86 /// This is designed for cases where functions appear to become dead but remain 87 /// alive due to other live entries in their comdat group. 88 /// 89 /// The \p DeadComdatFunctions container should only have pointers to 90 /// `Function`s which are members of a comdat group and are believed to be 91 /// dead. 92 /// 93 /// After this routine finishes, the only remaining `Function`s in \p 94 /// DeadComdatFunctions are those where every member of the comdat is listed 95 /// and thus removing them is safe (provided *all* are removed). 96 void filterDeadComdatFunctions( 97 SmallVectorImpl<Function *> &DeadComdatFunctions); 98 99 /// Produce a unique identifier for this module by taking the MD5 sum of 100 /// the names of the module's strong external symbols that are not comdat 101 /// members. 102 /// 103 /// This identifier is normally guaranteed to be unique, or the program would 104 /// fail to link due to multiply defined symbols. 105 /// 106 /// If the module has no strong external symbols (such a module may still have a 107 /// semantic effect if it performs global initialization), we cannot produce a 108 /// unique identifier for this module, so we return the empty string. 109 std::string getUniqueModuleId(Module *M); 110 111 /// Embed the memory buffer \p Buf into the module \p M as a global using the 112 /// specified section name. Also provide a metadata entry to identify it in the 113 /// module using the same section name. 114 void embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName, 115 Align Alignment = Align(1)); 116 117 class CallInst; 118 namespace VFABI { 119 /// Overwrite the Vector Function ABI variants attribute with the names provide 120 /// in \p VariantMappings. 121 void setVectorVariantNames(CallInst *CI, ArrayRef<std::string> VariantMappings); 122 } // End VFABI namespace 123 } // End llvm namespace 124 125 #endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H 126