https://gcc.gnu.org/g:992012439d28c56424189110e00321f257a31533

commit r16-9121-g992012439d28c56424189110e00321f257a31533
Author: Andrew Pinski <[email protected]>
Date:   Sat Jun 13 09:38:08 2026 -0700

    phiopt: Fix is_factor_profitable for debug stmts [PR125776]
    
    This is a latent bug in is_factor_profitable where we would
    consider a debug statement as an use for lifetime usage
    afterwards. So you would get a compare debug failure in some
    cases. pr125776-2.c fails since r15-4503-g8d6d6d537fdc75.
    
    Pushed as obvious after a bootstrap/test.
    
    Note this is slightly modifed from the trunk version since
    the loop is slightly different.
    
            PR tree-optimization/125776
    
    gcc/ChangeLog:
    
            * tree-ssa-phiopt.cc (is_factor_profitable): An
            usage in a debug stmt should be ignored.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/torture/pr125776-1.c: New test.
            * gcc.dg/torture/pr125776-2.c: New test.
    
    Signed-off-by: Andrew Pinski <[email protected]>
    (cherry picked from commit a4afea80fce5f1aafdd1174e48e9ebfc9ec8155d)

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr125776-1.c | 21 +++++++++++++++++++++
 gcc/testsuite/gcc.dg/torture/pr125776-2.c | 21 +++++++++++++++++++++
 gcc/tree-ssa-phiopt.cc                    |  2 ++
 3 files changed, 44 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/torture/pr125776-1.c 
b/gcc/testsuite/gcc.dg/torture/pr125776-1.c
new file mode 100644
index 000000000000..58f143d203f5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr125776-1.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-fcompare-debug" } */
+/* PR tree-optimization/125776 */
+
+int a, b, d, e, f;
+long c;
+void g();
+void h() {
+  unsigned i;
+  for (;;) {
+    unsigned j = a ? a : d;
+    i += j;
+    f = b & 40 | b > 4;
+    e = a ? a : b;
+    if (e)
+      break;
+    if (j)
+      g();
+  }
+  c = i;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr125776-2.c 
b/gcc/testsuite/gcc.dg/torture/pr125776-2.c
new file mode 100644
index 000000000000..6f89a5242ff7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr125776-2.c
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-fcompare-debug" } */
+/* PR tree-optimization/125776 */
+int a, b, d, e, f;
+long c;
+void g();
+void h(int i) {
+  for (;;) {
+    i++;
+    int j = a  ? a : d;
+    i = i > 0 ? i : -i;
+    f = b & 40 | b > 4;
+    e = a ? a : b;
+    if (e)
+      break;
+    if (j)
+      g();
+  }
+  c = i;
+}
+
diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 0bf7e58b8f03..ebbb5e2f1d8b 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -267,6 +267,8 @@ is_factor_profitable (gimple *def_stmt, basic_block merge, 
tree arg)
   FOR_EACH_IMM_USE_FAST (use_p, iter, arg)
     {
       gimple *use_stmt = USE_STMT (use_p);
+      if (is_gimple_debug (use_stmt))
+       continue;
       basic_block use_bb = gimple_bb (use_stmt);
       if (dominated_by_p (CDI_DOMINATORS, merge, use_bb))
        return true;

Reply via email to