[PATCH] D52127: [MF][MBB]: Add the ability to register callbacks for removal and insertion of MachineInstrs

Aditya Nandakumar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 14 17:17:20 PDT 2018


aditya_nandakumar created this revision.
aditya_nandakumar added reviewers: ab, bogner, dsanders, qcolombet, t.p.northover, rtereshin, rovka, arsenm, volkan, MatzeB.
Herald added a subscriber: wdng.

This patch adds the ability to watch for insertions/deletions of MachineInstructions. Currently in GISel, we rely on the MachineIRBuilder::recordInsertion to further legalize the intermediate instructions - this won't work if the target builds instructions with BuildMI interface. Similarly, this makes it easy to watch for MI::eraseFromParent to keep various data structures up to date in the legalizer/combiner etc. I suspect there would be non GISel use cases as well.
While this patch adds the ability to register callbacks, nothing makes use of it as I didn't want to put in too much effort before getting feedback. The GISel legalizer would be one place that would be able to use of this.


Repository:
  rL LLVM

https://reviews.llvm.org/D52127

Files:
  include/llvm/CodeGen/MachineFunction.h
  lib/CodeGen/MachineBasicBlock.cpp
  lib/CodeGen/MachineFunction.cpp


Index: lib/CodeGen/MachineFunction.cpp
===================================================================
--- lib/CodeGen/MachineFunction.cpp
+++ lib/CodeGen/MachineFunction.cpp
@@ -188,6 +188,8 @@
   PSVManager =
     llvm::make_unique<PseudoSourceValueManager>(*(getSubtarget().
                                                   getInstrInfo()));
+  InsertCallbackFn = nullptr;
+  RemoveCallbackFn = nullptr;
 }
 
 MachineFunction::~MachineFunction() {
@@ -233,6 +235,8 @@
     WinEHInfo->~WinEHFuncInfo();
     Allocator.Deallocate(WinEHInfo);
   }
+  InsertCallbackFn = nullptr;
+  RemoveCallbackFn = nullptr;
 }
 
 const DataLayout &MachineFunction::getDataLayout() const {
Index: lib/CodeGen/MachineBasicBlock.cpp
===================================================================
--- lib/CodeGen/MachineBasicBlock.cpp
+++ lib/CodeGen/MachineBasicBlock.cpp
@@ -110,16 +110,19 @@
   // use/def lists.
   MachineFunction *MF = Parent->getParent();
   N->AddRegOperandsToUseLists(MF->getRegInfo());
+  MF->doInsertCallback(N);
 }
 
 /// When we remove an instruction from a basic block list, we update its parent
 /// pointer and remove its operands from reg use/def lists if appropriate.
 void ilist_traits<MachineInstr>::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->doInsertCallback(N);
     N->RemoveRegOperandsFromUseLists(MF->getRegInfo());
+  }
 
   N->setParent(nullptr);
 }
Index: include/llvm/CodeGen/MachineFunction.h
===================================================================
--- include/llvm/CodeGen/MachineFunction.h
+++ include/llvm/CodeGen/MachineFunction.h
@@ -351,6 +351,10 @@
   /// \pre Fn, Target, MMI, and FunctionNumber are properly set.
   void init();
 
+  using MICallbackFnTy = std::function<void(const MachineInstr*)>;
+  MICallbackFnTy InsertCallbackFn;
+  MICallbackFnTy RemoveCallbackFn;
+
 public:
   struct VariableDbgInfo {
     const DILocalVariable *Var;
@@ -379,6 +383,24 @@
     init();
   }
 
+  void doInsertCallback(const MachineInstr *MI) {
+    if (InsertCallbackFn)
+      InsertCallbackFn(MI);
+  }
+
+  void doRemoveCallback(const MachineInstr *MI) {
+    if (RemoveCallbackFn)
+      RemoveCallbackFn(MI);
+  }
+
+  void setInsertCallback(MICallbackFnTy Fn) {
+    InsertCallbackFn = std::move(Fn);
+  }
+
+  void setRemoveCallback(MICallbackFnTy Fn) {
+    RemoveCallbackFn = std::move(Fn);
+  }
+
   MachineModuleInfo &getMMI() const { return MMI; }
   MCContext &getContext() const { return Ctx; }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D52127.165615.patch
Type: text/x-patch
Size: 2662 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180915/d8876332/attachment.bin>


More information about the llvm-commits mailing list