LLVM  14.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 
17 #include "llvm/CodeGen/Passes.h"
21 #include "llvm/InitializePasses.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 /// Returns true if instruction \p MI will not result in actual machine code
41 /// instructions.
42 static bool doesNotGeneratecode(const MachineInstr &MI) {
43  // TODO: Introduce an MCInstrDesc flag for this
44  switch (MI.getOpcode()) {
45  default: return false;
46  case TargetOpcode::IMPLICIT_DEF:
47  case TargetOpcode::KILL:
48  case TargetOpcode::CFI_INSTRUCTION:
50  case TargetOpcode::GC_LABEL:
51  case TargetOpcode::DBG_VALUE:
52  case TargetOpcode::DBG_LABEL:
53  return true;
54  }
55 }
56 
57 bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) {
58  if (MF.getFunction().hasFnAttribute("patchable-function-entry")) {
59  MachineBasicBlock &FirstMBB = *MF.begin();
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  }
66 
67  if (!MF.getFunction().hasFnAttribute("patchable-function"))
68  return false;
69 
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 
76  auto &FirstMBB = *MF.begin();
77  MachineBasicBlock::iterator FirstActualI = FirstMBB.begin();
78  for (; doesNotGeneratecode(*FirstActualI); ++FirstActualI)
79  assert(FirstActualI != FirstMBB.end());
80 
81  auto *TII = MF.getSubtarget().getInstrInfo();
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)
doesNotGeneratecode
static bool doesNotGeneratecode(const MachineInstr &MI)
Returns true if instruction MI will not result in actual machine code instructions.
Definition: PatchableFunction.cpp:42
MI
IRTranslator LLVM IR MI
Definition: IRTranslator.cpp:103
llvm::MachineInstrBuilder::addImm
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Definition: MachineInstrBuilder.h:131
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
TargetFrameLowering.h
llvm::MachineInstrBuilder::add
const MachineInstrBuilder & add(const MachineOperand &MO) const
Definition: MachineInstrBuilder.h:224
llvm::Attribute
Definition: Attributes.h:52
llvm::TargetSubtargetInfo::getInstrInfo
virtual const TargetInstrInfo * getInstrInfo() const
Definition: TargetSubtargetInfo.h:92
llvm::ISD::EH_LABEL
@ EH_LABEL
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:988
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:680
TargetInstrInfo.h
llvm::MachineFunctionProperties
Properties which a MachineFunction may have at a given point in time.
Definition: MachineFunction.h:111
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::TargetInstrInfo
TargetInstrInfo - Interface to description of machine instruction set.
Definition: TargetInstrInfo.h:97
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:31
INITIALIZE_PASS
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:37
TII
const HexagonInstrInfo * TII
Definition: HexagonCopyToCombine.cpp:129
llvm::Function::getFnAttribute
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.cpp:652
llvm::MachineFunctionProperties::set
MachineFunctionProperties & set(Property P)
Definition: MachineFunction.h:169
llvm::MachineFunction::begin
iterator begin()
Definition: MachineFunction.h:808
Align
uint64_t Align
Definition: ELFObjHandler.cpp:83
llvm::Attribute::getValueAsString
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:301
llvm::MachineBasicBlock
Definition: MachineBasicBlock.h:95
llvm::MachineFunctionProperties::Property::NoVRegs
@ NoVRegs
Passes.h
llvm::MachineFunction::getSubtarget
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Definition: MachineFunction.h:626
llvm::Function::hasFnAttribute
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.cpp:626
llvm::MachineInstr
Representation of each machine instruction.
Definition: MachineInstr.h:64
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:230
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
TargetSubtargetInfo.h
llvm::initializePatchableFunctionPass
void initializePatchableFunctionPass(PassRegistry &)
llvm::MachineFunction::getFunction
Function & getFunction()
Return the LLVM function that this machine code represents.
Definition: MachineFunction.h:592
llvm::MachineBasicBlock::begin
iterator begin()
Definition: MachineBasicBlock.h:268
MachineInstrBuilder.h
llvm::BuildMI
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
Definition: MachineInstrBuilder.h:328
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:270
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37