This patch adds the IFN and optabs needed by the later no-epilogue early-break
patches.  The vectorizer can already build predicates that describe the active
lanes of a vector iteration, but it did not have a common IFN/optab interface
for asking a target to count those lanes, find the first active lane, build
inclusive or exclusive prefixes, or extract the selected scalar value.

The new IFNs are grouped as follows:

  Unconditional forms:
    COUNT_ACTIVE, INDEX_FIRST_ACTIVE, TAKE_INCL_ACTIVE,
    TAKE_EXCL_ACTIVE and EXTRACT_FIRST_ACTIVE operate on the full predicate.  On
    Adv.SIMD these enable ADDHN/UMOV compression followed by RBIT/CLZ and TBL
    extraction.  On SVE they enable CNTP, BRK*, FIRSTP and SPLICE-based
    first-active extraction.

  Conditional forms:
    COND_COUNT_ACTIVE, COND_INDEX_FIRST_ACTIVE, COND_TAKE_INCL_ACTIVE and
    COND_TAKE_EXCL_ACTIVE apply the same operations under an existing mask.
    The count and index forms take the mask and predicate; the prefix forms
    also take a fallback value for lanes outside the mask.  These are used when
    an early exit is governed by a loop mask or a previous vector predicate.

  Length-limited forms:
    COND_LEN_COUNT_ACTIVE, COND_LEN_INDEX_FIRST_ACTIVE,
    COND_LEN_TAKE_INCL_ACTIVE and COND_LEN_TAKE_EXCL_ACTIVE add the length
    and bias operands used by partial-vector loops.  These let targets account
    for both the governing predicate and the active vector length in one
    operation.

The documentation also describes the conditional vector compare interface added
later in the series, with GIMPLE CFG examples showing the early-exit branch and
live-value extraction.

For example, consider:

  unsigned char a[1008], b[1008];

  unsigned char f1 (void)
  {
    unsigned char res = 0;
    for (unsigned int i = 0; i < 1008; ++i)
      {
        unsigned char x = a[i] & b[i];
        res = x;
        if (x > 5)
          break;
      }
    return res;
  }

Used to generate on GCC trunk using -O3 -march=armv8-a

  cmhi    v30.16b, v31.16b, v29.16b
  umaxp   v30.4s, v30.4s, v30.4s
  fmov    x0, d30
  cbz     x0, .L2
  ldrb    w0, [x2, w1, uxtw]
  cmp     w0, 5
  bhi     .L3
  ... repeated scalar lane checks ...

which branches to scalar to find the element. With this branch we get with
Adv.SIMD:

  cmhi    v31.16b, v30.16b, v29.16b
  addhn   v31.8b, v31.8h, v31.8h
  umov    x0, v31.d[0]
  cbz     x0, .L8
  rbit    x0, x0
  clz     x0, x0
  ... construction of tbl index from x0 
  tbl     v30.16b, {v30.16b}, v31.16b
  fmov    w0, s30

Or with SVE2:

  cmphi   p6.b, p7/z, z31.b, #5
  b.none  .L8
  splice  z31.b, p6, z31.b, z31.b
  fmov    w0, s31
  ret

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:

        * doc/ifn.texi: Document the active-predicate and conditional vector
        compare IFNs, including early-exit CFG examples.
        * doc/md.texi: Document the active-predicate and conditional vector
        compare patterns.
        * internal-fn.cc (cond_mask_unary_direct): Add conditional
        active-predicate direct optab descriptors.
        (cond_len_mask_unary_direct): Add length-limited descriptors.
        (expand_cond_mask_unary_optab_fn): Expand conditional unary IFNs.
        (expand_cond_len_mask_unary_optab_fn): Expand length forms.
        (direct_cond_mask_unary_optab_supported_p): Query optab support.
        (direct_cond_len_mask_unary_optab_supported_p): Likewise.
        (FOR_EACH_COND_FN_PAIR): Add active-predicate IFN mappings.
        (get_len_internal_fn): Likewise.
        (internal_fn_len_index): Return length operand positions for the
        active-predicate and conditional vector compare IFNs.
        (internal_fn_else_index): Return else operand positions.
        (internal_fn_mask_index): Return the mask operand position for
        conditional count and first-active index operations.
        * internal-fn.def (COUNT_ACTIVE): New internal function.
        (COND_COUNT_ACTIVE): Likewise.
        (COND_LEN_COUNT_ACTIVE): Likewise.
        (INDEX_FIRST_ACTIVE): Likewise.
        (COND_INDEX_FIRST_ACTIVE): Likewise.
        (COND_LEN_INDEX_FIRST_ACTIVE): Likewise.
        (TAKE_INCL_ACTIVE): Likewise.
        (COND_TAKE_INCL_ACTIVE): Likewise.
        (COND_LEN_TAKE_INCL_ACTIVE): Likewise.
        (TAKE_EXCL_ACTIVE): Likewise.
        (COND_TAKE_EXCL_ACTIVE): Likewise.
        (COND_LEN_TAKE_EXCL_ACTIVE): Likewise.
        (EXTRACT_FIRST_ACTIVE): Likewise.
        * optabs.def (extract_first_active_optab, count_active_optab,
        cond_count_active_optab, cond_len_count_active_optab,
        index_first_active_optab, cond_index_first_active_optab,
        cond_len_index_first_active_optab, take_incl_active_optab,
        take_excl_active_optab, cond_take_incl_active_optab,
        cond_take_excl_active_optab, cond_len_take_incl_active_optab,
        cond_len_take_excl_active_optab): New.

---
diff --git a/gcc/doc/ifn.texi b/gcc/doc/ifn.texi
index 
4340d01d10854ffcbeed905a885b471eb7ba3e43..ff926922f8bd448a4cdebc15f512b021a5acac3d
 100644
--- a/gcc/doc/ifn.texi
+++ b/gcc/doc/ifn.texi
@@ -457,6 +457,190 @@ The @code{IFN_COND_NOT} internal function directly maps 
to the
 The @code{IFN_COND_LEN_NOT} internal function directly maps to the
 @code{cond_len_one_cmpl} instruction pattern.
 
