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