LCOV - code coverage report
Current view: top level - gcc - tree-vect-patterns.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 89.1 % 3222 2872
Test Date: 2026-12-05 16:10:02 Functions: 97.7 % 87 85
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Analysis Utilities for Loop Vectorization.
       2              :    Copyright (C) 2006-2026 Free Software Foundation, Inc.
       3              :    Contributed by Dorit Nuzman <dorit@il.ibm.com>
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify it under
       8              : the terms of the GNU General Public License as published by the Free
       9              : Software Foundation; either version 3, or (at your option) any later
      10              : version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      13              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      14              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      15              : for more details.
      16              : 
      17              : You should have received a copy of the GNU General Public License
      18              : along with GCC; see the file COPYING3.  If not see
      19              : <http://www.gnu.org/licenses/>.  */
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "backend.h"
      25              : #include "rtl.h"
      26              : #include "tree.h"
      27              : #include "gimple.h"
      28              : #include "gimple-iterator.h"
      29              : #include "gimple-fold.h"
      30              : #include "ssa.h"
      31              : #include "expmed.h"
      32              : #include "optabs-tree.h"
      33              : #include "insn-config.h"
      34              : #include "recog.h"            /* FIXME: for insn_data */
      35              : #include "fold-const.h"
      36              : #include "stor-layout.h"
      37              : #include "tree-eh.h"
      38              : #include "gimplify.h"
      39              : #include "gimplify-me.h"
      40              : #include "cfgloop.h"
      41              : #include "tree-vectorizer.h"
      42              : #include "dumpfile.h"
      43              : #include "builtins.h"
      44              : #include "internal-fn.h"
      45              : #include "case-cfn-macros.h"
      46              : #include "fold-const-call.h"
      47              : #include "attribs.h"
      48              : #include "cgraph.h"
      49              : #include "omp-simd-clone.h"
      50              : #include "predict.h"
      51              : #include "tree-vector-builder.h"
      52              : #include "tree-ssa-loop-ivopts.h"
      53              : #include "vec-perm-indices.h"
      54              : #include "gimple-range.h"
      55              : #include "alias.h"
      56              : 
      57              : 
      58              : /* TODO:  Note the vectorizer still builds COND_EXPRs with GENERIC compares
      59              :    in the first operand.  Disentangling this is future work, the
      60              :    IL is properly transferred to VEC_COND_EXPRs with separate compares.  */
      61              : 
      62              : 
      63              : /* Return true if we have a useful VR_RANGE range for VAR, storing it
      64              :    in *MIN_VALUE and *MAX_VALUE if so.  Note the range in the dump files.  */
      65              : 
      66              : bool
      67     13326051 : vect_get_range_info (tree var, wide_int *min_value, wide_int *max_value)
      68              : {
      69     13326051 :   int_range_max vr;
      70     13326051 :   tree vr_min, vr_max;
      71     26652102 :   get_range_query (cfun)->range_of_expr (vr, var);
      72     13326051 :   if (vr.undefined_p ())
      73           53 :     vr.set_varying (TREE_TYPE (var));
      74     13326051 :   value_range_kind vr_type = get_legacy_range (vr, vr_min, vr_max);
      75     13326051 :   *min_value = wi::to_wide (vr_min);
      76     13326051 :   *max_value = wi::to_wide (vr_max);
      77     13326051 :   wide_int nonzero = get_nonzero_bits (var);
      78     13326051 :   signop sgn = TYPE_SIGN (TREE_TYPE (var));
      79     13326051 :   if (intersect_range_with_nonzero_bits (vr_type, min_value, max_value,
      80              :                                          nonzero, sgn) == VR_RANGE)
      81              :     {
      82      6430447 :       if (dump_enabled_p ())
      83              :         {
      84        97855 :           dump_generic_expr_loc (MSG_NOTE, vect_location, TDF_SLIM, var);
      85        97855 :           dump_printf (MSG_NOTE, " has range [");
      86        97855 :           dump_hex (MSG_NOTE, *min_value);
      87        97855 :           dump_printf (MSG_NOTE, ", ");
      88        97855 :           dump_hex (MSG_NOTE, *max_value);
      89        97855 :           dump_printf (MSG_NOTE, "]\n");
      90              :         }
      91              :       return true;
      92              :     }
      93              :   else
      94              :     {
      95      6895604 :       if (dump_enabled_p ())
      96              :         {
      97        72337 :           dump_generic_expr_loc (MSG_NOTE, vect_location, TDF_SLIM, var);
      98        72337 :           dump_printf (MSG_NOTE, " has no range info\n");
      99              :         }
     100              :       return false;
     101              :     }
     102     13326051 : }
     103              : 
     104              : /* Report that we've found an instance of pattern PATTERN in
     105              :    statement STMT.  */
     106              : 
     107              : static void
     108      1404872 : vect_pattern_detected (const char *name, gimple *stmt)
     109              : {
     110      1404872 :   if (dump_enabled_p ())
     111        28702 :     dump_printf_loc (MSG_NOTE, vect_location, "%s: detected: %G", name, stmt);
     112      1404872 : }
     113              : 
     114              : /* Associate pattern statement PATTERN_STMT with ORIG_STMT_INFO and
     115              :    return the pattern statement's stmt_vec_info.  Set its vector type to
     116              :    VECTYPE if it doesn't have one already.  */
     117              : 
     118              : static stmt_vec_info
     119      2438592 : vect_init_pattern_stmt (vec_info *vinfo, gimple *pattern_stmt,
     120              :                         stmt_vec_info orig_stmt_info, tree vectype)
     121              : {
     122      2438592 :   stmt_vec_info pattern_stmt_info = vinfo->lookup_stmt (pattern_stmt);
     123      2438592 :   if (pattern_stmt_info == NULL)
     124      1279563 :     pattern_stmt_info = vinfo->add_stmt (pattern_stmt);
     125      2438592 :   gimple_set_bb (pattern_stmt, gimple_bb (orig_stmt_info->stmt));
     126              : 
     127      2438592 :   pattern_stmt_info->pattern_stmt_p = true;
     128      2438592 :   STMT_VINFO_RELATED_STMT (pattern_stmt_info) = orig_stmt_info;
     129      2438592 :   STMT_VINFO_DEF_TYPE (pattern_stmt_info)
     130      2438592 :     = STMT_VINFO_DEF_TYPE (orig_stmt_info);
     131      2438592 :   if (!STMT_VINFO_VECTYPE (pattern_stmt_info))
     132              :     {
     133      2097520 :       gcc_assert (!vectype
     134              :                   || is_a <gcond *> (pattern_stmt)
     135              :                   || (VECTOR_BOOLEAN_TYPE_P (vectype)
     136              :                       == vect_use_mask_type_p (orig_stmt_info)));
     137      1290592 :       STMT_VINFO_VECTYPE (pattern_stmt_info) = vectype;
     138      1290592 :       pattern_stmt_info->mask_precision = orig_stmt_info->mask_precision;
     139      1290592 :       if (dump_enabled_p ())
     140              :         {
     141        28172 :           if (pattern_stmt_info->mask_precision == ~0U)
     142           63 :             dump_printf_loc (MSG_NOTE, vect_location,
     143              :                              "using normal nonmask vectors for %G",
     144              :                              pattern_stmt_info->stmt);
     145              :           else
     146        28109 :             dump_printf_loc (MSG_NOTE, vect_location,
     147              :                              "using boolean precision %d for %G",
     148              :                              pattern_stmt_info->mask_precision,
     149              :                              pattern_stmt_info->stmt);
     150              :         }
     151              :     }
     152      2438592 :   return pattern_stmt_info;
     153              : }
     154              : 
     155              : /* Set the pattern statement of ORIG_STMT_INFO to PATTERN_STMT.
     156              :    Also set the vector type of PATTERN_STMT to VECTYPE, if it doesn't
     157              :    have one already.  */
     158              : 
     159              : static void
     160      1119468 : vect_set_pattern_stmt (vec_info *vinfo, gimple *pattern_stmt,
     161              :                        stmt_vec_info orig_stmt_info, tree vectype)
     162              : {
     163      1119468 :   STMT_VINFO_IN_PATTERN_P (orig_stmt_info) = true;
     164      1119468 :   STMT_VINFO_RELATED_STMT (orig_stmt_info)
     165            0 :     = vect_init_pattern_stmt (vinfo, pattern_stmt, orig_stmt_info, vectype);
     166            0 : }
     167              : 
     168              : /* Add NEW_STMT to STMT_INFO's pattern definition statements.  If VECTYPE
     169              :    is nonnull, record that NEW_STMT's vector type is VECTYPE, which might
     170              :    be different from the vector type of the final pattern statement.
     171              :    If VECTYPE is a mask type, SCALAR_TYPE_FOR_MASK is the scalar type
     172              :    from which it was derived.  */
     173              : 
     174              : static inline void
     175      1282669 : append_pattern_def_seq (vec_info *vinfo,
     176              :                         stmt_vec_info stmt_info, gimple *new_stmt,
     177              :                         tree vectype = NULL_TREE,
     178              :                         tree scalar_type_for_mask = NULL_TREE)
     179              : {
     180      1927379 :   gcc_assert (!scalar_type_for_mask
     181              :               == (!vectype || !VECTOR_BOOLEAN_TYPE_P (vectype)));
     182      1282669 :   if (vectype)
     183              :     {
     184      1148230 :       stmt_vec_info new_stmt_info = vinfo->add_stmt (new_stmt);
     185      1148230 :       STMT_VINFO_VECTYPE (new_stmt_info) = vectype;
     186      1148230 :       if (scalar_type_for_mask)
     187              :         {
     188       637959 :           new_stmt_info->mask_precision
     189       637959 :             = GET_MODE_BITSIZE (SCALAR_TYPE_MODE (scalar_type_for_mask));
     190       637959 :           if (dump_enabled_p ())
     191              :             {
     192         7225 :               if (new_stmt_info->mask_precision == ~0U)
     193              :                 dump_printf_loc (MSG_NOTE, vect_location,
     194              :                                  "using normal nonmask vectors for %G",
     195              :                                  new_stmt_info->stmt);
     196              :               else
     197         7225 :                 dump_printf_loc (MSG_NOTE, vect_location,
     198              :                                  "using boolean precision %d for %G",
     199              :                                  new_stmt_info->mask_precision,
     200              :                                  new_stmt_info->stmt);
     201              :             }
     202              :         }
     203              :     }
     204      1282669 :   gimple_seq_add_stmt_without_update (&STMT_VINFO_PATTERN_DEF_SEQ (stmt_info),
     205              :                                       new_stmt);
     206      1282669 : }
     207              : 
     208              : 
     209              : /* Add NEW_STMT to VINFO's invariant pattern definition statements.  These
     210              :    statements are not vectorized but are materialized as scalar in the loop
     211              :    preheader.  */
     212              : 
     213              : static inline void
     214         1588 : append_inv_pattern_def_seq (vec_info *vinfo, gimple *new_stmt)
     215              : {
     216         1588 :   gimple_seq_add_stmt_without_update (&vinfo->inv_pattern_def_seq, new_stmt);
     217              : }
     218              : 
     219              : /* The caller wants to perform new operations on vect_external variable
     220              :    VAR, so that the result of the operations would also be vect_external.
     221              :    Return the edge on which the operations can be performed, if one exists.
     222              :    Return null if the operations should instead be treated as part of
     223              :    the pattern that needs them.  */
     224              : 
     225              : static edge
     226         6821 : vect_get_external_def_edge (vec_info *vinfo, tree var)
     227              : {
     228         6821 :   edge e = NULL;
     229         6821 :   if (loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo))
     230              :     {
     231          960 :       e = loop_preheader_edge (loop_vinfo->loop);
     232          960 :       if (!SSA_NAME_IS_DEFAULT_DEF (var))
     233              :         {
     234          757 :           basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (var));
     235          757 :           if (bb == NULL
     236          757 :               || !dominated_by_p (CDI_DOMINATORS, e->dest, bb))
     237              :             e = NULL;
     238              :         }
     239              :     }
     240         6821 :   return e;
     241              : }
     242              : 
     243              : /* Return true if the target supports a vector version of CODE,
     244              :    where CODE is known to map to a direct optab with the given SUBTYPE.
     245              :    ITYPE specifies the type of (some of) the scalar inputs and OTYPE
     246              :    specifies the type of the scalar result.
     247              : 
     248              :    If CODE allows the inputs and outputs to have different type
     249              :    (such as for WIDEN_SUM_EXPR), it is the input mode rather
     250              :    than the output mode that determines the appropriate target pattern.
     251              :    Operand 0 of the target pattern then specifies the mode that the output
     252              :    must have.
     253              : 
     254              :    When returning true, set *VECOTYPE_OUT to the vector version of OTYPE.
     255              :    Also set *VECITYPE_OUT to the vector version of ITYPE if VECITYPE_OUT
     256              :    is nonnull.  */
     257              : 
     258              : static bool
     259           84 : vect_supportable_direct_optab_p (vec_info *vinfo, tree otype, tree_code code,
     260              :                                  tree itype, tree *vecotype_out,
     261              :                                  tree *vecitype_out = NULL,
     262              :                                  enum optab_subtype subtype = optab_default)
     263              : {
     264           84 :   tree vecitype = get_vectype_for_scalar_type (vinfo, itype);
     265           84 :   if (!vecitype)
     266              :     return false;
     267              : 
     268           82 :   tree vecotype = get_vectype_for_scalar_type (vinfo, otype);
     269           82 :   if (!vecotype)
     270              :     return false;
     271              : 
     272           82 :   optab optab = optab_for_tree_code (code, vecitype, subtype);
     273           82 :   if (!optab)
     274              :     return false;
     275              : 
     276           82 :   insn_code icode = optab_handler (optab, TYPE_MODE (vecitype));
     277           82 :   if (icode == CODE_FOR_nothing
     278           82 :       || insn_data[icode].operand[0].mode != TYPE_MODE (vecotype))
     279              :     return false;
     280              : 
     281           31 :   *vecotype_out = vecotype;
     282           31 :   if (vecitype_out)
     283            0 :     *vecitype_out = vecitype;
     284              :   return true;
     285              : }
     286              : 
     287              : /* Return true if the target supports a vector version of CODE,
     288              :    where CODE is known to map to a conversion optab with the given SUBTYPE.
     289              :    ITYPE specifies the type of (some of) the scalar inputs and OTYPE
     290              :    specifies the type of the scalar result.
     291              : 
     292              :    When returning true, set *VECOTYPE_OUT to the vector version of OTYPE.
     293              :    Also set *VECITYPE_OUT to the vector version of ITYPE if VECITYPE_OUT
     294              :    is nonnull.  */
     295              : 
     296              : static bool
     297         6875 : vect_supportable_conv_optab_p (vec_info *vinfo, tree otype, tree_code code,
     298              :                                  tree itype, tree *vecotype_out,
     299              :                                  tree *vecitype_out = NULL,
     300              :                                  enum optab_subtype subtype = optab_default)
     301              : {
     302         6875 :   tree vecitype = get_vectype_for_scalar_type (vinfo, itype);
     303         6875 :   tree vecotype = get_vectype_for_scalar_type (vinfo, otype);
     304         6875 :   if (!vecitype || !vecotype)
     305              :     return false;
     306              : 
     307         6307 :   if (!directly_supported_p (code, vecotype, vecitype, subtype))
     308              :     return false;
     309              : 
     310         1240 :   *vecotype_out = vecotype;
     311         1240 :   if (vecitype_out)
     312         1240 :     *vecitype_out = vecitype;
     313              :   return true;
     314              : }
     315              : 
     316              : /* Round bit precision PRECISION up to a full element.  */
     317              : 
     318              : static unsigned int
     319      3709888 : vect_element_precision (unsigned int precision)
     320              : {
     321       281221 :   precision = 1 << ceil_log2 (precision);
     322      5423960 :   return MAX (precision, BITS_PER_UNIT);
     323              : }
     324              : 
     325              : /* If OP is defined by a statement that's being considered for vectorization,
     326              :    return information about that statement, otherwise return NULL.  */
     327              : 
     328              : static stmt_vec_info
     329       427707 : vect_get_internal_def (vec_info *vinfo, tree op)
     330              : {
     331       427707 :   stmt_vec_info def_stmt_info = vinfo->lookup_def (op);
     332       427707 :   if (def_stmt_info
     333       413655 :       && STMT_VINFO_DEF_TYPE (def_stmt_info) == vect_internal_def)
     334       395263 :     return vect_stmt_to_vectorize (def_stmt_info);
     335              :   return NULL;
     336              : }
     337              : 
     338              : /* Holds information about an input operand after some sign changes
     339              :    and type promotions have been peeled away.  */
     340              : class vect_unpromoted_value {
     341              : public:
     342              :   vect_unpromoted_value ();
     343              : 
     344              :   void set_op (tree, vect_def_type, stmt_vec_info = NULL);
     345              : 
     346              :   /* The value obtained after peeling away zero or more casts.  */
     347              :   tree op;
     348              : 
     349              :   /* The type of OP.  */
     350              :   tree type;
     351              : 
     352              :   /* The definition type of OP.  */
     353              :   vect_def_type dt;
     354              : 
     355              :   /* If OP is the result of peeling at least one cast, and if the cast
     356              :      of OP itself is a vectorizable statement, CASTER identifies that
     357              :      statement, otherwise it is null.  */
     358              :   stmt_vec_info caster;
     359              : };
     360              : 
     361    319898641 : inline vect_unpromoted_value::vect_unpromoted_value ()
     362    319898641 :   : op (NULL_TREE),
     363    319898641 :     type (NULL_TREE),
     364    319898641 :     dt (vect_uninitialized_def),
     365      3417120 :     caster (NULL)
     366              : {
     367              : }
     368              : 
     369              : /* Set the operand to OP_IN, its definition type to DT_IN, and the
     370              :    statement that casts it to CASTER_IN.  */
     371              : 
     372              : inline void
     373     12275910 : vect_unpromoted_value::set_op (tree op_in, vect_def_type dt_in,
     374              :                                stmt_vec_info caster_in)
     375              : {
     376     12275910 :   op = op_in;
     377     12275910 :   type = TREE_TYPE (op);
     378     12275910 :   dt = dt_in;
     379     12275910 :   caster = caster_in;
     380     12275910 : }
     381              : 
     382              : /* If OP is a vectorizable SSA name, strip a sequence of integer conversions
     383              :    to reach some vectorizable inner operand OP', continuing as long as it
     384              :    is possible to convert OP' back to OP using a possible sign change
     385              :    followed by a possible promotion P.  Return this OP', or null if OP is
     386              :    not a vectorizable SSA name.  If there is a promotion P, describe its
     387              :    input in UNPROM, otherwise describe OP' in UNPROM.  If SINGLE_USE_P
     388              :    is nonnull, set *SINGLE_USE_P to false if any of the SSA names involved
     389              :    have more than one user.
     390              : 
     391              :    A successful return means that it is possible to go from OP' to OP
     392              :    via UNPROM.  The cast from OP' to UNPROM is at most a sign change,
     393              :    whereas the cast from UNPROM to OP might be a promotion, a sign
     394              :    change, or a nop.
     395              : 
     396              :    E.g. say we have:
     397              : 
     398              :        signed short *ptr = ...;
     399              :        signed short C = *ptr;
     400              :        unsigned short B = (unsigned short) C;    // sign change
     401              :        signed int A = (signed int) B;            // unsigned promotion
     402              :        ...possible other uses of A...
     403              :        unsigned int OP = (unsigned int) A;       // sign change
     404              : 
     405              :    In this case it's possible to go directly from C to OP using:
     406              : 
     407              :        OP = (unsigned int) (unsigned short) C;
     408              :             +------------+ +--------------+
     409              :                promotion      sign change
     410              : 
     411              :    so OP' would be C.  The input to the promotion is B, so UNPROM
     412              :    would describe B.  */
     413              : 
     414              : static tree
     415      9010660 : vect_look_through_possible_promotion (vec_info *vinfo, tree op,
     416              :                                       vect_unpromoted_value *unprom,
     417              :                                       bool *single_use_p = NULL)
     418              : {
     419      9010660 :   tree op_type = TREE_TYPE (op);
     420      9010660 :   if (!INTEGRAL_TYPE_P (op_type))
     421              :     return NULL_TREE;
     422              : 
     423      8976245 :   tree res = NULL_TREE;
     424      8976245 :   unsigned int orig_precision = TYPE_PRECISION (op_type);
     425      8976245 :   unsigned int min_precision = orig_precision;
     426      8976245 :   stmt_vec_info caster = NULL;
     427     10757855 :   while (TREE_CODE (op) == SSA_NAME && INTEGRAL_TYPE_P (op_type))
     428              :     {
     429              :       /* See whether OP is simple enough to vectorize.  */
     430     10530160 :       stmt_vec_info def_stmt_info;
     431     10530160 :       gimple *def_stmt;
     432     10530160 :       vect_def_type dt;
     433     10530160 :       if (!vect_is_simple_use (op, vinfo, &dt, &def_stmt_info, &def_stmt))
     434              :         break;
     435              : 
     436              :       /* If OP is the input of a demotion, skip over it to see whether
     437              :          OP is itself the result of a promotion.  If so, the combined
     438              :          effect of the promotion and the demotion might fit the required
     439              :          pattern, otherwise neither operation fits.
     440              : 
     441              :          This copes with cases such as the result of an arithmetic
     442              :          operation being truncated before being stored, and where that
     443              :          arithmetic operation has been recognized as an over-widened one.  */
     444     10514316 :       if (TYPE_PRECISION (op_type) <= min_precision)
     445              :         {
     446              :           /* Use OP as the UNPROM described above if we haven't yet
     447              :              found a promotion, or if using the new input preserves the
     448              :              sign of the previous promotion.  */
     449     10379100 :           if (!res
     450      1520054 :               || TYPE_PRECISION (unprom->type) == orig_precision
     451        38859 :               || TYPE_SIGN (unprom->type) == TYPE_SIGN (op_type)
     452     10414577 :               || (TYPE_UNSIGNED (op_type)
     453        24365 :                   && TYPE_PRECISION (op_type) < TYPE_PRECISION (unprom->type)))
     454              :             {
     455     10344082 :               unprom->set_op (op, dt, caster);
     456     10344082 :               min_precision = TYPE_PRECISION (op_type);
     457              :             }
     458              :           /* Stop if we've already seen a promotion and if this
     459              :              conversion does more than change the sign.  */
     460        35018 :           else if (TYPE_PRECISION (op_type)
     461        35018 :                    != TYPE_PRECISION (unprom->type))
     462              :             break;
     463              : 
     464              :           /* The sequence now extends to OP.  */
     465              :           res = op;
     466              :         }
     467              : 
     468              :       /* See whether OP is defined by a cast.  Record it as CASTER if
     469              :          the cast is potentially vectorizable.  */
     470     10514286 :       if (!def_stmt)
     471              :         break;
     472     10301081 :       caster = def_stmt_info;
     473              : 
     474              :       /* Ignore pattern statements, since we don't link uses for them.  */
     475     10301081 :       if (caster
     476     10301081 :           && single_use_p
     477      2065587 :           && !STMT_VINFO_RELATED_STMT (caster)
     478     12235968 :           && !has_single_use (res))
     479      1151787 :         *single_use_p = false;
     480              : 
     481     15258321 :       gassign *assign = dyn_cast <gassign *> (def_stmt);
     482      6509771 :       if (!assign || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
     483              :         break;
     484              : 
     485              :       /* Continue with the input to the cast.  */
     486      1781610 :       op = gimple_assign_rhs1 (assign);
     487      1781610 :       op_type = TREE_TYPE (op);
     488              :     }
     489              :   return res;
     490              : }
     491              : 
     492              : /* OP is an integer operand to an operation that returns TYPE, and we
     493              :    want to treat the operation as a widening one.  So far we can treat
     494              :    it as widening from *COMMON_TYPE.
     495              : 
     496              :    Return true if OP is suitable for such a widening operation,
     497              :    either widening from *COMMON_TYPE or from some supertype of it.
     498              :    Update *COMMON_TYPE to the supertype in the latter case.
     499              : 
     500              :    SHIFT_P is true if OP is a shift amount.  */
     501              : 
     502              : static bool
     503       363652 : vect_joust_widened_integer (tree type, bool shift_p, tree op,
     504              :                             tree *common_type)
     505              : {
     506              :   /* Calculate the minimum precision required by OP, without changing
     507              :      the sign of either operand.  */
     508       363652 :   unsigned int precision;
     509       363652 :   if (shift_p)
     510              :     {
     511        13589 :       if (!wi::leu_p (wi::to_widest (op), TYPE_PRECISION (type) / 2))
     512              :         return false;
     513        10910 :       precision = TREE_INT_CST_LOW (op);
     514              :     }
     515              :   else
     516              :     {
     517       350063 :       precision = wi::min_precision (wi::to_widest (op),
     518       350063 :                                      TYPE_SIGN (*common_type));
     519       350063 :       if (precision * 2 > TYPE_PRECISION (type))
     520              :         return false;
     521              :     }
     522              : 
     523              :   /* If OP requires a wider type, switch to that type.  The checks
     524              :      above ensure that this is still narrower than the result.  */
     525       344700 :   precision = vect_element_precision (precision);
     526       344700 :   if (TYPE_PRECISION (*common_type) < precision)
     527         6894 :     *common_type = build_nonstandard_integer_type
     528         6894 :       (precision, TYPE_UNSIGNED (*common_type));
     529              :   return true;
     530              : }
     531              : 
     532              : /* Return true if the common supertype of NEW_TYPE and *COMMON_TYPE
     533              :    is narrower than type, storing the supertype in *COMMON_TYPE if so.  */
     534              : 
     535              : static bool
     536        53015 : vect_joust_widened_type (tree type, tree new_type, tree *common_type)
     537              : {
     538        53015 :   if (types_compatible_p (*common_type, new_type))
     539              :     return true;
     540              : 
     541              :   /* See if *COMMON_TYPE can hold all values of NEW_TYPE.  */
     542         5213 :   if ((TYPE_PRECISION (new_type) < TYPE_PRECISION (*common_type))
     543         5213 :       && (TYPE_UNSIGNED (new_type) || !TYPE_UNSIGNED (*common_type)))
     544              :     return true;
     545              : 
     546              :   /* See if NEW_TYPE can hold all values of *COMMON_TYPE.  */
     547         4177 :   if (TYPE_PRECISION (*common_type) < TYPE_PRECISION (new_type)
     548         4177 :       && (TYPE_UNSIGNED (*common_type) || !TYPE_UNSIGNED (new_type)))
     549              :     {
     550         1022 :       *common_type = new_type;
     551         1022 :       return true;
     552              :     }
     553              : 
     554              :   /* We have mismatched signs, with the signed type being
     555              :      no wider than the unsigned type.  In this case we need
     556              :      a wider signed type.  */
     557         3155 :   unsigned int precision = MAX (TYPE_PRECISION (*common_type),
     558              :                                 TYPE_PRECISION (new_type));
     559         3155 :   precision *= 2;
     560              : 
     561         3155 :   if (precision * 2 > TYPE_PRECISION (type))
     562              :     return false;
     563              : 
     564           49 :   *common_type = build_nonstandard_integer_type (precision, false);
     565           49 :   return true;
     566              : }
     567              : 
     568              : /* If the range of UNPROM->OP at STMT fits in a narrower element type than
     569              :    UNPROM->TYPE, change UNPROM->TYPE to that type and return true.  */
     570              : 
     571              : static bool
     572       282485 : vect_narrow_unprom_to_range (vect_unpromoted_value *unprom, gimple *stmt)
     573              : {
     574       282485 :   int_range_max r;
     575       564970 :   get_range_query (cfun)->range_of_expr (r, unprom->op, stmt);
     576       282485 :   if (r.undefined_p ())
     577              :     return false;
     578              : 
     579       281221 :   signop sgn = TYPE_SIGN (unprom->type);
     580       281221 :   unsigned int precision
     581       843663 :     = vect_element_precision (MAX (wi::min_precision (r.lower_bound (), sgn),
     582              :                                    wi::min_precision (r.upper_bound (), sgn)));
     583       281221 :   if (precision >= TYPE_PRECISION (unprom->type))
     584              :     return false;
     585              : 
     586        35402 :   unprom->type = build_nonstandard_integer_type (precision, sgn == UNSIGNED);
     587        35402 :   return true;
     588       282485 : }
     589              : 
     590              : /* Check whether STMT_INFO can be viewed as a tree of integer operations
     591              :    in which each node either performs CODE or WIDENED_CODE, and where
     592              :    each leaf operand is narrower than the result of STMT_INFO.  MAX_NOPS
     593              :    specifies the maximum number of leaf operands.  SHIFT_P says whether
     594              :    CODE and WIDENED_CODE are some sort of shift.
     595              : 
     596              :    If STMT_INFO is such a tree, return the number of leaf operands
     597              :    and describe them in UNPROM[0] onwards.  Also set *COMMON_TYPE
     598              :    to a type that (a) is narrower than the result of STMT_INFO and
     599              :    (b) can hold all leaf operand values.
     600              : 
     601              :    If SUBTYPE then allow that the signs of the operands
     602              :    may differ in signs but not in precision.  SUBTYPE is updated to reflect
     603              :    this.
     604              : 
     605              :    Return 0 if STMT_INFO isn't such a tree, or if no such COMMON_TYPE
     606              :    exists.  */
     607              : 
     608              : static unsigned int
     609    134297501 : vect_widened_op_tree (vec_info *vinfo, stmt_vec_info stmt_info, tree_code code,
     610              :                       code_helper widened_code, bool shift_p,
     611              :                       unsigned int max_nops,
     612              :                       vect_unpromoted_value *unprom, tree *common_type,
     613              :                       enum optab_subtype *subtype = NULL)
     614              : {
     615              :   /* Check for an integer operation with the right code.  */
     616    134297501 :   gimple* stmt = stmt_info->stmt;
     617    134297501 :   if (!(is_gimple_assign (stmt) || is_gimple_call (stmt)))
     618              :     return 0;
     619              : 
     620    110612706 :   code_helper rhs_code;
     621    110612706 :   if (is_gimple_assign (stmt))
     622     95159015 :     rhs_code = gimple_assign_rhs_code (stmt);
     623     15453691 :   else if (is_gimple_call (stmt))
     624     15453691 :     rhs_code = gimple_call_combined_fn (stmt);
     625              :   else
     626              :     return 0;
     627              : 
     628    110612706 :   if (rhs_code != code
     629    110612706 :       && rhs_code != widened_code)
     630              :     return 0;
     631              : 
     632      7048051 :   tree lhs = gimple_get_lhs (stmt);
     633      7048051 :   tree type = TREE_TYPE (lhs);
     634      7048051 :   if (!INTEGRAL_TYPE_P (type))
     635              :     return 0;
     636              : 
     637              :   /* Assume that both operands will be leaf operands.  */
     638      6137959 :   max_nops -= 2;
     639              : 
     640              :   /* Check the operands.  */
     641      6137959 :   unsigned int next_op = 0;
     642      7048373 :   for (unsigned int i = 0; i < 2; ++i)
     643              :     {
     644      6653359 :       vect_unpromoted_value *this_unprom = &unprom[next_op];
     645      6653359 :       unsigned int nops = 1;
     646      6653359 :       tree op = gimple_arg (stmt, i);
     647      6653359 :       if (i == 1 && TREE_CODE (op) == INTEGER_CST)
     648              :         {
     649              :           /* We already have a common type from earlier operands.
     650              :              Update it to account for OP.  */
     651       363652 :           this_unprom->set_op (op, vect_constant_def);
     652       363652 :           if (!vect_joust_widened_integer (type, shift_p, op, common_type))
     653              :             return 0;
     654              :         }
     655              :       else
     656              :         {
     657              :           /* Only allow shifts by constants.  */
     658      6289707 :           if (shift_p && i == 1)
     659              :             return 0;
     660              : 
     661      6283557 :           if (rhs_code != code)
     662              :             {
     663              :               /* If rhs_code is widened_code, don't look through further
     664              :                  possible promotions, there is a promotion already embedded
     665              :                  in the WIDEN_*_EXPR.  */
     666         1712 :               if (TREE_CODE (op) != SSA_NAME
     667         1712 :                   || !INTEGRAL_TYPE_P (TREE_TYPE (op)))
     668            0 :                 return 0;
     669              : 
     670         1712 :               stmt_vec_info def_stmt_info;
     671         1712 :               gimple *def_stmt;
     672         1712 :               vect_def_type dt;
     673         1712 :               if (!vect_is_simple_use (op, vinfo, &dt, &def_stmt_info,
     674              :                                        &def_stmt))
     675              :                 return 0;
     676         1712 :               this_unprom->set_op (op, dt, NULL);
     677              :             }
     678      6281845 :           else if (!vect_look_through_possible_promotion (vinfo, op,
     679              :                                                           this_unprom))
     680              :             return 0;
     681              : 
     682      6169908 :           if (TYPE_PRECISION (this_unprom->type) == TYPE_PRECISION (type)
     683      6452393 :               && !(this_unprom->dt == vect_external_def
     684       282485 :                    && vect_narrow_unprom_to_range (this_unprom, stmt)))
     685              :             {
     686              :               /* The operand isn't widened.  If STMT_INFO has the code
     687              :                  for an unwidened operation, recursively check whether
     688              :                  this operand is a node of the tree.  */
     689      5597383 :               if (rhs_code != code
     690      5597383 :                   || max_nops == 0
     691      5597934 :                   || this_unprom->dt != vect_internal_def)
     692              :                 return 0;
     693              : 
     694              :               /* Give back the leaf slot allocated above now that we're
     695              :                  not treating this as a leaf operand.  */
     696          551 :               max_nops += 1;
     697              : 
     698              :               /* Recursively process the definition of the operand.  */
     699          551 :               stmt_vec_info def_stmt_info
     700          551 :                 = vect_get_internal_def (vinfo, this_unprom->op);
     701              : 
     702          551 :               nops = vect_widened_op_tree (vinfo, def_stmt_info, code,
     703              :                                            widened_code, shift_p, max_nops,
     704              :                                            this_unprom, common_type,
     705              :                                            subtype);
     706          551 :               if (nops == 0)
     707              :                 return 0;
     708              : 
     709          367 :               max_nops -= nops;
     710              :             }
     711              :           else
     712              :             {
     713              :               /* Make sure that the operand is narrower than the result.  */
     714       572525 :               if (TYPE_PRECISION (this_unprom->type) * 2
     715       572525 :                   > TYPE_PRECISION (type))
     716              :                 return 0;
     717              : 
     718              :               /* Update COMMON_TYPE for the new operand.  */
     719       568162 :               if (i == 0)
     720       515147 :                 *common_type = this_unprom->type;
     721        53015 :               else if (!vect_joust_widened_type (type, this_unprom->type,
     722              :                                                  common_type))
     723              :                 {
     724         3106 :                   if (subtype)
     725              :                     {
     726              :                       /* See if we can sign extend the smaller type.  */
     727          291 :                       if (TYPE_PRECISION (this_unprom->type)
     728          291 :                           > TYPE_PRECISION (*common_type))
     729           27 :                         *common_type = this_unprom->type;
     730          291 :                       *subtype = optab_vector_mixed_sign;
     731              :                     }
     732              :                   else
     733              :                     return 0;
     734              :                 }
     735              :             }
     736              :         }
     737       910414 :       next_op += nops;
     738              :     }
     739              :   return next_op;
     740              : }
     741              : 
     742              : /* Helper to return a new temporary for pattern of TYPE for STMT.  If STMT
     743              :    is NULL, the caller must set SSA_NAME_DEF_STMT for the returned SSA var. */
     744              : 
     745              : static tree
     746      2042691 : vect_recog_temp_ssa_var (tree type, gimple *stmt = NULL)
     747              : {
     748         2653 :   return make_temp_ssa_name (type, stmt, "patt");
     749              : }
     750              : 
     751              : /* STMT2_INFO describes a type conversion that could be split into STMT1
     752              :    followed by a version of STMT2_INFO that takes NEW_RHS as its first
     753              :    input.  Try to do this using pattern statements, returning true on
     754              :    success.  */
     755              : 
     756              : static bool
     757        25950 : vect_split_statement (vec_info *vinfo, stmt_vec_info stmt2_info, tree new_rhs,
     758              :                       gimple *stmt1, tree vectype)
     759              : {
     760        25950 :   if (is_pattern_stmt_p (stmt2_info))
     761              :     {
     762              :       /* STMT2_INFO is part of a pattern.  Get the statement to which
     763              :          the pattern is attached.  */
     764          493 :       stmt_vec_info orig_stmt2_info = STMT_VINFO_RELATED_STMT (stmt2_info);
     765          493 :       vect_init_pattern_stmt (vinfo, stmt1, orig_stmt2_info, vectype);
     766              : 
     767          493 :       if (dump_enabled_p ())
     768           19 :         dump_printf_loc (MSG_NOTE, vect_location,
     769              :                          "Splitting pattern statement: %G", stmt2_info->stmt);
     770              : 
     771              :       /* Since STMT2_INFO is a pattern statement, we can change it
     772              :          in-situ without worrying about changing the code for the
     773              :          containing block.  */
     774          493 :       gimple_assign_set_rhs1 (stmt2_info->stmt, new_rhs);
     775              : 
     776          493 :       if (dump_enabled_p ())
     777              :         {
     778           19 :           dump_printf_loc (MSG_NOTE, vect_location, "into: %G", stmt1);
     779           19 :           dump_printf_loc (MSG_NOTE, vect_location, "and: %G",
     780              :                            stmt2_info->stmt);
     781              :         }
     782              : 
     783          493 :       gimple_seq *def_seq = &STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt2_info);
     784          493 :       if (STMT_VINFO_RELATED_STMT (orig_stmt2_info) == stmt2_info)
     785              :         /* STMT2_INFO is the actual pattern statement.  Add STMT1
     786              :            to the end of the definition sequence.  */
     787          484 :         gimple_seq_add_stmt_without_update (def_seq, stmt1);
     788              :       else
     789              :         {
     790              :           /* STMT2_INFO belongs to the definition sequence.  Insert STMT1
     791              :              before it.  */
     792            9 :           gimple_stmt_iterator gsi = gsi_for_stmt (stmt2_info->stmt, def_seq);
     793            9 :           gsi_insert_before_without_update (&gsi, stmt1, GSI_SAME_STMT);
     794              :         }
     795              :       return true;
     796              :     }
     797              :   else
     798              :     {
     799              :       /* STMT2_INFO doesn't yet have a pattern.  Try to create a
     800              :          two-statement pattern now.  */
     801        25457 :       gcc_assert (!STMT_VINFO_RELATED_STMT (stmt2_info));
     802        25457 :       tree lhs_type = TREE_TYPE (gimple_get_lhs (stmt2_info->stmt));
     803        25457 :       tree lhs_vectype = get_vectype_for_scalar_type (vinfo, lhs_type);
     804        25457 :       if (!lhs_vectype)
     805              :         return false;
     806              : 
     807        25457 :       if (dump_enabled_p ())
     808         2076 :         dump_printf_loc (MSG_NOTE, vect_location,
     809              :                          "Splitting statement: %G", stmt2_info->stmt);
     810              : 
     811              :       /* Add STMT1 as a singleton pattern definition sequence.  */
     812        25457 :       gimple_seq *def_seq = &STMT_VINFO_PATTERN_DEF_SEQ (stmt2_info);
     813        25457 :       vect_init_pattern_stmt (vinfo, stmt1, stmt2_info, vectype);
     814        25457 :       gimple_seq_add_stmt_without_update (def_seq, stmt1);
     815              : 
     816              :       /* Build the second of the two pattern statements.  */
     817        25457 :       tree new_lhs = vect_recog_temp_ssa_var (lhs_type, NULL);
     818        25457 :       gassign *new_stmt2 = gimple_build_assign (new_lhs, NOP_EXPR, new_rhs);
     819        25457 :       vect_set_pattern_stmt (vinfo, new_stmt2, stmt2_info, lhs_vectype);
     820              : 
     821        25457 :       if (dump_enabled_p ())
     822              :         {
     823         2076 :           dump_printf_loc (MSG_NOTE, vect_location,
     824              :                            "into pattern statements: %G", stmt1);
     825         2076 :           dump_printf_loc (MSG_NOTE, vect_location, "and: %G",
     826              :                            (gimple *) new_stmt2);
     827              :         }
     828              : 
     829              :       return true;
     830              :     }
     831              : }
     832              : 
     833              : /* Look for the following pattern
     834              :         X = x[i]
     835              :         Y = y[i]
     836              :         DIFF = X - Y
     837              :         DAD = ABS_EXPR<DIFF>
     838              : 
     839              :    ABS_STMT should point to a statement of code ABS_EXPR or ABSU_EXPR.
     840              :    HALF_TYPE and UNPROM will be set should the statement be found to
     841              :    be a widened operation.
     842              :    DIFF_STMT will be set to the MINUS_EXPR
     843              :    statement that precedes the ABS_STMT if it is a MINUS_EXPR..
     844              :  */
     845              : static bool
     846     23484224 : vect_recog_absolute_difference (vec_info *vinfo, gassign *abs_stmt,
     847              :                                 tree *half_type,
     848              :                                 vect_unpromoted_value unprom[2],
     849              :                                 gassign **diff_stmt)
     850              : {
     851     23484224 :   if (!abs_stmt)
     852              :     return false;
     853              : 
     854              :   /* FORNOW.  Can continue analyzing the def-use chain when this stmt in a phi
     855              :      inside the loop (in case we are analyzing an outer-loop).  */
     856     23484224 :   enum tree_code code = gimple_assign_rhs_code (abs_stmt);
     857     23484224 :   if (code != ABS_EXPR && code != ABSU_EXPR)
     858              :     return false;
     859              : 
     860        34507 :   tree abs_oprnd = gimple_assign_rhs1 (abs_stmt);
     861        34507 :   tree abs_type = TREE_TYPE (abs_oprnd);
     862        34507 :   if (!abs_oprnd)
     863              :     return false;
     864        27080 :   if (!ANY_INTEGRAL_TYPE_P (abs_type)
     865         7678 :       || TYPE_OVERFLOW_WRAPS (abs_type)
     866        42035 :       || TYPE_UNSIGNED (abs_type))
     867              :     return false;
     868              : 
     869              :   /* Peel off conversions from the ABS input.  This can involve sign
     870              :      changes (e.g. from an unsigned subtraction to a signed ABS input)
     871              :      or signed promotion, but it can't include unsigned promotion.
     872              :      (Note that ABS of an unsigned promotion should have been folded
     873              :      away before now anyway.)  */
     874         7528 :   vect_unpromoted_value unprom_diff;
     875         7528 :   abs_oprnd = vect_look_through_possible_promotion (vinfo, abs_oprnd,
     876              :                                                     &unprom_diff);
     877         7528 :   if (!abs_oprnd)
     878              :     return false;
     879         7277 :   if (TYPE_PRECISION (unprom_diff.type) != TYPE_PRECISION (abs_type)
     880         7277 :       && TYPE_UNSIGNED (unprom_diff.type))
     881              :     return false;
     882              : 
     883              :   /* We then detect if the operand of abs_expr is defined by a minus_expr.  */
     884         7277 :   stmt_vec_info diff_stmt_vinfo = vect_get_internal_def (vinfo, abs_oprnd);
     885         7277 :   if (!diff_stmt_vinfo)
     886              :     return false;
     887              : 
     888         7065 :   gassign *diff = dyn_cast <gassign *> (STMT_VINFO_STMT (diff_stmt_vinfo));
     889         7065 :   if (diff_stmt && diff
     890         5801 :       && gimple_assign_rhs_code (diff) == MINUS_EXPR
     891         8918 :       && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (abs_oprnd)))
     892          251 :     *diff_stmt = diff;
     893              : 
     894              :   /* FORNOW.  Can continue analyzing the def-use chain when this stmt in a phi
     895              :      inside the loop (in case we are analyzing an outer-loop).  */
     896         7065 :   if (vect_widened_op_tree (vinfo, diff_stmt_vinfo,
     897         7065 :                             MINUS_EXPR, IFN_VEC_WIDEN_MINUS,
     898              :                             false, 2, unprom, half_type))
     899         2408 :     return true;
     900              : 
     901              :   return false;
     902              : }
     903              : 
     904              : /* Convert UNPROM to TYPE and return the result, adding new statements
     905              :    to STMT_INFO's pattern definition statements if no better way is
     906              :    available.  VECTYPE is the vector form of TYPE.
     907              : 
     908              :    If SUBTYPE then convert the type based on the subtype.  */
     909              : 
     910              : static tree
     911       508181 : vect_convert_input (vec_info *vinfo, stmt_vec_info stmt_info, tree type,
     912              :                     vect_unpromoted_value *unprom, tree vectype,
     913              :                     enum optab_subtype subtype = optab_default)
     914              : {
     915              :   /* Update the type if the signs differ.  */
     916       508181 :   if (subtype == optab_vector_mixed_sign)
     917              :     {
     918          328 :       gcc_assert (!TYPE_UNSIGNED (type));
     919          328 :       if (TYPE_UNSIGNED (TREE_TYPE (unprom->op)))
     920              :         {
     921          164 :           type = unsigned_type_for (type);
     922          164 :           vectype = unsigned_type_for (vectype);
     923              :         }
     924              :     }
     925              : 
     926              :   /* Check for a no-op conversion.  */
     927       508181 :   if (types_compatible_p (type, TREE_TYPE (unprom->op)))
     928       177810 :     return unprom->op;
     929              : 
     930              :   /* Allow the caller to create constant vect_unpromoted_values.  */
     931       330371 :   if (TREE_CODE (unprom->op) == INTEGER_CST)
     932       206182 :     return wide_int_to_tree (type, wi::to_widest (unprom->op));
     933              : 
     934       124189 :   tree input = unprom->op;
     935       124189 :   if (unprom->caster)
     936              :     {
     937        61599 :       tree lhs = gimple_get_lhs (unprom->caster->stmt);
     938        61599 :       tree lhs_type = TREE_TYPE (lhs);
     939              : 
     940              :       /* If the result of the existing cast is the right width, use it
     941              :          instead of the source of the cast.  */
     942        61599 :       if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
     943              :         input = lhs;
     944              :       /* If the precision we want is between the source and result
     945              :          precisions of the existing cast, try splitting the cast into
     946              :          two and tapping into a mid-way point.  */
     947        59314 :       else if (TYPE_PRECISION (lhs_type) > TYPE_PRECISION (type)
     948        59314 :                && TYPE_PRECISION (type) > TYPE_PRECISION (unprom->type))
     949              :         {
     950              :           /* In order to preserve the semantics of the original cast,
     951              :              give the mid-way point the same signedness as the input value.
     952              : 
     953              :              It would be possible to use a signed type here instead if
     954              :              TYPE is signed and UNPROM->TYPE is unsigned, but that would
     955              :              make the sign of the midtype sensitive to the order in
     956              :              which we process the statements, since the signedness of
     957              :              TYPE is the signedness required by just one of possibly
     958              :              many users.  Also, unsigned promotions are usually as cheap
     959              :              as or cheaper than signed ones, so it's better to keep an
     960              :              unsigned promotion.  */
     961        25950 :           tree midtype = build_nonstandard_integer_type
     962        25950 :             (TYPE_PRECISION (type), TYPE_UNSIGNED (unprom->type));
     963        25950 :           tree vec_midtype = get_vectype_for_scalar_type (vinfo, midtype);
     964        25950 :           if (vec_midtype)
     965              :             {
     966        25950 :               input = vect_recog_temp_ssa_var (midtype, NULL);
     967        25950 :               gassign *new_stmt = gimple_build_assign (input, NOP_EXPR,
     968              :                                                        unprom->op);
     969        25950 :               if (!vect_split_statement (vinfo, unprom->caster, input, new_stmt,
     970              :                                          vec_midtype))
     971            0 :                 append_pattern_def_seq (vinfo, stmt_info,
     972              :                                         new_stmt, vec_midtype);
     973              :             }
     974              :         }
     975              : 
     976              :       /* See if we can reuse an existing result.  */
     977        61599 :       if (types_compatible_p (type, TREE_TYPE (input)))
     978              :         return input;
     979              :     }
     980              : 
     981              :   /* We need a new conversion statement.  */
     982       103295 :   tree new_op = vect_recog_temp_ssa_var (type, NULL);
     983       103295 :   gassign *new_stmt = gimple_build_assign (new_op, NOP_EXPR, input);
     984              : 
     985              :   /* If OP is an external value, see if we can insert the new statement
     986              :      on an incoming edge.  */
     987       103295 :   if (input == unprom->op && unprom->dt == vect_external_def)
     988         6806 :     if (edge e = vect_get_external_def_edge (vinfo, input))
     989              :       {
     990          945 :         basic_block new_bb = gsi_insert_on_edge_immediate (e, new_stmt);
     991          945 :         gcc_assert (!new_bb);
     992              :         return new_op;
     993              :       }
     994              : 
     995              :   /* As a (common) last resort, add the statement to the pattern itself.  */
     996       102350 :   append_pattern_def_seq (vinfo, stmt_info, new_stmt, vectype);
     997       102350 :   return new_op;
     998              : }
     999              : 
    1000              : /* Invoke vect_convert_input for N elements of UNPROM and store the
    1001              :    result in the corresponding elements of RESULT.
    1002              : 
    1003              :    If SUBTYPE then convert the type based on the subtype.  */
    1004              : 
    1005              : static void
    1006       256181 : vect_convert_inputs (vec_info *vinfo, stmt_vec_info stmt_info, unsigned int n,
    1007              :                      tree *result, tree type, vect_unpromoted_value *unprom,
    1008              :                      tree vectype, enum optab_subtype subtype = optab_default)
    1009              : {
    1010       764440 :   for (unsigned int i = 0; i < n; ++i)
    1011              :     {
    1012              :       unsigned int j;
    1013       759751 :       for (j = 0; j < i; ++j)
    1014       252078 :         if (unprom[j].op == unprom[i].op)
    1015              :           break;
    1016              : 
    1017       508259 :       if (j < i)
    1018          586 :         result[i] = result[j];
    1019              :       else
    1020       507673 :         result[i] = vect_convert_input (vinfo, stmt_info,
    1021       507673 :                                         type, &unprom[i], vectype, subtype);
    1022              :     }
    1023       256181 : }
    1024              : 
    1025              : /* The caller has created a (possibly empty) sequence of pattern definition
    1026              :    statements followed by a single statement PATTERN_STMT.  Cast the result
    1027              :    of this final statement to TYPE.  If a new statement is needed, add
    1028              :    PATTERN_STMT to the end of STMT_INFO's pattern definition statements
    1029              :    and return the new statement, otherwise return PATTERN_STMT as-is.
    1030              :    VECITYPE is the vector form of PATTERN_STMT's result type.  */
    1031              : 
    1032              : static gimple *
    1033       282089 : vect_convert_output (vec_info *vinfo, stmt_vec_info stmt_info, tree type,
    1034              :                      gimple *pattern_stmt, tree vecitype)
    1035              : {
    1036       282089 :   tree lhs = gimple_get_lhs (pattern_stmt);
    1037       282089 :   if (!types_compatible_p (type, TREE_TYPE (lhs)))
    1038              :     {
    1039       249813 :       append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vecitype);
    1040       249813 :       tree cast_var = vect_recog_temp_ssa_var (type, NULL);
    1041       249813 :       pattern_stmt = gimple_build_assign (cast_var, NOP_EXPR, lhs);
    1042              :     }
    1043       282089 :   return pattern_stmt;
    1044              : }
    1045              : 
    1046              : /* Return true if STMT_VINFO describes a reduction for which reassociation
    1047              :    is allowed.  If STMT_INFO is part of a group, assume that it's part of
    1048              :    a reduction chain and optimistically assume that all statements
    1049              :    except the last allow reassociation.
    1050              :    Also require it to have code CODE and to be a reduction
    1051              :    in the outermost loop.  When returning true, store the operands in
    1052              :    *OP0_OUT and *OP1_OUT.  */
    1053              : 
    1054              : static bool
    1055    100433547 : vect_reassociating_reduction_p (vec_info *vinfo,
    1056              :                                 stmt_vec_info stmt_info, tree_code code,
    1057              :                                 tree *op0_out, tree *op1_out)
    1058              : {
    1059    100433547 :   loop_vec_info loop_info = dyn_cast <loop_vec_info> (vinfo);
    1060     14212200 :   if (!loop_info)
    1061              :     return false;
    1062              : 
    1063     14212200 :   gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
    1064     15500916 :   if (!assign || gimple_assign_rhs_code (assign) != code)
    1065              :     return false;
    1066              : 
    1067              :   /* We don't allow changing the order of the computation in the inner-loop
    1068              :      when doing outer-loop vectorization.  */
    1069      2786210 :   class loop *loop = LOOP_VINFO_LOOP (loop_info);
    1070      2786210 :   if (loop && nested_in_vect_loop_p (loop, stmt_info))
    1071              :     return false;
    1072              : 
    1073      2730614 :   if (!vect_is_reduction (stmt_info))
    1074              :     return false;
    1075              : 
    1076       177830 :   if (needs_fold_left_reduction_p (TREE_TYPE (gimple_assign_lhs (assign)),
    1077       177830 :                                    code))
    1078              :     return false;
    1079              : 
    1080       163868 :   *op0_out = gimple_assign_rhs1 (assign);
    1081       163868 :   *op1_out = gimple_assign_rhs2 (assign);
    1082       163868 :   if (commutative_tree_code (code) && STMT_VINFO_REDUC_IDX (stmt_info) == 0)
    1083        64446 :     std::swap (*op0_out, *op1_out);
    1084              :   return true;
    1085              : }
    1086              : 
    1087              : /* Return true iff the target has a vector optab implementing the operation
    1088              :    CODE on type VECTYPE with SUBTYPE.  */
    1089              : 
    1090              : static bool
    1091       924498 : target_has_vecop_for_code (tree_code code, tree vectype,
    1092              :                            enum optab_subtype subtype = optab_vector)
    1093              : {
    1094       924498 :   optab voptab = optab_for_tree_code (code, vectype, subtype);
    1095       924498 :   return voptab && can_implement_p (voptab, TYPE_MODE (vectype));
    1096              : }
    1097              : 
    1098              : /* match.pd function to match
    1099              :    (cond (cmp@3 a b) (convert@1 c) (convert@2 d))
    1100              :    with conditions:
    1101              :    1) @1, @2, c, d, a, b are all integral type.
    1102              :    2) There's single_use for both @1 and @2.
    1103              :    3) a, c have same precision.
    1104              :    4) c and @1 have different precision.
    1105              :    5) c, d are the same type or they can differ in sign when convert is
    1106              :    truncation.
    1107              : 
    1108              :    record a and c and d and @3.  */
    1109              : 
    1110              : extern bool gimple_cond_expr_convert_p (tree, tree*, tree (*)(tree));
    1111              : 
    1112              : /* Function vect_recog_cond_expr_convert
    1113              : 
    1114              :    Try to find the following pattern:
    1115              : 
    1116              :    TYPE_AB A,B;
    1117              :    TYPE_CD C,D;
    1118              :    TYPE_E E;
    1119              :    TYPE_E op_true = (TYPE_E) A;
    1120              :    TYPE_E op_false = (TYPE_E) B;
    1121              : 
    1122              :    E = C cmp D ? op_true : op_false;
    1123              : 
    1124              :    where
    1125              :    TYPE_PRECISION (TYPE_E) != TYPE_PRECISION (TYPE_CD);
    1126              :    TYPE_PRECISION (TYPE_AB) == TYPE_PRECISION (TYPE_CD);
    1127              :    single_use of op_true and op_false.
    1128              :    TYPE_AB could differ in sign when (TYPE_E) A is a truncation.
    1129              : 
    1130              :    Input:
    1131              : 
    1132              :    * STMT_VINFO: The stmt from which the pattern search begins.
    1133              :    here it starts with E = c cmp D ? op_true : op_false;
    1134              : 
    1135              :    Output:
    1136              : 
    1137              :    TYPE1 E' = C cmp D ? A : B;
    1138              :    TYPE3 E = (TYPE3) E';
    1139              : 
    1140              :    There may extra nop_convert for A or B to handle different signness.
    1141              : 
    1142              :    * TYPE_OUT: The vector type of the output of this pattern.
    1143              : 
    1144              :    * Return value: A new stmt that will be used to replace the sequence of
    1145              :    stmts that constitute the pattern. In this case it will be:
    1146              :    E = (TYPE3)E';
    1147              :    E' = C cmp D ? A : B; is recorded in pattern definition statements;  */
    1148              : 
    1149              : static gimple *
    1150     33561003 : vect_recog_cond_expr_convert_pattern (vec_info *vinfo,
    1151              :                                       stmt_vec_info stmt_vinfo, tree *type_out)
    1152              : {
    1153     33561003 :   gassign *last_stmt = dyn_cast <gassign *> (stmt_vinfo->stmt);
    1154     23580719 :   tree lhs, match[4], temp, type, new_lhs, op2, op1;
    1155     23580719 :   gimple *cond_stmt;
    1156     23580719 :   gimple *pattern_stmt;
    1157     57141722 :   enum tree_code code = NOP_EXPR;
    1158              : 
    1159     23580719 :   if (!last_stmt)
    1160              :     return NULL;
    1161              : 
    1162     23580719 :   lhs = gimple_assign_lhs (last_stmt);
    1163              : 
    1164              :   /* Find E = C cmp D ? (TYPE3) A ? (TYPE3) B;
    1165              :      TYPE_PRECISION (A) == TYPE_PRECISION (C).  */
    1166     23580719 :   if (!gimple_cond_expr_convert_p (lhs, &match[0], NULL))
    1167              :     return NULL;
    1168              : 
    1169           33 :   if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (lhs)))
    1170           20 :     code = INTEGRAL_TYPE_P (TREE_TYPE (match[1])) ? FLOAT_EXPR : CONVERT_EXPR;
    1171           13 :   else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (match[1])))
    1172            0 :     code = FIX_TRUNC_EXPR;
    1173              : 
    1174           33 :   op1 = match[1];
    1175           33 :   op2 = match[2];
    1176           33 :   type = TREE_TYPE (op1);
    1177              :   /* When op1/op2 is REAL_CST, the conversion must be CONVERT_EXPR from
    1178              :      SCALAR_FLOAT_TYPE_P which is restricted in gimple_cond_expr_convert_p.
    1179              :      Otherwise, the conversion could be FLOAT_EXPR, FIX_TRUNC_EXPR
    1180              :      or CONVERT_EXPR.  */
    1181           33 :   if (TREE_CODE (op1) == REAL_CST)
    1182              :     {
    1183           20 :       op1 = const_unop (CONVERT_EXPR, TREE_TYPE (op2), op1);
    1184           20 :       type = TREE_TYPE (op2);
    1185           20 :       if (op1 == NULL_TREE)
    1186              :         return NULL;
    1187              :     }
    1188           13 :   else if (TREE_CODE (op2) == REAL_CST)
    1189              :     {
    1190            0 :       op2 = const_unop (FLOAT_EXPR, TREE_TYPE (op1), op2);
    1191            0 :       if (op2 == NULL_TREE)
    1192              :         return NULL;
    1193              :     }
    1194           13 :   else if (code == NOP_EXPR)
    1195              :     {
    1196           13 :       if (TYPE_SIGN (type) != TYPE_SIGN (TREE_TYPE (match[2])))
    1197              :         {
    1198            9 :           op2 = vect_recog_temp_ssa_var (type, NULL);
    1199            9 :           gimple* nop_stmt = gimple_build_assign (op2, NOP_EXPR, match[2]);
    1200            9 :           append_pattern_def_seq (vinfo, stmt_vinfo, nop_stmt);
    1201              :         }
    1202              :     }
    1203              : 
    1204           33 :   vect_pattern_detected ("vect_recog_cond_expr_convert_pattern", last_stmt);
    1205              : 
    1206           33 :   temp = vect_recog_temp_ssa_var (type, NULL);
    1207           33 :   cond_stmt = gimple_build_assign (temp, build3 (COND_EXPR, type, match[3],
    1208              :                                                  op1, op2));
    1209           33 :   append_pattern_def_seq (vinfo, stmt_vinfo, cond_stmt);
    1210           33 :   new_lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
    1211           33 :   pattern_stmt = gimple_build_assign (new_lhs, code, temp);
    1212           33 :   *type_out = NULL_TREE;
    1213              : 
    1214           33 :   if (dump_enabled_p ())
    1215           24 :     dump_printf_loc (MSG_NOTE, vect_location,
    1216              :                      "created pattern stmt: %G", pattern_stmt);
    1217              :   return pattern_stmt;
    1218              : }
    1219              : 
    1220              : /* Function vect_recog_dot_prod_pattern
    1221              : 
    1222              :    Try to find the following pattern:
    1223              : 
    1224              :      type1a x_t
    1225              :      type1b y_t;
    1226              :      TYPE1 prod;
    1227              :      TYPE2 sum = init;
    1228              :    loop:
    1229              :      sum_0 = phi <init, sum_1>
    1230              :      S1  x_t = ...
    1231              :      S2  y_t = ...
    1232              :      S3  x_T = (TYPE1) x_t;
    1233              :      S4  y_T = (TYPE1) y_t;
    1234              :      S5  prod = x_T * y_T;
    1235              :      [S6  prod = (TYPE2) prod;  #optional]
    1236              :      S7  sum_1 = prod + sum_0;
    1237              : 
    1238              :    For integer reductions, 'TYPE1' is exactly double the size of 'type1a'
    1239              :    and 'type1b', and its sign must match one of them.  Floating-point
    1240              :    reductions require matching input types at most half the width of TYPE1,
    1241              :    with the inputs converted before multiplication, and TYPE2 the same as TYPE1.
    1242              : 
    1243              :    Input:
    1244              : 
    1245              :    * STMT_VINFO: The stmt from which the pattern search begins.  In the
    1246              :    example, when this function is called with S7, the pattern {S3,S4,S5,S6,S7}
    1247              :    will be detected.
    1248              : 
    1249              :    Output:
    1250              : 
    1251              :    * TYPE_OUT: The type of the output  of this pattern.
    1252              : 
    1253              :    * Return value: A new stmt that will be used to replace the sequence of
    1254              :    stmts that constitute the pattern. In this case it will be:
    1255              :         WIDEN_DOT_PRODUCT <x_t, y_t, sum_0>
    1256              : 
    1257              :    Note: The dot-prod idiom is a widening reduction pattern that is
    1258              :          vectorized without preserving all the intermediate results. It
    1259              :          produces only N/2 (widened) results (by summing up pairs of
    1260              :          intermediate results) rather than all N results.  Therefore, we
    1261              :          cannot allow this pattern when we want to get all the results and in
    1262              :          the correct order (as is the case when this computation is in an
    1263              :          inner-loop nested in an outer-loop that us being vectorized).  */
    1264              : 
    1265              : static gimple *
    1266     33478519 : vect_recog_dot_prod_pattern (vec_info *vinfo,
    1267              :                              stmt_vec_info stmt_vinfo, tree *type_out)
    1268              : {
    1269     33478519 :   tree oprnd0, oprnd1;
    1270     33478519 :   gimple *last_stmt = stmt_vinfo->stmt;
    1271     33478519 :   tree type, half_type;
    1272     33478519 :   gimple *pattern_stmt;
    1273     33478519 :   tree var;
    1274              : 
    1275              :   /* Look for the following pattern
    1276              :           DX = (TYPE1) X;
    1277              :           DY = (TYPE1) Y;
    1278              :           DPROD = DX * DY;
    1279              :           DDPROD = (TYPE2) DPROD;
    1280              :           sum_1 = DDPROD + sum_0;
    1281              :      In which
    1282              :      - DX and DY are at least twice as wide as X and Y (exactly twice for
    1283              :        integers).
    1284              :      - DX, DY, DPROD all have the same type but the sign
    1285              :        between X, Y and DPROD can differ.
    1286              :      - sum is the same size of DPROD or bigger
    1287              :      - sum has been recognized as a reduction variable.
    1288              : 
    1289              :      This is equivalent to:
    1290              :        DPROD = X w* Y;          #widen mult
    1291              :        sum_1 = DPROD w+ sum_0;  #widen summation
    1292              :      or
    1293              :        DPROD = X w* Y;          #widen mult
    1294              :        sum_1 = DPROD + sum_0;   #summation
    1295              :    */
    1296              : 
    1297              :   /* Starting from LAST_STMT, follow the defs of its uses in search
    1298              :      of the above pattern.  */
    1299              : 
    1300     33478519 :   if (!vect_reassociating_reduction_p (vinfo, stmt_vinfo, PLUS_EXPR,
    1301              :                                        &oprnd0, &oprnd1))
    1302              :     return NULL;
    1303              : 
    1304        55296 :   type = TREE_TYPE (gimple_get_lhs (last_stmt));
    1305              : 
    1306              :   /* So far so good.  Since LAST_STMT is a summation reduction, OPRND1 is
    1307              :      the reduction variable defined by a loop-header phi.  OPRND0 is the
    1308              :      value added to it, which must come from a multiplication.  For integer
    1309              :      reductions, first look through a possible promotion of OPRND0.  */
    1310        55296 :   bool float_p = SCALAR_FLOAT_TYPE_P (type);
    1311        55296 :   enum optab_subtype subtype = optab_vector;
    1312        55296 :   vect_unpromoted_value unprom_mult;
    1313        55296 :   if (!float_p)
    1314        38165 :     oprnd0 = vect_look_through_possible_promotion (vinfo, oprnd0, &unprom_mult);
    1315        55296 :   if (!oprnd0 || TREE_CODE (oprnd0) != SSA_NAME)
    1316              :     return NULL;
    1317              : 
    1318        54998 :   stmt_vec_info mult_vinfo = vect_get_internal_def (vinfo, oprnd0);
    1319        54998 :   if (!mult_vinfo)
    1320              :     return NULL;
    1321              : 
    1322              :   /* FORNOW.  We could continue following the def-use chain if this statement
    1323              :      is a phi inside the loop, as when analyzing an outer loop.  */
    1324       162327 :   vect_unpromoted_value unprom0[2];
    1325        54109 :   if (float_p)
    1326              :     {
    1327              :       /* Require multiplication of two directly promoted floating-point
    1328              :          inputs.  A product rounded before promotion is not equivalent.  */
    1329        17119 :       gassign *mult = dyn_cast <gassign *> (mult_vinfo->stmt);
    1330        17349 :       if (!mult || gimple_assign_rhs_code (mult) != MULT_EXPR)
    1331              :         return NULL;
    1332              : 
    1333        17015 :       for (unsigned int i = 0; i < 2; ++i)
    1334              :         {
    1335        16918 :           tree op = gimple_op (mult, i + 1);
    1336        16918 :           stmt_vec_info cast_info
    1337        16918 :             = (TREE_CODE (op) == SSA_NAME
    1338        16918 :                ? vect_get_internal_def (vinfo, op) : NULL);
    1339        16888 :           gassign *cast
    1340        16888 :             = cast_info ? dyn_cast <gassign *> (cast_info->stmt) : NULL;
    1341        33455 :           if (!cast || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (cast)))
    1342        16694 :             return NULL;
    1343              : 
    1344          224 :           op = gimple_assign_rhs1 (cast);
    1345          224 :           tree op_type = TREE_TYPE (op);
    1346          224 :           vect_def_type dt;
    1347          224 :           if (TREE_CODE (op) != SSA_NAME
    1348          224 :               || !SCALAR_FLOAT_TYPE_P (op_type)
    1349          224 :               || TYPE_PRECISION (op_type) * 2 > TYPE_PRECISION (type)
    1350          448 :               || !vect_is_simple_use (op, vinfo, &dt))
    1351              :             return NULL;
    1352          224 :           unprom0[i].set_op (op, dt, cast_info);
    1353              :         }
    1354           97 :       half_type = unprom0[0].type;
    1355           97 :       if (!types_compatible_p (half_type, unprom0[1].type))
    1356              :         return NULL;
    1357              :     }
    1358        36990 :   else if (!vect_widened_op_tree (vinfo, mult_vinfo, MULT_EXPR, WIDEN_MULT_EXPR,
    1359              :                                 false, 2, unprom0, &half_type, &subtype))
    1360              :     return NULL;
    1361              : 
    1362              :   /* If there are two integer widening operations, make sure they agree on
    1363              :      the sign of the extension.  The result of an optab_vector_mixed_sign
    1364              :      operation is signed; otherwise, it has the same sign as the operands.  */
    1365         1472 :   if (!float_p
    1366         1375 :       && TYPE_PRECISION (unprom_mult.type) != TYPE_PRECISION (type)
    1367         2213 :       && (subtype == optab_vector_mixed_sign
    1368          741 :           ? TYPE_UNSIGNED (unprom_mult.type)
    1369          532 :           : TYPE_SIGN (unprom_mult.type) != TYPE_SIGN (half_type)))
    1370              :     return NULL;
    1371              : 
    1372         1391 :   vect_pattern_detected ("vect_recog_dot_prod_pattern", last_stmt);
    1373              : 
    1374              :   /* If the inputs have mixed signs, canonicalize on using the signed
    1375              :      input type for analysis.  This also helps when emulating mixed-sign
    1376              :      operations using signed operations.  */
    1377         1391 :   if (subtype == optab_vector_mixed_sign)
    1378          246 :     half_type = signed_type_for (half_type);
    1379              : 
    1380         1391 :   tree half_vectype;
    1381         1391 :   if (!vect_supportable_conv_optab_p (vinfo, type, DOT_PROD_EXPR, half_type,
    1382              :                                         type_out, &half_vectype, subtype))
    1383              :     {
    1384              :       /* We can emulate a mixed-sign dot-product using a sequence of
    1385              :          signed dot-products; see vect_emulate_mixed_dot_prod for details.  */
    1386          626 :       if (subtype != optab_vector_mixed_sign
    1387          626 :           || !vect_supportable_conv_optab_p (vinfo, signed_type_for (type),
    1388              :                                                DOT_PROD_EXPR, half_type,
    1389              :                                                type_out, &half_vectype,
    1390              :                                                optab_vector))
    1391              :         return NULL;
    1392              : 
    1393           15 :       *type_out = signed_or_unsigned_type_for (TYPE_UNSIGNED (type),
    1394              :                                                *type_out);
    1395              :     }
    1396              : 
    1397              :   /* Get the inputs in the appropriate types.  */
    1398          780 :   tree mult_oprnd[2];
    1399          780 :   vect_convert_inputs (vinfo, stmt_vinfo, 2, mult_oprnd, half_type,
    1400              :                        unprom0, half_vectype, subtype);
    1401              : 
    1402          780 :   var = vect_recog_temp_ssa_var (type, NULL);
    1403          780 :   pattern_stmt = gimple_build_assign (var, DOT_PROD_EXPR,
    1404              :                                       mult_oprnd[0], mult_oprnd[1], oprnd1);
    1405              : 
    1406          780 :   return pattern_stmt;
    1407              : }
    1408              : 
    1409              : 
    1410              : /* Function vect_recog_sad_pattern
    1411              : 
    1412              :    Try to find the following Sum of Absolute Difference (SAD) pattern:
    1413              : 
    1414              :      type x_t, y_t;
    1415              :      signed TYPE1 diff, abs_diff;
    1416              :      TYPE2 sum = init;
    1417              :    loop:
    1418              :      sum_0 = phi <init, sum_1>
    1419              :      S1  x_t = ...
    1420              :      S2  y_t = ...
    1421              :      S3  x_T = (TYPE1) x_t;
    1422              :      S4  y_T = (TYPE1) y_t;
    1423              :      S5  diff = x_T - y_T;
    1424              :      S6  abs_diff = ABS_EXPR <diff>;
    1425              :      [S7  abs_diff = (TYPE2) abs_diff;  #optional]
    1426              :      S8  sum_1 = abs_diff + sum_0;
    1427              : 
    1428              :    where 'TYPE1' is at least double the size of type 'type', and 'TYPE2' is the
    1429              :    same size of 'TYPE1' or bigger. This is a special case of a reduction
    1430              :    computation.
    1431              : 
    1432              :    Input:
    1433              : 
    1434              :    * STMT_VINFO: The stmt from which the pattern search begins.  In the
    1435              :    example, when this function is called with S8, the pattern
    1436              :    {S3,S4,S5,S6,S7,S8} will be detected.
    1437              : 
    1438              :    Output:
    1439              : 
    1440              :    * TYPE_OUT: The type of the output of this pattern.
    1441              : 
    1442              :    * Return value: A new stmt that will be used to replace the sequence of
    1443              :    stmts that constitute the pattern. In this case it will be:
    1444              :         SAD_EXPR <x_t, y_t, sum_0>
    1445              :   */
    1446              : 
    1447              : static gimple *
    1448     33477744 : vect_recog_sad_pattern (vec_info *vinfo,
    1449              :                         stmt_vec_info stmt_vinfo, tree *type_out)
    1450              : {
    1451     33477744 :   gimple *last_stmt = stmt_vinfo->stmt;
    1452     33477744 :   tree half_type;
    1453              : 
    1454              :   /* Look for the following pattern
    1455              :           DX = (TYPE1) X;
    1456              :           DY = (TYPE1) Y;
    1457              :           DDIFF = DX - DY;
    1458              :           DAD = ABS_EXPR <DDIFF>;
    1459              :           DDPROD = (TYPE2) DPROD;
    1460              :           sum_1 = DAD + sum_0;
    1461              :      In which
    1462              :      - DX is at least double the size of X
    1463              :      - DY is at least double the size of Y
    1464              :      - DX, DY, DDIFF, DAD all have the same type
    1465              :      - sum is the same size of DAD or bigger
    1466              :      - sum has been recognized as a reduction variable.
    1467              : 
    1468              :      This is equivalent to:
    1469              :        DDIFF = X w- Y;          #widen sub
    1470              :        DAD = ABS_EXPR <DDIFF>;
    1471              :        sum_1 = DAD w+ sum_0;    #widen summation
    1472              :      or
    1473              :        DDIFF = X w- Y;          #widen sub
    1474              :        DAD = ABS_EXPR <DDIFF>;
    1475              :        sum_1 = DAD + sum_0;     #summation
    1476              :    */
    1477              : 
    1478              :   /* Starting from LAST_STMT, follow the defs of its uses in search
    1479              :      of the above pattern.  */
    1480              : 
    1481     33477744 :   tree plus_oprnd0, plus_oprnd1;
    1482     33477744 :   if (!vect_reassociating_reduction_p (vinfo, stmt_vinfo, PLUS_EXPR,
    1483              :                                        &plus_oprnd0, &plus_oprnd1))
    1484              :     return NULL;
    1485              : 
    1486        54516 :   tree sum_type = TREE_TYPE (gimple_get_lhs (last_stmt));
    1487              : 
    1488              :   /* Any non-truncating sequence of conversions is OK here, since
    1489              :      with a successful match, the result of the ABS(U) is known to fit
    1490              :      within the nonnegative range of the result type.  (It cannot be the
    1491              :      negative of the minimum signed value due to the range of the widening
    1492              :      MINUS_EXPR.)  */
    1493        54516 :   vect_unpromoted_value unprom_abs;
    1494        54516 :   plus_oprnd0 = vect_look_through_possible_promotion (vinfo, plus_oprnd0,
    1495              :                                                       &unprom_abs);
    1496              : 
    1497              :   /* So far so good.  Since last_stmt was detected as a (summation) reduction,
    1498              :      we know that plus_oprnd1 is the reduction variable (defined by a loop-header
    1499              :      phi), and plus_oprnd0 is an ssa-name defined by a stmt in the loop body.
    1500              :      Then check that plus_oprnd0 is defined by an abs_expr.  */
    1501              : 
    1502        54516 :   if (!plus_oprnd0)
    1503              :     return NULL;
    1504              : 
    1505        37135 :   stmt_vec_info abs_stmt_vinfo = vect_get_internal_def (vinfo, plus_oprnd0);
    1506        37135 :   if (!abs_stmt_vinfo)
    1507              :     return NULL;
    1508              : 
    1509              :   /* FORNOW.  Can continue analyzing the def-use chain when this stmt in a phi
    1510              :      inside the loop (in case we are analyzing an outer-loop).  */
    1511        36258 :   gassign *abs_stmt = dyn_cast <gassign *> (abs_stmt_vinfo->stmt);
    1512       108774 :   vect_unpromoted_value unprom[2];
    1513              : 
    1514        36258 :   if (!abs_stmt)
    1515              :     {
    1516          367 :       gcall *abd_stmt = dyn_cast <gcall *> (abs_stmt_vinfo->stmt);
    1517          302 :       if (!abd_stmt
    1518          302 :           || !gimple_call_internal_p (abd_stmt)
    1519            0 :           || gimple_call_num_args (abd_stmt) != 2)
    1520              :         return NULL;
    1521              : 
    1522            0 :       tree abd_oprnd0 = gimple_call_arg (abd_stmt, 0);
    1523            0 :       tree abd_oprnd1 = gimple_call_arg (abd_stmt, 1);
    1524              : 
    1525            0 :       if (gimple_call_internal_fn (abd_stmt) == IFN_ABD
    1526            0 :           || gimple_call_internal_fn (abd_stmt) == IFN_VEC_WIDEN_ABD)
    1527              :         {
    1528            0 :           unprom[0].op = abd_oprnd0;
    1529            0 :           unprom[0].type = TREE_TYPE (abd_oprnd0);
    1530            0 :           unprom[1].op = abd_oprnd1;
    1531            0 :           unprom[1].type = TREE_TYPE (abd_oprnd1);
    1532              :         }
    1533              :       else
    1534              :         return NULL;
    1535              : 
    1536            0 :       half_type = unprom[0].type;
    1537              :     }
    1538        35891 :   else if (!vect_recog_absolute_difference (vinfo, abs_stmt, &half_type,
    1539              :                                             unprom, NULL))
    1540              :     return NULL;
    1541              : 
    1542          854 :   vect_pattern_detected ("vect_recog_sad_pattern", last_stmt);
    1543              : 
    1544          854 :   tree half_vectype;
    1545          854 :   if (!vect_supportable_conv_optab_p (vinfo, sum_type, SAD_EXPR, half_type,
    1546              :                                       type_out, &half_vectype))
    1547              :     return NULL;
    1548              : 
    1549              :   /* Get the inputs to the SAD_EXPR in the appropriate types.  */
    1550          460 :   tree sad_oprnd[2];
    1551          460 :   vect_convert_inputs (vinfo, stmt_vinfo, 2, sad_oprnd, half_type,
    1552              :                        unprom, half_vectype);
    1553              : 
    1554          460 :   tree var = vect_recog_temp_ssa_var (sum_type, NULL);
    1555          460 :   gimple *pattern_stmt = gimple_build_assign (var, SAD_EXPR, sad_oprnd[0],
    1556              :                                               sad_oprnd[1], plus_oprnd1);
    1557              : 
    1558          460 :   return pattern_stmt;
    1559              : }
    1560              : 
    1561              : /* Function vect_recog_abd_pattern
    1562              : 
    1563              :    Try to find the following ABsolute Difference (ABD) or
    1564              :    widening ABD (WIDEN_ABD) pattern:
    1565              : 
    1566              :    TYPE1 x;
    1567              :    TYPE2 y;
    1568              :    TYPE3 x_cast = (TYPE3) x;              // widening or no-op
    1569              :    TYPE3 y_cast = (TYPE3) y;              // widening or no-op
    1570              :    TYPE3 diff = x_cast - y_cast;
    1571              :    TYPE4 diff_cast = (TYPE4) diff;        // widening or no-op
    1572              :    TYPE5 abs = ABS(U)_EXPR <diff_cast>;
    1573              : 
    1574              :    WIDEN_ABD exists to optimize the case where TYPE4 is at least
    1575              :    twice as wide as TYPE3.
    1576              : 
    1577              :    Input:
    1578              : 
    1579              :    * STMT_VINFO: The stmt from which the pattern search begins
    1580              : 
    1581              :    Output:
    1582              : 
    1583              :    * TYPE_OUT: The type of the output of this pattern
    1584              : 
    1585              :    * Return value: A new stmt that will be used to replace the sequence of
    1586              :      stmts that constitute the pattern, principally:
    1587              :         out = IFN_ABD (x, y)
    1588              :         out = IFN_WIDEN_ABD (x, y)
    1589              :  */
    1590              : 
    1591              : static gimple *
    1592     33428411 : vect_recog_abd_pattern (vec_info *vinfo,
    1593              :                         stmt_vec_info stmt_vinfo, tree *type_out)
    1594              : {
    1595     33428411 :   gassign *last_stmt = dyn_cast <gassign *> (STMT_VINFO_STMT (stmt_vinfo));
    1596     23448333 :   if (!last_stmt)
    1597              :     return NULL;
    1598              : 
    1599     23448333 :   tree out_type = TREE_TYPE (gimple_assign_lhs (last_stmt));
    1600              : 
    1601     70344999 :   vect_unpromoted_value unprom[2];
    1602     23448333 :   gassign *diff_stmt = NULL;
    1603     23448333 :   tree abd_in_type;
    1604     23448333 :   if (!vect_recog_absolute_difference (vinfo, last_stmt, &abd_in_type,
    1605              :                                        unprom, &diff_stmt))
    1606              :     {
    1607              :       /* We cannot try further without having a non-widening MINUS.  */
    1608     23446779 :       if (!diff_stmt)
    1609              :         return NULL;
    1610              : 
    1611          251 :       unprom[0].op = gimple_assign_rhs1 (diff_stmt);
    1612          251 :       unprom[1].op = gimple_assign_rhs2 (diff_stmt);
    1613          251 :       abd_in_type = signed_type_for (out_type);
    1614              :     }
    1615              : 
    1616         1805 :   tree abd_out_type = abd_in_type;
    1617              : 
    1618         1805 :   tree vectype_in = get_vectype_for_scalar_type (vinfo, abd_in_type);
    1619         1805 :   if (!vectype_in)
    1620              :     return NULL;
    1621              : 
    1622         1787 :   internal_fn ifn = IFN_ABD;
    1623         1787 :   tree vectype_out = vectype_in;
    1624              : 
    1625         1787 :   if (TYPE_PRECISION (out_type) >= TYPE_PRECISION (abd_in_type) * 2
    1626         1787 :       && stmt_vinfo->min_output_precision >= TYPE_PRECISION (abd_in_type) * 2)
    1627              :     {
    1628         1547 :       tree mid_type
    1629         1547 :         = build_nonstandard_integer_type (TYPE_PRECISION (abd_in_type) * 2,
    1630         1547 :                                           TYPE_UNSIGNED (abd_in_type));
    1631         1547 :       tree mid_vectype = get_vectype_for_scalar_type (vinfo, mid_type);
    1632              : 
    1633         1547 :       code_helper dummy_code;
    1634         1547 :       int dummy_int;
    1635         1547 :       auto_vec<tree> dummy_vec;
    1636         1547 :       if (mid_vectype
    1637         1547 :           && supportable_widening_operation (IFN_VEC_WIDEN_ABD,
    1638              :                                              mid_vectype, vectype_in, false,
    1639              :                                              &dummy_code, &dummy_code,
    1640              :                                              &dummy_int, &dummy_vec))
    1641              :         {
    1642            0 :           ifn = IFN_VEC_WIDEN_ABD;
    1643            0 :           abd_out_type = mid_type;
    1644            0 :           vectype_out = mid_vectype;
    1645              :         }
    1646         1547 :     }
    1647              : 
    1648         1547 :   if (ifn == IFN_ABD
    1649         1787 :       && !direct_internal_fn_supported_p (ifn, vectype_in,
    1650              :                                           OPTIMIZE_FOR_SPEED))
    1651              :     return NULL;
    1652              : 
    1653            0 :   vect_pattern_detected ("vect_recog_abd_pattern", last_stmt);
    1654              : 
    1655            0 :   tree abd_oprnds[2];
    1656            0 :   vect_convert_inputs (vinfo, stmt_vinfo, 2, abd_oprnds,
    1657              :                        abd_in_type, unprom, vectype_in);
    1658              : 
    1659            0 :   *type_out = get_vectype_for_scalar_type (vinfo, out_type);
    1660              : 
    1661            0 :   tree abd_result = vect_recog_temp_ssa_var (abd_out_type, NULL);
    1662            0 :   gcall *abd_stmt = gimple_build_call_internal (ifn, 2,
    1663              :                                                 abd_oprnds[0], abd_oprnds[1]);
    1664            0 :   gimple_call_set_lhs (abd_stmt, abd_result);
    1665            0 :   gimple_set_location (abd_stmt, gimple_location (last_stmt));
    1666              : 
    1667            0 :   gimple *stmt = abd_stmt;
    1668            0 :   if (TYPE_PRECISION (abd_in_type) == TYPE_PRECISION (abd_out_type)
    1669            0 :       && TYPE_PRECISION (abd_out_type) < TYPE_PRECISION (out_type)
    1670            0 :       && !TYPE_UNSIGNED (abd_out_type))
    1671              :     {
    1672            0 :       tree unsign = unsigned_type_for (abd_out_type);
    1673            0 :       stmt = vect_convert_output (vinfo, stmt_vinfo, unsign, stmt, vectype_out);
    1674            0 :       vectype_out = get_vectype_for_scalar_type (vinfo, unsign);
    1675              :     }
    1676              : 
    1677            0 :   return vect_convert_output (vinfo, stmt_vinfo, out_type, stmt, vectype_out);
    1678              : }
    1679              : 
    1680              : /* Recognize an operation that performs ORIG_CODE on widened inputs,
    1681              :    so that it can be treated as though it had the form:
    1682              : 
    1683              :       A_TYPE a;
    1684              :       B_TYPE b;
    1685              :       HALF_TYPE a_cast = (HALF_TYPE) a;  // possible no-op
    1686              :       HALF_TYPE b_cast = (HALF_TYPE) b;  // possible no-op
    1687              :     | RES_TYPE a_extend = (RES_TYPE) a_cast;  // promotion from HALF_TYPE
    1688              :     | RES_TYPE b_extend = (RES_TYPE) b_cast;  // promotion from HALF_TYPE
    1689              :     | RES_TYPE res = a_extend ORIG_CODE b_extend;
    1690              : 
    1691              :    Try to replace the pattern with:
    1692              : 
    1693              :       A_TYPE a;
    1694              :       B_TYPE b;
    1695              :       HALF_TYPE a_cast = (HALF_TYPE) a;  // possible no-op
    1696              :       HALF_TYPE b_cast = (HALF_TYPE) b;  // possible no-op
    1697              :     | EXT_TYPE ext = a_cast WIDE_CODE b_cast;
    1698              :     | RES_TYPE res = (EXT_TYPE) ext;  // possible no-op
    1699              : 
    1700              :    where EXT_TYPE is wider than HALF_TYPE but has the same signedness.
    1701              : 
    1702              :    SHIFT_P is true if ORIG_CODE and WIDE_CODE are shifts.  NAME is the
    1703              :    name of the pattern being matched, for dump purposes.  */
    1704              : 
    1705              : static gimple *
    1706    134209241 : vect_recog_widen_op_pattern (vec_info *vinfo,
    1707              :                              stmt_vec_info last_stmt_info, tree *type_out,
    1708              :                              tree_code orig_code, code_helper wide_code,
    1709              :                              bool shift_p, const char *name)
    1710              : {
    1711    134209241 :   gimple *last_stmt = last_stmt_info->stmt;
    1712              : 
    1713    402627723 :   vect_unpromoted_value unprom[2];
    1714    134209241 :   tree half_type;
    1715    134209241 :   if (!vect_widened_op_tree (vinfo, last_stmt_info, orig_code, orig_code,
    1716              :                              shift_p, 2, unprom, &half_type))
    1717              : 
    1718              :     return NULL;
    1719              : 
    1720              :   /* Pattern detected.  */
    1721       379018 :   vect_pattern_detected (name, last_stmt);
    1722              : 
    1723       379018 :   tree type = TREE_TYPE (gimple_get_lhs (last_stmt));
    1724       379018 :   tree itype = type;
    1725       379018 :   if (TYPE_PRECISION (type) != TYPE_PRECISION (half_type) * 2
    1726       379018 :       || TYPE_UNSIGNED (type) != TYPE_UNSIGNED (half_type))
    1727       261908 :     itype = build_nonstandard_integer_type (TYPE_PRECISION (half_type) * 2,
    1728       261908 :                                             TYPE_UNSIGNED (half_type));
    1729              : 
    1730              :   /* Check target support  */
    1731       379018 :   tree vectype = get_vectype_for_scalar_type (vinfo, half_type);
    1732       379018 :   tree vecitype = get_vectype_for_scalar_type (vinfo, itype);
    1733       379018 :   tree ctype = itype;
    1734       379018 :   tree vecctype = vecitype;
    1735       379018 :   if (orig_code == MINUS_EXPR
    1736         8783 :       && TYPE_UNSIGNED (itype)
    1737       385765 :       && TYPE_PRECISION (type) > TYPE_PRECISION (itype))
    1738              :     {
    1739              :       /* Subtraction is special, even if half_type is unsigned and no matter
    1740              :          whether type is signed or unsigned, if type is wider than itype,
    1741              :          we need to sign-extend from the widening operation result to the
    1742              :          result type.
    1743              :          Consider half_type unsigned char, operand 1 0xfe, operand 2 0xff,
    1744              :          itype unsigned short and type either int or unsigned int.
    1745              :          Widened (unsigned short) 0xfe - (unsigned short) 0xff is
    1746              :          (unsigned short) 0xffff, but for type int we want the result -1
    1747              :          and for type unsigned int 0xffffffff rather than 0xffff.  */
    1748         2887 :       ctype = build_nonstandard_integer_type (TYPE_PRECISION (itype), 0);
    1749         2887 :       vecctype = get_vectype_for_scalar_type (vinfo, ctype);
    1750              :     }
    1751              : 
    1752       379018 :   code_helper dummy_code;
    1753       379018 :   int dummy_int;
    1754       379018 :   auto_vec<tree> dummy_vec;
    1755       379018 :   if (!vectype
    1756       379018 :       || !vecitype
    1757       293917 :       || !vecctype
    1758       672935 :       || !supportable_widening_operation (wide_code, vecitype, vectype, true,
    1759              :                                           &dummy_code, &dummy_code,
    1760              :                                           &dummy_int, &dummy_vec))
    1761              :     return NULL;
    1762              : 
    1763       120061 :   *type_out = get_vectype_for_scalar_type (vinfo, type);
    1764       120061 :   if (!*type_out)
    1765              :     return NULL;
    1766              : 
    1767       120061 :   tree oprnd[2];
    1768       120061 :   vect_convert_inputs (vinfo, last_stmt_info,
    1769              :                        2, oprnd, half_type, unprom, vectype);
    1770              : 
    1771       120061 :   tree var = vect_recog_temp_ssa_var (itype, NULL);
    1772       120061 :   gimple *pattern_stmt = vect_gimple_build (var, wide_code, oprnd[0], oprnd[1]);
    1773              : 
    1774       120061 :   if (vecctype != vecitype)
    1775            0 :     pattern_stmt = vect_convert_output (vinfo, last_stmt_info, ctype,
    1776              :                                         pattern_stmt, vecitype);
    1777              : 
    1778       120061 :   return vect_convert_output (vinfo, last_stmt_info,
    1779       120061 :                               type, pattern_stmt, vecctype);
    1780       379018 : }
    1781              : 
    1782              : /* Try to detect multiplication on widened inputs, converting MULT_EXPR
    1783              :    to WIDEN_MULT_EXPR.  See vect_recog_widen_op_pattern for details.  */
    1784              : 
    1785              : static gimple *
    1786     33507299 : vect_recog_widen_mult_pattern (vec_info *vinfo, stmt_vec_info last_stmt_info,
    1787              :                                tree *type_out)
    1788              : {
    1789     33507299 :   return vect_recog_widen_op_pattern (vinfo, last_stmt_info, type_out,
    1790     33507299 :                                       MULT_EXPR, WIDEN_MULT_EXPR, false,
    1791     33507299 :                                       "vect_recog_widen_mult_pattern");
    1792              : }
    1793              : 
    1794              : /* Try to detect addition on widened inputs, converting PLUS_EXPR
    1795              :    to IFN_VEC_WIDEN_PLUS.  See vect_recog_widen_op_pattern for details.  */
    1796              : 
    1797              : static gimple *
    1798     33611988 : vect_recog_widen_plus_pattern (vec_info *vinfo, stmt_vec_info last_stmt_info,
    1799              :                                tree *type_out)
    1800              : {
    1801     33611988 :   return vect_recog_widen_op_pattern (vinfo, last_stmt_info, type_out,
    1802     33611988 :                                       PLUS_EXPR, IFN_VEC_WIDEN_PLUS,
    1803     33611988 :                                       false, "vect_recog_widen_plus_pattern");
    1804              : }
    1805              : 
    1806              : /* Try to detect subtraction on widened inputs, converting MINUS_EXPR
    1807              :    to IFN_VEC_WIDEN_MINUS.  See vect_recog_widen_op_pattern for details.  */
    1808              : static gimple *
    1809     33611988 : vect_recog_widen_minus_pattern (vec_info *vinfo, stmt_vec_info last_stmt_info,
    1810              :                                tree *type_out)
    1811              : {
    1812     33611988 :   return vect_recog_widen_op_pattern (vinfo, last_stmt_info, type_out,
    1813     33611988 :                                       MINUS_EXPR, IFN_VEC_WIDEN_MINUS,
    1814     33611988 :                                       false, "vect_recog_widen_minus_pattern");
    1815              : }
    1816              : 
    1817              : /* Try to detect abd on widened inputs, converting IFN_ABD
    1818              :    to IFN_VEC_WIDEN_ABD.  */
    1819              : static gimple *
    1820     33611988 : vect_recog_widen_abd_pattern (vec_info *vinfo, stmt_vec_info stmt_vinfo,
    1821              :                               tree *type_out)
    1822              : {
    1823     33611988 :   gassign *last_stmt = dyn_cast <gassign *> (STMT_VINFO_STMT (stmt_vinfo));
    1824     31851441 :   if (!last_stmt || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (last_stmt)))
    1825              :     return NULL;
    1826              : 
    1827      3412677 :   tree last_rhs = gimple_assign_rhs1 (last_stmt);
    1828              : 
    1829      3412677 :   tree in_type = TREE_TYPE (last_rhs);
    1830      3412677 :   tree out_type = TREE_TYPE (gimple_assign_lhs (last_stmt));
    1831      3412677 :   if (!INTEGRAL_TYPE_P (in_type)
    1832      2988761 :       || !INTEGRAL_TYPE_P (out_type)
    1833      2861501 :       || TYPE_PRECISION (in_type) * 2 != TYPE_PRECISION (out_type)
    1834      4105613 :       || !TYPE_UNSIGNED (in_type))
    1835              :     return NULL;
    1836              : 
    1837       251149 :   vect_unpromoted_value unprom;
    1838       251149 :   tree op = vect_look_through_possible_promotion (vinfo, last_rhs, &unprom);
    1839       251149 :   if (!op || TYPE_PRECISION (TREE_TYPE (op)) != TYPE_PRECISION (in_type))
    1840              :     return NULL;
    1841              : 
    1842       249071 :   stmt_vec_info abd_pattern_vinfo = vect_get_internal_def (vinfo, op);
    1843       249071 :   if (!abd_pattern_vinfo)
    1844              :     return NULL;
    1845              : 
    1846       223383 :   gcall *abd_stmt = dyn_cast <gcall *> (STMT_VINFO_STMT (abd_pattern_vinfo));
    1847         9308 :   if (!abd_stmt
    1848         9308 :       || !gimple_call_internal_p (abd_stmt)
    1849          320 :       || gimple_call_internal_fn (abd_stmt) != IFN_ABD)
    1850              :     return NULL;
    1851              : 
    1852            0 :   tree vectype_in = get_vectype_for_scalar_type (vinfo, in_type);
    1853            0 :   tree vectype_out = get_vectype_for_scalar_type (vinfo, out_type);
    1854              : 
    1855            0 :   code_helper dummy_code;
    1856            0 :   int dummy_int;
    1857            0 :   auto_vec<tree> dummy_vec;
    1858            0 :   if (!supportable_widening_operation (IFN_VEC_WIDEN_ABD, vectype_out,
    1859              :                                        vectype_in, false,
    1860              :                                        &dummy_code, &dummy_code,
    1861              :                                        &dummy_int, &dummy_vec))
    1862              :     return NULL;
    1863              : 
    1864            0 :   vect_pattern_detected ("vect_recog_widen_abd_pattern", last_stmt);
    1865              : 
    1866            0 :   *type_out = vectype_out;
    1867              : 
    1868            0 :   tree abd_oprnd0 = gimple_call_arg (abd_stmt, 0);
    1869            0 :   tree abd_oprnd1 = gimple_call_arg (abd_stmt, 1);
    1870            0 :   tree widen_abd_result = vect_recog_temp_ssa_var (out_type, NULL);
    1871            0 :   gcall *widen_abd_stmt = gimple_build_call_internal (IFN_VEC_WIDEN_ABD, 2,
    1872              :                                                       abd_oprnd0, abd_oprnd1);
    1873            0 :   gimple_call_set_lhs (widen_abd_stmt, widen_abd_result);
    1874            0 :   gimple_set_location (widen_abd_stmt, gimple_location (last_stmt));
    1875            0 :   return widen_abd_stmt;
    1876            0 : }
    1877              : 
    1878              : /* Function vect_recog_ctz_ffs_pattern
    1879              : 
    1880              :    Try to find the following pattern:
    1881              : 
    1882              :    TYPE1 A;
    1883              :    TYPE1 B;
    1884              : 
    1885              :    B = __builtin_ctz{,l,ll} (A);
    1886              : 
    1887              :    or
    1888              : 
    1889              :    B = __builtin_ffs{,l,ll} (A);
    1890              : 
    1891              :    Input:
    1892              : 
    1893              :    * STMT_VINFO: The stmt from which the pattern search begins.
    1894              :    here it starts with B = __builtin_* (A);
    1895              : 
    1896              :    Output:
    1897              : 
    1898              :    * TYPE_OUT: The vector type of the output of this pattern.
    1899              : 
    1900              :    * Return value: A new stmt that will be used to replace the sequence of
    1901              :    stmts that constitute the pattern, using clz or popcount builtins.  */
    1902              : 
    1903              : static gimple *
    1904     33477708 : vect_recog_ctz_ffs_pattern (vec_info *vinfo, stmt_vec_info stmt_vinfo,
    1905              :                             tree *type_out)
    1906              : {
    1907     33477708 :   gimple *call_stmt = stmt_vinfo->stmt;
    1908     33477708 :   gimple *pattern_stmt;
    1909     33477708 :   tree rhs_oprnd, rhs_type, lhs_oprnd, lhs_type, vec_type, vec_rhs_type;
    1910     33477708 :   tree new_var;
    1911     33477708 :   internal_fn ifn = IFN_LAST, ifnnew = IFN_LAST;
    1912     33477708 :   bool defined_at_zero = true, defined_at_zero_new = false;
    1913     33477708 :   int val = 0, val_new = 0, val_cmp = 0;
    1914     33477708 :   int prec;
    1915     33477708 :   int sub = 0, add = 0;
    1916     33477708 :   location_t loc;
    1917              : 
    1918     33477708 :   if (!is_gimple_call (call_stmt))
    1919              :     return NULL;
    1920              : 
    1921      3866731 :   if (gimple_call_num_args (call_stmt) != 1
    1922      3866731 :       && gimple_call_num_args (call_stmt) != 2)
    1923              :     return NULL;
    1924              : 
    1925      2188131 :   rhs_oprnd = gimple_call_arg (call_stmt, 0);
    1926      2188131 :   rhs_type = TREE_TYPE (rhs_oprnd);
    1927      2188131 :   lhs_oprnd = gimple_call_lhs (call_stmt);
    1928      2188131 :   if (!lhs_oprnd)
    1929              :     return NULL;
    1930      1076342 :   lhs_type = TREE_TYPE (lhs_oprnd);
    1931      1076342 :   if (!INTEGRAL_TYPE_P (lhs_type)
    1932       359916 :       || !INTEGRAL_TYPE_P (rhs_type)
    1933        57402 :       || !type_has_mode_precision_p (rhs_type)
    1934      1130551 :       || TREE_CODE (rhs_oprnd) != SSA_NAME)
    1935              :     return NULL;
    1936              : 
    1937        39875 :   switch (gimple_call_combined_fn (call_stmt))
    1938              :     {
    1939         2211 :     CASE_CFN_CTZ:
    1940         2211 :       ifn = IFN_CTZ;
    1941         2211 :       if (!gimple_call_internal_p (call_stmt)
    1942         2211 :           || gimple_call_num_args (call_stmt) != 2)
    1943              :         defined_at_zero = false;
    1944              :       else
    1945          228 :         val = tree_to_shwi (gimple_call_arg (call_stmt, 1));
    1946              :       break;
    1947              :     CASE_CFN_FFS:
    1948              :       ifn = IFN_FFS;
    1949              :       break;
    1950              :     default:
    1951              :       return NULL;
    1952              :     }
    1953              : 
    1954         2446 :   prec = TYPE_PRECISION (rhs_type);
    1955         2446 :   loc = gimple_location (call_stmt);
    1956              : 
    1957         2446 :   vec_type = get_vectype_for_scalar_type (vinfo, lhs_type);
    1958         2446 :   if (!vec_type)
    1959              :     return NULL;
    1960              : 
    1961         2440 :   vec_rhs_type = get_vectype_for_scalar_type (vinfo, rhs_type);
    1962         2440 :   if (!vec_rhs_type)
    1963              :     return NULL;
    1964              : 
    1965              :   /* Do it only if the backend doesn't have ctz<vector_mode>2 or
    1966              :      ffs<vector_mode>2 pattern but does have clz<vector_mode>2 or
    1967              :      popcount<vector_mode>2.  */
    1968         2139 :   if (!vec_type
    1969         2139 :       || direct_internal_fn_supported_p (ifn, vec_rhs_type,
    1970              :                                          OPTIMIZE_FOR_SPEED))
    1971              :     return NULL;
    1972              : 
    1973         2139 :   if (ifn == IFN_FFS
    1974         2139 :       && direct_internal_fn_supported_p (IFN_CTZ, vec_rhs_type,
    1975              :                                          OPTIMIZE_FOR_SPEED))
    1976              :     {
    1977            0 :       ifnnew = IFN_CTZ;
    1978            0 :       defined_at_zero_new
    1979            0 :         = CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (vec_rhs_type),
    1980              :                                      val_new) == 2;
    1981              :     }
    1982         2139 :   else if (direct_internal_fn_supported_p (IFN_CLZ, vec_rhs_type,
    1983              :                                            OPTIMIZE_FOR_SPEED))
    1984              :     {
    1985          166 :       ifnnew = IFN_CLZ;
    1986          166 :       defined_at_zero_new
    1987          166 :         = CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (vec_rhs_type),
    1988              :                                      val_new) == 2;
    1989              :     }
    1990          166 :   if ((ifnnew == IFN_LAST
    1991          166 :        || (defined_at_zero && !defined_at_zero_new))
    1992         1973 :       && direct_internal_fn_supported_p (IFN_POPCOUNT, vec_rhs_type,
    1993              :                                          OPTIMIZE_FOR_SPEED))
    1994              :     {
    1995              :       ifnnew = IFN_POPCOUNT;
    1996              :       defined_at_zero_new = true;
    1997              :       val_new = prec;
    1998              :     }
    1999         2013 :   if (ifnnew == IFN_LAST)
    2000              :     return NULL;
    2001              : 
    2002              :   /* Only .FFS (X) = .CTZ (X) + 1 can be part of a reduction, and only
    2003              :      if the result for zero needs no select.  */
    2004          292 :   if (vect_is_reduction (stmt_vinfo)
    2005          292 :       && (ifnnew != IFN_CTZ
    2006            0 :           || (defined_at_zero
    2007            0 :               && (!defined_at_zero_new || val != val_new + 1))))
    2008              :     return NULL;
    2009              : 
    2010          286 :   vect_pattern_detected ("vec_recog_ctz_ffs_pattern", call_stmt);
    2011              : 
    2012              :   /* .CLZ, .CTZ and .POPCOUNT take an unsigned argument.  */
    2013          286 :   if (!TYPE_UNSIGNED (rhs_type))
    2014              :     {
    2015           51 :       rhs_type = unsigned_type_for (rhs_type);
    2016           51 :       vec_rhs_type = get_vectype_for_scalar_type (vinfo, rhs_type);
    2017           51 :       new_var = vect_recog_temp_ssa_var (rhs_type, NULL);
    2018           51 :       pattern_stmt = gimple_build_assign (new_var, NOP_EXPR, rhs_oprnd);
    2019           51 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
    2020           51 :       rhs_oprnd = new_var;
    2021              :     }
    2022              : 
    2023          286 :   val_cmp = val_new;
    2024          286 :   if ((ifnnew == IFN_CLZ
    2025          286 :        && defined_at_zero
    2026          124 :        && defined_at_zero_new
    2027          124 :        && val == prec
    2028           72 :        && val_new == prec)
    2029          214 :       || (ifnnew == IFN_POPCOUNT && ifn == IFN_CTZ))
    2030              :     {
    2031              :       /* .CTZ (X) = PREC - .CLZ ((X - 1) & ~X)
    2032              :          .CTZ (X) = .POPCOUNT ((X - 1) & ~X).  */
    2033              :       if (ifnnew == IFN_CLZ)
    2034              :         sub = prec;
    2035          155 :       val_cmp = prec;
    2036              : 
    2037          155 :       tree m1 = vect_recog_temp_ssa_var (rhs_type, NULL);
    2038          155 :       pattern_stmt = gimple_build_assign (m1, PLUS_EXPR, rhs_oprnd,
    2039              :                                           build_int_cst (rhs_type, -1));
    2040          155 :       gimple_set_location (pattern_stmt, loc);
    2041          155 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
    2042              : 
    2043          155 :       new_var = vect_recog_temp_ssa_var (rhs_type, NULL);
    2044          155 :       pattern_stmt = gimple_build_assign (new_var, BIT_NOT_EXPR, rhs_oprnd);
    2045          155 :       gimple_set_location (pattern_stmt, loc);
    2046          155 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
    2047          155 :       rhs_oprnd = new_var;
    2048              : 
    2049          155 :       new_var = vect_recog_temp_ssa_var (rhs_type, NULL);
    2050          155 :       pattern_stmt = gimple_build_assign (new_var, BIT_AND_EXPR,
    2051              :                                           m1, rhs_oprnd);
    2052          155 :       gimple_set_location (pattern_stmt, loc);
    2053          155 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
    2054          155 :       rhs_oprnd = new_var;
    2055          155 :     }
    2056          131 :   else if (ifnnew == IFN_CLZ)
    2057              :     {
    2058              :       /* .CTZ (X) = (PREC - 1) - .CLZ (X & -X)
    2059              :          .FFS (X) = PREC - .CLZ (X & -X).  */
    2060           88 :       sub = prec - (ifn == IFN_CTZ);
    2061           88 :       val_cmp = sub - val_new;
    2062              : 
    2063           88 :       tree neg = vect_recog_temp_ssa_var (rhs_type, NULL);
    2064           88 :       pattern_stmt = gimple_build_assign (neg, NEGATE_EXPR, rhs_oprnd);
    2065           88 :       gimple_set_location (pattern_stmt, loc);
    2066           88 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
    2067              : 
    2068           88 :       new_var = vect_recog_temp_ssa_var (rhs_type, NULL);
    2069           88 :       pattern_stmt = gimple_build_assign (new_var, BIT_AND_EXPR,
    2070              :                                           rhs_oprnd, neg);
    2071           88 :       gimple_set_location (pattern_stmt, loc);
    2072           88 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
    2073           88 :       rhs_oprnd = new_var;
    2074              :     }
    2075           43 :   else if (ifnnew == IFN_POPCOUNT)
    2076              :     {
    2077              :       /* .CTZ (X) = PREC - .POPCOUNT (X | -X)
    2078              :          .FFS (X) = (PREC + 1) - .POPCOUNT (X | -X).  */
    2079           43 :       sub = prec + (ifn == IFN_FFS);
    2080           43 :       val_cmp = sub;
    2081              : 
    2082           43 :       tree neg = vect_recog_temp_ssa_var (rhs_type, NULL);
    2083           43 :       pattern_stmt = gimple_build_assign (neg, NEGATE_EXPR, rhs_oprnd);
    2084           43 :       gimple_set_location (pattern_stmt, loc);
    2085           43 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
    2086              : 
    2087           43 :       new_var = vect_recog_temp_ssa_var (rhs_type, NULL);
    2088           43 :       pattern_stmt = gimple_build_assign (new_var, BIT_IOR_EXPR,
    2089              :                                           rhs_oprnd, neg);
    2090           43 :       gimple_set_location (pattern_stmt, loc);
    2091           43 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_rhs_type);
    2092           43 :       rhs_oprnd = new_var;
    2093              :     }
    2094            0 :   else if (ifnnew == IFN_CTZ)
    2095              :     {
    2096              :       /* .FFS (X) = .CTZ (X) + 1.  */
    2097            0 :       add = 1;
    2098            0 :       val_cmp++;
    2099              :     }
    2100              : 
    2101              :   /* Create B = .IFNNEW (A).  */
    2102          286 :   new_var = vect_recog_temp_ssa_var (lhs_type, NULL);
    2103          286 :   if ((ifnnew == IFN_CLZ || ifnnew == IFN_CTZ) && defined_at_zero_new)
    2104          160 :     pattern_stmt
    2105          160 :       = gimple_build_call_internal (ifnnew, 2, rhs_oprnd,
    2106              :                                     build_int_cst (integer_type_node,
    2107          160 :                                                    val_new));
    2108              :   else
    2109          126 :     pattern_stmt = gimple_build_call_internal (ifnnew, 1, rhs_oprnd);
    2110          286 :   gimple_call_set_lhs (pattern_stmt, new_var);
    2111          286 :   gimple_set_location (pattern_stmt, loc);
    2112          286 :   *type_out = vec_type;
    2113              : 
    2114          286 :   if (sub)
    2115              :     {
    2116          203 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_type);
    2117          203 :       tree ret_var = vect_recog_temp_ssa_var (lhs_type, NULL);
    2118          203 :       pattern_stmt = gimple_build_assign (ret_var, MINUS_EXPR,
    2119          203 :                                           build_int_cst (lhs_type, sub),
    2120              :                                           new_var);
    2121          203 :       gimple_set_location (pattern_stmt, loc);
    2122          203 :       new_var = ret_var;
    2123              :     }
    2124           83 :   else if (add)
    2125              :     {
    2126            0 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_type);
    2127            0 :       tree ret_var = vect_recog_temp_ssa_var (lhs_type, NULL);
    2128            0 :       pattern_stmt = gimple_build_assign (ret_var, PLUS_EXPR, new_var,
    2129            0 :                                           build_int_cst (lhs_type, add));
    2130            0 :       gimple_set_location (pattern_stmt, loc);
    2131            0 :       new_var = ret_var;
    2132              :     }
    2133              : 
    2134          286 :   if (defined_at_zero
    2135          235 :       && (!defined_at_zero_new || val != val_cmp))
    2136              :     {
    2137           63 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_type);
    2138           63 :       tree ret_var = vect_recog_temp_ssa_var (lhs_type, NULL);
    2139           63 :       rhs_oprnd = gimple_call_arg (call_stmt, 0);
    2140           63 :       rhs_type = TREE_TYPE (rhs_oprnd);
    2141           63 :       tree cmp = vect_recog_temp_ssa_var (boolean_type_node, NULL);
    2142           63 :       pattern_stmt = gimple_build_assign (cmp, NE_EXPR, rhs_oprnd,
    2143              :                                           build_zero_cst (rhs_type));
    2144           63 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt,
    2145              :                               truth_type_for (vec_type), rhs_type);
    2146           63 :       pattern_stmt = gimple_build_assign (ret_var, COND_EXPR, cmp,
    2147              :                                           new_var,
    2148           63 :                                           build_int_cst (lhs_type, val));
    2149              :     }
    2150              : 
    2151          286 :   if (dump_enabled_p ())
    2152           36 :     dump_printf_loc (MSG_NOTE, vect_location,
    2153              :                      "created pattern stmt: %G", pattern_stmt);
    2154              : 
    2155              :   return pattern_stmt;
    2156              : }
    2157              : 
    2158              : /* Function vect_recog_popcount_clz_ctz_ffs_pattern
    2159              : 
    2160              :    Try to find the following pattern:
    2161              : 
    2162              :    UTYPE1 A;
    2163              :    TYPE1 B;
    2164              :    UTYPE2 temp_in;
    2165              :    TYPE3 temp_out;
    2166              :    temp_in = (UTYPE2)A;
    2167              : 
    2168              :    temp_out = __builtin_popcount{,l,ll} (temp_in);
    2169              :    B = (TYPE1) temp_out;
    2170              : 
    2171              :    TYPE2 may or may not be equal to TYPE3.
    2172              :    i.e. TYPE2 is equal to TYPE3 for __builtin_popcount
    2173              :    i.e. TYPE2 is not equal to TYPE3 for __builtin_popcountll
    2174              : 
    2175              :    Input:
    2176              : 
    2177              :    * STMT_VINFO: The stmt from which the pattern search begins.
    2178              :    here it starts with B = (TYPE1) temp_out;
    2179              : 
    2180              :    Output:
    2181              : 
    2182              :    * TYPE_OUT: The vector type of the output of this pattern.
    2183              : 
    2184              :    * Return value: A new stmt that will be used to replace the sequence of
    2185              :    stmts that constitute the pattern. In this case it will be:
    2186              :    B = .POPCOUNT (A);
    2187              : 
    2188              :    Similarly for clz, ctz and ffs.
    2189              : */
    2190              : 
    2191              : static gimple *
    2192     33477275 : vect_recog_popcount_clz_ctz_ffs_pattern (vec_info *vinfo,
    2193              :                                          stmt_vec_info stmt_vinfo,
    2194              :                                          tree *type_out)
    2195              : {
    2196     33477275 :   gassign *last_stmt = dyn_cast <gassign *> (stmt_vinfo->stmt);
    2197     23492422 :   gimple *call_stmt, *pattern_stmt;
    2198     23492422 :   tree rhs_oprnd, rhs_origin, lhs_oprnd, lhs_type, vec_type, new_var;
    2199     56969697 :   internal_fn ifn = IFN_LAST;
    2200     56969697 :   int addend = 0;
    2201              : 
    2202              :   /* Find B = (TYPE1) temp_out. */
    2203     23492422 :   if (!last_stmt)
    2204              :     return NULL;
    2205     23492422 :   tree_code code = gimple_assign_rhs_code (last_stmt);
    2206     23492422 :   if (!CONVERT_EXPR_CODE_P (code))
    2207              :     return NULL;
    2208              : 
    2209      3257725 :   lhs_oprnd = gimple_assign_lhs (last_stmt);
    2210      3257725 :   lhs_type = TREE_TYPE (lhs_oprnd);
    2211      3257725 :   if (!INTEGRAL_TYPE_P (lhs_type))
    2212              :     return NULL;
    2213              : 
    2214      2966363 :   rhs_oprnd = gimple_assign_rhs1 (last_stmt);
    2215      2966363 :   if (TREE_CODE (rhs_oprnd) != SSA_NAME
    2216      2966363 :       || !has_single_use (rhs_oprnd))
    2217              :     return NULL;
    2218      1529665 :   call_stmt = SSA_NAME_DEF_STMT (rhs_oprnd);
    2219              : 
    2220              :   /* Find temp_out = __builtin_popcount{,l,ll} (temp_in);  */
    2221      1529665 :   if (!is_gimple_call (call_stmt))
    2222              :     return NULL;
    2223        60923 :   switch (gimple_call_combined_fn (call_stmt))
    2224              :     {
    2225              :       int val;
    2226              :     CASE_CFN_POPCOUNT:
    2227              :       ifn = IFN_POPCOUNT;
    2228              :       break;
    2229         2137 :     CASE_CFN_CLZ:
    2230         2137 :       ifn = IFN_CLZ;
    2231              :       /* Punt if call result is unsigned and defined value at zero
    2232              :          is negative, as the negative value doesn't extend correctly.  */
    2233         2137 :       if (TYPE_UNSIGNED (TREE_TYPE (rhs_oprnd))
    2234            0 :           && gimple_call_internal_p (call_stmt)
    2235         2137 :           && CLZ_DEFINED_VALUE_AT_ZERO
    2236              :                (SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs_oprnd)), val) == 2
    2237         2137 :           && val < 0)
    2238              :         return NULL;
    2239              :       break;
    2240          787 :     CASE_CFN_CTZ:
    2241          787 :       ifn = IFN_CTZ;
    2242              :       /* Punt if call result is unsigned and defined value at zero
    2243              :          is negative, as the negative value doesn't extend correctly.  */
    2244          787 :       if (TYPE_UNSIGNED (TREE_TYPE (rhs_oprnd))
    2245            0 :           && gimple_call_internal_p (call_stmt)
    2246          787 :           && CTZ_DEFINED_VALUE_AT_ZERO
    2247              :                (SCALAR_INT_TYPE_MODE (TREE_TYPE (rhs_oprnd)), val) == 2
    2248          787 :           && val < 0)
    2249              :         return NULL;
    2250              :       break;
    2251           57 :     CASE_CFN_FFS:
    2252           57 :       ifn = IFN_FFS;
    2253           57 :       break;
    2254              :     default:
    2255              :       return NULL;
    2256              :     }
    2257              : 
    2258         3357 :   if (gimple_call_num_args (call_stmt) != 1
    2259         3357 :       && gimple_call_num_args (call_stmt) != 2)
    2260              :     return NULL;
    2261              : 
    2262         3357 :   rhs_oprnd = gimple_call_arg (call_stmt, 0);
    2263         3357 :   vect_unpromoted_value unprom_diff;
    2264         3357 :   rhs_origin
    2265         3357 :     = vect_look_through_possible_promotion (vinfo, rhs_oprnd, &unprom_diff);
    2266              : 
    2267         3357 :   if (!rhs_origin)
    2268              :     return NULL;
    2269              : 
    2270              :   /* Input and output of .POPCOUNT should be same-precision integer.  */
    2271         3347 :   if (TYPE_PRECISION (unprom_diff.type) != TYPE_PRECISION (lhs_type))
    2272              :     return NULL;
    2273              : 
    2274         1893 :   vec_type = get_vectype_for_scalar_type (vinfo, lhs_type);
    2275         1893 :   if (!vec_type || !VECTOR_MODE_P (TYPE_MODE (vec_type)))
    2276              :     return NULL;
    2277              : 
    2278              :   /* Also A should be unsigned or same precision as temp_in, otherwise
    2279              :      different builtins/internal functions have different behaviors.  */
    2280         1759 :   if (TYPE_PRECISION (unprom_diff.type)
    2281         1759 :       != TYPE_PRECISION (TREE_TYPE (rhs_oprnd)))
    2282          336 :     switch (ifn)
    2283              :       {
    2284          164 :       case IFN_POPCOUNT:
    2285              :         /* For popcount require zero extension, which doesn't add any
    2286              :            further bits to the count.  */
    2287          164 :         if (!TYPE_UNSIGNED (unprom_diff.type))
    2288              :           return NULL;
    2289              :         break;
    2290          109 :       case IFN_CLZ:
    2291              :         /* clzll (x) == clz (x) + 32 for unsigned x != 0.  */
    2292          109 :         if (!TYPE_UNSIGNED (unprom_diff.type))
    2293              :           return NULL;
    2294          109 :         if (!type_has_mode_precision_p (lhs_type)
    2295          109 :             || !type_has_mode_precision_p (TREE_TYPE (rhs_oprnd)))
    2296              :           return NULL;
    2297          109 :         addend = (TYPE_PRECISION (TREE_TYPE (rhs_oprnd))
    2298          109 :                   - TYPE_PRECISION (lhs_type));
    2299          109 :         break;
    2300              :       case IFN_CTZ:
    2301              :         /* ctzll (x) == ctz (x) for unsigned or signed x != 0.  */
    2302              :         break;
    2303              :       case IFN_FFS:
    2304              :         /* ffsll (x) == ffs (x) for unsigned or signed x.  */
    2305              :         break;
    2306              :       default:
    2307              :         gcc_unreachable ();
    2308              :       }
    2309              : 
    2310              :   /* For zero the pattern gives the value of the target for VEC_TYPE plus
    2311              :      ADDEND.  If the call has another value at zero, select it.  */
    2312         1759 :   tree zero_val = NULL_TREE;
    2313         1759 :   if ((ifn == IFN_CLZ || ifn == IFN_CTZ)
    2314         1385 :       && gimple_call_internal_p (call_stmt)
    2315         2003 :       && gimple_call_num_args (call_stmt) == 2)
    2316              :     {
    2317          230 :       int val;
    2318          230 :       int d = (ifn == IFN_CLZ
    2319          230 :                ? CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (vec_type), val)
    2320          311 :                : CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (vec_type), val));
    2321          230 :       if (d != 2
    2322          230 :           || tree_to_shwi (gimple_call_arg (call_stmt, 1)) != val + addend)
    2323          132 :         zero_val = gimple_call_arg (call_stmt, 1);
    2324              :     }
    2325              : 
    2326              :   /* Do it only if the backend has popcount<vector_mode>2 etc. pattern.  */
    2327         1759 :   bool supported
    2328         1759 :     = direct_internal_fn_supported_p (ifn, vec_type, OPTIMIZE_FOR_SPEED);
    2329              :   /* We cannot rely on the reduction check in vect_recog_ctz_ffs_pattern
    2330              :      because we do not set up the temporary pattern stmt finally for
    2331              :      the recursive analysis.  */
    2332         1759 :   if (!supported && vect_is_reduction (stmt_vinfo))
    2333              :     return NULL;
    2334         1743 :   if (!supported)
    2335         1556 :     switch (ifn)
    2336              :       {
    2337              :       case IFN_POPCOUNT:
    2338              :       case IFN_CLZ:
    2339              :         return NULL;
    2340           57 :       case IFN_FFS:
    2341              :         /* vect_recog_ctz_ffs_pattern can implement ffs using ctz.  */
    2342           57 :         if (direct_internal_fn_supported_p (IFN_CTZ, vec_type,
    2343              :                                             OPTIMIZE_FOR_SPEED))
    2344              :           break;
    2345              :         /* FALLTHRU */
    2346          554 :       case IFN_CTZ:
    2347              :         /* vect_recog_ctz_ffs_pattern can implement ffs or ctz using
    2348              :            clz or popcount.  */
    2349          554 :         if (direct_internal_fn_supported_p (IFN_CLZ, vec_type,
    2350              :                                             OPTIMIZE_FOR_SPEED))
    2351              :           break;
    2352          494 :         if (direct_internal_fn_supported_p (IFN_POPCOUNT, vec_type,
    2353              :                                             OPTIMIZE_FOR_SPEED))
    2354              :           break;
    2355              :         return NULL;
    2356              :       default:
    2357              :         gcc_unreachable ();
    2358              :       }
    2359              : 
    2360          328 :   vect_pattern_detected ("vec_recog_popcount_clz_ctz_ffs_pattern",
    2361              :                          call_stmt);
    2362              : 
    2363          328 :   tree orig_lhs_type = lhs_type;
    2364          328 :   if (!type_has_mode_precision_p (lhs_type))
    2365            0 :     lhs_type = TREE_TYPE (vec_type);
    2366          328 :   tree arg_type = (lhs_type != orig_lhs_type
    2367          328 :                    ? lhs_type : TREE_TYPE (unprom_diff.op));
    2368              :   /* .CLZ, .CTZ and .POPCOUNT take an unsigned argument.  */
    2369          328 :   if (ifn != IFN_FFS && !TYPE_UNSIGNED (arg_type))
    2370           40 :     arg_type = unsigned_type_for (arg_type);
    2371          328 :   gassign *cast_stmt = NULL;
    2372          328 :   tree arg_vectype = vec_type;
    2373          328 :   if (arg_type != TREE_TYPE (unprom_diff.op))
    2374              :     {
    2375           40 :       arg_vectype = get_vectype_for_scalar_type (vinfo, arg_type);
    2376           40 :       if (!arg_vectype)
    2377              :         return NULL;
    2378           40 :       cast_stmt
    2379           40 :         = gimple_build_assign (vect_recog_temp_ssa_var (arg_type, NULL),
    2380              :                                NOP_EXPR, unprom_diff.op);
    2381           40 :       unprom_diff.op = gimple_assign_lhs (cast_stmt);
    2382              :     }
    2383              : 
    2384              :   /* Create B = .POPCOUNT (A).  */
    2385          328 :   new_var = vect_recog_temp_ssa_var (lhs_type, NULL);
    2386          328 :   tree arg2 = NULL_TREE;
    2387          328 :   int val;
    2388          328 :   if (ifn == IFN_CLZ
    2389          378 :       && CLZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (vec_type),
    2390              :                                     val) == 2)
    2391           48 :     arg2 = build_int_cst (integer_type_node, val);
    2392          280 :   else if (ifn == IFN_CTZ
    2393          373 :            && CTZ_DEFINED_VALUE_AT_ZERO (TYPE_MODE (vec_type),
    2394              :                                          val) == 2)
    2395           93 :     arg2 = build_int_cst (integer_type_node, val);
    2396              :   /* vect_recog_ctz_ffs_pattern selects the value at zero itself.  */
    2397          328 :   if (zero_val && !supported)
    2398              :     {
    2399              :       arg2 = zero_val;
    2400              :       zero_val = NULL_TREE;
    2401              :     }
    2402          308 :   if (arg2)
    2403          141 :     pattern_stmt = gimple_build_call_internal (ifn, 2, unprom_diff.op, arg2);
    2404              :   else
    2405          187 :     pattern_stmt = gimple_build_call_internal (ifn, 1, unprom_diff.op);
    2406          328 :   gimple_call_set_lhs (pattern_stmt, new_var);
    2407          328 :   gimple_set_location (pattern_stmt, gimple_location (last_stmt));
    2408          328 :   *type_out = vec_type;
    2409              : 
    2410          328 :   if (dump_enabled_p ())
    2411           24 :     dump_printf_loc (MSG_NOTE, vect_location,
    2412              :                      "created pattern stmt: %G", pattern_stmt);
    2413              : 
    2414          328 :   if (cast_stmt && supported)
    2415           28 :     append_pattern_def_seq (vinfo, stmt_vinfo, cast_stmt, arg_vectype);
    2416              : 
    2417          328 :   if (addend)
    2418              :     {
    2419           12 :       gcc_assert (supported);
    2420           12 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_type);
    2421           12 :       tree ret_var = vect_recog_temp_ssa_var (lhs_type, NULL);
    2422           12 :       pattern_stmt = gimple_build_assign (ret_var, PLUS_EXPR, new_var,
    2423           12 :                                           build_int_cst (lhs_type, addend));
    2424              :     }
    2425          316 :   else if (!supported)
    2426              :     {
    2427          141 :       stmt_vec_info new_stmt_info = vinfo->add_stmt (pattern_stmt);
    2428          141 :       STMT_VINFO_VECTYPE (new_stmt_info) = vec_type;
    2429          141 :       pattern_stmt
    2430          141 :         = vect_recog_ctz_ffs_pattern (vinfo, new_stmt_info, type_out);
    2431          141 :       if (pattern_stmt == NULL)
    2432              :         return NULL;
    2433          141 :       if (cast_stmt)
    2434           12 :         append_pattern_def_seq (vinfo, stmt_vinfo, cast_stmt, arg_vectype);
    2435          141 :       if (gimple_seq seq = STMT_VINFO_PATTERN_DEF_SEQ (new_stmt_info))
    2436              :         {
    2437          141 :           gimple_seq *pseq = &STMT_VINFO_PATTERN_DEF_SEQ (stmt_vinfo);
    2438          141 :           gimple_seq_add_seq_without_update (pseq, seq);
    2439              :         }
    2440              :     }
    2441              : 
    2442          328 :   if (zero_val)
    2443              :     {
    2444            0 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_type);
    2445            0 :       tree op = unprom_diff.op;
    2446            0 :       tree cmp = vect_recog_temp_ssa_var (boolean_type_node, NULL);
    2447            0 :       gimple *cmp_stmt = gimple_build_assign (cmp, NE_EXPR, op,
    2448            0 :                                               build_zero_cst (TREE_TYPE (op)));
    2449            0 :       append_pattern_def_seq (vinfo, stmt_vinfo, cmp_stmt,
    2450            0 :                               truth_type_for (vec_type), TREE_TYPE (op));
    2451            0 :       tree ret_var = vect_recog_temp_ssa_var (lhs_type, NULL);
    2452            0 :       pattern_stmt = gimple_build_assign (ret_var, COND_EXPR, cmp,
    2453              :                                   gimple_get_lhs (pattern_stmt),
    2454              :                                   fold_convert (lhs_type, zero_val));
    2455              :     }
    2456              : 
    2457          328 :   if (lhs_type != orig_lhs_type)
    2458              :     {
    2459            0 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vec_type);
    2460            0 :       tree ret_var = vect_recog_temp_ssa_var (orig_lhs_type, NULL);
    2461            0 :       pattern_stmt = gimple_build_assign (ret_var, NOP_EXPR,
    2462              :                                           gimple_get_lhs (pattern_stmt));
    2463              :     }
    2464              :   return pattern_stmt;
    2465              : }
    2466              : 
    2467              : /* Function vect_recog_pow_pattern
    2468              : 
    2469              :    Try to find the following pattern:
    2470              : 
    2471              :      x = POW (y, N);
    2472              : 
    2473              :    with POW being one of pow, powf, powi, powif and N being
    2474              :    either 2 or 0.5.
    2475              : 
    2476              :    Input:
    2477              : 
    2478              :    * STMT_VINFO: The stmt from which the pattern search begins.
    2479              : 
    2480              :    Output:
    2481              : 
    2482              :    * TYPE_OUT: The type of the output of this pattern.
    2483              : 
    2484              :    * Return value: A new stmt that will be used to replace the sequence of
    2485              :    stmts that constitute the pattern. In this case it will be:
    2486              :         x = x * x
    2487              :    or
    2488              :         x = sqrt (x)
    2489              : */
    2490              : 
    2491              : static gimple *
    2492     33477284 : vect_recog_pow_pattern (vec_info *vinfo,
    2493              :                         stmt_vec_info stmt_vinfo, tree *type_out)
    2494              : {
    2495     33477284 :   gimple *last_stmt = stmt_vinfo->stmt;
    2496     33477284 :   tree base, exp;
    2497     33477284 :   gimple *stmt;
    2498     33477284 :   tree var;
    2499              : 
    2500     33477284 :   if (!is_gimple_call (last_stmt) || gimple_call_lhs (last_stmt) == NULL)
    2501              :     return NULL;
    2502              : 
    2503      1575721 :   switch (gimple_call_combined_fn (last_stmt))
    2504              :     {
    2505          552 :     CASE_CFN_POW:
    2506          552 :     CASE_CFN_POWI:
    2507          552 :       break;
    2508              : 
    2509              :     default:
    2510              :       return NULL;
    2511              :     }
    2512              : 
    2513          552 :   base = gimple_call_arg (last_stmt, 0);
    2514          552 :   exp = gimple_call_arg (last_stmt, 1);
    2515          552 :   if (TREE_CODE (exp) != REAL_CST
    2516          497 :       && TREE_CODE (exp) != INTEGER_CST)
    2517              :     {
    2518          497 :       if (flag_unsafe_math_optimizations
    2519           57 :           && TREE_CODE (base) == REAL_CST
    2520          500 :           && gimple_call_builtin_p (last_stmt, BUILT_IN_NORMAL))
    2521              :         {
    2522            3 :           combined_fn log_cfn;
    2523            3 :           built_in_function exp_bfn;
    2524            3 :           switch (DECL_FUNCTION_CODE (gimple_call_fndecl (last_stmt)))
    2525              :             {
    2526              :             case BUILT_IN_POW:
    2527              :               log_cfn = CFN_BUILT_IN_LOG;
    2528              :               exp_bfn = BUILT_IN_EXP;
    2529              :               break;
    2530            0 :             case BUILT_IN_POWF:
    2531            0 :               log_cfn = CFN_BUILT_IN_LOGF;
    2532            0 :               exp_bfn = BUILT_IN_EXPF;
    2533            0 :               break;
    2534            0 :             case BUILT_IN_POWL:
    2535            0 :               log_cfn = CFN_BUILT_IN_LOGL;
    2536            0 :               exp_bfn = BUILT_IN_EXPL;
    2537            0 :               break;
    2538              :             default:
    2539              :               return NULL;
    2540              :             }
    2541            3 :           tree logc = fold_const_call (log_cfn, TREE_TYPE (base), base);
    2542            3 :           tree exp_decl = builtin_decl_implicit (exp_bfn);
    2543              :           /* Optimize pow (C, x) as exp (log (C) * x).  Normally match.pd
    2544              :              does that, but if C is a power of 2, we want to use
    2545              :              exp2 (log2 (C) * x) in the non-vectorized version, but for
    2546              :              vectorization we don't have vectorized exp2.  */
    2547            3 :           if (logc
    2548            3 :               && TREE_CODE (logc) == REAL_CST
    2549            3 :               && exp_decl
    2550            6 :               && lookup_attribute ("omp declare simd",
    2551            3 :                                    DECL_ATTRIBUTES (exp_decl)))
    2552              :             {
    2553            3 :               cgraph_node *node = cgraph_node::get_create (exp_decl);
    2554            3 :               if (node->simd_clones == NULL)
    2555              :                 {
    2556            2 :                   if (targetm.simd_clone.compute_vecsize_and_simdlen == NULL
    2557            2 :                       || node->definition)
    2558              :                     return NULL;
    2559            2 :                   expand_simd_clones (node);
    2560            2 :                   if (node->simd_clones == NULL)
    2561              :                     return NULL;
    2562              :                 }
    2563            3 :               *type_out = get_vectype_for_scalar_type (vinfo, TREE_TYPE (base));
    2564            3 :               if (!*type_out)
    2565              :                 return NULL;
    2566            3 :               tree def = vect_recog_temp_ssa_var (TREE_TYPE (base), NULL);
    2567            3 :               gimple *g = gimple_build_assign (def, MULT_EXPR, exp, logc);
    2568            3 :               append_pattern_def_seq (vinfo, stmt_vinfo, g);
    2569            3 :               tree res = vect_recog_temp_ssa_var (TREE_TYPE (base), NULL);
    2570            3 :               g = gimple_build_call (exp_decl, 1, def);
    2571            3 :               gimple_call_set_lhs (g, res);
    2572            3 :               return g;
    2573              :             }
    2574              :         }
    2575              : 
    2576              :       return NULL;
    2577              :     }
    2578              : 
    2579              :   /* We now have a pow or powi builtin function call with a constant
    2580              :      exponent.  */
    2581              : 
    2582              :   /* Catch squaring.  */
    2583           55 :   if ((tree_fits_shwi_p (exp)
    2584            0 :        && tree_to_shwi (exp) == 2)
    2585           55 :       || (TREE_CODE (exp) == REAL_CST
    2586           55 :           && real_equal (&TREE_REAL_CST (exp), &dconst2)))
    2587              :     {
    2588           11 :       if (!vect_supportable_direct_optab_p (vinfo, TREE_TYPE (base), MULT_EXPR,
    2589           11 :                                             TREE_TYPE (base), type_out))
    2590              :         return NULL;
    2591              : 
    2592            9 :       var = vect_recog_temp_ssa_var (TREE_TYPE (base), NULL);
    2593            9 :       stmt = gimple_build_assign (var, MULT_EXPR, base, base);
    2594            9 :       return stmt;
    2595              :     }
    2596              : 
    2597              :   /* Catch square root.  Transforming pow(x,0.5) to sqrt(x) is unsafe
    2598              :      for signed zeroes and negative infinity.
    2599              :      pow(-0,0.5) = +0, while sqrt(-0) = -0.
    2600              :      pow(-Inf,0.5) = +Inf, while sqrt(-Inf) is NaN.  */
    2601           44 :   if (TREE_CODE (exp) == REAL_CST
    2602           44 :       && real_equal (&TREE_REAL_CST (exp), &dconsthalf)
    2603           10 :       && !tree_expr_maybe_real_minus_zero_p (base)
    2604           44 :       && (!tree_expr_maybe_infinite_p (base)
    2605            0 :           || tree_expr_nonnegative_p (base)))
    2606              :     {
    2607            0 :       *type_out = get_vectype_for_scalar_type (vinfo, TREE_TYPE (base));
    2608            0 :       if (*type_out
    2609            0 :           && direct_internal_fn_supported_p (IFN_SQRT, *type_out,
    2610              :                                              OPTIMIZE_FOR_SPEED))
    2611              :         {
    2612            0 :           gcall *stmt = gimple_build_call_internal (IFN_SQRT, 1, base);
    2613            0 :           var = vect_recog_temp_ssa_var (TREE_TYPE (base), stmt);
    2614            0 :           gimple_call_set_lhs (stmt, var);
    2615            0 :           gimple_call_set_nothrow (stmt, true);
    2616            0 :           return stmt;
    2617              :         }
    2618              :     }
    2619              : 
    2620              :   return NULL;
    2621              : }
    2622              : 
    2623              : 
    2624              : /* Function vect_recog_widen_sum_pattern
    2625              : 
    2626              :    Try to find the following pattern:
    2627              : 
    2628              :      type x_t;
    2629              :      TYPE x_T, sum = init;
    2630              :    loop:
    2631              :      sum_0 = phi <init, sum_1>
    2632              :      S1  x_t = *p;
    2633              :      S2  x_T = (TYPE) x_t;
    2634              :      S3  sum_1 = x_T + sum_0;
    2635              : 
    2636              :    where type 'TYPE' is at least double the size of type 'type', i.e - we're
    2637              :    summing elements of type 'type' into an accumulator of type 'TYPE'. This is
    2638              :    a special case of a reduction computation.
    2639              : 
    2640              :    Input:
    2641              : 
    2642              :    * STMT_VINFO: The stmt from which the pattern search begins. In the example,
    2643              :    when this function is called with S3, the pattern {S2,S3} will be detected.
    2644              : 
    2645              :    Output:
    2646              : 
    2647              :    * TYPE_OUT: The type of the output of this pattern.
    2648              : 
    2649              :    * Return value: A new stmt that will be used to replace the sequence of
    2650              :    stmts that constitute the pattern. In this case it will be:
    2651              :         WIDEN_SUM <x_t, sum_0>
    2652              : 
    2653              :    Note: The widening-sum idiom is a widening reduction pattern that is
    2654              :          vectorized without preserving all the intermediate results. It
    2655              :          produces only N/2 (widened) results (by summing up pairs of
    2656              :          intermediate results) rather than all N results.  Therefore, we
    2657              :          cannot allow this pattern when we want to get all the results and in
    2658              :          the correct order (as is the case when this computation is in an
    2659              :          inner-loop nested in an outer-loop that us being vectorized).  */
    2660              : 
    2661              : static gimple *
    2662     33477284 : vect_recog_widen_sum_pattern (vec_info *vinfo,
    2663              :                               stmt_vec_info stmt_vinfo, tree *type_out)
    2664              : {
    2665     33477284 :   gimple *last_stmt = stmt_vinfo->stmt;
    2666     33477284 :   tree oprnd0, oprnd1;
    2667     33477284 :   tree type;
    2668     33477284 :   gimple *pattern_stmt;
    2669     33477284 :   tree var;
    2670              : 
    2671              :   /* Look for the following pattern
    2672              :           DX = (TYPE) X;
    2673              :           sum_1 = DX + sum_0;
    2674              :      In which DX is at least double the size of X, and sum_1 has been
    2675              :      recognized as a reduction variable.
    2676              :    */
    2677              : 
    2678              :   /* Starting from LAST_STMT, follow the defs of its uses in search
    2679              :      of the above pattern.  */
    2680              : 
    2681     33477284 :   if (!vect_reassociating_reduction_p (vinfo, stmt_vinfo, PLUS_EXPR,
    2682              :                                        &oprnd0, &oprnd1)
    2683        54056 :       || TREE_CODE (oprnd0) != SSA_NAME
    2684     33531069 :       || !vinfo->lookup_def (oprnd0))
    2685              :     return NULL;
    2686              : 
    2687        53708 :   type = TREE_TYPE (gimple_get_lhs (last_stmt));
    2688              : 
    2689              :   /* So far so good.  Since last_stmt was detected as a (summation) reduction,
    2690              :      we know that oprnd1 is the reduction variable (defined by a loop-header
    2691              :      phi), and oprnd0 is an ssa-name defined by a stmt in the loop body.
    2692              :      Left to check that oprnd0 is defined by a cast from type 'type' to type
    2693              :      'TYPE'.  */
    2694              : 
    2695        53708 :   vect_unpromoted_value unprom0;
    2696        53708 :   if (!vect_look_through_possible_promotion (vinfo, oprnd0, &unprom0)
    2697        53708 :       || TYPE_PRECISION (unprom0.type) * 2 > TYPE_PRECISION (type))
    2698              :     return NULL;
    2699              : 
    2700         2428 :   vect_pattern_detected ("vect_recog_widen_sum_pattern", last_stmt);
    2701              : 
    2702              :   /* If the exact input type is not supported, widen it to an intermediate
    2703              :      type before adding it to the accumulator.  Start with the narrowest
    2704              :      type in order to retain the largest vectorization factor.  */
    2705         2428 :   tree input_type = unprom0.type;
    2706         2428 :   tree input_vectype = NULL_TREE;
    2707              : 
    2708         4533 :   while (!vect_supportable_conv_optab_p (vinfo, type, WIDEN_SUM_EXPR,
    2709              :                                          input_type, type_out, &input_vectype))
    2710              :     {
    2711         4533 :       unsigned int precision
    2712         4533 :         = vect_element_precision (TYPE_PRECISION (input_type) + 1);
    2713              : 
    2714         4533 :       if (precision > TYPE_PRECISION (type) / 2)
    2715              :         return NULL;
    2716              : 
    2717         2105 :       input_type = build_nonstandard_integer_type
    2718         2105 :         (precision, TYPE_UNSIGNED (unprom0.type));
    2719              :     }
    2720              : 
    2721            0 :   tree input = vect_convert_input (vinfo, stmt_vinfo, input_type, &unprom0,
    2722              :                                    input_vectype);
    2723              : 
    2724            0 :   var = vect_recog_temp_ssa_var (type, NULL);
    2725            0 :   pattern_stmt = gimple_build_assign (var, WIDEN_SUM_EXPR, input, oprnd1);
    2726              : 
    2727            0 :   return pattern_stmt;
    2728              : }
    2729              : 
    2730              : /* Function vect_recog_bitfield_ref_pattern
    2731              : 
    2732              :    Try to find the following pattern:
    2733              : 
    2734              :    bf_value = BIT_FIELD_REF (container, bitsize, bitpos);
    2735              :    result = (type_out) bf_value;
    2736              : 
    2737              :    or
    2738              : 
    2739              :    if (BIT_FIELD_REF (container, bitsize, bitpos) `cmp` <constant>)
    2740              : 
    2741              :    where type_out is a non-bitfield type, that is to say, it's precision matches
    2742              :    2^(TYPE_SIZE(type_out) - (TYPE_UNSIGNED (type_out) ? 1 : 2)).
    2743              : 
    2744              :    Input:
    2745              : 
    2746              :    * STMT_VINFO: The stmt from which the pattern search begins.
    2747              :    here it starts with:
    2748              :    result = (type_out) bf_value;
    2749              : 
    2750              :    or
    2751              : 
    2752              :    if (BIT_FIELD_REF (container, bitsize, bitpos) `cmp` <constant>)
    2753              : 
    2754              :    Output:
    2755              : 
    2756              :    * TYPE_OUT: The vector type of the output of this pattern.
    2757              : 
    2758              :    * Return value: A new stmt that will be used to replace the sequence of
    2759              :    stmts that constitute the pattern. If the precision of type_out is bigger
    2760              :    than the precision type of _1 we perform the widening before the shifting,
    2761              :    since the new precision will be large enough to shift the value and moving
    2762              :    widening operations up the statement chain enables the generation of
    2763              :    widening loads.  If we are widening and the operation after the pattern is
    2764              :    an addition then we mask first and shift later, to enable the generation of
    2765              :    shifting adds.  In the case of narrowing we will always mask first, shift
    2766              :    last and then perform a narrowing operation.  This will enable the
    2767              :    generation of narrowing shifts.
    2768              : 
    2769              :    Widening with mask first, shift later:
    2770              :    container = (type_out) container;
    2771              :    masked = container & (((1 << bitsize) - 1) << bitpos);
    2772              :    result = masked >> bitpos;
    2773              : 
    2774              :    Widening with shift first, mask last:
    2775              :    container = (type_out) container;
    2776              :    shifted = container >> bitpos;
    2777              :    result = shifted & ((1 << bitsize) - 1);
    2778              : 
    2779              :    Narrowing:
    2780              :    masked = container & (((1 << bitsize) - 1) << bitpos);
    2781              :    result = masked >> bitpos;
    2782              :    result = (type_out) result;
    2783              : 
    2784              :    If the bitfield is signed and it's wider than type_out, we need to
    2785              :    keep the result sign-extended:
    2786              :    container = (type) container;
    2787              :    masked = container << (prec - bitsize - bitpos);
    2788              :    result = (type_out) (masked >> (prec - bitsize));
    2789              : 
    2790              :    Here type is the signed variant of the wider of type_out and the type
    2791              :    of container.
    2792              : 
    2793              :    The shifting is always optional depending on whether bitpos != 0.
    2794              : 
    2795              :    When the original bitfield was inside a gcond then an new gcond is also
    2796              :    generated with the newly `result` as the operand to the comparison.
    2797              : 
    2798              : */
    2799              : 
    2800              : static gimple *
    2801     33424905 : vect_recog_bitfield_ref_pattern (vec_info *vinfo, stmt_vec_info stmt_info,
    2802              :                                  tree *type_out)
    2803              : {
    2804     33424905 :   gassign *bf_stmt = NULL;
    2805     33424905 :   tree lhs = NULL_TREE;
    2806     33424905 :   tree ret_type = NULL_TREE;
    2807     33424905 :   gimple *stmt = STMT_VINFO_STMT (stmt_info);
    2808     33424905 :   auto assign = dyn_cast<gassign *> (stmt);
    2809     33424905 :   if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
    2810              :     {
    2811      5510716 :       tree op = gimple_cond_lhs (cond_stmt);
    2812      5510716 :       if (TREE_CODE (op) != SSA_NAME)
    2813              :         return NULL;
    2814      5510401 :       bf_stmt = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (op));
    2815      5510401 :       if (TREE_CODE (gimple_cond_rhs (cond_stmt)) != INTEGER_CST)
    2816              :         return NULL;
    2817              :     }
    2818     27914189 :   else if (assign
    2819     23444208 :            && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (assign))
    2820     31095111 :            && TREE_CODE (gimple_assign_rhs1 (assign)) == SSA_NAME)
    2821              :     {
    2822      3138095 :       gimple *second_stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (assign));
    2823      3138095 :       bf_stmt = dyn_cast <gassign *> (second_stmt);
    2824      3138095 :       lhs = gimple_assign_lhs (stmt);
    2825      3138095 :       ret_type = TREE_TYPE (lhs);
    2826              :     }
    2827              : 
    2828      6697319 :   if (!bf_stmt
    2829     11741554 :       || gimple_assign_rhs_code (bf_stmt) != BIT_FIELD_REF)
    2830              :     return NULL;
    2831              : 
    2832        18357 :   tree bf_ref = gimple_assign_rhs1 (bf_stmt);
    2833        18357 :   tree container = TREE_OPERAND (bf_ref, 0);
    2834        18357 :   ret_type = ret_type ? ret_type : TREE_TYPE (container);
    2835              : 
    2836        18357 :   if (!bit_field_offset (bf_ref).is_constant ()
    2837        18357 :       || !bit_field_size (bf_ref).is_constant ()
    2838        18357 :       || !tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (container))))
    2839              :     return NULL;
    2840              : 
    2841        36336 :   if (!INTEGRAL_TYPE_P (TREE_TYPE (bf_ref))
    2842        18355 :       || !INTEGRAL_TYPE_P (TREE_TYPE (container))
    2843        20530 :       || TYPE_MODE (TREE_TYPE (container)) == E_BLKmode)
    2844              :     return NULL;
    2845              : 
    2846         2173 :   gimple *use_stmt, *pattern_stmt;
    2847         2173 :   use_operand_p use_p;
    2848         2173 :   bool shift_first = true;
    2849         2173 :   tree container_type = TREE_TYPE (container);
    2850         2173 :   tree vectype = get_vectype_for_scalar_type (vinfo, container_type);
    2851              : 
    2852              :   /* Calculate shift_n before the adjustments for widening loads, otherwise
    2853              :      the container may change and we have to consider offset change for
    2854              :      widening loads on big endianness.  The shift_n calculated here can be
    2855              :      independent of widening.  */
    2856         2173 :   unsigned HOST_WIDE_INT shift_n = bit_field_offset (bf_ref).to_constant ();
    2857         2173 :   unsigned HOST_WIDE_INT mask_width = bit_field_size (bf_ref).to_constant ();
    2858         2173 :   unsigned HOST_WIDE_INT prec = tree_to_uhwi (TYPE_SIZE (container_type));
    2859         2173 :   if (BYTES_BIG_ENDIAN)
    2860              :     shift_n = prec - shift_n - mask_width;
    2861              : 
    2862         2173 :   bool ref_sext = (!TYPE_UNSIGNED (TREE_TYPE (bf_ref)) &&
    2863         1400 :                    TYPE_PRECISION (ret_type) > mask_width);
    2864         2173 :   bool load_widen = (TYPE_PRECISION (TREE_TYPE (container)) <
    2865         2173 :                      TYPE_PRECISION (ret_type));
    2866              : 
    2867              :   /* We move the conversion earlier if the loaded type is smaller than the
    2868              :      return type to enable the use of widening loads.  And if we need a
    2869              :      sign extension, we need to convert the loaded value early to a signed
    2870              :      type as well.  */
    2871         2173 :   if (ref_sext || load_widen)
    2872              :     {
    2873          947 :       tree type = load_widen ? ret_type : container_type;
    2874          947 :       if (ref_sext)
    2875          908 :         type = gimple_signed_type (type);
    2876          947 :       pattern_stmt = gimple_build_assign (vect_recog_temp_ssa_var (type),
    2877              :                                           NOP_EXPR, container);
    2878          947 :       container = gimple_get_lhs (pattern_stmt);
    2879          947 :       container_type = TREE_TYPE (container);
    2880          947 :       prec = tree_to_uhwi (TYPE_SIZE (container_type));
    2881          947 :       vectype = get_vectype_for_scalar_type (vinfo, container_type);
    2882          947 :       append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype);
    2883              :     }
    2884         1226 :   else if (!useless_type_conversion_p (TREE_TYPE (container), ret_type))
    2885              :     /* If we are doing the conversion last then also delay the shift as we may
    2886              :        be able to combine the shift and conversion in certain cases.  */
    2887              :     shift_first = false;
    2888              : 
    2889              :   /* If the only use of the result of this BIT_FIELD_REF + CONVERT is a
    2890              :      PLUS_EXPR then do the shift last as some targets can combine the shift and
    2891              :      add into a single instruction.  */
    2892         1416 :   if (lhs && !is_pattern_stmt_p (stmt_info)
    2893         3589 :       && single_imm_use (lhs, &use_p, &use_stmt))
    2894              :     {
    2895         1049 :       if (gimple_code (use_stmt) == GIMPLE_ASSIGN
    2896         1049 :           && gimple_assign_rhs_code (use_stmt) == PLUS_EXPR)
    2897              :         shift_first = false;
    2898              :     }
    2899              : 
    2900              :   /* If we don't have to shift we only generate the mask, so just fix the
    2901              :      code-path to shift_first.  */
    2902         2173 :   if (shift_n == 0)
    2903          763 :     shift_first = true;
    2904              : 
    2905         2173 :   tree result;
    2906         2173 :   if (shift_first && !ref_sext)
    2907              :     {
    2908          513 :       tree shifted = container;
    2909          513 :       if (shift_n)
    2910              :         {
    2911           65 :           pattern_stmt
    2912           65 :             = gimple_build_assign (vect_recog_temp_ssa_var (container_type),
    2913              :                                    RSHIFT_EXPR, container,
    2914           65 :                                    build_int_cst (sizetype, shift_n));
    2915           65 :           shifted = gimple_assign_lhs (pattern_stmt);
    2916           65 :           append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype);
    2917              :         }
    2918              : 
    2919          513 :       tree mask = wide_int_to_tree (container_type,
    2920          513 :                                     wi::mask (mask_width, false, prec));
    2921              : 
    2922          513 :       pattern_stmt
    2923          513 :         = gimple_build_assign (vect_recog_temp_ssa_var (container_type),
    2924              :                                BIT_AND_EXPR, shifted, mask);
    2925          513 :       result = gimple_assign_lhs (pattern_stmt);
    2926              :     }
    2927              :   else
    2928              :     {
    2929         1660 :       tree temp = vect_recog_temp_ssa_var (container_type);
    2930         1660 :       if (!ref_sext)
    2931              :         {
    2932          752 :           tree mask = wide_int_to_tree (container_type,
    2933          752 :                                         wi::shifted_mask (shift_n,
    2934              :                                                           mask_width,
    2935              :                                                           false, prec));
    2936          752 :           pattern_stmt = gimple_build_assign (temp, BIT_AND_EXPR,
    2937              :                                               container, mask);
    2938              :         }
    2939              :       else
    2940              :         {
    2941          908 :           HOST_WIDE_INT shl = prec - shift_n - mask_width;
    2942          908 :           shift_n += shl;
    2943          908 :           pattern_stmt = gimple_build_assign (temp, LSHIFT_EXPR,
    2944              :                                               container,
    2945              :                                               build_int_cst (sizetype,
    2946          908 :                                                              shl));
    2947              :         }
    2948              : 
    2949         1660 :       tree masked = gimple_assign_lhs (pattern_stmt);
    2950         1660 :       append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype);
    2951         1660 :       pattern_stmt
    2952         1660 :         = gimple_build_assign (vect_recog_temp_ssa_var (container_type),
    2953              :                                RSHIFT_EXPR, masked,
    2954         1660 :                                build_int_cst (sizetype, shift_n));
    2955         1660 :       result = gimple_assign_lhs (pattern_stmt);
    2956              :     }
    2957              : 
    2958         2173 :   if (!useless_type_conversion_p (TREE_TYPE (result), ret_type))
    2959              :     {
    2960         1444 :       append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype);
    2961         1444 :       pattern_stmt
    2962         1444 :         = gimple_build_assign (vect_recog_temp_ssa_var (ret_type),
    2963              :                                NOP_EXPR, result);
    2964              :     }
    2965              : 
    2966         2173 :   if (!lhs)
    2967              :     {
    2968          757 :       if (!vectype)
    2969              :         return NULL;
    2970              : 
    2971          619 :       append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype);
    2972          619 :       vectype = truth_type_for (vectype);
    2973              : 
    2974              :       /* FIXME: This part extracts the boolean value out of the bitfield in the
    2975              :                 same way as vect_recog_gcond_pattern does.  However because
    2976              :                 patterns cannot match the same root twice,  when we handle and
    2977              :                 lower the bitfield in the gcond, vect_recog_gcond_pattern can't
    2978              :                 apply anymore.  We should really fix it so that we don't need to
    2979              :                 duplicate transformations like these.  */
    2980          619 :       tree new_lhs = vect_recog_temp_ssa_var (boolean_type_node, NULL);
    2981          619 :       gcond *cond_stmt = dyn_cast <gcond *> (stmt_info->stmt);
    2982          619 :       tree cond_cst = gimple_cond_rhs (cond_stmt);
    2983          619 :       gimple *new_stmt
    2984          619 :         = gimple_build_assign (new_lhs, gimple_cond_code (cond_stmt),
    2985              :                                gimple_get_lhs (pattern_stmt),
    2986              :                                fold_convert (container_type, cond_cst));
    2987          619 :       append_pattern_def_seq (vinfo, stmt_info, new_stmt, vectype, container_type);
    2988          619 :       pattern_stmt
    2989          619 :         = gimple_build_cond (NE_EXPR, new_lhs,
    2990          619 :                              build_zero_cst (TREE_TYPE (new_lhs)),
    2991              :                              NULL_TREE, NULL_TREE);
    2992              :     }
    2993              : 
    2994         2035 :   *type_out = STMT_VINFO_VECTYPE (stmt_info);
    2995         2035 :   vect_pattern_detected ("bitfield_ref pattern", stmt_info->stmt);
    2996              : 
    2997         2035 :   return pattern_stmt;
    2998              : }
    2999              : 
    3000              : /* Function vect_recog_bit_insert_pattern
    3001              : 
    3002              :    Try to find the following pattern:
    3003              : 
    3004              :    written = BIT_INSERT_EXPR (container, value, bitpos);
    3005              : 
    3006              :    Input:
    3007              : 
    3008              :    * STMT_VINFO: The stmt we want to replace.
    3009              : 
    3010              :    Output:
    3011              : 
    3012              :    * TYPE_OUT: The vector type of the output of this pattern.
    3013              : 
    3014              :    * Return value: A new stmt that will be used to replace the sequence of
    3015              :    stmts that constitute the pattern. In this case it will be:
    3016              :    value = (container_type) value;          // Make sure
    3017              :    shifted = value << bitpos;                 // Shift value into place
    3018              :    masked = shifted & (mask << bitpos);           // Mask off the non-relevant bits in
    3019              :                                             // the 'to-write value'.
    3020              :    cleared = container & ~(mask << bitpos); // Clearing the bits we want to
    3021              :                                             // write to from the value we want
    3022              :                                             // to write to.
    3023              :    written = cleared | masked;              // Write bits.
    3024              : 
    3025              : 
    3026              :    where mask = ((1 << TYPE_PRECISION (value)) - 1), a mask to keep the number of
    3027              :    bits corresponding to the real size of the bitfield value we are writing to.
    3028              :    The shifting is always optional depending on whether bitpos != 0.
    3029              : 
    3030              : */
    3031              : 
    3032              : static gimple *
    3033     33427955 : vect_recog_bit_insert_pattern (vec_info *vinfo, stmt_vec_info stmt_info,
    3034              :                                tree *type_out)
    3035              : {
    3036     33427955 :   gassign *bf_stmt = dyn_cast <gassign *> (stmt_info->stmt);
    3037     31276577 :   if (!bf_stmt || gimple_assign_rhs_code (bf_stmt) != BIT_INSERT_EXPR)
    3038              :     return NULL;
    3039              : 
    3040          666 :   tree container = gimple_assign_rhs1 (bf_stmt);
    3041          666 :   tree value = gimple_assign_rhs2 (bf_stmt);
    3042          666 :   tree shift = gimple_assign_rhs3 (bf_stmt);
    3043              : 
    3044          666 :   tree bf_type = TREE_TYPE (value);
    3045          666 :   tree container_type = TREE_TYPE (container);
    3046              : 
    3047          666 :   if (!INTEGRAL_TYPE_P (container_type)
    3048          666 :       || !tree_fits_uhwi_p (TYPE_SIZE (container_type)))
    3049              :     return NULL;
    3050              : 
    3051          508 :   gimple *pattern_stmt;
    3052              : 
    3053          508 :   vect_unpromoted_value unprom;
    3054          508 :   unprom.set_op (value, vect_internal_def);
    3055          508 :   value = vect_convert_input (vinfo, stmt_info, container_type, &unprom,
    3056              :                               get_vectype_for_scalar_type (vinfo,
    3057              :                                                            container_type));
    3058              : 
    3059          508 :   unsigned HOST_WIDE_INT mask_width = TYPE_PRECISION (bf_type);
    3060          508 :   unsigned HOST_WIDE_INT prec = tree_to_uhwi (TYPE_SIZE (container_type));
    3061          508 :   unsigned HOST_WIDE_INT shift_n = tree_to_uhwi (shift);
    3062          508 :   if (BYTES_BIG_ENDIAN)
    3063              :     {
    3064              :       shift_n = prec - shift_n - mask_width;
    3065              :       shift = build_int_cst (TREE_TYPE (shift), shift_n);
    3066              :     }
    3067              : 
    3068          508 :   if (!useless_type_conversion_p (TREE_TYPE (value), container_type))
    3069              :     {
    3070            0 :       pattern_stmt =
    3071            0 :         gimple_build_assign (vect_recog_temp_ssa_var (container_type),
    3072              :                              NOP_EXPR, value);
    3073            0 :       append_pattern_def_seq (vinfo, stmt_info, pattern_stmt);
    3074            0 :       value = gimple_get_lhs (pattern_stmt);
    3075              :     }
    3076              : 
    3077              :   /* Shift VALUE into place.  */
    3078          508 :   tree shifted = value;
    3079          508 :   if (shift_n)
    3080              :     {
    3081          249 :       gimple_seq stmts = NULL;
    3082          249 :       shifted
    3083          249 :         = gimple_build (&stmts, LSHIFT_EXPR, container_type, value, shift);
    3084          249 :       if (!gimple_seq_empty_p (stmts))
    3085          112 :         append_pattern_def_seq (vinfo, stmt_info,
    3086              :                                 gimple_seq_first_stmt (stmts));
    3087              :     }
    3088              : 
    3089          508 :   tree mask_t
    3090          508 :     = wide_int_to_tree (container_type,
    3091          508 :                         wi::shifted_mask (shift_n, mask_width, false, prec));
    3092              : 
    3093              :   /* Clear bits we don't want to write back from SHIFTED.  */
    3094          508 :   gimple_seq stmts = NULL;
    3095          508 :   tree masked = gimple_build (&stmts, BIT_AND_EXPR, container_type, shifted,
    3096              :                               mask_t);
    3097          508 :   if (!gimple_seq_empty_p (stmts))
    3098              :     {
    3099          118 :       pattern_stmt = gimple_seq_first_stmt (stmts);
    3100          118 :       append_pattern_def_seq (vinfo, stmt_info, pattern_stmt);
    3101              :     }
    3102              : 
    3103              :   /* Mask off the bits in the container that we are to write to.  */
    3104          508 :   mask_t = wide_int_to_tree (container_type,
    3105          508 :                              wi::shifted_mask (shift_n, mask_width, true, prec));
    3106          508 :   tree cleared = vect_recog_temp_ssa_var (container_type);
    3107          508 :   pattern_stmt = gimple_build_assign (cleared, BIT_AND_EXPR, container, mask_t);
    3108          508 :   append_pattern_def_seq (vinfo, stmt_info, pattern_stmt);
    3109              : 
    3110              :   /* Write MASKED into CLEARED.  */
    3111          508 :   pattern_stmt
    3112          508 :     = gimple_build_assign (vect_recog_temp_ssa_var (container_type),
    3113              :                            BIT_IOR_EXPR, cleared, masked);
    3114              : 
    3115          508 :   *type_out = STMT_VINFO_VECTYPE (stmt_info);
    3116          508 :   vect_pattern_detected ("bit_insert pattern", stmt_info->stmt);
    3117              : 
    3118          508 :   return pattern_stmt;
    3119              : }
    3120              : 
    3121              : 
    3122              : /* Recognize cases in which an operation is performed in one type WTYPE
    3123              :    but could be done more efficiently in a narrower type NTYPE.  For example,
    3124              :    if we have:
    3125              : 
    3126              :      ATYPE a;  // narrower than NTYPE
    3127              :      BTYPE b;  // narrower than NTYPE
    3128              :      WTYPE aw = (WTYPE) a;
    3129              :      WTYPE bw = (WTYPE) b;
    3130              :      WTYPE res = aw + bw;  // only uses of aw and bw
    3131              : 
    3132              :    then it would be more efficient to do:
    3133              : 
    3134              :      NTYPE an = (NTYPE) a;
    3135              :      NTYPE bn = (NTYPE) b;
    3136              :      NTYPE resn = an + bn;
    3137              :      WTYPE res = (WTYPE) resn;
    3138              : 
    3139              :    Other situations include things like:
    3140              : 
    3141              :      ATYPE a;  // NTYPE or narrower
    3142              :      WTYPE aw = (WTYPE) a;
    3143              :      WTYPE res = aw + b;
    3144              : 
    3145              :    when only "(NTYPE) res" is significant.  In that case it's more efficient
    3146              :    to truncate "b" and do the operation on NTYPE instead:
    3147              : 
    3148              :      NTYPE an = (NTYPE) a;
    3149              :      NTYPE bn = (NTYPE) b;  // truncation
    3150              :      NTYPE resn = an + bn;
    3151              :      WTYPE res = (WTYPE) resn;
    3152              : 
    3153              :    All users of "res" should then use "resn" instead, making the final
    3154              :    statement dead (not marked as relevant).  The final statement is still
    3155              :    needed to maintain the type correctness of the IR.
    3156              : 
    3157              :    vect_determine_precisions has already determined the minimum
    3158              :    precision of the operation and the minimum precision required
    3159              :    by users of the result.  */
    3160              : 
    3161              : static gimple *
    3162     33428411 : vect_recog_over_widening_pattern (vec_info *vinfo,
    3163              :                                   stmt_vec_info last_stmt_info, tree *type_out)
    3164              : {
    3165     33428411 :   gassign *last_stmt = dyn_cast <gassign *> (last_stmt_info->stmt);
    3166     23448333 :   if (!last_stmt)
    3167              :     return NULL;
    3168              : 
    3169              :   /* See whether we have found that this operation can be done on a
    3170              :      narrower type without changing its semantics.  */
    3171     23448333 :   unsigned int new_precision = last_stmt_info->operation_precision;
    3172     23448333 :   if (!new_precision)
    3173              :     return NULL;
    3174              : 
    3175      1723947 :   tree lhs = gimple_assign_lhs (last_stmt);
    3176      1723947 :   tree type = TREE_TYPE (lhs);
    3177      1723947 :   tree_code code = gimple_assign_rhs_code (last_stmt);
    3178              : 
    3179              :   /* Punt for reductions where we don't handle the type conversions.  */
    3180      1723947 :   if (vect_is_reduction (last_stmt_info))
    3181              :     return NULL;
    3182              : 
    3183              :   /* Keep the first operand of a COND_EXPR as-is: only the other two
    3184              :      operands are interesting.  */
    3185      1715441 :   unsigned int first_op = (code == COND_EXPR ? 2 : 1);
    3186              : 
    3187              :   /* Check the operands.  */
    3188      1715441 :   unsigned int nops = gimple_num_ops (last_stmt) - first_op;
    3189      1715441 :   auto_vec <vect_unpromoted_value, 3> unprom (nops);
    3190      1715441 :   unprom.quick_grow_cleared (nops);
    3191      1715441 :   unsigned int min_precision = 0;
    3192      1715441 :   bool single_use_p = false;
    3193      6845636 :   for (unsigned int i = 0; i < nops; ++i)
    3194              :     {
    3195      3416123 :       tree op = gimple_op (last_stmt, first_op + i);
    3196      3416123 :       if (TREE_CODE (op) == INTEGER_CST)
    3197      1565732 :         unprom[i].set_op (op, vect_constant_def);
    3198      1850391 :       else if (TREE_CODE (op) == SSA_NAME)
    3199              :         {
    3200      1850391 :           bool op_single_use_p = true;
    3201      1850391 :           if (!vect_look_through_possible_promotion (vinfo, op, &unprom[i],
    3202              :                                                      &op_single_use_p))
    3203         1369 :             return NULL;
    3204              :           /* If:
    3205              : 
    3206              :              (1) N bits of the result are needed;
    3207              :              (2) all inputs are widened from M<N bits; and
    3208              :              (3) one operand OP is a single-use SSA name
    3209              : 
    3210              :              we can shift the M->N widening from OP to the output
    3211              :              without changing the number or type of extensions involved.
    3212              :              This then reduces the number of copies of STMT_INFO.
    3213              : 
    3214              :              If instead of (3) more than one operand is a single-use SSA name,
    3215              :              shifting the extension to the output is even more of a win.
    3216              : 
    3217              :              If instead:
    3218              : 
    3219              :              (1) N bits of the result are needed;
    3220              :              (2) one operand OP2 is widened from M2<N bits;
    3221              :              (3) another operand OP1 is widened from M1<M2 bits; and
    3222              :              (4) both OP1 and OP2 are single-use
    3223              : 
    3224              :              the choice is between:
    3225              : 
    3226              :              (a) truncating OP2 to M1, doing the operation on M1,
    3227              :                  and then widening the result to N
    3228              : 
    3229              :              (b) widening OP1 to M2, doing the operation on M2, and then
    3230              :                  widening the result to N
    3231              : 
    3232              :              Both shift the M2->N widening of the inputs to the output.
    3233              :              (a) additionally shifts the M1->M2 widening to the output;
    3234              :              it requires fewer copies of STMT_INFO but requires an extra
    3235              :              M2->M1 truncation.
    3236              : 
    3237              :              Which is better will depend on the complexity and cost of
    3238              :              STMT_INFO, which is hard to predict at this stage.  However,
    3239              :              a clear tie-breaker in favor of (b) is the fact that the
    3240              :              truncation in (a) increases the length of the operation chain.
    3241              : 
    3242              :              If instead of (4) only one of OP1 or OP2 is single-use,
    3243              :              (b) is still a win over doing the operation in N bits:
    3244              :              it still shifts the M2->N widening on the single-use operand
    3245              :              to the output and reduces the number of STMT_INFO copies.
    3246              : 
    3247              :              If neither operand is single-use then operating on fewer than
    3248              :              N bits might lead to more extensions overall.  Whether it does
    3249              :              or not depends on global information about the vectorization
    3250              :              region, and whether that's a good trade-off would again
    3251              :              depend on the complexity and cost of the statements involved,
    3252              :              as well as things like register pressure that are not normally
    3253              :              modelled at this stage.  We therefore ignore these cases
    3254              :              and just optimize the clear single-use wins above.
    3255              : 
    3256              :              Thus we take the maximum precision of the unpromoted operands
    3257              :              and record whether any operand is single-use.  */
    3258      1849022 :           if (unprom[i].dt == vect_internal_def)
    3259              :             {
    3260      1065793 :               min_precision = MAX (min_precision,
    3261              :                                    TYPE_PRECISION (unprom[i].type));
    3262      1065793 :               single_use_p |= op_single_use_p;
    3263              :             }
    3264              :         }
    3265              :       else
    3266              :         return NULL;
    3267              :     }
    3268              : 
    3269              :   /* Although the operation could be done in operation_precision, we have
    3270              :      to balance that against introducing extra truncations or extensions.
    3271              :      Calculate the minimum precision that can be handled efficiently.
    3272              : 
    3273              :      The loop above determined that the operation could be handled
    3274              :      efficiently in MIN_PRECISION if SINGLE_USE_P; this would shift an
    3275              :      extension from the inputs to the output without introducing more
    3276              :      instructions, and would reduce the number of instructions required
    3277              :      for STMT_INFO itself.
    3278              : 
    3279              :      vect_determine_precisions has also determined that the result only
    3280              :      needs min_output_precision bits.  Truncating by a factor of N times
    3281              :      requires a tree of N - 1 instructions, so if TYPE is N times wider
    3282              :      than min_output_precision, doing the operation in TYPE and truncating
    3283              :      the result requires N + (N - 1) = 2N - 1 instructions per output vector.
    3284              :      In contrast:
    3285              : 
    3286              :      - truncating the input to a unary operation and doing the operation
    3287              :        in the new type requires at most N - 1 + 1 = N instructions per
    3288              :        output vector
    3289              : 
    3290              :      - doing the same for a binary operation requires at most
    3291              :        (N - 1) * 2 + 1 = 2N - 1 instructions per output vector
    3292              : 
    3293              :      Both unary and binary operations require fewer instructions than
    3294              :      this if the operands were extended from a suitable truncated form.
    3295              :      Thus there is usually nothing to lose by doing operations in
    3296              :      min_output_precision bits, but there can be something to gain.  */
    3297      1714072 :   if (!single_use_p)
    3298      1350127 :     min_precision = last_stmt_info->min_output_precision;
    3299              :   else
    3300       363945 :     min_precision = MIN (min_precision, last_stmt_info->min_output_precision);
    3301              : 
    3302              :   /* Apply the minimum efficient precision we just calculated.  */
    3303      1714072 :   if (new_precision < min_precision)
    3304              :     new_precision = min_precision;
    3305      1714072 :   new_precision = vect_element_precision (new_precision);
    3306      1714072 :   if (new_precision >= TYPE_PRECISION (type))
    3307              :     return NULL;
    3308              : 
    3309       150257 :   vect_pattern_detected ("vect_recog_over_widening_pattern", last_stmt);
    3310              : 
    3311       150257 :   *type_out = get_vectype_for_scalar_type (vinfo, type);
    3312       150257 :   if (!*type_out)
    3313              :     return NULL;
    3314              : 
    3315              :   /* We've found a viable pattern.  Get the new type of the operation.  */
    3316       130508 :   bool unsigned_p = (last_stmt_info->operation_sign == UNSIGNED);
    3317       130508 :   tree new_type = build_nonstandard_integer_type (new_precision, unsigned_p);
    3318              : 
    3319              :   /* If we're truncating an operation, we need to make sure that we
    3320              :      don't introduce new undefined overflow.  The codes tested here are
    3321              :      a subset of those accepted by vect_truncatable_operation_p.  */
    3322       130508 :   tree op_type = new_type;
    3323       130508 :   if (TYPE_OVERFLOW_UNDEFINED (new_type)
    3324       169618 :       && (code == PLUS_EXPR || code == MINUS_EXPR || code == MULT_EXPR))
    3325        27155 :     op_type = build_nonstandard_integer_type (new_precision, true);
    3326              : 
    3327       130508 :   tree new_vectype = get_vectype_for_scalar_type (vinfo, new_type);
    3328       130508 :   tree op_vectype = get_vectype_for_scalar_type (vinfo, op_type);
    3329       130508 :   if (!new_vectype || !op_vectype)
    3330              :     return NULL;
    3331              : 
    3332              :   /* Verify we can handle the new operation.  For shifts and rotates
    3333              :      apply heuristic of whether we are likely facing vector-vector or
    3334              :      vector-scalar operation.  Since we are eventually expecting that
    3335              :      a later pattern might eventually want to rewrite an unsupported
    3336              :      into a supported case error on that side in case the original
    3337              :      operation was not supported either or this is a binary operation
    3338              :      and the 2nd operand is constant.  */
    3339       130508 :   if (code == RSHIFT_EXPR || code == LSHIFT_EXPR || code == RROTATE_EXPR)
    3340              :     {
    3341        29424 :       if (!target_has_vecop_for_code (code, op_vectype, optab_vector)
    3342        28347 :           && ((unprom[1].dt != vect_external_def
    3343        27929 :                && unprom[1].dt != vect_constant_def)
    3344        19394 :               || !target_has_vecop_for_code (code, op_vectype, optab_scalar))
    3345        38400 :           && !(!target_has_vecop_for_code (code, *type_out, optab_vector)
    3346         7878 :                && ((unprom[1].dt != vect_external_def
    3347         7878 :                     || unprom[1].dt != vect_constant_def)
    3348              :                    || !target_has_vecop_for_code (code, *type_out,
    3349              :                                                   optab_scalar))))
    3350              :         return NULL;
    3351              :     }
    3352       101084 :   else if (!target_has_vecop_for_code (code, op_vectype, optab_vector)
    3353       101084 :            && (target_has_vecop_for_code (code, *type_out, optab_vector)
    3354           29 :                && !(nops == 2 && unprom[1].dt == vect_constant_def)))
    3355              :     return NULL;
    3356              : 
    3357       129400 :   if (dump_enabled_p ())
    3358         4544 :     dump_printf_loc (MSG_NOTE, vect_location, "demoting %T to %T\n",
    3359              :                      type, new_type);
    3360              : 
    3361              :   /* Calculate the rhs operands for an operation on OP_TYPE.  */
    3362       129400 :   tree ops[3] = {};
    3363       129719 :   for (unsigned int i = 1; i < first_op; ++i)
    3364          319 :     ops[i - 1] = gimple_op (last_stmt, i);
    3365       129400 :   vect_convert_inputs (vinfo, last_stmt_info, nops, &ops[first_op - 1],
    3366       129400 :                        op_type, &unprom[0], op_vectype);
    3367              : 
    3368              :   /* Use the operation to produce a result of type OP_TYPE.  */
    3369       129400 :   tree new_var = vect_recog_temp_ssa_var (op_type, NULL);
    3370       129400 :   gimple *pattern_stmt = gimple_build_assign (new_var, code,
    3371              :                                               ops[0], ops[1], ops[2]);
    3372       129400 :   gimple_set_location (pattern_stmt, gimple_location (last_stmt));
    3373              : 
    3374       129400 :   if (dump_enabled_p ())
    3375         4544 :     dump_printf_loc (MSG_NOTE, vect_location,
    3376              :                      "created pattern stmt: %G", pattern_stmt);
    3377              : 
    3378              :   /* Convert back to the original signedness, if OP_TYPE is different
    3379              :      from NEW_TYPE.  */
    3380       129400 :   if (op_type != new_type)
    3381        27148 :     pattern_stmt = vect_convert_output (vinfo, last_stmt_info, new_type,
    3382              :                                         pattern_stmt, op_vectype);
    3383              : 
    3384              :   /* Promote the result to the original type.  */
    3385       129400 :   pattern_stmt = vect_convert_output (vinfo, last_stmt_info, type,
    3386              :                                       pattern_stmt, new_vectype);
    3387              : 
    3388       129400 :   return pattern_stmt;
    3389      1715441 : }
    3390              : 
    3391              : /* Recognize the following patterns:
    3392              : 
    3393              :      ATYPE a;  // narrower than TYPE
    3394              :      BTYPE b;  // narrower than TYPE
    3395              : 
    3396              :    1) Multiply high with scaling
    3397              :      TYPE res = ((TYPE) a * (TYPE) b) >> c;
    3398              :      Here, c is bitsize (TYPE) / 2 - 1.
    3399              : 
    3400              :    2) ... or also with rounding
    3401              :      TYPE res = (((TYPE) a * (TYPE) b) >> d + 1) >> 1;
    3402              :      Here, d is bitsize (TYPE) / 2 - 2.
    3403              : 
    3404              :    3) Normal multiply high
    3405              :      TYPE res = ((TYPE) a * (TYPE) b) >> e;
    3406              :      Here, e is bitsize (TYPE) / 2.
    3407              : 
    3408              :    where only the bottom half of res is used.  */
    3409              : 
    3410              : static gimple *
    3411     33561012 : vect_recog_mulhs_pattern (vec_info *vinfo,
    3412              :                           stmt_vec_info last_stmt_info, tree *type_out)
    3413              : {
    3414              :   /* Check for a right shift.  */
    3415     33561012 :   gassign *last_stmt = dyn_cast <gassign *> (last_stmt_info->stmt);
    3416     23580728 :   if (!last_stmt
    3417     23580728 :       || gimple_assign_rhs_code (last_stmt) != RSHIFT_EXPR)
    3418              :     return NULL;
    3419              : 
    3420              :   /* Check that the shift result is wider than the users of the
    3421              :      result need (i.e. that narrowing would be a natural choice).  */
    3422       377923 :   tree lhs_type = TREE_TYPE (gimple_assign_lhs (last_stmt));
    3423       377923 :   unsigned int target_precision
    3424       377923 :     = vect_element_precision (last_stmt_info->min_output_precision);
    3425       377923 :   if (!INTEGRAL_TYPE_P (lhs_type)
    3426       377923 :       || target_precision >= TYPE_PRECISION (lhs_type))
    3427              :     return NULL;
    3428              : 
    3429              :   /* Look through any change in sign on the outer shift input.  */
    3430        64274 :   vect_unpromoted_value unprom_rshift_input;
    3431        64274 :   tree rshift_input = vect_look_through_possible_promotion
    3432        64274 :     (vinfo, gimple_assign_rhs1 (last_stmt), &unprom_rshift_input);
    3433        64274 :   if (!rshift_input
    3434        64274 :       || TYPE_PRECISION (TREE_TYPE (rshift_input))
    3435        63648 :            != TYPE_PRECISION (lhs_type))
    3436              :     return NULL;
    3437              : 
    3438              :   /* Get the definition of the shift input.  */
    3439        59342 :   stmt_vec_info rshift_input_stmt_info
    3440        59342 :     = vect_get_internal_def (vinfo, rshift_input);
    3441        59342 :   if (!rshift_input_stmt_info)
    3442              :     return NULL;
    3443        54586 :   gassign *rshift_input_stmt
    3444        54586 :     = dyn_cast <gassign *> (rshift_input_stmt_info->stmt);
    3445        51084 :   if (!rshift_input_stmt)
    3446              :     return NULL;
    3447              : 
    3448        51084 :   stmt_vec_info mulh_stmt_info;
    3449        51084 :   tree scale_term;
    3450        51084 :   bool rounding_p = false;
    3451              : 
    3452              :   /* Check for the presence of the rounding term.  */
    3453        58996 :   if (gimple_assign_rhs_code (rshift_input_stmt) == PLUS_EXPR)
    3454              :     {
    3455              :       /* Check that the outer shift was by 1.  */
    3456        17538 :       if (!integer_onep (gimple_assign_rhs2 (last_stmt)))
    3457         8702 :         return NULL;
    3458              : 
    3459              :       /* Check that the second operand of the PLUS_EXPR is 1.  */
    3460          514 :       if (!integer_onep (gimple_assign_rhs2 (rshift_input_stmt)))
    3461              :         return NULL;
    3462              : 
    3463              :       /* Look through any change in sign on the addition input.  */
    3464          108 :       vect_unpromoted_value unprom_plus_input;
    3465          108 :       tree plus_input = vect_look_through_possible_promotion
    3466          108 :         (vinfo, gimple_assign_rhs1 (rshift_input_stmt), &unprom_plus_input);
    3467          108 :       if (!plus_input
    3468          108 :            || TYPE_PRECISION (TREE_TYPE (plus_input))
    3469          108 :                 != TYPE_PRECISION (TREE_TYPE (rshift_input)))
    3470              :         return NULL;
    3471              : 
    3472              :       /* Get the definition of the multiply-high-scale part.  */
    3473           95 :       stmt_vec_info plus_input_stmt_info
    3474           95 :         = vect_get_internal_def (vinfo, plus_input);
    3475           95 :       if (!plus_input_stmt_info)
    3476              :         return NULL;
    3477           95 :       gassign *plus_input_stmt
    3478         8797 :         = dyn_cast <gassign *> (plus_input_stmt_info->stmt);
    3479           95 :       if (!plus_input_stmt
    3480           95 :           || gimple_assign_rhs_code (plus_input_stmt) != RSHIFT_EXPR)
    3481              :         return NULL;
    3482              : 
    3483              :       /* Look through any change in sign on the scaling input.  */
    3484           67 :       vect_unpromoted_value unprom_scale_input;
    3485           67 :       tree scale_input = vect_look_through_possible_promotion
    3486           67 :         (vinfo, gimple_assign_rhs1 (plus_input_stmt), &unprom_scale_input);
    3487           67 :       if (!scale_input
    3488           67 :           || TYPE_PRECISION (TREE_TYPE (scale_input))
    3489           67 :                != TYPE_PRECISION (TREE_TYPE (plus_input)))
    3490              :         return NULL;
    3491              : 
    3492              :       /* Get the definition of the multiply-high part.  */
    3493           67 :       mulh_stmt_info = vect_get_internal_def (vinfo, scale_input);
    3494           67 :       if (!mulh_stmt_info)
    3495              :         return NULL;
    3496              : 
    3497              :       /* Get the scaling term.  */
    3498           67 :       scale_term = gimple_assign_rhs2 (plus_input_stmt);
    3499           67 :       rounding_p = true;
    3500              :     }
    3501              :   else
    3502              :     {
    3503        42315 :       mulh_stmt_info = rshift_input_stmt_info;
    3504        42315 :       scale_term = gimple_assign_rhs2 (last_stmt);
    3505              :     }
    3506              : 
    3507              :   /* Check that the scaling factor is constant.  */
    3508        42382 :   if (TREE_CODE (scale_term) != INTEGER_CST)
    3509              :     return NULL;
    3510              : 
    3511              :   /* Check whether the scaling input term can be seen as two widened
    3512              :      inputs multiplied together.  */
    3513       124158 :   vect_unpromoted_value unprom_mult[2];
    3514        41386 :   tree new_type;
    3515        41386 :   unsigned int nops
    3516        41386 :     = vect_widened_op_tree (vinfo, mulh_stmt_info, MULT_EXPR, WIDEN_MULT_EXPR,
    3517              :                             false, 2, unprom_mult, &new_type);
    3518        41386 :   if (nops != 2)
    3519              :     return NULL;
    3520              : 
    3521              :   /* Adjust output precision.  */
    3522        10874 :   if (TYPE_PRECISION (new_type) < target_precision)
    3523            0 :     new_type = build_nonstandard_integer_type
    3524            0 :       (target_precision, TYPE_UNSIGNED (new_type));
    3525              : 
    3526        10874 :   unsigned mult_precision = TYPE_PRECISION (new_type);
    3527        10874 :   internal_fn ifn;
    3528              :   /* Check that the scaling factor is expected.  Instead of
    3529              :      target_precision, we should use the one that we actually
    3530              :      use for internal function.  */
    3531        10874 :   if (rounding_p)
    3532              :     {
    3533              :       /* Check pattern 2).  */
    3534          134 :       if (wi::to_widest (scale_term) + mult_precision + 2
    3535          201 :           != TYPE_PRECISION (lhs_type))
    3536              :         return NULL;
    3537              : 
    3538              :       ifn = IFN_MULHRS;
    3539              :     }
    3540              :   else
    3541              :     {
    3542              :       /* Check for pattern 1).  */
    3543        21614 :       if (wi::to_widest (scale_term) + mult_precision + 1
    3544        32421 :           == TYPE_PRECISION (lhs_type))
    3545              :         ifn = IFN_MULHS;
    3546              :       /* Check for pattern 3).  */
    3547        10773 :       else if (wi::to_widest (scale_term) + mult_precision
    3548        21546 :                == TYPE_PRECISION (lhs_type))
    3549              :         ifn = IFN_MULH;
    3550              :       else
    3551              :         return NULL;
    3552              :     }
    3553              : 
    3554        10785 :   vect_pattern_detected ("vect_recog_mulhs_pattern", last_stmt);
    3555              : 
    3556              :   /* Check for target support.  */
    3557        10785 :   tree new_vectype = get_vectype_for_scalar_type (vinfo, new_type);
    3558        10785 :   if (!new_vectype
    3559        18194 :       || !direct_internal_fn_supported_p
    3560         7409 :             (ifn, new_vectype, OPTIMIZE_FOR_SPEED))
    3561              :     return NULL;
    3562              : 
    3563              :   /* The result is cast back to LHS_TYPE, a cast that the over-widening
    3564              :      machinery then removes.  LHS_TYPE need not have a vector type, as for the
    3565              :      128-bit product of a 64-bit high-part multiply, so leave *TYPE_OUT null
    3566              :      rather than giving up.  */
    3567         4581 :   *type_out = get_vectype_for_scalar_type (vinfo, lhs_type);
    3568              : 
    3569              :   /* Generate the IFN_MULHRS call.  */
    3570         4581 :   tree new_var = vect_recog_temp_ssa_var (new_type, NULL);
    3571         4581 :   tree new_ops[2];
    3572         4581 :   vect_convert_inputs (vinfo, last_stmt_info, 2, new_ops, new_type,
    3573              :                        unprom_mult, new_vectype);
    3574         4581 :   gcall *mulhrs_stmt
    3575         4581 :     = gimple_build_call_internal (ifn, 2, new_ops[0], new_ops[1]);
    3576         4581 :   gimple_call_set_lhs (mulhrs_stmt, new_var);
    3577         4581 :   gimple_set_location (mulhrs_stmt, gimple_location (last_stmt));
    3578              : 
    3579         4581 :   if (dump_enabled_p ())
    3580            3 :     dump_printf_loc (MSG_NOTE, vect_location,
    3581              :                      "created pattern stmt: %G", (gimple *) mulhrs_stmt);
    3582              : 
    3583         4581 :   return vect_convert_output (vinfo, last_stmt_info, lhs_type,
    3584         4581 :                               mulhrs_stmt, new_vectype);
    3585              : }
    3586              : 
    3587              : /* Recognize the patterns:
    3588              : 
    3589              :             ATYPE a;  // narrower than TYPE
    3590              :             BTYPE b;  // narrower than TYPE
    3591              :         (1) TYPE avg = ((TYPE) a + (TYPE) b) >> 1;
    3592              :      or (2) TYPE avg = ((TYPE) a + (TYPE) b + 1) >> 1;
    3593              : 
    3594              :    where only the bottom half of avg is used.  Try to transform them into:
    3595              : 
    3596              :         (1) NTYPE avg' = .AVG_FLOOR ((NTYPE) a, (NTYPE) b);
    3597              :      or (2) NTYPE avg' = .AVG_CEIL ((NTYPE) a, (NTYPE) b);
    3598              : 
    3599              :   followed by:
    3600              : 
    3601              :             TYPE avg = (TYPE) avg';
    3602              : 
    3603              :   where NTYPE is no wider than half of TYPE.  Since only the bottom half
    3604              :   of avg is used, all or part of the cast of avg' should become redundant.
    3605              : 
    3606              :   If there is no target support available, generate code to distribute rshift
    3607              :   over plus and add a carry.  */
    3608              : 
    3609              : static gimple *
    3610     33556775 : vect_recog_average_pattern (vec_info *vinfo,
    3611              :                             stmt_vec_info last_stmt_info, tree *type_out)
    3612              : {
    3613              :   /* Check for a shift right by one bit.  */
    3614     33556775 :   gassign *last_stmt = dyn_cast <gassign *> (last_stmt_info->stmt);
    3615     23576697 :   if (!last_stmt
    3616     23576697 :       || gimple_assign_rhs_code (last_stmt) != RSHIFT_EXPR
    3617       377436 :       || !integer_onep (gimple_assign_rhs2 (last_stmt)))
    3618              :     return NULL;
    3619              : 
    3620              :   /* Check that the shift result is wider than the users of the
    3621              :      result need (i.e. that narrowing would be a natural choice).  */
    3622        59930 :   tree lhs = gimple_assign_lhs (last_stmt);
    3623        59930 :   tree type = TREE_TYPE (lhs);
    3624        59930 :   unsigned int target_precision
    3625        59930 :     = vect_element_precision (last_stmt_info->min_output_precision);
    3626        59930 :   if (!INTEGRAL_TYPE_P (type) || target_precision >= TYPE_PRECISION (type))
    3627              :     return NULL;
    3628              : 
    3629              :   /* Look through any change in sign on the shift input.  */
    3630         2289 :   tree rshift_rhs = gimple_assign_rhs1 (last_stmt);
    3631         2289 :   vect_unpromoted_value unprom_plus;
    3632         2289 :   rshift_rhs = vect_look_through_possible_promotion (vinfo, rshift_rhs,
    3633              :                                                      &unprom_plus);
    3634         2289 :   if (!rshift_rhs
    3635         2289 :       || TYPE_PRECISION (TREE_TYPE (rshift_rhs)) != TYPE_PRECISION (type))
    3636              :     return NULL;
    3637              : 
    3638              :   /* Get the definition of the shift input.  */
    3639         2283 :   stmt_vec_info plus_stmt_info = vect_get_internal_def (vinfo, rshift_rhs);
    3640         2283 :   if (!plus_stmt_info)
    3641              :     return NULL;
    3642              : 
    3643              :   /* Check whether the shift input can be seen as a tree of additions on
    3644              :      2 or 3 widened inputs.
    3645              : 
    3646              :      Note that the pattern should be a win even if the result of one or
    3647              :      more additions is reused elsewhere: if the pattern matches, we'd be
    3648              :      replacing 2N RSHIFT_EXPRs and N VEC_PACK_*s with N IFN_AVG_*s.  */
    3649         2268 :   internal_fn ifn = IFN_AVG_FLOOR;
    3650         9072 :   vect_unpromoted_value unprom[3];
    3651         2268 :   tree new_type;
    3652         2268 :   unsigned int nops = vect_widened_op_tree (vinfo, plus_stmt_info, PLUS_EXPR,
    3653         2268 :                                             IFN_VEC_WIDEN_PLUS, false, 3,
    3654              :                                             unprom, &new_type);
    3655         2268 :   if (nops == 0)
    3656              :     return NULL;
    3657          972 :   if (nops == 3)
    3658              :     {
    3659              :       /* Check that one operand is 1.  */
    3660              :       unsigned int i;
    3661         1155 :       for (i = 0; i < 3; ++i)
    3662         1101 :         if (integer_onep (unprom[i].op))
    3663              :           break;
    3664          367 :       if (i == 3)
    3665              :         return NULL;
    3666              :       /* Throw away the 1 operand and keep the other two.  */
    3667          313 :       if (i < 2)
    3668            0 :         unprom[i] = unprom[2];
    3669              :       ifn = IFN_AVG_CEIL;
    3670              :     }
    3671              : 
    3672          918 :   vect_pattern_detected ("vect_recog_average_pattern", last_stmt);
    3673              : 
    3674              :   /* We know that:
    3675              : 
    3676              :      (a) the operation can be viewed as:
    3677              : 
    3678              :            TYPE widened0 = (TYPE) UNPROM[0];
    3679              :            TYPE widened1 = (TYPE) UNPROM[1];
    3680              :            TYPE tmp1 = widened0 + widened1 {+ 1};
    3681              :            TYPE tmp2 = tmp1 >> 1;   // LAST_STMT_INFO
    3682              : 
    3683              :      (b) the first two statements are equivalent to:
    3684              : 
    3685              :            TYPE widened0 = (TYPE) (NEW_TYPE) UNPROM[0];
    3686              :            TYPE widened1 = (TYPE) (NEW_TYPE) UNPROM[1];
    3687              : 
    3688              :      (c) vect_recog_over_widening_pattern has already tried to narrow TYPE
    3689              :          where sensible;
    3690              : 
    3691              :      (d) all the operations can be performed correctly at twice the width of
    3692              :          NEW_TYPE, due to the nature of the average operation; and
    3693              : 
    3694              :      (e) users of the result of the right shift need only TARGET_PRECISION
    3695              :          bits, where TARGET_PRECISION is no more than half of TYPE's
    3696              :          precision.
    3697              : 
    3698              :      Under these circumstances, the only situation in which NEW_TYPE
    3699              :      could be narrower than TARGET_PRECISION is if widened0, widened1
    3700              :      and an addition result are all used more than once.  Thus we can
    3701              :      treat any widening of UNPROM[0] and UNPROM[1] to TARGET_PRECISION
    3702              :      as "free", whereas widening the result of the average instruction
    3703              :      from NEW_TYPE to TARGET_PRECISION would be a new operation.  It's
    3704              :      therefore better not to go narrower than TARGET_PRECISION.  */
    3705          918 :   if (TYPE_PRECISION (new_type) < target_precision)
    3706            0 :     new_type = build_nonstandard_integer_type (target_precision,
    3707            0 :                                                TYPE_UNSIGNED (new_type));
    3708              : 
    3709              :   /* Check for target support.  */
    3710          918 :   tree new_vectype = get_vectype_for_scalar_type (vinfo, new_type);
    3711          918 :   if (!new_vectype)
    3712              :     return NULL;
    3713              : 
    3714          918 :   bool fallback_p = !direct_internal_fn_supported_p (ifn, new_vectype,
    3715          918 :                                                       OPTIMIZE_FOR_SPEED);
    3716          918 :   if (fallback_p
    3717          918 :       && (!target_supports_op_p (new_vectype, RSHIFT_EXPR, optab_scalar)
    3718          697 :           || !target_supports_op_p (new_vectype, PLUS_EXPR, optab_default)
    3719          697 :           || !target_supports_op_p (new_vectype, BIT_AND_EXPR, optab_default)
    3720          697 :           || (ifn == IFN_AVG_CEIL
    3721          105 :               && !target_supports_op_p (new_vectype, BIT_IOR_EXPR,
    3722              :                                         optab_default))))
    3723              :     return NULL;
    3724              : 
    3725              :   /* The IR requires a valid vector type for the cast result, even though
    3726              :      it's likely to be discarded.  */
    3727          903 :   *type_out = get_vectype_for_scalar_type (vinfo, type);
    3728          903 :   if (!*type_out)
    3729              :     return NULL;
    3730              : 
    3731          899 :   tree new_var = vect_recog_temp_ssa_var (new_type, NULL);
    3732          899 :   tree new_ops[2];
    3733          899 :   vect_convert_inputs (vinfo, last_stmt_info, 2, new_ops, new_type,
    3734              :                        unprom, new_vectype);
    3735              : 
    3736          899 :   if (fallback_p)
    3737              :     {
    3738              :       /* As a fallback, generate code for following sequence:
    3739              : 
    3740              :          shifted_op0 = new_ops[0] >> 1;
    3741              :          shifted_op1 = new_ops[1] >> 1;
    3742              :          sum_of_shifted = shifted_op0 + shifted_op1;
    3743              :          unmasked_carry = new_ops[0] and/or new_ops[1];
    3744              :          carry = unmasked_carry & 1;
    3745              :          new_var = sum_of_shifted + carry;
    3746              : 
    3747              :          For signed types, arithmetic shifts round down and the carry is one
    3748              :          when both operands are odd (or either operand for IFN_AVG_CEIL).  */
    3749              : 
    3750          693 :       tree one_cst = build_one_cst (new_type);
    3751          693 :       gassign *g;
    3752              : 
    3753          693 :       tree shifted_op0 = vect_recog_temp_ssa_var (new_type, NULL);
    3754          693 :       g = gimple_build_assign (shifted_op0, RSHIFT_EXPR, new_ops[0], one_cst);
    3755          693 :       append_pattern_def_seq (vinfo, last_stmt_info, g, new_vectype);
    3756              : 
    3757          693 :       tree shifted_op1 = vect_recog_temp_ssa_var (new_type, NULL);
    3758          693 :       g = gimple_build_assign (shifted_op1, RSHIFT_EXPR, new_ops[1], one_cst);
    3759          693 :       append_pattern_def_seq (vinfo, last_stmt_info, g, new_vectype);
    3760              : 
    3761          693 :       tree sum_of_shifted = vect_recog_temp_ssa_var (new_type, NULL);
    3762          693 :       g = gimple_build_assign (sum_of_shifted, PLUS_EXPR,
    3763              :                                shifted_op0, shifted_op1);
    3764          693 :       append_pattern_def_seq (vinfo, last_stmt_info, g, new_vectype);
    3765              : 
    3766          693 :       tree unmasked_carry = vect_recog_temp_ssa_var (new_type, NULL);
    3767          693 :       tree_code c = (ifn == IFN_AVG_CEIL) ? BIT_IOR_EXPR : BIT_AND_EXPR;
    3768          693 :       g = gimple_build_assign (unmasked_carry, c, new_ops[0], new_ops[1]);
    3769          693 :       append_pattern_def_seq (vinfo, last_stmt_info, g, new_vectype);
    3770              : 
    3771          693 :       tree carry = vect_recog_temp_ssa_var (new_type, NULL);
    3772          693 :       g = gimple_build_assign (carry, BIT_AND_EXPR, unmasked_carry, one_cst);
    3773          693 :       append_pattern_def_seq (vinfo, last_stmt_info, g, new_vectype);
    3774              : 
    3775          693 :       g = gimple_build_assign (new_var, PLUS_EXPR, sum_of_shifted, carry);
    3776          693 :       return vect_convert_output (vinfo, last_stmt_info, type, g, new_vectype);
    3777              :     }
    3778              : 
    3779              :   /* Generate the IFN_AVG* call.  */
    3780          206 :   gcall *average_stmt = gimple_build_call_internal (ifn, 2, new_ops[0],
    3781              :                                                     new_ops[1]);
    3782          206 :   gimple_call_set_lhs (average_stmt, new_var);
    3783          206 :   gimple_set_location (average_stmt, gimple_location (last_stmt));
    3784              : 
    3785          206 :   if (dump_enabled_p ())
    3786           79 :     dump_printf_loc (MSG_NOTE, vect_location,
    3787              :                      "created pattern stmt: %G", (gimple *) average_stmt);
    3788              : 
    3789          206 :   return vect_convert_output (vinfo, last_stmt_info,
    3790          206 :                               type, average_stmt, new_vectype);
    3791              : }
    3792              : 
    3793              : /* Recognize cases in which the input to a cast is wider than its
    3794              :    output, and the input is fed by a widening operation.  Fold this
    3795              :    by removing the unnecessary intermediate widening.  E.g.:
    3796              : 
    3797              :      unsigned char a;
    3798              :      unsigned int b = (unsigned int) a;
    3799              :      unsigned short c = (unsigned short) b;
    3800              : 
    3801              :    -->
    3802              : 
    3803              :      unsigned short c = (unsigned short) a;
    3804              : 
    3805              :    Although this is rare in input IR, it is an expected side-effect
    3806              :    of the over-widening pattern above.
    3807              : 
    3808              :    This is beneficial also for integer-to-float conversions, if the
    3809              :    widened integer has more bits than the float, and if the unwidened
    3810              :    input doesn't.  */
    3811              : 
    3812              : static gimple *
    3813     33561012 : vect_recog_cast_forwprop_pattern (vec_info *vinfo,
    3814              :                                   stmt_vec_info last_stmt_info, tree *type_out)
    3815              : {
    3816              :   /* Check for a cast, including an integer-to-float conversion.  */
    3817     33561012 :   gassign *last_stmt = dyn_cast <gassign *> (last_stmt_info->stmt);
    3818     23576147 :   if (!last_stmt)
    3819              :     return NULL;
    3820     23576147 :   tree_code code = gimple_assign_rhs_code (last_stmt);
    3821     23576147 :   if (!CONVERT_EXPR_CODE_P (code) && code != FLOAT_EXPR)
    3822              :     return NULL;
    3823              : 
    3824              :   /* Make sure that the rhs is a scalar with a natural bitsize.  */
    3825      3438859 :   tree lhs = gimple_assign_lhs (last_stmt);
    3826      3438859 :   if (!lhs)
    3827              :     return NULL;
    3828      3438859 :   tree lhs_type = TREE_TYPE (lhs);
    3829      3438859 :   scalar_mode lhs_mode;
    3830      3417802 :   if (VECT_SCALAR_BOOLEAN_TYPE_P (lhs_type)
    3831      6854869 :       || !is_a <scalar_mode> (TYPE_MODE (lhs_type), &lhs_mode))
    3832              :     return NULL;
    3833              : 
    3834              :   /* Check for a narrowing operation (from a vector point of view).  */
    3835      3412214 :   tree rhs = gimple_assign_rhs1 (last_stmt);
    3836      3412214 :   tree rhs_type = TREE_TYPE (rhs);
    3837      3412214 :   if (!INTEGRAL_TYPE_P (rhs_type)
    3838      2990511 :       || VECT_SCALAR_BOOLEAN_TYPE_P (rhs_type)
    3839      9233794 :       || TYPE_PRECISION (rhs_type) <= GET_MODE_BITSIZE (lhs_mode))
    3840              :     return NULL;
    3841              : 
    3842              :   /* Try to find an unpromoted input.  */
    3843       403190 :   vect_unpromoted_value unprom;
    3844       403190 :   if (!vect_look_through_possible_promotion (vinfo, rhs, &unprom)
    3845       403190 :       || TYPE_PRECISION (unprom.type) >= TYPE_PRECISION (rhs_type))
    3846              :     return NULL;
    3847              : 
    3848              :   /* If the bits above RHS_TYPE matter, make sure that they're the
    3849              :      same when extending from UNPROM as they are when extending from RHS.  */
    3850        53833 :   if (!INTEGRAL_TYPE_P (lhs_type)
    3851        53833 :       && TYPE_SIGN (rhs_type) != TYPE_SIGN (unprom.type))
    3852              :     return NULL;
    3853              : 
    3854              :   /* We can get the same result by casting UNPROM directly, to avoid
    3855              :      the unnecessary widening and narrowing.  */
    3856        53713 :   vect_pattern_detected ("vect_recog_cast_forwprop_pattern", last_stmt);
    3857              : 
    3858        53713 :   *type_out = get_vectype_for_scalar_type (vinfo, lhs_type);
    3859        53713 :   if (!*type_out)
    3860              :     return NULL;
    3861              : 
    3862        53713 :   tree new_var = vect_recog_temp_ssa_var (lhs_type, NULL);
    3863        53713 :   gimple *pattern_stmt = gimple_build_assign (new_var, code, unprom.op);
    3864        53713 :   gimple_set_location (pattern_stmt, gimple_location (last_stmt));
    3865              : 
    3866        53713 :   return pattern_stmt;
    3867              : }
    3868              : 
    3869              : /* Try to detect a shift left of a widened input, converting LSHIFT_EXPR
    3870              :    to WIDEN_LSHIFT_EXPR.  See vect_recog_widen_op_pattern for details.  */
    3871              : 
    3872              : static gimple *
    3873     33477966 : vect_recog_widen_shift_pattern (vec_info *vinfo,
    3874              :                                 stmt_vec_info last_stmt_info, tree *type_out)
    3875              : {
    3876     33477966 :   return vect_recog_widen_op_pattern (vinfo, last_stmt_info, type_out,
    3877     33477966 :                                       LSHIFT_EXPR, WIDEN_LSHIFT_EXPR, true,
    3878     33477966 :                                       "vect_recog_widen_shift_pattern");
    3879              : }
    3880              : 
    3881              : /* Detect a rotate pattern wouldn't be otherwise vectorized:
    3882              : 
    3883              :    type a_t, b_t, c_t;
    3884              : 
    3885              :    S0 a_t = b_t r<< c_t;
    3886              : 
    3887              :   Input/Output:
    3888              : 
    3889              :   * STMT_VINFO: The stmt from which the pattern search begins,
    3890              :     i.e. the shift/rotate stmt.  The original stmt (S0) is replaced
    3891              :     with a sequence:
    3892              : 
    3893              :    S1 d_t = -c_t;
    3894              :    S2 e_t = d_t & (B - 1);
    3895              :    S3 f_t = b_t << c_t;
    3896              :    S4 g_t = b_t >> e_t;
    3897              :    S0 a_t = f_t | g_t;
    3898              : 
    3899              :     where B is element bitsize of type.
    3900              : 
    3901              :   Output:
    3902              : 
    3903              :   * TYPE_OUT: The type of the output of this pattern.
    3904              : 
    3905              :   * Return value: A new stmt that will be used to replace the rotate
    3906              :     S0 stmt.  */
    3907              : 
    3908              : static gimple *
    3909     33477966 : vect_recog_rotate_pattern (vec_info *vinfo,
    3910              :                            stmt_vec_info stmt_vinfo, tree *type_out)
    3911              : {
    3912     33477966 :   gimple *last_stmt = stmt_vinfo->stmt;
    3913     33477966 :   tree oprnd0, oprnd1, lhs, var, var1, var2, vectype, type, stype, def, def2;
    3914     33477966 :   gimple *pattern_stmt, *def_stmt;
    3915     33477966 :   enum tree_code rhs_code;
    3916     33477966 :   enum vect_def_type dt;
    3917     33477966 :   optab optab1, optab2;
    3918     33477966 :   edge ext_def = NULL;
    3919     33477966 :   bool bswap16_p = false;
    3920              : 
    3921     33477966 :   if (is_gimple_assign (last_stmt))
    3922              :     {
    3923     23493018 :       rhs_code = gimple_assign_rhs_code (last_stmt);
    3924     23493018 :       switch (rhs_code)
    3925              :         {
    3926         8586 :         case LROTATE_EXPR:
    3927         8586 :         case RROTATE_EXPR:
    3928         8586 :           break;
    3929              :         default:
    3930              :           return NULL;
    3931              :         }
    3932              : 
    3933         8586 :       lhs = gimple_assign_lhs (last_stmt);
    3934         8586 :       oprnd0 = gimple_assign_rhs1 (last_stmt);
    3935         8586 :       type = TREE_TYPE (oprnd0);
    3936         8586 :       oprnd1 = gimple_assign_rhs2 (last_stmt);
    3937              :     }
    3938      9984948 :   else if (gimple_call_builtin_p (last_stmt, BUILT_IN_BSWAP16))
    3939              :     {
    3940              :       /* __builtin_bswap16 (x) is another form of x r>> 8.
    3941              :          The vectorizer has bswap support, but only if the argument isn't
    3942              :          promoted.  */
    3943          285 :       lhs = gimple_call_lhs (last_stmt);
    3944          285 :       oprnd0 = gimple_call_arg (last_stmt, 0);
    3945          285 :       type = TREE_TYPE (oprnd0);
    3946          285 :       if (!lhs
    3947          285 :           || TYPE_PRECISION (TREE_TYPE (lhs)) != 16
    3948          285 :           || TYPE_PRECISION (type) <= 16
    3949            0 :           || TREE_CODE (oprnd0) != SSA_NAME
    3950          285 :           || BITS_PER_UNIT != 8)
    3951          285 :         return NULL;
    3952              : 
    3953            0 :       stmt_vec_info def_stmt_info;
    3954            0 :       if (!vect_is_simple_use (oprnd0, vinfo, &dt, &def_stmt_info, &def_stmt))
    3955              :         return NULL;
    3956              : 
    3957            0 :       if (dt != vect_internal_def)
    3958              :         return NULL;
    3959              : 
    3960            0 :       if (gimple_assign_cast_p (def_stmt))
    3961              :         {
    3962            0 :           def = gimple_assign_rhs1 (def_stmt);
    3963            0 :           if (INTEGRAL_TYPE_P (TREE_TYPE (def))
    3964            0 :               && TYPE_PRECISION (TREE_TYPE (def)) == 16)
    3965              :             oprnd0 = def;
    3966              :         }
    3967              : 
    3968            0 :       type = TREE_TYPE (lhs);
    3969            0 :       vectype = get_vectype_for_scalar_type (vinfo, type);
    3970            0 :       if (vectype == NULL_TREE)
    3971              :         return NULL;
    3972              : 
    3973            0 :       if (tree char_vectype = get_same_sized_vectype (char_type_node, vectype))
    3974              :         {
    3975              :           /* The encoding uses one stepped pattern for each byte in the
    3976              :              16-bit word.  */
    3977            0 :           vec_perm_builder elts (TYPE_VECTOR_SUBPARTS (char_vectype), 2, 3);
    3978            0 :           for (unsigned i = 0; i < 3; ++i)
    3979            0 :             for (unsigned j = 0; j < 2; ++j)
    3980            0 :               elts.quick_push ((i + 1) * 2 - j - 1);
    3981              : 
    3982            0 :           vec_perm_indices indices (elts, 1,
    3983            0 :                                     TYPE_VECTOR_SUBPARTS (char_vectype));
    3984            0 :           machine_mode vmode = TYPE_MODE (char_vectype);
    3985            0 :           if (can_vec_perm_const_p (vmode, vmode, indices))
    3986              :             {
    3987              :               /* vectorizable_bswap can handle the __builtin_bswap16 if we
    3988              :                  undo the argument promotion.  */
    3989            0 :               if (!useless_type_conversion_p (type, TREE_TYPE (oprnd0)))
    3990              :                 {
    3991            0 :                   def = vect_recog_temp_ssa_var (type, NULL);
    3992            0 :                   def_stmt = gimple_build_assign (def, NOP_EXPR, oprnd0);
    3993            0 :                   append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    3994            0 :                   oprnd0 = def;
    3995              :                 }
    3996              : 
    3997              :               /* Pattern detected.  */
    3998            0 :               vect_pattern_detected ("vect_recog_rotate_pattern", last_stmt);
    3999              : 
    4000            0 :               *type_out = vectype;
    4001              : 
    4002              :               /* Pattern supported.  Create a stmt to be used to replace the
    4003              :                  pattern, with the unpromoted argument.  */
    4004            0 :               var = vect_recog_temp_ssa_var (type, NULL);
    4005            0 :               pattern_stmt = gimple_build_call (gimple_call_fndecl (last_stmt),
    4006              :                                                 1, oprnd0);
    4007            0 :               gimple_call_set_lhs (pattern_stmt, var);
    4008            0 :               gimple_call_set_fntype (as_a <gcall *> (pattern_stmt),
    4009              :                                       gimple_call_fntype (last_stmt));
    4010            0 :               return pattern_stmt;
    4011              :             }
    4012            0 :         }
    4013              : 
    4014            0 :       oprnd1 = build_int_cst (integer_type_node, 8);
    4015            0 :       rhs_code = LROTATE_EXPR;
    4016            0 :       bswap16_p = true;
    4017              :     }
    4018              :   else
    4019              :     return NULL;
    4020              : 
    4021         8586 :   if (TREE_CODE (oprnd0) != SSA_NAME
    4022         8466 :       || !INTEGRAL_TYPE_P (type)
    4023        16731 :       || TYPE_PRECISION (TREE_TYPE (lhs)) != TYPE_PRECISION (type))
    4024              :     return NULL;
    4025              : 
    4026         8145 :   stmt_vec_info def_stmt_info;
    4027         8145 :   if (!vect_is_simple_use (oprnd1, vinfo, &dt, &def_stmt_info, &def_stmt))
    4028              :     return NULL;
    4029              : 
    4030         8145 :   if (dt != vect_internal_def
    4031         7078 :       && dt != vect_constant_def
    4032           25 :       && dt != vect_external_def)
    4033              :     return NULL;
    4034              : 
    4035         8139 :   vectype = get_vectype_for_scalar_type (vinfo, type);
    4036         8139 :   if (vectype == NULL_TREE)
    4037              :     return NULL;
    4038              : 
    4039              :   /* If vector/vector or vector/scalar rotate is supported by the target,
    4040              :      don't do anything here.  */
    4041         7772 :   optab1 = optab_for_tree_code (rhs_code, vectype, optab_vector);
    4042         7772 :   if (optab1
    4043         7772 :       && can_implement_p (optab1, TYPE_MODE (vectype)))
    4044              :     {
    4045          564 :      use_rotate:
    4046          564 :       if (bswap16_p)
    4047              :         {
    4048            0 :           if (!useless_type_conversion_p (type, TREE_TYPE (oprnd0)))
    4049              :             {
    4050            0 :               def = vect_recog_temp_ssa_var (type, NULL);
    4051            0 :               def_stmt = gimple_build_assign (def, NOP_EXPR, oprnd0);
    4052            0 :               append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    4053            0 :               oprnd0 = def;
    4054              :             }
    4055              : 
    4056              :           /* Pattern detected.  */
    4057            0 :           vect_pattern_detected ("vect_recog_rotate_pattern", last_stmt);
    4058              : 
    4059            0 :           *type_out = vectype;
    4060              : 
    4061              :           /* Pattern supported.  Create a stmt to be used to replace the
    4062              :              pattern.  */
    4063            0 :           var = vect_recog_temp_ssa_var (type, NULL);
    4064            0 :           pattern_stmt = gimple_build_assign (var, LROTATE_EXPR, oprnd0,
    4065              :                                               oprnd1);
    4066            0 :           return pattern_stmt;
    4067              :         }
    4068              :       return NULL;
    4069              :     }
    4070              : 
    4071         7748 :   if (is_a <bb_vec_info> (vinfo) || dt != vect_internal_def)
    4072              :     {
    4073         7664 :       optab2 = optab_for_tree_code (rhs_code, vectype, optab_scalar);
    4074         7664 :       if (optab2
    4075         7664 :           && can_implement_p (optab2, TYPE_MODE (vectype)))
    4076          540 :         goto use_rotate;
    4077              :     }
    4078              : 
    4079              :   /* We may not use a reduction operand twice.  */
    4080         7208 :   if (vect_is_reduction (stmt_vinfo))
    4081              :     return NULL;
    4082              : 
    4083         7187 :   tree utype = unsigned_type_for (type);
    4084         7187 :   tree uvectype = get_vectype_for_scalar_type (vinfo, utype);
    4085         7187 :   if (!uvectype)
    4086              :     return NULL;
    4087              : 
    4088              :   /* If vector/vector or vector/scalar shifts aren't supported by the target,
    4089              :      don't do anything here either.  */
    4090         7187 :   optab1 = optab_for_tree_code (LSHIFT_EXPR, uvectype, optab_vector);
    4091         7187 :   optab2 = optab_for_tree_code (RSHIFT_EXPR, uvectype, optab_vector);
    4092         7187 :   if (!optab1
    4093         7187 :       || !can_implement_p (optab1, TYPE_MODE (uvectype))
    4094          746 :       || !optab2
    4095         7933 :       || !can_implement_p (optab2, TYPE_MODE (uvectype)))
    4096              :     {
    4097         6441 :       if (! is_a <bb_vec_info> (vinfo) && dt == vect_internal_def)
    4098              :         return NULL;
    4099         6378 :       optab1 = optab_for_tree_code (LSHIFT_EXPR, uvectype, optab_scalar);
    4100         6378 :       optab2 = optab_for_tree_code (RSHIFT_EXPR, uvectype, optab_scalar);
    4101         6378 :       if (!optab1
    4102         6378 :           || !can_implement_p (optab1, TYPE_MODE (uvectype))
    4103         4728 :           || !optab2
    4104        11106 :           || !can_implement_p (optab2, TYPE_MODE (uvectype)))
    4105              :         return NULL;
    4106              :     }
    4107              : 
    4108         5474 :   *type_out = vectype;
    4109              : 
    4110         5474 :   if (!useless_type_conversion_p (utype, TREE_TYPE (oprnd0)))
    4111              :     {
    4112           58 :       def = vect_recog_temp_ssa_var (utype, NULL);
    4113           58 :       def_stmt = gimple_build_assign (def, NOP_EXPR, oprnd0);
    4114           58 :       append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, uvectype);
    4115           58 :       oprnd0 = def;
    4116              :     }
    4117              : 
    4118         5474 :   if (dt == vect_external_def && TREE_CODE (oprnd1) == SSA_NAME)
    4119           15 :     ext_def = vect_get_external_def_edge (vinfo, oprnd1);
    4120              : 
    4121         5474 :   def = NULL_TREE;
    4122         5474 :   scalar_int_mode mode = SCALAR_INT_TYPE_MODE (utype);
    4123         5474 :   if (dt != vect_internal_def || TYPE_MODE (TREE_TYPE (oprnd1)) == mode)
    4124              :     def = oprnd1;
    4125          124 :   else if (def_stmt && gimple_assign_cast_p (def_stmt))
    4126              :     {
    4127           96 :       tree rhs1 = gimple_assign_rhs1 (def_stmt);
    4128           96 :       if (TYPE_MODE (TREE_TYPE (rhs1)) == mode
    4129           96 :           && TYPE_PRECISION (TREE_TYPE (rhs1))
    4130            0 :              == TYPE_PRECISION (type))
    4131              :         def = rhs1;
    4132              :     }
    4133              : 
    4134         5350 :   if (def == NULL_TREE)
    4135              :     {
    4136          124 :       def = vect_recog_temp_ssa_var (utype, NULL);
    4137          124 :       def_stmt = gimple_build_assign (def, NOP_EXPR, oprnd1);
    4138          124 :       append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, uvectype);
    4139              :     }
    4140         5474 :   stype = TREE_TYPE (def);
    4141              : 
    4142         5474 :   if (TREE_CODE (def) == INTEGER_CST)
    4143              :     {
    4144         4896 :       if (!tree_fits_uhwi_p (def)
    4145         4896 :           || tree_to_uhwi (def) >= GET_MODE_PRECISION (mode)
    4146         9792 :           || integer_zerop (def))
    4147              :         return NULL;
    4148         4896 :       def2 = build_int_cst (stype,
    4149         4896 :                             GET_MODE_PRECISION (mode) - tree_to_uhwi (def));
    4150              :     }
    4151              :   else
    4152              :     {
    4153          578 :       tree vecstype = get_vectype_for_scalar_type (vinfo, stype);
    4154              : 
    4155          578 :       if (vecstype == NULL_TREE)
    4156              :         return NULL;
    4157          578 :       def2 = vect_recog_temp_ssa_var (stype, NULL);
    4158          578 :       def_stmt = gimple_build_assign (def2, NEGATE_EXPR, def);
    4159          578 :       if (ext_def)
    4160              :         {
    4161           15 :           basic_block new_bb
    4162           15 :             = gsi_insert_on_edge_immediate (ext_def, def_stmt);
    4163           15 :           gcc_assert (!new_bb);
    4164              :         }
    4165              :       else
    4166          563 :         append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vecstype);
    4167              : 
    4168          578 :       def2 = vect_recog_temp_ssa_var (stype, NULL);
    4169          578 :       tree mask = build_int_cst (stype, GET_MODE_PRECISION (mode) - 1);
    4170          578 :       def_stmt = gimple_build_assign (def2, BIT_AND_EXPR,
    4171              :                                       gimple_assign_lhs (def_stmt), mask);
    4172          578 :       if (ext_def)
    4173              :         {
    4174           15 :           basic_block new_bb
    4175           15 :             = gsi_insert_on_edge_immediate (ext_def, def_stmt);
    4176           15 :           gcc_assert (!new_bb);
    4177              :         }
    4178              :       else
    4179          563 :         append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vecstype);
    4180              :     }
    4181              : 
    4182         5474 :   var1 = vect_recog_temp_ssa_var (utype, NULL);
    4183        10637 :   def_stmt = gimple_build_assign (var1, rhs_code == LROTATE_EXPR
    4184              :                                         ? LSHIFT_EXPR : RSHIFT_EXPR,
    4185              :                                   oprnd0, def);
    4186         5474 :   append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, uvectype);
    4187              : 
    4188         5474 :   var2 = vect_recog_temp_ssa_var (utype, NULL);
    4189        10637 :   def_stmt = gimple_build_assign (var2, rhs_code == LROTATE_EXPR
    4190              :                                         ? RSHIFT_EXPR : LSHIFT_EXPR,
    4191              :                                   oprnd0, def2);
    4192         5474 :   append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, uvectype);
    4193              : 
    4194              :   /* Pattern detected.  */
    4195         5474 :   vect_pattern_detected ("vect_recog_rotate_pattern", last_stmt);
    4196              : 
    4197              :   /* Pattern supported.  Create a stmt to be used to replace the pattern.  */
    4198         5474 :   var = vect_recog_temp_ssa_var (utype, NULL);
    4199         5474 :   pattern_stmt = gimple_build_assign (var, BIT_IOR_EXPR, var1, var2);
    4200              : 
    4201         5474 :   if (!useless_type_conversion_p (type, utype))
    4202              :     {
    4203           58 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, uvectype);
    4204           58 :       tree result = vect_recog_temp_ssa_var (type, NULL);
    4205           58 :       pattern_stmt = gimple_build_assign (result, NOP_EXPR, var);
    4206              :     }
    4207              :   return pattern_stmt;
    4208              : }
    4209              : 
    4210              : /* Detect a vector by vector shift pattern that wouldn't be otherwise
    4211              :    vectorized:
    4212              : 
    4213              :    type a_t;
    4214              :    TYPE b_T, res_T;
    4215              : 
    4216              :    S1 a_t = ;
    4217              :    S2 b_T = ;
    4218              :    S3 res_T = b_T op a_t;
    4219              : 
    4220              :   where type 'TYPE' is a type with different size than 'type',
    4221              :   and op is <<, >> or rotate.
    4222              : 
    4223              :   Also detect cases:
    4224              : 
    4225              :    type a_t;
    4226              :    TYPE b_T, c_T, res_T;
    4227              : 
    4228              :    S0 c_T = ;
    4229              :    S1 a_t = (type) c_T;
    4230              :    S2 b_T = ;
    4231              :    S3 res_T = b_T op a_t;
    4232              : 
    4233              :   Input/Output:
    4234              : 
    4235              :   * STMT_VINFO: The stmt from which the pattern search begins,
    4236              :     i.e. the shift/rotate stmt.  The original stmt (S3) is replaced
    4237              :     with a shift/rotate which has same type on both operands, in the
    4238              :     second case just b_T op c_T, in the first case with added cast
    4239              :     from a_t to c_T in STMT_VINFO_PATTERN_DEF_SEQ.
    4240              : 
    4241              :   Output:
    4242              : 
    4243              :   * TYPE_OUT: The type of the output of this pattern.
    4244              : 
    4245              :   * Return value: A new stmt that will be used to replace the shift/rotate
    4246              :     S3 stmt.  */
    4247              : 
    4248              : static gimple *
    4249     33484806 : vect_recog_vector_vector_shift_pattern (vec_info *vinfo,
    4250              :                                         stmt_vec_info stmt_vinfo,
    4251              :                                         tree *type_out)
    4252              : {
    4253     33484806 :   auto last_stmt = dyn_cast<gassign *> (stmt_vinfo->stmt);
    4254     23499858 :   tree oprnd0, oprnd1, lhs, var;
    4255     23499858 :   gimple *pattern_stmt;
    4256     23499858 :   enum tree_code rhs_code;
    4257              : 
    4258     23499858 :   if (!last_stmt)
    4259              :     return NULL;
    4260              : 
    4261     23499858 :   rhs_code = gimple_assign_rhs_code (last_stmt);
    4262     23499858 :   switch (rhs_code)
    4263              :     {
    4264       525198 :     case LSHIFT_EXPR:
    4265       525198 :     case RSHIFT_EXPR:
    4266       525198 :     case LROTATE_EXPR:
    4267       525198 :     case RROTATE_EXPR:
    4268       525198 :       break;
    4269              :     default:
    4270              :       return NULL;
    4271              :     }
    4272              : 
    4273       525198 :   lhs = gimple_assign_lhs (last_stmt);
    4274       525198 :   oprnd0 = gimple_assign_rhs1 (last_stmt);
    4275       525198 :   oprnd1 = gimple_assign_rhs2 (last_stmt);
    4276       525198 :   if (TREE_CODE (oprnd1) != SSA_NAME
    4277       112143 :       || TYPE_MODE (TREE_TYPE (oprnd0)) == TYPE_MODE (TREE_TYPE (oprnd1))
    4278        49040 :       || !INTEGRAL_TYPE_P (TREE_TYPE (oprnd0))
    4279        48419 :       || !type_has_mode_precision_p (TREE_TYPE (oprnd1))
    4280       573617 :       || TYPE_PRECISION (TREE_TYPE (lhs))
    4281        48419 :          != TYPE_PRECISION (TREE_TYPE (oprnd0)))
    4282              :     return NULL;
    4283              : 
    4284        48419 :   stmt_vec_info def_vinfo = vinfo->lookup_def (oprnd1);
    4285        48419 :   if (!def_vinfo || STMT_VINFO_DEF_TYPE (def_vinfo) == vect_external_def)
    4286              :     return NULL;
    4287              : 
    4288        46432 :   def_vinfo = vect_stmt_to_vectorize (def_vinfo);
    4289         1114 :   gcc_assert (def_vinfo);
    4290              : 
    4291        46432 :   *type_out = get_vectype_for_scalar_type (vinfo, TREE_TYPE (oprnd0));
    4292        46432 :   if (*type_out == NULL_TREE)
    4293              :     return NULL;
    4294              : 
    4295        33282 :   tree def = NULL_TREE;
    4296        33282 :   gassign *def_stmt = dyn_cast <gassign *> (def_vinfo->stmt);
    4297        19778 :   if (def_stmt && gimple_assign_cast_p (def_stmt))
    4298              :     {
    4299         5527 :       tree rhs1 = gimple_assign_rhs1 (def_stmt);
    4300         5527 :       if (TYPE_MODE (TREE_TYPE (rhs1)) == TYPE_MODE (TREE_TYPE (oprnd0))
    4301         5527 :           && TYPE_PRECISION (TREE_TYPE (rhs1))
    4302         1296 :              == TYPE_PRECISION (TREE_TYPE (oprnd0)))
    4303              :         {
    4304         1296 :           if (TYPE_PRECISION (TREE_TYPE (oprnd1))
    4305         1296 :               >= TYPE_PRECISION (TREE_TYPE (rhs1)))
    4306              :             def = rhs1;
    4307              :           else
    4308              :             {
    4309         1194 :               tree mask
    4310         1194 :                 = build_low_bits_mask (TREE_TYPE (rhs1),
    4311         1194 :                                        TYPE_PRECISION (TREE_TYPE (oprnd1)));
    4312         1194 :               def = vect_recog_temp_ssa_var (TREE_TYPE (rhs1), NULL);
    4313         1194 :               def_stmt = gimple_build_assign (def, BIT_AND_EXPR, rhs1, mask);
    4314         1194 :               tree vecstype = get_vectype_for_scalar_type (vinfo,
    4315         1194 :                                                            TREE_TYPE (rhs1));
    4316         1194 :               append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vecstype);
    4317              :             }
    4318              :         }
    4319              :     }
    4320              : 
    4321         1296 :   if (def == NULL_TREE)
    4322              :     {
    4323        31986 :       def = vect_recog_temp_ssa_var (TREE_TYPE (oprnd0), NULL);
    4324        31986 :       def_stmt = gimple_build_assign (def, NOP_EXPR, oprnd1);
    4325        31986 :       append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    4326              :     }
    4327              : 
    4328              :   /* Pattern detected.  */
    4329        33282 :   vect_pattern_detected ("vect_recog_vector_vector_shift_pattern", last_stmt);
    4330              : 
    4331              :   /* Pattern supported.  Create a stmt to be used to replace the pattern.  */
    4332        33282 :   var = vect_recog_temp_ssa_var (TREE_TYPE (oprnd0), NULL);
    4333        33282 :   pattern_stmt = gimple_build_assign (var, rhs_code, oprnd0, def);
    4334              : 
    4335        33282 :   return pattern_stmt;
    4336              : }
    4337              : 
    4338              : /* Verify that the target has optabs of VECTYPE to perform all the steps
    4339              :    needed by the multiplication-by-immediate synthesis algorithm described by
    4340              :    ALG and VAR.  If SYNTH_SHIFT_P is true ensure that vector addition is
    4341              :    present.  Return true iff the target supports all the steps.  */
    4342              : 
    4343              : static bool
    4344       325566 : target_supports_mult_synth_alg (struct algorithm *alg, mult_variant var,
    4345              :                                  tree vectype, bool synth_shift_p)
    4346              : {
    4347       325566 :   if (alg->op[0] != alg_zero && alg->op[0] != alg_m)
    4348              :     return false;
    4349              : 
    4350       325566 :   bool supports_vminus = target_has_vecop_for_code (MINUS_EXPR, vectype);
    4351       325566 :   bool supports_vplus = target_has_vecop_for_code (PLUS_EXPR, vectype);
    4352              : 
    4353       325566 :   if (var == negate_variant
    4354       325566 :       && !target_has_vecop_for_code (NEGATE_EXPR, vectype))
    4355              :     return false;
    4356              : 
    4357              :   /* If we must synthesize shifts with additions make sure that vector
    4358              :      addition is available.  */
    4359       324855 :   if ((var == add_variant || synth_shift_p) && !supports_vplus)
    4360              :     return false;
    4361              : 
    4362       161941 :   for (int i = 1; i < alg->ops; i++)
    4363              :     {
    4364       120903 :       switch (alg->op[i])
    4365              :         {
    4366              :         case alg_shift:
    4367              :           break;
    4368        29552 :         case alg_add_t_m2:
    4369        29552 :         case alg_add_t2_m:
    4370        29552 :         case alg_add_factor:
    4371        29552 :           if (!supports_vplus)
    4372              :             return false;
    4373              :           break;
    4374        19169 :         case alg_sub_t_m2:
    4375        19169 :         case alg_sub_t2_m:
    4376        19169 :         case alg_sub_factor:
    4377        19169 :           if (!supports_vminus)
    4378              :             return false;
    4379              :           break;
    4380              :         case alg_unknown:
    4381              :         case alg_m:
    4382              :         case alg_zero:
    4383              :         case alg_impossible:
    4384              :           return false;
    4385            0 :         default:
    4386            0 :           gcc_unreachable ();
    4387              :         }
    4388              :     }
    4389              : 
    4390              :   return true;
    4391              : }
    4392              : 
    4393              : /* Synthesize a left shift of OP by AMNT bits using a series of additions and
    4394              :    putting the final result in DEST.  Append all statements but the last into
    4395              :    VINFO.  Return the last statement.  */
    4396              : 
    4397              : static gimple *
    4398            0 : synth_lshift_by_additions (vec_info *vinfo,
    4399              :                            tree dest, tree op, HOST_WIDE_INT amnt,
    4400              :                            stmt_vec_info stmt_info, tree vectype)
    4401              : {
    4402            0 :   HOST_WIDE_INT i;
    4403            0 :   tree itype = TREE_TYPE (op);
    4404            0 :   tree prev_res = op;
    4405            0 :   gcc_assert (amnt >= 0);
    4406            0 :   for (i = 0; i < amnt; i++)
    4407              :     {
    4408            0 :       tree tmp_var = (i < amnt - 1) ? vect_recog_temp_ssa_var (itype, NULL)
    4409              :                       : dest;
    4410            0 :       gimple *stmt
    4411            0 :         = gimple_build_assign (tmp_var, PLUS_EXPR, prev_res, prev_res);
    4412            0 :       prev_res = tmp_var;
    4413            0 :       if (i < amnt - 1)
    4414            0 :         append_pattern_def_seq (vinfo, stmt_info, stmt, vectype);
    4415              :       else
    4416            0 :         return stmt;
    4417              :     }
    4418            0 :   gcc_unreachable ();
    4419              :   return NULL;
    4420              : }
    4421              : 
    4422              : /* Helper for vect_synth_mult_by_constant.  Apply a binary operation
    4423              :    CODE to operands OP1 and OP2, creating a new temporary SSA var in
    4424              :    the process if necessary.  Append the resulting assignment statements
    4425              :    to the sequence in STMT_VINFO.  Return the SSA variable that holds the
    4426              :    result of the binary operation.  If SYNTH_SHIFT_P is true synthesize
    4427              :    left shifts using additions.  */
    4428              : 
    4429              : static tree
    4430        48403 : apply_binop_and_append_stmt (vec_info *vinfo,
    4431              :                              tree_code code, tree op1, tree op2,
    4432              :                              stmt_vec_info stmt_vinfo, tree vectype,
    4433              :                              bool synth_shift_p)
    4434              : {
    4435        48403 :   if (integer_zerop (op2)
    4436        48403 :       && (code == LSHIFT_EXPR
    4437        42382 :           || code == PLUS_EXPR))
    4438              :     {
    4439        42382 :       gcc_assert (TREE_CODE (op1) == SSA_NAME);
    4440              :       return op1;
    4441              :     }
    4442              : 
    4443         6021 :   gimple *stmt;
    4444         6021 :   tree itype = TREE_TYPE (op1);
    4445         6021 :   tree tmp_var = vect_recog_temp_ssa_var (itype, NULL);
    4446              : 
    4447         6021 :   if (code == LSHIFT_EXPR
    4448         6021 :       && synth_shift_p)
    4449              :     {
    4450            0 :       stmt = synth_lshift_by_additions (vinfo, tmp_var, op1,
    4451            0 :                                         TREE_INT_CST_LOW (op2), stmt_vinfo,
    4452              :                                         vectype);
    4453            0 :       append_pattern_def_seq (vinfo, stmt_vinfo, stmt, vectype);
    4454            0 :       return tmp_var;
    4455              :     }
    4456              : 
    4457         6021 :   stmt = gimple_build_assign (tmp_var, code, op1, op2);
    4458         6021 :   append_pattern_def_seq (vinfo, stmt_vinfo, stmt, vectype);
    4459         6021 :   return tmp_var;
    4460              : }
    4461              : 
    4462              : /* Synthesize a multiplication of OP by an INTEGER_CST VAL using shifts
    4463              :    and simple arithmetic operations to be vectorized.  Record the statements
    4464              :    produced in STMT_VINFO and return the last statement in the sequence or
    4465              :    NULL if it's not possible to synthesize such a multiplication.
    4466              :    This function mirrors the behavior of expand_mult_const in expmed.cc but
    4467              :    works on tree-ssa form.  */
    4468              : 
    4469              : static gimple *
    4470       329119 : vect_synth_mult_by_constant (vec_info *vinfo, tree op, tree val,
    4471              :                              stmt_vec_info stmt_vinfo)
    4472              : {
    4473       329119 :   tree itype = TREE_TYPE (op);
    4474       329119 :   machine_mode mode = TYPE_MODE (itype);
    4475       329119 :   struct algorithm alg;
    4476       329119 :   mult_variant variant;
    4477       329119 :   if (!tree_fits_shwi_p (val))
    4478              :     return NULL;
    4479              : 
    4480              :   /* Multiplication synthesis by shifts, adds and subs can introduce
    4481              :      signed overflow where the original operation didn't.  Perform the
    4482              :      operations on an unsigned type and cast back to avoid this.
    4483              :      In the future we may want to relax this for synthesis algorithms
    4484              :      that we can prove do not cause unexpected overflow.  */
    4485       325581 :   bool cast_to_unsigned_p = !TYPE_OVERFLOW_WRAPS (itype);
    4486              : 
    4487        64083 :   tree multtype = cast_to_unsigned_p ? unsigned_type_for (itype) : itype;
    4488       325581 :   tree vectype = get_vectype_for_scalar_type (vinfo, multtype);
    4489       325581 :   if (!vectype)
    4490              :     return NULL;
    4491              : 
    4492              :   /* Targets that don't support vector shifts but support vector additions
    4493              :      can synthesize shifts that way.  */
    4494       325581 :   bool synth_shift_p = !vect_supportable_shift (vinfo, LSHIFT_EXPR, multtype);
    4495              : 
    4496       325581 :   HOST_WIDE_INT hwval = tree_to_shwi (val);
    4497              :   /* Use MAX_COST here as we don't want to limit the sequence on rtx costs.
    4498              :      The vectorizer's benefit analysis will decide whether it's beneficial
    4499              :      to do this.  */
    4500       650966 :   bool possible = choose_mult_variant (VECTOR_MODE_P (TYPE_MODE (vectype))
    4501       325385 :                                        ? TYPE_MODE (vectype) : mode,
    4502              :                                        hwval, &alg, &variant, MAX_COST);
    4503       325581 :   if (!possible)
    4504              :     return NULL;
    4505              : 
    4506       325581 :   if (vect_is_reduction (stmt_vinfo))
    4507              :     {
    4508           26 :       int op_uses = alg.op[0] != alg_zero;
    4509           45 :       for (int i = 1; i < alg.ops; i++)
    4510           32 :         switch (alg.op[i])
    4511              :           {
    4512            4 :           case alg_add_t_m2:
    4513            4 :           case alg_sub_t_m2:
    4514            4 :             if (synth_shift_p && alg.log[i])
    4515              :               return NULL;
    4516              :             else
    4517            4 :               op_uses++;
    4518            4 :             break;
    4519            0 :           case alg_add_t2_m:
    4520            0 :           case alg_sub_t2_m:
    4521            0 :             op_uses++;
    4522              :             /* Fallthru.  */
    4523           28 :           case alg_shift:
    4524           28 :             if (synth_shift_p && alg.log[i])
    4525              :               return NULL;
    4526              :             break;
    4527              :           case alg_add_factor:
    4528              :           case alg_sub_factor:
    4529              :             return NULL;
    4530              :           default:
    4531              :             break;
    4532              :           }
    4533           13 :       if (variant == add_variant)
    4534            0 :         op_uses++;
    4535              :       /* When we'll synthesize more than a single use of the reduction
    4536              :          operand the reduction constraints are violated.  Avoid this
    4537              :          situation.  */
    4538           13 :       if (op_uses > 1)
    4539              :         return NULL;
    4540              :     }
    4541              : 
    4542       325566 :   if (!target_supports_mult_synth_alg (&alg, variant, vectype, synth_shift_p))
    4543              :     return NULL;
    4544              : 
    4545        41038 :   tree accumulator;
    4546              : 
    4547              :   /* Clear out the sequence of statements so we can populate it below.  */
    4548        41038 :   gimple *stmt = NULL;
    4549              : 
    4550        41038 :   if (cast_to_unsigned_p)
    4551              :     {
    4552        13995 :       tree tmp_op = vect_recog_temp_ssa_var (multtype, NULL);
    4553        13995 :       stmt = gimple_build_assign (tmp_op, CONVERT_EXPR, op);
    4554        13995 :       append_pattern_def_seq (vinfo, stmt_vinfo, stmt, vectype);
    4555        13995 :       op = tmp_op;
    4556              :     }
    4557              : 
    4558        41038 :   if (alg.op[0] == alg_zero)
    4559          246 :     accumulator = build_int_cst (multtype, 0);
    4560              :   else
    4561              :     accumulator = op;
    4562              : 
    4563        41038 :   bool needs_fixup = (variant == negate_variant)
    4564        41038 :                       || (variant == add_variant);
    4565              : 
    4566       161350 :   for (int i = 1; i < alg.ops; i++)
    4567              :     {
    4568       120312 :       tree shft_log = build_int_cst (multtype, alg.log[i]);
    4569       120312 :       tree accum_tmp = vect_recog_temp_ssa_var (multtype, NULL);
    4570       120312 :       tree tmp_var = NULL_TREE;
    4571              : 
    4572       120312 :       switch (alg.op[i])
    4573              :         {
    4574        71909 :         case alg_shift:
    4575        71909 :           if (synth_shift_p)
    4576            0 :             stmt
    4577            0 :               = synth_lshift_by_additions (vinfo, accum_tmp, accumulator,
    4578            0 :                                            alg.log[i], stmt_vinfo, vectype);
    4579              :           else
    4580        71909 :             stmt = gimple_build_assign (accum_tmp, LSHIFT_EXPR, accumulator,
    4581              :                                          shft_log);
    4582              :           break;
    4583        24531 :         case alg_add_t_m2:
    4584        24531 :           tmp_var
    4585        24531 :             = apply_binop_and_append_stmt (vinfo, LSHIFT_EXPR, op, shft_log,
    4586              :                                            stmt_vinfo, vectype, synth_shift_p);
    4587        24531 :           stmt = gimple_build_assign (accum_tmp, PLUS_EXPR, accumulator,
    4588              :                                        tmp_var);
    4589        24531 :           break;
    4590        18054 :         case alg_sub_t_m2:
    4591        18054 :           tmp_var = apply_binop_and_append_stmt (vinfo, LSHIFT_EXPR, op,
    4592              :                                                  shft_log, stmt_vinfo,
    4593              :                                                  vectype, synth_shift_p);
    4594              :           /* In some algorithms the first step involves zeroing the
    4595              :              accumulator.  If subtracting from such an accumulator
    4596              :              just emit the negation directly.  */
    4597        18054 :           if (integer_zerop (accumulator))
    4598          246 :             stmt = gimple_build_assign (accum_tmp, NEGATE_EXPR, tmp_var);
    4599              :           else
    4600        17808 :             stmt = gimple_build_assign (accum_tmp, MINUS_EXPR, accumulator,
    4601              :                                         tmp_var);
    4602              :           break;
    4603            0 :         case alg_add_t2_m:
    4604            0 :           tmp_var
    4605            0 :             = apply_binop_and_append_stmt (vinfo, LSHIFT_EXPR, accumulator,
    4606              :                                            shft_log, stmt_vinfo, vectype,
    4607              :                                            synth_shift_p);
    4608            0 :           stmt = gimple_build_assign (accum_tmp, PLUS_EXPR, tmp_var, op);
    4609            0 :           break;
    4610            0 :         case alg_sub_t2_m:
    4611            0 :           tmp_var
    4612            0 :             = apply_binop_and_append_stmt (vinfo, LSHIFT_EXPR, accumulator,
    4613              :                                            shft_log, stmt_vinfo, vectype,
    4614              :                                            synth_shift_p);
    4615            0 :           stmt = gimple_build_assign (accum_tmp, MINUS_EXPR, tmp_var, op);
    4616            0 :           break;
    4617         4908 :         case alg_add_factor:
    4618         4908 :           tmp_var
    4619         4908 :             = apply_binop_and_append_stmt (vinfo, LSHIFT_EXPR, accumulator,
    4620              :                                            shft_log, stmt_vinfo, vectype,
    4621              :                                            synth_shift_p);
    4622         4908 :           stmt = gimple_build_assign (accum_tmp, PLUS_EXPR, accumulator,
    4623              :                                        tmp_var);
    4624         4908 :           break;
    4625          910 :         case alg_sub_factor:
    4626          910 :           tmp_var
    4627          910 :             = apply_binop_and_append_stmt (vinfo, LSHIFT_EXPR, accumulator,
    4628              :                                            shft_log, stmt_vinfo, vectype,
    4629              :                                            synth_shift_p);
    4630          910 :           stmt = gimple_build_assign (accum_tmp, MINUS_EXPR, tmp_var,
    4631              :                                       accumulator);
    4632          910 :           break;
    4633            0 :         default:
    4634            0 :           gcc_unreachable ();
    4635              :         }
    4636              :       /* We don't want to append the last stmt in the sequence to stmt_vinfo
    4637              :          but rather return it directly.  */
    4638              : 
    4639       120312 :       if ((i < alg.ops - 1) || needs_fixup || cast_to_unsigned_p)
    4640        93635 :         append_pattern_def_seq (vinfo, stmt_vinfo, stmt, vectype);
    4641       120312 :       accumulator = accum_tmp;
    4642              :     }
    4643        41038 :   if (variant == negate_variant)
    4644              :     {
    4645          529 :       tree accum_tmp = vect_recog_temp_ssa_var (multtype, NULL);
    4646          529 :       stmt = gimple_build_assign (accum_tmp, NEGATE_EXPR, accumulator);
    4647          529 :       accumulator = accum_tmp;
    4648          529 :       if (cast_to_unsigned_p)
    4649          169 :         append_pattern_def_seq (vinfo, stmt_vinfo, stmt, vectype);
    4650              :     }
    4651        40509 :   else if (variant == add_variant)
    4652              :     {
    4653           97 :       tree accum_tmp = vect_recog_temp_ssa_var (multtype, NULL);
    4654           97 :       stmt = gimple_build_assign (accum_tmp, PLUS_EXPR, accumulator, op);
    4655           97 :       accumulator = accum_tmp;
    4656           97 :       if (cast_to_unsigned_p)
    4657           91 :         append_pattern_def_seq (vinfo, stmt_vinfo, stmt, vectype);
    4658              :     }
    4659              :   /* Move back to a signed if needed.  */
    4660        40672 :   if (cast_to_unsigned_p)
    4661              :     {
    4662        13995 :       tree accum_tmp = vect_recog_temp_ssa_var (itype, NULL);
    4663        13995 :       stmt = gimple_build_assign (accum_tmp, CONVERT_EXPR, accumulator);
    4664              :     }
    4665              : 
    4666              :   return stmt;
    4667              : }
    4668              : 
    4669              : /* Detect multiplication by constant and convert it into a sequence of
    4670              :    shifts and additions, subtractions, negations.  We reuse the
    4671              :    choose_mult_variant algorithms from expmed.cc
    4672              : 
    4673              :    Input/Output:
    4674              : 
    4675              :    STMT_VINFO: The stmt from which the pattern search begins,
    4676              :    i.e. the mult stmt.
    4677              : 
    4678              :  Output:
    4679              : 
    4680              :   * TYPE_OUT: The type of the output of this pattern.
    4681              : 
    4682              :   * Return value: A new stmt that will be used to replace
    4683              :     the multiplication.  */
    4684              : 
    4685              : static gimple *
    4686     33508827 : vect_recog_mult_pattern (vec_info *vinfo,
    4687              :                          stmt_vec_info stmt_vinfo, tree *type_out)
    4688              : {
    4689     33508827 :   tree oprnd0, oprnd1, vectype, itype;
    4690     33508827 :   gimple *pattern_stmt;
    4691              : 
    4692     33508827 :   auto *last_stmt = dyn_cast<gassign *> (stmt_vinfo->stmt);
    4693     23523879 :   if (!last_stmt)
    4694              :     return NULL;
    4695              : 
    4696     31352655 :   if (gimple_assign_rhs_code (last_stmt) != MULT_EXPR)
    4697              :     return NULL;
    4698              : 
    4699      1619602 :   oprnd0 = gimple_assign_rhs1 (last_stmt);
    4700      1619602 :   oprnd1 = gimple_assign_rhs2 (last_stmt);
    4701      1619602 :   itype = TREE_TYPE (oprnd0);
    4702              : 
    4703      1619602 :   if (TREE_CODE (oprnd0) != SSA_NAME
    4704      1619535 :       || TREE_CODE (oprnd1) != INTEGER_CST
    4705       973196 :       || !INTEGRAL_TYPE_P (itype)
    4706      2592798 :       || !type_has_mode_precision_p (itype))
    4707              :     return NULL;
    4708              : 
    4709       973129 :   vectype = get_vectype_for_scalar_type (vinfo, itype);
    4710       973129 :   if (vectype == NULL_TREE)
    4711              :     return NULL;
    4712              : 
    4713              :   /* If the target can handle vectorized multiplication natively,
    4714              :      don't attempt to optimize this.  */
    4715       792533 :   optab mul_optab = optab_for_tree_code (MULT_EXPR, vectype, optab_default);
    4716       792533 :   if (mul_optab != unknown_optab
    4717       792533 :       && can_implement_p (mul_optab, TYPE_MODE (vectype)))
    4718              :     return NULL;
    4719              : 
    4720       329119 :   pattern_stmt = vect_synth_mult_by_constant (vinfo,
    4721              :                                               oprnd0, oprnd1, stmt_vinfo);
    4722       329119 :   if (!pattern_stmt)
    4723              :     return NULL;
    4724              : 
    4725              :   /* Pattern detected.  */
    4726        41038 :   vect_pattern_detected ("vect_recog_mult_pattern", last_stmt);
    4727              : 
    4728        41038 :   *type_out = vectype;
    4729              : 
    4730        41038 :   return pattern_stmt;
    4731              : }
    4732              : 
    4733              : extern bool gimple_unsigned_integer_sat_add (tree, tree*, tree (*)(tree));
    4734              : extern bool gimple_unsigned_integer_sat_sub (tree, tree*, tree (*)(tree));
    4735              : extern bool gimple_unsigned_integer_sat_trunc (tree, tree*, tree (*)(tree));
    4736              : 
    4737              : extern bool gimple_unsigned_integer_narrow_clip (tree, tree*, tree (*)(tree));
    4738              : 
    4739              : extern bool gimple_signed_integer_sat_add (tree, tree*, tree (*)(tree));
    4740              : extern bool gimple_signed_integer_sat_sub (tree, tree*, tree (*)(tree));
    4741              : extern bool gimple_signed_integer_sat_trunc (tree, tree*, tree (*)(tree));
    4742              : 
    4743              : static gimple *
    4744         1277 : vect_recog_build_binary_gimple_stmt (vec_info *vinfo, stmt_vec_info stmt_info,
    4745              :                                      internal_fn fn, tree *type_out,
    4746              :                                      tree lhs, tree op_0, tree op_1)
    4747              : {
    4748         1277 :   tree itype = TREE_TYPE (op_0);
    4749         1277 :   tree otype = TREE_TYPE (lhs);
    4750         1277 :   tree v_itype = get_vectype_for_scalar_type (vinfo, itype);
    4751         1277 :   tree v_otype = get_vectype_for_scalar_type (vinfo, otype);
    4752              : 
    4753         1277 :   if (v_itype != NULL_TREE && v_otype != NULL_TREE
    4754         1277 :     && direct_internal_fn_supported_p (fn, v_itype, OPTIMIZE_FOR_BOTH))
    4755              :     {
    4756         1019 :       gcall *call = gimple_build_call_internal (fn, 2, op_0, op_1);
    4757         1019 :       tree in_ssa = vect_recog_temp_ssa_var (itype, NULL);
    4758              : 
    4759         1019 :       gimple_call_set_lhs (call, in_ssa);
    4760         1019 :       gimple_call_set_nothrow (call, /* nothrow_p */ false);
    4761         1019 :       gimple_set_location (call, gimple_location (STMT_VINFO_STMT (stmt_info)));
    4762              : 
    4763         1019 :       *type_out = v_otype;
    4764              : 
    4765         1019 :       if (types_compatible_p (itype, otype))
    4766              :         return call;
    4767              :       else
    4768              :         {
    4769            0 :           append_pattern_def_seq (vinfo, stmt_info, call, v_itype);
    4770            0 :           tree out_ssa = vect_recog_temp_ssa_var (otype, NULL);
    4771              : 
    4772            0 :           return gimple_build_assign (out_ssa, NOP_EXPR, in_ssa);
    4773              :         }
    4774              :     }
    4775              : 
    4776              :   return NULL;
    4777              : }
    4778              : 
    4779              : /*
    4780              :  * Try to detect saturation add pattern (SAT_ADD), aka below gimple:
    4781              :  *   _7 = _4 + _6;
    4782              :  *   _8 = _4 > _7;
    4783              :  *   _9 = (long unsigned int) _8;
    4784              :  *   _10 = -_9;
    4785              :  *   _12 = _7 | _10;
    4786              :  *
    4787              :  * And then simplified to
    4788              :  *   _12 = .SAT_ADD (_4, _6);
    4789              :  */
    4790              : 
    4791              : static gimple *
    4792     33587062 : vect_recog_sat_add_pattern (vec_info *vinfo, stmt_vec_info stmt_vinfo,
    4793              :                             tree *type_out)
    4794              : {
    4795     33587062 :   auto last_stmt = dyn_cast<gassign *> (STMT_VINFO_STMT (stmt_vinfo));
    4796              : 
    4797     23602114 :   if (!last_stmt)
    4798              :     return NULL;
    4799              : 
    4800     23602114 :   tree ops[2];
    4801     23602114 :   tree lhs = gimple_assign_lhs (last_stmt);
    4802              : 
    4803     23602114 :   if (gimple_unsigned_integer_sat_add (lhs, ops, NULL)
    4804     23602114 :       || gimple_signed_integer_sat_add (lhs, ops, NULL))
    4805              :     {
    4806              :       /* .SAT_ADD saturates in the type of its first operand, which must be
    4807              :          the type of the result.  */
    4808          839 :       gcc_checking_assert (types_compatible_p (TREE_TYPE (lhs),
    4809              :                                                TREE_TYPE (ops[0])));
    4810          839 :       if (TREE_CODE (ops[1]) == INTEGER_CST)
    4811           21 :         ops[1] = fold_convert (TREE_TYPE (ops[0]), ops[1]);
    4812              : 
    4813          839 :       gimple *stmt = vect_recog_build_binary_gimple_stmt (vinfo, stmt_vinfo,
    4814              :                                                           IFN_SAT_ADD, type_out,
    4815              :                                                           lhs, ops[0], ops[1]);
    4816          839 :       if (stmt)
    4817              :         {
    4818          804 :           vect_pattern_detected ("vect_recog_sat_add_pattern", last_stmt);
    4819          804 :           return stmt;
    4820              :         }
    4821              :     }
    4822              : 
    4823              :   return NULL;
    4824              : }
    4825              : 
    4826              : /*
    4827              :  * Try to transform the truncation for .SAT_SUB pattern,  mostly occurs in
    4828              :  * the benchmark zip.  Aka:
    4829              :  *
    4830              :  *   unsigned int _1;
    4831              :  *   unsigned int _2;
    4832              :  *   unsigned short int _4;
    4833              :  *   _9 = (unsigned short int).SAT_SUB (_1, _2);
    4834              :  *
    4835              :  *   if _1 is known to be in the range of unsigned short int.  For example
    4836              :  *   there is a def _1 = (unsigned short int)_4.  Then we can transform the
    4837              :  *   truncation to:
    4838              :  *
    4839              :  *   _3 = (unsigned short int) MIN (65535, _2); // aka _3 = .SAT_TRUNC (_2);
    4840              :  *   _9 = .SAT_SUB (_4, _3);
    4841              :  *
    4842              :  *   Then,  we can better vectorized code and avoid the unnecessary narrowing
    4843              :  *   stmt during vectorization with below stmt(s).
    4844              :  *
    4845              :  *   _3 = .SAT_TRUNC(_2); // SI => HI
    4846              :  *   _9 = .SAT_SUB (_4, _3);
    4847              :  */
    4848              : static void
    4849          438 : vect_recog_sat_sub_pattern_transform (vec_info *vinfo,
    4850              :                                       stmt_vec_info stmt_vinfo,
    4851              :                                       tree lhs, tree *ops)
    4852              : {
    4853          438 :   tree otype = TREE_TYPE (lhs);
    4854          438 :   tree itype = TREE_TYPE (ops[0]);
    4855          438 :   unsigned itype_prec = TYPE_PRECISION (itype);
    4856          438 :   unsigned otype_prec = TYPE_PRECISION (otype);
    4857              : 
    4858          438 :   if (types_compatible_p (otype, itype) || otype_prec >= itype_prec)
    4859          438 :     return;
    4860              : 
    4861            0 :   tree v_otype = get_vectype_for_scalar_type (vinfo, otype);
    4862            0 :   tree v_itype = get_vectype_for_scalar_type (vinfo, itype);
    4863            0 :   tree_pair v_pair = tree_pair (v_otype, v_itype);
    4864              : 
    4865            0 :   if (v_otype == NULL_TREE || v_itype == NULL_TREE
    4866            0 :     || !direct_internal_fn_supported_p (IFN_SAT_TRUNC, v_pair,
    4867              :                                         OPTIMIZE_FOR_BOTH))
    4868              :     return;
    4869              : 
    4870              :   /* 1. Find the _4 and update ops[0] as above example.  */
    4871            0 :   vect_unpromoted_value unprom;
    4872            0 :   tree tmp = vect_look_through_possible_promotion (vinfo, ops[0], &unprom);
    4873              : 
    4874            0 :   if (tmp == NULL_TREE || TYPE_PRECISION (unprom.type) != otype_prec)
    4875              :     return;
    4876              : 
    4877            0 :   ops[0] = tmp;
    4878              : 
    4879              :   /* 2. Generate _3 = .SAT_TRUNC (_2) and update ops[1] as above example.  */
    4880            0 :   tree trunc_lhs_ssa = vect_recog_temp_ssa_var (otype, NULL);
    4881            0 :   gcall *call = gimple_build_call_internal (IFN_SAT_TRUNC, 1, ops[1]);
    4882              : 
    4883            0 :   gimple_call_set_lhs (call, trunc_lhs_ssa);
    4884            0 :   gimple_call_set_nothrow (call, /* nothrow_p */ false);
    4885            0 :   append_pattern_def_seq (vinfo, stmt_vinfo, call, v_otype);
    4886              : 
    4887            0 :   ops[1] = trunc_lhs_ssa;
    4888              : }
    4889              : 
    4890              : /*
    4891              :  * Try to detect saturation sub pattern (SAT_ADD), aka below gimple:
    4892              :  * Unsigned:
    4893              :  *   _7 = _1 >= _2;
    4894              :  *   _8 = _1 - _2;
    4895              :  *   _10 = (long unsigned int) _7;
    4896              :  *   _9 = _8 * _10;
    4897              :  *
    4898              :  * And then simplified to
    4899              :  *   _9 = .SAT_SUB (_1, _2);
    4900              :  *
    4901              :  * Signed:
    4902              :  *   x.0_4 = (unsigned char) x_16;
    4903              :  *   y.1_5 = (unsigned char) y_18;
    4904              :  *   _6 = x.0_4 - y.1_5;
    4905              :  *   minus_19 = (int8_t) _6;
    4906              :  *   _7 = x_16 ^ y_18;
    4907              :  *   _8 = x_16 ^ minus_19;
    4908              :  *   _44 = _7 < 0;
    4909              :  *   _23 = x_16 < 0;
    4910              :  *   _24 = (signed char) _23;
    4911              :  *   _58 = (unsigned char) _24;
    4912              :  *   _59 = -_58;
    4913              :  *   _25 = (signed char) _59;
    4914              :  *   _26 = _25 ^ 127;
    4915              :  *   _42 = _8 < 0;
    4916              :  *   _41 = _42 & _44;
    4917              :  *   iftmp.2_11 = _41 ? _26 : minus_19;
    4918              :  *
    4919              :  * And then simplified to
    4920              :  *   iftmp.2_11 = .SAT_SUB (x_16, y_18);
    4921              :  */
    4922              : 
    4923              : static gimple *
    4924     33586258 : vect_recog_sat_sub_pattern (vec_info *vinfo, stmt_vec_info stmt_vinfo,
    4925              :                             tree *type_out)
    4926              : {
    4927     33586258 :   gimple *last_stmt = STMT_VINFO_STMT (stmt_vinfo);
    4928              : 
    4929     33586258 :   if (!is_gimple_assign (last_stmt))
    4930              :     return NULL;
    4931              : 
    4932     23601310 :   tree ops[2];
    4933     23601310 :   tree lhs = gimple_assign_lhs (last_stmt);
    4934              : 
    4935     23601310 :   if (gimple_unsigned_integer_sat_sub (lhs, ops, NULL)
    4936     23601310 :       || gimple_signed_integer_sat_sub (lhs, ops, NULL))
    4937              :     {
    4938          438 :       vect_recog_sat_sub_pattern_transform (vinfo, stmt_vinfo, lhs, ops);
    4939          438 :       gimple *stmt = vect_recog_build_binary_gimple_stmt (vinfo, stmt_vinfo,
    4940              :                                                           IFN_SAT_SUB, type_out,
    4941              :                                                           lhs, ops[0], ops[1]);
    4942          438 :       if (stmt)
    4943              :         {
    4944          215 :           vect_pattern_detected ("vect_recog_sat_sub_pattern", last_stmt);
    4945          215 :           return stmt;
    4946              :         }
    4947              :     }
    4948              : 
    4949              :   return NULL;
    4950              : }
    4951              : 
    4952              : /* Return the vector type for ITYPE if the target supports the MAX_EXPR
    4953              :    and SAT_TRUNC sequence.  */
    4954              : 
    4955              : static tree
    4956           73 : vect_sat_trunc_clip_input_vectype (vec_info *vinfo, tree v_otype,
    4957              :                                    tree itype)
    4958              : {
    4959           73 :   tree v_itype;
    4960           73 :   if (!vect_supportable_direct_optab_p
    4961           73 :         (vinfo, itype, MAX_EXPR, itype, &v_itype)
    4962           95 :       || !direct_internal_fn_supported_p
    4963           95 :            (IFN_SAT_TRUNC, tree_pair (v_otype, v_itype),
    4964              :             OPTIMIZE_FOR_BOTH))
    4965              :     return NULL_TREE;
    4966              : 
    4967            0 :   return v_itype;
    4968              : }
    4969              : 
    4970              : /*
    4971              :  * Try to detect saturation truncation pattern (SAT_TRUNC), aka below gimple:
    4972              :  *   overflow_5 = x_4(D) > 4294967295;
    4973              :  *   _1 = (unsigned int) x_4(D);
    4974              :  *   _2 = (unsigned int) overflow_5;
    4975              :  *   _3 = -_2;
    4976              :  *   _6 = _1 | _3;
    4977              :  *
    4978              :  * And then simplified to
    4979              :  *   _6 = .SAT_TRUNC (x_4(D));
    4980              :  */
    4981              : 
    4982              : static gimple *
    4983     33586043 : vect_recog_sat_trunc_pattern (vec_info *vinfo, stmt_vec_info stmt_vinfo,
    4984              :                               tree *type_out)
    4985              : {
    4986     33586043 :   auto last_stmt = dyn_cast<gassign *> (STMT_VINFO_STMT (stmt_vinfo));
    4987              : 
    4988     23601095 :   if (!last_stmt)
    4989              :     return NULL;
    4990              : 
    4991     23601095 :   tree ops[1];
    4992     23601095 :   tree lhs = gimple_assign_lhs (last_stmt);
    4993     23601095 :   tree otype = TREE_TYPE (lhs);
    4994              : 
    4995     23601095 :   if ((gimple_unsigned_integer_narrow_clip (lhs, ops, NULL))
    4996     23601095 :        && type_has_mode_precision_p (otype))
    4997              :     {
    4998           73 :       tree input = ops[0];
    4999           73 :       tree itype = TREE_TYPE (input);
    5000           73 :       tree v_otype = get_vectype_for_scalar_type (vinfo, otype);
    5001           73 :       internal_fn fn = IFN_SAT_TRUNC;
    5002           73 :       tree v_itype = NULL_TREE;
    5003              : 
    5004           73 :       if (v_otype != NULL_TREE)
    5005              :         {
    5006              :           /* Avoid widening an input only to narrow it again.  A signed
    5007              :              promotion does not affect MAX (X, 0), so use the unpromoted
    5008              :              value when the target can narrow it directly to the output
    5009              :              type.  */
    5010           73 :           vect_unpromoted_value unprom;
    5011           73 :           bool single_use_p = true;
    5012           73 :           if (vect_look_through_possible_promotion (vinfo, input, &unprom,
    5013              :                                                       &single_use_p)
    5014           73 :               && single_use_p
    5015           34 :               && INTEGRAL_TYPE_P (unprom.type)
    5016           34 :               && TYPE_SIGN (unprom.type) == TYPE_SIGN (itype)
    5017           34 :               && type_has_mode_precision_p (unprom.type)
    5018           34 :               && TYPE_PRECISION (otype) < TYPE_PRECISION (unprom.type)
    5019          107 :               && TYPE_PRECISION (unprom.type) < TYPE_PRECISION (itype))
    5020              :             {
    5021            0 :               tree v_unprom_type
    5022            0 :                 = vect_sat_trunc_clip_input_vectype (vinfo, v_otype,
    5023              :                                                        unprom.type);
    5024            0 :               if (v_unprom_type != NULL_TREE)
    5025              :                 {
    5026            0 :                   input = unprom.op;
    5027            0 :                   itype = unprom.type;
    5028            0 :                   v_itype = v_unprom_type;
    5029              :                 }
    5030              :             }
    5031              : 
    5032            0 :           if (v_itype == NULL_TREE)
    5033           73 :             v_itype
    5034           73 :               = vect_sat_trunc_clip_input_vectype (vinfo, v_otype, itype);
    5035              : 
    5036           73 :           if (v_itype != NULL_TREE)
    5037              :             {
    5038            0 :               tree temp = vect_recog_temp_ssa_var (itype, NULL);
    5039            0 :               gimple *max_stmt
    5040            0 :                 = gimple_build_assign (temp, MAX_EXPR,
    5041              :                                        build_zero_cst (itype), input);
    5042            0 :               append_pattern_def_seq (vinfo, stmt_vinfo, max_stmt, v_itype);
    5043              : 
    5044            0 :               gcall *call = gimple_build_call_internal (fn, 1, temp);
    5045            0 :               tree out_ssa = vect_recog_temp_ssa_var (otype, NULL);
    5046              : 
    5047            0 :               gimple_call_set_lhs (call, out_ssa);
    5048            0 :               gimple_call_set_nothrow (call, false);
    5049            0 :               gimple_set_location (call, gimple_location (last_stmt));
    5050              : 
    5051            0 :               *type_out = v_otype;
    5052              : 
    5053            0 :               return call;
    5054              :             }
    5055              :         }
    5056              :     }
    5057              : 
    5058     23601095 :   if ((gimple_unsigned_integer_sat_trunc (lhs, ops, NULL)
    5059     23600710 :        || gimple_signed_integer_sat_trunc (lhs, ops, NULL))
    5060     23601267 :       && type_has_mode_precision_p (otype))
    5061              :     {
    5062          545 :       tree itype = TREE_TYPE (ops[0]);
    5063          545 :       tree v_itype = get_vectype_for_scalar_type (vinfo, itype);
    5064          545 :       tree v_otype = get_vectype_for_scalar_type (vinfo, otype);
    5065          545 :       internal_fn fn = IFN_SAT_TRUNC;
    5066              : 
    5067          537 :       if (v_itype != NULL_TREE && v_otype != NULL_TREE
    5068         1082 :         && direct_internal_fn_supported_p (fn, tree_pair (v_otype, v_itype),
    5069              :                                            OPTIMIZE_FOR_BOTH))
    5070              :         {
    5071            0 :           gcall *call = gimple_build_call_internal (fn, 1, ops[0]);
    5072            0 :           tree out_ssa = vect_recog_temp_ssa_var (otype, NULL);
    5073              : 
    5074            0 :           gimple_call_set_lhs (call, out_ssa);
    5075            0 :           gimple_call_set_nothrow (call, /* nothrow_p */ false);
    5076            0 :           gimple_set_location (call, gimple_location (last_stmt));
    5077              : 
    5078            0 :           *type_out = v_otype;
    5079              : 
    5080            0 :           return call;
    5081              :         }
    5082              :     }
    5083              : 
    5084              :   return NULL;
    5085              : }
    5086              : 
    5087              : 
    5088              : /* Function add_code_for_floorceilround_divmod
    5089              :    A helper function to add compensation code for implementing FLOOR_MOD_EXPR,
    5090              :    FLOOR_DIV_EXPR, CEIL_MOD_EXPR, CEIL_DIV_EXPR, ROUND_MOD_EXPR and
    5091              :    ROUND_DIV_EXPR
    5092              :    The quotient and remainder are needed for implemented these operators.
    5093              :    FLOOR cases
    5094              :    r = x %[fl] y; r = x/[fl] y;
    5095              :    is
    5096              :    r = x % y; if (r && (x ^ y) < 0) r += y;
    5097              :    r = x % y; d = x/y; if (r && (x ^ y) < 0) d--; Respectively
    5098              :    Produce following sequence
    5099              :    v0 = x^y
    5100              :    v1 = -r
    5101              :    v2 = r | -r
    5102              :    v3 = v0 & v2
    5103              :    v4 = v3 < 0
    5104              :    if (floor_mod)
    5105              :      v5 = v4 ? y : 0
    5106              :      v6 = r + v5
    5107              :    if (floor_div)
    5108              :      v5 = v4 ? 1 : 0
    5109              :      v6 = d - 1
    5110              :    Similar sequences of vector instructions are produces for following cases
    5111              :    CEIL cases
    5112              :    r = x %[cl] y; r = x/[cl] y;
    5113              :    is
    5114              :    r = x % y; if (r && (x ^ y) >= 0) r -= y;
    5115              :    r = x % y; if (r) r -= y; (unsigned)
    5116              :    r = x % y; d = x/y; if (r && (x ^ y) >= 0) d++;
    5117              :    r = x % y; d = x/y; if (r) d++; (unsigned)
    5118              :    ROUND cases
    5119              :    r = x %[rd] y; r = x/[rd] y;
    5120              :    is
    5121              :    r = x % y; if (r > ((y-1)/2)) if ((x ^ y) >= 0) r -= y; else r += y;
    5122              :    r = x % y; if (r > ((y-1)/2)) r -= y; (unsigned)
    5123              :    r = x % y; d = x/y; if (r > ((y-1)/2)) if ((x ^ y) >= 0) d++; else d--;
    5124              :    r = x % y; d = x/y; if (r > ((y-1)/2)) d++; (unsigned)
    5125              :    Inputs:
    5126              :      VECTYPE: Vector type of the operands
    5127              :      STMT_VINFO: Statement where pattern begins
    5128              :      RHS_CODE: Should either be FLOOR_MOD_EXPR or FLOOR_DIV_EXPR
    5129              :      Q: The quotient of division
    5130              :      R: Remainder of division
    5131              :      OPRDN0/OPRND1: Actual operands involved
    5132              :      ITYPE: tree type of oprnd0
    5133              :    Output:
    5134              :      NULL if vectorization not possible
    5135              :      Gimple statement based on rhs_code
    5136              : */
    5137              : static gimple *
    5138          420 : add_code_for_floorceilround_divmod (tree vectype, vec_info *vinfo,
    5139              :                                     stmt_vec_info stmt_vinfo,
    5140              :                                     enum tree_code rhs_code, tree q, tree r,
    5141              :                                     tree oprnd0, tree oprnd1, tree itype)
    5142              : {
    5143          420 :   gimple *def_stmt;
    5144          420 :   tree mask_vectype = truth_type_for (vectype);
    5145          420 :   if (!mask_vectype)
    5146              :     return NULL;
    5147          420 :   tree bool_cond;
    5148          420 :   bool unsigned_p = TYPE_UNSIGNED (itype);
    5149              : 
    5150          420 :   switch (rhs_code)
    5151              :     {
    5152          384 :     case FLOOR_MOD_EXPR:
    5153          384 :     case FLOOR_DIV_EXPR:
    5154          384 :     case CEIL_MOD_EXPR:
    5155          384 :     case CEIL_DIV_EXPR:
    5156          384 :       {
    5157          384 :         if (!target_has_vecop_for_code (NEGATE_EXPR, vectype)
    5158          360 :             || !target_has_vecop_for_code (BIT_XOR_EXPR, vectype)
    5159          360 :             || !target_has_vecop_for_code (BIT_IOR_EXPR, vectype)
    5160          360 :             || !target_has_vecop_for_code (PLUS_EXPR, vectype)
    5161          360 :             || !target_has_vecop_for_code (MINUS_EXPR, vectype)
    5162          360 :             || !expand_vec_cmp_expr_p (vectype, mask_vectype, LT_EXPR)
    5163          610 :             || !expand_vec_cond_expr_p (vectype, mask_vectype))
    5164              :           return NULL;
    5165          226 :         if (unsigned_p)
    5166              :           {
    5167           18 :             gcc_assert (rhs_code == CEIL_MOD_EXPR || rhs_code == CEIL_DIV_EXPR);
    5168              : 
    5169           18 :             if (!expand_vec_cmp_expr_p (vectype, mask_vectype, GT_EXPR))
    5170              :               return NULL;
    5171           18 :             bool is_mod = rhs_code == CEIL_MOD_EXPR;
    5172              :             // r > 0
    5173           18 :             bool_cond = vect_recog_temp_ssa_var (boolean_type_node, NULL);
    5174           18 :             def_stmt = gimple_build_assign (bool_cond, GT_EXPR, r,
    5175              :                                             build_int_cst (itype, 0));
    5176           18 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, mask_vectype,
    5177              :                                     itype);
    5178              : 
    5179              :             // (r > 0) ? y : 0 (mod)
    5180              :             // (r > 0) ? 1 : 0 (ceil)
    5181           18 :             tree extr_cond = vect_recog_temp_ssa_var (itype, NULL);
    5182           18 :             def_stmt
    5183           27 :               = gimple_build_assign (extr_cond, COND_EXPR, bool_cond,
    5184            9 :                                      is_mod ? oprnd1 : build_int_cst (itype, 1),
    5185              :                                      build_int_cst (itype, 0));
    5186           18 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5187              : 
    5188              :             // r -= (r > 0) ? y : 0 (mod)
    5189              :             // d += (x^y < 0 && r) ? -1 : 0 (ceil)
    5190           18 :             tree result = vect_recog_temp_ssa_var (itype, NULL);
    5191           27 :             return gimple_build_assign (result, is_mod ? MINUS_EXPR : PLUS_EXPR,
    5192           18 :                                         is_mod ? r : q, extr_cond);
    5193              :           }
    5194              :         else
    5195              :           {
    5196          208 :             bool ceil_p
    5197          208 :               = (rhs_code == CEIL_MOD_EXPR || rhs_code == CEIL_DIV_EXPR);
    5198          208 :             if (ceil_p && !target_has_vecop_for_code (BIT_NOT_EXPR, vectype))
    5199              :               return NULL;
    5200              :             // x ^ y
    5201          208 :             tree xort = vect_recog_temp_ssa_var (itype, NULL);
    5202          208 :             def_stmt = gimple_build_assign (xort, BIT_XOR_EXPR, oprnd0, oprnd1);
    5203          208 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5204              : 
    5205          208 :             tree cond_reg = xort;
    5206              :             // ~(x ^ y) (ceil)
    5207          208 :             if (ceil_p)
    5208              :               {
    5209           18 :                 cond_reg = vect_recog_temp_ssa_var (itype, NULL);
    5210           18 :                 def_stmt = gimple_build_assign (cond_reg, BIT_NOT_EXPR, xort);
    5211           18 :                 append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5212              :               }
    5213              : 
    5214              :             // -r
    5215          208 :             tree negate_r = vect_recog_temp_ssa_var (itype, NULL);
    5216          208 :             def_stmt = gimple_build_assign (negate_r, NEGATE_EXPR, r);
    5217          208 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5218              : 
    5219              :             // r | -r , sign bit is set if r!=0
    5220          208 :             tree r_or_negr = vect_recog_temp_ssa_var (itype, NULL);
    5221          208 :             def_stmt
    5222          208 :               = gimple_build_assign (r_or_negr, BIT_IOR_EXPR, r, negate_r);
    5223          208 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5224              : 
    5225              :             // (x ^ y) & (r | -r)
    5226              :             // ~(x ^ y) & (r | -r) (ceil)
    5227          208 :             tree r_or_negr_and_xor = vect_recog_temp_ssa_var (itype, NULL);
    5228          208 :             def_stmt = gimple_build_assign (r_or_negr_and_xor, BIT_AND_EXPR,
    5229              :                                             r_or_negr, cond_reg);
    5230          208 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5231              : 
    5232              :             // (x ^ y) & (r | -r) < 0 which is equivalent to (x^y < 0 && r!=0)
    5233          208 :             bool_cond = vect_recog_temp_ssa_var (boolean_type_node, NULL);
    5234          208 :             def_stmt
    5235          208 :               = gimple_build_assign (bool_cond, LT_EXPR, r_or_negr_and_xor,
    5236              :                                      build_int_cst (itype, 0));
    5237          208 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, mask_vectype,
    5238              :                                     itype);
    5239              : 
    5240              :             // (x^y < 0 && r) ? y : 0 (mod)
    5241              :             // (x^y < 0 && r) ? -1 : 0 (div)
    5242          208 :             bool is_mod
    5243          208 :               = (rhs_code == FLOOR_MOD_EXPR || rhs_code == CEIL_MOD_EXPR);
    5244          208 :             tree extr_cond = vect_recog_temp_ssa_var (itype, NULL);
    5245          248 :             def_stmt = gimple_build_assign (extr_cond, COND_EXPR, bool_cond,
    5246              :                                             is_mod ? oprnd1
    5247           40 :                                                    : build_int_cst (itype, -1),
    5248              :                                             build_int_cst (itype, 0));
    5249          208 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5250              : 
    5251              :             // r += (x ^ y < 0 && r) ? y : 0 (floor mod)
    5252              :             // d += (x^y < 0 && r) ? -1 : 0 (floor div)
    5253              :             // r -= (x ^ y < 0 && r) ? y : 0 (ceil mod)
    5254              :             // d -= (x^y < 0 && r) ? -1 : 0 (ceil div)
    5255          208 :             tree result = vect_recog_temp_ssa_var (itype, NULL);
    5256          416 :             return gimple_build_assign (result,
    5257          208 :                                         (rhs_code == FLOOR_MOD_EXPR
    5258          208 :                                          || rhs_code == FLOOR_DIV_EXPR)
    5259              :                                           ? PLUS_EXPR
    5260              :                                           : MINUS_EXPR,
    5261          208 :                                         is_mod ? r : q, extr_cond);
    5262              :           }
    5263              :       }
    5264           36 :     case ROUND_MOD_EXPR:
    5265           36 :     case ROUND_DIV_EXPR:
    5266           36 :       {
    5267           36 :         if (!target_has_vecop_for_code (BIT_AND_EXPR, vectype)
    5268           36 :             || !target_has_vecop_for_code (PLUS_EXPR, vectype)
    5269           36 :             || !expand_vec_cmp_expr_p (vectype, mask_vectype, LT_EXPR)
    5270           36 :             || !expand_vec_cmp_expr_p (vectype, mask_vectype, GT_EXPR)
    5271           72 :             || !expand_vec_cond_expr_p (vectype, mask_vectype))
    5272              :           return NULL;
    5273              : 
    5274           36 :         bool is_mod = rhs_code == ROUND_MOD_EXPR;
    5275           36 :         HOST_WIDE_INT d = TREE_INT_CST_LOW (oprnd1);
    5276           36 :         unsigned HOST_WIDE_INT abs_d
    5277              :           = (d >= 0 ? (unsigned HOST_WIDE_INT) d : -(unsigned HOST_WIDE_INT) d);
    5278           36 :         unsigned HOST_WIDE_INT mid_d = (abs_d - 1) >> 1;
    5279           36 :         if (!unsigned_p)
    5280              :           {
    5281              :             // check availability of abs expression for vector
    5282           18 :             if (!target_has_vecop_for_code (ABS_EXPR, vectype))
    5283              :               return NULL;
    5284              :             // abs (r)
    5285           18 :             tree abs_r = vect_recog_temp_ssa_var (itype, NULL);
    5286           18 :             def_stmt = gimple_build_assign (abs_r, ABS_EXPR, r);
    5287           18 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5288              : 
    5289              :             // abs (r) > (abs (y-1) >> 1)
    5290           18 :             tree round_p = vect_recog_temp_ssa_var (boolean_type_node, NULL);
    5291           18 :             def_stmt = gimple_build_assign (round_p, GT_EXPR, abs_r,
    5292           18 :                                             build_int_cst (itype, mid_d));
    5293           18 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, mask_vectype,
    5294              :                                     itype);
    5295              : 
    5296              :             // x ^ y
    5297           18 :             tree cond_reg = vect_recog_temp_ssa_var (itype, NULL);
    5298           18 :             def_stmt
    5299           18 :               = gimple_build_assign (cond_reg, BIT_XOR_EXPR, oprnd0, oprnd1);
    5300           18 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5301              : 
    5302              :             // x ^ y < 0
    5303           18 :             bool_cond = vect_recog_temp_ssa_var (boolean_type_node, NULL);
    5304           18 :             def_stmt = gimple_build_assign (bool_cond, LT_EXPR, cond_reg,
    5305              :                                             build_int_cst (itype, 0));
    5306           18 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, mask_vectype,
    5307              :                                     itype);
    5308              : 
    5309              :             // x ^ y < 0 ? y : -y (mod)
    5310              :             // x ^ y < 0 ? -1 : 1 (div)
    5311           18 :             tree val1 = vect_recog_temp_ssa_var (itype, NULL);
    5312           18 :             def_stmt
    5313           36 :               = gimple_build_assign (val1, COND_EXPR, bool_cond,
    5314           27 :                                      build_int_cst (itype, is_mod ? d : -1),
    5315           18 :                                      build_int_cst (itype, is_mod ? -d : 1));
    5316           18 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5317           18 :             int precision = TYPE_PRECISION (itype);
    5318           18 :             wide_int wmask = wi::mask (precision, false, precision);
    5319              : 
    5320              :             // abs (r) > (abs (y-1) >> 1) ? 0xffffffff : 0
    5321           18 :             tree val2 = vect_recog_temp_ssa_var (itype, NULL);
    5322           36 :             def_stmt = gimple_build_assign (val2, COND_EXPR, round_p,
    5323           18 :                                             wide_int_to_tree (itype, wmask),
    5324              :                                             build_int_cst (itype, 0));
    5325           18 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5326              : 
    5327           18 :             tree fval = vect_recog_temp_ssa_var (itype, NULL);
    5328           18 :             def_stmt = gimple_build_assign (fval, BIT_AND_EXPR, val1, val2);
    5329           18 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5330              : 
    5331           18 :             tree result = vect_recog_temp_ssa_var (itype, NULL);
    5332           27 :             return gimple_build_assign (result, PLUS_EXPR, is_mod ? r : q,
    5333              :                                         fval);
    5334           18 :           }
    5335              :         else
    5336              :           {
    5337              :             // r > (y-1 >> 1)
    5338           18 :             tree round_p = vect_recog_temp_ssa_var (boolean_type_node, NULL);
    5339           18 :             def_stmt = gimple_build_assign (round_p, GT_EXPR, r,
    5340           18 :                                             build_int_cst (itype, mid_d));
    5341           18 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, mask_vectype,
    5342              :                                     itype);
    5343              : 
    5344              :             // (r > (y-1)>>1) ? -d : 1
    5345           18 :             tree val2 = vect_recog_temp_ssa_var (itype, NULL);
    5346           18 :             def_stmt
    5347           36 :               = gimple_build_assign (val2, COND_EXPR, round_p,
    5348           18 :                                      build_int_cst (itype, is_mod ? -d : 1),
    5349              :                                      build_int_cst (itype, 0));
    5350           18 :             append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5351              : 
    5352           18 :             tree result = vect_recog_temp_ssa_var (itype, NULL);
    5353           27 :             return gimple_build_assign (result, PLUS_EXPR, is_mod ? r : q,
    5354           18 :                                         val2);
    5355              :           }
    5356              :       }
    5357              :     default:
    5358              :       return NULL;
    5359              :     }
    5360              : }
    5361              : 
    5362              : /* Detect a signed division by a constant that wouldn't be
    5363              :    otherwise vectorized:
    5364              : 
    5365              :    type a_t, b_t;
    5366              : 
    5367              :    S1 a_t = b_t / N;
    5368              : 
    5369              :   where type 'type' is an integral type and N is a constant.
    5370              : 
    5371              :   Similarly handle modulo by a constant:
    5372              : 
    5373              :    S4 a_t = b_t % N;
    5374              : 
    5375              :   Input/Output:
    5376              : 
    5377              :   * STMT_VINFO: The stmt from which the pattern search begins,
    5378              :     i.e. the division stmt.  S1 is replaced by if N is a power
    5379              :     of two constant and type is signed:
    5380              :   S3  y_t = b_t < 0 ? N - 1 : 0;
    5381              :   S2  x_t = b_t + y_t;
    5382              :   S1' a_t = x_t >> log2 (N);
    5383              : 
    5384              :     S4 is replaced if N is a power of two constant and
    5385              :     type is signed by (where *_T temporaries have unsigned type):
    5386              :   S9  y_T = b_t < 0 ? -1U : 0U;
    5387              :   S8  z_T = y_T >> (sizeof (type_t) * CHAR_BIT - log2 (N));
    5388              :   S7  z_t = (type) z_T;
    5389              :   S6  w_t = b_t + z_t;
    5390              :   S5  x_t = w_t & (N - 1);
    5391              :   S4' a_t = x_t - z_t;
    5392              : 
    5393              :   Output:
    5394              : 
    5395              :   * TYPE_OUT: The type of the output of this pattern.
    5396              : 
    5397              :   * Return value: A new stmt that will be used to replace the division
    5398              :     S1 or modulo S4 stmt.  */
    5399              : 
    5400              : static gimple *
    5401     33484704 : vect_recog_divmod_pattern (vec_info *vinfo,
    5402              :                            stmt_vec_info stmt_vinfo, tree *type_out)
    5403              : {
    5404     33484704 :   tree oprnd0, oprnd1, vectype, itype, cond;
    5405     33484704 :   gimple *pattern_stmt = NULL;
    5406     33484704 :   gassign *def_stmt = NULL;
    5407     33484704 :   enum tree_code rhs_code;
    5408     33484704 :   optab optab;
    5409     33484704 :   tree q, cst;
    5410     33484704 :   int prec;
    5411              : 
    5412     33484704 :   auto *last_stmt = dyn_cast<gassign *> (stmt_vinfo->stmt);
    5413     23499756 :   if (!last_stmt
    5414              :       /* The pattern will disrupt the reduction chain with multiple uses.  */
    5415     23499756 :       || vect_is_reduction (stmt_vinfo))
    5416              :     return NULL;
    5417              : 
    5418     23387648 :   rhs_code = gimple_assign_rhs_code (last_stmt);
    5419     23387648 :   switch (rhs_code)
    5420              :     {
    5421       311741 :     case TRUNC_DIV_EXPR:
    5422       311741 :     case EXACT_DIV_EXPR:
    5423       311741 :     case TRUNC_MOD_EXPR:
    5424       311741 :     case FLOOR_MOD_EXPR:
    5425       311741 :     case FLOOR_DIV_EXPR:
    5426       311741 :     case CEIL_MOD_EXPR:
    5427       311741 :     case CEIL_DIV_EXPR:
    5428       311741 :     case ROUND_MOD_EXPR:
    5429       311741 :     case ROUND_DIV_EXPR:
    5430       311741 :       break;
    5431              :     default:
    5432              :       return NULL;
    5433              :     }
    5434              : 
    5435       311741 :   oprnd0 = gimple_assign_rhs1 (last_stmt);
    5436       311741 :   oprnd1 = gimple_assign_rhs2 (last_stmt);
    5437       311741 :   itype = TREE_TYPE (oprnd0);
    5438       311741 :   if (TREE_CODE (oprnd0) != SSA_NAME
    5439       290217 :       || TREE_CODE (oprnd1) != INTEGER_CST
    5440       166305 :       || TREE_CODE (itype) != INTEGER_TYPE
    5441       478046 :       || !type_has_mode_precision_p (itype))
    5442              :     return NULL;
    5443              : 
    5444       166291 :   scalar_int_mode itype_mode = SCALAR_INT_TYPE_MODE (itype);
    5445       166291 :   vectype = get_vectype_for_scalar_type (vinfo, itype);
    5446       166291 :   if (vectype == NULL_TREE)
    5447              :     return NULL;
    5448              : 
    5449       134495 :   if (optimize_bb_for_size_p (gimple_bb (last_stmt)))
    5450              :     {
    5451              :       /* If the target can handle vectorized division or modulo natively,
    5452              :          don't attempt to optimize this, since native division is likely
    5453              :          to give smaller code.  */
    5454         2298 :       optab = optab_for_tree_code (rhs_code, vectype, optab_default);
    5455         2298 :       if (optab != unknown_optab
    5456         2298 :           && can_implement_p (optab, TYPE_MODE (vectype)))
    5457              :         return NULL;
    5458              :     }
    5459              : 
    5460       134495 :   prec = TYPE_PRECISION (itype);
    5461              : 
    5462       268990 :   bool is_flclrd_moddiv_p
    5463       134495 :     = rhs_code == FLOOR_MOD_EXPR || rhs_code == FLOOR_DIV_EXPR
    5464              :     || rhs_code == CEIL_MOD_EXPR || rhs_code == CEIL_DIV_EXPR
    5465       133874 :     || rhs_code == ROUND_MOD_EXPR || rhs_code == ROUND_DIV_EXPR;
    5466       134495 :   if (integer_pow2p (oprnd1))
    5467              :     {
    5468        78181 :       if ((TYPE_UNSIGNED (itype)
    5469           36 :            && (rhs_code == FLOOR_MOD_EXPR || rhs_code == FLOOR_DIV_EXPR))
    5470        78217 :           || tree_int_cst_sgn (oprnd1) != 1)
    5471              :         return NULL;
    5472              : 
    5473              :       /* Pattern detected.  */
    5474        78181 :       vect_pattern_detected ("vect_recog_divmod_pattern", last_stmt);
    5475              : 
    5476        78181 :       *type_out = vectype;
    5477              : 
    5478              :       /* OPRND1 is a positive power of two here and the dividend of an
    5479              :          exact division is a multiple of it, so the right shift is exact
    5480              :          also for negative dividends and needs no rounding fixup.  */
    5481        78181 :       if (rhs_code == EXACT_DIV_EXPR)
    5482              :         {
    5483        58814 :           tree shift = build_int_cst (itype, tree_log2 (oprnd1));
    5484        58814 :           pattern_stmt
    5485        58814 :             = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL),
    5486              :                                    RSHIFT_EXPR, oprnd0, shift);
    5487        58814 :           gimple_set_location (pattern_stmt, gimple_location (last_stmt));
    5488        58814 :           return pattern_stmt;
    5489              :         }
    5490              : 
    5491              :       /* Check if the target supports this internal function.  */
    5492        19367 :       internal_fn ifn = IFN_DIV_POW2;
    5493        19367 :       if (!TYPE_UNSIGNED (itype)
    5494        19367 :           && direct_internal_fn_supported_p (ifn, vectype, OPTIMIZE_FOR_SPEED))
    5495              :         {
    5496            0 :           tree shift = build_int_cst (itype, tree_log2 (oprnd1));
    5497              : 
    5498            0 :           tree var_div = vect_recog_temp_ssa_var (itype, NULL);
    5499            0 :           gimple *div_stmt = gimple_build_call_internal (ifn, 2, oprnd0, shift);
    5500            0 :           gimple_call_set_lhs (div_stmt, var_div);
    5501            0 :           if (rhs_code == TRUNC_MOD_EXPR || is_flclrd_moddiv_p)
    5502              :             {
    5503            0 :               append_pattern_def_seq (vinfo, stmt_vinfo, div_stmt);
    5504            0 :               tree t1 = vect_recog_temp_ssa_var (itype, NULL);
    5505            0 :               def_stmt
    5506            0 :                 = gimple_build_assign (t1, LSHIFT_EXPR, var_div, shift);
    5507            0 :               append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5508            0 :               tree r = vect_recog_temp_ssa_var (itype, NULL);
    5509            0 :               pattern_stmt = gimple_build_assign (r, MINUS_EXPR, oprnd0, t1);
    5510            0 :               if (is_flclrd_moddiv_p)
    5511              :                 {
    5512            0 :                   append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt);
    5513            0 :                   pattern_stmt
    5514            0 :                     = add_code_for_floorceilround_divmod (vectype, vinfo,
    5515              :                                                           stmt_vinfo, rhs_code,
    5516              :                                                           var_div, r, oprnd0,
    5517              :                                                           oprnd1, itype);
    5518            0 :                   if (pattern_stmt == NULL)
    5519              :                     return NULL;
    5520              :                 }
    5521              :             }
    5522              :           else
    5523              :             pattern_stmt = div_stmt;
    5524            0 :           gimple_set_location (pattern_stmt, gimple_location (last_stmt));
    5525              : 
    5526            0 :           return pattern_stmt;
    5527              :         }
    5528              : 
    5529        19367 :       cond = vect_recog_temp_ssa_var (boolean_type_node, NULL);
    5530        19367 :       def_stmt = gimple_build_assign (cond, LT_EXPR, oprnd0,
    5531              :                                       build_int_cst (itype, 0));
    5532        19367 :       append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt,
    5533              :                               truth_type_for (vectype), itype);
    5534        19367 :       tree div_result = NULL_TREE;
    5535        19367 :       if (rhs_code == TRUNC_DIV_EXPR
    5536        19367 :           || rhs_code == FLOOR_DIV_EXPR
    5537         2518 :           || rhs_code == CEIL_DIV_EXPR
    5538         2518 :           || rhs_code == ROUND_DIV_EXPR)
    5539              :         {
    5540        16892 :           tree var = vect_recog_temp_ssa_var (itype, NULL);
    5541        16892 :           tree shift;
    5542        16892 :           def_stmt
    5543        16892 :             = gimple_build_assign (var, COND_EXPR, cond,
    5544              :                                    fold_build2 (MINUS_EXPR, itype, oprnd1,
    5545              :                                                 build_int_cst (itype, 1)),
    5546              :                                    build_int_cst (itype, 0));
    5547        16892 :           append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5548        16892 :           var = vect_recog_temp_ssa_var (itype, NULL);
    5549        16892 :           def_stmt
    5550        16892 :             = gimple_build_assign (var, PLUS_EXPR, oprnd0,
    5551              :                                    gimple_assign_lhs (def_stmt));
    5552        16892 :           append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5553              : 
    5554        16892 :           shift = build_int_cst (itype, tree_log2 (oprnd1));
    5555        16892 :           div_result = vect_recog_temp_ssa_var (itype, NULL);
    5556        16892 :           pattern_stmt
    5557        16892 :             = gimple_build_assign (div_result, RSHIFT_EXPR, var, shift);
    5558              :         }
    5559        19367 :       if (rhs_code == TRUNC_MOD_EXPR || is_flclrd_moddiv_p)
    5560              :         {
    5561         2653 :           if (rhs_code == FLOOR_DIV_EXPR
    5562              :               || rhs_code == CEIL_DIV_EXPR
    5563         2653 :               || rhs_code == ROUND_DIV_EXPR)
    5564          178 :             append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt);
    5565              : 
    5566         2653 :           tree signmask;
    5567         2653 :           if (compare_tree_int (oprnd1, 2) == 0)
    5568              :             {
    5569         1339 :               signmask = vect_recog_temp_ssa_var (itype, NULL);
    5570         1339 :               def_stmt = gimple_build_assign (signmask, COND_EXPR, cond,
    5571              :                                               build_int_cst (itype, 1),
    5572              :                                               build_int_cst (itype, 0));
    5573         1339 :               append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5574              :             }
    5575              :           else
    5576              :             {
    5577         1314 :               tree utype
    5578         1314 :                 = build_nonstandard_integer_type (prec, 1);
    5579         1314 :               tree vecutype = get_vectype_for_scalar_type (vinfo, utype);
    5580         1314 :               tree shift
    5581         1314 :                 = build_int_cst (utype, GET_MODE_BITSIZE (itype_mode)
    5582         1314 :                                         - tree_log2 (oprnd1));
    5583         1314 :               tree var = vect_recog_temp_ssa_var (utype, NULL);
    5584              : 
    5585         1314 :               def_stmt = gimple_build_assign (var, COND_EXPR, cond,
    5586              :                                               build_int_cst (utype, -1),
    5587              :                                               build_int_cst (utype, 0));
    5588         1314 :               append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vecutype);
    5589         1314 :               var = vect_recog_temp_ssa_var (utype, NULL);
    5590         1314 :               def_stmt = gimple_build_assign (var, RSHIFT_EXPR,
    5591              :                                               gimple_assign_lhs (def_stmt),
    5592              :                                               shift);
    5593         1314 :               append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vecutype);
    5594         1314 :               signmask = vect_recog_temp_ssa_var (itype, NULL);
    5595         1314 :               def_stmt
    5596         1314 :                 = gimple_build_assign (signmask, NOP_EXPR, var);
    5597         1314 :               append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5598              :             }
    5599         2653 :           def_stmt
    5600         2653 :             = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL),
    5601              :                                    PLUS_EXPR, oprnd0, signmask);
    5602         2653 :           append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5603         2653 :           def_stmt
    5604         2653 :             = gimple_build_assign (vect_recog_temp_ssa_var (itype, NULL),
    5605              :                                    BIT_AND_EXPR, gimple_assign_lhs (def_stmt),
    5606              :                                    fold_build2 (MINUS_EXPR, itype, oprnd1,
    5607              :                                                 build_int_cst (itype, 1)));
    5608         2653 :           append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5609              : 
    5610         2653 :           tree r = vect_recog_temp_ssa_var (itype, NULL);
    5611         2653 :           pattern_stmt
    5612         2653 :             = gimple_build_assign (r, MINUS_EXPR, gimple_assign_lhs (def_stmt),
    5613              :                                    signmask);
    5614         2653 :           if (is_flclrd_moddiv_p)
    5615              :             {
    5616          272 :               append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt);
    5617          272 :               pattern_stmt
    5618          272 :                 = add_code_for_floorceilround_divmod (vectype, vinfo,
    5619              :                                                       stmt_vinfo, rhs_code,
    5620              :                                                       div_result, r, oprnd0,
    5621              :                                                       oprnd1, itype);
    5622          272 :               if (pattern_stmt == NULL)
    5623              :                 return NULL;
    5624              :             }
    5625              :         }
    5626              : 
    5627        19209 :       return pattern_stmt;
    5628              :     }
    5629              : 
    5630        56314 :   if ((cst = uniform_integer_cst_p (oprnd1))
    5631        56314 :       && TYPE_UNSIGNED (itype)
    5632              :       && rhs_code == TRUNC_DIV_EXPR
    5633        33138 :       && vectype
    5634        75728 :       && targetm.vectorize.preferred_div_as_shifts_over_mult (vectype))
    5635              :     {
    5636              :       /* We can use the relationship:
    5637              : 
    5638              :            x // N == ((x+N+2) // (N+1) + x) // (N+1)  for 0 <= x < N(N+3)
    5639              : 
    5640              :          to optimize cases where N+1 is a power of 2, and where // (N+1)
    5641              :          is therefore a shift right.  When operating in modes that are
    5642              :          multiples of a byte in size, there are two cases:
    5643              : 
    5644              :          (1) N(N+3) is not representable, in which case the question
    5645              :              becomes whether the replacement expression overflows.
    5646              :              It is enough to test that x+N+2 does not overflow,
    5647              :              i.e. that x < MAX-(N+1).
    5648              : 
    5649              :          (2) N(N+3) is representable, in which case it is the (only)
    5650              :              bound that we need to check.
    5651              : 
    5652              :          ??? For now we just handle the case where // (N+1) is a shift
    5653              :          right by half the precision, since some architectures can
    5654              :          optimize the associated addition and shift combinations
    5655              :          into single instructions.  */
    5656              : 
    5657         8226 :       auto wcst = wi::to_wide (cst);
    5658         8226 :       int pow = wi::exact_log2 (wcst + 1);
    5659         8226 :       if (pow == prec / 2)
    5660              :         {
    5661              :           /* Check that no overflow will occur.  If we don't have range
    5662              :              information we can't perform the optimization.  */
    5663              : 
    5664          515 :           int_range_max r;
    5665         1030 :           if (get_range_query (cfun)->range_of_expr (r, oprnd0, last_stmt)
    5666          515 :               && !r.undefined_p ())
    5667              :             {
    5668          515 :               wide_int max = r.upper_bound ();
    5669          515 :               wide_int one = wi::shwi (1, prec);
    5670          515 :               wide_int adder = wi::add (one, wi::lshift (one, pow));
    5671          515 :               wi::overflow_type ovf;
    5672          515 :               wi::add (max, adder, UNSIGNED, &ovf);
    5673          515 :               if (ovf == wi::OVF_NONE)
    5674              :                 {
    5675          321 :                   *type_out = vectype;
    5676          321 :                   tree tadder = wide_int_to_tree (itype, adder);
    5677          321 :                   tree rshift = wide_int_to_tree (itype, pow);
    5678              : 
    5679          321 :                   tree new_lhs1 = vect_recog_temp_ssa_var (itype, NULL);
    5680          321 :                   gassign *patt1
    5681          321 :                     = gimple_build_assign (new_lhs1, PLUS_EXPR, oprnd0, tadder);
    5682          321 :                   append_pattern_def_seq (vinfo, stmt_vinfo, patt1, vectype);
    5683              : 
    5684          321 :                   tree new_lhs2 = vect_recog_temp_ssa_var (itype, NULL);
    5685          321 :                   patt1 = gimple_build_assign (new_lhs2, RSHIFT_EXPR, new_lhs1,
    5686              :                                                rshift);
    5687          321 :                   append_pattern_def_seq (vinfo, stmt_vinfo, patt1, vectype);
    5688              : 
    5689          321 :                   tree new_lhs3 = vect_recog_temp_ssa_var (itype, NULL);
    5690          321 :                   patt1 = gimple_build_assign (new_lhs3, PLUS_EXPR, new_lhs2,
    5691              :                                                oprnd0);
    5692          321 :                   append_pattern_def_seq (vinfo, stmt_vinfo, patt1, vectype);
    5693              : 
    5694          321 :                   tree new_lhs4 = vect_recog_temp_ssa_var (itype, NULL);
    5695          321 :                   pattern_stmt = gimple_build_assign (new_lhs4, RSHIFT_EXPR,
    5696              :                                                       new_lhs3, rshift);
    5697              : 
    5698          321 :                   return pattern_stmt;
    5699              :                 }
    5700          515 :             }
    5701          515 :         }
    5702              :     }
    5703              : 
    5704        55993 :   if (prec > HOST_BITS_PER_WIDE_INT
    5705        55993 :       || integer_zerop (oprnd1))
    5706              :     return NULL;
    5707              : 
    5708        55696 :   if (!can_mult_highpart_p (TYPE_MODE (vectype), TYPE_UNSIGNED (itype)))
    5709              :     return NULL;
    5710              : 
    5711        23524 :   if (TYPE_UNSIGNED (itype))
    5712              :     {
    5713        18475 :       unsigned HOST_WIDE_INT mh, ml;
    5714        18475 :       int pre_shift, post_shift;
    5715        18475 :       unsigned HOST_WIDE_INT d = (TREE_INT_CST_LOW (oprnd1)
    5716        18475 :                                   & GET_MODE_MASK (itype_mode));
    5717        18475 :       tree t1, t2, t3, t4;
    5718              : 
    5719        18475 :       if (d >= (HOST_WIDE_INT_1U << (prec - 1)))
    5720              :         /* FIXME: Can transform this into oprnd0 >= oprnd1 ? 1 : 0.  */
    5721           45 :         return NULL;
    5722              : 
    5723              :       /* Find a suitable multiplier and right shift count instead of
    5724              :          directly dividing by D.  */
    5725        18430 :       mh = choose_multiplier (d, prec, prec, &ml, &post_shift);
    5726              : 
    5727              :       /* If the suggested multiplier is more than PREC bits, we can do better
    5728              :          for even divisors, using an initial right shift.  */
    5729        18430 :       if (mh != 0 && (d & 1) == 0)
    5730              :         {
    5731         1555 :           pre_shift = ctz_or_zero (d);
    5732         1555 :           mh = choose_multiplier (d >> pre_shift, prec, prec - pre_shift,
    5733              :                                   &ml, &post_shift);
    5734         1555 :           gcc_assert (!mh);
    5735              :         }
    5736              :       else
    5737              :         pre_shift = 0;
    5738              : 
    5739         1707 :       if (mh != 0)
    5740              :         {
    5741         1707 :           if (post_shift - 1 >= prec)
    5742              :             return NULL;
    5743              : 
    5744              :           /* t1 = oprnd0 h* ml;
    5745              :              t2 = oprnd0 - t1;
    5746              :              t3 = t2 >> 1;
    5747              :              t4 = t1 + t3;
    5748              :              q = t4 >> (post_shift - 1);  */
    5749         1707 :           t1 = vect_recog_temp_ssa_var (itype, NULL);
    5750         1707 :           def_stmt = gimple_build_assign (t1, MULT_HIGHPART_EXPR, oprnd0,
    5751         1707 :                                           build_int_cst (itype, ml));
    5752         1707 :           append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5753              : 
    5754         1707 :           t2 = vect_recog_temp_ssa_var (itype, NULL);
    5755         1707 :           def_stmt
    5756         1707 :             = gimple_build_assign (t2, MINUS_EXPR, oprnd0, t1);
    5757         1707 :           append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5758              : 
    5759         1707 :           t3 = vect_recog_temp_ssa_var (itype, NULL);
    5760         1707 :           def_stmt
    5761         1707 :             = gimple_build_assign (t3, RSHIFT_EXPR, t2, integer_one_node);
    5762         1707 :           append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5763              : 
    5764         1707 :           t4 = vect_recog_temp_ssa_var (itype, NULL);
    5765         1707 :           def_stmt
    5766         1707 :             = gimple_build_assign (t4, PLUS_EXPR, t1, t3);
    5767              : 
    5768         1707 :           if (post_shift != 1)
    5769              :             {
    5770         1707 :               append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5771              : 
    5772         1707 :               q = vect_recog_temp_ssa_var (itype, NULL);
    5773         1707 :               pattern_stmt
    5774         1707 :                 = gimple_build_assign (q, RSHIFT_EXPR, t4,
    5775         1707 :                                        build_int_cst (itype, post_shift - 1));
    5776              :             }
    5777              :           else
    5778              :             {
    5779              :               q = t4;
    5780              :               pattern_stmt = def_stmt;
    5781              :             }
    5782              :         }
    5783              :       else
    5784              :         {
    5785        16723 :           if (pre_shift >= prec || post_shift >= prec)
    5786              :             return NULL;
    5787              : 
    5788              :           /* t1 = oprnd0 >> pre_shift;
    5789              :              t2 = t1 h* ml;
    5790              :              q = t2 >> post_shift;  */
    5791        16723 :           if (pre_shift)
    5792              :             {
    5793         1555 :               t1 = vect_recog_temp_ssa_var (itype, NULL);
    5794         1555 :               def_stmt
    5795         1555 :                 = gimple_build_assign (t1, RSHIFT_EXPR, oprnd0,
    5796         1555 :                                        build_int_cst (integer_type_node, pre_shift));
    5797         1555 :               append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5798              :             }
    5799              :           else
    5800              :             t1 = oprnd0;
    5801              : 
    5802        16723 :           t2 = vect_recog_temp_ssa_var (itype, NULL);
    5803        16723 :           def_stmt = gimple_build_assign (t2, MULT_HIGHPART_EXPR, t1,
    5804        16723 :                                           build_int_cst (itype, ml));
    5805              : 
    5806        16723 :           if (post_shift)
    5807              :             {
    5808        16677 :               append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5809              : 
    5810        16677 :               q = vect_recog_temp_ssa_var (itype, NULL);
    5811        16677 :               def_stmt
    5812        16677 :                 = gimple_build_assign (q, RSHIFT_EXPR, t2,
    5813        16677 :                                        build_int_cst (itype, post_shift));
    5814              :             }
    5815              :           else
    5816              :             q = t2;
    5817              : 
    5818              :           pattern_stmt = def_stmt;
    5819              :         }
    5820              :     }
    5821              :   else
    5822              :     {
    5823         5049 :       unsigned HOST_WIDE_INT ml;
    5824         5049 :       int post_shift;
    5825         5049 :       HOST_WIDE_INT d = TREE_INT_CST_LOW (oprnd1);
    5826         5049 :       unsigned HOST_WIDE_INT abs_d;
    5827         5049 :       bool add = false;
    5828         5049 :       tree t1, t2, t3, t4;
    5829              : 
    5830              :       /* Give up for -1.  */
    5831         5049 :       if (d == -1)
    5832            0 :         return NULL;
    5833              : 
    5834              :       /* Since d might be INT_MIN, we have to cast to
    5835              :          unsigned HOST_WIDE_INT before negating to avoid
    5836              :          undefined signed overflow.  */
    5837         5049 :       abs_d = (d >= 0
    5838         5049 :                ? (unsigned HOST_WIDE_INT) d
    5839              :                : - (unsigned HOST_WIDE_INT) d);
    5840              : 
    5841              :       /* n rem d = n rem -d */
    5842         5049 :       if (rhs_code == TRUNC_MOD_EXPR && d < 0)
    5843              :         {
    5844            0 :           d = abs_d;
    5845            0 :           oprnd1 = build_int_cst (itype, abs_d);
    5846              :         }
    5847         5049 :       if (HOST_BITS_PER_WIDE_INT >= prec
    5848         5049 :           && abs_d == HOST_WIDE_INT_1U << (prec - 1))
    5849              :         /* This case is not handled correctly below.  */
    5850              :         return NULL;
    5851              : 
    5852         5049 :       choose_multiplier (abs_d, prec, prec - 1, &ml, &post_shift);
    5853         5049 :       if (ml >= HOST_WIDE_INT_1U << (prec - 1))
    5854              :         {
    5855         1658 :           add = true;
    5856         1658 :           ml |= HOST_WIDE_INT_M1U << (prec - 1);
    5857              :         }
    5858         5049 :       if (post_shift >= prec)
    5859              :         return NULL;
    5860              : 
    5861              :       /* t1 = oprnd0 h* ml;  */
    5862         5049 :       t1 = vect_recog_temp_ssa_var (itype, NULL);
    5863         5049 :       def_stmt = gimple_build_assign (t1, MULT_HIGHPART_EXPR, oprnd0,
    5864         5049 :                                       build_int_cst (itype, ml));
    5865              : 
    5866         5049 :       if (add)
    5867              :         {
    5868              :           /* t2 = t1 + oprnd0;  */
    5869         1658 :           append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5870         1658 :           t2 = vect_recog_temp_ssa_var (itype, NULL);
    5871         1658 :           def_stmt = gimple_build_assign (t2, PLUS_EXPR, t1, oprnd0);
    5872              :         }
    5873              :       else
    5874              :         t2 = t1;
    5875              : 
    5876         5049 :       if (post_shift)
    5877              :         {
    5878              :           /* t3 = t2 >> post_shift;  */
    5879         4148 :           append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5880         4148 :           t3 = vect_recog_temp_ssa_var (itype, NULL);
    5881         4148 :           def_stmt = gimple_build_assign (t3, RSHIFT_EXPR, t2,
    5882         4148 :                                           build_int_cst (itype, post_shift));
    5883              :         }
    5884              :       else
    5885              :         t3 = t2;
    5886              : 
    5887         5049 :       int msb = 1;
    5888         5049 :       int_range_max r;
    5889        10098 :       get_range_query (cfun)->range_of_expr (r, oprnd0);
    5890         5049 :       if (!r.varying_p () && !r.undefined_p ())
    5891              :         {
    5892         3169 :           if (!wi::neg_p (r.lower_bound (), TYPE_SIGN (itype)))
    5893              :             msb = 0;
    5894          746 :           else if (wi::neg_p (r.upper_bound (), TYPE_SIGN (itype)))
    5895              :             msb = -1;
    5896              :         }
    5897              : 
    5898         2423 :       if (msb == 0 && d >= 0)
    5899              :         {
    5900              :           /* q = t3;  */
    5901              :           q = t3;
    5902              :           pattern_stmt = def_stmt;
    5903              :         }
    5904              :       else
    5905              :         {
    5906              :           /* t4 = oprnd0 >> (prec - 1);
    5907              :              or if we know from VRP that oprnd0 >= 0
    5908              :              t4 = 0;
    5909              :              or if we know from VRP that oprnd0 < 0
    5910              :              t4 = -1;  */
    5911         2694 :           append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5912         2694 :           t4 = vect_recog_temp_ssa_var (itype, NULL);
    5913         2694 :           if (msb != 1)
    5914           76 :             def_stmt = gimple_build_assign (t4, INTEGER_CST,
    5915           76 :                                             build_int_cst (itype, msb));
    5916              :           else
    5917         2618 :             def_stmt = gimple_build_assign (t4, RSHIFT_EXPR, oprnd0,
    5918         2618 :                                             build_int_cst (itype, prec - 1));
    5919         2694 :           append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5920              : 
    5921              :           /* q = t3 - t4;  or q = t4 - t3;  */
    5922         2694 :           q = vect_recog_temp_ssa_var (itype, NULL);
    5923         5202 :           pattern_stmt = gimple_build_assign (q, MINUS_EXPR, d < 0 ? t4 : t3,
    5924              :                                               d < 0 ? t3 : t4);
    5925              :         }
    5926         5049 :     }
    5927              : 
    5928        23479 :   if (rhs_code == TRUNC_MOD_EXPR || is_flclrd_moddiv_p)
    5929              :     {
    5930        10562 :       tree r, t1;
    5931              : 
    5932              :       /* We divided.  Now finish by:
    5933              :          t1 = q * oprnd1;
    5934              :          r = oprnd0 - t1;  */
    5935        10562 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt);
    5936              : 
    5937        10562 :       t1 = vect_recog_temp_ssa_var (itype, NULL);
    5938        10562 :       def_stmt = gimple_build_assign (t1, MULT_EXPR, q, oprnd1);
    5939        10562 :       append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt);
    5940              : 
    5941        10562 :       r = vect_recog_temp_ssa_var (itype, NULL);
    5942        10562 :       pattern_stmt = gimple_build_assign (r, MINUS_EXPR, oprnd0, t1);
    5943              : 
    5944        10562 :       if (is_flclrd_moddiv_p)
    5945              :         {
    5946          148 :         append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt);
    5947          148 :         pattern_stmt
    5948          148 :           = add_code_for_floorceilround_divmod (vectype, vinfo, stmt_vinfo,
    5949              :                                                 rhs_code, q, r, oprnd0, oprnd1,
    5950              :                                                 itype);
    5951          148 :         if (pattern_stmt == NULL)
    5952              :           return NULL;
    5953              :         }
    5954              :     }
    5955              : 
    5956              :   /* Pattern detected.  */
    5957        23479 :   vect_pattern_detected ("vect_recog_divmod_pattern", last_stmt);
    5958              : 
    5959        23479 :   *type_out = vectype;
    5960        23479 :   return pattern_stmt;
    5961              : }
    5962              : 
    5963              : /* Detects pattern with a modulo operation (S1) where both arguments
    5964              :    are variables of integral type.
    5965              :    The statement is replaced by division, multiplication, and subtraction.
    5966              :    The last statement (S4) is returned.
    5967              : 
    5968              :    Example:
    5969              :    S1 c_t = a_t % b_t;
    5970              : 
    5971              :    is replaced by
    5972              :    S2 x_t = a_t / b_t;
    5973              :    S3 y_t = x_t * b_t;
    5974              :    S4 z_t = a_t - y_t;  */
    5975              : 
    5976              : static gimple *
    5977     33508827 : vect_recog_mod_var_pattern (vec_info *vinfo,
    5978              :                             stmt_vec_info stmt_vinfo, tree *type_out)
    5979              : {
    5980     33508827 :   tree oprnd0, oprnd1, vectype, itype;
    5981     33508827 :   gimple *pattern_stmt, *def_stmt;
    5982     33508827 :   enum tree_code rhs_code;
    5983              : 
    5984     33508827 :   auto *last_stmt = dyn_cast<gassign *> (STMT_VINFO_STMT (stmt_vinfo));
    5985     23523879 :   if (!last_stmt || vect_is_reduction (stmt_vinfo))
    5986              :     return NULL;
    5987              : 
    5988     23411771 :   rhs_code = gimple_assign_rhs_code (last_stmt);
    5989     23411771 :   if (rhs_code != TRUNC_MOD_EXPR)
    5990              :     return NULL;
    5991              : 
    5992        84187 :   oprnd0 = gimple_assign_rhs1 (last_stmt);
    5993        84187 :   oprnd1 = gimple_assign_rhs2 (last_stmt);
    5994        84187 :   itype = TREE_TYPE (oprnd0);
    5995        84187 :   if (TREE_CODE (oprnd0) != SSA_NAME
    5996        73885 :       || TREE_CODE (oprnd1) != SSA_NAME
    5997        59491 :       || TREE_CODE (itype) != INTEGER_TYPE)
    5998              :     return NULL;
    5999              : 
    6000        59319 :   vectype = get_vectype_for_scalar_type (vinfo, itype);
    6001              : 
    6002        59319 :   if (!vectype
    6003        50244 :       || target_has_vecop_for_code (TRUNC_MOD_EXPR, vectype)
    6004        50244 :       || !target_has_vecop_for_code (TRUNC_DIV_EXPR, vectype)
    6005            0 :       || !target_has_vecop_for_code (MULT_EXPR, vectype)
    6006        59319 :       || !target_has_vecop_for_code (MINUS_EXPR, vectype))
    6007              :     return NULL;
    6008              : 
    6009            0 :   tree q, tmp, r;
    6010            0 :   q = vect_recog_temp_ssa_var (itype, NULL);
    6011            0 :   def_stmt = gimple_build_assign (q, TRUNC_DIV_EXPR, oprnd0, oprnd1);
    6012            0 :   append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vectype);
    6013              : 
    6014            0 :   tmp = vect_recog_temp_ssa_var (itype, NULL);
    6015            0 :   def_stmt = gimple_build_assign (tmp, MULT_EXPR, q, oprnd1);
    6016            0 :   append_pattern_def_seq (vinfo, stmt_vinfo, def_stmt, vectype);
    6017              : 
    6018            0 :   r = vect_recog_temp_ssa_var (itype, NULL);
    6019            0 :   pattern_stmt = gimple_build_assign (r, MINUS_EXPR, oprnd0, tmp);
    6020              : 
    6021              :   /* Pattern detected.  */
    6022            0 :   *type_out = vectype;
    6023            0 :   vect_pattern_detected ("vect_recog_mod_var_pattern", last_stmt);
    6024              : 
    6025            0 :   return pattern_stmt;
    6026              : }
    6027              : 
    6028              : 
    6029              : /* Return the proper type for converting bool VAR into
    6030              :    an integer value or NULL_TREE if no such type exists.
    6031              :    The type is chosen so that the converted value has the
    6032              :    same number of elements as VAR's vector type.  */
    6033              : 
    6034              : static tree
    6035      4549711 : integer_type_for_mask (tree var, vec_info *vinfo, vect_def_type *dt = nullptr)
    6036              : {
    6037      4549711 :   if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (var)))
    6038              :     return NULL_TREE;
    6039              : 
    6040      2049151 :   stmt_vec_info def_stmt_info = vinfo->lookup_def (var);
    6041      2049151 :   if (dt)
    6042              :     {
    6043       390336 :       if (!def_stmt_info)
    6044         3694 :         *dt = vect_external_def;
    6045              :       else
    6046       386642 :         *dt = STMT_VINFO_DEF_TYPE (def_stmt_info);
    6047              :     }
    6048       390336 :   if (!def_stmt_info
    6049      1951449 :       || STMT_VINFO_DEF_TYPE (def_stmt_info) == vect_external_def
    6050      3610264 :       || !vect_use_mask_type_p (def_stmt_info))
    6051              :     return NULL_TREE;
    6052              : 
    6053      1213553 :   return build_nonstandard_integer_type (def_stmt_info->mask_precision, 1);
    6054              : }
    6055              : 
    6056              : /* Function vect_recog_gcond_pattern
    6057              : 
    6058              :    Try to find pattern like following:
    6059              : 
    6060              :      if (a op b)
    6061              : 
    6062              :    where operator 'op' is not != and convert it to an adjusted boolean pattern
    6063              : 
    6064              :      mask = a op b
    6065              :      if (mask != 0)
    6066              : 
    6067              :    and set the mask type on MASK.
    6068              : 
    6069              :    Input:
    6070              : 
    6071              :    * STMT_VINFO: The stmt at the end from which the pattern
    6072              :                  search begins, i.e. cast of a bool to
    6073              :                  an integer type.
    6074              : 
    6075              :    Output:
    6076              : 
    6077              :    * TYPE_OUT: The type of the output of this pattern.
    6078              : 
    6079              :    * Return value: A new stmt that will be used to replace the pattern.  */
    6080              : 
    6081              : static gimple *
    6082     33586043 : vect_recog_gcond_pattern (vec_info *vinfo,
    6083              :                          stmt_vec_info stmt_vinfo, tree *type_out)
    6084              : {
    6085              :   /* Currently we only support this for loop vectorization and when multiple
    6086              :      exits.  */
    6087     33586043 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    6088      4761289 :   if (!loop_vinfo || !LOOP_VINFO_EARLY_BREAKS (loop_vinfo))
    6089              :     return NULL;
    6090              : 
    6091      1757830 :   gimple *last_stmt = STMT_VINFO_STMT (stmt_vinfo);
    6092      1757830 :   gcond* cond = NULL;
    6093      1757830 :   if (!(cond = dyn_cast <gcond *> (last_stmt)))
    6094              :     return NULL;
    6095              : 
    6096       416479 :   auto lhs = gimple_cond_lhs (cond);
    6097       416479 :   auto rhs = gimple_cond_rhs (cond);
    6098       416479 :   auto code = gimple_cond_code (cond);
    6099              : 
    6100       416479 :   tree scalar_type = TREE_TYPE (lhs);
    6101       416479 :   if (VECTOR_TYPE_P (scalar_type))
    6102              :     return NULL;
    6103              : 
    6104              :   /* If the input is a boolean then try to figure out the precision that the
    6105              :      vector type should use.  We cannot use the scalar precision as this would
    6106              :      later mismatch.  This is similar to what recog_bool does.  */
    6107       416479 :   if (VECT_SCALAR_BOOLEAN_TYPE_P (scalar_type))
    6108              :     {
    6109        10228 :       if (tree stype = integer_type_for_mask (lhs, vinfo))
    6110       416479 :         scalar_type = stype;
    6111              :     }
    6112              : 
    6113       416479 :   tree vectype = get_mask_type_for_scalar_type (vinfo, scalar_type);
    6114       416479 :   if (vectype == NULL_TREE)
    6115              :     return NULL;
    6116              : 
    6117       394566 :   tree new_lhs = vect_recog_temp_ssa_var (boolean_type_node, NULL);
    6118       394566 :   gimple *new_stmt = gimple_build_assign (new_lhs, code, lhs, rhs);
    6119       394566 :   append_pattern_def_seq (vinfo, stmt_vinfo, new_stmt, vectype, scalar_type);
    6120              : 
    6121       394566 :   gimple *pattern_stmt
    6122       394566 :     = gimple_build_cond (NE_EXPR, new_lhs,
    6123       394566 :                          build_int_cst (TREE_TYPE (new_lhs), 0),
    6124              :                          NULL_TREE, NULL_TREE);
    6125       394566 :   *type_out = vectype;
    6126       394566 :   vect_pattern_detected ("vect_recog_gcond_pattern", last_stmt);
    6127       394566 :   return pattern_stmt;
    6128              : }
    6129              : 
    6130              : 
    6131              : /* A helper for vect_recog_mask_conversion_pattern.  Build
    6132              :    conversion of MASK to a type suitable for masking VECTYPE.
    6133              :    Built statement gets required vectype and is appended to
    6134              :    a pattern sequence of STMT_VINFO.
    6135              :    If VECTYPE is a mask type, SCALAR_TYPE_FOR_MASK is the scalar type
    6136              :    from which it was derived.
    6137              : 
    6138              :    Return converted mask.  */
    6139              : 
    6140              : static tree
    6141       146610 : build_mask_conversion (vec_info *vinfo,
    6142              :                        tree mask, tree vectype, stmt_vec_info stmt_vinfo,
    6143              :                        tree scalar_type_for_mask = NULL_TREE)
    6144              : {
    6145       146610 :   gimple *stmt;
    6146       146610 :   tree masktype, tmp;
    6147              : 
    6148       233898 :   gcc_assert (!scalar_type_for_mask == !VECTOR_BOOLEAN_TYPE_P (vectype));
    6149              : 
    6150       146610 :   masktype = truth_type_for (vectype);
    6151       146610 :   tmp = vect_recog_temp_ssa_var (TREE_TYPE (masktype), NULL);
    6152       146610 :   stmt = gimple_build_assign (tmp, CONVERT_EXPR, mask);
    6153       233898 :   append_pattern_def_seq (vinfo, stmt_vinfo,
    6154              :                           stmt, masktype,
    6155              :                           scalar_type_for_mask
    6156        87288 :                           ? scalar_type_for_mask : TREE_TYPE (vectype));
    6157              : 
    6158       146610 :   return tmp;
    6159              : }
    6160              : 
    6161              : 
    6162              : /* Return MASK if MASK is suitable for masking an operation on vectors
    6163              :    of type VECTYPE, otherwise convert it into such a form and return
    6164              :    the result.  Associate any conversion statements with STMT_INFO's
    6165              :    pattern.  If VECTYPE is a mask type, SCALAR_TYPE_FOR_MASK is the scalar
    6166              :    type from which it was derived.  */
    6167              : 
    6168              : static tree
    6169        81050 : vect_convert_mask_for_vectype (tree mask, tree vectype,
    6170              :                                stmt_vec_info stmt_info, vec_info *vinfo,
    6171              :                                tree scalar_type_for_mask = NULL_TREE)
    6172              : {
    6173        81050 :   tree mask_type = integer_type_for_mask (mask, vinfo);
    6174        81050 :   if (mask_type)
    6175              :     {
    6176        81050 :       tree mask_vectype = get_mask_type_for_scalar_type (vinfo, mask_type);
    6177        81050 :       if (mask_vectype
    6178       162100 :           && maybe_ne (TYPE_VECTOR_SUBPARTS (vectype),
    6179        99983 :                        TYPE_VECTOR_SUBPARTS (mask_vectype)))
    6180        62117 :         mask = build_mask_conversion (vinfo, mask, vectype, stmt_info,
    6181              :                                       scalar_type_for_mask);
    6182              :     }
    6183        81050 :   return mask;
    6184              : }
    6185              : 
    6186              : 
    6187              : /* Function vect_recog_bool_pattern
    6188              : 
    6189              :    Try to find pattern like following:
    6190              : 
    6191              :      bool a_b, b_b, c_b, d_b, e_b;
    6192              :      TYPE f_T;
    6193              :    loop:
    6194              :      S1  a_b = x1 CMP1 y1;
    6195              :      S2  b_b = x2 CMP2 y2;
    6196              :      S3  c_b = a_b & b_b;
    6197              :      S4  d_b = x3 CMP3 y3;
    6198              :      S5  e_b = c_b | d_b;
    6199              :      S6  f_T = (TYPE) e_b;
    6200              : 
    6201              :    where type 'TYPE' is an integral type.  Or a similar pattern
    6202              :    ending in
    6203              : 
    6204              :      S6  f_Y = e_b ? r_Y : s_Y;
    6205              : 
    6206              :    as results from if-conversion of a complex condition.
    6207              : 
    6208              :    Input:
    6209              : 
    6210              :    * STMT_VINFO: The stmt at the end from which the pattern
    6211              :                  search begins, i.e. cast of a bool to
    6212              :                  an integer type.
    6213              : 
    6214              :    Output:
    6215              : 
    6216              :    * TYPE_OUT: The type of the output of this pattern.
    6217              : 
    6218              :    * Return value: A new stmt that will be used to replace the pattern.
    6219              : 
    6220              :         Assuming size of TYPE is the same as size of all comparisons
    6221              :         (otherwise some casts would be added where needed), the above
    6222              :         sequence we create related pattern stmts:
    6223              :         S1'  a_T = x1 CMP1 y1 ? 1 : 0;
    6224              :         S3'  c_T = x2 CMP2 y2 ? a_T : 0;
    6225              :         S4'  d_T = x3 CMP3 y3 ? 1 : 0;
    6226              :         S5'  e_T = c_T | d_T;
    6227              :         S6'  f_T = e_T;
    6228              : 
    6229              :         Instead of the above S3' we could emit:
    6230              :         S2'  b_T = x2 CMP2 y2 ? 1 : 0;
    6231              :         S3'  c_T = a_T | b_T;
    6232              :         but the above is more efficient.  */
    6233              : 
    6234              : static gimple *
    6235     33586043 : vect_recog_bool_pattern (vec_info *vinfo,
    6236              :                          stmt_vec_info stmt_vinfo, tree *type_out)
    6237              : {
    6238     33586043 :   auto last_stmt = dyn_cast<gassign *> (stmt_vinfo->stmt);
    6239     23995661 :   enum tree_code rhs_code;
    6240     23995661 :   tree var, lhs, rhs, vectype;
    6241     23995661 :   gimple *pattern_stmt;
    6242              : 
    6243     23995661 :   if (!last_stmt)
    6244              :     return NULL;
    6245              : 
    6246     23995661 :   var = gimple_assign_rhs1 (last_stmt);
    6247     23995661 :   lhs = gimple_assign_lhs (last_stmt);
    6248     23995661 :   rhs_code = gimple_assign_rhs_code (last_stmt);
    6249              : 
    6250     23995661 :   if (rhs_code == VIEW_CONVERT_EXPR)
    6251       200279 :     var = TREE_OPERAND (var, 0);
    6252              : 
    6253     23995661 :   if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (var)))
    6254              :     return NULL;
    6255              : 
    6256       709901 :   hash_set<gimple *> bool_stmts;
    6257              : 
    6258       709901 :   if (CONVERT_EXPR_CODE_P (rhs_code)
    6259              :       || rhs_code == VIEW_CONVERT_EXPR
    6260              :       || rhs_code == FLOAT_EXPR)
    6261              :     {
    6262       176112 :       if (! (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
    6263         2179 :              || SCALAR_FLOAT_TYPE_P (TREE_TYPE (lhs)))
    6264       174595 :           || VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (lhs)))
    6265              :         return NULL;
    6266        81926 :       vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (lhs));
    6267              : 
    6268        81926 :       tree type = integer_type_for_mask (var, vinfo);
    6269        81926 :       tree cst0, cst1, tmp;
    6270              : 
    6271        81926 :       if (!type)
    6272              :         return NULL;
    6273              : 
    6274              :       /* We may directly use cond with narrowed type to avoid multiple cond
    6275              :          exprs with following result packing and perform single cond with
    6276              :          packed mask instead.  In case of widening we better make cond first
    6277              :          and then extract results.  */
    6278        39918 :       if (TYPE_MODE (type) == TYPE_MODE (TREE_TYPE (lhs)))
    6279        25945 :         type = TREE_TYPE (lhs);
    6280              : 
    6281        39918 :       cst0 = build_int_cst (type, 0);
    6282        39918 :       cst1 = build_int_cst (type, 1);
    6283        39918 :       tmp = vect_recog_temp_ssa_var (type, NULL);
    6284        39918 :       pattern_stmt = gimple_build_assign (tmp, COND_EXPR, var, cst1, cst0);
    6285              : 
    6286        39918 :       if (!useless_type_conversion_p (type, TREE_TYPE (lhs)))
    6287              :         {
    6288        13973 :           tree new_vectype = get_vectype_for_scalar_type (vinfo, type);
    6289        13973 :           append_pattern_def_seq (vinfo, stmt_vinfo,
    6290              :                                   pattern_stmt, new_vectype);
    6291              : 
    6292        13973 :           lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
    6293        13973 :           pattern_stmt
    6294        27570 :             = gimple_build_assign (lhs, (rhs_code == FLOAT_EXPR
    6295              :                                          ? FLOAT_EXPR : CONVERT_EXPR), tmp);
    6296              :         }
    6297              : 
    6298        39918 :       *type_out = vectype;
    6299        39918 :       vect_pattern_detected ("vect_recog_bool_pattern", last_stmt);
    6300              : 
    6301        39918 :       return pattern_stmt;
    6302              :     }
    6303              :   else if (rhs_code == COND_EXPR
    6304       149507 :            && TREE_CODE (var) == SSA_NAME)
    6305              :     {
    6306       149507 :       vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (lhs));
    6307       149507 :       if (vectype == NULL_TREE)
    6308              :         return NULL;
    6309              : 
    6310              :       /* Build a scalar type for the boolean result that when
    6311              :          vectorized matches the vector type of the result in
    6312              :          size and number of elements.  */
    6313       135125 :       unsigned prec
    6314       135125 :         = vector_element_size (tree_to_poly_uint64 (TYPE_SIZE (vectype)),
    6315              :                                TYPE_VECTOR_SUBPARTS (vectype));
    6316              : 
    6317       135125 :       tree type
    6318       270250 :         = build_nonstandard_integer_type (prec,
    6319       135125 :                                           TYPE_UNSIGNED (TREE_TYPE (var)));
    6320       135125 :       if (get_vectype_for_scalar_type (vinfo, type) == NULL_TREE)
    6321              :         return NULL;
    6322              : 
    6323       135125 :       enum vect_def_type dt;
    6324       135125 :       if (integer_type_for_mask (var, vinfo))
    6325              :         return NULL;
    6326        35862 :       else if (TREE_CODE (TREE_TYPE (var)) == BOOLEAN_TYPE
    6327        35862 :                && vect_is_simple_use (var, vinfo, &dt)
    6328        35862 :                && (dt == vect_external_def
    6329        35855 :                    || dt == vect_constant_def))
    6330              :         {
    6331              :           /* If the condition is already a boolean then manually convert it to a
    6332              :              mask of the given integer type but don't set a vectype.  */
    6333         1588 :           tree lhs_ivar = vect_recog_temp_ssa_var (type, NULL);
    6334         1588 :           pattern_stmt = gimple_build_assign (lhs_ivar, COND_EXPR, var,
    6335              :                                               build_all_ones_cst (type),
    6336              :                                               build_zero_cst (type));
    6337         1588 :           append_inv_pattern_def_seq (vinfo, pattern_stmt);
    6338         1588 :           var = lhs_ivar;
    6339              :         }
    6340              : 
    6341        35862 :       tree lhs_var = vect_recog_temp_ssa_var (boolean_type_node, NULL);
    6342        35862 :       pattern_stmt = gimple_build_assign (lhs_var, NE_EXPR, var,
    6343        35862 :                                           build_zero_cst (TREE_TYPE (var)));
    6344              : 
    6345        35862 :       tree new_vectype = get_mask_type_for_scalar_type (vinfo, TREE_TYPE (var));
    6346        35862 :       if (!new_vectype)
    6347              :         return NULL;
    6348              : 
    6349        35862 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, new_vectype,
    6350        35862 :                               TREE_TYPE (var));
    6351              : 
    6352        35862 :       lhs_var = vect_convert_mask_for_vectype (lhs_var, vectype, stmt_vinfo,
    6353              :                                                vinfo);
    6354              : 
    6355        35862 :       lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
    6356        35862 :       pattern_stmt
    6357        71724 :         = gimple_build_assign (lhs, COND_EXPR, lhs_var,
    6358              :                                gimple_assign_rhs2 (last_stmt),
    6359              :                                gimple_assign_rhs3 (last_stmt));
    6360        35862 :       *type_out = vectype;
    6361        35862 :       vect_pattern_detected ("vect_recog_bool_pattern", last_stmt);
    6362              : 
    6363        35862 :       return pattern_stmt;
    6364              :     }
    6365       471957 :   else if (rhs_code == BIT_NOT_EXPR
    6366       471957 :            && !vect_use_mask_type_p (stmt_vinfo))
    6367              :     {
    6368              :       /* When we have a bool data inversion rewrite that to an XOR to
    6369              :          cope with the fact that we'll use a wider vector element type.  */
    6370        10473 :       lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
    6371        10473 :       pattern_stmt
    6372        10473 :         = gimple_build_assign (lhs, BIT_XOR_EXPR, var,
    6373        10473 :                                build_all_ones_cst (TREE_TYPE (var)));
    6374        10473 :       *type_out = NULL_TREE;
    6375        10473 :       vect_pattern_detected ("vect_recog_bool_pattern", last_stmt);
    6376              : 
    6377        10473 :       return pattern_stmt;
    6378              :     }
    6379       461484 :   else if ((rhs_code == BIT_XOR_EXPR
    6380              :             || rhs_code == BIT_AND_EXPR
    6381       461484 :             || rhs_code == BIT_IOR_EXPR)
    6382       366539 :            && TREE_CODE (var) == SSA_NAME)
    6383              :     {
    6384       366539 :       tree rhs2 = gimple_assign_rhs2 (last_stmt);
    6385       366539 :       if (TREE_CODE (rhs2) != SSA_NAME)
    6386              :         return NULL;
    6387       366539 :       tree lhs_type = integer_type_for_mask (lhs, vinfo);
    6388       366539 :       if (!lhs_type)
    6389              :         return NULL;
    6390       195168 :       vectype = get_mask_type_for_scalar_type (vinfo, lhs_type);
    6391       195168 :       if (!vectype)
    6392              :         return NULL;
    6393       195168 :       vect_def_type dt1, dt2;
    6394       195168 :       tree rhs1_type = integer_type_for_mask (var, vinfo, &dt1);
    6395       195168 :       tree rhs2_type = integer_type_for_mask (rhs2, vinfo, &dt2);
    6396       195168 :       if ((rhs1_type || dt1 == vect_external_def)
    6397       178813 :           && (rhs2_type || dt2 == vect_external_def))
    6398              :         return NULL;
    6399              :       /* When one input is a mask and the other is not create a pattern
    6400              :          stmt sequence that creates a mask for the non-mask input and
    6401              :          convert it to one suitable for the output mask used.  */
    6402        40592 :       if (rhs1_type && !rhs2_type)
    6403              :         {
    6404        24237 :           tree rhs1_vectype = get_mask_type_for_scalar_type (vinfo, rhs1_type);
    6405        24237 :           if (!rhs1_vectype)
    6406              :             return NULL;
    6407        24237 :           tree rhs2_vectype = get_vectype_for_scalar_type (vinfo,
    6408        24237 :                                                            TREE_TYPE (rhs2));
    6409        24237 :           if (!rhs2_vectype)
    6410              :             return NULL;
    6411        24237 :           tree new_vectype = truth_type_for (rhs2_vectype);
    6412        24237 :           tree tem = vect_recog_temp_ssa_var (TREE_TYPE (new_vectype), NULL);
    6413        24237 :           pattern_stmt = gimple_build_assign (tem, NE_EXPR, rhs2,
    6414              :                                               build_zero_cst
    6415        24237 :                                                 (TREE_TYPE (rhs2)));
    6416        24237 :           append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt,
    6417        24237 :                                   new_vectype, TREE_TYPE (new_vectype));
    6418        24237 :           rhs2 = vect_convert_mask_for_vectype (tem, rhs1_vectype,
    6419              :                                                 stmt_vinfo, vinfo, rhs1_type);
    6420              :         }
    6421        16355 :       else if (!rhs1_type && rhs2_type)
    6422              :         {
    6423        16355 :           tree rhs2_vectype = get_mask_type_for_scalar_type (vinfo, rhs2_type);
    6424        16355 :           if (!rhs2_vectype)
    6425              :             return NULL;
    6426        16355 :           tree rhs1_vectype = get_vectype_for_scalar_type (vinfo,
    6427        16355 :                                                            TREE_TYPE (var));
    6428        16355 :           if (!rhs1_vectype)
    6429              :             return NULL;
    6430        16355 :           tree new_vectype = truth_type_for (rhs1_vectype);
    6431        16355 :           tree tem = vect_recog_temp_ssa_var (TREE_TYPE (new_vectype), NULL);
    6432        16355 :           pattern_stmt = gimple_build_assign (tem, NE_EXPR, var,
    6433              :                                               build_zero_cst
    6434        16355 :                                                 (TREE_TYPE (var)));
    6435        16355 :           append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt,
    6436        16355 :                                   new_vectype, TREE_TYPE (new_vectype));
    6437        16355 :           var = vect_convert_mask_for_vectype (tem, rhs2_vectype,
    6438              :                                                stmt_vinfo, vinfo, rhs2_type);
    6439              :         }
    6440        40592 :       lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
    6441        40592 :       pattern_stmt = gimple_build_assign (lhs, rhs_code, var, rhs2);
    6442        40592 :       vect_pattern_detected ("vect_recog_bool_pattern", last_stmt);
    6443        40592 :       *type_out = vectype;
    6444        40592 :       return pattern_stmt;
    6445              :     }
    6446        94945 :   else if (rhs_code == SSA_NAME
    6447        26213 :            && STMT_VINFO_DATA_REF (stmt_vinfo))
    6448              :     {
    6449         8040 :       stmt_vec_info pattern_stmt_info;
    6450         8040 :       vectype = get_vectype_for_scalar_type (vinfo, TREE_TYPE (lhs));
    6451         8040 :       if (!vectype || !VECTOR_MODE_P (TYPE_MODE (vectype)))
    6452              :         return NULL;
    6453              : 
    6454         8040 :       tree type = integer_type_for_mask (var, vinfo);
    6455         8040 :       if (!type)
    6456              :         return NULL;
    6457              : 
    6458         4596 :       var = vect_convert_mask_for_vectype (var, vectype, stmt_vinfo, vinfo);
    6459              : 
    6460         4596 :       tree cst0 = build_int_cst (TREE_TYPE (vectype), 0);
    6461         4596 :       tree cst1 = build_int_cst (TREE_TYPE (vectype), 1);
    6462         4596 :       rhs = vect_recog_temp_ssa_var (TREE_TYPE (vectype), NULL);
    6463         4596 :       pattern_stmt = gimple_build_assign (rhs, COND_EXPR, var, cst1, cst0);
    6464         4596 :       append_pattern_def_seq (vinfo, stmt_vinfo, pattern_stmt, vectype);
    6465              : 
    6466         4596 :       lhs = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (vectype), lhs);
    6467         4596 :       pattern_stmt = gimple_build_assign (lhs, SSA_NAME, rhs);
    6468         4596 :       pattern_stmt_info = vinfo->add_stmt (pattern_stmt);
    6469         4596 :       vinfo->move_dr (pattern_stmt_info, stmt_vinfo);
    6470         4596 :       *type_out = vectype;
    6471         4596 :       vect_pattern_detected ("vect_recog_bool_pattern", last_stmt);
    6472              : 
    6473         4596 :       return pattern_stmt;
    6474              :     }
    6475              :   else
    6476              :     return NULL;
    6477       709901 : }
    6478              : 
    6479              : 
    6480              : /* Function vect_recog_mask_conversion_pattern
    6481              : 
    6482              :    Try to find statements which require boolean type
    6483              :    conversion.  Additional conversion statements are
    6484              :    added to handle such cases.  For example:
    6485              : 
    6486              :    bool m_1, m_2, m_3;
    6487              :    int i_4, i_5;
    6488              :    double d_6, d_7;
    6489              :    char c_1, c_2, c_3;
    6490              : 
    6491              :    S1   m_1 = i_4 > i_5;
    6492              :    S2   m_2 = d_6 < d_7;
    6493              :    S3   m_3 = m_1 & m_2;
    6494              :    S4   c_1 = m_3 ? c_2 : c_3;
    6495              : 
    6496              :    Will be transformed into:
    6497              : 
    6498              :    S1   m_1 = i_4 > i_5;
    6499              :    S2   m_2 = d_6 < d_7;
    6500              :    S3'' m_2' = (_Bool[bitsize=32])m_2
    6501              :    S3'  m_3' = m_1 & m_2';
    6502              :    S4'' m_3'' = (_Bool[bitsize=8])m_3'
    6503              :    S4'  c_1' = m_3'' ? c_2 : c_3;  */
    6504              : 
    6505              : static gimple *
    6506     33611944 : vect_recog_mask_conversion_pattern (vec_info *vinfo,
    6507              :                                     stmt_vec_info stmt_vinfo, tree *type_out)
    6508              : {
    6509     33611944 :   gimple *last_stmt = stmt_vinfo->stmt;
    6510     33611944 :   enum tree_code rhs_code;
    6511     33611944 :   tree lhs = NULL_TREE, rhs1, rhs2, tmp, rhs1_type, rhs2_type;
    6512     33611944 :   tree vectype1, vectype2;
    6513     33611944 :   stmt_vec_info pattern_stmt_info;
    6514              : 
    6515              :   /* Check for MASK_LOAD and MASK_STORE as well as COND_OP calls requiring mask
    6516              :      conversion.  */
    6517     33611944 :   if (is_gimple_call (last_stmt)
    6518     33611944 :       && gimple_call_internal_p (last_stmt))
    6519              :     {
    6520       144293 :       gcall *pattern_stmt;
    6521              : 
    6522       144293 :       internal_fn ifn = gimple_call_internal_fn (last_stmt);
    6523       144293 :       int mask_argno = internal_fn_mask_index (ifn);
    6524       144293 :       if (mask_argno < 0)
    6525              :         return NULL;
    6526              : 
    6527        15505 :       bool store_p = internal_store_fn_p (ifn);
    6528        15505 :       bool load_p = internal_store_fn_p (ifn);
    6529        15505 :       if (store_p)
    6530              :         {
    6531         2796 :           int rhs_index = internal_fn_stored_value_index (ifn);
    6532         2796 :           tree rhs = gimple_call_arg (last_stmt, rhs_index);
    6533         2796 :           vectype1 = get_vectype_for_scalar_type (vinfo, TREE_TYPE (rhs));
    6534              :         }
    6535              :       else
    6536              :         {
    6537        12709 :           lhs = gimple_call_lhs (last_stmt);
    6538        12709 :           if (!lhs)
    6539              :             return NULL;
    6540        12709 :           vectype1 = get_vectype_for_scalar_type (vinfo, TREE_TYPE (lhs));
    6541              :         }
    6542              : 
    6543        15505 :       if (!vectype1)
    6544              :         return NULL;
    6545              : 
    6546        15185 :       tree mask_arg = gimple_call_arg (last_stmt, mask_argno);
    6547        15185 :       tree mask_arg_type = integer_type_for_mask (mask_arg, vinfo);
    6548        15185 :       if (mask_arg_type)
    6549              :         {
    6550        13416 :           vectype2 = get_mask_type_for_scalar_type (vinfo, mask_arg_type);
    6551              : 
    6552        13416 :           if (!vectype2
    6553        13416 :               || known_eq (TYPE_VECTOR_SUBPARTS (vectype1),
    6554              :                            TYPE_VECTOR_SUBPARTS (vectype2)))
    6555              :             return NULL;
    6556              :         }
    6557         1769 :       else if (store_p || load_p)
    6558              :         return NULL;
    6559              : 
    6560         6433 :       tmp = build_mask_conversion (vinfo, mask_arg, vectype1, stmt_vinfo);
    6561              : 
    6562         6433 :       auto_vec<tree, 8> args;
    6563         6433 :       unsigned int nargs = gimple_call_num_args (last_stmt);
    6564         6433 :       args.safe_grow (nargs, true);
    6565        38598 :       for (unsigned int i = 0; i < nargs; ++i)
    6566        25732 :         args[i] = ((int) i == mask_argno
    6567        25732 :                    ? tmp
    6568        19299 :                    : gimple_call_arg (last_stmt, i));
    6569         6433 :       pattern_stmt = gimple_build_call_internal_vec (ifn, args);
    6570              : 
    6571         6433 :       if (!store_p)
    6572              :         {
    6573         6126 :           lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
    6574         6126 :           gimple_call_set_lhs (pattern_stmt, lhs);
    6575              :         }
    6576              : 
    6577         6126 :       if (load_p || store_p)
    6578          307 :         gimple_call_set_nothrow (pattern_stmt, true);
    6579              : 
    6580         6433 :       pattern_stmt_info = vinfo->add_stmt (pattern_stmt);
    6581         6433 :       if (STMT_VINFO_DATA_REF (stmt_vinfo))
    6582         1957 :         vinfo->move_dr (pattern_stmt_info, stmt_vinfo);
    6583              : 
    6584         6433 :       *type_out = vectype1;
    6585         6433 :       vect_pattern_detected ("vect_recog_mask_conversion_pattern", last_stmt);
    6586              : 
    6587         6433 :       return pattern_stmt;
    6588         6433 :     }
    6589              : 
    6590     33467651 :   auto assign = dyn_cast<gassign *> (last_stmt);
    6591     24021562 :   if (!assign)
    6592              :     return NULL;
    6593              : 
    6594     24021562 :   gimple *pattern_stmt;
    6595     24021562 :   lhs = gimple_assign_lhs (assign);
    6596     24021562 :   rhs1 = gimple_assign_rhs1 (assign);
    6597     24021562 :   rhs_code = gimple_assign_rhs_code (assign);
    6598              : 
    6599              :   /* Check for cond expression requiring mask conversion.  */
    6600     24021562 :   if (rhs_code == COND_EXPR)
    6601              :     {
    6602       132416 :       vectype1 = get_vectype_for_scalar_type (vinfo, TREE_TYPE (lhs));
    6603              : 
    6604       132416 :       gcc_assert (! COMPARISON_CLASS_P (rhs1));
    6605       132416 :       if (TREE_CODE (rhs1) == SSA_NAME)
    6606              :         {
    6607       132416 :           rhs1_type = integer_type_for_mask (rhs1, vinfo);
    6608       132416 :           if (!rhs1_type)
    6609              :             return NULL;
    6610              :         }
    6611              :       else
    6612              :         return NULL;
    6613              : 
    6614       120386 :       vectype2 = get_mask_type_for_scalar_type (vinfo, rhs1_type);
    6615              : 
    6616       120386 :       if (!vectype1 || !vectype2)
    6617              :         return NULL;
    6618              : 
    6619              :       /* Continue if a conversion is needed.  Also continue if we have
    6620              :          a comparison whose vector type would normally be different from
    6621              :          VECTYPE2 when considered in isolation.  In that case we'll
    6622              :          replace the comparison with an SSA name (so that we can record
    6623              :          its vector type) and behave as though the comparison was an SSA
    6624              :          name from the outset.  */
    6625       118034 :       if (known_eq (TYPE_VECTOR_SUBPARTS (vectype1),
    6626              :                     TYPE_VECTOR_SUBPARTS (vectype2)))
    6627              :         return NULL;
    6628              : 
    6629        44043 :       if (maybe_ne (TYPE_VECTOR_SUBPARTS (vectype1),
    6630        88086 :                     TYPE_VECTOR_SUBPARTS (vectype2)))
    6631        44043 :         tmp = build_mask_conversion (vinfo, rhs1, vectype1, stmt_vinfo);
    6632              :       else
    6633              :         tmp = rhs1;
    6634              : 
    6635        44043 :       lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
    6636        44043 :       pattern_stmt = gimple_build_assign (lhs, COND_EXPR, tmp,
    6637              :                                           gimple_assign_rhs2 (last_stmt),
    6638              :                                           gimple_assign_rhs3 (last_stmt));
    6639              : 
    6640        44043 :       *type_out = vectype1;
    6641        44043 :       vect_pattern_detected ("vect_recog_mask_conversion_pattern", last_stmt);
    6642              : 
    6643        44043 :       return pattern_stmt;
    6644              :     }
    6645              : 
    6646              :   /* Now check for binary boolean operations requiring conversion for
    6647              :      one of operands.  */
    6648     23889146 :   if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (lhs)))
    6649              :     return NULL;
    6650              : 
    6651      1850428 :   if (rhs_code != BIT_IOR_EXPR
    6652              :       && rhs_code != BIT_XOR_EXPR
    6653      1850428 :       && rhs_code != BIT_AND_EXPR
    6654      1524481 :       && TREE_CODE_CLASS (rhs_code) != tcc_comparison)
    6655              :     return NULL;
    6656              : 
    6657      1664433 :   rhs2 = gimple_assign_rhs2 (last_stmt);
    6658              : 
    6659      1664433 :   rhs1_type = integer_type_for_mask (rhs1, vinfo);
    6660      1664433 :   rhs2_type = integer_type_for_mask (rhs2, vinfo);
    6661              : 
    6662      1664433 :   if (!rhs1_type || !rhs2_type
    6663      1664433 :       || TYPE_PRECISION (rhs1_type) == TYPE_PRECISION (rhs2_type))
    6664              :     return NULL;
    6665              : 
    6666        34017 :   if (TYPE_PRECISION (rhs1_type) < TYPE_PRECISION (rhs2_type))
    6667              :     {
    6668        26446 :       vectype1 = get_mask_type_for_scalar_type (vinfo, rhs1_type);
    6669        26446 :       if (!vectype1)
    6670              :         return NULL;
    6671        26446 :       rhs2 = build_mask_conversion (vinfo, rhs2, vectype1, stmt_vinfo,
    6672              :                                     rhs1_type);
    6673              :     }
    6674              :   else
    6675              :     {
    6676         7571 :       vectype1 = get_mask_type_for_scalar_type (vinfo, rhs2_type);
    6677         7571 :       if (!vectype1)
    6678              :         return NULL;
    6679         7571 :       rhs1 = build_mask_conversion (vinfo, rhs1, vectype1, stmt_vinfo,
    6680              :                                     rhs2_type);
    6681              :     }
    6682              : 
    6683        34017 :   lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
    6684        34017 :   pattern_stmt = gimple_build_assign (lhs, rhs_code, rhs1, rhs2);
    6685              : 
    6686        34017 :   *type_out = vectype1;
    6687        34017 :   vect_pattern_detected ("vect_recog_mask_conversion_pattern", last_stmt);
    6688              : 
    6689        34017 :   return pattern_stmt;
    6690              : }
    6691              : 
    6692              : /* STMT_INFO is a load or store.  If the load or store is conditional, return
    6693              :    the boolean condition under which it occurs, otherwise return null.  */
    6694              : 
    6695              : static tree
    6696       103893 : vect_get_load_store_mask (stmt_vec_info stmt_info)
    6697              : {
    6698       103893 :   if (gassign *def_assign = dyn_cast <gassign *> (stmt_info->stmt))
    6699              :     {
    6700       102840 :       gcc_assert (gimple_assign_single_p (def_assign));
    6701              :       return NULL_TREE;
    6702              :     }
    6703              : 
    6704         1053 :   if (gcall *def_call = dyn_cast <gcall *> (stmt_info->stmt))
    6705              :     {
    6706         1053 :       internal_fn ifn = gimple_call_internal_fn (def_call);
    6707         1053 :       int mask_index = internal_fn_mask_index (ifn);
    6708         1053 :       return gimple_call_arg (def_call, mask_index);
    6709              :     }
    6710              : 
    6711            0 :   gcc_unreachable ();
    6712              : }
    6713              : 
    6714              : /* Return the equivalent of:
    6715              : 
    6716              :      fold_convert (TYPE, VALUE)
    6717              : 
    6718              :    with the expectation that the operation will be vectorized.
    6719              :    If new statements are needed, add them as pattern statements
    6720              :    to STMT_INFO.  */
    6721              : 
    6722              : static tree
    6723            0 : vect_add_conversion_to_pattern (vec_info *vinfo,
    6724              :                                 tree type, tree value, stmt_vec_info stmt_info)
    6725              : {
    6726            0 :   if (useless_type_conversion_p (type, TREE_TYPE (value)))
    6727              :     return value;
    6728              : 
    6729            0 :   tree new_value = vect_recog_temp_ssa_var (type, NULL);
    6730            0 :   gassign *conversion = gimple_build_assign (new_value, CONVERT_EXPR, value);
    6731            0 :   append_pattern_def_seq (vinfo, stmt_info, conversion,
    6732              :                           get_vectype_for_scalar_type (vinfo, type));
    6733            0 :   return new_value;
    6734              : }
    6735              : 
    6736              : /* Try to convert STMT_INFO into a call to a gather load or scatter store
    6737              :    internal function.  Return the final statement on success and set
    6738              :    *TYPE_OUT to the vector type being loaded or stored.
    6739              : 
    6740              :    This function only handles gathers and scatters that were recognized
    6741              :    as such from the outset (indicated by STMT_VINFO_GATHER_SCATTER_P).  */
    6742              : 
    6743              : static gimple *
    6744     33611944 : vect_recog_gather_scatter_pattern (vec_info *vinfo,
    6745              :                                    stmt_vec_info stmt_info, tree *type_out)
    6746              : {
    6747              :   /* Currently we only support this for loop vectorization.  */
    6748     33611944 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    6749      4774025 :   if (!loop_vinfo)
    6750              :     return NULL;
    6751              : 
    6752              :   /* Make sure that we're looking at a gather load or scatter store.  */
    6753      4774025 :   data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
    6754      4774025 :   if (!dr || !STMT_VINFO_GATHER_SCATTER_P (stmt_info))
    6755              :     return NULL;
    6756              : 
    6757              :   /* Get the boolean that controls whether the load or store happens.
    6758              :      This is null if the operation is unconditional.  */
    6759       103893 :   tree mask = vect_get_load_store_mask (stmt_info);
    6760              : 
    6761              :   /* DR analysis nailed down the vector type for the access.  */
    6762       103893 :   tree gs_vectype = STMT_VINFO_VECTYPE (stmt_info);
    6763              : 
    6764              :   /* Make sure that the target supports an appropriate internal
    6765              :      function for the gather/scatter operation.  */
    6766       103893 :   gather_scatter_info gs_info;
    6767       103893 :   if (!vect_check_gather_scatter (stmt_info, gs_vectype, loop_vinfo, &gs_info)
    6768       103893 :       || gs_info.ifn == IFN_LAST)
    6769              :     return NULL;
    6770              : 
    6771              :   /* Convert the mask to the right form.  */
    6772            0 :   if (mask)
    6773            0 :     mask = vect_convert_mask_for_vectype (mask, gs_vectype, stmt_info,
    6774              :                                           loop_vinfo);
    6775            0 :   else if (gs_info.ifn == IFN_MASK_SCATTER_STORE
    6776            0 :            || gs_info.ifn == IFN_MASK_GATHER_LOAD
    6777            0 :            || gs_info.ifn == IFN_MASK_LEN_SCATTER_STORE
    6778            0 :            || gs_info.ifn == IFN_MASK_LEN_GATHER_LOAD)
    6779            0 :     mask = build_int_cst (TREE_TYPE (truth_type_for (gs_vectype)), -1);
    6780              : 
    6781              :   /* Get the invariant base and non-invariant offset, converting the
    6782              :      latter to the same width as the vector elements.  */
    6783            0 :   tree base = gs_info.base;
    6784            0 :   tree offset_type = TREE_TYPE (gs_info.offset_vectype);
    6785            0 :   tree offset = vect_add_conversion_to_pattern (vinfo, offset_type,
    6786              :                                                 gs_info.offset, stmt_info);
    6787              : 
    6788              :   /* Build the new pattern statement.  */
    6789            0 :   tree scale = size_int (gs_info.scale);
    6790            0 :   gcall *pattern_stmt;
    6791              : 
    6792            0 :   if (DR_IS_READ (dr))
    6793              :     {
    6794            0 :       tree zero = build_zero_cst (gs_info.element_type);
    6795            0 :       if (mask != NULL)
    6796              :         {
    6797            0 :           int elsval = MASK_LOAD_ELSE_ZERO;
    6798              : 
    6799            0 :           tree vec_els
    6800            0 :             = vect_get_mask_load_else (elsval, TREE_TYPE (gs_vectype));
    6801            0 :           pattern_stmt = gimple_build_call_internal (gs_info.ifn, 7, base,
    6802              :                                                      gs_info.alias_ptr,
    6803              :                                                      offset, scale, zero, mask,
    6804              :                                                      vec_els);
    6805              :         }
    6806              :       else
    6807            0 :         pattern_stmt = gimple_build_call_internal (gs_info.ifn, 5, base,
    6808              :                                                    gs_info.alias_ptr,
    6809              :                                                    offset, scale, zero);
    6810            0 :       tree lhs = gimple_get_lhs (stmt_info->stmt);
    6811            0 :       tree load_lhs = vect_recog_temp_ssa_var (TREE_TYPE (lhs), NULL);
    6812            0 :       gimple_call_set_lhs (pattern_stmt, load_lhs);
    6813              :     }
    6814              :   else
    6815              :     {
    6816            0 :       tree rhs = vect_get_store_rhs (stmt_info);
    6817            0 :       if (mask != NULL)
    6818            0 :         pattern_stmt = gimple_build_call_internal (gs_info.ifn, 6,
    6819              :                                                    base, gs_info.alias_ptr,
    6820              :                                                    offset, scale, rhs, mask);
    6821              :       else
    6822            0 :         pattern_stmt = gimple_build_call_internal (gs_info.ifn, 5,
    6823              :                                                    base, gs_info.alias_ptr,
    6824              :                                                    offset, scale, rhs);
    6825              :     }
    6826            0 :   gimple_call_set_nothrow (pattern_stmt, true);
    6827              : 
    6828              :   /* Copy across relevant vectorization info and associate DR with the
    6829              :      new pattern statement instead of the original statement.  */
    6830            0 :   stmt_vec_info pattern_stmt_info = loop_vinfo->add_stmt (pattern_stmt);
    6831            0 :   loop_vinfo->move_dr (pattern_stmt_info, stmt_info);
    6832              : 
    6833            0 :   *type_out = gs_vectype;
    6834            0 :   vect_pattern_detected ("gather/scatter pattern", stmt_info->stmt);
    6835              : 
    6836            0 :   return pattern_stmt;
    6837              : }
    6838              : 
    6839              : /* Helper method of vect_recog_cond_store_pattern,  checks to see if COND_ARG
    6840              :    is points to a load statement that reads the same data as that of
    6841              :    STORE_VINFO.  */
    6842              : 
    6843              : static bool
    6844        34689 : vect_cond_store_pattern_same_ref (vec_info *vinfo,
    6845              :                                   stmt_vec_info store_vinfo, tree cond_arg)
    6846              : {
    6847        34689 :   stmt_vec_info load_stmt_vinfo = vinfo->lookup_def (cond_arg);
    6848        34689 :   if (!load_stmt_vinfo
    6849        19406 :       || !STMT_VINFO_DATA_REF (load_stmt_vinfo)
    6850        12011 :       || DR_IS_WRITE (STMT_VINFO_DATA_REF (load_stmt_vinfo))
    6851        46700 :       || !same_data_refs (STMT_VINFO_DATA_REF (store_vinfo),
    6852              :                           STMT_VINFO_DATA_REF (load_stmt_vinfo)))
    6853        25344 :     return false;
    6854              : 
    6855              :   return true;
    6856              : }
    6857              : 
    6858              : /* Function vect_recog_cond_store_pattern
    6859              : 
    6860              :    Try to find the following pattern:
    6861              : 
    6862              :    x = *_3;
    6863              :    c = a CMP b;
    6864              :    y = c ? t_20 : x;
    6865              :    *_3 = y;
    6866              : 
    6867              :    where the store of _3 happens on a conditional select on a value loaded
    6868              :    from the same location.  In such case we can elide the initial load if
    6869              :    MASK_STORE is supported and instead only conditionally write out the result.
    6870              : 
    6871              :    The pattern produces for the above:
    6872              : 
    6873              :    c = a CMP b;
    6874              :    .MASK_STORE (_3, c, t_20)
    6875              : 
    6876              :    Input:
    6877              : 
    6878              :    * STMT_VINFO: The stmt from which the pattern search begins.  In the
    6879              :    example, when this function is called with _3 then the search begins.
    6880              : 
    6881              :    Output:
    6882              : 
    6883              :    * TYPE_OUT: The type of the output  of this pattern.
    6884              : 
    6885              :    * Return value: A new stmt that will be used to replace the sequence.  */
    6886              : 
    6887              : static gimple *
    6888     33611944 : vect_recog_cond_store_pattern (vec_info *vinfo,
    6889              :                                stmt_vec_info stmt_vinfo, tree *type_out)
    6890              : {
    6891     33611944 :   loop_vec_info loop_vinfo = dyn_cast <loop_vec_info> (vinfo);
    6892      4774025 :   if (!loop_vinfo)
    6893              :     return NULL;
    6894              : 
    6895      4774025 :   gimple *store_stmt = STMT_VINFO_STMT (stmt_vinfo);
    6896              : 
    6897              :   /* Needs to be a gimple store where we have DR info for.  */
    6898      4774025 :   if (!STMT_VINFO_DATA_REF (stmt_vinfo)
    6899      1144963 :       || DR_IS_READ (STMT_VINFO_DATA_REF (stmt_vinfo))
    6900      5178987 :       || !gimple_store_p (store_stmt))
    6901              :     return NULL;
    6902              : 
    6903       402266 :   tree st_rhs = gimple_assign_rhs1 (store_stmt);
    6904              : 
    6905       402266 :   if (TREE_CODE (st_rhs) != SSA_NAME)
    6906              :     return NULL;
    6907              : 
    6908       312203 :   auto cond_vinfo = vinfo->lookup_def (st_rhs);
    6909              : 
    6910              :   /* If the condition isn't part of the loop then bool recog wouldn't have seen
    6911              :      it and so this transformation may not be valid.  */
    6912       312203 :   if (!cond_vinfo)
    6913              :     return NULL;
    6914              : 
    6915       291856 :   cond_vinfo = vect_stmt_to_vectorize (cond_vinfo);
    6916       291856 :   gassign *cond_stmt = dyn_cast<gassign *> (STMT_VINFO_STMT (cond_vinfo));
    6917       359404 :   if (!cond_stmt || gimple_assign_rhs_code (cond_stmt) != COND_EXPR)
    6918              :     return NULL;
    6919              : 
    6920              :   /* Check if the else value matches the original loaded one.  */
    6921        18396 :   bool invert = false;
    6922        18396 :   tree cmp_ls = gimple_arg (cond_stmt, 0);
    6923        18396 :   if (TREE_CODE (cmp_ls) != SSA_NAME)
    6924              :     return NULL;
    6925              : 
    6926        18396 :   tree cond_arg1 = gimple_arg (cond_stmt, 1);
    6927        18396 :   tree cond_arg2 = gimple_arg (cond_stmt, 2);
    6928              : 
    6929        18396 :   if (!vect_cond_store_pattern_same_ref (vinfo, stmt_vinfo, cond_arg2)
    6930        18396 :       && !(invert = vect_cond_store_pattern_same_ref (vinfo, stmt_vinfo,
    6931              :                                                       cond_arg1)))
    6932              :     return NULL;
    6933              : 
    6934         9345 :   vect_pattern_detected ("vect_recog_cond_store_pattern", store_stmt);
    6935              : 
    6936         9345 :   tree scalar_type = TREE_TYPE (st_rhs);
    6937         9345 :   if (VECTOR_TYPE_P (scalar_type))
    6938              :     return NULL;
    6939              : 
    6940         9345 :   tree vectype = get_vectype_for_scalar_type (vinfo, scalar_type);
    6941         9345 :   if (vectype == NULL_TREE)
    6942              :     return NULL;
    6943              : 
    6944         9345 :   machine_mode mask_mode;
    6945         9345 :   machine_mode vecmode = TYPE_MODE (vectype);
    6946         1884 :   if (!VECTOR_MODE_P (vecmode)
    6947         9345 :       || targetm.vectorize.conditional_operation_is_expensive (IFN_MASK_STORE)
    6948         9345 :       || !targetm.vectorize.get_mask_mode (vecmode).exists (&mask_mode)
    6949         9345 :       || !can_vec_mask_load_store_p (vecmode, mask_mode, false))
    6950              :     return NULL;
    6951              : 
    6952            0 :   tree base = DR_REF (STMT_VINFO_DATA_REF (stmt_vinfo));
    6953            0 :   if (may_be_nonaddressable_p (base))
    6954              :     return NULL;
    6955              : 
    6956              :   /* We need to use the false parameter of the conditional select.  */
    6957            0 :   tree cond_store_arg = invert ? cond_arg2 : cond_arg1;
    6958            0 :   tree cond_load_arg = invert ? cond_arg1 : cond_arg2;
    6959            0 :   gimple *load_stmt = SSA_NAME_DEF_STMT (cond_load_arg);
    6960              : 
    6961              :   /* This is a rough estimation to check that there aren't any aliasing stores
    6962              :      in between the load and store.  It's a bit strict, but for now it's good
    6963              :      enough.  */
    6964            0 :   if (gimple_vuse (load_stmt) != gimple_vuse (store_stmt))
    6965              :     return NULL;
    6966              : 
    6967              :   /* If we have to invert the condition, i.e. use the true argument rather than
    6968              :      the false argument, we have to negate the mask.  */
    6969            0 :   if (invert)
    6970              :     {
    6971            0 :       tree var = vect_recog_temp_ssa_var (boolean_type_node, NULL);
    6972              : 
    6973              :       /* Invert the mask using ^ 1.  */
    6974            0 :       tree itype = TREE_TYPE (cmp_ls);
    6975            0 :       gassign *conv = gimple_build_assign (var, BIT_XOR_EXPR, cmp_ls,
    6976              :                                            build_int_cst (itype, 1));
    6977              : 
    6978            0 :       tree mask_vec_type = get_mask_type_for_scalar_type (vinfo, itype);
    6979            0 :       append_pattern_def_seq (vinfo, stmt_vinfo, conv, mask_vec_type, itype);
    6980            0 :       cmp_ls= var;
    6981              :     }
    6982              : 
    6983            0 :   if (TREE_CODE (base) != MEM_REF)
    6984            0 :    base = build_fold_addr_expr (base);
    6985              : 
    6986            0 :   tree ptr = build_int_cst (reference_alias_ptr_type (base),
    6987            0 :                             get_object_alignment (base));
    6988              : 
    6989              :   /* Convert the mask to the right form.  */
    6990            0 :   tree mask = vect_convert_mask_for_vectype (cmp_ls, vectype, stmt_vinfo,
    6991              :                                              vinfo);
    6992              : 
    6993            0 :   gcall *call
    6994            0 :     = gimple_build_call_internal (IFN_MASK_STORE, 4, base, ptr, mask,
    6995              :                                   cond_store_arg);
    6996            0 :   gimple_set_location (call, gimple_location (store_stmt));
    6997              : 
    6998              :   /* Copy across relevant vectorization info and associate DR with the
    6999              :      new pattern statement instead of the original statement.  */
    7000            0 :   stmt_vec_info pattern_stmt_info = loop_vinfo->add_stmt (call);
    7001            0 :   loop_vinfo->move_dr (pattern_stmt_info, stmt_vinfo);
    7002              : 
    7003            0 :   *type_out = vectype;
    7004            0 :   return call;
    7005              : }
    7006              : 
    7007              : /* Return true if TYPE is a non-boolean integer type.  These are the types
    7008              :    that we want to consider for narrowing.  */
    7009              : 
    7010              : static bool
    7011     69614440 : vect_narrowable_type_p (tree type)
    7012              : {
    7013     69614440 :   return INTEGRAL_TYPE_P (type) && !VECT_SCALAR_BOOLEAN_TYPE_P (type);
    7014              : }
    7015              : 
    7016              : /* Return true if the operation given by CODE can be truncated to N bits
    7017              :    when only N bits of the output are needed.  This is only true if bit N+1
    7018              :    of the inputs has no effect on the low N bits of the result.  */
    7019              : 
    7020              : static bool
    7021     17420310 : vect_truncatable_operation_p (tree_code code)
    7022              : {
    7023     17420310 :   switch (code)
    7024              :     {
    7025              :     case NEGATE_EXPR:
    7026              :     case PLUS_EXPR:
    7027              :     case MINUS_EXPR:
    7028              :     case MULT_EXPR:
    7029              :     case BIT_NOT_EXPR:
    7030              :     case BIT_AND_EXPR:
    7031              :     case BIT_IOR_EXPR:
    7032              :     case BIT_XOR_EXPR:
    7033              :     case COND_EXPR:
    7034              :       return true;
    7035              : 
    7036      6872737 :     default:
    7037      6872737 :       return false;
    7038              :     }
    7039              : }
    7040              : 
    7041              : /* Record that STMT_INFO could be changed from operating on TYPE to
    7042              :    operating on a type with the precision and sign given by PRECISION
    7043              :    and SIGN respectively.  PRECISION is an arbitrary bit precision;
    7044              :    it might not be a whole number of bytes.  */
    7045              : 
    7046              : static void
    7047      2641581 : vect_set_operation_type (stmt_vec_info stmt_info, tree type,
    7048              :                          unsigned int precision, signop sign)
    7049              : {
    7050              :   /* Round the precision up to a whole number of bytes.  */
    7051      2641581 :   precision = vect_element_precision (precision);
    7052      2641581 :   if (precision < TYPE_PRECISION (type)
    7053      2641581 :       && (!stmt_info->operation_precision
    7054        32155 :           || stmt_info->operation_precision > precision))
    7055              :     {
    7056      1731569 :       stmt_info->operation_precision = precision;
    7057      1731569 :       stmt_info->operation_sign = sign;
    7058              :     }
    7059      2641581 : }
    7060              : 
    7061              : /* Record that STMT_INFO only requires MIN_INPUT_PRECISION from its
    7062              :    non-boolean inputs, all of which have type TYPE.  MIN_INPUT_PRECISION
    7063              :    is an arbitrary bit precision; it might not be a whole number of bytes.  */
    7064              : 
    7065              : static void
    7066     12309567 : vect_set_min_input_precision (stmt_vec_info stmt_info, tree type,
    7067              :                               unsigned int min_input_precision)
    7068              : {
    7069              :   /* This operation in isolation only requires the inputs to have
    7070              :      MIN_INPUT_PRECISION of precision,  However, that doesn't mean
    7071              :      that MIN_INPUT_PRECISION is a natural precision for the chain
    7072              :      as a whole.  E.g. consider something like:
    7073              : 
    7074              :          unsigned short *x, *y;
    7075              :          *y = ((*x & 0xf0) >> 4) | (*y << 4);
    7076              : 
    7077              :      The right shift can be done on unsigned chars, and only requires the
    7078              :      result of "*x & 0xf0" to be done on unsigned chars.  But taking that
    7079              :      approach would mean turning a natural chain of single-vector unsigned
    7080              :      short operations into one that truncates "*x" and then extends
    7081              :      "(*x & 0xf0) >> 4", with two vectors for each unsigned short
    7082              :      operation and one vector for each unsigned char operation.
    7083              :      This would be a significant pessimization.
    7084              : 
    7085              :      Instead only propagate the maximum of this precision and the precision
    7086              :      required by the users of the result.  This means that we don't pessimize
    7087              :      the case above but continue to optimize things like:
    7088              : 
    7089              :          unsigned char *y;
    7090              :          unsigned short *x;
    7091              :          *y = ((*x & 0xf0) >> 4) | (*y << 4);
    7092              : 
    7093              :      Here we would truncate two vectors of *x to a single vector of
    7094              :      unsigned chars and use single-vector unsigned char operations for
    7095              :      everything else, rather than doing two unsigned short copies of
    7096              :      "(*x & 0xf0) >> 4" and then truncating the result.  */
    7097     12309567 :   min_input_precision = MAX (min_input_precision,
    7098              :                              stmt_info->min_output_precision);
    7099              : 
    7100     12309567 :   if (min_input_precision < TYPE_PRECISION (type)
    7101     12309567 :       && (!stmt_info->min_input_precision
    7102        35309 :           || stmt_info->min_input_precision > min_input_precision))
    7103       609261 :     stmt_info->min_input_precision = min_input_precision;
    7104     12309567 : }
    7105              : 
    7106              : /* Subroutine of vect_determine_min_output_precision.  Return true if
    7107              :    we can calculate a reduced number of output bits for STMT_INFO,
    7108              :    whose result is LHS.  */
    7109              : 
    7110              : static bool
    7111     16024677 : vect_determine_min_output_precision_1 (vec_info *vinfo,
    7112              :                                        stmt_vec_info stmt_info, tree lhs)
    7113              : {
    7114              :   /* Take the maximum precision required by users of the result.  */
    7115     16024677 :   unsigned int precision = 0;
    7116     16024677 :   imm_use_iterator iter;
    7117     16024677 :   use_operand_p use;
    7118     17022796 :   FOR_EACH_IMM_USE_FAST (use, iter, lhs)
    7119              :     {
    7120     16741276 :       gimple *use_stmt = USE_STMT (use);
    7121     16741276 :       if (is_gimple_debug (use_stmt))
    7122       719442 :         continue;
    7123     16021834 :       stmt_vec_info use_stmt_info = vinfo->lookup_stmt (use_stmt);
    7124     16021834 :       if (!use_stmt_info || !use_stmt_info->min_input_precision)
    7125              :         return false;
    7126              :       /* The input precision recorded for COND_EXPRs applies only to the
    7127              :          "then" and "else" values.  */
    7128       285738 :       gassign *assign = dyn_cast <gassign *> (stmt_info->stmt);
    7129       253387 :       if (assign
    7130       253387 :           && gimple_assign_rhs_code (assign) == COND_EXPR
    7131          578 :           && use->use != gimple_assign_rhs2_ptr (assign)
    7132          578 :           && use->use != gimple_assign_rhs3_ptr (assign))
    7133              :         return false;
    7134              :       /* The input precision recorded for shifts applies only to the
    7135              :          shifted value, not the shift amount.  */
    7136       285160 :       gassign *use_assign = dyn_cast <gassign *> (use_stmt);
    7137       285160 :       if (use_assign
    7138       285160 :           && (gimple_assign_rhs_code (use_assign) == LSHIFT_EXPR
    7139       277666 :               || gimple_assign_rhs_code (use_assign) == RSHIFT_EXPR)
    7140        24878 :           && use->use != gimple_assign_rhs1_ptr (use_assign))
    7141              :         return false;
    7142      1000972 :       precision = MAX (precision, use_stmt_info->min_input_precision);
    7143     15743157 :     }
    7144              : 
    7145       281520 :   if (dump_enabled_p ())
    7146         5797 :     dump_printf_loc (MSG_NOTE, vect_location,
    7147              :                      "only the low %d bits of %T are significant\n",
    7148              :                      precision, lhs);
    7149       281520 :   stmt_info->min_output_precision = precision;
    7150       281520 :   return true;
    7151              : }
    7152              : 
    7153              : /* Calculate min_output_precision for STMT_INFO.  */
    7154              : 
    7155              : static void
    7156     40770798 : vect_determine_min_output_precision (vec_info *vinfo, stmt_vec_info stmt_info)
    7157              : {
    7158              :   /* We're only interested in statements with a narrowable result.  */
    7159     40770798 :   tree lhs = gimple_get_lhs (stmt_info->stmt);
    7160     40770798 :   if (!lhs
    7161     32361035 :       || TREE_CODE (lhs) != SSA_NAME
    7162     68166760 :       || !vect_narrowable_type_p (TREE_TYPE (lhs)))
    7163              :     return;
    7164              : 
    7165     16024677 :   if (!vect_determine_min_output_precision_1 (vinfo, stmt_info, lhs))
    7166     15743157 :     stmt_info->min_output_precision = TYPE_PRECISION (TREE_TYPE (lhs));
    7167              : }
    7168              : 
    7169              : /* Use range information to decide whether STMT (described by STMT_INFO)
    7170              :    could be done in a narrower type.  This is effectively a forward
    7171              :    propagation, since it uses context-independent information that applies
    7172              :    to all users of an SSA name.  */
    7173              : 
    7174              : static void
    7175     23444208 : vect_determine_precisions_from_range (stmt_vec_info stmt_info, gassign *stmt)
    7176              : {
    7177     23444208 :   tree lhs = gimple_assign_lhs (stmt);
    7178     23444208 :   if (!lhs || TREE_CODE (lhs) != SSA_NAME)
    7179     20907287 :     return;
    7180              : 
    7181     18774270 :   tree type = TREE_TYPE (lhs);
    7182     18774270 :   if (!vect_narrowable_type_p (type))
    7183              :     return;
    7184              : 
    7185              :   /* First see whether we have any useful range information for the result.  */
    7186     12416824 :   unsigned int precision = TYPE_PRECISION (type);
    7187     12416824 :   signop sign = TYPE_SIGN (type);
    7188     12416824 :   wide_int min_value, max_value;
    7189     12416824 :   if (!vect_get_range_info (lhs, &min_value, &max_value))
    7190              :     return;
    7191              : 
    7192      5945970 :   tree_code code = gimple_assign_rhs_code (stmt);
    7193      5945970 :   unsigned int nops = gimple_num_ops (stmt);
    7194              : 
    7195      5945970 :   if (!vect_truncatable_operation_p (code))
    7196              :     {
    7197              :       /* Handle operations that can be computed in type T if all inputs
    7198              :          and outputs can be represented in type T.  Also handle left and
    7199              :          right shifts, where (in addition) the maximum shift amount must
    7200              :          be less than the number of bits in T.  */
    7201      2210400 :       bool is_shift;
    7202      2210400 :       switch (code)
    7203              :         {
    7204              :         case LSHIFT_EXPR:
    7205              :         case RSHIFT_EXPR:
    7206              :           is_shift = true;
    7207              :           break;
    7208              : 
    7209       323445 :         case ABS_EXPR:
    7210       323445 :         case MIN_EXPR:
    7211       323445 :         case MAX_EXPR:
    7212       323445 :         case TRUNC_DIV_EXPR:
    7213       323445 :         case CEIL_DIV_EXPR:
    7214       323445 :         case FLOOR_DIV_EXPR:
    7215       323445 :         case ROUND_DIV_EXPR:
    7216       323445 :         case EXACT_DIV_EXPR:
    7217              :           /* Modulus is excluded because it is typically calculated by doing
    7218              :              a division, for which minimum signed / -1 isn't representable in
    7219              :              the original signed type.  We could take the division range into
    7220              :              account instead, if handling modulus ever becomes important.  */
    7221       323445 :           is_shift = false;
    7222       323445 :           break;
    7223              : 
    7224              :         default:
    7225              :           return;
    7226              :         }
    7227      1451553 :       for (unsigned int i = 1; i < nops; ++i)
    7228              :         {
    7229      1129407 :           tree op = gimple_op (stmt, i);
    7230      1129407 :           wide_int op_min_value, op_max_value;
    7231      1129407 :           if (TREE_CODE (op) == INTEGER_CST)
    7232              :             {
    7233       328451 :               unsigned int op_precision = TYPE_PRECISION (TREE_TYPE (op));
    7234       328451 :               op_min_value = op_max_value = wi::to_wide (op, op_precision);
    7235              :             }
    7236       800956 :           else if (TREE_CODE (op) == SSA_NAME)
    7237              :             {
    7238       800956 :               if (!vect_get_range_info (op, &op_min_value, &op_max_value))
    7239              :                 return;
    7240              :             }
    7241              :           else
    7242              :             return;
    7243              : 
    7244       729249 :           if (is_shift && i == 2)
    7245              :             {
    7246              :               /* There needs to be one more bit than the maximum shift amount.
    7247              : 
    7248              :                  If the maximum shift amount is already 1 less than PRECISION
    7249              :                  then we can't narrow the shift further.  Dealing with that
    7250              :                  case first ensures that we can safely use an unsigned range
    7251              :                  below.
    7252              : 
    7253              :                  op_min_value isn't relevant, since shifts by negative amounts
    7254              :                  are UB.  */
    7255       208512 :               if (wi::geu_p (op_max_value, precision - 1))
    7256              :                 return;
    7257       184928 :               unsigned int min_bits = op_max_value.to_uhwi () + 1;
    7258              : 
    7259              :               /* As explained below, we can convert a signed shift into an
    7260              :                  unsigned shift if the sign bit is always clear.  At this
    7261              :                  point we've already processed the ranges of the output and
    7262              :                  the first input.  */
    7263       184928 :               auto op_sign = sign;
    7264       184928 :               if (sign == SIGNED && !wi::neg_p (min_value))
    7265              :                 op_sign = UNSIGNED;
    7266       369856 :               op_min_value = wide_int::from (wi::min_value (min_bits, op_sign),
    7267       184928 :                                              precision, op_sign);
    7268       369856 :               op_max_value = wide_int::from (wi::max_value (min_bits, op_sign),
    7269       184928 :                                              precision, op_sign);
    7270              :             }
    7271       705665 :           min_value = wi::min (min_value, op_min_value, sign);
    7272       705665 :           max_value = wi::max (max_value, op_max_value, sign);
    7273      1129407 :         }
    7274              :     }
    7275              : 
    7276              :   /* Try to switch signed types for unsigned types if we can.
    7277              :      This is better for two reasons.  First, unsigned ops tend
    7278              :      to be cheaper than signed ops.  Second, it means that we can
    7279              :      handle things like:
    7280              : 
    7281              :         signed char c;
    7282              :         int res = (int) c & 0xff00; // range [0x0000, 0xff00]
    7283              : 
    7284              :      as:
    7285              : 
    7286              :         signed char c;
    7287              :         unsigned short res_1 = (unsigned short) c & 0xff00;
    7288              :         int res = (int) res_1;
    7289              : 
    7290              :      where the intermediate result res_1 has unsigned rather than
    7291              :      signed type.  */
    7292      4057716 :   if (sign == SIGNED && !wi::neg_p (min_value))
    7293              :     sign = UNSIGNED;
    7294              : 
    7295              :   /* See what precision is required for MIN_VALUE and MAX_VALUE.  */
    7296      4057716 :   unsigned int precision1 = wi::min_precision (min_value, sign);
    7297      4057716 :   unsigned int precision2 = wi::min_precision (max_value, sign);
    7298      4057716 :   unsigned int value_precision = MAX (precision1, precision2);
    7299      4057716 :   if (value_precision >= precision)
    7300              :     return;
    7301              : 
    7302      2536921 :   if (dump_enabled_p ())
    7303       121939 :     dump_printf_loc (MSG_NOTE, vect_location, "can narrow to %s:%d"
    7304              :                      " without loss of precision: %G",
    7305              :                      sign == SIGNED ? "signed" : "unsigned",
    7306              :                      value_precision, (gimple *) stmt);
    7307              : 
    7308      2536921 :   vect_set_operation_type (stmt_info, type, value_precision, sign);
    7309              :   /* Non-truncatable operations like division depend on all bits of their
    7310              :      inputs if the operation itself is not narrowed - which we cannot
    7311              :      guarantee if doing BB vectorization.  So do not claim that only
    7312              :      VALUE_PRECISION bits of the inputs are needed.  */
    7313      2536921 :   if (vect_truncatable_operation_p (code))
    7314      2364040 :     vect_set_min_input_precision (stmt_info, type, value_precision);
    7315     12416824 : }
    7316              : 
    7317              : /* Use information about the users of STMT's result to decide whether
    7318              :    STMT (described by STMT_INFO) could be done in a narrower type.
    7319              :    This is effectively a backward propagation.  */
    7320              : 
    7321              : static void
    7322     23444208 : vect_determine_precisions_from_users (stmt_vec_info stmt_info, gassign *stmt)
    7323              : {
    7324     23444208 :   tree_code code = gimple_assign_rhs_code (stmt);
    7325     23444208 :   unsigned int opno = (code == COND_EXPR ? 2 : 1);
    7326     23444208 :   tree type = TREE_TYPE (gimple_op (stmt, opno));
    7327     23444208 :   if (!vect_narrowable_type_p (type))
    7328     13498681 :     return;
    7329              : 
    7330     14643991 :   unsigned int precision = TYPE_PRECISION (type);
    7331     14643991 :   unsigned int operation_precision, min_input_precision;
    7332     14643991 :   switch (code)
    7333              :     {
    7334      2672930 :     CASE_CONVERT:
    7335              :       /* Only the bits that contribute to the output matter.  Don't change
    7336              :          the precision of the operation itself.  */
    7337      2672930 :       operation_precision = precision;
    7338      2672930 :       min_input_precision = stmt_info->min_output_precision;
    7339      2672930 :       break;
    7340              : 
    7341       496721 :     case LSHIFT_EXPR:
    7342       496721 :     case RSHIFT_EXPR:
    7343       496721 :       {
    7344       496721 :         tree shift = gimple_assign_rhs2 (stmt);
    7345       496721 :         unsigned int min_const_shift, max_const_shift;
    7346       496721 :         wide_int min_shift, max_shift;
    7347       496721 :         if (TREE_CODE (shift) == SSA_NAME
    7348       108138 :             && vect_get_range_info (shift, &min_shift, &max_shift)
    7349        83650 :             && wi::ge_p (min_shift, 0, TYPE_SIGN (TREE_TYPE (shift)))
    7350       577527 :             && wi::lt_p (max_shift, TYPE_PRECISION (type),
    7351        80806 :                          TYPE_SIGN (TREE_TYPE (shift))))
    7352              :           {
    7353        72119 :             min_const_shift = min_shift.to_uhwi ();
    7354        72119 :             max_const_shift = max_shift.to_uhwi ();
    7355              :           }
    7356       424602 :         else if (TREE_CODE (shift) == INTEGER_CST
    7357       813185 :                  && wi::ltu_p (wi::to_widest (shift), precision))
    7358       388475 :           min_const_shift = max_const_shift = TREE_INT_CST_LOW (shift);
    7359              :         else
    7360        36127 :           return;
    7361       460594 :         if (code == LSHIFT_EXPR)
    7362              :           {
    7363              :             /* Avoid creating an undefined shift.
    7364              : 
    7365              :                ??? We could instead use min_output_precision as-is and
    7366              :                optimize out-of-range shifts to zero.  However, only
    7367              :                degenerate testcases shift away all their useful input data,
    7368              :                and it isn't natural to drop input operations in the middle
    7369              :                of vectorization.  This sort of thing should really be
    7370              :                handled before vectorization.  */
    7371       110023 :             operation_precision = MAX (stmt_info->min_output_precision,
    7372              :                                        max_const_shift + 1);
    7373              :             /* We need CONST_SHIFT fewer bits of the input.  */
    7374       110023 :             min_input_precision = (MAX (operation_precision, max_const_shift)
    7375              :                                    - min_const_shift);
    7376              :           }
    7377              :         else
    7378              :           {
    7379              :             /* We need CONST_SHIFT extra bits to do the operation.  */
    7380       350571 :             operation_precision = (stmt_info->min_output_precision
    7381              :                                    + max_const_shift);
    7382       350571 :             min_input_precision = operation_precision;
    7383              :           }
    7384       460594 :         break;
    7385       496721 :       }
    7386              : 
    7387     11474340 :     default:
    7388     11474340 :       if (vect_truncatable_operation_p (code))
    7389              :         {
    7390              :           /* Input bit N has no effect on output bits N-1 and lower.  */
    7391      6812003 :           operation_precision = stmt_info->min_output_precision;
    7392      6812003 :           min_input_precision = operation_precision;
    7393      6812003 :           break;
    7394              :         }
    7395              :       return;
    7396              :     }
    7397              : 
    7398      9945527 :   if (operation_precision < precision)
    7399              :     {
    7400       104660 :       if (dump_enabled_p ())
    7401         2763 :         dump_printf_loc (MSG_NOTE, vect_location, "can narrow to %s:%d"
    7402              :                          " without affecting users: %G",
    7403         2763 :                          TYPE_UNSIGNED (type) ? "unsigned" : "signed",
    7404              :                          operation_precision, (gimple *) stmt);
    7405       209320 :       vect_set_operation_type (stmt_info, type, operation_precision,
    7406       104660 :                                TYPE_SIGN (type));
    7407              :     }
    7408      9945527 :   vect_set_min_input_precision (stmt_info, type, min_input_precision);
    7409              : }
    7410              : 
    7411              : /* Return true if the statement described by STMT_INFO sets a boolean
    7412              :    SSA_NAME and if we know how to vectorize this kind of statement using
    7413              :    vector mask types.  */
    7414              : 
    7415              : static bool
    7416     41886836 : possible_vector_mask_operation_p (stmt_vec_info stmt_info)
    7417              : {
    7418     41886836 :   tree lhs = gimple_get_lhs (stmt_info->stmt);
    7419     41886836 :   tree_code code = ERROR_MARK;
    7420     41886836 :   gassign *assign = NULL;
    7421     41886836 :   gcond *cond = NULL;
    7422              : 
    7423     41886836 :   if ((assign = dyn_cast <gassign *> (stmt_info->stmt)))
    7424     24241570 :     code = gimple_assign_rhs_code (assign);
    7425     17645266 :   else if ((cond = dyn_cast <gcond *> (stmt_info->stmt)))
    7426              :     {
    7427      5601773 :       lhs = gimple_cond_lhs (cond);
    7428      5601773 :       code = gimple_cond_code (cond);
    7429              :     }
    7430              : 
    7431     41886836 :   if (!lhs
    7432     38982366 :       || TREE_CODE (lhs) != SSA_NAME
    7433     75864655 :       || !VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (lhs)))
    7434              :     return false;
    7435              : 
    7436      2274276 :   if (code != ERROR_MARK)
    7437              :     {
    7438      2007550 :       switch (code)
    7439              :         {
    7440              :         CASE_CONVERT:
    7441              :         case SSA_NAME:
    7442              :         case BIT_NOT_EXPR:
    7443              :         case BIT_IOR_EXPR:
    7444              :         case BIT_XOR_EXPR:
    7445              :         case BIT_AND_EXPR:
    7446              :           return true;
    7447              : 
    7448      1573738 :         default:
    7449      1573738 :           return TREE_CODE_CLASS (code) == tcc_comparison;
    7450              :         }
    7451              :     }
    7452       266726 :   else if (is_a <gphi *> (stmt_info->stmt))
    7453       149758 :     return true;
    7454              :   return false;
    7455              : }
    7456              : 
    7457              : /* If STMT_INFO sets a boolean SSA_NAME, see whether we should use
    7458              :    a vector mask type instead of a normal vector type.  Record the
    7459              :    result in STMT_INFO->mask_precision.  Returns true when the
    7460              :    precision changed.  */
    7461              : 
    7462              : static bool
    7463     41886836 : vect_determine_mask_precision (vec_info *vinfo, stmt_vec_info stmt_info)
    7464              : {
    7465     41886836 :   if (!possible_vector_mask_operation_p (stmt_info))
    7466              :     return false;
    7467              : 
    7468              :   /* If at least one boolean input uses a vector mask type,
    7469              :      pick the mask type with the narrowest elements.
    7470              : 
    7471              :      ??? This is the traditional behavior.  It should always produce
    7472              :      the smallest number of operations, but isn't necessarily the
    7473              :      optimal choice.  For example, if we have:
    7474              : 
    7475              :        a = b & c
    7476              : 
    7477              :      where:
    7478              : 
    7479              :        - the user of a wants it to have a mask type for 16-bit elements (M16)
    7480              :        - b also uses M16
    7481              :        - c uses a mask type for 8-bit elements (M8)
    7482              : 
    7483              :      then picking M8 gives:
    7484              : 
    7485              :        - 1 M16->M8 pack for b
    7486              :        - 1 M8 AND for a
    7487              :        - 2 M8->M16 unpacks for the user of a
    7488              : 
    7489              :      whereas picking M16 would have given:
    7490              : 
    7491              :        - 2 M8->M16 unpacks for c
    7492              :        - 2 M16 ANDs for a
    7493              : 
    7494              :      The number of operations are equal, but M16 would have given
    7495              :      a shorter dependency chain and allowed more ILP.  */
    7496      2104879 :   unsigned int precision = ~0U;
    7497      2104879 :   gimple *stmt = STMT_VINFO_STMT (stmt_info);
    7498              : 
    7499              :   /* If the statement compares two values that shouldn't use vector masks,
    7500              :      try comparing the values as normal scalars instead.  */
    7501      2104879 :   tree_code code = ERROR_MARK;
    7502      2104879 :   tree op0_type;
    7503      2104879 :   unsigned int nops = -1;
    7504      2104879 :   unsigned int ops_start = 0;
    7505              : 
    7506      2104879 :   if (gassign *assign = dyn_cast <gassign *> (stmt))
    7507              :     {
    7508      1373881 :       code = gimple_assign_rhs_code (assign);
    7509      1373881 :       op0_type = TREE_TYPE (gimple_assign_rhs1 (assign));
    7510      1373881 :       nops = gimple_num_ops (assign);
    7511      1373881 :       ops_start = 1;
    7512              :     }
    7513       730998 :   else if (gcond *cond = dyn_cast <gcond *> (stmt))
    7514              :     {
    7515       581240 :       code = gimple_cond_code (cond);
    7516       581240 :       op0_type = TREE_TYPE (gimple_cond_lhs (cond));
    7517       581240 :       nops = 2;
    7518       581240 :       ops_start = 0;
    7519              :     }
    7520              : 
    7521      1955121 :   if (code != ERROR_MARK)
    7522              :     {
    7523      5820003 :       for (unsigned int i = ops_start; i < nops; ++i)
    7524              :         {
    7525      3864882 :           tree rhs = gimple_op (stmt, i);
    7526      3864882 :           if (!VECT_SCALAR_BOOLEAN_TYPE_P (TREE_TYPE (rhs)))
    7527      1890412 :             continue;
    7528              : 
    7529      1974470 :           stmt_vec_info def_stmt_info = vinfo->lookup_def (rhs);
    7530      1974470 :           if (!def_stmt_info)
    7531              :             /* Don't let external or constant operands influence the choice.
    7532              :                We can convert them to whichever vector type we pick.  */
    7533       596574 :             continue;
    7534              : 
    7535      1377896 :           if (def_stmt_info->mask_precision)
    7536              :             {
    7537      1146346 :               if (precision > def_stmt_info->mask_precision)
    7538      3864882 :                 precision = def_stmt_info->mask_precision;
    7539              :             }
    7540              :         }
    7541              : 
    7542      1955121 :       if (precision == ~0U
    7543      1573617 :           && TREE_CODE_CLASS (code) == tcc_comparison)
    7544              :         {
    7545      1363860 :           scalar_mode mode;
    7546      1363860 :           tree vectype, mask_type;
    7547      1363860 :           if (is_a <scalar_mode> (TYPE_MODE (op0_type), &mode)
    7548              :               /* Do not allow this to set vinfo->vector_mode, this might
    7549              :                  disrupt the result for the next iteration.  */
    7550      1363860 :               && (vectype = get_related_vectype_for_scalar_type
    7551      1636370 :                                                 (vinfo->vector_mode, op0_type))
    7552      1192512 :               && (mask_type = truth_type_for (vectype))
    7553      1192512 :               && expand_vec_cmp_expr_p (vectype, mask_type, code))
    7554      1840004 :             precision = GET_MODE_BITSIZE (mode);
    7555              :         }
    7556              :     }
    7557              :   else
    7558              :     {
    7559       149758 :       gphi *phi = as_a <gphi *> (stmt_info->stmt);
    7560       767928 :       for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
    7561              :         {
    7562       468412 :           tree rhs = gimple_phi_arg_def (phi, i);
    7563              : 
    7564       468412 :           stmt_vec_info def_stmt_info = vinfo->lookup_def (rhs);
    7565       468412 :           if (!def_stmt_info)
    7566              :             /* Don't let external or constant operands influence the choice.
    7567              :                We can convert them to whichever vector type we pick.  */
    7568       309503 :             continue;
    7569              : 
    7570       158909 :           if (def_stmt_info->mask_precision)
    7571              :             {
    7572       133107 :               if (precision > def_stmt_info->mask_precision)
    7573       468412 :                 precision = def_stmt_info->mask_precision;
    7574              :             }
    7575              :         }
    7576              :     }
    7577              : 
    7578      2104879 :   if (stmt_info->mask_precision != precision)
    7579              :     {
    7580      1973929 :       if (dump_enabled_p ())
    7581              :         {
    7582         8922 :           if (precision == ~0U)
    7583         1934 :             dump_printf_loc (MSG_NOTE, vect_location,
    7584              :                              "using normal nonmask vectors for %G",
    7585              :                              stmt_info->stmt);
    7586              :           else
    7587         6988 :             dump_printf_loc (MSG_NOTE, vect_location,
    7588              :                              "using boolean precision %d for %G",
    7589              :                              precision, stmt_info->stmt);
    7590              :         }
    7591              : 
    7592              :       /* ???  We'd like to assert stmt_info->mask_precision == 0
    7593              :          || stmt_info->mask_precision > precision, thus that we only
    7594              :          decrease mask precisions throughout iteration, but the
    7595              :          tcc_comparison handling above means for comparisons of bools
    7596              :          we start with 8 but might increase in case the bools get mask
    7597              :          precision on their own.  */
    7598      1973929 :       stmt_info->mask_precision = precision;
    7599      1973929 :       return true;
    7600              :     }
    7601              :   return false;
    7602              : }
    7603              : 
    7604              : /* Handle vect_determine_precisions for STMT_INFO, given that we
    7605              :    have already done so for the users of its result.  */
    7606              : 
    7607              : void
    7608     40770798 : vect_determine_stmt_precisions (vec_info *vinfo, stmt_vec_info stmt_info)
    7609              : {
    7610     40770798 :   vect_determine_min_output_precision (vinfo, stmt_info);
    7611     40770798 :   if (gassign *stmt = dyn_cast <gassign *> (stmt_info->stmt))
    7612              :     {
    7613     23444208 :       vect_determine_precisions_from_range (stmt_info, stmt);
    7614     23444208 :       vect_determine_precisions_from_users (stmt_info, stmt);
    7615              :     }
    7616     40770798 : }
    7617              : 
    7618              : /* Walk backwards through the vectorizable region to determine the
    7619              :    values of these fields:
    7620              : 
    7621              :    - min_output_precision
    7622              :    - min_input_precision
    7623              :    - operation_precision
    7624              :    - operation_sign.  */
    7625              : 
    7626              : void
    7627      1155572 : vect_determine_precisions (vec_info *vinfo)
    7628              : {
    7629      1155572 :   basic_block *bbs = vinfo->bbs;
    7630      1155572 :   unsigned int nbbs = vinfo->nbbs;
    7631              : 
    7632      1176893 :   DUMP_VECT_SCOPE ("vect_determine_precisions");
    7633              : 
    7634              :   /* For mask precisions we have to iterate since otherwise we do not
    7635              :      get reduction PHI precision correct.  For now do this only for
    7636              :      loop vectorization.  */
    7637      1227899 :   bool changed;
    7638      1227899 :   do
    7639              :     {
    7640      1227899 :       changed = false;
    7641     13635856 :       for (unsigned int i = 0; i < nbbs; i++)
    7642              :         {
    7643     12407957 :           basic_block bb = bbs[i];
    7644     12407957 :           for (auto gsi = gsi_start_phis (bb);
    7645     20146047 :                !gsi_end_p (gsi); gsi_next (&gsi))
    7646              :             {
    7647      7738090 :               stmt_vec_info stmt_info = vinfo->lookup_stmt (gsi.phi ());
    7648      7738090 :               if (stmt_info && STMT_VINFO_VECTORIZABLE (stmt_info))
    7649      7554224 :                 changed |= vect_determine_mask_precision (vinfo, stmt_info);
    7650              :             }
    7651    131335650 :           for (auto gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    7652              :             {
    7653    106519736 :               stmt_vec_info stmt_info = vinfo->lookup_stmt (gsi_stmt (gsi));
    7654    106519736 :               if (stmt_info && STMT_VINFO_VECTORIZABLE (stmt_info))
    7655     34332612 :                 changed |= vect_determine_mask_precision (vinfo, stmt_info);
    7656              :             }
    7657              :         }
    7658              :     }
    7659      2383471 :   while (changed && is_a <loop_vec_info> (vinfo));
    7660              : 
    7661     13397181 :   for (unsigned int i = 0; i < nbbs; i++)
    7662              :     {
    7663     12241609 :       basic_block bb = bbs[nbbs - i - 1];
    7664    138944813 :       for (auto gsi = gsi_last_bb (bb); !gsi_end_p (gsi); gsi_prev (&gsi))
    7665              :         {
    7666    104896776 :           stmt_vec_info stmt_info = vinfo->lookup_stmt (gsi_stmt (gsi));
    7667    104896776 :           if (stmt_info && STMT_VINFO_VECTORIZABLE (stmt_info))
    7668     33424905 :             vect_determine_stmt_precisions (vinfo, stmt_info);
    7669              :         }
    7670     19771368 :       for (auto gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    7671              :         {
    7672      7529759 :           stmt_vec_info stmt_info = vinfo->lookup_stmt (gsi.phi ());
    7673      7529759 :           if (stmt_info && STMT_VINFO_VECTORIZABLE (stmt_info))
    7674      7345893 :             vect_determine_stmt_precisions (vinfo, stmt_info);
    7675              :         }
    7676              :     }
    7677      1155572 : }
    7678              : 
    7679              : typedef gimple *(*vect_recog_func_ptr) (vec_info *, stmt_vec_info, tree *);
    7680              : 
    7681              : struct vect_recog_func
    7682              : {
    7683              :   vect_recog_func_ptr fn;
    7684              :   const char *name;
    7685              : };
    7686              : 
    7687              : /* Note that ordering matters - the first pattern matching on a stmt is
    7688              :    taken which means usually the more complex one needs to precede the
    7689              :    less comples onex (widen_sum only after dot_prod or sad for example).  */
    7690              : static vect_recog_func vect_vect_recog_func_ptrs[] = {
    7691              :   { vect_recog_bitfield_ref_pattern, "bitfield_ref" },
    7692              :   { vect_recog_bit_insert_pattern, "bit_insert" },
    7693              :   { vect_recog_abd_pattern, "abd" },
    7694              :   { vect_recog_over_widening_pattern, "over_widening" },
    7695              :   /* Must come after over_widening, which narrows the shift as much as
    7696              :      possible beforehand.  */
    7697              :   { vect_recog_average_pattern, "average" },
    7698              :   { vect_recog_cond_expr_convert_pattern, "cond_expr_convert" },
    7699              :   { vect_recog_mulhs_pattern, "mult_high" },
    7700              :   { vect_recog_cast_forwprop_pattern, "cast_forwprop" },
    7701              :   { vect_recog_widen_mult_pattern, "widen_mult" },
    7702              :   { vect_recog_dot_prod_pattern, "dot_prod" },
    7703              :   { vect_recog_sad_pattern, "sad" },
    7704              :   { vect_recog_widen_sum_pattern, "widen_sum" },
    7705              :   { vect_recog_pow_pattern, "pow" },
    7706              :   { vect_recog_popcount_clz_ctz_ffs_pattern, "popcount_clz_ctz_ffs" },
    7707              :   { vect_recog_ctz_ffs_pattern, "ctz_ffs" },
    7708              :   { vect_recog_widen_shift_pattern, "widen_shift" },
    7709              :   { vect_recog_rotate_pattern, "rotate" },
    7710              :   { vect_recog_vector_vector_shift_pattern, "vector_vector_shift" },
    7711              :   { vect_recog_divmod_pattern, "divmod" },
    7712              :   { vect_recog_mod_var_pattern, "modvar" },
    7713              :   { vect_recog_mult_pattern, "mult" },
    7714              :   { vect_recog_sat_add_pattern, "sat_add" },
    7715              :   { vect_recog_sat_sub_pattern, "sat_sub" },
    7716              :   { vect_recog_sat_trunc_pattern, "sat_trunc" },
    7717              :   { vect_recog_gcond_pattern, "gcond" },
    7718              :   { vect_recog_bool_pattern, "bool" },
    7719              :   /* This must come before mask conversion, and includes the parts
    7720              :      of mask conversion that are needed for gather and scatter
    7721              :      internal functions.  */
    7722              :   { vect_recog_gather_scatter_pattern, "gather_scatter" },
    7723              :   { vect_recog_cond_store_pattern, "cond_store" },
    7724              :   { vect_recog_mask_conversion_pattern, "mask_conversion" },
    7725              :   { vect_recog_widen_plus_pattern, "widen_plus" },
    7726              :   { vect_recog_widen_minus_pattern, "widen_minus" },
    7727              :   { vect_recog_widen_abd_pattern, "widen_abd" },
    7728              :   /* These must come after the double widening ones.  */
    7729              : };
    7730              : 
    7731              : /* Mark statements that are involved in a pattern.  */
    7732              : 
    7733              : void
    7734      1106091 : vect_mark_pattern_stmts (vec_info *vinfo,
    7735              :                          stmt_vec_info orig_stmt_info, gimple *pattern_stmt,
    7736              :                          tree pattern_vectype)
    7737              : {
    7738      1106091 :   stmt_vec_info orig_stmt_info_saved = orig_stmt_info;
    7739      1106091 :   gimple *def_seq = STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt_info);
    7740              : 
    7741      1106091 :   gimple *orig_pattern_stmt = NULL;
    7742      1106091 :   if (is_pattern_stmt_p (orig_stmt_info))
    7743              :     {
    7744              :       /* We're replacing a statement in an existing pattern definition
    7745              :          sequence.  */
    7746        12080 :       orig_pattern_stmt = orig_stmt_info->stmt;
    7747        12080 :       if (dump_enabled_p ())
    7748         1012 :         dump_printf_loc (MSG_NOTE, vect_location,
    7749              :                          "replacing earlier pattern %G", orig_pattern_stmt);
    7750              : 
    7751              :       /* To keep the book-keeping simple, just swap the lhs of the
    7752              :          old and new statements, so that the old one has a valid but
    7753              :          unused lhs.  */
    7754        12080 :       tree old_lhs = gimple_get_lhs (orig_pattern_stmt);
    7755        12080 :       gimple_set_lhs (orig_pattern_stmt, gimple_get_lhs (pattern_stmt));
    7756        12080 :       gimple_set_lhs (pattern_stmt, old_lhs);
    7757              : 
    7758        12080 :       if (dump_enabled_p ())
    7759         1012 :         dump_printf_loc (MSG_NOTE, vect_location, "with %G", pattern_stmt);
    7760              : 
    7761              :       /* Switch to the statement that ORIG replaces.  */
    7762        12080 :       orig_stmt_info = STMT_VINFO_RELATED_STMT (orig_stmt_info);
    7763              : 
    7764              :       /* We shouldn't be replacing the main pattern statement.  */
    7765        12080 :       gcc_assert (STMT_VINFO_RELATED_STMT (orig_stmt_info)->stmt
    7766              :                   != orig_pattern_stmt);
    7767              :     }
    7768              : 
    7769      1106091 :   if (def_seq)
    7770      2185692 :     for (gimple_stmt_iterator si = gsi_start (def_seq);
    7771      2185692 :          !gsi_end_p (si); gsi_next (&si))
    7772              :       {
    7773      1281094 :         if (dump_enabled_p ())
    7774        29237 :           dump_printf_loc (MSG_NOTE, vect_location,
    7775              :                            "extra pattern stmt: %G", gsi_stmt (si));
    7776      1281094 :         stmt_vec_info pattern_stmt_info
    7777      1281094 :           = vect_init_pattern_stmt (vinfo, gsi_stmt (si),
    7778              :                                     orig_stmt_info, pattern_vectype);
    7779              :         /* Stmts in the def sequence are not vectorizable cycle or
    7780              :            induction defs, instead they should all be vect_internal_def
    7781              :            feeding the main pattern stmt which retains this def type.  */
    7782      1281094 :         STMT_VINFO_DEF_TYPE (pattern_stmt_info) = vect_internal_def;
    7783              :       }
    7784              : 
    7785      1106091 :   if (orig_pattern_stmt)
    7786              :     {
    7787        12080 :       vect_init_pattern_stmt (vinfo, pattern_stmt,
    7788              :                               orig_stmt_info, pattern_vectype);
    7789              : 
    7790              :       /* Insert all the new pattern statements before the original one.  */
    7791        12080 :       gimple_seq *orig_def_seq = &STMT_VINFO_PATTERN_DEF_SEQ (orig_stmt_info);
    7792        12080 :       gimple_stmt_iterator gsi = gsi_for_stmt (orig_pattern_stmt,
    7793              :                                                orig_def_seq);
    7794        12080 :       gsi_insert_seq_before_without_update (&gsi, def_seq, GSI_SAME_STMT);
    7795        12080 :       gsi_insert_before_without_update (&gsi, pattern_stmt, GSI_SAME_STMT);
    7796              : 
    7797              :       /* Remove the pattern statement that this new pattern replaces.  */
    7798        12080 :       gsi_remove (&gsi, false);
    7799              :     }
    7800              :   else
    7801      1094011 :     vect_set_pattern_stmt (vinfo,
    7802              :                            pattern_stmt, orig_stmt_info, pattern_vectype);
    7803              : 
    7804              :   /* For any conditionals mark them as vect_condition_def.  */
    7805      1106091 :   if (is_a <gcond *> (pattern_stmt))
    7806       395185 :     STMT_VINFO_DEF_TYPE (STMT_VINFO_RELATED_STMT (orig_stmt_info)) = vect_condition_def;
    7807              : 
    7808              :   /* Transfer reduction path info to the pattern.  */
    7809      1106091 :   if (STMT_VINFO_REDUC_IDX (orig_stmt_info_saved) != -1)
    7810              :     {
    7811        16389 :       gimple_match_op op;
    7812        16389 :       if (!gimple_extract_op (orig_stmt_info_saved->stmt, &op))
    7813            0 :         gcc_unreachable ();
    7814        16389 :       tree lookfor = op.ops[STMT_VINFO_REDUC_IDX (orig_stmt_info)];
    7815              :       /* Search the pattern def sequence and the main pattern stmt.  Note
    7816              :          we may have inserted all into a containing pattern def sequence
    7817              :          so the following is a bit awkward.  */
    7818        16389 :       gimple_stmt_iterator si;
    7819        16389 :       gimple *s;
    7820        16389 :       if (def_seq)
    7821              :         {
    7822        15134 :           si = gsi_start (def_seq);
    7823        15134 :           s = gsi_stmt (si);
    7824        15134 :           gsi_next (&si);
    7825              :         }
    7826              :       else
    7827              :         {
    7828              :           si = gsi_none ();
    7829        16389 :           s = pattern_stmt;
    7830              :         }
    7831        34540 :       do
    7832              :         {
    7833        34540 :           bool found = false;
    7834        34540 :           if (gimple_extract_op (s, &op))
    7835              :             {
    7836        84318 :               for (unsigned i = 0; i < op.num_ops; ++i)
    7837        66161 :                 if (op.ops[i] == lookfor)
    7838              :                   {
    7839        16383 :                     STMT_VINFO_REDUC_IDX (vinfo->lookup_stmt (s)) = i;
    7840        16383 :                     lookfor = gimple_get_lhs (s);
    7841        16383 :                     found = true;
    7842        16383 :                     break;
    7843              :                   }
    7844              :               /* Try harder to find a mid-entry into an earlier pattern
    7845              :                  sequence.  Likewise an entry to a stmt skipping a conversion
    7846              :                  on an input.  This means that the initial 'lookfor' was
    7847              :                  bogus.  */
    7848        16383 :               if (!found)
    7849              :                 {
    7850        39520 :                   for (unsigned i = 0; i < op.num_ops; ++i)
    7851        21369 :                     if (TREE_CODE (op.ops[i]) == SSA_NAME)
    7852        18157 :                       if (auto def = vinfo->lookup_def (op.ops[i]))
    7853        17963 :                         if (vect_is_reduction (def)
    7854        17963 :                             || (is_a <gphi *> (def->stmt)
    7855            6 :                                 && STMT_VINFO_REDUC_DEF (def) != NULL))
    7856              :                           {
    7857            6 :                             STMT_VINFO_REDUC_IDX (vinfo->lookup_stmt (s)) = i;
    7858            6 :                             lookfor = gimple_get_lhs (s);
    7859            6 :                             found = true;
    7860            6 :                             break;
    7861              :                           }
    7862              :                 }
    7863              :             }
    7864        34540 :           if (s == pattern_stmt)
    7865              :             {
    7866        16389 :               if (!found && dump_enabled_p ())
    7867            0 :                 dump_printf_loc (MSG_NOTE, vect_location,
    7868              :                                  "failed to update reduction index.\n");
    7869        16389 :               break;
    7870              :             }
    7871        18151 :           if (gsi_end_p (si))
    7872              :             s = pattern_stmt;
    7873              :           else
    7874              :             {
    7875         3017 :               s = gsi_stmt (si);
    7876         3017 :               if (s == pattern_stmt)
    7877              :                 /* Found the end inside a bigger pattern def seq.  */
    7878              :                 si = gsi_none ();
    7879              :               else
    7880         3017 :                 gsi_next (&si);
    7881              :             }
    7882              :         } while (1);
    7883              :     }
    7884      1106091 : }
    7885              : 
    7886              : /* Function vect_pattern_recog_1
    7887              : 
    7888              :    Input:
    7889              :    PATTERN_RECOG_FUNC: A pointer to a function that detects a certain
    7890              :         computation pattern.
    7891              :    STMT_INFO: A stmt from which the pattern search should start.
    7892              : 
    7893              :    If PATTERN_RECOG_FUNC successfully detected the pattern, it creates
    7894              :    a sequence of statements that has the same functionality and can be
    7895              :    used to replace STMT_INFO.  It returns the last statement in the sequence
    7896              :    and adds any earlier statements to STMT_INFO's STMT_VINFO_PATTERN_DEF_SEQ.
    7897              :    PATTERN_RECOG_FUNC also sets *TYPE_OUT to the vector type of the final
    7898              :    statement, having first checked that the target supports the new operation
    7899              :    in that type.
    7900              : 
    7901              :    This function also does some bookkeeping, as explained in the documentation
    7902              :    for vect_recog_pattern.  */
    7903              : 
    7904              : static void
    7905   1086752048 : vect_pattern_recog_1 (vec_info *vinfo,
    7906              :                       const vect_recog_func &recog_func, stmt_vec_info stmt_info)
    7907              : {
    7908   1086752048 :   gimple *pattern_stmt;
    7909   1086752048 :   tree pattern_vectype;
    7910              : 
    7911              :   /* If this statement has already been replaced with pattern statements,
    7912              :      leave the original statement alone, since the first match wins.
    7913              :      Instead try to match against the definition statements that feed
    7914              :      the main pattern statement.  */
    7915   1086752048 :   if (STMT_VINFO_IN_PATTERN_P (stmt_info))
    7916              :     {
    7917     13883251 :       gimple_stmt_iterator gsi;
    7918     13883251 :       for (gsi = gsi_start (STMT_VINFO_PATTERN_DEF_SEQ (stmt_info));
    7919     31038339 :            !gsi_end_p (gsi); gsi_next (&gsi))
    7920     17155088 :         vect_pattern_recog_1 (vinfo, recog_func,
    7921              :                               vinfo->lookup_stmt (gsi_stmt (gsi)));
    7922              :       return;
    7923              :     }
    7924              : 
    7925   1072868797 :   gcc_assert (!STMT_VINFO_PATTERN_DEF_SEQ (stmt_info));
    7926   1072868797 :   pattern_stmt = recog_func.fn (vinfo, stmt_info, &pattern_vectype);
    7927   1072868797 :   if (!pattern_stmt)
    7928              :     {
    7929              :       /* Clear any half-formed pattern definition sequence.  */
    7930   1071762706 :       STMT_VINFO_PATTERN_DEF_SEQ (stmt_info) = NULL;
    7931   1071762706 :       return;
    7932              :     }
    7933              : 
    7934              :   /* Found a vectorizable pattern.  */
    7935      1106091 :   if (dump_enabled_p ())
    7936        21663 :     dump_printf_loc (MSG_NOTE, vect_location,
    7937              :                      "%s pattern recognized: %G",
    7938        21663 :                      recog_func.name, pattern_stmt);
    7939              : 
    7940              :   /* Mark the stmts that are involved in the pattern. */
    7941      1106091 :   vect_mark_pattern_stmts (vinfo, stmt_info, pattern_stmt, pattern_vectype);
    7942              : }
    7943              : 
    7944              : 
    7945              : /* Function vect_pattern_recog
    7946              : 
    7947              :    Input:
    7948              :    LOOP_VINFO - a struct_loop_info of a loop in which we want to look for
    7949              :         computation idioms.
    7950              : 
    7951              :    Output - for each computation idiom that is detected we create a new stmt
    7952              :         that provides the same functionality and that can be vectorized.  We
    7953              :         also record some information in the struct_stmt_info of the relevant
    7954              :         stmts, as explained below:
    7955              : 
    7956              :    At the entry to this function we have the following stmts, with the
    7957              :    following initial value in the STMT_VINFO fields:
    7958              : 
    7959              :          stmt                     in_pattern_p  related_stmt    vec_stmt
    7960              :          S1: a_i = ....                 -       -               -
    7961              :          S2: a_2 = ..use(a_i)..         -       -               -
    7962              :          S3: a_1 = ..use(a_2)..         -       -               -
    7963              :          S4: a_0 = ..use(a_1)..         -       -               -
    7964              :          S5: ... = ..use(a_0)..         -       -               -
    7965              : 
    7966              :    Say the sequence {S1,S2,S3,S4} was detected as a pattern that can be
    7967              :    represented by a single stmt.  We then:
    7968              :    - create a new stmt S6 equivalent to the pattern (the stmt is not
    7969              :      inserted into the code)
    7970              :    - fill in the STMT_VINFO fields as follows:
    7971              : 
    7972              :                                   in_pattern_p  related_stmt    vec_stmt
    7973              :          S1: a_i = ....                 -       -               -
    7974              :          S2: a_2 = ..use(a_i)..         -       -               -
    7975              :          S3: a_1 = ..use(a_2)..         -       -               -
    7976              :          S4: a_0 = ..use(a_1)..         true    S6              -
    7977              :           '---> S6: a_new = ....        -       S4              -
    7978              :          S5: ... = ..use(a_0)..         -       -               -
    7979              : 
    7980              :    (the last stmt in the pattern (S4) and the new pattern stmt (S6) point
    7981              :    to each other through the RELATED_STMT field).
    7982              : 
    7983              :    S6 will be marked as relevant in vect_mark_stmts_to_be_vectorized instead
    7984              :    of S4 because it will replace all its uses.  Stmts {S1,S2,S3} will
    7985              :    remain irrelevant unless used by stmts other than S4.
    7986              : 
    7987              :    If vectorization succeeds, vect_transform_stmt will skip over {S1,S2,S3}
    7988              :    (because they are marked as irrelevant).  It will vectorize S6, and record
    7989              :    a pointer to the new vector stmt VS6 from S6 (as usual).
    7990              :    S4 will be skipped, and S5 will be vectorized as usual:
    7991              : 
    7992              :                                   in_pattern_p  related_stmt    vec_stmt
    7993              :          S1: a_i = ....                 -       -               -
    7994              :          S2: a_2 = ..use(a_i)..         -       -               -
    7995              :          S3: a_1 = ..use(a_2)..         -       -               -
    7996              :        > VS6: va_new = ....             -       -               -
    7997              :          S4: a_0 = ..use(a_1)..         true    S6              VS6
    7998              :           '---> S6: a_new = ....        -       S4              VS6
    7999              :        > VS5: ... = ..vuse(va_new)..    -       -               -
    8000              :          S5: ... = ..use(a_0)..         -       -               -
    8001              : 
    8002              :    DCE could then get rid of {S1,S2,S3,S4,S5} (if their defs are not used
    8003              :    elsewhere), and we'll end up with:
    8004              : 
    8005              :         VS6: va_new = ....
    8006              :         VS5: ... = ..vuse(va_new)..
    8007              : 
    8008              :    In case of more than one pattern statements, e.g., widen-mult with
    8009              :    intermediate type:
    8010              : 
    8011              :      S1  a_t = ;
    8012              :      S2  a_T = (TYPE) a_t;
    8013              :            '--> S3: a_it = (interm_type) a_t;
    8014              :      S4  prod_T = a_T * CONST;
    8015              :            '--> S5: prod_T' = a_it w* CONST;
    8016              : 
    8017              :    there may be other users of a_T outside the pattern.  In that case S2 will
    8018              :    be marked as relevant (as well as S3), and both S2 and S3 will be analyzed
    8019              :    and vectorized.  The vector stmt VS2 will be recorded in S2, and VS3 will
    8020              :    be recorded in S3.  */
    8021              : 
    8022              : void
    8023      1155572 : vect_pattern_recog (vec_info *vinfo)
    8024              : {
    8025      1155572 :   basic_block *bbs = vinfo->bbs;
    8026      1155572 :   unsigned int nbbs = vinfo->nbbs;
    8027              : 
    8028      1155572 :   vect_determine_precisions (vinfo);
    8029              : 
    8030      1155572 :   DUMP_VECT_SCOPE ("vect_pattern_recog");
    8031              : 
    8032              :   /* Scan through the stmts in the region, applying the pattern recognition
    8033              :      functions starting at each stmt visited.  */
    8034     13397181 :   for (unsigned i = 0; i < nbbs; i++)
    8035              :     {
    8036     12241609 :       basic_block bb = bbs[i];
    8037              : 
    8038    129379994 :       for (auto si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
    8039              :         {
    8040    104896776 :           stmt_vec_info stmt_info = vinfo->lookup_stmt (gsi_stmt (si));
    8041              : 
    8042    104896776 :           if (!stmt_info || !STMT_VINFO_VECTORIZABLE (stmt_info))
    8043     71471871 :             continue;
    8044              : 
    8045              :           /* Scan over all generic vect_recog_xxx_pattern functions.  */
    8046   1103021865 :           for (const auto &func_ptr : vect_vect_recog_func_ptrs)
    8047   1069596960 :             vect_pattern_recog_1 (vinfo, func_ptr,
    8048              :                                   stmt_info);
    8049              :         }
    8050              :     }
    8051              : 
    8052              :   /* After this no more add_stmt calls are allowed.  */
    8053      1155572 :   vinfo->stmt_vec_info_ro = true;
    8054      1155572 : }
    8055              : 
    8056              : /* Build a GIMPLE_ASSIGN or GIMPLE_CALL with the tree_code,
    8057              :    or internal_fn contained in ch, respectively.  */
    8058              : gimple *
    8059       174758 : vect_gimple_build (tree lhs, code_helper ch, tree op0, tree op1)
    8060              : {
    8061       174758 :   gcc_assert (op0 != NULL_TREE);
    8062       174758 :   if (ch.is_tree_code ())
    8063       174758 :     return gimple_build_assign (lhs, (tree_code) ch, op0, op1);
    8064              : 
    8065            0 :   gcc_assert (ch.is_internal_fn ());
    8066            0 :   gimple* stmt = gimple_build_call_internal (as_internal_fn ((combined_fn) ch),
    8067              :                                              op1 == NULL_TREE ? 1 : 2,
    8068              :                                              op0, op1);
    8069            0 :   gimple_call_set_lhs (stmt, lhs);
    8070            0 :   return stmt;
    8071              : }
        

Generated by: LCOV version 2.6-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.