+@cindex @code{IFN_COUNT_ACTIVE} internal function
+@item @samp{IFN_COUNT_ACTIVE}
+The @code{IFN_COUNT_ACTIVE} internal function directly maps to the
+@code{count_active} instruction pattern.  It returns the number of active
+elements in its argument.
+For an early-exit loop with a live induction variable, the vectorizer can
+count the lanes before the first exit lane as follows, when the target does
+not support @code{IFN_INDEX_FIRST_ACTIVE}.  In this simplified GIMPLE CFG,
+@code{vector_start_2} counts scalar iterations before the current vector
+iteration:
+@smallexample
+<bb 4>:
+  exit_mask_5 = data_3 > needle_1;
+  if (exit_mask_5 != @{ 0, ... @})
+    goto <bb 6>;
+  else
+    goto <bb 5>;
+
+<bb 5>:
+  goto <bb 4>;
+
+<bb 6>:
+  before_7 = .TAKE_EXCL_ACTIVE (exit_mask_5);
+  first_8 = .COUNT_ACTIVE (before_7);
+  iterations_9 = vector_start_2 + first_8;
+  offset_10 = iterations_9 * step_11;
+  live_iv_12 = init_13 + offset_10;
+  goto <bb 7>;
+
+<bb 7>:
+  return live_iv_12;
+@end smallexample
+
+@cindex @code{IFN_COND_COUNT_ACTIVE} internal function
+@item @samp{IFN_COND_COUNT_ACTIVE}
+The @code{IFN_COND_COUNT_ACTIVE} internal function directly maps to the
+@code{cond_count_active} instruction pattern.  It returns the number of
+active elements in its second argument under the mask in its first
+argument.
+
+@cindex @code{IFN_COND_LEN_COUNT_ACTIVE} internal function
+@item @samp{IFN_COND_LEN_COUNT_ACTIVE}
+The @code{IFN_COND_LEN_COUNT_ACTIVE} internal function directly maps to the
+@code{cond_len_count_active} instruction pattern.  It is like
+@code{IFN_COND_COUNT_ACTIVE}, but also ignores elements whose index is
+greater than or equal to the sum of the length and bias arguments.
+
+@cindex @code{IFN_INDEX_FIRST_ACTIVE} internal function
+@item @samp{IFN_INDEX_FIRST_ACTIVE}
+The @code{IFN_INDEX_FIRST_ACTIVE} internal function directly maps to the
+@code{index_first_active} instruction pattern.  It returns the index of the
+first active element in its argument.  The result is undefined if the
+predicate has no active elements.
+When the target supports this operation, the vectorizer can use it directly
+for the same early-exit induction variable update.  The exit block in the CFG
+above then becomes:
+@smallexample
+<bb 6>:
+  first_8 = .INDEX_FIRST_ACTIVE (exit_mask_5);
+  iterations_9 = vector_start_2 + first_8;
+  offset_10 = iterations_9 * step_11;
+  live_iv_12 = init_13 + offset_10;
+  goto <bb 7>;
+@end smallexample
+The branch from block 4 reaches block 6 only when @code{exit_mask_5} is
+nonzero.  Thus the vectorizer calls @code{IFN_INDEX_FIRST_ACTIVE} here only
+with a predicate that has an active element.  It can also use @code{first_8}
+as an offset to reload a scalar live value from a vector load on this edge.
+
+@cindex @code{IFN_COND_INDEX_FIRST_ACTIVE} internal function
+@item @samp{IFN_COND_INDEX_FIRST_ACTIVE}
+The @code{IFN_COND_INDEX_FIRST_ACTIVE} internal function directly maps to the
+@code{cond_index_first_active} instruction pattern.  It returns the index of
+the first active element in its second argument under the mask in its first
+argument.  The result is undefined if the selected predicate has no active
+elements.
+
+@cindex @code{IFN_COND_LEN_INDEX_FIRST_ACTIVE} internal function
+@item @samp{IFN_COND_LEN_INDEX_FIRST_ACTIVE}
+The @code{IFN_COND_LEN_INDEX_FIRST_ACTIVE} internal function directly maps to
+the @code{cond_len_index_first_active} instruction pattern.  It is like
+@code{IFN_COND_INDEX_FIRST_ACTIVE}, but also ignores elements whose index is
+greater than or equal to the sum of the length and bias arguments.
+
+@cindex @code{IFN_TAKE_INCL_ACTIVE} internal function
+@item @samp{IFN_TAKE_INCL_ACTIVE}
+The @code{IFN_TAKE_INCL_ACTIVE} internal function directly maps to the
+@code{take_incl_active} instruction pattern.  It returns a predicate with
+all lanes up to and including the first active lane of its argument set.
+
+@cindex @code{IFN_COND_TAKE_INCL_ACTIVE} internal function
+@item @samp{IFN_COND_TAKE_INCL_ACTIVE}
+The @code{IFN_COND_TAKE_INCL_ACTIVE} internal function directly maps to the
+@code{cond_take_incl_active} instruction pattern.  It returns a predicate
+that finds the first lane active in both its first and second arguments.
+Under the mask in its first argument, all lanes up to and including that lane
+are set and the remaining lanes are cleared.  Outside the mask, the result
+comes from its third argument.
+
+@cindex @code{IFN_COND_LEN_TAKE_INCL_ACTIVE} internal function
+@item @samp{IFN_COND_LEN_TAKE_INCL_ACTIVE}
+The @code{IFN_COND_LEN_TAKE_INCL_ACTIVE} internal function directly maps to
+the @code{cond_len_take_incl_active} instruction pattern.  It is like
+@code{IFN_COND_TAKE_INCL_ACTIVE}, but also ignores elements whose index is
+greater than or equal to the sum of the length and bias arguments.
+
+@cindex @code{IFN_TAKE_EXCL_ACTIVE} internal function
+@item @samp{IFN_TAKE_EXCL_ACTIVE}
+The @code{IFN_TAKE_EXCL_ACTIVE} internal function directly maps to the
+@code{take_excl_active} instruction pattern.  It returns a predicate with
+all lanes before, but excluding, the first active lane of its argument set.
+
+@cindex @code{IFN_COND_TAKE_EXCL_ACTIVE} internal function
+@item @samp{IFN_COND_TAKE_EXCL_ACTIVE}
+The @code{IFN_COND_TAKE_EXCL_ACTIVE} internal function directly maps to the
+@code{cond_take_excl_active} instruction pattern.  It returns a predicate
+that finds the first lane active in both its first and second arguments.
+Under the mask in its first argument, all lanes before that lane are set and
+the remaining lanes are cleared.  Outside the mask, the result comes from
+its third argument.
+
+@cindex @code{IFN_COND_LEN_TAKE_EXCL_ACTIVE} internal function
+@item @samp{IFN_COND_LEN_TAKE_EXCL_ACTIVE}
+The @code{IFN_COND_LEN_TAKE_EXCL_ACTIVE} internal function directly maps to
+the @code{cond_len_take_excl_active} instruction pattern.  It is like
+@code{IFN_COND_TAKE_EXCL_ACTIVE}, but also ignores elements whose index is
+greater than or equal to the sum of the length and bias arguments.
+
+@cindex @code{IFN_COND_VEC_CMP} internal function
+@item @samp{IFN_COND_VEC_CMP}
+The @code{IFN_COND_VEC_CMP} internal function directly maps to the
+@code{cond_vec_cmp} instruction pattern.  It performs the comparison
+identified by its first argument on its third and fourth arguments, then
+returns the comparison result after applying the predicate mask in its
+second argument.
+For example, the vectorizer can produce this GIMPLE CFG for a masked
+early-exit comparison:
+@smallexample
+<bb 4>:
+  cmp_7 = a_5 > b_6;
+  masked_8 = cmp_7 & loop_mask_4;
+  if (masked_8 != @{ 0, ... @})
+    goto <bb 6>;
+  else
+    goto <bb 5>;
+
+<bb 5>:
+  goto <bb 4>;
+
+<bb 6>:
+  return 1;
+@end smallexample
+The GIMPLE instruction-selection pass can replace the comparison and mask
+in block 4, leaving the branch and its successors in place:
+@smallexample
+<bb 4>:
+  masked_8 = .COND_VEC_CMP (GT_EXPR, loop_mask_4, a_5, b_6);
+  if (masked_8 != @{ 0, ... @})
+    goto <bb 6>;
+  else
+    goto <bb 5>;
+@end smallexample
+Here @code{GT_EXPR} stands for the integer tree-code argument that identifies
+the comparison.
+
+@cindex @code{IFN_COND_LEN_VEC_CMP} internal function
+@item @samp{IFN_COND_LEN_VEC_CMP}
+The @code{IFN_COND_LEN_VEC_CMP} internal function directly maps to the
+@code{cond_len_vec_cmp} instruction pattern.  It is like
+@code{IFN_COND_VEC_CMP}, but also ignores elements whose index is greater
+than or equal to the sum of the length and bias arguments.
+For a length-controlled loop, the corresponding CFG has two more arguments
+on the call.  The active length controls which lanes can trigger the early
+exit:
+@smallexample
+<bb 4>:
+  masked_8 = .COND_LEN_VEC_CMP (GT_EXPR, loop_mask_4, a_5, b_6,
+                                 length_7, 0);
+  if (masked_8 != @{ 0, ... @})
+    goto <bb 6>;
+  else
+    goto <bb 5>;
+@end smallexample
+
 @cindex @code{IFN_RSQRT} internal function
 @item @samp{IFN_RSQRT}
 The @code{IFN_RSQRT} internal function directly maps to the @code{rsqrt}
@@ -512,6 +696,31 @@ The @code{IFN_REDUC_SBOOL_XOR} internal function directly 
maps to the
 The @code{IFN_EXTRACT_LAST_ACTIVE} internal function directly maps to the
 @code{extract_last_active} instruction pattern.
 
