Instrumentation.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===//
  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 the common initialization infrastructure for the
  10. // Instrumentation library.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Instrumentation.h"
  14. #include "llvm-c/Initialization.h"
  15. #include "llvm/ADT/Triple.h"
  16. #include "llvm/IR/IntrinsicInst.h"
  17. #include "llvm/IR/Module.h"
  18. #include "llvm/InitializePasses.h"
  19. #include "llvm/PassRegistry.h"
  20. using namespace llvm;
  21. /// Moves I before IP. Returns new insert point.
  22. static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP) {
  23. // If I is IP, move the insert point down.
  24. if (I == IP) {
  25. ++IP;
  26. } else {
  27. // Otherwise, move I before IP and return IP.
  28. I->moveBefore(&*IP);
  29. }
  30. return IP;
  31. }
  32. /// Instrumentation passes often insert conditional checks into entry blocks.
  33. /// Call this function before splitting the entry block to move instructions
  34. /// that must remain in the entry block up before the split point. Static
  35. /// allocas and llvm.localescape calls, for example, must remain in the entry
  36. /// block.
  37. BasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB,
  38. BasicBlock::iterator IP) {
  39. assert(&BB.getParent()->getEntryBlock() == &BB);
  40. for (auto I = IP, E = BB.end(); I != E; ++I) {
  41. bool KeepInEntry = false;
  42. if (auto *AI = dyn_cast<AllocaInst>(I)) {
  43. if (AI->isStaticAlloca())
  44. KeepInEntry = true;
  45. } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
  46. if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
  47. KeepInEntry = true;
  48. }
  49. if (KeepInEntry)
  50. IP = moveBeforeInsertPoint(I, IP);
  51. }
  52. return IP;
  53. }
  54. // Create a constant for Str so that we can pass it to the run-time lib.
  55. GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str,
  56. bool AllowMerging,
  57. const char *NamePrefix) {
  58. Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str);
  59. // We use private linkage for module-local strings. If they can be merged
  60. // with another one, we set the unnamed_addr attribute.
  61. GlobalVariable *GV =
  62. new GlobalVariable(M, StrConst->getType(), true,
  63. GlobalValue::PrivateLinkage, StrConst, NamePrefix);
  64. if (AllowMerging)
  65. GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global);
  66. GV->setAlignment(Align(1)); // Strings may not be merged w/o setting
  67. // alignment explicitly.
  68. return GV;
  69. }
  70. Comdat *llvm::getOrCreateFunctionComdat(Function &F, Triple &T) {
  71. if (auto Comdat = F.getComdat()) return Comdat;
  72. assert(F.hasName());
  73. Module *M = F.getParent();
  74. // Make a new comdat for the function. Use the "no duplicates" selection kind
  75. // if the object file format supports it. For COFF we restrict it to non-weak
  76. // symbols.
  77. Comdat *C = M->getOrInsertComdat(F.getName());
  78. if (T.isOSBinFormatELF() || (T.isOSBinFormatCOFF() && !F.isWeakForLinker()))
  79. C->setSelectionKind(Comdat::NoDeduplicate);
  80. F.setComdat(C);
  81. return C;
  82. }
  83. /// initializeInstrumentation - Initialize all passes in the TransformUtils
  84. /// library.
  85. void llvm::initializeInstrumentation(PassRegistry &Registry) {
  86. initializeAddressSanitizerLegacyPassPass(Registry);
  87. initializeModuleAddressSanitizerLegacyPassPass(Registry);
  88. initializeMemProfilerLegacyPassPass(Registry);
  89. initializeModuleMemProfilerLegacyPassPass(Registry);
  90. initializeBoundsCheckingLegacyPassPass(Registry);
  91. initializeControlHeightReductionLegacyPassPass(Registry);
  92. initializeGCOVProfilerLegacyPassPass(Registry);
  93. initializePGOInstrumentationGenLegacyPassPass(Registry);
  94. initializePGOInstrumentationUseLegacyPassPass(Registry);
  95. initializePGOIndirectCallPromotionLegacyPassPass(Registry);
  96. initializePGOMemOPSizeOptLegacyPassPass(Registry);
  97. initializeCGProfileLegacyPassPass(Registry);
  98. initializeInstrOrderFileLegacyPassPass(Registry);
  99. initializeInstrProfilingLegacyPassPass(Registry);
  100. initializeMemorySanitizerLegacyPassPass(Registry);
  101. initializeHWAddressSanitizerLegacyPassPass(Registry);
  102. initializeThreadSanitizerLegacyPassPass(Registry);
  103. initializeModuleSanitizerCoverageLegacyPassPass(Registry);
  104. initializeDataFlowSanitizerLegacyPassPass(Registry);
  105. }
  106. /// LLVMInitializeInstrumentation - C binding for
  107. /// initializeInstrumentation.
  108. void LLVMInitializeInstrumentation(LLVMPassRegistryRef R) {
  109. initializeInstrumentation(*unwrap(R));
  110. }