I had to debug an ssa data corruption, and wrote this routine in the process of tracking that down.

It simply prints the def stmt and each use stmt. If the var is used multiple times in a single stmt, that stmt gets repeated.

ok?

nathan

--
Nathan Sidwell
From b8500f461cb546350384d2301a892a114d7eeab7 Mon Sep 17 00:00:00 2001
From: Nathan Sidwell <[email protected]>
Date: Wed, 9 Sep 2026 12:10:21 -0400
Subject: [PATCH] Add ssa def/use printer

	gcc/
	* gimple-pretty-print.h (print_ssa_def_use): Declare.
	(debug_ssa_def_use): Declare.
	* gimple-pretty-print.cc (print_ssa_def_use): New.
	(debug_ssa_def_use): New.
---
 gcc/gimple-pretty-print.cc | 25 +++++++++++++++++++++++++
 gcc/gimple-pretty-print.h  |  4 ++++
 2 files changed, 29 insertions(+)

diff --git a/gcc/gimple-pretty-print.cc b/gcc/gimple-pretty-print.cc
index 001865605a1..69b03b21b72 100644
--- a/gcc/gimple-pretty-print.cc
+++ b/gcc/gimple-pretty-print.cc
@@ -89,6 +89,31 @@ debug_gimple_stmt (gimple *gs)
   print_gimple_stmt (stderr, gs, 0, TDF_VOPS|TDF_MEMSYMS);
 }
 
+/* Print the defining ssa stmt and each use stmt for VAR to STREAM.  */
+
+void
+print_ssa_def_use (FILE *stream, tree var)
+{
+  if (!SSA_VAR_P (var))
+    return;
+
+  auto def = SSA_NAME_DEF_STMT (var);
+  print_gimple_stmt (stream, def, 0);
+
+  use_operand_p use_p;
+  imm_use_iterator iter;
+
+  FOR_EACH_IMM_USE_FAST (use_p, iter, var)
+    print_gimple_stmt (stream, USE_STMT (use_p), 0);
+}
+
+/* Print the defining ssa stmt and each use stmt for VAR to stderr.  */
+
+DEBUG_FUNCTION void
+debug_ssa_def_use (tree var)
+{
+  print_ssa_def_use (stderr, var);
+}
 
 /* Return formatted string of a VALUE probability
    (biased by REG_BR_PROB_BASE).  Returned string is allocated
diff --git a/gcc/gimple-pretty-print.h b/gcc/gimple-pretty-print.h
index 8013f2d5909..261c5ee26d3 100644
--- a/gcc/gimple-pretty-print.h
+++ b/gcc/gimple-pretty-print.h
@@ -33,6 +33,10 @@ extern void print_gimple_seq (FILE *, gimple_seq, int, dump_flags_t);
 extern void print_gimple_stmt (FILE *, gimple *, int, dump_flags_t = TDF_NONE);
 extern void debug (gimple &ref);
 extern void debug (gimple *ptr);
+
+extern void debug_ssa_def_use (tree ssa_var);
+extern void print_ssa_def_use (FILE *, tree ssa_var);
+
 extern void print_gimple_expr (FILE *, gimple *, int, dump_flags_t = TDF_NONE);
 extern void pp_gimple_stmt_1 (pretty_printer *, const gimple *, int,
 			      dump_flags_t);
-- 
2.55.0

Reply via email to