LLVM 22.0.0git
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
17#include "llvm/ADT/StringRef.h"
18#include "llvm/IR/GlobalIFunc.h"
22#include <utility> // for std::pair
23
24namespace llvm {
25template <typename T> class SmallVectorImpl;
26
27template <typename T> class ArrayRef;
28class Module;
29class Function;
30class FunctionCallee;
31class GlobalIFunc;
32class GlobalValue;
33class Constant;
34class ConstantStruct;
35class Value;
36class Type;
37
38/// Append F to the list of global ctors of module M with the given Priority.
39/// This wraps the function in the appropriate structure and stores it along
40/// side other global constructors. For details see
41/// https://llvm.org/docs/LangRef.html#the-llvm-global-ctors-global-variable
42LLVM_ABI void appendToGlobalCtors(Module &M, Function *F, int Priority,
43 Constant *Data = nullptr);
44
45/// Same as appendToGlobalCtors(), but for global dtors.
46LLVM_ABI void appendToGlobalDtors(Module &M, Function *F, int Priority,
47 Constant *Data = nullptr);
48
49/// Apply 'Fn' to the list of global ctors of module M and replace contructor
50/// record with the one returned by `Fn`. If `nullptr` was returned, the
51/// corresponding constructor will be removed from the array. For details see
52/// https://llvm.org/docs/LangRef.html#the-llvm-global-ctors-global-variable
56
57/// Sets the KCFI type for the function. Used for compiler-generated functions
58/// that are indirectly called in instrumented code.
59LLVM_ABI void setKCFIType(Module &M, Function &F, StringRef MangledType);
60
63 ArrayRef<Type *> InitArgTypes, bool Weak = false);
64
65/// Creates sanitizer constructor function.
66/// \return Returns pointer to constructor.
68
69/// Creates sanitizer constructor function, and calls sanitizer's init
70/// function from it.
71/// \return Returns pair of pointers to constructor, and init functions
72/// respectively.
73LLVM_ABI std::pair<Function *, FunctionCallee>
75 StringRef InitName,
76 ArrayRef<Type *> InitArgTypes,
77 ArrayRef<Value *> InitArgs,
78 StringRef VersionCheckName = StringRef(),
79 bool Weak = false);
80
81/// Creates sanitizer constructor function lazily. If a constructor and init
82/// function already exist, this function returns it. Otherwise it calls \c
83/// createSanitizerCtorAndInitFunctions. The FunctionsCreatedCallback is invoked
84/// in that case, passing the new Ctor and Init function.
85///
86/// \return Returns pair of pointers to constructor, and init functions
87/// respectively.
88LLVM_ABI std::pair<Function *, FunctionCallee>
90 Module &M, StringRef CtorName, StringRef InitName,
91 ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
92 function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
93 StringRef VersionCheckName = StringRef(), bool Weak = false);
94
95/// Rename all the anon globals in the module using a hash computed from
96/// the list of public globals in the module.
98
99/// Adds global values to the llvm.used list.
101
102/// Adds global values to the llvm.compiler.used list.
104
105/// Removes global values from the llvm.used and llvm.compiler.used arrays. \p
106/// ShouldRemove should return true for any initializer field that should not be
107/// included in the replacement global.
109 function_ref<bool(Constant *)> ShouldRemove);
110
111/// Filter out potentially dead comdat functions where other entries keep the
112/// entire comdat group alive.
113///
114/// This is designed for cases where functions appear to become dead but remain
115/// alive due to other live entries in their comdat group.
116///
117/// The \p DeadComdatFunctions container should only have pointers to
118/// `Function`s which are members of a comdat group and are believed to be
119/// dead.
120///
121/// After this routine finishes, the only remaining `Function`s in \p
122/// DeadComdatFunctions are those where every member of the comdat is listed
123/// and thus removing them is safe (provided *all* are removed).
124LLVM_ABI void
126
127/// Produce a unique identifier for this module by taking the MD5 sum of
128/// the names of the module's strong external symbols that are not comdat
129/// members.
130///
131/// This identifier is normally guaranteed to be unique, or the program would
132/// fail to link due to multiply defined symbols.
133///
134/// If the module has no strong external symbols (such a module may still have a
135/// semantic effect if it performs global initialization), we cannot produce a
136/// unique identifier for this module, so we return the empty string.
137LLVM_ABI std::string getUniqueModuleId(Module *M);
138
139/// Embed the memory buffer \p Buf into the module \p M as a global using the
140/// specified section name. Also provide a metadata entry to identify it in the
141/// module using the same section name.
144 Align Alignment = Align(1));
145
146/// Lower all calls to ifuncs by replacing uses with indirect calls loaded out
147/// of a global table initialized in a global constructor. This will introduce
148/// one constructor function and adds it to llvm.global_ctors. The constructor
149/// will call the resolver function once for each ifunc.
150///
151/// Leaves any unhandled constant initializer uses as-is.
152///
153/// If \p IFuncsToLower is empty, all ifuncs in the module will be lowered.
154/// If \p IFuncsToLower is non-empty, only the selected ifuncs will be lowered.
155///
156/// The processed ifuncs without remaining users will be removed from the
157/// module.
158LLVM_ABI bool
160 ArrayRef<GlobalIFunc *> IFuncsToLower = {});
161
162} // End llvm namespace
163
164#endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
RelocType Type
Definition: COFFYAML.cpp:410
#define LLVM_ABI
Definition: Compiler.h:213
uint64_t Align
This file contains the declaration of the GlobalIFunc class, which represents a single indirect funct...
#define F(x, y, z)
Definition: MD5.cpp:55
Machine Check Debug Module
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
This is an important base class in LLVM.
Definition: Constant.h:43
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:170
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI Function * createSanitizerCtor(Module &M, StringRef CtorName)
Creates sanitizer constructor function.
LLVM_ABI FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName, ArrayRef< Type * > InitArgTypes, bool Weak=false)
LLVM_ABI void transformGlobalDtors(Module &M, const GlobalCtorTransformFn &Fn)
LLVM_ABI std::string getUniqueModuleId(Module *M)
Produce a unique identifier for this module by taking the MD5 sum of the names of the module's strong...
LLVM_ABI 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(), bool Weak=false)
Creates sanitizer constructor function lazily.
LLVM_ABI std::pair< Function *, FunctionCallee > createSanitizerCtorAndInitFunctions(Module &M, StringRef CtorName, StringRef InitName, ArrayRef< Type * > InitArgTypes, ArrayRef< Value * > InitArgs, StringRef VersionCheckName=StringRef(), bool Weak=false)
Creates sanitizer constructor function, and calls sanitizer's init function from it.
LLVM_ABI bool nameUnamedGlobals(Module &M)
Rename all the anon globals in the module using a hash computed from the list of public globals in th...
LLVM_ABI void removeFromUsedLists(Module &M, function_ref< bool(Constant *)> ShouldRemove)
Removes global values from the llvm.used and llvm.compiler.used arrays.
LLVM_ABI void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI 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:74
LLVM_ABI void setKCFIType(Module &M, Function &F, StringRef MangledType)
Sets the KCFI type for the function.
LLVM_ABI void transformGlobalCtors(Module &M, const GlobalCtorTransformFn &Fn)
LLVM_ABI void filterDeadComdatFunctions(SmallVectorImpl< Function * > &DeadComdatFunctions)
Filter out potentially dead comdat functions where other entries keep the entire comdat group alive.
LLVM_ABI void embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName, Align Alignment=Align(1))
Embed the memory buffer Buf into the module M as a global using the specified section name.
LLVM_ABI bool lowerGlobalIFuncUsersAsGlobalCtor(Module &M, ArrayRef< GlobalIFunc * > IFuncsToLower={})
Lower all calls to ifuncs by replacing uses with indirect calls loaded out of a global table initiali...
LLVM_ABI void appendToUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.used list.
LLVM_ABI void appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Same as appendToGlobalCtors(), but for global dtors.
Definition: ModuleUtils.cpp:78
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39