LCOV - code coverage report
Current view: top level - gcc - optabs.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 68.1 % 3890 2651
Test Date: 2026-12-05 16:10:02 Functions: 84.3 % 134 113
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
       2              :    Copyright (C) 1987-2026 Free Software Foundation, Inc.
       3              : 
       4              : This file is part of GCC.
       5              : 
       6              : GCC is free software; you can redistribute it and/or modify it under
       7              : the terms of the GNU General Public License as published by the Free
       8              : Software Foundation; either version 3, or (at your option) any later
       9              : version.
      10              : 
      11              : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
      12              : WARRANTY; without even the implied warranty of MERCHANTABILITY or
      13              : FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
      14              : for more details.
      15              : 
      16              : You should have received a copy of the GNU General Public License
      17              : along with GCC; see the file COPYING3.  If not see
      18              : <http://www.gnu.org/licenses/>.  */
      19              : 
      20              : 
      21              : #include "config.h"
      22              : #include "system.h"
      23              : #include "coretypes.h"
      24              : #include "backend.h"
      25              : #include "target.h"
      26              : #include "rtl.h"
      27              : #include "tree.h"
      28              : #include "memmodel.h"
      29              : #include "predict.h"
      30              : #include "tm_p.h"
      31              : #include "optabs.h"
      32              : #include "expmed.h"
      33              : #include "emit-rtl.h"
      34              : #include "recog.h"
      35              : #include "diagnostic-core.h"
      36              : #include "rtx-vector-builder.h"
      37              : 
      38              : /* Include insn-config.h before expr.h so that HAVE_conditional_move
      39              :    is properly defined.  */
      40              : #include "stor-layout.h"
      41              : #include "except.h"
      42              : #include "dojump.h"
      43              : #include "explow.h"
      44              : #include "expr.h"
      45              : #include "optabs-tree.h"
      46              : #include "libfuncs.h"
      47              : #include "internal-fn.h"
      48              : #include "langhooks.h"
      49              : #include "gimple.h"
      50              : #include "ssa.h"
      51              : #include "tree-ssa-live.h"
      52              : #include "tree-outof-ssa.h"
      53              : 
      54              : static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
      55              :                                    machine_mode *);
      56              : static rtx expand_unop_direct (machine_mode, optab, rtx, rtx, int);
      57              : static void emit_libcall_block_1 (rtx_insn *, rtx, rtx, rtx, bool);
      58              : 
      59              : static rtx emit_conditional_move_1 (rtx, rtx, rtx, rtx, machine_mode);
      60              : 
      61              : /* Debug facility for use in GDB.  */
      62              : void debug_optab_libfuncs (void);
      63              : 
      64              : /* Add a REG_EQUAL note to the last insn in INSNS.  TARGET is being set to
      65              :    the result of operation CODE applied to OP0 (and OP1 if it is a binary
      66              :    operation).  OP0_MODE is OP0's mode.
      67              : 
      68              :    If the last insn does not set TARGET, don't do anything, but return true.
      69              : 
      70              :    If the last insn or a previous insn sets TARGET and TARGET is one of OP0
      71              :    or OP1, don't add the REG_EQUAL note but return false.  Our caller can then
      72              :    try again, ensuring that TARGET is not one of the operands.  */
      73              : 
      74              : static bool
      75      1288277 : add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0,
      76              :                 rtx op1, machine_mode op0_mode)
      77              : {
      78      1288277 :   rtx_insn *last_insn;
      79      1288277 :   rtx set;
      80      1288277 :   rtx note;
      81              : 
      82      2576554 :   gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
      83              : 
      84      1288277 :   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
      85              :       && GET_RTX_CLASS (code) != RTX_BIN_ARITH
      86              :       && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
      87              :       && GET_RTX_CLASS (code) != RTX_COMPARE
      88              :       && GET_RTX_CLASS (code) != RTX_UNARY)
      89              :     return true;
      90              : 
      91      1280636 :   if (GET_CODE (target) == ZERO_EXTRACT)
      92              :     return true;
      93              : 
      94              :   for (last_insn = insns;
      95      2840331 :        NEXT_INSN (last_insn) != NULL_RTX;
      96              :        last_insn = NEXT_INSN (last_insn))
      97              :     ;
      98              : 
      99              :   /* If TARGET is in OP0 or OP1, punt.  We'd end up with a note referencing
     100              :      a value changing in the insn, so the note would be invalid for CSE.  */
     101      1280636 :   if (reg_overlap_mentioned_p (target, op0)
     102      1280636 :       || (op1 && reg_overlap_mentioned_p (target, op1)))
     103              :     {
     104        59884 :       if (MEM_P (target)
     105        59884 :           && (rtx_equal_p (target, op0)
     106        43927 :               || (op1 && rtx_equal_p (target, op1))))
     107              :         {
     108              :           /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
     109              :              over expanding it as temp = MEM op X, MEM = temp.  If the target
     110              :              supports MEM = MEM op X instructions, it is sometimes too hard
     111              :              to reconstruct that form later, especially if X is also a memory,
     112              :              and due to multiple occurrences of addresses the address might
     113              :              be forced into register unnecessarily.
     114              :              Note that not emitting the REG_EQUIV note might inhibit
     115              :              CSE in some cases.  */
     116         3564 :           set = single_set (last_insn);
     117         3564 :           if (set
     118         3564 :               && GET_CODE (SET_SRC (set)) == code
     119         3537 :               && MEM_P (SET_DEST (set))
     120         7101 :               && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
     121            0 :                   || (op1 && rtx_equal_p (SET_DEST (set),
     122            0 :                                           XEXP (SET_SRC (set), 1)))))
     123              :             return true;
     124              :         }
     125        56347 :       return false;
     126              :     }
     127              : 
     128      1220752 :   set = set_for_reg_notes (last_insn);
     129      1220752 :   if (set == NULL_RTX)
     130              :     return true;
     131              : 
     132       743618 :   if (! rtx_equal_p (SET_DEST (set), target)
     133              :       /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it.  */
     134       743618 :       && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
     135            0 :           || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
     136              :     return true;
     137              : 
     138       742831 :   if (GET_RTX_CLASS (code) == RTX_UNARY)
     139        43486 :     switch (code)
     140              :       {
     141         2996 :       case FFS:
     142         2996 :       case CLZ:
     143         2996 :       case CTZ:
     144         2996 :       case CLRSB:
     145         2996 :       case POPCOUNT:
     146         2996 :       case PARITY:
     147         2996 :       case BSWAP:
     148         2996 :         if (op0_mode != VOIDmode && GET_MODE (target) != op0_mode)
     149              :           {
     150          251 :             note = gen_rtx_fmt_e (code, op0_mode, copy_rtx (op0));
     151          251 :             if (GET_MODE_UNIT_SIZE (op0_mode)
     152          502 :                 > GET_MODE_UNIT_SIZE (GET_MODE (target)))
     153          251 :               note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
     154              :                                          note, op0_mode);
     155              :             else
     156            0 :               note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
     157              :                                          note, op0_mode);
     158              :             break;
     159              :           }
     160              :         /* FALLTHRU */
     161        43235 :       default:
     162        43235 :         note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
     163        43235 :         break;
     164              :       }
     165              :   else
     166       699345 :     note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
     167              : 
     168       742831 :   set_unique_reg_note (last_insn, REG_EQUAL, note);
     169              : 
     170       742831 :   return true;
     171              : }
     172              : 
     173              : /* Given two input operands, OP0 and OP1, determine what the correct from_mode
     174              :    for a widening operation would be.  In most cases this would be OP0, but if
     175              :    that's a constant it'll be VOIDmode, which isn't useful.  */
     176              : 
     177              : static machine_mode
     178        39396 : widened_mode (machine_mode to_mode, rtx op0, rtx op1)
     179              : {
     180        39396 :   machine_mode m0 = GET_MODE (op0);
     181        39396 :   machine_mode m1 = GET_MODE (op1);
     182        39396 :   machine_mode result;
     183              : 
     184        39396 :   if (m0 == VOIDmode && m1 == VOIDmode)
     185              :     return to_mode;
     186        78792 :   else if (m0 == VOIDmode || GET_MODE_UNIT_SIZE (m0) < GET_MODE_UNIT_SIZE (m1))
     187              :     result = m1;
     188              :   else
     189              :     result = m0;
     190              : 
     191       118188 :   if (GET_MODE_UNIT_SIZE (result) > GET_MODE_UNIT_SIZE (to_mode))
     192            0 :     return to_mode;
     193              : 
     194              :   return result;
     195              : }
     196              : 
     197              : /* Widen OP to MODE and return the rtx for the widened operand.  UNSIGNEDP
     198              :    says whether OP is signed or unsigned.  NO_EXTEND is true if we need
     199              :    not actually do a sign-extend or zero-extend, but can leave the
     200              :    higher-order bits of the result rtx undefined, for example, in the case
     201              :    of logical operations, but not right shifts.  */
     202              : 
     203              : static rtx
     204          591 : widen_operand (rtx op, machine_mode mode, machine_mode oldmode,
     205              :                int unsignedp, bool no_extend)
     206              : {
     207          591 :   rtx result;
     208          591 :   scalar_int_mode int_mode;
     209              : 
     210              :   /* If we don't have to extend and this is a constant, return it.  */
     211          591 :   if (no_extend && GET_MODE (op) == VOIDmode)
     212              :     return op;
     213              : 
     214              :   /* If we must extend do so.  If OP is a SUBREG for a promoted object, also
     215              :      extend since it will be more efficient to do so unless the signedness of
     216              :      a promoted object differs from our extension.  */
     217            7 :   if (! no_extend
     218          591 :       || !is_a <scalar_int_mode> (mode, &int_mode)
     219            7 :       || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
     220            0 :           && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
     221          584 :     return convert_modes (mode, oldmode, op, unsignedp);
     222              : 
     223              :   /* If MODE is no wider than a single word, we return a lowpart or paradoxical
     224              :      SUBREG.  */
     225           14 :   if (GET_MODE_SIZE (int_mode) <= UNITS_PER_WORD)
     226            7 :     return gen_lowpart (int_mode, force_reg (GET_MODE (op), op));
     227              : 
     228              :   /* Otherwise, get an object of MODE, clobber it, and set the low-order
     229              :      part to OP.  */
     230              : 
     231            0 :   result = gen_reg_rtx (int_mode);
     232            0 :   emit_clobber (result);
     233            0 :   emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
     234            0 :   return result;
     235              : }
     236              : 
     237              : /* Expand vector widening operations.
     238              : 
     239              :    There are two different classes of operations handled here:
     240              :    1) Operations whose result is wider than all the arguments to the operation.
     241              :       Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
     242              :       In this case OP0 and optionally OP1 would be initialized,
     243              :       but WIDE_OP wouldn't (not relevant for this case).
     244              :    2) Operations whose result is of the same size as the last argument to the
     245              :       operation, but wider than all the other arguments to the operation.
     246              :       Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
     247              :       In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
     248              : 
     249              :    E.g, when called to expand the following operations, this is how
     250              :    the arguments will be initialized:
     251              :                                 nops    OP0     OP1     WIDE_OP
     252              :    widening-sum                 2       oprnd0  -       oprnd1
     253              :    widening-dot-product         3       oprnd0  oprnd1  oprnd2
     254              :    widening-mult                2       oprnd0  oprnd1  -
     255              :    type-promotion (vec-unpack)  1       oprnd0  -       -  */
     256              : 
     257              : rtx
     258        21996 : expand_widen_pattern_expr (const_sepops ops, rtx op0, rtx op1, rtx wide_op,
     259              :                            rtx target, int unsignedp)
     260              : {
     261        21996 :   class expand_operand eops[4];
     262        21996 :   tree oprnd0, oprnd1, oprnd2;
     263        21996 :   machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
     264        21996 :   optab widen_pattern_optab;
     265        21996 :   enum insn_code icode;
     266        21996 :   int nops = TREE_CODE_LENGTH (ops->code);
     267        21996 :   int op;
     268        21996 :   bool sbool = false;
     269              : 
     270        21996 :   oprnd0 = ops->op0;
     271        21996 :   oprnd1 = nops >= 2 ? ops->op1 : NULL_TREE;
     272         1725 :   oprnd2 = nops >= 3 ? ops->op2 : NULL_TREE;
     273              : 
     274        21996 :   tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
     275        21996 :   if (ops->code == VEC_UNPACK_FIX_TRUNC_HI_EXPR
     276        21996 :       || ops->code == VEC_UNPACK_FIX_TRUNC_LO_EXPR)
     277              :     /* The sign is from the result type rather than operand's type
     278              :        for these ops.  */
     279           34 :     widen_pattern_optab
     280           34 :       = optab_for_tree_code (ops->code, ops->type, optab_default);
     281        21962 :   else if ((ops->code == VEC_UNPACK_HI_EXPR
     282        21962 :             || ops->code == VEC_UNPACK_LO_EXPR)
     283        18305 :            && VECTOR_BOOLEAN_TYPE_P (ops->type)
     284         4424 :            && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (oprnd0))
     285         4424 :            && TYPE_MODE (ops->type) == TYPE_MODE (TREE_TYPE (oprnd0))
     286        22462 :            && SCALAR_INT_MODE_P (TYPE_MODE (ops->type)))
     287              :     {
     288              :       /* For VEC_UNPACK_{LO,HI}_EXPR if the mode of op0 and result is
     289              :          the same scalar mode for VECTOR_BOOLEAN_TYPE_P vectors, use
     290              :          vec_unpacks_sbool_{lo,hi}_optab, so that we can pass in
     291              :          the pattern number of elements in the wider vector.  */
     292          500 :       widen_pattern_optab
     293          247 :         = (ops->code == VEC_UNPACK_HI_EXPR
     294          500 :            ? vec_unpacks_sbool_hi_optab : vec_unpacks_sbool_lo_optab);
     295              :       sbool = true;
     296              :     }
     297        21462 :   else if (ops->code == DOT_PROD_EXPR)
     298              :     {
     299          507 :       enum optab_subtype subtype = optab_default;
     300          507 :       signop sign1 = TYPE_SIGN (TREE_TYPE (oprnd0));
     301          507 :       signop sign2 = TYPE_SIGN (TREE_TYPE (oprnd1));
     302          507 :       if (sign1 == sign2)
     303              :         ;
     304           82 :       else if (sign1 == SIGNED && sign2 == UNSIGNED)
     305              :         {
     306              :           subtype = optab_vector_mixed_sign;
     307              :           /* Same as optab_vector_mixed_sign but flip the operands.  */
     308              :           std::swap (op0, op1);
     309              :         }
     310           60 :       else if (sign1 == UNSIGNED && sign2 == SIGNED)
     311              :         subtype = optab_vector_mixed_sign;
     312              :       else
     313            0 :         gcc_unreachable ();
     314              : 
     315          507 :       widen_pattern_optab
     316          507 :         = optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), subtype);
     317              :     }
     318              :   else
     319        20955 :     widen_pattern_optab
     320        20955 :       = optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
     321        21996 :   if (ops->code == WIDEN_MULT_PLUS_EXPR
     322        21996 :       || ops->code == WIDEN_MULT_MINUS_EXPR
     323        21996 :       || ops->code == DOT_PROD_EXPR
     324        21489 :       || ops->code == SAD_EXPR)
     325          626 :     icode = find_widening_optab_handler (widen_pattern_optab,
     326              :                                          TYPE_MODE (TREE_TYPE (ops->op2)),
     327              :                                          tmode0);
     328        21370 :   else if (ops->code == WIDEN_SUM_EXPR)
     329            0 :     icode = find_widening_optab_handler (widen_pattern_optab,
     330              :                                          TYPE_MODE (TREE_TYPE (ops->op1)),
     331              :                                          tmode0);
     332              :   else
     333        21370 :     icode = optab_handler (widen_pattern_optab, tmode0);
     334        21996 :   gcc_assert (icode != CODE_FOR_nothing);
     335              : 
     336        21996 :   if (nops >= 2)
     337         1725 :     tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
     338        20271 :   else if (sbool)
     339              :     {
     340          500 :       nops = 2;
     341          500 :       op1 = GEN_INT (TYPE_VECTOR_SUBPARTS (TREE_TYPE (oprnd0)).to_constant ());
     342          500 :       tmode1 = tmode0;
     343              :     }
     344              : 
     345              :   /* The last operand is of a wider mode than the rest of the operands.  */
     346        21996 :   if (nops == 2)
     347              :     wmode = tmode1;
     348        20397 :   else if (nops == 3)
     349              :     {
     350          626 :       gcc_assert (tmode1 == tmode0);
     351          626 :       gcc_assert (op1);
     352          626 :       wmode = TYPE_MODE (TREE_TYPE (oprnd2));
     353              :     }
     354              : 
     355        21996 :   op = 0;
     356        21996 :   create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
     357        21996 :   create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
     358        21996 :   if (op1)
     359         2225 :     create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
     360        21996 :   if (wide_op)
     361          626 :     create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
     362        21996 :   expand_insn (icode, op, eops);
     363        21996 :   return eops[0].value;
     364              : }
     365              : 
     366              : /* Generate code to perform an operation specified by TERNARY_OPTAB
     367              :    on operands OP0, OP1 and OP2, with result having machine-mode MODE.
     368              : 
     369              :    UNSIGNEDP is for the case where we have to widen the operands
     370              :    to perform the operation.  It says to use zero-extension.
     371              : 
     372              :    If TARGET is nonzero, the value
     373              :    is generated there, if it is convenient to do so.
     374              :    In all cases an rtx is returned for the locus of the value;
     375              :    this may or may not be TARGET.  */
     376              : 
     377              : rtx
     378            0 : expand_ternary_op (machine_mode mode, optab ternary_optab, rtx op0,
     379              :                    rtx op1, rtx op2, rtx target, int unsignedp)
     380              : {
     381            0 :   class expand_operand ops[4];
     382            0 :   enum insn_code icode = optab_handler (ternary_optab, mode);
     383              : 
     384            0 :   gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
     385              : 
     386            0 :   create_output_operand (&ops[0], target, mode);
     387            0 :   create_convert_operand_from (&ops[1], op0, mode, unsignedp);
     388            0 :   create_convert_operand_from (&ops[2], op1, mode, unsignedp);
     389            0 :   create_convert_operand_from (&ops[3], op2, mode, unsignedp);
     390            0 :   expand_insn (icode, 4, ops);
     391            0 :   return ops[0].value;
     392              : }
     393              : 
     394              : 
     395              : /* Like expand_binop, but return a constant rtx if the result can be
     396              :    calculated at compile time.  The arguments and return value are
     397              :    otherwise the same as for expand_binop.  */
     398              : 
     399              : rtx
     400         1693 : simplify_expand_binop (machine_mode mode, optab binoptab,
     401              :                        rtx op0, rtx op1, rtx target, int unsignedp,
     402              :                        enum optab_methods methods)
     403              : {
     404         1693 :   if (CONSTANT_P (op0) && CONSTANT_P (op1))
     405              :     {
     406          184 :       rtx x = simplify_binary_operation (optab_to_code (binoptab),
     407              :                                          mode, op0, op1);
     408          184 :       if (x)
     409              :         return x;
     410              :     }
     411              : 
     412         1509 :   return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
     413              : }
     414              : 
     415              : /* Like simplify_expand_binop, but always put the result in TARGET.
     416              :    Return true if the expansion succeeded.  */
     417              : 
     418              : bool
     419         1325 : force_expand_binop (machine_mode mode, optab binoptab,
     420              :                     rtx op0, rtx op1, rtx target, int unsignedp,
     421              :                     enum optab_methods methods)
     422              : {
     423         1325 :   rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
     424              :                                  target, unsignedp, methods);
     425         1325 :   if (x == 0)
     426              :     return false;
     427         1325 :   if (x != target)
     428            0 :     emit_move_insn (target, x);
     429              :   return true;
     430              : }
     431              : 
     432              : /* Create a new vector value in VMODE with all elements set to OP.  If OP
     433              :    is not a constant, the mode of it must be the element mode of VMODE
     434              :    (if the element is BImode, additionally OP is allowed to be in QImode).
     435              :    If OP is a constant, then the return value will be a constant.  */
     436              : 
     437              : rtx
     438           46 : expand_vector_broadcast (machine_mode vmode, rtx op)
     439              : {
     440           46 :   int n;
     441           46 :   rtvec vec;
     442              : 
     443           46 :   gcc_checking_assert (VECTOR_MODE_P (vmode));
     444           46 :   gcc_checking_assert (CONST_INT_P (op)
     445              :                        || GET_MODE_INNER (vmode) == GET_MODE (op)
     446              :                        || (GET_MODE_INNER (vmode) == BImode
     447              :                            && GET_MODE (op) == QImode));
     448              : 
     449           46 :   if (valid_for_const_vector_p (vmode, op))
     450           46 :     return gen_const_vec_duplicate (vmode, op);
     451              : 
     452            0 :   insn_code icode = optab_handler (vec_duplicate_optab, vmode);
     453            0 :   if (icode != CODE_FOR_nothing)
     454              :     {
     455            0 :       class expand_operand ops[2];
     456            0 :       create_output_operand (&ops[0], NULL_RTX, vmode);
     457            0 :       create_input_operand (&ops[1], op, GET_MODE (op));
     458            0 :       expand_insn (icode, 2, ops);
     459            0 :       return ops[0].value;
     460              :     }
     461              : 
     462            0 :   if (!GET_MODE_NUNITS (vmode).is_constant (&n))
     463              :     return NULL;
     464              : 
     465              :   /* ??? If the target doesn't have a vec_init, then we have no easy way
     466              :      of performing this operation.  Most of this sort of generic support
     467              :      is hidden away in the vector lowering support in gimple.  */
     468            0 :   icode = convert_optab_handler (vec_init_optab, vmode,
     469            0 :                                  GET_MODE_INNER (vmode));
     470            0 :   if (icode == CODE_FOR_nothing)
     471              :     return NULL;
     472              : 
     473            0 :   vec = rtvec_alloc (n);
     474            0 :   for (int i = 0; i < n; ++i)
     475            0 :     RTVEC_ELT (vec, i) = op;
     476            0 :   rtx ret = gen_reg_rtx (vmode);
     477            0 :   emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
     478              : 
     479            0 :   return ret;
     480              : }
     481              : 
     482              : /* This subroutine of expand_doubleword_shift handles the cases in which
     483              :    the effective shift value is >= BITS_PER_WORD.  The arguments and return
     484              :    value are the same as for the parent routine, except that SUPERWORD_OP1
     485              :    is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
     486              :    INTO_TARGET may be null if the caller has decided to calculate it.  */
     487              : 
     488              : static bool
     489            0 : expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
     490              :                         rtx outof_target, rtx into_target,
     491              :                         int unsignedp, enum optab_methods methods)
     492              : {
     493            0 :   if (into_target != 0)
     494            0 :     if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
     495              :                              into_target, unsignedp, methods))
     496              :       return false;
     497              : 
     498            0 :   if (outof_target != 0)
     499              :     {
     500              :       /* For a signed right shift, we must fill OUTOF_TARGET with copies
     501              :          of the sign bit, otherwise we must fill it with zeros.  */
     502            0 :       if (binoptab != ashr_optab)
     503            0 :         emit_move_insn (outof_target, CONST0_RTX (word_mode));
     504              :       else
     505            0 :         if (!force_expand_binop (word_mode, binoptab, outof_input,
     506              :                                  gen_int_shift_amount (word_mode,
     507            0 :                                                        BITS_PER_WORD - 1),
     508              :                                  outof_target, unsignedp, methods))
     509              :           return false;
     510              :     }
     511              :   return true;
     512              : }
     513              : 
     514              : /* This subroutine of expand_doubleword_shift handles the cases in which
     515              :    the effective shift value is < BITS_PER_WORD.  The arguments and return
     516              :    value are the same as for the parent routine.  */
     517              : 
     518              : static bool
     519            0 : expand_subword_shift (scalar_int_mode op1_mode, optab binoptab,
     520              :                       rtx outof_input, rtx into_input, rtx op1,
     521              :                       rtx outof_target, rtx into_target,
     522              :                       int unsignedp, enum optab_methods methods,
     523              :                       unsigned HOST_WIDE_INT shift_mask)
     524              : {
     525            0 :   optab reverse_unsigned_shift, unsigned_shift;
     526            0 :   rtx tmp, carries;
     527              : 
     528            0 :   reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
     529            0 :   unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
     530              : 
     531              :   /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
     532              :      We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
     533              :      the opposite direction to BINOPTAB.  */
     534            0 :   if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
     535              :     {
     536            0 :       carries = outof_input;
     537            0 :       tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
     538              :                                             op1_mode), op1_mode);
     539            0 :       tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
     540              :                                    0, true, methods);
     541              :     }
     542              :   else
     543              :     {
     544              :       /* We must avoid shifting by BITS_PER_WORD bits since that is either
     545              :          the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
     546              :          has unknown behavior.  Do a single shift first, then shift by the
     547              :          remainder.  It's OK to use ~OP1 as the remainder if shift counts
     548              :          are truncated to the mode size.  */
     549            0 :       carries = simplify_expand_binop (word_mode, reverse_unsigned_shift,
     550              :                                        outof_input, const1_rtx, 0,
     551              :                                        unsignedp, methods);
     552            0 :       if (carries == const0_rtx)
     553              :         tmp = const0_rtx;
     554            0 :       else if (shift_mask == BITS_PER_WORD - 1)
     555            0 :         tmp = expand_unop (op1_mode, one_cmpl_optab, op1, 0, true);
     556              :       else
     557              :         {
     558            0 :           tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
     559              :                                                 op1_mode), op1_mode);
     560            0 :           tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
     561              :                                        0, true, methods);
     562              :         }
     563              :     }
     564            0 :   if (tmp == 0 || carries == 0)
     565              :     return false;
     566            0 :   if (carries != const0_rtx && tmp != const0_rtx)
     567            0 :     carries = simplify_expand_binop (word_mode, reverse_unsigned_shift,
     568              :                                      carries, tmp, 0, unsignedp, methods);
     569            0 :   if (carries == 0)
     570              :     return false;
     571              : 
     572            0 :   if (into_input != const0_rtx)
     573              :     {
     574              :       /* Shift INTO_INPUT logically by OP1.  This is the last use of
     575              :          INTO_INPUT so the result can go directly into INTO_TARGET if
     576              :          convenient.  */
     577            0 :       tmp = simplify_expand_binop (word_mode, unsigned_shift, into_input,
     578              :                                    op1, into_target, unsignedp, methods);
     579            0 :       if (tmp == 0)
     580              :         return false;
     581              : 
     582              :       /* Now OR/PLUS in the bits carried over from OUTOF_INPUT.  */
     583            0 :       if (!force_expand_binop (word_mode, add_optab, tmp, carries,
     584              :                                into_target, unsignedp, methods))
     585              :         return false;
     586              :     }
     587              :   else
     588            0 :     emit_move_insn (into_target, carries);
     589              : 
     590              :   /* Use a standard word_mode shift for the out-of half.  */
     591            0 :   if (outof_target != 0)
     592            0 :     if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
     593              :                              outof_target, unsignedp, methods))
     594              :       return false;
     595              : 
     596              :   return true;
     597              : }
     598              : 
     599              : 
     600              : /* Try implementing expand_doubleword_shift using conditional moves.
     601              :    The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
     602              :    otherwise it is by >= BITS_PER_WORD.  SUBWORD_OP1 and SUPERWORD_OP1
     603              :    are the shift counts to use in the former and latter case.  All other
     604              :    arguments are the same as the parent routine.  */
     605              : 
     606              : static bool
     607            0 : expand_doubleword_shift_condmove (scalar_int_mode op1_mode, optab binoptab,
     608              :                                   enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
     609              :                                   rtx outof_input, rtx into_input,
     610              :                                   rtx subword_op1, rtx superword_op1,
     611              :                                   rtx outof_target, rtx into_target,
     612              :                                   int unsignedp, enum optab_methods methods,
     613              :                                   unsigned HOST_WIDE_INT shift_mask)
     614              : {
     615            0 :   rtx outof_superword, into_superword;
     616              : 
     617              :   /* Put the superword version of the output into OUTOF_SUPERWORD and
     618              :      INTO_SUPERWORD.  */
     619            0 :   outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
     620            0 :   if (outof_target != 0 && subword_op1 == superword_op1)
     621              :     {
     622              :       /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
     623              :          OUTOF_TARGET, is the same as the value of INTO_SUPERWORD.  */
     624            0 :       into_superword = outof_target;
     625            0 :       if (!expand_superword_shift (binoptab, outof_input, superword_op1,
     626              :                                    outof_superword, 0, unsignedp, methods))
     627              :         return false;
     628              :     }
     629              :   else
     630              :     {
     631            0 :       into_superword = gen_reg_rtx (word_mode);
     632            0 :       if (!expand_superword_shift (binoptab, outof_input, superword_op1,
     633              :                                    outof_superword, into_superword,
     634              :                                    unsignedp, methods))
     635              :         return false;
     636              :     }
     637              : 
     638              :   /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET.  */
     639            0 :   if (!expand_subword_shift (op1_mode, binoptab,
     640              :                              outof_input, into_input, subword_op1,
     641              :                              outof_target, into_target,
     642              :                              unsignedp, methods, shift_mask))
     643              :     return false;
     644              : 
     645              :   /* Select between them.  Do the INTO half first because INTO_SUPERWORD
     646              :      might be the current value of OUTOF_TARGET.  */
     647            0 :   if (!emit_conditional_move (into_target, { cmp_code, cmp1, cmp2, op1_mode },
     648              :                               into_target, into_superword, word_mode, false))
     649              :     return false;
     650              : 
     651            0 :   if (outof_target != 0)
     652            0 :     if (!emit_conditional_move (outof_target,
     653            0 :                                 { cmp_code, cmp1, cmp2, op1_mode },
     654              :                                 outof_target, outof_superword,
     655              :                                 word_mode, false))
     656            0 :       return false;
     657              : 
     658              :   return true;
     659              : }
     660              : 
     661              : /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
     662              :    OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
     663              :    input operand; the shift moves bits in the direction OUTOF_INPUT->
     664              :    INTO_TARGET.  OUTOF_TARGET and INTO_TARGET are the equivalent words
     665              :    of the target.  OP1 is the shift count and OP1_MODE is its mode.
     666              :    If OP1 is constant, it will have been truncated as appropriate
     667              :    and is known to be nonzero.
     668              : 
     669              :    If SHIFT_MASK is zero, the result of word shifts is undefined when the
     670              :    shift count is outside the range [0, BITS_PER_WORD).  This routine must
     671              :    avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
     672              : 
     673              :    If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
     674              :    masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
     675              :    fill with zeros or sign bits as appropriate.
     676              : 
     677              :    If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
     678              :    a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
     679              :    Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
     680              :    In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
     681              :    are undefined.
     682              : 
     683              :    BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop.  This function
     684              :    may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
     685              :    OUTOF_INPUT and OUTOF_TARGET.  OUTOF_TARGET can be null if the parent
     686              :    function wants to calculate it itself.
     687              : 
     688              :    Return true if the shift could be successfully synthesized.  */
     689              : 
     690              : static bool
     691            0 : expand_doubleword_shift (scalar_int_mode op1_mode, optab binoptab,
     692              :                          rtx outof_input, rtx into_input, rtx op1,
     693              :                          rtx outof_target, rtx into_target,
     694              :                          int unsignedp, enum optab_methods methods,
     695              :                          unsigned HOST_WIDE_INT shift_mask)
     696              : {
     697            0 :   rtx superword_op1, tmp, cmp1, cmp2;
     698            0 :   enum rtx_code cmp_code;
     699              : 
     700              :   /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
     701              :      fill the result with sign or zero bits as appropriate.  If so, the value
     702              :      of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1).   Recursively call
     703              :      this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
     704              :      and INTO_INPUT), then emit code to set up OUTOF_TARGET.
     705              : 
     706              :      This isn't worthwhile for constant shifts since the optimizers will
     707              :      cope better with in-range shift counts.  */
     708            0 :   if (shift_mask >= BITS_PER_WORD
     709            0 :       && outof_target != 0
     710            0 :       && !CONSTANT_P (op1))
     711              :     {
     712            0 :       if (!expand_doubleword_shift (op1_mode, binoptab,
     713              :                                     outof_input, into_input, op1,
     714              :                                     0, into_target,
     715              :                                     unsignedp, methods, shift_mask))
     716              :         return false;
     717            0 :       if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
     718              :                                outof_target, unsignedp, methods))
     719              :         return false;
     720              :       return true;
     721              :     }
     722              : 
     723              :   /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
     724              :      is true when the effective shift value is less than BITS_PER_WORD.
     725              :      Set SUPERWORD_OP1 to the shift count that should be used to shift
     726              :      OUTOF_INPUT into INTO_TARGET when the condition is false.  */
     727            0 :   tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
     728            0 :   if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
     729              :     {
     730              :       /* Set CMP1 to OP1 & BITS_PER_WORD.  The result is zero iff OP1
     731              :          is a subword shift count.  */
     732            0 :       cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
     733              :                                     0, true, methods);
     734            0 :       cmp2 = CONST0_RTX (op1_mode);
     735            0 :       cmp_code = EQ;
     736            0 :       superword_op1 = op1;
     737              :     }
     738              :   else
     739              :     {
     740              :       /* Set CMP1 to OP1 - BITS_PER_WORD.  */
     741            0 :       cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
     742              :                                     0, true, methods);
     743            0 :       cmp2 = CONST0_RTX (op1_mode);
     744            0 :       cmp_code = LT;
     745            0 :       superword_op1 = cmp1;
     746              :     }
     747            0 :   if (cmp1 == 0)
     748              :     return false;
     749              : 
     750              :   /* If we can compute the condition at compile time, pick the
     751              :      appropriate subroutine.  */
     752            0 :   tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
     753            0 :   if (tmp != 0 && CONST_INT_P (tmp))
     754              :     {
     755            0 :       if (tmp == const0_rtx)
     756            0 :         return expand_superword_shift (binoptab, outof_input, superword_op1,
     757              :                                        outof_target, into_target,
     758            0 :                                        unsignedp, methods);
     759              :       else
     760            0 :         return expand_subword_shift (op1_mode, binoptab,
     761              :                                      outof_input, into_input, op1,
     762              :                                      outof_target, into_target,
     763            0 :                                      unsignedp, methods, shift_mask);
     764              :     }
     765              : 
     766              :   /* Try using conditional moves to generate straight-line code.  */
     767            0 :   if (HAVE_conditional_move)
     768              :     {
     769            0 :       rtx_insn *start = get_last_insn ();
     770            0 :       if (expand_doubleword_shift_condmove (op1_mode, binoptab,
     771              :                                             cmp_code, cmp1, cmp2,
     772              :                                             outof_input, into_input,
     773              :                                             op1, superword_op1,
     774              :                                             outof_target, into_target,
     775              :                                             unsignedp, methods, shift_mask))
     776              :         return true;
     777            0 :       delete_insns_since (start);
     778              :     }
     779              : 
     780              :   /* As a last resort, use branches to select the correct alternative.  */
     781            0 :   rtx_code_label *subword_label = gen_label_rtx ();
     782            0 :   rtx_code_label *done_label = gen_label_rtx ();
     783              : 
     784            0 :   NO_DEFER_POP;
     785            0 :   do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
     786              :                            0, 0, subword_label,
     787              :                            profile_probability::uninitialized ());
     788            0 :   OK_DEFER_POP;
     789              : 
     790            0 :   if (!expand_superword_shift (binoptab, outof_input, superword_op1,
     791              :                                outof_target, into_target,
     792              :                                unsignedp, methods))
     793              :     return false;
     794              : 
     795            0 :   emit_jump_insn (targetm.gen_jump (done_label));
     796            0 :   emit_barrier ();
     797            0 :   emit_label (subword_label);
     798              : 
     799            0 :   if (!expand_subword_shift (op1_mode, binoptab,
     800              :                              outof_input, into_input, op1,
     801              :                              outof_target, into_target,
     802              :                              unsignedp, methods, shift_mask))
     803              :     return false;
     804              : 
     805            0 :   emit_label (done_label);
     806            0 :   return true;
     807              : }
     808              : 
     809              : /* Subroutine of expand_binop.  Perform a double word multiplication of
     810              :    operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
     811              :    as the target's word_mode.  This function return NULL_RTX if anything
     812              :    goes wrong, in which case it may have already emitted instructions
     813              :    which need to be deleted.
     814              : 
     815              :    If we want to multiply two two-word values and have normal and widening
     816              :    multiplies of single-word values, we can do this with three smaller
     817              :    multiplications.
     818              : 
     819              :    The multiplication proceeds as follows:
     820              :                                  _______________________
     821              :                                 [__op0_high_|__op0_low__]
     822              :                                  _______________________
     823              :         *                       [__op1_high_|__op1_low__]
     824              :         _______________________________________________
     825              :                                  _______________________
     826              :     (1)                         [__op0_low__*__op1_low__]
     827              :                      _______________________
     828              :     (2a)            [__op0_low__*__op1_high_]
     829              :                      _______________________
     830              :     (2b)            [__op0_high_*__op1_low__]
     831              :          _______________________
     832              :     (3) [__op0_high_*__op1_high_]
     833              : 
     834              : 
     835              :   This gives a 4-word result.  Since we are only interested in the
     836              :   lower 2 words, partial result (3) and the upper words of (2a) and
     837              :   (2b) don't need to be calculated.  Hence (2a) and (2b) can be
     838              :   calculated using non-widening multiplication.
     839              : 
     840              :   (1), however, needs to be calculated with an unsigned widening
     841              :   multiplication.  If this operation is not directly supported we
     842              :   try using a signed widening multiplication and adjust the result.
     843              :   This adjustment works as follows:
     844              : 
     845              :       If both operands are positive then no adjustment is needed.
     846              : 
     847              :       If the operands have different signs, for example op0_low < 0 and
     848              :       op1_low >= 0, the instruction treats the most significant bit of
     849              :       op0_low as a sign bit instead of a bit with significance
     850              :       2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
     851              :       with 2**BITS_PER_WORD - op0_low, and two's complements the
     852              :       result.  Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
     853              :       the result.
     854              : 
     855              :       Similarly, if both operands are negative, we need to add
     856              :       (op0_low + op1_low) * 2**BITS_PER_WORD.
     857              : 
     858              :       We use a trick to adjust quickly.  We logically shift op0_low right
     859              :       (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
     860              :       op0_high (op1_high) before it is used to calculate 2b (2a).  If no
     861              :       logical shift exists, we do an arithmetic right shift and subtract
     862              :       the 0 or -1.  */
     863              : 
     864              : static rtx
     865        16155 : expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
     866              :                        bool umulp, enum optab_methods methods)
     867              : {
     868        16155 :   int low = (WORDS_BIG_ENDIAN ? 1 : 0);
     869        16155 :   int high = (WORDS_BIG_ENDIAN ? 0 : 1);
     870        16155 :   rtx wordm1 = (umulp ? NULL_RTX
     871            0 :                 : gen_int_shift_amount (word_mode, BITS_PER_WORD - 1));
     872        16155 :   rtx product, adjust, product_high, temp;
     873              : 
     874        16155 :   rtx op0_high = operand_subword_force (op0, high, mode);
     875        16155 :   rtx op0_low = operand_subword_force (op0, low, mode);
     876        16155 :   rtx op1_high = operand_subword_force (op1, high, mode);
     877        16155 :   rtx op1_low = operand_subword_force (op1, low, mode);
     878              : 
     879              :   /* If we're using an unsigned multiply to directly compute the product
     880              :      of the low-order words of the operands and perform any required
     881              :      adjustments of the operands, we begin by trying two more multiplications
     882              :      and then computing the appropriate sum.
     883              : 
     884              :      We have checked above that the required addition is provided.
     885              :      Full-word addition will normally always succeed, especially if
     886              :      it is provided at all, so we don't worry about its failure.  The
     887              :      multiplication may well fail, however, so we do handle that.  */
     888              : 
     889        16155 :   if (!umulp)
     890              :     {
     891              :       /* ??? This could be done with emit_store_flag where available.  */
     892            0 :       temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
     893              :                            NULL_RTX, 1, methods);
     894            0 :       if (temp)
     895            0 :         op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
     896              :                                  NULL_RTX, 0, OPTAB_DIRECT);
     897              :       else
     898              :         {
     899            0 :           temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
     900              :                                NULL_RTX, 0, methods);
     901            0 :           if (!temp)
     902              :             return NULL_RTX;
     903            0 :           op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
     904              :                                    NULL_RTX, 0, OPTAB_DIRECT);
     905              :         }
     906              : 
     907            0 :       if (!op0_high)
     908              :         return NULL_RTX;
     909              :     }
     910              : 
     911        16155 :   if (op1_low == const1_rtx)
     912              :     adjust = op0_high;
     913        16115 :   else if (op1_low == const0_rtx)
     914              :     adjust = const0_rtx;
     915        16040 :   else if (op1_low == const2_rtx)
     916            0 :     adjust = expand_binop (word_mode, add_optab, op0_high, op0_high,
     917              :                            NULL_RTX, 0, OPTAB_DIRECT);
     918              :   else
     919        16040 :     adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
     920              :                            NULL_RTX, 0, OPTAB_DIRECT);
     921        16155 :   if (!adjust)
     922              :     return NULL_RTX;
     923              : 
     924              :   /* OP0_HIGH should now be dead.  */
     925              : 
     926        16155 :   if (!umulp)
     927              :     {
     928              :       /* ??? This could be done with emit_store_flag where available.  */
     929            0 :       temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
     930              :                            NULL_RTX, 1, methods);
     931            0 :       if (temp)
     932            0 :         op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
     933              :                                  NULL_RTX, 0, OPTAB_DIRECT);
     934              :       else
     935              :         {
     936            0 :           temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
     937              :                                NULL_RTX, 0, methods);
     938            0 :           if (!temp)
     939              :             return NULL_RTX;
     940            0 :           op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
     941              :                                    NULL_RTX, 0, OPTAB_DIRECT);
     942              :         }
     943              : 
     944            0 :       if (!op1_high)
     945              :         return NULL_RTX;
     946              :     }
     947              : 
     948        16155 :   if (op1_high == const1_rtx)
     949              :     temp = op0_low;
     950        16154 :   else if (op1_high == const0_rtx)
     951              :     temp = const0_rtx;
     952        15342 :   else if (op1_high == const2_rtx)
     953            0 :     temp = expand_binop (word_mode, add_optab, op0_low, op0_low,
     954              :                          NULL_RTX, 0, OPTAB_DIRECT);
     955              :   else
     956        15342 :     temp = expand_binop (word_mode, smul_optab, op0_low, op1_high,
     957              :                          NULL_RTX, 0, OPTAB_DIRECT);
     958        16155 :   if (!temp)
     959              :     return NULL_RTX;
     960              : 
     961              :   /* OP1_HIGH should now be dead.  */
     962              : 
     963        16155 :   adjust = expand_binop (word_mode, add_optab, adjust, temp,
     964              :                          NULL_RTX, 0, OPTAB_DIRECT);
     965              : 
     966        16155 :   if (target && !REG_P (target))
     967         9682 :     target = NULL_RTX;
     968              : 
     969              :   /* *_widen_optab needs to determine operand mode, make sure at least
     970              :      one operand has non-VOID mode.  */
     971        16155 :   if (GET_MODE (op0_low) == VOIDmode && GET_MODE (op1_low) == VOIDmode)
     972            0 :     op0_low = force_reg (word_mode, op0_low);
     973              : 
     974        16155 :   if (op1_low == const1_rtx)
     975           40 :     product = convert_modes (mode, word_mode, op0_low, umulp);
     976        16115 :   else if (umulp)
     977        16115 :     product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
     978              :                             target, 1, OPTAB_DIRECT);
     979              :   else
     980            0 :     product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
     981              :                             target, 1, OPTAB_DIRECT);
     982              : 
     983        16155 :   if (!product)
     984              :     return NULL_RTX;
     985              : 
     986        16155 :   product_high = operand_subword (product, high, 1, mode);
     987        16155 :   adjust = expand_binop (word_mode, add_optab, product_high, adjust,
     988              :                          NULL_RTX, 0, OPTAB_DIRECT);
     989        16155 :   emit_move_insn (product_high, adjust);
     990        16155 :   return product;
     991              : }
     992              : 
     993              : /* Subroutine of expand_binop.  Optimize unsigned double-word OP0 % OP1 for
     994              :    constant OP1.  If for some bit in [BITS_PER_WORD / 2, BITS_PER_WORD] range
     995              :    (prefer higher bits) ((1w << bit) % OP1) == 1, then the modulo can be
     996              :    computed in word-mode as ((OP0 & (bit - 1)) + ((OP0 >> bit) & (bit - 1))
     997              :    + (OP0 >> (2 * bit))) % OP1.  Whether we need to sum 2, 3 or 4 values
     998              :    depends on the bit value, if 2, then carry from the addition needs to be
     999              :    added too, i.e. like:
    1000              :    sum += __builtin_add_overflow (low, high, &sum)
    1001              : 
    1002              :    Optimize signed double-word OP0 % OP1 similarly, just apply some correction
    1003              :    factor to the sum before doing unsigned remainder, in the form of
    1004              :    sum += (((signed) OP0 >> (2 * BITS_PER_WORD - 1)) & const);
    1005              :    then perform unsigned
    1006              :    remainder = sum % OP1;
    1007              :    and finally
    1008              :    remainder += ((signed) OP0 >> (2 * BITS_PER_WORD - 1)) & (1 - OP1);  */
    1009              : 
    1010              : static rtx
    1011        21179 : expand_doubleword_mod (machine_mode mode, rtx op0, rtx op1, bool unsignedp)
    1012              : {
    1013        21179 :   if (INTVAL (op1) <= 1 || (INTVAL (op1) & 1) == 0)
    1014              :     return NULL_RTX;
    1015              : 
    1016        20783 :   rtx_insn *last = get_last_insn ();
    1017       685752 :   for (int bit = BITS_PER_WORD; bit >= BITS_PER_WORD / 2; bit--)
    1018              :     {
    1019       664124 :       wide_int w = wi::shifted_mask (bit, 1, false, 2 * BITS_PER_WORD);
    1020       630939 :       if (wi::ne_p (wi::umod_trunc (w, INTVAL (op1)), 1))
    1021       629625 :         continue;
    1022         1314 :       rtx sum = NULL_RTX, mask = NULL_RTX;
    1023         1795 :       if (bit == BITS_PER_WORD)
    1024              :         {
    1025              :           /* For signed modulo we need to add correction to the sum
    1026              :              and that might again overflow.  */
    1027          353 :           if (!unsignedp)
    1028          143 :             continue;
    1029          210 :           if (optab_handler (uaddv4_optab, word_mode) == CODE_FOR_nothing)
    1030            0 :             continue;
    1031          210 :           tree wtype = lang_hooks.types.type_for_mode (word_mode, 1);
    1032          210 :           if (wtype == NULL_TREE)
    1033            0 :             continue;
    1034          210 :           tree ctype = build_complex_type (wtype);
    1035          210 :           if (TYPE_MODE (ctype) != GET_MODE_COMPLEX_MODE (word_mode))
    1036            0 :             continue;
    1037          210 :           machine_mode cmode = TYPE_MODE (ctype);
    1038          210 :           rtx op00 = operand_subword_force (op0, 0, mode);
    1039          210 :           rtx op01 = operand_subword_force (op0, 1, mode);
    1040          210 :           rtx cres = gen_rtx_CONCAT (cmode, gen_reg_rtx (word_mode),
    1041              :                                      gen_reg_rtx (word_mode));
    1042          210 :           tree lhs = make_tree (ctype, cres);
    1043          210 :           tree arg0 = make_tree (wtype, op00);
    1044          210 :           tree arg1 = make_tree (wtype, op01);
    1045          210 :           expand_addsub_overflow (UNKNOWN_LOCATION, PLUS_EXPR, lhs, arg0,
    1046              :                                   arg1, true, true, true, false, NULL);
    1047          210 :           sum = expand_simple_binop (word_mode, PLUS, XEXP (cres, 0),
    1048              :                                      XEXP (cres, 1), NULL_RTX, 1,
    1049              :                                      OPTAB_DIRECT);
    1050          210 :           if (sum == NULL_RTX)
    1051              :             return NULL_RTX;
    1052              :         }
    1053              :       else
    1054              :         {
    1055              :           /* Code below uses GEN_INT, so we need the masks to be representable
    1056              :              in HOST_WIDE_INTs.  */
    1057          961 :           if (bit >= HOST_BITS_PER_WIDE_INT)
    1058            0 :             continue;
    1059              :           /* If op0 is e.g. -1 or -2 unsigned, then the 2 additions might
    1060              :              overflow.  Consider 64-bit -1ULL for word size 32, if we add
    1061              :              0x7fffffffU + 0x7fffffffU + 3U, it wraps around to 1.  */
    1062         1213 :           if (bit == BITS_PER_WORD - 1)
    1063          150 :             continue;
    1064              : 
    1065          811 :           int count = (2 * BITS_PER_WORD + bit - 1) / bit;
    1066          811 :           rtx sum_corr = NULL_RTX;
    1067              : 
    1068          811 :           if (!unsignedp)
    1069              :             {
    1070              :               /* For signed modulo, compute it as unsigned modulo of
    1071              :                  sum with a correction added to it if OP0 is negative,
    1072              :                  such that the result can be computed as unsigned
    1073              :                  remainder + ((OP1 >> (2 * BITS_PER_WORD - 1)) & (1 - OP1).  */
    1074          537 :               w = wi::min_value (2 * BITS_PER_WORD, SIGNED);
    1075          427 :               wide_int wmod1 = wi::umod_trunc (w, INTVAL (op1));
    1076          427 :               wide_int wmod2 = wi::smod_trunc (w, INTVAL (op1));
    1077              :               /* wmod2 == -wmod1.  */
    1078          427 :               wmod2 = wmod2 + (INTVAL (op1) - 1);
    1079          427 :               if (wi::ne_p (wmod1, wmod2))
    1080              :                 {
    1081          427 :                   wide_int wcorr = wmod2 - wmod1;
    1082          427 :                   if (wi::neg_p (w))
    1083          427 :                     wcorr = wcorr + INTVAL (op1);
    1084              :                   /* Now verify if the count sums can't overflow, and punt
    1085              :                      if they could.  */
    1086          537 :                   w = wi::mask (bit, false, 2 * BITS_PER_WORD);
    1087          427 :                   w = w * (count - 1);
    1088          964 :                   w = w + wi::mask (2 * BITS_PER_WORD - (count - 1) * bit,
    1089          854 :                                     false, 2 * BITS_PER_WORD);
    1090          427 :                   w = w + wcorr;
    1091          537 :                   w = wi::lrshift (w, BITS_PER_WORD);
    1092          427 :                   if (wi::ne_p (w, 0))
    1093            0 :                     continue;
    1094              : 
    1095          427 :                   mask = operand_subword_force (op0, WORDS_BIG_ENDIAN ? 0 : 1,
    1096              :                                                 mode);
    1097          427 :                   mask = expand_simple_binop (word_mode, ASHIFTRT, mask,
    1098          427 :                                               GEN_INT (BITS_PER_WORD - 1),
    1099              :                                               NULL_RTX, 0, OPTAB_DIRECT);
    1100          427 :                   if (mask == NULL_RTX)
    1101              :                     return NULL_RTX;
    1102          427 :                   sum_corr = immed_wide_int_const (wcorr, word_mode);
    1103          427 :                   sum_corr = expand_simple_binop (word_mode, AND, mask,
    1104              :                                                   sum_corr, NULL_RTX, 1,
    1105              :                                                   OPTAB_DIRECT);
    1106          427 :                   if (sum_corr == NULL_RTX)
    1107              :                     return NULL_RTX;
    1108          427 :                 }
    1109          427 :             }
    1110              : 
    1111         3437 :           for (int i = 0; i < count; i++)
    1112              :             {
    1113         2626 :               rtx v = op0;
    1114         2626 :               if (i)
    1115         1815 :                 v = expand_simple_binop (mode, LSHIFTRT, v, GEN_INT (i * bit),
    1116              :                                          NULL_RTX, 1, OPTAB_DIRECT);
    1117         2626 :               if (v == NULL_RTX)
    1118              :                 return NULL_RTX;
    1119         2626 :               v = force_lowpart_subreg (word_mode, v, mode);
    1120         2626 :               if (v == NULL_RTX)
    1121              :                 return NULL_RTX;
    1122         2626 :               if (i != count - 1)
    1123         1815 :                 v = expand_simple_binop (word_mode, AND, v,
    1124         1815 :                                          GEN_INT ((HOST_WIDE_INT_1U << bit)
    1125              :                                                   - 1), NULL_RTX, 1,
    1126              :                                          OPTAB_DIRECT);
    1127         2626 :               if (v == NULL_RTX)
    1128              :                 return NULL_RTX;
    1129         2626 :               if (sum == NULL_RTX)
    1130              :                 sum = v;
    1131              :               else
    1132         1815 :                 sum = expand_simple_binop (word_mode, PLUS, sum, v, NULL_RTX,
    1133              :                                            1, OPTAB_DIRECT);
    1134         2626 :               if (sum == NULL_RTX)
    1135              :                 return NULL_RTX;
    1136              :             }
    1137          811 :           if (sum_corr)
    1138              :             {
    1139          427 :               sum = expand_simple_binop (word_mode, PLUS, sum, sum_corr,
    1140              :                                          NULL_RTX, 1, OPTAB_DIRECT);
    1141          427 :               if (sum == NULL_RTX)
    1142              :                 return NULL_RTX;
    1143              :             }
    1144              :         }
    1145         1021 :       rtx remainder = expand_divmod (1, TRUNC_MOD_EXPR, word_mode, sum,
    1146         1021 :                                      gen_int_mode (INTVAL (op1), word_mode),
    1147              :                                      NULL_RTX, 1, OPTAB_DIRECT);
    1148         1021 :       if (remainder == NULL_RTX)
    1149              :         return NULL_RTX;
    1150              : 
    1151         1021 :       if (!unsignedp)
    1152              :         {
    1153          427 :           if (mask == NULL_RTX)
    1154              :             {
    1155            0 :               mask = operand_subword_force (op0, WORDS_BIG_ENDIAN ? 0 : 1,
    1156              :                                             mode);
    1157            0 :               mask = expand_simple_binop (word_mode, ASHIFTRT, mask,
    1158            0 :                                           GEN_INT (BITS_PER_WORD - 1),
    1159              :                                           NULL_RTX, 0, OPTAB_DIRECT);
    1160            0 :               if (mask == NULL_RTX)
    1161              :                 return NULL_RTX;
    1162              :             }
    1163          427 :           mask = expand_simple_binop (word_mode, AND, mask,
    1164          427 :                                       gen_int_mode (1 - INTVAL (op1),
    1165              :                                                     word_mode),
    1166              :                                       NULL_RTX, 1, OPTAB_DIRECT);
    1167          427 :           if (mask == NULL_RTX)
    1168              :             return NULL_RTX;
    1169          427 :           remainder = expand_simple_binop (word_mode, PLUS, remainder,
    1170              :                                            mask, NULL_RTX, 1, OPTAB_DIRECT);
    1171          427 :           if (remainder == NULL_RTX)
    1172              :             return NULL_RTX;
    1173              :         }
    1174              : 
    1175         1021 :       remainder = convert_modes (mode, word_mode, remainder, unsignedp);
    1176              :       /* Punt if we need any library calls.  */
    1177         1021 :       if (last)
    1178          889 :         last = NEXT_INSN (last);
    1179              :       else
    1180          132 :         last = get_insns ();
    1181        21044 :       for (; last; last = NEXT_INSN (last))
    1182        20023 :         if (CALL_P (last))
    1183              :           return NULL_RTX;
    1184              :       return remainder;
    1185       630939 :     }
    1186              :   return NULL_RTX;
    1187              : }
    1188              : 
    1189              : /* Similarly to the above function, but compute both quotient and remainder.
    1190              :    Quotient can be computed from the remainder as:
    1191              :    rem = op0 % op1;  // Handled using expand_doubleword_mod
    1192              :    quot = (op0 - rem) * inv; // inv is multiplicative inverse of op1 modulo
    1193              :                              // 2 * BITS_PER_WORD
    1194              : 
    1195              :    We can also handle cases where op1 is a multiple of power of two constant
    1196              :    and constant handled by expand_doubleword_mod.
    1197              :    op11 = 1 << __builtin_ctz (op1);
    1198              :    op12 = op1 / op11;
    1199              :    rem1 = op0 % op12;  // Handled using expand_doubleword_mod
    1200              :    quot1 = (op0 - rem1) * inv; // inv is multiplicative inverse of op12 modulo
    1201              :                                // 2 * BITS_PER_WORD
    1202              :    rem = (quot1 % op11) * op12 + rem1;
    1203              :    quot = quot1 / op11;  */
    1204              : 
    1205              : rtx
    1206        20832 : expand_doubleword_divmod (machine_mode mode, rtx op0, rtx op1, rtx *rem,
    1207              :                           bool unsignedp)
    1208              : {
    1209        20832 :   *rem = NULL_RTX;
    1210              : 
    1211              :   /* Negative dividend should have been optimized into positive,
    1212              :      similarly modulo by 1 and modulo by power of two is optimized
    1213              :      differently too.  */
    1214        20832 :   if (INTVAL (op1) <= 1 || pow2p_hwi (INTVAL (op1)))
    1215              :     return NULL_RTX;
    1216              : 
    1217        20783 :   rtx op11 = const1_rtx;
    1218        20783 :   rtx op12 = op1;
    1219        20783 :   if ((INTVAL (op1) & 1) == 0)
    1220              :     {
    1221         3787 :       int bit = ctz_hwi (INTVAL (op1));
    1222         3787 :       op11 = GEN_INT (HOST_WIDE_INT_1 << bit);
    1223         3787 :       op12 = GEN_INT (INTVAL (op1) >> bit);
    1224              :     }
    1225              : 
    1226        20783 :   rtx rem1 = expand_doubleword_mod (mode, op0, op12, unsignedp);
    1227        20783 :   if (rem1 == NULL_RTX)
    1228              :     return NULL_RTX;
    1229              : 
    1230         1021 :   int prec = 2 * BITS_PER_WORD;
    1231         1021 :   wide_int a = wide_int::from (INTVAL (op12), prec + 1, UNSIGNED);
    1232         1021 :   wide_int b = wi::shifted_mask (prec, 1, false, prec + 1);
    1233         1021 :   wide_int m = wide_int::from (wi::mod_inv (a, b), prec, UNSIGNED);
    1234         1021 :   rtx inv = immed_wide_int_const (m, mode);
    1235              : 
    1236         1021 :   rtx_insn *last = get_last_insn ();
    1237         1021 :   rtx quot1 = expand_simple_binop (mode, MINUS, op0, rem1,
    1238              :                                    NULL_RTX, unsignedp, OPTAB_DIRECT);
    1239         1021 :   if (quot1 == NULL_RTX)
    1240              :     return NULL_RTX;
    1241              : 
    1242         1021 :   quot1 = expand_simple_binop (mode, MULT, quot1, inv,
    1243              :                                NULL_RTX, unsignedp, OPTAB_DIRECT);
    1244         1021 :   if (quot1 == NULL_RTX)
    1245              :     return NULL_RTX;
    1246              : 
    1247         1021 :   if (op11 != const1_rtx)
    1248              :     {
    1249          380 :       rtx rem2 = expand_divmod (1, TRUNC_MOD_EXPR, mode, quot1, op11,
    1250              :                                 NULL_RTX, unsignedp, OPTAB_DIRECT);
    1251          380 :       if (rem2 == NULL_RTX)
    1252              :         return NULL_RTX;
    1253              : 
    1254          380 :       rem2 = expand_simple_binop (mode, MULT, rem2, op12, NULL_RTX,
    1255              :                                   unsignedp, OPTAB_DIRECT);
    1256          380 :       if (rem2 == NULL_RTX)
    1257              :         return NULL_RTX;
    1258              : 
    1259          380 :       rem2 = expand_simple_binop (mode, PLUS, rem2, rem1, NULL_RTX,
    1260              :                                   unsignedp, OPTAB_DIRECT);
    1261          380 :       if (rem2 == NULL_RTX)
    1262              :         return NULL_RTX;
    1263              : 
    1264          380 :       rtx quot2 = expand_divmod (0, TRUNC_DIV_EXPR, mode, quot1, op11,
    1265              :                                  NULL_RTX, unsignedp, OPTAB_DIRECT);
    1266          380 :       if (quot2 == NULL_RTX)
    1267              :         return NULL_RTX;
    1268              : 
    1269              :       rem1 = rem2;
    1270              :       quot1 = quot2;
    1271              :     }
    1272              : 
    1273              :   /* Punt if we need any library calls.  */
    1274         1021 :   if (last)
    1275         1021 :     last = NEXT_INSN (last);
    1276              :   else
    1277            0 :     last = get_insns ();
    1278        16234 :   for (; last; last = NEXT_INSN (last))
    1279        15213 :     if (CALL_P (last))
    1280              :       return NULL_RTX;
    1281              : 
    1282         1021 :   *rem = rem1;
    1283         1021 :   return quot1;
    1284         1021 : }
    1285              : 
    1286              : /* Wrapper around expand_binop which takes an rtx code to specify
    1287              :    the operation to perform, not an optab pointer.  All other
    1288              :    arguments are the same.  */
    1289              : rtx
    1290      4169545 : expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
    1291              :                      rtx op1, rtx target, int unsignedp,
    1292              :                      enum optab_methods methods)
    1293              : {
    1294      4169545 :   optab binop = code_to_optab (code);
    1295      4169545 :   gcc_assert (binop);
    1296              : 
    1297      4169545 :   return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
    1298              : }
    1299              : 
    1300              : /* Return whether OP0 and OP1 should be swapped when expanding a commutative
    1301              :    binop.  Order them according to commutative_operand_precedence and, if
    1302              :    possible, try to put TARGET or a pseudo first.  */
    1303              : static bool
    1304     12628798 : swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
    1305              : {
    1306     12628798 :   int op0_prec = commutative_operand_precedence (op0);
    1307     12628798 :   int op1_prec = commutative_operand_precedence (op1);
    1308              : 
    1309     12628798 :   if (op0_prec < op1_prec)
    1310              :     return true;
    1311              : 
    1312     12428520 :   if (op0_prec > op1_prec)
    1313              :     return false;
    1314              : 
    1315              :   /* With equal precedence, both orders are ok, but it is better if the
    1316              :      first operand is TARGET, or if both TARGET and OP0 are pseudos.  */
    1317      2360588 :   if (target == 0 || REG_P (target))
    1318      2268157 :     return (REG_P (op1) && !REG_P (op0)) || target == op1;
    1319              :   else
    1320        92431 :     return rtx_equal_p (op1, target);
    1321              : }
    1322              : 
    1323              : /* Return true if BINOPTAB implements a shift operation.  */
    1324              : 
    1325              : static bool
    1326     24989188 : shift_optab_p (optab binoptab)
    1327              : {
    1328     10035239 :   switch (optab_to_code (binoptab))
    1329              :     {
    1330              :     case ASHIFT:
    1331              :     case SS_ASHIFT:
    1332              :     case US_ASHIFT:
    1333              :     case ASHIFTRT:
    1334              :     case LSHIFTRT:
    1335              :     case ROTATE:
    1336              :     case ROTATERT:
    1337              :       return true;
    1338              : 
    1339     13311025 :     default:
    1340            0 :       return false;
    1341              :     }
    1342              : }
    1343              : 
    1344              : /* Return true if BINOPTAB implements a commutative binary operation.  */
    1345              : 
    1346              : static bool
    1347     15141001 : commutative_optab_p (optab binoptab)
    1348              : {
    1349     15141001 :   return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
    1350              :           || binoptab == smul_widen_optab
    1351      2606714 :           || binoptab == umul_widen_optab
    1352              :           || binoptab == smul_highpart_optab
    1353      2567318 :           || binoptab == umul_highpart_optab
    1354      2512203 :           || binoptab == vec_widen_sadd_optab
    1355      2512203 :           || binoptab == vec_widen_uadd_optab
    1356              :           || binoptab == vec_widen_sadd_hi_optab
    1357      2512203 :           || binoptab == vec_widen_sadd_lo_optab
    1358              :           || binoptab == vec_widen_uadd_hi_optab
    1359      2512203 :           || binoptab == vec_widen_uadd_lo_optab
    1360              :           || binoptab == vec_widen_sadd_even_optab
    1361      2512203 :           || binoptab == vec_widen_sadd_odd_optab
    1362              :           || binoptab == vec_widen_uadd_even_optab
    1363     17653204 :           || binoptab == vec_widen_uadd_odd_optab);
    1364              : }
    1365              : 
    1366              : /* X is to be used in mode MODE as operand OPN to BINOPTAB.  If we're
    1367              :    optimizing, and if the operand is a constant that costs more than
    1368              :    1 instruction, force the constant into a register and return that
    1369              :    register.  Return X otherwise.  UNSIGNEDP says whether X is unsigned.  */
    1370              : 
    1371              : static rtx
    1372     28479073 : avoid_expensive_constant (machine_mode mode, optab binoptab,
    1373              :                           int opn, rtx x, bool unsignedp)
    1374              : {
    1375     28479073 :   bool speed = optimize_insn_for_speed_p ();
    1376              : 
    1377     28479073 :   if (mode != VOIDmode
    1378     28479073 :       && optimize
    1379     21578224 :       && CONSTANT_P (x)
    1380     35871449 :       && (rtx_cost (x, mode, optab_to_code (binoptab), opn, speed)
    1381      7392376 :           > set_src_cost (x, mode, speed)))
    1382              :     {
    1383            0 :       if (CONST_INT_P (x))
    1384              :         {
    1385            0 :           HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
    1386            0 :           if (intval != INTVAL (x))
    1387            0 :             x = GEN_INT (intval);
    1388              :         }
    1389              :       else
    1390            0 :         x = convert_modes (mode, VOIDmode, x, unsignedp);
    1391            0 :       x = force_reg (mode, x);
    1392              :     }
    1393     28479073 :   return x;
    1394              : }
    1395              : 
    1396              : /* Helper function for expand_binop: handle the case where there
    1397              :    is an insn ICODE that directly implements the indicated operation.
    1398              :    Returns null if this is not possible.  */
    1399              : static rtx
    1400     14923199 : expand_binop_directly (enum insn_code icode, machine_mode mode, optab binoptab,
    1401              :                        rtx op0, rtx op1,
    1402              :                        rtx target, int unsignedp, enum optab_methods methods)
    1403              : {
    1404     14923199 :   machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
    1405     14923199 :   machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
    1406     14923199 :   machine_mode mode0, mode1, tmp_mode;
    1407     14923199 :   class expand_operand ops[3];
    1408     14923199 :   bool commutative_p;
    1409     14923199 :   rtx_insn *pat;
    1410     14923199 :   rtx xop0 = op0, xop1 = op1;
    1411     14923199 :   bool canonicalize_op1 = false;
    1412     14923199 :   rtx_insn *last = get_last_insn ();
    1413              : 
    1414              :   /* If it is a commutative operator and the modes would match
    1415              :      if we would swap the operands, we can save the conversions.  */
    1416     14923199 :   commutative_p = commutative_optab_p (binoptab);
    1417     14923199 :   if (commutative_p
    1418     12565439 :       && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
    1419        13817 :       && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode0)
    1420              :     std::swap (xop0, xop1);
    1421              : 
    1422              :   /* If we are optimizing, force expensive constants into a register.  */
    1423     14923199 :   xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
    1424     14923199 :   if (!shift_optab_p (binoptab))
    1425     13311025 :     xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
    1426              :   else
    1427              :     /* Shifts and rotates often use a different mode for op1 from op0;
    1428              :        for VOIDmode constants we don't know the mode, so force it
    1429              :        to be canonicalized using convert_modes.  */
    1430              :     canonicalize_op1 = true;
    1431              : 
    1432              :   /* In case the insn wants input operands in modes different from
    1433              :      those of the actual operands, convert the operands.  It would
    1434              :      seem that we don't need to convert CONST_INTs, but we do, so
    1435              :      that they're properly zero-extended, sign-extended or truncated
    1436              :      for their mode.  */
    1437              : 
    1438     14923199 :   mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
    1439     14923199 :   if (xmode0 != VOIDmode && xmode0 != mode0)
    1440              :     {
    1441         3374 :       xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
    1442         3374 :       mode0 = xmode0;
    1443              :     }
    1444              : 
    1445     14923199 :   mode1 = ((GET_MODE (xop1) != VOIDmode || canonicalize_op1)
    1446     14923199 :            ? GET_MODE (xop1) : mode);
    1447     14923199 :   if (xmode1 != VOIDmode && xmode1 != mode1)
    1448              :     {
    1449      1613507 :       xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
    1450      1613507 :       mode1 = xmode1;
    1451              :     }
    1452              : 
    1453              :   /* If operation is commutative,
    1454              :      try to make the first operand a register.
    1455              :      Even better, try to make it the same as the target.
    1456              :      Also try to make the last operand a constant.  */
    1457     14923199 :   if (commutative_p
    1458     14923199 :       && swap_commutative_operands_with_target (target, xop0, xop1))
    1459              :     std::swap (xop0, xop1);
    1460              : 
    1461              :   /* Now, if insn's predicates don't allow our operands, put them into
    1462              :      pseudo regs.  */
    1463              : 
    1464     14923199 :   if (binoptab == vec_pack_trunc_optab
    1465     14923199 :       || binoptab == vec_pack_usat_optab
    1466     14912468 :       || binoptab == vec_pack_ssat_optab
    1467     14912468 :       || binoptab == vec_pack_ufix_trunc_optab
    1468     14912310 :       || binoptab == vec_pack_sfix_trunc_optab
    1469     14912310 :       || binoptab == vec_packu_float_optab
    1470     14912090 :       || binoptab == vec_packs_float_optab)
    1471              :     {
    1472              :       /* The mode of the result is different then the mode of the
    1473              :          arguments.  */
    1474        11129 :       tmp_mode = insn_data[(int) icode].operand[0].mode;
    1475         2696 :       if (VECTOR_MODE_P (mode)
    1476        31793 :           && maybe_ne (GET_MODE_NUNITS (tmp_mode), 2 * GET_MODE_NUNITS (mode)))
    1477              :         return NULL_RTX;
    1478              :     }
    1479              :   else
    1480              :     tmp_mode = mode;
    1481              : 
    1482     14923199 :   create_output_operand (&ops[0], target, tmp_mode);
    1483     14923199 :   create_input_operand (&ops[1], xop0, mode0);
    1484     14923199 :   create_input_operand (&ops[2], xop1, mode1);
    1485     14923199 :   pat = maybe_gen_insn (icode, 3, ops);
    1486     14923199 :   if (pat)
    1487              :     {
    1488              :       /* If PAT is composed of more than one insn, try to add an appropriate
    1489              :          REG_EQUAL note to it.  If we can't because TEMP conflicts with an
    1490              :          operand, call expand_binop again, this time without a target.  */
    1491     13726099 :       if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
    1492     16145902 :           && ! add_equal_note (pat, ops[0].value,
    1493              :                                optab_to_code (binoptab),
    1494              :                                ops[1].value, ops[2].value, mode0))
    1495              :         {
    1496        55232 :           delete_insns_since (last);
    1497        55232 :           return expand_binop_directly (icode, mode, binoptab, op0, op1,
    1498        55232 :                                         NULL_RTX, unsignedp, methods);
    1499              :         }
    1500              : 
    1501     14867961 :       emit_insn (pat);
    1502     14867961 :       return ops[0].value;
    1503              :     }
    1504              : 
    1505              :   return NULL_RTX;
    1506              : }
    1507              : 
    1508              : /* Generate code to perform an operation specified by BINOPTAB
    1509              :    on operands OP0 and OP1, with result having machine-mode MODE.
    1510              : 
    1511              :    UNSIGNEDP is for the case where we have to widen the operands
    1512              :    to perform the operation.  It says to use zero-extension.
    1513              : 
    1514              :    If TARGET is nonzero, the value
    1515              :    is generated there, if it is convenient to do so.
    1516              :    In all cases an rtx is returned for the locus of the value;
    1517              :    this may or may not be TARGET.  */
    1518              : 
    1519              : rtx
    1520     15085929 : expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
    1521              :               rtx target, int unsignedp, enum optab_methods methods)
    1522              : {
    1523     13030042 :   enum optab_methods next_methods
    1524     15196758 :     = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
    1525     15196758 :        ? OPTAB_WIDEN : methods);
    1526     15196758 :   enum mode_class mclass;
    1527     15196758 :   enum insn_code icode;
    1528     15196758 :   machine_mode wider_mode;
    1529     15196758 :   scalar_int_mode int_mode;
    1530     15196758 :   rtx libfunc;
    1531     15196758 :   rtx temp;
    1532     15196758 :   rtx_insn *entry_last = get_last_insn ();
    1533     15196758 :   rtx_insn *last;
    1534              : 
    1535     15196758 :   mclass = GET_MODE_CLASS (mode);
    1536              : 
    1537              :   /* If subtracting an integer constant, convert this into an addition of
    1538              :      the negated constant.  */
    1539              : 
    1540     15196758 :   if (binoptab == sub_optab && CONST_INT_P (op1))
    1541              :     {
    1542      1040497 :       op1 = negate_rtx (mode, op1);
    1543      1040497 :       binoptab = add_optab;
    1544              :     }
    1545              :   /* For shifts, constant invalid op1 might be expanded from different
    1546              :      mode than MODE.  As those are invalid, force them to a register
    1547              :      to avoid further problems during expansion.  */
    1548     14156261 :   else if (CONST_INT_P (op1)
    1549     16745632 :            && shift_optab_p (binoptab)
    1550     18802883 :            && UINTVAL (op1) >= GET_MODE_BITSIZE (GET_MODE_INNER (mode)))
    1551              :     {
    1552          160 :       op1 = gen_int_mode (INTVAL (op1), GET_MODE_INNER (mode));
    1553          160 :       op1 = force_reg (GET_MODE_INNER (mode), op1);
    1554              :     }
    1555              : 
    1556              :   /* Record where to delete back to if we backtrack.  */
    1557     15196758 :   last = get_last_insn ();
    1558              : 
    1559              :   /* If we can do it with a three-operand insn, do so.  */
    1560              : 
    1561     15196758 :   if (methods != OPTAB_MUST_WIDEN)
    1562              :     {
    1563     15196758 :       if (convert_optab_p (binoptab))
    1564              :         {
    1565        39396 :           machine_mode from_mode = widened_mode (mode, op0, op1);
    1566        39396 :           icode = find_widening_optab_handler (binoptab, mode, from_mode);
    1567              :         }
    1568              :       else
    1569     15157362 :         icode = optab_handler (binoptab, mode);
    1570     15196758 :       if (icode != CODE_FOR_nothing)
    1571              :         {
    1572     14867966 :           temp = expand_binop_directly (icode, mode, binoptab, op0, op1,
    1573              :                                         target, unsignedp, methods);
    1574     14867966 :           if (temp)
    1575              :             return temp;
    1576            5 :           delete_insns_since (last);
    1577              :         }
    1578              :     }
    1579              : 
    1580              :   /* If we were trying to rotate, and that didn't work, try rotating
    1581              :      the other direction before falling back to shifts and bitwise-or.  */
    1582       328797 :   if (((binoptab == rotl_optab
    1583           25 :         && (icode = optab_handler (rotr_optab, mode)) != CODE_FOR_nothing)
    1584       328794 :        || (binoptab == rotr_optab
    1585           13 :            && (icode = optab_handler (rotl_optab, mode)) != CODE_FOR_nothing))
    1586       328802 :       && is_int_mode (mode, &int_mode))
    1587              :     {
    1588            1 :       optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
    1589            1 :       rtx newop1;
    1590            1 :       unsigned int bits = GET_MODE_PRECISION (int_mode);
    1591              : 
    1592            1 :       if (CONST_INT_P (op1))
    1593            0 :         newop1 = gen_int_shift_amount (int_mode, bits - INTVAL (op1));
    1594            1 :       else if (targetm.shift_truncation_mask (int_mode) == bits - 1)
    1595            0 :         newop1 = negate_rtx (GET_MODE (op1), op1);
    1596              :       else
    1597            1 :         newop1 = expand_binop (GET_MODE (op1), sub_optab,
    1598            1 :                                gen_int_mode (bits, GET_MODE (op1)), op1,
    1599              :                                NULL_RTX, unsignedp, OPTAB_DIRECT);
    1600              : 
    1601            1 :       temp = expand_binop_directly (icode, int_mode, otheroptab, op0, newop1,
    1602              :                                     target, unsignedp, methods);
    1603            1 :       if (temp)
    1604     15085929 :         return temp;
    1605            1 :       delete_insns_since (last);
    1606              :     }
    1607              : 
    1608              :   /* If this is a multiply, see if we can do a widening operation that
    1609              :      takes operands of this mode and makes a wider mode.  */
    1610              : 
    1611       328797 :   if (binoptab == smul_optab
    1612        26960 :       && GET_MODE_2XWIDER_MODE (mode).exists (&wider_mode)
    1613       348967 :       && (convert_optab_handler ((unsignedp
    1614              :                                   ? umul_widen_optab
    1615              :                                   : smul_widen_optab),
    1616              :                                  wider_mode, mode) != CODE_FOR_nothing))
    1617              :     {
    1618              :       /* *_widen_optab needs to determine operand mode, make sure at least
    1619              :          one operand has non-VOID mode.  */
    1620            0 :       if (GET_MODE (op0) == VOIDmode && GET_MODE (op1) == VOIDmode)
    1621            0 :         op0 = force_reg (mode, op0);
    1622            0 :       temp = expand_binop (wider_mode,
    1623              :                            unsignedp ? umul_widen_optab : smul_widen_optab,
    1624              :                            op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
    1625              : 
    1626            0 :       if (temp != 0)
    1627              :         {
    1628            0 :           if (GET_MODE_CLASS (mode) == MODE_INT
    1629            0 :               && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
    1630            0 :             return gen_lowpart (mode, temp);
    1631              :           else
    1632            0 :             return convert_to_mode (mode, temp, unsignedp);
    1633              :         }
    1634              :     }
    1635              : 
    1636              :   /* If backend's machine description doesn't specify an any_or_plus
    1637              :      (AOP) preference, choose for it.  */
    1638       328797 :   if (binoptab == aop_optab)
    1639              :     {
    1640       110829 :       binoptab = (mode == word_mode || mode == SImode) ? add_optab
    1641              :                                                        : ior_optab;
    1642       110829 :       return expand_binop (mode, binoptab, op0, op1,
    1643       110829 :                            target, unsignedp, methods);
    1644              :     }
    1645              : 
    1646              :   /* If this is a vector shift by a scalar, see if we can do a vector
    1647              :      shift by a vector.  If so, broadcast the scalar into a vector.  */
    1648       217968 :   if (mclass == MODE_VECTOR_INT)
    1649              :     {
    1650           37 :       optab otheroptab = unknown_optab;
    1651              : 
    1652           37 :       if (binoptab == ashl_optab)
    1653              :         otheroptab = vashl_optab;
    1654              :       else if (binoptab == ashr_optab)
    1655              :         otheroptab = vashr_optab;
    1656              :       else if (binoptab == lshr_optab)
    1657              :         otheroptab = vlshr_optab;
    1658              :       else if (binoptab == rotl_optab)
    1659              :         otheroptab = vrotl_optab;
    1660              :       else if (binoptab == rotr_optab)
    1661              :         otheroptab = vrotr_optab;
    1662              : 
    1663              :       if (otheroptab
    1664           37 :           && (icode = optab_handler (otheroptab, mode)) != CODE_FOR_nothing)
    1665              :         {
    1666              :           /* The scalar may be wider or narrower than the vector element.
    1667              :              Truncate or extend it to the proper size to fit in the
    1668              :              broadcast vector.  */
    1669            0 :           scalar_mode inner_mode = GET_MODE_INNER (mode);
    1670            0 :           if (!CONST_INT_P (op1))
    1671              :             {
    1672            0 :               auto mode1 = as_a <scalar_int_mode> (GET_MODE (op1));
    1673            0 :               int size1 = GET_MODE_BITSIZE (mode1);
    1674            0 :               int inner_size = GET_MODE_BITSIZE (inner_mode);
    1675              : 
    1676            0 :               if (size1 != inner_size)
    1677              :                 {
    1678            0 :                   auto unary = size1 > inner_size ? TRUNCATE : ZERO_EXTEND;
    1679            0 :                   op1 = force_reg (inner_mode,
    1680              :                                    simplify_gen_unary (unary, inner_mode,
    1681              :                                                        op1, mode1));
    1682              :                 }
    1683              :             }
    1684              : 
    1685            0 :           rtx vop1 = expand_vector_broadcast (mode, op1);
    1686            0 :           if (vop1)
    1687              :             {
    1688            0 :               temp = expand_binop_directly (icode, mode, otheroptab, op0, vop1,
    1689              :                                             target, unsignedp, methods);
    1690            0 :               if (temp)
    1691     15085929 :                 return temp;
    1692            0 :               delete_insns_since (last);
    1693              :             }
    1694              :         }
    1695              :     }
    1696              : 
    1697              :   /* Look for a wider mode of the same class for which we think we
    1698              :      can open-code the operation.  Check for a widening multiply at the
    1699              :      wider mode as well.  */
    1700              : 
    1701       217968 :   if (CLASS_HAS_WIDER_MODES_P (mclass)
    1702       217931 :       && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
    1703       119962 :     FOR_EACH_WIDER_MODE (wider_mode, mode)
    1704              :       {
    1705        65066 :         machine_mode next_mode;
    1706        65066 :         if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
    1707        65066 :             || (binoptab == smul_optab
    1708        82705 :                 && GET_MODE_WIDER_MODE (wider_mode).exists (&next_mode)
    1709        21393 :                 && (find_widening_optab_handler ((unsignedp
    1710              :                                                   ? umul_widen_optab
    1711              :                                                   : smul_widen_optab),
    1712              :                                                  next_mode, mode)
    1713              :                     != CODE_FOR_nothing)))
    1714              :           {
    1715          166 :             rtx xop0 = op0, xop1 = op1;
    1716          166 :             bool no_extend = false;
    1717              : 
    1718              :             /* For certain integer operations, we need not actually extend
    1719              :                the narrow operands, as long as we will truncate
    1720              :                the results to the same narrowness.  */
    1721              : 
    1722          166 :             if ((binoptab == ior_optab || binoptab == and_optab
    1723          166 :                  || binoptab == xor_optab
    1724          166 :                  || binoptab == add_optab || binoptab == sub_optab
    1725              :                  || binoptab == smul_optab || binoptab == ashl_optab)
    1726           91 :                 && mclass == MODE_INT)
    1727              :               {
    1728            7 :                 no_extend = true;
    1729            7 :                 xop0 = avoid_expensive_constant (mode, binoptab, 0,
    1730              :                                                  xop0, unsignedp);
    1731            7 :                 if (binoptab != ashl_optab)
    1732            0 :                   xop1 = avoid_expensive_constant (mode, binoptab, 1,
    1733              :                                                    xop1, unsignedp);
    1734              :               }
    1735              : 
    1736          166 :             xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
    1737              : 
    1738              :             /* The second operand of a shift must always be extended.  */
    1739          332 :             xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
    1740          166 :                                   no_extend && binoptab != ashl_optab);
    1741              : 
    1742          166 :             temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
    1743              :                                  unsignedp, OPTAB_DIRECT);
    1744          166 :             if (temp)
    1745              :               {
    1746          166 :                 if (mclass != MODE_INT
    1747          166 :                     || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
    1748              :                   {
    1749          159 :                     if (target == 0)
    1750           51 :                       target = gen_reg_rtx (mode);
    1751          159 :                     convert_move (target, temp, 0);
    1752          159 :                     return target;
    1753              :                   }
    1754              :                 else
    1755            7 :                   return gen_lowpart (mode, temp);
    1756              :               }
    1757              :             else
    1758            0 :               delete_insns_since (last);
    1759              :           }
    1760              :       }
    1761              : 
    1762              :   /* If operation is commutative,
    1763              :      try to make the first operand a register.
    1764              :      Even better, try to make it the same as the target.
    1765              :      Also try to make the last operand a constant.  */
    1766       217802 :   if (commutative_optab_p (binoptab)
    1767       217802 :       && swap_commutative_operands_with_target (target, op0, op1))
    1768              :     std::swap (op0, op1);
    1769              : 
    1770              :   /* These can be done a word at a time.  */
    1771       217802 :   if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
    1772            8 :       && is_int_mode (mode, &int_mode)
    1773           16 :       && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD
    1774       217810 :       && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
    1775              :     {
    1776            8 :       int i;
    1777            8 :       rtx_insn *insns;
    1778              : 
    1779              :       /* If TARGET is the same as one of the operands, the REG_EQUAL note
    1780              :          won't be accurate, so use a new target.  */
    1781            8 :       if (target == 0
    1782            8 :           || target == op0
    1783            8 :           || target == op1
    1784            8 :           || reg_overlap_mentioned_p (target, op0)
    1785            8 :           || reg_overlap_mentioned_p (target, op1)
    1786           16 :           || !valid_multiword_target_p (target))
    1787            0 :         target = gen_reg_rtx (int_mode);
    1788              : 
    1789            8 :       start_sequence ();
    1790              : 
    1791              :       /* Do the actual arithmetic.  */
    1792            8 :       machine_mode op0_mode = GET_MODE (op0);
    1793            8 :       machine_mode op1_mode = GET_MODE (op1);
    1794            8 :       if (op0_mode == VOIDmode)
    1795            0 :         op0_mode = int_mode;
    1796            8 :       if (op1_mode == VOIDmode)
    1797            8 :         op1_mode = int_mode;
    1798          120 :       for (i = 0; i < GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD; i++)
    1799              :         {
    1800           32 :           rtx target_piece = operand_subword (target, i, 1, int_mode);
    1801           32 :           rtx x = expand_binop (word_mode, binoptab,
    1802           32 :                                 operand_subword_force (op0, i, op0_mode),
    1803           32 :                                 operand_subword_force (op1, i, op1_mode),
    1804              :                                 target_piece, unsignedp, next_methods);
    1805              : 
    1806           32 :           if (x == 0)
    1807              :             break;
    1808              : 
    1809           32 :           if (target_piece != x)
    1810            0 :             emit_move_insn (target_piece, x);
    1811              :         }
    1812              : 
    1813            8 :       insns = end_sequence ();
    1814              : 
    1815           24 :       if (i == GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD)
    1816              :         {
    1817            8 :           emit_insn (insns);
    1818            8 :           return target;
    1819              :         }
    1820              :     }
    1821              : 
    1822              :   /* Synthesize double word shifts from single word shifts.  */
    1823       217794 :   if ((binoptab == lshr_optab || binoptab == ashl_optab
    1824       217794 :        || binoptab == ashr_optab)
    1825            0 :       && is_int_mode (mode, &int_mode)
    1826            0 :       && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
    1827            0 :       && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
    1828            0 :       && GET_MODE_PRECISION (int_mode) == GET_MODE_BITSIZE (int_mode)
    1829            0 :       && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
    1830            0 :       && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
    1831       217794 :       && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
    1832              :     {
    1833            0 :       unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
    1834            0 :       scalar_int_mode op1_mode;
    1835              : 
    1836            0 :       double_shift_mask = targetm.shift_truncation_mask (int_mode);
    1837            0 :       shift_mask = targetm.shift_truncation_mask (word_mode);
    1838            0 :       op1_mode = (GET_MODE (op1) != VOIDmode
    1839            0 :                   ? as_a <scalar_int_mode> (GET_MODE (op1))
    1840              :                   : word_mode);
    1841              : 
    1842              :       /* Apply the truncation to constant shifts.  */
    1843            0 :       if (double_shift_mask > 0 && CONST_INT_P (op1))
    1844            0 :         op1 = gen_int_mode (INTVAL (op1) & double_shift_mask, op1_mode);
    1845              : 
    1846            0 :       if (op1 == CONST0_RTX (op1_mode))
    1847     15085929 :         return op0;
    1848              : 
    1849              :       /* Make sure that this is a combination that expand_doubleword_shift
    1850              :          can handle.  See the comments there for details.  */
    1851            0 :       if (double_shift_mask == 0
    1852            0 :           || (shift_mask == BITS_PER_WORD - 1
    1853            0 :               && double_shift_mask == BITS_PER_WORD * 2 - 1))
    1854              :         {
    1855            0 :           rtx_insn *insns;
    1856            0 :           rtx into_target, outof_target;
    1857            0 :           rtx into_input, outof_input;
    1858            0 :           int left_shift, outof_word;
    1859              : 
    1860              :           /* If TARGET is the same as one of the operands, the REG_EQUAL note
    1861              :              won't be accurate, so use a new target.  */
    1862            0 :           if (target == 0
    1863            0 :               || target == op0
    1864            0 :               || target == op1
    1865            0 :               || reg_overlap_mentioned_p (target, op0)
    1866            0 :               || reg_overlap_mentioned_p (target, op1)
    1867            0 :               || !valid_multiword_target_p (target))
    1868            0 :             target = gen_reg_rtx (int_mode);
    1869              : 
    1870            0 :           start_sequence ();
    1871              : 
    1872              :           /* OUTOF_* is the word we are shifting bits away from, and
    1873              :              INTO_* is the word that we are shifting bits towards, thus
    1874              :              they differ depending on the direction of the shift and
    1875              :              WORDS_BIG_ENDIAN.  */
    1876              : 
    1877            0 :           left_shift = binoptab == ashl_optab;
    1878            0 :           outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
    1879              : 
    1880            0 :           outof_target = operand_subword (target, outof_word, 1, int_mode);
    1881            0 :           into_target = operand_subword (target, 1 - outof_word, 1, int_mode);
    1882              : 
    1883            0 :           outof_input = operand_subword_force (op0, outof_word, int_mode);
    1884            0 :           into_input = operand_subword_force (op0, 1 - outof_word, int_mode);
    1885              : 
    1886            0 :           if (expand_doubleword_shift (op1_mode, binoptab,
    1887              :                                        outof_input, into_input, op1,
    1888              :                                        outof_target, into_target,
    1889              :                                        unsignedp, next_methods, shift_mask))
    1890              :             {
    1891            0 :               insns = end_sequence ();
    1892              : 
    1893            0 :               emit_insn (insns);
    1894            0 :               return target;
    1895              :             }
    1896            0 :           end_sequence ();
    1897              :         }
    1898              :     }
    1899              : 
    1900              :   /* Synthesize double word rotates from single word shifts.  */
    1901       217794 :   if ((binoptab == rotl_optab || binoptab == rotr_optab)
    1902           38 :       && is_int_mode (mode, &int_mode)
    1903            1 :       && CONST_INT_P (op1)
    1904            0 :       && GET_MODE_PRECISION (int_mode) == 2 * BITS_PER_WORD
    1905            0 :       && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
    1906       217794 :       && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
    1907              :     {
    1908            0 :       rtx_insn *insns;
    1909            0 :       rtx into_target, outof_target;
    1910            0 :       rtx into_input, outof_input;
    1911            0 :       rtx inter;
    1912            0 :       int shift_count, left_shift, outof_word;
    1913              : 
    1914              :       /* If TARGET is the same as one of the operands, the REG_EQUAL note
    1915              :          won't be accurate, so use a new target. Do this also if target is not
    1916              :          a REG, first because having a register instead may open optimization
    1917              :          opportunities, and second because if target and op0 happen to be MEMs
    1918              :          designating the same location, we would risk clobbering it too early
    1919              :          in the code sequence we generate below.  */
    1920            0 :       if (target == 0
    1921            0 :           || target == op0
    1922            0 :           || target == op1
    1923            0 :           || !REG_P (target)
    1924            0 :           || reg_overlap_mentioned_p (target, op0)
    1925            0 :           || reg_overlap_mentioned_p (target, op1)
    1926            0 :           || !valid_multiword_target_p (target))
    1927            0 :         target = gen_reg_rtx (int_mode);
    1928              : 
    1929            0 :       start_sequence ();
    1930              : 
    1931            0 :       shift_count = INTVAL (op1);
    1932              : 
    1933              :       /* OUTOF_* is the word we are shifting bits away from, and
    1934              :          INTO_* is the word that we are shifting bits towards, thus
    1935              :          they differ depending on the direction of the shift and
    1936              :          WORDS_BIG_ENDIAN.  */
    1937              : 
    1938            0 :       left_shift = (binoptab == rotl_optab);
    1939            0 :       outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
    1940              : 
    1941            0 :       outof_target = operand_subword (target, outof_word, 1, int_mode);
    1942            0 :       into_target = operand_subword (target, 1 - outof_word, 1, int_mode);
    1943              : 
    1944            0 :       outof_input = operand_subword_force (op0, outof_word, int_mode);
    1945            0 :       into_input = operand_subword_force (op0, 1 - outof_word, int_mode);
    1946              : 
    1947            0 :       if (shift_count == BITS_PER_WORD)
    1948              :         {
    1949              :           /* This is just a word swap.  */
    1950            0 :           emit_move_insn (outof_target, into_input);
    1951            0 :           emit_move_insn (into_target, outof_input);
    1952            0 :           inter = const0_rtx;
    1953              :         }
    1954              :       else
    1955              :         {
    1956            0 :           rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
    1957            0 :           HOST_WIDE_INT first_shift_count, second_shift_count;
    1958            0 :           optab reverse_unsigned_shift, unsigned_shift;
    1959              : 
    1960            0 :           reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
    1961            0 :                                     ? lshr_optab : ashl_optab);
    1962              : 
    1963            0 :           unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
    1964            0 :                             ? ashl_optab : lshr_optab);
    1965              : 
    1966            0 :           if (shift_count > BITS_PER_WORD)
    1967              :             {
    1968            0 :               first_shift_count = shift_count - BITS_PER_WORD;
    1969            0 :               second_shift_count = 2 * BITS_PER_WORD - shift_count;
    1970              :             }
    1971              :           else
    1972              :             {
    1973            0 :               first_shift_count = BITS_PER_WORD - shift_count;
    1974            0 :               second_shift_count = shift_count;
    1975              :             }
    1976            0 :           rtx first_shift_count_rtx
    1977            0 :             = gen_int_shift_amount (word_mode, first_shift_count);
    1978            0 :           rtx second_shift_count_rtx
    1979            0 :             = gen_int_shift_amount (word_mode, second_shift_count);
    1980              : 
    1981            0 :           into_temp1 = expand_binop (word_mode, unsigned_shift,
    1982              :                                      outof_input, first_shift_count_rtx,
    1983              :                                      NULL_RTX, unsignedp, next_methods);
    1984            0 :           into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
    1985              :                                      into_input, second_shift_count_rtx,
    1986              :                                      NULL_RTX, unsignedp, next_methods);
    1987              : 
    1988            0 :           if (into_temp1 != 0 && into_temp2 != 0)
    1989            0 :             inter = expand_binop (word_mode, aop_optab, into_temp1, into_temp2,
    1990              :                                   into_target, unsignedp, next_methods);
    1991              :           else
    1992              :             inter = 0;
    1993              : 
    1994            0 :           if (inter != 0 && inter != into_target)
    1995            0 :             emit_move_insn (into_target, inter);
    1996              : 
    1997            0 :           outof_temp1 = expand_binop (word_mode, unsigned_shift,
    1998              :                                       into_input, first_shift_count_rtx,
    1999              :                                       NULL_RTX, unsignedp, next_methods);
    2000            0 :           outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
    2001              :                                       outof_input, second_shift_count_rtx,
    2002              :                                       NULL_RTX, unsignedp, next_methods);
    2003              : 
    2004            0 :           if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
    2005            0 :             inter = expand_binop (word_mode, aop_optab,
    2006              :                                   outof_temp1, outof_temp2,
    2007              :                                   outof_target, unsignedp, next_methods);
    2008              : 
    2009            0 :           if (inter != 0 && inter != outof_target)
    2010            0 :             emit_move_insn (outof_target, inter);
    2011              :         }
    2012              : 
    2013            0 :       insns = end_sequence ();
    2014              : 
    2015            0 :       if (inter != 0)
    2016              :         {
    2017            0 :           emit_insn (insns);
    2018            0 :           return target;
    2019              :         }
    2020              :     }
    2021              : 
    2022              :   /* These can be done a word at a time by propagating carries.  */
    2023       217794 :   if ((binoptab == add_optab || binoptab == sub_optab)
    2024        13733 :       && is_int_mode (mode, &int_mode)
    2025            0 :       && GET_MODE_SIZE (int_mode) >= 2 * UNITS_PER_WORD
    2026       217794 :       && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
    2027              :     {
    2028            0 :       unsigned int i;
    2029            0 :       optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
    2030            0 :       const unsigned int nwords = GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD;
    2031            0 :       rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
    2032            0 :       rtx xop0, xop1, xtarget;
    2033              : 
    2034              :       /* We can handle either a 1 or -1 value for the carry.  If STORE_FLAG
    2035              :          value is one of those, use it.  Otherwise, use 1 since it is the
    2036              :          one easiest to get.  */
    2037              : #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
    2038            0 :       int normalizep = STORE_FLAG_VALUE;
    2039              : #else
    2040              :       int normalizep = 1;
    2041              : #endif
    2042              : 
    2043              :       /* Prepare the operands.  */
    2044            0 :       xop0 = force_reg (int_mode, op0);
    2045            0 :       xop1 = force_reg (int_mode, op1);
    2046              : 
    2047            0 :       xtarget = gen_reg_rtx (int_mode);
    2048              : 
    2049            0 :       if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
    2050              :         target = xtarget;
    2051              : 
    2052              :       /* Indicate for flow that the entire target reg is being set.  */
    2053            0 :       if (REG_P (target))
    2054            0 :         emit_clobber (xtarget);
    2055              : 
    2056              :       /* Do the actual arithmetic.  */
    2057            0 :       for (i = 0; i < nwords; i++)
    2058              :         {
    2059            0 :           int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
    2060            0 :           rtx target_piece = operand_subword (xtarget, index, 1, int_mode);
    2061            0 :           rtx op0_piece = operand_subword_force (xop0, index, int_mode);
    2062            0 :           rtx op1_piece = operand_subword_force (xop1, index, int_mode);
    2063            0 :           rtx x;
    2064              : 
    2065              :           /* Main add/subtract of the input operands.  */
    2066            0 :           x = expand_binop (word_mode, binoptab,
    2067              :                             op0_piece, op1_piece,
    2068              :                             target_piece, unsignedp, next_methods);
    2069            0 :           if (x == 0)
    2070              :             break;
    2071              : 
    2072            0 :           if (i + 1 < nwords)
    2073              :             {
    2074              :               /* Store carry from main add/subtract.  */
    2075            0 :               carry_out = gen_reg_rtx (word_mode);
    2076            0 :               carry_out = emit_store_flag_force (carry_out,
    2077              :                                                  (binoptab == add_optab
    2078              :                                                   ? LT : GT),
    2079              :                                                  x, op0_piece,
    2080              :                                                  word_mode, 1, normalizep);
    2081              :             }
    2082              : 
    2083            0 :           if (i > 0)
    2084              :             {
    2085            0 :               rtx newx;
    2086              : 
    2087              :               /* Add/subtract previous carry to main result.  */
    2088            0 :               newx = expand_binop (word_mode,
    2089              :                                    normalizep == 1 ? binoptab : otheroptab,
    2090              :                                    x, carry_in,
    2091              :                                    NULL_RTX, 1, next_methods);
    2092              : 
    2093            0 :               if (i + 1 < nwords)
    2094              :                 {
    2095              :                   /* Get out carry from adding/subtracting carry in.  */
    2096            0 :                   rtx carry_tmp = gen_reg_rtx (word_mode);
    2097            0 :                   carry_tmp = emit_store_flag_force (carry_tmp,
    2098              :                                                      (binoptab == add_optab
    2099              :                                                       ? LT : GT),
    2100              :                                                      newx, x,
    2101              :                                                      word_mode, 1, normalizep);
    2102              : 
    2103              :                   /* Logical-ior the two poss. carry together.  */
    2104            0 :                   carry_out = expand_binop (word_mode, ior_optab,
    2105              :                                             carry_out, carry_tmp,
    2106              :                                             carry_out, 0, next_methods);
    2107            0 :                   if (carry_out == 0)
    2108              :                     break;
    2109              :                 }
    2110            0 :               emit_move_insn (target_piece, newx);
    2111              :             }
    2112              :           else
    2113              :             {
    2114            0 :               if (x != target_piece)
    2115            0 :                 emit_move_insn (target_piece, x);
    2116              :             }
    2117              : 
    2118            0 :           carry_in = carry_out;
    2119              :         }
    2120              : 
    2121            0 :       if (i == GET_MODE_BITSIZE (int_mode) / (unsigned) BITS_PER_WORD)
    2122              :         {
    2123            0 :           if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing
    2124            0 :               || ! rtx_equal_p (target, xtarget))
    2125              :             {
    2126            0 :               rtx_insn *temp = emit_move_insn (target, xtarget);
    2127              : 
    2128            0 :               set_dst_reg_note (temp, REG_EQUAL,
    2129              :                                 gen_rtx_fmt_ee (optab_to_code (binoptab),
    2130              :                                                 int_mode, copy_rtx (xop0),
    2131              :                                                 copy_rtx (xop1)),
    2132              :                                 target);
    2133              :             }
    2134              :           else
    2135              :             target = xtarget;
    2136              : 
    2137              :           return target;
    2138              :         }
    2139              : 
    2140              :       else
    2141            0 :         delete_insns_since (last);
    2142              :     }
    2143              : 
    2144              :   /* Attempt to synthesize double word multiplies using a sequence of word
    2145              :      mode multiplications.  We first attempt to generate a sequence using a
    2146              :      more efficient unsigned widening multiply, and if that fails we then
    2147              :      try using a signed widening multiply.  */
    2148              : 
    2149       217794 :   if (binoptab == smul_optab
    2150        26938 :       && is_int_mode (mode, &int_mode)
    2151        19514 :       && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
    2152        16155 :       && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
    2153       233949 :       && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
    2154              :     {
    2155        16155 :       rtx product = NULL_RTX;
    2156        16155 :       if (convert_optab_handler (umul_widen_optab, int_mode, word_mode)
    2157              :           != CODE_FOR_nothing)
    2158              :         {
    2159        16155 :           product = expand_doubleword_mult (int_mode, op0, op1, target,
    2160              :                                             true, methods);
    2161        16155 :           if (!product)
    2162            0 :             delete_insns_since (last);
    2163              :         }
    2164              : 
    2165            0 :       if (product == NULL_RTX
    2166            0 :           && (convert_optab_handler (smul_widen_optab, int_mode, word_mode)
    2167              :               != CODE_FOR_nothing))
    2168              :         {
    2169            0 :           product = expand_doubleword_mult (int_mode, op0, op1, target,
    2170              :                                             false, methods);
    2171            0 :           if (!product)
    2172            0 :             delete_insns_since (last);
    2173              :         }
    2174              : 
    2175        16155 :       if (product != NULL_RTX)
    2176              :         {
    2177        16155 :           if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing)
    2178              :             {
    2179        25748 :               rtx_insn *move = emit_move_insn (target ? target : product,
    2180              :                                                product);
    2181        16155 :               set_dst_reg_note (move,
    2182              :                                 REG_EQUAL,
    2183              :                                 gen_rtx_fmt_ee (MULT, int_mode,
    2184              :                                                 copy_rtx (op0),
    2185              :                                                 copy_rtx (op1)),
    2186              :                                 target ? target : product);
    2187              :             }
    2188              :           return product;
    2189              :         }
    2190              :     }
    2191              : 
    2192              :   /* Attempt to synthesize double word modulo by constant divisor.  */
    2193       201639 :   if ((binoptab == umod_optab
    2194       201639 :        || binoptab == smod_optab
    2195       201639 :        || binoptab == udiv_optab
    2196              :        || binoptab == sdiv_optab)
    2197       137566 :       && optimize
    2198       111593 :       && CONST_INT_P (op1)
    2199        23028 :       && is_int_mode (mode, &int_mode)
    2200        25560 :       && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
    2201        31624 :       && optab_handler ((binoptab == umod_optab || binoptab == udiv_optab)
    2202              :                         ? udivmod_optab : sdivmod_optab,
    2203              :                         int_mode) == CODE_FOR_nothing
    2204        20252 :       && optab_handler (and_optab, word_mode) != CODE_FOR_nothing
    2205        20252 :       && optab_handler (add_optab, word_mode) != CODE_FOR_nothing
    2206        20252 :       && optimize_insn_for_speed_p ())
    2207              :     {
    2208        20054 :       rtx res = NULL_RTX;
    2209        20054 :       if ((binoptab == umod_optab || binoptab == smod_optab)
    2210         5196 :           && (INTVAL (op1) & 1) == 0)
    2211          396 :         res = expand_doubleword_mod (int_mode, op0, op1,
    2212              :                                      binoptab == umod_optab);
    2213              :       else
    2214              :         {
    2215        19658 :           rtx quot = expand_doubleword_divmod (int_mode, op0, op1, &res,
    2216              :                                                binoptab == umod_optab
    2217              :                                                || binoptab == udiv_optab);
    2218        19658 :           if (quot == NULL_RTX)
    2219        18977 :             res = NULL_RTX;
    2220          681 :           else if (binoptab == udiv_optab || binoptab == sdiv_optab)
    2221          474 :             res = quot;
    2222              :         }
    2223        20054 :       if (res != NULL_RTX)
    2224              :         {
    2225          681 :           if (optab_handler (mov_optab, int_mode) != CODE_FOR_nothing)
    2226              :             {
    2227          848 :               rtx_insn *move = emit_move_insn (target ? target : res,
    2228              :                                                res);
    2229          681 :               set_dst_reg_note (move, REG_EQUAL,
    2230              :                                 gen_rtx_fmt_ee (optab_to_code (binoptab),
    2231              :                                                 int_mode, copy_rtx (op0), op1),
    2232              :                                 target ? target : res);
    2233              :             }
    2234          681 :           return res;
    2235              :         }
    2236              :       else
    2237        19373 :         delete_insns_since (last);
    2238              :     }
    2239              : 
    2240              :   /* It can't be open-coded in this mode.
    2241              :      Use a library call if one is available and caller says that's ok.  */
    2242              : 
    2243       200958 :   libfunc = optab_libfunc (binoptab, mode);
    2244       200958 :   if (libfunc
    2245       133204 :       && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
    2246              :     {
    2247        30750 :       rtx_insn *insns;
    2248        30750 :       rtx op1x = op1;
    2249        30750 :       machine_mode op1_mode = mode;
    2250        30750 :       rtx value;
    2251              : 
    2252        30750 :       start_sequence ();
    2253              : 
    2254        30750 :       if (shift_optab_p (binoptab))
    2255              :         {
    2256            0 :           op1_mode = targetm.libgcc_shift_count_mode ();
    2257              :           /* Specify unsigned here,
    2258              :              since negative shift counts are meaningless.  */
    2259            0 :           op1x = convert_to_mode (op1_mode, op1, 1);
    2260              :         }
    2261              : 
    2262        30750 :       if (GET_MODE (op0) != VOIDmode
    2263        30698 :           && GET_MODE (op0) != mode)
    2264            0 :         op0 = convert_to_mode (mode, op0, unsignedp);
    2265              : 
    2266              :       /* Pass 1 for NO_QUEUE so we don't lose any increments
    2267              :          if the libcall is cse'd or moved.  */
    2268        30750 :       value = emit_library_call_value (libfunc,
    2269              :                                        NULL_RTX, LCT_CONST, mode,
    2270              :                                        op0, mode, op1x, op1_mode);
    2271              : 
    2272        30750 :       insns = end_sequence ();
    2273              : 
    2274        30750 :       bool trapv = trapv_binoptab_p (binoptab);
    2275        30750 :       target = gen_reg_rtx (mode);
    2276        30750 :       emit_libcall_block_1 (insns, target, value,
    2277              :                             trapv ? NULL_RTX
    2278        30240 :                             : gen_rtx_fmt_ee (optab_to_code (binoptab),
    2279              :                                               mode, op0, op1), trapv);
    2280              : 
    2281        30750 :       return target;
    2282              :     }
    2283              : 
    2284       170208 :   delete_insns_since (last);
    2285              : 
    2286              :   /* It can't be done in this mode.  Can we do it in a wider mode?  */
    2287              : 
    2288       170208 :   if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
    2289              :          || methods == OPTAB_MUST_WIDEN))
    2290              :     {
    2291              :       /* Caller says, don't even try.  */
    2292       157279 :       delete_insns_since (entry_last);
    2293       157279 :       return 0;
    2294              :     }
    2295              : 
    2296              :   /* Compute the value of METHODS to pass to recursive calls.
    2297              :      Don't allow widening to be tried recursively.  */
    2298              : 
    2299        12929 :   methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
    2300              : 
    2301              :   /* Look for a wider mode of the same class for which it appears we can do
    2302              :      the operation.  */
    2303              : 
    2304        12929 :   if (CLASS_HAS_WIDER_MODES_P (mclass))
    2305              :     {
    2306              :       /* This code doesn't make sense for conversion optabs, since we
    2307              :          wouldn't then want to extend the operands to be the same size
    2308              :          as the result.  */
    2309        12929 :       gcc_assert (!convert_optab_p (binoptab));
    2310        40775 :       FOR_EACH_WIDER_MODE (wider_mode, mode)
    2311              :         {
    2312        27847 :           if (optab_handler (binoptab, wider_mode)
    2313        27847 :               || (methods == OPTAB_LIB
    2314            2 :                   && optab_libfunc (binoptab, wider_mode)))
    2315              :             {
    2316            1 :               rtx xop0 = op0, xop1 = op1;
    2317            1 :               bool no_extend = false;
    2318              : 
    2319              :               /* For certain integer operations, we need not actually extend
    2320              :                  the narrow operands, as long as we will truncate
    2321              :                  the results to the same narrowness.  */
    2322              : 
    2323            1 :               if ((binoptab == ior_optab || binoptab == and_optab
    2324            1 :                    || binoptab == xor_optab
    2325            1 :                    || binoptab == add_optab || binoptab == sub_optab
    2326              :                    || binoptab == smul_optab || binoptab == ashl_optab)
    2327            0 :                   && mclass == MODE_INT)
    2328            1 :                 no_extend = true;
    2329              : 
    2330            1 :               xop0 = widen_operand (xop0, wider_mode, mode,
    2331              :                                     unsignedp, no_extend);
    2332              : 
    2333              :               /* The second operand of a shift must always be extended.  */
    2334            2 :               xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
    2335            1 :                                     no_extend && binoptab != ashl_optab);
    2336              : 
    2337            1 :               temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
    2338              :                                    unsignedp, methods);
    2339            1 :               if (temp)
    2340              :                 {
    2341            1 :                   if (mclass != MODE_INT
    2342            1 :                       || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
    2343              :                     {
    2344            0 :                       if (target == 0)
    2345            0 :                         target = gen_reg_rtx (mode);
    2346            0 :                       convert_move (target, temp, 0);
    2347            0 :                       return target;
    2348              :                     }
    2349              :                   else
    2350            1 :                     return gen_lowpart (mode, temp);
    2351              :                 }
    2352              :               else
    2353            0 :                 delete_insns_since (last);
    2354              :             }
    2355              :         }
    2356              :     }
    2357              : 
    2358        12928 :   delete_insns_since (entry_last);
    2359        12928 :   return 0;
    2360              : }
    2361              : 
    2362              : /* Expand a binary operator which has both signed and unsigned forms.
    2363              :    UOPTAB is the optab for unsigned operations, and SOPTAB is for
    2364              :    signed operations.
    2365              : 
    2366              :    If we widen unsigned operands, we may use a signed wider operation instead
    2367              :    of an unsigned wider operation, since the result would be the same.  */
    2368              : 
    2369              : rtx
    2370       119022 : sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
    2371              :                    rtx op0, rtx op1, rtx target, int unsignedp,
    2372              :                    enum optab_methods methods)
    2373              : {
    2374       119022 :   rtx temp;
    2375       119022 :   optab direct_optab = unsignedp ? uoptab : soptab;
    2376       119022 :   bool save_enable;
    2377              : 
    2378              :   /* Do it without widening, if possible.  */
    2379       119022 :   temp = expand_binop (mode, direct_optab, op0, op1, target,
    2380              :                        unsignedp, OPTAB_DIRECT);
    2381       119022 :   if (temp || methods == OPTAB_DIRECT)
    2382              :     return temp;
    2383              : 
    2384              :   /* Try widening to a signed int.  Disable any direct use of any
    2385              :      signed insn in the current mode.  */
    2386         8452 :   save_enable = swap_optab_enable (soptab, mode, false);
    2387              : 
    2388         8452 :   temp = expand_binop (mode, soptab, op0, op1, target,
    2389              :                        unsignedp, OPTAB_WIDEN);
    2390              : 
    2391              :   /* For unsigned operands, try widening to an unsigned int.  */
    2392         8452 :   if (!temp && unsignedp)
    2393         4329 :     temp = expand_binop (mode, uoptab, op0, op1, target,
    2394              :                          unsignedp, OPTAB_WIDEN);
    2395         8452 :   if (temp || methods == OPTAB_WIDEN)
    2396         4869 :     goto egress;
    2397              : 
    2398              :   /* Use the right width libcall if that exists.  */
    2399         3583 :   temp = expand_binop (mode, direct_optab, op0, op1, target,
    2400              :                        unsignedp, OPTAB_LIB);
    2401         3583 :   if (temp || methods == OPTAB_LIB)
    2402         3583 :     goto egress;
    2403              : 
    2404              :   /* Must widen and use a libcall, use either signed or unsigned.  */
    2405            0 :   temp = expand_binop (mode, soptab, op0, op1, target,
    2406              :                        unsignedp, methods);
    2407            0 :   if (!temp && unsignedp)
    2408            0 :     temp = expand_binop (mode, uoptab, op0, op1, target,
    2409              :                          unsignedp, methods);
    2410              : 
    2411            0 :  egress:
    2412              :   /* Undo the fiddling above.  */
    2413         8452 :   if (save_enable)
    2414            0 :     swap_optab_enable (soptab, mode, true);
    2415              :   return temp;
    2416              : }
    2417              : 
    2418              : /* Generate code to perform an operation specified by UNOPPTAB
    2419              :    on operand OP0, with two results to TARG0 and TARG1.
    2420              :    We assume that the order of the operands for the instruction
    2421              :    is TARG0, TARG1, OP0.
    2422              : 
    2423              :    Either TARG0 or TARG1 may be zero, but what that means is that
    2424              :    the result is not actually wanted.  We will generate it into
    2425              :    a dummy pseudo-reg and discard it.  They may not both be zero.
    2426              : 
    2427              :    Returns true if this operation can be performed; false if not.  */
    2428              : 
    2429              : bool
    2430            6 : expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
    2431              :                     int unsignedp)
    2432              : {
    2433            6 :   machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
    2434            6 :   enum mode_class mclass;
    2435            6 :   machine_mode wider_mode;
    2436            6 :   rtx_insn *entry_last = get_last_insn ();
    2437            6 :   rtx_insn *last;
    2438              : 
    2439            6 :   mclass = GET_MODE_CLASS (mode);
    2440              : 
    2441            6 :   if (!targ0)
    2442            0 :     targ0 = gen_reg_rtx (mode);
    2443            6 :   if (!targ1)
    2444            0 :     targ1 = gen_reg_rtx (mode);
    2445              : 
    2446              :   /* Record where to go back to if we fail.  */
    2447            6 :   last = get_last_insn ();
    2448              : 
    2449            6 :   if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
    2450              :     {
    2451            6 :       class expand_operand ops[3];
    2452            6 :       enum insn_code icode = optab_handler (unoptab, mode);
    2453              : 
    2454            6 :       create_fixed_operand (&ops[0], targ0);
    2455            6 :       create_fixed_operand (&ops[1], targ1);
    2456            6 :       create_convert_operand_from (&ops[2], op0, mode, unsignedp);
    2457            6 :       if (maybe_expand_insn (icode, 3, ops))
    2458            6 :         return true;
    2459              :     }
    2460              : 
    2461              :   /* It can't be done in this mode.  Can we do it in a wider mode?  */
    2462              : 
    2463            0 :   if (CLASS_HAS_WIDER_MODES_P (mclass))
    2464              :     {
    2465            0 :       FOR_EACH_WIDER_MODE (wider_mode, mode)
    2466              :         {
    2467            0 :           if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
    2468              :             {
    2469            0 :               rtx t0 = gen_reg_rtx (wider_mode);
    2470            0 :               rtx t1 = gen_reg_rtx (wider_mode);
    2471            0 :               rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
    2472              : 
    2473            0 :               if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
    2474              :                 {
    2475            0 :                   convert_move (targ0, t0, unsignedp);
    2476            0 :                   convert_move (targ1, t1, unsignedp);
    2477            0 :                   return true;
    2478              :                 }
    2479              :               else
    2480            0 :                 delete_insns_since (last);
    2481              :             }
    2482              :         }
    2483              :     }
    2484              : 
    2485            0 :   delete_insns_since (entry_last);
    2486            0 :   return false;
    2487              : }
    2488              : 
    2489              : /* Generate code to perform an operation specified by BINOPTAB
    2490              :    on operands OP0 and OP1, with two results to TARG1 and TARG2.
    2491              :    We assume that the order of the operands for the instruction
    2492              :    is TARG0, OP0, OP1, TARG1, which would fit a pattern like
    2493              :    [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
    2494              : 
    2495              :    Either TARG0 or TARG1 may be zero, but what that means is that
    2496              :    the result is not actually wanted.  We will generate it into
    2497              :    a dummy pseudo-reg and discard it.  They may not both be zero.
    2498              : 
    2499              :    Returns true if this operation can be performed; false if not.  */
    2500              : 
    2501              : bool
    2502       127326 : expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
    2503              :                      int unsignedp)
    2504              : {
    2505       127326 :   machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
    2506       127326 :   enum mode_class mclass;
    2507       127326 :   machine_mode wider_mode;
    2508       127326 :   rtx_insn *entry_last = get_last_insn ();
    2509       127326 :   rtx_insn *last;
    2510              : 
    2511       127326 :   mclass = GET_MODE_CLASS (mode);
    2512              : 
    2513       127326 :   if (!targ0)
    2514        44147 :     targ0 = gen_reg_rtx (mode);
    2515       127326 :   if (!targ1)
    2516        70650 :     targ1 = gen_reg_rtx (mode);
    2517              : 
    2518              :   /* Record where to go back to if we fail.  */
    2519       127326 :   last = get_last_insn ();
    2520              : 
    2521       127326 :   if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
    2522              :     {
    2523       122421 :       class expand_operand ops[4];
    2524       122421 :       enum insn_code icode = optab_handler (binoptab, mode);
    2525       122421 :       machine_mode mode0 = insn_data[icode].operand[1].mode;
    2526       122421 :       machine_mode mode1 = insn_data[icode].operand[2].mode;
    2527       122421 :       rtx xop0 = op0, xop1 = op1;
    2528              : 
    2529              :       /* If we are optimizing, force expensive constants into a register.  */
    2530       122421 :       xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
    2531       122421 :       xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
    2532              : 
    2533       122421 :       create_fixed_operand (&ops[0], targ0);
    2534       122421 :       create_convert_operand_from (&ops[1], xop0, mode, unsignedp);
    2535       122421 :       create_convert_operand_from (&ops[2], xop1, mode, unsignedp);
    2536       122421 :       create_fixed_operand (&ops[3], targ1);
    2537       122421 :       if (maybe_expand_insn (icode, 4, ops))
    2538       122421 :         return true;
    2539            0 :       delete_insns_since (last);
    2540              :     }
    2541              : 
    2542              :   /* It can't be done in this mode.  Can we do it in a wider mode?  */
    2543              : 
    2544         4905 :   if (CLASS_HAS_WIDER_MODES_P (mclass))
    2545              :     {
    2546        15597 :       FOR_EACH_WIDER_MODE (wider_mode, mode)
    2547              :         {
    2548        10692 :           if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
    2549              :             {
    2550            0 :               rtx t0 = gen_reg_rtx (wider_mode);
    2551            0 :               rtx t1 = gen_reg_rtx (wider_mode);
    2552            0 :               rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
    2553            0 :               rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
    2554              : 
    2555            0 :               if (expand_twoval_binop (binoptab, cop0, cop1,
    2556              :                                        t0, t1, unsignedp))
    2557              :                 {
    2558            0 :                   convert_move (targ0, t0, unsignedp);
    2559            0 :                   convert_move (targ1, t1, unsignedp);
    2560            0 :                   return true;
    2561              :                 }
    2562              :               else
    2563            0 :                 delete_insns_since (last);
    2564              :             }
    2565              :         }
    2566              :     }
    2567              : 
    2568         4905 :   delete_insns_since (entry_last);
    2569         4905 :   return false;
    2570              : }
    2571              : 
    2572              : /* Expand the two-valued library call indicated by BINOPTAB, but
    2573              :    preserve only one of the values.  If TARG0 is non-NULL, the first
    2574              :    value is placed into TARG0; otherwise the second value is placed
    2575              :    into TARG1.  Exactly one of TARG0 and TARG1 must be non-NULL.  The
    2576              :    value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
    2577              :    This routine assumes that the value returned by the library call is
    2578              :    as if the return value was of an integral mode twice as wide as the
    2579              :    mode of OP0.  Returns 1 if the call was successful.  */
    2580              : 
    2581              : bool
    2582            0 : expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
    2583              :                              rtx targ0, rtx targ1, enum rtx_code code)
    2584              : {
    2585            0 :   machine_mode mode;
    2586            0 :   machine_mode libval_mode;
    2587            0 :   rtx libval;
    2588            0 :   rtx_insn *insns;
    2589            0 :   rtx libfunc;
    2590              : 
    2591              :   /* Exactly one of TARG0 or TARG1 should be non-NULL.  */
    2592            0 :   gcc_assert (!targ0 != !targ1);
    2593              : 
    2594            0 :   mode = GET_MODE (op0);
    2595            0 :   libfunc = optab_libfunc (binoptab, mode);
    2596            0 :   if (!libfunc)
    2597              :     return false;
    2598              : 
    2599              :   /* The value returned by the library function will have twice as
    2600              :      many bits as the nominal MODE.  */
    2601            0 :   auto return_size = 2 * GET_MODE_BITSIZE (mode);
    2602            0 :   if (!smallest_int_mode_for_size (return_size).exists (&libval_mode))
    2603            0 :     return false;
    2604              : 
    2605            0 :   start_sequence ();
    2606            0 :   libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
    2607              :                                     libval_mode,
    2608              :                                     op0, mode,
    2609              :                                     op1, mode);
    2610              :   /* Get the part of VAL containing the value that we want.  */
    2611            0 :   libval = simplify_gen_subreg (mode, libval, libval_mode,
    2612            0 :                                 targ0 ? 0 : GET_MODE_SIZE (mode));
    2613            0 :   insns = end_sequence ();
    2614              :   /* Move the into the desired location.  */
    2615            0 :   emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
    2616              :                       gen_rtx_fmt_ee (code, mode, op0, op1));
    2617              : 
    2618            0 :   return true;
    2619              : }
    2620              : 
    2621              : 
    2622              : /* Wrapper around expand_unop which takes an rtx code to specify
    2623              :    the operation to perform, not an optab pointer.  All other
    2624              :    arguments are the same.  */
    2625              : rtx
    2626        20033 : expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
    2627              :                     rtx target, int unsignedp)
    2628              : {
    2629        20033 :   optab unop = code_to_optab (code);
    2630        20033 :   gcc_assert (unop);
    2631              : 
    2632        20033 :   return expand_unop (mode, unop, op0, target, unsignedp);
    2633              : }
    2634              : 
    2635              : /* Try calculating
    2636              :         (clz:narrow x)
    2637              :    as
    2638              :         (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
    2639              : 
    2640              :    A similar operation can be used for clrsb.  UNOPTAB says which operation
    2641              :    we are trying to expand.  */
    2642              : static rtx
    2643          298 : widen_leading (scalar_int_mode mode, rtx op0, rtx target, optab unoptab)
    2644              : {
    2645          298 :   opt_scalar_int_mode wider_mode_iter;
    2646         1223 :   FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
    2647              :     {
    2648          925 :       scalar_int_mode wider_mode = wider_mode_iter.require ();
    2649          925 :       if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
    2650              :         {
    2651            0 :           rtx xop0, temp;
    2652            0 :           rtx_insn *last;
    2653              : 
    2654            0 :           last = get_last_insn ();
    2655              : 
    2656            0 :           if (target == 0)
    2657            0 :             target = gen_reg_rtx (mode);
    2658            0 :           xop0 = widen_operand (op0, wider_mode, mode,
    2659              :                                 unoptab != clrsb_optab, false);
    2660            0 :           temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
    2661              :                               unoptab != clrsb_optab);
    2662            0 :           if (temp != 0)
    2663            0 :             temp = expand_binop
    2664            0 :               (wider_mode, sub_optab, temp,
    2665            0 :                gen_int_mode (GET_MODE_PRECISION (wider_mode)
    2666            0 :                              - GET_MODE_PRECISION (mode),
    2667              :                              wider_mode),
    2668              :                target, true, OPTAB_DIRECT);
    2669            0 :           if (temp == 0)
    2670            0 :             delete_insns_since (last);
    2671              : 
    2672          298 :           return temp;
    2673              :         }
    2674              :     }
    2675              :   return 0;
    2676              : }
    2677              : 
    2678              : /* Attempt to emit (clrsb:mode op0) as
    2679              :    (plus:mode (clz:mode (xor:mode op0 (ashr:mode op0 (const_int prec-1))))
    2680              :               (const_int -1))
    2681              :    if CLZ_DEFINED_VALUE_AT_ZERO (mode, val) is 2 and val is prec,
    2682              :    or as
    2683              :    (clz:mode (ior:mode (xor:mode (ashl:mode op0 (const_int 1))
    2684              :                                  (ashr:mode op0 (const_int prec-1)))
    2685              :                        (const_int 1)))
    2686              :    otherwise.  */
    2687              : 
    2688              : static rtx
    2689           92 : expand_clrsb_using_clz (scalar_int_mode mode, rtx op0, rtx target)
    2690              : {
    2691           92 :   if (optimize_insn_for_size_p ()
    2692           92 :       || optab_handler (clz_optab, mode) == CODE_FOR_nothing)
    2693              :     return NULL_RTX;
    2694              : 
    2695           88 :   start_sequence ();
    2696           88 :   HOST_WIDE_INT val = 0;
    2697           88 :   if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) != 2
    2698           88 :       || val != GET_MODE_PRECISION (mode))
    2699              :     val = 0;
    2700              :   else
    2701              :     val = 1;
    2702              : 
    2703           86 :   rtx temp2 = op0;
    2704           86 :   if (!val)
    2705              :     {
    2706           86 :       temp2 = expand_binop (mode, ashl_optab, op0, const1_rtx,
    2707              :                             NULL_RTX, 0, OPTAB_DIRECT);
    2708           86 :       if (!temp2)
    2709              :         {
    2710            0 :         fail:
    2711            0 :           end_sequence ();
    2712            0 :           return NULL_RTX;
    2713              :         }
    2714              :     }
    2715              : 
    2716           88 :   rtx temp = expand_binop (mode, ashr_optab, op0,
    2717           88 :                            GEN_INT (GET_MODE_PRECISION (mode) - 1),
    2718              :                            NULL_RTX, 0, OPTAB_DIRECT);
    2719           88 :   if (!temp)
    2720            0 :     goto fail;
    2721              : 
    2722           88 :   temp = expand_binop (mode, xor_optab, temp2, temp, NULL_RTX, 0,
    2723              :                        OPTAB_DIRECT);
    2724           88 :   if (!temp)
    2725            0 :     goto fail;
    2726              : 
    2727           88 :   if (!val)
    2728              :     {
    2729           86 :       temp = expand_binop (mode, ior_optab, temp, const1_rtx,
    2730              :                            NULL_RTX, 0, OPTAB_DIRECT);
    2731           86 :       if (!temp)
    2732            0 :         goto fail;
    2733              :     }
    2734           88 :   temp = expand_unop_direct (mode, clz_optab, temp, val ? NULL_RTX : target,
    2735              :                              true);
    2736           88 :   if (!temp)
    2737            0 :     goto fail;
    2738           88 :   if (val)
    2739              :     {
    2740            2 :       temp = expand_binop (mode, add_optab, temp, constm1_rtx,
    2741              :                            target, 0, OPTAB_DIRECT);
    2742            2 :       if (!temp)
    2743            0 :         goto fail;
    2744              :     }
    2745              : 
    2746           88 :   rtx_insn *seq = end_sequence ();
    2747              : 
    2748           88 :   add_equal_note (seq, temp, CLRSB, op0, NULL_RTX, mode);
    2749           88 :   emit_insn (seq);
    2750           88 :   return temp;
    2751              : }
    2752              : 
    2753              : static rtx expand_ffs (scalar_int_mode, rtx, rtx);
    2754              : 
    2755              : /* Try calculating clz, ctz or ffs of a double-word quantity as two clz, ctz or
    2756              :    ffs operations on word-sized quantities, choosing which based on whether the
    2757              :    high (for clz) or low (for ctz and ffs) word is nonzero.  */
    2758              : static rtx
    2759          251 : expand_doubleword_clz_ctz_ffs (scalar_int_mode mode, rtx op0, rtx target,
    2760              :                                optab unoptab)
    2761              : {
    2762          251 :   rtx xop0 = force_reg (mode, op0);
    2763          251 :   rtx subhi = gen_highpart (word_mode, xop0);
    2764          251 :   rtx sublo = gen_lowpart (word_mode, xop0);
    2765          251 :   rtx_code_label *hi0_label = gen_label_rtx ();
    2766          251 :   rtx_code_label *after_label = gen_label_rtx ();
    2767          251 :   rtx_insn *seq;
    2768          251 :   rtx temp, result;
    2769          251 :   int addend = 0;
    2770              : 
    2771              :   /* If we were not given a target, use a word_mode register, not a
    2772              :      'mode' register.  The result will fit, and nobody is expecting
    2773              :      anything bigger (the return type of __builtin_clz* is int).  */
    2774          251 :   if (!target)
    2775            0 :     target = gen_reg_rtx (word_mode);
    2776              : 
    2777              :   /* In any case, write to a word_mode scratch in both branches of the
    2778              :      conditional, so we can ensure there is a single move insn setting
    2779              :      'target' to tag a REG_EQUAL note on.  */
    2780          251 :   result = gen_reg_rtx (word_mode);
    2781              : 
    2782          251 :   if (unoptab != clz_optab)
    2783           45 :     std::swap (subhi, sublo);
    2784              : 
    2785          251 :   start_sequence ();
    2786              : 
    2787              :   /* If the high word is not equal to zero,
    2788              :      then clz of the full value is clz of the high word.  */
    2789          251 :   emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
    2790              :                            word_mode, true, hi0_label);
    2791              : 
    2792          251 :   if (optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
    2793          251 :     temp = expand_unop_direct (word_mode, unoptab, subhi, result, true);
    2794              :   else
    2795              :     {
    2796            0 :       gcc_assert (unoptab == ffs_optab);
    2797            0 :       temp = expand_ffs (word_mode, subhi, result);
    2798              :     }
    2799          251 :   if (!temp)
    2800            0 :     goto fail;
    2801              : 
    2802          251 :   if (temp != result)
    2803            0 :     convert_move (result, temp, true);
    2804              : 
    2805          251 :   emit_jump_insn (targetm.gen_jump (after_label));
    2806          251 :   emit_barrier ();
    2807              : 
    2808              :   /* Else clz of the full value is clz of the low word plus the number
    2809              :      of bits in the high word.  Similarly for ctz/ffs of the high word,
    2810              :      except that ffs should be 0 when both words are zero.  */
    2811          251 :   emit_label (hi0_label);
    2812              : 
    2813          251 :   if (unoptab == ffs_optab)
    2814              :     {
    2815            0 :       convert_move (result, const0_rtx, true);
    2816            0 :       emit_cmp_and_jump_insns (sublo, CONST0_RTX (word_mode), EQ, 0,
    2817              :                                word_mode, true, after_label);
    2818              :     }
    2819              : 
    2820          251 :   if (optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
    2821          251 :     temp = expand_unop_direct (word_mode, unoptab, sublo, NULL_RTX, true);
    2822              :   else
    2823              :     {
    2824            0 :       gcc_assert (unoptab == ffs_optab);
    2825            0 :       temp = expand_unop_direct (word_mode, ctz_optab, sublo, NULL_RTX, true);
    2826            0 :       addend = 1;
    2827              :     }
    2828              : 
    2829          251 :   if (!temp)
    2830            0 :     goto fail;
    2831              : 
    2832          502 :   temp = expand_binop (word_mode, add_optab, temp,
    2833          251 :                        gen_int_mode (GET_MODE_BITSIZE (word_mode) + addend,
    2834              :                                      word_mode),
    2835              :                        result, true, OPTAB_DIRECT);
    2836          251 :   if (!temp)
    2837            0 :     goto fail;
    2838          251 :   if (temp != result)
    2839            0 :     convert_move (result, temp, true);
    2840              : 
    2841          251 :   emit_label (after_label);
    2842          251 :   convert_move (target, result, true);
    2843              : 
    2844          251 :   seq = end_sequence ();
    2845              : 
    2846          251 :   add_equal_note (seq, target, optab_to_code (unoptab), xop0, NULL_RTX, mode);
    2847          251 :   emit_insn (seq);
    2848          251 :   return target;
    2849              : 
    2850            0 :  fail:
    2851            0 :   end_sequence ();
    2852            0 :   return 0;
    2853              : }
    2854              : 
    2855              : /* Try calculating popcount of a double-word quantity as two popcount's of
    2856              :    word-sized quantities and summing up the results.  */
    2857              : static rtx
    2858            0 : expand_doubleword_popcount (scalar_int_mode mode, rtx op0, rtx target)
    2859              : {
    2860            0 :   rtx t0, t1, t;
    2861            0 :   rtx_insn *seq;
    2862              : 
    2863            0 :   start_sequence ();
    2864              : 
    2865            0 :   t0 = expand_unop_direct (word_mode, popcount_optab,
    2866              :                            operand_subword_force (op0, 0, mode), NULL_RTX,
    2867              :                            true);
    2868            0 :   t1 = expand_unop_direct (word_mode, popcount_optab,
    2869              :                            operand_subword_force (op0, 1, mode), NULL_RTX,
    2870              :                            true);
    2871            0 :   if (!t0 || !t1)
    2872              :     {
    2873            0 :       end_sequence ();
    2874            0 :       return NULL_RTX;
    2875              :     }
    2876              : 
    2877              :   /* If we were not given a target, use a word_mode register, not a
    2878              :      'mode' register.  The result will fit, and nobody is expecting
    2879              :      anything bigger (the return type of __builtin_popcount* is int).  */
    2880            0 :   if (!target)
    2881            0 :     target = gen_reg_rtx (word_mode);
    2882              : 
    2883            0 :   t = expand_binop (word_mode, add_optab, t0, t1, target, 0, OPTAB_DIRECT);
    2884              : 
    2885            0 :   seq = end_sequence ();
    2886              : 
    2887            0 :   add_equal_note (seq, t, POPCOUNT, op0, NULL_RTX, mode);
    2888            0 :   emit_insn (seq);
    2889            0 :   return t;
    2890              : }
    2891              : 
    2892              : /* Try calculating
    2893              :         (parity:wide x)
    2894              :    as
    2895              :         (parity:narrow (low (x) ^ high (x))) */
    2896              : static rtx
    2897            0 : expand_doubleword_parity (scalar_int_mode mode, rtx op0, rtx target)
    2898              : {
    2899            0 :   rtx t = expand_binop (word_mode, xor_optab,
    2900              :                         operand_subword_force (op0, 0, mode),
    2901              :                         operand_subword_force (op0, 1, mode),
    2902              :                         NULL_RTX, 0, OPTAB_DIRECT);
    2903            0 :   return expand_unop (word_mode, parity_optab, t, target, true);
    2904              : }
    2905              : 
    2906              : /* Try calculating
    2907              :         (bswap:narrow x)
    2908              :    as
    2909              :         (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow)))
    2910              :    or similarly for bitreverse.  */
    2911              : static rtx
    2912          274 : widen_bswap_or_bitreverse (scalar_int_mode mode, rtx op0, rtx target,
    2913              :                            optab unoptab)
    2914              : {
    2915          274 :   rtx x;
    2916          274 :   rtx_insn *last;
    2917          274 :   opt_scalar_int_mode wider_mode_iter;
    2918              : 
    2919         1109 :   FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
    2920          835 :     if (optab_handler (unoptab, wider_mode_iter.require ())
    2921              :         != CODE_FOR_nothing)
    2922              :       break;
    2923              : 
    2924          274 :   if (!wider_mode_iter.exists ())
    2925              :     return NULL_RTX;
    2926              : 
    2927            0 :   scalar_int_mode wider_mode = wider_mode_iter.require ();
    2928            0 :   last = get_last_insn ();
    2929              : 
    2930            0 :   x = widen_operand (op0, wider_mode, mode, true, true);
    2931            0 :   x = expand_unop (wider_mode, unoptab, x, NULL_RTX, true);
    2932              : 
    2933            0 :   gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
    2934              :               && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
    2935            0 :   if (x != 0)
    2936            0 :     x = expand_shift (RSHIFT_EXPR, wider_mode, x,
    2937            0 :                       GET_MODE_BITSIZE (wider_mode)
    2938            0 :                       - GET_MODE_BITSIZE (mode),
    2939              :                       NULL_RTX, true);
    2940              : 
    2941            0 :   if (x != 0)
    2942              :     {
    2943            0 :       if (target == 0)
    2944            0 :         target = gen_reg_rtx (mode);
    2945            0 :       emit_move_insn (target, gen_lowpart (mode, x));
    2946              :     }
    2947              :   else
    2948            0 :     delete_insns_since (last);
    2949              : 
    2950              :   return target;
    2951              : }
    2952              : 
    2953              : /* Try calculating bswap as two bswaps of two word-sized operands.
    2954              :    Similarly for bitreverse.  */
    2955              : 
    2956              : static rtx
    2957          165 : expand_doubleword_bswap_or_bitreverse (machine_mode mode, rtx op, rtx target,
    2958              :                                        optab unoptab)
    2959              : {
    2960          165 :   rtx t0, t1;
    2961              : 
    2962          165 :   t1 = expand_unop (word_mode, unoptab,
    2963              :                     operand_subword_force (op, 0, mode), NULL_RTX, true);
    2964          165 :   t0 = expand_unop (word_mode, unoptab,
    2965              :                     operand_subword_force (op, 1, mode), NULL_RTX, true);
    2966              : 
    2967          165 :   if (target == 0 || !valid_multiword_target_p (target))
    2968            6 :     target = gen_reg_rtx (mode);
    2969          165 :   if (REG_P (target))
    2970          165 :     emit_clobber (target);
    2971          165 :   emit_move_insn (operand_subword (target, 0, 1, mode), t0);
    2972          165 :   emit_move_insn (operand_subword (target, 1, 1, mode), t1);
    2973              : 
    2974          165 :   return target;
    2975              : }
    2976              : 
    2977              : /* Try calculating (bitreverse x) using masks and shifts.  */
    2978              : 
    2979              : static rtx
    2980          109 : expand_bitreverse (scalar_int_mode mode, rtx op0, rtx target)
    2981              : {
    2982          109 :   unsigned int precision = GET_MODE_BITSIZE (mode);
    2983          109 :   rtx_insn *last;
    2984              : 
    2985              :   /* Operation requires at least 4 bits (one nibble swap makes no sense below
    2986              :      that).  */
    2987          109 :   if (precision < 4)
    2988              :     return NULL_RTX;
    2989              : 
    2990          109 :   if (rtx temp = widen_bswap_or_bitreverse (mode, op0, target,
    2991              :                                             bitreverse_optab))
    2992              :     return temp;
    2993              : 
    2994          218 :   if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
    2995          109 :       && optab_handler (bitreverse_optab, word_mode) != CODE_FOR_nothing)
    2996            0 :     if (rtx temp = expand_doubleword_bswap_or_bitreverse (mode, op0, target,
    2997              :                                                           bitreverse_optab))
    2998            0 :       return temp;
    2999              : 
    3000          109 :   if (target == NULL_RTX
    3001          109 :       || target == op0
    3002          109 :       || reg_overlap_mentioned_p (target, op0))
    3003            0 :     target = gen_reg_rtx (mode);
    3004              : 
    3005          109 :   last = get_last_insn ();
    3006              : 
    3007          109 :   rtx x, lo, hi;
    3008              : 
    3009              :   /* Step 1: byte-swap (only meaningful for >= 16 bits).  */
    3010          109 :   if (precision >= 16)
    3011              :     {
    3012          104 :       x = expand_unop (mode, bswap_optab, op0, NULL_RTX, true);
    3013          104 :       if (x == NULL_RTX)
    3014            0 :         goto fail;
    3015              :     }
    3016              :   else
    3017              :     x = op0;
    3018              : 
    3019              :   /* Step 2: swap nibbles within each byte (shift=4, only for >= 8 bits).  */
    3020          109 :   if (precision >= 8)
    3021              :     {
    3022          109 :       wide_int mask = wi::zero (precision);
    3023          912 :       for (unsigned int start = 0; start < precision; start += 8)
    3024         1606 :         mask = wi::bit_or (mask, wi::shifted_mask (start, 4, false,
    3025          803 :                                                    precision));
    3026              : 
    3027          109 :       rtx mask_rtx = immed_wide_int_const (mask, mode);
    3028              : 
    3029          109 :       hi = expand_simple_binop (mode, LSHIFTRT, x, GEN_INT (4),
    3030              :                                 NULL_RTX, true, OPTAB_LIB_WIDEN);
    3031          109 :       if (hi == NULL_RTX) goto fail;
    3032          109 :       hi = expand_binop (mode, and_optab, hi, mask_rtx,
    3033              :                          NULL_RTX, true, OPTAB_LIB_WIDEN);
    3034          109 :       if (hi == NULL_RTX) goto fail;
    3035              : 
    3036          109 :       lo = expand_binop (mode, and_optab, x, mask_rtx,
    3037              :                          NULL_RTX, true, OPTAB_LIB_WIDEN);
    3038          109 :       if (lo == NULL_RTX) goto fail;
    3039          109 :       lo = expand_simple_binop (mode, ASHIFT, lo, GEN_INT (4),
    3040              :                                 NULL_RTX, true, OPTAB_LIB_WIDEN);
    3041          109 :       if (lo == NULL_RTX) goto fail;
    3042              : 
    3043          109 :       x = expand_binop (mode, aop_optab, hi, lo,
    3044              :                         NULL_RTX, true, OPTAB_LIB_WIDEN);
    3045          109 :       if (x == NULL_RTX) goto fail;
    3046            0 :     }
    3047              : 
    3048              :   /* Step 3: swap pairs of bits within each nibble (shift=2).  */
    3049          109 :   {
    3050          109 :     wide_int mask = wi::zero (precision);
    3051         1715 :     for (unsigned int start = 0; start < precision; start += 4)
    3052         1606 :       mask = wi::bit_or (mask, wi::shifted_mask (start, 2, false, precision));
    3053              : 
    3054          109 :     rtx mask_rtx = immed_wide_int_const (mask, mode);
    3055              : 
    3056          109 :     hi = expand_simple_binop (mode, LSHIFTRT, x, GEN_INT (2),
    3057              :                               NULL_RTX, true, OPTAB_LIB_WIDEN);
    3058          109 :     if (hi == NULL_RTX) goto fail;
    3059          109 :     hi = expand_binop (mode, and_optab, hi, mask_rtx,
    3060              :                        NULL_RTX, true, OPTAB_LIB_WIDEN);
    3061          109 :     if (hi == NULL_RTX) goto fail;
    3062              : 
    3063          109 :     lo = expand_binop (mode, and_optab, x, mask_rtx,
    3064              :                        NULL_RTX, true, OPTAB_LIB_WIDEN);
    3065          109 :     if (lo == NULL_RTX) goto fail;
    3066          109 :     lo = expand_simple_binop (mode, ASHIFT, lo, GEN_INT (2),
    3067              :                               NULL_RTX, true, OPTAB_LIB_WIDEN);
    3068          109 :     if (lo == NULL_RTX) goto fail;
    3069              : 
    3070          109 :     x = expand_binop (mode, aop_optab, hi, lo,
    3071              :                       NULL_RTX, true, OPTAB_LIB_WIDEN);
    3072          109 :     if (x == NULL_RTX) goto fail;
    3073            0 :   }
    3074              : 
    3075              :   /* Step 4: swap adjacent bits (shift=1).  */
    3076          109 :   {
    3077          109 :     wide_int mask = wi::zero (precision);
    3078         3321 :     for (unsigned int start = 0; start < precision; start += 2)
    3079         6424 :       mask = wi::bit_or (mask, wi::shifted_mask (start, 1, false,
    3080         3212 :                                                  precision));
    3081              : 
    3082          109 :     rtx mask_rtx = immed_wide_int_const (mask, mode);
    3083              : 
    3084          109 :     hi = expand_simple_binop (mode, LSHIFTRT, x, GEN_INT (1),
    3085              :                               NULL_RTX, true, OPTAB_LIB_WIDEN);
    3086          109 :     if (hi == NULL_RTX) goto fail;
    3087          109 :     hi = expand_binop (mode, and_optab, hi, mask_rtx,
    3088              :                        NULL_RTX, true, OPTAB_LIB_WIDEN);
    3089          109 :     if (hi == NULL_RTX) goto fail;
    3090              : 
    3091          109 :     lo = expand_binop (mode, and_optab, x, mask_rtx,
    3092              :                        NULL_RTX, true, OPTAB_LIB_WIDEN);
    3093          109 :     if (lo == NULL_RTX) goto fail;
    3094          109 :     lo = expand_simple_binop (mode, ASHIFT, lo, GEN_INT (1),
    3095              :                               NULL_RTX, true, OPTAB_LIB_WIDEN);
    3096          109 :     if (lo == NULL_RTX) goto fail;
    3097              : 
    3098          109 :     x = expand_binop (mode, aop_optab, hi, lo,
    3099              :                       target, true, OPTAB_LIB_WIDEN);
    3100          109 :     if (x == NULL_RTX) goto fail;
    3101            0 :   }
    3102              : 
    3103          109 :   if (x != target)
    3104            0 :     emit_move_insn (target, x);
    3105              : 
    3106              :   return target;
    3107              : 
    3108            0 :  fail:
    3109            0 :   delete_insns_since (last);
    3110            0 :   return NULL_RTX;
    3111              : }
    3112              : 
    3113              : /* Try calculating (parity x) as (and (popcount x) 1), where
    3114              :    popcount can also be done in a wider mode.  */
    3115              : static rtx
    3116           11 : expand_parity (scalar_int_mode mode, rtx op0, rtx target)
    3117              : {
    3118           11 :   enum mode_class mclass = GET_MODE_CLASS (mode);
    3119           11 :   opt_scalar_int_mode wider_mode_iter;
    3120           11 :   FOR_EACH_MODE_FROM (wider_mode_iter, mode)
    3121              :     {
    3122           11 :       scalar_int_mode wider_mode = wider_mode_iter.require ();
    3123           11 :       if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
    3124              :         {
    3125           11 :           rtx xop0, temp;
    3126           11 :           rtx_insn *last;
    3127              : 
    3128           11 :           last = get_last_insn ();
    3129              : 
    3130           11 :           if (target == 0 || GET_MODE (target) != wider_mode)
    3131            6 :             target = gen_reg_rtx (wider_mode);
    3132              : 
    3133           11 :           xop0 = widen_operand (op0, wider_mode, mode, true, false);
    3134           11 :           temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
    3135              :                               true);
    3136           11 :           if (temp != 0)
    3137           11 :             temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
    3138              :                                  target, true, OPTAB_DIRECT);
    3139              : 
    3140           11 :           if (temp)
    3141              :             {
    3142           11 :               if (mclass != MODE_INT
    3143           11 :                   || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
    3144            0 :                 return convert_to_mode (mode, temp, 0);
    3145              :               else
    3146           11 :                 return gen_lowpart (mode, temp);
    3147              :             }
    3148              :           else
    3149            0 :             delete_insns_since (last);
    3150              :         }
    3151              :     }
    3152              :   return 0;
    3153              : }
    3154              : 
    3155              : /* Try calculating ctz(x) as K - clz(x & -x) ,
    3156              :    where K is GET_MODE_PRECISION(mode) - 1.
    3157              : 
    3158              :    Both __builtin_ctz and __builtin_clz are undefined at zero, so we
    3159              :    don't have to worry about what the hardware does in that case.  (If
    3160              :    the clz instruction produces the usual value at 0, which is K, the
    3161              :    result of this code sequence will be -1; expand_ffs, below, relies
    3162              :    on this.  It might be nice to have it be K instead, for consistency
    3163              :    with the (very few) processors that provide a ctz with a defined
    3164              :    value, but that would take one more instruction, and it would be
    3165              :    less convenient for expand_ffs anyway.  */
    3166              : 
    3167              : static rtx
    3168           48 : expand_ctz (scalar_int_mode mode, rtx op0, rtx target)
    3169              : {
    3170           48 :   rtx_insn *seq;
    3171           48 :   rtx temp;
    3172              : 
    3173           48 :   if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
    3174              :     return 0;
    3175              : 
    3176            0 :   start_sequence ();
    3177              : 
    3178            0 :   temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
    3179            0 :   if (temp)
    3180            0 :     temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
    3181              :                          true, OPTAB_DIRECT);
    3182            0 :   if (temp)
    3183            0 :     temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
    3184            0 :   if (temp)
    3185            0 :     temp = expand_binop (mode, sub_optab,
    3186            0 :                          gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
    3187              :                          temp, target,
    3188              :                          true, OPTAB_DIRECT);
    3189            0 :   if (temp == 0)
    3190              :     {
    3191            0 :       end_sequence ();
    3192            0 :       return 0;
    3193              :     }
    3194              : 
    3195            0 :   seq = end_sequence ();
    3196              : 
    3197            0 :   add_equal_note (seq, temp, CTZ, op0, NULL_RTX, mode);
    3198            0 :   emit_insn (seq);
    3199            0 :   return temp;
    3200              : }
    3201              : 
    3202              : 
    3203              : /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
    3204              :    else with the sequence used by expand_clz.
    3205              : 
    3206              :    The ffs builtin promises to return zero for a zero value and ctz/clz
    3207              :    may have an undefined value in that case.  If they do not give us a
    3208              :    convenient value, we have to generate a test and branch.  */
    3209              : static rtx
    3210            0 : expand_ffs (scalar_int_mode mode, rtx op0, rtx target)
    3211              : {
    3212            0 :   HOST_WIDE_INT val = 0;
    3213            0 :   bool defined_at_zero = false;
    3214            0 :   rtx temp;
    3215            0 :   rtx_insn *seq;
    3216              : 
    3217            0 :   if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
    3218              :     {
    3219            0 :       start_sequence ();
    3220              : 
    3221            0 :       temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
    3222            0 :       if (!temp)
    3223            0 :         goto fail;
    3224              : 
    3225            0 :       defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
    3226              :     }
    3227            0 :   else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
    3228              :     {
    3229            0 :       start_sequence ();
    3230            0 :       temp = expand_ctz (mode, op0, 0);
    3231            0 :       if (!temp)
    3232            0 :         goto fail;
    3233              : 
    3234            0 :       if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
    3235              :         {
    3236            0 :           defined_at_zero = true;
    3237            0 :           val = (GET_MODE_PRECISION (mode) - 1) - val;
    3238              :         }
    3239              :     }
    3240              :   else
    3241              :     return 0;
    3242              : 
    3243            0 :   if (defined_at_zero && val == -1)
    3244              :     /* No correction needed at zero.  */;
    3245              :   else
    3246              :     {
    3247              :       /* We don't try to do anything clever with the situation found
    3248              :          on some processors (eg Alpha) where ctz(0:mode) ==
    3249              :          bitsize(mode).  If someone can think of a way to send N to -1
    3250              :          and leave alone all values in the range 0..N-1 (where N is a
    3251              :          power of two), cheaper than this test-and-branch, please add it.
    3252              : 
    3253              :          The test-and-branch is done after the operation itself, in case
    3254              :          the operation sets condition codes that can be recycled for this.
    3255              :          (This is true on i386, for instance.)  */
    3256              : 
    3257            0 :       rtx_code_label *nonzero_label = gen_label_rtx ();
    3258            0 :       emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
    3259              :                                mode, true, nonzero_label);
    3260              : 
    3261            0 :       convert_move (temp, GEN_INT (-1), false);
    3262            0 :       emit_label (nonzero_label);
    3263              :     }
    3264              : 
    3265              :   /* temp now has a value in the range -1..bitsize-1.  ffs is supposed
    3266              :      to produce a value in the range 0..bitsize.  */
    3267            0 :   temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
    3268              :                        target, false, OPTAB_DIRECT);
    3269            0 :   if (!temp)
    3270            0 :     goto fail;
    3271              : 
    3272            0 :   seq = end_sequence ();
    3273              : 
    3274            0 :   add_equal_note (seq, temp, FFS, op0, NULL_RTX, mode);
    3275            0 :   emit_insn (seq);
    3276            0 :   return temp;
    3277              : 
    3278            0 :  fail:
    3279            0 :   end_sequence ();
    3280            0 :   return 0;
    3281              : }
    3282              : 
    3283              : /* Expand a floating point absolute value or negation operation via a
    3284              :    logical operation on the sign bit.  MODE is the mode of the operands
    3285              :    and FMODE is the scalar inner mode.  */
    3286              : 
    3287              : static rtx
    3288         2404 : expand_absneg_bit (rtx_code code, machine_mode mode,
    3289              :                    scalar_float_mode fmode, rtx op0, rtx target)
    3290              : {
    3291         2404 :   int bitpos, word, nwords, i;
    3292         2404 :   machine_mode new_mode;
    3293         2404 :   scalar_int_mode imode;
    3294         2404 :   rtx temp;
    3295         2404 :   rtx_insn *insns;
    3296              : 
    3297         2404 :   auto op = code == NEG ? neg_optab : abs_optab;
    3298         2404 :   if (!get_absneg_bit_mode (op, mode, fmode, &bitpos).exists (&new_mode))
    3299            0 :     return NULL_RTX;
    3300              : 
    3301         4808 :   imode = as_a<scalar_int_mode> (GET_MODE_INNER (new_mode));
    3302         4876 :   if (VECTOR_MODE_P (mode) || GET_MODE_SIZE (fmode) <= UNITS_PER_WORD)
    3303              :     {
    3304              :       word = 0;
    3305              :       nwords = 1;
    3306              :     }
    3307              :   else
    3308              :     {
    3309           17 :       if (FLOAT_WORDS_BIG_ENDIAN)
    3310              :         word = (GET_MODE_BITSIZE (fmode) - bitpos) / BITS_PER_WORD;
    3311              :       else
    3312           17 :         word = bitpos / BITS_PER_WORD;
    3313           17 :       bitpos = bitpos % BITS_PER_WORD;
    3314           34 :       nwords = (GET_MODE_BITSIZE (fmode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
    3315              :     }
    3316              : 
    3317         2404 :   wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
    3318         2404 :   if (code == ABS)
    3319         2112 :     mask = ~mask;
    3320              : 
    3321         2404 :   if (target == 0
    3322         2404 :       || target == op0
    3323          688 :       || reg_overlap_mentioned_p (target, op0)
    3324         3088 :       || (nwords > 1 && !valid_multiword_target_p (target)))
    3325         1720 :     target = gen_reg_rtx (mode);
    3326              : 
    3327         2404 :   if (nwords > 1)
    3328              :     {
    3329           17 :       start_sequence ();
    3330              : 
    3331           68 :       for (i = 0; i < nwords; ++i)
    3332              :         {
    3333           34 :           rtx targ_piece = operand_subword (target, i, 1, mode);
    3334           34 :           rtx op0_piece = operand_subword_force (op0, i, mode);
    3335              : 
    3336           34 :           if (i == word)
    3337              :             {
    3338           46 :               temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
    3339              :                                    op0_piece,
    3340           17 :                                    immed_wide_int_const (mask, imode),
    3341              :                                    targ_piece, 1, OPTAB_LIB_WIDEN);
    3342           17 :               if (temp != targ_piece)
    3343            0 :                 emit_move_insn (targ_piece, temp);
    3344              :             }
    3345              :           else
    3346           17 :             emit_move_insn (targ_piece, op0_piece);
    3347              :         }
    3348              : 
    3349           17 :       insns = end_sequence ();
    3350              : 
    3351           17 :       emit_insn (insns);
    3352              :     }
    3353              :   else
    3354              :     {
    3355         2387 :       rtx mask_rtx = immed_wide_int_const (mask, imode);
    3356         2387 :       if (VECTOR_MODE_P (new_mode))
    3357            0 :         mask_rtx = gen_const_vec_duplicate (new_mode, mask_rtx);
    3358         2667 :       temp = expand_binop (new_mode, code == ABS ? and_optab : xor_optab,
    3359         2387 :                            gen_lowpart (new_mode, op0), mask_rtx,
    3360         2387 :                            gen_lowpart (new_mode, target), 1, OPTAB_LIB_WIDEN);
    3361         2387 :       target = force_lowpart_subreg (mode, temp, new_mode);
    3362              : 
    3363         2387 :       set_dst_reg_note (get_last_insn (), REG_EQUAL,
    3364              :                         gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
    3365              :                         target);
    3366              :     }
    3367              : 
    3368         2404 :   return target;
    3369         2404 : }
    3370              : 
    3371              : /* As expand_unop, but will fail rather than attempt the operation in a
    3372              :    different mode or with a libcall.  */
    3373              : static rtx
    3374       187172 : expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
    3375              :                     int unsignedp)
    3376              : {
    3377       187172 :   if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
    3378              :     {
    3379       183214 :       class expand_operand ops[2];
    3380       183214 :       enum insn_code icode = optab_handler (unoptab, mode);
    3381       183214 :       rtx_insn *last = get_last_insn ();
    3382       183214 :       rtx_insn *pat;
    3383              : 
    3384       183214 :       create_output_operand (&ops[0], target, mode);
    3385       183214 :       create_convert_operand_from (&ops[1], op0, mode, unsignedp);
    3386       183214 :       pat = maybe_gen_insn (icode, 2, ops);
    3387       183214 :       if (pat)
    3388              :         {
    3389       182512 :           if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
    3390       235684 :               && ! add_equal_note (pat, ops[0].value,
    3391              :                                    optab_to_code (unoptab),
    3392              :                                    ops[1].value, NULL_RTX, mode))
    3393              :             {
    3394         1086 :               delete_insns_since (last);
    3395       183214 :               return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
    3396              :             }
    3397              : 
    3398       182128 :           emit_insn (pat);
    3399              : 
    3400       182128 :           return ops[0].value;
    3401              :         }
    3402              :     }
    3403              :   return 0;
    3404              : }
    3405              : 
    3406              : /* Generate code to perform an operation specified by UNOPTAB
    3407              :    on operand OP0, with result having machine-mode MODE.
    3408              : 
    3409              :    UNSIGNEDP is for the case where we have to widen the operands
    3410              :    to perform the operation.  It says to use zero-extension.
    3411              : 
    3412              :    If TARGET is nonzero, the value
    3413              :    is generated there, if it is convenient to do so.
    3414              :    In all cases an rtx is returned for the locus of the value;
    3415              :    this may or may not be TARGET.  */
    3416              : 
    3417              : rtx
    3418       186582 : expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
    3419              :              int unsignedp)
    3420              : {
    3421       186582 :   enum mode_class mclass = GET_MODE_CLASS (mode);
    3422       186582 :   machine_mode wider_mode;
    3423       186582 :   scalar_int_mode int_mode;
    3424       186582 :   scalar_float_mode float_mode;
    3425       186582 :   rtx temp;
    3426       186582 :   rtx libfunc;
    3427              : 
    3428       186582 :   temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
    3429       186582 :   if (temp)
    3430              :     return temp;
    3431              : 
    3432              :   /* It can't be done in this mode.  Can we open-code it in a wider mode?  */
    3433              : 
    3434              :   /* Widening (or narrowing) clz needs special treatment.  */
    3435         3958 :   if (unoptab == clz_optab)
    3436              :     {
    3437          206 :       if (is_a <scalar_int_mode> (mode, &int_mode))
    3438              :         {
    3439          206 :           temp = widen_leading (int_mode, op0, target, unoptab);
    3440          206 :           if (temp)
    3441              :             return temp;
    3442              : 
    3443          412 :           if (GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
    3444          206 :               && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
    3445              :             {
    3446          206 :               temp = expand_doubleword_clz_ctz_ffs (int_mode, op0, target,
    3447              :                                                     unoptab);
    3448          206 :               if (temp)
    3449              :                 return temp;
    3450              :             }
    3451              :         }
    3452              : 
    3453            0 :       goto try_libcall;
    3454              :     }
    3455              : 
    3456         3752 :   if (unoptab == clrsb_optab)
    3457              :     {
    3458           92 :       if (is_a <scalar_int_mode> (mode, &int_mode))
    3459              :         {
    3460           92 :           temp = widen_leading (int_mode, op0, target, unoptab);
    3461           92 :           if (temp)
    3462              :             return temp;
    3463           92 :           temp = expand_clrsb_using_clz (int_mode, op0, target);
    3464           92 :           if (temp)
    3465              :             return temp;
    3466              :         }
    3467            4 :       goto try_libcall;
    3468              :     }
    3469              : 
    3470         3660 :   if (unoptab == popcount_optab
    3471          741 :       && is_a <scalar_int_mode> (mode, &int_mode)
    3472          773 :       && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
    3473            2 :       && optab_handler (unoptab, word_mode) != CODE_FOR_nothing
    3474         3660 :       && optimize_insn_for_speed_p ())
    3475              :     {
    3476            0 :       temp = expand_doubleword_popcount (int_mode, op0, target);
    3477            0 :       if (temp)
    3478              :         return temp;
    3479              :     }
    3480              : 
    3481         3660 :   if (unoptab == parity_optab
    3482           11 :       && is_a <scalar_int_mode> (mode, &int_mode)
    3483           11 :       && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
    3484            0 :       && (optab_handler (unoptab, word_mode) != CODE_FOR_nothing
    3485            0 :           || optab_handler (popcount_optab, word_mode) != CODE_FOR_nothing)
    3486         3660 :       && optimize_insn_for_speed_p ())
    3487              :     {
    3488            0 :       temp = expand_doubleword_parity (int_mode, op0, target);
    3489            0 :       if (temp)
    3490              :         return temp;
    3491              :     }
    3492              : 
    3493              :   /* Widening (or narrowing) bswap needs special treatment.  */
    3494         3660 :   if (unoptab == bswap_optab)
    3495              :     {
    3496              :       /* HImode is special because in this mode BSWAP is equivalent to ROTATE
    3497              :          or ROTATERT.  First try these directly; if this fails, then try the
    3498              :          obvious pair of shifts with allowed widening, as this will probably
    3499              :          be always more efficient than the other fallback methods.  */
    3500          165 :       if (mode == HImode)
    3501              :         {
    3502            0 :           rtx_insn *last;
    3503            0 :           rtx temp1, temp2;
    3504              : 
    3505            0 :           if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
    3506              :             {
    3507            0 :               temp = expand_binop (mode, rotl_optab, op0,
    3508              :                                    gen_int_shift_amount (mode, 8),
    3509              :                                    target, unsignedp, OPTAB_DIRECT);
    3510            0 :               if (temp)
    3511              :                 return temp;
    3512              :              }
    3513              : 
    3514            0 :           if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
    3515              :             {
    3516            0 :               temp = expand_binop (mode, rotr_optab, op0,
    3517              :                                    gen_int_shift_amount (mode, 8),
    3518              :                                    target, unsignedp, OPTAB_DIRECT);
    3519            0 :               if (temp)
    3520              :                 return temp;
    3521              :             }
    3522              : 
    3523            0 :           last = get_last_insn ();
    3524              : 
    3525            0 :           temp1 = expand_binop (mode, ashl_optab, op0,
    3526              :                                 gen_int_shift_amount (mode, 8), NULL_RTX,
    3527              :                                 unsignedp, OPTAB_WIDEN);
    3528            0 :           temp2 = expand_binop (mode, lshr_optab, op0,
    3529              :                                 gen_int_shift_amount (mode, 8), NULL_RTX,
    3530              :                                 unsignedp, OPTAB_WIDEN);
    3531            0 :           if (temp1 && temp2)
    3532              :             {
    3533            0 :               temp = expand_binop (mode, ior_optab, temp1, temp2, target,
    3534              :                                    unsignedp, OPTAB_WIDEN);
    3535            0 :               if (temp)
    3536              :                 return temp;
    3537              :             }
    3538              : 
    3539            0 :           delete_insns_since (last);
    3540              :         }
    3541              : 
    3542          165 :       if (is_a <scalar_int_mode> (mode, &int_mode))
    3543              :         {
    3544          165 :           temp = widen_bswap_or_bitreverse (int_mode, op0, target, unoptab);
    3545          165 :           if (temp)
    3546              :             return temp;
    3547              : 
    3548              :           /* We do not provide a 128-bit bswap in libgcc so force the use of
    3549              :              a double bswap for 64-bit targets.  */
    3550          330 :           if (GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
    3551          165 :               && (UNITS_PER_WORD == 8
    3552          147 :                   || optab_handler (unoptab, word_mode) != CODE_FOR_nothing))
    3553              :             {
    3554          165 :               temp = expand_doubleword_bswap_or_bitreverse (mode, op0, target,
    3555              :                                                             unoptab);
    3556          165 :               if (temp)
    3557              :                 return temp;
    3558              :             }
    3559              :         }
    3560              : 
    3561            0 :       goto try_libcall;
    3562              :     }
    3563              : 
    3564         3495 :   if (unoptab == bitreverse_optab && is_a <scalar_int_mode> (mode, &int_mode))
    3565          109 :     if (rtx tem = expand_bitreverse (int_mode, op0, target))
    3566              :       return tem;
    3567              : 
    3568              :   /* Neg should be tried via expand_absneg_bit before widening.  */
    3569         3386 :   if (optab_to_code (unoptab) == NEG)
    3570              :     {
    3571              :       /* Try negating floating point values by flipping the sign bit.  */
    3572          772 :       if (is_a <scalar_float_mode> (GET_MODE_INNER (mode), &float_mode))
    3573              :         {
    3574          292 :           temp = expand_absneg_bit (NEG, mode, float_mode, op0, target);
    3575          292 :           if (temp)
    3576              :             return temp;
    3577              :         }
    3578              : 
    3579              :       /* If there is no negation pattern, and we have no negative zero,
    3580              :          try subtracting from zero.  */
    3581           94 :       if (!HONOR_SIGNED_ZEROS (mode))
    3582              :         {
    3583           94 :           temp = expand_binop (mode, (unoptab == negv_optab
    3584              :                                       ? subv_optab : sub_optab),
    3585              :                                CONST0_RTX (mode), op0, target,
    3586              :                                unsignedp, OPTAB_DIRECT);
    3587           94 :           if (temp)
    3588              :             return temp;
    3589              :         }
    3590              :     }
    3591              : 
    3592              :   /* ABS also needs to be handled similarly.  */
    3593         3094 :   if (optab_to_code (unoptab) == ABS
    3594         5294 :       && is_a <scalar_float_mode> (GET_MODE_INNER (mode), &float_mode))
    3595              :     {
    3596         2112 :       temp = expand_absneg_bit (ABS, mode, float_mode, op0, target);
    3597         2112 :       if (temp)
    3598              :         return temp;
    3599              :     }
    3600              : 
    3601          982 :   if (CLASS_HAS_WIDER_MODES_P (mclass))
    3602         4386 :     FOR_EACH_WIDER_MODE (wider_mode, mode)
    3603              :       {
    3604         3404 :         if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
    3605              :           {
    3606            0 :             rtx xop0 = op0;
    3607            0 :             rtx_insn *last = get_last_insn ();
    3608              : 
    3609              :             /* For certain operations, we need not actually extend
    3610              :                the narrow operand, as long as we will truncate the
    3611              :                results to the same narrowness.  */
    3612              : 
    3613            0 :             xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
    3614            0 :                                   (unoptab == neg_optab
    3615            0 :                                    || unoptab == one_cmpl_optab)
    3616              :                                   && mclass == MODE_INT);
    3617              : 
    3618            0 :             temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
    3619              :                                 unsignedp);
    3620              : 
    3621            0 :             if (temp)
    3622              :               {
    3623            0 :                 if (mclass != MODE_INT
    3624            0 :                     || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
    3625              :                   {
    3626            0 :                     if (target == 0)
    3627            0 :                       target = gen_reg_rtx (mode);
    3628            0 :                     convert_move (target, temp, 0);
    3629            0 :                     return target;
    3630              :                   }
    3631              :                 else
    3632            0 :                   return gen_lowpart (mode, temp);
    3633              :               }
    3634              :             else
    3635            0 :               delete_insns_since (last);
    3636              :           }
    3637              :       }
    3638              : 
    3639              :   /* These can be done a word at a time.  */
    3640          982 :   if (unoptab == one_cmpl_optab
    3641            0 :       && is_int_mode (mode, &int_mode)
    3642            0 :       && GET_MODE_SIZE (int_mode) > UNITS_PER_WORD
    3643          982 :       && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
    3644              :     {
    3645            0 :       int i;
    3646            0 :       rtx_insn *insns;
    3647              : 
    3648            0 :       if (target == 0
    3649            0 :           || target == op0
    3650            0 :           || reg_overlap_mentioned_p (target, op0)
    3651            0 :           || !valid_multiword_target_p (target))
    3652            0 :         target = gen_reg_rtx (int_mode);
    3653              : 
    3654            0 :       start_sequence ();
    3655              : 
    3656              :       /* Do the actual arithmetic.  */
    3657            0 :       for (i = 0; i < GET_MODE_BITSIZE (int_mode) / BITS_PER_WORD; i++)
    3658              :         {
    3659            0 :           rtx target_piece = operand_subword (target, i, 1, int_mode);
    3660            0 :           rtx x = expand_unop (word_mode, unoptab,
    3661            0 :                                operand_subword_force (op0, i, int_mode),
    3662              :                                target_piece, unsignedp);
    3663              : 
    3664            0 :           if (target_piece != x)
    3665            0 :             emit_move_insn (target_piece, x);
    3666              :         }
    3667              : 
    3668            0 :       insns = end_sequence ();
    3669              : 
    3670            0 :       emit_insn (insns);
    3671            0 :       return target;
    3672              :     }
    3673              : 
    3674              :   /* Emit ~op0 as op0 ^ -1.  */
    3675          982 :   if (unoptab == one_cmpl_optab
    3676            0 :       && (SCALAR_INT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_VECTOR_INT)
    3677          982 :       && optab_handler (xor_optab, mode) != CODE_FOR_nothing)
    3678              :     {
    3679            0 :       temp = expand_binop (mode, xor_optab, op0, CONSTM1_RTX (mode),
    3680              :                            target, unsignedp, OPTAB_DIRECT);
    3681            0 :       if (temp)
    3682              :         return temp;
    3683              :     }
    3684              : 
    3685              :   /* Try calculating parity (x) as popcount (x) % 2.  */
    3686          982 :   if (unoptab == parity_optab && is_a <scalar_int_mode> (mode, &int_mode))
    3687              :     {
    3688           11 :       temp = expand_parity (int_mode, op0, target);
    3689           11 :       if (temp)
    3690              :         return temp;
    3691              :     }
    3692              : 
    3693              :   /* Try implementing ffs (x) in terms of clz (x).  */
    3694          971 :   if (unoptab == ffs_optab && is_a <scalar_int_mode> (mode, &int_mode))
    3695              :     {
    3696            0 :       temp = expand_ffs (int_mode, op0, target);
    3697            0 :       if (temp)
    3698              :         return temp;
    3699              :     }
    3700              : 
    3701              :   /* Try implementing ctz (x) in terms of clz (x).  */
    3702          971 :   if (unoptab == ctz_optab && is_a <scalar_int_mode> (mode, &int_mode))
    3703              :     {
    3704           48 :       temp = expand_ctz (int_mode, op0, target);
    3705           48 :       if (temp)
    3706              :         return temp;
    3707              :     }
    3708              : 
    3709          971 :   if ((unoptab == ctz_optab || unoptab == ffs_optab)
    3710           48 :       && optimize_insn_for_speed_p ()
    3711           45 :       && is_a <scalar_int_mode> (mode, &int_mode)
    3712           90 :       && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
    3713         1016 :       && (optab_handler (unoptab, word_mode) != CODE_FOR_nothing
    3714            0 :           || optab_handler (ctz_optab, word_mode) != CODE_FOR_nothing))
    3715              :     {
    3716           45 :       temp = expand_doubleword_clz_ctz_ffs (int_mode, op0, target, unoptab);
    3717           45 :       if (temp)
    3718              :         return temp;
    3719              :     }
    3720              : 
    3721          930 :  try_libcall:
    3722              :   /* Now try a library call in this mode.  */
    3723          930 :   libfunc = optab_libfunc (unoptab, mode);
    3724          930 :   if (libfunc)
    3725              :     {
    3726          596 :       rtx_insn *insns;
    3727          596 :       rtx value;
    3728          596 :       rtx eq_value;
    3729          596 :       machine_mode outmode = mode;
    3730              : 
    3731              :       /* All of these functions return small values.  Thus we choose to
    3732              :          have them return something that isn't a double-word.  */
    3733          596 :       if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
    3734              :           || unoptab == clrsb_optab || unoptab == popcount_optab
    3735          596 :           || unoptab == parity_optab)
    3736          502 :         outmode
    3737          502 :           = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
    3738              :                                           optab_libfunc (unoptab, mode)));
    3739              : 
    3740          596 :       start_sequence ();
    3741              : 
    3742              :       /* Pass 1 for NO_QUEUE so we don't lose any increments
    3743              :          if the libcall is cse'd or moved.  */
    3744          596 :       value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
    3745              :                                        op0, mode);
    3746          596 :       insns = end_sequence ();
    3747              : 
    3748          596 :       target = gen_reg_rtx (outmode);
    3749          596 :       bool trapv = trapv_unoptab_p (unoptab);
    3750          596 :       if (trapv)
    3751              :         eq_value = NULL_RTX;
    3752              :       else
    3753              :         {
    3754          502 :           eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
    3755         1506 :           if (GET_MODE_UNIT_SIZE (outmode) < GET_MODE_UNIT_SIZE (mode))
    3756          472 :             eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
    3757           90 :           else if (GET_MODE_UNIT_SIZE (outmode) > GET_MODE_UNIT_SIZE (mode))
    3758            0 :             eq_value = simplify_gen_unary (ZERO_EXTEND,
    3759              :                                            outmode, eq_value, mode);
    3760              :         }
    3761          596 :       emit_libcall_block_1 (insns, target, value, eq_value, trapv);
    3762              : 
    3763          596 :       return target;
    3764              :     }
    3765              : 
    3766              :   /* It can't be done in this mode.  Can we do it in a wider mode?  */
    3767              : 
    3768          334 :   if (CLASS_HAS_WIDER_MODES_P (mclass))
    3769              :     {
    3770          685 :       FOR_EACH_WIDER_MODE (wider_mode, mode)
    3771              :         {
    3772          597 :           if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
    3773          597 :               || optab_libfunc (unoptab, wider_mode))
    3774              :             {
    3775          246 :               rtx xop0 = op0;
    3776          246 :               rtx_insn *last = get_last_insn ();
    3777              : 
    3778              :               /* For certain operations, we need not actually extend
    3779              :                  the narrow operand, as long as we will truncate the
    3780              :                  results to the same narrowness.  */
    3781          246 :               xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
    3782          246 :                                     (unoptab == neg_optab
    3783          246 :                                      || unoptab == one_cmpl_optab
    3784          246 :                                      || unoptab == bswap_optab)
    3785              :                                     && mclass == MODE_INT);
    3786              : 
    3787          246 :               temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
    3788              :                                   unsignedp);
    3789              : 
    3790              :               /* If we are generating clz using wider mode, adjust the
    3791              :                  result.  Similarly for clrsb.  */
    3792          246 :               if ((unoptab == clz_optab || unoptab == clrsb_optab)
    3793            1 :                   && temp != 0)
    3794              :                 {
    3795            1 :                   scalar_int_mode wider_int_mode
    3796            1 :                     = as_a <scalar_int_mode> (wider_mode);
    3797            1 :                   int_mode = as_a <scalar_int_mode> (mode);
    3798            1 :                   temp = expand_binop
    3799            1 :                     (wider_mode, sub_optab, temp,
    3800            1 :                      gen_int_mode (GET_MODE_PRECISION (wider_int_mode)
    3801            2 :                                    - GET_MODE_PRECISION (int_mode),
    3802              :                                    wider_int_mode),
    3803              :                      target, true, OPTAB_DIRECT);
    3804              :                 }
    3805              : 
    3806              :               /* Likewise for bswap.  */
    3807          246 :               if (unoptab == bswap_optab && temp != 0)
    3808              :                 {
    3809            0 :                   scalar_int_mode wider_int_mode
    3810            0 :                     = as_a <scalar_int_mode> (wider_mode);
    3811            0 :                   int_mode = as_a <scalar_int_mode> (mode);
    3812            0 :                   gcc_assert (GET_MODE_PRECISION (wider_int_mode)
    3813              :                               == GET_MODE_BITSIZE (wider_int_mode)
    3814              :                               && GET_MODE_PRECISION (int_mode)
    3815              :                                  == GET_MODE_BITSIZE (int_mode));
    3816              : 
    3817            0 :                   temp = expand_shift (RSHIFT_EXPR, wider_int_mode, temp,
    3818            0 :                                        GET_MODE_BITSIZE (wider_int_mode)
    3819            0 :                                        - GET_MODE_BITSIZE (int_mode),
    3820              :                                        NULL_RTX, true);
    3821              :                 }
    3822              : 
    3823          246 :               if (temp)
    3824              :                 {
    3825          246 :                   if (mclass != MODE_INT)
    3826              :                     {
    3827            0 :                       if (target == 0)
    3828            0 :                         target = gen_reg_rtx (mode);
    3829            0 :                       convert_move (target, temp, 0);
    3830            0 :                       return target;
    3831              :                     }
    3832              :                   else
    3833          246 :                     return gen_lowpart (mode, temp);
    3834              :                 }
    3835              :               else
    3836            0 :                 delete_insns_since (last);
    3837              :             }
    3838              :         }
    3839              :     }
    3840              : 
    3841              :   /* One final attempt at implementing negation via subtraction,
    3842              :      this time allowing widening of the operand.  */
    3843           88 :   if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
    3844              :     {
    3845            0 :       rtx temp;
    3846            0 :       temp = expand_binop (mode,
    3847              :                            unoptab == negv_optab ? subv_optab : sub_optab,
    3848              :                            CONST0_RTX (mode), op0,
    3849              :                            target, unsignedp, OPTAB_LIB_WIDEN);
    3850            0 :       if (temp)
    3851              :         return temp;
    3852              :     }
    3853              : 
    3854              :   return 0;
    3855              : }
    3856              : 
    3857              : /* Emit code to compute the absolute value of OP0, with result to
    3858              :    TARGET if convenient.  (TARGET may be 0.)  The return value says
    3859              :    where the result actually is to be found.
    3860              : 
    3861              :    MODE is the mode of the operand; the mode of the result is
    3862              :    different but can be deduced from MODE.
    3863              : 
    3864              :  */
    3865              : 
    3866              : rtx
    3867        28512 : expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
    3868              :                    int result_unsignedp)
    3869              : {
    3870        28512 :   rtx temp;
    3871              : 
    3872        28512 :   if (GET_MODE_CLASS (mode) != MODE_INT
    3873         5063 :       || ! flag_trapv)
    3874              :     result_unsignedp = 1;
    3875              : 
    3876              :   /* First try to do it with a special abs instruction.  */
    3877        28600 :   temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
    3878              :                       op0, target, 0);
    3879        28512 :   if (temp != 0)
    3880              :     return temp;
    3881              : 
    3882              :   /* If we have a MAX insn, we can do this as MAX (x, -x).  */
    3883           88 :   if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
    3884           88 :       && !HONOR_SIGNED_ZEROS (mode))
    3885              :     {
    3886           88 :       rtx_insn *last = get_last_insn ();
    3887              : 
    3888          176 :       temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
    3889              :                           op0, NULL_RTX, 0);
    3890           88 :       if (temp != 0)
    3891           88 :         temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
    3892              :                              OPTAB_WIDEN);
    3893              : 
    3894           88 :       if (temp != 0)
    3895              :         return temp;
    3896              : 
    3897            0 :       delete_insns_since (last);
    3898              :     }
    3899              : 
    3900              :   /* If this machine has expensive jumps, we can do integer absolute
    3901              :      value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
    3902              :      where W is the width of MODE.  */
    3903              : 
    3904            0 :   scalar_int_mode int_mode;
    3905            0 :   if (is_int_mode (mode, &int_mode)
    3906            0 :       && BRANCH_COST (optimize_insn_for_speed_p (),
    3907              :                       false) >= 2)
    3908              :     {
    3909            0 :       rtx extended = expand_shift (RSHIFT_EXPR, int_mode, op0,
    3910            0 :                                    GET_MODE_PRECISION (int_mode) - 1,
    3911              :                                    NULL_RTX, 0);
    3912              : 
    3913            0 :       temp = expand_binop (int_mode, xor_optab, extended, op0, target, 0,
    3914              :                            OPTAB_LIB_WIDEN);
    3915            0 :       if (temp != 0)
    3916            0 :         temp = expand_binop (int_mode,
    3917              :                              result_unsignedp ? sub_optab : subv_optab,
    3918              :                              temp, extended, target, 0, OPTAB_LIB_WIDEN);
    3919              : 
    3920            0 :       if (temp != 0)
    3921              :         return temp;
    3922              :     }
    3923              : 
    3924              :   return NULL_RTX;
    3925              : }
    3926              : 
    3927              : rtx
    3928        28488 : expand_abs (machine_mode mode, rtx op0, rtx target,
    3929              :             int result_unsignedp, int safe)
    3930              : {
    3931        28488 :   rtx temp;
    3932        28488 :   rtx_code_label *op1;
    3933              : 
    3934        28488 :   if (GET_MODE_CLASS (mode) != MODE_INT
    3935         5039 :       || ! flag_trapv)
    3936        28400 :     result_unsignedp = 1;
    3937              : 
    3938        28488 :   temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
    3939        28488 :   if (temp != 0)
    3940              :     return temp;
    3941              : 
    3942              :   /* If that does not win, use conditional jump and negate.  */
    3943              : 
    3944              :   /* It is safe to use the target if it is the same
    3945              :      as the source if this is also a pseudo register */
    3946            0 :   if (op0 == target && REG_P (op0)
    3947            0 :       && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
    3948              :     safe = 1;
    3949              : 
    3950            0 :   op1 = gen_label_rtx ();
    3951            0 :   if (target == 0 || ! safe
    3952            0 :       || GET_MODE (target) != mode
    3953            0 :       || (MEM_P (target) && MEM_VOLATILE_P (target))
    3954            0 :       || (REG_P (target)
    3955            0 :           && REGNO (target) < FIRST_PSEUDO_REGISTER))
    3956            0 :     target = gen_reg_rtx (mode);
    3957              : 
    3958            0 :   emit_move_insn (target, op0);
    3959            0 :   NO_DEFER_POP;
    3960              : 
    3961            0 :   do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
    3962              :                            NULL_RTX, NULL, op1,
    3963              :                            profile_probability::uninitialized ());
    3964              : 
    3965            0 :   op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
    3966              :                      target, target, 0);
    3967            0 :   if (op0 != target)
    3968            0 :     emit_move_insn (target, op0);
    3969            0 :   emit_label (op1);
    3970            0 :   OK_DEFER_POP;
    3971            0 :   return target;
    3972              : }
    3973              : 
    3974              : /* Emit code to compute the one's complement absolute value of OP0
    3975              :    (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
    3976              :    (TARGET may be NULL_RTX.)  The return value says where the result
    3977              :    actually is to be found.
    3978              : 
    3979              :    MODE is the mode of the operand; the mode of the result is
    3980              :    different but can be deduced from MODE.  */
    3981              : 
    3982              : rtx
    3983            0 : expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
    3984              : {
    3985            0 :   rtx temp;
    3986              : 
    3987              :   /* Not applicable for floating point modes.  */
    3988            0 :   if (FLOAT_MODE_P (mode))
    3989              :     return NULL_RTX;
    3990              : 
    3991              :   /* If we have a MAX insn, we can do this as MAX (x, ~x).  */
    3992            0 :   if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
    3993              :     {
    3994            0 :       rtx_insn *last = get_last_insn ();
    3995              : 
    3996            0 :       temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
    3997            0 :       if (temp != 0)
    3998            0 :         temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
    3999              :                              OPTAB_WIDEN);
    4000              : 
    4001            0 :       if (temp != 0)
    4002              :         return temp;
    4003              : 
    4004            0 :       delete_insns_since (last);
    4005              :     }
    4006              : 
    4007              :   /* If this machine has expensive jumps, we can do one's complement
    4008              :      absolute value of X as (((signed) x >> (W-1)) ^ x).  */
    4009              : 
    4010            0 :   scalar_int_mode int_mode;
    4011            0 :   if (is_int_mode (mode, &int_mode)
    4012            0 :       && BRANCH_COST (optimize_insn_for_speed_p (),
    4013              :                      false) >= 2)
    4014              :     {
    4015            0 :       rtx extended = expand_shift (RSHIFT_EXPR, int_mode, op0,
    4016            0 :                                    GET_MODE_PRECISION (int_mode) - 1,
    4017              :                                    NULL_RTX, 0);
    4018              : 
    4019            0 :       temp = expand_binop (int_mode, xor_optab, extended, op0, target, 0,
    4020              :                            OPTAB_LIB_WIDEN);
    4021              : 
    4022            0 :       if (temp != 0)
    4023              :         return temp;
    4024              :     }
    4025              : 
    4026              :   return NULL_RTX;
    4027              : }
    4028              : 
    4029              : /* A subroutine of expand_copysign, perform the copysign operation using the
    4030              :    abs and neg primitives advertised to exist on the target.  The assumption
    4031              :    is that we have a split register file, and leaving op0 in fp registers,
    4032              :    and not playing with subregs so much, will help the register allocator.  */
    4033              : 
    4034              : static rtx
    4035        11608 : expand_copysign_absneg (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
    4036              :                         int bitpos, bool op0_is_abs)
    4037              : {
    4038        11608 :   scalar_int_mode imode;
    4039        11608 :   enum insn_code icode;
    4040        11608 :   rtx sign;
    4041        11608 :   rtx_code_label *label;
    4042              : 
    4043        11608 :   if (target == op1)
    4044          132 :     target = NULL_RTX;
    4045              : 
    4046              :   /* Check if the back end provides an insn that handles signbit for the
    4047              :      argument's mode. */
    4048        11608 :   icode = optab_handler (signbit_optab, mode);
    4049        11608 :   if (icode != CODE_FOR_nothing)
    4050              :     {
    4051        11568 :       imode = as_a <scalar_int_mode> (insn_data[(int) icode].operand[0].mode);
    4052        11568 :       sign = gen_reg_rtx (imode);
    4053        11568 :       emit_unop_insn (icode, sign, op1, UNKNOWN);
    4054              :     }
    4055              :   else
    4056              :     {
    4057          100 :       if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
    4058              :         {
    4059           40 :           if (!int_mode_for_mode (mode).exists (&imode))
    4060            0 :             return NULL_RTX;
    4061           40 :           op1 = gen_lowpart (imode, op1);
    4062              :         }
    4063              :       else
    4064              :         {
    4065            0 :           int word;
    4066              : 
    4067            0 :           imode = word_mode;
    4068            0 :           if (FLOAT_WORDS_BIG_ENDIAN)
    4069              :             word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
    4070              :           else
    4071            0 :             word = bitpos / BITS_PER_WORD;
    4072            0 :           bitpos = bitpos % BITS_PER_WORD;
    4073            0 :           op1 = operand_subword_force (op1, word, mode);
    4074              :         }
    4075              : 
    4076           40 :       wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
    4077           80 :       sign = expand_binop (imode, and_optab, op1,
    4078           80 :                            immed_wide_int_const (mask, imode),
    4079              :                            NULL_RTX, 1, OPTAB_LIB_WIDEN);
    4080           40 :     }
    4081              : 
    4082        11608 :   if (!op0_is_abs)
    4083              :     {
    4084          154 :       op0 = expand_unop (mode, abs_optab, op0, target, 0);
    4085          154 :       if (op0 == NULL)
    4086              :         return NULL_RTX;
    4087              :       target = op0;
    4088              :     }
    4089              :   else
    4090              :     {
    4091        11454 :       if (target == NULL_RTX)
    4092           84 :         target = copy_to_reg (op0);
    4093              :       else
    4094        11370 :         emit_move_insn (target, op0);
    4095              :     }
    4096              : 
    4097        11608 :   label = gen_label_rtx ();
    4098        11608 :   emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
    4099              : 
    4100        11608 :   if (CONST_DOUBLE_AS_FLOAT_P (op0))
    4101        11454 :     op0 = simplify_unary_operation (NEG, mode, op0, mode);
    4102              :   else
    4103          154 :     op0 = expand_unop (mode, neg_optab, op0, target, 0);
    4104        11608 :   if (op0 != target)
    4105        11454 :     emit_move_insn (target, op0);
    4106              : 
    4107        11608 :   emit_label (label);
    4108              : 
    4109        11608 :   return target;
    4110              : }
    4111              : 
    4112              : 
    4113              : /* A subroutine of expand_copysign, perform the entire copysign operation
    4114              :    with integer bitmasks.  BITPOS is the position of the sign bit; OP0_IS_ABS
    4115              :    is true if op0 is known to have its sign bit clear.  */
    4116              : 
    4117              : static rtx
    4118           68 : expand_copysign_bit (scalar_float_mode mode, rtx op0, rtx op1, rtx target,
    4119              :                      int bitpos, bool op0_is_abs)
    4120              : {
    4121           68 :   scalar_int_mode imode;
    4122           68 :   int word, nwords, i;
    4123           68 :   rtx temp;
    4124           68 :   rtx_insn *insns;
    4125              : 
    4126          156 :   if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
    4127              :     {
    4128           68 :       if (!int_mode_for_mode (mode).exists (&imode))
    4129            0 :         return NULL_RTX;
    4130              :       word = 0;
    4131              :       nwords = 1;
    4132              :     }
    4133              :   else
    4134              :     {
    4135            0 :       imode = word_mode;
    4136              : 
    4137            0 :       if (FLOAT_WORDS_BIG_ENDIAN)
    4138              :         word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
    4139              :       else
    4140            0 :         word = bitpos / BITS_PER_WORD;
    4141            0 :       bitpos = bitpos % BITS_PER_WORD;
    4142            0 :       nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
    4143              :     }
    4144              : 
    4145           68 :   wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
    4146              : 
    4147           68 :   if (target == 0
    4148           68 :       || target == op0
    4149           68 :       || target == op1
    4150           64 :       || reg_overlap_mentioned_p (target, op0)
    4151           64 :       || reg_overlap_mentioned_p (target, op1)
    4152          132 :       || (nwords > 1 && !valid_multiword_target_p (target)))
    4153            4 :     target = gen_reg_rtx (mode);
    4154              : 
    4155           68 :   if (nwords > 1)
    4156              :     {
    4157            0 :       start_sequence ();
    4158              : 
    4159            0 :       for (i = 0; i < nwords; ++i)
    4160              :         {
    4161            0 :           rtx targ_piece = operand_subword (target, i, 1, mode);
    4162            0 :           rtx op0_piece = operand_subword_force (op0, i, mode);
    4163              : 
    4164            0 :           if (i == word)
    4165              :             {
    4166            0 :               if (!op0_is_abs)
    4167            0 :                 op0_piece
    4168            0 :                   = expand_binop (imode, and_optab, op0_piece,
    4169            0 :                                   immed_wide_int_const (~mask, imode),
    4170              :                                   NULL_RTX, 1, OPTAB_LIB_WIDEN);
    4171            0 :               op1 = expand_binop (imode, and_optab,
    4172            0 :                                   operand_subword_force (op1, i, mode),
    4173            0 :                                   immed_wide_int_const (mask, imode),
    4174              :                                   NULL_RTX, 1, OPTAB_LIB_WIDEN);
    4175              : 
    4176            0 :               temp = expand_binop (imode, ior_optab, op0_piece, op1,
    4177              :                                    targ_piece, 1, OPTAB_LIB_WIDEN);
    4178            0 :               if (temp != targ_piece)
    4179            0 :                 emit_move_insn (targ_piece, temp);
    4180              :             }
    4181              :           else
    4182            0 :             emit_move_insn (targ_piece, op0_piece);
    4183              :         }
    4184              : 
    4185            0 :       insns = end_sequence ();
    4186              : 
    4187            0 :       emit_insn (insns);
    4188              :     }
    4189              :   else
    4190              :     {
    4191          136 :       op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
    4192           68 :                           immed_wide_int_const (mask, imode),
    4193              :                           NULL_RTX, 1, OPTAB_LIB_WIDEN);
    4194              : 
    4195           68 :       op0 = gen_lowpart (imode, op0);
    4196           68 :       if (!op0_is_abs)
    4197          136 :         op0 = expand_binop (imode, and_optab, op0,
    4198          136 :                             immed_wide_int_const (~mask, imode),
    4199              :                             NULL_RTX, 1, OPTAB_LIB_WIDEN);
    4200              : 
    4201           68 :       temp = expand_binop (imode, ior_optab, op0, op1,
    4202           68 :                            gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
    4203           68 :       target = force_lowpart_subreg (mode, temp, imode);
    4204              :     }
    4205              : 
    4206           68 :   return target;
    4207           68 : }
    4208              : 
    4209              : /* Expand the C99 copysign operation.  OP0 and OP1 must be the same
    4210              :    scalar floating point mode.  Return NULL if we do not know how to
    4211              :    expand the operation inline.  */
    4212              : 
    4213              : rtx
    4214        11676 : expand_copysign (rtx op0, rtx op1, rtx target)
    4215              : {
    4216        11676 :   scalar_float_mode mode;
    4217        11676 :   const struct real_format *fmt;
    4218        11676 :   bool op0_is_abs;
    4219        11676 :   rtx temp;
    4220              : 
    4221        11676 :   mode = as_a <scalar_float_mode> (GET_MODE (op0));
    4222        11676 :   gcc_assert (GET_MODE (op1) == mode);
    4223              : 
    4224              :   /* First try to do it with a special instruction.  */
    4225        11676 :   temp = expand_binop (mode, copysign_optab, op0, op1,
    4226              :                        target, 0, OPTAB_DIRECT);
    4227        11676 :   if (temp)
    4228              :     return temp;
    4229              : 
    4230        11676 :   fmt = REAL_MODE_FORMAT (mode);
    4231        11676 :   if (fmt == NULL || !fmt->has_signed_zero)
    4232              :     return NULL_RTX;
    4233              : 
    4234        11676 :   op0_is_abs = false;
    4235        11676 :   if (CONST_DOUBLE_AS_FLOAT_P (op0))
    4236              :     {
    4237        11454 :       if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
    4238            0 :         op0 = simplify_unary_operation (ABS, mode, op0, mode);
    4239              :       op0_is_abs = true;
    4240              :     }
    4241              : 
    4242        11676 :   if (fmt->signbit_ro >= 0
    4243        11676 :       && (CONST_DOUBLE_AS_FLOAT_P (op0)
    4244          222 :           || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
    4245          154 :               && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
    4246              :     {
    4247        23216 :       temp = expand_copysign_absneg (mode, op0, op1, target,
    4248        11608 :                                      fmt->signbit_ro, op0_is_abs);
    4249        11608 :       if (temp)
    4250              :         return temp;
    4251              :     }
    4252              : 
    4253           68 :   if (fmt->signbit_rw < 0)
    4254              :     return NULL_RTX;
    4255           68 :   return expand_copysign_bit (mode, op0, op1, target,
    4256           68 :                               fmt->signbit_rw, op0_is_abs);
    4257              : }
    4258              : 
    4259              : /* Generate an instruction whose insn-code is INSN_CODE,
    4260              :    with two operands: an output TARGET and an input OP0.
    4261              :    TARGET *must* be nonzero, and the output is always stored there.
    4262              :    CODE is an rtx code such that (CODE OP0) is an rtx that describes
    4263              :    the value that is stored into TARGET.
    4264              : 
    4265              :    Return false if expansion failed.  */
    4266              : 
    4267              : bool
    4268      2186862 : maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
    4269              :                       enum rtx_code code)
    4270              : {
    4271      2186862 :   class expand_operand ops[2];
    4272      2186862 :   rtx_insn *pat;
    4273              : 
    4274      2186862 :   create_output_operand (&ops[0], target, GET_MODE (target));
    4275      2186862 :   create_input_operand (&ops[1], op0, GET_MODE (op0));
    4276      2186862 :   pat = maybe_gen_insn (icode, 2, ops);
    4277      2186862 :   if (!pat)
    4278              :     return false;
    4279              : 
    4280      1238681 :   if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
    4281      2211341 :       && code != UNKNOWN)
    4282        12759 :     add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX,
    4283        12759 :                     GET_MODE (op0));
    4284              : 
    4285      2186861 :   emit_insn (pat);
    4286              : 
    4287      2186861 :   if (ops[0].value != target)
    4288        39485 :     emit_move_insn (target, ops[0].value);
    4289              :   return true;
    4290              : }
    4291              : /* Generate an instruction whose insn-code is INSN_CODE,
    4292              :    with two operands: an output TARGET and an input OP0.
    4293              :    TARGET *must* be nonzero, and the output is always stored there.
    4294              :    CODE is an rtx code such that (CODE OP0) is an rtx that describes
    4295              :    the value that is stored into TARGET.  */
    4296              : 
    4297              : void
    4298      2139714 : emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
    4299              : {
    4300      2139714 :   bool ok = maybe_emit_unop_insn (icode, target, op0, code);
    4301      2139714 :   gcc_assert (ok);
    4302      2139714 : }
    4303              : 
    4304              : struct no_conflict_data
    4305              : {
    4306              :   rtx target;
    4307              :   rtx_insn *first, *insn;
    4308              :   bool must_stay;
    4309              : };
    4310              : 
    4311              : /* Called via note_stores by emit_libcall_block.  Set P->must_stay if
    4312              :    the currently examined clobber / store has to stay in the list of
    4313              :    insns that constitute the actual libcall block.  */
    4314              : static void
    4315        57374 : no_conflict_move_test (rtx dest, const_rtx set, void *p0)
    4316              : {
    4317        57374 :   struct no_conflict_data *p= (struct no_conflict_data *) p0;
    4318              : 
    4319              :   /* If this inns directly contributes to setting the target, it must stay.  */
    4320        57374 :   if (reg_overlap_mentioned_p (p->target, dest))
    4321            0 :     p->must_stay = true;
    4322              :   /* If we haven't committed to keeping any other insns in the list yet,
    4323              :      there is nothing more to check.  */
    4324        57374 :   else if (p->insn == p->first)
    4325              :     return;
    4326              :   /* If this insn sets / clobbers a register that feeds one of the insns
    4327              :      already in the list, this insn has to stay too.  */
    4328        27021 :   else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
    4329        27021 :            || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
    4330        27021 :            || reg_used_between_p (dest, p->first, p->insn)
    4331              :            /* Likewise if this insn depends on a register set by a previous
    4332              :               insn in the list, or if it sets a result (presumably a hard
    4333              :               register) that is set or clobbered by a previous insn.
    4334              :               N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
    4335              :               SET_DEST perform the former check on the address, and the latter
    4336              :               check on the MEM.  */
    4337        54042 :            || (GET_CODE (set) == SET
    4338        27021 :                && (modified_in_p (SET_SRC (set), p->first)
    4339        27019 :                    || modified_in_p (SET_DEST (set), p->first)
    4340        27019 :                    || modified_between_p (SET_SRC (set), p->first, p->insn)
    4341        27019 :                    || modified_between_p (SET_DEST (set), p->first, p->insn))))
    4342            2 :     p->must_stay = true;
    4343              : }
    4344              : 
    4345              : 
    4346              : /* Emit code to make a call to a constant function or a library call.
    4347              : 
    4348              :    INSNS is a list containing all insns emitted in the call.
    4349              :    These insns leave the result in RESULT.  Our block is to copy RESULT
    4350              :    to TARGET, which is logically equivalent to EQUIV.
    4351              : 
    4352              :    We first emit any insns that set a pseudo on the assumption that these are
    4353              :    loading constants into registers; doing so allows them to be safely cse'ed
    4354              :    between blocks.  Then we emit all the other insns in the block, followed by
    4355              :    an insn to move RESULT to TARGET.  This last insn will have a REQ_EQUAL
    4356              :    note with an operand of EQUIV.  */
    4357              : 
    4358              : static void
    4359       123412 : emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
    4360              :                       bool equiv_may_trap)
    4361              : {
    4362       123412 :   rtx final_dest = target;
    4363       123412 :   rtx_insn *next, *last, *insn;
    4364              : 
    4365              :   /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
    4366              :      into a MEM later.  Protect the libcall block from this change.  */
    4367       123412 :   if (! REG_P (target) || REG_USERVAR_P (target))
    4368         1859 :     target = gen_reg_rtx (GET_MODE (target));
    4369              : 
    4370              :   /* If we're using non-call exceptions, a libcall corresponding to an
    4371              :      operation that may trap may also trap.  */
    4372              :   /* ??? See the comment in front of make_reg_eh_region_note.  */
    4373       123412 :   if (cfun->can_throw_non_call_exceptions
    4374       123412 :       && (equiv_may_trap || may_trap_p (equiv)))
    4375              :     {
    4376         1064 :       for (insn = insns; insn; insn = NEXT_INSN (insn))
    4377          855 :         if (CALL_P (insn))
    4378              :           {
    4379          209 :             rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
    4380          209 :             if (note)
    4381              :               {
    4382          209 :                 int lp_nr = INTVAL (XEXP (note, 0));
    4383          209 :                 if (lp_nr == 0 || lp_nr == INT_MIN)
    4384          209 :                   remove_note (insn, note);
    4385              :               }
    4386              :           }
    4387              :     }
    4388              :   else
    4389              :     {
    4390              :       /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
    4391              :          reg note to indicate that this call cannot throw or execute a nonlocal
    4392              :          goto (unless there is already a REG_EH_REGION note, in which case
    4393              :          we update it).  */
    4394       536640 :       for (insn = insns; insn; insn = NEXT_INSN (insn))
    4395       413437 :         if (CALL_P (insn))
    4396       123203 :           make_reg_eh_region_note_nothrow_nononlocal (insn);
    4397              :     }
    4398              : 
    4399              :   /* First emit all insns that set pseudos.  Remove them from the list as
    4400              :      we go.  Avoid insns that set pseudos which were referenced in previous
    4401              :      insns.  These can be generated by move_by_pieces, for example,
    4402              :      to update an address.  Similarly, avoid insns that reference things
    4403              :      set in previous insns.  */
    4404              : 
    4405       537704 :   for (insn = insns; insn; insn = next)
    4406              :     {
    4407       414292 :       rtx set = single_set (insn);
    4408              : 
    4409       414292 :       next = NEXT_INSN (insn);
    4410              : 
    4411       399766 :       if (set != 0 && REG_P (SET_DEST (set))
    4412       776712 :           && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
    4413              :         {
    4414        47283 :           struct no_conflict_data data;
    4415              : 
    4416        47283 :           data.target = const0_rtx;
    4417        47283 :           data.first = insns;
    4418        47283 :           data.insn = insn;
    4419        47283 :           data.must_stay = 0;
    4420        47283 :           note_stores (insn, no_conflict_move_test, &data);
    4421        47283 :           if (! data.must_stay)
    4422              :             {
    4423        47281 :               if (PREV_INSN (insn))
    4424        27019 :                 SET_NEXT_INSN (PREV_INSN (insn)) = next;
    4425              :               else
    4426              :                 insns = next;
    4427              : 
    4428        47281 :               if (next)
    4429        47281 :                 SET_PREV_INSN (next) = PREV_INSN (insn);
    4430              : 
    4431        47281 :               add_insn (insn);
    4432              :             }
    4433              :         }
    4434              : 
    4435              :       /* Some ports use a loop to copy large arguments onto the stack.
    4436              :          Don't move anything outside such a loop.  */
    4437       414292 :       if (LABEL_P (insn))
    4438              :         break;
    4439              :     }
    4440              : 
    4441              :   /* Write the remaining insns followed by the final copy.  */
    4442       490423 :   for (insn = insns; insn; insn = next)
    4443              :     {
    4444       367011 :       next = NEXT_INSN (insn);
    4445              : 
    4446       367011 :       add_insn (insn);
    4447              :     }
    4448              : 
    4449       123412 :   last = emit_move_insn (target, result);
    4450       123412 :   if (equiv)
    4451       122808 :     set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
    4452              : 
    4453       123412 :   if (final_dest != target)
    4454         1859 :     emit_move_insn (final_dest, target);
    4455       123412 : }
    4456              : 
    4457              : void
    4458        92066 : emit_libcall_block (rtx_insn *insns, rtx target, rtx result, rtx equiv)
    4459              : {
    4460        92066 :   emit_libcall_block_1 (insns, target, result, equiv, false);
    4461        92066 : }
    4462              : 
    4463              : /* True if we can perform a comparison of mode MODE straightforwardly.
    4464              :    PURPOSE describes how this comparison will be used.  CODE is the rtx
    4465              :    comparison code we will be using.
    4466              : 
    4467              :    ??? Actually, CODE is slightly weaker than that.  A target is still
    4468              :    required to implement all of the normal bcc operations, but not
    4469              :    required to implement all (or any) of the unordered bcc operations.  */
    4470              : 
    4471              : bool
    4472     18404571 : can_compare_p (enum rtx_code code, machine_mode mode,
    4473              :                enum can_compare_purpose purpose)
    4474              : {
    4475     18404571 :   rtx test;
    4476     18404571 :   test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
    4477     20497794 :   do
    4478              :     {
    4479     20497794 :       enum insn_code icode;
    4480              : 
    4481     20497794 :       if (purpose == ccp_jump
    4482     20294826 :           && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
    4483     39429843 :           && insn_operand_matches (icode, 0, test))
    4484              :         return true;
    4485      3392433 :       if (purpose == ccp_store_flag
    4486       202968 :           && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
    4487      3526391 :           && insn_operand_matches (icode, 1, test))
    4488              :         return true;
    4489              : 
    4490      3387972 :       mode = GET_MODE_WIDER_MODE (mode).else_void ();
    4491      3387972 :       PUT_MODE (test, mode);
    4492              :     }
    4493      3387972 :   while (mode != VOIDmode);
    4494              : 
    4495              :   return false;
    4496              : }
    4497              : 
    4498              : /* Return whether RTL code CODE corresponds to an unsigned optab.  */
    4499              : 
    4500              : static bool
    4501      1479574 : unsigned_optab_p (enum rtx_code code)
    4502              : {
    4503      1479574 :   return code == LTU || code == LEU || code == GTU || code == GEU;
    4504              : }
    4505              : 
    4506              : /* Return whether the backend-emitted comparison for code CODE, comparing
    4507              :    operands of mode VALUE_MODE and producing a result with MASK_MODE, matches
    4508              :    operand OPNO of pattern ICODE.  */
    4509              : 
    4510              : static bool
    4511      1067112 : insn_predicate_matches_p (enum insn_code icode, unsigned int opno,
    4512              :                           enum rtx_code code, machine_mode mask_mode,
    4513              :                           machine_mode value_mode)
    4514              : {
    4515      1067112 :   rtx reg1 = alloca_raw_REG (value_mode, LAST_VIRTUAL_REGISTER + 1);
    4516      1067112 :   rtx reg2 = alloca_raw_REG (value_mode, LAST_VIRTUAL_REGISTER + 2);
    4517      1067112 :   rtx test = alloca_rtx_fmt_ee (code, mask_mode, reg1, reg2);
    4518      1067112 :   return insn_operand_matches (icode, opno, test);
    4519              : }
    4520              : 
    4521              : /* Return whether the backend can emit a vector comparison (vec_cmp/vec_cmpu)
    4522              :    for code CODE, comparing operands of mode VALUE_MODE and producing a result
    4523              :    with MASK_MODE.  */
    4524              : 
    4525              : bool
    4526      1479574 : can_vec_cmp_compare_p (enum rtx_code code, machine_mode value_mode,
    4527              :                        machine_mode mask_mode)
    4528              : {
    4529      1479574 :   enum insn_code icode
    4530      1479574 :       = get_vec_cmp_icode (value_mode, mask_mode, unsigned_optab_p (code));
    4531      1479574 :   if (icode == CODE_FOR_nothing)
    4532              :     return false;
    4533              : 
    4534      1067112 :   return insn_predicate_matches_p (icode, 1, code, mask_mode, value_mode);
    4535              : }
    4536              : 
    4537              : /* Return whether the backend can emit vector set instructions for inserting
    4538              :    element into vector at variable index position.  */
    4539              : 
    4540              : bool
    4541          207 : can_vec_set_var_idx_p (machine_mode vec_mode)
    4542              : {
    4543          207 :   if (!VECTOR_MODE_P (vec_mode))
    4544              :     return false;
    4545              : 
    4546          205 :   machine_mode inner_mode = GET_MODE_INNER (vec_mode);
    4547              : 
    4548          205 :   rtx reg1 = alloca_raw_REG (vec_mode, LAST_VIRTUAL_REGISTER + 1);
    4549          205 :   rtx reg2 = alloca_raw_REG (inner_mode, LAST_VIRTUAL_REGISTER + 2);
    4550              : 
    4551          205 :   enum insn_code icode = optab_handler (vec_set_optab, vec_mode);
    4552              : 
    4553          205 :   const struct insn_data_d *data = &insn_data[icode];
    4554          205 :   machine_mode idx_mode = data->operand[2].mode;
    4555              : 
    4556          205 :   rtx reg3 = alloca_raw_REG (idx_mode, LAST_VIRTUAL_REGISTER + 3);
    4557              : 
    4558          204 :   return icode != CODE_FOR_nothing && insn_operand_matches (icode, 0, reg1)
    4559          204 :          && insn_operand_matches (icode, 1, reg2)
    4560          409 :          && insn_operand_matches (icode, 2, reg3);
    4561              : }
    4562              : 
    4563              : /* Return whether the backend can emit a vec_extract instruction with
    4564              :    a non-constant index.  */
    4565              : bool
    4566        21651 : can_vec_extract_var_idx_p (machine_mode vec_mode, machine_mode extr_mode)
    4567              : {
    4568        21651 :   if (!VECTOR_MODE_P (vec_mode))
    4569              :     return false;
    4570              : 
    4571        17030 :   rtx reg1 = alloca_raw_REG (extr_mode, LAST_VIRTUAL_REGISTER + 1);
    4572        17030 :   rtx reg2 = alloca_raw_REG (vec_mode, LAST_VIRTUAL_REGISTER + 2);
    4573              : 
    4574        17030 :   enum insn_code icode = convert_optab_handler (vec_extract_optab,
    4575              :                                                 vec_mode, extr_mode);
    4576              : 
    4577        17030 :   const struct insn_data_d *data = &insn_data[icode];
    4578        17030 :   machine_mode idx_mode = data->operand[2].mode;
    4579              : 
    4580        17030 :   rtx reg3 = alloca_raw_REG (idx_mode, LAST_VIRTUAL_REGISTER + 3);
    4581              : 
    4582        16159 :   return icode != CODE_FOR_nothing && insn_operand_matches (icode, 0, reg1)
    4583        16159 :          && insn_operand_matches (icode, 1, reg2)
    4584        33189 :          && insn_operand_matches (icode, 2, reg3);
    4585              : }
    4586              : 
    4587              : /* This function is called when we are going to emit a compare instruction that
    4588              :    compares the values found in X and Y, using the rtl operator COMPARISON.
    4589              : 
    4590              :    If they have mode BLKmode, then SIZE specifies the size of both operands.
    4591              : 
    4592              :    UNSIGNEDP nonzero says that the operands are unsigned;
    4593              :    this matters if they need to be widened (as given by METHODS).
    4594              : 
    4595              :    *PTEST is where the resulting comparison RTX is returned or NULL_RTX
    4596              :    if we failed to produce one.
    4597              : 
    4598              :    *PMODE is the mode of the inputs (in case they are const_int).
    4599              : 
    4600              :    *OPTAB is the optab to check for OPTAB_DIRECT support.  Defaults to
    4601              :    cbranch_optab.
    4602              : 
    4603              :    This function performs all the setup necessary so that the caller only has
    4604              :    to emit a single comparison insn.  This setup can involve doing a BLKmode
    4605              :    comparison or emitting a library call to perform the comparison if no insn
    4606              :    is available to handle it.
    4607              :    The values which are passed in through pointers can be modified; the caller
    4608              :    should perform the comparison on the modified values.  Constant
    4609              :    comparisons must have already been folded.  */
    4610              : 
    4611              : static void
    4612      7021766 : prepare_cmp_insn (rtx x, rtx y, rtx *mask, enum rtx_code comparison, rtx size,
    4613              :                   int unsignedp, enum optab_methods methods,
    4614              :                   rtx *ptest, machine_mode *pmode, optab optab)
    4615              : {
    4616      7021766 :   machine_mode mode = *pmode;
    4617      7021766 :   rtx libfunc, test;
    4618      7021766 :   machine_mode cmp_mode;
    4619              : 
    4620              :   /* The other methods are not needed.  */
    4621      7021766 :   gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
    4622              :               || methods == OPTAB_LIB_WIDEN);
    4623              : 
    4624      7021766 :   if (CONST_SCALAR_INT_P (y))
    4625      4550409 :     canonicalize_comparison (mode, &comparison, &y);
    4626              : 
    4627              :   /* If we are optimizing, force expensive constants into a register.  */
    4628            1 :   if (CONSTANT_P (x) && optimize
    4629            1 :       && (rtx_cost (x, mode, COMPARE, 0, optimize_insn_for_speed_p ())
    4630              :           > COSTS_N_INSNS (1))
    4631      7021766 :       && can_create_pseudo_p ())
    4632            0 :     x = force_reg (mode, x);
    4633              : 
    4634      5068854 :   if (CONSTANT_P (y) && optimize
    4635      4006779 :       && (rtx_cost (y, mode, COMPARE, 1, optimize_insn_for_speed_p ())
    4636              :           > COSTS_N_INSNS (1))
    4637      7190658 :       && can_create_pseudo_p ())
    4638       168892 :     y = force_reg (mode, y);
    4639              : 
    4640              :   /* Don't let both operands fail to indicate the mode.  */
    4641      7021766 :   if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
    4642            0 :     x = force_reg (mode, x);
    4643      7021766 :   if (mode == VOIDmode)
    4644         5187 :     mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
    4645              : 
    4646              :   /* Handle all BLKmode compares.  */
    4647              : 
    4648      7021766 :   if (mode == BLKmode)
    4649              :     {
    4650            0 :       machine_mode result_mode;
    4651            0 :       enum insn_code cmp_code;
    4652            0 :       rtx result;
    4653            0 :       rtx opalign
    4654            0 :         = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
    4655              : 
    4656            0 :       gcc_assert (size);
    4657              : 
    4658              :       /* Try to use a memory block compare insn.  Only cmpmem compares
    4659              :          the whole block.  cmpstr and cmpstrn stop at a null byte.  */
    4660            0 :       opt_scalar_int_mode cmp_mode_iter;
    4661            0 :       FOR_EACH_MODE_IN_CLASS (cmp_mode_iter, MODE_INT)
    4662              :         {
    4663            0 :           scalar_int_mode cmp_mode = cmp_mode_iter.require ();
    4664            0 :           cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
    4665            0 :           if (cmp_code == CODE_FOR_nothing)
    4666            0 :             continue;
    4667              : 
    4668              :           /* Must make sure the size fits the insn's mode.  */
    4669            0 :           if (CONST_INT_P (size)
    4670            0 :               ? UINTVAL (size) > GET_MODE_MASK (cmp_mode)
    4671            0 :               : (GET_MODE_BITSIZE (as_a <scalar_int_mode> (GET_MODE (size)))
    4672            0 :                  > GET_MODE_BITSIZE (cmp_mode)))
    4673            0 :             continue;
    4674              : 
    4675            0 :           result_mode = insn_data[cmp_code].operand[0].mode;
    4676            0 :           result = gen_reg_rtx (result_mode);
    4677            0 :           size = convert_to_mode (cmp_mode, size, 1);
    4678            0 :           rtx_insn *insn = GEN_FCN (cmp_code) (result, x, y, size, opalign);
    4679            0 :           if (!insn)
    4680            0 :             continue;
    4681            0 :           emit_insn (insn);
    4682              : 
    4683            0 :           *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
    4684            0 :           *pmode = result_mode;
    4685            0 :           return;
    4686              :         }
    4687              : 
    4688            0 :       if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
    4689            0 :         goto fail;
    4690              : 
    4691              :       /* Otherwise call a library function.  */
    4692            0 :       result = emit_block_comp_via_libcall (x, y, size);
    4693              : 
    4694            0 :       x = result;
    4695            0 :       y = const0_rtx;
    4696            0 :       mode = TYPE_MODE (integer_type_node);
    4697            0 :       methods = OPTAB_LIB_WIDEN;
    4698            0 :       unsignedp = false;
    4699              :     }
    4700              : 
    4701              :   /* Don't allow operands to the compare to trap, as that can put the
    4702              :      compare and branch in different basic blocks.  */
    4703      7021766 :   if (cfun->can_throw_non_call_exceptions)
    4704              :     {
    4705      1134629 :       if (!can_create_pseudo_p () && (may_trap_p (x) || may_trap_p (y)))
    4706            0 :         goto fail;
    4707      1134629 :       if (may_trap_p (x))
    4708        24750 :         x = copy_to_reg (x);
    4709      1134629 :       if (may_trap_p (y))
    4710         1534 :         y = copy_to_reg (y);
    4711              :     }
    4712              : 
    4713      7021766 :   if (GET_MODE_CLASS (mode) == MODE_CC)
    4714              :     {
    4715          436 :       enum insn_code icode = optab_handler (cbranch_optab, CCmode);
    4716          436 :       test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
    4717          436 :       if (icode != CODE_FOR_nothing
    4718          436 :           && insn_operand_matches (icode, 0, test))
    4719              :         {
    4720          436 :           *ptest = test;
    4721          436 :           return;
    4722              :         }
    4723              :       else
    4724            0 :         goto fail;
    4725              :     }
    4726              : 
    4727      7021330 :   test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
    4728      7109781 :   FOR_EACH_WIDER_MODE_FROM (cmp_mode, mode)
    4729              :     {
    4730      7061771 :       enum insn_code icode;
    4731      7061771 :       icode = optab_handler (optab, cmp_mode);
    4732      7061771 :       if (icode != CODE_FOR_nothing
    4733      7061771 :           && insn_operand_matches (icode, 0, test))
    4734              :         {
    4735      6973320 :           rtx_insn *last = get_last_insn ();
    4736      6973320 :           rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
    4737      6973320 :           rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
    4738      6973320 :           if (op0 && op1
    4739      6973320 :               && insn_operand_matches (icode, 1, op0)
    4740     13946640 :               && insn_operand_matches (icode, 2, op1))
    4741              :             {
    4742      6973320 :               XEXP (test, 0) = op0;
    4743      6973320 :               XEXP (test, 1) = op1;
    4744      6973320 :               *ptest = test;
    4745      6973320 :               *pmode = cmp_mode;
    4746      6973320 :               return;
    4747              :             }
    4748            0 :           delete_insns_since (last);
    4749              :         }
    4750              : 
    4751        88451 :       if (methods == OPTAB_DIRECT)
    4752              :         break;
    4753              :     }
    4754              : 
    4755        48010 :   if (methods != OPTAB_LIB_WIDEN)
    4756         3030 :     goto fail;
    4757              : 
    4758        44980 :   if (SCALAR_FLOAT_MODE_P (mode))
    4759              :     {
    4760              :       /* Small trick if UNORDERED isn't implemented by the hardware.  */
    4761        44980 :       if (comparison == UNORDERED && rtx_equal_p (x, y))
    4762              :         {
    4763          773 :           prepare_cmp_insn (x, y, mask, UNLT, NULL_RTX, unsignedp, OPTAB_WIDEN,
    4764              :                             ptest, pmode, optab);
    4765          773 :           if (*ptest)
    4766              :             return;
    4767              :         }
    4768              : 
    4769        44980 :       prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
    4770              :     }
    4771              :   else
    4772              :     {
    4773            0 :       rtx result;
    4774            0 :       machine_mode ret_mode;
    4775              : 
    4776              :       /* Handle a libcall just for the mode we are using.  */
    4777            0 :       libfunc = optab_libfunc (cmp_optab, mode);
    4778            0 :       gcc_assert (libfunc);
    4779              : 
    4780              :       /* If we want unsigned, and this mode has a distinct unsigned
    4781              :          comparison routine, use that.  */
    4782            0 :       if (unsignedp)
    4783              :         {
    4784            0 :           rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
    4785            0 :           if (ulibfunc)
    4786            0 :             libfunc = ulibfunc;
    4787              :         }
    4788              : 
    4789            0 :       ret_mode = targetm.libgcc_cmp_return_mode ();
    4790            0 :       result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
    4791              :                                         ret_mode, x, mode, y, mode);
    4792              : 
    4793              :       /* There are two kinds of comparison routines. Biased routines
    4794              :          return 0/1/2, and unbiased routines return -1/0/1. Other parts
    4795              :          of gcc expect that the comparison operation is equivalent
    4796              :          to the modified comparison. For signed comparisons compare the
    4797              :          result against 1 in the biased case, and zero in the unbiased
    4798              :          case. For unsigned comparisons always compare against 1 after
    4799              :          biasing the unbiased result by adding 1. This gives us a way to
    4800              :          represent LTU.
    4801              :          The comparisons in the fixed-point helper library are always
    4802              :          biased.  */
    4803            0 :       x = result;
    4804            0 :       y = const1_rtx;
    4805              : 
    4806            0 :       if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
    4807              :         {
    4808              :           if (unsignedp)
    4809              :             x = plus_constant (ret_mode, result, 1);
    4810              :           else
    4811              :             y = const0_rtx;
    4812              :         }
    4813              : 
    4814            0 :       *pmode = ret_mode;
    4815            0 :       prepare_cmp_insn (x, y, mask, comparison, NULL_RTX, unsignedp, methods,
    4816              :                         ptest, pmode, optab);
    4817              :     }
    4818              : 
    4819              :   return;
    4820              : 
    4821         3030 :  fail:
    4822         3030 :   *ptest = NULL_RTX;
    4823              : }
    4824              : 
    4825              : /* Before emitting an insn with code ICODE, make sure that X, which is going
    4826              :    to be used for operand OPNUM of the insn, is converted from mode MODE to
    4827              :    WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
    4828              :    that it is accepted by the operand predicate.  Return the new value.  */
    4829              : 
    4830              : rtx
    4831     15469976 : prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
    4832              :                  machine_mode wider_mode, int unsignedp)
    4833              : {
    4834     15469976 :   if (mode != wider_mode)
    4835        10852 :     x = convert_modes (wider_mode, mode, x, unsignedp);
    4836              : 
    4837     15469976 :   if (!insn_operand_matches (icode, opnum, x))
    4838              :     {
    4839        64475 :       machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
    4840        64475 :       if (reload_completed)
    4841              :         return NULL_RTX;
    4842        64475 :       if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
    4843              :         return NULL_RTX;
    4844        64191 :       x = copy_to_mode_reg (op_mode, x);
    4845              :     }
    4846              : 
    4847              :   return x;
    4848              : }
    4849              : 
    4850              : /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
    4851              :    we can do the branch.  */
    4852              : 
    4853              : static void
    4854      6505290 : emit_cmp_and_jump_insn_1 (rtx test, rtx cond, rtx len, rtx bias,
    4855              :                           machine_mode mode, rtx label, direct_optab cmp_optab,
    4856              :                           profile_probability prob, bool test_branch)
    4857              : {
    4858      6505290 :   machine_mode optab_mode;
    4859      6505290 :   enum mode_class mclass;
    4860      6505290 :   enum insn_code icode;
    4861      6505290 :   rtx_insn *insn;
    4862              : 
    4863      6505290 :   mclass = GET_MODE_CLASS (mode);
    4864      6505290 :   optab_mode = (mclass == MODE_CC) ? CCmode : mode;
    4865      6505290 :   icode = optab_handler (cmp_optab, optab_mode);
    4866              : 
    4867      6505290 :   gcc_assert (icode != CODE_FOR_nothing);
    4868      6505290 :   gcc_assert (test_branch || insn_operand_matches (icode, 0, test));
    4869      6505290 :   gcc_assert (cond == NULL_RTX || (cond != NULL_RTX && !test_branch));
    4870      6505272 :   if (test_branch)
    4871            0 :     insn = emit_jump_insn (GEN_FCN (icode) (XEXP (test, 0),
    4872            0 :                                             XEXP (test, 1), label));
    4873      6505290 :   else if (len)
    4874              :     {
    4875            0 :       gcc_assert (cond);
    4876            0 :       gcc_assert (bias);
    4877            0 :       insn = emit_jump_insn (GEN_FCN (icode) (test, cond, XEXP (test, 0),
    4878            0 :                                               XEXP (test, 1), len, bias,
    4879              :                                               label));
    4880              :     }
    4881      6505290 :   else if (cond)
    4882           18 :     insn = emit_jump_insn (GEN_FCN (icode) (test, cond, XEXP (test, 0),
    4883           18 :                                             XEXP (test, 1), label));
    4884              :   else
    4885      6505272 :     insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
    4886      6505272 :                                             XEXP (test, 1), label));
    4887              : 
    4888      6505290 :   if (prob.initialized_p ()
    4889      4936727 :       && profile_status_for_fn (cfun) != PROFILE_ABSENT
    4890      4828263 :       && insn
    4891      4828263 :       && JUMP_P (insn)
    4892      4827375 :       && any_condjump_p (insn)
    4893     11332665 :       && !find_reg_note (insn, REG_BR_PROB, 0))
    4894      4825903 :     add_reg_br_prob_note (insn, prob);
    4895      6505290 : }
    4896              : 
    4897              : /* PTEST points to a comparison that compares its first operand with zero.
    4898              :    Check to see if it can be performed as a bit-test-and-branch instead.
    4899              :    On success, return the instruction that performs the bit-test-and-branch
    4900              :    and replace the second operand of *PTEST with the bit number to test.
    4901              :    On failure, return CODE_FOR_nothing and leave *PTEST unchanged.
    4902              : 
    4903              :    Note that the comparison described by *PTEST should not be taken
    4904              :    literally after a successful return.  *PTEST is just a convenient
    4905              :    place to store the two operands of the bit-and-test.
    4906              : 
    4907              :    VAL must contain the original tree expression for the first operand
    4908              :    of *PTEST.  */
    4909              : 
    4910              : static enum insn_code
    4911      2670050 : validate_test_and_branch (tree val, rtx *ptest, machine_mode *pmode, optab *res)
    4912              : {
    4913      2670050 :   if (!val || TREE_CODE (val) != SSA_NAME)
    4914              :     return CODE_FOR_nothing;
    4915              : 
    4916      2565859 :   machine_mode mode = TYPE_MODE (TREE_TYPE (val));
    4917      2565859 :   rtx test = *ptest;
    4918      2565859 :   direct_optab optab;
    4919              : 
    4920      2565859 :   if (GET_CODE (test) == EQ)
    4921              :     optab = tbranch_eq_optab;
    4922      1336510 :   else if (GET_CODE (test) == NE)
    4923              :     optab = tbranch_ne_optab;
    4924              :   else
    4925              :     return CODE_FOR_nothing;
    4926              : 
    4927      2329077 :   *res = optab;
    4928              : 
    4929              :   /* If the target supports the testbit comparison directly, great.  */
    4930      2329077 :   auto icode = direct_optab_handler (optab, mode);
    4931      2329077 :   if (icode == CODE_FOR_nothing)
    4932              :     return icode;
    4933              : 
    4934            0 :   if (tree_zero_one_valued_p (val))
    4935              :     {
    4936            0 :       auto pos = BITS_BIG_ENDIAN ? GET_MODE_BITSIZE (mode) - 1 : 0;
    4937            0 :       XEXP (test, 1) = gen_int_mode (pos, mode);
    4938            0 :       *ptest = test;
    4939            0 :       *pmode = mode;
    4940            0 :       return icode;
    4941              :     }
    4942              : 
    4943            0 :   wide_int wcst = get_nonzero_bits (val);
    4944            0 :   if (wcst == -1)
    4945              :     return CODE_FOR_nothing;
    4946              : 
    4947            0 :   int bitpos;
    4948              : 
    4949            0 :   if ((bitpos = wi::exact_log2 (wcst)) == -1)
    4950              :     return CODE_FOR_nothing;
    4951              : 
    4952            0 :   auto pos = BITS_BIG_ENDIAN ? GET_MODE_BITSIZE (mode) - 1 - bitpos : bitpos;
    4953            0 :   XEXP (test, 1) = gen_int_mode (pos, mode);
    4954            0 :   *ptest = test;
    4955            0 :   *pmode = mode;
    4956            0 :   return icode;
    4957            0 : }
    4958              : 
    4959              : /* Generate code to compare X with Y so that the condition codes are
    4960              :    set and to jump to LABEL if the condition is true.  If X is a
    4961              :    constant and Y is not a constant, then the comparison is swapped to
    4962              :    ensure that the comparison RTL has the canonical form.
    4963              : 
    4964              :    UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
    4965              :    need to be widened.  UNSIGNEDP is also used to select the proper
    4966              :    branch condition code.
    4967              : 
    4968              :    If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
    4969              : 
    4970              :    MODE is the mode of the inputs (in case they are const_int).
    4971              : 
    4972              :    COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
    4973              :    It will be potentially converted into an unsigned variant based on
    4974              :    UNSIGNEDP to select a proper jump instruction.
    4975              : 
    4976              :    PROB is the probability of jumping to LABEL.  If the comparison is against
    4977              :    zero then VAL contains the expression from which the non-zero RTL is
    4978              :    derived.  */
    4979              : 
    4980              : void
    4981      6505290 : emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
    4982              :                          machine_mode mode, int unsignedp, tree val, rtx label,
    4983              :                          profile_probability prob)
    4984              : {
    4985      6505290 :   rtx op0 = x, op1 = y;
    4986      6505290 :   rtx test;
    4987              : 
    4988              :   /* Swap operands and condition to ensure canonical RTL.  */
    4989      6505290 :   if (swap_commutative_operands_p (x, y)
    4990      6505290 :       && can_compare_p (swap_condition (comparison), mode, ccp_jump))
    4991              :     {
    4992            0 :       op0 = y, op1 = x;
    4993            0 :       comparison = swap_condition (comparison);
    4994              :     }
    4995              : 
    4996              :   /* If OP0 is still a constant, then both X and Y must be constants
    4997              :      or the opposite comparison is not supported.  Force X into a register
    4998              :      to create canonical RTL.  */
    4999      6505290 :   if (CONSTANT_P (op0))
    5000        20263 :     op0 = force_reg (mode, op0);
    5001              : 
    5002      6505290 :   if (unsignedp)
    5003      3869827 :     comparison = unsigned_condition (comparison);
    5004              : 
    5005              :   /* cbranch is no longer preferred for vectors, so when using a vector mode
    5006              :      check vec_cbranch variants instead.  */
    5007      6505290 :   if (!VECTOR_MODE_P (GET_MODE (op0)))
    5008      6485794 :     prepare_cmp_insn (op0, op1, NULL, comparison, size, unsignedp,
    5009              :                       OPTAB_LIB_WIDEN, &test, &mode, cbranch_optab);
    5010              : 
    5011              :   /* Check if we're comparing a truth type with 0, and if so check if
    5012              :      the target supports tbranch.  */
    5013      6505290 :   machine_mode tmode = mode;
    5014      6505290 :   direct_optab optab;
    5015      6505290 :   if (op1 == CONST0_RTX (GET_MODE (op1)))
    5016              :     {
    5017      2671749 :       if (!VECTOR_MODE_P (GET_MODE (op1))
    5018      2670050 :           && validate_test_and_branch (val, &test, &tmode,
    5019              :                                        &optab) != CODE_FOR_nothing)
    5020              :         {
    5021            0 :           emit_cmp_and_jump_insn_1 (test, NULL_RTX, NULL_RTX, NULL_RTX, tmode,
    5022              :                                     label, optab, prob, true);
    5023           18 :           return;
    5024              :         }
    5025              : 
    5026              :       /* If we are comparing equality with 0, check if VAL is another equality
    5027              :          comparison and if the target supports it directly.  */
    5028      2671749 :       gimple *def_stmt = NULL;
    5029      2568400 :       if (val && TREE_CODE (val) == SSA_NAME
    5030      2567527 :           && VECTOR_BOOLEAN_TYPE_P (TREE_TYPE (val))
    5031         2598 :           && (comparison == NE || comparison == EQ)
    5032      2674347 :           && (def_stmt = get_gimple_for_ssa_name (val)))
    5033              :         {
    5034         1823 :           tree masked_op = NULL_TREE;
    5035         1823 :           tree len_op = NULL_TREE;
    5036         1823 :           tree len_bias = NULL_TREE;
    5037              :           /* First determine if the operation should be masked or unmasked.  */
    5038         1823 :           if (is_gimple_assign (def_stmt)
    5039         1823 :               && gimple_assign_rhs_code (def_stmt) == BIT_AND_EXPR)
    5040              :             {
    5041              :               /* See if one side if a comparison, if so use the other side as
    5042              :                  the mask.  */
    5043           20 :               gimple *mask_def = NULL;
    5044           20 :               tree rhs1 = gimple_assign_rhs1 (def_stmt);
    5045           20 :               tree rhs2 = gimple_assign_rhs2 (def_stmt);
    5046           20 :               if (TREE_CODE (rhs1) == SSA_NAME
    5047           20 :                   && (mask_def = get_gimple_for_ssa_name (rhs1))
    5048           20 :                   && is_gimple_assign (mask_def)
    5049           40 :                   && TREE_CODE_CLASS (gimple_assign_rhs_code (mask_def)))
    5050              :                 masked_op = rhs2;
    5051            0 :               else if (TREE_CODE (rhs2) == SSA_NAME
    5052            0 :                        && (mask_def = get_gimple_for_ssa_name (rhs2))
    5053            0 :                        && is_gimple_assign (mask_def)
    5054            0 :                        && TREE_CODE_CLASS (gimple_assign_rhs_code (mask_def)))
    5055              :                 masked_op = rhs1;
    5056              : 
    5057           20 :               if (masked_op)
    5058         1823 :                 def_stmt = mask_def;
    5059              :             }
    5060              :             /* Else check to see if we're a LEN target.  */
    5061         1803 :           else if (is_gimple_call (def_stmt)
    5062            3 :                    && gimple_call_internal_p (def_stmt)
    5063         1806 :                    && gimple_call_internal_fn (def_stmt) == IFN_VCOND_MASK_LEN)
    5064              :             {
    5065              :               /* Example to consume:
    5066              : 
    5067              :                    a = _59 != vect__4.17_75;
    5068              :                    vcmp = .VCOND_MASK_LEN (a, { -1, ... }, { 0, ... }, _90, 0);
    5069              :                    if (vcmp != { 0, ... })
    5070              : 
    5071              :                 and transform into
    5072              : 
    5073              :                    if (cond_len_vec_cbranch_any ({-1, ...}, a, _90, 0)).  */
    5074            0 :               gcall *call = dyn_cast <gcall *> (def_stmt);
    5075            0 :               tree true_branch = gimple_call_arg (call, 1);
    5076            0 :               tree false_branch = gimple_call_arg (call, 2);
    5077            0 :               if (integer_minus_onep (true_branch)
    5078            0 :                   && integer_zerop (false_branch))
    5079              :                 {
    5080            0 :                   len_op = gimple_call_arg (call, 3);
    5081            0 :                   len_bias = gimple_call_arg (call, 4);
    5082            0 :                   tree arg0 = gimple_call_arg (call, 0);
    5083              : 
    5084            0 :                   if (TREE_CODE (arg0) == SSA_NAME)
    5085            0 :                     def_stmt = get_gimple_for_ssa_name (arg0);
    5086              :                 }
    5087              :             }
    5088              : 
    5089         1823 :           enum insn_code icode;
    5090         1823 :           if (def_stmt
    5091         1823 :               && is_gimple_assign (def_stmt)
    5092         3643 :               && TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt))
    5093              :                    == tcc_comparison)
    5094              :             {
    5095         1599 :               class expand_operand ops[5];
    5096         1599 :               rtx_insn *tmp = NULL;
    5097         1599 :               start_sequence ();
    5098         1599 :               tree t_op0 = gimple_assign_rhs1 (def_stmt);
    5099         1599 :               tree t_op1 = gimple_assign_rhs2 (def_stmt);
    5100         1599 :               rtx op0c = expand_normal (t_op0);
    5101         1599 :               rtx op1c = expand_normal (t_op1);
    5102         1599 :               machine_mode mode2 = GET_MODE (op0c);
    5103              : 
    5104         1599 :               int nops = masked_op ? 3 : (len_op ? 5 : 2);
    5105         1599 :               int offset = masked_op || len_op ? 1 : 0;
    5106         1599 :               create_input_operand (&ops[offset + 0], op0c, mode2);
    5107         1599 :               create_input_operand (&ops[offset + 1], op1c, mode2);
    5108         1599 :               if (masked_op)
    5109              :                 {
    5110           18 :                   auto mask_mode = TYPE_MODE (TREE_TYPE (masked_op));
    5111           18 :                   rtx mask_op = expand_normal (masked_op);
    5112           18 :                   create_input_operand (&ops[0], mask_op, mask_mode);
    5113              :                 }
    5114         1581 :               else if (len_op)
    5115              :                 {
    5116            0 :                   rtx len_rtx = expand_normal (len_op);
    5117            0 :                   rtx len_bias_rtx = expand_normal (len_bias);
    5118            0 :                   tree lhs = gimple_get_lhs (def_stmt);
    5119            0 :                   auto mask_mode = TYPE_MODE (TREE_TYPE (lhs));
    5120              :                   /* ??? We could use something like internal_fn's
    5121              :                      add_mask_else_and_len_args here.  Currently it
    5122              :                      only supports a fixed, consecutive order of
    5123              :                      mask and len, though.  */
    5124            0 :                   create_input_operand (&ops[0], CONSTM1_RTX (mask_mode),
    5125              :                                         mask_mode);
    5126            0 :                   create_convert_operand_from
    5127            0 :                     (&ops[3], len_rtx, TYPE_MODE (TREE_TYPE (len_op)),
    5128            0 :                      TYPE_UNSIGNED (TREE_TYPE (len_op)));
    5129            0 :                   create_input_operand (&ops[4], len_bias_rtx, QImode);
    5130              :                 }
    5131              : 
    5132         1599 :               int unsignedp2 = TYPE_UNSIGNED (TREE_TYPE (t_op0));
    5133         1599 :               auto inner_code = gimple_assign_rhs_code (def_stmt);
    5134         1599 :               rtx test2 = NULL_RTX;
    5135              : 
    5136         1599 :               enum rtx_code comparison2 = get_rtx_code (inner_code, unsignedp2);
    5137         1599 :               if (unsignedp2)
    5138          530 :                 comparison2 = unsigned_condition (comparison2);
    5139         1599 :               if (comparison == NE)
    5140         1831 :                 optab = masked_op ? cond_vec_cbranch_any_optab
    5141          910 :                                   : len_op ? cond_len_vec_cbranch_any_optab
    5142              :                                            : vec_cbranch_any_optab;
    5143              :               else
    5144         1349 :                 optab = masked_op ? cond_vec_cbranch_all_optab
    5145          671 :                                   : len_op ? cond_len_vec_cbranch_all_optab
    5146              :                                            : vec_cbranch_all_optab;
    5147              : 
    5148         1599 :               if ((icode = optab_handler (optab, mode2))
    5149              :                   != CODE_FOR_nothing
    5150         1599 :                   && maybe_legitimize_operands (icode, 1, nops, ops))
    5151              :                 {
    5152           18 :                   test2 = gen_rtx_fmt_ee (comparison2, VOIDmode,
    5153              :                                           ops[offset + 0].value,
    5154              :                                           ops[offset + 1].value);
    5155           18 :                   if (insn_operand_matches (icode, 0, test2))
    5156              :                     {
    5157           36 :                       rtx mask
    5158           18 :                         = (masked_op || len_op) ? ops[0].value : NULL_RTX;
    5159           18 :                       rtx len = len_op ? ops[3].value : NULL_RTX;
    5160            0 :                       rtx bias = len_op ? ops[4].value : NULL_RTX;
    5161           18 :                       emit_cmp_and_jump_insn_1 (test2, mask, len, bias, mode2,
    5162              :                                                 label, optab, prob, false);
    5163           18 :                       tmp = get_insns ();
    5164              :                     }
    5165              :                 }
    5166              : 
    5167           18 :               end_sequence ();
    5168         1599 :               if (tmp)
    5169              :                 {
    5170           18 :                   emit_insn (tmp);
    5171           18 :                   return;
    5172              :                 }
    5173              :             }
    5174              :         }
    5175              :     }
    5176              : 
    5177              :   /*  cbranch should only be used for VECTOR_BOOLEAN_TYPE_P values.   */
    5178      6505272 :   direct_optab base_optab = cbranch_optab;
    5179      6505272 :   if (VECTOR_MODE_P (GET_MODE (op0)))
    5180              :     {
    5181              :       /* If cbranch is provided, use it.  If we get here it means we have an
    5182              :          instruction in between what created the boolean value and the gcond
    5183              :          that is not a masking operation.  This can happen for instance during
    5184              :          unrolling of early-break where we have an OR-reduction to reduce the
    5185              :          masks.  In this case knowing we have a mask can let us generate better
    5186              :          code.  If it's not there there then check the vector specific
    5187              :          optabs.  */
    5188        19496 :       if (optab_handler (cbranch_optab, mode) == CODE_FOR_nothing)
    5189              :         {
    5190            0 :           if (comparison == NE)
    5191              :             base_optab = vec_cbranch_any_optab;
    5192              :           else
    5193            0 :             base_optab = vec_cbranch_all_optab;
    5194              : 
    5195            0 :           prepare_cmp_insn (op0, op1, NULL, comparison, size, unsignedp,
    5196              :                             OPTAB_DIRECT, &test, &mode, base_optab);
    5197              : 
    5198            0 :           enum insn_code icode = optab_handler (base_optab, mode);
    5199              : 
    5200              :           /* If the new cbranch isn't supported, degrade back to old one.  */
    5201            0 :           if (icode == CODE_FOR_nothing
    5202            0 :               || !test
    5203            0 :               || !insn_operand_matches (icode, 0, test))
    5204              :             base_optab = cbranch_optab;
    5205              :         }
    5206              : 
    5207        19496 :       prepare_cmp_insn (op0, op1, NULL, comparison, size, unsignedp,
    5208              :                         OPTAB_LIB_WIDEN, &test, &mode, base_optab);
    5209              :     }
    5210              : 
    5211      6505272 :   emit_cmp_and_jump_insn_1 (test, NULL_RTX, NULL_RTX, NULL_RTX, mode, label,
    5212              :                             base_optab, prob, false);
    5213              : }
    5214              : 
    5215              : /* Overloaded version of emit_cmp_and_jump_insns in which VAL is unknown.  */
    5216              : 
    5217              : void
    5218       114627 : emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
    5219              :                          machine_mode mode, int unsignedp, rtx label,
    5220              :                          profile_probability prob)
    5221              : {
    5222       114627 :   emit_cmp_and_jump_insns (x, y, comparison, size, mode, unsignedp, NULL,
    5223              :                            label, prob);
    5224       114627 : }
    5225              : 
    5226              : 
    5227              : /* Emit a library call comparison between floating point X and Y.
    5228              :    COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).  */
    5229              : 
    5230              : static void
    5231        44980 : prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
    5232              :                        rtx *ptest, machine_mode *pmode)
    5233              : {
    5234        44980 :   enum rtx_code swapped = swap_condition (comparison);
    5235        44980 :   enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
    5236        44980 :   machine_mode orig_mode = GET_MODE (x);
    5237        44980 :   machine_mode mode;
    5238        44980 :   rtx true_rtx, false_rtx;
    5239        44980 :   rtx value, target, equiv;
    5240        44980 :   rtx_insn *insns;
    5241        44980 :   rtx libfunc = 0;
    5242        44980 :   bool reversed_p = false;
    5243        44980 :   scalar_int_mode cmp_mode = targetm.libgcc_cmp_return_mode ();
    5244              : 
    5245        44980 :   FOR_EACH_WIDER_MODE_FROM (mode, orig_mode)
    5246              :     {
    5247        44980 :       if (code_to_optab (comparison)
    5248        44980 :           && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
    5249              :         break;
    5250              : 
    5251            0 :       if (code_to_optab (swapped)
    5252            0 :           && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
    5253              :         {
    5254              :           std::swap (x, y);
    5255              :           comparison = swapped;
    5256              :           break;
    5257              :         }
    5258              : 
    5259            0 :       if (code_to_optab (reversed)
    5260            0 :           && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
    5261              :         {
    5262              :           comparison = reversed;
    5263              :           reversed_p = true;
    5264              :           break;
    5265              :         }
    5266              :     }
    5267              : 
    5268        44980 :   gcc_assert (mode != VOIDmode);
    5269              : 
    5270        44980 :   if (mode != orig_mode)
    5271              :     {
    5272            0 :       x = convert_to_mode (mode, x, 0);
    5273            0 :       y = convert_to_mode (mode, y, 0);
    5274              :     }
    5275              : 
    5276              :   /* Attach a REG_EQUAL note describing the semantics of the libcall to
    5277              :      the RTL.  The allows the RTL optimizers to delete the libcall if the
    5278              :      condition can be determined at compile-time.  */
    5279        44980 :   if (comparison == UNORDERED
    5280              :       || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
    5281              :     {
    5282         5809 :       true_rtx = const_true_rtx;
    5283         5809 :       false_rtx = const0_rtx;
    5284              :     }
    5285              :   else
    5286              :     {
    5287        39171 :       switch (comparison)
    5288              :         {
    5289         3505 :         case EQ:
    5290         3505 :           true_rtx = const0_rtx;
    5291         3505 :           false_rtx = const_true_rtx;
    5292         3505 :           break;
    5293              : 
    5294        23595 :         case NE:
    5295        23595 :           true_rtx = const_true_rtx;
    5296        23595 :           false_rtx = const0_rtx;
    5297        23595 :           break;
    5298              : 
    5299         3030 :         case GT:
    5300         3030 :           true_rtx = const1_rtx;
    5301         3030 :           false_rtx = const0_rtx;
    5302         3030 :           break;
    5303              : 
    5304         3025 :         case GE:
    5305         3025 :           true_rtx = const0_rtx;
    5306         3025 :           false_rtx = constm1_rtx;
    5307         3025 :           break;
    5308              : 
    5309         2487 :         case LT:
    5310         2487 :           true_rtx = constm1_rtx;
    5311         2487 :           false_rtx = const0_rtx;
    5312         2487 :           break;
    5313              : 
    5314         3529 :         case LE:
    5315         3529 :           true_rtx = const0_rtx;
    5316         3529 :           false_rtx = const1_rtx;
    5317         3529 :           break;
    5318              : 
    5319            0 :         default:
    5320            0 :           gcc_unreachable ();
    5321              :         }
    5322              :     }
    5323              : 
    5324        44980 :   if (comparison == UNORDERED)
    5325              :     {
    5326         5809 :       rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
    5327         5809 :       equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
    5328         5809 :       equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
    5329              :                                     temp, const_true_rtx, equiv);
    5330              :     }
    5331              :   else
    5332              :     {
    5333        39171 :       equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
    5334        39171 :       if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
    5335        39171 :         equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
    5336              :                                       equiv, true_rtx, false_rtx);
    5337              :     }
    5338              : 
    5339        44980 :   start_sequence ();
    5340        44980 :   value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
    5341              :                                    cmp_mode, x, mode, y, mode);
    5342        44980 :   insns = end_sequence ();
    5343              : 
    5344        44980 :   target = gen_reg_rtx (cmp_mode);
    5345        44980 :   emit_libcall_block (insns, target, value, equiv);
    5346              : 
    5347        44980 :   if (comparison == UNORDERED
    5348              :       || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
    5349        44980 :       || reversed_p)
    5350        11618 :     *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
    5351              :   else
    5352        39171 :     *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
    5353              : 
    5354        44980 :   *pmode = cmp_mode;
    5355        44980 : }
    5356              : 
    5357              : /* Generate code to indirectly jump to a location given in the rtx LOC.  */
    5358              : 
    5359              : void
    5360         1361 : emit_indirect_jump (rtx loc)
    5361              : {
    5362         1361 :   if (!targetm.have_indirect_jump ())
    5363            0 :     sorry ("indirect jumps are not available on this target");
    5364              :   else
    5365              :     {
    5366         1361 :       class expand_operand ops[1];
    5367         1361 :       create_address_operand (&ops[0], loc);
    5368         1361 :       expand_jump_insn (targetm.code_for_indirect_jump, 1, ops);
    5369         1361 :       emit_barrier ();
    5370              :     }
    5371         1361 : }
    5372              : 
    5373              : 
    5374              : /* Emit a conditional move instruction if the machine supports one for that
    5375              :    condition and machine mode.
    5376              : 
    5377              :    OP0 and OP1 are the operands that should be compared using CODE.  CMODE is
    5378              :    the mode to use should they be constants.  If it is VOIDmode, they cannot
    5379              :    both be constants.
    5380              : 
    5381              :    OP2 should be stored in TARGET if the comparison is true, otherwise OP3
    5382              :    should be stored there.  MODE is the mode to use should they be constants.
    5383              :    If it is VOIDmode, they cannot both be constants.
    5384              : 
    5385              :    The result is either TARGET (perhaps modified) or NULL_RTX if the operation
    5386              :    is not supported.  */
    5387              : 
    5388              : rtx
    5389       483065 : emit_conditional_move (rtx target, struct rtx_comparison comp,
    5390              :                        rtx op2, rtx op3,
    5391              :                        machine_mode mode, int unsignedp)
    5392              : {
    5393       483065 :   rtx comparison;
    5394       483065 :   rtx_insn *last;
    5395       483065 :   enum insn_code icode;
    5396       483065 :   enum rtx_code reversed;
    5397              : 
    5398              :   /* If the two source operands are identical, that's just a move.  */
    5399              : 
    5400       483065 :   if (rtx_equal_p (op2, op3))
    5401              :     {
    5402         1707 :       if (!target)
    5403            0 :         target = gen_reg_rtx (mode);
    5404              : 
    5405         1707 :       emit_move_insn (target, op3);
    5406         1707 :       return target;
    5407              :     }
    5408              : 
    5409              :   /* If one operand is constant, make it the second one.  Only do this
    5410              :      if the other operand is not constant as well.  */
    5411              : 
    5412       481358 :   if (swap_commutative_operands_p (comp.op0, comp.op1))
    5413              :     {
    5414        20872 :       std::swap (comp.op0, comp.op1);
    5415        20872 :       comp.code = swap_condition (comp.code);
    5416              :     }
    5417              : 
    5418              :   /* get_condition will prefer to generate LT and GT even if the old
    5419              :      comparison was against zero, so undo that canonicalization here since
    5420              :      comparisons against zero are cheaper.  */
    5421              : 
    5422       481358 :   if (comp.code == LT && comp.op1 == const1_rtx)
    5423         3327 :     comp.code = LE, comp.op1 = const0_rtx;
    5424       478031 :   else if (comp.code == GT && comp.op1 == constm1_rtx)
    5425         7247 :     comp.code = GE, comp.op1 = const0_rtx;
    5426              : 
    5427       481358 :   if (comp.mode == VOIDmode)
    5428       394566 :     comp.mode = GET_MODE (comp.op0);
    5429              : 
    5430       481358 :   enum rtx_code orig_code = comp.code;
    5431       481358 :   bool swapped = false;
    5432       481358 :   if (swap_commutative_operands_p (op2, op3)
    5433       481358 :       && ((reversed =
    5434       125990 :            reversed_comparison_code_parts (comp.code, comp.op0, comp.op1, NULL))
    5435              :           != UNKNOWN))
    5436              :     {
    5437              :       std::swap (op2, op3);
    5438              :       comp.code = reversed;
    5439              :       swapped = true;
    5440              :     }
    5441              : 
    5442       481358 :   if (mode == VOIDmode)
    5443           86 :     mode = GET_MODE (op2);
    5444              : 
    5445       481358 :   icode = direct_optab_handler (movcc_optab, mode);
    5446              : 
    5447       481358 :   if (icode == CODE_FOR_nothing)
    5448              :     return NULL_RTX;
    5449              : 
    5450       473459 :   if (!target)
    5451           32 :     target = gen_reg_rtx (mode);
    5452              : 
    5453       473459 :   for (int pass = 0; ; pass++)
    5454              :     {
    5455       506326 :       comp.code = unsignedp ? unsigned_condition (comp.code) : comp.code;
    5456      1012652 :       comparison =
    5457       506326 :         simplify_gen_relational (comp.code, VOIDmode,
    5458              :                                  comp.mode, comp.op0, comp.op1);
    5459              : 
    5460              :       /* We can get const0_rtx or const_true_rtx in some circumstances.  Just
    5461              :          punt and let the caller figure out how best to deal with this
    5462              :          situation.  */
    5463       506326 :       if (COMPARISON_P (comparison))
    5464              :         {
    5465       505482 :           saved_pending_stack_adjust save;
    5466       505482 :           save_pending_stack_adjust (&save);
    5467       505482 :           last = get_last_insn ();
    5468       505482 :           do_pending_stack_adjust ();
    5469       505482 :           machine_mode cmpmode = comp.mode;
    5470       505482 :           rtx orig_op0 = XEXP (comparison, 0);
    5471       505482 :           rtx orig_op1 = XEXP (comparison, 1);
    5472       505482 :           rtx op2p = op2;
    5473       505482 :           rtx op3p = op3;
    5474              :           /* If we are optimizing, force expensive constants into a register
    5475              :              but preserve an eventual equality with op2/op3.  */
    5476            1 :           if (CONSTANT_P (orig_op0) && optimize
    5477            1 :               && cmpmode == mode
    5478            1 :               && (rtx_cost (orig_op0, mode, COMPARE, 0,
    5479              :                             optimize_insn_for_speed_p ())
    5480              :                   > COSTS_N_INSNS (1))
    5481       505482 :               && can_create_pseudo_p ())
    5482              :             {
    5483            0 :               if (rtx_equal_p (orig_op0, op2))
    5484            0 :                 op2p = XEXP (comparison, 0) = force_reg (cmpmode, orig_op0);
    5485            0 :               else if (rtx_equal_p (orig_op0, op3))
    5486            0 :                 op3p = XEXP (comparison, 0) = force_reg (cmpmode, orig_op0);
    5487              :             }
    5488       283108 :           if (CONSTANT_P (orig_op1) && optimize
    5489       262008 :               && cmpmode == mode
    5490        91416 :               && (rtx_cost (orig_op1, mode, COMPARE, 0,
    5491              :                             optimize_insn_for_speed_p ())
    5492              :                   > COSTS_N_INSNS (1))
    5493       505659 :               && can_create_pseudo_p ())
    5494              :             {
    5495          177 :               if (rtx_equal_p (orig_op1, op2))
    5496          110 :                 op2p = XEXP (comparison, 1) = force_reg (cmpmode, orig_op1);
    5497           67 :               else if (rtx_equal_p (orig_op1, op3))
    5498           45 :                 op3p = XEXP (comparison, 1) = force_reg (cmpmode, orig_op1);
    5499              :             }
    5500       505482 :           prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1), NULL,
    5501       505482 :                             GET_CODE (comparison), NULL_RTX, unsignedp,
    5502              :                             OPTAB_WIDEN, &comparison, &cmpmode, cbranch_optab);
    5503       505482 :           if (comparison)
    5504              :             {
    5505       503225 :                rtx res = emit_conditional_move_1 (target, comparison,
    5506              :                                                   op2p, op3p, mode);
    5507       503225 :                if (res != NULL_RTX)
    5508       437909 :                  return res;
    5509              :             }
    5510        67573 :           delete_insns_since (last);
    5511        67573 :           restore_pending_stack_adjust (&save);
    5512              :         }
    5513              : 
    5514        68417 :       if (pass == 1)
    5515              :         return NULL_RTX;
    5516              : 
    5517              :       /* If the preferred op2/op3 order is not usable, retry with other
    5518              :          operand order, perhaps it will expand successfully.  */
    5519        35608 :       if (swapped)
    5520              :         comp.code = orig_code;
    5521        70332 :       else if ((reversed =
    5522        35166 :                 reversed_comparison_code_parts (orig_code, comp.op0, comp.op1,
    5523              :                                                            NULL))
    5524              :                != UNKNOWN)
    5525              :         comp.code = reversed;
    5526              :       else
    5527              :         return NULL_RTX;
    5528        32867 :       std::swap (op2, op3);
    5529        32867 :     }
    5530              : }
    5531              : 
    5532              : /* Helper function that, in addition to COMPARISON, also tries
    5533              :    the reversed REV_COMPARISON with swapped OP2 and OP3.  As opposed
    5534              :    to when we pass the specific constituents of a comparison, no
    5535              :    additional insns are emitted for it.  It might still be necessary
    5536              :    to emit more than one insn for the final conditional move, though.  */
    5537              : 
    5538              : rtx
    5539       108866 : emit_conditional_move (rtx target, rtx comparison, rtx rev_comparison,
    5540              :                        rtx op2, rtx op3, machine_mode mode)
    5541              : {
    5542       108866 :   rtx res = emit_conditional_move_1 (target, comparison, op2, op3, mode);
    5543              : 
    5544       108866 :   if (res != NULL_RTX)
    5545              :     return res;
    5546              : 
    5547         5116 :   return emit_conditional_move_1 (target, rev_comparison, op3, op2, mode);
    5548              : }
    5549              : 
    5550              : /* Helper for emitting a conditional move.  */
    5551              : 
    5552              : static rtx
    5553       617207 : emit_conditional_move_1 (rtx target, rtx comparison,
    5554              :                          rtx op2, rtx op3, machine_mode mode)
    5555              : {
    5556       617207 :   enum insn_code icode;
    5557              : 
    5558       617207 :   if (comparison == NULL_RTX || !COMPARISON_P (comparison))
    5559              :     return NULL_RTX;
    5560              : 
    5561              :   /* If the two source operands are identical, that's just a move.
    5562              :      As the comparison comes in non-canonicalized, we must make
    5563              :      sure not to discard any possible side effects.  If there are
    5564              :      side effects, just let the target handle it.  */
    5565       617207 :   if (!side_effects_p (comparison) && rtx_equal_p (op2, op3))
    5566              :     {
    5567            0 :       if (!target)
    5568            0 :         target = gen_reg_rtx (mode);
    5569              : 
    5570            0 :       emit_move_insn (target, op3);
    5571            0 :       return target;
    5572              :     }
    5573              : 
    5574       617207 :   if (mode == VOIDmode)
    5575            0 :     mode = GET_MODE (op2);
    5576              : 
    5577       617207 :   icode = direct_optab_handler (movcc_optab, mode);
    5578              : 
    5579       617207 :   if (icode == CODE_FOR_nothing)
    5580              :     return NULL_RTX;
    5581              : 
    5582       617207 :   if (!target)
    5583            0 :     target = gen_reg_rtx (mode);
    5584              : 
    5585       617207 :   class expand_operand ops[4];
    5586              : 
    5587       617207 :   create_output_operand (&ops[0], target, mode);
    5588       617207 :   create_fixed_operand (&ops[1], comparison);
    5589       617207 :   create_input_operand (&ops[2], op2, mode);
    5590       617207 :   create_input_operand (&ops[3], op3, mode);
    5591              : 
    5592       617207 :   if (maybe_expand_insn (icode, 4, ops))
    5593              :     {
    5594       541659 :       if (ops[0].value != target)
    5595           57 :         convert_move (target, ops[0].value, false);
    5596              :       return target;
    5597              :     }
    5598              : 
    5599              :   return NULL_RTX;
    5600              : }
    5601              : 
    5602              : 
    5603              : /* Emit a conditional negate or bitwise complement using the
    5604              :    negcc or notcc optabs if available.  Return NULL_RTX if such operations
    5605              :    are not available.  Otherwise return the RTX holding the result.
    5606              :    TARGET is the desired destination of the result.  COMP is the comparison
    5607              :    on which to negate.  If COND is true move into TARGET the negation
    5608              :    or bitwise complement of OP1.  Otherwise move OP2 into TARGET.
    5609              :    CODE is either NEG or NOT.  MODE is the machine mode in which the
    5610              :    operation is performed.  */
    5611              : 
    5612              : rtx
    5613         1380 : emit_conditional_neg_or_complement (rtx target, rtx_code code,
    5614              :                                      machine_mode mode, rtx cond, rtx op1,
    5615              :                                      rtx op2)
    5616              : {
    5617         1380 :   optab op = unknown_optab;
    5618         1380 :   if (code == NEG)
    5619              :     op = negcc_optab;
    5620           68 :   else if (code == NOT)
    5621              :     op = notcc_optab;
    5622              :   else
    5623            0 :     gcc_unreachable ();
    5624              : 
    5625         1380 :   insn_code icode = direct_optab_handler (op, mode);
    5626              : 
    5627         1380 :   if (icode == CODE_FOR_nothing)
    5628              :     return NULL_RTX;
    5629              : 
    5630            0 :   if (!target)
    5631            0 :     target = gen_reg_rtx (mode);
    5632              : 
    5633            0 :   rtx_insn *last = get_last_insn ();
    5634            0 :   class expand_operand ops[4];
    5635              : 
    5636            0 :   create_output_operand (&ops[0], target, mode);
    5637            0 :   create_fixed_operand (&ops[1], cond);
    5638            0 :   create_input_operand (&ops[2], op1, mode);
    5639            0 :   create_input_operand (&ops[3], op2, mode);
    5640              : 
    5641            0 :   if (maybe_expand_insn (icode, 4, ops))
    5642              :     {
    5643            0 :       if (ops[0].value != target)
    5644            0 :         convert_move (target, ops[0].value, false);
    5645              : 
    5646              :       return target;
    5647              :     }
    5648            0 :   delete_insns_since (last);
    5649            0 :   return NULL_RTX;
    5650              : }
    5651              : 
    5652              : /* Emit a conditional addition instruction if the machine supports one for that
    5653              :    condition and machine mode.
    5654              : 
    5655              :    OP0 and OP1 are the operands that should be compared using CODE.  CMODE is
    5656              :    the mode to use should they be constants.  If it is VOIDmode, they cannot
    5657              :    both be constants.
    5658              : 
    5659              :    OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
    5660              :    should be stored there.  MODE is the mode to use should they be constants.
    5661              :    If it is VOIDmode, they cannot both be constants.
    5662              : 
    5663              :    The result is either TARGET (perhaps modified) or NULL_RTX if the operation
    5664              :    is not supported.  */
    5665              : 
    5666              : rtx
    5667        10948 : emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
    5668              :                       machine_mode cmode, rtx op2, rtx op3,
    5669              :                       machine_mode mode, int unsignedp)
    5670              : {
    5671        10948 :   rtx comparison;
    5672        10948 :   rtx_insn *last;
    5673        10948 :   enum insn_code icode;
    5674              : 
    5675              :   /* If one operand is constant, make it the second one.  Only do this
    5676              :      if the other operand is not constant as well.  */
    5677              : 
    5678        10948 :   if (swap_commutative_operands_p (op0, op1))
    5679              :     {
    5680            2 :       std::swap (op0, op1);
    5681            2 :       code = swap_condition (code);
    5682              :     }
    5683              : 
    5684              :   /* get_condition will prefer to generate LT and GT even if the old
    5685              :      comparison was against zero, so undo that canonicalization here since
    5686              :      comparisons against zero are cheaper.  */
    5687        10948 :   if (code == LT && op1 == const1_rtx)
    5688           20 :     code = LE, op1 = const0_rtx;
    5689        10928 :   else if (code == GT && op1 == constm1_rtx)
    5690           58 :     code = GE, op1 = const0_rtx;
    5691              : 
    5692        10948 :   if (cmode == VOIDmode)
    5693        10948 :     cmode = GET_MODE (op0);
    5694              : 
    5695        10948 :   if (mode == VOIDmode)
    5696            0 :     mode = GET_MODE (op2);
    5697              : 
    5698        10948 :   icode = optab_handler (addcc_optab, mode);
    5699              : 
    5700        10948 :   if (icode == CODE_FOR_nothing)
    5701              :     return 0;
    5702              : 
    5703        10221 :   if (!target)
    5704            0 :     target = gen_reg_rtx (mode);
    5705              : 
    5706        10221 :   code = unsignedp ? unsigned_condition (code) : code;
    5707        10221 :   comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
    5708              : 
    5709              :   /* We can get const0_rtx or const_true_rtx in some circumstances.  Just
    5710              :      return NULL and let the caller figure out how best to deal with this
    5711              :      situation.  */
    5712        10221 :   if (!COMPARISON_P (comparison))
    5713              :     return NULL_RTX;
    5714              : 
    5715        10221 :   do_pending_stack_adjust ();
    5716        10221 :   last = get_last_insn ();
    5717        10221 :   prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1), NULL,
    5718        10221 :                     GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
    5719              :                     &comparison, &cmode, cbranch_optab);
    5720        10221 :   if (comparison)
    5721              :     {
    5722        10221 :       class expand_operand ops[4];
    5723              : 
    5724        10221 :       create_output_operand (&ops[0], target, mode);
    5725        10221 :       create_fixed_operand (&ops[1], comparison);
    5726        10221 :       create_input_operand (&ops[2], op2, mode);
    5727        10221 :       create_input_operand (&ops[3], op3, mode);
    5728        10221 :       if (maybe_expand_insn (icode, 4, ops))
    5729              :         {
    5730          341 :           if (ops[0].value != target)
    5731            0 :             convert_move (target, ops[0].value, false);
    5732          341 :           return target;
    5733              :         }
    5734              :     }
    5735         9880 :   delete_insns_since (last);
    5736         9880 :   return NULL_RTX;
    5737              : }
    5738              : 
    5739              : /* These functions attempt to generate an insn body, rather than
    5740              :    emitting the insn, but if the gen function already emits them, we
    5741              :    make no attempt to turn them back into naked patterns.  */
    5742              : 
    5743              : /* Generate and return an insn body to add Y to X.  */
    5744              : 
    5745              : rtx_insn *
    5746          252 : gen_add2_insn (rtx x, rtx y)
    5747              : {
    5748          252 :   enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
    5749              : 
    5750          252 :   gcc_assert (insn_operand_matches (icode, 0, x));
    5751          252 :   gcc_assert (insn_operand_matches (icode, 1, x));
    5752          252 :   gcc_assert (insn_operand_matches (icode, 2, y));
    5753              : 
    5754          252 :   return GEN_FCN (icode) (x, x, y);
    5755              : }
    5756              : 
    5757              : /* Generate and return an insn body to add r1 and c,
    5758              :    storing the result in r0.  */
    5759              : 
    5760              : rtx_insn *
    5761            0 : gen_add3_insn (rtx r0, rtx r1, rtx c)
    5762              : {
    5763            0 :   enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
    5764              : 
    5765            0 :   if (icode == CODE_FOR_nothing
    5766            0 :       || !insn_operand_matches (icode, 0, r0)
    5767            0 :       || !insn_operand_matches (icode, 1, r1)
    5768            0 :       || !insn_operand_matches (icode, 2, c))
    5769              :     return NULL;
    5770              : 
    5771            0 :   return GEN_FCN (icode) (r0, r1, c);
    5772              : }
    5773              : 
    5774              : bool
    5775         4205 : have_add2_insn (rtx x, rtx y)
    5776              : {
    5777         4205 :   enum insn_code icode;
    5778              : 
    5779         4205 :   gcc_assert (GET_MODE (x) != VOIDmode);
    5780              : 
    5781         4205 :   icode = optab_handler (add_optab, GET_MODE (x));
    5782              : 
    5783         4205 :   if (icode == CODE_FOR_nothing)
    5784              :     return false;
    5785              : 
    5786         4205 :   if (!insn_operand_matches (icode, 0, x)
    5787         4205 :       || !insn_operand_matches (icode, 1, x)
    5788         8410 :       || !insn_operand_matches (icode, 2, y))
    5789          187 :     return false;
    5790              : 
    5791              :   return true;
    5792              : }
    5793              : 
    5794              : /* Generate and return an insn body to add Y to X.  */
    5795              : 
    5796              : rtx_insn *
    5797            0 : gen_addptr3_insn (rtx x, rtx y, rtx z)
    5798              : {
    5799            0 :   enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
    5800              : 
    5801            0 :   gcc_assert (insn_operand_matches (icode, 0, x));
    5802            0 :   gcc_assert (insn_operand_matches (icode, 1, y));
    5803            0 :   gcc_assert (insn_operand_matches (icode, 2, z));
    5804              : 
    5805            0 :   return GEN_FCN (icode) (x, y, z);
    5806              : }
    5807              : 
    5808              : /* Return true if the target implements an addptr pattern and X, Y,
    5809              :    and Z are valid for the pattern predicates.  */
    5810              : 
    5811              : bool
    5812       693040 : have_addptr3_insn (rtx x, rtx y, rtx z)
    5813              : {
    5814       693040 :   enum insn_code icode;
    5815              : 
    5816       693040 :   gcc_assert (GET_MODE (x) != VOIDmode);
    5817              : 
    5818       693040 :   icode = optab_handler (addptr3_optab, GET_MODE (x));
    5819              : 
    5820       693040 :   if (icode == CODE_FOR_nothing)
    5821              :     return false;
    5822              : 
    5823            0 :   if (!insn_operand_matches (icode, 0, x)
    5824            0 :       || !insn_operand_matches (icode, 1, y)
    5825            0 :       || !insn_operand_matches (icode, 2, z))
    5826            0 :     return false;
    5827              : 
    5828              :   return true;
    5829              : }
    5830              : 
    5831              : /* Generate and return an insn body to subtract Y from X.  */
    5832              : 
    5833              : rtx_insn *
    5834           11 : gen_sub2_insn (rtx x, rtx y)
    5835              : {
    5836           11 :   enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
    5837              : 
    5838           11 :   gcc_assert (insn_operand_matches (icode, 0, x));
    5839           11 :   gcc_assert (insn_operand_matches (icode, 1, x));
    5840           11 :   gcc_assert (insn_operand_matches (icode, 2, y));
    5841              : 
    5842           11 :   return GEN_FCN (icode) (x, x, y);
    5843              : }
    5844              : 
    5845              : /* Generate and return an insn body to subtract r1 and c,
    5846              :    storing the result in r0.  */
    5847              : 
    5848              : rtx_insn *
    5849         1932 : gen_sub3_insn (rtx r0, rtx r1, rtx c)
    5850              : {
    5851         1932 :   enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
    5852              : 
    5853         1932 :   if (icode == CODE_FOR_nothing
    5854         1932 :       || !insn_operand_matches (icode, 0, r0)
    5855         1932 :       || !insn_operand_matches (icode, 1, r1)
    5856         3864 :       || !insn_operand_matches (icode, 2, c))
    5857              :     return NULL;
    5858              : 
    5859         1932 :   return GEN_FCN (icode) (r0, r1, c);
    5860              : }
    5861              : 
    5862              : bool
    5863            0 : have_sub2_insn (rtx x, rtx y)
    5864              : {
    5865            0 :   enum insn_code icode;
    5866              : 
    5867            0 :   gcc_assert (GET_MODE (x) != VOIDmode);
    5868              : 
    5869            0 :   icode = optab_handler (sub_optab, GET_MODE (x));
    5870              : 
    5871            0 :   if (icode == CODE_FOR_nothing)
    5872              :     return false;
    5873              : 
    5874            0 :   if (!insn_operand_matches (icode, 0, x)
    5875            0 :       || !insn_operand_matches (icode, 1, x)
    5876            0 :       || !insn_operand_matches (icode, 2, y))
    5877            0 :     return false;
    5878              : 
    5879              :   return true;
    5880              : }
    5881              : 
    5882              : /* Generate the body of an insn to extend Y (with mode MFROM)
    5883              :    into X (with mode MTO).  Do zero-extension if UNSIGNEDP is nonzero.  */
    5884              : 
    5885              : rtx_insn *
    5886        30492 : gen_extend_insn (rtx x, rtx y, machine_mode mto,
    5887              :                  machine_mode mfrom, int unsignedp)
    5888              : {
    5889        30492 :   enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
    5890        30492 :   return GEN_FCN (icode) (x, y);
    5891              : }
    5892              : 
    5893              : /* Generate code to convert FROM to floating point
    5894              :    and store in TO.  FROM must be fixed point and not VOIDmode.
    5895              :    UNSIGNEDP nonzero means regard FROM as unsigned.
    5896              :    Normally this is done by correcting the final value
    5897              :    if it is negative.  */
    5898              : 
    5899              : void
    5900       148172 : expand_float (rtx to, rtx from, int unsignedp)
    5901              : {
    5902       148172 :   enum insn_code icode;
    5903       148172 :   rtx target = to;
    5904       148172 :   scalar_mode from_mode, to_mode;
    5905       148172 :   machine_mode fmode, imode;
    5906       148172 :   bool can_do_signed = false;
    5907              : 
    5908              :   /* Crash now, because we won't be able to decide which mode to use.  */
    5909       148172 :   gcc_assert (GET_MODE (from) != VOIDmode);
    5910              : 
    5911              :   /* Look for an insn to do the conversion.  Do it in the specified
    5912              :      modes if possible; otherwise convert either input, output or both to
    5913              :      wider mode.  If the integer mode is wider than the mode of FROM,
    5914              :      we can do the conversion signed even if the input is unsigned.  */
    5915              : 
    5916       170145 :   FOR_EACH_MODE_FROM (fmode, GET_MODE (to))
    5917       264639 :     FOR_EACH_MODE_FROM (imode, GET_MODE (from))
    5918              :       {
    5919       242666 :         int doing_unsigned = unsignedp;
    5920              : 
    5921       272924 :         if (fmode != GET_MODE (to)
    5922       242666 :             && (significand_size (fmode)
    5923        94540 :                 < GET_MODE_UNIT_PRECISION (GET_MODE (from))))
    5924        30258 :           continue;
    5925              : 
    5926       212408 :         icode = can_float_p (fmode, imode, unsignedp);
    5927       212408 :         if (icode == CODE_FOR_nothing && unsignedp)
    5928              :           {
    5929        35722 :             enum insn_code scode = can_float_p (fmode, imode, 0);
    5930        35722 :             if (scode != CODE_FOR_nothing)
    5931         6473 :               can_do_signed = true;
    5932        35722 :             if (imode != GET_MODE (from))
    5933        24591 :               icode = scode, doing_unsigned = 0;
    5934              :           }
    5935              : 
    5936       212408 :         if (icode != CODE_FOR_nothing)
    5937              :           {
    5938       139654 :             if (imode != GET_MODE (from))
    5939        10378 :               from = convert_to_mode (imode, from, unsignedp);
    5940              : 
    5941       139654 :             if (fmode != GET_MODE (to))
    5942         1645 :               target = gen_reg_rtx (fmode);
    5943              : 
    5944       273577 :             emit_unop_insn (icode, target, from,
    5945              :                             doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
    5946              : 
    5947       139654 :             if (target != to)
    5948         1645 :               convert_move (to, target, 0);
    5949       139654 :             return;
    5950              :           }
    5951              :       }
    5952              : 
    5953              :   /* Unsigned integer, and no way to convert directly.  Convert as signed,
    5954              :      then unconditionally adjust the result.  */
    5955         8518 :   if (unsignedp
    5956         8518 :       && can_do_signed
    5957         1118 :       && is_a <scalar_mode> (GET_MODE (to), &to_mode)
    5958         9636 :       && is_a <scalar_mode> (GET_MODE (from), &from_mode))
    5959              :     {
    5960         1118 :       opt_scalar_mode fmode_iter;
    5961         1118 :       rtx_code_label *label = gen_label_rtx ();
    5962         1118 :       rtx temp;
    5963         1118 :       REAL_VALUE_TYPE offset;
    5964              : 
    5965              :       /* Look for a usable floating mode FMODE wider than the source and at
    5966              :          least as wide as the target.  Using FMODE will avoid rounding woes
    5967              :          with unsigned values greater than the signed maximum value.  */
    5968              : 
    5969         2139 :       FOR_EACH_MODE_FROM (fmode_iter, to_mode)
    5970              :         {
    5971         2139 :           scalar_mode fmode = fmode_iter.require ();
    5972         3160 :           if (GET_MODE_PRECISION (from_mode) < GET_MODE_BITSIZE (fmode)
    5973         2139 :               && can_float_p (fmode, from_mode, 0) != CODE_FOR_nothing)
    5974              :             break;
    5975              :         }
    5976              : 
    5977         1118 :       if (!fmode_iter.exists (&fmode))
    5978              :         {
    5979              :           /* There is no such mode.  Pretend the target is wide enough.  */
    5980            0 :           fmode = to_mode;
    5981              : 
    5982              :           /* Avoid double-rounding when TO is narrower than FROM.  */
    5983            0 :           if ((significand_size (fmode) + 1)
    5984            0 :               < GET_MODE_PRECISION (from_mode))
    5985              :             {
    5986            0 :               rtx temp1;
    5987            0 :               rtx_code_label *neglabel = gen_label_rtx ();
    5988              : 
    5989              :               /* Don't use TARGET if it isn't a register, is a hard register,
    5990              :                  or is the wrong mode.  */
    5991            0 :               if (!REG_P (target)
    5992            0 :                   || REGNO (target) < FIRST_PSEUDO_REGISTER
    5993            0 :                   || GET_MODE (target) != fmode)
    5994            0 :                 target = gen_reg_rtx (fmode);
    5995              : 
    5996            0 :               imode = from_mode;
    5997            0 :               do_pending_stack_adjust ();
    5998              : 
    5999              :               /* Test whether the sign bit is set.  */
    6000            0 :               emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
    6001              :                                        0, neglabel);
    6002              : 
    6003              :               /* The sign bit is not set.  Convert as signed.  */
    6004            0 :               expand_float (target, from, 0);
    6005            0 :               emit_jump_insn (targetm.gen_jump (label));
    6006            0 :               emit_barrier ();
    6007              : 
    6008              :               /* The sign bit is set.
    6009              :                  Convert to a usable (positive signed) value by shifting right
    6010              :                  one bit, while remembering if a nonzero bit was shifted
    6011              :                  out; i.e., compute  (from & 1) | (from >> 1).  */
    6012              : 
    6013            0 :               emit_label (neglabel);
    6014            0 :               temp = expand_binop (imode, and_optab, from, const1_rtx,
    6015              :                                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
    6016            0 :               temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
    6017            0 :               temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
    6018              :                                    OPTAB_LIB_WIDEN);
    6019            0 :               expand_float (target, temp, 0);
    6020              : 
    6021              :               /* Multiply by 2 to undo the shift above.  */
    6022            0 :               temp = expand_binop (fmode, add_optab, target, target,
    6023              :                                    target, 0, OPTAB_LIB_WIDEN);
    6024            0 :               if (temp != target)
    6025            0 :                 emit_move_insn (target, temp);
    6026              : 
    6027            0 :               do_pending_stack_adjust ();
    6028            0 :               emit_label (label);
    6029            0 :               goto done;
    6030              :             }
    6031              :         }
    6032              : 
    6033              :       /* If we are about to do some arithmetic to correct for an
    6034              :          unsigned operand, do it in a pseudo-register.  */
    6035              : 
    6036         1118 :       if (to_mode != fmode
    6037         1118 :           || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
    6038          403 :         target = gen_reg_rtx (fmode);
    6039              : 
    6040              :       /* Convert as signed integer to floating.  */
    6041         1118 :       expand_float (target, from, 0);
    6042              : 
    6043              :       /* If FROM is negative (and therefore TO is negative),
    6044              :          correct its value by 2**bitwidth.  */
    6045              : 
    6046         1118 :       do_pending_stack_adjust ();
    6047         1118 :       emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, from_mode,
    6048              :                                0, label);
    6049              : 
    6050              : 
    6051         1118 :       real_2expN (&offset, GET_MODE_PRECISION (from_mode), fmode);
    6052         1118 :       temp = expand_binop (fmode, add_optab, target,
    6053              :                            const_double_from_real_value (offset, fmode),
    6054              :                            target, 0, OPTAB_LIB_WIDEN);
    6055         1118 :       if (temp != target)
    6056            0 :         emit_move_insn (target, temp);
    6057              : 
    6058         1118 :       do_pending_stack_adjust ();
    6059         1118 :       emit_label (label);
    6060         1118 :       goto done;
    6061              :     }
    6062              : 
    6063              :   /* No hardware instruction available; call a library routine.  */
    6064         7400 :     {
    6065         7400 :       rtx libfunc;
    6066         7400 :       rtx_insn *insns;
    6067         7400 :       rtx value;
    6068         7400 :       convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
    6069              : 
    6070         7400 :       if (is_narrower_int_mode (GET_MODE (from), SImode))
    6071          978 :         from = convert_to_mode (SImode, from, unsignedp);
    6072              : 
    6073         7400 :       libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
    6074         7400 :       gcc_assert (libfunc);
    6075              : 
    6076         7400 :       start_sequence ();
    6077              : 
    6078        14800 :       value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
    6079         7400 :                                        GET_MODE (to), from, GET_MODE (from));
    6080         7400 :       insns = end_sequence ();
    6081              : 
    6082         7400 :       emit_libcall_block (insns, target, value,
    6083         7400 :                           gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
    6084              :                                          GET_MODE (to), from));
    6085              :     }
    6086              : 
    6087         8518 :  done:
    6088              : 
    6089              :   /* Copy result to requested destination
    6090              :      if we have been computing in a temp location.  */
    6091              : 
    6092         8518 :   if (target != to)
    6093              :     {
    6094          403 :       if (GET_MODE (target) == GET_MODE (to))
    6095            4 :         emit_move_insn (to, target);
    6096              :       else
    6097          399 :         convert_move (to, target, 0);
    6098              :     }
    6099              : }
    6100              : 
    6101              : /* Generate code to convert FROM to fixed point and store in TO.  FROM
    6102              :    must be floating point.  */
    6103              : 
    6104              : void
    6105        60027 : expand_fix (rtx to, rtx from, int unsignedp)
    6106              : {
    6107        60027 :   enum insn_code icode;
    6108        60027 :   rtx target = to;
    6109        60027 :   machine_mode fmode, imode;
    6110        60027 :   opt_scalar_mode fmode_iter;
    6111        60027 :   bool must_trunc = false;
    6112              : 
    6113              :   /* We first try to find a pair of modes, one real and one integer, at
    6114              :      least as wide as FROM and TO, respectively, in which we can open-code
    6115              :      this conversion.  If the integer mode is wider than the mode of TO,
    6116              :      we can do the conversion either signed or unsigned.  */
    6117              : 
    6118        90593 :   FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
    6119       216199 :     FOR_EACH_MODE_FROM (imode, GET_MODE (to))
    6120              :       {
    6121       185633 :         int doing_unsigned = unsignedp;
    6122              : 
    6123       185633 :         icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
    6124       185633 :         if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
    6125        62529 :           icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
    6126              : 
    6127       141872 :         if (icode != CODE_FOR_nothing)
    6128              :           {
    6129        46942 :             rtx_insn *last = get_last_insn ();
    6130        46942 :             rtx from1 = from;
    6131        46942 :             if (fmode != GET_MODE (from))
    6132              :               {
    6133         1114 :                 if (REAL_MODE_FORMAT (GET_MODE (from))
    6134              :                     == &arm_bfloat_half_format
    6135         1114 :                     && REAL_MODE_FORMAT (fmode) == &ieee_single_format)
    6136              :                   /* The BF -> SF conversions can be just a shift, doesn't
    6137              :                      need to handle sNANs.  */
    6138              :                   {
    6139           46 :                     int save_flag_finite_math_only = flag_finite_math_only;
    6140           46 :                     flag_finite_math_only = true;
    6141           46 :                     from1 = convert_to_mode (fmode, from, 0);
    6142           46 :                     flag_finite_math_only = save_flag_finite_math_only;
    6143              :                   }
    6144              :                 else
    6145         1068 :                   from1 = convert_to_mode (fmode, from, 0);
    6146              :               }
    6147              : 
    6148        46942 :             if (must_trunc)
    6149              :               {
    6150            0 :                 rtx temp = gen_reg_rtx (GET_MODE (from1));
    6151            0 :                 from1 = expand_unop (GET_MODE (from1), ftrunc_optab, from1,
    6152              :                                      temp, 0);
    6153              :               }
    6154              : 
    6155        46942 :             if (imode != GET_MODE (to))
    6156        10792 :               target = gen_reg_rtx (imode);
    6157              : 
    6158        89624 :             if (maybe_emit_unop_insn (icode, target, from1,
    6159              :                                       doing_unsigned ? UNSIGNED_FIX : FIX))
    6160              :               {
    6161        46942 :                 if (target != to)
    6162        10792 :                   convert_move (to, target, unsignedp);
    6163        51023 :                 return;
    6164              :               }
    6165            0 :             delete_insns_since (last);
    6166              :           }
    6167              :       }
    6168              : 
    6169              :   /* For an unsigned conversion, there is one more way to do it.
    6170              :      If we have a signed conversion, we generate code that compares
    6171              :      the real value to the largest representable positive number.  If if
    6172              :      is smaller, the conversion is done normally.  Otherwise, subtract
    6173              :      one plus the highest signed number, convert, and add it back.
    6174              : 
    6175              :      We only need to check all real modes, since we know we didn't find
    6176              :      anything with a wider integer mode.
    6177              : 
    6178              :      This code used to extend FP value into mode wider than the destination.
    6179              :      This is needed for decimal float modes which cannot accurately
    6180              :      represent one plus the highest signed number of the same size, but
    6181              :      not for binary modes.  Consider, for instance conversion from SFmode
    6182              :      into DImode.
    6183              : 
    6184              :      The hot path through the code is dealing with inputs smaller than 2^63
    6185              :      and doing just the conversion, so there is no bits to lose.
    6186              : 
    6187              :      In the other path we know the value is positive in the range 2^63..2^64-1
    6188              :      inclusive.  (as for other input overflow happens and result is undefined)
    6189              :      So we know that the most important bit set in mantissa corresponds to
    6190              :      2^63.  The subtraction of 2^63 should not generate any rounding as it
    6191              :      simply clears out that bit.  The rest is trivial.  */
    6192              : 
    6193        13085 :   scalar_int_mode to_mode;
    6194        13085 :   if (unsignedp
    6195         7457 :       && is_a <scalar_int_mode> (GET_MODE (to), &to_mode)
    6196        20542 :       && HWI_COMPUTABLE_MODE_P (to_mode))
    6197         9083 :     FOR_EACH_MODE_FROM (fmode_iter, as_a <scalar_mode> (GET_MODE (from)))
    6198              :       {
    6199         6878 :         scalar_mode fmode = fmode_iter.require ();
    6200         6878 :         if (CODE_FOR_nothing != can_fix_p (to_mode, fmode,
    6201              :                                            0, &must_trunc)
    6202         6878 :             && (!DECIMAL_FLOAT_MODE_P (fmode)
    6203            0 :                 || (GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (to_mode))))
    6204              :           {
    6205         4075 :             int bitsize;
    6206         4075 :             REAL_VALUE_TYPE offset;
    6207         4075 :             rtx limit;
    6208         4075 :             rtx_code_label *lab1, *lab2;
    6209         4075 :             rtx_insn *insn;
    6210              : 
    6211         4075 :             bitsize = GET_MODE_PRECISION (to_mode);
    6212         4075 :             real_2expN (&offset, bitsize - 1, fmode);
    6213         4075 :             limit = const_double_from_real_value (offset, fmode);
    6214         4075 :             lab1 = gen_label_rtx ();
    6215         4075 :             lab2 = gen_label_rtx ();
    6216              : 
    6217         4075 :             if (fmode != GET_MODE (from))
    6218              :               {
    6219          188 :                 if (REAL_MODE_FORMAT (GET_MODE (from))
    6220              :                     == &arm_bfloat_half_format
    6221          188 :                     && REAL_MODE_FORMAT (fmode) == &ieee_single_format)
    6222              :                   /* The BF -> SF conversions can be just a shift, doesn't
    6223              :                      need to handle sNANs.  */
    6224              :                   {
    6225            6 :                     int save_flag_finite_math_only = flag_finite_math_only;
    6226            6 :                     flag_finite_math_only = true;
    6227            6 :                     from = convert_to_mode (fmode, from, 0);
    6228            6 :                     flag_finite_math_only = save_flag_finite_math_only;
    6229              :                   }
    6230              :                 else
    6231          182 :                   from = convert_to_mode (fmode, from, 0);
    6232              :               }
    6233              : 
    6234              :             /* See if we need to do the subtraction.  */
    6235         4075 :             do_pending_stack_adjust ();
    6236         4075 :             emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX,
    6237         4075 :                                      GET_MODE (from), 0, lab1);
    6238              : 
    6239              :             /* If not, do the signed "fix" and branch around fixup code.  */
    6240         4075 :             expand_fix (to, from, 0);
    6241         4075 :             emit_jump_insn (targetm.gen_jump (lab2));
    6242         4075 :             emit_barrier ();
    6243              : 
    6244              :             /* Otherwise, subtract 2**(N-1), convert to signed number,
    6245              :                then add 2**(N-1).  Do the addition using XOR since this
    6246              :                will often generate better code.  */
    6247         4075 :             emit_label (lab1);
    6248         4075 :             target = expand_binop (GET_MODE (from), sub_optab, from, limit,
    6249              :                                    NULL_RTX, 0, OPTAB_LIB_WIDEN);
    6250         4075 :             expand_fix (to, target, 0);
    6251         4075 :             target = expand_binop (to_mode, xor_optab, to,
    6252              :                                    gen_int_mode
    6253         4075 :                                    (HOST_WIDE_INT_1 << (bitsize - 1),
    6254              :                                     to_mode),
    6255              :                                    to, 1, OPTAB_LIB_WIDEN);
    6256              : 
    6257         4075 :             if (target != to)
    6258            0 :               emit_move_insn (to, target);
    6259              : 
    6260         4075 :             emit_label (lab2);
    6261              : 
    6262         4075 :             if (optab_handler (mov_optab, to_mode) != CODE_FOR_nothing)
    6263              :               {
    6264              :                 /* Make a place for a REG_NOTE and add it.  */
    6265         4075 :                 insn = emit_move_insn (to, to);
    6266         4075 :                 set_dst_reg_note (insn, REG_EQUAL,
    6267              :                                   gen_rtx_fmt_e (UNSIGNED_FIX, to_mode,
    6268              :                                                  copy_rtx (from)),
    6269              :                                   to);
    6270              :               }
    6271              : 
    6272         4075 :             return;
    6273              :           }
    6274              :       }
    6275              : 
    6276              : #ifdef HAVE_SFmode
    6277         9010 :   if (REAL_MODE_FORMAT (GET_MODE (from)) == &arm_bfloat_half_format
    6278         9010 :       && REAL_MODE_FORMAT (SFmode) == &ieee_single_format)
    6279              :     /* We don't have BF -> TI library functions, use BF -> SF -> TI
    6280              :        instead but the BF -> SF conversion can be just a shift, doesn't
    6281              :        need to handle sNANs.  */
    6282              :     {
    6283            6 :       int save_flag_finite_math_only = flag_finite_math_only;
    6284            6 :       flag_finite_math_only = true;
    6285            6 :       from = convert_to_mode (SFmode, from, 0);
    6286            6 :       flag_finite_math_only = save_flag_finite_math_only;
    6287            6 :       expand_fix (to, from, unsignedp);
    6288            6 :       return;
    6289              :     }
    6290              : #endif
    6291              : 
    6292              :   /* We can't do it with an insn, so use a library call.  But first ensure
    6293              :      that the mode of TO is at least as wide as SImode, since those are the
    6294              :      only library calls we know about.  */
    6295              : 
    6296         9004 :   if (is_narrower_int_mode (GET_MODE (to), SImode))
    6297              :     {
    6298         1958 :       target = gen_reg_rtx (SImode);
    6299              : 
    6300         1958 :       expand_fix (target, from, unsignedp);
    6301              :     }
    6302              :   else
    6303              :     {
    6304         7046 :       rtx_insn *insns;
    6305         7046 :       rtx value;
    6306         7046 :       rtx libfunc;
    6307              : 
    6308         7046 :       convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
    6309         7046 :       libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
    6310         7046 :       gcc_assert (libfunc);
    6311              : 
    6312         7046 :       start_sequence ();
    6313              : 
    6314        14092 :       value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
    6315         7046 :                                        GET_MODE (to), from, GET_MODE (from));
    6316         7046 :       insns = end_sequence ();
    6317              : 
    6318         7046 :       emit_libcall_block (insns, target, value,
    6319         7046 :                           gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
    6320              :                                          GET_MODE (to), from));
    6321              :     }
    6322              : 
    6323         9004 :   if (target != to)
    6324              :     {
    6325         1958 :       if (GET_MODE (to) == GET_MODE (target))
    6326            0 :         emit_move_insn (to, target);
    6327              :       else
    6328         1958 :         convert_move (to, target, 0);
    6329              :     }
    6330              : }
    6331              : 
    6332              : 
    6333              : /* Promote integer arguments for a libcall if necessary.
    6334              :    emit_library_call_value cannot do the promotion because it does not
    6335              :    know if it should do a signed or unsigned promotion.  This is because
    6336              :    there are no tree types defined for libcalls.  */
    6337              : 
    6338              : static rtx
    6339            0 : prepare_libcall_arg (rtx arg, int uintp)
    6340              : {
    6341            0 :   scalar_int_mode mode;
    6342            0 :   machine_mode arg_mode;
    6343            0 :   if (is_a <scalar_int_mode> (GET_MODE (arg), &mode))
    6344              :     {
    6345              :       /*  If we need to promote the integer function argument we need to do
    6346              :           it here instead of inside emit_library_call_value because in
    6347              :           emit_library_call_value we don't know if we should do a signed or
    6348              :           unsigned promotion.  */
    6349              : 
    6350            0 :       int unsigned_p = 0;
    6351            0 :       arg_mode = promote_function_mode (NULL_TREE, mode,
    6352              :                                         &unsigned_p, NULL_TREE, 0);
    6353            0 :       if (arg_mode != mode)
    6354            0 :         return convert_to_mode (arg_mode, arg, uintp);
    6355              :     }
    6356              :     return arg;
    6357              : }
    6358              : 
    6359              : /* Generate code to convert FROM or TO a fixed-point.
    6360              :    If UINTP is true, either TO or FROM is an unsigned integer.
    6361              :    If SATP is true, we need to saturate the result.  */
    6362              : 
    6363              : void
    6364            0 : expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
    6365              : {
    6366            0 :   machine_mode to_mode = GET_MODE (to);
    6367            0 :   machine_mode from_mode = GET_MODE (from);
    6368            0 :   convert_optab tab;
    6369            0 :   enum rtx_code this_code;
    6370            0 :   enum insn_code code;
    6371            0 :   rtx_insn *insns;
    6372            0 :   rtx value;
    6373            0 :   rtx libfunc;
    6374              : 
    6375            0 :   if (to_mode == from_mode)
    6376              :     {
    6377            0 :       emit_move_insn (to, from);
    6378            0 :       return;
    6379              :     }
    6380              : 
    6381            0 :   if (uintp)
    6382              :     {
    6383            0 :       tab = satp ? satfractuns_optab : fractuns_optab;
    6384            0 :       this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
    6385              :     }
    6386              :   else
    6387              :     {
    6388            0 :       tab = satp ? satfract_optab : fract_optab;
    6389            0 :       this_code = satp ? SAT_FRACT : FRACT_CONVERT;
    6390              :     }
    6391            0 :   code = convert_optab_handler (tab, to_mode, from_mode);
    6392            0 :   if (code != CODE_FOR_nothing)
    6393              :     {
    6394            0 :       emit_unop_insn (code, to, from, this_code);
    6395            0 :       return;
    6396              :     }
    6397              : 
    6398            0 :   libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
    6399            0 :   gcc_assert (libfunc);
    6400              : 
    6401            0 :   from = prepare_libcall_arg (from, uintp);
    6402            0 :   from_mode = GET_MODE (from);
    6403              : 
    6404            0 :   start_sequence ();
    6405            0 :   value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
    6406              :                                    from, from_mode);
    6407            0 :   insns = end_sequence ();
    6408              : 
    6409            0 :   emit_libcall_block (insns, to, value,
    6410              :                       gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
    6411              : }
    6412              : 
    6413              : /* Generate code to convert FROM to fixed point and store in TO.  FROM
    6414              :    must be floating point, TO must be signed.  Use the conversion optab
    6415              :    TAB to do the conversion.  */
    6416              : 
    6417              : bool
    6418          464 : expand_sfix_optab (rtx to, rtx from, convert_optab tab)
    6419              : {
    6420          464 :   enum insn_code icode;
    6421          464 :   rtx target = to;
    6422          464 :   machine_mode fmode, imode;
    6423              : 
    6424              :   /* We first try to find a pair of modes, one real and one integer, at
    6425              :      least as wide as FROM and TO, respectively, in which we can open-code
    6426              :      this conversion.  If the integer mode is wider than the mode of TO,
    6427              :      we can do the conversion either signed or unsigned.  */
    6428              : 
    6429         1928 :   FOR_EACH_MODE_FROM (fmode, GET_MODE (from))
    6430         8234 :     FOR_EACH_MODE_FROM (imode, GET_MODE (to))
    6431              :       {
    6432         6770 :         icode = convert_optab_handler (tab, imode, fmode,
    6433              :                                        insn_optimization_type ());
    6434         6770 :         if (icode != CODE_FOR_nothing)
    6435              :           {
    6436           52 :             rtx_insn *last = get_last_insn ();
    6437           52 :             if (fmode != GET_MODE (from))
    6438           52 :               from = convert_to_mode (fmode, from, 0);
    6439              : 
    6440           52 :             if (imode != GET_MODE (to))
    6441            0 :               target = gen_reg_rtx (imode);
    6442              : 
    6443           52 :             if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
    6444              :               {
    6445            0 :                 delete_insns_since (last);
    6446            0 :                 continue;
    6447              :               }
    6448           52 :             if (target != to)
    6449            0 :               convert_move (to, target, 0);
    6450              :             return true;
    6451              :           }
    6452              :       }
    6453              : 
    6454              :   return false;
    6455              : }
    6456              : 
    6457              : /* Report whether we have an instruction to perform the operation
    6458              :    specified by CODE on operands of mode MODE.  */
    6459              : bool
    6460     99971701 : have_insn_for (enum rtx_code code, machine_mode mode)
    6461              : {
    6462     99971701 :   return (code_to_optab (code)
    6463     99971701 :           && (optab_handler (code_to_optab (code), mode)
    6464     99971701 :               != CODE_FOR_nothing));
    6465              : }
    6466              : 
    6467              : /* Print information about the current contents of the optabs on
    6468              :    STDERR.  */
    6469              : 
    6470              : DEBUG_FUNCTION void
    6471            0 : debug_optab_libfuncs (void)
    6472              : {
    6473            0 :   int i, j, k;
    6474              : 
    6475              :   /* Dump the arithmetic optabs.  */
    6476            0 :   for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
    6477            0 :     for (j = 0; j < NUM_MACHINE_MODES; ++j)
    6478              :       {
    6479            0 :         rtx l = optab_libfunc ((optab) i, (machine_mode) j);
    6480            0 :         if (l)
    6481              :           {
    6482            0 :             gcc_assert (GET_CODE (l) == SYMBOL_REF);
    6483            0 :             fprintf (stderr, "%s\t%s:\t%s\n",
    6484            0 :                      GET_RTX_NAME (optab_to_code ((optab) i)),
    6485            0 :                      GET_MODE_NAME (j),
    6486              :                      XSTR (l, 0));
    6487              :           }
    6488              :       }
    6489              : 
    6490              :   /* Dump the conversion optabs.  */
    6491            0 :   for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
    6492            0 :     for (j = 0; j < NUM_MACHINE_MODES; ++j)
    6493            0 :       for (k = 0; k < NUM_MACHINE_MODES; ++k)
    6494              :         {
    6495            0 :           rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
    6496              :                                          (machine_mode) k);
    6497            0 :           if (l)
    6498              :             {
    6499            0 :               gcc_assert (GET_CODE (l) == SYMBOL_REF);
    6500            0 :               fprintf (stderr, "%s\t%s\t%s:\t%s\n",
    6501            0 :                        GET_RTX_NAME (optab_to_code ((optab) i)),
    6502            0 :                        GET_MODE_NAME (j),
    6503            0 :                        GET_MODE_NAME (k),
    6504              :                        XSTR (l, 0));
    6505              :             }
    6506              :         }
    6507            0 : }
    6508              : 
    6509              : /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
    6510              :    CODE.  Return 0 on failure.  */
    6511              : 
    6512              : rtx_insn *
    6513            0 : gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
    6514              : {
    6515            0 :   machine_mode mode = GET_MODE (op1);
    6516            0 :   enum insn_code icode;
    6517            0 :   rtx_insn *insn;
    6518            0 :   rtx trap_rtx;
    6519              : 
    6520            0 :   if (mode == VOIDmode)
    6521              :     return 0;
    6522              : 
    6523            0 :   icode = optab_handler (ctrap_optab, mode);
    6524            0 :   if (icode == CODE_FOR_nothing)
    6525              :     return 0;
    6526              : 
    6527              :   /* Some targets only accept a zero trap code.  */
    6528            0 :   if (!insn_operand_matches (icode, 3, tcode))
    6529              :     return 0;
    6530              : 
    6531            0 :   do_pending_stack_adjust ();
    6532            0 :   start_sequence ();
    6533            0 :   prepare_cmp_insn (op1, op2, NULL, code, NULL_RTX, false, OPTAB_DIRECT,
    6534              :                     &trap_rtx, &mode, cbranch_optab);
    6535            0 :   if (!trap_rtx)
    6536              :     insn = NULL;
    6537              :   else
    6538            0 :     insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
    6539              :                             tcode);
    6540              : 
    6541              :   /* If that failed, then give up.  */
    6542            0 :   if (insn == 0)
    6543              :     {
    6544            0 :       end_sequence ();
    6545            0 :       return 0;
    6546              :     }
    6547              : 
    6548            0 :   emit_insn (insn);
    6549            0 :   insn = end_sequence ();
    6550            0 :   return insn;
    6551              : }
    6552              : 
    6553              : /* Return rtx code for TCODE or UNKNOWN.  Use UNSIGNEDP to select signed
    6554              :    or unsigned operation code.  */
    6555              : 
    6556              : enum rtx_code
    6557      1639522 : get_rtx_code_1 (enum tree_code tcode, bool unsignedp)
    6558              : {
    6559      1639522 :   enum rtx_code code;
    6560      1639522 :   switch (tcode)
    6561              :     {
    6562              :     case EQ_EXPR:
    6563              :       code = EQ;
    6564              :       break;
    6565       824521 :     case NE_EXPR:
    6566       824521 :       code = NE;
    6567       824521 :       break;
    6568       149824 :     case LT_EXPR:
    6569       149824 :       code = unsignedp ? LTU : LT;
    6570              :       break;
    6571        74735 :     case LE_EXPR:
    6572        74735 :       code = unsignedp ? LEU : LE;
    6573              :       break;
    6574       115741 :     case GT_EXPR:
    6575       115741 :       code = unsignedp ? GTU : GT;
    6576              :       break;
    6577        81946 :     case GE_EXPR:
    6578        81946 :       code = unsignedp ? GEU : GE;
    6579              :       break;
    6580              : 
    6581         2012 :     case UNORDERED_EXPR:
    6582         2012 :       code = UNORDERED;
    6583         2012 :       break;
    6584         1393 :     case ORDERED_EXPR:
    6585         1393 :       code = ORDERED;
    6586         1393 :       break;
    6587          735 :     case UNLT_EXPR:
    6588          735 :       code = UNLT;
    6589          735 :       break;
    6590         4950 :     case UNLE_EXPR:
    6591         4950 :       code = UNLE;
    6592         4950 :       break;
    6593          726 :     case UNGT_EXPR:
    6594          726 :       code = UNGT;
    6595          726 :       break;
    6596         3754 :     case UNGE_EXPR:
    6597         3754 :       code = UNGE;
    6598         3754 :       break;
    6599          543 :     case UNEQ_EXPR:
    6600          543 :       code = UNEQ;
    6601          543 :       break;
    6602           89 :     case LTGT_EXPR:
    6603           89 :       code = LTGT;
    6604           89 :       break;
    6605              : 
    6606        32814 :     case BIT_AND_EXPR:
    6607        32814 :       code = AND;
    6608        32814 :       break;
    6609              : 
    6610           28 :     case BIT_IOR_EXPR:
    6611           28 :       code = IOR;
    6612           28 :       break;
    6613              : 
    6614            0 :     default:
    6615            0 :       code = UNKNOWN;
    6616            0 :       break;
    6617              :     }
    6618      1639522 :   return code;
    6619              : }
    6620              : 
    6621              : /* Return rtx code for TCODE.  Use UNSIGNEDP to select signed
    6622              :    or unsigned operation code.  */
    6623              : 
    6624              : enum rtx_code
    6625       159948 : get_rtx_code (enum tree_code tcode, bool unsignedp)
    6626              : {
    6627       159948 :   enum rtx_code code = get_rtx_code_1 (tcode, unsignedp);
    6628       159948 :   gcc_assert (code != UNKNOWN);
    6629       159948 :   return code;
    6630              : }
    6631              : 
    6632              : /* Return a comparison rtx of mode CMP_MODE for COND.  Use UNSIGNEDP to
    6633              :    select signed or unsigned operators.  OPNO holds the index of the
    6634              :    first comparison operand for insn ICODE.  Do not generate the
    6635              :    compare instruction itself.  */
    6636              : 
    6637              : rtx
    6638        27045 : vector_compare_rtx (machine_mode cmp_mode, enum tree_code tcode,
    6639              :                     tree t_op0, tree t_op1, bool unsignedp,
    6640              :                     enum insn_code icode, unsigned int opno)
    6641              : {
    6642        27045 :   class expand_operand ops[2];
    6643        27045 :   rtx rtx_op0, rtx_op1;
    6644        27045 :   machine_mode m0, m1;
    6645        27045 :   enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
    6646              : 
    6647        27045 :   gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
    6648              : 
    6649              :   /* Expand operands.  For vector types with scalar modes, e.g. where int64x1_t
    6650              :      has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
    6651              :      cases, use the original mode.  */
    6652        27045 :   rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
    6653              :                          EXPAND_STACK_PARM);
    6654        27045 :   m0 = GET_MODE (rtx_op0);
    6655        27045 :   if (m0 == VOIDmode)
    6656            0 :     m0 = TYPE_MODE (TREE_TYPE (t_op0));
    6657              : 
    6658        27045 :   rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
    6659              :                          EXPAND_STACK_PARM);
    6660        27045 :   m1 = GET_MODE (rtx_op1);
    6661        27045 :   if (m1 == VOIDmode)
    6662            0 :     m1 = TYPE_MODE (TREE_TYPE (t_op1));
    6663              : 
    6664        27045 :   create_input_operand (&ops[0], rtx_op0, m0);
    6665        27045 :   create_input_operand (&ops[1], rtx_op1, m1);
    6666        27045 :   if (!maybe_legitimize_operands (icode, opno, 2, ops))
    6667            0 :     gcc_unreachable ();
    6668        27045 :   return gen_rtx_fmt_ee (rcode, cmp_mode, ops[0].value, ops[1].value);
    6669              : }
    6670              : 
    6671              : /* Check if vec_perm mask SEL is a constant equivalent to a shift of
    6672              :    the first vec_perm operand, assuming the second operand (for left shift
    6673              :    first operand) is a constant vector of zeros.  Return the shift distance
    6674              :    in bits if so, or NULL_RTX if the vec_perm is not a shift.  MODE is the
    6675              :    mode of the value being shifted.  SHIFT_OPTAB is vec_shr_optab for right
    6676              :    shift or vec_shl_optab for left shift.  */
    6677              : static rtx
    6678         3016 : shift_amt_for_vec_perm_mask (machine_mode mode, const vec_perm_indices &sel,
    6679              :                              optab shift_optab)
    6680              : {
    6681         3016 :   unsigned int bitsize = GET_MODE_UNIT_BITSIZE (mode);
    6682         3016 :   poly_int64 first = sel[0];
    6683         6032 :   if (maybe_ge (sel[0], GET_MODE_NUNITS (mode)))
    6684              :     return NULL_RTX;
    6685              : 
    6686         3015 :   if (shift_optab == vec_shl_optab)
    6687              :     {
    6688          881 :       unsigned int nelt;
    6689         1762 :       if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
    6690         3016 :         return NULL_RTX;
    6691          881 :       unsigned firstidx = 0;
    6692         5798 :       for (unsigned int i = 0; i < nelt; i++)
    6693              :         {
    6694         5036 :           if (known_eq (sel[i], nelt))
    6695              :             {
    6696          798 :               if (i == 0 || firstidx)
    6697              :                 return NULL_RTX;
    6698              :               firstidx = i;
    6699              :             }
    6700         8476 :           else if (firstidx
    6701         5886 :                    ? maybe_ne (sel[i], nelt + i - firstidx)
    6702         4238 :                    : maybe_ge (sel[i], nelt))
    6703              :             return NULL_RTX;
    6704              :         }
    6705              : 
    6706          762 :       if (firstidx == 0)
    6707              :         return NULL_RTX;
    6708          762 :       first = firstidx;
    6709              :     }
    6710         2134 :   else if (!sel.series_p (0, 1, first, 1))
    6711              :     {
    6712          376 :       unsigned int nelt;
    6713          752 :       if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
    6714         3016 :         return NULL_RTX;
    6715         2166 :       for (unsigned int i = 1; i < nelt; i++)
    6716              :         {
    6717         1964 :           poly_int64 expected = i + first;
    6718              :           /* Indices into the second vector are all equivalent.  */
    6719         1964 :           if (maybe_lt (sel[i], nelt)
    6720         2770 :               ? maybe_ne (sel[i], expected)
    6721          806 :               : maybe_lt (expected, nelt))
    6722         3016 :             return NULL_RTX;
    6723              :         }
    6724              :     }
    6725              : 
    6726         2722 :   return gen_int_shift_amount (mode, first * bitsize);
    6727              : }
    6728              : 
    6729              : /* A subroutine of expand_vec_perm_var for expanding one vec_perm insn.  */
    6730              : 
    6731              : static rtx
    6732           18 : expand_vec_perm_1 (enum insn_code icode, rtx target,
    6733              :                    rtx v0, rtx v1, rtx sel)
    6734              : {
    6735           18 :   machine_mode tmode = GET_MODE (target);
    6736           18 :   machine_mode smode = GET_MODE (sel);
    6737           18 :   class expand_operand ops[4];
    6738              : 
    6739           18 :   gcc_assert (GET_MODE_CLASS (smode) == MODE_VECTOR_INT
    6740              :               || related_int_vector_mode (tmode).require () == smode);
    6741           18 :   create_output_operand (&ops[0], target, tmode);
    6742           18 :   create_input_operand (&ops[3], sel, smode);
    6743              : 
    6744              :   /* Make an effort to preserve v0 == v1.  The target expander is able to
    6745              :      rely on this to determine if we're permuting a single input operand.  */
    6746           18 :   if (rtx_equal_p (v0, v1))
    6747              :     {
    6748           14 :       if (!insn_operand_matches (icode, 1, v0))
    6749            0 :         v0 = force_reg (tmode, v0);
    6750           14 :       gcc_checking_assert (insn_operand_matches (icode, 1, v0));
    6751           14 :       gcc_checking_assert (insn_operand_matches (icode, 2, v0));
    6752              : 
    6753           14 :       create_fixed_operand (&ops[1], v0);
    6754           14 :       create_fixed_operand (&ops[2], v0);
    6755              :     }
    6756              :   else
    6757              :     {
    6758            4 :       create_input_operand (&ops[1], v0, tmode);
    6759            4 :       create_input_operand (&ops[2], v1, tmode);
    6760              :     }
    6761              : 
    6762           18 :   if (maybe_expand_insn (icode, 4, ops))
    6763           18 :     return ops[0].value;
    6764              :   return NULL_RTX;
    6765              : }
    6766              : 
    6767              : /* Check if vec_perm mask SEL is a constant equivalent to an and operation of
    6768              :    the non-zero vec_perm operand with some mask consisting of 0xffs and 0x00s,
    6769              :    assuming the other vec_perm operand is a constant vector of zeros.  Return
    6770              :    the mask for the equivalent and operation, or NULL_RTX if the vec_perm can
    6771              :    not be modeled as an and.  MODE is the mode of the value being anded.
    6772              :    ZERO_OP0_P is true if the first operand of the vec_perm is a constant vector
    6773              :    of zeros or false if the second operand of the vec_perm is a constant vector
    6774              :    of zeros.  */
    6775              : rtx
    6776            0 : vec_perm_and_mask (machine_mode mode, const vec_perm_indices &sel,
    6777              :                    bool zero_op0_p)
    6778              : {
    6779            0 :   unsigned int nelt;
    6780            0 :   if (!GET_MODE_NUNITS (mode).is_constant (&nelt))
    6781              :     return NULL_RTX;
    6782              : 
    6783            0 :   rtx_vector_builder builder (mode, nelt, 1);
    6784            0 :   machine_mode emode = GET_MODE_INNER (mode);
    6785              : 
    6786            0 :   for (unsigned int i = 0; i < nelt; i++)
    6787              :     {
    6788            0 :       if (zero_op0_p)
    6789              :         {
    6790            0 :           if (known_eq (sel[i], nelt + i))
    6791            0 :             builder.quick_push (CONSTM1_RTX (emode));
    6792            0 :           else if (known_lt (sel[i], nelt))
    6793            0 :             builder.quick_push (CONST0_RTX (emode));
    6794              :           else
    6795            0 :             return NULL_RTX;
    6796              :         }
    6797              :       else
    6798              :         {
    6799            0 :           if (known_eq (sel[i], i))
    6800            0 :             builder.quick_push (CONSTM1_RTX (emode));
    6801            0 :           else if (known_ge (sel[i], nelt))
    6802            0 :             builder.quick_push (CONST0_RTX (emode));
    6803              :           else
    6804              :             return NULL_RTX;
    6805              :         }
    6806              :     }
    6807              : 
    6808            0 :   return builder.build ();
    6809            0 : }
    6810              : 
    6811              : /* Implement a permutation of vectors v0 and v1 using the permutation
    6812              :    vector in SEL and return the result.  Use TARGET to hold the result
    6813              :    if nonnull and convenient.
    6814              : 
    6815              :    MODE is the mode of the result.  V0 and V1 have the same mode, which
    6816              :    has the same element mode as MODE but may have a different number of
    6817              :    elements.  SEL_MODE is the TYPE_MODE associated with SEL, or BLKmode
    6818              :    if SEL isn't known to have a particular mode.  */
    6819              : 
    6820              : rtx
    6821        77167 : expand_vec_perm_const (machine_mode mode, rtx v0, rtx v1,
    6822              :                        const vec_perm_builder &sel, machine_mode sel_mode,
    6823              :                        rtx target)
    6824              : {
    6825        77167 :   if (!target || !register_operand (target, mode))
    6826        62405 :     target = gen_reg_rtx (mode);
    6827              : 
    6828              :   /* Set QIMODE to a different vector mode with byte elements.
    6829              :      If no such mode, or if MODE already has byte elements, use VOIDmode.  */
    6830        77167 :   machine_mode qimode;
    6831        77167 :   if (!qimode_for_vec_perm (mode).exists (&qimode))
    6832        77167 :     qimode = VOIDmode;
    6833              : 
    6834        77167 :   rtx_insn *last = get_last_insn ();
    6835              : 
    6836        77167 :   bool single_arg_p = rtx_equal_p (v0, v1);
    6837              :   /* The selector indexes the concatenation of the inputs, which may have
    6838              :      a different number of elements than the result.  */
    6839        77167 :   machine_mode op_mode = GET_MODE (v0);
    6840        77167 :   gcc_checking_assert (op_mode == GET_MODE (v1));
    6841              :   /* Always specify two input vectors here and leave the target to handle
    6842              :      cases in which the inputs are equal.  Not all backends can cope with
    6843              :      the single-input representation when testing for a double-input
    6844              :      target instruction.  */
    6845       154334 :   vec_perm_indices indices (sel, 2, GET_MODE_NUNITS (op_mode));
    6846              : 
    6847              :   /* See if this can be handled with a vec_shr or vec_shl.  We only do this
    6848              :      if the second (for vec_shr) or first (for vec_shl) vector is all
    6849              :      zeroes.  */
    6850        77167 :   insn_code shift_code = CODE_FOR_nothing;
    6851        77167 :   insn_code shift_code_qi = CODE_FOR_nothing;
    6852        77167 :   optab shift_optab = unknown_optab;
    6853        77167 :   rtx v2 = v0;
    6854        77167 :   if (v1 == CONST0_RTX (op_mode))
    6855              :     shift_optab = vec_shr_optab;
    6856        74969 :   else if (v0 == CONST0_RTX (op_mode))
    6857              :     {
    6858         1205 :       shift_optab = vec_shl_optab;
    6859         1205 :       v2 = v1;
    6860              :     }
    6861        77167 :   if (mode == op_mode
    6862        77167 :       && GET_MODE_NUNITS (mode).is_constant ()
    6863        77167 :       && shift_optab != unknown_optab)
    6864              :     {
    6865         3403 :       shift_code = optab_handler (shift_optab, mode);
    6866         6010 :       shift_code_qi = ((qimode != VOIDmode && qimode != mode)
    6867         6408 :                        ? optab_handler (shift_optab, qimode)
    6868              :                        : CODE_FOR_nothing);
    6869              :     }
    6870         3403 :   if (shift_code != CODE_FOR_nothing || shift_code_qi != CODE_FOR_nothing)
    6871              :     {
    6872         3016 :       rtx shift_amt = shift_amt_for_vec_perm_mask (mode, indices, shift_optab);
    6873         3016 :       if (shift_amt)
    6874              :         {
    6875         2722 :           class expand_operand ops[3];
    6876         2722 :           if (shift_amt == const0_rtx)
    6877         2722 :             return v2;
    6878         2722 :           if (shift_code != CODE_FOR_nothing)
    6879              :             {
    6880         2722 :               create_output_operand (&ops[0], target, mode);
    6881         2722 :               create_input_operand (&ops[1], v2, mode);
    6882         2722 :               create_convert_operand_from_type (&ops[2], shift_amt, sizetype);
    6883         2722 :               if (maybe_expand_insn (shift_code, 3, ops))
    6884         2722 :                 return ops[0].value;
    6885              :             }
    6886            0 :           if (shift_code_qi != CODE_FOR_nothing)
    6887              :             {
    6888            0 :               rtx tmp = gen_reg_rtx (qimode);
    6889            0 :               create_output_operand (&ops[0], tmp, qimode);
    6890            0 :               create_input_operand (&ops[1], gen_lowpart (qimode, v2), qimode);
    6891            0 :               create_convert_operand_from_type (&ops[2], shift_amt, sizetype);
    6892            0 :               if (maybe_expand_insn (shift_code_qi, 3, ops))
    6893            0 :                 return gen_lowpart (mode, ops[0].value);
    6894              :             }
    6895              :         }
    6896              :     }
    6897              : 
    6898        74445 :   if (targetm.vectorize.vec_perm_const != NULL)
    6899              :     {
    6900        74445 :       if (single_arg_p)
    6901        18185 :         v1 = v0;
    6902              : 
    6903        74445 :       if (targetm.vectorize.vec_perm_const (mode, op_mode, target, v0, v1,
    6904              :                                             indices))
    6905              :         return target;
    6906              :     }
    6907              : 
    6908              :   /* The fallbacks below need the inputs to have the result mode.  */
    6909            0 :   if (mode != op_mode)
    6910              :     {
    6911            0 :       delete_insns_since (last);
    6912            0 :       return NULL_RTX;
    6913              :     }
    6914              : 
    6915              :   /* Fall back to a constant byte-based permutation.  */
    6916            0 :   vec_perm_indices qimode_indices;
    6917            0 :   rtx target_qi = NULL_RTX, v0_qi = NULL_RTX, v1_qi = NULL_RTX;
    6918            0 :   if (qimode != VOIDmode)
    6919              :     {
    6920            0 :       qimode_indices.new_expanded_vector (indices, GET_MODE_UNIT_SIZE (mode));
    6921            0 :       target_qi = gen_reg_rtx (qimode);
    6922            0 :       v0_qi = gen_lowpart (qimode, v0);
    6923            0 :       v1_qi = gen_lowpart (qimode, v1);
    6924            0 :       if (targetm.vectorize.vec_perm_const != NULL
    6925            0 :           && targetm.vectorize.vec_perm_const (qimode, qimode, target_qi, v0_qi,
    6926              :                                                v1_qi, qimode_indices))
    6927            0 :         return gen_lowpart (mode, target_qi);
    6928              :     }
    6929              : 
    6930            0 :   v0 = force_reg (mode, v0);
    6931            0 :   if (single_arg_p)
    6932            0 :     v1 = v0;
    6933            0 :   v1 = force_reg (mode, v1);
    6934              : 
    6935              :   /* Otherwise expand as a fully variable permutation.  */
    6936              : 
    6937              :   /* The optabs are only defined for selectors with the same width
    6938              :      as the values being permuted.  */
    6939            0 :   machine_mode required_sel_mode;
    6940            0 :   if (!related_int_vector_mode (mode).exists (&required_sel_mode))
    6941              :     {
    6942            0 :       delete_insns_since (last);
    6943            0 :       return NULL_RTX;
    6944              :     }
    6945              : 
    6946              :   /* We know that it is semantically valid to treat SEL as having SEL_MODE.
    6947              :      If that isn't the mode we want then we need to prove that using
    6948              :      REQUIRED_SEL_MODE is OK.  */
    6949            0 :   if (sel_mode != required_sel_mode)
    6950              :     {
    6951            0 :       if (!selector_fits_mode_p (required_sel_mode, indices))
    6952              :         {
    6953            0 :           delete_insns_since (last);
    6954            0 :           return NULL_RTX;
    6955              :         }
    6956              :       sel_mode = required_sel_mode;
    6957              :     }
    6958              : 
    6959            0 :   insn_code icode = direct_optab_handler (vec_perm_optab, mode);
    6960            0 :   if (icode != CODE_FOR_nothing)
    6961              :     {
    6962            0 :       rtx sel_rtx = vec_perm_indices_to_rtx (sel_mode, indices);
    6963            0 :       rtx tmp = expand_vec_perm_1 (icode, target, v0, v1, sel_rtx);
    6964            0 :       if (tmp)
    6965              :         return tmp;
    6966              :     }
    6967              : 
    6968            0 :   if (qimode != VOIDmode
    6969            0 :       && selector_fits_mode_p (qimode, qimode_indices))
    6970              :     {
    6971            0 :       icode = direct_optab_handler (vec_perm_optab, qimode);
    6972            0 :       if (icode != CODE_FOR_nothing)
    6973              :         {
    6974            0 :           rtx sel_qi = vec_perm_indices_to_rtx (qimode, qimode_indices);
    6975            0 :           rtx tmp = expand_vec_perm_1 (icode, target_qi, v0_qi, v1_qi, sel_qi);
    6976            0 :           if (tmp)
    6977            0 :             return gen_lowpart (mode, tmp);
    6978              :         }
    6979              :     }
    6980              : 
    6981            0 :   delete_insns_since (last);
    6982            0 :   return NULL_RTX;
    6983            0 : }
    6984              : 
    6985              : /* Implement a permutation of vectors v0 and v1 using the permutation
    6986              :    vector in SEL and return the result.  Use TARGET to hold the result
    6987              :    if nonnull and convenient.
    6988              : 
    6989              :    MODE is the mode of the vectors being permuted (V0 and V1).
    6990              :    SEL must have the integer equivalent of MODE and is known to be
    6991              :    unsuitable for permutes with a constant permutation vector.  */
    6992              : 
    6993              : rtx
    6994           18 : expand_vec_perm_var (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
    6995              : {
    6996           18 :   enum insn_code icode;
    6997           18 :   unsigned int i, u;
    6998           18 :   rtx tmp, sel_qi;
    6999              : 
    7000           18 :   u = GET_MODE_UNIT_SIZE (mode);
    7001              : 
    7002           18 :   if (!target || GET_MODE (target) != mode)
    7003            0 :     target = gen_reg_rtx (mode);
    7004              : 
    7005           18 :   icode = direct_optab_handler (vec_perm_optab, mode);
    7006           18 :   if (icode != CODE_FOR_nothing)
    7007              :     {
    7008           18 :       tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
    7009           18 :       if (tmp)
    7010              :         return tmp;
    7011              :     }
    7012              : 
    7013              :   /* As a special case to aid several targets, lower the element-based
    7014              :      permutation to a byte-based permutation and try again.  */
    7015            0 :   machine_mode qimode;
    7016            0 :   if (!qimode_for_vec_perm (mode).exists (&qimode)
    7017            0 :       || maybe_gt (GET_MODE_NUNITS (qimode), GET_MODE_MASK (QImode) + 1))
    7018              :     return NULL_RTX;
    7019            0 :   icode = direct_optab_handler (vec_perm_optab, qimode);
    7020            0 :   if (icode == CODE_FOR_nothing)
    7021              :     return NULL_RTX;
    7022              : 
    7023              :   /* Multiply each element by its byte size.  */
    7024            0 :   machine_mode selmode = GET_MODE (sel);
    7025            0 :   if (u == 2)
    7026            0 :     sel = expand_simple_binop (selmode, PLUS, sel, sel,
    7027              :                                NULL, 0, OPTAB_DIRECT);
    7028              :   else
    7029            0 :     sel = expand_simple_binop (selmode, ASHIFT, sel,
    7030            0 :                                gen_int_shift_amount (selmode, exact_log2 (u)),
    7031              :                                NULL, 0, OPTAB_DIRECT);
    7032            0 :   gcc_assert (sel != NULL);
    7033              : 
    7034              :   /* Broadcast the low byte each element into each of its bytes.
    7035              :      The encoding has U interleaved stepped patterns, one for each
    7036              :      byte of an element.  */
    7037            0 :   vec_perm_builder const_sel (GET_MODE_SIZE (mode), u, 3);
    7038            0 :   unsigned int low_byte_in_u = BYTES_BIG_ENDIAN ? u - 1 : 0;
    7039            0 :   for (i = 0; i < 3; ++i)
    7040            0 :     for (unsigned int j = 0; j < u; ++j)
    7041            0 :       const_sel.quick_push (i * u + low_byte_in_u);
    7042            0 :   sel = gen_lowpart (qimode, sel);
    7043            0 :   sel = expand_vec_perm_const (qimode, sel, sel, const_sel, qimode, NULL);
    7044            0 :   gcc_assert (sel != NULL);
    7045              : 
    7046              :   /* Add the byte offset to each byte element.  */
    7047              :   /* Note that the definition of the indices here is memory ordering,
    7048              :      so there should be no difference between big and little endian.  */
    7049            0 :   rtx_vector_builder byte_indices (qimode, u, 1);
    7050            0 :   for (i = 0; i < u; ++i)
    7051            0 :     byte_indices.quick_push (GEN_INT (i));
    7052            0 :   tmp = byte_indices.build ();
    7053            0 :   sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
    7054              :                                 sel, 0, OPTAB_DIRECT);
    7055            0 :   gcc_assert (sel_qi != NULL);
    7056              : 
    7057            0 :   tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
    7058            0 :   tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
    7059            0 :                            gen_lowpart (qimode, v1), sel_qi);
    7060            0 :   if (tmp)
    7061            0 :     tmp = gen_lowpart (mode, tmp);
    7062            0 :   return tmp;
    7063            0 : }
    7064              : 
    7065              : /* Generate VEC_SERIES_EXPR <OP0, OP1>, returning a value of mode VMODE.
    7066              :    Use TARGET for the result if nonnull and convenient.  */
    7067              : 
    7068              : rtx
    7069            0 : expand_vec_series_expr (machine_mode vmode, rtx op0, rtx op1, rtx target)
    7070              : {
    7071            0 :   class expand_operand ops[3];
    7072            0 :   enum insn_code icode;
    7073            0 :   machine_mode emode = GET_MODE_INNER (vmode);
    7074              : 
    7075            0 :   icode = direct_optab_handler (vec_series_optab, vmode);
    7076            0 :   gcc_assert (icode != CODE_FOR_nothing);
    7077              : 
    7078            0 :   create_output_operand (&ops[0], target, vmode);
    7079            0 :   create_input_operand (&ops[1], op0, emode);
    7080            0 :   create_input_operand (&ops[2], op1, emode);
    7081              : 
    7082            0 :   expand_insn (icode, 3, ops);
    7083            0 :   return ops[0].value;
    7084              : }
    7085              : 
    7086              : /* Generate insns for a vector comparison into a mask.  */
    7087              : 
    7088              : rtx
    7089        27045 : expand_vec_cmp_expr (tree type, tree exp, rtx target)
    7090              : {
    7091        27045 :   class expand_operand ops[4];
    7092        27045 :   enum insn_code icode;
    7093        27045 :   rtx comparison;
    7094        27045 :   machine_mode mask_mode = TYPE_MODE (type);
    7095        27045 :   machine_mode vmode;
    7096        27045 :   bool unsignedp;
    7097        27045 :   tree op0a, op0b;
    7098        27045 :   enum tree_code tcode;
    7099              : 
    7100        27045 :   op0a = TREE_OPERAND (exp, 0);
    7101        27045 :   op0b = TREE_OPERAND (exp, 1);
    7102        27045 :   tcode = TREE_CODE (exp);
    7103              : 
    7104        27045 :   unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
    7105        27045 :   vmode = TYPE_MODE (TREE_TYPE (op0a));
    7106              : 
    7107        27045 :   icode = get_vec_cmp_icode (vmode, mask_mode, unsignedp);
    7108        27045 :   if (icode == CODE_FOR_nothing)
    7109              :     {
    7110          999 :       if (tcode == EQ_EXPR || tcode == NE_EXPR)
    7111          999 :         icode = get_vec_cmp_eq_icode (vmode, mask_mode);
    7112          999 :       if (icode == CODE_FOR_nothing)
    7113              :         return 0;
    7114              :     }
    7115              : 
    7116        27045 :   comparison = vector_compare_rtx (mask_mode, tcode, op0a, op0b,
    7117              :                                    unsignedp, icode, 2);
    7118        27045 :   create_output_operand (&ops[0], target, mask_mode);
    7119        27045 :   create_fixed_operand (&ops[1], comparison);
    7120        27045 :   create_fixed_operand (&ops[2], XEXP (comparison, 0));
    7121        27045 :   create_fixed_operand (&ops[3], XEXP (comparison, 1));
    7122        27045 :   expand_insn (icode, 4, ops);
    7123        27045 :   return ops[0].value;
    7124              : }
    7125              : 
    7126              : /* Expand a highpart multiply.  */
    7127              : 
    7128              : rtx
    7129         3536 : expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
    7130              :                       rtx target, bool uns_p)
    7131              : {
    7132         3536 :   class expand_operand eops[3];
    7133         3536 :   enum insn_code icode;
    7134         3536 :   int method, i;
    7135         3536 :   machine_mode wmode;
    7136         3536 :   rtx m1, m2;
    7137         3536 :   optab tab1, tab2;
    7138              : 
    7139         3536 :   method = can_mult_highpart_p (mode, uns_p);
    7140         3536 :   switch (method)
    7141              :     {
    7142              :     case 0:
    7143              :       return NULL_RTX;
    7144         2800 :     case 1:
    7145         2800 :       tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
    7146         2800 :       return expand_binop (mode, tab1, op0, op1, target, uns_p,
    7147         2800 :                            OPTAB_LIB_WIDEN);
    7148            0 :     case 2:
    7149            0 :       return expmed_mult_highpart_optab (as_a <scalar_int_mode> (mode),
    7150            0 :                                          op0, op1, target, uns_p, INT_MAX);
    7151          727 :     case 3:
    7152          727 :       tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
    7153          528 :       tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
    7154              :       break;
    7155            9 :     case 4:
    7156            9 :       tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
    7157            9 :       tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
    7158              :       if (BYTES_BIG_ENDIAN)
    7159              :         std::swap (tab1, tab2);
    7160              :       break;
    7161            0 :     default:
    7162            0 :       gcc_unreachable ();
    7163              :     }
    7164              : 
    7165          736 :   icode = optab_handler (tab1, mode);
    7166          736 :   wmode = insn_data[icode].operand[0].mode;
    7167         2208 :   gcc_checking_assert (known_eq (2 * GET_MODE_NUNITS (wmode),
    7168              :                                  GET_MODE_NUNITS (mode)));
    7169         2208 :   gcc_checking_assert (known_eq (GET_MODE_SIZE (wmode), GET_MODE_SIZE (mode)));
    7170              : 
    7171          736 :   create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
    7172          736 :   create_input_operand (&eops[1], op0, mode);
    7173          736 :   create_input_operand (&eops[2], op1, mode);
    7174          736 :   expand_insn (icode, 3, eops);
    7175          736 :   m1 = gen_lowpart (mode, eops[0].value);
    7176              : 
    7177          736 :   create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
    7178          736 :   create_input_operand (&eops[1], op0, mode);
    7179          736 :   create_input_operand (&eops[2], op1, mode);
    7180          736 :   expand_insn (optab_handler (tab2, mode), 3, eops);
    7181          736 :   m2 = gen_lowpart (mode, eops[0].value);
    7182              : 
    7183         1472 :   vec_perm_builder sel;
    7184          736 :   if (method == 3)
    7185              :     {
    7186              :       /* The encoding has 2 interleaved stepped patterns.  */
    7187         1454 :       sel.new_vector (GET_MODE_NUNITS (mode), 2, 3);
    7188         5089 :       for (i = 0; i < 6; ++i)
    7189        13086 :         sel.quick_push (!BYTES_BIG_ENDIAN + (i & ~1)
    7190         6543 :                         + ((i & 1) ? GET_MODE_NUNITS (mode) : 0));
    7191              :     }
    7192              :   else
    7193              :     {
    7194              :       /* The encoding has a single interleaved stepped pattern.  */
    7195           18 :       sel.new_vector (GET_MODE_NUNITS (mode), 1, 3);
    7196           36 :       for (i = 0; i < 3; ++i)
    7197           27 :         sel.quick_push (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
    7198              :     }
    7199              : 
    7200          736 :   return expand_vec_perm_const (mode, m1, m2, sel, BLKmode, target);
    7201              : }
    7202              : 
    7203              : /* Helper function to find the MODE_CC set in a sync_compare_and_swap
    7204              :    pattern.  */
    7205              : 
    7206              : static void
    7207            0 : find_cc_set (rtx x, const_rtx pat, void *data)
    7208              : {
    7209            0 :   if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
    7210            0 :       && GET_CODE (pat) == SET)
    7211              :     {
    7212            0 :       rtx *p_cc_reg = (rtx *) data;
    7213            0 :       gcc_assert (!*p_cc_reg);
    7214            0 :       *p_cc_reg = x;
    7215              :     }
    7216            0 : }
    7217              : 
    7218              : /* This is a helper function for the other atomic operations.  This function
    7219              :    emits a loop that contains SEQ that iterates until a compare-and-swap
    7220              :    operation at the end succeeds.  MEM is the memory to be modified.  SEQ is
    7221              :    a set of instructions that takes a value from OLD_REG as an input and
    7222              :    produces a value in NEW_REG as an output.  Before SEQ, OLD_REG will be
    7223              :    set to the current contents of MEM.  After SEQ, a compare-and-swap will
    7224              :    attempt to update MEM with NEW_REG.  The function returns true when the
    7225              :    loop was generated successfully.  */
    7226              : 
    7227              : static bool
    7228         5256 : expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx_insn *seq)
    7229              : {
    7230         5256 :   machine_mode mode = GET_MODE (mem);
    7231         5256 :   rtx_code_label *label;
    7232         5256 :   rtx cmp_reg, success, oldval;
    7233              : 
    7234              :   /* The loop we want to generate looks like
    7235              : 
    7236              :         cmp_reg = mem;
    7237              :       label:
    7238              :         old_reg = cmp_reg;
    7239              :         seq;
    7240              :         (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
    7241              :         if (success)
    7242              :           goto label;
    7243              : 
    7244              :      Note that we only do the plain load from memory once.  Subsequent
    7245              :      iterations use the value loaded by the compare-and-swap pattern.  */
    7246              : 
    7247         5256 :   label = gen_label_rtx ();
    7248         5256 :   cmp_reg = gen_reg_rtx (mode);
    7249              : 
    7250         5256 :   emit_move_insn (cmp_reg, mem);
    7251         5256 :   emit_label (label);
    7252         5256 :   emit_move_insn (old_reg, cmp_reg);
    7253         5256 :   if (seq)
    7254         5243 :     emit_insn (seq);
    7255              : 
    7256         5256 :   success = NULL_RTX;
    7257         5256 :   oldval = cmp_reg;
    7258         5256 :   if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
    7259              :                                        new_reg, false, MEMMODEL_SYNC_SEQ_CST,
    7260              :                                        MEMMODEL_RELAXED))
    7261              :     return false;
    7262              : 
    7263         5256 :   if (oldval != cmp_reg)
    7264            0 :     emit_move_insn (cmp_reg, oldval);
    7265              : 
    7266              :   /* Mark this jump predicted not taken.  */
    7267         5256 :   emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
    7268         5256 :                            GET_MODE (success), 1, label,
    7269              :                            profile_probability::guessed_never ());
    7270         5256 :   return true;
    7271              : }
    7272              : 
    7273              : 
    7274              : /* This function tries to emit an atomic_exchange instruction.  VAL is written
    7275              :    to *MEM using memory model MODEL. The previous contents of *MEM are returned,
    7276              :    using TARGET if possible.  */
    7277              : 
    7278              : static rtx
    7279         3914 : maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
    7280              : {
    7281         3914 :   machine_mode mode = GET_MODE (mem);
    7282         3914 :   enum insn_code icode;
    7283              : 
    7284              :   /* If the target supports the exchange directly, great.  */
    7285         3914 :   icode = direct_optab_handler (atomic_exchange_optab, mode);
    7286         3914 :   if (icode != CODE_FOR_nothing)
    7287              :     {
    7288         3897 :       class expand_operand ops[4];
    7289              : 
    7290         3897 :       create_output_operand (&ops[0], target, mode);
    7291         3897 :       create_fixed_operand (&ops[1], mem);
    7292         3897 :       create_input_operand (&ops[2], val, mode);
    7293         3897 :       create_integer_operand (&ops[3], model);
    7294         3897 :       if (maybe_expand_insn (icode, 4, ops))
    7295         3897 :         return ops[0].value;
    7296              :     }
    7297              : 
    7298              :   return NULL_RTX;
    7299              : }
    7300              : 
    7301              : /* This function tries to implement an atomic exchange operation using
    7302              :    __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
    7303              :    The previous contents of *MEM are returned, using TARGET if possible.
    7304              :    Since this instructionn is an acquire barrier only, stronger memory
    7305              :    models may require additional barriers to be emitted.  */
    7306              : 
    7307              : static rtx
    7308            2 : maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
    7309              :                                    enum memmodel model)
    7310              : {
    7311            2 :   machine_mode mode = GET_MODE (mem);
    7312            2 :   enum insn_code icode;
    7313            2 :   rtx_insn *last_insn = get_last_insn ();
    7314              : 
    7315            2 :   icode = optab_handler (sync_lock_test_and_set_optab, mode);
    7316              : 
    7317              :   /* Legacy sync_lock_test_and_set is an acquire barrier.  If the pattern
    7318              :      exists, and the memory model is stronger than acquire, add a release
    7319              :      barrier before the instruction.  */
    7320              : 
    7321            2 :   if (is_mm_seq_cst (model) || is_mm_release (model) || is_mm_acq_rel (model))
    7322            0 :     expand_mem_thread_fence (model);
    7323              : 
    7324            2 :   if (icode != CODE_FOR_nothing)
    7325              :     {
    7326            0 :       class expand_operand ops[3];
    7327            0 :       create_output_operand (&ops[0], target, mode);
    7328            0 :       create_fixed_operand (&ops[1], mem);
    7329            0 :       create_input_operand (&ops[2], val, mode);
    7330            0 :       if (maybe_expand_insn (icode, 3, ops))
    7331            0 :         return ops[0].value;
    7332              :     }
    7333              : 
    7334              :   /* If an external test-and-set libcall is provided, use that instead of
    7335              :      any external compare-and-swap that we might get from the compare-and-
    7336              :      swap-loop expansion later.  */
    7337            2 :   if (!can_compare_and_swap_p (mode, false))
    7338              :     {
    7339            2 :       rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
    7340            2 :       if (libfunc != NULL)
    7341              :         {
    7342            0 :           rtx addr;
    7343              : 
    7344            0 :           addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
    7345            0 :           return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
    7346              :                                           mode, addr, ptr_mode,
    7347            0 :                                           val, mode);
    7348              :         }
    7349              :     }
    7350              : 
    7351              :   /* If the test_and_set can't be emitted, eliminate any barrier that might
    7352              :      have been emitted.  */
    7353            2 :   delete_insns_since (last_insn);
    7354            2 :   return NULL_RTX;
    7355              : }
    7356              : 
    7357              : /* This function tries to implement an atomic exchange operation using a
    7358              :    compare_and_swap loop. VAL is written to *MEM.  The previous contents of
    7359              :    *MEM are returned, using TARGET if possible.  No memory model is required
    7360              :    since a compare_and_swap loop is seq-cst.  */
    7361              : 
    7362              : static rtx
    7363           17 : maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
    7364              : {
    7365           17 :   machine_mode mode = GET_MODE (mem);
    7366              : 
    7367           17 :   if (can_compare_and_swap_p (mode, true))
    7368              :     {
    7369           13 :       rtx_insn *start = get_last_insn ();
    7370              :       /* Force val into a register if it could change value when the
    7371              :          atomic insn updates mem.  */
    7372           13 :       if (reg_overlap_mentioned_p (mem, val))
    7373            2 :         val = force_reg (mode, val);
    7374           13 :       if (!target || !register_operand (target, mode))
    7375            1 :         target = gen_reg_rtx (mode);
    7376           13 :       if (expand_compare_and_swap_loop (mem, target, val, nullptr))
    7377              :         return target;
    7378              :       else
    7379            0 :         delete_insns_since (start);
    7380              :     }
    7381              : 
    7382              :   return NULL_RTX;
    7383              : }
    7384              : 
    7385              : /* This function tries to implement an atomic test-and-set operation
    7386              :    using the atomic_test_and_set instruction pattern.  A boolean value
    7387              :    is returned from the operation, using TARGET if possible.  */
    7388              : 
    7389              : static rtx
    7390          268 : maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
    7391              : {
    7392          268 :   machine_mode pat_bool_mode;
    7393          268 :   class expand_operand ops[3];
    7394              : 
    7395          268 :   if (!targetm.have_atomic_test_and_set ())
    7396              :     return NULL_RTX;
    7397              : 
    7398              :   /* While we always get QImode from __atomic_test_and_set, we get
    7399              :      other memory modes from __sync_lock_test_and_set.  Note that we
    7400              :      use no endian adjustment here.  This matches the 4.6 behavior
    7401              :      in the Sparc backend.  */
    7402            0 :   enum insn_code icode = targetm.code_for_atomic_test_and_set;
    7403            0 :   gcc_checking_assert (insn_data[icode].operand[1].mode == QImode);
    7404            0 :   if (GET_MODE (mem) != QImode)
    7405            0 :     mem = adjust_address_nv (mem, QImode, 0);
    7406              : 
    7407            0 :   pat_bool_mode = insn_data[icode].operand[0].mode;
    7408            0 :   create_output_operand (&ops[0], target, pat_bool_mode);
    7409            0 :   create_fixed_operand (&ops[1], mem);
    7410            0 :   create_integer_operand (&ops[2], model);
    7411              : 
    7412            0 :   if (maybe_expand_insn (icode, 3, ops))
    7413            0 :     return ops[0].value;
    7414              :   return NULL_RTX;
    7415              : }
    7416              : 
    7417              : /* This function expands the legacy _sync_lock test_and_set operation which is
    7418              :    generally an atomic exchange.  Some limited targets only allow the
    7419              :    constant 1 to be stored.  This is an ACQUIRE operation.
    7420              : 
    7421              :    TARGET is an optional place to stick the return value.
    7422              :    MEM is where VAL is stored.  */
    7423              : 
    7424              : rtx
    7425          326 : expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
    7426              : {
    7427          326 :   rtx ret;
    7428              : 
    7429              :   /* Try an atomic_exchange first.  */
    7430          326 :   ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_SYNC_ACQUIRE);
    7431          326 :   if (ret)
    7432              :     return ret;
    7433              : 
    7434            2 :   ret = maybe_emit_sync_lock_test_and_set (target, mem, val,
    7435              :                                            MEMMODEL_SYNC_ACQUIRE);
    7436            2 :   if (ret)
    7437              :     return ret;
    7438              : 
    7439            2 :   ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
    7440            2 :   if (ret)
    7441              :     return ret;
    7442              : 
    7443              :   /* If there are no other options, try atomic_test_and_set if the value
    7444              :      being stored is 1.  */
    7445            2 :   if (val == const1_rtx)
    7446            2 :     ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_SYNC_ACQUIRE);
    7447              : 
    7448              :   return ret;
    7449              : }
    7450              : 
    7451              : /* This function expands the atomic test_and_set operation:
    7452              :    atomically store a boolean TRUE into MEM and return the previous value.
    7453              : 
    7454              :    MEMMODEL is the memory model variant to use.
    7455              :    TARGET is an optional place to stick the return value.  */
    7456              : 
    7457              : rtx
    7458          266 : expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
    7459              : {
    7460          266 :   machine_mode mode = GET_MODE (mem);
    7461          266 :   rtx ret, trueval, subtarget;
    7462              : 
    7463          266 :   ret = maybe_emit_atomic_test_and_set (target, mem, model);
    7464          266 :   if (ret)
    7465              :     return ret;
    7466              : 
    7467              :   /* Be binary compatible with non-default settings of trueval, and different
    7468              :      cpu revisions.  E.g. one revision may have atomic-test-and-set, but
    7469              :      another only has atomic-exchange.  */
    7470          266 :   if (targetm.atomic_test_and_set_trueval == 1)
    7471              :     {
    7472          266 :       trueval = const1_rtx;
    7473          266 :       subtarget = target ? target : gen_reg_rtx (mode);
    7474              :     }
    7475              :   else
    7476              :     {
    7477            0 :       trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
    7478            0 :       subtarget = gen_reg_rtx (mode);
    7479              :     }
    7480              : 
    7481              :   /* Try the atomic-exchange optab...  */
    7482          266 :   ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
    7483              : 
    7484              :   /* ... then an atomic-compare-and-swap loop ... */
    7485          266 :   if (!ret)
    7486            0 :     ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
    7487              : 
    7488              :   /* ... before trying the vaguely defined legacy lock_test_and_set. */
    7489            0 :   if (!ret)
    7490            0 :     ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
    7491              : 
    7492              :   /* Recall that the legacy lock_test_and_set optab was allowed to do magic
    7493              :      things with the value 1.  Thus we try again without trueval.  */
    7494          266 :   if (!ret && targetm.atomic_test_and_set_trueval != 1)
    7495              :     {
    7496            0 :       ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
    7497              : 
    7498            0 :       if (ret)
    7499              :         {
    7500              :           /* Rectify the not-one trueval.  */
    7501            0 :           ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
    7502            0 :           gcc_assert (ret);
    7503              :         }
    7504              :     }
    7505              : 
    7506              :   return ret;
    7507              : }
    7508              : 
    7509              : /* This function expands the atomic exchange operation:
    7510              :    atomically store VAL in MEM and return the previous value in MEM.
    7511              : 
    7512              :    MEMMODEL is the memory model variant to use.
    7513              :    TARGET is an optional place to stick the return value.  */
    7514              : 
    7515              : rtx
    7516         2859 : expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
    7517              : {
    7518         2859 :   machine_mode mode = GET_MODE (mem);
    7519         2859 :   rtx ret;
    7520              : 
    7521              :   /* If loads are not atomic for the required size and we are not called to
    7522              :      provide a __sync builtin, do not do anything so that we stay consistent
    7523              :      with atomic loads of the same size.  */
    7524         2859 :   if (!can_atomic_load_p (mode) && !is_mm_sync (model))
    7525              :     return NULL_RTX;
    7526              : 
    7527         2828 :   ret = maybe_emit_atomic_exchange (target, mem, val, model);
    7528              : 
    7529              :   /* Next try a compare-and-swap loop for the exchange.  */
    7530         2828 :   if (!ret)
    7531           13 :     ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
    7532              : 
    7533              :   return ret;
    7534              : }
    7535              : 
    7536              : /* This function expands the atomic compare exchange operation:
    7537              : 
    7538              :    *PTARGET_BOOL is an optional place to store the boolean success/failure.
    7539              :    *PTARGET_OVAL is an optional place to store the old value from memory.
    7540              :    Both target parameters may be NULL or const0_rtx to indicate that we do
    7541              :    not care about that return value.  Both target parameters are updated on
    7542              :    success to the actual location of the corresponding result.
    7543              : 
    7544              :    MEMMODEL is the memory model variant to use.
    7545              : 
    7546              :    The return value of the function is true for success.  */
    7547              : 
    7548              : bool
    7549        29359 : expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
    7550              :                                 rtx mem, rtx expected, rtx desired,
    7551              :                                 bool is_weak, enum memmodel succ_model,
    7552              :                                 enum memmodel fail_model)
    7553              : {
    7554        29359 :   machine_mode mode = GET_MODE (mem);
    7555        29359 :   class expand_operand ops[8];
    7556        29359 :   enum insn_code icode;
    7557        29359 :   rtx target_oval, target_bool = NULL_RTX;
    7558        29359 :   rtx libfunc;
    7559              : 
    7560              :   /* If loads are not atomic for the required size and we are not called to
    7561              :      provide a __sync builtin, do not do anything so that we stay consistent
    7562              :      with atomic loads of the same size.  */
    7563        29359 :   if (!can_atomic_load_p (mode) && !is_mm_sync (succ_model))
    7564              :     return false;
    7565              : 
    7566              :   /* Load expected into a register for the compare and swap.  */
    7567        27610 :   if (MEM_P (expected))
    7568         7645 :     expected = copy_to_reg (expected);
    7569              : 
    7570              :   /* Make sure we always have some place to put the return oldval.
    7571              :      Further, make sure that place is distinct from the input expected,
    7572              :      just in case we need that path down below.  */
    7573        27610 :   if (ptarget_oval && *ptarget_oval == const0_rtx)
    7574              :     ptarget_oval = NULL;
    7575              : 
    7576        27379 :   if (ptarget_oval == NULL
    7577        27379 :       || (target_oval = *ptarget_oval) == NULL
    7578         5486 :       || reg_overlap_mentioned_p (expected, target_oval))
    7579        22202 :     target_oval = gen_reg_rtx (mode);
    7580              : 
    7581        27610 :   icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
    7582        27610 :   if (icode != CODE_FOR_nothing)
    7583              :     {
    7584        27607 :       machine_mode bool_mode = insn_data[icode].operand[0].mode;
    7585              : 
    7586        27607 :       if (ptarget_bool && *ptarget_bool == const0_rtx)
    7587              :         ptarget_bool = NULL;
    7588              : 
    7589              :       /* Make sure we always have a place for the bool operand.  */
    7590        27352 :       if (ptarget_bool == NULL
    7591        27352 :           || (target_bool = *ptarget_bool) == NULL
    7592         7532 :           || GET_MODE (target_bool) != bool_mode)
    7593        20075 :         target_bool = gen_reg_rtx (bool_mode);
    7594              : 
    7595              :       /* Emit the compare_and_swap.  */
    7596        27607 :       create_output_operand (&ops[0], target_bool, bool_mode);
    7597        27607 :       create_output_operand (&ops[1], target_oval, mode);
    7598        27607 :       create_fixed_operand (&ops[2], mem);
    7599        27607 :       create_input_operand (&ops[3], expected, mode);
    7600        27607 :       create_input_operand (&ops[4], desired, mode);
    7601        27607 :       create_integer_operand (&ops[5], is_weak);
    7602        27607 :       create_integer_operand (&ops[6], succ_model);
    7603        27607 :       create_integer_operand (&ops[7], fail_model);
    7604        27607 :       if (maybe_expand_insn (icode, 8, ops))
    7605              :         {
    7606              :           /* Return success/failure.  */
    7607        27607 :           target_bool = ops[0].value;
    7608        27607 :           target_oval = ops[1].value;
    7609        27607 :           goto success;
    7610              :         }
    7611              :     }
    7612              : 
    7613              :   /* Otherwise fall back to the original __sync_val_compare_and_swap
    7614              :      which is always seq-cst.  */
    7615            3 :   icode = optab_handler (sync_compare_and_swap_optab, mode);
    7616            3 :   if (icode != CODE_FOR_nothing)
    7617              :     {
    7618            0 :       rtx cc_reg;
    7619              : 
    7620            0 :       create_output_operand (&ops[0], target_oval, mode);
    7621            0 :       create_fixed_operand (&ops[1], mem);
    7622            0 :       create_input_operand (&ops[2], expected, mode);
    7623            0 :       create_input_operand (&ops[3], desired, mode);
    7624            0 :       if (!maybe_expand_insn (icode, 4, ops))
    7625            0 :         return false;
    7626              : 
    7627            0 :       target_oval = ops[0].value;
    7628              : 
    7629              :       /* If the caller isn't interested in the boolean return value,
    7630              :          skip the computation of it.  */
    7631            0 :       if (ptarget_bool == NULL)
    7632            0 :         goto success;
    7633              : 
    7634              :       /* Otherwise, work out if the compare-and-swap succeeded.  */
    7635            0 :       cc_reg = NULL_RTX;
    7636            0 :       if (have_insn_for (COMPARE, CCmode))
    7637            0 :         note_stores (get_last_insn (), find_cc_set, &cc_reg);
    7638            0 :       if (cc_reg)
    7639              :         {
    7640            0 :           target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
    7641              :                                                const0_rtx, VOIDmode, 0, 1);
    7642            0 :           goto success;
    7643              :         }
    7644            0 :       goto success_bool_from_val;
    7645              :     }
    7646              : 
    7647              :   /* Also check for library support for __sync_val_compare_and_swap.  */
    7648            3 :   libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
    7649            3 :   if (libfunc != NULL)
    7650              :     {
    7651            0 :       rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
    7652            0 :       rtx target = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
    7653              :                                             mode, addr, ptr_mode,
    7654              :                                             expected, mode, desired, mode);
    7655            0 :       emit_move_insn (target_oval, target);
    7656              : 
    7657              :       /* Compute the boolean return value only if requested.  */
    7658            0 :       if (ptarget_bool)
    7659            0 :         goto success_bool_from_val;
    7660              :       else
    7661            0 :         goto success;
    7662              :     }
    7663              : 
    7664              :   /* Failure.  */
    7665              :   return false;
    7666              : 
    7667            0 :  success_bool_from_val:
    7668            0 :    target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
    7669              :                                         expected, VOIDmode, 1, 1);
    7670        27607 :  success:
    7671              :   /* Make sure that the oval output winds up where the caller asked.  */
    7672        27607 :   if (ptarget_oval)
    7673        27379 :     *ptarget_oval = target_oval;
    7674        27607 :   if (ptarget_bool)
    7675        27352 :     *ptarget_bool = target_bool;
    7676              :   return true;
    7677              : }
    7678              : 
    7679              : /* Generate asm volatile("" : : : "memory") as the memory blockage.  */
    7680              : 
    7681              : static void
    7682            0 : expand_asm_memory_blockage (void)
    7683              : {
    7684            0 :   rtx asm_op, clob;
    7685              : 
    7686            0 :   asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, "", "", 0,
    7687              :                                  rtvec_alloc (0), rtvec_alloc (0),
    7688              :                                  rtvec_alloc (0), UNKNOWN_LOCATION);
    7689            0 :   MEM_VOLATILE_P (asm_op) = 1;
    7690              : 
    7691            0 :   clob = gen_rtx_SCRATCH (VOIDmode);
    7692            0 :   clob = gen_rtx_MEM (BLKmode, clob);
    7693            0 :   clob = gen_rtx_CLOBBER (VOIDmode, clob);
    7694              : 
    7695            0 :   emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
    7696            0 : }
    7697              : 
    7698              : /* Do not propagate memory accesses across this point.  */
    7699              : 
    7700              : static void
    7701       116899 : expand_memory_blockage (void)
    7702              : {
    7703       116899 :   if (targetm.have_memory_blockage ())
    7704       116899 :     emit_insn (targetm.gen_memory_blockage ());
    7705              :   else
    7706            0 :     expand_asm_memory_blockage ();
    7707       116899 : }
    7708              : 
    7709              : /* Generate asm volatile("" : : : "memory") as a memory blockage, at the
    7710              :    same time clobbering the register set specified by REGS.  */
    7711              : 
    7712              : void
    7713          131 : expand_asm_reg_clobber_mem_blockage (HARD_REG_SET regs)
    7714              : {
    7715          131 :   rtx asm_op, clob_mem;
    7716              : 
    7717          131 :   unsigned int num_of_regs = 0;
    7718          131 :   unsigned int i;
    7719              : 
    7720          131 :   num_of_regs = hard_reg_set_popcount (regs);
    7721              : 
    7722          131 :   asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, "", "", 0,
    7723              :                                  rtvec_alloc (0), rtvec_alloc (0),
    7724              :                                  rtvec_alloc (0), UNKNOWN_LOCATION);
    7725          131 :   MEM_VOLATILE_P (asm_op) = 1;
    7726              : 
    7727          131 :   rtvec v = rtvec_alloc (num_of_regs + 2);
    7728              : 
    7729          131 :   clob_mem = gen_rtx_SCRATCH (VOIDmode);
    7730          131 :   clob_mem = gen_rtx_MEM (BLKmode, clob_mem);
    7731          131 :   clob_mem = gen_rtx_CLOBBER (VOIDmode, clob_mem);
    7732              : 
    7733          131 :   RTVEC_ELT (v, 0) = asm_op;
    7734          131 :   RTVEC_ELT (v, 1) = clob_mem;
    7735              : 
    7736          131 :   if (num_of_regs > 0)
    7737              :     {
    7738          131 :       unsigned int j = 2;
    7739          131 :       hard_reg_set_iterator hrsi2;
    7740          131 :       i = 0;
    7741         1250 :       EXECUTE_IF_SET_IN_HARD_REG_SET (regs, 0, i, hrsi2)
    7742              :         {
    7743         1119 :           RTVEC_ELT (v, j) = gen_rtx_CLOBBER (VOIDmode, regno_reg_rtx[i]);
    7744         1119 :           j++;
    7745              :         }
    7746          131 :       gcc_assert (j == (num_of_regs + 2));
    7747              :     }
    7748              : 
    7749          131 :   emit_insn (gen_rtx_PARALLEL (VOIDmode, v));
    7750          131 : }
    7751              : 
    7752              : /* This routine will either emit the mem_thread_fence pattern or issue a
    7753              :    sync_synchronize to generate a fence for memory model MEMMODEL.  */
    7754              : 
    7755              : void
    7756          994 : expand_mem_thread_fence (enum memmodel model)
    7757              : {
    7758          994 :   if (is_mm_relaxed (model))
    7759              :     return;
    7760          985 :   if (targetm.have_mem_thread_fence ())
    7761              :     {
    7762          985 :       emit_insn (targetm.gen_mem_thread_fence (GEN_INT (model)));
    7763          985 :       expand_memory_blockage ();
    7764              :     }
    7765            0 :   else if (targetm.have_memory_barrier ())
    7766            0 :     emit_insn (targetm.gen_memory_barrier ());
    7767            0 :   else if (synchronize_libfunc != NULL_RTX)
    7768            0 :     emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode);
    7769              :   else
    7770            0 :     expand_memory_blockage ();
    7771              : }
    7772              : 
    7773              : /* Emit a signal fence with given memory model.  */
    7774              : 
    7775              : void
    7776           60 : expand_mem_signal_fence (enum memmodel model)
    7777              : {
    7778              :   /* No machine barrier is required to implement a signal fence, but
    7779              :      a compiler memory barrier must be issued, except for relaxed MM.  */
    7780           60 :   if (!is_mm_relaxed (model))
    7781           51 :     expand_memory_blockage ();
    7782           60 : }
    7783              : 
    7784              : /* This function expands the atomic load operation:
    7785              :    return the atomically loaded value in MEM.
    7786              : 
    7787              :    MEMMODEL is the memory model variant to use.
    7788              :    TARGET is an option place to stick the return value.  */
    7789              : 
    7790              : rtx
    7791        71699 : expand_atomic_load (rtx target, rtx mem, enum memmodel model)
    7792              : {
    7793        71699 :   machine_mode mode = GET_MODE (mem);
    7794        71699 :   enum insn_code icode;
    7795              : 
    7796              :   /* If the target supports the load directly, great.  */
    7797        71699 :   icode = direct_optab_handler (atomic_load_optab, mode);
    7798        71699 :   if (icode != CODE_FOR_nothing)
    7799              :     {
    7800        67871 :       class expand_operand ops[3];
    7801        67871 :       rtx_insn *last = get_last_insn ();
    7802        67871 :       if (is_mm_seq_cst (model))
    7803        38348 :         expand_memory_blockage ();
    7804              : 
    7805        67871 :       create_output_operand (&ops[0], target, mode);
    7806        67871 :       create_fixed_operand (&ops[1], mem);
    7807        67871 :       create_integer_operand (&ops[2], model);
    7808        67871 :       if (maybe_expand_insn (icode, 3, ops))
    7809              :         {
    7810        67871 :           if (!is_mm_relaxed (model))
    7811        52653 :             expand_memory_blockage ();
    7812        67871 :           return ops[0].value;
    7813              :         }
    7814            0 :       delete_insns_since (last);
    7815              :     }
    7816              : 
    7817              :   /* If the size of the object is greater than word size on this target,
    7818              :      then we assume that a load will not be atomic.  We could try to
    7819              :      emulate a load with a compare-and-swap operation, but the store that
    7820              :      doing this could result in would be incorrect if this is a volatile
    7821              :      atomic load or targeting read-only-mapped memory.  */
    7822         3828 :   if (maybe_gt (GET_MODE_PRECISION (mode), BITS_PER_WORD))
    7823              :     /* If there is no atomic load, leave the library call.  */
    7824              :     return NULL_RTX;
    7825              : 
    7826              :   /* Otherwise assume loads are atomic, and emit the proper barriers.  */
    7827            0 :   if (!target || target == const0_rtx)
    7828            0 :     target = gen_reg_rtx (mode);
    7829              : 
    7830              :   /* For SEQ_CST, emit a barrier before the load.  */
    7831            0 :   if (is_mm_seq_cst (model))
    7832            0 :     expand_mem_thread_fence (model);
    7833              : 
    7834            0 :   emit_move_insn (target, mem);
    7835              : 
    7836              :   /* Emit the appropriate barrier after the load.  */
    7837            0 :   expand_mem_thread_fence (model);
    7838              : 
    7839            0 :   return target;
    7840              : }
    7841              : 
    7842              : /* This function expands the atomic store operation:
    7843              :    Atomically store VAL in MEM.
    7844              :    MEMMODEL is the memory model variant to use.
    7845              :    USE_RELEASE is true if __sync_lock_release can be used as a fall back.
    7846              :    function returns const0_rtx if a pattern was emitted.  */
    7847              : 
    7848              : rtx
    7849        17726 : expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
    7850              : {
    7851        17726 :   machine_mode mode = GET_MODE (mem);
    7852        17726 :   enum insn_code icode;
    7853        17726 :   class expand_operand ops[3];
    7854              : 
    7855              :   /* If the target supports the store directly, great.  */
    7856        17726 :   icode = direct_optab_handler (atomic_store_optab, mode);
    7857        17726 :   if (icode != CODE_FOR_nothing)
    7858              :     {
    7859        16123 :       rtx_insn *last = get_last_insn ();
    7860        16123 :       if (!is_mm_relaxed (model))
    7861        13300 :         expand_memory_blockage ();
    7862        16123 :       create_fixed_operand (&ops[0], mem);
    7863        16123 :       create_input_operand (&ops[1], val, mode);
    7864        16123 :       create_integer_operand (&ops[2], model);
    7865        16123 :       if (maybe_expand_insn (icode, 3, ops))
    7866              :         {
    7867        16123 :           if (is_mm_seq_cst (model))
    7868        11562 :             expand_memory_blockage ();
    7869        16123 :           return const0_rtx;
    7870              :         }
    7871            0 :       delete_insns_since (last);
    7872              :     }
    7873              : 
    7874              :   /* If using __sync_lock_release is a viable alternative, try it.
    7875              :      Note that this will not be set to true if we are expanding a generic
    7876              :      __atomic_store_n.  */
    7877         1603 :   if (use_release)
    7878              :     {
    7879            2 :       icode = direct_optab_handler (sync_lock_release_optab, mode);
    7880            2 :       if (icode != CODE_FOR_nothing)
    7881              :         {
    7882            0 :           create_fixed_operand (&ops[0], mem);
    7883            0 :           create_input_operand (&ops[1], const0_rtx, mode);
    7884            0 :           if (maybe_expand_insn (icode, 2, ops))
    7885              :             {
    7886              :               /* lock_release is only a release barrier.  */
    7887            0 :               if (is_mm_seq_cst (model))
    7888            0 :                 expand_mem_thread_fence (model);
    7889            0 :               return const0_rtx;
    7890              :             }
    7891              :         }
    7892              :     }
    7893              : 
    7894              :   /* If the size of the object is greater than word size on this target,
    7895              :      a default store will not be atomic.  */
    7896         1603 :   if (maybe_gt (GET_MODE_PRECISION (mode), BITS_PER_WORD))
    7897              :     {
    7898              :       /* If loads are atomic or we are called to provide a __sync builtin,
    7899              :          we can try a atomic_exchange and throw away the result.  Otherwise,
    7900              :          don't do anything so that we do not create an inconsistency between
    7901              :          loads and stores.  */
    7902         1603 :       if (can_atomic_load_p (mode) || is_mm_sync (model))
    7903              :         {
    7904            2 :           rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
    7905            2 :           if (!target)
    7906            2 :             target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem,
    7907              :                                                                 val);
    7908            2 :           if (target)
    7909            0 :             return const0_rtx;
    7910              :         }
    7911              :         return NULL_RTX;
    7912              :     }
    7913              : 
    7914              :   /* Otherwise assume stores are atomic, and emit the proper barriers.  */
    7915            0 :   expand_mem_thread_fence (model);
    7916              : 
    7917            0 :   emit_move_insn (mem, val);
    7918              : 
    7919              :   /* For SEQ_CST, also emit a barrier after the store.  */
    7920            0 :   if (is_mm_seq_cst (model))
    7921            0 :     expand_mem_thread_fence (model);
    7922              : 
    7923            0 :   return const0_rtx;
    7924              : }
    7925              : 
    7926              : 
    7927              : /* Structure containing the pointers and values required to process the
    7928              :    various forms of the atomic_fetch_op and atomic_op_fetch builtins.  */
    7929              : 
    7930              : struct atomic_op_functions
    7931              : {
    7932              :   direct_optab mem_fetch_before;
    7933              :   direct_optab mem_fetch_after;
    7934              :   direct_optab mem_no_result;
    7935              :   optab fetch_before;
    7936              :   optab fetch_after;
    7937              :   direct_optab no_result;
    7938              :   enum rtx_code reverse_code;
    7939              : };
    7940              : 
    7941              : 
    7942              : /* Fill in structure pointed to by OP with the various optab entries for an
    7943              :    operation of type CODE.  */
    7944              : 
    7945              : static void
    7946        35481 : get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
    7947              : {
    7948        35481 :   gcc_assert (op != NULL);
    7949              : 
    7950              :   /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
    7951              :      in the source code during compilation, and the optab entries are not
    7952              :      computable until runtime.  Fill in the values at runtime.  */
    7953        35481 :   switch (code)
    7954              :     {
    7955        22854 :     case PLUS:
    7956        22854 :       op->mem_fetch_before = atomic_fetch_add_optab;
    7957        22854 :       op->mem_fetch_after = atomic_add_fetch_optab;
    7958        22854 :       op->mem_no_result = atomic_add_optab;
    7959        22854 :       op->fetch_before = sync_old_add_optab;
    7960        22854 :       op->fetch_after = sync_new_add_optab;
    7961        22854 :       op->no_result = sync_add_optab;
    7962        22854 :       op->reverse_code = MINUS;
    7963        22854 :       break;
    7964         4843 :     case MINUS:
    7965         4843 :       op->mem_fetch_before = atomic_fetch_sub_optab;
    7966         4843 :       op->mem_fetch_after = atomic_sub_fetch_optab;
    7967         4843 :       op->mem_no_result = atomic_sub_optab;
    7968         4843 :       op->fetch_before = sync_old_sub_optab;
    7969         4843 :       op->fetch_after = sync_new_sub_optab;
    7970         4843 :       op->no_result = sync_sub_optab;
    7971         4843 :       op->reverse_code = PLUS;
    7972         4843 :       break;
    7973         2268 :     case XOR:
    7974         2268 :       op->mem_fetch_before = atomic_fetch_xor_optab;
    7975         2268 :       op->mem_fetch_after = atomic_xor_fetch_optab;
    7976         2268 :       op->mem_no_result = atomic_xor_optab;
    7977         2268 :       op->fetch_before = sync_old_xor_optab;
    7978         2268 :       op->fetch_after = sync_new_xor_optab;
    7979         2268 :       op->no_result = sync_xor_optab;
    7980         2268 :       op->reverse_code = XOR;
    7981         2268 :       break;
    7982         2146 :     case AND:
    7983         2146 :       op->mem_fetch_before = atomic_fetch_and_optab;
    7984         2146 :       op->mem_fetch_after = atomic_and_fetch_optab;
    7985         2146 :       op->mem_no_result = atomic_and_optab;
    7986         2146 :       op->fetch_before = sync_old_and_optab;
    7987         2146 :       op->fetch_after = sync_new_and_optab;
    7988         2146 :       op->no_result = sync_and_optab;
    7989         2146 :       op->reverse_code = UNKNOWN;
    7990         2146 :       break;
    7991         2617 :     case IOR:
    7992         2617 :       op->mem_fetch_before = atomic_fetch_or_optab;
    7993         2617 :       op->mem_fetch_after = atomic_or_fetch_optab;
    7994         2617 :       op->mem_no_result = atomic_or_optab;
    7995         2617 :       op->fetch_before = sync_old_ior_optab;
    7996         2617 :       op->fetch_after = sync_new_ior_optab;
    7997         2617 :       op->no_result = sync_ior_optab;
    7998         2617 :       op->reverse_code = UNKNOWN;
    7999         2617 :       break;
    8000          753 :     case NOT:
    8001          753 :       op->mem_fetch_before = atomic_fetch_nand_optab;
    8002          753 :       op->mem_fetch_after = atomic_nand_fetch_optab;
    8003          753 :       op->mem_no_result = atomic_nand_optab;
    8004          753 :       op->fetch_before = sync_old_nand_optab;
    8005          753 :       op->fetch_after = sync_new_nand_optab;
    8006          753 :       op->no_result = sync_nand_optab;
    8007          753 :       op->reverse_code = UNKNOWN;
    8008          753 :       break;
    8009            0 :     default:
    8010            0 :       gcc_unreachable ();
    8011              :     }
    8012        35481 : }
    8013              : 
    8014              : /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
    8015              :    using memory order MODEL.  If AFTER is true the operation needs to return
    8016              :    the value of *MEM after the operation, otherwise the previous value.
    8017              :    TARGET is an optional place to place the result.  The result is unused if
    8018              :    it is const0_rtx.
    8019              :    Return the result if there is a better sequence, otherwise NULL_RTX.  */
    8020              : 
    8021              : static rtx
    8022        35479 : maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
    8023              :                          enum memmodel model, bool after)
    8024              : {
    8025              :   /* If the value is prefetched, or not used, it may be possible to replace
    8026              :      the sequence with a native exchange operation.  */
    8027        35479 :   if (!after || target == const0_rtx)
    8028              :     {
    8029              :       /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m).  */
    8030        27323 :       if (code == AND && val == const0_rtx)
    8031              :         {
    8032          327 :           if (target == const0_rtx)
    8033          150 :             target = gen_reg_rtx (GET_MODE (mem));
    8034          327 :           return maybe_emit_atomic_exchange (target, mem, val, model);
    8035              :         }
    8036              : 
    8037              :       /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m).  */
    8038        26996 :       if (code == IOR && val == constm1_rtx)
    8039              :         {
    8040          165 :           if (target == const0_rtx)
    8041          164 :             target = gen_reg_rtx (GET_MODE (mem));
    8042          165 :           return maybe_emit_atomic_exchange (target, mem, val, model);
    8043              :         }
    8044              :     }
    8045              : 
    8046              :   return NULL_RTX;
    8047              : }
    8048              : 
    8049              : /* Try to emit an instruction for a specific operation variation.
    8050              :    OPTAB contains the OP functions.
    8051              :    TARGET is an optional place to return the result. const0_rtx means unused.
    8052              :    MEM is the memory location to operate on.
    8053              :    VAL is the value to use in the operation.
    8054              :    USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
    8055              :    MODEL is the memory model, if used.
    8056              :    AFTER is true if the returned result is the value after the operation.  */
    8057              : 
    8058              : static rtx
    8059        65420 : maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
    8060              :                rtx val, bool use_memmodel, enum memmodel model, bool after)
    8061              : {
    8062        65420 :   machine_mode mode = GET_MODE (mem);
    8063        65420 :   class expand_operand ops[4];
    8064        65420 :   enum insn_code icode;
    8065        65420 :   int op_counter = 0;
    8066        65420 :   int num_ops;
    8067              : 
    8068              :   /* Check to see if there is a result returned.  */
    8069        65420 :   if (target == const0_rtx)
    8070              :     {
    8071        14963 :       if (use_memmodel)
    8072              :         {
    8073        14604 :           icode = direct_optab_handler (optab->mem_no_result, mode);
    8074        14604 :           create_integer_operand (&ops[2], model);
    8075        14604 :           num_ops = 3;
    8076              :         }
    8077              :       else
    8078              :         {
    8079          359 :           icode = direct_optab_handler (optab->no_result, mode);
    8080          359 :           num_ops = 2;
    8081              :         }
    8082              :     }
    8083              :   /* Otherwise, we need to generate a result.  */
    8084              :   else
    8085              :     {
    8086        50457 :       if (use_memmodel)
    8087              :         {
    8088        31457 :           icode = direct_optab_handler (after ? optab->mem_fetch_after
    8089              :                                         : optab->mem_fetch_before, mode);
    8090        31457 :           create_integer_operand (&ops[3], model);
    8091        31457 :           num_ops = 4;
    8092              :         }
    8093              :       else
    8094              :         {
    8095        19000 :           icode = optab_handler (after ? optab->fetch_after
    8096              :                                  : optab->fetch_before, mode);
    8097        19000 :           num_ops = 3;
    8098              :         }
    8099        50457 :       create_output_operand (&ops[op_counter++], target, mode);
    8100              :     }
    8101        65420 :   if (icode == CODE_FOR_nothing)
    8102              :     return NULL_RTX;
    8103              : 
    8104        26702 :   create_fixed_operand (&ops[op_counter++], mem);
    8105              :   /* VAL may have been promoted to a wider mode.  Shrink it if so.  */
    8106        26702 :   create_convert_operand_to (&ops[op_counter++], val, mode, true);
    8107              : 
    8108        26702 :   if (maybe_expand_insn (icode, num_ops, ops))
    8109        26702 :     return (target == const0_rtx ? const0_rtx : ops[0].value);
    8110              : 
    8111              :   return NULL_RTX;
    8112              : }
    8113              : 
    8114              : 
    8115              : /* This function expands an atomic fetch_OP or OP_fetch operation:
    8116              :    TARGET is an option place to stick the return value.  const0_rtx indicates
    8117              :    the result is unused.
    8118              :    atomically fetch MEM, perform the operation with VAL and return it to MEM.
    8119              :    CODE is the operation being performed (OP)
    8120              :    MEMMODEL is the memory model variant to use.
    8121              :    AFTER is true to return the result of the operation (OP_fetch).
    8122              :    AFTER is false to return the value before the operation (fetch_OP).
    8123              : 
    8124              :    This function will *only* generate instructions if there is a direct
    8125              :    optab. No compare and swap loops or libcalls will be generated. */
    8126              : 
    8127              : static rtx
    8128        35479 : expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
    8129              :                                     enum rtx_code code, enum memmodel model,
    8130              :                                     bool after)
    8131              : {
    8132        35479 :   machine_mode mode = GET_MODE (mem);
    8133        35479 :   struct atomic_op_functions optab;
    8134        35479 :   rtx result;
    8135        35479 :   bool unused_result = (target == const0_rtx);
    8136              : 
    8137        35479 :   get_atomic_op_for_code (&optab, code);
    8138              : 
    8139              :   /* Check to see if there are any better instructions.  */
    8140        35479 :   result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
    8141        35479 :   if (result)
    8142              :     return result;
    8143              : 
    8144              :   /* Check for the case where the result isn't used and try those patterns.  */
    8145        34987 :   if (unused_result)
    8146              :     {
    8147              :       /* Try the memory model variant first.  */
    8148        14604 :       result = maybe_emit_op (&optab, target, mem, val, true, model, true);
    8149        14604 :       if (result)
    8150              :         return result;
    8151              : 
    8152              :       /* Next try the old style without a memory model.  */
    8153          359 :       result = maybe_emit_op (&optab, target, mem, val, false, model, true);
    8154          359 :       if (result)
    8155              :         return result;
    8156              : 
    8157              :       /* There is no no-result pattern, so try patterns with a result.  */
    8158              :       target = NULL_RTX;
    8159              :     }
    8160              : 
    8161              :   /* Try the __atomic version.  */
    8162        20742 :   result = maybe_emit_op (&optab, target, mem, val, true, model, after);
    8163        20742 :   if (result)
    8164              :     return result;
    8165              : 
    8166              :   /* Try the older __sync version.  */
    8167        12129 :   result = maybe_emit_op (&optab, target, mem, val, false, model, after);
    8168        12129 :   if (result)
    8169              :     return result;
    8170              : 
    8171              :   /* If the fetch value can be calculated from the other variation of fetch,
    8172              :      try that operation.  */
    8173        12129 :   if (after || unused_result || optab.reverse_code != UNKNOWN)
    8174              :     {
    8175        10715 :       rtx_insn *start = get_last_insn ();
    8176              :       /* Force val into a register if it could change value when the
    8177              :          atomic insn updates mem.  */
    8178        10715 :       if (!unused_result && reg_overlap_mentioned_p (mem, val))
    8179          114 :         val = force_reg (mode, val);
    8180              : 
    8181              :       /* Try the __atomic version, then the older __sync version.  */
    8182        10715 :       result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
    8183        10715 :       if (!result)
    8184         6871 :         result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
    8185              : 
    8186         6871 :       if (result)
    8187              :         {
    8188              :           /* If the result isn't used, no need to do compensation code.  */
    8189         3844 :           if (unused_result)
    8190              :             return result;
    8191              : 
    8192              :           /* Issue compensation code.  Fetch_after  == fetch_before OP val.
    8193              :              Fetch_before == after REVERSE_OP val.  */
    8194         3844 :           if (!after)
    8195            0 :             code = optab.reverse_code;
    8196         3844 :           if (code == NOT)
    8197              :             {
    8198            0 :               result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
    8199              :                                             true, OPTAB_LIB_WIDEN);
    8200            0 :               result = expand_simple_unop (mode, NOT, result, target, true);
    8201              :             }
    8202              :           else
    8203         3844 :             result = expand_simple_binop (mode, code, result, val, target,
    8204              :                                           true, OPTAB_LIB_WIDEN);
    8205              :           return result;
    8206              :         }
    8207              :       else
    8208         6871 :         delete_insns_since (start);
    8209              :     }
    8210              : 
    8211              :   /* No direct opcode can be generated.  */
    8212              :   return NULL_RTX;
    8213              : }
    8214              : 
    8215              : 
    8216              : 
    8217              : /* This function expands an atomic fetch_OP or OP_fetch operation:
    8218              :    TARGET is an option place to stick the return value.  const0_rtx indicates
    8219              :    the result is unused.
    8220              :    atomically fetch MEM, perform the operation with VAL and return it to MEM.
    8221              :    CODE is the operation being performed (OP)
    8222              :    MEMMODEL is the memory model variant to use.
    8223              :    AFTER is true to return the result of the operation (OP_fetch).
    8224              :    AFTER is false to return the value before the operation (fetch_OP).  */
    8225              : rtx
    8226        33055 : expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
    8227              :                         enum memmodel model, bool after)
    8228              : {
    8229        33055 :   machine_mode mode = GET_MODE (mem);
    8230        33055 :   rtx result;
    8231        33055 :   bool unused_result = (target == const0_rtx);
    8232              : 
    8233              :   /* If loads are not atomic for the required size and we are not called to
    8234              :      provide a __sync builtin, do not do anything so that we stay consistent
    8235              :      with atomic loads of the same size.  */
    8236        33055 :   if (!can_atomic_load_p (mode) && !is_mm_sync (model))
    8237              :     return NULL_RTX;
    8238              : 
    8239        32439 :   result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
    8240              :                                                after);
    8241              : 
    8242        32439 :   if (result)
    8243              :     return result;
    8244              : 
    8245              :   /* Add/sub can be implemented by doing the reverse operation with -(val).  */
    8246         8246 :   if (code == PLUS || code == MINUS)
    8247              :     {
    8248         3040 :       rtx tmp;
    8249         3040 :       enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
    8250              : 
    8251         3040 :       start_sequence ();
    8252         3040 :       tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
    8253         3040 :       result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
    8254              :                                                    model, after);
    8255         3040 :       if (result)
    8256              :         {
    8257              :           /* PLUS worked so emit the insns and return.  */
    8258         3001 :           tmp = end_sequence ();
    8259         3001 :           emit_insn (tmp);
    8260         3001 :           return result;
    8261              :         }
    8262              : 
    8263              :       /* PLUS did not work, so throw away the negation code and continue.  */
    8264           39 :       end_sequence ();
    8265              :     }
    8266              : 
    8267              :   /* Try the __sync libcalls only if we can't do compare-and-swap inline.  */
    8268         5245 :   if (!can_compare_and_swap_p (mode, false))
    8269              :     {
    8270            2 :       rtx libfunc;
    8271            2 :       bool fixup = false;
    8272            2 :       enum rtx_code orig_code = code;
    8273            2 :       struct atomic_op_functions optab;
    8274              : 
    8275            2 :       get_atomic_op_for_code (&optab, code);
    8276            2 :       libfunc = optab_libfunc (after ? optab.fetch_after
    8277              :                                : optab.fetch_before, mode);
    8278            2 :       if (libfunc == NULL
    8279            2 :           && (after || unused_result || optab.reverse_code != UNKNOWN))
    8280              :         {
    8281            2 :           fixup = true;
    8282            2 :           if (!after)
    8283            1 :             code = optab.reverse_code;
    8284            2 :           libfunc = optab_libfunc (after ? optab.fetch_before
    8285              :                                    : optab.fetch_after, mode);
    8286              :         }
    8287            2 :       if (libfunc != NULL)
    8288              :         {
    8289            0 :           rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
    8290            0 :           result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
    8291              :                                             addr, ptr_mode, val, mode);
    8292              : 
    8293            0 :           if (!unused_result && fixup)
    8294            0 :             result = expand_simple_binop (mode, code, result, val, target,
    8295              :                                           true, OPTAB_LIB_WIDEN);
    8296            0 :           return result;
    8297              :         }
    8298              : 
    8299              :       /* We need the original code for any further attempts.  */
    8300            2 :       code = orig_code;
    8301              :     }
    8302              : 
    8303              :   /* If nothing else has succeeded, default to a compare and swap loop.  */
    8304         5245 :   if (can_compare_and_swap_p (mode, true))
    8305              :     {
    8306         5243 :       rtx_insn *insn;
    8307         5243 :       rtx t0 = gen_reg_rtx (mode), t1;
    8308         5243 :       rtx_insn *start = get_last_insn ();
    8309              :       /* Force val into a register if it could change value when the
    8310              :          atomic insn updates mem.  */
    8311         5243 :       if (reg_overlap_mentioned_p (mem, val))
    8312          128 :         val = force_reg (mode, val);
    8313              : 
    8314         5243 :       start_sequence ();
    8315              : 
    8316              :       /* If the result is used, get a register for it.  */
    8317         5243 :       if (!unused_result)
    8318              :         {
    8319         4907 :           if (!target || !register_operand (target, mode))
    8320           12 :             target = gen_reg_rtx (mode);
    8321              :           /* If fetch_before, copy the value now.  */
    8322         4907 :           if (!after)
    8323         2235 :             emit_move_insn (target, t0);
    8324              :         }
    8325              :       else
    8326          336 :         target = const0_rtx;
    8327              : 
    8328         5243 :       t1 = t0;
    8329         5243 :       if (code == NOT)
    8330              :         {
    8331          733 :           t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
    8332              :                                     true, OPTAB_LIB_WIDEN);
    8333          733 :           t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
    8334              :         }
    8335              :       else
    8336         4510 :         t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
    8337              :                                   OPTAB_LIB_WIDEN);
    8338              : 
    8339              :       /* For after, copy the value now.  */
    8340         5243 :       if (!unused_result && after)
    8341         2672 :         emit_move_insn (target, t1);
    8342         5243 :       insn = end_sequence ();
    8343              : 
    8344         5243 :       if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
    8345              :         return target;
    8346              :       else
    8347            0 :         delete_insns_since (start);
    8348              :     }
    8349              : 
    8350              :   return NULL_RTX;
    8351              : }
    8352              : 
    8353              : /* Return true if OPERAND is suitable for operand number OPNO of
    8354              :    instruction ICODE.  */
    8355              : 
    8356              : bool
    8357    158322651 : insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
    8358              : {
    8359    158322651 :   return (!insn_data[(int) icode].operand[opno].predicate
    8360    315087242 :           || (insn_data[(int) icode].operand[opno].predicate
    8361    156764591 :               (operand, insn_data[(int) icode].operand[opno].mode)));
    8362              : }
    8363              : 
    8364              : /* TARGET is a target of a multiword operation that we are going to
    8365              :    implement as a series of word-mode operations.  Return true if
    8366              :    TARGET is suitable for this purpose.  */
    8367              : 
    8368              : bool
    8369          203 : valid_multiword_target_p (rtx target)
    8370              : {
    8371          203 :   machine_mode mode;
    8372          203 :   int i, size;
    8373              : 
    8374          203 :   mode = GET_MODE (target);
    8375          406 :   if (!GET_MODE_SIZE (mode).is_constant (&size))
    8376              :     return false;
    8377          951 :   for (i = 0; i < size; i += UNITS_PER_WORD)
    8378          422 :     if (!validate_subreg (word_mode, mode, target, i))
    8379              :       return false;
    8380              :   return true;
    8381              : }
    8382              : 
    8383              : /* Make OP describe an input operand that has value INTVAL and that has
    8384              :    no inherent mode.  This function should only be used for operands that
    8385              :    are always expand-time constants.  The backend may request that INTVAL
    8386              :    be copied into a different kind of rtx, but it must specify the mode
    8387              :    of that rtx if so.  */
    8388              : 
    8389              : void
    8390      2066959 : create_integer_operand (class expand_operand *op, poly_int64 intval)
    8391              : {
    8392      2066959 :   create_expand_operand (op, EXPAND_INTEGER,
    8393              :                          gen_int_mode (intval, MAX_MODE_INT),
    8394              :                          VOIDmode, false, intval);
    8395      2066959 : }
    8396              : 
    8397              : /* Like maybe_legitimize_operand, but do not change the code of the
    8398              :    current rtx value.  */
    8399              : 
    8400              : static bool
    8401     53210497 : maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
    8402              :                                     class expand_operand *op)
    8403              : {
    8404              :   /* See if the operand matches in its current form.  */
    8405     53210497 :   if (insn_operand_matches (icode, opno, op->value))
    8406              :     return true;
    8407              : 
    8408              :   /* If the operand is a memory whose address has no side effects,
    8409              :      try forcing the address into a non-virtual pseudo register.
    8410              :      The check for side effects is important because copy_to_mode_reg
    8411              :      cannot handle things like auto-modified addresses.  */
    8412      1640763 :   if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
    8413              :     {
    8414        13174 :       rtx addr, mem;
    8415              : 
    8416        13174 :       mem = op->value;
    8417        13174 :       addr = XEXP (mem, 0);
    8418         1006 :       if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
    8419        13980 :           && !side_effects_p (addr))
    8420              :         {
    8421         8441 :           rtx_insn *last;
    8422         8441 :           machine_mode mode;
    8423              : 
    8424         8441 :           last = get_last_insn ();
    8425         8441 :           mode = get_address_mode (mem);
    8426         8441 :           mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
    8427         8441 :           if (insn_operand_matches (icode, opno, mem))
    8428              :             {
    8429            0 :               op->value = mem;
    8430            0 :               return true;
    8431              :             }
    8432         8441 :           delete_insns_since (last);
    8433              :         }
    8434              :     }
    8435              : 
    8436              :   return false;
    8437              : }
    8438              : 
    8439              : /* Try to make OP match operand OPNO of instruction ICODE.  Return true
    8440              :    on success, storing the new operand value back in OP.  */
    8441              : 
    8442              : static bool
    8443     61026496 : maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
    8444              :                           class expand_operand *op)
    8445              : {
    8446     61026496 :   machine_mode mode, imode, tmode;
    8447              : 
    8448     61026496 :   mode = op->mode;
    8449     61026496 :   switch (op->type)
    8450              :     {
    8451      4583330 :     case EXPAND_FIXED:
    8452      4583330 :       {
    8453      4583330 :         temporary_volatile_ok v (true);
    8454      4583330 :         return maybe_legitimize_operand_same_code (icode, opno, op);
    8455      4583330 :       }
    8456              : 
    8457     19621595 :     case EXPAND_OUTPUT:
    8458     19621595 :       gcc_assert (mode != VOIDmode);
    8459     19621595 :       if (op->value
    8460     12200608 :           && op->value != const0_rtx
    8461     12200179 :           && GET_MODE (op->value) == mode
    8462     31785411 :           && maybe_legitimize_operand_same_code (icode, opno, op))
    8463              :         return true;
    8464              : 
    8465      7583832 :       op->value = gen_reg_rtx (mode);
    8466      7583832 :       op->target = 0;
    8467      7583832 :       break;
    8468              : 
    8469     36463351 :     case EXPAND_INPUT:
    8470     36463351 :     input:
    8471     36463351 :       gcc_assert (mode != VOIDmode);
    8472     36463351 :       gcc_assert (GET_MODE (op->value) == VOIDmode
    8473              :                   || GET_MODE (op->value) == mode);
    8474     36463351 :       if (maybe_legitimize_operand_same_code (icode, opno, op))
    8475              :         return true;
    8476              : 
    8477      1368231 :       op->value = copy_to_mode_reg (mode, op->value);
    8478      1368231 :       break;
    8479              : 
    8480       208849 :     case EXPAND_CONVERT_TO:
    8481       208849 :       gcc_assert (mode != VOIDmode);
    8482       208849 :       op->value = convert_to_mode (mode, op->value, op->unsigned_p);
    8483       208849 :       goto input;
    8484              : 
    8485       689348 :     case EXPAND_CONVERT_FROM:
    8486       689348 :       if (GET_MODE (op->value) != VOIDmode)
    8487       474722 :         mode = GET_MODE (op->value);
    8488              :       else
    8489              :         /* The caller must tell us what mode this value has.  */
    8490       214626 :         gcc_assert (mode != VOIDmode);
    8491              : 
    8492       689348 :       imode = insn_data[(int) icode].operand[opno].mode;
    8493       689348 :       tmode = (VECTOR_MODE_P (imode) && !VECTOR_MODE_P (mode)
    8494       689348 :                ? GET_MODE_INNER (imode) : imode);
    8495       689348 :       if (tmode != VOIDmode && tmode != mode)
    8496              :         {
    8497         1772 :           op->value = convert_modes (tmode, mode, op->value, op->unsigned_p);
    8498         1772 :           mode = tmode;
    8499              :         }
    8500       689348 :       if (imode != VOIDmode && imode != mode)
    8501              :         {
    8502           46 :           gcc_assert (VECTOR_MODE_P (imode) && !VECTOR_MODE_P (mode));
    8503           46 :           op->value = expand_vector_broadcast (imode, op->value);
    8504           46 :           mode = imode;
    8505              :         }
    8506       689348 :       goto input;
    8507              : 
    8508         3422 :     case EXPAND_ADDRESS:
    8509         3422 :       op->value = convert_memory_address (as_a <scalar_int_mode> (mode),
    8510              :                                           op->value);
    8511         3422 :       goto input;
    8512              : 
    8513      1682596 :     case EXPAND_INTEGER:
    8514      1682596 :       mode = insn_data[(int) icode].operand[opno].mode;
    8515      1682596 :       if (mode != VOIDmode
    8516      1682596 :           && known_eq (trunc_int_for_mode (op->int_value, mode),
    8517              :                        op->int_value))
    8518              :         {
    8519      1324376 :           op->value = gen_int_mode (op->int_value, mode);
    8520      1324376 :           goto input;
    8521              :         }
    8522              :       break;
    8523              : 
    8524          249 :     case EXPAND_UNDEFINED_INPUT:
    8525              :       /* See if the predicate accepts a SCRATCH rtx, which in this context
    8526              :          indicates an undefined value.  Use an uninitialized register if not. */
    8527          249 :       if (!insn_operand_matches (icode, opno, op->value))
    8528              :         {
    8529          249 :           op->value = gen_reg_rtx (op->mode);
    8530          249 :           goto input;
    8531              :         }
    8532              :       return true;
    8533              :     }
    8534      9310283 :   return insn_operand_matches (icode, opno, op->value);
    8535              : }
    8536              : 
    8537              : /* Make OP describe an input operand that should have the same value
    8538              :    as VALUE, after any mode conversion that the target might request.
    8539              :    TYPE is the type of VALUE.  */
    8540              : 
    8541              : void
    8542         2722 : create_convert_operand_from_type (class expand_operand *op,
    8543              :                                   rtx value, tree type)
    8544              : {
    8545         2722 :   create_convert_operand_from (op, value, TYPE_MODE (type),
    8546         2722 :                                TYPE_UNSIGNED (type));
    8547         2722 : }
    8548              : 
    8549              : /* Return true if the requirements on operands OP1 and OP2 of instruction
    8550              :    ICODE are similar enough for the result of legitimizing OP1 to be
    8551              :    reusable for OP2.  OPNO1 and OPNO2 are the operand numbers associated
    8552              :    with OP1 and OP2 respectively.  */
    8553              : 
    8554              : static inline bool
    8555     68010545 : can_reuse_operands_p (enum insn_code icode,
    8556              :                       unsigned int opno1, unsigned int opno2,
    8557              :                       const class expand_operand *op1,
    8558              :                       const class expand_operand *op2)
    8559              : {
    8560              :   /* Check requirements that are common to all types.  */
    8561     68010545 :   if (op1->type != op2->type
    8562     21131349 :       || op1->mode != op2->mode
    8563     19549088 :       || (insn_data[(int) icode].operand[opno1].mode
    8564     19549088 :           != insn_data[(int) icode].operand[opno2].mode))
    8565              :     return false;
    8566              : 
    8567              :   /* Check the requirements for specific types.  */
    8568     17586651 :   switch (op1->type)
    8569              :     {
    8570              :     case EXPAND_OUTPUT:
    8571              :     case EXPAND_UNDEFINED_INPUT:
    8572              :       /* Outputs and undefined inputs must remain distinct.  */
    8573              :       return false;
    8574              : 
    8575              :     case EXPAND_FIXED:
    8576              :     case EXPAND_INPUT:
    8577              :     case EXPAND_ADDRESS:
    8578              :     case EXPAND_INTEGER:
    8579              :       return true;
    8580              : 
    8581       125754 :     case EXPAND_CONVERT_TO:
    8582       125754 :     case EXPAND_CONVERT_FROM:
    8583       125754 :       return op1->unsigned_p == op2->unsigned_p;
    8584              :     }
    8585            0 :   gcc_unreachable ();
    8586              : }
    8587              : 
    8588              : /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
    8589              :    of instruction ICODE.  Return true on success, leaving the new operand
    8590              :    values in the OPS themselves.  Emit no code on failure.  */
    8591              : 
    8592              : bool
    8593     20390690 : maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
    8594              :                            unsigned int nops, class expand_operand *ops)
    8595              : {
    8596     20390690 :   rtx_insn *last = get_last_insn ();
    8597     20390690 :   rtx *orig_values = XALLOCAVEC (rtx, nops);
    8598     81667151 :   for (unsigned int i = 0; i < nops; i++)
    8599              :     {
    8600     61430388 :       orig_values[i] = ops[i].value;
    8601              : 
    8602              :       /* First try reusing the result of an earlier legitimization.
    8603              :          This avoids duplicate rtl and ensures that tied operands
    8604              :          remain tied.
    8605              : 
    8606              :          This search is linear, but NOPS is bounded at compile time
    8607              :          to a small number (current a single digit).  */
    8608     61430388 :       unsigned int j = 0;
    8609    129037041 :       for (; j < i; ++j)
    8610     68010545 :         if (can_reuse_operands_p (icode, opno + j, opno + i, &ops[j], &ops[i])
    8611     17564287 :             && rtx_equal_p (orig_values[j], orig_values[i])
    8612       422575 :             && ops[j].value
    8613     68414437 :             && insn_operand_matches (icode, opno + i, ops[j].value))
    8614              :           {
    8615       403892 :             ops[i].value = copy_rtx (ops[j].value);
    8616       403892 :             break;
    8617              :           }
    8618              : 
    8619              :       /* Otherwise try legitimizing the operand on its own.  */
    8620     61430388 :       if (j == i && !maybe_legitimize_operand (icode, opno + i, &ops[i]))
    8621              :         {
    8622       153927 :           delete_insns_since (last);
    8623       153927 :           return false;
    8624              :         }
    8625              :     }
    8626              :   return true;
    8627              : }
    8628              : 
    8629              : /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
    8630              :    as its operands.  Return the instruction pattern on success,
    8631              :    and emit any necessary set-up code.  Return null and emit no
    8632              :    code on failure.  */
    8633              : 
    8634              : rtx_insn *
    8635     20363625 : maybe_gen_insn (enum insn_code icode, unsigned int nops,
    8636              :                 class expand_operand *ops)
    8637              : {
    8638     20363625 :   gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
    8639     20363625 :   if (!maybe_legitimize_operands (icode, 0, nops, ops))
    8640              :     return NULL;
    8641              : 
    8642     20209698 :   switch (nops)
    8643              :     {
    8644            0 :     case 0:
    8645            0 :       return GEN_FCN (icode) ();
    8646         1364 :     case 1:
    8647         1364 :       return GEN_FCN (icode) (ops[0].value);
    8648      2916387 :     case 2:
    8649      2916387 :       return GEN_FCN (icode) (ops[0].value, ops[1].value);
    8650     15128374 :     case 3:
    8651     15128374 :       return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
    8652      1767410 :     case 4:
    8653      1767410 :       return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
    8654      1767410 :                               ops[3].value);
    8655       193035 :     case 5:
    8656       193035 :       return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
    8657       193035 :                               ops[3].value, ops[4].value);
    8658          376 :     case 6:
    8659          376 :       return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
    8660          376 :                               ops[3].value, ops[4].value, ops[5].value);
    8661            0 :     case 7:
    8662            0 :       return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
    8663            0 :                               ops[3].value, ops[4].value, ops[5].value,
    8664            0 :                               ops[6].value);
    8665        27607 :     case 8:
    8666        27607 :       return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
    8667        27607 :                               ops[3].value, ops[4].value, ops[5].value,
    8668        27607 :                               ops[6].value, ops[7].value);
    8669       175145 :     case 9:
    8670       175145 :       return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
    8671       175145 :                               ops[3].value, ops[4].value, ops[5].value,
    8672       175145 :                               ops[6].value, ops[7].value, ops[8].value);
    8673            0 :     case 10:
    8674            0 :       return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
    8675            0 :                               ops[3].value, ops[4].value, ops[5].value,
    8676            0 :                               ops[6].value, ops[7].value, ops[8].value,
    8677            0 :                               ops[9].value);
    8678            0 :     case 11:
    8679            0 :       return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
    8680            0 :                               ops[3].value, ops[4].value, ops[5].value,
    8681            0 :                               ops[6].value, ops[7].value, ops[8].value,
    8682            0 :                               ops[9].value, ops[10].value);
    8683              :     }
    8684            0 :   gcc_unreachable ();
    8685              : }
    8686              : 
    8687              : /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
    8688              :    as its operands.  Return true on success and emit no code on failure.  */
    8689              : 
    8690              : bool
    8691      3068989 : maybe_expand_insn (enum insn_code icode, unsigned int nops,
    8692              :                    class expand_operand *ops)
    8693              : {
    8694      3068989 :   rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
    8695      3068989 :   if (pat)
    8696              :     {
    8697      2238251 :       emit_insn (pat);
    8698      2238251 :       return true;
    8699              :     }
    8700              :   return false;
    8701              : }
    8702              : 
    8703              : /* Like maybe_expand_insn, but for jumps.  */
    8704              : 
    8705              : bool
    8706         1361 : maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
    8707              :                         class expand_operand *ops)
    8708              : {
    8709         1361 :   rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
    8710         1361 :   if (pat)
    8711              :     {
    8712         1361 :       emit_jump_insn (pat);
    8713         1361 :       return true;
    8714              :     }
    8715              :   return false;
    8716              : }
    8717              : 
    8718              : /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
    8719              :    as its operands.  */
    8720              : 
    8721              : void
    8722       647561 : expand_insn (enum insn_code icode, unsigned int nops,
    8723              :              class expand_operand *ops)
    8724              : {
    8725       647561 :   if (!maybe_expand_insn (icode, nops, ops))
    8726            0 :     gcc_unreachable ();
    8727       647561 : }
    8728              : 
    8729              : /* Like expand_insn, but for jumps.  */
    8730              : 
    8731              : void
    8732         1361 : expand_jump_insn (enum insn_code icode, unsigned int nops,
    8733              :                   class expand_operand *ops)
    8734              : {
    8735         1361 :   if (!maybe_expand_jump_insn (icode, nops, ops))
    8736            0 :     gcc_unreachable ();
    8737         1361 : }
        

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.