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__anonfdbc729e0111::PatchableFunction28 PatchableFunction() : MachineFunctionPass(ID) {
29 initializePatchableFunctionPass(*PassRegistry::getPassRegistry());
30 }
31
32 bool runOnMachineFunction(MachineFunction &F) override;
getRequiredProperties__anonfdbc729e0111::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 if (MF.getFunction().hasFnAttribute("patchable-function-entry")) {
42 MachineBasicBlock &FirstMBB = *MF.begin();
43 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
44 // The initial .loc covers PATCHABLE_FUNCTION_ENTER.
45 BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
46 TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
47 return true;
48 }
49
50 if (!MF.getFunction().hasFnAttribute("patchable-function"))
51 return false;
52
53 #ifndef NDEBUG
54 Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function");
55 StringRef PatchType = PatchAttr.getValueAsString();
56 assert(PatchType == "prologue-short-redirect" && "Only possibility today!");
57 #endif
58
59 auto &FirstMBB = *MF.begin();
60 auto *TII = MF.getSubtarget().getInstrInfo();
61
62 MachineBasicBlock::iterator FirstActualI = llvm::find_if(
63 FirstMBB, [](const MachineInstr &MI) { return !MI.isMetaInstruction(); });
64
65 if (FirstActualI == FirstMBB.end()) {
66 // As of Microsoft documentation on /hotpatch feature, we must ensure that
67 // "the first instruction of each function is at least two bytes, and no
68 // jump within the function goes to the first instruction"
69
70 // When the first MBB is empty, insert a patchable no-op. This ensures the
71 // first instruction is patchable in two special cases:
72 // - the function is empty (e.g. unreachable)
73 // - the function jumps back to the first instruction, which is in a
74 // successor MBB.
75 BuildMI(&FirstMBB, DebugLoc(), TII->get(TargetOpcode::PATCHABLE_OP))
76 .addImm(2)
77 .addImm(TargetOpcode::PATCHABLE_OP);
78 MF.ensureAlignment(Align(16));
79 return true;
80 }
81
82 auto MIB = BuildMI(FirstMBB, FirstActualI, FirstActualI->getDebugLoc(),
83 TII->get(TargetOpcode::PATCHABLE_OP))
84 .addImm(2)
85 .addImm(FirstActualI->getOpcode());
86
87 for (auto &MO : FirstActualI->operands())
88 MIB.add(MO);
89
90 FirstActualI->eraseFromParent();
91 MF.ensureAlignment(Align(16));
92 return true;
93 }
94
95 char PatchableFunction::ID = 0;
96 char &llvm::PatchableFunctionID = PatchableFunction::ID;
97 INITIALIZE_PASS(PatchableFunction, "patchable-function",
98 "Implement the 'patchable-function' attribute", false, false)
99