1c0441c29SSanjoy Das //===-- PatchableFunction.cpp - Patchable prologues for LLVM -------------===//
2c0441c29SSanjoy Das //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6c0441c29SSanjoy Das //
7c0441c29SSanjoy Das //===----------------------------------------------------------------------===//
8c0441c29SSanjoy Das //
9c0441c29SSanjoy Das // This file implements edits function bodies in place to support the
10c0441c29SSanjoy Das // "patchable-function" attribute.
11c0441c29SSanjoy Das //
12c0441c29SSanjoy Das //===----------------------------------------------------------------------===//
13c0441c29SSanjoy Das 
14c0441c29SSanjoy Das #include "llvm/CodeGen/MachineFunction.h"
15c0441c29SSanjoy Das #include "llvm/CodeGen/MachineFunctionPass.h"
16e9c32c7eSCharles Davis #include "llvm/CodeGen/MachineInstrBuilder.h"
176bda14b3SChandler Carruth #include "llvm/CodeGen/Passes.h"
181be62f03SDavid Blaikie #include "llvm/CodeGen/TargetFrameLowering.h"
193f833edcSDavid Blaikie #include "llvm/CodeGen/TargetInstrInfo.h"
20b3bde2eaSDavid Blaikie #include "llvm/CodeGen/TargetSubtargetInfo.h"
2105da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
22c0441c29SSanjoy Das 
23c0441c29SSanjoy Das using namespace llvm;
24c0441c29SSanjoy Das 
25c0441c29SSanjoy Das namespace {
26c0441c29SSanjoy Das struct PatchableFunction : public MachineFunctionPass {
27c0441c29SSanjoy Das   static char ID; // Pass identification, replacement for typeid
28c0441c29SSanjoy Das   PatchableFunction() : MachineFunctionPass(ID) {
29c0441c29SSanjoy Das     initializePatchableFunctionPass(*PassRegistry::getPassRegistry());
30c0441c29SSanjoy Das   }
31c0441c29SSanjoy Das 
32c0441c29SSanjoy Das   bool runOnMachineFunction(MachineFunction &F) override;
33c0441c29SSanjoy Das    MachineFunctionProperties getRequiredProperties() const override {
34c0441c29SSanjoy Das     return MachineFunctionProperties().set(
351eb47368SMatthias Braun         MachineFunctionProperties::Property::NoVRegs);
36c0441c29SSanjoy Das   }
37c0441c29SSanjoy Das };
38c0441c29SSanjoy Das }
39c0441c29SSanjoy Das 
40512424f2SMatthias Braun /// Returns true if instruction \p MI will not result in actual machine code
41512424f2SMatthias Braun /// instructions.
42512424f2SMatthias Braun static bool doesNotGeneratecode(const MachineInstr &MI) {
43512424f2SMatthias Braun   // TODO: Introduce an MCInstrDesc flag for this
44512424f2SMatthias Braun   switch (MI.getOpcode()) {
45512424f2SMatthias Braun   default: return false;
46512424f2SMatthias Braun   case TargetOpcode::IMPLICIT_DEF:
47512424f2SMatthias Braun   case TargetOpcode::KILL:
48512424f2SMatthias Braun   case TargetOpcode::CFI_INSTRUCTION:
49512424f2SMatthias Braun   case TargetOpcode::EH_LABEL:
50512424f2SMatthias Braun   case TargetOpcode::GC_LABEL:
51512424f2SMatthias Braun   case TargetOpcode::DBG_VALUE:
52cd070cdcSShiva Chen   case TargetOpcode::DBG_LABEL:
53512424f2SMatthias Braun     return true;
54512424f2SMatthias Braun   }
55512424f2SMatthias Braun }
56512424f2SMatthias Braun 
57c0441c29SSanjoy Das bool PatchableFunction::runOnMachineFunction(MachineFunction &MF) {
584d1e23e3SFangrui Song   if (MF.getFunction().hasFnAttribute("patchable-function-entry")) {
594d1e23e3SFangrui Song     MachineBasicBlock &FirstMBB = *MF.begin();
604d1e23e3SFangrui Song     const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
61*50a3ff30SFangrui Song     if (FirstMBB.empty()) {
62*50a3ff30SFangrui Song       BuildMI(&FirstMBB, DebugLoc(),
63*50a3ff30SFangrui Song               TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
64*50a3ff30SFangrui Song     } else {
65*50a3ff30SFangrui Song       MachineInstr &FirstMI = *FirstMBB.begin();
664d1e23e3SFangrui Song       BuildMI(FirstMBB, FirstMI, FirstMI.getDebugLoc(),
674d1e23e3SFangrui Song               TII->get(TargetOpcode::PATCHABLE_FUNCTION_ENTER));
68*50a3ff30SFangrui Song     }
694d1e23e3SFangrui Song     return true;
704d1e23e3SFangrui Song   }
714d1e23e3SFangrui Song 
72f1caa283SMatthias Braun   if (!MF.getFunction().hasFnAttribute("patchable-function"))
73c0441c29SSanjoy Das     return false;
74c0441c29SSanjoy Das 
75e9c32c7eSCharles Davis #ifndef NDEBUG
76f1caa283SMatthias Braun   Attribute PatchAttr = MF.getFunction().getFnAttribute("patchable-function");
77c0441c29SSanjoy Das   StringRef PatchType = PatchAttr.getValueAsString();
78e9c32c7eSCharles Davis   assert(PatchType == "prologue-short-redirect" && "Only possibility today!");
79e9c32c7eSCharles Davis #endif
80c0441c29SSanjoy Das 
81c0441c29SSanjoy Das   auto &FirstMBB = *MF.begin();
82512424f2SMatthias Braun   MachineBasicBlock::iterator FirstActualI = FirstMBB.begin();
83512424f2SMatthias Braun   for (; doesNotGeneratecode(*FirstActualI); ++FirstActualI)
84512424f2SMatthias Braun     assert(FirstActualI != FirstMBB.end());
85c0441c29SSanjoy Das 
86e9c32c7eSCharles Davis   auto *TII = MF.getSubtarget().getInstrInfo();
87e9c32c7eSCharles Davis   auto MIB = BuildMI(FirstMBB, FirstActualI, FirstActualI->getDebugLoc(),
88e9c32c7eSCharles Davis                      TII->get(TargetOpcode::PATCHABLE_OP))
89e9c32c7eSCharles Davis                  .addImm(2)
90e9c32c7eSCharles Davis                  .addImm(FirstActualI->getOpcode());
91c0441c29SSanjoy Das 
92e9c32c7eSCharles Davis   for (auto &MO : FirstActualI->operands())
93116bbab4SDiana Picus     MIB.add(MO);
94e9c32c7eSCharles Davis 
95e9c32c7eSCharles Davis   FirstActualI->eraseFromParent();
9618f805a7SGuillaume Chatelet   MF.ensureAlignment(Align(16));
97c0441c29SSanjoy Das   return true;
98c0441c29SSanjoy Das }
99c0441c29SSanjoy Das 
100c0441c29SSanjoy Das char PatchableFunction::ID = 0;
101c0441c29SSanjoy Das char &llvm::PatchableFunctionID = PatchableFunction::ID;
1024519ff73SSanjoy Das INITIALIZE_PASS(PatchableFunction, "patchable-function",
1034519ff73SSanjoy Das                 "Implement the 'patchable-function' attribute", false, false)
104