[gcc r17-3219] Remove dead code from vectorizable_condition

Richard Biener via Gcc-cvs <[email protected]>
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:a3732e7393cead21429e6d70217069fafbc69d61

commit r17-3219-ga3732e7393cead21429e6d70217069fafbc69d61
Author: Richard Biener <[email protected]>
Date:   Wed Aug 12 11:24:01 2026 +0200

    Remove dead code from vectorizable_condition
    
    We no longer have COMPARISON_CLASS_P first operand of COND_EXPRs, not
    from patterns either, as the assert in vectorizable_condition shows.
    The following prunes unreachable code from masked being always true.
    
            * tree-vect-stmts.cc (vect_is_simple_cond): Simplify and
            inline ...
            (vectorizable_condition): ... here.  Simplify.

Diff:
---
 gcc/tree-vect-stmts.cc | 312 +++++--------------------------------------------
 1 file changed, 27 insertions(+), 285 deletions(-)

diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
index cfbd2040b8b0..50fa0e4bc422 100644
--- a/gcc/tree-vect-stmts.cc
+++ b/gcc/tree-vect-stmts.cc
@@ -11998,99 +11998,6 @@ vectorizable_load (vec_info *vinfo,
   return true;
 }
 
-/* Function vect_is_simple_cond.
-
-   Input:
-   LOOP - the loop that is being vectorized.
-   COND - Condition that is checked for simple use.
-
-   Output:
-   *COMP_VECTYPE - the vector type for the comparison.
-   *DTS - The def types for the arguments of the comparison
-
-   Returns whether a COND can be vectorized.  Checks whether
-   condition operands are supportable using vec_is_simple_use.  */
-
-static bool
-vect_is_simple_cond (tree cond, vec_info *vinfo,
-		     slp_tree slp_node, tree *comp_vectype,
-		     enum vect_def_type *dts, tree vectype)
-{
-  tree lhs, rhs;
-  tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
-  slp_tree slp_op;
-
-  /* Mask case.  */
-  if (TREE_CODE (cond) == SSA_NAME
-      && VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (cond)))
-    {
-      if (!vect_is_simple_use (vinfo, slp_node, 0, &cond,
-			       &slp_op, &dts[0], comp_vectype)
-	  || !*comp_vectype
-	  || !VECTOR_BOOLEAN_TYPE_P (*comp_vectype))
-	return false;
-      return true;
-    }
-
-  if (!COMPARISON_CLASS_P (cond))
-    return false;
-
-  lhs = TREE_OPERAND (cond, 0);
-  rhs = TREE_OPERAND (cond, 1);
-
-  if (TREE_CODE (lhs) == SSA_NAME)
-    {
-      if (!vect_is_simple_use (vinfo, slp_node, 0,
-			       &lhs, &slp_op, &dts[0], &vectype1))
-	return false;
-    }
-  else if (TREE_CODE (lhs) == INTEGER_CST || TREE_CODE (lhs) == REAL_CST
-	   || TREE_CODE (lhs) == FIXED_CST)
-    dts[0] = vect_constant_def;
-  else
-    return false;
-
-  if (TREE_CODE (rhs) == SSA_NAME)
-    {
-      if (!vect_is_simple_use (vinfo, slp_node, 1,
-			       &rhs, &slp_op, &dts[1], &vectype2))
-	return false;
-    }
-  else if (TREE_CODE (rhs) == INTEGER_CST || TREE_CODE (rhs) == REAL_CST
-	   || TREE_CODE (rhs) == FIXED_CST)
-    dts[1] = vect_constant_def;
-  else
-    return false;
-
-  if (vectype1 && vectype2
-      && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
-		   TYPE_VECTOR_SUBPARTS (vectype2)))
-    return false;
-
-  *comp_vectype = vectype1 ? vectype1 : vectype2;
-  /* Invariant comparison.  */
-  if (! *comp_vectype)
-    {
-      tree scalar_type = TREE_TYPE (lhs);
-      if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
-	*comp_vectype = truth_type_for (vectype);
-      else
-	{
-	  /* If we can widen the comparison to match vectype do so.  */
-	  if (INTEGRAL_TYPE_P (scalar_type)
-	      && !slp_node
-	      && tree_int_cst_lt (TYPE_SIZE (scalar_type),
-				  TYPE_SIZE (TREE_TYPE (vectype))))
-	    scalar_type = build_nonstandard_integer_type
-	      (vector_element_bits (vectype), TYPE_UNSIGNED (scalar_type));
-	  *comp_vectype = get_vectype_for_scalar_type (vinfo, scalar_type,
-						       slp_node);
-	}
-    }
-
-  return true;
-}
-
 /* vectorizable_condition.
 
    Check if STMT_INFO is conditional modify expression that can be vectorized.
@@ -12109,10 +12016,8 @@ vectorizable_condition (vec_info *vinfo,
 {
   tree scalar_dest = NULL_TREE;
   tree vec_dest = NULL_TREE;
-  tree cond_expr, cond_expr0 = NULL_TREE, cond_expr1 = NULL_TREE;
   tree then_clause, else_clause;
-  tree comp_vectype = NULL_TREE;
-  tree vec_cond_lhs = NULL_TREE, vec_cond_rhs = NULL_TREE;
+  tree vec_cond_lhs = NULL_TREE;
   tree vec_then_clause = NULL_TREE, vec_else_clause = NULL_TREE;
   tree vec_compare;
   tree new_temp;
@@ -12120,15 +12025,13 @@ vectorizable_condition (vec_info *vinfo,
   enum vect_def_type dts[4]
     = {vect_unknown_def_type, vect_unknown_def_type,
        vect_unknown_def_type, vect_unknown_def_type};
-  enum tree_code code, cond_code, bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
+  enum tree_code code;
   int i;
   bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
   vec<tree> vec_oprnds0 = vNULL;
-  vec<tree> vec_oprnds1 = vNULL;
   vec<tree> vec_oprnds2 = vNULL;
   vec<tree> vec_oprnds3 = vNULL;
   tree vec_cmp_type;
-  bool masked = false;
 
   if (!STMT_VINFO_RELEVANT_P (stmt_info) && !bb_vinfo)
     return false;
@@ -12172,20 +12075,22 @@ vectorizable_condition (vec_info *vinfo,
 
   int vec_num = vect_get_num_copies (vinfo, slp_node);
 
-  cond_expr = gimple_assign_rhs1 (stmt);
+  slp_tree slp_cond;
+  tree cond_expr = gimple_assign_rhs1 (stmt);
   gcc_assert (! COMPARISON_CLASS_P (cond_expr));
-
-  if (!vect_is_simple_cond (cond_expr, vinfo, slp_node,
-			    &comp_vectype, &dts[0], vectype)
-      || !comp_vectype)
+  if (TREE_CODE (cond_expr) != SSA_NAME
+      || !VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (cond_expr))
+      || !vect_is_simple_use (vinfo, slp_node, 0,
+			      &slp_cond, &dts[0], &vec_cmp_type)
+      || !vec_cmp_type
+      || !VECTOR_BOOLEAN_TYPE_P (vec_cmp_type))
     return false;
 
-  unsigned op_adjust = COMPARISON_CLASS_P (cond_expr) ? 1 : 0;
   slp_tree then_slp_node, else_slp_node;
-  if (!vect_is_simple_use (vinfo, slp_node, 1 + op_adjust,
+  if (!vect_is_simple_use (vinfo, slp_node, 1,
 			   &then_clause, &then_slp_node, &dts[2], &vectype1))
     return false;
-  if (!vect_is_simple_use (vinfo, slp_node, 2 + op_adjust,
+  if (!vect_is_simple_use (vinfo, slp_node, 2,
 			   &else_clause, &else_slp_node, &dts[3], &vectype2))
     return false;
 
@@ -12195,20 +12100,10 @@ vectorizable_condition (vec_info *vinfo,
   if (vectype2 && !useless_type_conversion_p (vectype, vectype2))
     return false;
 
-  masked = !COMPARISON_CLASS_P (cond_expr);
-  vec_cmp_type = truth_type_for (comp_vectype);
-  if (vec_cmp_type == NULL_TREE
-      || maybe_ne (TYPE_VECTOR_SUBPARTS (vectype),
-		   TYPE_VECTOR_SUBPARTS (vec_cmp_type)))
+  if (maybe_ne (TYPE_VECTOR_SUBPARTS (vectype),
+		TYPE_VECTOR_SUBPARTS (vec_cmp_type)))
     return false;
 
-  cond_code = TREE_CODE (cond_expr);
-  if (!masked)
-    {
-      cond_expr0 = TREE_OPERAND (cond_expr, 0);
-      cond_expr1 = TREE_OPERAND (cond_expr, 1);
-    }
-
   /* For conditional reductions, the "then" value needs to be the candidate
      value calculated by this iteration while the "else" value needs to be
      the result carried over from previous iterations.  If the COND_EXPR
@@ -12216,114 +12111,24 @@ vectorizable_condition (vec_info *vinfo,
   bool must_invert_cmp_result = false;
   if (reduction_type == EXTRACT_LAST_REDUCTION && reduc_index == 1)
     {
-      if (masked)
-	must_invert_cmp_result = true;
-      else
-	{
-	  bool honor_nans = HONOR_NANS (TREE_TYPE (cond_expr0));
-	  tree_code new_code = invert_tree_comparison (cond_code, honor_nans);
-	  if (new_code == ERROR_MARK)
-	    must_invert_cmp_result = true;
-	  else
-	    {
-	      cond_code = new_code;
-	      /* Make sure we don't accidentally use the old condition.  */
-	      cond_expr = NULL_TREE;
-	    }
-	}
+      must_invert_cmp_result = true;
       /* ???  The vectorized operand query below doesn't allow swapping
 	 this way for SLP.  */
       return false;
       /* std::swap (then_clause, else_clause); */
     }
 
