1 //===-- Instrumentation.cpp - TransformUtils Infrastructure ---------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the common initialization infrastructure for the 11 // Instrumentation library. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/Transforms/Instrumentation.h" 16 #include "llvm-c/Initialization.h" 17 #include "llvm/IR/IntrinsicInst.h" 18 #include "llvm/IR/Module.h" 19 #include "llvm/InitializePasses.h" 20 #include "llvm/PassRegistry.h" 21 22 using namespace llvm; 23 24 /// Moves I before IP. Returns new insert point. 25 static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP) { 26 // If I is IP, move the insert point down. 27 if (I == IP) 28 return ++IP; 29 // Otherwise, move I before IP and return IP. 30 I->moveBefore(&*IP); 31 return IP; 32 } 33 34 /// Instrumentation passes often insert conditional checks into entry blocks. 35 /// Call this function before splitting the entry block to move instructions 36 /// that must remain in the entry block up before the split point. Static 37 /// allocas and llvm.localescape calls, for example, must remain in the entry 38 /// block. 39 BasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB, 40 BasicBlock::iterator IP) { 41 assert(&BB.getParent()->getEntryBlock() == &BB); 42 for (auto I = IP, E = BB.end(); I != E; ++I) { 43 bool KeepInEntry = false; 44 if (auto *AI = dyn_cast<AllocaInst>(I)) { 45 if (AI->isStaticAlloca()) 46 KeepInEntry = true; 47 } else if (auto *II = dyn_cast<IntrinsicInst>(I)) { 48 if (II->getIntrinsicID() == llvm::Intrinsic::localescape) 49 KeepInEntry = true; 50 } 51 if (KeepInEntry) 52 IP = moveBeforeInsertPoint(I, IP); 53 } 54 return IP; 55 } 56 57 // Create a constant for Str so that we can pass it to the run-time lib. 58 GlobalVariable *llvm::createPrivateGlobalForString(Module &M, StringRef Str, 59 bool AllowMerging, 60 const char *NamePrefix) { 61 Constant *StrConst = ConstantDataArray::getString(M.getContext(), Str); 62 // We use private linkage for module-local strings. If they can be merged 63 // with another one, we set the unnamed_addr attribute. 64 GlobalVariable *GV = 65 new GlobalVariable(M, StrConst->getType(), true, 66 GlobalValue::PrivateLinkage, StrConst, NamePrefix); 67 if (AllowMerging) 68 GV->setUnnamedAddr(GlobalValue::UnnamedAddr::Global); 69 GV->setAlignment(1); // Strings may not be merged w/o setting align 1. 70 return GV; 71 } 72 73 /// initializeInstrumentation - Initialize all passes in the TransformUtils 74 /// library. 75 void llvm::initializeInstrumentation(PassRegistry &Registry) { 76 initializeAddressSanitizerLegacyPassPass(Registry); 77 initializeAddressSanitizerModuleLegacyPassPass(Registry); 78 initializeBoundsCheckingLegacyPassPass(Registry); 79 initializeControlHeightReductionLegacyPassPass(Registry); 80 initializeGCOVProfilerLegacyPassPass(Registry); 81 initializePGOInstrumentationGenLegacyPassPass(Registry); 82 initializePGOInstrumentationUseLegacyPassPass(Registry); 83 initializePGOIndirectCallPromotionLegacyPassPass(Registry); 84 initializePGOMemOPSizeOptLegacyPassPass(Registry); 85 initializeInstrProfilingLegacyPassPass(Registry); 86 initializeMemorySanitizerPass(Registry); 87 initializeHWAddressSanitizerPass(Registry); 88 initializeThreadSanitizerPass(Registry); 89 initializeSanitizerCoverageModulePass(Registry); 90 initializeDataFlowSanitizerPass(Registry); 91 initializeEfficiencySanitizerPass(Registry); 92 } 93 94 /// LLVMInitializeInstrumentation - C binding for 95 /// initializeInstrumentation. 96 void LLVMInitializeInstrumentation(LLVMPassRegistryRef R) { 97 initializeInstrumentation(*unwrap(R)); 98 } 99