Sanjoy Das | 1513285 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 1 | //===-- PatchableFunction.cpp - Patchable prologues for LLVM -------------===// |
| 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 edits function bodies in place to support the |
| 11 | // "patchable-function" attribute. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Sanjoy Das | 1513285 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/MachineFunction.h" |
| 16 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Charles Davis | d77fdcb | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "llvm/CodeGen/Passes.h" |
David Blaikie | 803f827 | 2017-11-03 22:32:11 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/TargetFrameLowering.h" |
David Blaikie | 4831923 | 2017-11-08 01:01:31 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/TargetInstrInfo.h" |
David Blaikie | e3a9b4c | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Sanjoy Das | 1513285 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
| 26 | struct PatchableFunction : public MachineFunctionPass { |
| 27 | static char ID; // Pass identification, replacement for typeid |
| 28 | PatchableFunction() : MachineFunctionPass(ID) { |
| 29 | initializePatchableFunctionPass(*PassRegistry::getPassRegistry()); |
| 30 | } |
| 31 | |
| 32 | bool runOnMachineFunction(MachineFunction &F) override; |
Charles Davis | d77fdcb | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 33 | MachineFunctionProperties getRequiredProperties() const override { |
Sanjoy Das | 1513285 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 34 | return MachineFunctionProperties().set( |
Matthias Braun | 690a3cb | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 35 | MachineFunctionProperties::Property::NoVRegs); |
Sanjoy Das | 1513285 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 36 | } |
| 37 | }; |
| 38 | } |
| 39 | |
Matthias Braun | e01c096 | 2016-07-13 16:37:29 +0000 | [diff] [blame] | 40 | /// Returns true if instruction \p MI will not result in actual machine code |
| 41 | /// instructions. |
| 42 | static bool doesNotGeneratecode(const MachineInstr &MI) { |
| 43 | // TODO: Introduce an MCInstrDesc flag for this |
| 44 | switch (MI.getOpcode()) { |
| 45 | default: return false; |
| 46 | case TargetOpcode::IMPLICIT_DEF: |
| 47 | case TargetOpcode::KILL: |
| 48 | case TargetOpcode::CFI_INSTRUCTION: |
| 49 | case TargetOpcode::EH_LABEL: |
| 50 | case TargetOpcode::GC_LABEL: |
| 51 | case TargetOpcode::DBG_VALUE: |
Shiva Chen | 0853422 | 2018-05-09 02:41:08 +0000 | [diff] [blame] | 52 | case TargetOpcode::DBG_LABEL: |
Matthias Braun | e01c096 | 2016-07-13 16:37:29 +0000 | [diff] [blame] | 53 | return true; |
| 54 | } |
| 55 | } |
| 56 | |
Sanjoy Das | 1513285 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 57 | bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) { |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 58 | if (!MF.getFunction().hasFnAttribute("patchable-function")) |
Sanjoy Das | 1513285 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 59 | return false; |
| 60 | |
Charles Davis | d77fdcb | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 61 | #ifndef NDEBUG |
Matthias Braun | d318139 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 62 | Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function"); |
Sanjoy Das | 1513285 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 63 | StringRef PatchType = PatchAttr.getValueAsString(); |
Charles Davis | d77fdcb | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 64 | assert(PatchType == "prologue-short-redirect" && "Only possibility today!"); |
| 65 | #endif |
Sanjoy Das | 1513285 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 66 | |
| 67 | auto &FirstMBB = *MF.begin(); |
Matthias Braun | e01c096 | 2016-07-13 16:37:29 +0000 | [diff] [blame] | 68 | MachineBasicBlock::iterator FirstActualI = FirstMBB.begin(); |
| 69 | for (; doesNotGeneratecode(*FirstActualI); ++FirstActualI) |
| 70 | assert(FirstActualI != FirstMBB.end()); |
Sanjoy Das | 1513285 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 71 | |
Charles Davis | d77fdcb | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 72 | auto *TII = MF.getSubtarget().getInstrInfo(); |
| 73 | auto MIB = BuildMI(FirstMBB, FirstActualI, FirstActualI->getDebugLoc(), |
| 74 | TII->get(TargetOpcode::PATCHABLE_OP)) |
| 75 | .addImm(2) |
| 76 | .addImm(FirstActualI->getOpcode()); |
Sanjoy Das | 1513285 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 77 | |
Charles Davis | d77fdcb | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 78 | for (auto &MO : FirstActualI->operands()) |
Diana Picus | 8a47810 | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 79 | MIB.add(MO); |
Charles Davis | d77fdcb | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 80 | |
| 81 | FirstActualI->eraseFromParent(); |
Sanjoy Das | 1513285 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 82 | MF.ensureAlignment(4); |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | char PatchableFunction::ID = 0; |
| 87 | char &llvm::PatchableFunctionID = PatchableFunction::ID; |
Sanjoy Das | a1ff4a6 | 2016-04-19 06:25:02 +0000 | [diff] [blame] | 88 | INITIALIZE_PASS(PatchableFunction, "patchable-function", |
| 89 | "Implement the 'patchable-function' attribute", false, false) |