LLVM 24.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
22#include "llvm/Pass.h"
23#include "llvm/PassRegistry.h"
24
25using namespace llvm;
26
27namespace {
28struct PatchableFunction {
29 bool run(MachineFunction &F);
30};
31
32struct PatchableFunctionLegacy : public MachineFunctionPass {
33 static char ID;
34 PatchableFunctionLegacy() : MachineFunctionPass(ID) {}
35 bool runOnMachineFunction(MachineFunction &F) override {
36 return PatchableFunction().run(F);
37 }
38
39 MachineFunctionProperties getRequiredProperties() const override {
40 return MachineFunctionProperties().setNoVRegs();
41 }
42
43 void getAnalysisUsage(AnalysisUsage &AU) const override {
44 AU.addPreserved<MachineRegisterClassInfoWrapperPass>();
46 }
47};
48
49} // namespace
50
54 MFPropsModifier _(*this, MF);
55 if (!PatchableFunction().run(MF))
58}
59
60bool PatchableFunction::run(MachineFunction &MF) {
61 MachineBasicBlock &FirstMBB = *MF.begin();
62
63 if (MF.getFunction().hasFnAttribute("patchable-function-entry")) {
65 // The initial .loc covers PATCHABLE_FUNCTION_ENTER.
66 BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
67 TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
68 return true;
69 } else if (MF.getFunction().hasFnAttribute("patchable-function")) {
70#ifndef NDEBUG
71 Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function");
72 StringRef PatchType = PatchAttr.getValueAsString();
73 assert(PatchType == "prologue-short-redirect" && "Only possibility today!");
74#endif
75 auto *TII = MF.getSubtarget().getInstrInfo();
76 BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(),
77 TII->get(TargetOpcode::PATCHABLE_OP))
78 .addImm(2);
79 MF.ensureAlignment(Align(16));
80 return true;
81 }
82 return false;
83}
84
85char PatchableFunctionLegacy::ID = 0;
86char &llvm::PatchableFunctionID = PatchableFunctionLegacy::ID;
87INITIALIZE_PASS(PatchableFunctionLegacy, "patchable-function",
88 "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:54
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:105
LLVM_ABI StringRef getValueAsString() const
Return the attribute's value as a string.
A debug info location.
Definition DebugLoc.h:126
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...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
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.
LLVM_ABI 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
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
DXILDebugInfoMap run(Module &M)
This is an optimization pass for GlobalISel generic memory operations.
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.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39