1 //===- XRayInstrumentation.cpp - Adds XRay instrumentation to functions. --===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a MachineFunctionPass that inserts the appropriate
11 // XRay instrumentation instructions. We look for XRay-specific attributes
12 // on the function to determine whether we should insert the replacement
15 //===---------------------------------------------------------------------===//
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/CodeGen/MachineDominators.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineLoopInfo.h"
26 #include "llvm/CodeGen/TargetInstrInfo.h"
27 #include "llvm/CodeGen/TargetSubtargetInfo.h"
28 #include "llvm/IR/Attributes.h"
29 #include "llvm/IR/Function.h"
30 #include "llvm/Pass.h"
31 #include "llvm/Target/TargetMachine.h"
37 struct InstrumentationOptions {
38 // Whether to emit PATCHABLE_TAIL_CALL.
41 // Whether to emit PATCHABLE_RET/PATCHABLE_FUNCTION_EXIT for all forms of
42 // return, e.g. conditional return.
43 bool HandleAllReturns;
46 struct XRayInstrumentation : public MachineFunctionPass {
49 XRayInstrumentation() : MachineFunctionPass(ID) {
50 initializeXRayInstrumentationPass(*PassRegistry::getPassRegistry());
53 void getAnalysisUsage(AnalysisUsage &AU) const override {
55 AU.addRequired<MachineLoopInfo>();
56 AU.addPreserved<MachineLoopInfo>();
57 AU.addPreserved<MachineDominatorTree>();
58 MachineFunctionPass::getAnalysisUsage(AU);
61 bool runOnMachineFunction(MachineFunction &MF) override;
64 // Replace the original RET instruction with the exit sled code ("patchable
65 // ret" pseudo-instruction), so that at runtime XRay can replace the sled
66 // with a code jumping to XRay trampoline, which calls the tracing handler
67 // and, in the end, issues the RET instruction.
68 // This is the approach to go on CPUs which have a single RET instruction,
70 void replaceRetWithPatchableRet(MachineFunction &MF,
71 const TargetInstrInfo *TII,
72 InstrumentationOptions);
74 // Prepend the original return instruction with the exit sled code ("patchable
75 // function exit" pseudo-instruction), preserving the original return
76 // instruction just after the exit sled code.
77 // This is the approach to go on CPUs which have multiple options for the
78 // return instruction, like ARM. For such CPUs we can't just jump into the
79 // XRay trampoline and issue a single return instruction there. We rather
80 // have to call the trampoline and return from it to the original return
81 // instruction of the function being instrumented.
82 void prependRetWithPatchableExit(MachineFunction &MF,
83 const TargetInstrInfo *TII,
84 InstrumentationOptions);
87 } // end anonymous namespace
89 void XRayInstrumentation::replaceRetWithPatchableRet(
90 MachineFunction &MF, const TargetInstrInfo *TII,
91 InstrumentationOptions op) {
92 // We look for *all* terminators and returns, then replace those with
93 // PATCHABLE_RET instructions.
94 SmallVector<MachineInstr *, 4> Terminators;
95 for (auto &MBB : MF) {
96 for (auto &T : MBB.terminators()) {
99 (op.HandleAllReturns || T.getOpcode() == TII->getReturnOpcode())) {
100 // Replace return instructions with:
101 // PATCHABLE_RET <Opcode>, <Operand>...
102 Opc = TargetOpcode::PATCHABLE_RET;
104 if (TII->isTailCall(T) && op.HandleTailcall) {
105 // Treat the tail call as a return instruction, which has a
106 // different-looking sled than the normal return case.
107 Opc = TargetOpcode::PATCHABLE_TAIL_CALL;
110 auto MIB = BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc))
111 .addImm(T.getOpcode());
112 for (auto &MO : T.operands())
114 Terminators.push_back(&T);
119 for (auto &I : Terminators)
120 I->eraseFromParent();
123 void XRayInstrumentation::prependRetWithPatchableExit(
124 MachineFunction &MF, const TargetInstrInfo *TII,
125 InstrumentationOptions op) {
127 for (auto &T : MBB.terminators()) {
130 (op.HandleAllReturns || T.getOpcode() == TII->getReturnOpcode())) {
131 Opc = TargetOpcode::PATCHABLE_FUNCTION_EXIT;
133 if (TII->isTailCall(T) && op.HandleTailcall) {
134 Opc = TargetOpcode::PATCHABLE_TAIL_CALL;
137 // Prepend the return instruction with PATCHABLE_FUNCTION_EXIT or
138 // PATCHABLE_TAIL_CALL .
139 BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc));
144 bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
145 auto &F = MF.getFunction();
146 auto InstrAttr = F.getFnAttribute("function-instrument");
147 bool AlwaysInstrument = !InstrAttr.hasAttribute(Attribute::None) &&
148 InstrAttr.isStringAttribute() &&
149 InstrAttr.getValueAsString() == "xray-always";
150 Attribute Attr = F.getFnAttribute("xray-instruction-threshold");
151 unsigned XRayThreshold = 0;
152 if (!AlwaysInstrument) {
153 if (Attr.hasAttribute(Attribute::None) || !Attr.isStringAttribute())
154 return false; // XRay threshold attribute not found.
155 if (Attr.getValueAsString().getAsInteger(10, XRayThreshold))
156 return false; // Invalid value for threshold.
158 // Count the number of MachineInstr`s in MachineFunction
160 for (const auto &MBB : MF)
161 MICount += MBB.size();
163 // Check if we have a loop.
164 // FIXME: Maybe make this smarter, and see whether the loops are dependent
165 // on inputs or side-effects?
166 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
167 if (MLI.empty() && MICount < XRayThreshold)
168 return false; // Function is too small and has no loops.
171 // We look for the first non-empty MachineBasicBlock, so that we can insert
172 // the function instrumentation in the appropriate place.
173 auto MBI = llvm::find_if(
174 MF, [&](const MachineBasicBlock &MBB) { return !MBB.empty(); });
176 return false; // The function is empty.
178 auto *TII = MF.getSubtarget().getInstrInfo();
179 auto &FirstMBB = *MBI;
180 auto &FirstMI = *FirstMBB.begin();
182 if (!MF.getSubtarget().isXRaySupported()) {
183 FirstMI.emitError("An attempt to perform XRay instrumentation for an"
184 " unsupported target.");
188 // First, insert an PATCHABLE_FUNCTION_ENTER as the first instruction of the
190 BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(),
191 TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
193 switch (MF.getTarget().getTargetTriple().getArch()) {
194 case Triple::ArchType::arm:
195 case Triple::ArchType::thumb:
196 case Triple::ArchType::aarch64:
197 case Triple::ArchType::mips:
198 case Triple::ArchType::mipsel:
199 case Triple::ArchType::mips64:
200 case Triple::ArchType::mips64el: {
201 // For the architectures which don't have a single return instruction
202 InstrumentationOptions op;
203 op.HandleTailcall = false;
204 op.HandleAllReturns = true;
205 prependRetWithPatchableExit(MF, TII, op);
208 case Triple::ArchType::ppc64le: {
209 // PPC has conditional returns. Turn them into branch and plain returns.
210 InstrumentationOptions op;
211 op.HandleTailcall = false;
212 op.HandleAllReturns = true;
213 replaceRetWithPatchableRet(MF, TII, op);
217 // For the architectures that have a single return instruction (such as
219 InstrumentationOptions op;
220 op.HandleTailcall = true;
221 op.HandleAllReturns = false;
222 replaceRetWithPatchableRet(MF, TII, op);
229 char XRayInstrumentation::ID = 0;
230 char &llvm::XRayInstrumentationID = XRayInstrumentation::ID;
231 INITIALIZE_PASS_BEGIN(XRayInstrumentation, "xray-instrumentation",
232 "Insert XRay ops", false, false)
233 INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
234 INITIALIZE_PASS_END(XRayInstrumentation, "xray-instrumentation",
235 "Insert XRay ops", false, false)