LLVM  9.0.0svn
ModuleUtils.h
Go to the documentation of this file.
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 <utility> // for std::pair
18 
19 namespace llvm {
20 
21 template <typename T> class ArrayRef;
22 class Module;
23 class Function;
24 class FunctionCallee;
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 FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName,
44  ArrayRef<Type *> InitArgTypes);
45 
46 /// Creates sanitizer constructor function, and calls sanitizer's init
47 /// function from it.
48 /// \return Returns pair of pointers to constructor, and init functions
49 /// respectively.
50 std::pair<Function *, FunctionCallee> createSanitizerCtorAndInitFunctions(
51  Module &M, StringRef CtorName, StringRef InitName,
52  ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
53  StringRef VersionCheckName = StringRef());
54 
55 /// Creates sanitizer constructor function lazily. If a constructor and init
56 /// function already exist, this function returns it. Otherwise it calls \c
57 /// createSanitizerCtorAndInitFunctions. The FunctionsCreatedCallback is invoked
58 /// in that case, passing the new Ctor and Init function.
59 ///
60 /// \return Returns pair of pointers to constructor, and init functions
61 /// respectively.
62 std::pair<Function *, FunctionCallee> getOrCreateSanitizerCtorAndInitFunctions(
63  Module &M, StringRef CtorName, StringRef InitName,
64  ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
65  function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
66  StringRef VersionCheckName = StringRef());
67 
68 // Creates and returns a sanitizer init function without argument if it doesn't
69 // exist, and adds it to the global constructors list. Otherwise it returns the
70 // existing function.
71 Function *getOrCreateInitFunction(Module &M, StringRef Name);
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).
97  Module &M, 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 } // End llvm namespace
112 
113 #endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.compiler.used list.
Type
MessagePack types as defined in the standard, with the exception of Integer being divided into a sign...
Definition: MsgPackReader.h:48
void appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Same as appendToGlobalCtors(), but for global dtors.
Definition: ModuleUtils.cpp:67
Function * getOrCreateInitFunction(Module &M, StringRef Name)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
F(f)
bool nameUnamedGlobals(Module &M)
Rename all the anon globals in the module using a hash computed from the list of public globals in th...
void appendToUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.used list.
std::pair< Function *, FunctionCallee > getOrCreateSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type *> InitArgTypes, ArrayRef< Value *> InitArgs, function_ref< void(Function *, FunctionCallee)> FunctionsCreatedCallback, StringRef VersionCheckName=StringRef())
Creates sanitizer constructor function lazily.
std::string getUniqueModuleId(Module *M)
Produce a unique identifier for this module by taking the MD5 sum of the names of the module&#39;s strong...
void appendToGlobalCtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Append F to the list of global ctors of module M with the given Priority.
Definition: ModuleUtils.cpp:63
FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName, ArrayRef< Type *> InitArgTypes)
void filterDeadComdatFunctions(Module &M, SmallVectorImpl< Function *> &DeadComdatFunctions)
Filter out potentially dead comdat functions where other entries keep the entire comdat group alive...
std::pair< Function *, FunctionCallee > createSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type *> InitArgTypes, ArrayRef< Value *> InitArgs, StringRef VersionCheckName=StringRef())
Creates sanitizer constructor function, and calls sanitizer&#39;s init function from it.