LLVM  17.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 
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
28  PatchableFunction() : MachineFunctionPass(ID) {
30  }
31 
32  bool runOnMachineFunction(MachineFunction &F) override;
33  MachineFunctionProperties getRequiredProperties() const override {
36  }
37 };
38 }
39 
40 bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) {
41  if (MF.getFunction().hasFnAttribute("patchable-function-entry")) {
42  MachineBasicBlock &FirstMBB = *MF.begin();
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 
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;
97 INITIALIZE_PASS(PatchableFunction, "patchable-function",
98  "Implement the 'patchable-function' attribute", false, false)
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:109
llvm::MachineInstrBuilder::addImm
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Definition: MachineInstrBuilder.h:131
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::AArch64PACKey::ID
ID
Definition: AArch64BaseInfo.h:824
llvm::MachineInstrBuilder::add
const MachineInstrBuilder & add(const MachineOperand &MO) const
Definition: MachineInstrBuilder.h:224
llvm::Attribute
Definition: Attributes.h:67
Pass.h
llvm::TargetSubtargetInfo::getInstrInfo
virtual const TargetInstrInfo * getInstrInfo() const
Definition: TargetSubtargetInfo.h:95
llvm::MachineFunctionPass
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
Definition: MachineFunctionPass.h:30
llvm::MachineFunction::ensureAlignment
void ensureAlignment(Align A)
ensureAlignment - Make sure the function is at least A bytes aligned.
Definition: MachineFunction.h:726
TargetInstrInfo.h
llvm::MachineFunctionProperties
Properties which a MachineFunction may have at a given point in time.
Definition: MachineFunction.h:128
PassRegistry.h
F
#define F(x, y, z)
Definition: MD5.cpp:55
llvm::TargetInstrInfo
TargetInstrInfo - Interface to description of machine instruction set.
Definition: TargetInstrInfo.h:98
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
INITIALIZE_PASS
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:125
llvm::Function::getFnAttribute
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.cpp:666
llvm::MachineFunctionProperties::set
MachineFunctionProperties & set(Property P)
Definition: MachineFunction.h:197
llvm::MachineFunction::begin
iterator begin()
Definition: MachineFunction.h:870
Align
uint64_t Align
Definition: ELFObjHandler.cpp:82
llvm::Attribute::getValueAsString
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:312
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:94
llvm::MachineFunctionProperties::Property::NoVRegs
@ NoVRegs
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:672
llvm::Function::hasFnAttribute
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:640
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:66
MachineFunctionPass.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::PatchableFunctionID
char & PatchableFunctionID
This pass implements the "patchable-function" attribute.
Definition: PatchableFunction.cpp:96
llvm::MachineFunction
Definition: MachineFunction.h:258
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
TargetSubtargetInfo.h
llvm::initializePatchableFunctionPass
void initializePatchableFunctionPass(PassRegistry &)
llvm::find_if
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1762
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:638
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:357
llvm::MachineBasicBlock::begin
iterator begin()
Definition: MachineBasicBlock.h:309
MachineInstrBuilder.h
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
MachineFunction.h
llvm::MachineInstrBundleIterator< MachineInstr >
InitializePasses.h
llvm::MachineBasicBlock::end
iterator end()
Definition: MachineBasicBlock.h:311