On Thu, 22 Oct 2026, Tamar Christina wrote:
> This patch first refactors the masked vector-compare recognition so it can be
> re-used outside the old optabs.cc local code and uses it during out-of-SSA.
>
> The new helper identifies plain masked comparisons, length-masked
> VCOND_MASK_LEN
> comparisons, and the conditional compare IFNs added by the following patches.
>
> The out-of-SSA path can then duplicate the comparison at the point where a
> conditional branch needs it. That matters for vector predicates because the
> branch expander often needs to see the original comparison operands, not only
> an
> SSA name that has already been lowered to a predicate value.
>
> For example:
>
> int f (int *__restrict a, int *__restrict b, int *__restrict c,
> int *__restrict d)
> {
> for (int i = 0; i < 1024; ++i)
> if (c[i] != 0)
> return c[i];
> return 0;
> }
>
> The vector compare can be needed both as a data value and for the branch. For
> vec_cbranch_* to be used, TER needs to be able to replace the branch's compare
> use, but the result may also be needed for live-value extraction.
> To fix this we duplicate vector compares during out-of-SSA and redirect the
> non-branch uses to the copy. The original comparison is then available for
> branch expansion.
For TER you mean. I do wonder how to ever deal with this without TER,
aka from ISEL. The main issue is that branch patterns like vec_cbranch
are not control stmts so on GIMPLE we'd need
if (.VEC_CBRANCH (...))
> If the comparison and mask are still separate, we avoid
> duplicating a potentially trapping comparison across the intervening code.
>
> There is a bit of code movement as gimple_is_masked_p is refactored to a
> helper
> that can be used by ISEL as well.
>
> Bootstrapped Regtested on aarch64-none-linux-gnu,
> arm-none-linux-gnueabihf, x86_64-pc-linux-gnu
> -m32, -m64 and no issues.
>
> -O3, profiled, profiled -O3 and -O3 -LTO bootstrap tested and no issues.
>
> Also SPECCPU 2017 and SPECCPU 2026 tested.
>
> OK for master?
>
> Thanks,
> Tamar
>
> gcc/ChangeLog:
>
> * gimple.cc: Include tree-ssa-live.h and tree-outof-ssa.h.
> (gimple_get_ssa_value): New helper to find SSA definitions before
> and after out-of-SSA.
> (gimple_is_masked_p): New helper. Recognize masked comparisons,
> VCOND_MASK_LEN and conditional vector compare IFNs.
> * gimple.h (gimple_is_masked_p): Declare.
> * optabs.cc (emit_cmp_and_jump_insns): Use gimple_is_masked_p.
> * tree-outof-ssa.cc: Include gimplify.h.
> (duplicate_vector_condition_def): New helper. Do not duplicate a
> potentially trapping comparison across its separate mask operation.
> (insert_backedge_copies): Duplicate vector comparisons with multiple
> uses and redirect non-branch uses to the copy, leaving the original
> comparison available for branch expansion.
>
> ---
> diff --git a/gcc/gimple.cc b/gcc/gimple.cc
> index
> 3b68902b610997aa7bc772bbffdd4c01626953eb..3bd19953cb8d511405948e69626f12646d4325e4
> 100644
> --- a/gcc/gimple.cc
> +++ b/gcc/gimple.cc
> @@ -50,6 +50,8 @@ along with GCC; see the file COPYING3. If not see
> #include "ipa-modref-tree.h"
> #include "ipa-modref.h"
> #include "dbgcnt.h"
> +#include "tree-ssa-live.h"
> +#include "tree-outof-ssa.h"
IMO this is a layering violation, so ...
> /* All the tuples have their operand vector (if present) at the very bottom
> of
> the structure. Therefore, the offset required to find the operands
> vector is
> @@ -2484,6 +2486,116 @@ gimple_could_trap_p (const gimple *s)
> return gimple_could_trap_p_1 (s, true, true);
> }
>
> +/* Return the gimple def of ssa_name NAME using a method that's safe to call
> + regarless of whether we are still in SSA or not. */
> +static inline gimple *
> +gimple_get_ssa_value (tree name)
> +{
> + if (gimple_in_ssa_p (cfun))
> + return SSA_NAME_DEF_STMT (name);
> + return get_gimple_for_ssa_name (name);
> +}
this has to go elsewhere, in fact I don't like it at all :/
> +
> +/* Return true if the operation in s is a masked operation. If so return the
> + operation being masked in OP and if available the MASK, LEN and BIAS for
> + the operation. */
> +
So as alternative solution I can offer to put the below matching
into a (match ...) and use the valueize hook to do the expand/pre-expand
difference (return NULL when it cannot be TERed).
> +bool
> +gimple_is_masked_p (gimple *s, gimple **op, tree *mask, tree *len, tree
> *bias)
> +{
> + tree masked_op = NULL_TREE;
> + tree len_op = NULL_TREE;
> + tree len_bias = NULL_TREE;
> + gimple *def_stmt = s;
> +
> + /* First determine if the operation should be masked or unmasked. */
> + if (is_gimple_assign (def_stmt)
> + && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
> + {
> + /* See if one side if a comparison, if so use the other side as
> + the mask. */
> + gimple *mask_def = NULL;
> + tree rhs1 = gimple_assign_rhs1 (def_stmt);
> + tree rhs2 = gimple_assign_rhs2 (def_stmt);
> + if (TREE_CODE (rhs1) == SSA_NAME
> + && (mask_def = gimple_get_ssa_value (rhs1))
> + && is_gimple_assign (mask_def)
> + && TREE_CODE_CLASS (gimple_assign_rhs_code (mask_def))
> + == tcc_comparison)
> + masked_op = rhs2;
> + else if (TREE_CODE (rhs2) == SSA_NAME
> + && (mask_def = gimple_get_ssa_value (rhs2))
> + && is_gimple_assign (mask_def)
> + && TREE_CODE_CLASS (gimple_assign_rhs_code (mask_def))
> + == tcc_comparison)
> + masked_op = rhs1;
> +
> + if (masked_op)
> + def_stmt = mask_def;
> + }
> + /* Else check to see if we're a LEN target. */
> + else if (is_gimple_call (def_stmt)
> + && gimple_call_internal_p (def_stmt)
> + && gimple_call_internal_fn (def_stmt) == IFN_VCOND_MASK_LEN)
> + {
> + /* Example to consume:
> +
> + a = _59 != vect__4.17_75;
> + vcmp = .VCOND_MASK_LEN (a, { -1, ... }, { 0, ... }, _90, 0);
> + if (vcmp != { 0, ... })
> +
> + and transform into
> +
> + if (cond_len_vec_cbranch_any ({-1, ...}, a, _90, 0)). */
> + gcall *call = dyn_cast <gcall *> (def_stmt);
> + tree true_branch = gimple_call_arg (call, 1);
> + tree false_branch = gimple_call_arg (call, 2);
> + if (integer_minus_onep (true_branch)
> + && integer_zerop (false_branch))
> + {
> + len_op = gimple_call_arg (call, 3);
> + len_bias = gimple_call_arg (call, 4);
> + tree arg0 = gimple_call_arg (call, 0);
> +
> + if (TREE_CODE (arg0) == SSA_NAME)
> + def_stmt = gimple_get_ssa_value (arg0);
> + }
> + }
> + else if (is_gimple_call (def_stmt)
> + && gimple_call_internal_p (def_stmt))
> + {
> + switch (gimple_call_internal_fn (def_stmt))
> + {
> + case IFN_COND_VEC_CMP:
> + {
> + masked_op = gimple_call_arg (def_stmt, 1);
> + break;
> + }
> + case IFN_COND_LEN_VEC_CMP:
> + {
> + masked_op = gimple_call_arg (def_stmt, 1);
> + len_op = gimple_call_arg (def_stmt, 4);
> + len_bias = gimple_call_arg (def_stmt, 5);
> + break;
> + }
> + default:
> + return false;
> + }
> + }
> + else
> + return false;
> +
> + if (!masked_op && !len_op)
> + return false;
> +
> + *op = def_stmt;
> + *mask = masked_op;
> + *len = len_op;
> + *bias = len_bias;
> +
> + return true;
> +}
> +
> /* Return true if RHS of a GIMPLE_ASSIGN S can trap. */
>
> bool
> diff --git a/gcc/gimple.h b/gcc/gimple.h
> index
> 0245e5e0934bd37ed24b98ce30640897ea828256..9d981d81fdb2096f661046b073388411812611f2
> 100644
> --- a/gcc/gimple.h
> +++ b/gcc/gimple.h
> @@ -1653,6 +1653,7 @@ bool gimple_has_side_effects (const gimple *);
> bool gimple_could_trap_p_1 (const gimple *, bool, bool);
> bool gimple_could_trap_p (const gimple *);
> bool gimple_assign_rhs_could_trap_p (gimple *);
> +bool gimple_is_masked_p (gimple *, gimple **, tree *, tree *, tree *);
> extern void dump_gimple_statistics (void);
> unsigned get_gimple_rhs_num_ops (enum tree_code);
> gcall *gimple_call_copy_skip_args (gcall *, bitmap);
> diff --git a/gcc/optabs.cc b/gcc/optabs.cc
> index
> 3b7cf55a81bd62011e32a9a762eb69c4347b3b0b..a7988e0c26c992419871b97a157504e2d96ab434
> 100644
> --- a/gcc/optabs.cc
> +++ b/gcc/optabs.cc
> @@ -5034,57 +5034,10 @@ emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code
> comparison, rtx size,
> tree masked_op = NULL_TREE;
> tree len_op = NULL_TREE;
> tree len_bias = NULL_TREE;
> - /* First determine if the operation should be masked or unmasked. */
> - if (is_gimple_assign (def_stmt)
> - && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
> - {
> - /* See if one side if a comparison, if so use the other side as
> - the mask. */
> - gimple *mask_def = NULL;
> - tree rhs1 = gimple_assign_rhs1 (def_stmt);
> - tree rhs2 = gimple_assign_rhs2 (def_stmt);
> - if (TREE_CODE (rhs1) == SSA_NAME
> - && (mask_def = get_gimple_for_ssa_name (rhs1))
> - && is_gimple_assign (mask_def)
> - && TREE_CODE_CLASS (gimple_assign_rhs_code (mask_def)))
> - masked_op = rhs2;
> - else if (TREE_CODE (rhs2) == SSA_NAME
> - && (mask_def = get_gimple_for_ssa_name (rhs2))
> - && is_gimple_assign (mask_def)
> - && TREE_CODE_CLASS (gimple_assign_rhs_code (mask_def)))
> - masked_op = rhs1;
> -
> - if (masked_op)
> - def_stmt = mask_def;
> - }
> - /* Else check to see if we're a LEN target. */
> - else if (is_gimple_call (def_stmt)
> - && gimple_call_internal_p (def_stmt)
> - && gimple_call_internal_fn (def_stmt) == IFN_VCOND_MASK_LEN)
> - {
> - /* Example to consume:
> -
> - a = _59 != vect__4.17_75;
> - vcmp = .VCOND_MASK_LEN (a, { -1, ... }, { 0, ... }, _90, 0);
> - if (vcmp != { 0, ... })
>
> - and transform into
> -
> - if (cond_len_vec_cbranch_any ({-1, ...}, a, _90, 0)). */
> - gcall *call = dyn_cast <gcall *> (def_stmt);
> - tree true_branch = gimple_call_arg (call, 1);
> - tree false_branch = gimple_call_arg (call, 2);
> - if (integer_minus_onep (true_branch)
> - && integer_zerop (false_branch))
> - {
> - len_op = gimple_call_arg (call, 3);
> - len_bias = gimple_call_arg (call, 4);
> - tree arg0 = gimple_call_arg (call, 0);
> -
> - if (TREE_CODE (arg0) == SSA_NAME)
> - def_stmt = get_gimple_for_ssa_name (arg0);
> - }
> - }
> + /* First determine if the operation should be masked or unmasked. */
> + gimple_is_masked_p (def_stmt, &def_stmt, &masked_op, &len_op,
> + &len_bias);
>
> enum insn_code icode;
> if (def_stmt
> diff --git a/gcc/tree-outof-ssa.cc b/gcc/tree-outof-ssa.cc
> index
> f113709e50d27f06138f24c8fc38a4eab198d238..c59c080383195d6ad96d9cc09090ad0ce6245703
> 100644
> --- a/gcc/tree-outof-ssa.cc
> +++ b/gcc/tree-outof-ssa.cc
> @@ -49,6 +49,7 @@ along with GCC; see the file COPYING3. If not see
> #include "dojump.h"
> #include "internal-fn.h"
> #include "gimple-fold.h"
> +#include "gimplify.h"
>
> /* FIXME: A lot of code here deals with expanding to RTL. All that code
> should be in cfgexpand.cc. */
> @@ -1272,6 +1273,82 @@ trivially_conflicts_p (basic_block bb, tree result,
> tree arg)
> return false;
> }
>
> +/* Duplicate the vector comparison that feeds LHS, returning the new lhs or
> + null if LHS isn't a supported conditional operand. Add any new statements
> + to STMTS. */
> +
> +static tree
> +duplicate_vector_condition_def (tree lhs, gimple_seq *stmts)
Why not do this as part of ISEL and only when the result is then
matched by vec_cbranch?
> +{
> + gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
> + gimple *masked_stmt = def_stmt;
> + tree masked_op = NULL_TREE;
> + tree len_op = NULL_TREE;
> + tree len_bias = NULL_TREE;
> +
> + gimple_is_masked_p (def_stmt, &masked_stmt, &masked_op, &len_op,
> + &len_bias);
> +
> + if (is_gimple_assign (masked_stmt)
> + && TREE_CODE_CLASS (gimple_assign_rhs_code (masked_stmt))
> + == tcc_comparison)
> + {
> + /* If isel left a separate comparison and mask, the copy goes after
> + the mask. Do not repeat a trapping comparison after any statements
> + between the original comparison and the mask. */
> + if (masked_stmt != def_stmt)
> + {
> + tree_code code = gimple_assign_rhs_code (masked_stmt);
> + tree op_type = TREE_TYPE (gimple_assign_rhs1 (masked_stmt));
> + if (VECTOR_TYPE_P (op_type))
> + op_type = TREE_TYPE (op_type);
> + if (gimple_could_trap_p (masked_stmt)
> + || operation_could_trap_p (code, FLOAT_TYPE_P (op_type),
> + false, NULL_TREE))
> + return NULL_TREE;
> + }
> +
> + tree new_lhs
> + = gimple_build (stmts, gimple_assign_rhs_code (masked_stmt),
> + TREE_TYPE (gimple_get_lhs (masked_stmt)),
> + gimple_assign_rhs1 (masked_stmt),
> + gimple_assign_rhs2 (masked_stmt));
> + tree type = TREE_TYPE (new_lhs);
> +
> + if (masked_op)
> + return gimple_build (stmts, BIT_AND_EXPR, type, masked_op, new_lhs);
> +
> + if (!len_op)
> + return new_lhs;
> +
> + tree cmp_lhs = new_lhs;
> + new_lhs = make_ssa_name (type);
> + gcall *call = gimple_build_call_internal (IFN_VCOND_MASK_LEN, 5,
> + cmp_lhs,
> + build_minus_one_cst (type),
> + build_zero_cst (type),
> + len_op, len_bias);
> + gimple_call_set_lhs (call, new_lhs);
> + gimple_set_location (call, gimple_location (masked_stmt));
> + gimple_seq_add_stmt (stmts, call);
> + return new_lhs;
> + }
> +
> + gcall *call = dyn_cast <gcall *> (masked_stmt);
> + if (!call || !gimple_call_internal_p (call))
> + return NULL_TREE;
> +
> + internal_fn ifn = gimple_call_internal_fn (call);
> + if (ifn != IFN_COND_VEC_CMP && ifn != IFN_COND_LEN_VEC_CMP)
> + return NULL_TREE;
> +
> + gcall *copy = as_a <gcall *> (gimple_copy (call));
> + tree new_lhs = duplicate_ssa_name (lhs, copy);
> + gimple_call_set_lhs (copy, new_lhs);
> + gimple_seq_add_stmt (stmts, copy);
> + return new_lhs;
> +}
> +
>
> /* Search every PHI node for arguments associated with backedges which
> we can trivially determine will need a copy (the argument is either
> @@ -1448,6 +1525,52 @@ insert_backedge_copies (void)
> }
> }
>
> + /* Find any block with a jump inside it and identify if it's a vector
> + conditional. */
> + gcond *cond = NULL;
> + if (!single_succ_p (bb))
> + {
> + gimple *last_stmt = gsi_stmt (gsi_last_nondebug_bb (bb));
> + cond = dyn_cast<gcond *> (last_stmt);
> + }
> + tree lhs = cond ? gimple_cond_lhs (cond) : NULL_TREE;
> + if (lhs
> + && TREE_CODE (lhs) == SSA_NAME
> + && VECTOR_TYPE_P (TREE_TYPE (lhs))
> + && !has_single_use (lhs))
> + {
> + gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
> + gimple_seq stmts = NULL;
> + tree new_lhs = duplicate_vector_condition_def (lhs, &stmts);
> + if (new_lhs)
> + {
> + gimple_stmt_iterator def_gsi = gsi_for_stmt (def_stmt);
> + gsi_insert_seq_after (&def_gsi, stmts, GSI_SAME_STMT);
> +
> + imm_use_iterator imm_iter;
> + gimple *use_stmt;
> + use_operand_p use_p;
> + FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, lhs)
> + {
> + if (is_a <gcond *> (use_stmt))
> + continue;
> +
> + FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
> + SET_USE (use_p, new_lhs);
> +
> + update_stmt (use_stmt);
> + }
> +
> + if (dump_file && (dump_flags & TDF_DETAILS))
> + {
> + fprintf (dump_file, "Duplicating vector compare ");
> + print_gimple_stmt (dump_file, def_stmt, 3, TDF_DETAILS);
> + fprintf (dump_file, " to ");
> + print_gimple_stmt (dump_file, stmts, 3, TDF_DETAILS);
> + fprintf (dump_file, "\n");
> + }
> + }
> + }
> /* Unmark this block again. */
> bb->aux = NULL;
> }
>
>
>
--
Richard Biener <[email protected]>
SUSE Software Solutions Germany GmbH,
Frankenstrasse 146, 90461 Nuernberg, Germany;
GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri; (HRB 36809, AG Nuernberg)