blob: e6b65825f5b5dc1d197928000b733660747c7d58 [file] [log] [blame]
Eugene Zelenkofb69e662017-06-06 22:22:41 +00001//===- XRayInstrumentation.cpp - Adds XRay instrumentation to functions. --===//
Dean Michael Berris52735fc2016-07-14 04:06:33 +00002//
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
Eugene Zelenkofb69e662017-06-06 22:22:41 +000017#include "llvm/ADT/STLExtras.h"
Dean Michael Berris711dec22017-09-08 01:47:56 +000018#include "llvm/ADT/SmallVector.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000019#include "llvm/ADT/Triple.h"
20#include "llvm/CodeGen/MachineBasicBlock.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "llvm/CodeGen/MachineDominators.h"
Dean Michael Berris52735fc2016-07-14 04:06:33 +000022#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Dean Michael Berris22f2bcf2017-05-04 01:24:26 +000025#include "llvm/CodeGen/MachineLoopInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000026#include "llvm/IR/Attributes.h"
27#include "llvm/IR/Function.h"
28#include "llvm/Pass.h"
Dean Michael Berris52735fc2016-07-14 04:06:33 +000029#include "llvm/Target/TargetInstrInfo.h"
Eugene Zelenkofb69e662017-06-06 22:22:41 +000030#include "llvm/Target/TargetMachine.h"
Dean Michael Berris52735fc2016-07-14 04:06:33 +000031#include "llvm/Target/TargetSubtargetInfo.h"
32
33using namespace llvm;
34
35namespace {
Eugene Zelenkofb69e662017-06-06 22:22:41 +000036
Dean Michael Berris52735fc2016-07-14 04:06:33 +000037struct XRayInstrumentation : public MachineFunctionPass {
38 static char ID;
39
40 XRayInstrumentation() : MachineFunctionPass(ID) {
41 initializeXRayInstrumentationPass(*PassRegistry::getPassRegistry());
42 }
43
Dean Michael Berris22f2bcf2017-05-04 01:24:26 +000044 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 AU.setPreservesCFG();
46 AU.addRequired<MachineLoopInfo>();
47 AU.addPreserved<MachineLoopInfo>();
48 AU.addPreserved<MachineDominatorTree>();
49 MachineFunctionPass::getAnalysisUsage(AU);
50 }
51
Dean Michael Berris52735fc2016-07-14 04:06:33 +000052 bool runOnMachineFunction(MachineFunction &MF) override;
Dean Michael Berris464015442016-09-19 00:54:35 +000053
54private:
55 // Replace the original RET instruction with the exit sled code ("patchable
56 // ret" pseudo-instruction), so that at runtime XRay can replace the sled
57 // with a code jumping to XRay trampoline, which calls the tracing handler
58 // and, in the end, issues the RET instruction.
59 // This is the approach to go on CPUs which have a single RET instruction,
60 // like x86/x86_64.
61 void replaceRetWithPatchableRet(MachineFunction &MF,
Dean Michael Berris22f2bcf2017-05-04 01:24:26 +000062 const TargetInstrInfo *TII);
Serge Rogatcha3311332016-11-24 18:51:47 +000063
Dean Michael Berris464015442016-09-19 00:54:35 +000064 // Prepend the original return instruction with the exit sled code ("patchable
65 // function exit" pseudo-instruction), preserving the original return
66 // instruction just after the exit sled code.
67 // This is the approach to go on CPUs which have multiple options for the
68 // return instruction, like ARM. For such CPUs we can't just jump into the
69 // XRay trampoline and issue a single return instruction there. We rather
70 // have to call the trampoline and return from it to the original return
71 // instruction of the function being instrumented.
72 void prependRetWithPatchableExit(MachineFunction &MF,
Dean Michael Berris22f2bcf2017-05-04 01:24:26 +000073 const TargetInstrInfo *TII);
Dean Michael Berris52735fc2016-07-14 04:06:33 +000074};
Eugene Zelenkofb69e662017-06-06 22:22:41 +000075
76} // end anonymous namespace
Dean Michael Berris52735fc2016-07-14 04:06:33 +000077
Dean Michael Berris22f2bcf2017-05-04 01:24:26 +000078void XRayInstrumentation::replaceRetWithPatchableRet(
79 MachineFunction &MF, const TargetInstrInfo *TII) {
Dean Michael Berris464015442016-09-19 00:54:35 +000080 // We look for *all* terminators and returns, then replace those with
Dean Michael Berris52735fc2016-07-14 04:06:33 +000081 // PATCHABLE_RET instructions.
82 SmallVector<MachineInstr *, 4> Terminators;
83 for (auto &MBB : MF) {
84 for (auto &T : MBB.terminators()) {
Dean Michael Berrise8ae5ba2016-09-01 01:29:13 +000085 unsigned Opc = 0;
Dean Michael Berris52735fc2016-07-14 04:06:33 +000086 if (T.isReturn() && T.getOpcode() == TII->getReturnOpcode()) {
87 // Replace return instructions with:
88 // PATCHABLE_RET <Opcode>, <Operand>...
Dean Michael Berrise8ae5ba2016-09-01 01:29:13 +000089 Opc = TargetOpcode::PATCHABLE_RET;
90 }
91 if (TII->isTailCall(T)) {
92 // Treat the tail call as a return instruction, which has a
93 // different-looking sled than the normal return case.
94 Opc = TargetOpcode::PATCHABLE_TAIL_CALL;
95 }
96 if (Opc != 0) {
97 auto MIB = BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc))
Dean Michael Berris52735fc2016-07-14 04:06:33 +000098 .addImm(T.getOpcode());
99 for (auto &MO : T.operands())
Diana Picus116bbab2017-01-13 09:58:52 +0000100 MIB.add(MO);
Dean Michael Berris52735fc2016-07-14 04:06:33 +0000101 Terminators.push_back(&T);
Dean Michael Berris52735fc2016-07-14 04:06:33 +0000102 }
103 }
104 }
105
106 for (auto &I : Terminators)
107 I->eraseFromParent();
Dean Michael Berris464015442016-09-19 00:54:35 +0000108}
Dean Michael Berris52735fc2016-07-14 04:06:33 +0000109
Dean Michael Berris22f2bcf2017-05-04 01:24:26 +0000110void XRayInstrumentation::prependRetWithPatchableExit(
111 MachineFunction &MF, const TargetInstrInfo *TII) {
Dean Michael Berris711dec22017-09-08 01:47:56 +0000112 for (auto &MBB : MF)
113 for (auto &T : MBB.terminators())
Dean Michael Berris464015442016-09-19 00:54:35 +0000114 if (T.isReturn()) {
Dean Michael Berris711dec22017-09-08 01:47:56 +0000115 // Prepend the return instruction with PATCHABLE_FUNCTION_EXIT.
116 BuildMI(MBB, T, T.getDebugLoc(),
117 TII->get(TargetOpcode::PATCHABLE_FUNCTION_EXIT));
Dean Michael Berris156f6ca2016-10-18 05:54:15 +0000118 }
Dean Michael Berris464015442016-09-19 00:54:35 +0000119}
120
121bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) {
122 auto &F = *MF.getFunction();
123 auto InstrAttr = F.getFnAttribute("function-instrument");
124 bool AlwaysInstrument = !InstrAttr.hasAttribute(Attribute::None) &&
125 InstrAttr.isStringAttribute() &&
126 InstrAttr.getValueAsString() == "xray-always";
127 Attribute Attr = F.getFnAttribute("xray-instruction-threshold");
128 unsigned XRayThreshold = 0;
129 if (!AlwaysInstrument) {
130 if (Attr.hasAttribute(Attribute::None) || !Attr.isStringAttribute())
131 return false; // XRay threshold attribute not found.
132 if (Attr.getValueAsString().getAsInteger(10, XRayThreshold))
133 return false; // Invalid value for threshold.
Dean Michael Berris22f2bcf2017-05-04 01:24:26 +0000134
Serge Rogatch85427c02017-06-09 13:23:23 +0000135 // Count the number of MachineInstr`s in MachineFunction
Dimitry Andrice4b97452017-07-14 21:14:58 +0000136 int64_t MICount = 0;
Dean Michael Berris711dec22017-09-08 01:47:56 +0000137 for (const auto &MBB : MF)
Dimitry Andrice4b97452017-07-14 21:14:58 +0000138 MICount += MBB.size();
Serge Rogatch85427c02017-06-09 13:23:23 +0000139
Dean Michael Berris22f2bcf2017-05-04 01:24:26 +0000140 // Check if we have a loop.
141 // FIXME: Maybe make this smarter, and see whether the loops are dependent
142 // on inputs or side-effects?
143 MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>();
Serge Rogatch85427c02017-06-09 13:23:23 +0000144 if (MLI.empty() && MICount < XRayThreshold)
Dean Michael Berris22f2bcf2017-05-04 01:24:26 +0000145 return false; // Function is too small and has no loops.
Dean Michael Berris464015442016-09-19 00:54:35 +0000146 }
147
Dean Michael Berris03b8be52016-12-19 09:20:38 +0000148 // We look for the first non-empty MachineBasicBlock, so that we can insert
149 // the function instrumentation in the appropriate place.
Eugene Zelenkofb69e662017-06-06 22:22:41 +0000150 auto MBI = llvm::find_if(
151 MF, [&](const MachineBasicBlock &MBB) { return !MBB.empty(); });
Dean Michael Berris03b8be52016-12-19 09:20:38 +0000152 if (MBI == MF.end())
153 return false; // The function is empty.
154
155 auto *TII = MF.getSubtarget().getInstrInfo();
156 auto &FirstMBB = *MBI;
Dean Michael Berris464015442016-09-19 00:54:35 +0000157 auto &FirstMI = *FirstMBB.begin();
158
159 if (!MF.getSubtarget().isXRaySupported()) {
160 FirstMI.emitError("An attempt to perform XRay instrumentation for an"
Dean Michael Berris22f2bcf2017-05-04 01:24:26 +0000161 " unsupported target.");
Dean Michael Berris464015442016-09-19 00:54:35 +0000162 return false;
163 }
164
Dean Michael Berris464015442016-09-19 00:54:35 +0000165 // First, insert an PATCHABLE_FUNCTION_ENTER as the first instruction of the
166 // MachineFunction.
Dean Michael Berris464015442016-09-19 00:54:35 +0000167 BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(),
168 TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
169
170 switch (MF.getTarget().getTargetTriple().getArch()) {
171 case Triple::ArchType::arm:
172 case Triple::ArchType::thumb:
Dean Michael Berris3234d3a2016-11-17 05:15:37 +0000173 case Triple::ArchType::aarch64:
Tim Shen918ed872017-02-10 21:03:24 +0000174 case Triple::ArchType::ppc64le:
Sagar Thakurec657922017-02-15 10:48:11 +0000175 case Triple::ArchType::mips:
176 case Triple::ArchType::mipsel:
177 case Triple::ArchType::mips64:
178 case Triple::ArchType::mips64el:
Dean Michael Berris464015442016-09-19 00:54:35 +0000179 // For the architectures which don't have a single return instruction
180 prependRetWithPatchableExit(MF, TII);
181 break;
182 default:
183 // For the architectures that have a single return instruction (such as
184 // RETQ on x86_64).
185 replaceRetWithPatchableRet(MF, TII);
186 break;
187 }
Dean Michael Berris52735fc2016-07-14 04:06:33 +0000188 return true;
189}
190
191char XRayInstrumentation::ID = 0;
192char &llvm::XRayInstrumentationID = XRayInstrumentation::ID;
Dean Michael Berris22f2bcf2017-05-04 01:24:26 +0000193INITIALIZE_PASS_BEGIN(XRayInstrumentation, "xray-instrumentation",
194 "Insert XRay ops", false, false)
195INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo)
196INITIALIZE_PASS_END(XRayInstrumentation, "xray-instrumentation",
197 "Insert XRay ops", false, false)