1//===- EntryExitInstrumenter.cpp - Function Entry/Exit Instrumentation ----===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
10#include "llvm/Analysis/GlobalsModRef.h"
11#include "llvm/IR/DebugInfoMetadata.h"
12#include "llvm/IR/Dominators.h"
13#include "llvm/IR/Function.h"
14#include "llvm/IR/Instructions.h"
15#include "llvm/IR/Intrinsics.h"
16#include "llvm/IR/Module.h"
17#include "llvm/IR/Type.h"
18#include "llvm/InitializePasses.h"
19#include "llvm/TargetParser/Triple.h"
20#include "llvm/Pass.h"
21#include "llvm/Transforms/Utils.h"
22
23using namespace llvm;
24
25static void insertCall(Function &CurFn, StringRef Func,
26 BasicBlock::iterator InsertionPt, DebugLoc DL) {
27 Module &M = *InsertionPt->getParent()->getParent()->getParent();
28 LLVMContext &C = InsertionPt->getParent()->getContext();
29
30 if (Func == "mcount" ||
31 Func == ".mcount" ||
32 Func == "llvm.arm.gnu.eabi.mcount" ||
33 Func == "\01_mcount" ||
34 Func == "\01mcount" ||
35 Func == "__mcount" ||
36 Func == "_mcount" ||
37 Func == "__cyg_profile_func_enter_bare") {
38 Triple TargetTriple(M.getTargetTriple());
39 if (TargetTriple.isOSAIX() && Func == "__mcount") {
40 Type *SizeTy = M.getDataLayout().getIntPtrType(C);
41 Type *SizePtrTy = PointerType::getUnqual(C);
42 GlobalVariable *GV = new GlobalVariable(M, SizeTy, /*isConstant=*/false,
43 GlobalValue::InternalLinkage,
44 ConstantInt::get(Ty: SizeTy, V: 0));
45 CallInst *Call = CallInst::Create(
46 Func: M.getOrInsertFunction(Name: Func,
47 T: FunctionType::get(Result: Type::getVoidTy(C), Params: {SizePtrTy},
48 /*isVarArg=*/false)),
49 Args: {GV}, NameStr: "", InsertBefore: InsertionPt);
50 Call->setDebugLoc(DL);
51 } else if (TargetTriple.isRISCV() || TargetTriple.isAArch64() ||
52 TargetTriple.isLoongArch()) {
53 // On RISC-V, AArch64, and LoongArch, the `_mcount` function takes
54 // `__builtin_return_address(0)` as an argument since
55 // `__builtin_return_address(1)` is not available on these platforms.
56 auto ProgASPtr =
57 PointerType::get(C, AddressSpace: M.getDataLayout().getProgramAddressSpace());
58 Instruction *RetAddr = CallInst::Create(
59 Func: Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::returnaddress,
60 OverloadTys: {ProgASPtr}),
61 Args: ConstantInt::get(Ty: Type::getInt32Ty(C), V: 0), NameStr: "", InsertBefore: InsertionPt);
62 RetAddr->setDebugLoc(DL);
63
64 FunctionCallee Fn = M.getOrInsertFunction(
65 Name: Func, T: FunctionType::get(Result: Type::getVoidTy(C), Params: PointerType::getUnqual(C),
66 isVarArg: false));
67 CallInst *Call = CallInst::Create(Func: Fn, Args: RetAddr, NameStr: "", InsertBefore: InsertionPt);
68 Call->setDebugLoc(DL);
69 } else if (TargetTriple.isSystemZ()) {
70 // skip insertion for `mcount` on SystemZ. This will be handled later in
71 // `emitPrologue`. Add custom attribute to denote this.
72 CurFn.addFnAttr(
73 Attr: llvm::Attribute::get(Context&: C, Kind: "systemz-instrument-function-entry", Val: Func));
74 } else {
75 FunctionCallee Fn = M.getOrInsertFunction(Name: Func, RetTy: Type::getVoidTy(C));
76 CallInst *Call = CallInst::Create(Func: Fn, NameStr: "", InsertBefore: InsertionPt);
77 Call->setDebugLoc(DL);
78 }
79 return;
80 }
81
82 if (Func == "__cyg_profile_func_enter" || Func == "__cyg_profile_func_exit") {
83 auto ProgASPtr =
84 PointerType::get(C, AddressSpace: M.getDataLayout().getProgramAddressSpace());
85 Type *ArgTypes[] = {ProgASPtr, ProgASPtr};
86
87 FunctionCallee Fn = M.getOrInsertFunction(
88 Name: Func, T: FunctionType::get(Result: Type::getVoidTy(C), Params: ArgTypes, isVarArg: false));
89
90 Instruction *RetAddr = CallInst::Create(
91 Func: Intrinsic::getOrInsertDeclaration(M: &M, id: Intrinsic::returnaddress,
92 OverloadTys: {ProgASPtr}),
93 Args: ArrayRef<Value *>(ConstantInt::get(Ty: Type::getInt32Ty(C), V: 0)), NameStr: "",
94 InsertBefore: InsertionPt);
95 RetAddr->setDebugLoc(DL);
96
97 Value *Args[] = {&CurFn, RetAddr};
98 CallInst *Call =
99 CallInst::Create(Func: Fn, Args: ArrayRef<Value *>(Args), NameStr: "", InsertBefore: InsertionPt);
100 Call->setDebugLoc(DL);
101 return;
102 }
103
104 // We only know how to call a fixed set of instrumentation functions, because
105 // they all expect different arguments, etc.
106 report_fatal_error(reason: Twine("Unknown instrumentation function: '") + Func + "'");
107}
108
109static bool runOnFunction(Function &F, bool PostInlining) {
110 // The asm in a naked function may reasonably expect the argument registers
111 // and the return address register (if present) to be live. An inserted
112 // function call will clobber these registers. Simply skip naked functions for
113 // all targets.
114 if (F.hasFnAttribute(Kind: Attribute::Naked))
115 return false;
116
117 // available_externally functions may not have definitions external to the
118 // module (e.g. gnu::always_inline). Instrumenting them might lead to linker
119 // errors if they are optimized out. Skip them like GCC.
120 if (F.hasAvailableExternallyLinkage())
121 return false;
122
123 StringRef EntryAttr = PostInlining ? "instrument-function-entry-inlined"
124 : "instrument-function-entry";
125
126 StringRef ExitAttr = PostInlining ? "instrument-function-exit-inlined"
127 : "instrument-function-exit";
128
129 StringRef EntryFunc = F.getFnAttribute(Kind: EntryAttr).getValueAsString();
130 StringRef ExitFunc = F.getFnAttribute(Kind: ExitAttr).getValueAsString();
131
132 bool Changed = false;
133
134 // If the attribute is specified, insert instrumentation and then "consume"
135 // the attribute so that it's not inserted again if the pass should happen to
136 // run later for some reason.
137
138 if (!EntryFunc.empty()) {
139 DebugLoc DL;
140 if (auto SP = F.getSubprogram())
141 DL = DILocation::get(Context&: SP->getContext(), Line: SP->getScopeLine(), Column: 0, Scope: SP);
142
143 insertCall(CurFn&: F, Func: EntryFunc, InsertionPt: F.begin()->getFirstInsertionPt(), DL);
144 Changed = true;
145 F.removeFnAttr(Kind: EntryAttr);
146 }
147
148 if (!ExitFunc.empty()) {
149 for (BasicBlock &BB : F) {
150 Instruction *T = BB.getTerminator();
151 if (!isa<ReturnInst>(Val: T))
152 continue;
153
154 // If T is preceded by a musttail call, that's the real terminator.
155 if (CallInst *CI = BB.getTerminatingMustTailCall())
156 T = CI;
157
158 DebugLoc DL;
159 if (DebugLoc TerminatorDL = T->getDebugLoc())
160 DL = TerminatorDL;
161 else if (auto SP = F.getSubprogram())
162 DL = DILocation::get(Context&: SP->getContext(), Line: 0, Column: 0, Scope: SP);
163
164 insertCall(CurFn&: F, Func: ExitFunc, InsertionPt: T->getIterator(), DL);
165 Changed = true;
166 }
167 F.removeFnAttr(Kind: ExitAttr);
168 }
169
170 return Changed;
171}
172
173namespace {
174struct PostInlineEntryExitInstrumenter : public FunctionPass {
175 static char ID;
176 PostInlineEntryExitInstrumenter() : FunctionPass(ID) {
177 initializePostInlineEntryExitInstrumenterPass(
178 *PassRegistry::getPassRegistry());
179 }
180 void getAnalysisUsage(AnalysisUsage &AU) const override {
181 AU.addPreserved<GlobalsAAWrapperPass>();
182 AU.setPreservesCFG();
183 }
184 bool runOnFunction(Function &F) override { return ::runOnFunction(F, PostInlining: true); }
185};
186char PostInlineEntryExitInstrumenter::ID = 0;
187}
188
189INITIALIZE_PASS_BEGIN(
190 PostInlineEntryExitInstrumenter, "post-inline-ee-instrument",
191 "Instrument function entry/exit with calls to e.g. mcount() "
192 "(post inlining)",
193 false, false)
194INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
195INITIALIZE_PASS_END(
196 PostInlineEntryExitInstrumenter, "post-inline-ee-instrument",
197 "Instrument function entry/exit with calls to e.g. mcount() "
198 "(post inlining)",
199 false, false)
200
201FunctionPass *llvm::createPostInlineEntryExitInstrumenterPass() {
202 return new PostInlineEntryExitInstrumenter();
203}
204
205PreservedAnalyses
206llvm::EntryExitInstrumenterPass::run(Function &F, FunctionAnalysisManager &AM) {
207 if (!runOnFunction(F, PostInlining))
208 return PreservedAnalyses::all();
209 PreservedAnalyses PA;
210 PA.preserveSet<CFGAnalyses>();
211 return PA;
212}
213
214void llvm::EntryExitInstrumenterPass::printPipeline(
215 raw_ostream &OS, function_ref<StringRef(StringRef)> MapClassName2PassName) {
216 static_cast<PassInfoMixin<llvm::EntryExitInstrumenterPass> *>(this)
217 ->printPipeline(OS, MapClassName2PassName);
218 OS << '<';
219 if (PostInlining)
220 OS << "post-inline";
221 OS << '>';
222}
223