]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/CodeGen/PatchableFunction.cpp
Merge llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / CodeGen / PatchableFunction.cpp
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/MachineFunction.h"
15 #include "llvm/CodeGen/MachineFunctionPass.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/CodeGen/TargetFrameLowering.h"
19 #include "llvm/CodeGen/TargetInstrInfo.h"
20 #include "llvm/CodeGen/TargetSubtargetInfo.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   bool runOnMachineFunction(MachineFunction &F) override;
32    MachineFunctionProperties getRequiredProperties() const override {
33     return MachineFunctionProperties().set(
34         MachineFunctionProperties::Property::NoVRegs);
35   }
36 };
37 }
38
39 /// Returns true if instruction \p MI will not result in actual machine code
40 /// instructions.
41 static bool doesNotGeneratecode(const MachineInstr &MI) {
42   // TODO: Introduce an MCInstrDesc flag for this
43   switch (MI.getOpcode()) {
44   default: return false;
45   case TargetOpcode::IMPLICIT_DEF:
46   case TargetOpcode::KILL:
47   case TargetOpcode::CFI_INSTRUCTION:
48   case TargetOpcode::EH_LABEL:
49   case TargetOpcode::GC_LABEL:
50   case TargetOpcode::DBG_VALUE:
51   case TargetOpcode::DBG_LABEL:
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;
86 char &llvm::PatchableFunctionID = PatchableFunction::ID;
87 INITIALIZE_PASS(PatchableFunction, "patchable-function",
88                 "Implement the 'patchable-function' attribute", false, false)