diff --git a/llvm/include/llvm/CodeGen/MachineInstr.h b/llvm/include/llvm/CodeGen/MachineInstr.h --- a/llvm/include/llvm/CodeGen/MachineInstr.h +++ b/llvm/include/llvm/CodeGen/MachineInstr.h @@ -262,6 +262,11 @@ // MachineInstrs are pool-allocated and owned by MachineFunction. friend class MachineFunction; + void + dumprImpl(const MachineRegisterInfo &MRI, unsigned Depth, + Optional MaxDepth, + SmallPtrSetImpl &AlreadySeenInstrs) const; + public: MachineInstr(const MachineInstr &) = delete; MachineInstr &operator=(const MachineInstr &) = delete; @@ -1534,6 +1539,10 @@ bool AddNewLine = true, const TargetInstrInfo *TII = nullptr) const; void dump() const; + /// Print on dbgs() the current instruction and the instructions defining its + /// operands and so on until we reach \p MaxDepth. + void dumpr(const MachineRegisterInfo &MRI, + Optional MaxDepth = None) const; /// @} //===--------------------------------------------------------------------===// diff --git a/llvm/lib/CodeGen/MachineInstr.cpp b/llvm/lib/CodeGen/MachineInstr.cpp --- a/llvm/lib/CodeGen/MachineInstr.cpp +++ b/llvm/lib/CodeGen/MachineInstr.cpp @@ -1463,6 +1463,32 @@ dbgs() << " "; print(dbgs()); } + +LLVM_DUMP_METHOD void MachineInstr::dumprImpl( + const MachineRegisterInfo &MRI, unsigned Depth, Optional MaxDepth, + SmallPtrSetImpl &AlreadySeenInstrs) const { + if (MaxDepth && Depth >= *MaxDepth) + return; + if (!AlreadySeenInstrs.insert(this).second) + return; + std::string Prefix; + for (unsigned i = 0; i != Depth; ++i) + Prefix += " "; + dbgs() << Prefix; + dump(); + for (const MachineOperand &MO : operands()) { + const MachineInstr *NewMI; + if (!MO.isReg() || MO.isDef() || !(NewMI = MRI.getVRegDef(MO.getReg()))) + continue; + NewMI->dumprImpl(MRI, Depth + 1, MaxDepth, AlreadySeenInstrs); + } +} + +LLVM_DUMP_METHOD void MachineInstr::dumpr(const MachineRegisterInfo &MRI, + Optional MaxDepth) const { + SmallPtrSet AlreadySeenInstrs; + dumprImpl(MRI, 0, MaxDepth, AlreadySeenInstrs); +} #endif void MachineInstr::print(raw_ostream &OS, bool IsStandalone, bool SkipOpers,