LLVM 23.0.0git
XRayInstrumentation.cpp
Go to the documentation of this file.
1//===- XRayInstrumentation.cpp - Adds XRay instrumentation to functions. --===//
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// This file implements a MachineFunctionPass that inserts the appropriate
10// XRay instrumentation instructions. We look for XRay-specific attributes
11// on the function to determine whether we should insert the replacement
12// operations.
13//
14//===---------------------------------------------------------------------===//
15
17#include "llvm/ADT/STLExtras.h"
29#include "llvm/IR/Attributes.h"
31#include "llvm/IR/Function.h"
33#include "llvm/Pass.h"
36
37using namespace llvm;
38
39namespace {
40
41struct InstrumentationOptions {
42 // Whether to emit PATCHABLE_TAIL_CALL.
43 bool HandleTailcall;
44
45 // Whether to emit PATCHABLE_RET/PATCHABLE_FUNCTION_EXIT for all forms of
46 // return, e.g. conditional return.
47 bool HandleAllReturns;
48};
49
50struct XRayInstrumentationLegacy : public MachineFunctionPass {
51 static char ID;
52
53 XRayInstrumentationLegacy() : MachineFunctionPass(ID) {}
54
55 void getAnalysisUsage(AnalysisUsage &AU) const override {
56 AU.setPreservesCFG();
57 AU.addPreserved<MachineLoopInfoWrapperPass>();
58 AU.addPreserved<MachineDominatorTreeWrapperPass>();
60 }
61
62 bool runOnMachineFunction(MachineFunction &MF) override;
63};
64
65struct XRayInstrumentation {
66 XRayInstrumentation(MachineDominatorTree *MDT, MachineLoopInfo *MLI)
67 : MDT(MDT), MLI(MLI) {}
68
69 bool run(MachineFunction &MF);
70
71 // Methods for use in the NPM and legacy passes, can be removed once migration
72 // is complete.
73 static bool alwaysInstrument(Function &F) {
74 auto InstrAttr = F.getFnAttribute("function-instrument");
75 return InstrAttr.isStringAttribute() &&
76 InstrAttr.getValueAsString() == "xray-always";
77 }
78
79 static bool needMDTAndMLIAnalyses(Function &F) {
80 auto IgnoreLoopsAttr = F.getFnAttribute("xray-ignore-loops");
81 auto AlwaysInstrument = XRayInstrumentation::alwaysInstrument(F);
82 return !AlwaysInstrument && !IgnoreLoopsAttr.isValid();
83 }
84
85private:
86 // Replace the original RET instruction with the exit sled code ("patchable
87 // ret" pseudo-instruction), so that at runtime XRay can replace the sled
88 // with a code jumping to XRay trampoline, which calls the tracing handler
89 // and, in the end, issues the RET instruction.
90 // This is the approach to go on CPUs which have a single RET instruction,
91 // like x86/x86_64.
92 void replaceRetWithPatchableRet(MachineFunction &MF,
93 const TargetInstrInfo *TII,
94 InstrumentationOptions);
95
96 // Prepend the original return instruction with the exit sled code ("patchable
97 // function exit" pseudo-instruction), preserving the original return
98 // instruction just after the exit sled code.
99 // This is the approach to go on CPUs which have multiple options for the
100 // return instruction, like ARM. For such CPUs we can't just jump into the
101 // XRay trampoline and issue a single return instruction there. We rather
102 // have to call the trampoline and return from it to the original return
103 // instruction of the function being instrumented.
104 void prependRetWithPatchableExit(MachineFunction &MF,
105 const TargetInstrInfo *TII,
106 InstrumentationOptions);
107
108 MachineDominatorTree *MDT;
109 MachineLoopInfo *MLI;
110};
111
112} // end anonymous namespace
113
114void XRayInstrumentation::replaceRetWithPatchableRet(
116 InstrumentationOptions op) {
117 // We look for *all* terminators and returns, then replace those with
118 // PATCHABLE_RET instructions.
119 SmallVector<MachineInstr *, 4> Terminators;
120 for (auto &MBB : MF) {
121 for (auto &T : MBB.terminators()) {
122 unsigned Opc = 0;
123 if (T.isReturn() &&
124 (op.HandleAllReturns || T.getOpcode() == TII->getReturnOpcode())) {
125 // Replace return instructions with:
126 // PATCHABLE_RET <Opcode>, <Operand>...
127 Opc = TargetOpcode::PATCHABLE_RET;
128 }
129 if (TII->isTailCall(T) && op.HandleTailcall) {
130 // Treat the tail call as a return instruction, which has a
131 // different-looking sled than the normal return case.
132 Opc = TargetOpcode::PATCHABLE_TAIL_CALL;
133 }
134 if (Opc != 0) {
135 auto MIB = BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc))
136 .addImm(T.getOpcode());
137 for (auto &MO : T.operands())
138 MIB.add(MO);
139 Terminators.push_back(&T);
140 if (T.shouldUpdateAdditionalCallInfo())
141 MF.eraseAdditionalCallInfo(&T);
142 }
143 }
144 }
145
146 for (auto &I : Terminators)
147 I->eraseFromParent();
148}
149
150void XRayInstrumentation::prependRetWithPatchableExit(
151 MachineFunction &MF, const TargetInstrInfo *TII,
152 InstrumentationOptions op) {
153 for (auto &MBB : MF)
154 for (auto &T : MBB.terminators()) {
155 unsigned Opc = 0;
156 if (T.isReturn() &&
157 (op.HandleAllReturns || T.getOpcode() == TII->getReturnOpcode())) {
158 Opc = TargetOpcode::PATCHABLE_FUNCTION_EXIT;
159 }
160 if (TII->isTailCall(T) && op.HandleTailcall) {
161 Opc = TargetOpcode::PATCHABLE_TAIL_CALL;
162 }
163 if (Opc != 0) {
164 // Prepend the return instruction with PATCHABLE_FUNCTION_EXIT or
165 // PATCHABLE_TAIL_CALL .
166 BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc));
167 }
168 }
169}
170
171PreservedAnalyses
174 MachineDominatorTree *MDT = nullptr;
175 MachineLoopInfo *MLI = nullptr;
176
177 if (XRayInstrumentation::needMDTAndMLIAnalyses(MF.getFunction())) {
179 MLI = MFAM.getCachedResult<MachineLoopAnalysis>(MF);
180 }
181
182 if (!XRayInstrumentation(MDT, MLI).run(MF))
183 return PreservedAnalyses::all();
184
186 PA.preserveSet<CFGAnalyses>();
187 return PA;
188}
189
190bool XRayInstrumentationLegacy::runOnMachineFunction(MachineFunction &MF) {
191 MachineDominatorTree *MDT = nullptr;
192 MachineLoopInfo *MLI = nullptr;
193 if (XRayInstrumentation::needMDTAndMLIAnalyses(MF.getFunction())) {
194 auto *MDTWrapper =
195 getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>();
196 MDT = MDTWrapper ? &MDTWrapper->getDomTree() : nullptr;
197 auto *MLIWrapper = getAnalysisIfAvailable<MachineLoopInfoWrapperPass>();
198 MLI = MLIWrapper ? &MLIWrapper->getLI() : nullptr;
199 }
200 return XRayInstrumentation(MDT, MLI).run(MF);
201}
202
203bool XRayInstrumentation::run(MachineFunction &MF) {
204 auto &F = MF.getFunction();
205 auto InstrAttr = F.getFnAttribute("function-instrument");
206 bool AlwaysInstrument = alwaysInstrument(F);
207 bool NeverInstrument = InstrAttr.isStringAttribute() &&
208 InstrAttr.getValueAsString() == "xray-never";
209 if (NeverInstrument && !AlwaysInstrument)
210 return false;
211 auto IgnoreLoopsAttr = F.getFnAttribute("xray-ignore-loops");
212
213 uint64_t XRayThreshold = 0;
214 if (!AlwaysInstrument) {
215 bool IgnoreLoops = IgnoreLoopsAttr.isValid();
216 XRayThreshold = F.getFnAttributeAsParsedInteger(
217 "xray-instruction-threshold", std::numeric_limits<uint64_t>::max());
218 if (XRayThreshold == std::numeric_limits<uint64_t>::max())
219 return false;
220
221 // Count the number of MachineInstr`s in MachineFunction
222 uint64_t MICount = 0;
223 for (const auto &MBB : MF)
224 MICount += MBB.size();
225
226 bool TooFewInstrs = MICount < XRayThreshold;
227
228 if (!IgnoreLoops) {
229 // Get MachineDominatorTree or compute it on the fly if it's unavailable
230 MachineDominatorTree ComputedMDT;
231 if (!MDT) {
232 ComputedMDT.recalculate(MF);
233 MDT = &ComputedMDT;
234 }
235
236 // Get MachineLoopInfo or compute it on the fly if it's unavailable
237 MachineLoopInfo ComputedMLI;
238 if (!MLI) {
239 ComputedMLI.analyze(*MDT);
240 MLI = &ComputedMLI;
241 }
242
243 // Check if we have a loop.
244 // FIXME: Maybe make this smarter, and see whether the loops are dependent
245 // on inputs or side-effects?
246 if (MLI->empty() && TooFewInstrs)
247 return false; // Function is too small and has no loops.
248 } else if (TooFewInstrs) {
249 // Function is too small
250 return false;
251 }
252 }
253
254 // We look for the first non-empty MachineBasicBlock, so that we can insert
255 // the function instrumentation in the appropriate place.
256 auto MBI = llvm::find_if(
257 MF, [&](const MachineBasicBlock &MBB) { return !MBB.empty(); });
258 if (MBI == MF.end())
259 return false; // The function is empty.
260
261 auto *TII = MF.getSubtarget().getInstrInfo();
262 auto &FirstMBB = *MBI;
263 auto &FirstMI = *FirstMBB.begin();
264
265 if (!MF.getSubtarget().isXRaySupported()) {
266
267 const Function &Fn = FirstMBB.getParent()->getFunction();
268 Fn.getContext().diagnose(DiagnosticInfoUnsupported(
269 Fn, "An attempt to perform XRay instrumentation for an"
270 " unsupported target."));
271
272 return false;
273 }
274
275 if (!F.hasFnAttribute("xray-skip-entry")) {
276 // First, insert an PATCHABLE_FUNCTION_ENTER as the first instruction of the
277 // MachineFunction.
278 BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(),
279 TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
280 }
281
282 if (!F.hasFnAttribute("xray-skip-exit")) {
283 switch (MF.getTarget().getTargetTriple().getArch()) {
284 case Triple::ArchType::arm:
285 case Triple::ArchType::thumb:
286 case Triple::ArchType::aarch64:
287 case Triple::ArchType::hexagon:
288 case Triple::ArchType::loongarch64:
289 case Triple::ArchType::mips:
290 case Triple::ArchType::mipsel:
291 case Triple::ArchType::mips64:
292 case Triple::ArchType::mips64el:
293 case Triple::ArchType::riscv32:
294 case Triple::ArchType::riscv64: {
295 // For the architectures which don't have a single return instruction
296 InstrumentationOptions op;
297 // AArch64 and RISC-V support patching tail calls.
298 op.HandleTailcall = MF.getTarget().getTargetTriple().isAArch64() ||
299 MF.getTarget().getTargetTriple().isRISCV();
300 op.HandleAllReturns = true;
301 prependRetWithPatchableExit(MF, TII, op);
302 break;
303 }
304 case Triple::ArchType::ppc64le:
305 case Triple::ArchType::systemz: {
306 // PPC has conditional returns. Turn them into branch and plain returns.
307 InstrumentationOptions op;
308 op.HandleTailcall = false;
309 op.HandleAllReturns = true;
310 replaceRetWithPatchableRet(MF, TII, op);
311 break;
312 }
313 default: {
314 // For the architectures that have a single return instruction (such as
315 // RETQ on x86_64).
316 InstrumentationOptions op;
317 op.HandleTailcall = true;
318 op.HandleAllReturns = false;
319 replaceRetWithPatchableRet(MF, TII, op);
320 break;
321 }
322 }
323 }
324 return true;
325}
326
327char XRayInstrumentationLegacy::ID = 0;
328char &llvm::XRayInstrumentationID = XRayInstrumentationLegacy::ID;
329INITIALIZE_PASS_BEGIN(XRayInstrumentationLegacy, "xray-instrumentation",
330 "Insert XRay ops", false, false)
332INITIALIZE_PASS_END(XRayInstrumentationLegacy, "xray-instrumentation",
333 "Insert XRay ops", false, false)
MachineBasicBlock & MBB
This file contains the simple types necessary to represent the attributes associated with functions a...
#define op(i)
const HexagonInstrInfo * TII
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
PassT::Result * getCachedResult(IRUnitT &IR) const
Get the cached result of an analysis pass for a given IR unit.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
void recalculate(ParentType &Func)
recalculate - compute a dominator tree for the given function
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition Function.cpp:764
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:358
bool isTailCall(const MachineInstr &MI) const override
LLVM_ABI void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
void analyze(const DominatorTreeBase< BlockT, false > &DomTree)
Create the loop forest using a stable algorithm.
iterator_range< iterator > terminators()
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & add(const MachineOperand &MO) const
Analysis pass that exposes the MachineLoopInfo for a machine function.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
void push_back(const T &Elt)
TargetInstrInfo - Interface to description of machine instruction set.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI char & XRayInstrumentationID
This pass inserts the XRay instrumentation sleds if they are supported by the target platform.
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1770