LLVM  6.0.0svn
PatchableFunction.cpp
Go to the documentation of this file.
1 //===-- PatchableFunction.cpp - Patchable prologues for LLVM -------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements edits function bodies in place to support the
11 // "patchable-function" attribute.
12 //
13 //===----------------------------------------------------------------------===//
14 
18 #include "llvm/CodeGen/Passes.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  return true;
53  }
54 }
55 
56 bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) {
57  if (!MF.getFunction()->hasFnAttribute("patchable-function"))
58  return false;
59 
60 #ifndef NDEBUG
61  Attribute PatchAttr = MF.getFunction()->getFnAttribute("patchable-function");
62  StringRef PatchType = PatchAttr.getValueAsString();
63  assert(PatchType == "prologue-short-redirect" && "Only possibility today!");
64 #endif
65 
66  auto &FirstMBB = *MF.begin();
67  MachineBasicBlock::iterator FirstActualI = FirstMBB.begin();
68  for (; doesNotGeneratecode(*FirstActualI); ++FirstActualI)
69  assert(FirstActualI != FirstMBB.end());
70 
71  auto *TII = MF.getSubtarget().getInstrInfo();
72  auto MIB = BuildMI(FirstMBB, FirstActualI, FirstActualI->getDebugLoc(),
73  TII->get(TargetOpcode::PATCHABLE_OP))
74  .addImm(2)
75  .addImm(FirstActualI->getOpcode());
76 
77  for (auto &MO : FirstActualI->operands())
78  MIB.add(MO);
79 
80  FirstActualI->eraseFromParent();
81  MF.ensureAlignment(4);
82  return true;
83 }
84 
85 char PatchableFunction::ID = 0;
87 INITIALIZE_PASS(PatchableFunction, "patchable-function",
88  "Implement the 'patchable-function' attribute", false, false)
const MachineInstrBuilder & add(const MachineOperand &MO) const
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
void initializePatchableFunctionPass(PassRegistry &)
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:262
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:290
static bool doesNotGeneratecode(const MachineInstr &MI)
Returns true if instruction MI will not result in actual machine code instructions.
virtual const TargetInstrInfo * getInstrInfo() const
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(unsigned A)
ensureAlignment - Make sure the function is at least 1 << A bytes aligned.
EH_LABEL - Represents a label in mid basic block used to track locations needed for debug and excepti...
Definition: ISDOpcodes.h:639
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:34
MachineFunctionProperties & set(Property P)
Representation of each machine instruction.
Definition: MachineInstr.h:59
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&#39;s value as a string.
Definition: Attributes.cpp:195
const Function * getFunction() const
getFunction - Return the LLVM function that this machine code represents
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:270
IRTranslator LLVM IR MI
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Properties which a MachineFunction may have at a given point in time.