Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 1 | //===- EntryExitInstrumenter.cpp - Function Entry/Exit Instrumentation ----===// |
| 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 | #include "llvm/Transforms/Utils/EntryExitInstrumenter.h" |
| 11 | #include "llvm/Analysis/GlobalsModRef.h" |
Hans Wennborg | ca46db9 | 2017-11-28 18:44:26 +0000 | [diff] [blame] | 12 | #include "llvm/IR/DebugInfoMetadata.h" |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 13 | #include "llvm/IR/Function.h" |
| 14 | #include "llvm/IR/Instructions.h" |
| 15 | #include "llvm/IR/Module.h" |
| 16 | #include "llvm/IR/Type.h" |
| 17 | #include "llvm/Pass.h" |
David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 18 | #include "llvm/Transforms/Utils.h" |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
| 21 | static void insertCall(Function &CurFn, StringRef Func, |
Hans Wennborg | ca46db9 | 2017-11-28 18:44:26 +0000 | [diff] [blame] | 22 | Instruction *InsertionPt, DebugLoc DL) { |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 23 | Module &M = *InsertionPt->getParent()->getParent()->getParent(); |
| 24 | LLVMContext &C = InsertionPt->getParent()->getContext(); |
| 25 | |
| 26 | if (Func == "mcount" || |
| 27 | Func == ".mcount" || |
| 28 | Func == "\01__gnu_mcount_nc" || |
| 29 | Func == "\01_mcount" || |
| 30 | Func == "\01mcount" || |
| 31 | Func == "__mcount" || |
Hans Wennborg | 37cbf28 | 2017-11-21 17:22:19 +0000 | [diff] [blame] | 32 | Func == "_mcount" || |
| 33 | Func == "__cyg_profile_func_enter_bare") { |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 34 | Constant *Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C)); |
Hans Wennborg | ca46db9 | 2017-11-28 18:44:26 +0000 | [diff] [blame] | 35 | CallInst *Call = CallInst::Create(Fn, "", InsertionPt); |
| 36 | Call->setDebugLoc(DL); |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 37 | return; |
| 38 | } |
| 39 | |
| 40 | if (Func == "__cyg_profile_func_enter" || Func == "__cyg_profile_func_exit") { |
| 41 | Type *ArgTypes[] = {Type::getInt8PtrTy(C), Type::getInt8PtrTy(C)}; |
| 42 | |
| 43 | Constant *Fn = M.getOrInsertFunction( |
| 44 | Func, FunctionType::get(Type::getVoidTy(C), ArgTypes, false)); |
| 45 | |
| 46 | Instruction *RetAddr = CallInst::Create( |
| 47 | Intrinsic::getDeclaration(&M, Intrinsic::returnaddress), |
| 48 | ArrayRef<Value *>(ConstantInt::get(Type::getInt32Ty(C), 0)), "", |
| 49 | InsertionPt); |
Hans Wennborg | ca46db9 | 2017-11-28 18:44:26 +0000 | [diff] [blame] | 50 | RetAddr->setDebugLoc(DL); |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 51 | |
| 52 | Value *Args[] = {ConstantExpr::getBitCast(&CurFn, Type::getInt8PtrTy(C)), |
| 53 | RetAddr}; |
| 54 | |
Hans Wennborg | ca46db9 | 2017-11-28 18:44:26 +0000 | [diff] [blame] | 55 | CallInst *Call = |
| 56 | CallInst::Create(Fn, ArrayRef<Value *>(Args), "", InsertionPt); |
| 57 | Call->setDebugLoc(DL); |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | |
| 61 | // We only know how to call a fixed set of instrumentation functions, because |
| 62 | // they all expect different arguments, etc. |
| 63 | report_fatal_error(Twine("Unknown instrumentation function: '") + Func + "'"); |
| 64 | } |
| 65 | |
| 66 | static bool runOnFunction(Function &F, bool PostInlining) { |
| 67 | StringRef EntryAttr = PostInlining ? "instrument-function-entry-inlined" |
| 68 | : "instrument-function-entry"; |
| 69 | |
| 70 | StringRef ExitAttr = PostInlining ? "instrument-function-exit-inlined" |
| 71 | : "instrument-function-exit"; |
| 72 | |
| 73 | StringRef EntryFunc = F.getFnAttribute(EntryAttr).getValueAsString(); |
| 74 | StringRef ExitFunc = F.getFnAttribute(ExitAttr).getValueAsString(); |
| 75 | |
| 76 | bool Changed = false; |
| 77 | |
| 78 | // If the attribute is specified, insert instrumentation and then "consume" |
| 79 | // the attribute so that it's not inserted again if the pass should happen to |
| 80 | // run later for some reason. |
| 81 | |
| 82 | if (!EntryFunc.empty()) { |
Hans Wennborg | ca46db9 | 2017-11-28 18:44:26 +0000 | [diff] [blame] | 83 | DebugLoc DL; |
| 84 | if (auto SP = F.getSubprogram()) |
| 85 | DL = DebugLoc::get(SP->getScopeLine(), 0, SP); |
| 86 | |
| 87 | insertCall(F, EntryFunc, &*F.begin()->getFirstInsertionPt(), DL); |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 88 | Changed = true; |
| 89 | F.removeAttribute(AttributeList::FunctionIndex, EntryAttr); |
| 90 | } |
| 91 | |
| 92 | if (!ExitFunc.empty()) { |
| 93 | for (BasicBlock &BB : F) { |
Hans Wennborg | b230c76 | 2018-04-06 10:14:09 +0000 | [diff] [blame] | 94 | Instruction *T = BB.getTerminator(); |
| 95 | if (!isa<ReturnInst>(T)) |
| 96 | continue; |
| 97 | |
| 98 | // If T is preceded by a musttail call, that's the real terminator. |
| 99 | Instruction *Prev = T->getPrevNode(); |
| 100 | if (BitCastInst *BCI = dyn_cast_or_null<BitCastInst>(Prev)) |
| 101 | Prev = BCI->getPrevNode(); |
| 102 | if (CallInst *CI = dyn_cast_or_null<CallInst>(Prev)) { |
| 103 | if (CI->isMustTailCall()) |
| 104 | T = CI; |
| 105 | } |
| 106 | |
Hans Wennborg | ca46db9 | 2017-11-28 18:44:26 +0000 | [diff] [blame] | 107 | DebugLoc DL; |
| 108 | if (DebugLoc TerminatorDL = T->getDebugLoc()) |
| 109 | DL = TerminatorDL; |
| 110 | else if (auto SP = F.getSubprogram()) |
| 111 | DL = DebugLoc::get(0, 0, SP); |
| 112 | |
Hans Wennborg | b230c76 | 2018-04-06 10:14:09 +0000 | [diff] [blame] | 113 | insertCall(F, ExitFunc, T, DL); |
| 114 | Changed = true; |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 115 | } |
| 116 | F.removeAttribute(AttributeList::FunctionIndex, ExitAttr); |
| 117 | } |
| 118 | |
| 119 | return Changed; |
| 120 | } |
| 121 | |
| 122 | namespace { |
| 123 | struct EntryExitInstrumenter : public FunctionPass { |
| 124 | static char ID; |
| 125 | EntryExitInstrumenter() : FunctionPass(ID) { |
| 126 | initializeEntryExitInstrumenterPass(*PassRegistry::getPassRegistry()); |
| 127 | } |
| 128 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 129 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 130 | } |
| 131 | bool runOnFunction(Function &F) override { return ::runOnFunction(F, false); } |
| 132 | }; |
| 133 | char EntryExitInstrumenter::ID = 0; |
| 134 | |
| 135 | struct PostInlineEntryExitInstrumenter : public FunctionPass { |
| 136 | static char ID; |
| 137 | PostInlineEntryExitInstrumenter() : FunctionPass(ID) { |
| 138 | initializePostInlineEntryExitInstrumenterPass( |
| 139 | *PassRegistry::getPassRegistry()); |
| 140 | } |
| 141 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 142 | AU.addPreserved<GlobalsAAWrapperPass>(); |
| 143 | } |
| 144 | bool runOnFunction(Function &F) override { return ::runOnFunction(F, true); } |
| 145 | }; |
| 146 | char PostInlineEntryExitInstrumenter::ID = 0; |
| 147 | } |
| 148 | |
| 149 | INITIALIZE_PASS( |
| 150 | EntryExitInstrumenter, "ee-instrument", |
| 151 | "Instrument function entry/exit with calls to e.g. mcount() (pre inlining)", |
Davide Italiano | 1380cb8 | 2017-11-14 23:13:38 +0000 | [diff] [blame] | 152 | false, false) |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 153 | INITIALIZE_PASS(PostInlineEntryExitInstrumenter, "post-inline-ee-instrument", |
| 154 | "Instrument function entry/exit with calls to e.g. mcount() " |
| 155 | "(post inlining)", |
Davide Italiano | 1380cb8 | 2017-11-14 23:13:38 +0000 | [diff] [blame] | 156 | false, false) |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 157 | |
| 158 | FunctionPass *llvm::createEntryExitInstrumenterPass() { |
| 159 | return new EntryExitInstrumenter(); |
| 160 | } |
| 161 | |
| 162 | FunctionPass *llvm::createPostInlineEntryExitInstrumenterPass() { |
| 163 | return new PostInlineEntryExitInstrumenter(); |
| 164 | } |
| 165 | |
| 166 | PreservedAnalyses |
| 167 | llvm::EntryExitInstrumenterPass::run(Function &F, FunctionAnalysisManager &AM) { |
| 168 | runOnFunction(F, PostInlining); |
| 169 | PreservedAnalyses PA; |
| 170 | PA.preserveSet<CFGAnalyses>(); |
| 171 | return PA; |
| 172 | } |