public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <rguenther@suse.de>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] tree-optimization/106922 - extend same-val clobber FRE
Date: Fri, 23 Sep 2022 14:32:57 +0200 (CEST)	[thread overview]
Message-ID: <20220923123258.176D213A00@imap2.suse-dmz.suse.de> (raw)

The following extends the skipping of same valued stores to
handle an arbitrary number of them as long as they are from the
same value (which we now record).  That's an obvious extension
which allows to optimize the m_engaged member of std::optional
more reliably.

Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.

	PR tree-optimization/106922
	* tree-ssa-sccvn.cc (vn_reference_lookup_3): Allow
	an arbitrary number of same valued skipped stores.

	* g++.dg/torture/pr106922.C: New testcase.
---
 gcc/testsuite/g++.dg/torture/pr106922.C | 48 +++++++++++++++++++++++++
 gcc/tree-ssa-sccvn.cc                   | 10 ++++--
 2 files changed, 55 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/torture/pr106922.C

diff --git a/gcc/testsuite/g++.dg/torture/pr106922.C b/gcc/testsuite/g++.dg/torture/pr106922.C
new file mode 100644
index 00000000000..046fc6cce76
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/pr106922.C
@@ -0,0 +1,48 @@
+// { dg-do compile }
+// { dg-require-effective-target c++17 }
+// { dg-additional-options "-Wall" }
+// -O1 doesn't iterate VN and thus has bogus uninit diagnostics
+// { dg-skip-if "" { *-*-* } { "-O1" } { "" } }
+
+#include <vector>
+
+#include <optional>
+template <class T>
+using Optional = std::optional<T>;
+
+#include <sstream>
+
+struct MyOptionalStructWithInt {
+    int myint; /* works without this */
+    Optional<std::vector<std::string>> myoptional;
+};
+
+struct MyOptionalsStruct {
+    MyOptionalStructWithInt external1;
+    MyOptionalStructWithInt external2;
+};
+
+struct MyStruct { };
+std::ostream &operator << (std::ostream &os, const MyStruct &myStruct);
+
+std::vector<MyStruct> getMyStructs();
+
+void test()
+{
+    MyOptionalsStruct externals;
+    MyOptionalStructWithInt internal1;
+    MyOptionalStructWithInt internal2;
+
+    std::vector<MyStruct> myStructs;
+    myStructs = getMyStructs();
+
+    for (const auto& myStruct : myStructs)
+    {
+        std::stringstream address_stream;
+        address_stream << myStruct;
+        internal1.myint = internal2.myint = 0;
+        externals.external1 = internal1;
+        externals.external2 = internal2;
+        externals.external2 = internal2;
+    }
+}
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index 9c12a8e4f03..2cc2c0e1e34 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -2680,7 +2680,6 @@ vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
       if (is_gimple_reg_type (TREE_TYPE (lhs))
 	  && types_compatible_p (TREE_TYPE (lhs), vr->type)
 	  && (ref->ref || data->orig_ref.ref)
-	  && !data->same_val
 	  && !data->mask
 	  && data->partial_defs.is_empty ()
 	  && multiple_p (get_object_alignment
@@ -2693,8 +2692,13 @@ vn_reference_lookup_3 (ao_ref *ref, tree vuse, void *data_,
 	     a different loop iteration but only to loop invariants.  Use
 	     CONSTANT_CLASS_P (unvalueized!) as conservative approximation.
 	     The one-hop lookup below doesn't have this issue since there's
-	     a virtual PHI before we ever reach a backedge to cross.  */
-	  if (CONSTANT_CLASS_P (rhs))
+	     a virtual PHI before we ever reach a backedge to cross.
+	     We can skip multiple defs as long as they are from the same
+	     value though.  */
+	  if (data->same_val
+	      && !operand_equal_p (data->same_val, rhs))
+	    ;
+	  else if (CONSTANT_CLASS_P (rhs))
 	    {
 	      if (dump_file && (dump_flags & TDF_DETAILS))
 		{
-- 
2.35.3

                 reply	other threads:[~2022-09-23 12:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220923123258.176D213A00@imap2.suse-dmz.suse.de \
    --to=rguenther@suse.de \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).