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

commit r16-3930-gf8cf09130b75ea8e310debbc0a165f5906ee56d6
Author: Richard Biener <[email protected]>
Date:   Wed Sep 17 10:22:26 2025 +0200

    [gimplefe] fix SSA operand creation
    
    When transitioning gcc.dg/torture/pr84830.c to a GIMPLE testcase to
    feed the IL into PRE that caused the original issue (and verify it's
    still there with the fix reverted), I noticed we put up SSA operands
    before having fully parsed the function and thus with not all
    variables having the final TREE_ADDRESSABLE state.  The following
    fixes this, delaying update_stmt calls to when we create PHI nodes.
    It also makes the pr84830.c not rely on the particular fake exit
    edge source location by making the loop have an exit.
    
    gcc/c/
            * gimple-parser.cc (c_parser_parse_gimple_body): Initialize
            SSA operands for each stmt.
            (c_parser_gimple_compound_statement): Append stmts without
            updating SSA operands.
    
    gcc/testsuite/
            * gcc.dg/torture/pr84830.c: Turn into GIMPLE unit test for PRE.

Diff:
---
 gcc/c/gimple-parser.cc                 |  51 +++++-----
 gcc/testsuite/gcc.dg/torture/pr84830.c | 170 +++++++++++++++++++++++++--------
 2 files changed, 161 insertions(+), 60 deletions(-)

diff --git a/gcc/c/gimple-parser.cc b/gcc/c/gimple-parser.cc
index 5fd1db874530..d4c5001623e8 100644
--- a/gcc/c/gimple-parser.cc
+++ b/gcc/c/gimple-parser.cc
@@ -356,28 +356,33 @@ c_parser_parse_gimple_body (c_parser *cparser, char 
*gimple_pass,
         update stmts.  */
       if (cdil == cdil_gimple_ssa)
        {
-         /* Create PHI nodes, they are parsed into __PHI internal calls.  */
+         /* Create PHI nodes, they are parsed into __PHI internal calls
+            and update SSA operands.  */
          FOR_EACH_BB_FN (bb, cfun)
-           for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
-                !gsi_end_p (gsi);)
-             {
-               gimple *stmt = gsi_stmt (gsi);
-               if (!gimple_call_internal_p (stmt, IFN_PHI))
-                 break;
+           {
+             gimple_stmt_iterator gsi;
+             for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
+               {
+                 gimple *stmt = gsi_stmt (gsi);
+                 if (!gimple_call_internal_p (stmt, IFN_PHI))
+                   break;
 
-               gphi *phi = create_phi_node (gimple_call_lhs (stmt), bb);
-               for (unsigned i = 0; i < gimple_call_num_args (stmt); i += 2)
-                 {
-                   int srcidx = TREE_INT_CST_LOW (gimple_call_arg (stmt, i));
-                   edge e = find_edge (BASIC_BLOCK_FOR_FN (cfun, srcidx), bb);
-                   if (!e)
-                     c_parser_error (parser, "edge not found");
-                   else
-                     add_phi_arg (phi, gimple_call_arg (stmt, i + 1), e,
-                                  UNKNOWN_LOCATION);
-                 }
-               gsi_remove (&gsi, true);
-             }
+                 gphi *phi = create_phi_node (gimple_call_lhs (stmt), bb);
+                 for (unsigned i = 0; i < gimple_call_num_args (stmt); i += 2)
+                   {
+                     int srcidx = TREE_INT_CST_LOW (gimple_call_arg (stmt, i));
+                     edge e = find_edge (BASIC_BLOCK_FOR_FN (cfun, srcidx), 
bb);
+                     if (!e)
+                       c_parser_error (parser, "edge not found");
+                     else
+                       add_phi_arg (phi, gimple_call_arg (stmt, i + 1), e,
+                                    UNKNOWN_LOCATION);
+                   }
+                 gsi_remove (&gsi, true);
+               }
+             for (; !gsi_end_p (gsi); gsi_next (&gsi))
+               update_stmt (gsi_stmt (gsi));
+           }
          /* Fill SSA name gaps, putting them on the freelist and diagnose
             SSA names without definition.  */
          for (unsigned i = 1; i < num_ssa_names; ++i)
@@ -639,7 +644,8 @@ c_parser_gimple_compound_statement (gimple_parser &parser, 
gimple_seq *seq)
                    {
                      gimple_stmt_iterator gsi
                        = gsi_start_bb (parser.current_bb);
-                     gsi_insert_seq_after (&gsi, *seq, GSI_CONTINUE_LINKING);
+                     gsi_insert_seq_after_without_update (&gsi, *seq,
+                                                          
GSI_CONTINUE_LINKING);
                    }
                  *seq = NULL;
                }
@@ -735,7 +741,8 @@ expr_stmt:
       else
        {
          gimple_stmt_iterator gsi = gsi_start_bb (parser.current_bb);
-         gsi_insert_seq_after (&gsi, *seq, GSI_CONTINUE_LINKING);
+         gsi_insert_seq_after_without_update (&gsi, *seq,
+                                              GSI_CONTINUE_LINKING);
        }
       *seq = NULL;
     }
