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
PatchableFunction__anona6494cf10111::PatchableFunction28 PatchableFunction() : MachineFunctionPass(ID) {
29 initializePatchableFunctionPass(*PassRegistry::getPassRegistry());
30 }
31
32 bool runOnMachineFunction(MachineFunction &F) override;
getRequiredProperties__anona6494cf10111::PatchableFunction33 MachineFunctionProperties getRequiredProperties() const override {
34 return MachineFunctionProperties().set(
35 MachineFunctionProperties::Property::NoVRegs);
36 }
37 };
38 }
39
runOnMachineFunction(MachineFunction & MF)40 bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) {
41 MachineBasicBlock &FirstMBB = *MF.begin();
42
43 if (MF.getFunction().hasFnAttribute("patchable-function-entry")) {
44 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
45 // The initial .loc covers PATCHABLE_FUNCTION_ENTER.
46 BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
47 TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
48 return true;
49 } else if (MF.getFunction().hasFnAttribute("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(FirstMBB, FirstMBB.begin(), DebugLoc(),
57 TII->get(TargetOpcode::PATCHABLE_OP))
58 .addImm(2);
59 MF.ensureAlignment(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