On Mon, Oct 05, 2026 at 01:48:01AM -0600, Jeff Law wrote:
> 
> 
> On 10/4/26 5:24 AM, Aldy Hernandez wrote:
> > [Well, since we can't get rid of DOM yet, why don't we clean it up and
> > remove stuff we can clearly do better with ranges.  Maybe we can
> > reduce it to a bare minimum ;-).]
> > 
> > We now have all the pieces to do what test_for_singularity does, but
> > with ranges.  Remove the function and use simplify_using_ranges
> > instead.
> > 
> > This makes DOM a tiny bit smarter, since simplify_using_ranges is far
> > more capable than the ad-hoc stuff we did.  As much as I hate making
> > it smarter, it's all in the interest of nuking it :).
> > 
> > Tested on ppc64le Linux.
> > 
> > OK for trunk?
> > 
> > gcc/ChangeLog:
> > 
> >     * tree-ssa-dom.cc (dom_opt_dom_walker::m_dummy_cond): Remove.
> >     (dom_opt_dom_walker::test_for_singularity): Remove.
> >     (dom_opt_dom_walker::fold_cond): Remove.
> >     (dom_opt_dom_walker::simplify_stmt): New.
> >     (dom_opt_dom_walker::optimize_stmt): Call it in place of
> >     test_for_singularity and fold_cond.
> > 
> > gcc/testsuite/ChangeLog:
> > 
> >     * gcc.dg/tree-ssa/predcom-7.c: Add -fno-tree-dominator-opts.
> >     * gcc.dg/tree-ssa/ssa-dom-simplify-3.c: New test.
> IIRC we've wanted to nuke this optimization as interacts poorly with SCEV.
> 
> Furthermore, the main purpose was to turn these things into equality
> comparisons which in turn would expose the constant equivalence on one of
> the edges, something done quite well by using Ranger already.
> 
> So no objection to the patch, but if you wanted to get a lot more aggressive
> about this blob of code and nuke it, you've got my blessing.

How about this?

Aldy
>From 26d148267c9183307c9cda8f36c3cabefec77a4b Mon Sep 17 00:00:00 2001
From: Aldy Hernandez <[email protected]>
Date: Mon, 5 Oct 2026 10:48:43 +0000
Subject: [PATCH] DOM: stop rewriting comparisons into equalities

Instead of keeping the test_for_singularity() functionality we are now
doing with simplify_using_ranges::simplify(), just do
simplify_using_ranges::fold_cond() which folds the conditional to a
constant if possible, but avoids the singularity transformation.  This
transformation isn't needed as ranger already knows what x is on each
arm of the conditional.  Besides, this transformation was problematic
for loop analyzers.

Also, keep assignments from going through the simplifier, since this
was a side-effect of the last patch.

Tested on ppc64le Linux.

gcc/ChangeLog:

	* tree-ssa-dom.cc (dom_opt_dom_walker::simplify_stmt): Rename to...
	(dom_opt_dom_walker::fold_cond): ...this.  Fold a conditional with
	the ranger, do not simplify anything else.
	(dom_opt_dom_walker::optimize_stmt): Adjust.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/predcom-7.c: Drop -fno-tree-dominator-opts.
	* gcc.dg/tree-ssa/ssa-dom-simplify-3.c: Expect the relationals to
	stay.
---
 gcc/testsuite/gcc.dg/tree-ssa/predcom-7.c     |  5 +--
 .../gcc.dg/tree-ssa/ssa-dom-simplify-3.c      |  5 +--
 gcc/tree-ssa-dom.cc                           | 31 +++++--------------
 3 files changed, 12 insertions(+), 29 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/predcom-7.c b/gcc/testsuite/gcc.dg/tree-ssa/predcom-7.c
index 0a1bc5b3a2b..99939767562 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/predcom-7.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/predcom-7.c
@@ -1,8 +1,5 @@
 /* { dg-do run } */
