tree-optimization/111715 - improve TBAA for access paths with pun

Message ID 20231009111403.221E8385B53E@sourceware.org
State Accepted
Headers
Series tree-optimization/111715 - improve TBAA for access paths with pun |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Richard Biener Oct. 9, 2023, 11:13 a.m. UTC
  The following improves basic TBAA for access paths formed by
C++ abstraction where we are able to combine a path from an
address-taking operation with a path based on that access using
a pun to avoid memory access semantics on the address-taking part.

The trick is to identify the point the semantic memory access path
starts which allows us to use the alias set of the outermost access
instead of only that of the base of this path.

Bootstrapped and tested on x86_64-unknown-linux-gnu for all languages
with a slightly different variant, re-bootstrapping/testing now
(with doing the extra walk just for AGGREGATE_TYPE_P).

	PR tree-optimization/111715
	* alias.cc (reference_alias_ptr_type_1): When we have
	a type-punning ref at the base search for the access
	path part that's still semantically valid.

	* gcc.dg/tree-ssa/ssa-fre-102.c: New testcase.
---
 gcc/alias.cc                                | 20 ++++++++++++-
 gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-102.c | 32 +++++++++++++++++++++
 2 files changed, 51 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-102.c
  

Patch

diff --git a/gcc/alias.cc b/gcc/alias.cc
index 7c1af1fe96e..4060ff72949 100644
--- a/gcc/alias.cc
+++ b/gcc/alias.cc
@@ -774,7 +774,25 @@  reference_alias_ptr_type_1 (tree *t)
       && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
 	  != TYPE_MAIN_VARIANT
 	       (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
-    return TREE_TYPE (TREE_OPERAND (inner, 1));
+    {
+      tree alias_ptrtype = TREE_TYPE (TREE_OPERAND (inner, 1));
+      /* Unless we have the (aggregate) effective type of the access
+	 somewhere on the access path.  If we have for example
+	 (&a->elts[i])->l.len exposed by abstraction we'd see
+	 MEM <A> [(B *)a].elts[i].l.len and we can use the alias set
+	 of 'len' when typeof (MEM <A> [(B *)a].elts[i]) == B for
+	 example.  See PR111715.  */
+      if (AGGREGATE_TYPE_P (TREE_TYPE (alias_ptrtype)))
+	{
+	  tree inner = *t;
+	  while (handled_component_p (inner)
+		 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
+		     != TYPE_MAIN_VARIANT (TREE_TYPE (alias_ptrtype))))
+	    inner = TREE_OPERAND (inner, 0);
+	}
+      if (TREE_CODE (inner) == MEM_REF)
+	return alias_ptrtype;
+    }
 
   /* Otherwise, pick up the outermost object that we could have
      a pointer to.  */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-102.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-102.c
new file mode 100644
index 00000000000..afd48050819
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-102.c
@@ -0,0 +1,32 @@ 
+/* PR/111715 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-fre1" } */
+
+struct B {
+   struct { int len; } l;
+   long n;
+};
+struct A {
+   struct B elts[8];
+};
+
+static void
+set_len (struct B *b, int len)
+{
+  b->l.len = len;
+}
+
+static int
+get_len (struct B *b)
+{
+  return b->l.len;
+}
+
+int foo (struct A *a, int i, long *q)
+{
+  set_len (&a->elts[i], 1);
+  *q = 2;
+  return get_len (&a->elts[i]);
+}
+
+/* { dg-final { scan-tree-dump "return 1;" "fre1" } } */