diff --git a/gcc/testsuite/gcc.dg/torture/pr84830.c 
b/gcc/testsuite/gcc.dg/torture/pr84830.c
index d076170d2587..7921fa506b32 100644
--- a/gcc/testsuite/gcc.dg/torture/pr84830.c
+++ b/gcc/testsuite/gcc.dg/torture/pr84830.c
@@ -1,46 +1,140 @@
 /* { dg-do compile } */
-/* { dg-additional-options "-fno-tree-ch -fno-tree-vrp" } */
+/* { dg-additional-options "-fgimple" } */
 
 int x0;
 
-void
+void __GIMPLE (ssa,startwith("pre"))
 br (int yp, int oo)
 {
-  int *qi = &yp;
-
-  if (oo == 0)
-    {
-g8:
-      if (x0 != 0)
-       x0 = yp;
-      else if (oo != 0)
-       x0 = yp;
-
-      if (x0 == 0)
-       {
-         *qi = 0;
-         x0 = *qi;
-       }
-
-      if (x0 != 0)
-       {
-         ++oo;
-         goto g8;
-       }
-
-      if (yp == oo)
-       yp += !!oo;
-    }
+  int oo_lsm_17;
+  int x0_lsm_16;
+  _Bool x0_lsm_15;
+  int x0_lsm_14;
+  int * qi;
+  int _1;
+  int _2;
+  int _3;
+  int _4;
+  int _5;
+  int _6;
+  int _8;
+  int _9;
+  int _10;
+  int _11;
+  _Bool _12;
+  int _13;
+  int _14;
+  int _15;
+  int _16;
+  int _17;
+
+  __BB(2):
+  _1 = oo;
+  if (_1 == 0)
+    goto __BB13;
+  else
+    goto __BB11;
+
+  __BB(3):
+  x0_lsm_14_44 = __PHI (__BB10: x0_lsm_14_50, __BB14: x0_lsm_14_25, __BB9: 
x0_lsm_14_50);
+  goto __BB4;
+
+  __BB(4):
+  x0_lsm_14_39 = __PHI (__BB3: x0_lsm_14_44, __BB13: x0_lsm_14_38);
+  _2 = x0_lsm_14_39;
+  if (_2 != 0)
+    goto __BB5;
+  else
+    goto __BB6;
+
+  __BB(5):
+  _3 = yp;
+  x0_lsm_14_7 = _3;
+  x0_lsm_15_47 = _Literal (_Bool) 1;
+  goto __BB8;
+
+  __BB(6):
+  _4 = oo;
+  if (_4 != 0)
+    goto __BB7;
+  else
+    goto __BB9;
+
+  __BB(7):
+  _5 = yp;
+  x0_lsm_14_48 = _5;
+  x0_lsm_15_49 = _Literal (_Bool) 1;
+  goto __BB8;
+
+  __BB(8):
+  x0_lsm_14_25 = __PHI (__BB5: x0_lsm_14_7, __BB7: x0_lsm_14_48);
+  _6 = x0_lsm_14_25;
+  if (_6 == 0)
+    goto __BB9;
+  else
+    goto __BB14;
+
+  __BB(9):
+  __MEM <int> (qi_36) = 0;
+  x0_lsm_14_50 = 0;
+  x0_lsm_15_51 = _Literal (_Bool) 1;
+  _10 = yp;
+  _11 = oo;
+  if (_10 == _11)
+    goto __BB10;
+  else
+    goto __BB3;
+
+  __BB(10):
+  _12 = _11 != 0;
+  _13 = (int) _12;
+  _14 = _10 + _13;
+  yp = _14;
+  if (_14 == 0)
+    goto __BB17;
+  else
+    goto __BB3;
+
+  __BB(11):
+  x0 = 1;
+  oo_lsm_17_24 = oo;
+  x0_lsm_16_19 = x0;
+  goto __BB12;
+
+  __BB(12):
+  oo_lsm_17_41 = __PHI (__BB11: oo_lsm_17_24, __BB15: oo_lsm_17_43);
+  _15 = oo_lsm_17_41;
+  _16 = _15 + 1;
+  oo_lsm_17_43 = _16;
+  x0_lsm_16_42 = 1;
+  _17 = x0_lsm_16_42;
+  if (_17 <= 1)
+    goto __BB15;
   else
-    {
-      x0 = 1;
-      while (x0 < 2)
-       {
-         qi = &oo;
-         ++oo;
-         x0 = 1;
-       }
-    }
-
-  goto g8;
+    goto __BB16;
+
+  __BB(15):
+  goto __BB12;
+
+  __BB(16):
+  x0_lsm_16_52 = __PHI (__BB12: x0_lsm_16_42);
+  oo_lsm_17_53 = __PHI (__BB12: oo_lsm_17_43);
+  x0 = x0_lsm_16_52;
+  oo = oo_lsm_17_53;
+  goto __BB13;
+
+  __BB(13):
+  qi_36 = __PHI (__BB2: &yp, __BB16: &oo);
+  x0_lsm_14_38 = x0;
+  x0_lsm_15_23 = _Literal (_Bool) 0;
+  goto __BB4;
+
+  __BB(14):
+  _8 = oo;
+  _9 = _8 + 1;
+  oo = _9;
+  goto __BB3;
+
+  __BB(17):
+  return;
 }

Reply via email to