]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/111715 - improve TBAA for access paths with pun
authorRichard Biener <rguenther@suse.de>
Mon, 9 Oct 2023 11:05:10 +0000 (13:05 +0200)
committerRichard Biener <rguenther@suse.de>
Mon, 9 Oct 2023 13:17:11 +0000 (15:17 +0200)
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.

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
gcc/testsuite/gcc.dg/tree-ssa/ssa-fre-102.c [new file with mode: 0644]

index 7c1af1fe96ecbf0d8097db5ec0d9cd09da0c76cd..86d8f7104ad3d60f3f659f9cb3eb68dd5d9e2e2b 100644 (file)
@@ -774,7 +774,22 @@ 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.  */
+      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 (file)
index 0000000..afd4805
--- /dev/null
@@ -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" } } */