blob: 1035bc3d4a0b742c9e6942b042b645f8c768203d [file] [log] [blame]
Dean Michael Berris52735fc2016-07-14 04:06:33 +00001//===-- XRayInstrumentation.cpp - Adds XRay instrumentation to functions. -===//
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// 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
13// operations.
14//
15//===---------------------------------------------------------------------===//
16
17#include "llvm/CodeGen/Analysis.h"
18#include "llvm/CodeGen/MachineFunction.h"
19#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/Passes.h"
22#include "llvm/Support/TargetRegistry.h"
23#include "llvm/Target/TargetInstrInfo.h"
24#include "llvm/Target/TargetSubtargetInfo.h"
25
26using namespace llvm;
27
28namespace {
29struct XRayInstrumentation : public MachineFunctionPass {
30 static char ID;
31
32 XRayInstrumentation() : MachineFunctionPass(ID) {
33 initializeXRayInstrumentationPass(*PassRegistry::getPassRegistry());
34 }
35
36 bool runOnMachineFunction(MachineFunction &MF) override;
Dean Michael Berris17d94e22016-09-08 00:19:04 +000037
38private:
39 // Replace the original RET instruction with the exit sled code ("patchable
40 // ret" pseudo-instruction), so that at runtime XRay can replace the sled
41 // with a code jumping to XRay trampoline, which calls the tracing handler
42 // and, in the end, issues the RET instruction.
43 // This is the approach to go on CPUs which have a single RET instruction,
44 // like x86/x86_64.
45 void replaceRetWithPatchableRet(MachineFunction &MF,
46 const TargetInstrInfo *TII);
47 // Prepend the original return instruction with the exit sled code ("patchable
48 // function exit" pseudo-instruction), preserving the original return
49 // instruction just after the exit sled code.
50 // This is the approach to go on CPUs which have multiple options for the
51 // return instruction, like ARM. For such CPUs we can't just jump into the
52 // XRay trampoline and issue a single return instruction there. We rather
53 // have to call the trampoline and return from it to the original return
54 // instruction of the function being instrumented.
55 void prependRetWithPatchableExit(MachineFunction &MF,
56 const TargetInstrInfo *TII);
Dean Michael Berris52735fc2016-07-14 04:06:33 +000057};
Dean Michael Berris17d94e22016-09-08 00:19:04 +000058} // anonymous namespace
Dean Michael Berris52735fc2016-07-14 04:06:33 +000059
Dean Michael Berris17d94e22016-09-08 00:19:04 +000060void XRayInstrumentation::replaceRetWithPatchableRet(MachineFunction &MF,
61 const TargetInstrInfo *TII)
62{
63 // We look for *all* terminators and returns, then replace those with
Dean Michael Berris52735fc2016-07-14 04:06:33 +000064 // PATCHABLE_RET instructions.
65 SmallVector<MachineInstr *, 4> Terminators;
66 for (auto &MBB : MF) {
67 for (auto &T : MBB.terminators()) {
Dean Michael Berrise8ae5ba2016-09-01 01:29:13 +000068 unsigned Opc = 0;
Dean Michael Berris52735fc2016-07-14 04:06:33 +000069 if (T.isReturn() && T.getOpcode() == TII->getReturnOpcode()) {
70 // Replace return instructions with:
71 // PATCHABLE_RET <Opcode>, <Operand>...
Dean Michael Berrise8ae5ba2016-09-01 01:29:13 +000072 Opc = TargetOpcode::PATCHABLE_RET;
73 }
74 if (TII->isTailCall(T)) {
75 // Treat the tail call as a return instruction, which has a
76 // different-looking sled than the normal return case.
77 Opc = TargetOpcode::PATCHABLE_TAIL_CALL;
78 }
79 if (Opc != 0) {
80 auto MIB = BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc))
Dean Michael Berris52735fc2016-07-14 04:06:33 +000081 .addImm(T.getOpcode());
82 for (auto &MO : T.operands())
83 MIB.addOperand(MO);
84 Terminators.push_back(&T);
Dean Michael Berris52735fc2016-07-14 04:06:33 +000085 }
86 }
87 }
88
89 for (auto &I : Terminators)
90 I->eraseFromParent();
Dean Michael Berris17d94e22016-09-08 00:19:04 +000091}
Dean Michael Berris52735fc2016-07-14 04:06:33 +000092
Dean Michael Berris17d94e22016-09-08 00:19:04 +000093void XRayInstrumentation::prependRetWithPatchableExit(MachineFunction &MF,
94 const TargetInstrInfo *TII)
95{
96 for (auto &MBB : MF) {
97 for (auto &T : MBB.terminators()) {
98 if (T.isReturn()) {
99 // Prepend the return instruction with PATCHABLE_FUNCTION_EXIT
Dean Michael Berriscf3801e2016-09-08 00:38:22 +0000100 BuildMI(MBB, T, T.getDebugLoc(),
101 TII->get(TargetOpcode::PATCHABLE_FUNCTION_EXIT));
Dean Michael Berris17d94e22016-09-08 00:19:04 +0000102 }
103 }
104 }
105}
106
107bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
108 auto &F = *MF.getFunction();
109 auto InstrAttr = F.getFnAttribute("function-instrument");
110 bool AlwaysInstrument = !InstrAttr.hasAttribute(Attribute::None) &&
111 InstrAttr.isStringAttribute() &&
112 InstrAttr.getValueAsString() == "xray-always";
113 Attribute Attr = F.getFnAttribute("xray-instruction-threshold");
114 unsigned XRayThreshold = 0;
115 if (!AlwaysInstrument) {
116 if (Attr.hasAttribute(Attribute::None) || !Attr.isStringAttribute())
117 return false; // XRay threshold attribute not found.
118 if (Attr.getValueAsString().getAsInteger(10, XRayThreshold))
119 return false; // Invalid value for threshold.
120 if (F.size() < XRayThreshold)
121 return false; // Function is too small.
122 }
123
124 if (!MF.getSubtarget().isXRaySupported()) {
125 //FIXME: can this be reported somehow?
126 return false;
127 }
128
129 // FIXME: Do the loop triviality analysis here or in an earlier pass.
130
131 // First, insert an PATCHABLE_FUNCTION_ENTER as the first instruction of the
132 // MachineFunction.
133 auto &FirstMBB = *MF.begin();
134 auto &FirstMI = *FirstMBB.begin();
135 auto *TII = MF.getSubtarget().getInstrInfo();
136 BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(),
137 TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
138
139 switch (MF.getTarget().getTargetTriple().getArch()) {
140 case Triple::ArchType::arm:
141 // For the architectures which don't have a single return instruction
142 prependRetWithPatchableExit(MF, TII);
143 break;
144 default:
145 // For the architectures that have a single return instruction (such as
146 // RETQ on x86_64).
147 replaceRetWithPatchableRet(MF, TII);
148 break;
149 }
Dean Michael Berris52735fc2016-07-14 04:06:33 +0000150 return true;
151}
152
153char XRayInstrumentation::ID = 0;
154char &llvm::XRayInstrumentationID = XRayInstrumentation::ID;
155INITIALIZE_PASS(XRayInstrumentation, "xray-instrumentation", "Insert XRay ops",
Dean Michael Berris086639a2016-07-14 11:46:41 +0000156 false, false)