Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1 | //===- XRayInstrumentation.cpp - Adds XRay instrumentation to functions. --===// |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 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 | |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Dean Michael Berris | f7f70f7 | 2017-09-08 01:47:56 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Triple.h" |
| 20 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineDominators.h" |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineFunction.h" |
| 23 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 24 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Dean Michael Berris | 875f0a3 | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineLoopInfo.h" |
David Blaikie | 4831923 | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Attributes.h" |
| 29 | #include "llvm/IR/Function.h" |
| 30 | #include "llvm/Pass.h" |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetMachine.h" |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace llvm; |
| 34 | |
| 35 | namespace { |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 36 | |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 37 | struct InstrumentationOptions { |
| 38 | // Whether to emit PATCHABLE_TAIL_CALL. |
| 39 | bool HandleTailcall; |
| 40 | |
| 41 | // Whether to emit PATCHABLE_RET/PATCHABLE_FUNCTION_EXIT for all forms of |
| 42 | // return, e.g. conditional return. |
| 43 | bool HandleAllReturns; |
| 44 | }; |
| 45 | |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 46 | struct XRayInstrumentation : public MachineFunctionPass { |
| 47 | static char ID; |
| 48 | |
| 49 | XRayInstrumentation() : MachineFunctionPass(ID) { |
| 50 | initializeXRayInstrumentationPass(*PassRegistry::getPassRegistry()); |
| 51 | } |
| 52 | |
Dean Michael Berris | 875f0a3 | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 53 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 54 | AU.setPreservesCFG(); |
Dean Michael Berris | 875f0a3 | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 55 | AU.addPreserved<MachineLoopInfo>(); |
| 56 | AU.addPreserved<MachineDominatorTree>(); |
| 57 | MachineFunctionPass::getAnalysisUsage(AU); |
| 58 | } |
| 59 | |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 60 | bool runOnMachineFunction(MachineFunction &MF) override; |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 61 | |
| 62 | private: |
| 63 | // Replace the original RET instruction with the exit sled code ("patchable |
| 64 | // ret" pseudo-instruction), so that at runtime XRay can replace the sled |
| 65 | // with a code jumping to XRay trampoline, which calls the tracing handler |
| 66 | // and, in the end, issues the RET instruction. |
| 67 | // This is the approach to go on CPUs which have a single RET instruction, |
| 68 | // like x86/x86_64. |
| 69 | void replaceRetWithPatchableRet(MachineFunction &MF, |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 70 | const TargetInstrInfo *TII, |
| 71 | InstrumentationOptions); |
Serge Rogatch | ef5c24c | 2016-11-24 18:51:47 +0000 | [diff] [blame] | 72 | |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 73 | // Prepend the original return instruction with the exit sled code ("patchable |
| 74 | // function exit" pseudo-instruction), preserving the original return |
| 75 | // instruction just after the exit sled code. |
| 76 | // This is the approach to go on CPUs which have multiple options for the |
| 77 | // return instruction, like ARM. For such CPUs we can't just jump into the |
| 78 | // XRay trampoline and issue a single return instruction there. We rather |
| 79 | // have to call the trampoline and return from it to the original return |
| 80 | // instruction of the function being instrumented. |
| 81 | void prependRetWithPatchableExit(MachineFunction &MF, |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 82 | const TargetInstrInfo *TII, |
| 83 | InstrumentationOptions); |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 84 | }; |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 85 | |
| 86 | } // end anonymous namespace |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 87 | |
Dean Michael Berris | 875f0a3 | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 88 | void XRayInstrumentation::replaceRetWithPatchableRet( |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 89 | MachineFunction &MF, const TargetInstrInfo *TII, |
| 90 | InstrumentationOptions op) { |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 91 | // We look for *all* terminators and returns, then replace those with |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 92 | // PATCHABLE_RET instructions. |
| 93 | SmallVector<MachineInstr *, 4> Terminators; |
| 94 | for (auto &MBB : MF) { |
| 95 | for (auto &T : MBB.terminators()) { |
Dean Michael Berris | d17ccfb | 2016-09-01 01:29:13 +0000 | [diff] [blame] | 96 | unsigned Opc = 0; |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 97 | if (T.isReturn() && |
| 98 | (op.HandleAllReturns || T.getOpcode() == TII->getReturnOpcode())) { |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 99 | // Replace return instructions with: |
| 100 | // PATCHABLE_RET <Opcode>, <Operand>... |
Dean Michael Berris | d17ccfb | 2016-09-01 01:29:13 +0000 | [diff] [blame] | 101 | Opc = TargetOpcode::PATCHABLE_RET; |
| 102 | } |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 103 | if (TII->isTailCall(T) && op.HandleTailcall) { |
Dean Michael Berris | d17ccfb | 2016-09-01 01:29:13 +0000 | [diff] [blame] | 104 | // Treat the tail call as a return instruction, which has a |
| 105 | // different-looking sled than the normal return case. |
| 106 | Opc = TargetOpcode::PATCHABLE_TAIL_CALL; |
| 107 | } |
| 108 | if (Opc != 0) { |
| 109 | auto MIB = BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc)) |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 110 | .addImm(T.getOpcode()); |
| 111 | for (auto &MO : T.operands()) |
Diana Picus | 8a47810 | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 112 | MIB.add(MO); |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 113 | Terminators.push_back(&T); |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | for (auto &I : Terminators) |
| 119 | I->eraseFromParent(); |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 120 | } |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 121 | |
Dean Michael Berris | 875f0a3 | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 122 | void XRayInstrumentation::prependRetWithPatchableExit( |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 123 | MachineFunction &MF, const TargetInstrInfo *TII, |
| 124 | InstrumentationOptions op) { |
Dean Michael Berris | f7f70f7 | 2017-09-08 01:47:56 +0000 | [diff] [blame] | 125 | for (auto &MBB : MF) |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 126 | for (auto &T : MBB.terminators()) { |
| 127 | unsigned Opc = 0; |
| 128 | if (T.isReturn() && |
| 129 | (op.HandleAllReturns || T.getOpcode() == TII->getReturnOpcode())) { |
| 130 | Opc = TargetOpcode::PATCHABLE_FUNCTION_EXIT; |
Dean Michael Berris | dfab481 | 2016-10-18 05:54:15 +0000 | [diff] [blame] | 131 | } |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 132 | if (TII->isTailCall(T) && op.HandleTailcall) { |
| 133 | Opc = TargetOpcode::PATCHABLE_TAIL_CALL; |
| 134 | } |
| 135 | if (Opc != 0) { |
| 136 | // Prepend the return instruction with PATCHABLE_FUNCTION_EXIT or |
| 137 | // PATCHABLE_TAIL_CALL . |
| 138 | BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc)); |
| 139 | } |
| 140 | } |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) { |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 144 | auto &F = MF.getFunction(); |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 145 | auto InstrAttr = F.getFnAttribute("function-instrument"); |
| 146 | bool AlwaysInstrument = !InstrAttr.hasAttribute(Attribute::None) && |
| 147 | InstrAttr.isStringAttribute() && |
| 148 | InstrAttr.getValueAsString() == "xray-always"; |
| 149 | Attribute Attr = F.getFnAttribute("xray-instruction-threshold"); |
| 150 | unsigned XRayThreshold = 0; |
| 151 | if (!AlwaysInstrument) { |
| 152 | if (Attr.hasAttribute(Attribute::None) || !Attr.isStringAttribute()) |
| 153 | return false; // XRay threshold attribute not found. |
| 154 | if (Attr.getValueAsString().getAsInteger(10, XRayThreshold)) |
| 155 | return false; // Invalid value for threshold. |
Dean Michael Berris | 875f0a3 | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 156 | |
Serge Rogatch | 30c7582 | 2017-06-09 13:23:23 +0000 | [diff] [blame] | 157 | // Count the number of MachineInstr`s in MachineFunction |
Dimitry Andric | a774076 | 2017-07-14 21:14:58 +0000 | [diff] [blame] | 158 | int64_t MICount = 0; |
Dean Michael Berris | f7f70f7 | 2017-09-08 01:47:56 +0000 | [diff] [blame] | 159 | for (const auto &MBB : MF) |
Dimitry Andric | a774076 | 2017-07-14 21:14:58 +0000 | [diff] [blame] | 160 | MICount += MBB.size(); |
Serge Rogatch | 30c7582 | 2017-06-09 13:23:23 +0000 | [diff] [blame] | 161 | |
Michael Zolotukhin | 5926c6d | 2018-03-20 17:02:29 +0000 | [diff] [blame] | 162 | // Get MachineDominatorTree or compute it on the fly if it's unavailable |
| 163 | auto *MDT = getAnalysisIfAvailable<MachineDominatorTree>(); |
| 164 | MachineDominatorTree ComputedMDT; |
| 165 | if (!MDT) { |
| 166 | ComputedMDT.getBase().recalculate(MF); |
| 167 | MDT = &ComputedMDT; |
| 168 | } |
| 169 | |
| 170 | // Get MachineLoopInfo or compute it on the fly if it's unavailable |
| 171 | auto *MLI = getAnalysisIfAvailable<MachineLoopInfo>(); |
| 172 | MachineLoopInfo ComputedMLI; |
| 173 | if (!MLI) { |
| 174 | ComputedMLI.getBase().analyze(MDT->getBase()); |
| 175 | MLI = &ComputedMLI; |
| 176 | } |
| 177 | |
Dean Michael Berris | 875f0a3 | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 178 | // Check if we have a loop. |
| 179 | // FIXME: Maybe make this smarter, and see whether the loops are dependent |
| 180 | // on inputs or side-effects? |
Michael Zolotukhin | 5926c6d | 2018-03-20 17:02:29 +0000 | [diff] [blame] | 181 | if (MLI->empty() && MICount < XRayThreshold) |
Dean Michael Berris | 875f0a3 | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 182 | return false; // Function is too small and has no loops. |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Dean Michael Berris | 62e99e1 | 2016-12-19 09:20:38 +0000 | [diff] [blame] | 185 | // We look for the first non-empty MachineBasicBlock, so that we can insert |
| 186 | // the function instrumentation in the appropriate place. |
Eugene Zelenko | e74c436 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 187 | auto MBI = llvm::find_if( |
| 188 | MF, [&](const MachineBasicBlock &MBB) { return !MBB.empty(); }); |
Dean Michael Berris | 62e99e1 | 2016-12-19 09:20:38 +0000 | [diff] [blame] | 189 | if (MBI == MF.end()) |
| 190 | return false; // The function is empty. |
| 191 | |
| 192 | auto *TII = MF.getSubtarget().getInstrInfo(); |
| 193 | auto &FirstMBB = *MBI; |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 194 | auto &FirstMI = *FirstMBB.begin(); |
| 195 | |
| 196 | if (!MF.getSubtarget().isXRaySupported()) { |
| 197 | FirstMI.emitError("An attempt to perform XRay instrumentation for an" |
Dean Michael Berris | 875f0a3 | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 198 | " unsupported target."); |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 199 | return false; |
| 200 | } |
| 201 | |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 202 | // First, insert an PATCHABLE_FUNCTION_ENTER as the first instruction of the |
| 203 | // MachineFunction. |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 204 | BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(), |
| 205 | TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER)); |
| 206 | |
| 207 | switch (MF.getTarget().getTargetTriple().getArch()) { |
| 208 | case Triple::ArchType::arm: |
| 209 | case Triple::ArchType::thumb: |
Dean Michael Berris | a685ab4 | 2016-11-17 05:15:37 +0000 | [diff] [blame] | 210 | case Triple::ArchType::aarch64: |
Sagar Thakur | 22d520c | 2017-02-15 10:48:11 +0000 | [diff] [blame] | 211 | case Triple::ArchType::mips: |
| 212 | case Triple::ArchType::mipsel: |
| 213 | case Triple::ArchType::mips64: |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 214 | case Triple::ArchType::mips64el: { |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 215 | // For the architectures which don't have a single return instruction |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 216 | InstrumentationOptions op; |
| 217 | op.HandleTailcall = false; |
| 218 | op.HandleAllReturns = true; |
| 219 | prependRetWithPatchableExit(MF, TII, op); |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 220 | break; |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 221 | } |
| 222 | case Triple::ArchType::ppc64le: { |
| 223 | // PPC has conditional returns. Turn them into branch and plain returns. |
| 224 | InstrumentationOptions op; |
| 225 | op.HandleTailcall = false; |
| 226 | op.HandleAllReturns = true; |
| 227 | replaceRetWithPatchableRet(MF, TII, op); |
| 228 | break; |
| 229 | } |
| 230 | default: { |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 231 | // For the architectures that have a single return instruction (such as |
| 232 | // RETQ on x86_64). |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 233 | InstrumentationOptions op; |
| 234 | op.HandleTailcall = true; |
| 235 | op.HandleAllReturns = false; |
| 236 | replaceRetWithPatchableRet(MF, TII, op); |
Dean Michael Berris | 916b366 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 237 | break; |
| 238 | } |
Tim Shen | 1f90df4 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 239 | } |
Dean Michael Berris | cee9af9 | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 240 | return true; |
| 241 | } |
| 242 | |
| 243 | char XRayInstrumentation::ID = 0; |
| 244 | char &llvm::XRayInstrumentationID = XRayInstrumentation::ID; |
Dean Michael Berris | 875f0a3 | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 245 | INITIALIZE_PASS_BEGIN(XRayInstrumentation, "xray-instrumentation", |
| 246 | "Insert XRay ops", false, false) |
| 247 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 248 | INITIALIZE_PASS_END(XRayInstrumentation, "xray-instrumentation", |
| 249 | "Insert XRay ops", false, false) |