-  if (!masked && VECTOR_BOOLEAN_TYPE_P (comp_vectype))
-    {
-      /* Boolean values may have another representation in vectors
-	 and therefore we prefer bit operations over comparison for
-	 them (which also works for scalar masks).  We store opcodes
-	 to use in bitop1 and bitop2.  Statement is vectorized as
-	 BITOP2 (rhs1 BITOP1 rhs2) or rhs1 BITOP2 (BITOP1 rhs2)
-	 depending on bitop1 and bitop2 arity.  */
-      switch (cond_code)
-	{
-	case GT_EXPR:
-	  bitop1 = BIT_NOT_EXPR;
-	  bitop2 = BIT_AND_EXPR;
-	  break;
-	case GE_EXPR:
-	  bitop1 = BIT_NOT_EXPR;
-	  bitop2 = BIT_IOR_EXPR;
-	  break;
-	case LT_EXPR:
-	  bitop1 = BIT_NOT_EXPR;
-	  bitop2 = BIT_AND_EXPR;
-	  std::swap (cond_expr0, cond_expr1);
-	  break;
-	case LE_EXPR:
-	  bitop1 = BIT_NOT_EXPR;
-	  bitop2 = BIT_IOR_EXPR;
-	  std::swap (cond_expr0, cond_expr1);
-	  break;
-	case NE_EXPR:
-	  bitop1 = BIT_XOR_EXPR;
-	  break;
-	case EQ_EXPR:
-	  bitop1 = BIT_XOR_EXPR;
-	  bitop2 = BIT_NOT_EXPR;
-	  break;
-	default:
-	  return false;
-	}
-      cond_code = SSA_NAME;
-    }
-
-  if (TREE_CODE_CLASS (cond_code) == tcc_comparison
-      && reduction_type == EXTRACT_LAST_REDUCTION
-      && !expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type, cond_code))
-    {
-      if (dump_enabled_p ())
-	dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
-			 "reduction comparison operation not supported.\n");
-      return false;
-    }
-
   if (cost_vec)
     {
-      if (bitop1 != NOP_EXPR)
-	{
-	  machine_mode mode = TYPE_MODE (comp_vectype);
-	  optab optab;
-
-	  optab = optab_for_tree_code (bitop1, comp_vectype, optab_default);
-	  if (!optab || !can_implement_p (optab, mode))
-	    return false;
-
-	  if (bitop2 != NOP_EXPR)
-	    {
-	      optab = optab_for_tree_code (bitop2, comp_vectype,
-					   optab_default);
-	      if (!optab || !can_implement_p (optab, mode))
-		return false;
-	    }
-	}
-
       vect_cost_for_stmt kind = vector_stmt;
       if (reduction_type == EXTRACT_LAST_REDUCTION)
 	/* Count one reduction-like operation per vector.  */
 	kind = vec_to_scalar;
-      else if ((masked && !expand_vec_cond_expr_p (vectype, comp_vectype))
-	       || (!masked
-		   && (!expand_vec_cmp_expr_p (comp_vectype, vec_cmp_type,
-					       cond_code)
-		       || !expand_vec_cond_expr_p (vectype, vec_cmp_type))))
+      else if (!expand_vec_cond_expr_p (vectype, vec_cmp_type))
 	return false;
 
       if (!vect_maybe_update_slp_op_vectype (SLP_TREE_CHILDREN (slp_node)[0],
-					     comp_vectype)
-	  || (op_adjust == 1
-	      && !vect_maybe_update_slp_op_vectype
-			      (SLP_TREE_CHILDREN (slp_node)[1], comp_vectype))
+					     vec_cmp_type)
 	  || !vect_maybe_update_slp_op_vectype (then_slp_node, vectype)
 	  || !vect_maybe_update_slp_op_vectype (else_slp_node, vectype))
 	{
@@ -12397,39 +12202,24 @@ vectorizable_condition (vec_info *vinfo,
 	      bool honor_nans = HONOR_NANS (TREE_TYPE (cond.op0));
 	      tree_code orig_code = cond.code;
 	      cond.code = invert_tree_comparison (cond.code, honor_nans);
-	      if (!masked && loop_vinfo->scalar_cond_masked_set.contains (cond))
+	      /* Try the inverse of the current mask.  We check if the
+		 inverse mask is live and if so we generate a negate of
+		 the current mask such that we still honor NaNs.  */
+	      cond.inverted_p = true;
+	      cond.code = orig_code;
+	      if (loop_vinfo->scalar_cond_masked_set.contains (cond))
 		{
 		  masks = &LOOP_VINFO_MASKS (loop_vinfo);
-		  cond_code = cond.code;
 		  swap_cond_operands = true;
-		}
-	      else
-		{
-		  /* Try the inverse of the current mask.  We check if the
-		     inverse mask is live and if so we generate a negate of
-		     the current mask such that we still honor NaNs.  */
-		  cond.inverted_p = true;
-		  cond.code = orig_code;
-		  if (loop_vinfo->scalar_cond_masked_set.contains (cond))
-		    {
-		      masks = &LOOP_VINFO_MASKS (loop_vinfo);
-		      cond_code = cond.code;
-		      swap_cond_operands = true;
-		      must_invert_cmp_result = true;
-		    }
+		  must_invert_cmp_result = true;
 		}
 	    }
 	}
     }
 
   /* Handle cond expr.  */
