blob: 421663f825653ec62b31cb5a4f71bb16d0599f79 [file] [log] [blame]
Hans Wennborge1ecd612017-11-14 21:09:45 +00001//===- 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"
12#include "llvm/CodeGen/Passes.h"
Hans Wennborgca46db92017-11-28 18:44:26 +000013#include "llvm/IR/DebugInfoMetadata.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +000014#include "llvm/IR/Function.h"
15#include "llvm/IR/Instructions.h"
16#include "llvm/IR/Module.h"
17#include "llvm/IR/Type.h"
18#include "llvm/Pass.h"
19#include "llvm/Transforms/Scalar.h"
20using namespace llvm;
21
22static void insertCall(Function &CurFn, StringRef Func,
Hans Wennborgca46db92017-11-28 18:44:26 +000023 Instruction *InsertionPt, DebugLoc DL) {
Hans Wennborge1ecd612017-11-14 21:09:45 +000024 Module &M = *InsertionPt->getParent()->getParent()->getParent();
25 LLVMContext &C = InsertionPt->getParent()->getContext();
26
27 if (Func == "mcount" ||
28 Func == ".mcount" ||
29 Func == "\01__gnu_mcount_nc" ||
30 Func == "\01_mcount" ||
31 Func == "\01mcount" ||
32 Func == "__mcount" ||
Hans Wennborg37cbf282017-11-21 17:22:19 +000033 Func == "_mcount" ||
34 Func == "__cyg_profile_func_enter_bare") {
Hans Wennborge1ecd612017-11-14 21:09:45 +000035 Constant *Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C));
Hans Wennborgca46db92017-11-28 18:44:26 +000036 CallInst *Call = CallInst::Create(Fn, "", InsertionPt);
37 Call->setDebugLoc(DL);
Hans Wennborge1ecd612017-11-14 21:09:45 +000038 return;
39 }
40
41 if (Func == "__cyg_profile_func_enter" || Func == "__cyg_profile_func_exit") {
42 Type *ArgTypes[] = {Type::getInt8PtrTy(C), Type::getInt8PtrTy(C)};
43
44 Constant *Fn = M.getOrInsertFunction(
45 Func, FunctionType::get(Type::getVoidTy(C), ArgTypes, false));
46
47 Instruction *RetAddr = CallInst::Create(
48 Intrinsic::getDeclaration(&M, Intrinsic::returnaddress),
49 ArrayRef<Value *>(ConstantInt::get(Type::getInt32Ty(C), 0)), "",
50 InsertionPt);
Hans Wennborgca46db92017-11-28 18:44:26 +000051 RetAddr->setDebugLoc(DL);
Hans Wennborge1ecd612017-11-14 21:09:45 +000052
53 Value *Args[] = {ConstantExpr::getBitCast(&CurFn, Type::getInt8PtrTy(C)),
54 RetAddr};
55
Hans Wennborgca46db92017-11-28 18:44:26 +000056 CallInst *Call =
57 CallInst::Create(Fn, ArrayRef<Value *>(Args), "", InsertionPt);
58 Call->setDebugLoc(DL);
Hans Wennborge1ecd612017-11-14 21:09:45 +000059 return;
60 }
61
62 // We only know how to call a fixed set of instrumentation functions, because
63 // they all expect different arguments, etc.
64 report_fatal_error(Twine("Unknown instrumentation function: '") + Func + "'");
65}
66
67static bool runOnFunction(Function &F, bool PostInlining) {
68 StringRef EntryAttr = PostInlining ? "instrument-function-entry-inlined"
69 : "instrument-function-entry";
70
71 StringRef ExitAttr = PostInlining ? "instrument-function-exit-inlined"
72 : "instrument-function-exit";
73
74 StringRef EntryFunc = F.getFnAttribute(EntryAttr).getValueAsString();
75 StringRef ExitFunc = F.getFnAttribute(ExitAttr).getValueAsString();
76
77 bool Changed = false;
78
79 // If the attribute is specified, insert instrumentation and then "consume"
80 // the attribute so that it's not inserted again if the pass should happen to
81 // run later for some reason.
82
83 if (!EntryFunc.empty()) {
Hans Wennborgca46db92017-11-28 18:44:26 +000084 DebugLoc DL;
85 if (auto SP = F.getSubprogram())
86 DL = DebugLoc::get(SP->getScopeLine(), 0, SP);
87
88 insertCall(F, EntryFunc, &*F.begin()->getFirstInsertionPt(), DL);
Hans Wennborge1ecd612017-11-14 21:09:45 +000089 Changed = true;
90 F.removeAttribute(AttributeList::FunctionIndex, EntryAttr);
91 }
92
93 if (!ExitFunc.empty()) {
94 for (BasicBlock &BB : F) {
95 TerminatorInst *T = BB.getTerminator();
Hans Wennborgca46db92017-11-28 18:44:26 +000096 DebugLoc DL;
97 if (DebugLoc TerminatorDL = T->getDebugLoc())
98 DL = TerminatorDL;
99 else if (auto SP = F.getSubprogram())
100 DL = DebugLoc::get(0, 0, SP);
101
Hans Wennborge1ecd612017-11-14 21:09:45 +0000102 if (isa<ReturnInst>(T)) {
Hans Wennborgca46db92017-11-28 18:44:26 +0000103 insertCall(F, ExitFunc, T, DL);
Hans Wennborge1ecd612017-11-14 21:09:45 +0000104 Changed = true;
105 }
106 }
107 F.removeAttribute(AttributeList::FunctionIndex, ExitAttr);
108 }
109
110 return Changed;
111}
112
113namespace {
114struct EntryExitInstrumenter : public FunctionPass {
115 static char ID;
116 EntryExitInstrumenter() : FunctionPass(ID) {
117 initializeEntryExitInstrumenterPass(*PassRegistry::getPassRegistry());
118 }
119 void getAnalysisUsage(AnalysisUsage &AU) const override {
120 AU.addPreserved<GlobalsAAWrapperPass>();
121 }
122 bool runOnFunction(Function &F) override { return ::runOnFunction(F, false); }
123};
124char EntryExitInstrumenter::ID = 0;
125
126struct PostInlineEntryExitInstrumenter : public FunctionPass {
127 static char ID;
128 PostInlineEntryExitInstrumenter() : FunctionPass(ID) {
129 initializePostInlineEntryExitInstrumenterPass(
130 *PassRegistry::getPassRegistry());
131 }
132 void getAnalysisUsage(AnalysisUsage &AU) const override {
133 AU.addPreserved<GlobalsAAWrapperPass>();
134 }
135 bool runOnFunction(Function &F) override { return ::runOnFunction(F, true); }
136};
137char PostInlineEntryExitInstrumenter::ID = 0;
138}
139
140INITIALIZE_PASS(
141 EntryExitInstrumenter, "ee-instrument",
142 "Instrument function entry/exit with calls to e.g. mcount() (pre inlining)",
Davide Italiano1380cb82017-11-14 23:13:38 +0000143 false, false)
Hans Wennborge1ecd612017-11-14 21:09:45 +0000144INITIALIZE_PASS(PostInlineEntryExitInstrumenter, "post-inline-ee-instrument",
145 "Instrument function entry/exit with calls to e.g. mcount() "
146 "(post inlining)",
Davide Italiano1380cb82017-11-14 23:13:38 +0000147 false, false)
Hans Wennborge1ecd612017-11-14 21:09:45 +0000148
149FunctionPass *llvm::createEntryExitInstrumenterPass() {
150 return new EntryExitInstrumenter();
151}
152
153FunctionPass *llvm::createPostInlineEntryExitInstrumenterPass() {
154 return new PostInlineEntryExitInstrumenter();
155}
156
157PreservedAnalyses
158llvm::EntryExitInstrumenterPass::run(Function &F, FunctionAnalysisManager &AM) {
159 runOnFunction(F, PostInlining);
160 PreservedAnalyses PA;
161 PA.preserveSet<CFGAnalyses>();
162 return PA;
163}