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" |
Reid Kleckner | 05da2fe | 2019-11-13 13:15:01 -0800 | [diff] [blame] | 21 | #include "llvm/InitializePasses.h" |
Sanjoy Das | c0441c2 | 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 | e9c32c7 | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 33 | MachineFunctionProperties getRequiredProperties() const override { |
Sanjoy Das | c0441c2 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 34 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 35 | MachineFunctionProperties::Property::NoVRegs); |
Sanjoy Das | c0441c2 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 36 | } |
| 37 | }; |
| 38 | } |
| 39 | |
Matthias Braun | 512424f | 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 | cd070cd | 2018-05-09 02:41:08 +0000 | [diff] [blame] | 52 | case TargetOpcode::DBG_LABEL: |
Matthias Braun | 512424f | 2016-07-13 16:37:29 +0000 | [diff] [blame] | 53 | return true; |
| 54 | } |
| 55 | } |
| 56 | |
Sanjoy Das | c0441c2 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 57 | bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) { |
Fangrui Song | 4d1e23e | 2020-01-03 00:35:47 -0800 | [diff] [blame] | 58 | if (MF.getFunction().hasFnAttribute("patchable-function-entry")) { |
| 59 | MachineBasicBlock &FirstMBB = *MF.begin(); |
Fangrui Song | 4d1e23e | 2020-01-03 00:35:47 -0800 | [diff] [blame] | 60 | const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
Fangrui Song | 5932f7b | 2020-02-01 13:28:19 -0800 | [diff] [blame] | 61 | // The initial .loc covers PATCHABLE_FUNCTION_ENTER. |
| 62 | BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(), |
| 63 | TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER)); |
Fangrui Song | 4d1e23e | 2020-01-03 00:35:47 -0800 | [diff] [blame] | 64 | return true; |
| 65 | } |
| 66 | |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 67 | if (!MF.getFunction().hasFnAttribute("patchable-function")) |
Sanjoy Das | c0441c2 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 68 | return false; |
| 69 | |
Charles Davis | e9c32c7 | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 70 | #ifndef NDEBUG |
Matthias Braun | f1caa28 | 2017-12-15 22:22:58 +0000 | [diff] [blame] | 71 | Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function"); |
Sanjoy Das | c0441c2 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 72 | StringRef PatchType = PatchAttr.getValueAsString(); |
Charles Davis | e9c32c7 | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 73 | assert(PatchType == "prologue-short-redirect" && "Only possibility today!"); |
| 74 | #endif |
Sanjoy Das | c0441c2 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 75 | |
| 76 | auto &FirstMBB = *MF.begin(); |
Matthias Braun | 512424f | 2016-07-13 16:37:29 +0000 | [diff] [blame] | 77 | MachineBasicBlock::iterator FirstActualI = FirstMBB.begin(); |
| 78 | for (; doesNotGeneratecode(*FirstActualI); ++FirstActualI) |
| 79 | assert(FirstActualI != FirstMBB.end()); |
Sanjoy Das | c0441c2 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 80 | |
Charles Davis | e9c32c7 | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 81 | auto *TII = MF.getSubtarget().getInstrInfo(); |
| 82 | auto MIB = BuildMI(FirstMBB, FirstActualI, FirstActualI->getDebugLoc(), |
| 83 | TII->get(TargetOpcode::PATCHABLE_OP)) |
| 84 | .addImm(2) |
| 85 | .addImm(FirstActualI->getOpcode()); |
Sanjoy Das | c0441c2 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 86 | |
Charles Davis | e9c32c7 | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 87 | for (auto &MO : FirstActualI->operands()) |
Diana Picus | 116bbab | 2017-01-13 09:58:52 +0000 | [diff] [blame] | 88 | MIB.add(MO); |
Charles Davis | e9c32c7 | 2016-08-08 21:20:15 +0000 | [diff] [blame] | 89 | |
| 90 | FirstActualI->eraseFromParent(); |
Guillaume Chatelet | 18f805a | 2019-09-27 12:54:21 +0000 | [diff] [blame] | 91 | MF.ensureAlignment(Align(16)); |
Sanjoy Das | c0441c2 | 2016-04-19 05:24:47 +0000 | [diff] [blame] | 92 | return true; |
| 93 | } |
| 94 | |
| 95 | char PatchableFunction::ID = 0; |
| 96 | char &llvm::PatchableFunctionID = PatchableFunction::ID; |
Sanjoy Das | 4519ff7 | 2016-04-19 06:25:02 +0000 | [diff] [blame] | 97 | INITIALIZE_PASS(PatchableFunction, "patchable-function", |
| 98 | "Implement the 'patchable-function' attribute", false, false) |