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