-  if (masked)
-    vect_get_vec_defs (vinfo, slp_node, true, &vec_oprnds0, true, &vec_oprnds2,
-		       reduction_type != EXTRACT_LAST_REDUCTION, &vec_oprnds3);
-  else
-    vect_get_vec_defs (vinfo, slp_node, true, &vec_oprnds0, true, &vec_oprnds1,
-		       true, &vec_oprnds2,
-		       reduction_type != EXTRACT_LAST_REDUCTION, &vec_oprnds3);
+  vect_get_vec_defs (vinfo, slp_node, true, &vec_oprnds0, true, &vec_oprnds2,
+		     reduction_type != EXTRACT_LAST_REDUCTION, &vec_oprnds3);
 
   if (reduction_type == EXTRACT_LAST_REDUCTION)
     vec_else_clause = else_clause;
@@ -12444,54 +12234,7 @@ vectorizable_condition (vec_info *vinfo,
       if (swap_cond_operands)
 	std::swap (vec_then_clause, vec_else_clause);
 
-      if (masked)
-	vec_compare = vec_cond_lhs;
-      else
-	{
-	  vec_cond_rhs = vec_oprnds1[i];
-	  if (bitop1 == NOP_EXPR)
-	    {
-	      gimple_seq stmts = NULL;
-	      vec_compare = gimple_build (&stmts, cond_code, vec_cmp_type,
-					   vec_cond_lhs, vec_cond_rhs);
-	      gsi_insert_before (gsi, stmts, GSI_SAME_STMT);
-	    }
-	  else
-	    {
-	      new_temp = make_ssa_name (vec_cmp_type);
-	      gassign *new_stmt;
-	      if (bitop1 == BIT_NOT_EXPR)
-		new_stmt = gimple_build_assign (new_temp, bitop1,
-						vec_cond_rhs);
-	      else
-		new_stmt
-		  = gimple_build_assign (new_temp, bitop1, vec_cond_lhs,
-					 vec_cond_rhs);
-	      vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
-	      if (bitop2 == NOP_EXPR)
-		vec_compare = new_temp;
-	      else if (bitop2 == BIT_NOT_EXPR
-		       && reduction_type != EXTRACT_LAST_REDUCTION)
-		{
-		  /* Instead of doing ~x ? y : z do x ? z : y.  */
-		  vec_compare = new_temp;
-		  std::swap (vec_then_clause, vec_else_clause);
-		}
-	      else
-		{
-		  vec_compare = make_ssa_name (vec_cmp_type);
-		  if (bitop2 == BIT_NOT_EXPR)
-		    new_stmt
-		      = gimple_build_assign (vec_compare, bitop2, new_temp);
-		  else
-		    new_stmt
-		      = gimple_build_assign (vec_compare, bitop2,
-					     vec_cond_lhs, new_temp);
-		  vect_finish_stmt_generation (vinfo, stmt_info,
-					       new_stmt, gsi);
-		}
-	    }
-	}
+      vec_compare = vec_cond_lhs;
 
       /* If we decided to apply a loop mask to the result of the vector
 	 comparison, AND the comparison with the mask now.  Later passes
@@ -12618,7 +12361,6 @@ vectorizable_condition (vec_info *vinfo,
     }
 
   vec_oprnds0.release ();
-  vec_oprnds1.release ();
   vec_oprnds2.release ();
   vec_oprnds3.release ();
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.