The following fixes miscompilations that result from partial-defs
support in value-numbering where the issue is that we ultimately
record a load combining multiple memory states into the expression
hashtable where it resembles a single store that is not there in
the IL.  Don't do that and skip expression insertion for loads
value-numbered this way.

Bootstrap and regtest running on x86_64-unknown-linux-gnu, will push
if that succeeds and sashiko is reasonably happy.

Richard.

        PR tree-optimization/128637
        PR tree-optimization/128688
        * tree-ssa-sccvn.cc (vn_walk_cb_data::dummy_vr): New member.
        (vn_walk_cb_data::using_dummy_vr): Likewise.
        (vn_walk_cb_data::vn_walk_cb_data): Initialize using_dummy_vr.
        (vn_walk_cb_data::finish): Add parameter to indicate whether
        to insert an expression and use the dummy_vr if not.
        (vn_walk_cb_data::push_partial_def): Skip expression insertion.
        (vn_reference_lookup_pieces): Do not expose the dummy VR.
        (vn_reference_lookup): Likewise.
        (eliminate_dom_walker::eliminate_stmt): When there is no
        actual single store found avoid redundant store elimination.

        * gcc.dg/torture/pr128637-1.c: New testcase.
        * gcc.dg/torture/pr128637-2.c: Likewise.
        * gcc.dg/torture/pr128688.c: Likewise.
---
 gcc/testsuite/gcc.dg/torture/pr128637-1.c | 27 +++++++++++++++++
 gcc/testsuite/gcc.dg/torture/pr128637-2.c | 24 +++++++++++++++
 gcc/testsuite/gcc.dg/torture/pr128688.c   | 32 ++++++++++++++++++++
 gcc/tree-ssa-sccvn.cc                     | 36 ++++++++++++++++-------
 4 files changed, 109 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr128637-1.c
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr128637-2.c
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr128688.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr128637-1.c 
b/gcc/testsuite/gcc.dg/torture/pr128637-1.c
new file mode 100644
index 00000000000..e50509e825f
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128637-1.c
@@ -0,0 +1,27 @@
+/* { dg-do run { target { lp64 && le } } } */
+
+void abort (void);
+void *malloc(unsigned long);
+void *memset(void *, int, unsigned long);
+
+int x;
+long r;
+long long __attribute__((noipa))
+foo (unsigned char *b, unsigned char *w, long long z)
+{
+  memset (b, 0, 8);
+  *(long long *)w = z;
+  b[4] = 1;
+  if (x)
+    r = *(long *)b;
+  return *(long long *)b;
+}
+
+int main(void)
+{
+  unsigned char *b = malloc(sizeof (long long));
+  long long t = foo (b, b, 1);
+  if (t != 0x100000001)
+    abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr128637-2.c 
b/gcc/testsuite/gcc.dg/torture/pr128637-2.c
new file mode 100644
index 00000000000..bc6640b67e1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128637-2.c
@@ -0,0 +1,24 @@
+/* { dg-do run { target { lp64 && le } } } */
+
+void abort (void);
+void *malloc(unsigned long);
+void *memset(void *, int, unsigned long);
+void *memcpy(void *, const void *, unsigned long);
+
+int x;
+long r;
+
+int main(void)
+{
+  unsigned char *b = malloc(sizeof (long long));
+  memset(b, 0, 8);
+  *(long long *)b = 1;
+  b[4] = 1;
+  if (x)
+    r = *(long *)b;
+  long long t;
+  memcpy(&t, b, 8);
+  if (t != 0x100000001)
+    abort ();
+  return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr128688.c 
b/gcc/testsuite/gcc.dg/torture/pr128688.c
new file mode 100644
index 00000000000..0633b0beeea
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128688.c
@@ -0,0 +1,32 @@
+/* { dg-do run } */
+/* { dg-require-effective-target int32plus } */
+/* { dg-require-effective-target le } */
+
+typedef __UINT64_TYPE__ u64;
+
+static inline u64
+g(u64 *restrict p, unsigned char *q, int c)
+{
+  *q = 1;                          /* byte 0, not through p                */
+  ((unsigned char *)p)[4] = 7;     /* byte 4, through p                     */
+  u64 r = 0;
+  if (c) r = *p;                   /* not executed (c == 0): no restrict UB */
+  return r;
+}
+
+__attribute__((noinline)) u64
+f(u64 *x, unsigned char *y, int c)
+{
+  *x = 5;
+  u64 r = g(x, y, c);
+  return r + *x;                   /* outside g: must see *y = 1            */
+}
+
+int main()
+{
+  u64 a;
+  u64 r = f(&a, (unsigned char *)&a, 0);
+  if (r != 0x700000001ULL)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 8dd1c92c44b..9379d1069b1 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -1947,7 +1947,7 @@ struct vn_walk_cb_data
       vn_walk_kind (vn_walk_kind_),
       tbaa_p (tbaa_p_), redundant_store_removal_p (redundant_store_removal_p_),
       saved_operands (vNULL), first_range (), first_set (-2),
-      first_base_set (-2)
+      first_base_set (-2), using_dummy_vr (false)
   {
     if (!last_vuse_ptr)
       last_vuse_ptr = &last_vuse;
@@ -1994,7 +1994,7 @@ struct vn_walk_cb_data
       }
   }
   ~vn_walk_cb_data ();
-  void *finish (alias_set_type, alias_set_type, tree);
+  void *finish (alias_set_type, alias_set_type, tree, bool insert = true);
   void *push_partial_def (pd_data pd,
                          alias_set_type, alias_set_type, HOST_WIDE_INT,
                          HOST_WIDE_INT);
@@ -2020,6 +2020,9 @@ struct vn_walk_cb_data
   default_splay_tree<pd_range *> known_ranges;
   obstack ranges_obstack;
   static constexpr HOST_WIDE_INT bufsize = 64;
+
+  bool using_dummy_vr;
+  vn_reference_s dummy_vr;
 };
 
 vn_walk_cb_data::~vn_walk_cb_data ()
@@ -2030,7 +2033,8 @@ vn_walk_cb_data::~vn_walk_cb_data ()
 }
 
 void *
