https://gcc.gnu.org/g:f3dcc49945ea9ba0355d5c87686c0ed99e7c233d

commit r15-9841-gf3dcc49945ea9ba0355d5c87686c0ed99e7c233d
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Tue Jun 17 13:20:11 2025 +0200

    crc: Fix up ICE from optimize_crc_loop [PR120677]
    
    The following testcase ICEs, because optimize_crc_loop inserts a call
    statement before labels instead of after labels.
    
    Fixed thusly (plus fixed other issues noticed around it).
    
    2025-06-17  Jakub Jelinek  <ja...@redhat.com>
    
            PR tree-optimization/120677
            * gimple-crc-optimization.cc (crc_optimization::optimize_crc_loop):
            Insert before gsi_after_labels instead of gsi_start_bb.  Use
            gimple_bb (output_crc) instead of output_crc->bb.  Formatting fix.
    
            * gcc.c-torture/execute/pr120677.c: New test.
    
    (cherry picked from commit 75ffef5c6fa4e6e9576285e0403c06728309ae3e)

Diff:
---
 gcc/gimple-crc-optimization.cc                 |  9 +++-----
 gcc/testsuite/gcc.c-torture/execute/pr120677.c | 31 ++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 6 deletions(-)

diff --git a/gcc/gimple-crc-optimization.cc b/gcc/gimple-crc-optimization.cc
index a98cbe6752b5..1751be9bc97f 100644
--- a/gcc/gimple-crc-optimization.cc
+++ b/gcc/gimple-crc-optimization.cc
@@ -1261,15 +1261,12 @@ crc_optimization::optimize_crc_loop (gphi *output_crc)
   loc = EXPR_LOCATION (phi_result);
 
   /* Add IFN call and write the return value in the phi_result.  */
-  gcall *call
-      = gimple_build_call_internal (ifn, 3,
-                                   m_crc_arg,
-                                   m_data_arg,
-                                   polynomial_arg);
+  gcall *call = gimple_build_call_internal (ifn, 3, m_crc_arg, m_data_arg,
+                                           polynomial_arg);
 
   gimple_call_set_lhs (call, phi_result);
   gimple_set_location (call, loc);
-  gimple_stmt_iterator si = gsi_start_bb (output_crc->bb);
+  gimple_stmt_iterator si = gsi_after_labels (gimple_bb (output_crc));
   gsi_insert_before (&si, call, GSI_SAME_STMT);
 
   /* Remove phi statement, which was holding CRC result.  */
diff --git a/gcc/testsuite/gcc.c-torture/execute/pr120677.c 
b/gcc/testsuite/gcc.c-torture/execute/pr120677.c
new file mode 100644
index 000000000000..3cff04ea2fd5
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr120677.c
@@ -0,0 +1,31 @@
+/* PR tree-optimization/120677 */
+/* { dg-do run { target int32plus } } */
+
+unsigned a;
+int b, e;
+
+int
+foo (int d)
+{
+  switch (d)
+    {
+    case 0:
+    case 2:
+      return 0;
+    default:
+      return 1;
+    }
+}
+
+int
+main ()
+{
+  for (b = 8; b; b--)
+    if (a & 1)
+      a = a >> 1 ^ 20000000;
+    else
+      a >>= 1;
+  e = foo (0);
+  if (e || a)
+    __builtin_abort ();
+}

Reply via email to