+@cindex @code{IFN_EXTRACT_FIRST_ACTIVE} internal function
+@item @samp{IFN_EXTRACT_FIRST_ACTIVE}
+The @code{IFN_EXTRACT_FIRST_ACTIVE} internal function directly maps to the
+@code{extract_first_active} instruction pattern.
+For an early-exit loop with a live vector value, the vectorizer can extract
+the value on the exit edge, before its out-of-loop use:
+@smallexample
+<bb 4>:
+  exit_mask_5 = vector_4 > threshold_3;
+  if (exit_mask_5 != @{ 0, ... @})
+    goto <bb 6>;
+  else
+    goto <bb 5>;
+
+<bb 5>:
+  goto <bb 4>;
+
+<bb 6>:
+  value_9 = .EXTRACT_FIRST_ACTIVE (exit_mask_5, vector_4);
+  goto <bb 7>;
+
+<bb 7>:
+  return value_9;
+@end smallexample
+
 @cindex @code{IFN_FOLD_EXTRACT_LAST} internal function
 @item @samp{IFN_FOLD_EXTRACT_LAST}
 The @code{IFN_FOLD_EXTRACT_LAST} internal function directly maps to the
@@ -1559,4 +1768,3 @@ definition is still being built by the current pass.  The 
pass that creates
 it must replace it before finishing; the verifier rejects any remaining calls.
 
 @end table
-
diff --git a/gcc/doc/md.texi b/gcc/doc/md.texi
index 
d72f29e5a71a755e1d647e175152039859b0f2c2..596d9dc23c8af368cea9c485c8b00aa12f00fede
 100644
--- a/gcc/doc/md.texi
+++ b/gcc/doc/md.texi
@@ -7921,6 +7921,34 @@ has vector mode @var{m} while operand 0 has the mode 
appropriate for one
 element of @var{m}.  Operand 1 has the usual mask mode for vectors of mode
 @var{m}; see @code{TARGET_VECTORIZE_GET_MASK_MODE}.
 
+@mdindex extract_first_active_@var{m}
+@item @code{extract_first_active_@var{m}}
+Find the first set bit in mask operand 1 and extract the associated element
+of vector operand 2.  Store the result in scalar operand 0.  Operand 2
+has vector mode @var{m} while operand 0 has the mode appropriate for one
+element of @var{m}.  Operand 1 has the usual mask mode for vectors of mode
+@var{m}; see @code{TARGET_VECTORIZE_GET_MASK_MODE}.
+
+@mdindex count_active_@var{m}
+@item @code{count_active_@var{m}}
+Count the set bits in mask operand 1 and store the result in scalar
+operand 0.
+
+@mdindex index_first_active_@var{m}
+@item @code{index_first_active_@var{m}}
+Find the first set bit in mask operand 1 and store the zero-based element
+index in scalar operand 0.  The result is undefined if no bits are set.
+
+@mdindex take_incl_active_@var{m}
+@item @code{take_incl_active_@var{m}}
+Set every element of mask operand 0 up to and including the first active
+element of mask operand 1, and clear the remaining elements.
+
+@mdindex take_excl_active_@var{m}
+@item @code{take_excl_active_@var{m}}
+Set every element of mask operand 0 before the first active element of mask
+operand 1, and clear the remaining elements.
+
 @mdindex fold_extract_last_@var{m}
 @item @code{fold_extract_last_@var{m}}
 If any bits of mask operand 2 are set, find the last set bit, extract
@@ -8691,6 +8719,76 @@ for (i = 0; i < GET_MODE_NUNITS (@var{m}); i++)
             : op5[i]);
 @end smallexample
 
+@mdindex cond_count_active_@var{mode}
+@item @samp{cond_count_active_@var{mode}}
+Set operand 0 to the number of active elements in operand 2 under the mask
+in operand 1.  Operand 0 has a scalar integer mode.  Operands 1 and 2 have
+mode @var{mode}.
+
+@mdindex cond_len_count_active_@var{mode}
+@item @samp{cond_len_count_active_@var{mode}}
+Like @samp{cond_count_active_@var{mode}}, but additionally ignore elements
+whose index is greater than or equal to the sum of operands 3 and 4.
+Operand 3 has whichever integer mode the target prefers, and operand 4 is
+a bias.
+
+@mdindex cond_index_first_active_@var{mode}
+@item @samp{cond_index_first_active_@var{mode}}
+Set operand 0 to the index of the first active element in operand 2 under
+the mask in operand 1.  Operand 0 has a scalar integer mode.  Operands 1
+and 2 have mode @var{mode}.  The result is undefined if the selected
+predicate has no
+active elements.
+
+@mdindex cond_len_index_first_active_@var{mode}
+@item @samp{cond_len_index_first_active_@var{mode}}
+Like @samp{cond_index_first_active_@var{mode}}, but additionally ignore
+elements whose index is greater than or equal to the sum of operands 3
+and 4.  Operand 3 has whichever integer mode the target prefers, and
+operand 4 is a bias.
+
+@mdindex cond_take_incl_active_@var{mode}
+@item @samp{cond_take_incl_active_@var{mode}}
+Find the first element that is active in both operands 1 and 2.  Under the
+mask in operand 1, set operand 0's elements up to and including that element
+and clear the rest.  Copy operand 3 to operand 0 outside the mask.
+Operands 0, 1, 2 and 3 have mode @var{mode}.
+
+@mdindex cond_len_take_incl_active_@var{mode}
+@item @samp{cond_len_take_incl_active_@var{mode}}
+Like @samp{cond_take_incl_active_@var{mode}}, but additionally ignore
+elements whose index is greater than or equal to the sum of operands 4
+and 5.  Operand 4 has whichever integer mode the target prefers, and
+operand 5 is a bias.
+
+@mdindex cond_take_excl_active_@var{mode}
+@item @samp{cond_take_excl_active_@var{mode}}
+Find the first element that is active in both operands 1 and 2.  Under the
+mask in operand 1, set operand 0's elements before that element and clear
+the rest.  Copy operand 3 to operand 0 outside the mask.  Operands 0, 1, 2
+and 3 have mode @var{mode}.
+
+@mdindex cond_len_take_excl_active_@var{mode}
+@item @samp{cond_len_take_excl_active_@var{mode}}
+Like @samp{cond_take_excl_active_@var{mode}}, but additionally ignore
+elements whose index is greater than or equal to the sum of operands 4
+and 5.  Operand 4 has whichever integer mode the target prefers, and
+operand 5 is a bias.
+
+@mdindex cond_vec_cmp@var{m}@var{n}
+@item @samp{cond_vec_cmp@var{m}@var{n}}
+Set operand 0 to the result of applying comparison operand 1 to operands
+3 and 4, after applying predicate mask operand 2.  Operand 0 has mode
+@var{n}; operands 3 and 4 have mode @var{m}.  Operand 2 has the mode
+returned by @code{TARGET_VECTORIZE_GET_MASK_MODE} for @var{m}.
+
+@mdindex cond_len_vec_cmp@var{m}@var{n}
+@item @samp{cond_len_vec_cmp@var{m}@var{n}}
+Like @samp{cond_vec_cmp@var{m}@var{n}}, but additionally ignore elements
+whose index is greater than or equal to the sum of operands 5 and 6.
+Operand 5 has whichever integer mode the target prefers, and operand 6 is
+a bias.
+
 @mdindex cbranch@var{mode}4
 @item @samp{cbranch@var{mode}4}
 Conditional branch instruction combined with a compare instruction.
diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc
index 
45ad08d2cf6120b700fe63233157744e52cca454..7d2f887e8c72aedad28161da75d9a580dfc05609
 100644
--- a/gcc/internal-fn.cc
+++ b/gcc/internal-fn.cc
@@ -186,6 +186,8 @@ init_internal_fns ()
 #define cond_len_unary_direct { 1, 1, true }
 #define cond_len_binary_direct { 1, 1, true }
 #define cond_len_ternary_direct { 1, 1, true }
+#define cond_mask_unary_direct { 0, 0, true }
+#define cond_len_mask_unary_direct { 0, 0, true }
 #define while_direct { 0, 2, false }
 #define fold_extract_direct { 2, 2, false }
 #define fold_len_extract_direct { 2, 2, false }
@@ -4305,6 +4307,12 @@ expand_reduc_sbool_optab_fn (internal_fn fn, gcall 
*stmt, direct_optab optab)
 #define expand_cond_len_ternary_optab_fn(FN, STMT, OPTAB) \
   expand_direct_optab_fn (FN, STMT, OPTAB, 7)
 
