LLVM  10.0.0
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  if (FirstMBB.empty()) {
62  BuildMI(&FirstMBB, DebugLoc(),
63  TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
64  } else {
65  MachineInstr &FirstMI = *FirstMBB.begin();
66  BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(),
67  TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
68  }
69  return true;
70  }
71 
72  if (!MF.getFunction().hasFnAttribute("patchable-function"))
73  return false;
74 
75 #ifndef NDEBUG
76  Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function");
77  StringRef PatchType = PatchAttr.getValueAsString();
78  assert(PatchType == "prologue-short-redirect" && "Only possibility today!");
79 #endif
80 
81  auto &FirstMBB = *MF.begin();
82  MachineBasicBlock::iterator FirstActualI = FirstMBB.begin();
83  for (; doesNotGeneratecode(*FirstActualI); ++FirstActualI)
84  assert(FirstActualI != FirstMBB.end());
85 
86  auto *TII = MF.getSubtarget().getInstrInfo();
87  auto MIB = BuildMI(FirstMBB, FirstActualI, FirstActualI->getDebugLoc(),
88  TII->get(TargetOpcode::PATCHABLE_OP))
89  .addImm(2)
90  .addImm(FirstActualI->getOpcode());
91 
92  for (auto &MO : FirstActualI->operands())
93  MIB.add(MO);
94 
95  FirstActualI->eraseFromParent();
96  MF.ensureAlignment(Align(16));
97  return true;
98 }
99 
100 char PatchableFunction::ID = 0;
102 INITIALIZE_PASS(PatchableFunction, "patchable-function",
103  "Implement the 'patchable-function' attribute", false, false)
const MachineInstrBuilder & add(const MachineOperand &MO) const
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:400
void initializePatchableFunctionPass(PassRegistry &)
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:323
A debug info location.
Definition: DebugLoc.h:33
F(f)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
Definition: MachineInstr.h:426
static bool doesNotGeneratecode(const MachineInstr &MI)
Returns true if instruction MI will not result in actual machine code instructions.
virtual const TargetInstrInfo * getInstrInfo() const
TargetInstrInfo - Interface to description of machine instruction set.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
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.
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:733
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:33
const Function & getFunction() const
Return the LLVM function that this machine code represents.
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
Definition: MachineInstr.h:63
char & PatchableFunctionID
This pass implements the "patchable-function" attribute.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:220
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:44
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:333
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:54
Properties which a MachineFunction may have at a given point in time.