blob: d7c11de21a872e6e48b77ffc88209ea3893dfaf7 [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"
Hans Wennborgca46db92017-11-28 18:44:26 +000012#include "llvm/IR/DebugInfoMetadata.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +000013#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 Blaikiea373d182018-03-28 17:44:36 +000018#include "llvm/Transforms/Utils.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +000019using namespace llvm;
20
21static void insertCall(Function &CurFn, StringRef Func,
Hans Wennborgca46db92017-11-28 18:44:26 +000022 Instruction *InsertionPt, DebugLoc DL) {
Hans Wennborge1ecd612017-11-14 21:09:45 +000023 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 Wennborg37cbf282017-11-21 17:22:19 +000032 Func == "_mcount" ||
33 Func == "__cyg_profile_func_enter_bare") {
Hans Wennborge1ecd612017-11-14 21:09:45 +000034 Constant *Fn = M.getOrInsertFunction(Func, Type::getVoidTy(C));
Hans Wennborgca46db92017-11-28 18:44:26 +000035 CallInst *Call = CallInst::Create(Fn, "", InsertionPt);
36 Call->setDebugLoc(DL);
Hans Wennborge1ecd612017-11-14 21:09:45 +000037 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 Wennborgca46db92017-11-28 18:44:26 +000050 RetAddr->setDebugLoc(DL);
Hans Wennborge1ecd612017-11-14 21:09:45 +000051
52 Value *Args[] = {ConstantExpr::getBitCast(&CurFn, Type::getInt8PtrTy(C)),
53 RetAddr};
54
Hans Wennborgca46db92017-11-28 18:44:26 +000055 CallInst *Call =
56 CallInst::Create(Fn, ArrayRef<Value *>(Args), "", InsertionPt);
57 Call->setDebugLoc(DL);
Hans Wennborge1ecd612017-11-14 21:09:45 +000058 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
66static 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 Wennborgca46db92017-11-28 18:44:26 +000083 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 Wennborge1ecd612017-11-14 21:09:45 +000088 Changed = true;
89 F.removeAttribute(AttributeList::FunctionIndex, EntryAttr);
90 }
91
92 if (!ExitFunc.empty()) {
93 for (BasicBlock &BB : F) {
94 TerminatorInst *T = BB.getTerminator();
Hans Wennborgca46db92017-11-28 18:44:26 +000095 DebugLoc DL;
96 if (DebugLoc TerminatorDL = T->getDebugLoc())
97 DL = TerminatorDL;
98 else if (auto SP = F.getSubprogram())
99 DL = DebugLoc::get(0, 0, SP);
100
Hans Wennborge1ecd612017-11-14 21:09:45 +0000101 if (isa<ReturnInst>(T)) {
Hans Wennborgca46db92017-11-28 18:44:26 +0000102 insertCall(F, ExitFunc, T, DL);
Hans Wennborge1ecd612017-11-14 21:09:45 +0000103 Changed = true;
104 }
105 }
106 F.removeAttribute(AttributeList::FunctionIndex, ExitAttr);
107 }
108
109 return Changed;
110}
111
112namespace {
113struct EntryExitInstrumenter : public FunctionPass {
114 static char ID;
115 EntryExitInstrumenter() : FunctionPass(ID) {
116 initializeEntryExitInstrumenterPass(*PassRegistry::getPassRegistry());
117 }
118 void getAnalysisUsage(AnalysisUsage &AU) const override {
119 AU.addPreserved<GlobalsAAWrapperPass>();
120 }
121 bool runOnFunction(Function &F) override { return ::runOnFunction(F, false); }
122};
123char EntryExitInstrumenter::ID = 0;
124
125struct PostInlineEntryExitInstrumenter : public FunctionPass {
126 static char ID;
127 PostInlineEntryExitInstrumenter() : FunctionPass(ID) {
128 initializePostInlineEntryExitInstrumenterPass(
129 *PassRegistry::getPassRegistry());
130 }
131 void getAnalysisUsage(AnalysisUsage &AU) const override {
132 AU.addPreserved<GlobalsAAWrapperPass>();
133 }
134 bool runOnFunction(Function &F) override { return ::runOnFunction(F, true); }
135};
136char PostInlineEntryExitInstrumenter::ID = 0;
137}
138
139INITIALIZE_PASS(
140 EntryExitInstrumenter, "ee-instrument",
141 "Instrument function entry/exit with calls to e.g. mcount() (pre inlining)",
Davide Italiano1380cb82017-11-14 23:13:38 +0000142 false, false)
Hans Wennborge1ecd612017-11-14 21:09:45 +0000143INITIALIZE_PASS(PostInlineEntryExitInstrumenter, "post-inline-ee-instrument",
144 "Instrument function entry/exit with calls to e.g. mcount() "
145 "(post inlining)",
Davide Italiano1380cb82017-11-14 23:13:38 +0000146 false, false)
Hans Wennborge1ecd612017-11-14 21:09:45 +0000147
148FunctionPass *llvm::createEntryExitInstrumenterPass() {
149 return new EntryExitInstrumenter();
150}
151
152FunctionPass *llvm::createPostInlineEntryExitInstrumenterPass() {
153 return new PostInlineEntryExitInstrumenter();
154}
155
156PreservedAnalyses
157llvm::EntryExitInstrumenterPass::run(Function &F, FunctionAnalysisManager &AM) {
158 runOnFunction(F, PostInlining);
159 PreservedAnalyses PA;
160 PA.preserveSet<CFGAnalyses>();
161 return PA;
162}