LLVM 22.0.0git
PatchableFunction.cpp
Go to the documentation of this file.
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
21#include "llvm/Pass.h"
22#include "llvm/PassRegistry.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) {
35 }
36 bool runOnMachineFunction(MachineFunction &F) override {
37 return PatchableFunction().run(F);
38 }
39
41 return MachineFunctionProperties().setNoVRegs();
42 }
43};
44
45} // namespace
46
50 MFPropsModifier _(*this, MF);
51 if (!PatchableFunction().run(MF))
54}
55
56bool PatchableFunction::run(MachineFunction &MF) {
57 MachineBasicBlock &FirstMBB = *MF.begin();
58
59 if (MF.getFunction().hasFnAttribute("patchable-function-entry")) {
61 // The initial .loc covers PATCHABLE_FUNCTION_ENTER.
62 BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
63 TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
64 return true;
65 } else if (MF.getFunction().hasFnAttribute("patchable-function")) {
66#ifndef NDEBUG
67 Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function");
68 StringRef PatchType = PatchAttr.getValueAsString();
69 assert(PatchType == "prologue-short-redirect" && "Only possibility today!");
70#endif
71 auto *TII = MF.getSubtarget().getInstrInfo();
72 BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
73 TII->get(TargetOpcode::PATCHABLE_OP))
74 .addImm(2);
75 MF.ensureAlignment(Align(16));
76 return true;
77 }
78 return false;
79}
80
81char PatchableFunctionLegacy::ID = 0;
82char &llvm::PatchableFunctionID = PatchableFunctionLegacy::ID;
83INITIALIZE_PASS(PatchableFunctionLegacy, "patchable-function",
84 "Implement the 'patchable-function' attribute", false, false)
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const HexagonInstrInfo * TII
#define _
#define F(x, y, z)
Definition: MD5.cpp:55
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:56
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:400
A debug info location.
Definition: DebugLoc.h:124
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.cpp:762
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:727
An RAII based helper class to modify MachineFunctionProperties when running pass.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void ensureAlignment(Align A)
ensureAlignment - Make sure the function is at least A bytes aligned.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:118
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
LLVM_ABI char & PatchableFunctionID
This pass implements the "patchable-function" attribute.
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
LLVM_ABI void initializePatchableFunctionLegacyPass(PassRegistry &)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39