LLVM 17.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"
21#include <utility> // for std::pair
22
23namespace llvm {
24template <typename T> class SmallVectorImpl;
25
26template <typename T> class ArrayRef;
27class Module;
28class Function;
29class FunctionCallee;
30class GlobalIFunc;
31class GlobalValue;
32class Constant;
33class Value;
34class Type;
35
36/// Append F to the list of global ctors of module M with the given Priority.
37/// This wraps the function in the appropriate structure and stores it along
38/// side other global constructors. For details see
39/// https://llvm.org/docs/LangRef.html#the-llvm-global-ctors-global-variable
40void appendToGlobalCtors(Module &M, Function *F, int Priority,
41 Constant *Data = nullptr);
42
43/// Same as appendToGlobalCtors(), but for global dtors.
44void appendToGlobalDtors(Module &M, Function *F, int Priority,
45 Constant *Data = nullptr);
46
47/// Sets the KCFI type for the function. Used for compiler-generated functions
48/// that are indirectly called in instrumented code.
49void setKCFIType(Module &M, Function &F, StringRef MangledType);
50
51FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName,
52 ArrayRef<Type *> InitArgTypes,
53 bool Weak = false);
54
55/// Creates sanitizer constructor function.
56/// \return Returns pointer to constructor.
57Function *createSanitizerCtor(Module &M, StringRef CtorName);
58
59/// Creates sanitizer constructor function, and calls sanitizer's init
60/// function from it.
61/// \return Returns pair of pointers to constructor, and init functions
62/// respectively.
63std::pair<Function *, FunctionCallee> createSanitizerCtorAndInitFunctions(
64 Module &M, StringRef CtorName, StringRef InitName,
65 ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
66 StringRef VersionCheckName = StringRef(), bool Weak = false);
67
68/// Creates sanitizer constructor function lazily. If a constructor and init
69/// function already exist, this function returns it. Otherwise it calls \c
70/// createSanitizerCtorAndInitFunctions. The FunctionsCreatedCallback is invoked
71/// in that case, passing the new Ctor and Init function.
72///
73/// \return Returns pair of pointers to constructor, and init functions
74/// respectively.
75std::pair<Function *, FunctionCallee> getOrCreateSanitizerCtorAndInitFunctions(
76 Module &M, StringRef CtorName, StringRef InitName,
77 ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
78 function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
79 StringRef VersionCheckName = StringRef(), bool Weak = false);
80
81/// Rename all the anon globals in the module using a hash computed from
82/// the list of public globals in the module.
84
85/// Adds global values to the llvm.used list.
86void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values);
87
88/// Adds global values to the llvm.compiler.used list.
89void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values);
90
91/// Removes global values from the llvm.used and llvm.compiler.used arrays. \p
92/// ShouldRemove should return true for any initializer field that should not be
93/// included in the replacement global.
95 function_ref<bool(Constant *)> ShouldRemove);
96
97/// Filter out potentially dead comdat functions where other entries keep the
98/// entire comdat group alive.
99///
100/// This is designed for cases where functions appear to become dead but remain
101/// alive due to other live entries in their comdat group.
102///
103/// The \p DeadComdatFunctions container should only have pointers to
104/// `Function`s which are members of a comdat group and are believed to be
105/// dead.
106///
107/// After this routine finishes, the only remaining `Function`s in \p
108/// DeadComdatFunctions are those where every member of the comdat is listed
109/// and thus removing them is safe (provided *all* are removed).
111 SmallVectorImpl<Function *> &DeadComdatFunctions);
112
113/// Produce a unique identifier for this module by taking the MD5 sum of
114/// the names of the module's strong external symbols that are not comdat
115/// members.
116///
117/// This identifier is normally guaranteed to be unique, or the program would
118/// fail to link due to multiply defined symbols.
119///
120/// If the module has no strong external symbols (such a module may still have a
121/// semantic effect if it performs global initialization), we cannot produce a
122/// unique identifier for this module, so we return the empty string.
123std::string getUniqueModuleId(Module *M);
124
125/// Embed the memory buffer \p Buf into the module \p M as a global using the
126/// specified section name. Also provide a metadata entry to identify it in the
127/// module using the same section name.
128void embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName,
129 Align Alignment = Align(1));
130
131/// Lower all calls to ifuncs by replacing uses with indirect calls loaded out
132/// of a global table initialized in a global constructor. This will introduce
133/// one constructor function and adds it to llvm.global_ctors. The constructor
134/// will call the resolver function once for each ifunc.
135///
136/// Leaves any unhandled constant initializer uses as-is.
137///
138/// If \p IFuncsToLower is empty, all ifuncs in the module will be lowered.
139/// If \p IFuncsToLower is non-empty, only the selected ifuncs will be lowered.
140///
141/// The processed ifuncs without remaining users will be removed from the
142/// module.
144 Module &M, ArrayRef<GlobalIFunc *> IFuncsToLower = {});
145
146class CallInst;
147namespace VFABI {
148/// Overwrite the Vector Function ABI variants attribute with the names provide
149/// in \p VariantMappings.
150void setVectorVariantNames(CallInst *CI, ArrayRef<std::string> VariantMappings);
151} // End VFABI namespace
152} // End llvm namespace
153
154#endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
RelocType Type
Definition: COFFYAML.cpp:390
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
void setVectorVariantNames(CallInst *CI, ArrayRef< std::string > VariantMappings)
Overwrite the Vector Function ABI variants attribute with the names provide in VariantMappings.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Function * createSanitizerCtor(Module &M, StringRef CtorName)
Creates sanitizer constructor function.
FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName, ArrayRef< Type * > InitArgTypes, bool Weak=false)
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...
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.
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.
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 removeFromUsedLists(Module &M, function_ref< bool(Constant *)> ShouldRemove)
Removes global values from the llvm.used and llvm.compiler.used arrays.
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
ArrayRef(const T &OneElt) -> ArrayRef< T >
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:69
void setKCFIType(Module &M, Function &F, StringRef MangledType)
Sets the KCFI type for the function.
void filterDeadComdatFunctions(SmallVectorImpl< Function * > &DeadComdatFunctions)
Filter out potentially dead comdat functions where other entries keep the entire comdat group alive.
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.
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...
void appendToUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.used list.
void appendToGlobalDtors(Module &M, Function *F, int Priority, Constant *Data=nullptr)
Same as appendToGlobalCtors(), but for global dtors.
Definition: ModuleUtils.cpp:73