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" |
| 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 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | namespace { |
| 29 | struct XRayInstrumentation : public MachineFunctionPass { |
| 30 | static char ID; |
| 31 | |
| 32 | XRayInstrumentation() : MachineFunctionPass(ID) { |
| 33 | initializeXRayInstrumentationPass(*PassRegistry::getPassRegistry()); |
| 34 | } |
| 35 | |
| 36 | bool runOnMachineFunction(MachineFunction &MF) override; |
| 37 | }; |
Renato Golin | 049f387 | 2016-09-08 17:10:39 +0000 | [diff] [blame] | 38 | } |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 39 | |
Renato Golin | 049f387 | 2016-09-08 17:10:39 +0000 | [diff] [blame] | 40 | bool XRayInstrumentation::runOnMachineFunction(MachineFunction &MF) { |
| 41 | auto &F = *MF.getFunction(); |
| 42 | auto InstrAttr = F.getFnAttribute("function-instrument"); |
| 43 | bool AlwaysInstrument = !InstrAttr.hasAttribute(Attribute::None) && |
| 44 | InstrAttr.isStringAttribute() && |
| 45 | InstrAttr.getValueAsString() == "xray-always"; |
| 46 | Attribute Attr = F.getFnAttribute("xray-instruction-threshold"); |
| 47 | unsigned XRayThreshold = 0; |
| 48 | if (!AlwaysInstrument) { |
| 49 | if (Attr.hasAttribute(Attribute::None) || !Attr.isStringAttribute()) |
| 50 | return false; // XRay threshold attribute not found. |
| 51 | if (Attr.getValueAsString().getAsInteger(10, XRayThreshold)) |
| 52 | return false; // Invalid value for threshold. |
| 53 | if (F.size() < XRayThreshold) |
| 54 | return false; // Function is too small. |
| 55 | } |
| 56 | |
| 57 | // FIXME: Do the loop triviality analysis here or in an earlier pass. |
| 58 | |
| 59 | // First, insert an PATCHABLE_FUNCTION_ENTER as the first instruction of the |
| 60 | // MachineFunction. |
| 61 | auto &FirstMBB = *MF.begin(); |
| 62 | auto &FirstMI = *FirstMBB.begin(); |
| 63 | auto *TII = MF.getSubtarget().getInstrInfo(); |
| 64 | BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(), |
| 65 | TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER)); |
| 66 | |
| 67 | // Then we look for *all* terminators and returns, then replace those with |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 68 | // PATCHABLE_RET instructions. |
| 69 | SmallVector<MachineInstr *, 4> Terminators; |
| 70 | for (auto &MBB : MF) { |
| 71 | for (auto &T : MBB.terminators()) { |
Dean Michael Berris | e8ae5ba | 2016-09-01 01:29:13 +0000 | [diff] [blame] | 72 | unsigned Opc = 0; |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 73 | if (T.isReturn() && T.getOpcode() == TII->getReturnOpcode()) { |
| 74 | // Replace return instructions with: |
| 75 | // PATCHABLE_RET <Opcode>, <Operand>... |
Dean Michael Berris | e8ae5ba | 2016-09-01 01:29:13 +0000 | [diff] [blame] | 76 | Opc = TargetOpcode::PATCHABLE_RET; |
| 77 | } |
| 78 | if (TII->isTailCall(T)) { |
| 79 | // Treat the tail call as a return instruction, which has a |
| 80 | // different-looking sled than the normal return case. |
| 81 | Opc = TargetOpcode::PATCHABLE_TAIL_CALL; |
| 82 | } |
| 83 | if (Opc != 0) { |
| 84 | auto MIB = BuildMI(MBB, T, T.getDebugLoc(), TII->get(Opc)) |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 85 | .addImm(T.getOpcode()); |
| 86 | for (auto &MO : T.operands()) |
| 87 | MIB.addOperand(MO); |
| 88 | Terminators.push_back(&T); |
Dean Michael Berris | 52735fc | 2016-07-14 04:06:33 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | for (auto &I : Terminators) |
| 94 | I->eraseFromParent(); |
| 95 | |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | char XRayInstrumentation::ID = 0; |
| 100 | char &llvm::XRayInstrumentationID = XRayInstrumentation::ID; |
| 101 | INITIALIZE_PASS(XRayInstrumentation, "xray-instrumentation", "Insert XRay ops", |
Dean Michael Berris | 086639a | 2016-07-14 11:46:41 +0000 | [diff] [blame] | 102 | false, false) |