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