LLVM 24.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 Value;
35class Type;
36
37/// Append F to the list of global ctors of module M with the given Priority.
38/// This wraps the function in the appropriate structure and stores it along
39/// side other global constructors. For details see
40/// https://llvm.org/docs/LangRef.html#the-llvm-global-ctors-global-variable
41LLVM_ABI void appendToGlobalCtors(Module &M, Function *F, int Priority,
42 Constant *Data = nullptr);
43
44/// Same as appendToGlobalCtors(), but for global dtors.
45LLVM_ABI void appendToGlobalDtors(Module &M, Function *F, int Priority,
46 Constant *Data = nullptr);
47
48/// Apply 'Fn' to the list of global ctors of module M and replace contructor
49/// record with the one returned by `Fn`. If `nullptr` was returned, the
50/// corresponding constructor will be removed from the array. For details see
51/// https://llvm.org/docs/LangRef.html#the-llvm-global-ctors-global-variable
55
56/// Sets the KCFI type for the function. Used for compiler-generated functions
57/// that are indirectly called in instrumented code.
58LLVM_ABI void setKCFIType(Module &M, Function &F, StringRef MangledType);
59
62 ArrayRef<Type *> InitArgTypes, bool Weak = false);
63
64/// Creates sanitizer constructor function.
65/// \return Returns pointer to constructor.
67
68/// Creates sanitizer constructor function, and calls sanitizer's init
69/// function from it.
70/// \return Returns pair of pointers to constructor, and init functions
71/// respectively.
72LLVM_ABI std::pair<Function *, FunctionCallee>
74 StringRef InitName,
75 ArrayRef<Type *> InitArgTypes,
76 ArrayRef<Value *> InitArgs,
77 StringRef VersionCheckName = StringRef(),
78 bool Weak = false);
79
80/// Creates sanitizer constructor function lazily. If a constructor and init
81/// function already exist, this function returns it. Otherwise it calls \c
82/// createSanitizerCtorAndInitFunctions. The FunctionsCreatedCallback is invoked
83/// in that case, passing the new Ctor and Init function.
84///
85/// \return Returns pair of pointers to constructor, and init functions
86/// respectively.
87LLVM_ABI std::pair<Function *, FunctionCallee>
89 Module &M, StringRef CtorName, StringRef InitName,
90 ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
91 function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
92 StringRef VersionCheckName = StringRef(), bool Weak = false);
93
94/// Rename all the anon globals in the module using a hash computed from
95/// the list of public globals in the module.
97
98/// Adds global values to the llvm.used list.
100
101/// Adds global values to the llvm.compiler.used list.
103
104/// Removes global values from the llvm.used and llvm.compiler.used arrays. \p
105/// ShouldRemove should return true for any initializer field that should not be
106/// included in the replacement global.
108 function_ref<bool(Constant *)> ShouldRemove);
109
110/// Filter out potentially dead comdat functions where other entries keep the
111/// entire comdat group alive.
112///
113/// This is designed for cases where functions appear to become dead but remain
114/// alive due to other live entries in their comdat group.
115///
116/// The \p DeadComdatFunctions container should only have pointers to
117/// `Function`s which are members of a comdat group and are believed to be
118/// dead.
119///
120/// After this routine finishes, the only remaining `Function`s in \p
121/// DeadComdatFunctions are those where every member of the comdat is listed
122/// and thus removing them is safe (provided *all* are removed).
123LLVM_ABI void
125
126/// Replace the body of the dead function \p F with a single unreachable block.
127///
128/// Within a CGSCC walk, dead functions are only erased in a batch at the end of
129/// the walk, and their LazyCallGraph nodes stay valid until then. Use this
130/// instead of `Function::deleteBody` so such a function remains a definition
131/// until it is erased.
133
134/// Produce a unique identifier for this module by taking the MD5 sum of
135/// the names of the module's strong external symbols that are not comdat
136/// members.
137///
138/// The result is a hex string without a leading separator.
139///
140/// This identifier is normally guaranteed to be unique, or the program would
141/// fail to link due to multiply defined symbols.
142///
143/// If the module has no strong external symbols (such a module may still have a
144/// semantic effect if it performs global initialization), we cannot produce a
145/// unique identifier for this module, so we return the empty string.
146LLVM_ABI std::string getUniqueModuleId(Module *M);
147
148/// Embed the memory buffer \p Buf into the module \p M as a global using the
149/// specified section name. Also provide a metadata entry to identify it in the
150/// module using the same section name. If \p SectionExclude is true !exclude
151/// is applied to the global in order to apply necessary linkage flags to
152/// exclude the section from a link. If false, apply !metadata_section_kind
153/// which results in no additional section linkage flags.
156 Align Alignment = Align(1),
157 bool SectionExclude = true);
158
159/// Lower all calls to ifuncs by replacing uses with indirect calls loaded out
160/// of a global table initialized in a global constructor. This will introduce
161/// one constructor function and adds it to llvm.global_ctors. The constructor
162/// will call the resolver function once for each ifunc.
163///
164/// Leaves any unhandled constant initializer uses as-is.
165///
166/// If \p IFuncsToLower is empty, all ifuncs in the module will be lowered.
167/// If \p IFuncsToLower is non-empty, only the selected ifuncs will be lowered.
168///
169/// The processed ifuncs without remaining users will be removed from the
170/// module.
171LLVM_ABI bool
173 ArrayRef<GlobalIFunc *> IFuncsToLower = {});
174
175} // End llvm namespace
176
177#endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
#define LLVM_ABI
Definition Compiler.h:211
This file contains the declaration of the GlobalIFunc class, which represents a single indirect funct...
#define F(x, y, z)
Definition MD5.cpp:54
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This is an important base class in LLVM.
Definition Constant.h:44
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM Value Representation.
Definition Value.h:75
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void makeFunctionBodyUnreachable(Function &F)
Replace the body of the dead function F with a single unreachable block.
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
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.
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.
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::function_ref< Constant *(Constant *)> GlobalCtorTransformFn
Apply 'Fn' to the list of global ctors of module M and replace contructor record with the one returne...
Definition ModuleUtils.h:52
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.
LLVM_ABI GlobalVariable * embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName, Align Alignment=Align(1), bool SectionExclude=true)
Embed the memory buffer Buf into the module M as a global using the specified section name.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39