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 15 #include "llvm/CodeGen/Passes.h" 16 #include "llvm/Analysis/TargetTransformInfo.h" 17 #include "llvm/CodeGen/Analysis.h" 18 #include "llvm/CodeGen/MachineFunction.h" 19 #include "llvm/CodeGen/MachineFunctionPass.h" 20 #include "llvm/Target/TargetFrameLowering.h" 21 22 using namespace llvm; 23 24 namespace { 25 struct PatchableFunction : public MachineFunctionPass { 26 static char ID; // Pass identification, replacement for typeid 27 PatchableFunction() : MachineFunctionPass(ID) { 28 initializePatchableFunctionPass(*PassRegistry::getPassRegistry()); 29 } 30 31 void getAnalysisUsage(AnalysisUsage &AU) const override; 32 bool runOnMachineFunction(MachineFunction &F) override; 33 MachineFunctionProperties getRequiredProperties() const override { 34 return MachineFunctionProperties().set( 35 MachineFunctionProperties::Property::AllVRegsAllocated); 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: 49 case TargetOpcode::EH_LABEL: 50 case TargetOpcode::GC_LABEL: 51 case TargetOpcode::DBG_VALUE: 52 return true; 53 } 54 } 55 56 void PatchableFunction::getAnalysisUsage(AnalysisUsage &AU) const { 57 MachineFunctionPass::getAnalysisUsage(AU); 58 AU.addRequired<TargetTransformInfoWrapperPass>(); 59 } 60 61 bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) { 62 if (!MF.getFunction()->hasFnAttribute("patchable-function")) 63 return false; 64 65 Attribute PatchAttr = MF.getFunction()->getFnAttribute("patchable-function"); 66 StringRef PatchType = PatchAttr.getValueAsString(); 67 assert((PatchType == "prologue-short-redirect" || 68 PatchType == "ms-hotpatch") && "Only possibilities today!"); 69 70 auto &FirstMBB = *MF.begin(); 71 MachineBasicBlock::iterator FirstActualI = FirstMBB.begin(); 72 for (; doesNotGeneratecode(*FirstActualI); ++FirstActualI) 73 assert(FirstActualI != FirstMBB.end()); 74 75 const TargetTransformInfo &TTI = 76 getAnalysis<TargetTransformInfoWrapperPass>().getTTI(*MF.getFunction()); 77 TTI.emitPatchableOp(PatchType, FirstMBB, FirstActualI); 78 79 MF.ensureAlignment(4); 80 return true; 81 } 82 83 char PatchableFunction::ID = 0; 84 char &llvm::PatchableFunctionID = PatchableFunction::ID; 85 INITIALIZE_PASS(PatchableFunction, "patchable-function", 86 "Implement the 'patchable-function' attribute", false, false) 87