-/* { dg-options "-O3 -fdump-tree-pcom-details-blocks -fno-tree-dominator-opts" } */
-/* DOM folds the exit test to c + 1 == 1 from c's range, which leaves c a
-   two-valued PHI rather than an induction variable and predcom nothing to
-   do; keep it out of the way.  */
+/* { dg-options "-O3 -fdump-tree-pcom-details-blocks" } */
 
 int b, f, d[5][2];
 unsigned int c;
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dom-simplify-3.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dom-simplify-3.c
index 89d545e621d..20e5ee69c20 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/ssa-dom-simplify-3.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-dom-simplify-3.c
@@ -86,6 +86,7 @@ by_range_min (int a, int b)
   return 0;
 }
 
-/* { dg-final { scan-tree-dump "if \\(x_1\\(D\\) == 4\\)" "dom2" } } */
-/* { dg-final { scan-tree-dump "if \\(i_1\\(D\\) == j_2\\(D\\)\\)" "dom2" } } */
+/* DOM leaves the relationals alone.  */
+/* { dg-final { scan-tree-dump-not "if \\(x_1\\(D\\) == 4\\)" "dom2" } } */
+/* { dg-final { scan-tree-dump-not "if \\(i_1\\(D\\) == j_2\\(D\\)\\)" "dom2" } } */
 /* { dg-final { scan-tree-dump-not "MIN_EXPR" "dom2" } } */
diff --git a/gcc/tree-ssa-dom.cc b/gcc/tree-ssa-dom.cc
index 7a30983572b..1470d65f4e3 100644
--- a/gcc/tree-ssa-dom.cc
+++ b/gcc/tree-ssa-dom.cc
@@ -694,7 +694,7 @@ private:
 
   void set_global_ranges_from_unreachable_edges (basic_block);
 
-  void simplify_stmt (gimple_stmt_iterator *);
+  void fold_cond (gimple *);
   void record_equivalences_from_incoming_edge (basic_block);
   void eliminate_redundant_computations (gimple_stmt_iterator *);
   void record_equivalences_from_stmt (gimple *, int);
@@ -1983,32 +1983,18 @@ cprop_operand (gimple *stmt, use_operand_p op_p, range_query *query)
     }
 }
 
-/* Attempt to simplify the statement in GSI with range info.  */
+/* Fold the conditional STMT with range info.  */
 
 void
-dom_opt_dom_walker::simplify_stmt (gimple_stmt_iterator *gsi)
+dom_opt_dom_walker::fold_cond (gimple *stmt)
 {
-  gimple *stmt = gsi_stmt (*gsi);
-
-  /* Avoid switches as touching those could remove edges mid-walk.  */
-  if (gimple_code (stmt) == GIMPLE_SWITCH)
+  gcond *cond = dyn_cast <gcond *> (stmt);
+  if (!cond)
     return;
 
-  gimple_stmt_iterator i = *gsi;
-  gsi_prev (&i);
-  gimple *before = gsi_end_p (i) ? NULL : gsi_stmt (i);
   simplify_using_ranges simplify (m_ranger);
-  if (!simplify.simplify (gsi))
-    return;
-
-  stmt = gsi_stmt (*gsi);
-  gimple_set_modified (stmt, true);
-
-  /* Our main loop will go back over the statements inserted in front of STMT,
-     so mark those as visited to avoid looking at them again.  */
-  i = *gsi;
-  for (gsi_prev (&i); !gsi_end_p (i) && gsi_stmt (i) != before; gsi_prev (&i))
-    gimple_set_visited (gsi_stmt (i), true);
+  if (simplify.fold_cond (cond))
+    gimple_set_modified (cond, true);
 }
 
 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
@@ -2269,8 +2255,7 @@ dom_opt_dom_walker::optimize_stmt (basic_block bb, gimple_stmt_iterator *si,
 	 it, which may in turn allow other part of DOM or other passes to do
 	 a better job.  */
       if (!gimple_modified_p (stmt))
-	simplify_stmt (si);
-      stmt = gsi_stmt (*si);
+	fold_cond (stmt);
     }
 
   /* Record any additional equivalences created by this statement.  */
-- 
2.53.0

Reply via email to