Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 1 | //===- XRayInstrumentation.cpp - Adds XRay instrumentation to functions. --===// |
Dean Michael Berris | 52735fc | 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 | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/STLExtras.h" |
Dean Michael Berris | 711dec2 | 2017-09-08 01:47:56 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/Triple.h" |
| 20 | #include "llvm/CodeGen/MachineBasicBlock.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineDominators.h" |
Dean Michael Berris | 52735fc | 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 | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/MachineLoopInfo.h" |
David Blaikie | 3f833ed | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 26 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 27 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Eugene Zelenko | fb69e66 | 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 | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetMachine.h" |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace llvm; |
| 34 | |
| 35 | namespace { |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 36 | |
Tim Shen | cee7536 | 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 | 52735fc | 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 | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 53 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 54 | AU.setPreservesCFG(); |
| 55 | AU.addRequired<MachineLoopInfo>(); |
| 56 | AU.addPreserved<MachineLoopInfo>(); |
| 57 | AU.addPreserved<MachineDominatorTree>(); |
| 58 | MachineFunctionPass::getAnalysisUsage(AU); |
| 59 | } |
| 60 | |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 61 | bool runOnMachineFunction(MachineFunction &MF) override; |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 62 | |
| 63 | private: |
| 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, |
| 69 | // like x86/x86_64. |
| 70 | void replaceRetWithPatchableRet(MachineFunction &MF, |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 71 | const TargetInstrInfo *TII, |
| 72 | InstrumentationOptions); |
Serge Rogatch | a331133 | 2016-11-24 18:51:47 +0000 | [diff] [blame] | 73 | |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 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, |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 83 | const TargetInstrInfo *TII, |
| 84 | InstrumentationOptions); |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 85 | }; |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 86 | |
| 87 | } // end anonymous namespace |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 88 | |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 89 | void XRayInstrumentation::replaceRetWithPatchableRet( |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 90 | MachineFunction &MF, const TargetInstrInfo *TII, |
| 91 | InstrumentationOptions op) { |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 92 | // We look for *all* terminators and returns, then replace those with |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 93 | // PATCHABLE_RET instructions. |
| 94 | SmallVector<MachineInstr *, 4> Terminators; |
| 95 | for (auto &MBB : MF) { |
| 96 | for (auto &T : MBB.terminators()) { |
Dean Michael Berris | e8ae5ba | 2016-09-01 01:29:13 +0000 | [diff] [blame] | 97 | unsigned Opc = 0; |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 98 | if (T.isReturn() && |
| 99 | (op.HandleAllReturns || T.getOpcode() == TII->getReturnOpcode())) { |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 100 | // Replace return instructions with: |
| 101 | // PATCHABLE_RET <Opcode>, <Operand>... |
Dean Michael Berris | e8ae5ba | 2016-09-01 01:29:13 +0000 | [diff] [blame] | 102 | Opc = TargetOpcode::PATCHABLE_RET; |
| 103 | } |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 104 | if (TII->isTailCall(T) && op.HandleTailcall) { |
Dean Michael Berris | e8ae5ba | 2016-09-01 01:29:13 +0000 | [diff] [blame] | 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; |
| 108 | } |
| 109 | if (Opc != 0) { |
| 110 | auto MIB = BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc)) |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 111 | .addImm(T.getOpcode()); |
| 112 | for (auto &MO : T.operands()) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 113 | MIB.add(MO); |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 114 | Terminators.push_back(&T); |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | for (auto &I : Terminators) |
| 120 | I->eraseFromParent(); |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 121 | } |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 122 | |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 123 | void XRayInstrumentation::prependRetWithPatchableExit( |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 124 | MachineFunction &MF, const TargetInstrInfo *TII, |
| 125 | InstrumentationOptions op) { |
Dean Michael Berris | 711dec2 | 2017-09-08 01:47:56 +0000 | [diff] [blame] | 126 | for (auto &MBB : MF) |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 127 | for (auto &T : MBB.terminators()) { |
| 128 | unsigned Opc = 0; |
| 129 | if (T.isReturn() && |
| 130 | (op.HandleAllReturns || T.getOpcode() == TII->getReturnOpcode())) { |
| 131 | Opc = TargetOpcode::PATCHABLE_FUNCTION_EXIT; |
Dean Michael Berris | 156f6ca | 2016-10-18 05:54:15 +0000 | [diff] [blame] | 132 | } |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 133 | if (TII->isTailCall(T) && op.HandleTailcall) { |
| 134 | Opc = TargetOpcode::PATCHABLE_TAIL_CALL; |
| 135 | } |
| 136 | if (Opc != 0) { |
| 137 | // Prepend the return instruction with PATCHABLE_FUNCTION_EXIT or |
| 138 | // PATCHABLE_TAIL_CALL . |
| 139 | BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc)); |
| 140 | } |
| 141 | } |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 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. |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 157 | |
Serge Rogatch | 85427c0 | 2017-06-09 13:23:23 +0000 | [diff] [blame] | 158 | // Count the number of MachineInstr`s in MachineFunction |
Dimitry Andric | e4b9745 | 2017-07-14 21:14:58 +0000 | [diff] [blame] | 159 | int64_t MICount = 0; |
Dean Michael Berris | 711dec2 | 2017-09-08 01:47:56 +0000 | [diff] [blame] | 160 | for (const auto &MBB : MF) |
Dimitry Andric | e4b9745 | 2017-07-14 21:14:58 +0000 | [diff] [blame] | 161 | MICount += MBB.size(); |
Serge Rogatch | 85427c0 | 2017-06-09 13:23:23 +0000 | [diff] [blame] | 162 | |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 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>(); |
Serge Rogatch | 85427c0 | 2017-06-09 13:23:23 +0000 | [diff] [blame] | 167 | if (MLI.empty() && MICount < XRayThreshold) |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 168 | return false; // Function is too small and has no loops. |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Dean Michael Berris | 03b8be5 | 2016-12-19 09:20:38 +0000 | [diff] [blame] | 171 | // We look for the first non-empty MachineBasicBlock, so that we can insert |
| 172 | // the function instrumentation in the appropriate place. |
Eugene Zelenko | fb69e66 | 2017-06-06 22:22:41 +0000 | [diff] [blame] | 173 | auto MBI = llvm::find_if( |
| 174 | MF, [&](const MachineBasicBlock &MBB) { return !MBB.empty(); }); |
Dean Michael Berris | 03b8be5 | 2016-12-19 09:20:38 +0000 | [diff] [blame] | 175 | if (MBI == MF.end()) |
| 176 | return false; // The function is empty. |
| 177 | |
| 178 | auto *TII = MF.getSubtarget().getInstrInfo(); |
| 179 | auto &FirstMBB = *MBI; |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 180 | auto &FirstMI = *FirstMBB.begin(); |
| 181 | |
| 182 | if (!MF.getSubtarget().isXRaySupported()) { |
| 183 | FirstMI.emitError("An attempt to perform XRay instrumentation for an" |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 184 | " unsupported target."); |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 185 | return false; |
| 186 | } |
| 187 | |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 188 | // First, insert an PATCHABLE_FUNCTION_ENTER as the first instruction of the |
| 189 | // MachineFunction. |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 190 | BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(), |
| 191 | TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER)); |
| 192 | |
| 193 | switch (MF.getTarget().getTargetTriple().getArch()) { |
| 194 | case Triple::ArchType::arm: |
| 195 | case Triple::ArchType::thumb: |
Dean Michael Berris | 3234d3a | 2016-11-17 05:15:37 +0000 | [diff] [blame] | 196 | case Triple::ArchType::aarch64: |
Sagar Thakur | ec65792 | 2017-02-15 10:48:11 +0000 | [diff] [blame] | 197 | case Triple::ArchType::mips: |
| 198 | case Triple::ArchType::mipsel: |
| 199 | case Triple::ArchType::mips64: |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 200 | case Triple::ArchType::mips64el: { |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 201 | // For the architectures which don't have a single return instruction |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 202 | InstrumentationOptions op; |
| 203 | op.HandleTailcall = false; |
| 204 | op.HandleAllReturns = true; |
| 205 | prependRetWithPatchableExit(MF, TII, op); |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 206 | break; |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 207 | } |
| 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); |
| 214 | break; |
| 215 | } |
| 216 | default: { |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 217 | // For the architectures that have a single return instruction (such as |
| 218 | // RETQ on x86_64). |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 219 | InstrumentationOptions op; |
| 220 | op.HandleTailcall = true; |
| 221 | op.HandleAllReturns = false; |
| 222 | replaceRetWithPatchableRet(MF, TII, op); |
Dean Michael Berris | 46401544 | 2016-09-19 00:54:35 +0000 | [diff] [blame] | 223 | break; |
| 224 | } |
Tim Shen | cee7536 | 2017-09-22 18:30:02 +0000 | [diff] [blame] | 225 | } |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 226 | return true; |
| 227 | } |
| 228 | |
| 229 | char XRayInstrumentation::ID = 0; |
| 230 | char &llvm::XRayInstrumentationID = XRayInstrumentation::ID; |
Dean Michael Berris | 22f2bcf | 2017-05-04 01:24:26 +0000 | [diff] [blame] | 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) |