public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-7612] tree-optimization/110799 - fix bug in code hoisting
@ 2023-07-27 10:34 Richard Biener
  0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2023-07-27 10:34 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:064a4ec9d7107c8278a759cb61dc161548c846ee

commit r13-7612-g064a4ec9d7107c8278a759cb61dc161548c846ee
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Jul 26 09:28:10 2023 +0200

    tree-optimization/110799 - fix bug in code hoisting
    
    Code hoisting part of GIMPLE PRE failed to adjust the TBAA behavior
    of common loads in the case the alias set of the ref was the same
    but the base alias set was not.  It also failed to adjust the
    base behavior, assuming it would match.  The following plugs this
    hole.
    
            PR tree-optimization/110799
            * tree-ssa-pre.cc (compute_avail): More thoroughly match
            up TBAA behavior of redundant loads.
    
            * gcc.dg/torture/pr110799.c: New testcase.
    
    (cherry picked from commit 565e0e80660dac24e5193873ca07542b99cd8bc3)

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr110799.c | 46 +++++++++++++++++++++++++++++++++
 gcc/tree-ssa-pre.cc                     | 15 +++++++----
 2 files changed, 56 insertions(+), 5 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/torture/pr110799.c b/gcc/testsuite/gcc.dg/torture/pr110799.c
new file mode 100644
index 00000000000..53f06f079e1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr110799.c
@@ -0,0 +1,46 @@
+/* { dg-do run { target { { *-*-linux* *-*-gnu* *-*-uclinux* } && mmap } } } */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/mman.h>
+
+struct S {
+    int a;
+};
+struct M {
+    int a, b;
+};
+
+int __attribute__((noipa))
+f(struct S *p, int c, int d)
+{
+  int r;
+  if (c)
+    {
+      if (d)
+	r = p->a;
+      else
+	r = ((struct M*)p)->a;
+    }
+  else
+    r = ((struct M*)p)->b;
+  return r;
+}
+
+int main ()
+{
+  long pgsz = sysconf(_SC_PAGESIZE);
+  if (pgsz < sizeof (struct M))
+    return 0;
+  char *p = mmap ((void *) 0, 2 * pgsz, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS,
+		  -1, 0);
+  if (p == MAP_FAILED)
+    return 0;
+  if (mprotect (p, pgsz, PROT_READ | PROT_WRITE))
+    return 0;
+  struct S *q = (struct S *)(p + pgsz) - 1;
+  q->a = 42;
+  if (f (q, 1, 1) != 42)
+    abort ();
+  return 0;
+}
diff --git a/gcc/tree-ssa-pre.cc b/gcc/tree-ssa-pre.cc
index 7bc96ba95fa..dfcc9ec4711 100644
--- a/gcc/tree-ssa-pre.cc
+++ b/gcc/tree-ssa-pre.cc
@@ -4181,8 +4181,10 @@ compute_avail (function *fun)
 		      /* TBAA behavior is an obvious part so make sure
 		         that the hashtable one covers this as well
 			 by adjusting the ref alias set and its base.  */
-		      if (ref->set == set
-			  || alias_set_subset_of (set, ref->set))
+		      if ((ref->set == set
+			   || alias_set_subset_of (set, ref->set))
+			  && (ref->base_set == base_set
+			      || alias_set_subset_of (base_set, ref->base_set)))
 			;
 		      else if (ref1->opcode != ref2->opcode
 			       || (ref1->opcode != MEM_REF
@@ -4194,16 +4196,19 @@ compute_avail (function *fun)
 			  operands.release ();
 			  continue;
 			}
-		      else if (alias_set_subset_of (ref->set, set))
+		      else if (ref->set == set
+			       || alias_set_subset_of (ref->set, set))
 			{
+			  tree reft = reference_alias_ptr_type (rhs1);
 			  ref->set = set;
+			  ref->base_set = set;
 			  if (ref1->opcode == MEM_REF)
 			    ref1->op0
-			      = wide_int_to_tree (TREE_TYPE (ref2->op0),
+			      = wide_int_to_tree (reft,
 						  wi::to_wide (ref1->op0));
 			  else
 			    ref1->op2
-			      = wide_int_to_tree (TREE_TYPE (ref2->op2),
+			      = wide_int_to_tree (reft,
 						  wi::to_wide (ref1->op2));
 			}
 		      else

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-07-27 10:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-27 10:34 [gcc r13-7612] tree-optimization/110799 - fix bug in code hoisting Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).