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