ModuleUtils.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/SmallVector.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/Support/MemoryBuffer.h"
  22. #include <utility> // for std::pair
  23. namespace llvm {
  24. template <typename T> class ArrayRef;
  25. class Module;
  26. class Function;
  27. class FunctionCallee;
  28. class GlobalValue;
  29. class Constant;
  30. class Value;
  31. class Type;
  32. /// Append F to the list of global ctors of module M with the given Priority.
  33. /// This wraps the function in the appropriate structure and stores it along
  34. /// side other global constructors. For details see
  35. /// http://llvm.org/docs/LangRef.html#intg_global_ctors
  36. void appendToGlobalCtors(Module &M, Function *F, int Priority,
  37. Constant *Data = nullptr);
  38. /// Same as appendToGlobalCtors(), but for global dtors.
  39. void appendToGlobalDtors(Module &M, Function *F, int Priority,
  40. Constant *Data = nullptr);
  41. FunctionCallee declareSanitizerInitFunction(Module &M, StringRef InitName,
  42. ArrayRef<Type *> InitArgTypes);
  43. /// Creates sanitizer constructor function.
  44. /// \return Returns pointer to constructor.
  45. Function *createSanitizerCtor(Module &M, StringRef CtorName);
  46. /// Creates sanitizer constructor function, and calls sanitizer's init
  47. /// function from it.
  48. /// \return Returns pair of pointers to constructor, and init functions
  49. /// respectively.
  50. std::pair<Function *, FunctionCallee> createSanitizerCtorAndInitFunctions(
  51. Module &M, StringRef CtorName, StringRef InitName,
  52. ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
  53. StringRef VersionCheckName = StringRef());
  54. /// Creates sanitizer constructor function lazily. If a constructor and init
  55. /// function already exist, this function returns it. Otherwise it calls \c
  56. /// createSanitizerCtorAndInitFunctions. The FunctionsCreatedCallback is invoked
  57. /// in that case, passing the new Ctor and Init function.
  58. ///
  59. /// \return Returns pair of pointers to constructor, and init functions
  60. /// respectively.
  61. std::pair<Function *, FunctionCallee> getOrCreateSanitizerCtorAndInitFunctions(
  62. Module &M, StringRef CtorName, StringRef InitName,
  63. ArrayRef<Type *> InitArgTypes, ArrayRef<Value *> InitArgs,
  64. function_ref<void(Function *, FunctionCallee)> FunctionsCreatedCallback,
  65. StringRef VersionCheckName = StringRef());
  66. /// Rename all the anon globals in the module using a hash computed from
  67. /// the list of public globals in the module.
  68. bool nameUnamedGlobals(Module &M);
  69. /// Adds global values to the llvm.used list.
  70. void appendToUsed(Module &M, ArrayRef<GlobalValue *> Values);
  71. /// Adds global values to the llvm.compiler.used list.
  72. void appendToCompilerUsed(Module &M, ArrayRef<GlobalValue *> Values);
  73. /// Filter out potentially dead comdat functions where other entries keep the
  74. /// entire comdat group alive.
  75. ///
  76. /// This is designed for cases where functions appear to become dead but remain
  77. /// alive due to other live entries in their comdat group.
  78. ///
  79. /// The \p DeadComdatFunctions container should only have pointers to
  80. /// `Function`s which are members of a comdat group and are believed to be
  81. /// dead.
  82. ///
  83. /// After this routine finishes, the only remaining `Function`s in \p
  84. /// DeadComdatFunctions are those where every member of the comdat is listed
  85. /// and thus removing them is safe (provided *all* are removed).
  86. void filterDeadComdatFunctions(
  87. SmallVectorImpl<Function *> &DeadComdatFunctions);
  88. /// Produce a unique identifier for this module by taking the MD5 sum of
  89. /// the names of the module's strong external symbols that are not comdat
  90. /// members.
  91. ///
  92. /// This identifier is normally guaranteed to be unique, or the program would
  93. /// fail to link due to multiply defined symbols.
  94. ///
  95. /// If the module has no strong external symbols (such a module may still have a
  96. /// semantic effect if it performs global initialization), we cannot produce a
  97. /// unique identifier for this module, so we return the empty string.
  98. std::string getUniqueModuleId(Module *M);
  99. /// Embed the memory buffer \p Buf into the module \p M as a global using the
  100. /// specified section name.
  101. void embedBufferInModule(Module &M, MemoryBufferRef Buf, StringRef SectionName);
  102. class CallInst;
  103. namespace VFABI {
  104. /// Overwrite the Vector Function ABI variants attribute with the names provide
  105. /// in \p VariantMappings.
  106. void setVectorVariantNames(CallInst *CI,
  107. const SmallVector<std::string, 8> &VariantMappings);
  108. } // End VFABI namespace
  109. } // End llvm namespace
  110. #endif // LLVM_TRANSFORMS_UTILS_MODULEUTILS_H
  111. #ifdef __GNUC__
  112. #pragma GCC diagnostic pop
  113. #endif