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

commit r16-8428-gbd39cdaabfe7fa17b14bd34b812982b8212fb19a
Author: Andrew Pinski <[email protected]>
Date:   Wed Apr 1 10:43:17 2026 -0700

    phiprop: Fix speculating aggregate copies [PR124746]
    
    r16-5555-g952e145796da0f introduced a problem for aggregates.
    Allowing speculating non-trapping loads is ok but allowing
    speculating non-trapping aggregate copies is not valid as
    there is no ssa renaming for the lhs. So for aggregates,
    we need to treat it similar as a trapping load to find if
    it is speculating. Since we already reject 'delayed' loads
    for aggregates, we will skip over this load.
    
    Bootstrapped and tested on x86_64-linux-gnu.
    
            PR tree-optimization/124746
    gcc/ChangeLog:
    
            * tree-ssa-phiprop.cc (propagate_with_phi): Treat
            and aggregate copy as it is a trapping load.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/tree-ssa/pr124746-1.c: New test.
    
    Signed-off-by: Andrew Pinski <[email protected]>

Diff:
---
 gcc/testsuite/gcc.dg/tree-ssa/pr124746-1.c | 37 ++++++++++++++++++++++++++++++
 gcc/tree-ssa-phiprop.cc                    | 13 +++++++----
 2 files changed, 46 insertions(+), 4 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr124746-1.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr124746-1.c
new file mode 100644
index 000000000000..27ddce60a22f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr124746-1.c
@@ -0,0 +1,37 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -fno-tree-fre -fdump-tree-phiprop-details" } */
+
+/* PR tree-optimization/124746 */
+
+struct a {
+  int b;
+} c, e = {1}, f;
+int d, g, j, *k = &d, n;
+static int i;
+static struct a *h = &e;
+int main() {
+  for (; n < 1; n++) {
+    int l;
+    struct a *m = &c;
+  L:
+    if (k) {
+      d = l = 0;
+      for (; l < 1; l++) {
+        j = i;
+        if (j) {
+          struct a **o = &m;
+          if (g)
+            goto L;
+          *o = &f;
+        }
+      }
+    } else
+      *h = *m;
+  }
+  if (e.b != 1)
+    __builtin_abort();
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-not "Inserting PHI for result of load e =" 
"phiprop2" } }  */
+
diff --git a/gcc/tree-ssa-phiprop.cc b/gcc/tree-ssa-phiprop.cc
index ca8c7cfe04d0..668a302597c0 100644
--- a/gcc/tree-ssa-phiprop.cc
+++ b/gcc/tree-ssa-phiprop.cc
@@ -358,13 +358,18 @@ propagate_with_phi (basic_block bb, gphi *vphi, gphi *phi,
            && !gimple_has_volatile_ops (use_stmt)))
        continue;
 
-      if (canpossible_trap
+      bool aggregate = false;
+      if (!is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (use_stmt))))
+       aggregate = true;
+
+      if ((canpossible_trap || aggregate)
          && !dom_info_available_p (cfun, CDI_POST_DOMINATORS))
        calculate_dominance_info (CDI_POST_DOMINATORS);
 
       /* Only replace loads in blocks that post-dominate the PHI node.  That
-        makes sure we don't end up speculating trapping loads.  */
-      if (canpossible_trap
+        makes sure we don't end up speculating trapping loads or
+        aggregate stores won't happen speculating.  */
+      if ((canpossible_trap || aggregate)
          && !dominated_by_p (CDI_POST_DOMINATORS,
                              bb, gimple_bb (use_stmt)))
        delay = true;
@@ -409,7 +414,7 @@ propagate_with_phi (basic_block bb, gphi *vphi, gphi *phi,
 
       /* Found a proper dereference with an aggregate copy.  Just
          insert aggregate copies on the edges instead.  */
-      if (!is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (use_stmt))))
+      if (aggregate)
        {
          /* aggregate copies are too hard to handled if delayed.  */
          if (delay)

Reply via email to