UpdateCompilerUsed.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //==-LTOInternalize.cpp - LLVM Link Time Optimizer Internalization Utility -==//
  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 file defines a helper to run the internalization part of LTO.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/LTO/legacy/UpdateCompilerUsed.h"
  13. #include "llvm/Analysis/TargetLibraryInfo.h"
  14. #include "llvm/CodeGen/TargetLowering.h"
  15. #include "llvm/CodeGen/TargetSubtargetInfo.h"
  16. #include "llvm/IR/LegacyPassManager.h"
  17. #include "llvm/IR/Mangler.h"
  18. #include "llvm/Transforms/Utils/ModuleUtils.h"
  19. #include "llvm/Target/TargetMachine.h"
  20. using namespace llvm;
  21. namespace {
  22. // Helper class that collects AsmUsed and user supplied libcalls.
  23. class PreserveLibCallsAndAsmUsed {
  24. public:
  25. PreserveLibCallsAndAsmUsed(const StringSet<> &AsmUndefinedRefs,
  26. const TargetMachine &TM,
  27. std::vector<GlobalValue *> &LLVMUsed)
  28. : AsmUndefinedRefs(AsmUndefinedRefs), TM(TM), LLVMUsed(LLVMUsed) {}
  29. void findInModule(Module &TheModule) {
  30. initializeLibCalls(TheModule);
  31. for (Function &F : TheModule)
  32. findLibCallsAndAsm(F);
  33. for (GlobalVariable &GV : TheModule.globals())
  34. findLibCallsAndAsm(GV);
  35. for (GlobalAlias &GA : TheModule.aliases())
  36. findLibCallsAndAsm(GA);
  37. }
  38. private:
  39. // Inputs
  40. const StringSet<> &AsmUndefinedRefs;
  41. const TargetMachine &TM;
  42. // Temps
  43. llvm::Mangler Mangler;
  44. StringSet<> Libcalls;
  45. // Output
  46. std::vector<GlobalValue *> &LLVMUsed;
  47. // Collect names of runtime library functions. User-defined functions with the
  48. // same names are added to llvm.compiler.used to prevent them from being
  49. // deleted by optimizations.
  50. void initializeLibCalls(const Module &TheModule) {
  51. TargetLibraryInfoImpl TLII(Triple(TM.getTargetTriple()));
  52. TargetLibraryInfo TLI(TLII);
  53. // TargetLibraryInfo has info on C runtime library calls on the current
  54. // target.
  55. for (unsigned I = 0, E = static_cast<unsigned>(LibFunc::NumLibFuncs);
  56. I != E; ++I) {
  57. LibFunc F = static_cast<LibFunc>(I);
  58. if (TLI.has(F))
  59. Libcalls.insert(TLI.getName(F));
  60. }
  61. SmallPtrSet<const TargetLowering *, 1> TLSet;
  62. for (const Function &F : TheModule) {
  63. const TargetLowering *Lowering =
  64. TM.getSubtargetImpl(F)->getTargetLowering();
  65. if (Lowering && TLSet.insert(Lowering).second)
  66. // TargetLowering has info on library calls that CodeGen expects to be
  67. // available, both from the C runtime and compiler-rt.
  68. for (unsigned I = 0, E = static_cast<unsigned>(RTLIB::UNKNOWN_LIBCALL);
  69. I != E; ++I)
  70. if (const char *Name =
  71. Lowering->getLibcallName(static_cast<RTLIB::Libcall>(I)))
  72. Libcalls.insert(Name);
  73. }
  74. }
  75. void findLibCallsAndAsm(GlobalValue &GV) {
  76. // There are no restrictions to apply to declarations.
  77. if (GV.isDeclaration())
  78. return;
  79. // There is nothing more restrictive than private linkage.
  80. if (GV.hasPrivateLinkage())
  81. return;
  82. // Conservatively append user-supplied runtime library functions (supplied
  83. // either directly, or via a function alias) to llvm.compiler.used. These
  84. // could be internalized and deleted by optimizations like -globalopt,
  85. // causing problems when later optimizations add new library calls (e.g.,
  86. // llvm.memset => memset and printf => puts).
  87. // Leave it to the linker to remove any dead code (e.g. with -dead_strip).
  88. GlobalValue *FuncAliasee = nullptr;
  89. if (isa<GlobalAlias>(GV)) {
  90. auto *A = cast<GlobalAlias>(&GV);
  91. FuncAliasee = dyn_cast<Function>(A->getAliasee());
  92. }
  93. if ((isa<Function>(GV) || FuncAliasee) && Libcalls.count(GV.getName())) {
  94. LLVMUsed.push_back(&GV);
  95. return;
  96. }
  97. SmallString<64> Buffer;
  98. TM.getNameWithPrefix(Buffer, &GV, Mangler);
  99. if (AsmUndefinedRefs.count(Buffer))
  100. LLVMUsed.push_back(&GV);
  101. }
  102. };
  103. } // namespace anonymous
  104. void llvm::updateCompilerUsed(Module &TheModule, const TargetMachine &TM,
  105. const StringSet<> &AsmUndefinedRefs) {
  106. std::vector<GlobalValue *> UsedValues;
  107. PreserveLibCallsAndAsmUsed(AsmUndefinedRefs, TM, UsedValues)
  108. .findInModule(TheModule);
  109. if (UsedValues.empty())
  110. return;
  111. appendToCompilerUsed(TheModule, UsedValues);
  112. }