Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 1 | //===-- 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" |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineLoopInfo.h" |
| 22 | #include "llvm/CodeGen/MachineDominators.h" |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/Passes.h" |
| 24 | #include "llvm/Support/TargetRegistry.h" |
| 25 | #include "llvm/Target/TargetInstrInfo.h" |
| 26 | #include "llvm/Target/TargetSubtargetInfo.h" |
| 27 | |
| 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
| 31 | struct XRayInstrumentation : public MachineFunctionPass { |
| 32 | static char ID; |
| 33 | |
| 34 | XRayInstrumentation() : MachineFunctionPass(ID) { |
| 35 | initializeXRayInstrumentationPass(*PassRegistry::getPassRegistry()); |
| 36 | } |
| 37 | |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 38 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 39 | AU.setPreservesCFG(); |
| 40 | AU.addRequired<MachineLoopInfo>(); |
| 41 | AU.addPreserved<MachineLoopInfo>(); |
| 42 | AU.addPreserved<MachineDominatorTree>(); |
| 43 | MachineFunctionPass::getAnalysisUsage(AU); |
| 44 | } |
| 45 | |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 46 | bool runOnMachineFunction(MachineFunction &MF) override; |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | // Replace the original RET instruction with the exit sled code ("patchable |
| 50 | // ret" pseudo-instruction), so that at runtime XRay can replace the sled |
| 51 | // with a code jumping to XRay trampoline, which calls the tracing handler |
| 52 | // and, in the end, issues the RET instruction. |
| 53 | // This is the approach to go on CPUs which have a single RET instruction, |
| 54 | // like x86/x86_64. |
| 55 | void replaceRetWithPatchableRet(MachineFunction &MF, |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 56 | const TargetInstrInfo *TII); |
Serge Rogatch | a331133 | 2016-11-24 18:51:47 +0000 | [diff] [blame] | 57 | |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 58 | // Prepend the original return instruction with the exit sled code ("patchable |
| 59 | // function exit" pseudo-instruction), preserving the original return |
| 60 | // instruction just after the exit sled code. |
| 61 | // This is the approach to go on CPUs which have multiple options for the |
| 62 | // return instruction, like ARM. For such CPUs we can't just jump into the |
| 63 | // XRay trampoline and issue a single return instruction there. We rather |
| 64 | // have to call the trampoline and return from it to the original return |
| 65 | // instruction of the function being instrumented. |
| 66 | void prependRetWithPatchableExit(MachineFunction &MF, |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 67 | const TargetInstrInfo *TII); |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 68 | }; |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 69 | } // anonymous namespace |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 70 | |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 71 | void XRayInstrumentation::replaceRetWithPatchableRet( |
| 72 | MachineFunction &MF, const TargetInstrInfo *TII) { |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 73 | // We look for *all* terminators and returns, then replace those with |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 74 | // PATCHABLE_RET instructions. |
| 75 | SmallVector<MachineInstr *, 4> Terminators; |
| 76 | for (auto &MBB : MF) { |
| 77 | for (auto &T : MBB.terminators()) { |
Dean Michael Berris | e8ae5ba | 2016-09-01 01:29:13 +0000 | [diff] [blame] | 78 | unsigned Opc = 0; |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 79 | if (T.isReturn() && T.getOpcode() == TII->getReturnOpcode()) { |
| 80 | // Replace return instructions with: |
| 81 | // PATCHABLE_RET <Opcode>, <Operand>... |
Dean Michael Berris | e8ae5ba | 2016-09-01 01:29:13 +0000 | [diff] [blame] | 82 | Opc = TargetOpcode::PATCHABLE_RET; |
| 83 | } |
| 84 | if (TII->isTailCall(T)) { |
| 85 | // Treat the tail call as a return instruction, which has a |
| 86 | // different-looking sled than the normal return case. |
| 87 | Opc = TargetOpcode::PATCHABLE_TAIL_CALL; |
| 88 | } |
| 89 | if (Opc != 0) { |
| 90 | auto MIB = BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc)) |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 91 | .addImm(T.getOpcode()); |
| 92 | for (auto &MO : T.operands()) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 93 | MIB.add(MO); |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 94 | Terminators.push_back(&T); |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | for (auto &I : Terminators) |
| 100 | I->eraseFromParent(); |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 101 | } |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 102 | |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 103 | void XRayInstrumentation::prependRetWithPatchableExit( |
| 104 | MachineFunction &MF, const TargetInstrInfo *TII) { |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 105 | for (auto &MBB : MF) { |
| 106 | for (auto &T : MBB.terminators()) { |
Dean Michael Berris | 156f6ca | 2016-10-18 05:54:15 +0000 | [diff] [blame] | 107 | unsigned Opc = 0; |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 108 | if (T.isReturn()) { |
Dean Michael Berris | 156f6ca | 2016-10-18 05:54:15 +0000 | [diff] [blame] | 109 | Opc = TargetOpcode::PATCHABLE_FUNCTION_EXIT; |
| 110 | } |
| 111 | if (TII->isTailCall(T)) { |
| 112 | Opc = TargetOpcode::PATCHABLE_TAIL_CALL; |
| 113 | } |
| 114 | if (Opc != 0) { |
| 115 | // Prepend the return instruction with PATCHABLE_FUNCTION_EXIT or |
| 116 | // PATCHABLE_TAIL_CALL . |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 117 | BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc)); |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) { |
| 124 | auto &F = *MF.getFunction(); |
| 125 | auto InstrAttr = F.getFnAttribute("function-instrument"); |
| 126 | bool AlwaysInstrument = !InstrAttr.hasAttribute(Attribute::None) && |
| 127 | InstrAttr.isStringAttribute() && |
| 128 | InstrAttr.getValueAsString() == "xray-always"; |
| 129 | Attribute Attr = F.getFnAttribute("xray-instruction-threshold"); |
| 130 | unsigned XRayThreshold = 0; |
| 131 | if (!AlwaysInstrument) { |
| 132 | if (Attr.hasAttribute(Attribute::None) || !Attr.isStringAttribute()) |
| 133 | return false; // XRay threshold attribute not found. |
| 134 | if (Attr.getValueAsString().getAsInteger(10, XRayThreshold)) |
| 135 | return false; // Invalid value for threshold. |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 136 | |
| 137 | // Check if we have a loop. |
| 138 | // FIXME: Maybe make this smarter, and see whether the loops are dependent |
| 139 | // on inputs or side-effects? |
| 140 | MachineLoopInfo &MLI = getAnalysis<MachineLoopInfo>(); |
| 141 | if (MLI.empty() && F.size() < XRayThreshold) |
| 142 | return false; // Function is too small and has no loops. |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Dean Michael Berris | 03b8be5 | 2016-12-19 09:20:38 +0000 | [diff] [blame] | 145 | // We look for the first non-empty MachineBasicBlock, so that we can insert |
| 146 | // the function instrumentation in the appropriate place. |
| 147 | auto MBI = |
| 148 | find_if(MF, [&](const MachineBasicBlock &MBB) { return !MBB.empty(); }); |
| 149 | if (MBI == MF.end()) |
| 150 | return false; // The function is empty. |
| 151 | |
| 152 | auto *TII = MF.getSubtarget().getInstrInfo(); |
| 153 | auto &FirstMBB = *MBI; |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 154 | auto &FirstMI = *FirstMBB.begin(); |
| 155 | |
| 156 | if (!MF.getSubtarget().isXRaySupported()) { |
| 157 | FirstMI.emitError("An attempt to perform XRay instrumentation for an" |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 158 | " unsupported target."); |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 159 | return false; |
| 160 | } |
| 161 | |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 162 | // First, insert an PATCHABLE_FUNCTION_ENTER as the first instruction of the |
| 163 | // MachineFunction. |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 164 | BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(), |
| 165 | TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER)); |
| 166 | |
| 167 | switch (MF.getTarget().getTargetTriple().getArch()) { |
| 168 | case Triple::ArchType::arm: |
| 169 | case Triple::ArchType::thumb: |
Dean Michael Berris | 3234d3a | 2016-11-17 05:15:37 +0000 | [diff] [blame] | 170 | case Triple::ArchType::aarch64: |
Tim Shen | 918ed87 | 2017-02-10 21:03:24 +0000 | [diff] [blame] | 171 | case Triple::ArchType::ppc64le: |
Sagar Thakur | ec65792 | 2017-02-15 10:48:11 +0000 | [diff] [blame] | 172 | case Triple::ArchType::mips: |
| 173 | case Triple::ArchType::mipsel: |
| 174 | case Triple::ArchType::mips64: |
| 175 | case Triple::ArchType::mips64el: |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 176 | // For the architectures which don't have a single return instruction |
| 177 | prependRetWithPatchableExit(MF, TII); |
| 178 | break; |
| 179 | default: |
| 180 | // For the architectures that have a single return instruction (such as |
| 181 | // RETQ on x86_64). |
| 182 | replaceRetWithPatchableRet(MF, TII); |
| 183 | break; |
| 184 | } |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 185 | return true; |
| 186 | } |
| 187 | |
| 188 | char XRayInstrumentation::ID = 0; |
| 189 | char &llvm::XRayInstrumentationID = XRayInstrumentation::ID; |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 190 | INITIALIZE_PASS_BEGIN(XRayInstrumentation, "xray-instrumentation", |
| 191 | "Insert XRay ops", false, false) |
| 192 | INITIALIZE_PASS_DEPENDENCY(MachineLoopInfo) |
| 193 | INITIALIZE_PASS_END(XRayInstrumentation, "xray-instrumentation", |
| 194 | "Insert XRay ops", false, false) |