-vn_walk_cb_data::finish (alias_set_type set, alias_set_type base_set, tree val)
+vn_walk_cb_data::finish (alias_set_type set, alias_set_type base_set, tree val,
+                        bool insert)
 {
   if (first_set != -2)
     {
@@ -2044,6 +2048,13 @@ 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)
+    {
+      memset (&dummy_vr, 0, sizeof (vn_reference_s));
+      dummy_vr.result = val;
+      using_dummy_vr = true;
+      return &dummy_vr;
+    }
   vec<vn_reference_op_s> &operands
     = saved_operands.exists () ? saved_operands : vr->operands;
   return vn_reference_lookup_or_insert_for_pieces (last_vuse, set, base_set,
@@ -2407,9 +2418,7 @@ vn_walk_cb_data::push_partial_def (pd_data pd,
       if (dump_file && (dump_flags & TDF_DETAILS))
        fprintf (dump_file,
                 "Successfully combined %u partial definitions\n", ndefs);
-      /* We are using the alias-set of the first store we encounter which
-        should be appropriate here.  */
-      return finish (first_set, first_base_set, val);
+      return finish (-2, -2, val, false);
     }
   else
     {
@@ -4289,6 +4298,12 @@ vn_reference_lookup_pieces (tree vuse, alias_set_type 
set,
          *vnresult = NULL;
          return NULL_TREE;
        }
+      if (data.using_dummy_vr)
+       {
+         tree val = (*vnresult)->result;
+         *vnresult = NULL;
+         return val;
+       }
     }
 
   if (*vnresult)
@@ -4432,6 +4447,8 @@ vn_reference_lookup (tree op, tree vuse, vn_lookup_kind 
kind,
              && (!wvnresult->result
                  || !operand_equal_p (wvnresult->result, data.same_val)))
            return NULL_TREE;
+         if (data.using_dummy_vr)
+           return wvnresult->result;
          if (vnresult)
            *vnresult = wvnresult;
          return wvnresult->result;
@@ -7620,14 +7637,13 @@ eliminate_dom_walker::eliminate_stmt (basic_block b, 
gimple_stmt_iterator *gsi)
                  && tree_nop_conversion_p (TREE_TYPE (rhs), TREE_TYPE (val)))))
        {
          /* We can only remove the later store if the former aliases
-            at least all accesses the later one does or if the store
-            was to readonly memory storing the same value.  */
+            at least all accesses the later one does.  */
          ao_ref lhs_ref;
          ao_ref_init (&lhs_ref, lhs);
          alias_set_type set = ao_ref_alias_set (&lhs_ref);
          alias_set_type base_set = ao_ref_base_alias_set (&lhs_ref);
-         if (! vnresult
-             || ((vnresult->set == set
+         if (vnresult
+             && ((vnresult->set == set
                   || alias_set_subset_of (set, vnresult->set))
                  && (vnresult->base_set == base_set
                      || alias_set_subset_of (base_set, vnresult->base_set))))
-- 
2.51.0

Reply via email to