[llvm] [RegAlloc] Add printer and dump for VNInfo [nfc] (PR #160758)

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 26 07:05:35 PDT 2025


https://github.com/preames updated https://github.com/llvm/llvm-project/pull/160758

>From d7858f05c38af6f02089524ef46f31bd6b014fa0 Mon Sep 17 00:00:00 2001
From: Philip Reames <preames at rivosinc.com>
Date: Thu, 25 Sep 2025 11:47:18 -0700
Subject: [PATCH 1/2] [RegAlloc] Add printer and dump for VNInfo [nfc]

Uses the existing format of the LiveRange printer, and just factors
it out so that you can do vni->dump() when debugging, or log a vni
in a debug print statement.
---
 llvm/include/llvm/CodeGen/LiveInterval.h |  8 ++++++++
 llvm/lib/CodeGen/LiveInterval.cpp        | 25 ++++++++++++++++--------
 2 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/llvm/include/llvm/CodeGen/LiveInterval.h b/llvm/include/llvm/CodeGen/LiveInterval.h
index e1c5717f5face..f18c177b1c35b 100644
--- a/llvm/include/llvm/CodeGen/LiveInterval.h
+++ b/llvm/include/llvm/CodeGen/LiveInterval.h
@@ -83,8 +83,16 @@ namespace llvm {
 
     /// Mark this value as unused.
     void markUnused() { def = SlotIndex(); }
+
+    LLVM_ABI void print(raw_ostream &OS) const;
+    LLVM_ABI void dump() const;
   };
 
+  inline raw_ostream &operator<<(raw_ostream &OS, const VNInfo &VNI) {
+    VNI.print(OS);
+    return OS;
+  }
+
   /// Result of a LiveRange query. This class hides the implementation details
   /// of live ranges, and it should be used as the primary interface for
   /// examining live ranges around instructions.
diff --git a/llvm/lib/CodeGen/LiveInterval.cpp b/llvm/lib/CodeGen/LiveInterval.cpp
index b682998c329bc..c233b21a37022 100644
--- a/llvm/lib/CodeGen/LiveInterval.cpp
+++ b/llvm/lib/CodeGen/LiveInterval.cpp
@@ -996,6 +996,17 @@ LLVM_DUMP_METHOD void LiveRange::Segment::dump() const {
 }
 #endif
 
+void VNInfo::print(raw_ostream &OS) const {
+  OS << id << '@';
+  if (isUnused()) {
+    OS << 'x';
+  } else {
+    OS << def;
+    if (isPHIDef())
+      OS << "-phi";
+  }
+}
+
 void LiveRange::print(raw_ostream &OS) const {
   if (empty())
     OS << "EMPTY";
@@ -1014,14 +1025,8 @@ void LiveRange::print(raw_ostream &OS) const {
          ++i, ++vnum) {
       const VNInfo *vni = *i;
       if (vnum) OS << ' ';
-      OS << vnum << '@';
-      if (vni->isUnused()) {
-        OS << 'x';
-      } else {
-        OS << vni->def;
-        if (vni->isPHIDef())
-          OS << "-phi";
-      }
+      OS << *vni;
+      assert(vnum == vni->id && "Bad VNInfo");
     }
   }
 }
@@ -1041,6 +1046,10 @@ void LiveInterval::print(raw_ostream &OS) const {
 }
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+LLVM_DUMP_METHOD void VNInfo::dump() const {
+  dbgs() << *this << '\n';
+}
+
 LLVM_DUMP_METHOD void LiveRange::dump() const {
   dbgs() << *this << '\n';
 }

>From 59de3b4c870f8370ba85540d20b097c6624665c7 Mon Sep 17 00:00:00 2001
From: Philip Reames <preames at rivosinc.com>
Date: Fri, 26 Sep 2025 07:01:16 -0700
Subject: [PATCH 2/2] Clang format as requested

---
 llvm/lib/CodeGen/LiveInterval.cpp | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/CodeGen/LiveInterval.cpp b/llvm/lib/CodeGen/LiveInterval.cpp
index c233b21a37022..299db85233c2d 100644
--- a/llvm/lib/CodeGen/LiveInterval.cpp
+++ b/llvm/lib/CodeGen/LiveInterval.cpp
@@ -1024,7 +1024,8 @@ void LiveRange::print(raw_ostream &OS) const {
     for (const_vni_iterator i = vni_begin(), e = vni_end(); i != e;
          ++i, ++vnum) {
       const VNInfo *vni = *i;
-      if (vnum) OS << ' ';
+      if (vnum)
+        OS << ' ';
       OS << *vni;
       assert(vnum == vni->id && "Bad VNInfo");
     }
@@ -1046,13 +1047,9 @@ void LiveInterval::print(raw_ostream &OS) const {
 }
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
-LLVM_DUMP_METHOD void VNInfo::dump() const {
-  dbgs() << *this << '\n';
-}
+LLVM_DUMP_METHOD void VNInfo::dump() const { dbgs() << *this << '\n'; }
 
-LLVM_DUMP_METHOD void LiveRange::dump() const {
-  dbgs() << *this << '\n';
-}
+LLVM_DUMP_METHOD void LiveRange::dump() const { dbgs() << *this << '\n'; }
 
 LLVM_DUMP_METHOD void LiveInterval::SubRange::dump() const {
   dbgs() << *this << '\n';



More information about the llvm-commits mailing list