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 /// Rename all the anon globals in the module using a hash computed from
72 /// the list of public globals in the module.
73 bool nameUnamedGlobals(Module &M);
74 
75 /// Adds global values to the llvm.used list.
76 void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values);
77 
78 /// Adds global values to the llvm.compiler.used list.
79 void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values);
80 
81 /// Filter out potentially dead comdat functions where other entries keep the
82 /// entire comdat group alive.
83 ///
84 /// This is designed for cases where functions appear to become dead but remain
85 /// alive due to other live entries in their comdat group.
86 ///
87 /// The \p DeadComdatFunctions container should only have pointers to
88 /// `Function`s which are members of a comdat group and are believed to be
89 /// dead.
90 ///
91 /// After this routine finishes, the only remaining `Function`s in \p
92 /// DeadComdatFunctions are those where every member of the comdat is listed
93 /// and thus removing them is safe (provided *all* are removed).
94 void filterDeadComdatFunctions(
95     Module &M, SmallVectorImpl<Function *> &DeadComdatFunctions);
96 
97 /// Produce a unique identifier for this module by taking the MD5 sum of
98 /// the names of the module's strong external symbols that are not comdat
99 /// members.
100 ///
101 /// This identifier is normally guaranteed to be unique, or the program would
102 /// fail to link due to multiply defined symbols.
103 ///
104 /// If the module has no strong external symbols (such a module may still have a
105 /// semantic effect if it performs global initialization), we cannot produce a
106 /// unique identifier for this module, so we return the empty string.
107 std::string getUniqueModuleId(Module *M);
108 
109 class CallInst;
110 namespace VFABI {
111 /// Overwrite the Vector Function ABI variants attribute with the names provide
112 /// in \p VariantMappings.
113 void setVectorVariantNames(CallInst *CI,
114                            const SmallVector<std::string, 8> &VariantMappings);
115 } // End VFABI namespace
116 } // End llvm namespace
117 
118 #endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
119