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; + 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; } Index: lib/CodeGen/MachineBasicBlock.cpp =================================================================== --- lib/CodeGen/MachineBasicBlock.cpp +++ lib/CodeGen/MachineBasicBlock.cpp @@ -110,6 +110,7 @@ // 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 @@ -118,8 +119,10 @@ 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: lib/CodeGen/MachineFunction.cpp =================================================================== --- lib/CodeGen/MachineFunction.cpp +++ lib/CodeGen/MachineFunction.cpp @@ -188,6 +188,8 @@ PSVManager = llvm::make_unique(*(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 {