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