//===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // This file defines the common initialization infrastructure for the // Instrumentation library. // //===----------------------------------------------------------------------===// #include "llvm/Transforms/Instrumentation.h" #include "llvm-c/Initialization.h" #include "llvm/IR/IntrinsicInst.h" #include "llvm/IR/Module.h" #include "llvm/InitializePasses.h" #include "llvm/PassRegistry.h" using namespace llvm; /// Moves I before IP. Returns new insert point. static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP) { // If I is IP, move the insert point down. if (I == IP) return ++IP; // Otherwise, move I before IP and return IP. I->moveBefore(&*IP); return IP; } /// Instrumentation passes often insert conditional checks into entry blocks. /// Call this function before splitting the entry block to move instructions /// that must remain in the entry block up before the split point. Static /// allocas and llvm.localescape calls, for example, must remain in the entry /// block. BasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB, BasicBlock::iterator IP) { assert(&BB.getParent()->getEntryBlock() == &BB); for (auto I = IP, E = BB.end(); I != E; ++I) { bool KeepInEntry = false; if (auto *AI = dyn_cast(I)) { if (AI->isStaticAlloca()) KeepInEntry = true; } else if (auto *II = dyn_cast(I)) { if (II->getIntrinsicID() == llvm::Intrinsic::localescape) KeepInEntry = true; } if (KeepInEntry) IP = moveBeforeInsertPoint(I, IP); } return IP; } // Create a constant for Str so that we can pass it to the run-time lib. GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str, bool AllowMerging, const char *NamePrefix) { Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str); // We use private linkage for module-local strings. If they can be merged // with another one, we set the unnamed_addr attribute. GlobalVariable *GV = new GlobalVariable(M, StrConst->getType(), true, GlobalValue::PrivateLinkage, StrConst, NamePrefix); if (AllowMerging) GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); GV->setAlignment(1); // Strings may not be merged w/o setting align 1. return GV; } Comdat *llvm::GetOrCreateFunctionComdat(Function &F, const std::string &ModuleId) { if (auto Comdat = F.getComdat()) return Comdat; assert(F.hasName()); Module *M = F.getParent(); std::string Name = F.getName(); if (F.hasLocalLinkage()) { if (ModuleId.empty()) return nullptr; Name += ModuleId; } F.setComdat(M->getOrInsertComdat(Name)); return F.getComdat(); } /// initializeInstrumentation - Initialize all passes in the TransformUtils /// library. void llvm::initializeInstrumentation(PassRegistry &Registry) { initializeAddressSanitizerLegacyPassPass(Registry); initializeAddressSanitizerModuleLegacyPassPass(Registry); initializeBoundsCheckingLegacyPassPass(Registry); initializeControlHeightReductionLegacyPassPass(Registry); initializeGCOVProfilerLegacyPassPass(Registry); initializePGOInstrumentationGenLegacyPassPass(Registry); initializePGOInstrumentationUseLegacyPassPass(Registry); initializePGOIndirectCallPromotionLegacyPassPass(Registry); initializePGOMemOPSizeOptLegacyPassPass(Registry); initializeInstrProfilingLegacyPassPass(Registry); initializeMemorySanitizerPass(Registry); initializeHWAddressSanitizerPass(Registry); initializeThreadSanitizerPass(Registry); initializeSanitizerCoverageModulePass(Registry); initializeDataFlowSanitizerPass(Registry); initializeEfficiencySanitizerPass(Registry); } /// LLVMInitializeInstrumentation - C binding for /// initializeInstrumentation. void LLVMInitializeInstrumentation(LLVMPassRegistryRef R) { initializeInstrumentation(*unwrap(R)); }