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/PatchableFunction.h"
15#include "llvm/CodeGen/MachineFunction.h"
16#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/CodeGen/MachineInstrBuilder.h"
18#include "llvm/CodeGen/RegisterClassInfo.h"
19#include "llvm/CodeGen/TargetInstrInfo.h"
20#include "llvm/CodeGen/TargetSubtargetInfo.h"
21#include "llvm/InitializePasses.h"
22#include "llvm/Pass.h"
23
24using namespace llvm;
25
26namespace {
27struct PatchableFunction {
28 bool run(MachineFunction &F);
29};
30
31struct PatchableFunctionLegacy : public MachineFunctionPass {
32 static char ID;
33 PatchableFunctionLegacy() : MachineFunctionPass(ID) {}
34 bool runOnMachineFunction(MachineFunction &F) override {
35 return PatchableFunction().run(F);
36 }
37
38 MachineFunctionProperties getRequiredProperties() const override {
39 return MachineFunctionProperties().setNoVRegs();
40 }
41
42 void getAnalysisUsage(AnalysisUsage &AU) const override {
43 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
44 MachineFunctionPass::getAnalysisUsage(AU);
45 }
46};
47
48} // namespace
49
50PreservedAnalyses
51PatchableFunctionPass::run(MachineFunction &MF,
52 MachineFunctionAnalysisManager &MFAM) {
53 MFPropsModifier _(*this, MF);
54 if (!PatchableFunction().run(F&: MF))
55 return PreservedAnalyses::all();
56 return getMachineFunctionPassPreservedAnalyses();
57}
58
59bool PatchableFunction::run(MachineFunction &MF) {
60 MachineBasicBlock &FirstMBB = *MF.begin();
61
62 if (MF.getFunction().hasFnAttribute(Kind: "patchable-function-entry")) {
63 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
64 // The initial .loc covers PATCHABLE_FUNCTION_ENTER.
65 BuildMI(BB&: FirstMBB, I: FirstMBB.begin(), MIMD: DebugLoc(),
66 MCID: TII->get(Opcode: TargetOpcode::PATCHABLE_FUNCTION_ENTER));
67 return true;
68 } else if (MF.getFunction().hasFnAttribute(Kind: "patchable-function")) {
69#ifndef NDEBUG
70 Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function");
71 StringRef PatchType = PatchAttr.getValueAsString();
72 assert(PatchType == "prologue-short-redirect" && "Only possibility today!");
73#endif
74 auto *TII = MF.getSubtarget().getInstrInfo();
75 BuildMI(BB&: FirstMBB, I: FirstMBB.begin(), MIMD: DebugLoc(),
76 MCID: TII->get(Opcode: TargetOpcode::PATCHABLE_OP))
77 .addImm(Val: 2);
78 MF.ensureAlignment(A: Align(16));
79 return true;
80 }
81 return false;
82}
83
84char PatchableFunctionLegacy::ID = 0;
85char &llvm::PatchableFunctionID = PatchableFunctionLegacy::ID;
86INITIALIZE_PASS(PatchableFunctionLegacy, "patchable-function",
87 "Implement the 'patchable-function' attribute", false, false)
88