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 14 #include "llvm/CodeGen/PatchableFunction.h" 15 #include "llvm/CodeGen/MachineFunction.h" 16 #include "llvm/CodeGen/MachineFunctionPass.h" 17 #include "llvm/CodeGen/MachineInstrBuilder.h" 18 #include "llvm/CodeGen/TargetInstrInfo.h" 19 #include "llvm/CodeGen/TargetSubtargetInfo.h" 20 #include "llvm/InitializePasses.h" 21 #include "llvm/Pass.h" 22 #include "llvm/PassRegistry.h" 23 24 using namespace llvm; 25 26 namespace { 27 struct PatchableFunction { 28 bool run(MachineFunction &F); 29 }; 30 31 struct PatchableFunctionLegacy : public MachineFunctionPass { 32 static char ID; 33 PatchableFunctionLegacy() : MachineFunctionPass(ID) { 34 initializePatchableFunctionLegacyPass(*PassRegistry::getPassRegistry()); 35 } 36 bool runOnMachineFunction(MachineFunction &F) override { 37 return PatchableFunction().run(F); 38 } 39 40 MachineFunctionProperties getRequiredProperties() const override { 41 return MachineFunctionProperties().setNoVRegs(); 42 } 43 }; 44 45 } // namespace 46 47 PreservedAnalyses 48 PatchableFunctionPass::run(MachineFunction &MF, 49 MachineFunctionAnalysisManager &MFAM) { 50 MFPropsModifier _(*this, MF); 51 if (!PatchableFunction().run(MF)) 52 return PreservedAnalyses::all(); 53 return getMachineFunctionPassPreservedAnalyses(); 54 } 55 56 bool PatchableFunction::run(MachineFunction &MF) { 57 MachineBasicBlock &FirstMBB = *MF.begin(); 58 59 if (MF.getFunction().hasFnAttribute("patchable-function-entry")) { 60 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 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 } else if (MF.getFunction().hasFnAttribute("patchable-function")) { 66 #ifndef NDEBUG 67 Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function"); 68 StringRef PatchType = PatchAttr.getValueAsString(); 69 assert(PatchType == "prologue-short-redirect" && "Only possibility today!"); 70 #endif 71 auto *TII = MF.getSubtarget().getInstrInfo(); 72 BuildMI(FirstMBB, FirstMBB.begin(), DebugLoc(), 73 TII->get(TargetOpcode::PATCHABLE_OP)) 74 .addImm(2); 75 MF.ensureAlignment(Align(16)); 76 return true; 77 } 78 return false; 79 } 80 81 char PatchableFunctionLegacy::ID = 0; 82 char &llvm::PatchableFunctionID = PatchableFunctionLegacy::ID; 83 INITIALIZE_PASS(PatchableFunctionLegacy, "patchable-function", 84 "Implement the 'patchable-function' attribute", false, false) 85