blob: 57e2ff0251a914aceaee28ee157a1cf93b49db07 [file] [log] [blame]
Hans Wennborge1ecd612017-11-14 21:09:45 +00001//===- EntryExitInstrumenter.cpp - Function Entry/Exit Instrumentation ----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Wennborge1ecd612017-11-14 21:09:45 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
10#include "llvm/Analysis/GlobalsModRef.h"
Hans Wennborgca46db92017-11-28 18:44:26 +000011#include "llvm/IR/DebugInfoMetadata.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +000012#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 Blaikiea373d182018-03-28 17:44:36 +000017#include "llvm/Transforms/Utils.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +000018using namespace llvm;
19
20static void insertCall(Function &CurFn, StringRef Func,
Hans Wennborgca46db92017-11-28 18:44:26 +000021 Instruction *InsertionPt, DebugLoc DL) {
Hans Wennborge1ecd612017-11-14 21:09:45 +000022 Module &M = *InsertionPt->getParent()->getParent()->getParent();
23 LLVMContext &C = InsertionPt->getParent()->getContext();
24
25 if (Func == "mcount" ||
26 Func == ".mcount" ||
Jian Cai16fa8b02019-08-16 23:30:16 +000027 Func == "llvm.arm.gnu.eabi.mcount" ||
Hans Wennborge1ecd612017-11-14 21:09:45 +000028 Func == "\01_mcount" ||
29 Func == "\01mcount" ||
30 Func == "__mcount" ||
Hans Wennborg37cbf282017-11-21 17:22:19 +000031 Func == "_mcount" ||
32 Func == "__cyg_profile_func_enter_bare") {
James Y Knight13680222019-02-01 02:28:03 +000033 FunctionCallee Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C));
Hans Wennborgca46db92017-11-28 18:44:26 +000034 CallInst *Call = CallInst::Create(Fn, "", InsertionPt);
35 Call->setDebugLoc(DL);
Hans Wennborge1ecd612017-11-14 21:09:45 +000036 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 Knight13680222019-02-01 02:28:03 +000042 FunctionCallee Fn = M.getOrInsertFunction(
Hans Wennborge1ecd612017-11-14 21:09:45 +000043 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 Wennborgca46db92017-11-28 18:44:26 +000049 RetAddr->setDebugLoc(DL);
Hans Wennborge1ecd612017-11-14 21:09:45 +000050
51 Value *Args[] = {ConstantExpr::getBitCast(&CurFn, Type::getInt8PtrTy(C)),
52 RetAddr};
53
Hans Wennborgca46db92017-11-28 18:44:26 +000054 CallInst *Call =
55 CallInst::Create(Fn, ArrayRef<Value *>(Args), "", InsertionPt);
56 Call->setDebugLoc(DL);
Hans Wennborge1ecd612017-11-14 21:09:45 +000057 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
65static 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 Wennborgca46db92017-11-28 18:44:26 +000082 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 Wennborge1ecd612017-11-14 21:09:45 +000087 Changed = true;
88 F.removeAttribute(AttributeList::FunctionIndex, EntryAttr);
89 }
90
91 if (!ExitFunc.empty()) {
92 for (BasicBlock &BB : F) {
Hans Wennborgb230c762018-04-06 10:14:09 +000093 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 Wennborgca46db92017-11-28 18:44:26 +0000106 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 Wennborgb230c762018-04-06 10:14:09 +0000112 insertCall(F, ExitFunc, T, DL);
113 Changed = true;
Hans Wennborge1ecd612017-11-14 21:09:45 +0000114 }
115 F.removeAttribute(AttributeList::FunctionIndex, ExitAttr);
116 }
117
118 return Changed;
119}
120
121namespace {
122struct 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};
132char EntryExitInstrumenter::ID = 0;
133
134struct 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};
145char PostInlineEntryExitInstrumenter::ID = 0;
146}
147
148INITIALIZE_PASS(
149 EntryExitInstrumenter, "ee-instrument",
150 "Instrument function entry/exit with calls to e.g. mcount() (pre inlining)",
Davide Italiano1380cb82017-11-14 23:13:38 +0000151 false, false)
Hans Wennborge1ecd612017-11-14 21:09:45 +0000152INITIALIZE_PASS(PostInlineEntryExitInstrumenter, "post-inline-ee-instrument",
153 "Instrument function entry/exit with calls to e.g. mcount() "
154 "(post inlining)",
Davide Italiano1380cb82017-11-14 23:13:38 +0000155 false, false)
Hans Wennborge1ecd612017-11-14 21:09:45 +0000156
157FunctionPass *llvm::createEntryExitInstrumenterPass() {
158 return new EntryExitInstrumenter();
159}
160
161FunctionPass *llvm::createPostInlineEntryExitInstrumenterPass() {
162 return new PostInlineEntryExitInstrumenter();
163}
164
165PreservedAnalyses
166llvm::EntryExitInstrumenterPass::run(Function &F, FunctionAnalysisManager &AM) {
167 runOnFunction(F, PostInlining);
168 PreservedAnalyses PA;
169 PA.preserveSet<CFGAnalyses>();
170 return PA;
171}