+#define expand_cond_mask_unary_optab_fn(FN, STMT, OPTAB) \
+  expand_direct_optab_fn (FN, STMT, OPTAB, 2)
+
+#define expand_cond_len_mask_unary_optab_fn(FN, STMT, OPTAB) \
+  expand_direct_optab_fn (FN, STMT, OPTAB, 4)
+
 #define expand_fold_extract_optab_fn(FN, STMT, OPTAB) \
   expand_direct_optab_fn (FN, STMT, OPTAB, 3)
 
@@ -4415,6 +4423,8 @@ multi_vector_optab_supported_p (convert_optab optab, 
tree_pair types,
 #define direct_cond_len_unary_optab_supported_p direct_optab_supported_p
 #define direct_cond_len_binary_optab_supported_p direct_optab_supported_p
 #define direct_cond_len_ternary_optab_supported_p direct_optab_supported_p
+#define direct_cond_mask_unary_optab_supported_p direct_optab_supported_p
+#define direct_cond_len_mask_unary_optab_supported_p direct_optab_supported_p
 #define direct_crc_optab_supported_p convert_optab_supported_p
 #define direct_mask_load_optab_supported_p convert_optab_supported_p
 #define direct_load_lanes_optab_supported_p multi_vector_optab_supported_p
@@ -4903,6 +4913,8 @@ get_conditional_len_internal_fn (tree_code code)
 /* Invoke T(IFN) for each internal function IFN that also has an
    IFN_COND_* form.  */
 #define FOR_EACH_COND_FN_PAIR(T) \
+  T (TAKE_INCL_ACTIVE) \
+  T (TAKE_EXCL_ACTIVE) \
   T (FMAX) \
   T (FMIN) \
   T (FMA) \
@@ -4969,6 +4981,14 @@ get_len_internal_fn (internal_fn fn)
       return IFN_MASK_LEN_LOAD_LANES;
     case IFN_MASK_GATHER_LOAD:
       return IFN_MASK_LEN_GATHER_LOAD;
+    case IFN_COND_COUNT_ACTIVE:
+      return IFN_COND_LEN_COUNT_ACTIVE;
+    case IFN_COND_INDEX_FIRST_ACTIVE:
+      return IFN_COND_LEN_INDEX_FIRST_ACTIVE;
+    case IFN_COND_TAKE_INCL_ACTIVE:
+      return IFN_COND_LEN_TAKE_INCL_ACTIVE;
+    case IFN_COND_TAKE_EXCL_ACTIVE:
+      return IFN_COND_LEN_TAKE_EXCL_ACTIVE;
     default:
       return IFN_LAST;
     }
@@ -5188,6 +5208,17 @@ internal_fn_len_index (internal_fn fn)
     case IFN_MASK_LEN_STRIDED_STORE:
       return 4;
 
+    case IFN_COND_LEN_COUNT_ACTIVE:
+    case IFN_COND_LEN_INDEX_FIRST_ACTIVE:
+      return 2;
+
+    case IFN_COND_LEN_TAKE_INCL_ACTIVE:
+    case IFN_COND_LEN_TAKE_EXCL_ACTIVE:
+      return 3;
+
+    case IFN_COND_LEN_VEC_CMP:
+      return 4;
+
     case IFN_COND_LEN_NEG:
     case IFN_MASK_LEN_STORE:
     case IFN_MASK_LEN_STORE_LANES:
@@ -5225,6 +5256,10 @@ internal_fn_else_index (internal_fn fn)
     case IFN_COND_LEN_FLOOR:
     case IFN_COND_LEN_ROUND:
     case IFN_COND_LEN_RINT:
+    case IFN_COND_TAKE_INCL_ACTIVE:
+    case IFN_COND_TAKE_EXCL_ACTIVE:
+    case IFN_COND_LEN_TAKE_INCL_ACTIVE:
+    case IFN_COND_LEN_TAKE_EXCL_ACTIVE:
       return 2;
 
     case IFN_LEN_LOAD:
@@ -5320,6 +5355,13 @@ internal_fn_mask_index (internal_fn fn)
     case IFN_VCOND_MASK_LEN:
       return 0;
 
+    /* These operations use a mask but have no fallback operand.  */
+    case IFN_COND_COUNT_ACTIVE:
+    case IFN_COND_LEN_COUNT_ACTIVE:
+    case IFN_COND_INDEX_FIRST_ACTIVE:
+    case IFN_COND_LEN_INDEX_FIRST_ACTIVE:
+      return 0;
+
     default:
       return (conditional_internal_fn_code (fn) != ERROR_MARK
              || get_unconditional_internal_fn (fn) != IFN_LAST ? 0 : -1);
diff --git a/gcc/internal-fn.def b/gcc/internal-fn.def
index 
7feb236d8026d0e3f542f2ee5573ce0784147a9a..daf716f1ba143012e7036020dc9e5d126916df30
 100644
--- a/gcc/internal-fn.def
+++ b/gcc/internal-fn.def
@@ -89,6 +89,10 @@ along with GCC; see the file COPYING3.  If not see
    - cond_len_binary: a conditional binary optab, such as cond_len_add<mode>
    - cond_len_ternary: a conditional ternary optab, such as
    cond_len_fma_rev<mode>
+   - cond_mask_unary: a unary optab that takes a mask and a predicate operand,
+     such as cond_count_active<mode>
+   - cond_len_mask_unary: a unary optab that takes a mask, a predicate
+     operand, a length and a bias, such as cond_len_count_active<mode>
 
    DEF_INTERNAL_SIGNED_OPTAB_FN defines an internal function that
    maps to one of two optabs, depending on the signedness of an input.
@@ -337,6 +341,28 @@ DEF_INTERNAL_COND_FN (FNMS, ECF_CONST, fnms, ternary)
 DEF_INTERNAL_COND_FN (NEG, ECF_CONST, neg, unary)
 DEF_INTERNAL_COND_FN (NOT, ECF_CONST, one_cmpl, unary)
 
