Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:43

0001 //===-- ModuleUtils.h - Functions to manipulate Modules ---------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This family of functions perform manipulations on Modules.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
0014 #define LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
0015 
0016 #include "llvm/ADT/STLFunctionalExtras.h"
0017 #include "llvm/ADT/StringRef.h"
0018 #include "llvm/IR/GlobalIFunc.h"
0019 #include "llvm/Support/Alignment.h"
0020 #include "llvm/Support/MemoryBufferRef.h"
0021 #include <utility> // for std::pair
0022 
0023 namespace llvm {
0024 template <typename T> class SmallVectorImpl;
0025 
0026 template <typename T> class ArrayRef;
0027 class Module;
0028 class Function;
0029 class FunctionCallee;
0030 class GlobalIFunc;
0031 class GlobalValue;
0032 class Constant;
0033 class ConstantStruct;
0034 class Value;
0035 class Type;
0036 
0037 /// Append F to the list of global ctors of module M with the given Priority.
0038 /// This wraps the function in the appropriate structure and stores it along
0039 /// side other global constructors. For details see
0040 /// https://llvm.org/docs/LangRef.html#the-llvm-global-ctors-global-variable
0041 void appendToGlobalCtors(Module &M, Function *F, int Priority,
0042                          Constant *Data = nullptr);
0043 
0044 /// Same as appendToGlobalCtors(), but for global dtors.
0045 void appendToGlobalDtors(Module &M, Function *F, int Priority,
0046                          Constant *Data = nullptr);
0047 
0048 /// Apply 'Fn' to the list of global ctors of module M and replace contructor
0049 /// record with the one returned by `Fn`. If `nullptr` was returned, the
0050 /// corresponding constructor will be removed from the array. For details see
0051 /// https://llvm.org/docs/LangRef.html#the-llvm-global-ctors-global-variable
0052 using GlobalCtorTransformFn = llvm::function_ref<Constant *(Constant *)>;
0053 void transformGlobalCtors(Module &M, const GlobalCtorTransformFn &Fn);
0054 void transformGlobalDtors(Module &M, const GlobalCtorTransformFn &Fn);
0055 
0056 /// Sets the KCFI type for the function. Used for compiler-generated functions
0057 /// that are indirectly called in instrumented code.
0058 void setKCFIType(Module &M, Function &F, StringRef MangledType);
0059 
0060 FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName,
0061                                             ArrayRef<Type *> InitArgTypes,
0062                                             bool Weak = false);
0063 
0064 /// Creates sanitizer constructor function.
0065 /// \return Returns pointer to constructor.
0066 Function *createSanitizerCtor(Module &M, StringRef CtorName);
0067 
0068 /// Creates sanitizer constructor function, and calls sanitizer's init
0069 /// function from it.
0070 /// \return Returns pair of pointers to constructor, and init functions
0071 /// respectively.
0072 std::pair<Function *, FunctionCallee> createSanitizerCtorAndInitFunctions(
0073     Module &M, StringRef CtorName, StringRef InitName,
0074     ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
0075     StringRef VersionCheckName = StringRef(), bool Weak = false);
0076 
0077 /// Creates sanitizer constructor function lazily. If a constructor and init
0078 /// function already exist, this function returns it. Otherwise it calls \c
0079 /// createSanitizerCtorAndInitFunctions. The FunctionsCreatedCallback is invoked
0080 /// in that case, passing the new Ctor and Init function.
0081 ///
0082 /// \return Returns pair of pointers to constructor, and init functions
0083 /// respectively.
0084 std::pair<Function *, FunctionCallee> getOrCreateSanitizerCtorAndInitFunctions(
0085     Module &M, StringRef CtorName, StringRef InitName,
0086     ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
0087     function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
0088     StringRef VersionCheckName = StringRef(), bool Weak = false);
0089 
0090 /// Rename all the anon globals in the module using a hash computed from
0091 /// the list of public globals in the module.
0092 bool nameUnamedGlobals(Module &M);
0093 
0094 /// Adds global values to the llvm.used list.
0095 void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values);
0096 
0097 /// Adds global values to the llvm.compiler.used list.
0098 void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values);
0099 
0100 /// Removes global values from the llvm.used and llvm.compiler.used arrays. \p
0101 /// ShouldRemove should return true for any initializer field that should not be
0102 /// included in the replacement global.
0103 void removeFromUsedLists(Module &M,
0104                          function_ref<bool(Constant *)> ShouldRemove);
0105 
0106 /// Filter out potentially dead comdat functions where other entries keep the
0107 /// entire comdat group alive.
0108 ///
0109 /// This is designed for cases where functions appear to become dead but remain
0110 /// alive due to other live entries in their comdat group.
0111 ///
0112 /// The \p DeadComdatFunctions container should only have pointers to
0113 /// `Function`s which are members of a comdat group and are believed to be
0114 /// dead.
0115 ///
0116 /// After this routine finishes, the only remaining `Function`s in \p
0117 /// DeadComdatFunctions are those where every member of the comdat is listed
0118 /// and thus removing them is safe (provided *all* are removed).
0119 void filterDeadComdatFunctions(
0120     SmallVectorImpl<Function *> &DeadComdatFunctions);
0121 
0122 /// Produce a unique identifier for this module by taking the MD5 sum of
0123 /// the names of the module's strong external symbols that are not comdat
0124 /// members.
0125 ///
0126 /// This identifier is normally guaranteed to be unique, or the program would
0127 /// fail to link due to multiply defined symbols.
0128 ///
0129 /// If the module has no strong external symbols (such a module may still have a
0130 /// semantic effect if it performs global initialization), we cannot produce a
0131 /// unique identifier for this module, so we return the empty string.
0132 std::string getUniqueModuleId(Module *M);
0133 
0134 /// Embed the memory buffer \p Buf into the module \p M as a global using the
0135 /// specified section name. Also provide a metadata entry to identify it in the
0136 /// module using the same section name.
0137 void embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName,
0138                          Align Alignment = Align(1));
0139 
0140 /// Lower all calls to ifuncs by replacing uses with indirect calls loaded out
0141 /// of a global table initialized in a global constructor. This will introduce
0142 /// one constructor function and adds it to llvm.global_ctors. The constructor
0143 /// will call the resolver function once for each ifunc.
0144 ///
0145 /// Leaves any unhandled constant initializer uses as-is.
0146 ///
0147 /// If \p IFuncsToLower is empty, all ifuncs in the module will be lowered.
0148 /// If \p IFuncsToLower is non-empty, only the selected ifuncs will be lowered.
0149 ///
0150 /// The processed ifuncs without remaining users will be removed from the
0151 /// module.
0152 bool lowerGlobalIFuncUsersAsGlobalCtor(
0153     Module &M, ArrayRef<GlobalIFunc *> IFuncsToLower = {});
0154 
0155 } // End llvm namespace
0156 
0157 #endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H