1 | //===-- PatchableFunction.cpp - Patchable prologues for LLVM -------------===// |
2 | // |
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 |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file implements edits function bodies in place to support the |
10 | // "patchable-function" attribute. |
11 | // |
12 | //===----------------------------------------------------------------------===// |
13 | |
14 | #include "llvm/CodeGen/MachineFunction.h" |
15 | #include "llvm/CodeGen/MachineFunctionPass.h" |
16 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
17 | #include "llvm/CodeGen/TargetInstrInfo.h" |
18 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
19 | #include "llvm/InitializePasses.h" |
20 | #include "llvm/Pass.h" |
21 | #include "llvm/PassRegistry.h" |
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; |
33 | MachineFunctionProperties getRequiredProperties() const override { |
34 | return MachineFunctionProperties().set( |
35 | MachineFunctionProperties::Property::NoVRegs); |
36 | } |
37 | }; |
38 | } |
39 | |
40 | bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) { |
41 | MachineBasicBlock &FirstMBB = *MF.begin(); |
42 | |
43 | if (MF.getFunction().hasFnAttribute(Kind: "patchable-function-entry" )) { |
44 | const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); |
45 | // The initial .loc covers PATCHABLE_FUNCTION_ENTER. |
46 | BuildMI(BB&: FirstMBB, I: FirstMBB.begin(), MIMD: DebugLoc(), |
47 | MCID: TII->get(Opcode: TargetOpcode::PATCHABLE_FUNCTION_ENTER)); |
48 | return true; |
49 | } else if (MF.getFunction().hasFnAttribute(Kind: "patchable-function" )) { |
50 | #ifndef NDEBUG |
51 | Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function" ); |
52 | StringRef PatchType = PatchAttr.getValueAsString(); |
53 | assert(PatchType == "prologue-short-redirect" && "Only possibility today!" ); |
54 | #endif |
55 | auto *TII = MF.getSubtarget().getInstrInfo(); |
56 | BuildMI(BB&: FirstMBB, I: FirstMBB.begin(), MIMD: DebugLoc(), |
57 | MCID: TII->get(Opcode: TargetOpcode::PATCHABLE_OP)) |
58 | .addImm(Val: 2); |
59 | MF.ensureAlignment(A: Align(16)); |
60 | return true; |
61 | } |
62 | return false; |
63 | } |
64 | |
65 | char PatchableFunction::ID = 0; |
66 | char &llvm::PatchableFunctionID = PatchableFunction::ID; |
67 | INITIALIZE_PASS(PatchableFunction, "patchable-function" , |
68 | "Implement the 'patchable-function' attribute" , false, false) |
69 | |