+DEF_INTERNAL_OPTAB_FN (COUNT_ACTIVE, ECF_CONST, count_active, unary)
+DEF_INTERNAL_OPTAB_FN (COND_COUNT_ACTIVE, ECF_CONST, cond_count_active,
+                      cond_mask_unary)
+DEF_INTERNAL_OPTAB_FN (COND_LEN_COUNT_ACTIVE, ECF_CONST,
+                      cond_len_count_active, cond_len_mask_unary)
+DEF_INTERNAL_OPTAB_FN (INDEX_FIRST_ACTIVE, ECF_CONST, index_first_active,
+                      unary)
+DEF_INTERNAL_OPTAB_FN (COND_INDEX_FIRST_ACTIVE, ECF_CONST,
+                      cond_index_first_active, cond_mask_unary)
+DEF_INTERNAL_OPTAB_FN (COND_LEN_INDEX_FIRST_ACTIVE, ECF_CONST,
+                      cond_len_index_first_active, cond_len_mask_unary)
+DEF_INTERNAL_OPTAB_FN (TAKE_INCL_ACTIVE, ECF_CONST, take_incl_active, unary)
+DEF_INTERNAL_OPTAB_FN (COND_TAKE_INCL_ACTIVE, ECF_CONST,
+                      cond_take_incl_active, cond_unary)
+DEF_INTERNAL_OPTAB_FN (COND_LEN_TAKE_INCL_ACTIVE, ECF_CONST,
+                      cond_len_take_incl_active, cond_len_unary)
+DEF_INTERNAL_OPTAB_FN (TAKE_EXCL_ACTIVE, ECF_CONST, take_excl_active, unary)
+DEF_INTERNAL_OPTAB_FN (COND_TAKE_EXCL_ACTIVE, ECF_CONST,
+                      cond_take_excl_active, cond_unary)
+DEF_INTERNAL_OPTAB_FN (COND_LEN_TAKE_EXCL_ACTIVE, ECF_CONST,
+                      cond_len_take_excl_active, cond_len_unary)
+
 DEF_INTERNAL_OPTAB_FN (RSQRT, ECF_CONST, rsqrt, unary)
 
 DEF_INTERNAL_OPTAB_FN (REDUC_PLUS, ECF_CONST | ECF_NOTHROW,
@@ -363,9 +389,11 @@ DEF_INTERNAL_OPTAB_FN (REDUC_SBOOL_XOR, ECF_CONST | 
ECF_NOTHROW,
                       reduc_sbool_xor_scal, reduc_sbool)
 
 
-/* Extract the last active element from a vector.  */
+/* Extract the first or last active element from a vector.  */
 DEF_INTERNAL_OPTAB_FN (EXTRACT_LAST_ACTIVE, ECF_CONST | ECF_NOTHROW,
                       extract_last_active, fold_left)
+DEF_INTERNAL_OPTAB_FN (EXTRACT_FIRST_ACTIVE, ECF_CONST | ECF_NOTHROW,
+                      extract_first_active, fold_left)
 
 /* Same, but return the first argument if no elements are active.  */
 DEF_INTERNAL_OPTAB_FN (FOLD_EXTRACT_LAST, ECF_CONST | ECF_NOTHROW,
diff --git a/gcc/optabs.def b/gcc/optabs.def
index 
4ee434ea1a7c4b8474d2691e53fcf9bf925c1efa..79751e28d9531620c01bb85f7bdb4a01c7e138a3
 100644
--- a/gcc/optabs.def
+++ b/gcc/optabs.def
@@ -430,8 +430,21 @@ OPTAB_D (mask_fold_left_plus_optab, 
"mask_fold_left_plus_$a")
 OPTAB_D (mask_len_fold_left_plus_optab, "mask_len_fold_left_plus_$a")
 
 OPTAB_D (extract_last_active_optab, "extract_last_active_$a")
+OPTAB_D (extract_first_active_optab, "extract_first_active_$a")
+OPTAB_D (count_active_optab, "count_active_$a")
+OPTAB_D (cond_count_active_optab, "cond_count_active_$a")
+OPTAB_D (cond_len_count_active_optab, "cond_len_count_active_$a")
+OPTAB_D (index_first_active_optab, "index_first_active_$a")
+OPTAB_D (cond_index_first_active_optab, "cond_index_first_active_$a")
+OPTAB_D (cond_len_index_first_active_optab, "cond_len_index_first_active_$a")
 OPTAB_D (fold_extract_last_optab, "fold_extract_last_$a")
 OPTAB_D (len_fold_extract_last_optab, "len_fold_extract_last_$a")
+OPTAB_D (take_incl_active_optab, "take_incl_active_$a")
+OPTAB_D (take_excl_active_optab, "take_excl_active_$a")
+OPTAB_D (cond_take_incl_active_optab, "cond_take_incl_active_$a")
+OPTAB_D (cond_take_excl_active_optab, "cond_take_excl_active_$a")
+OPTAB_D (cond_len_take_incl_active_optab, "cond_len_take_incl_active_$a")
+OPTAB_D (cond_len_take_excl_active_optab, "cond_len_take_excl_active_$a")
 
 OPTAB_D (uabd_optab, "uabd$a3")
 OPTAB_D (sabd_optab, "sabd$a3")


-- 
diff --git a/gcc/doc/ifn.texi b/gcc/doc/ifn.texi
index 4340d01d10854ffcbeed905a885b471eb7ba3e43..ff926922f8bd448a4cdebc15f512b021a5acac3d 100644
--- a/gcc/doc/ifn.texi
+++ b/gcc/doc/ifn.texi
@@ -457,6 +457,190 @@ The @code{IFN_COND_NOT} internal function directly maps to the
 The @code{IFN_COND_LEN_NOT} internal function directly maps to the
 @code{cond_len_one_cmpl} instruction pattern.
 
+@cindex @code{IFN_COUNT_ACTIVE} internal function
+@item @samp{IFN_COUNT_ACTIVE}
+The @code{IFN_COUNT_ACTIVE} internal function directly maps to the
+@code{count_active} instruction pattern.  It returns the number of active
+elements in its argument.
+For an early-exit loop with a live induction variable, the vectorizer can
+count the lanes before the first exit lane as follows, when the target does
+not support @code{IFN_INDEX_FIRST_ACTIVE}.  In this simplified GIMPLE CFG,
+@code{vector_start_2} counts scalar iterations before the current vector
+iteration:
+@smallexample
+<bb 4>:
+  exit_mask_5 = data_3 > needle_1;
+  if (exit_mask_5 != @{ 0, ... @})
+    goto <bb 6>;
+  else
+    goto <bb 5>;
+
+<bb 5>:
+  goto <bb 4>;
+
+<bb 6>:
+  before_7 = .TAKE_EXCL_ACTIVE (exit_mask_5);
+  first_8 = .COUNT_ACTIVE (before_7);
+  iterations_9 = vector_start_2 + first_8;
+  offset_10 = iterations_9 * step_11;
+  live_iv_12 = init_13 + offset_10;
+  goto <bb 7>;
+
+<bb 7>:
+  return live_iv_12;
+@end smallexample
+
+@cindex @code{IFN_COND_COUNT_ACTIVE} internal function
+@item @samp{IFN_COND_COUNT_ACTIVE}
+The @code{IFN_COND_COUNT_ACTIVE} internal function directly maps to the
+@code{cond_count_active} instruction pattern.  It returns the number of
+active elements in its second argument under the mask in its first
+argument.
+
+@cindex @code{IFN_COND_LEN_COUNT_ACTIVE} internal function
+@item @samp{IFN_COND_LEN_COUNT_ACTIVE}
+The @code{IFN_COND_LEN_COUNT_ACTIVE} internal function directly maps to the
+@code{cond_len_count_active} instruction pattern.  It is like
+@code{IFN_COND_COUNT_ACTIVE}, but also ignores elements whose index is
+greater than or equal to the sum of the length and bias arguments.
+
+@cindex @code{IFN_INDEX_FIRST_ACTIVE} internal function
+@item @samp{IFN_INDEX_FIRST_ACTIVE}
+The @code{IFN_INDEX_FIRST_ACTIVE} internal function directly maps to the
+@code{index_first_active} instruction pattern.  It returns the index of the
+first active element in its argument.  The result is undefined if the
+predicate has no active elements.
+When the target supports this operation, the vectorizer can use it directly
+for the same early-exit induction variable update.  The exit block in the CFG
+above then becomes:
+@smallexample
+<bb 6>:
+  first_8 = .INDEX_FIRST_ACTIVE (exit_mask_5);
+  iterations_9 = vector_start_2 + first_8;
+  offset_10 = iterations_9 * step_11;
+  live_iv_12 = init_13 + offset_10;
+  goto <bb 7>;
+@end smallexample
+The branch from block 4 reaches block 6 only when @code{exit_mask_5} is
+nonzero.  Thus the vectorizer calls @code{IFN_INDEX_FIRST_ACTIVE} here only
+with a predicate that has an active element.  It can also use @code{first_8}
+as an offset to reload a scalar live value from a vector load on this edge.
+
+@cindex @code{IFN_COND_INDEX_FIRST_ACTIVE} internal function
+@item @samp{IFN_COND_INDEX_FIRST_ACTIVE}
+The @code{IFN_COND_INDEX_FIRST_ACTIVE} internal function directly maps to the
+@code{cond_index_first_active} instruction pattern.  It returns the index of
+the first active element in its second argument under the mask in its first
+argument.  The result is undefined if the selected predicate has no active
+elements.
+
+@cindex @code{IFN_COND_LEN_INDEX_FIRST_ACTIVE} internal function
+@item @samp{IFN_COND_LEN_INDEX_FIRST_ACTIVE}
+The @code{IFN_COND_LEN_INDEX_FIRST_ACTIVE} internal function directly maps to
+the @code{cond_len_index_first_active} instruction pattern.  It is like
+@code{IFN_COND_INDEX_FIRST_ACTIVE}, but also ignores elements whose index is
+greater than or equal to the sum of the length and bias arguments.
+
+@cindex @code{IFN_TAKE_INCL_ACTIVE} internal function
+@item @samp{IFN_TAKE_INCL_ACTIVE}
+The @code{IFN_TAKE_INCL_ACTIVE} internal function directly maps to the
+@code{take_incl_active} instruction pattern.  It returns a predicate with
+all lanes up to and including the first active lane of its argument set.
+
+@cindex @code{IFN_COND_TAKE_INCL_ACTIVE} internal function
+@item @samp{IFN_COND_TAKE_INCL_ACTIVE}
+The @code{IFN_COND_TAKE_INCL_ACTIVE} internal function directly maps to the
+@code{cond_take_incl_active} instruction pattern.  It returns a predicate
+that finds the first lane active in both its first and second arguments.
+Under the mask in its first argument, all lanes up to and including that lane
+are set and the remaining lanes are cleared.  Outside the mask, the result
+comes from its third argument.
+
+@cindex @code{IFN_COND_LEN_TAKE_INCL_ACTIVE} internal function
+@item @samp{IFN_COND_LEN_TAKE_INCL_ACTIVE}
+The @code{IFN_COND_LEN_TAKE_INCL_ACTIVE} internal function directly maps to
+the @code{cond_len_take_incl_active} instruction pattern.  It is like
+@code{IFN_COND_TAKE_INCL_ACTIVE}, but also ignores elements whose index is
+greater than or equal to the sum of the length and bias arguments.
+
+@cindex @code{IFN_TAKE_EXCL_ACTIVE} internal function
+@item @samp{IFN_TAKE_EXCL_ACTIVE}
+The @code{IFN_TAKE_EXCL_ACTIVE} internal function directly maps to the
+@code{take_excl_active} instruction pattern.  It returns a predicate with
+all lanes before, but excluding, the first active lane of its argument set.
+
+@cindex @code{IFN_COND_TAKE_EXCL_ACTIVE} internal function
+@item @samp{IFN_COND_TAKE_EXCL_ACTIVE}
+The @code{IFN_COND_TAKE_EXCL_ACTIVE} internal function directly maps to the
+@code{cond_take_excl_active} instruction pattern.  It returns a predicate
+that finds the first lane active in both its first and second arguments.
+Under the mask in its first argument, all lanes before that lane are set and
+the remaining lanes are cleared.  Outside the mask, the result comes from
+its third argument.
+
+@cindex @code{IFN_COND_LEN_TAKE_EXCL_ACTIVE} internal function
+@item @samp{IFN_COND_LEN_TAKE_EXCL_ACTIVE}
+The @code{IFN_COND_LEN_TAKE_EXCL_ACTIVE} internal function directly maps to
+the @code{cond_len_take_excl_active} instruction pattern.  It is like
+@code{IFN_COND_TAKE_EXCL_ACTIVE}, but also ignores elements whose index is
+greater than or equal to the sum of the length and bias arguments.
+
+@cindex @code{IFN_COND_VEC_CMP} internal function
+@item @samp{IFN_COND_VEC_CMP}
+The @code{IFN_COND_VEC_CMP} internal function directly maps to the
+@code{cond_vec_cmp} instruction pattern.  It performs the comparison
+identified by its first argument on its third and fourth arguments, then
+returns the comparison result after applying the predicate mask in its
+second argument.
+For example, the vectorizer can produce this GIMPLE CFG for a masked
+early-exit comparison:
+@smallexample
+<bb 4>:
+  cmp_7 = a_5 > b_6;
+  masked_8 = cmp_7 & loop_mask_4;
+  if (masked_8 != @{ 0, ... @})
+    goto <bb 6>;
+  else
+    goto <bb 5>;
+
+<bb 5>:
+  goto <bb 4>;
+
+<bb 6>:
+  return 1;
+@end smallexample
+The GIMPLE instruction-selection pass can replace the comparison and mask
+in block 4, leaving the branch and its successors in place:
+@smallexample
+<bb 4>:
+  masked_8 = .COND_VEC_CMP (GT_EXPR, loop_mask_4, a_5, b_6);
+  if (masked_8 != @{ 0, ... @})
+    goto <bb 6>;
+  else
+    goto <bb 5>;
+@end smallexample
+Here @code{GT_EXPR} stands for the integer tree-code argument that identifies
+the comparison.
+
+@cindex @code{IFN_COND_LEN_VEC_CMP} internal function
+@item @samp{IFN_COND_LEN_VEC_CMP}
+The @code{IFN_COND_LEN_VEC_CMP} internal function directly maps to the
+@code{cond_len_vec_cmp} instruction pattern.  It is like
+@code{IFN_COND_VEC_CMP}, but also ignores elements whose index is greater
+than or equal to the sum of the length and bias arguments.
+For a length-controlled loop, the corresponding CFG has two more arguments
+on the call.  The active length controls which lanes can trigger the early
+exit:
+@smallexample
+<bb 4>:
+  masked_8 = .COND_LEN_VEC_CMP (GT_EXPR, loop_mask_4, a_5, b_6,
+                                 length_7, 0);
+  if (masked_8 != @{ 0, ... @})
+    goto <bb 6>;
+  else
+    goto <bb 5>;
+@end smallexample
+
 @cindex @code{IFN_RSQRT} internal function
 @item @samp{IFN_RSQRT}
 The @code{IFN_RSQRT} internal function directly maps to the @code{rsqrt}
@@ -512,6 +696,31 @@ The @code{IFN_REDUC_SBOOL_XOR} internal function directly maps to the
 The @code{IFN_EXTRACT_LAST_ACTIVE} internal function directly maps to the
 @code{extract_last_active} instruction pattern.
 
+@cindex @code{IFN_EXTRACT_FIRST_ACTIVE} internal function
+@item @samp{IFN_EXTRACT_FIRST_ACTIVE}
+The @code{IFN_EXTRACT_FIRST_ACTIVE} internal function directly maps to the
+@code{extract_first_active} instruction pattern.
+For an early-exit loop with a live vector value, the vectorizer can extract
+the value on the exit edge, before its out-of-loop use:
+@smallexample
+<bb 4>:
+  exit_mask_5 = vector_4 > threshold_3;
+  if (exit_mask_5 != @{ 0, ... @})
+    goto <bb 6>;
+  else
+    goto <bb 5>;
+
+<bb 5>:
+  goto <bb 4>;
+
+<bb 6>:
+  value_9 = .EXTRACT_FIRST_ACTIVE (exit_mask_5, vector_4);
+  goto <bb 7>;
+
+<bb 7>:
+  return value_9;
+@end smallexample
+
 @cindex @code{IFN_FOLD_EXTRACT_LAST} internal function
 @item @samp{IFN_FOLD_EXTRACT_LAST}
 The @code{IFN_FOLD_EXTRACT_LAST} internal function directly maps to the
@@ -1559,4 +1768,3 @@ definition is still being built by the current pass.  The pass that creates
 it must replace it before finishing; the verifier rejects any remaining calls.
 
 @end table
-
diff --git a/gcc/doc/md.texi b/gcc/doc/md.texi
index d72f29e5a71a755e1d647e175152039859b0f2c2..596d9dc23c8af368cea9c485c8b00aa12f00fede 100644
--- a/gcc/doc/md.texi
+++ b/gcc/doc/md.texi
@@ -7921,6 +7921,34 @@ has vector mode @var{m} while operand 0 has the mode appropriate for one
 element of @var{m}.  Operand 1 has the usual mask mode for vectors of mode
 @var{m}; see @code{TARGET_VECTORIZE_GET_MASK_MODE}.
 
+@mdindex extract_first_active_@var{m}
+@item @code{extract_first_active_@var{m}}
+Find the first set bit in mask operand 1 and extract the associated element
+of vector operand 2.  Store the result in scalar operand 0.  Operand 2
+has vector mode @var{m} while operand 0 has the mode appropriate for one
+element of @var{m}.  Operand 1 has the usual mask mode for vectors of mode
+@var{m}; see @code{TARGET_VECTORIZE_GET_MASK_MODE}.
+
+@mdindex count_active_@var{m}
+@item @code{count_active_@var{m}}
+Count the set bits in mask operand 1 and store the result in scalar
+operand 0.
+
+@mdindex index_first_active_@var{m}
+@item @code{index_first_active_@var{m}}
+Find the first set bit in mask operand 1 and store the zero-based element
+index in scalar operand 0.  The result is undefined if no bits are set.
+
+@mdindex take_incl_active_@var{m}
+@item @code{take_incl_active_@var{m}}
+Set every element of mask operand 0 up to and including the first active
+element of mask operand 1, and clear the remaining elements.
+
+@mdindex take_excl_active_@var{m}
+@item @code{take_excl_active_@var{m}}
+Set every element of mask operand 0 before the first active element of mask
+operand 1, and clear the remaining elements.
+
 @mdindex fold_extract_last_@var{m}
 @item @code{fold_extract_last_@var{m}}
 If any bits of mask operand 2 are set, find the last set bit, extract
@@ -8691,6 +8719,76 @@ for (i = 0; i < GET_MODE_NUNITS (@var{m}); i++)
             : op5[i]);
 @end smallexample
 
+@mdindex cond_count_active_@var{mode}
+@item @samp{cond_count_active_@var{mode}}
+Set operand 0 to the number of active elements in operand 2 under the mask
+in operand 1.  Operand 0 has a scalar integer mode.  Operands 1 and 2 have
+mode @var{mode}.
+
+@mdindex cond_len_count_active_@var{mode}
+@item @samp{cond_len_count_active_@var{mode}}
+Like @samp{cond_count_active_@var{mode}}, but additionally ignore elements
+whose index is greater than or equal to the sum of operands 3 and 4.
+Operand 3 has whichever integer mode the target prefers, and operand 4 is
+a bias.
+
+@mdindex cond_index_first_active_@var{mode}
+@item @samp{cond_index_first_active_@var{mode}}
+Set operand 0 to the index of the first active element in operand 2 under
+the mask in operand 1.  Operand 0 has a scalar integer mode.  Operands 1
+and 2 have mode @var{mode}.  The result is undefined if the selected
+predicate has no
+active elements.
+
+@mdindex cond_len_index_first_active_@var{mode}
+@item @samp{cond_len_index_first_active_@var{mode}}
+Like @samp{cond_index_first_active_@var{mode}}, but additionally ignore
+elements whose index is greater than or equal to the sum of operands 3
+and 4.  Operand 3 has whichever integer mode the target prefers, and
+operand 4 is a bias.
+
+@mdindex cond_take_incl_active_@var{mode}
+@item @samp{cond_take_incl_active_@var{mode}}
+Find the first element that is active in both operands 1 and 2.  Under the
+mask in operand 1, set operand 0's elements up to and including that element
+and clear the rest.  Copy operand 3 to operand 0 outside the mask.
+Operands 0, 1, 2 and 3 have mode @var{mode}.
+
+@mdindex cond_len_take_incl_active_@var{mode}
+@item @samp{cond_len_take_incl_active_@var{mode}}
+Like @samp{cond_take_incl_active_@var{mode}}, but additionally ignore
+elements whose index is greater than or equal to the sum of operands 4
+and 5.  Operand 4 has whichever integer mode the target prefers, and
+operand 5 is a bias.
+
+@mdindex cond_take_excl_active_@var{mode}
+@item @samp{cond_take_excl_active_@var{mode}}
+Find the first element that is active in both operands 1 and 2.  Under the
+mask in operand 1, set operand 0's elements before that element and clear
+the rest.  Copy operand 3 to operand 0 outside the mask.  Operands 0, 1, 2
+and 3 have mode @var{mode}.
+
+@mdindex cond_len_take_excl_active_@var{mode}
+@item @samp{cond_len_take_excl_active_@var{mode}}
+Like @samp{cond_take_excl_active_@var{mode}}, but additionally ignore
+elements whose index is greater than or equal to the sum of operands 4
+and 5.  Operand 4 has whichever integer mode the target prefers, and
+operand 5 is a bias.
+
+@mdindex cond_vec_cmp@var{m}@var{n}
+@item @samp{cond_vec_cmp@var{m}@var{n}}
+Set operand 0 to the result of applying comparison operand 1 to operands
+3 and 4, after applying predicate mask operand 2.  Operand 0 has mode
+@var{n}; operands 3 and 4 have mode @var{m}.  Operand 2 has the mode
+returned by @code{TARGET_VECTORIZE_GET_MASK_MODE} for @var{m}.
+
+@mdindex cond_len_vec_cmp@var{m}@var{n}
+@item @samp{cond_len_vec_cmp@var{m}@var{n}}
+Like @samp{cond_vec_cmp@var{m}@var{n}}, but additionally ignore elements
+whose index is greater than or equal to the sum of operands 5 and 6.
+Operand 5 has whichever integer mode the target prefers, and operand 6 is
+a bias.
+
 @mdindex cbranch@var{mode}4
 @item @samp{cbranch@var{mode}4}
 Conditional branch instruction combined with a compare instruction.
diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc
index 45ad08d2cf6120b700fe63233157744e52cca454..7d2f887e8c72aedad28161da75d9a580dfc05609 100644
--- a/gcc/internal-fn.cc
+++ b/gcc/internal-fn.cc
@@ -186,6 +186,8 @@ init_internal_fns ()
 #define cond_len_unary_direct { 1, 1, true }
 #define cond_len_binary_direct { 1, 1, true }
 #define cond_len_ternary_direct { 1, 1, true }
+#define cond_mask_unary_direct { 0, 0, true }
+#define cond_len_mask_unary_direct { 0, 0, true }
 #define while_direct { 0, 2, false }
 #define fold_extract_direct { 2, 2, false }
 #define fold_len_extract_direct { 2, 2, false }
@@ -4305,6 +4307,12 @@ expand_reduc_sbool_optab_fn (internal_fn fn, gcall *stmt, direct_optab optab)
 #define expand_cond_len_ternary_optab_fn(FN, STMT, OPTAB) \
   expand_direct_optab_fn (FN, STMT, OPTAB, 7)
 
+#define expand_cond_mask_unary_optab_fn(FN, STMT, OPTAB) \
+  expand_direct_optab_fn (FN, STMT, OPTAB, 2)
+
+#define expand_cond_len_mask_unary_optab_fn(FN, STMT, OPTAB) \
+  expand_direct_optab_fn (FN, STMT, OPTAB, 4)
+
 #define expand_fold_extract_optab_fn(FN, STMT, OPTAB) \
   expand_direct_optab_fn (FN, STMT, OPTAB, 3)
 
@@ -4415,6 +4423,8 @@ multi_vector_optab_supported_p (convert_optab optab, tree_pair types,
 #define direct_cond_len_unary_optab_supported_p direct_optab_supported_p
 #define direct_cond_len_binary_optab_supported_p direct_optab_supported_p
 #define direct_cond_len_ternary_optab_supported_p direct_optab_supported_p
+#define direct_cond_mask_unary_optab_supported_p direct_optab_supported_p
+#define direct_cond_len_mask_unary_optab_supported_p direct_optab_supported_p
 #define direct_crc_optab_supported_p convert_optab_supported_p
 #define direct_mask_load_optab_supported_p convert_optab_supported_p
 #define direct_load_lanes_optab_supported_p multi_vector_optab_supported_p
@@ -4903,6 +4913,8 @@ get_conditional_len_internal_fn (tree_code code)
 /* Invoke T(IFN) for each internal function IFN that also has an
    IFN_COND_* form.  */
 #define FOR_EACH_COND_FN_PAIR(T) \
+  T (TAKE_INCL_ACTIVE) \
+  T (TAKE_EXCL_ACTIVE) \
   T (FMAX) \
   T (FMIN) \
   T (FMA) \
@@ -4969,6 +4981,14 @@ get_len_internal_fn (internal_fn fn)
       return IFN_MASK_LEN_LOAD_LANES;
     case IFN_MASK_GATHER_LOAD:
       return IFN_MASK_LEN_GATHER_LOAD;
+    case IFN_COND_COUNT_ACTIVE:
+      return IFN_COND_LEN_COUNT_ACTIVE;
+    case IFN_COND_INDEX_FIRST_ACTIVE:
+      return IFN_COND_LEN_INDEX_FIRST_ACTIVE;
+    case IFN_COND_TAKE_INCL_ACTIVE:
+      return IFN_COND_LEN_TAKE_INCL_ACTIVE;
+    case IFN_COND_TAKE_EXCL_ACTIVE:
+      return IFN_COND_LEN_TAKE_EXCL_ACTIVE;
     default:
       return IFN_LAST;
     }
@@ -5188,6 +5208,17 @@ internal_fn_len_index (internal_fn fn)
     case IFN_MASK_LEN_STRIDED_STORE:
       return 4;
 
+    case IFN_COND_LEN_COUNT_ACTIVE:
+    case IFN_COND_LEN_INDEX_FIRST_ACTIVE:
+      return 2;
+
+    case IFN_COND_LEN_TAKE_INCL_ACTIVE:
+    case IFN_COND_LEN_TAKE_EXCL_ACTIVE:
+      return 3;
+
+    case IFN_COND_LEN_VEC_CMP:
+      return 4;
+
     case IFN_COND_LEN_NEG:
     case IFN_MASK_LEN_STORE:
     case IFN_MASK_LEN_STORE_LANES:
@@ -5225,6 +5256,10 @@ internal_fn_else_index (internal_fn fn)
     case IFN_COND_LEN_FLOOR:
     case IFN_COND_LEN_ROUND:
     case IFN_COND_LEN_RINT:
+    case IFN_COND_TAKE_INCL_ACTIVE:
+    case IFN_COND_TAKE_EXCL_ACTIVE:
+    case IFN_COND_LEN_TAKE_INCL_ACTIVE:
+    case IFN_COND_LEN_TAKE_EXCL_ACTIVE:
       return 2;
 
     case IFN_LEN_LOAD:
@@ -5320,6 +5355,13 @@ internal_fn_mask_index (internal_fn fn)
     case IFN_VCOND_MASK_LEN:
       return 0;
 
+    /* These operations use a mask but have no fallback operand.  */
+    case IFN_COND_COUNT_ACTIVE:
+    case IFN_COND_LEN_COUNT_ACTIVE:
+    case IFN_COND_INDEX_FIRST_ACTIVE:
+    case IFN_COND_LEN_INDEX_FIRST_ACTIVE:
+      return 0;
+
     default:
       return (conditional_internal_fn_code (fn) != ERROR_MARK
 	      || get_unconditional_internal_fn (fn) != IFN_LAST ? 0 : -1);
diff --git a/gcc/internal-fn.def b/gcc/internal-fn.def
index 7feb236d8026d0e3f542f2ee5573ce0784147a9a..daf716f1ba143012e7036020dc9e5d126916df30 100644
--- a/gcc/internal-fn.def
+++ b/gcc/internal-fn.def
@@ -89,6 +89,10 @@ along with GCC; see the file COPYING3.  If not see
    - cond_len_binary: a conditional binary optab, such as cond_len_add<mode>
    - cond_len_ternary: a conditional ternary optab, such as
    cond_len_fma_rev<mode>
+   - cond_mask_unary: a unary optab that takes a mask and a predicate operand,
+     such as cond_count_active<mode>
+   - cond_len_mask_unary: a unary optab that takes a mask, a predicate
+     operand, a length and a bias, such as cond_len_count_active<mode>
 
    DEF_INTERNAL_SIGNED_OPTAB_FN defines an internal function that
    maps to one of two optabs, depending on the signedness of an input.
@@ -337,6 +341,28 @@ DEF_INTERNAL_COND_FN (FNMS, ECF_CONST, fnms, ternary)
 DEF_INTERNAL_COND_FN (NEG, ECF_CONST, neg, unary)
 DEF_INTERNAL_COND_FN (NOT, ECF_CONST, one_cmpl, unary)
 
+DEF_INTERNAL_OPTAB_FN (COUNT_ACTIVE, ECF_CONST, count_active, unary)
+DEF_INTERNAL_OPTAB_FN (COND_COUNT_ACTIVE, ECF_CONST, cond_count_active,
+		       cond_mask_unary)
+DEF_INTERNAL_OPTAB_FN (COND_LEN_COUNT_ACTIVE, ECF_CONST,
+		       cond_len_count_active, cond_len_mask_unary)
+DEF_INTERNAL_OPTAB_FN (INDEX_FIRST_ACTIVE, ECF_CONST, index_first_active,
+		       unary)
+DEF_INTERNAL_OPTAB_FN (COND_INDEX_FIRST_ACTIVE, ECF_CONST,
+		       cond_index_first_active, cond_mask_unary)
+DEF_INTERNAL_OPTAB_FN (COND_LEN_INDEX_FIRST_ACTIVE, ECF_CONST,
+		       cond_len_index_first_active, cond_len_mask_unary)
+DEF_INTERNAL_OPTAB_FN (TAKE_INCL_ACTIVE, ECF_CONST, take_incl_active, unary)
+DEF_INTERNAL_OPTAB_FN (COND_TAKE_INCL_ACTIVE, ECF_CONST,
+		       cond_take_incl_active, cond_unary)
+DEF_INTERNAL_OPTAB_FN (COND_LEN_TAKE_INCL_ACTIVE, ECF_CONST,
+		       cond_len_take_incl_active, cond_len_unary)
+DEF_INTERNAL_OPTAB_FN (TAKE_EXCL_ACTIVE, ECF_CONST, take_excl_active, unary)
+DEF_INTERNAL_OPTAB_FN (COND_TAKE_EXCL_ACTIVE, ECF_CONST,
+		       cond_take_excl_active, cond_unary)
+DEF_INTERNAL_OPTAB_FN (COND_LEN_TAKE_EXCL_ACTIVE, ECF_CONST,
+		       cond_len_take_excl_active, cond_len_unary)
+
 DEF_INTERNAL_OPTAB_FN (RSQRT, ECF_CONST, rsqrt, unary)
 
 DEF_INTERNAL_OPTAB_FN (REDUC_PLUS, ECF_CONST | ECF_NOTHROW,
@@ -363,9 +389,11 @@ DEF_INTERNAL_OPTAB_FN (REDUC_SBOOL_XOR, ECF_CONST | ECF_NOTHROW,
 		       reduc_sbool_xor_scal, reduc_sbool)
 
 
-/* Extract the last active element from a vector.  */
+/* Extract the first or last active element from a vector.  */
 DEF_INTERNAL_OPTAB_FN (EXTRACT_LAST_ACTIVE, ECF_CONST | ECF_NOTHROW,
 		       extract_last_active, fold_left)
+DEF_INTERNAL_OPTAB_FN (EXTRACT_FIRST_ACTIVE, ECF_CONST | ECF_NOTHROW,
+		       extract_first_active, fold_left)
 
 /* Same, but return the first argument if no elements are active.  */
 DEF_INTERNAL_OPTAB_FN (FOLD_EXTRACT_LAST, ECF_CONST | ECF_NOTHROW,
diff --git a/gcc/optabs.def b/gcc/optabs.def
index 4ee434ea1a7c4b8474d2691e53fcf9bf925c1efa..79751e28d9531620c01bb85f7bdb4a01c7e138a3 100644
--- a/gcc/optabs.def
+++ b/gcc/optabs.def
@@ -430,8 +430,21 @@ OPTAB_D (mask_fold_left_plus_optab, "mask_fold_left_plus_$a")
 OPTAB_D (mask_len_fold_left_plus_optab, "mask_len_fold_left_plus_$a")
 
 OPTAB_D (extract_last_active_optab, "extract_last_active_$a")
+OPTAB_D (extract_first_active_optab, "extract_first_active_$a")
+OPTAB_D (count_active_optab, "count_active_$a")
+OPTAB_D (cond_count_active_optab, "cond_count_active_$a")
+OPTAB_D (cond_len_count_active_optab, "cond_len_count_active_$a")
+OPTAB_D (index_first_active_optab, "index_first_active_$a")
+OPTAB_D (cond_index_first_active_optab, "cond_index_first_active_$a")
+OPTAB_D (cond_len_index_first_active_optab, "cond_len_index_first_active_$a")
 OPTAB_D (fold_extract_last_optab, "fold_extract_last_$a")
 OPTAB_D (len_fold_extract_last_optab, "len_fold_extract_last_$a")
+OPTAB_D (take_incl_active_optab, "take_incl_active_$a")
+OPTAB_D (take_excl_active_optab, "take_excl_active_$a")
+OPTAB_D (cond_take_incl_active_optab, "cond_take_incl_active_$a")
+OPTAB_D (cond_take_excl_active_optab, "cond_take_excl_active_$a")
+OPTAB_D (cond_len_take_incl_active_optab, "cond_len_take_incl_active_$a")
+OPTAB_D (cond_len_take_excl_active_optab, "cond_len_take_excl_active_$a")
 
 OPTAB_D (uabd_optab, "uabd$a3")
 OPTAB_D (sabd_optab, "sabd$a3")

Reply via email to