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