ModuleUtils.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===-- ModuleUtils.h - Functions to manipulate Modules ---------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This family of functions perform manipulations on Modules.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
  18. #define LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
  19. #include "llvm/ADT/STLFunctionalExtras.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/IR/GlobalIFunc.h"
  22. #include "llvm/Support/Alignment.h"
  23. #include "llvm/Support/MemoryBufferRef.h"
  24. #include <utility> // for std::pair
  25. namespace llvm {
  26. template <typename T> class SmallVectorImpl;
  27. template <typename T> class ArrayRef;
  28. class Module;
  29. class Function;
  30. class FunctionCallee;
  31. class GlobalIFunc;
  32. class GlobalValue;
  33. class Constant;
  34. class Value;
  35. class Type;
  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
  40. void appendToGlobalCtors(Module &M, Function *F, int Priority,
  41. Constant *Data = nullptr);
  42. /// Same as appendToGlobalCtors(), but for global dtors.
  43. void appendToGlobalDtors(Module &M, Function *F, int Priority,
  44. Constant *Data = nullptr);
  45. /// Sets the KCFI type for the function. Used for compiler-generated functions
  46. /// that are indirectly called in instrumented code.
  47. void setKCFIType(Module &M, Function &F, StringRef MangledType);
  48. FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName,
  49. ArrayRef<Type *> InitArgTypes,
  50. bool Weak = false);
  51. /// Creates sanitizer constructor function.
  52. /// \return Returns pointer to constructor.
  53. Function *createSanitizerCtor(Module &M, StringRef CtorName);
  54. /// Creates sanitizer constructor function, and calls sanitizer's init
  55. /// function from it.
  56. /// \return Returns pair of pointers to constructor, and init functions
  57. /// respectively.
  58. std::pair<Function *, FunctionCallee> createSanitizerCtorAndInitFunctions(
  59. Module &M, StringRef CtorName, StringRef InitName,
  60. ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
  61. StringRef VersionCheckName = StringRef(), bool Weak = false);
  62. /// Creates sanitizer constructor function lazily. If a constructor and init
  63. /// function already exist, this function returns it. Otherwise it calls \c
  64. /// createSanitizerCtorAndInitFunctions. The FunctionsCreatedCallback is invoked
  65. /// in that case, passing the new Ctor and Init function.
  66. ///
  67. /// \return Returns pair of pointers to constructor, and init functions
  68. /// respectively.
  69. std::pair<Function *, FunctionCallee> getOrCreateSanitizerCtorAndInitFunctions(
  70. Module &M, StringRef CtorName, StringRef InitName,
  71. ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
  72. function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
  73. StringRef VersionCheckName = StringRef(), bool Weak = false);
  74. /// Rename all the anon globals in the module using a hash computed from
  75. /// the list of public globals in the module.
  76. bool nameUnamedGlobals(Module &M);
  77. /// Adds global values to the llvm.used list.
  78. void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values);
  79. /// Adds global values to the llvm.compiler.used list.
  80. void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values);
  81. /// Removes global values from the llvm.used and llvm.compiler.used arrays. \p
  82. /// ShouldRemove should return true for any initializer field that should not be
  83. /// included in the replacement global.
  84. void removeFromUsedLists(Module &M,
  85. function_ref<bool(Constant *)> ShouldRemove);
  86. /// Filter out potentially dead comdat functions where other entries keep the
  87. /// entire comdat group alive.
  88. ///
  89. /// This is designed for cases where functions appear to become dead but remain
  90. /// alive due to other live entries in their comdat group.
  91. ///
  92. /// The \p DeadComdatFunctions container should only have pointers to
  93. /// `Function`s which are members of a comdat group and are believed to be
  94. /// dead.
  95. ///
  96. /// After this routine finishes, the only remaining `Function`s in \p
  97. /// DeadComdatFunctions are those where every member of the comdat is listed
  98. /// and thus removing them is safe (provided *all* are removed).
  99. void filterDeadComdatFunctions(
  100. SmallVectorImpl<Function *> &DeadComdatFunctions);
  101. /// Produce a unique identifier for this module by taking the MD5 sum of
  102. /// the names of the module's strong external symbols that are not comdat
  103. /// members.
  104. ///
  105. /// This identifier is normally guaranteed to be unique, or the program would
  106. /// fail to link due to multiply defined symbols.
  107. ///
  108. /// If the module has no strong external symbols (such a module may still have a
  109. /// semantic effect if it performs global initialization), we cannot produce a
  110. /// unique identifier for this module, so we return the empty string.
  111. std::string getUniqueModuleId(Module *M);
  112. /// Embed the memory buffer \p Buf into the module \p M as a global using the
  113. /// specified section name. Also provide a metadata entry to identify it in the
  114. /// module using the same section name.
  115. void embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName,
  116. Align Alignment = Align(1));
  117. /// Lower all calls to ifuncs by replacing uses with indirect calls loaded out
  118. /// of a global table initialized in a global constructor. This will introduce
  119. /// one constructor function and adds it to llvm.global_ctors. The constructor
  120. /// will call the resolver function once for each ifunc.
  121. ///
  122. /// Leaves any unhandled constant initializer uses as-is.
  123. ///
  124. /// If \p IFuncsToLower is empty, all ifuncs in the module will be lowered.
  125. /// If \p IFuncsToLower is non-empty, only the selected ifuncs will be lowered.
  126. ///
  127. /// The processed ifuncs without remaining users will be removed from the
  128. /// module.
  129. bool lowerGlobalIFuncUsersAsGlobalCtor(
  130. Module &M, ArrayRef<GlobalIFunc *> IFuncsToLower = {});
  131. class CallInst;
  132. namespace VFABI {
  133. /// Overwrite the Vector Function ABI variants attribute with the names provide
  134. /// in \p VariantMappings.
  135. void setVectorVariantNames(CallInst *CI, ArrayRef<std::string> VariantMappings);
  136. } // End VFABI namespace
  137. } // End llvm namespace
  138. #endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
  139. #ifdef __GNUC__
  140. #pragma GCC diagnostic pop
  141. #endif