tree-optimization/111123 - indirect clobbers thrown away too early

Message ID 20230824090320.9947D385DC19@sourceware.org
State Accepted
Headers
Series tree-optimization/111123 - indirect clobbers thrown away too early |

Checks

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

Commit Message

Richard Biener Aug. 24, 2023, 9:02 a.m. UTC
  The testcase in the PR shows that late uninit diagnostic relies
on indirect clobbers in CTORs but we throw those away in the fab
pass which is too early.  The reasoning was they were supposed
to keep SSA names live but that's no longer the case since DCE
doesn't treat them as keeping SSA uses live.

The following instead removes them before out-of-SSA coalescing
which is the thing that's still affected by them.

Bootstrapped and tested on x86_64-unknown-linux-gnu, I'll push
this later if there are no quick comments.

Richard.

	PR tree-optimization/111123
	* tree-ssa-ccp.cc (pass_fold_builtins::execute): Do not
	remove indirect clobbers here ...
	* tree-outof-ssa.cc (rewrite_out_of_ssa): ... but here.
	(remove_indirect_clobbers): New function.

	* g++.dg/warn/Wuninitialized-pr111123-1.C: New testcase.
---
 .../g++.dg/warn/Wuninitialized-pr111123-1.C   | 20 ++++++++++++
 gcc/tree-outof-ssa.cc                         | 32 ++++++++++++++++++-
 gcc/tree-ssa-ccp.cc                           | 17 +---------
 3 files changed, 52 insertions(+), 17 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wuninitialized-pr111123-1.C
  

Patch

diff --git a/gcc/testsuite/g++.dg/warn/Wuninitialized-pr111123-1.C b/gcc/testsuite/g++.dg/warn/Wuninitialized-pr111123-1.C
new file mode 100644
index 00000000000..17a6f2b28d8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wuninitialized-pr111123-1.C
@@ -0,0 +1,20 @@ 
+// { dg-do compile }
+// { dg-require-effective-target c++11 }
+// { dg-options "-O -Wuninitialized" }
+
+#include <vector>
+
+struct Camera {
+    struct P2d {
+            float x, y;
+    };
+    std::vector<P2d> clip_area;
+    float border = 10.f;
+    [[gnu::noinline]] Camera() : clip_area({{border,border}}) { } // { dg-warning "uninitialized" }
+};
+
+Camera foo()
+{
+  Camera c;
+  return c;
+}
diff --git a/gcc/tree-outof-ssa.cc b/gcc/tree-outof-ssa.cc
index 0bf57a4bbc5..767623ab8ea 100644
--- a/gcc/tree-outof-ssa.cc
+++ b/gcc/tree-outof-ssa.cc
@@ -1283,6 +1283,33 @@  insert_backedge_copies (void)
     }
 }
 
+/* Remove indirect clobbers.  */
+
+static void
+remove_indirect_clobbers (void)
+{
+  basic_block bb;
+
+  FOR_EACH_BB_FN (bb, cfun)
+    for (auto gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
+      {
+	gimple *stmt = gsi_stmt (gsi);
+	if (gimple_clobber_p (stmt))
+	  {
+	    tree lhs = gimple_assign_lhs (stmt);
+	    if (TREE_CODE (lhs) == MEM_REF
+		&& TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
+	      {
+		unlink_stmt_vdef (stmt);
+		gsi_remove (&gsi, true);
+		release_defs (stmt);
+		continue;
+	      }
+	  }
+	gsi_next (&gsi);
+      }
+}
+
 /* Free all memory associated with going out of SSA form.  SA is
    the outof-SSA info object.  */
 
@@ -1305,6 +1332,10 @@  finish_out_of_ssa (struct ssaexpand *sa)
 unsigned int
 rewrite_out_of_ssa (struct ssaexpand *sa)
 {
+  /* Remove remaining indirect clobbers as we do not need those anymore.
+     Those might extend SSA lifetime and restrict coalescing.  */
+  remove_indirect_clobbers ();
+
   /* If elimination of a PHI requires inserting a copy on a backedge,
      then we will have to split the backedge which has numerous
      undesirable performance effects.
@@ -1313,7 +1344,6 @@  rewrite_out_of_ssa (struct ssaexpand *sa)
      copies into the loop itself.  */
   insert_backedge_copies ();
 
-
   /* Eliminate PHIs which are of no use, such as virtual or dead phis.  */
   eliminate_useless_phis ();
 
diff --git a/gcc/tree-ssa-ccp.cc b/gcc/tree-ssa-ccp.cc
index 15e65f16008..e048675577f 100644
--- a/gcc/tree-ssa-ccp.cc
+++ b/gcc/tree-ssa-ccp.cc
@@ -4302,22 +4302,7 @@  pass_fold_builtins::execute (function *fun)
 
           if (gimple_code (stmt) != GIMPLE_CALL)
 	    {
-	      /* Remove all *ssaname_N ={v} {CLOBBER}; stmts,
-		 after the last GIMPLE DSE they aren't needed and might
-		 unnecessarily keep the SSA_NAMEs live.  */
-	      if (gimple_clobber_p (stmt))
-		{
-		  tree lhs = gimple_assign_lhs (stmt);
-		  if (TREE_CODE (lhs) == MEM_REF
-		      && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
-		    {
-		      unlink_stmt_vdef (stmt);
-		      gsi_remove (&i, true);
-		      release_defs (stmt);
-		      continue;
-		    }
-		}
-	      else if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
+	      if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
 		optimize_memcpy (&i, gimple_assign_lhs (stmt),
 				 gimple_assign_rhs1 (stmt), NULL_TREE);
 	      gsi_next (&i);