[gcc r17-3299] Add new relation before starting search

Andrew Macleod via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:62214130b61f48cf7cdb37c69bb14c22aea6c65a

commit r17-3299-g62214130b61f48cf7cdb37c69bb14c22aea6c65a
Author: Andrew MacLeod <[email protected]>
Date:   Thu Aug 13 13:44:09 2026 -0400

    Add new relation before starting search
    
    Create a new relation immediately. Then Calculate any known range and
    intersect the result.
    
            PR tree-optimization/126845
            gcc/
            * value-relation.cc (dom_oracle::search_and_merge_relation): Create
            relation immediately and update it later.
    
            gcc/testsuite/
            * gcc.dg/pr126845.c: New.

Diff:
---
 gcc/testsuite/gcc.dg/pr126845.c | 19 +++++++++++++++++++
 gcc/value-relation.cc           | 27 ++++++++++++++++-----------
 2 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/pr126845.c b/gcc/testsuite/gcc.dg/pr126845.c
new file mode 100644
index 000000000000..0ae91d9a7731
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr126845.c
@@ -0,0 +1,19 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-move-loop-stores" } */
+
+int a, c, d, e, **f, *g, h;
+static int i() {}
+void j() {
+  do {
+    f = &g;
+    c = 1 + (1 >> a);
+    if (2 + c >= c - a)
+      goto k;
+    g = &c;
+  } while (i());
+  c = a + c + c;
+k:
+  e = **f;
+  for (int b = 0; b < 3; b++)
+    h = d = d ^ e;
+}
diff --git a/gcc/value-relation.cc b/gcc/value-relation.cc
index 04e9cc198437..cc9e8e56c67b 100644
--- a/gcc/value-relation.cc
+++ b/gcc/value-relation.cc
@@ -1241,22 +1241,22 @@ dom_oracle::search_and_merge_relation (basic_block bb, relation_kind k,
 {
   gcc_checking_assert (k != VREL_VARYING && k != VREL_EQ);
 
-  int bbi = bb->index;
-
-  relation_kind curr;
   relation_chain *ptr;
-  curr = find_relation_block (bbi, op1, op2, &ptr);
-  // There is an existing relation in this block, just intersect with it.
+  relation_kind curr = find_relation_block (bb->index, op1, op2, &ptr);
+
+  // If there is an existing relation in this block, just intersect with it.
   if (curr != VREL_VARYING)
     {
-      // Check into whether we can simply replace the relation rather than
-      // intersecting it.  This may help with some optimistic iterative
-      // updating algorithms.  If there was no change, return no record..
+      // If there was no change, return no record.
       value_relation vr (k, op1, op2);
       if (!ptr->intersect (vr))
 	return NULL;
+      return ptr;
     }
-  else
+
+  // Create the relation in this block.
+  ptr = create_relation_in_bb (bb, k, op1, op2);
+  if (ptr)
     {
       // Check for an existing relation further up the DOM chain.
       // By including dominating relations, The first one found in any search
@@ -1264,8 +1264,13 @@ dom_oracle::search_and_merge_relation (basic_block bb, relation_kind k,
       curr = find_relation_dom (get_immediate_dominator (CDI_DOMINATORS, bb),
 				op1, op2);
       if (curr != VREL_VARYING)
-	k = relation_intersect (curr, k);
-      ptr = create_relation_in_bb (bb, k, op1, op2);
+	{
+	  curr = relation_intersect (curr, k);
+	  // Intersect the new relation with the existing one, unless the
+	  // result is UNDEFINED.  Then just leave it.
+	  if (curr != k && curr != VREL_UNDEFINED)
+	    ptr->set_relation (curr, op1, op2);
+	}
     }
   return ptr;
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.