Similar issues as in PR128637 exists in the same-val store skipping
code as noted by Sashiko.  Two resolve around redundant store
elimination so the correct action is to avoid same-val store
skipping when searching for an earlier redundant store.
Another is inserting into the hashtable with an earlier vuse
when we skipped defs in vn_walk_cb_data::finish.

This makes us no longer optimize gcc.dg/tree-ssa/pr91091-2.c
and rightfully so because removing the later store changes
the effective type of the storage if the objects are at the
same address.

        PR tree-optimization/128818
        * tree-ssa-sccvn.cc (vn_reference_lookup_3): Avoid skipping
        same-val may-defs when doing redundant store elimination.
        (vn_walk_cb_data::finish): Also skip hashtable insertion when
        same-val stores were skipped.

        * gcc.dg/torture/pr128818-1.c: New testcase.
        * gcc.dg/torture/pr128818-2.c: Likewise.
        * gcc.dg/torture/pr128818-3.c: Likewise.
        * gcc.dg/tree-ssa/pr91091-2.c: Remove invalid testcase.
---
 gcc/testsuite/gcc.dg/torture/pr128818-1.c | 30 +++++++++++++++++++
 gcc/testsuite/gcc.dg/torture/pr128818-2.c | 35 +++++++++++++++++++++++
 gcc/testsuite/gcc.dg/torture/pr128818-3.c | 35 +++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr91091-2.c | 15 ----------
 gcc/tree-ssa-sccvn.cc                     |  3 +-
 5 files changed, 102 insertions(+), 16 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr128818-1.c
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr128818-2.c
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr128818-3.c
 delete mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr91091-2.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr128818-1.c 
b/gcc/testsuite/gcc.dg/torture/pr128818-1.c
new file mode 100644
index 00000000000..a560e20541a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128818-1.c
@@ -0,0 +1,30 @@
+/* { dg-do run } */
+
+typedef __UINT64_TYPE__ u64;
+
+static inline u64
+g(u64 *restrict p, u64 *y, int k, int c)
+{
+  *y = 2;
+  p[k] = 0;
+  u64 r = 0;
+  if (c) r = *p;
+  return r;
+}
+
+__attribute__((noinline)) u64
+f(u64 *x, u64 *y, int k, int c)
+{
+  __builtin_memset(x, 0, 16);
+  u64 r = g(x, y, k, c);
+  return r + *x;
+}
+
+int main()
+{
+  u64 a[2];
+  u64 r = f(&a[0], &a[0], 1, 0);
+  if (r != 2)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr128818-2.c 
b/gcc/testsuite/gcc.dg/torture/pr128818-2.c
new file mode 100644
index 00000000000..4fb5f5793b2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128818-2.c
@@ -0,0 +1,35 @@
+/* { dg-do run } */
+
+struct A { int f; };
+struct B { int f; };
+
+__attribute__((noinline))
+int foo (struct B *b1, void *p, int c, int v)
+{
+  struct B *b = (struct B *)p;
+  struct A *a = (struct A *)p;
+
+  b1->f = 1;
+  b->f = v;
+
+  int k;
+  if (c)
+    k = 0;
+  else
+    k = c;
+
+  a[k].f = v;
+  b->f = v;
+
+  return b1->f;
+}
+
+int main ()
+{
+  void *p = __builtin_malloc (sizeof (struct A) > sizeof (struct B)
+                             ? sizeof (struct A) : sizeof (struct B));
+  int res = foo ((struct B *)p, p, 0, 42);
+  if (res != 42)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr128818-3.c 
b/gcc/testsuite/gcc.dg/torture/pr128818-3.c
new file mode 100644
index 00000000000..a9a28d9b41b
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128818-3.c
@@ -0,0 +1,35 @@
+/* { dg-do run } */
+
+struct A { int f; };
+struct B { int f; };
+
+__attribute__((noinline))
+int foo (struct B *b1, void *p, int c)
+{
+  struct B *b = (struct B *)p;
+  struct A *a = (struct A *)p;
+
+  b1->f = 1;
+  b->f = 0;
+
+  int k;
+  if (c)
+    k = 0;
+  else
+    k = c;
+
+  a[k].f = 0;
+  b->f = 0;
+
+  return b1->f;
+}
+
+int main ()
+{
+  void *p = __builtin_malloc (sizeof (struct A) > sizeof (struct B)
+                             ? sizeof (struct A) : sizeof (struct B));
+  int res = foo ((struct B *)p, p, 0);
+  if (res != 0)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr91091-2.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr91091-2.c
deleted file mode 100644
index 79254150490..00000000000
--- a/gcc/testsuite/gcc.dg/tree-ssa/pr91091-2.c
+++ /dev/null
@@ -1,15 +0,0 @@
-/* { dg-do compile } */
-/* { dg-options "-O2 -fdump-tree-fre1" } */
-
-struct s { int x; };
-struct t { int x; };
-
-void swap(struct s* p, struct t* q)
-{
-  p->x = q->x;
-  q->x = p->x;
-}
-
-/* The second statement is redundant.  */
-/* { dg-final { scan-tree-dump-times "x = " 1 "fre1" { xfail { ! 
natural_alignment_32 } } } } */
-/* { dg-final { scan-tree-dump-times " = \[^;\]*x;" 1 "fre1" } } */
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index afa23a6abf5..d989786307c 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -2048,7 +2048,7 @@ vn_walk_cb_data::finish (alias_set_type set, 
alias_set_type base_set, tree val,
     }
   if (same_val && !operand_equal_p (val, same_val))
     return (void *) -1;
-  if (!insert)
+  if (!insert || same_val)
     {
       memset (&dummy_vr, 0, sizeof (vn_reference_s));
       dummy_vr.result = val;
@@ -2963,6 +2963,7 @@ vn_reference_lookup_3 (ao_ref *ref, tree vuse, void 
*data_,
              || TREE_CODE (gimple_assign_rhs1 (def_stmt)) == CONSTRUCTOR)
          && (ref->ref || data->orig_ref.ref)
          && !data->mask
+         && !data->redundant_store
          && data->partial_defs.is_empty ()
          && multiple_p (get_object_alignment
                           (ref->ref ? ref->ref : data->orig_ref.ref),
-- 
2.51.0

Reply via email to