https://gcc.gnu.org/g:59412e46c04fc5a6142283983ae7f8ff3d520c88
commit r17-6407-g59412e46c04fc5a6142283983ae7f8ff3d520c88 Author: Richard Biener <[email protected]> Date: Tue Nov 17 15:51:51 2026 +0100 tree-optimization/128818 - wrong-code with VN skipping same-val store Similar issues as in PR128637 exist in the same-val store skipping code as noted by Sashiko. Two revolve 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 the apparent redundant store in 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: Adjust. Diff: --- 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 | 5 +++-- gcc/tree-ssa-sccvn.cc | 3 ++- 5 files changed, 105 insertions(+), 3 deletions(-) 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 000000000000..a560e20541a2 --- /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 000000000000..4fb5f5793b28 --- /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 000000000000..a9a28d9b41b1 --- /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 index 792541504903..0f578ed04083 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/pr91091-2.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr91091-2.c @@ -10,6 +10,7 @@ void swap(struct s* p, struct t* q) q->x = p->x; } -/* The second statement is redundant. */ -/* { dg-final { scan-tree-dump-times "x = " 1 "fre1" { xfail { ! natural_alignment_32 } } } } */ +/* The second statement is _not_ redundant, it may change the dynamic + type back from struct s to struct t. */ +/* { dg-final { scan-tree-dump-times "x = " 2 "fre1" } } */ /* { dg-final { scan-tree-dump-times " = \[^;\]*x;" 1 "fre1" } } */ diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc index 828dc2d4703f..e78d54bb898c 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; @@ -2961,6 +2961,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),
