Hi All,
When vectorizing an early exit in a loop controlled by a vector length, we need
to make sure that inactive lanes cannot trigger the exit. For example,
unsigned
find_zero (const unsigned *a, unsigned n)
{
unsigned i;
for (i = 0; i < n; ++i)
if (a[i] == 0)
break;
return i;
}
On the final vector iteration, some lanes may be outside the scalar loop's
bounds. A comparison against zero can still be true in those lanes, so we must
exclude them before checking whether any lane wants to exit.
Currently we do this by generating the comparison and then applying the loop
length to its result through VCOND_MASK_LEN. In outline, this looks like
cmp = values == zeroes;
exit_mask = .VCOND_MASK_LEN (cmp, all_ones, all_zeroes, len, bias);
In the early break patch series I sent Robin raised the question whether this
would work for them. My preference is to keep the LEN path similar to the masked
and unmasked targets and so not to diverge too much in terms of control flow
handling.
After having a very productive chat with Robin at the Cauldron I understand that
the issue RISC-V has is that the LEN masking today is done though VCOND_MASK_LEN
which is an operation the target must implement for the check to pass.
However this check is not implementable for all vector lengths as I understood
because it requires a creation of a pointer. But there are only so many bits
you can squeeze into such pointers. As such this places a limitation on which
modes they can use for early break vectorization. The vectorizer does use this
in some other cases where correctness requires it, but not as prevailent as
early break.
So to unblock the series and move it forward I have prepared this solution which
uses the new .COND_LEN_VEC_CMP directly from the vectorizer.
The target has to either support .VCOND_MASK_LEN or .COND_LEN_VEC_CMP with a
preference for .COND_LEN_VEC_CMP when available.
The existing COND_LEN_VEC_CMP internal function can express the comparison and
the length restriction together. This patch teaches the vectorizer to use it
when the target supports the required comparison:
exit_mask = .COND_LEN_VEC_CMP (EQ_EXPR, all_ones, values, zeroes,
len, bias);
This gives the backend the comparison with its active length directly and avoids
the separate masking step in GIMPLE. I'm only doing this for LEN, because
emitting
.COND_VEC_CMP here has no benefit for masked targets and for now we lose a lot
of folding. So whenever these are fixed we could consider emitting that too,
as in principle it preserves the trapping semantics more correctly in GIMPLE
than
using two different operations.
The new support queries check both the operand and result modes and the actual
comparison code against cond_len_vec_cmp. This matters because having a pattern
for those modes does not necessarily mean that it accepts every comparison.
Comparisons implemented as boolean operations keep their existing path.
During analysis we record the loop lengths needed by the comparison. When
transforming a length-controlled loop, the comparison then uses those lengths
and the loop's bias. This applies to early-exit conditions; the normal
loop-termination condition keeps its existing handling.
There is one detail with flipped conditions. If we have to invert the
comparison result, false inactive lanes become true again. Those conditions
therefore still need masking after the inversion. The early-exit code checks
the comparison actually emitted and only skips its existing length masking when
the result already comes from COND_LEN_VEC_CMP. The same check covers both a
single result and multiple results that must be reduced together.
I implemented the optabs in a cross compiler for RISC-V and verified this works.
At GIMPLE level you get
<bb 15> [local count: 62282392]:
# i_47 = PHI <i_43(14), 0(2)>
bnd.7_51 = 800 - prolog_loop_niters.5_33;
_68 = (sizetype) prolog_loop_niters.5_33;
_69 = _68 * 4;
vectp_vect_a.11_67 = &vect_a + _69;
_76 = [vec_duplicate_expr] x_5(D);
_78 = (unsigned long) bnd.7_51;
<bb 3> [local count: 956703966]:
# _53 = PHI <_55(7), 0(15)>
# vectp_vect_a.10_70 = PHI <vectp_vect_a.10_71(7), vectp_vect_a.11_67(15)>
# ivtmp_79 = PHI <ivtmp_80(7), _78(15)>
_81 = .SELECT_VL (ivtmp_79, POLY_INT_CST [32, 32], { 0, ... });
vect__1.13_75 = .MASK_LEN_LOAD (vectp_vect_a.10_70, 1024B,
{ -1, ... }, _74(D), _81, 0);
mask_patt_11.14_77 = .COND_LEN_VEC_CMP (116, { -1, ... },
vect__1.13_75, _76, _81, 0);
if (mask_patt_11.14_77 != { 0, ... })
goto <bb 18>; [83.64%]
else
goto <bb 5>; [16.36%]
out of the vectorizer, and by implementing the vec_cond_len_cbranch optab as
well the sequence generated is:
.L8:
vsetvli t1,a1,e8,m2,ta,ma
vle32.v v8,0(t3)
vsetvli a3,zero,e32,m8,ta,mu
vmsltu.vx v0,v16,t1
vmv1r.v v2,v1
vmsne.vv v2,v8,v24,v0.t
vfirst.m t2,v2
blt t2,zero,.L6
addw t4,t2,a7
addw t5,t4,t0
mv a0,t5
ret
.L6:
slli a6,t1,2
sub a1,a1,t1
add a7,t1,a7
add t3,t3,a6
bne a1,zero,.L8
li a0,-1
ret
Which verifies that the "good" codegen that Robin wanted is now possible.
I've pulled this out of the early break series but this is essentially a
follow up.
Bootstrapped Regtested on aarch64-none-linux-gnu, arm-none-linux-gnueabihf,
x86_64-pc-linux-gnu -m32, -m64 and no issues.
Ok for master?
Thanks,
Tamar
gcc/ChangeLog:
* optabs-tree.cc (expand_cond_len_vec_cmp_p): New overload to check
length-controlled vector comparison support using tree types.
* optabs-tree.h (expand_cond_len_vec_cmp_p): Declare the overload.
* optabs.cc (can_cond_len_vec_cmp_compare_p): New function. Check
the cond_len_vec_cmp optab handler and its comparison predicate.
* optabs.h (can_cond_len_vec_cmp_compare_p): Declare.
* tree-vect-stmts.cc (vectorizable_comparison_1): Accept a request
for length-controlled comparisons. Check target support, record
loop lengths and emit IFN_COND_LEN_VEC_CMP when supported.
(vectorizable_comparison): Request length-controlled comparisons
for early-exit conditions in fully length-controlled loops.
(vectorizable_early_exit): Account for direct length-controlled
comparison support when recording lengths. Retain masking after
inversion for flipped conditions. Skip separate length masking
when the emitted comparison already applies the loop length.
---
diff --git a/gcc/optabs-tree.cc b/gcc/optabs-tree.cc
index
5360a509f8d85394c219acfbd6f87eb060928419..3a22cc0af753c2935c5e407e7edb1f8683972a25
100644
--- a/gcc/optabs-tree.cc
+++ b/gcc/optabs-tree.cc
@@ -439,6 +439,24 @@ expand_vec_cmp_expr_p (tree value_type, tree mask_type,
enum tree_code code)
|| vec_cmp_eq_icode_p (value_type, mask_type, code);
}
+/* Return TRUE if appropriate vector insn is available
+ for vector comparison expr with vector type VALUE_TYPE
+ and resulting mask with MASK_TYPE using a LEN and BIAS
+ that will be provided later. */
+
+bool
+expand_cond_len_vec_cmp_p (tree value_type, tree mask_type, enum tree_code
code)
+{
+ enum rtx_code rcode = get_rtx_code_1 (code, TYPE_UNSIGNED (value_type));
+ if (rcode == UNKNOWN)
+ return false;
+
+ machine_mode value_mode = TYPE_MODE (value_type);
+ machine_mode mask_mode = TYPE_MODE (mask_type);
+
+ return can_cond_len_vec_cmp_compare_p (rcode, value_mode, mask_mode);
+}
+
/* Return TRUE iff, appropriate vector insns are available
for vector cond expr with vector type VALUE_TYPE and a comparison
with operand vector types in CMP_OP_TYPE. */
diff --git a/gcc/optabs-tree.h b/gcc/optabs-tree.h
index
fb9469aaece8bc70f0a6815aaa9a934cb66ffd87..eda6f8adb757f6c7036c7210c5a6ad2407a9f178
100644
--- a/gcc/optabs-tree.h
+++ b/gcc/optabs-tree.h
@@ -41,6 +41,7 @@ bool supportable_half_widening_operation (enum tree_code,
tree, tree,
enum tree_code *);
bool supportable_convert_operation (enum tree_code, tree, tree);
bool expand_vec_cmp_expr_p (tree, tree, enum tree_code);
+bool expand_cond_len_vec_cmp_p (tree, tree, enum tree_code);
bool expand_vec_cond_expr_p (tree, tree);
void init_tree_optimization_optabs (tree);
bool target_supports_op_p (tree, enum tree_code,
diff --git a/gcc/optabs.cc b/gcc/optabs.cc
index
5b34938e5e9896667c00cc77de0d1948a3fb19cd..d2700c5c9d1b4ecb105f910db65c8ea8fcc2f088
100644
--- a/gcc/optabs.cc
+++ b/gcc/optabs.cc
@@ -4533,6 +4533,22 @@ can_vec_cmp_compare_p (enum rtx_code code, machine_mode
value_mode,
return insn_predicate_matches_p (icode, 1, code, mask_mode, value_mode);
}
+/* Return whether the backend can emit a conditional len vector comparison
+ (cond_len_vec_cmp) for code CODE, comparing operands of mode VALUE_MODE and
+ producing a result with MASK_MODE. */
+
+bool
+can_cond_len_vec_cmp_compare_p (enum rtx_code code, machine_mode value_mode,
+ machine_mode mask_mode)
+{
+ enum insn_code icode
+ = convert_optab_handler (cond_len_vec_cmp_optab, value_mode, mask_mode);
+ if (icode == CODE_FOR_nothing)
+ return false;
+
+ return insn_predicate_matches_p (icode, 1, code, mask_mode, value_mode);
+}
+
/* Return whether the backend can emit vector set instructions for inserting
element into vector at variable index position. */
diff --git a/gcc/optabs.h b/gcc/optabs.h
index
fe4152b348097affc6c89ab84fba26b76d54d06f..5da3893b85626628dd5387122a21d2cf27a412df
100644
--- a/gcc/optabs.h
+++ b/gcc/optabs.h
@@ -261,6 +261,12 @@ extern bool can_compare_p (enum rtx_code, machine_mode,
with MASK_MODE. */
extern bool can_vec_cmp_compare_p (enum rtx_code, machine_mode, machine_mode);
+/* Return whether the backend can emit a conditional len vector comparison
+ (cond_len_vec_cmp) for code CODE, comparing operands of mode VALUE_MODE and
+ producing a result with MASK_MODE. */
+extern bool can_cond_len_vec_cmp_compare_p (enum rtx_code, machine_mode,
+ machine_mode);
+
/* Return whether the backend can emit vector set instructions for inserting
element into vector at variable index position. */
extern bool can_vec_set_var_idx_p (machine_mode);
diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
index
422805e55c62808a09218dc87e6d3b22855da4ec..fe9597bf7e41b82d99421394193976af8dd8e199
100644
--- a/gcc/tree-vect-stmts.cc
+++ b/gcc/tree-vect-stmts.cc
@@ -12454,14 +12454,16 @@ vectorizable_condition (vec_info *vinfo,
Return true if STMT_INFO is vectorizable in this way. If
COMPARISON_VECTYPE_OUT is nonnull, store the vector type of the comparison
- inputs there. */
+ inputs there. If USE_COND_LEN_COMPARES is true, use LEN and BIAS directly
+ when supported, otherwise clear USE_COND_LEN_COMPARES. */
static bool
vectorizable_comparison_1 (vec_info *vinfo, tree vectype,
stmt_vec_info stmt_info, tree_code code,
gimple_stmt_iterator *gsi,
slp_tree slp_node, stmt_vector_for_cost *cost_vec,
- tree *comparison_vectype_out)
+ tree *comparison_vectype_out,
+ bool &use_cond_len_compares)
{
tree lhs, rhs1, rhs2;
tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
@@ -12472,6 +12474,7 @@ vectorizable_comparison_1 (vec_info *vinfo, tree
vectype,
enum tree_code bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
int i;
bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
+ loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
vec<tree> vec_oprnds0 = vNULL;
vec<tree> vec_oprnds1 = vNULL;
tree mask_type;
@@ -12570,11 +12573,27 @@ vectorizable_comparison_1 (vec_info *vinfo, tree
vectype,
}
}
+ int vec_num = vect_get_num_copies (vinfo, slp_node);
+ vec_loop_lens *lens = !loop_vinfo ? NULL : &LOOP_VINFO_LENS (loop_vinfo);
+ if (use_cond_len_compares && !loop_vinfo)
+ use_cond_len_compares = false;
+ if (use_cond_len_compares
+ && (bitop1 != NOP_EXPR
+ || !expand_cond_len_vec_cmp_p (vectype, mask_type, code)))
+ use_cond_len_compares = false;
+
if (cost_vec)
{
if (bitop1 == NOP_EXPR)
{
- if (!expand_vec_cmp_expr_p (vectype, mask_type, code))
+ if (use_cond_len_compares)
+ {
+ gcc_assert (lens);
+ vect_record_loop_len (loop_vinfo, lens, vec_num, vectype, 1);
+ }
+
+ if (!use_cond_len_compares
+ && !expand_vec_cmp_expr_p (vectype, mask_type, code))
return false;
}
else
@@ -12632,8 +12651,23 @@ vectorizable_comparison_1 (vec_info *vinfo, tree
vectype,
new_temp = make_temp_ssa_name (mask_type, NULL, "cmp");
if (bitop1 == NOP_EXPR)
{
- new_stmt = gimple_build_assign (new_temp, code,
- vec_rhs1, vec_rhs2);
+ if (use_cond_len_compares)
+ {
+ tree len = vect_get_loop_len (loop_vinfo, gsi, lens, vec_num,
+ vectype, i, 1, true);
+ tree bias = build_int_cst (intQI_type_node,
+ LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo));
+ tree all_one_mask = build_all_ones_cst (mask_type);
+ tree code_arg = build_int_cst (integer_type_node, code);
+ new_stmt
+ = gimple_build_call_internal (IFN_COND_LEN_VEC_CMP, 6, code_arg,
+ all_one_mask, vec_rhs1, vec_rhs2,
+ len, bias);
+ gimple_call_set_lhs (new_stmt, new_temp);
+ }
+ else
+ new_stmt = gimple_build_assign (new_temp, code,
+ vec_rhs1, vec_rhs2);
vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
}
else
@@ -12692,8 +12726,17 @@ vectorizable_comparison (vec_info *vinfo,
enum tree_code code = gimple_assign_rhs_code (stmt);
tree vectype = SLP_TREE_VECTYPE (slp_node);
+ bool use_cond_len_compares = false;
+ loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
+ stmt_vec_info orig_stmt_info = vect_orig_stmt (stmt_info);
+ if (gsi && loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
+ && STMT_VINFO_DEF_TYPE (orig_stmt_info) == vect_condition_def
+ && STMT_VINFO_STMT (orig_stmt_info)
+ != LOOP_VINFO_LOOP_IV_COND (loop_vinfo))
+ use_cond_len_compares = true;
if (!vectorizable_comparison_1 (vinfo, vectype, stmt_info, code, gsi,
- slp_node, cost_vec, NULL))
+ slp_node, cost_vec, NULL,
+ use_cond_len_compares))
return false;
if (cost_vec)
@@ -12888,6 +12931,9 @@ vectorizable_early_exit (loop_vec_info loop_vinfo,
stmt_vec_info stmt_info,
}
}
+ /* A flipped condition needs the inactive lanes masked after inversion. */
+ bool use_cond_len_compares = !flipped;
+
/* Analyze only. */
if (cost_vec)
{
@@ -12905,14 +12951,16 @@ vectorizable_early_exit (loop_vec_info loop_vinfo,
stmt_vec_info stmt_info,
tree comparison_vectype = NULL_TREE;
if (!vectorizable_comparison_1 (loop_vinfo, vectype, stmt_info, code,
gsi,
slp_node, cost_vec,
- &comparison_vectype))
+ &comparison_vectype,
+ use_cond_len_compares))
return false;
gcc_assert (comparison_vectype);
if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
{
- if (direct_internal_fn_supported_p (IFN_VCOND_MASK_LEN, vectype,
- OPTIMIZE_FOR_SPEED))
+ if (use_cond_len_compares
+ || direct_internal_fn_supported_p (IFN_VCOND_MASK_LEN, vectype,
+ OPTIMIZE_FOR_SPEED))
vect_record_loop_len (loop_vinfo, lens, vec_num, vectype, 1);
else
vect_record_loop_mask (loop_vinfo, masks, vec_num, vectype, NULL);
@@ -13031,6 +13079,11 @@ vectorizable_early_exit (loop_vec_info loop_vinfo,
stmt_vec_info stmt_info,
EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
}
+ /* Check whether vectorization honored the cond_len request. */
+ use_cond_len_compares
+ = gimple_call_internal_p (SSA_NAME_DEF_STMT (stmts[0]),
+ IFN_COND_LEN_VEC_CMP);
+
/* Determine if we need to reduce the final value. */
if (stmts.length () > 1)
{
@@ -13052,7 +13105,7 @@ vectorizable_early_exit (loop_vec_info loop_vinfo,
stmt_vec_info stmt_info,
stmts[i], &cond_gsi);
workset.quick_push (stmt_mask);
}
- else if (len_loop_p)
+ else if (len_loop_p && !use_cond_len_compares)
for (unsigned i = 0; i < stmts.length (); i++)
{
tree len_mask = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi,
@@ -13097,7 +13150,7 @@ vectorizable_early_exit (loop_vec_info loop_vinfo,
stmt_vec_info stmt_info,
new_temp = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
new_temp, &cond_gsi);
}
- else if (len_loop_p)
+ else if (len_loop_p && !use_cond_len_compares)
new_temp = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi, lens,
1, vectype, new_temp, 0, 1);
}
--
diff --git a/gcc/optabs-tree.cc b/gcc/optabs-tree.cc
index 5360a509f8d85394c219acfbd6f87eb060928419..3a22cc0af753c2935c5e407e7edb1f8683972a25 100644
--- a/gcc/optabs-tree.cc
+++ b/gcc/optabs-tree.cc
@@ -439,6 +439,24 @@ expand_vec_cmp_expr_p (tree value_type, tree mask_type, enum tree_code code)
|| vec_cmp_eq_icode_p (value_type, mask_type, code);
}
+/* Return TRUE if appropriate vector insn is available
+ for vector comparison expr with vector type VALUE_TYPE
+ and resulting mask with MASK_TYPE using a LEN and BIAS
+ that will be provided later. */
+
+bool
+expand_cond_len_vec_cmp_p (tree value_type, tree mask_type, enum tree_code code)
+{
+ enum rtx_code rcode = get_rtx_code_1 (code, TYPE_UNSIGNED (value_type));
+ if (rcode == UNKNOWN)
+ return false;
+
+ machine_mode value_mode = TYPE_MODE (value_type);
+ machine_mode mask_mode = TYPE_MODE (mask_type);
+
+ return can_cond_len_vec_cmp_compare_p (rcode, value_mode, mask_mode);
+}
+
/* Return TRUE iff, appropriate vector insns are available
for vector cond expr with vector type VALUE_TYPE and a comparison
with operand vector types in CMP_OP_TYPE. */
diff --git a/gcc/optabs-tree.h b/gcc/optabs-tree.h
index fb9469aaece8bc70f0a6815aaa9a934cb66ffd87..eda6f8adb757f6c7036c7210c5a6ad2407a9f178 100644
--- a/gcc/optabs-tree.h
+++ b/gcc/optabs-tree.h
@@ -41,6 +41,7 @@ bool supportable_half_widening_operation (enum tree_code, tree, tree,
enum tree_code *);
bool supportable_convert_operation (enum tree_code, tree, tree);
bool expand_vec_cmp_expr_p (tree, tree, enum tree_code);
+bool expand_cond_len_vec_cmp_p (tree, tree, enum tree_code);
bool expand_vec_cond_expr_p (tree, tree);
void init_tree_optimization_optabs (tree);
bool target_supports_op_p (tree, enum tree_code,
diff --git a/gcc/optabs.cc b/gcc/optabs.cc
index 5b34938e5e9896667c00cc77de0d1948a3fb19cd..d2700c5c9d1b4ecb105f910db65c8ea8fcc2f088 100644
--- a/gcc/optabs.cc
+++ b/gcc/optabs.cc
@@ -4533,6 +4533,22 @@ can_vec_cmp_compare_p (enum rtx_code code, machine_mode value_mode,
return insn_predicate_matches_p (icode, 1, code, mask_mode, value_mode);
}
+/* Return whether the backend can emit a conditional len vector comparison
+ (cond_len_vec_cmp) for code CODE, comparing operands of mode VALUE_MODE and
+ producing a result with MASK_MODE. */
+
+bool
+can_cond_len_vec_cmp_compare_p (enum rtx_code code, machine_mode value_mode,
+ machine_mode mask_mode)
+{
+ enum insn_code icode
+ = convert_optab_handler (cond_len_vec_cmp_optab, value_mode, mask_mode);
+ if (icode == CODE_FOR_nothing)
+ return false;
+
+ return insn_predicate_matches_p (icode, 1, code, mask_mode, value_mode);
+}
+
/* Return whether the backend can emit vector set instructions for inserting
element into vector at variable index position. */
diff --git a/gcc/optabs.h b/gcc/optabs.h
index fe4152b348097affc6c89ab84fba26b76d54d06f..5da3893b85626628dd5387122a21d2cf27a412df 100644
--- a/gcc/optabs.h
+++ b/gcc/optabs.h
@@ -261,6 +261,12 @@ extern bool can_compare_p (enum rtx_code, machine_mode,
with MASK_MODE. */
extern bool can_vec_cmp_compare_p (enum rtx_code, machine_mode, machine_mode);
+/* Return whether the backend can emit a conditional len vector comparison
+ (cond_len_vec_cmp) for code CODE, comparing operands of mode VALUE_MODE and
+ producing a result with MASK_MODE. */
+extern bool can_cond_len_vec_cmp_compare_p (enum rtx_code, machine_mode,
+ machine_mode);
+
/* Return whether the backend can emit vector set instructions for inserting
element into vector at variable index position. */
extern bool can_vec_set_var_idx_p (machine_mode);
diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
index 422805e55c62808a09218dc87e6d3b22855da4ec..fe9597bf7e41b82d99421394193976af8dd8e199 100644
--- a/gcc/tree-vect-stmts.cc
+++ b/gcc/tree-vect-stmts.cc
@@ -12454,14 +12454,16 @@ vectorizable_condition (vec_info *vinfo,
Return true if STMT_INFO is vectorizable in this way. If
COMPARISON_VECTYPE_OUT is nonnull, store the vector type of the comparison
- inputs there. */
+ inputs there. If USE_COND_LEN_COMPARES is true, use LEN and BIAS directly
+ when supported, otherwise clear USE_COND_LEN_COMPARES. */
static bool
vectorizable_comparison_1 (vec_info *vinfo, tree vectype,
stmt_vec_info stmt_info, tree_code code,
gimple_stmt_iterator *gsi,
slp_tree slp_node, stmt_vector_for_cost *cost_vec,
- tree *comparison_vectype_out)
+ tree *comparison_vectype_out,
+ bool &use_cond_len_compares)
{
tree lhs, rhs1, rhs2;
tree vectype1 = NULL_TREE, vectype2 = NULL_TREE;
@@ -12472,6 +12474,7 @@ vectorizable_comparison_1 (vec_info *vinfo, tree vectype,
enum tree_code bitop1 = NOP_EXPR, bitop2 = NOP_EXPR;
int i;
bb_vec_info bb_vinfo = dyn_cast <bb_vec_info> (vinfo);
+ loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
vec<tree> vec_oprnds0 = vNULL;
vec<tree> vec_oprnds1 = vNULL;
tree mask_type;
@@ -12570,11 +12573,27 @@ vectorizable_comparison_1 (vec_info *vinfo, tree vectype,
}
}
+ int vec_num = vect_get_num_copies (vinfo, slp_node);
+ vec_loop_lens *lens = !loop_vinfo ? NULL : &LOOP_VINFO_LENS (loop_vinfo);
+ if (use_cond_len_compares && !loop_vinfo)
+ use_cond_len_compares = false;
+ if (use_cond_len_compares
+ && (bitop1 != NOP_EXPR
+ || !expand_cond_len_vec_cmp_p (vectype, mask_type, code)))
+ use_cond_len_compares = false;
+
if (cost_vec)
{
if (bitop1 == NOP_EXPR)
{
- if (!expand_vec_cmp_expr_p (vectype, mask_type, code))
+ if (use_cond_len_compares)
+ {
+ gcc_assert (lens);
+ vect_record_loop_len (loop_vinfo, lens, vec_num, vectype, 1);
+ }
+
+ if (!use_cond_len_compares
+ && !expand_vec_cmp_expr_p (vectype, mask_type, code))
return false;
}
else
@@ -12632,8 +12651,23 @@ vectorizable_comparison_1 (vec_info *vinfo, tree vectype,
new_temp = make_temp_ssa_name (mask_type, NULL, "cmp");
if (bitop1 == NOP_EXPR)
{
- new_stmt = gimple_build_assign (new_temp, code,
- vec_rhs1, vec_rhs2);
+ if (use_cond_len_compares)
+ {
+ tree len = vect_get_loop_len (loop_vinfo, gsi, lens, vec_num,
+ vectype, i, 1, true);
+ tree bias = build_int_cst (intQI_type_node,
+ LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo));
+ tree all_one_mask = build_all_ones_cst (mask_type);
+ tree code_arg = build_int_cst (integer_type_node, code);
+ new_stmt
+ = gimple_build_call_internal (IFN_COND_LEN_VEC_CMP, 6, code_arg,
+ all_one_mask, vec_rhs1, vec_rhs2,
+ len, bias);
+ gimple_call_set_lhs (new_stmt, new_temp);
+ }
+ else
+ new_stmt = gimple_build_assign (new_temp, code,
+ vec_rhs1, vec_rhs2);
vect_finish_stmt_generation (vinfo, stmt_info, new_stmt, gsi);
}
else
@@ -12692,8 +12726,17 @@ vectorizable_comparison (vec_info *vinfo,
enum tree_code code = gimple_assign_rhs_code (stmt);
tree vectype = SLP_TREE_VECTYPE (slp_node);
+ bool use_cond_len_compares = false;
+ loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
+ stmt_vec_info orig_stmt_info = vect_orig_stmt (stmt_info);
+ if (gsi && loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)
+ && STMT_VINFO_DEF_TYPE (orig_stmt_info) == vect_condition_def
+ && STMT_VINFO_STMT (orig_stmt_info)
+ != LOOP_VINFO_LOOP_IV_COND (loop_vinfo))
+ use_cond_len_compares = true;
if (!vectorizable_comparison_1 (vinfo, vectype, stmt_info, code, gsi,
- slp_node, cost_vec, NULL))
+ slp_node, cost_vec, NULL,
+ use_cond_len_compares))
return false;
if (cost_vec)
@@ -12888,6 +12931,9 @@ vectorizable_early_exit (loop_vec_info loop_vinfo, stmt_vec_info stmt_info,
}
}
+ /* A flipped condition needs the inactive lanes masked after inversion. */
+ bool use_cond_len_compares = !flipped;
+
/* Analyze only. */
if (cost_vec)
{
@@ -12905,14 +12951,16 @@ vectorizable_early_exit (loop_vec_info loop_vinfo, stmt_vec_info stmt_info,
tree comparison_vectype = NULL_TREE;
if (!vectorizable_comparison_1 (loop_vinfo, vectype, stmt_info, code, gsi,
slp_node, cost_vec,
- &comparison_vectype))
+ &comparison_vectype,
+ use_cond_len_compares))
return false;
gcc_assert (comparison_vectype);
if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
{
- if (direct_internal_fn_supported_p (IFN_VCOND_MASK_LEN, vectype,
- OPTIMIZE_FOR_SPEED))
+ if (use_cond_len_compares
+ || direct_internal_fn_supported_p (IFN_VCOND_MASK_LEN, vectype,
+ OPTIMIZE_FOR_SPEED))
vect_record_loop_len (loop_vinfo, lens, vec_num, vectype, 1);
else
vect_record_loop_mask (loop_vinfo, masks, vec_num, vectype, NULL);
@@ -13031,6 +13079,11 @@ vectorizable_early_exit (loop_vec_info loop_vinfo, stmt_vec_info stmt_info,
EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
}
+ /* Check whether vectorization honored the cond_len request. */
+ use_cond_len_compares
+ = gimple_call_internal_p (SSA_NAME_DEF_STMT (stmts[0]),
+ IFN_COND_LEN_VEC_CMP);
+
/* Determine if we need to reduce the final value. */
if (stmts.length () > 1)
{
@@ -13052,7 +13105,7 @@ vectorizable_early_exit (loop_vec_info loop_vinfo, stmt_vec_info stmt_info,
stmts[i], &cond_gsi);
workset.quick_push (stmt_mask);
}
- else if (len_loop_p)
+ else if (len_loop_p && !use_cond_len_compares)
for (unsigned i = 0; i < stmts.length (); i++)
{
tree len_mask = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi,
@@ -13097,7 +13150,7 @@ vectorizable_early_exit (loop_vec_info loop_vinfo, stmt_vec_info stmt_info,
new_temp = prepare_vec_mask (loop_vinfo, TREE_TYPE (mask), mask,
new_temp, &cond_gsi);
}
- else if (len_loop_p)
+ else if (len_loop_p && !use_cond_len_compares)
new_temp = vect_gen_loop_len_mask (loop_vinfo, gsi, &cond_gsi, lens,
1, vectype, new_temp, 0, 1);
}