public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-7371] sccvn: Fix visit_reference_op_call value numbering of vdefs [PR104601]
@ 2022-02-24 14:30 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2022-02-24 14:30 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:9251b457eb8df912f2e8203d0ee8ab300c041520

commit r12-7371-g9251b457eb8df912f2e8203d0ee8ab300c041520
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Thu Feb 24 15:29:02 2022 +0100

    sccvn: Fix visit_reference_op_call value numbering of vdefs [PR104601]
    
    The following testcase is miscompiled, because -fipa-pure-const discovers
    that bar is const, but when sccvn during fre3 sees
      # .MEM_140 = VDEF <.MEM_96>
      *__pred$__d_43 = _50 (_49);
    where _50 value numbers to &bar, it value numbers .MEM_140 to
    vuse_ssa_val (gimple_vuse (stmt)).  For const/pure calls that return
    a SSA_NAME (or don't have lhs) that is fine, those calls don't store
    anything, but if the lhs is present and not an SSA_NAME, value numbering
    the vdef to anything but itself means that e.g. walk_non_aliased_vuses
    won't consider the call, but the call acts as a store to its lhs.
    When it is ignored, sccvn will return whatever has been stored to the
    lhs earlier.
    
    I've bootstrapped/regtested an earlier version of this patch, which did the
    if (!lhs && gimple_call_lhs (stmt))
      changed |= set_ssa_val_to (vdef, vdef);
    part before else if (vnresult->result_vdef), and that regressed
    +FAIL: gcc.dg/pr51879-16.c scan-tree-dump-times pre "foo \\\\(" 1
    +FAIL: gcc.dg/pr51879-16.c scan-tree-dump-times pre "foo2 \\\\(" 1
    so this updated patch uses result_vdef there as before and only otherwise
    (which I think must be the const/pure case) decides based on whether the
    lhs is non-SSA_NAME.
    
    2022-02-24  Jakub Jelinek  <jakub@redhat.com>
    
            PR tree-optimization/104601
            * tree-ssa-sccvn.cc (visit_reference_op_call): For calls with
            non-SSA_NAME lhs value number vdef to itself instead of e.g. the
            vuse value number.
    
            * g++.dg/torture/pr104601.C: New test.

Diff:
---
 gcc/testsuite/g++.dg/torture/pr104601.C | 32 ++++++++++++++++++++++++++++++++
 gcc/tree-ssa-sccvn.cc                   | 26 +++++++++++++++++++-------
 2 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/gcc/testsuite/g++.dg/torture/pr104601.C b/gcc/testsuite/g++.dg/torture/pr104601.C
new file mode 100644
index 00000000000..bb56479c745
--- /dev/null
+++ b/gcc/testsuite/g++.dg/torture/pr104601.C
@@ -0,0 +1,32 @@
+// PR tree-optimization/104601
+// { dg-do run }
+// { dg-options "-std=c++17" }
+
+#include <algorithm>
+#include <optional>
+
+inline std::optional<int>
+foo (std::vector<int>::iterator b, std::vector<int>::iterator c,
+     std::optional<int> h (int))
+{
+  std::optional<int> d;
+  find_if (b, c, [&](auto e) { d = h(e); return d; });
+  return d;
+}
+
+std::optional<int>
+bar (int)
+{
+  return 1;
+}
+
+int
+main ()
+{
+  std::vector<int> g(10);
+  auto b = g.begin ();
+  auto c = g.end ();
+  auto e = foo (b, c, bar);
+  if (!e)
+    __builtin_abort ();
+}
diff --git a/gcc/tree-ssa-sccvn.cc b/gcc/tree-ssa-sccvn.cc
index eb17549c185..1701861faed 100644
--- a/gcc/tree-ssa-sccvn.cc
+++ b/gcc/tree-ssa-sccvn.cc
@@ -5218,12 +5218,20 @@ visit_reference_op_call (tree lhs, gcall *stmt)
 
   if (vnresult)
     {
-      if (vnresult->result_vdef && vdef)
-	changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
-      else if (vdef)
-	/* If the call was discovered to be pure or const reflect
-	   that as far as possible.  */
-	changed |= set_ssa_val_to (vdef, vuse_ssa_val (gimple_vuse (stmt)));
+      if (vdef)
+	{
+	  if (vnresult->result_vdef)
+	    changed |= set_ssa_val_to (vdef, vnresult->result_vdef);
+	  else if (!lhs && gimple_call_lhs (stmt))
+	    /* If stmt has non-SSA_NAME lhs, value number the vdef to itself,
+	       as the call still acts as a lhs store.  */
+	    changed |= set_ssa_val_to (vdef, vdef);
+	  else
+	    /* If the call was discovered to be pure or const reflect
+	       that as far as possible.  */
+	    changed |= set_ssa_val_to (vdef,
+				       vuse_ssa_val (gimple_vuse (stmt)));
+	}
 
       if (!vnresult->result && lhs)
 	vnresult->result = lhs;
@@ -5248,7 +5256,11 @@ visit_reference_op_call (tree lhs, gcall *stmt)
 	      if (TREE_CODE (fn) == ADDR_EXPR
 		  && TREE_CODE (TREE_OPERAND (fn, 0)) == FUNCTION_DECL
 		  && (flags_from_decl_or_type (TREE_OPERAND (fn, 0))
-		      & (ECF_CONST | ECF_PURE)))
+		      & (ECF_CONST | ECF_PURE))
+		  /* If stmt has non-SSA_NAME lhs, value number the
+		     vdef to itself, as the call still acts as a lhs
+		     store.  */
+		  && (lhs || gimple_call_lhs (stmt) == NULL_TREE))
 		vdef_val = vuse_ssa_val (gimple_vuse (stmt));
 	    }
 	  changed |= set_ssa_val_to (vdef, vdef_val);


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

only message in thread, other threads:[~2022-02-24 14:30 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-24 14:30 [gcc r12-7371] sccvn: Fix visit_reference_op_call value numbering of vdefs [PR104601] Jakub Jelinek

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).