diff mbox series

More last_stmt removal

Message ID 20230503125824.813C913584@imap2.suse-dmz.suse.de
State New
Headers show
Series More last_stmt removal | expand

Commit Message

Richard Biener May 3, 2023, 12:58 p.m. UTC
This is the last set of changes removing calls to last_stmt in favor of
*gsi_last_bb where this is obviously correct.  As with the last changes
I tried to cleanup the code as far as dependences are concerned.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

	* tree-ssa-loop-split.cc (split_at_bb_p): Avoid last_stmt.
	(patch_loop_exit): Likewise.
	(connect_loops): Likewise.
	(split_loop): Likewise.
	(control_dep_semi_invariant_p): Likewise.
	(do_split_loop_on_cond): Likewise.
	(split_loop_on_cond): Likewise.
	* tree-ssa-loop-unswitch.cc (find_unswitching_predicates_for_bb):
	Likewise.
	(simplify_loop_version): Likewise.
	(evaluate_bbs): Likewise.
	(find_loop_guard): Likewise.
	(clean_up_after_unswitching): Likewise.
	* tree-ssa-math-opts.cc (maybe_optimize_guarding_check):
	Likewise.
	(optimize_spaceship): Take a gcond * argument, avoid
	last_stmt.
	(math_opts_dom_walker::after_dom_children): Adjust call to
	optimize_spaceship.
	* tree-vrp.cc (maybe_set_nonzero_bits): Avoid last_stmt.
	* value-pointer-equiv.cc (pointer_equiv_analyzer::visit_edge):
	Likewise.
---
 gcc/tree-ssa-loop-split.cc    | 29 +++++++++++---------------
 gcc/tree-ssa-loop-unswitch.cc | 10 ++++-----
 gcc/tree-ssa-math-opts.cc     | 39 ++++++++++++++---------------------
 gcc/tree-vrp.cc               | 13 ++++++------
 gcc/value-pointer-equiv.cc    |  5 ++---
 5 files changed, 41 insertions(+), 55 deletions(-)
diff mbox series

Patch

