]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
forwprop: Improve rejection of overlapping for copyprop of aggregates [PR121841]
authorAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Sun, 7 Sep 2025 16:33:07 +0000 (09:33 -0700)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Sun, 7 Sep 2025 20:46:39 +0000 (13:46 -0700)
Here we have:
tmp = src1[0];
dest1[0] = tmp;
where src1 and dest1 are decls.
We currently reject this as the bases are different but since the bases
are decls we know they won't overlap.
This adds the extra check to allow this.

Bootstrapped and tested on x86_64-linux-gnu.

PR tree-optimization/121841

gcc/ChangeLog:

* tree-ssa-forwprop.cc (optimize_agr_copyprop_1): Allow
two different decls as bases as non-overlapping bases.

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/copy-prop-aggregate-struct-1.c: New test.

Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
gcc/testsuite/gcc.dg/tree-ssa/copy-prop-aggregate-struct-1.c [new file with mode: 0644]
gcc/tree-ssa-forwprop.cc

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/copy-prop-aggregate-struct-1.c b/gcc/testsuite/gcc.dg/tree-ssa/copy-prop-aggregate-struct-1.c
new file mode 100644 (file)
index 0000000..3786250
--- /dev/null
@@ -0,0 +1,21 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-forwprop1-details -fdump-tree-optimized" } */
+/* PR tree-optimization/121841 */
+
+struct U2 {
+   int i[1024];
+};
+
+struct U2 g_284[1] = {{0UL}};
+struct U2 g_283[1];
+
+/* g_284[0] and g_283[0] are known not to
+   overlap so a copy prop can happen. */
+void func_1() {
+  struct U2 removeme;
+  removeme = g_284[0];
+  g_283[0] =  removeme;
+}
+
+/* { dg-final { scan-tree-dump-times "after previous" 1 "forwprop1" } } */
+/* { dg-final { scan-tree-dump-not "removeme " "optimized" } } */
index f244b12b6d138b5d23f573a957ac3ba2f955d2af..9c6f4b355d6de0c2e88aad03b4feb1517518ecc7 100644 (file)
@@ -1467,11 +1467,17 @@ optimize_agr_copyprop_1 (gimple *stmt, gimple *use_stmt,
       tree base1 = get_addr_base_and_unit_offset (dest2, &offset1);
       tree base2 = get_addr_base_and_unit_offset (src, &offset2);
       poly_int64 size = tree_to_poly_int64 (len);
+      /* If the bases are 2 different decls,
+        then there can be no overlapping.  */
+      if (base1 && base2
+         && DECL_P (base1) && DECL_P (base2)
+         && base1 != base2)
+       ;
       /* If we can't figure out the base or the bases are
         not equal then fall back to an alignment check.  */
-      if (!base1
-         || !base2
-         || !operand_equal_p (base1, base2))
+      else if (!base1
+              || !base2
+              || !operand_equal_p (base1, base2))
        {
          unsigned int align1 = get_object_alignment (src);
          unsigned int align2 = get_object_alignment (dest2);