From: Aditya Nandakumar Date: Thu, 20 Sep 2018 23:01:56 +0000 (+0000) Subject: Add the ability to register callbacks for removal and insertion of MachineInstrs X-Git-Tag: llvmorg-8.0.0-rc1~8285 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e5909431b5f3479dd713c7fe4919a028499dd709;p=platform%2Fupstream%2Fllvm.git Add the ability to register callbacks for removal and insertion of MachineInstrs https://reviews.llvm.org/D52127 This patch adds the ability to watch for insertions/deletions of MachineInstructions similar to MachineRegisterInfo. llvm-svn: 342696 --- diff --git a/llvm/include/llvm/CodeGen/MachineFunction.h b/llvm/include/llvm/CodeGen/MachineFunction.h index 9043256..cc5fcff 100644 --- a/llvm/include/llvm/CodeGen/MachineFunction.h +++ b/llvm/include/llvm/CodeGen/MachineFunction.h @@ -363,6 +363,25 @@ public: int Slot, const DILocation *Loc) : Var(Var), Expr(Expr), Slot(Slot), Loc(Loc) {} }; + + class Delegate { + virtual void anchor(); + + public: + virtual ~Delegate() = default; + virtual void MF_HandleInsertion(const MachineInstr &MI) = 0; + virtual void MF_HandleRemoval(const MachineInstr &MI) = 0; + }; + +private: + Delegate *TheDelegate = nullptr; + + // Callbacks for insertion and removal. + void handleInsertion(const MachineInstr &MI); + void handleRemoval(const MachineInstr &MI); + friend struct ilist_traits; + +public: using VariableDbgInfoMapTy = SmallVector; VariableDbgInfoMapTy VariableDbgInfos; @@ -379,6 +398,23 @@ public: init(); } + /// Reset the currently registered delegate - otherwise assert. + void resetDelegate(Delegate *delegate) { + assert(TheDelegate == delegate && + "Only the current delegate can perform reset!"); + TheDelegate = nullptr; + } + + /// Set the delegate. resetDelegate must be called before attempting + /// to set. + void setDelegate(Delegate *delegate) { + assert(delegate && !TheDelegate && + "Attempted to set delegate to null, or to change it without " + "first resetting it!"); + + TheDelegate = delegate; + } + MachineModuleInfo &getMMI() const { return MMI; } MCContext &getContext() const { return Ctx; } diff --git a/llvm/lib/CodeGen/MachineBasicBlock.cpp b/llvm/lib/CodeGen/MachineBasicBlock.cpp index 3e04071..49181e3 100644 --- a/llvm/lib/CodeGen/MachineBasicBlock.cpp +++ b/llvm/lib/CodeGen/MachineBasicBlock.cpp @@ -110,6 +110,7 @@ void ilist_traits::addNodeToList(MachineInstr *N) { // use/def lists. MachineFunction *MF = Parent->getParent(); N->AddRegOperandsToUseLists(MF->getRegInfo()); + MF->handleInsertion(*N); } /// When we remove an instruction from a basic block list, we update its parent @@ -118,8 +119,10 @@ void ilist_traits::removeNodeFromList(MachineInstr *N) { assert(N->getParent() && "machine instruction not in a basic block"); // Remove from the use/def lists. - if (MachineFunction *MF = N->getMF()) + if (MachineFunction *MF = N->getMF()) { + MF->handleRemoval(*N); N->RemoveRegOperandsFromUseLists(MF->getRegInfo()); + } N->setParent(nullptr); } diff --git a/llvm/lib/CodeGen/MachineFunction.cpp b/llvm/lib/CodeGen/MachineFunction.cpp index 1b15819c..81fc47b 100644 --- a/llvm/lib/CodeGen/MachineFunction.cpp +++ b/llvm/lib/CodeGen/MachineFunction.cpp @@ -99,6 +99,9 @@ static const char *getPropertyName(MachineFunctionProperties::Property Prop) { llvm_unreachable("Invalid machine function property"); } +// Pin the vtable to this file. +void MachineFunction::Delegate::anchor() {} + void MachineFunctionProperties::print(raw_ostream &OS) const { const char *Separator = ""; for (BitVector::size_type I = 0; I < Properties.size(); ++I) { @@ -135,6 +138,16 @@ MachineFunction::MachineFunction(const Function &F, const TargetMachine &Target, init(); } +void MachineFunction::handleInsertion(const MachineInstr &MI) { + if (TheDelegate) + TheDelegate->MF_HandleInsertion(MI); +} + +void MachineFunction::handleRemoval(const MachineInstr &MI) { + if (TheDelegate) + TheDelegate->MF_HandleRemoval(MI); +} + void MachineFunction::init() { // Assume the function starts in SSA form with correct liveness. Properties.set(MachineFunctionProperties::Property::IsSSA);