OSDN Git Service

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