public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-3325] tree-optimization/111019 - invariant motion and aliasing
@ 2023-08-18 13:05 Richard Biener
  0 siblings, 0 replies; only message in thread
From: Richard Biener @ 2023-08-18 13:05 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:745ec2135aabfbe2c0fb7780309837d17e8986d4

commit r14-3325-g745ec2135aabfbe2c0fb7780309837d17e8986d4
Author: Richard Biener <rguenther@suse.de>
Date:   Thu Aug 17 15:21:33 2023 +0200

    tree-optimization/111019 - invariant motion and aliasing
    
    The following fixes a bad choice in representing things to the alias
    oracle by LIM which while correct in pieces is inconsistent with itself.
    When canonicalizing a ref to a bare deref instead of leaving the base
    object and the extracted offset the same and just substituting an
    alternate ref the following replaces the base and the offset as well,
    avoiding the confusion that otherwise will arise in
    aliasing_matching_component_refs_p.
    
            PR tree-optimization/111019
            * tree-ssa-loop-im.cc (gather_mem_refs_stmt): When canonicalizing
            also scrap base and offset in case the ref is indirect.
    
            * g++.dg/torture/pr111019.C: New testcase.

Diff:
---
 gcc/testsuite/g++.dg/torture/pr111019.C | 65 +++++++++++++++++++++++++++++++++
 gcc/tree-ssa-loop-im.cc                 | 14 ++++++-
 2 files changed, 77 insertions(+), 2 deletions(-)

diff --git a/gcc/testsuite/g++.dg/torture/pr111019.C b/gcc/testsuite/g++.dg/torture/pr111019.C
new file mode 100644
index 000000000000..ce21a311c96e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/pr111019.C
@@ -0,0 +1,65 @@
+// { dg-do run }
+// { dg-additional-options "-fstrict-aliasing" }
+
+#include <cassert>
+#include <memory>
+#include <string>
+
+class Base
+{
+public:
+  Base* previous = nullptr;
+  Base* next = nullptr;
+  Base* target = nullptr;
+};
+
+class Target : public Base
+{
+public:
+  __attribute__((always_inline)) ~Target()
+  {
+    while (this->next)
+    {
+      Base* n = this->next;
+
+      if (n->previous)
+        n->previous->next = n->next;
+      if (n->next)
+        n->next->previous = n->previous;
+      n->previous = nullptr;
+      n->next = nullptr;
+      n->target = nullptr;
+    }
+  }
+};
+
+template <typename T>
+class TargetWithData final : public Target
+{
+public:
+  TargetWithData(T data)
+    : data(data)
+  {}
+  T data;
+};
+
+void test()
+{
+  printf("test\n");
+  Base ptr;
+  {
+    auto data = std::make_unique<TargetWithData<std::string>>(std::string("asdf"));
+    ptr.target = &*data;
+    ptr.previous = &*data;
+    data->next = &ptr;
+
+    assert(ptr.target != nullptr);
+  }
+  assert(ptr.target == nullptr);
+}
+
+int main(int, char**)
+{
+  test();
+  return 0;
+}
diff --git a/gcc/tree-ssa-loop-im.cc b/gcc/tree-ssa-loop-im.cc
index 268f466bdc9f..b8e33a4d49a1 100644
--- a/gcc/tree-ssa-loop-im.cc
+++ b/gcc/tree-ssa-loop-im.cc
@@ -1665,11 +1665,21 @@ gather_mem_refs_stmt (class loop *loop, gimple *stmt)
 				     unshare_expr (mem_base));
 		  if (TYPE_ALIGN (ref_type) != ref_align)
 		    ref_type = build_aligned_type (ref_type, ref_align);
-		  (*slot)->mem.ref
+		  tree new_ref
 		    = fold_build2 (MEM_REF, ref_type, tmp,
 				   build_int_cst (ref_alias_type, mem_off));
 		  if ((*slot)->mem.volatile_p)
-		    TREE_THIS_VOLATILE ((*slot)->mem.ref) = 1;
+		    TREE_THIS_VOLATILE (new_ref) = 1;
+		  (*slot)->mem.ref = new_ref;
+		  /* Make sure the recorded base and offset are consistent
+		     with the newly built ref.  */
+		  if (TREE_CODE (TREE_OPERAND (new_ref, 0)) == ADDR_EXPR)
+		    ;
+		  else
+		    {
+		      (*slot)->mem.base = new_ref;
+		      (*slot)->mem.offset = 0;
+		    }
 		  gcc_checking_assert (TREE_CODE ((*slot)->mem.ref) == MEM_REF
 				       && is_gimple_mem_ref_addr
 				            (TREE_OPERAND ((*slot)->mem.ref,

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

only message in thread, other threads:[~2023-08-18 13:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-18 13:05 [gcc r14-3325] tree-optimization/111019 - invariant motion and aliasing 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).