diff --git a/gcc/tree-ssa-loop-split.cc b/gcc/tree-ssa-loop-split.cc
index fca678113eb..e5e6aa8eede 100644
--- a/gcc/tree-ssa-loop-split.cc
+++ b/gcc/tree-ssa-loop-split.cc
@@ -76,15 +76,13 @@  along with GCC; see the file COPYING3.  If not see
 static tree
 split_at_bb_p (class loop *loop, basic_block bb, tree *border, affine_iv *iv)
 {
-  gimple *last;
   gcond *stmt;
   affine_iv iv2;
 
   /* BB must end in a simple conditional jump.  */
-  last = last_stmt (bb);
-  if (!last || gimple_code (last) != GIMPLE_COND)
+  stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
+  if (!stmt)
     return NULL_TREE;
-  stmt = as_a <gcond *> (last);
 
   enum tree_code code = gimple_cond_code (stmt);
 
@@ -158,7 +156,7 @@  patch_loop_exit (class loop *loop, gcond *guard, tree nextval, tree newbound,
 		 bool initial_true)
 {
   edge exit = single_exit (loop);
-  gcond *stmt = as_a <gcond *> (last_stmt (exit->src));
+  gcond *stmt = as_a <gcond *> (*gsi_last_bb (exit->src));
   gimple_cond_set_condition (stmt, gimple_cond_code (guard),
 			     nextval, newbound);
   update_stmt (stmt);
@@ -335,7 +333,7 @@  connect_loops (class loop *loop1, class loop *loop2)
   gimple_stmt_iterator gsi;
   edge new_e, skip_e;
 
-  gimple *stmt = last_stmt (exit->src);
+  gcond *stmt = as_a <gcond *> (*gsi_last_bb (exit->src));
   skip_stmt = gimple_build_cond (gimple_cond_code (stmt),
 				 gimple_cond_lhs (stmt),
 				 gimple_cond_rhs (stmt),
@@ -571,7 +569,7 @@  split_loop (class loop *loop1)
 	gphi *phi = find_or_create_guard_phi (loop1, guard_iv, &iv);
 	if (!phi)
 	  continue;
-	gcond *guard_stmt = as_a<gcond *> (last_stmt (bbs[i]));
+	gcond *guard_stmt = as_a<gcond *> (*gsi_last_bb (bbs[i]));
 	tree guard_init = PHI_ARG_DEF_FROM_EDGE (phi,
 						 loop_preheader_edge (loop1));
 	enum tree_code guard_code = gimple_cond_code (guard_stmt);
@@ -655,8 +653,8 @@  split_loop (class loop *loop1)
 
 	/* Finally patch out the two copies of the condition to be always
 	   true/false (or opposite).  */
-	gcond *force_true = as_a<gcond *> (last_stmt (bbs[i]));
-	gcond *force_false = as_a<gcond *> (last_stmt (get_bb_copy (bbs[i])));
+	gcond *force_true = as_a<gcond *> (*gsi_last_bb (bbs[i]));
+	gcond *force_false = as_a<gcond *> (*gsi_last_bb (get_bb_copy (bbs[i])));
 	if (!initial_true)
 	  std::swap (force_true, force_false);
 	gimple_cond_make_true (force_true);
@@ -1148,8 +1146,7 @@  control_dep_semi_invariant_p (struct loop *loop, basic_block bb,
   for (hash_set<basic_block>::iterator iter = dep_bbs->begin ();
        iter != dep_bbs->end (); ++iter)
     {
-      gimple *last = last_stmt (*iter);
-
+      gimple *last = *gsi_last_bb (*iter);
       if (!last)
 	return false;
 
@@ -1520,7 +1517,7 @@  do_split_loop_on_cond (struct loop *loop1, edge invar_branch)
 {
   basic_block cond_bb = invar_branch->src;
   bool true_invar = !!(invar_branch->flags & EDGE_TRUE_VALUE);
-  gcond *cond = as_a <gcond *> (last_stmt (cond_bb));
+  gcond *cond = as_a <gcond *> (*gsi_last_bb (cond_bb));
 
   gcc_assert (cond_bb->loop_father == loop1);
 
@@ -1544,7 +1541,7 @@  do_split_loop_on_cond (struct loop *loop1, edge invar_branch)
     }
 
   basic_block cond_bb_copy = get_bb_copy (cond_bb);
-  gcond *cond_copy = as_a<gcond *> (last_stmt (cond_bb_copy));
+  gcond *cond_copy = as_a<gcond *> (*gsi_last_bb (cond_bb_copy));
 
   /* Replace the condition in loop2 with a bool constant to let PassManager
      remove the variant branch after current pass completes.  */
@@ -1628,12 +1625,10 @@  split_loop_on_cond (struct loop *loop)
       if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
 	continue;
 
-      gimple *last = last_stmt (bb);
-
-      if (!last || gimple_code (last) != GIMPLE_COND)
+      gcond *cond = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
+      if (!cond)
 	continue;
 
-      gcond *cond = as_a <gcond *> (last);
       edge branch_edge = get_cond_branch_to_split_loop (loop, cond);
 
       if (branch_edge)
diff --git a/gcc/tree-ssa-loop-unswitch.cc b/gcc/tree-ssa-loop-unswitch.cc
index 081fb42ba54..95580768804 100644
--- a/gcc/tree-ssa-loop-unswitch.cc
+++ b/gcc/tree-ssa-loop-unswitch.cc
@@ -506,7 +506,7 @@  find_unswitching_predicates_for_bb (basic_block bb, class loop *loop,
   ssa_op_iter iter;
 
   /* BB must end in a simple conditional jump.  */
-  last = last_stmt (bb);
+  last = *gsi_last_bb (bb);
   if (!last)
     return;
 
@@ -806,7 +806,7 @@  simplify_loop_version (class loop *loop, predicate_vector &predicate_path,
       if (predicates.is_empty ())
 	continue;
 
-      gimple *stmt = last_stmt (bbs[i]);
+      gimple *stmt = *gsi_last_bb (bbs[i]);
       tree folded = evaluate_control_stmt_using_entry_checks (stmt,
 							      predicate_path,
 							      ignored_edge_flag,
@@ -886,7 +886,7 @@  evaluate_bbs (class loop *loop, predicate_vector *predicate_path,
       if (visit (bb))
 	break;
 
-      gimple *last = last_stmt (bb);
+      gimple *last = *gsi_last_bb (bb);
       if (gcond *cond = safe_dyn_cast <gcond *> (last))
 	{
 	  if (gimple_cond_true_p (cond))
@@ -1218,7 +1218,7 @@  find_loop_guard (class loop *loop, vec<gimple *> &dbg_to_reset)
 	next = single_succ (header);
       else
 	{
-	  cond = safe_dyn_cast <gcond *> (last_stmt (header));
+	  cond = safe_dyn_cast <gcond *> (*gsi_last_bb (header));
 	  if (! cond)
 	    return NULL;
 	  extract_true_false_edges_from_block (header, &te, &fe);
@@ -1638,7 +1638,7 @@  clean_up_after_unswitching (int ignored_edge_flag)
 
   FOR_EACH_BB_FN (bb, cfun)
     {
-      gswitch *stmt= safe_dyn_cast <gswitch *> (last_stmt (bb));
+      gswitch *stmt= safe_dyn_cast <gswitch *> (*gsi_last_bb (bb));
       if (stmt && !CONSTANT_CLASS_P (gimple_switch_index (stmt)))
 	{
 	  unsigned nlabels = gimple_switch_num_labels (stmt);
diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc
index 15eed3e960c..b58a2ac9e6a 100644
--- a/gcc/tree-ssa-math-opts.cc
+++ b/gcc/tree-ssa-math-opts.cc
@@ -3605,9 +3605,8 @@  maybe_optimize_guarding_check (vec<gimple *> &mul_stmts, gimple *cond_stmt,
     }
   else if (!single_succ_p (bb) || other_edge->dest != single_succ (bb))
     return;
-  gimple *zero_cond = last_stmt (pred_bb);
+  gcond *zero_cond = safe_dyn_cast <gcond *> (*gsi_last_bb (pred_bb));
   if (zero_cond == NULL
-      || gimple_code (zero_cond) != GIMPLE_COND
       || (gimple_cond_code (zero_cond)
 	  != ((pred_edge->flags & EDGE_TRUE_VALUE) ? NE_EXPR : EQ_EXPR))
       || !integer_zerop (gimple_cond_rhs (zero_cond)))
@@ -3726,11 +3725,10 @@  maybe_optimize_guarding_check (vec<gimple *> &mul_stmts, gimple *cond_stmt,
       else if (!integer_onep (other_val))
 	return;
     }
-  gcond *zero_gcond = as_a <gcond *> (zero_cond);
   if (pred_edge->flags & EDGE_TRUE_VALUE)
-    gimple_cond_make_true (zero_gcond);
+    gimple_cond_make_true (zero_cond);
   else
-    gimple_cond_make_false (zero_gcond);
+    gimple_cond_make_false (zero_cond);
   update_stmt (zero_cond);
   *cfg_changed = true;
 }
@@ -4778,7 +4776,7 @@  convert_mult_to_highpart (gassign *stmt, gimple_stmt_iterator *gsi)
    conditional jump sequence.  */
 
 static void
-optimize_spaceship (gimple *stmt)
+optimize_spaceship (gcond *stmt)
 {
   enum tree_code code = gimple_cond_code (stmt);
   if (code != EQ_EXPR && code != NE_EXPR)
@@ -4797,9 +4795,8 @@  optimize_spaceship (gimple *stmt)
   if (((EDGE_SUCC (bb0, 0)->flags & EDGE_TRUE_VALUE) != 0) ^ (code == EQ_EXPR))
     bb1 = EDGE_SUCC (bb0, 0)->dest;
 
-  gimple *g = last_stmt (bb1);
+  gcond *g = safe_dyn_cast <gcond *> (*gsi_last_bb (bb1));
   if (g == NULL
-      || gimple_code (g) != GIMPLE_COND
       || !single_pred_p (bb1)
       || (operand_equal_p (gimple_cond_lhs (g), arg1, 0)
 	  ? !operand_equal_p (gimple_cond_rhs (g), arg2, 0)
@@ -4833,9 +4830,8 @@  optimize_spaceship (gimple *stmt)
 	continue;
 
       bb2 = EDGE_SUCC (bb1, i)->dest;
-      g = last_stmt (bb2);
+      g = safe_dyn_cast <gcond *> (*gsi_last_bb (bb2));
       if (g == NULL
-	  || gimple_code (g) != GIMPLE_COND
 	  || !single_pred_p (bb2)
 	  || (operand_equal_p (gimple_cond_lhs (g), arg1, 0)
 	      ? !operand_equal_p (gimple_cond_rhs (g), arg2, 0)
@@ -4899,19 +4895,17 @@  optimize_spaceship (gimple *stmt)
 	}
     }
 
-  g = gimple_build_call_internal (IFN_SPACESHIP, 2, arg1, arg2);
+  gcall *gc = gimple_build_call_internal (IFN_SPACESHIP, 2, arg1, arg2);
   tree lhs = make_ssa_name (integer_type_node);
-  gimple_call_set_lhs (g, lhs);
+  gimple_call_set_lhs (gc, lhs);
   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
-  gsi_insert_before (&gsi, g, GSI_SAME_STMT);
+  gsi_insert_before (&gsi, gc, GSI_SAME_STMT);
 
-  gcond *cond = as_a <gcond *> (stmt);
-  gimple_cond_set_lhs (cond, lhs);
-  gimple_cond_set_rhs (cond, integer_zero_node);
+  gimple_cond_set_lhs (stmt, lhs);
+  gimple_cond_set_rhs (stmt, integer_zero_node);
   update_stmt (stmt);
 
-  g = last_stmt (bb1);
-  cond = as_a <gcond *> (g);
+  gcond *cond = as_a <gcond *> (*gsi_last_bb (bb1));
   gimple_cond_set_lhs (cond, lhs);
   if (em1->src == bb1 && e2 != em1)
     {
@@ -4926,12 +4920,11 @@  optimize_spaceship (gimple *stmt)
       gimple_cond_set_code (cond, (e1->flags & EDGE_TRUE_VALUE)
 				  ? EQ_EXPR : NE_EXPR);
     }
-  update_stmt (g);
+  update_stmt (cond);
 
   if (e2 != e1 && e2 != em1)
     {
-      g = last_stmt (bb2);
-      cond = as_a <gcond *> (g);
+      cond = as_a <gcond *> (*gsi_last_bb (bb2));
       gimple_cond_set_lhs (cond, lhs);
       if (em1->src == bb2)
 	gimple_cond_set_rhs (cond, integer_minus_one_node);
@@ -4942,7 +4935,7 @@  optimize_spaceship (gimple *stmt)
 	}
       gimple_cond_set_code (cond,
 			    (e2->flags & EDGE_TRUE_VALUE) ? NE_EXPR : EQ_EXPR);
-      update_stmt (g);
+      update_stmt (cond);
     }
 
   wide_int wm1 = wi::minus_one (TYPE_PRECISION (integer_type_node));
@@ -5113,7 +5106,7 @@  math_opts_dom_walker::after_dom_children (basic_block bb)
 	    }
 	}
       else if (gimple_code (stmt) == GIMPLE_COND)
-	optimize_spaceship (stmt);
+	optimize_spaceship (as_a <gcond *> (stmt));
       gsi_next (&gsi);
     }
   if (fma_state.m_deferring_p
diff --git a/gcc/tree-vrp.cc b/gcc/tree-vrp.cc
index 0761b6896fe..89707a56b21 100644
--- a/gcc/tree-vrp.cc
+++ b/gcc/tree-vrp.cc
@@ -650,18 +650,17 @@  void
 maybe_set_nonzero_bits (edge e, tree var)
 {
   basic_block cond_bb = e->src;
-  gimple *stmt = last_stmt (cond_bb);
+  gcond *cond = safe_dyn_cast <gcond *> (*gsi_last_bb (cond_bb));
   tree cst;
 
-  if (stmt == NULL
-      || gimple_code (stmt) != GIMPLE_COND
-      || gimple_cond_code (stmt) != ((e->flags & EDGE_TRUE_VALUE)
+  if (cond == NULL
+      || gimple_cond_code (cond) != ((e->flags & EDGE_TRUE_VALUE)
 				     ? EQ_EXPR : NE_EXPR)
-      || TREE_CODE (gimple_cond_lhs (stmt)) != SSA_NAME
-      || !integer_zerop (gimple_cond_rhs (stmt)))
+      || TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
+      || !integer_zerop (gimple_cond_rhs (cond)))
     return;
 
-  stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (stmt));
+  gimple *stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (cond));
   if (!is_gimple_assign (stmt)
       || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR
       || TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
diff --git a/gcc/value-pointer-equiv.cc b/gcc/value-pointer-equiv.cc
index aeff2e879b9..52365837ac7 100644
--- a/gcc/value-pointer-equiv.cc
+++ b/gcc/value-pointer-equiv.cc
@@ -302,12 +302,11 @@  pointer_equiv_analyzer::visit_stmt (gimple *stmt)
 void
 pointer_equiv_analyzer::visit_edge (edge e)
 {
-  gimple *stmt = last_stmt (e->src);
+  gcond *stmt = safe_dyn_cast <gcond *> (*gsi_last_bb (e->src));
   tree lhs;
   // Recognize: x_13 [==,!=] &foo.
   if (stmt
-      && gimple_code (stmt) == GIMPLE_COND
-      && (lhs = gimple_cond_lhs (stmt))
+      && ((lhs = gimple_cond_lhs (stmt)), true)
       && TREE_CODE (lhs) == SSA_NAME
       && POINTER_TYPE_P (TREE_TYPE (lhs))
       && TREE_CODE (gimple_cond_rhs (stmt)) == ADDR_EXPR)