On 6/30/20 3:03 PM, Richard Biener wrote:
Now to simply restore previous behavior for this particular case we should probably make expand_vector_comparison walk immediate uses as you do but then call expand_vector_condition for each VEC_COND_EXPR use, making that return whether it "consumed" the comparison. If all uses consumed the comparison we should DCE it, if not, we should lower it?
All right, I prepared patch for it. Patch can bootstrap on x86_64-linux-gnu and survives regression tests. Ready to be installed? Thanks, Martin
>From 366b61fb66efa9af0cfaa2a10df72a4224c708b8 Mon Sep 17 00:00:00 2001 From: Martin Liska <mli...@suse.cz> Date: Tue, 30 Jun 2020 08:57:27 +0200 Subject: [PATCH] VEC_COND_EXPR: do not expand comparisons feeding it gcc/ChangeLog: * tree-vect-generic.c (expand_vector_condition): Forward declaration. (expand_vector_comparison): Do not expand a comparison if all uses are consumed by a VEC_COND_EXPR. (expand_vector_operation): Change void return type to bool. (expand_vector_operations_1): Pass dce_ssa_names. --- gcc/tree-vect-generic.c | 59 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 52 insertions(+), 7 deletions(-) diff --git a/gcc/tree-vect-generic.c b/gcc/tree-vect-generic.c index a4b56195903..92884fad2d3 100644 --- a/gcc/tree-vect-generic.c +++ b/gcc/tree-vect-generic.c @@ -371,14 +371,53 @@ expand_vector_addition (gimple_stmt_iterator *gsi, a, b, code); } +static bool +expand_vector_condition (gimple_stmt_iterator *gsi, auto_bitmap *dce_ssa_names); + /* Try to expand vector comparison expression OP0 CODE OP1 by querying optab if the following expression: VEC_COND_EXPR< OP0 CODE OP1, {-1,...}, {0,...}> can be expanded. */ static tree expand_vector_comparison (gimple_stmt_iterator *gsi, tree type, tree op0, - tree op1, enum tree_code code) + tree op1, enum tree_code code, + auto_bitmap *dce_ssa_names) { + tree lhs = gimple_assign_lhs (gsi_stmt (*gsi)); + use_operand_p use_p; + imm_use_iterator iterator; + bool vec_cond_expr_only = true; + + /* As seen in PR95830, we should not expand comparisons that are only + feeding a VEC_COND_EXPR statement. */ + auto_vec<gimple *> uses; + FOR_EACH_IMM_USE_FAST (use_p, iterator, lhs) + uses.safe_push (USE_STMT (use_p)); + + for (unsigned i = 0; i < uses.length (); i ++) + { + gassign *use = dyn_cast<gassign *> (uses[i]); + if (use != NULL + && gimple_assign_rhs_code (use) == VEC_COND_EXPR + && gimple_assign_rhs1 (use) == lhs) + { + gimple_stmt_iterator it = gsi_for_stmt (use); + if (!expand_vector_condition (&it, dce_ssa_names)) + { + vec_cond_expr_only = false; + break; + } + } + else + { + vec_cond_expr_only = false; + break; + } + } + + if (!uses.is_empty () && vec_cond_expr_only) + return NULL_TREE; + tree t; if (!expand_vec_cmp_expr_p (TREE_TYPE (op0), type, code) && !expand_vec_cond_expr_p (type, TREE_TYPE (op0), code)) @@ -932,7 +971,8 @@ expand_vector_divmod (gimple_stmt_iterator *gsi, tree type, tree op0, /* Expand a vector condition to scalars, by using many conditions on the vector's elements. */ -static void + +static bool expand_vector_condition (gimple_stmt_iterator *gsi, auto_bitmap *dce_ssa_names) { gassign *stmt = as_a <gassign *> (gsi_stmt (*gsi)); @@ -975,7 +1015,7 @@ expand_vector_condition (gimple_stmt_iterator *gsi, auto_bitmap *dce_ssa_names) if (expand_vec_cond_expr_p (type, TREE_TYPE (a1), code)) { gcc_assert (TREE_CODE (a) == SSA_NAME || TREE_CODE (a) == VECTOR_CST); - return; + return true; } /* Handle vector boolean types with bitmasks. If there is a comparison @@ -1006,7 +1046,7 @@ expand_vector_condition (gimple_stmt_iterator *gsi, auto_bitmap *dce_ssa_names) a = gimplify_build2 (gsi, BIT_IOR_EXPR, type, a1, a2); gimple_assign_set_rhs_from_tree (gsi, a); update_stmt (gsi_stmt (*gsi)); - return; + return true; } /* TODO: try and find a smaller vector type. */ @@ -1070,11 +1110,14 @@ expand_vector_condition (gimple_stmt_iterator *gsi, auto_bitmap *dce_ssa_names) if (a_is_comparison) bitmap_set_bit (*dce_ssa_names, SSA_NAME_VERSION (gimple_assign_lhs (assign))); + + return false; } static tree expand_vector_operation (gimple_stmt_iterator *gsi, tree type, tree compute_type, - gassign *assign, enum tree_code code) + gassign *assign, enum tree_code code, + auto_bitmap *dce_ssa_names) { machine_mode compute_mode = TYPE_MODE (compute_type); @@ -1128,7 +1171,8 @@ expand_vector_operation (gimple_stmt_iterator *gsi, tree type, tree compute_type tree rhs1 = gimple_assign_rhs1 (assign); tree rhs2 = gimple_assign_rhs2 (assign); - return expand_vector_comparison (gsi, type, rhs1, rhs2, code); + return expand_vector_comparison (gsi, type, rhs1, rhs2, code, + dce_ssa_names); } case TRUNC_DIV_EXPR: @@ -2213,7 +2257,8 @@ expand_vector_operations_1 (gimple_stmt_iterator *gsi, if (compute_type == type) return; - new_rhs = expand_vector_operation (gsi, type, compute_type, stmt, code); + new_rhs = expand_vector_operation (gsi, type, compute_type, stmt, code, + dce_ssa_names); /* Leave expression untouched for later expansion. */ if (new_rhs == NULL_TREE) -- 2.27.0