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/InitializePasses.h"
19 #include "llvm/PassRegistry.h"
20 
21 using namespace llvm;
22 
23 /// Moves I before IP. Returns new insert point.
moveBeforeInsertPoint(BasicBlock::iterator I,BasicBlock::iterator IP)24 static BasicBlock::iterator moveBeforeInsertPoint(BasicBlock::iterator I, BasicBlock::iterator IP) {
25   // If I is IP, move the insert point down.
26   if (I == IP)
27     return ++IP;
28   // Otherwise, move I before IP and return IP.
29   I->moveBefore(&*IP);
30   return IP;
31 }
32 
33 /// Instrumentation passes often insert conditional checks into entry blocks.
34 /// Call this function before splitting the entry block to move instructions
35 /// that must remain in the entry block up before the split point. Static
36 /// allocas and llvm.localescape calls, for example, must remain in the entry
37 /// block.
PrepareToSplitEntryBlock(BasicBlock & BB,BasicBlock::iterator IP)38 BasicBlock::iterator llvm::PrepareToSplitEntryBlock(BasicBlock &BB,
39                                                     BasicBlock::iterator IP) {
40   assert(&BB.getParent()->getEntryBlock() == &BB);
41   for (auto I = IP, E = BB.end(); I != E; ++I) {
42     bool KeepInEntry = false;
43     if (auto *AI = dyn_cast<AllocaInst>(I)) {
44       if (AI->isStaticAlloca())
45         KeepInEntry = true;
46     } else if (auto *II = dyn_cast<IntrinsicInst>(I)) {
47       if (II->getIntrinsicID() == llvm::Intrinsic::localescape)
48         KeepInEntry = true;
49     }
50     if (KeepInEntry)
51       IP = moveBeforeInsertPoint(I, IP);
52   }
53   return IP;
54 }
55 
56 /// initializeInstrumentation - Initialize all passes in the TransformUtils
57 /// library.
initializeInstrumentation(PassRegistry & Registry)58 void llvm::initializeInstrumentation(PassRegistry &Registry) {
59   initializeAddressSanitizerPass(Registry);
60   initializeAddressSanitizerModulePass(Registry);
61   initializeBoundsCheckingPass(Registry);
62   initializeGCOVProfilerPass(Registry);
63   initializePGOInstrumentationGenPass(Registry);
64   initializePGOInstrumentationUsePass(Registry);
65   initializeInstrProfilingPass(Registry);
66   initializeMemorySanitizerPass(Registry);
67   initializeThreadSanitizerPass(Registry);
68   initializeSanitizerCoverageModulePass(Registry);
69   initializeDataFlowSanitizerPass(Registry);
70   initializeSafeStackPass(Registry);
71 }
72 
73 /// LLVMInitializeInstrumentation - C binding for
74 /// initializeInstrumentation.
LLVMInitializeInstrumentation(LLVMPassRegistryRef R)75 void LLVMInitializeInstrumentation(LLVMPassRegistryRef R) {
76   initializeInstrumentation(*unwrap(R));
77 }
78