OSDN Git Service

* builtins.c (expand_builtin_copysign): New.
[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 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.  */
21
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "toplev.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 "real.h"
45 #include "basic-block.h"
46 #include "target.h"
47
48 /* Each optab contains info on how this target machine
49    can perform a particular operation
50    for all sizes and kinds of operands.
51
52    The operation to be performed is often specified
53    by passing one of these optabs as an argument.
54
55    See expr.h for documentation of these optabs.  */
56
57 optab optab_table[OTI_MAX];
58
59 rtx libfunc_table[LTI_MAX];
60
61 /* Tables of patterns for converting one mode to another.  */
62 convert_optab convert_optab_table[CTI_MAX];
63
64 /* Contains the optab used for each rtx code.  */
65 optab code_to_optab[NUM_RTX_CODE + 1];
66
67 /* Indexed by the rtx-code for a conditional (eg. EQ, LT,...)
68    gives the gen_function to make a branch to test that condition.  */
69
70 rtxfun bcc_gen_fctn[NUM_RTX_CODE];
71
72 /* Indexed by the rtx-code for a conditional (eg. EQ, LT,...)
73    gives the insn code to make a store-condition insn
74    to test that condition.  */
75
76 enum insn_code setcc_gen_code[NUM_RTX_CODE];
77
78 #ifdef HAVE_conditional_move
79 /* Indexed by the machine mode, gives the insn code to make a conditional
80    move insn.  This is not indexed by the rtx-code like bcc_gen_fctn and
81    setcc_gen_code to cut down on the number of named patterns.  Consider a day
82    when a lot more rtx codes are conditional (eg: for the ARM).  */
83
84 enum insn_code movcc_gen_code[NUM_MACHINE_MODES];
85 #endif
86
87 /* Indexed by the machine mode, gives the insn code for vector conditional
88    operation.  */
89
90 enum insn_code vcond_gen_code[NUM_MACHINE_MODES];
91 enum insn_code vcondu_gen_code[NUM_MACHINE_MODES];
92
93 /* The insn generating function can not take an rtx_code argument.
94    TRAP_RTX is used as an rtx argument.  Its code is replaced with
95    the code to be used in the trap insn and all other fields are ignored.  */
96 static GTY(()) rtx trap_rtx;
97
98 static int add_equal_note (rtx, rtx, enum rtx_code, rtx, rtx);
99 static rtx widen_operand (rtx, enum machine_mode, enum machine_mode, int,
100                           int);
101 static void prepare_cmp_insn (rtx *, rtx *, enum rtx_code *, rtx,
102                               enum machine_mode *, int *,
103                               enum can_compare_purpose);
104 static enum insn_code can_fix_p (enum machine_mode, enum machine_mode, int,
105                                  int *);
106 static enum insn_code can_float_p (enum machine_mode, enum machine_mode, int);
107 static optab new_optab (void);
108 static convert_optab new_convert_optab (void);
109 static inline optab init_optab (enum rtx_code);
110 static inline optab init_optabv (enum rtx_code);
111 static inline convert_optab init_convert_optab (enum rtx_code);
112 static void init_libfuncs (optab, int, int, const char *, int);
113 static void init_integral_libfuncs (optab, const char *, int);
114 static void init_floating_libfuncs (optab, const char *, int);
115 static void init_interclass_conv_libfuncs (convert_optab, const char *,
116                                            enum mode_class, enum mode_class);
117 static void init_intraclass_conv_libfuncs (convert_optab, const char *,
118                                            enum mode_class, bool);
119 static void emit_cmp_and_jump_insn_1 (rtx, rtx, enum machine_mode,
120                                       enum rtx_code, int, rtx);
121 static void prepare_float_lib_cmp (rtx *, rtx *, enum rtx_code *,
122                                    enum machine_mode *, int *);
123 static rtx widen_clz (enum machine_mode, rtx, rtx);
124 static rtx expand_parity (enum machine_mode, rtx, rtx);
125 static enum rtx_code get_rtx_code (enum tree_code, bool);
126 static rtx vector_compare_rtx (tree, bool, enum insn_code);
127
128 #ifndef HAVE_conditional_trap
129 #define HAVE_conditional_trap 0
130 #define gen_conditional_trap(a,b) (abort (), NULL_RTX)
131 #endif
132 \f
133 /* Add a REG_EQUAL note to the last insn in INSNS.  TARGET is being set to
134    the result of operation CODE applied to OP0 (and OP1 if it is a binary
135    operation).
136
137    If the last insn does not set TARGET, don't do anything, but return 1.
138
139    If a previous insn sets TARGET and TARGET is one of OP0 or OP1,
140    don't add the REG_EQUAL note but return 0.  Our caller can then try
141    again, ensuring that TARGET is not one of the operands.  */
142
143 static int
144 add_equal_note (rtx insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
145 {
146   rtx last_insn, insn, set;
147   rtx note;
148
149   if (! insns
150       || ! INSN_P (insns)
151       || NEXT_INSN (insns) == NULL_RTX)
152     abort ();
153
154   if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
155       && GET_RTX_CLASS (code) != RTX_BIN_ARITH
156       && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
157       && GET_RTX_CLASS (code) != RTX_COMPARE
158       && GET_RTX_CLASS (code) != RTX_UNARY)
159     return 1;
160
161   if (GET_CODE (target) == ZERO_EXTRACT)
162     return 1;
163
164   for (last_insn = insns;
165        NEXT_INSN (last_insn) != NULL_RTX;
166        last_insn = NEXT_INSN (last_insn))
167     ;
168
169   set = single_set (last_insn);
170   if (set == NULL_RTX)
171     return 1;
172
173   if (! rtx_equal_p (SET_DEST (set), target)
174       /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it.  */
175       && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
176           || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
177     return 1;
178
179   /* If TARGET is in OP0 or OP1, check if anything in SEQ sets TARGET
180      besides the last insn.  */
181   if (reg_overlap_mentioned_p (target, op0)
182       || (op1 && reg_overlap_mentioned_p (target, op1)))
183     {
184       insn = PREV_INSN (last_insn);
185       while (insn != NULL_RTX)
186         {
187           if (reg_set_p (target, insn))
188             return 0;
189
190           insn = PREV_INSN (insn);
191         }
192     }
193
194   if (GET_RTX_CLASS (code) == RTX_UNARY)
195     note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
196   else
197     note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
198
199   set_unique_reg_note (last_insn, REG_EQUAL, note);
200
201   return 1;
202 }
203 \f
204 /* Widen OP to MODE and return the rtx for the widened operand.  UNSIGNEDP
205    says whether OP is signed or unsigned.  NO_EXTEND is nonzero if we need
206    not actually do a sign-extend or zero-extend, but can leave the
207    higher-order bits of the result rtx undefined, for example, in the case
208    of logical operations, but not right shifts.  */
209
210 static rtx
211 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
212                int unsignedp, int no_extend)
213 {
214   rtx result;
215
216   /* If we don't have to extend and this is a constant, return it.  */
217   if (no_extend && GET_MODE (op) == VOIDmode)
218     return op;
219
220   /* If we must extend do so.  If OP is a SUBREG for a promoted object, also
221      extend since it will be more efficient to do so unless the signedness of
222      a promoted object differs from our extension.  */
223   if (! no_extend
224       || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
225           && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
226     return convert_modes (mode, oldmode, op, unsignedp);
227
228   /* If MODE is no wider than a single word, we return a paradoxical
229      SUBREG.  */
230   if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
231     return gen_rtx_SUBREG (mode, force_reg (GET_MODE (op), op), 0);
232
233   /* Otherwise, get an object of MODE, clobber it, and set the low-order
234      part to OP.  */
235
236   result = gen_reg_rtx (mode);
237   emit_insn (gen_rtx_CLOBBER (VOIDmode, result));
238   emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
239   return result;
240 }
241 \f
242 /* Return the optab used for computing the operation given by
243    the tree code, CODE.  This function is not always usable (for
244    example, it cannot give complete results for multiplication
245    or division) but probably ought to be relied on more widely
246    throughout the expander.  */
247 optab
248 optab_for_tree_code (enum tree_code code, tree type)
249 {
250   bool trapv;
251   switch (code)
252     {
253     case BIT_AND_EXPR:
254       return and_optab;
255
256     case BIT_IOR_EXPR:
257       return ior_optab;
258
259     case BIT_NOT_EXPR:
260       return one_cmpl_optab;
261
262     case BIT_XOR_EXPR:
263       return xor_optab;
264
265     case TRUNC_MOD_EXPR:
266     case CEIL_MOD_EXPR:
267     case FLOOR_MOD_EXPR:
268     case ROUND_MOD_EXPR:
269       return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
270
271     case RDIV_EXPR:
272     case TRUNC_DIV_EXPR:
273     case CEIL_DIV_EXPR:
274     case FLOOR_DIV_EXPR:
275     case ROUND_DIV_EXPR:
276     case EXACT_DIV_EXPR:
277       return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
278
279     case LSHIFT_EXPR:
280       return ashl_optab;
281
282     case RSHIFT_EXPR:
283       return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
284
285     case LROTATE_EXPR:
286       return rotl_optab;
287
288     case RROTATE_EXPR:
289       return rotr_optab;
290
291     case MAX_EXPR:
292       return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
293
294     case MIN_EXPR:
295       return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
296
297     case REALIGN_LOAD_EXPR:
298       return vec_realign_load_optab;
299
300     default:
301       break;
302     }
303
304   trapv = flag_trapv && INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type);
305   switch (code)
306     {
307     case PLUS_EXPR:
308       return trapv ? addv_optab : add_optab;
309
310     case MINUS_EXPR:
311       return trapv ? subv_optab : sub_optab;
312
313     case MULT_EXPR:
314       return trapv ? smulv_optab : smul_optab;
315
316     case NEGATE_EXPR:
317       return trapv ? negv_optab : neg_optab;
318
319     case ABS_EXPR:
320       return trapv ? absv_optab : abs_optab;
321
322     default:
323       return NULL;
324     }
325 }
326 \f
327
328 /* Generate code to perform an operation specified by TERNARY_OPTAB
329    on operands OP0, OP1 and OP2, with result having machine-mode MODE.
330
331    UNSIGNEDP is for the case where we have to widen the operands
332    to perform the operation.  It says to use zero-extension.
333
334    If TARGET is nonzero, the value
335    is generated there, if it is convenient to do so.
336    In all cases an rtx is returned for the locus of the value;
337    this may or may not be TARGET.  */
338
339 rtx
340 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0, 
341                    rtx op1, rtx op2, rtx target, int unsignedp) 
342 {
343   int icode = (int) ternary_optab->handlers[(int) mode].insn_code;
344   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
345   enum machine_mode mode1 = insn_data[icode].operand[2].mode;
346   enum machine_mode mode2 = insn_data[icode].operand[3].mode;
347   rtx temp;
348   rtx pat;
349   rtx xop0 = op0, xop1 = op1, xop2 = op2;
350
351   if (ternary_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
352     abort ();
353
354   if (!target
355       || ! (*insn_data[icode].operand[0].predicate) (target, mode))
356     temp = gen_reg_rtx (mode);
357   else
358     temp = target;
359
360   /* In case the insn wants input operands in modes different from
361      those of the actual operands, convert the operands.  It would
362      seem that we don't need to convert CONST_INTs, but we do, so
363      that they're properly zero-extended, sign-extended or truncated
364      for their mode.  */
365
366   if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
367     xop0 = convert_modes (mode0,
368                           GET_MODE (op0) != VOIDmode
369                           ? GET_MODE (op0) 
370                           : mode,
371                           xop0, unsignedp);
372
373   if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
374     xop1 = convert_modes (mode1,
375                           GET_MODE (op1) != VOIDmode
376                           ? GET_MODE (op1)
377                           : mode,
378                           xop1, unsignedp);
379
380   if (GET_MODE (op2) != mode2 && mode2 != VOIDmode)
381     xop2 = convert_modes (mode2,
382                           GET_MODE (op2) != VOIDmode
383                           ? GET_MODE (op2)
384                           : mode,
385                           xop2, unsignedp);
386
387   /* Now, if insn's predicates don't allow our operands, put them into
388      pseudo regs.  */
389   
390   if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0)
391       && mode0 != VOIDmode) 
392     xop0 = copy_to_mode_reg (mode0, xop0);
393   
394   if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1)
395       && mode1 != VOIDmode)
396     xop1 = copy_to_mode_reg (mode1, xop1);
397     
398   if (! (*insn_data[icode].operand[3].predicate) (xop2, mode2)
399       && mode2 != VOIDmode)
400     xop2 = copy_to_mode_reg (mode2, xop2);
401     
402   pat = GEN_FCN (icode) (temp, xop0, xop1, xop2);
403     
404   emit_insn (pat);
405   return temp; 
406 }
407
408
409 /* Like expand_binop, but return a constant rtx if the result can be
410    calculated at compile time.  The arguments and return value are
411    otherwise the same as for expand_binop.  */
412
413 static rtx
414 simplify_expand_binop (enum machine_mode mode, optab binoptab,
415                        rtx op0, rtx op1, rtx target, int unsignedp,
416                        enum optab_methods methods)
417 {
418   if (CONSTANT_P (op0) && CONSTANT_P (op1))
419     return simplify_gen_binary (binoptab->code, mode, op0, op1);
420   else
421     return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
422 }
423
424 /* Like simplify_expand_binop, but always put the result in TARGET.
425    Return true if the expansion succeeded.  */
426
427 bool
428 force_expand_binop (enum machine_mode mode, optab binoptab,
429                     rtx op0, rtx op1, rtx target, int unsignedp,
430                     enum optab_methods methods)
431 {
432   rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
433                                  target, unsignedp, methods);
434   if (x == 0)
435     return false;
436   if (x != target)
437     emit_move_insn (target, x);
438   return true;
439 }
440
441 /* This subroutine of expand_doubleword_shift handles the cases in which
442    the effective shift value is >= BITS_PER_WORD.  The arguments and return
443    value are the same as for the parent routine, except that SUPERWORD_OP1
444    is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
445    INTO_TARGET may be null if the caller has decided to calculate it.  */
446
447 static bool
448 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
449                         rtx outof_target, rtx into_target,
450                         int unsignedp, enum optab_methods methods)
451 {
452   if (into_target != 0)
453     if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
454                              into_target, unsignedp, methods))
455       return false;
456
457   if (outof_target != 0)
458     {
459       /* For a signed right shift, we must fill OUTOF_TARGET with copies
460          of the sign bit, otherwise we must fill it with zeros.  */
461       if (binoptab != ashr_optab)
462         emit_move_insn (outof_target, CONST0_RTX (word_mode));
463       else
464         if (!force_expand_binop (word_mode, binoptab,
465                                  outof_input, GEN_INT (BITS_PER_WORD - 1),
466                                  outof_target, unsignedp, methods))
467           return false;
468     }
469   return true;
470 }
471
472 /* This subroutine of expand_doubleword_shift handles the cases in which
473    the effective shift value is < BITS_PER_WORD.  The arguments and return
474    value are the same as for the parent routine.  */
475
476 static bool
477 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
478                       rtx outof_input, rtx into_input, rtx op1,
479                       rtx outof_target, rtx into_target,
480                       int unsignedp, enum optab_methods methods,
481                       unsigned HOST_WIDE_INT shift_mask)
482 {
483   optab reverse_unsigned_shift, unsigned_shift;
484   rtx tmp, carries;
485
486   reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
487   unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
488
489   /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
490      We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
491      the opposite direction to BINOPTAB.  */
492   if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
493     {
494       carries = outof_input;
495       tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
496       tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
497                                    0, true, methods);
498     }
499   else
500     {
501       /* We must avoid shifting by BITS_PER_WORD bits since that is either
502          the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
503          has unknown behavior.  Do a single shift first, then shift by the
504          remainder.  It's OK to use ~OP1 as the remainder if shift counts
505          are truncated to the mode size.  */
506       carries = expand_binop (word_mode, reverse_unsigned_shift,
507                               outof_input, const1_rtx, 0, unsignedp, methods);
508       if (shift_mask == BITS_PER_WORD - 1)
509         {
510           tmp = immed_double_const (-1, -1, op1_mode);
511           tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
512                                        0, true, methods);
513         }
514       else
515         {
516           tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
517           tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
518                                        0, true, methods);
519         }
520     }
521   if (tmp == 0 || carries == 0)
522     return false;
523   carries = expand_binop (word_mode, reverse_unsigned_shift,
524                           carries, tmp, 0, unsignedp, methods);
525   if (carries == 0)
526     return false;
527
528   /* Shift INTO_INPUT logically by OP1.  This is the last use of INTO_INPUT
529      so the result can go directly into INTO_TARGET if convenient.  */
530   tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
531                       into_target, unsignedp, methods);
532   if (tmp == 0)
533     return false;
534
535   /* Now OR in the bits carried over from OUTOF_INPUT.  */
536   if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
537                            into_target, unsignedp, methods))
538     return false;
539
540   /* Use a standard word_mode shift for the out-of half.  */
541   if (outof_target != 0)
542     if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
543                              outof_target, unsignedp, methods))
544       return false;
545
546   return true;
547 }
548
549
550 #ifdef HAVE_conditional_move
551 /* Try implementing expand_doubleword_shift using conditional moves.
552    The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
553    otherwise it is by >= BITS_PER_WORD.  SUBWORD_OP1 and SUPERWORD_OP1
554    are the shift counts to use in the former and latter case.  All other
555    arguments are the same as the parent routine.  */
556
557 static bool
558 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
559                                   enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
560                                   rtx outof_input, rtx into_input,
561                                   rtx subword_op1, rtx superword_op1,
562                                   rtx outof_target, rtx into_target,
563                                   int unsignedp, enum optab_methods methods,
564                                   unsigned HOST_WIDE_INT shift_mask)
565 {
566   rtx outof_superword, into_superword;
567
568   /* Put the superword version of the output into OUTOF_SUPERWORD and
569      INTO_SUPERWORD.  */
570   outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
571   if (outof_target != 0 && subword_op1 == superword_op1)
572     {
573       /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
574          OUTOF_TARGET, is the same as the value of INTO_SUPERWORD.  */
575       into_superword = outof_target;
576       if (!expand_superword_shift (binoptab, outof_input, superword_op1,
577                                    outof_superword, 0, unsignedp, methods))
578         return false;
579     }
580   else
581     {
582       into_superword = gen_reg_rtx (word_mode);
583       if (!expand_superword_shift (binoptab, outof_input, superword_op1,
584                                    outof_superword, into_superword,
585                                    unsignedp, methods))
586         return false;
587     }
588
589   /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET.  */
590   if (!expand_subword_shift (op1_mode, binoptab,
591                              outof_input, into_input, subword_op1,
592                              outof_target, into_target,
593                              unsignedp, methods, shift_mask))
594     return false;
595
596   /* Select between them.  Do the INTO half first because INTO_SUPERWORD
597      might be the current value of OUTOF_TARGET.  */
598   if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
599                               into_target, into_superword, word_mode, false))
600     return false;
601
602   if (outof_target != 0)
603     if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
604                                 outof_target, outof_superword,
605                                 word_mode, false))
606       return false;
607
608   return true;
609 }
610 #endif
611
612 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
613    OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
614    input operand; the shift moves bits in the direction OUTOF_INPUT->
615    INTO_TARGET.  OUTOF_TARGET and INTO_TARGET are the equivalent words
616    of the target.  OP1 is the shift count and OP1_MODE is its mode.
617    If OP1 is constant, it will have been truncated as appropriate
618    and is known to be nonzero.
619
620    If SHIFT_MASK is zero, the result of word shifts is undefined when the
621    shift count is outside the range [0, BITS_PER_WORD).  This routine must
622    avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
623
624    If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
625    masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
626    fill with zeros or sign bits as appropriate.
627
628    If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
629    a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
630    Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
631    In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
632    are undefined.
633
634    BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop.  This function
635    may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
636    OUTOF_INPUT and OUTOF_TARGET.  OUTOF_TARGET can be null if the parent
637    function wants to calculate it itself.
638
639    Return true if the shift could be successfully synthesized.  */
640
641 static bool
642 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
643                          rtx outof_input, rtx into_input, rtx op1,
644                          rtx outof_target, rtx into_target,
645                          int unsignedp, enum optab_methods methods,
646                          unsigned HOST_WIDE_INT shift_mask)
647 {
648   rtx superword_op1, tmp, cmp1, cmp2;
649   rtx subword_label, done_label;
650   enum rtx_code cmp_code;
651
652   /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
653      fill the result with sign or zero bits as appropriate.  If so, the value
654      of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1).   Recursively call
655      this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
656      and INTO_INPUT), then emit code to set up OUTOF_TARGET.
657
658      This isn't worthwhile for constant shifts since the optimizers will
659      cope better with in-range shift counts.  */
660   if (shift_mask >= BITS_PER_WORD
661       && outof_target != 0
662       && !CONSTANT_P (op1))
663     {
664       if (!expand_doubleword_shift (op1_mode, binoptab,
665                                     outof_input, into_input, op1,
666                                     0, into_target,
667                                     unsignedp, methods, shift_mask))
668         return false;
669       if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
670                                outof_target, unsignedp, methods))
671         return false;
672       return true;
673     }
674
675   /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
676      is true when the effective shift value is less than BITS_PER_WORD.
677      Set SUPERWORD_OP1 to the shift count that should be used to shift
678      OUTOF_INPUT into INTO_TARGET when the condition is false.  */
679   tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
680   if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
681     {
682       /* Set CMP1 to OP1 & BITS_PER_WORD.  The result is zero iff OP1
683          is a subword shift count.  */
684       cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
685                                     0, true, methods);
686       cmp2 = CONST0_RTX (op1_mode);
687       cmp_code = EQ;
688       superword_op1 = op1;
689     }
690   else
691     {
692       /* Set CMP1 to OP1 - BITS_PER_WORD.  */
693       cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
694                                     0, true, methods);
695       cmp2 = CONST0_RTX (op1_mode);
696       cmp_code = LT;
697       superword_op1 = cmp1;
698     }
699   if (cmp1 == 0)
700     return false;
701
702   /* If we can compute the condition at compile time, pick the
703      appropriate subroutine.  */
704   tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
705   if (tmp != 0 && GET_CODE (tmp) == CONST_INT)
706     {
707       if (tmp == const0_rtx)
708         return expand_superword_shift (binoptab, outof_input, superword_op1,
709                                        outof_target, into_target,
710                                        unsignedp, methods);
711       else
712         return expand_subword_shift (op1_mode, binoptab,
713                                      outof_input, into_input, op1,
714                                      outof_target, into_target,
715                                      unsignedp, methods, shift_mask);
716     }
717
718 #ifdef HAVE_conditional_move
719   /* Try using conditional moves to generate straight-line code.  */
720   {
721     rtx start = get_last_insn ();
722     if (expand_doubleword_shift_condmove (op1_mode, binoptab,
723                                           cmp_code, cmp1, cmp2,
724                                           outof_input, into_input,
725                                           op1, superword_op1,
726                                           outof_target, into_target,
727                                           unsignedp, methods, shift_mask))
728       return true;
729     delete_insns_since (start);
730   }
731 #endif
732
733   /* As a last resort, use branches to select the correct alternative.  */
734   subword_label = gen_label_rtx ();
735   done_label = gen_label_rtx ();
736
737   do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
738                            0, 0, subword_label);
739
740   if (!expand_superword_shift (binoptab, outof_input, superword_op1,
741                                outof_target, into_target,
742                                unsignedp, methods))
743     return false;
744
745   emit_jump_insn (gen_jump (done_label));
746   emit_barrier ();
747   emit_label (subword_label);
748
749   if (!expand_subword_shift (op1_mode, binoptab,
750                              outof_input, into_input, op1,
751                              outof_target, into_target,
752                              unsignedp, methods, shift_mask))
753     return false;
754
755   emit_label (done_label);
756   return true;
757 }
758 \f
759 /* Wrapper around expand_binop which takes an rtx code to specify
760    the operation to perform, not an optab pointer.  All other
761    arguments are the same.  */
762 rtx
763 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
764                      rtx op1, rtx target, int unsignedp,
765                      enum optab_methods methods)
766 {
767   optab binop = code_to_optab[(int) code];
768   if (binop == 0)
769     abort ();
770
771   return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
772 }
773
774 /* Generate code to perform an operation specified by BINOPTAB
775    on operands OP0 and OP1, with result having machine-mode MODE.
776
777    UNSIGNEDP is for the case where we have to widen the operands
778    to perform the operation.  It says to use zero-extension.
779
780    If TARGET is nonzero, the value
781    is generated there, if it is convenient to do so.
782    In all cases an rtx is returned for the locus of the value;
783    this may or may not be TARGET.  */
784
785 rtx
786 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
787               rtx target, int unsignedp, enum optab_methods methods)
788 {
789   enum optab_methods next_methods
790     = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
791        ? OPTAB_WIDEN : methods);
792   enum mode_class class;
793   enum machine_mode wider_mode;
794   rtx temp;
795   int commutative_op = 0;
796   int shift_op = (binoptab->code == ASHIFT
797                   || binoptab->code == ASHIFTRT
798                   || binoptab->code == LSHIFTRT
799                   || binoptab->code == ROTATE
800                   || binoptab->code == ROTATERT);
801   rtx entry_last = get_last_insn ();
802   rtx last;
803
804   class = GET_MODE_CLASS (mode);
805
806   if (flag_force_mem)
807     {
808       /* Load duplicate non-volatile operands once.  */
809       if (rtx_equal_p (op0, op1) && ! volatile_refs_p (op0))
810         {
811           op0 = force_not_mem (op0);
812           op1 = op0;
813         }
814       else
815         {
816           op0 = force_not_mem (op0);
817           op1 = force_not_mem (op1);
818         }
819     }
820
821   /* If subtracting an integer constant, convert this into an addition of
822      the negated constant.  */
823
824   if (binoptab == sub_optab && GET_CODE (op1) == CONST_INT)
825     {
826       op1 = negate_rtx (mode, op1);
827       binoptab = add_optab;
828     }
829
830   /* If we are inside an appropriately-short loop and we are optimizing,
831      force expensive constants into a register.  */
832   if (CONSTANT_P (op0) && optimize
833       && rtx_cost (op0, binoptab->code) > COSTS_N_INSNS (1))
834     {
835       if (GET_MODE (op0) != VOIDmode)
836         op0 = convert_modes (mode, VOIDmode, op0, unsignedp);
837       op0 = force_reg (mode, op0);
838     }
839
840   if (CONSTANT_P (op1) && optimize
841       && ! shift_op && rtx_cost (op1, binoptab->code) > COSTS_N_INSNS (1))
842     {
843       if (GET_MODE (op1) != VOIDmode)
844         op1 = convert_modes (mode, VOIDmode, op1, unsignedp);
845       op1 = force_reg (mode, op1);
846     }
847
848   /* Record where to delete back to if we backtrack.  */
849   last = get_last_insn ();
850
851   /* If operation is commutative,
852      try to make the first operand a register.
853      Even better, try to make it the same as the target.
854      Also try to make the last operand a constant.  */
855   if (GET_RTX_CLASS (binoptab->code) == RTX_COMM_ARITH
856       || binoptab == smul_widen_optab
857       || binoptab == umul_widen_optab
858       || binoptab == smul_highpart_optab
859       || binoptab == umul_highpart_optab)
860     {
861       commutative_op = 1;
862
863       if (((target == 0 || REG_P (target))
864            ? ((REG_P (op1)
865                && !REG_P (op0))
866               || target == op1)
867            : rtx_equal_p (op1, target))
868           || GET_CODE (op0) == CONST_INT)
869         {
870           temp = op1;
871           op1 = op0;
872           op0 = temp;
873         }
874     }
875
876   /* If we can do it with a three-operand insn, do so.  */
877
878   if (methods != OPTAB_MUST_WIDEN
879       && binoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
880     {
881       int icode = (int) binoptab->handlers[(int) mode].insn_code;
882       enum machine_mode mode0 = insn_data[icode].operand[1].mode;
883       enum machine_mode mode1 = insn_data[icode].operand[2].mode;
884       rtx pat;
885       rtx xop0 = op0, xop1 = op1;
886
887       if (target)
888         temp = target;
889       else
890         temp = gen_reg_rtx (mode);
891
892       /* If it is a commutative operator and the modes would match
893          if we would swap the operands, we can save the conversions.  */
894       if (commutative_op)
895         {
896           if (GET_MODE (op0) != mode0 && GET_MODE (op1) != mode1
897               && GET_MODE (op0) == mode1 && GET_MODE (op1) == mode0)
898             {
899               rtx tmp;
900
901               tmp = op0; op0 = op1; op1 = tmp;
902               tmp = xop0; xop0 = xop1; xop1 = tmp;
903             }
904         }
905
906       /* In case the insn wants input operands in modes different from
907          those of the actual operands, convert the operands.  It would
908          seem that we don't need to convert CONST_INTs, but we do, so
909          that they're properly zero-extended, sign-extended or truncated
910          for their mode.  */
911
912       if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
913         xop0 = convert_modes (mode0,
914                               GET_MODE (op0) != VOIDmode
915                               ? GET_MODE (op0)
916                               : mode,
917                               xop0, unsignedp);
918
919       if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
920         xop1 = convert_modes (mode1,
921                               GET_MODE (op1) != VOIDmode
922                               ? GET_MODE (op1)
923                               : mode,
924                               xop1, unsignedp);
925
926       /* Now, if insn's predicates don't allow our operands, put them into
927          pseudo regs.  */
928
929       if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0)
930           && mode0 != VOIDmode)
931         xop0 = copy_to_mode_reg (mode0, xop0);
932
933       if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1)
934           && mode1 != VOIDmode)
935         xop1 = copy_to_mode_reg (mode1, xop1);
936
937       if (! (*insn_data[icode].operand[0].predicate) (temp, mode))
938         temp = gen_reg_rtx (mode);
939
940       pat = GEN_FCN (icode) (temp, xop0, xop1);
941       if (pat)
942         {
943           /* If PAT is composed of more than one insn, try to add an appropriate
944              REG_EQUAL note to it.  If we can't because TEMP conflicts with an
945              operand, call ourselves again, this time without a target.  */
946           if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
947               && ! add_equal_note (pat, temp, binoptab->code, xop0, xop1))
948             {
949               delete_insns_since (last);
950               return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
951                                    unsignedp, methods);
952             }
953
954           emit_insn (pat);
955           return temp;
956         }
957       else
958         delete_insns_since (last);
959     }
960
961   /* If this is a multiply, see if we can do a widening operation that
962      takes operands of this mode and makes a wider mode.  */
963
964   if (binoptab == smul_optab && GET_MODE_WIDER_MODE (mode) != VOIDmode
965       && (((unsignedp ? umul_widen_optab : smul_widen_optab)
966            ->handlers[(int) GET_MODE_WIDER_MODE (mode)].insn_code)
967           != CODE_FOR_nothing))
968     {
969       temp = expand_binop (GET_MODE_WIDER_MODE (mode),
970                            unsignedp ? umul_widen_optab : smul_widen_optab,
971                            op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
972
973       if (temp != 0)
974         {
975           if (GET_MODE_CLASS (mode) == MODE_INT)
976             return gen_lowpart (mode, temp);
977           else
978             return convert_to_mode (mode, temp, unsignedp);
979         }
980     }
981
982   /* Look for a wider mode of the same class for which we think we
983      can open-code the operation.  Check for a widening multiply at the
984      wider mode as well.  */
985
986   if ((class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
987       && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
988     for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
989          wider_mode = GET_MODE_WIDER_MODE (wider_mode))
990       {
991         if (binoptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
992             || (binoptab == smul_optab
993                 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
994                 && (((unsignedp ? umul_widen_optab : smul_widen_optab)
995                      ->handlers[(int) GET_MODE_WIDER_MODE (wider_mode)].insn_code)
996                     != CODE_FOR_nothing)))
997           {
998             rtx xop0 = op0, xop1 = op1;
999             int no_extend = 0;
1000
1001             /* For certain integer operations, we need not actually extend
1002                the narrow operands, as long as we will truncate
1003                the results to the same narrowness.  */
1004
1005             if ((binoptab == ior_optab || binoptab == and_optab
1006                  || binoptab == xor_optab
1007                  || binoptab == add_optab || binoptab == sub_optab
1008                  || binoptab == smul_optab || binoptab == ashl_optab)
1009                 && class == MODE_INT)
1010               no_extend = 1;
1011
1012             xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1013
1014             /* The second operand of a shift must always be extended.  */
1015             xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1016                                   no_extend && binoptab != ashl_optab);
1017
1018             temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1019                                  unsignedp, OPTAB_DIRECT);
1020             if (temp)
1021               {
1022                 if (class != MODE_INT)
1023                   {
1024                     if (target == 0)
1025                       target = gen_reg_rtx (mode);
1026                     convert_move (target, temp, 0);
1027                     return target;
1028                   }
1029                 else
1030                   return gen_lowpart (mode, temp);
1031               }
1032             else
1033               delete_insns_since (last);
1034           }
1035       }
1036
1037   /* These can be done a word at a time.  */
1038   if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1039       && class == MODE_INT
1040       && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1041       && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1042     {
1043       int i;
1044       rtx insns;
1045       rtx equiv_value;
1046
1047       /* If TARGET is the same as one of the operands, the REG_EQUAL note
1048          won't be accurate, so use a new target.  */
1049       if (target == 0 || target == op0 || target == op1)
1050         target = gen_reg_rtx (mode);
1051
1052       start_sequence ();
1053
1054       /* Do the actual arithmetic.  */
1055       for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1056         {
1057           rtx target_piece = operand_subword (target, i, 1, mode);
1058           rtx x = expand_binop (word_mode, binoptab,
1059                                 operand_subword_force (op0, i, mode),
1060                                 operand_subword_force (op1, i, mode),
1061                                 target_piece, unsignedp, next_methods);
1062
1063           if (x == 0)
1064             break;
1065
1066           if (target_piece != x)
1067             emit_move_insn (target_piece, x);
1068         }
1069
1070       insns = get_insns ();
1071       end_sequence ();
1072
1073       if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1074         {
1075           if (binoptab->code != UNKNOWN)
1076             equiv_value
1077               = gen_rtx_fmt_ee (binoptab->code, mode,
1078                                 copy_rtx (op0), copy_rtx (op1));
1079           else
1080             equiv_value = 0;
1081
1082           emit_no_conflict_block (insns, target, op0, op1, equiv_value);
1083           return target;
1084         }
1085     }
1086
1087   /* Synthesize double word shifts from single word shifts.  */
1088   if ((binoptab == lshr_optab || binoptab == ashl_optab
1089        || binoptab == ashr_optab)
1090       && class == MODE_INT
1091       && (GET_CODE (op1) == CONST_INT || !optimize_size)
1092       && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1093       && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1094       && ashl_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1095       && lshr_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1096     {
1097       unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1098       enum machine_mode op1_mode;
1099
1100       double_shift_mask = targetm.shift_truncation_mask (mode);
1101       shift_mask = targetm.shift_truncation_mask (word_mode);
1102       op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1103
1104       /* Apply the truncation to constant shifts.  */
1105       if (double_shift_mask > 0 && GET_CODE (op1) == CONST_INT)
1106         op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1107
1108       if (op1 == CONST0_RTX (op1_mode))
1109         return op0;
1110
1111       /* Make sure that this is a combination that expand_doubleword_shift
1112          can handle.  See the comments there for details.  */
1113       if (double_shift_mask == 0
1114           || (shift_mask == BITS_PER_WORD - 1
1115               && double_shift_mask == BITS_PER_WORD * 2 - 1))
1116         {
1117           rtx insns, equiv_value;
1118           rtx into_target, outof_target;
1119           rtx into_input, outof_input;
1120           int left_shift, outof_word;
1121
1122           /* If TARGET is the same as one of the operands, the REG_EQUAL note
1123              won't be accurate, so use a new target.  */
1124           if (target == 0 || target == op0 || target == op1)
1125             target = gen_reg_rtx (mode);
1126
1127           start_sequence ();
1128
1129           /* OUTOF_* is the word we are shifting bits away from, and
1130              INTO_* is the word that we are shifting bits towards, thus
1131              they differ depending on the direction of the shift and
1132              WORDS_BIG_ENDIAN.  */
1133
1134           left_shift = binoptab == ashl_optab;
1135           outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1136
1137           outof_target = operand_subword (target, outof_word, 1, mode);
1138           into_target = operand_subword (target, 1 - outof_word, 1, mode);
1139
1140           outof_input = operand_subword_force (op0, outof_word, mode);
1141           into_input = operand_subword_force (op0, 1 - outof_word, mode);
1142
1143           if (expand_doubleword_shift (op1_mode, binoptab,
1144                                        outof_input, into_input, op1,
1145                                        outof_target, into_target,
1146                                        unsignedp, methods, shift_mask))
1147             {
1148               insns = get_insns ();
1149               end_sequence ();
1150
1151               equiv_value = gen_rtx_fmt_ee (binoptab->code, mode, op0, op1);
1152               emit_no_conflict_block (insns, target, op0, op1, equiv_value);
1153               return target;
1154             }
1155           end_sequence ();
1156         }
1157     }
1158
1159   /* Synthesize double word rotates from single word shifts.  */
1160   if ((binoptab == rotl_optab || binoptab == rotr_optab)
1161       && class == MODE_INT
1162       && GET_CODE (op1) == CONST_INT
1163       && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1164       && ashl_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1165       && lshr_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1166     {
1167       rtx insns, equiv_value;
1168       rtx into_target, outof_target;
1169       rtx into_input, outof_input;
1170       rtx inter;
1171       int shift_count, left_shift, outof_word;
1172
1173       /* If TARGET is the same as one of the operands, the REG_EQUAL note
1174          won't be accurate, so use a new target. Do this also if target is not
1175          a REG, first because having a register instead may open optimization
1176          opportunities, and second because if target and op0 happen to be MEMs
1177          designating the same location, we would risk clobbering it too early
1178          in the code sequence we generate below.  */
1179       if (target == 0 || target == op0 || target == op1 || ! REG_P (target))
1180         target = gen_reg_rtx (mode);
1181
1182       start_sequence ();
1183
1184       shift_count = INTVAL (op1);
1185
1186       /* OUTOF_* is the word we are shifting bits away from, and
1187          INTO_* is the word that we are shifting bits towards, thus
1188          they differ depending on the direction of the shift and
1189          WORDS_BIG_ENDIAN.  */
1190
1191       left_shift = (binoptab == rotl_optab);
1192       outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1193
1194       outof_target = operand_subword (target, outof_word, 1, mode);
1195       into_target = operand_subword (target, 1 - outof_word, 1, mode);
1196
1197       outof_input = operand_subword_force (op0, outof_word, mode);
1198       into_input = operand_subword_force (op0, 1 - outof_word, mode);
1199
1200       if (shift_count == BITS_PER_WORD)
1201         {
1202           /* This is just a word swap.  */
1203           emit_move_insn (outof_target, into_input);
1204           emit_move_insn (into_target, outof_input);
1205           inter = const0_rtx;
1206         }
1207       else
1208         {
1209           rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1210           rtx first_shift_count, second_shift_count;
1211           optab reverse_unsigned_shift, unsigned_shift;
1212
1213           reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1214                                     ? lshr_optab : ashl_optab);
1215
1216           unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1217                             ? ashl_optab : lshr_optab);
1218
1219           if (shift_count > BITS_PER_WORD)
1220             {
1221               first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1222               second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1223             }
1224           else
1225             {
1226               first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1227               second_shift_count = GEN_INT (shift_count);
1228             }
1229
1230           into_temp1 = expand_binop (word_mode, unsigned_shift,
1231                                      outof_input, first_shift_count,
1232                                      NULL_RTX, unsignedp, next_methods);
1233           into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1234                                      into_input, second_shift_count,
1235                                      NULL_RTX, unsignedp, next_methods);
1236
1237           if (into_temp1 != 0 && into_temp2 != 0)
1238             inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1239                                   into_target, unsignedp, next_methods);
1240           else
1241             inter = 0;
1242
1243           if (inter != 0 && inter != into_target)
1244             emit_move_insn (into_target, inter);
1245
1246           outof_temp1 = expand_binop (word_mode, unsigned_shift,
1247                                       into_input, first_shift_count,
1248                                       NULL_RTX, unsignedp, next_methods);
1249           outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1250                                       outof_input, second_shift_count,
1251                                       NULL_RTX, unsignedp, next_methods);
1252
1253           if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1254             inter = expand_binop (word_mode, ior_optab,
1255                                   outof_temp1, outof_temp2,
1256                                   outof_target, unsignedp, next_methods);
1257
1258           if (inter != 0 && inter != outof_target)
1259             emit_move_insn (outof_target, inter);
1260         }
1261
1262       insns = get_insns ();
1263       end_sequence ();
1264
1265       if (inter != 0)
1266         {
1267           if (binoptab->code != UNKNOWN)
1268             equiv_value = gen_rtx_fmt_ee (binoptab->code, mode, op0, op1);
1269           else
1270             equiv_value = 0;
1271
1272           /* We can't make this a no conflict block if this is a word swap,
1273              because the word swap case fails if the input and output values
1274              are in the same register.  */
1275           if (shift_count != BITS_PER_WORD)
1276             emit_no_conflict_block (insns, target, op0, op1, equiv_value);
1277           else
1278             emit_insn (insns);
1279
1280
1281           return target;
1282         }
1283     }
1284
1285   /* These can be done a word at a time by propagating carries.  */
1286   if ((binoptab == add_optab || binoptab == sub_optab)
1287       && class == MODE_INT
1288       && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1289       && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1290     {
1291       unsigned int i;
1292       optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1293       const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1294       rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1295       rtx xop0, xop1, xtarget;
1296
1297       /* We can handle either a 1 or -1 value for the carry.  If STORE_FLAG
1298          value is one of those, use it.  Otherwise, use 1 since it is the
1299          one easiest to get.  */
1300 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1301       int normalizep = STORE_FLAG_VALUE;
1302 #else
1303       int normalizep = 1;
1304 #endif
1305
1306       /* Prepare the operands.  */
1307       xop0 = force_reg (mode, op0);
1308       xop1 = force_reg (mode, op1);
1309
1310       xtarget = gen_reg_rtx (mode);
1311
1312       if (target == 0 || !REG_P (target))
1313         target = xtarget;
1314
1315       /* Indicate for flow that the entire target reg is being set.  */
1316       if (REG_P (target))
1317         emit_insn (gen_rtx_CLOBBER (VOIDmode, xtarget));
1318
1319       /* Do the actual arithmetic.  */
1320       for (i = 0; i < nwords; i++)
1321         {
1322           int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1323           rtx target_piece = operand_subword (xtarget, index, 1, mode);
1324           rtx op0_piece = operand_subword_force (xop0, index, mode);
1325           rtx op1_piece = operand_subword_force (xop1, index, mode);
1326           rtx x;
1327
1328           /* Main add/subtract of the input operands.  */
1329           x = expand_binop (word_mode, binoptab,
1330                             op0_piece, op1_piece,
1331                             target_piece, unsignedp, next_methods);
1332           if (x == 0)
1333             break;
1334
1335           if (i + 1 < nwords)
1336             {
1337               /* Store carry from main add/subtract.  */
1338               carry_out = gen_reg_rtx (word_mode);
1339               carry_out = emit_store_flag_force (carry_out,
1340                                                  (binoptab == add_optab
1341                                                   ? LT : GT),
1342                                                  x, op0_piece,
1343                                                  word_mode, 1, normalizep);
1344             }
1345
1346           if (i > 0)
1347             {
1348               rtx newx;
1349
1350               /* Add/subtract previous carry to main result.  */
1351               newx = expand_binop (word_mode,
1352                                    normalizep == 1 ? binoptab : otheroptab,
1353                                    x, carry_in,
1354                                    NULL_RTX, 1, next_methods);
1355
1356               if (i + 1 < nwords)
1357                 {
1358                   /* Get out carry from adding/subtracting carry in.  */
1359                   rtx carry_tmp = gen_reg_rtx (word_mode);
1360                   carry_tmp = emit_store_flag_force (carry_tmp,
1361                                                      (binoptab == add_optab
1362                                                       ? LT : GT),
1363                                                      newx, x,
1364                                                      word_mode, 1, normalizep);
1365
1366                   /* Logical-ior the two poss. carry together.  */
1367                   carry_out = expand_binop (word_mode, ior_optab,
1368                                             carry_out, carry_tmp,
1369                                             carry_out, 0, next_methods);
1370                   if (carry_out == 0)
1371                     break;
1372                 }
1373               emit_move_insn (target_piece, newx);
1374             }
1375
1376           carry_in = carry_out;
1377         }
1378
1379       if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
1380         {
1381           if (mov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
1382               || ! rtx_equal_p (target, xtarget))
1383             {
1384               rtx temp = emit_move_insn (target, xtarget);
1385
1386               set_unique_reg_note (temp,
1387                                    REG_EQUAL,
1388                                    gen_rtx_fmt_ee (binoptab->code, mode,
1389                                                    copy_rtx (xop0),
1390                                                    copy_rtx (xop1)));
1391             }
1392           else
1393             target = xtarget;
1394
1395           return target;
1396         }
1397
1398       else
1399         delete_insns_since (last);
1400     }
1401
1402   /* If we want to multiply two two-word values and have normal and widening
1403      multiplies of single-word values, we can do this with three smaller
1404      multiplications.  Note that we do not make a REG_NO_CONFLICT block here
1405      because we are not operating on one word at a time.
1406
1407      The multiplication proceeds as follows:
1408                                  _______________________
1409                                 [__op0_high_|__op0_low__]
1410                                  _______________________
1411         *                       [__op1_high_|__op1_low__]
1412         _______________________________________________
1413                                  _______________________
1414     (1)                         [__op0_low__*__op1_low__]
1415                      _______________________
1416     (2a)            [__op0_low__*__op1_high_]
1417                      _______________________
1418     (2b)            [__op0_high_*__op1_low__]
1419          _______________________
1420     (3) [__op0_high_*__op1_high_]
1421
1422
1423     This gives a 4-word result.  Since we are only interested in the
1424     lower 2 words, partial result (3) and the upper words of (2a) and
1425     (2b) don't need to be calculated.  Hence (2a) and (2b) can be
1426     calculated using non-widening multiplication.
1427
1428     (1), however, needs to be calculated with an unsigned widening
1429     multiplication.  If this operation is not directly supported we
1430     try using a signed widening multiplication and adjust the result.
1431     This adjustment works as follows:
1432
1433       If both operands are positive then no adjustment is needed.
1434
1435       If the operands have different signs, for example op0_low < 0 and
1436       op1_low >= 0, the instruction treats the most significant bit of
1437       op0_low as a sign bit instead of a bit with significance
1438       2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1439       with 2**BITS_PER_WORD - op0_low, and two's complements the
1440       result.  Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1441       the result.
1442
1443       Similarly, if both operands are negative, we need to add
1444       (op0_low + op1_low) * 2**BITS_PER_WORD.
1445
1446       We use a trick to adjust quickly.  We logically shift op0_low right
1447       (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1448       op0_high (op1_high) before it is used to calculate 2b (2a).  If no
1449       logical shift exists, we do an arithmetic right shift and subtract
1450       the 0 or -1.  */
1451
1452   if (binoptab == smul_optab
1453       && class == MODE_INT
1454       && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1455       && smul_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1456       && add_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1457       && ((umul_widen_optab->handlers[(int) mode].insn_code
1458            != CODE_FOR_nothing)
1459           || (smul_widen_optab->handlers[(int) mode].insn_code
1460               != CODE_FOR_nothing)))
1461     {
1462       int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1463       int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1464       rtx op0_high = operand_subword_force (op0, high, mode);
1465       rtx op0_low = operand_subword_force (op0, low, mode);
1466       rtx op1_high = operand_subword_force (op1, high, mode);
1467       rtx op1_low = operand_subword_force (op1, low, mode);
1468       rtx product = 0;
1469       rtx op0_xhigh = NULL_RTX;
1470       rtx op1_xhigh = NULL_RTX;
1471
1472       /* If the target is the same as one of the inputs, don't use it.  This
1473          prevents problems with the REG_EQUAL note.  */
1474       if (target == op0 || target == op1
1475           || (target != 0 && !REG_P (target)))
1476         target = 0;
1477
1478       /* Multiply the two lower words to get a double-word product.
1479          If unsigned widening multiplication is available, use that;
1480          otherwise use the signed form and compensate.  */
1481
1482       if (umul_widen_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1483         {
1484           product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1485                                   target, 1, OPTAB_DIRECT);
1486
1487           /* If we didn't succeed, delete everything we did so far.  */
1488           if (product == 0)
1489             delete_insns_since (last);
1490           else
1491             op0_xhigh = op0_high, op1_xhigh = op1_high;
1492         }
1493
1494       if (product == 0
1495           && smul_widen_optab->handlers[(int) mode].insn_code
1496                != CODE_FOR_nothing)
1497         {
1498           rtx wordm1 = GEN_INT (BITS_PER_WORD - 1);
1499           product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1500                                   target, 1, OPTAB_DIRECT);
1501           op0_xhigh = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1502                                     NULL_RTX, 1, next_methods);
1503           if (op0_xhigh)
1504             op0_xhigh = expand_binop (word_mode, add_optab, op0_high,
1505                                       op0_xhigh, op0_xhigh, 0, next_methods);
1506           else
1507             {
1508               op0_xhigh = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1509                                         NULL_RTX, 0, next_methods);
1510               if (op0_xhigh)
1511                 op0_xhigh = expand_binop (word_mode, sub_optab, op0_high,
1512                                           op0_xhigh, op0_xhigh, 0,
1513                                           next_methods);
1514             }
1515
1516           op1_xhigh = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1517                                     NULL_RTX, 1, next_methods);
1518           if (op1_xhigh)
1519             op1_xhigh = expand_binop (word_mode, add_optab, op1_high,
1520                                       op1_xhigh, op1_xhigh, 0, next_methods);
1521           else
1522             {
1523               op1_xhigh = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1524                                         NULL_RTX, 0, next_methods);
1525               if (op1_xhigh)
1526                 op1_xhigh = expand_binop (word_mode, sub_optab, op1_high,
1527                                           op1_xhigh, op1_xhigh, 0,
1528                                           next_methods);
1529             }
1530         }
1531
1532       /* If we have been able to directly compute the product of the
1533          low-order words of the operands and perform any required adjustments
1534          of the operands, we proceed by trying two more multiplications
1535          and then computing the appropriate sum.
1536
1537          We have checked above that the required addition is provided.
1538          Full-word addition will normally always succeed, especially if
1539          it is provided at all, so we don't worry about its failure.  The
1540          multiplication may well fail, however, so we do handle that.  */
1541
1542       if (product && op0_xhigh && op1_xhigh)
1543         {
1544           rtx product_high = operand_subword (product, high, 1, mode);
1545           rtx temp = expand_binop (word_mode, binoptab, op0_low, op1_xhigh,
1546                                    NULL_RTX, 0, OPTAB_DIRECT);
1547
1548           if (!REG_P (product_high))
1549             product_high = force_reg (word_mode, product_high);
1550
1551           if (temp != 0)
1552             temp = expand_binop (word_mode, add_optab, temp, product_high,
1553                                  product_high, 0, next_methods);
1554
1555           if (temp != 0 && temp != product_high)
1556             emit_move_insn (product_high, temp);
1557
1558           if (temp != 0)
1559             temp = expand_binop (word_mode, binoptab, op1_low, op0_xhigh,
1560                                  NULL_RTX, 0, OPTAB_DIRECT);
1561
1562           if (temp != 0)
1563             temp = expand_binop (word_mode, add_optab, temp,
1564                                  product_high, product_high,
1565                                  0, next_methods);
1566
1567           if (temp != 0 && temp != product_high)
1568             emit_move_insn (product_high, temp);
1569
1570           emit_move_insn (operand_subword (product, high, 1, mode), product_high);
1571
1572           if (temp != 0)
1573             {
1574               if (mov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1575                 {
1576                   temp = emit_move_insn (product, product);
1577                   set_unique_reg_note (temp,
1578                                        REG_EQUAL,
1579                                        gen_rtx_fmt_ee (MULT, mode,
1580                                                        copy_rtx (op0),
1581                                                        copy_rtx (op1)));
1582                 }
1583
1584               return product;
1585             }
1586         }
1587
1588       /* If we get here, we couldn't do it for some reason even though we
1589          originally thought we could.  Delete anything we've emitted in
1590          trying to do it.  */
1591
1592       delete_insns_since (last);
1593     }
1594
1595   /* It can't be open-coded in this mode.
1596      Use a library call if one is available and caller says that's ok.  */
1597
1598   if (binoptab->handlers[(int) mode].libfunc
1599       && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
1600     {
1601       rtx insns;
1602       rtx op1x = op1;
1603       enum machine_mode op1_mode = mode;
1604       rtx value;
1605
1606       start_sequence ();
1607
1608       if (shift_op)
1609         {
1610           op1_mode = word_mode;
1611           /* Specify unsigned here,
1612              since negative shift counts are meaningless.  */
1613           op1x = convert_to_mode (word_mode, op1, 1);
1614         }
1615
1616       if (GET_MODE (op0) != VOIDmode
1617           && GET_MODE (op0) != mode)
1618         op0 = convert_to_mode (mode, op0, unsignedp);
1619
1620       /* Pass 1 for NO_QUEUE so we don't lose any increments
1621          if the libcall is cse'd or moved.  */
1622       value = emit_library_call_value (binoptab->handlers[(int) mode].libfunc,
1623                                        NULL_RTX, LCT_CONST, mode, 2,
1624                                        op0, mode, op1x, op1_mode);
1625
1626       insns = get_insns ();
1627       end_sequence ();
1628
1629       target = gen_reg_rtx (mode);
1630       emit_libcall_block (insns, target, value,
1631                           gen_rtx_fmt_ee (binoptab->code, mode, op0, op1));
1632
1633       return target;
1634     }
1635
1636   delete_insns_since (last);
1637
1638   /* It can't be done in this mode.  Can we do it in a wider mode?  */
1639
1640   if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
1641          || methods == OPTAB_MUST_WIDEN))
1642     {
1643       /* Caller says, don't even try.  */
1644       delete_insns_since (entry_last);
1645       return 0;
1646     }
1647
1648   /* Compute the value of METHODS to pass to recursive calls.
1649      Don't allow widening to be tried recursively.  */
1650
1651   methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
1652
1653   /* Look for a wider mode of the same class for which it appears we can do
1654      the operation.  */
1655
1656   if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1657     {
1658       for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1659            wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1660         {
1661           if ((binoptab->handlers[(int) wider_mode].insn_code
1662                != CODE_FOR_nothing)
1663               || (methods == OPTAB_LIB
1664                   && binoptab->handlers[(int) wider_mode].libfunc))
1665             {
1666               rtx xop0 = op0, xop1 = op1;
1667               int no_extend = 0;
1668
1669               /* For certain integer operations, we need not actually extend
1670                  the narrow operands, as long as we will truncate
1671                  the results to the same narrowness.  */
1672
1673               if ((binoptab == ior_optab || binoptab == and_optab
1674                    || binoptab == xor_optab
1675                    || binoptab == add_optab || binoptab == sub_optab
1676                    || binoptab == smul_optab || binoptab == ashl_optab)
1677                   && class == MODE_INT)
1678                 no_extend = 1;
1679
1680               xop0 = widen_operand (xop0, wider_mode, mode,
1681                                     unsignedp, no_extend);
1682
1683               /* The second operand of a shift must always be extended.  */
1684               xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1685                                     no_extend && binoptab != ashl_optab);
1686
1687               temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1688                                    unsignedp, methods);
1689               if (temp)
1690                 {
1691                   if (class != MODE_INT)
1692                     {
1693                       if (target == 0)
1694                         target = gen_reg_rtx (mode);
1695                       convert_move (target, temp, 0);
1696                       return target;
1697                     }
1698                   else
1699                     return gen_lowpart (mode, temp);
1700                 }
1701               else
1702                 delete_insns_since (last);
1703             }
1704         }
1705     }
1706
1707   delete_insns_since (entry_last);
1708   return 0;
1709 }
1710 \f
1711 /* Expand a binary operator which has both signed and unsigned forms.
1712    UOPTAB is the optab for unsigned operations, and SOPTAB is for
1713    signed operations.
1714
1715    If we widen unsigned operands, we may use a signed wider operation instead
1716    of an unsigned wider operation, since the result would be the same.  */
1717
1718 rtx
1719 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
1720                    rtx op0, rtx op1, rtx target, int unsignedp,
1721                    enum optab_methods methods)
1722 {
1723   rtx temp;
1724   optab direct_optab = unsignedp ? uoptab : soptab;
1725   struct optab wide_soptab;
1726
1727   /* Do it without widening, if possible.  */
1728   temp = expand_binop (mode, direct_optab, op0, op1, target,
1729                        unsignedp, OPTAB_DIRECT);
1730   if (temp || methods == OPTAB_DIRECT)
1731     return temp;
1732
1733   /* Try widening to a signed int.  Make a fake signed optab that
1734      hides any signed insn for direct use.  */
1735   wide_soptab = *soptab;
1736   wide_soptab.handlers[(int) mode].insn_code = CODE_FOR_nothing;
1737   wide_soptab.handlers[(int) mode].libfunc = 0;
1738
1739   temp = expand_binop (mode, &wide_soptab, op0, op1, target,
1740                        unsignedp, OPTAB_WIDEN);
1741
1742   /* For unsigned operands, try widening to an unsigned int.  */
1743   if (temp == 0 && unsignedp)
1744     temp = expand_binop (mode, uoptab, op0, op1, target,
1745                          unsignedp, OPTAB_WIDEN);
1746   if (temp || methods == OPTAB_WIDEN)
1747     return temp;
1748
1749   /* Use the right width lib call if that exists.  */
1750   temp = expand_binop (mode, direct_optab, op0, op1, target, unsignedp, OPTAB_LIB);
1751   if (temp || methods == OPTAB_LIB)
1752     return temp;
1753
1754   /* Must widen and use a lib call, use either signed or unsigned.  */
1755   temp = expand_binop (mode, &wide_soptab, op0, op1, target,
1756                        unsignedp, methods);
1757   if (temp != 0)
1758     return temp;
1759   if (unsignedp)
1760     return expand_binop (mode, uoptab, op0, op1, target,
1761                          unsignedp, methods);
1762   return 0;
1763 }
1764 \f
1765 /* Generate code to perform an operation specified by UNOPPTAB
1766    on operand OP0, with two results to TARG0 and TARG1.
1767    We assume that the order of the operands for the instruction
1768    is TARG0, TARG1, OP0.
1769
1770    Either TARG0 or TARG1 may be zero, but what that means is that
1771    the result is not actually wanted.  We will generate it into
1772    a dummy pseudo-reg and discard it.  They may not both be zero.
1773
1774    Returns 1 if this operation can be performed; 0 if not.  */
1775
1776 int
1777 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
1778                     int unsignedp)
1779 {
1780   enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1781   enum mode_class class;
1782   enum machine_mode wider_mode;
1783   rtx entry_last = get_last_insn ();
1784   rtx last;
1785
1786   class = GET_MODE_CLASS (mode);
1787
1788   if (flag_force_mem)
1789     op0 = force_not_mem (op0);
1790
1791   if (!targ0)
1792     targ0 = gen_reg_rtx (mode);
1793   if (!targ1)
1794     targ1 = gen_reg_rtx (mode);
1795
1796   /* Record where to go back to if we fail.  */
1797   last = get_last_insn ();
1798
1799   if (unoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1800     {
1801       int icode = (int) unoptab->handlers[(int) mode].insn_code;
1802       enum machine_mode mode0 = insn_data[icode].operand[2].mode;
1803       rtx pat;
1804       rtx xop0 = op0;
1805
1806       if (GET_MODE (xop0) != VOIDmode
1807           && GET_MODE (xop0) != mode0)
1808         xop0 = convert_to_mode (mode0, xop0, unsignedp);
1809
1810       /* Now, if insn doesn't accept these operands, put them into pseudos.  */
1811       if (! (*insn_data[icode].operand[2].predicate) (xop0, mode0))
1812         xop0 = copy_to_mode_reg (mode0, xop0);
1813
1814       /* We could handle this, but we should always be called with a pseudo
1815          for our targets and all insns should take them as outputs.  */
1816       if (! (*insn_data[icode].operand[0].predicate) (targ0, mode)
1817           || ! (*insn_data[icode].operand[1].predicate) (targ1, mode))
1818         abort ();
1819
1820       pat = GEN_FCN (icode) (targ0, targ1, xop0);
1821       if (pat)
1822         {
1823           emit_insn (pat);
1824           return 1;
1825         }
1826       else
1827         delete_insns_since (last);
1828     }
1829
1830   /* It can't be done in this mode.  Can we do it in a wider mode?  */
1831
1832   if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1833     {
1834       for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1835            wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1836         {
1837           if (unoptab->handlers[(int) wider_mode].insn_code
1838               != CODE_FOR_nothing)
1839             {
1840               rtx t0 = gen_reg_rtx (wider_mode);
1841               rtx t1 = gen_reg_rtx (wider_mode);
1842               rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
1843
1844               if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
1845                 {
1846                   convert_move (targ0, t0, unsignedp);
1847                   convert_move (targ1, t1, unsignedp);
1848                   return 1;
1849                 }
1850               else
1851                 delete_insns_since (last);
1852             }
1853         }
1854     }
1855
1856   delete_insns_since (entry_last);
1857   return 0;
1858 }
1859 \f
1860 /* Generate code to perform an operation specified by BINOPTAB
1861    on operands OP0 and OP1, with two results to TARG1 and TARG2.
1862    We assume that the order of the operands for the instruction
1863    is TARG0, OP0, OP1, TARG1, which would fit a pattern like
1864    [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
1865
1866    Either TARG0 or TARG1 may be zero, but what that means is that
1867    the result is not actually wanted.  We will generate it into
1868    a dummy pseudo-reg and discard it.  They may not both be zero.
1869
1870    Returns 1 if this operation can be performed; 0 if not.  */
1871
1872 int
1873 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
1874                      int unsignedp)
1875 {
1876   enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1877   enum mode_class class;
1878   enum machine_mode wider_mode;
1879   rtx entry_last = get_last_insn ();
1880   rtx last;
1881
1882   class = GET_MODE_CLASS (mode);
1883
1884   if (flag_force_mem)
1885     {
1886       op0 = force_not_mem (op0);
1887       op1 = force_not_mem (op1);
1888     }
1889
1890   /* If we are inside an appropriately-short loop and we are optimizing,
1891      force expensive constants into a register.  */
1892   if (CONSTANT_P (op0) && optimize
1893       && rtx_cost (op0, binoptab->code) > COSTS_N_INSNS (1))
1894     op0 = force_reg (mode, op0);
1895
1896   if (CONSTANT_P (op1) && optimize
1897       && rtx_cost (op1, binoptab->code) > COSTS_N_INSNS (1))
1898     op1 = force_reg (mode, op1);
1899
1900   if (!targ0)
1901     targ0 = gen_reg_rtx (mode);
1902   if (!targ1)
1903     targ1 = gen_reg_rtx (mode);
1904
1905   /* Record where to go back to if we fail.  */
1906   last = get_last_insn ();
1907
1908   if (binoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1909     {
1910       int icode = (int) binoptab->handlers[(int) mode].insn_code;
1911       enum machine_mode mode0 = insn_data[icode].operand[1].mode;
1912       enum machine_mode mode1 = insn_data[icode].operand[2].mode;
1913       rtx pat;
1914       rtx xop0 = op0, xop1 = op1;
1915
1916       /* In case the insn wants input operands in modes different from
1917          those of the actual operands, convert the operands.  It would
1918          seem that we don't need to convert CONST_INTs, but we do, so
1919          that they're properly zero-extended, sign-extended or truncated
1920          for their mode.  */
1921
1922       if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
1923         xop0 = convert_modes (mode0,
1924                               GET_MODE (op0) != VOIDmode
1925                               ? GET_MODE (op0)
1926                               : mode,
1927                               xop0, unsignedp);
1928
1929       if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
1930         xop1 = convert_modes (mode1,
1931                               GET_MODE (op1) != VOIDmode
1932                               ? GET_MODE (op1)
1933                               : mode,
1934                               xop1, unsignedp);
1935
1936       /* Now, if insn doesn't accept these operands, put them into pseudos.  */
1937       if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0))
1938         xop0 = copy_to_mode_reg (mode0, xop0);
1939
1940       if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1))
1941         xop1 = copy_to_mode_reg (mode1, xop1);
1942
1943       /* We could handle this, but we should always be called with a pseudo
1944          for our targets and all insns should take them as outputs.  */
1945       if (! (*insn_data[icode].operand[0].predicate) (targ0, mode)
1946           || ! (*insn_data[icode].operand[3].predicate) (targ1, mode))
1947         abort ();
1948
1949       pat = GEN_FCN (icode) (targ0, xop0, xop1, targ1);
1950       if (pat)
1951         {
1952           emit_insn (pat);
1953           return 1;
1954         }
1955       else
1956         delete_insns_since (last);
1957     }
1958
1959   /* It can't be done in this mode.  Can we do it in a wider mode?  */
1960
1961   if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1962     {
1963       for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1964            wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1965         {
1966           if (binoptab->handlers[(int) wider_mode].insn_code
1967               != CODE_FOR_nothing)
1968             {
1969               rtx t0 = gen_reg_rtx (wider_mode);
1970               rtx t1 = gen_reg_rtx (wider_mode);
1971               rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
1972               rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
1973
1974               if (expand_twoval_binop (binoptab, cop0, cop1,
1975                                        t0, t1, unsignedp))
1976                 {
1977                   convert_move (targ0, t0, unsignedp);
1978                   convert_move (targ1, t1, unsignedp);
1979                   return 1;
1980                 }
1981               else
1982                 delete_insns_since (last);
1983             }
1984         }
1985     }
1986
1987   delete_insns_since (entry_last);
1988   return 0;
1989 }
1990
1991 /* Expand the two-valued library call indicated by BINOPTAB, but
1992    preserve only one of the values.  If TARG0 is non-NULL, the first
1993    value is placed into TARG0; otherwise the second value is placed
1994    into TARG1.  Exactly one of TARG0 and TARG1 must be non-NULL.  The
1995    value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
1996    This routine assumes that the value returned by the library call is
1997    as if the return value was of an integral mode twice as wide as the
1998    mode of OP0.  Returns 1 if the call was successful.  */
1999
2000 bool
2001 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2002                              rtx targ0, rtx targ1, enum rtx_code code)
2003 {
2004   enum machine_mode mode;
2005   enum machine_mode libval_mode;
2006   rtx libval;
2007   rtx insns;
2008
2009   /* Exactly one of TARG0 or TARG1 should be non-NULL.  */
2010   if (!((targ0 != NULL_RTX) ^ (targ1 != NULL_RTX)))
2011     abort ();
2012
2013   mode = GET_MODE (op0);
2014   if (!binoptab->handlers[(int) mode].libfunc)
2015     return false;
2016
2017   /* The value returned by the library function will have twice as
2018      many bits as the nominal MODE.  */
2019   libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2020                                         MODE_INT);
2021   start_sequence ();
2022   libval = emit_library_call_value (binoptab->handlers[(int) mode].libfunc,
2023                                     NULL_RTX, LCT_CONST,
2024                                     libval_mode, 2,
2025                                     op0, mode,
2026                                     op1, mode);
2027   /* Get the part of VAL containing the value that we want.  */
2028   libval = simplify_gen_subreg (mode, libval, libval_mode,
2029                                 targ0 ? 0 : GET_MODE_SIZE (mode));
2030   insns = get_insns ();
2031   end_sequence ();
2032   /* Move the into the desired location.  */
2033   emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2034                       gen_rtx_fmt_ee (code, mode, op0, op1));
2035
2036   return true;
2037 }
2038
2039 \f
2040 /* Wrapper around expand_unop which takes an rtx code to specify
2041    the operation to perform, not an optab pointer.  All other
2042    arguments are the same.  */
2043 rtx
2044 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2045                     rtx target, int unsignedp)
2046 {
2047   optab unop = code_to_optab[(int) code];
2048   if (unop == 0)
2049     abort ();
2050
2051   return expand_unop (mode, unop, op0, target, unsignedp);
2052 }
2053
2054 /* Try calculating
2055         (clz:narrow x)
2056    as
2057         (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).  */
2058 static rtx
2059 widen_clz (enum machine_mode mode, rtx op0, rtx target)
2060 {
2061   enum mode_class class = GET_MODE_CLASS (mode);
2062   if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2063     {
2064       enum machine_mode wider_mode;
2065       for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2066            wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2067         {
2068           if (clz_optab->handlers[(int) wider_mode].insn_code
2069               != CODE_FOR_nothing)
2070             {
2071               rtx xop0, temp, last;
2072
2073               last = get_last_insn ();
2074
2075               if (target == 0)
2076                 target = gen_reg_rtx (mode);
2077               xop0 = widen_operand (op0, wider_mode, mode, true, false);
2078               temp = expand_unop (wider_mode, clz_optab, xop0, NULL_RTX, true);
2079               if (temp != 0)
2080                 temp = expand_binop (wider_mode, sub_optab, temp,
2081                                      GEN_INT (GET_MODE_BITSIZE (wider_mode)
2082                                               - GET_MODE_BITSIZE (mode)),
2083                                      target, true, OPTAB_DIRECT);
2084               if (temp == 0)
2085                 delete_insns_since (last);
2086
2087               return temp;
2088             }
2089         }
2090     }
2091   return 0;
2092 }
2093
2094 /* Try calculating (parity x) as (and (popcount x) 1), where
2095    popcount can also be done in a wider mode.  */
2096 static rtx
2097 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2098 {
2099   enum mode_class class = GET_MODE_CLASS (mode);
2100   if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2101     {
2102       enum machine_mode wider_mode;
2103       for (wider_mode = mode; wider_mode != VOIDmode;
2104            wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2105         {
2106           if (popcount_optab->handlers[(int) wider_mode].insn_code
2107               != CODE_FOR_nothing)
2108             {
2109               rtx xop0, temp, last;
2110
2111               last = get_last_insn ();
2112
2113               if (target == 0)
2114                 target = gen_reg_rtx (mode);
2115               xop0 = widen_operand (op0, wider_mode, mode, true, false);
2116               temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2117                                   true);
2118               if (temp != 0)
2119                 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2120                                      target, true, OPTAB_DIRECT);
2121               if (temp == 0)
2122                 delete_insns_since (last);
2123
2124               return temp;
2125             }
2126         }
2127     }
2128   return 0;
2129 }
2130
2131 /* Extract the OMODE lowpart from VAL, which has IMODE.  Under certain 
2132    conditions, VAL may already be a SUBREG against which we cannot generate
2133    a further SUBREG.  In this case, we expect forcing the value into a
2134    register will work around the situation.  */
2135
2136 static rtx
2137 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2138                            enum machine_mode imode)
2139 {
2140   rtx ret;
2141   ret = lowpart_subreg (omode, val, imode);
2142   if (ret == NULL)
2143     {
2144       val = force_reg (imode, val);
2145       ret = lowpart_subreg (omode, val, imode);
2146       gcc_assert (ret != NULL);
2147     }
2148   return ret;
2149 }
2150
2151 /* Generate code to perform an operation specified by UNOPTAB
2152    on operand OP0, with result having machine-mode MODE.
2153
2154    UNSIGNEDP is for the case where we have to widen the operands
2155    to perform the operation.  It says to use zero-extension.
2156
2157    If TARGET is nonzero, the value
2158    is generated there, if it is convenient to do so.
2159    In all cases an rtx is returned for the locus of the value;
2160    this may or may not be TARGET.  */
2161
2162 rtx
2163 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2164              int unsignedp)
2165 {
2166   enum mode_class class;
2167   enum machine_mode wider_mode;
2168   rtx temp;
2169   rtx last = get_last_insn ();
2170   rtx pat;
2171
2172   class = GET_MODE_CLASS (mode);
2173
2174   if (flag_force_mem)
2175     op0 = force_not_mem (op0);
2176
2177   if (unoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2178     {
2179       int icode = (int) unoptab->handlers[(int) mode].insn_code;
2180       enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2181       rtx xop0 = op0;
2182
2183       if (target)
2184         temp = target;
2185       else
2186         temp = gen_reg_rtx (mode);
2187
2188       if (GET_MODE (xop0) != VOIDmode
2189           && GET_MODE (xop0) != mode0)
2190         xop0 = convert_to_mode (mode0, xop0, unsignedp);
2191
2192       /* Now, if insn doesn't accept our operand, put it into a pseudo.  */
2193
2194       if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0))
2195         xop0 = copy_to_mode_reg (mode0, xop0);
2196
2197       if (! (*insn_data[icode].operand[0].predicate) (temp, mode))
2198         temp = gen_reg_rtx (mode);
2199
2200       pat = GEN_FCN (icode) (temp, xop0);
2201       if (pat)
2202         {
2203           if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2204               && ! add_equal_note (pat, temp, unoptab->code, xop0, NULL_RTX))
2205             {
2206               delete_insns_since (last);
2207               return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
2208             }
2209
2210           emit_insn (pat);
2211
2212           return temp;
2213         }
2214       else
2215         delete_insns_since (last);
2216     }
2217
2218   /* It can't be done in this mode.  Can we open-code it in a wider mode?  */
2219
2220   /* Widening clz needs special treatment.  */
2221   if (unoptab == clz_optab)
2222     {
2223       temp = widen_clz (mode, op0, target);
2224       if (temp)
2225         return temp;
2226       else
2227         goto try_libcall;
2228     }
2229
2230   if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2231     for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2232          wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2233       {
2234         if (unoptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing)
2235           {
2236             rtx xop0 = op0;
2237
2238             /* For certain operations, we need not actually extend
2239                the narrow operand, as long as we will truncate the
2240                results to the same narrowness.  */
2241
2242             xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2243                                   (unoptab == neg_optab
2244                                    || unoptab == one_cmpl_optab)
2245                                   && class == MODE_INT);
2246
2247             temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2248                                 unsignedp);
2249
2250             if (temp)
2251               {
2252                 if (class != MODE_INT)
2253                   {
2254                     if (target == 0)
2255                       target = gen_reg_rtx (mode);
2256                     convert_move (target, temp, 0);
2257                     return target;
2258                   }
2259                 else
2260                   return gen_lowpart (mode, temp);
2261               }
2262             else
2263               delete_insns_since (last);
2264           }
2265       }
2266
2267   /* These can be done a word at a time.  */
2268   if (unoptab == one_cmpl_optab
2269       && class == MODE_INT
2270       && GET_MODE_SIZE (mode) > UNITS_PER_WORD
2271       && unoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
2272     {
2273       int i;
2274       rtx insns;
2275
2276       if (target == 0 || target == op0)
2277         target = gen_reg_rtx (mode);
2278
2279       start_sequence ();
2280
2281       /* Do the actual arithmetic.  */
2282       for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
2283         {
2284           rtx target_piece = operand_subword (target, i, 1, mode);
2285           rtx x = expand_unop (word_mode, unoptab,
2286                                operand_subword_force (op0, i, mode),
2287                                target_piece, unsignedp);
2288
2289           if (target_piece != x)
2290             emit_move_insn (target_piece, x);
2291         }
2292
2293       insns = get_insns ();
2294       end_sequence ();
2295
2296       emit_no_conflict_block (insns, target, op0, NULL_RTX,
2297                               gen_rtx_fmt_e (unoptab->code, mode,
2298                                              copy_rtx (op0)));
2299       return target;
2300     }
2301
2302   /* Try negating floating point values by flipping the sign bit.  */
2303   if (unoptab->code == NEG && class == MODE_FLOAT
2304       && GET_MODE_BITSIZE (mode) <= 2 * HOST_BITS_PER_WIDE_INT)
2305     {
2306       const struct real_format *fmt = REAL_MODE_FORMAT (mode);
2307       enum machine_mode imode = int_mode_for_mode (mode);
2308       int bitpos = (fmt != 0) ? fmt->signbit : -1;
2309
2310       if (imode != BLKmode && bitpos >= 0 && fmt->has_signed_zero)
2311         {
2312           HOST_WIDE_INT hi, lo;
2313           rtx last = get_last_insn ();
2314
2315           /* Handle targets with different FP word orders.  */
2316           if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2317             {
2318               int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
2319               int word = nwords - (bitpos / BITS_PER_WORD) - 1;
2320               bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
2321             }
2322
2323           if (bitpos < HOST_BITS_PER_WIDE_INT)
2324             {
2325               hi = 0;
2326               lo = (HOST_WIDE_INT) 1 << bitpos;
2327             }
2328           else
2329             {
2330               hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
2331               lo = 0;
2332             }
2333           temp = expand_binop (imode, xor_optab,
2334                                gen_lowpart (imode, op0),
2335                                immed_double_const (lo, hi, imode),
2336                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
2337           if (temp != 0)
2338             {
2339               rtx insn;
2340               if (target == 0)
2341                 target = gen_reg_rtx (mode);
2342               temp = lowpart_subreg_maybe_copy (mode, temp, imode);
2343               insn = emit_move_insn (target, temp);
2344               set_unique_reg_note (insn, REG_EQUAL,
2345                                    gen_rtx_fmt_e (NEG, mode,
2346                                                   copy_rtx (op0)));
2347               return target;
2348             }
2349           delete_insns_since (last);
2350         }
2351     }
2352
2353   /* Try calculating parity (x) as popcount (x) % 2.  */
2354   if (unoptab == parity_optab)
2355     {
2356       temp = expand_parity (mode, op0, target);
2357       if (temp)
2358         return temp;
2359     }
2360
2361   /* If there is no negation pattern, try subtracting from zero.  */
2362   if (unoptab == neg_optab && class == MODE_INT)
2363     {
2364       temp = expand_binop (mode, sub_optab, CONST0_RTX (mode), op0,
2365                            target, unsignedp, OPTAB_DIRECT);
2366       if (temp)
2367         return temp;
2368     }
2369
2370  try_libcall:
2371   /* Now try a library call in this mode.  */
2372   if (unoptab->handlers[(int) mode].libfunc)
2373     {
2374       rtx insns;
2375       rtx value;
2376       enum machine_mode outmode = mode;
2377
2378       /* All of these functions return small values.  Thus we choose to
2379          have them return something that isn't a double-word.  */
2380       if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
2381           || unoptab == popcount_optab || unoptab == parity_optab)
2382         outmode
2383             = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node)));
2384
2385       start_sequence ();
2386
2387       /* Pass 1 for NO_QUEUE so we don't lose any increments
2388          if the libcall is cse'd or moved.  */
2389       value = emit_library_call_value (unoptab->handlers[(int) mode].libfunc,
2390                                        NULL_RTX, LCT_CONST, outmode,
2391                                        1, op0, mode);
2392       insns = get_insns ();
2393       end_sequence ();
2394
2395       target = gen_reg_rtx (outmode);
2396       emit_libcall_block (insns, target, value,
2397                           gen_rtx_fmt_e (unoptab->code, mode, op0));
2398
2399       return target;
2400     }
2401
2402   /* It can't be done in this mode.  Can we do it in a wider mode?  */
2403
2404   if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2405     {
2406       for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2407            wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2408         {
2409           if ((unoptab->handlers[(int) wider_mode].insn_code
2410                != CODE_FOR_nothing)
2411               || unoptab->handlers[(int) wider_mode].libfunc)
2412             {
2413               rtx xop0 = op0;
2414
2415               /* For certain operations, we need not actually extend
2416                  the narrow operand, as long as we will truncate the
2417                  results to the same narrowness.  */
2418
2419               xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2420                                     (unoptab == neg_optab
2421                                      || unoptab == one_cmpl_optab)
2422                                     && class == MODE_INT);
2423
2424               temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2425                                   unsignedp);
2426
2427               /* If we are generating clz using wider mode, adjust the
2428                  result.  */
2429               if (unoptab == clz_optab && temp != 0)
2430                 temp = expand_binop (wider_mode, sub_optab, temp,
2431                                      GEN_INT (GET_MODE_BITSIZE (wider_mode)
2432                                               - GET_MODE_BITSIZE (mode)),
2433                                      target, true, OPTAB_DIRECT);
2434
2435               if (temp)
2436                 {
2437                   if (class != MODE_INT)
2438                     {
2439                       if (target == 0)
2440                         target = gen_reg_rtx (mode);
2441                       convert_move (target, temp, 0);
2442                       return target;
2443                     }
2444                   else
2445                     return gen_lowpart (mode, temp);
2446                 }
2447               else
2448                 delete_insns_since (last);
2449             }
2450         }
2451     }
2452
2453   /* If there is no negate operation, try doing a subtract from zero.
2454      The US Software GOFAST library needs this.  FIXME: This is *wrong*
2455      for floating-point operations due to negative zeros!  */
2456   if (unoptab->code == NEG)
2457     {
2458       rtx temp;
2459       temp = expand_binop (mode,
2460                            unoptab == negv_optab ? subv_optab : sub_optab,
2461                            CONST0_RTX (mode), op0,
2462                            target, unsignedp, OPTAB_LIB_WIDEN);
2463       if (temp)
2464         return temp;
2465     }
2466
2467   return 0;
2468 }
2469 \f
2470 /* Emit code to compute the absolute value of OP0, with result to
2471    TARGET if convenient.  (TARGET may be 0.)  The return value says
2472    where the result actually is to be found.
2473
2474    MODE is the mode of the operand; the mode of the result is
2475    different but can be deduced from MODE.
2476
2477  */
2478
2479 rtx
2480 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
2481                    int result_unsignedp)
2482 {
2483   rtx temp;
2484
2485   if (! flag_trapv)
2486     result_unsignedp = 1;
2487
2488   /* First try to do it with a special abs instruction.  */
2489   temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
2490                       op0, target, 0);
2491   if (temp != 0)
2492     return temp;
2493
2494   /* For floating point modes, try clearing the sign bit.  */
2495   if (GET_MODE_CLASS (mode) == MODE_FLOAT
2496       && GET_MODE_BITSIZE (mode) <= 2 * HOST_BITS_PER_WIDE_INT)
2497     {
2498       const struct real_format *fmt = REAL_MODE_FORMAT (mode);
2499       enum machine_mode imode = int_mode_for_mode (mode);
2500       int bitpos = (fmt != 0) ? fmt->signbit : -1;
2501
2502       if (imode != BLKmode && bitpos >= 0)
2503         {
2504           HOST_WIDE_INT hi, lo;
2505           rtx last = get_last_insn ();
2506
2507           /* Handle targets with different FP word orders.  */
2508           if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2509             {
2510               int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
2511               int word = nwords - (bitpos / BITS_PER_WORD) - 1;
2512               bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
2513             }
2514
2515           if (bitpos < HOST_BITS_PER_WIDE_INT)
2516             {
2517               hi = 0;
2518               lo = (HOST_WIDE_INT) 1 << bitpos;
2519             }
2520           else
2521             {
2522               hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
2523               lo = 0;
2524             }
2525           temp = expand_binop (imode, and_optab,
2526                                gen_lowpart (imode, op0),
2527                                immed_double_const (~lo, ~hi, imode),
2528                                NULL_RTX, 1, OPTAB_LIB_WIDEN);
2529           if (temp != 0)
2530             {
2531               rtx insn;
2532               if (target == 0)
2533                 target = gen_reg_rtx (mode);
2534               temp = lowpart_subreg_maybe_copy (mode, temp, imode);
2535               insn = emit_move_insn (target, temp);
2536               set_unique_reg_note (insn, REG_EQUAL,
2537                                    gen_rtx_fmt_e (ABS, mode,
2538                                                   copy_rtx (op0)));
2539               return target;
2540             }
2541           delete_insns_since (last);
2542         }
2543     }
2544
2545   /* If we have a MAX insn, we can do this as MAX (x, -x).  */
2546   if (smax_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2547     {
2548       rtx last = get_last_insn ();
2549
2550       temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
2551       if (temp != 0)
2552         temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
2553                              OPTAB_WIDEN);
2554
2555       if (temp != 0)
2556         return temp;
2557
2558       delete_insns_since (last);
2559     }
2560
2561   /* If this machine has expensive jumps, we can do integer absolute
2562      value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
2563      where W is the width of MODE.  */
2564
2565   if (GET_MODE_CLASS (mode) == MODE_INT && BRANCH_COST >= 2)
2566     {
2567       rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
2568                                    size_int (GET_MODE_BITSIZE (mode) - 1),
2569                                    NULL_RTX, 0);
2570
2571       temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
2572                            OPTAB_LIB_WIDEN);
2573       if (temp != 0)
2574         temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
2575                              temp, extended, target, 0, OPTAB_LIB_WIDEN);
2576
2577       if (temp != 0)
2578         return temp;
2579     }
2580
2581   return NULL_RTX;
2582 }
2583
2584 rtx
2585 expand_abs (enum machine_mode mode, rtx op0, rtx target,
2586             int result_unsignedp, int safe)
2587 {
2588   rtx temp, op1;
2589
2590   if (! flag_trapv)
2591     result_unsignedp = 1;
2592
2593   temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
2594   if (temp != 0)
2595     return temp;
2596
2597   /* If that does not win, use conditional jump and negate.  */
2598
2599   /* It is safe to use the target if it is the same
2600      as the source if this is also a pseudo register */
2601   if (op0 == target && REG_P (op0)
2602       && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
2603     safe = 1;
2604
2605   op1 = gen_label_rtx ();
2606   if (target == 0 || ! safe
2607       || GET_MODE (target) != mode
2608       || (MEM_P (target) && MEM_VOLATILE_P (target))
2609       || (REG_P (target)
2610           && REGNO (target) < FIRST_PSEUDO_REGISTER))
2611     target = gen_reg_rtx (mode);
2612
2613   emit_move_insn (target, op0);
2614   NO_DEFER_POP;
2615
2616   /* If this mode is an integer too wide to compare properly,
2617      compare word by word.  Rely on CSE to optimize constant cases.  */
2618   if (GET_MODE_CLASS (mode) == MODE_INT
2619       && ! can_compare_p (GE, mode, ccp_jump))
2620     do_jump_by_parts_greater_rtx (mode, 0, target, const0_rtx,
2621                                   NULL_RTX, op1);
2622   else
2623     do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
2624                              NULL_RTX, NULL_RTX, op1);
2625
2626   op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
2627                      target, target, 0);
2628   if (op0 != target)
2629     emit_move_insn (target, op0);
2630   emit_label (op1);
2631   OK_DEFER_POP;
2632   return target;
2633 }
2634
2635 /* Expand the C99 copysign operation.  OP0 and OP1 must be the same 
2636    scalar floating point mode.  Return NULL if we do not know how to
2637    expand the operation inline.  */
2638
2639 rtx
2640 expand_copysign (rtx op0, rtx op1, rtx target)
2641 {
2642   enum machine_mode mode = GET_MODE (op0);
2643   const struct real_format *fmt;
2644   enum machine_mode imode;
2645   int bitpos;
2646   HOST_WIDE_INT hi, lo;
2647   rtx last, temp;
2648
2649   gcc_assert (SCALAR_FLOAT_MODE_P (mode));
2650   gcc_assert (GET_MODE (op1) == mode);
2651
2652   /* First try to do it with a special instruction.  */
2653   temp = expand_binop (mode, copysign_optab, op0, op1,
2654                        target, 0, OPTAB_DIRECT);
2655   if (temp)
2656     return temp;
2657
2658   /* Otherwise, use bit operations to move the sign from one to the other.  */
2659   if (GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT)
2660     return NULL_RTX;
2661
2662   imode = int_mode_for_mode (mode);
2663   if (imode == BLKmode)
2664     return NULL_RTX;
2665
2666   fmt = REAL_MODE_FORMAT (mode);
2667   bitpos = (fmt != 0) ? fmt->signbit : -1;
2668   if (bitpos < 0)
2669     return NULL_RTX;
2670
2671   last = get_last_insn ();
2672
2673   /* Handle targets with different FP word orders.  */
2674   if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2675     {
2676       int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
2677       int word = nwords - (bitpos / BITS_PER_WORD) - 1;
2678       bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
2679     }
2680
2681   if (bitpos < HOST_BITS_PER_WIDE_INT)
2682     {
2683       hi = 0;
2684       lo = (HOST_WIDE_INT) 1 << bitpos;
2685     }
2686   else
2687     {
2688       hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
2689       lo = 0;
2690     }
2691
2692   op0 = expand_binop (imode, and_optab,
2693                       gen_lowpart (imode, op0),
2694                       immed_double_const (~lo, ~hi, imode),
2695                       NULL_RTX, 1, OPTAB_LIB_WIDEN);
2696   op1 = expand_binop (imode, and_optab,
2697                       gen_lowpart (imode, op1),
2698                       immed_double_const (lo, hi, imode),
2699                       NULL_RTX, 1, OPTAB_LIB_WIDEN);
2700   if (op0 && op1)
2701     {
2702       target = expand_binop (imode, ior_optab, op0, op1, NULL,
2703                              1, OPTAB_LIB_WIDEN);
2704       if (target)
2705         {
2706           target = force_reg (imode, target);
2707           target = gen_lowpart (mode, target);
2708         }
2709     }
2710   else
2711     target = NULL_RTX;
2712
2713   if (!target)
2714     delete_insns_since (last);
2715
2716   return target;
2717 }
2718 \f
2719 /* Generate an instruction whose insn-code is INSN_CODE,
2720    with two operands: an output TARGET and an input OP0.
2721    TARGET *must* be nonzero, and the output is always stored there.
2722    CODE is an rtx code such that (CODE OP0) is an rtx that describes
2723    the value that is stored into TARGET.  */
2724
2725 void
2726 emit_unop_insn (int icode, rtx target, rtx op0, enum rtx_code code)
2727 {
2728   rtx temp;
2729   enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2730   rtx pat;
2731
2732   temp = target;
2733
2734   /* Sign and zero extension from memory is often done specially on
2735      RISC machines, so forcing into a register here can pessimize
2736      code.  */
2737   if (flag_force_mem && code != SIGN_EXTEND && code != ZERO_EXTEND)
2738     op0 = force_not_mem (op0);
2739
2740   /* Now, if insn does not accept our operands, put them into pseudos.  */
2741
2742   if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
2743     op0 = copy_to_mode_reg (mode0, op0);
2744
2745   if (! (*insn_data[icode].operand[0].predicate) (temp, GET_MODE (temp))
2746       || (flag_force_mem && MEM_P (temp)))
2747     temp = gen_reg_rtx (GET_MODE (temp));
2748
2749   pat = GEN_FCN (icode) (temp, op0);
2750
2751   if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
2752     add_equal_note (pat, temp, code, op0, NULL_RTX);
2753
2754   emit_insn (pat);
2755
2756   if (temp != target)
2757     emit_move_insn (target, temp);
2758 }
2759 \f
2760 /* Emit code to perform a series of operations on a multi-word quantity, one
2761    word at a time.
2762
2763    Such a block is preceded by a CLOBBER of the output, consists of multiple
2764    insns, each setting one word of the output, and followed by a SET copying
2765    the output to itself.
2766
2767    Each of the insns setting words of the output receives a REG_NO_CONFLICT
2768    note indicating that it doesn't conflict with the (also multi-word)
2769    inputs.  The entire block is surrounded by REG_LIBCALL and REG_RETVAL
2770    notes.
2771
2772    INSNS is a block of code generated to perform the operation, not including
2773    the CLOBBER and final copy.  All insns that compute intermediate values
2774    are first emitted, followed by the block as described above.
2775
2776    TARGET, OP0, and OP1 are the output and inputs of the operations,
2777    respectively.  OP1 may be zero for a unary operation.
2778
2779    EQUIV, if nonzero, is an expression to be placed into a REG_EQUAL note
2780    on the last insn.
2781
2782    If TARGET is not a register, INSNS is simply emitted with no special
2783    processing.  Likewise if anything in INSNS is not an INSN or if
2784    there is a libcall block inside INSNS.
2785
2786    The final insn emitted is returned.  */
2787
2788 rtx
2789 emit_no_conflict_block (rtx insns, rtx target, rtx op0, rtx op1, rtx equiv)
2790 {
2791   rtx prev, next, first, last, insn;
2792
2793   if (!REG_P (target) || reload_in_progress)
2794     return emit_insn (insns);
2795   else
2796     for (insn = insns; insn; insn = NEXT_INSN (insn))
2797       if (!NONJUMP_INSN_P (insn)
2798           || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
2799         return emit_insn (insns);
2800
2801   /* First emit all insns that do not store into words of the output and remove
2802      these from the list.  */
2803   for (insn = insns; insn; insn = next)
2804     {
2805       rtx set = 0, note;
2806       int i;
2807
2808       next = NEXT_INSN (insn);
2809
2810       /* Some ports (cris) create a libcall regions at their own.  We must
2811          avoid any potential nesting of LIBCALLs.  */
2812       if ((note = find_reg_note (insn, REG_LIBCALL, NULL)) != NULL)
2813         remove_note (insn, note);
2814       if ((note = find_reg_note (insn, REG_RETVAL, NULL)) != NULL)
2815         remove_note (insn, note);
2816
2817       if (GET_CODE (PATTERN (insn)) == SET || GET_CODE (PATTERN (insn)) == USE
2818           || GET_CODE (PATTERN (insn)) == CLOBBER)
2819         set = PATTERN (insn);
2820       else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2821         {
2822           for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2823             if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
2824               {
2825                 set = XVECEXP (PATTERN (insn), 0, i);
2826                 break;
2827               }
2828         }
2829
2830       if (set == 0)
2831         abort ();
2832
2833       if (! reg_overlap_mentioned_p (target, SET_DEST (set)))
2834         {
2835           if (PREV_INSN (insn))
2836             NEXT_INSN (PREV_INSN (insn)) = next;
2837           else
2838             insns = next;
2839
2840           if (next)
2841             PREV_INSN (next) = PREV_INSN (insn);
2842
2843           add_insn (insn);
2844         }
2845     }
2846
2847   prev = get_last_insn ();
2848
2849   /* Now write the CLOBBER of the output, followed by the setting of each
2850      of the words, followed by the final copy.  */
2851   if (target != op0 && target != op1)
2852     emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
2853
2854   for (insn = insns; insn; insn = next)
2855     {
2856       next = NEXT_INSN (insn);
2857       add_insn (insn);
2858
2859       if (op1 && REG_P (op1))
2860         REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_NO_CONFLICT, op1,
2861                                               REG_NOTES (insn));
2862
2863       if (op0 && REG_P (op0))
2864         REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_NO_CONFLICT, op0,
2865                                               REG_NOTES (insn));
2866     }
2867
2868   if (mov_optab->handlers[(int) GET_MODE (target)].insn_code
2869       != CODE_FOR_nothing)
2870     {
2871       last = emit_move_insn (target, target);
2872       if (equiv)
2873         set_unique_reg_note (last, REG_EQUAL, equiv);
2874     }
2875   else
2876     {
2877       last = get_last_insn ();
2878
2879       /* Remove any existing REG_EQUAL note from "last", or else it will
2880          be mistaken for a note referring to the full contents of the
2881          alleged libcall value when found together with the REG_RETVAL
2882          note added below.  An existing note can come from an insn
2883          expansion at "last".  */
2884       remove_note (last, find_reg_note (last, REG_EQUAL, NULL_RTX));
2885     }
2886
2887   if (prev == 0)
2888     first = get_insns ();
2889   else
2890     first = NEXT_INSN (prev);
2891
2892   /* Encapsulate the block so it gets manipulated as a unit.  */
2893   REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
2894                                          REG_NOTES (first));
2895   REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
2896
2897   return last;
2898 }
2899 \f
2900 /* Emit code to make a call to a constant function or a library call.
2901
2902    INSNS is a list containing all insns emitted in the call.
2903    These insns leave the result in RESULT.  Our block is to copy RESULT
2904    to TARGET, which is logically equivalent to EQUIV.
2905
2906    We first emit any insns that set a pseudo on the assumption that these are
2907    loading constants into registers; doing so allows them to be safely cse'ed
2908    between blocks.  Then we emit all the other insns in the block, followed by
2909    an insn to move RESULT to TARGET.  This last insn will have a REQ_EQUAL
2910    note with an operand of EQUIV.
2911
2912    Moving assignments to pseudos outside of the block is done to improve
2913    the generated code, but is not required to generate correct code,
2914    hence being unable to move an assignment is not grounds for not making
2915    a libcall block.  There are two reasons why it is safe to leave these
2916    insns inside the block: First, we know that these pseudos cannot be
2917    used in generated RTL outside the block since they are created for
2918    temporary purposes within the block.  Second, CSE will not record the
2919    values of anything set inside a libcall block, so we know they must
2920    be dead at the end of the block.
2921
2922    Except for the first group of insns (the ones setting pseudos), the
2923    block is delimited by REG_RETVAL and REG_LIBCALL notes.  */
2924
2925 void
2926 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
2927 {
2928   rtx final_dest = target;
2929   rtx prev, next, first, last, insn;
2930
2931   /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
2932      into a MEM later.  Protect the libcall block from this change.  */
2933   if (! REG_P (target) || REG_USERVAR_P (target))
2934     target = gen_reg_rtx (GET_MODE (target));
2935
2936   /* If we're using non-call exceptions, a libcall corresponding to an
2937      operation that may trap may also trap.  */
2938   if (flag_non_call_exceptions && may_trap_p (equiv))
2939     {
2940       for (insn = insns; insn; insn = NEXT_INSN (insn))
2941         if (CALL_P (insn))
2942           {
2943             rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2944
2945             if (note != 0 && INTVAL (XEXP (note, 0)) <= 0)
2946               remove_note (insn, note);
2947           }
2948     }
2949   else
2950   /* look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
2951      reg note to indicate that this call cannot throw or execute a nonlocal
2952      goto (unless there is already a REG_EH_REGION note, in which case
2953      we update it).  */
2954     for (insn = insns; insn; insn = NEXT_INSN (insn))
2955       if (CALL_P (insn))
2956         {
2957           rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2958
2959           if (note != 0)
2960             XEXP (note, 0) = constm1_rtx;
2961           else
2962             REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, constm1_rtx,
2963                                                   REG_NOTES (insn));
2964         }
2965
2966   /* First emit all insns that set pseudos.  Remove them from the list as
2967      we go.  Avoid insns that set pseudos which were referenced in previous
2968      insns.  These can be generated by move_by_pieces, for example,
2969      to update an address.  Similarly, avoid insns that reference things
2970      set in previous insns.  */
2971
2972   for (insn = insns; insn; insn = next)
2973     {
2974       rtx set = single_set (insn);
2975       rtx note;
2976
2977       /* Some ports (cris) create a libcall regions at their own.  We must
2978          avoid any potential nesting of LIBCALLs.  */
2979       if ((note = find_reg_note (insn, REG_LIBCALL, NULL)) != NULL)
2980         remove_note (insn, note);
2981       if ((note = find_reg_note (insn, REG_RETVAL, NULL)) != NULL)
2982         remove_note (insn, note);
2983
2984       next = NEXT_INSN (insn);
2985
2986       if (set != 0 && REG_P (SET_DEST (set))
2987           && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
2988           && (insn == insns
2989               || ((! INSN_P(insns)
2990                    || ! reg_mentioned_p (SET_DEST (set), PATTERN (insns)))
2991                   && ! reg_used_between_p (SET_DEST (set), insns, insn)
2992                   && ! modified_in_p (SET_SRC (set), insns)
2993                   && ! modified_between_p (SET_SRC (set), insns, insn))))
2994         {
2995           if (PREV_INSN (insn))
2996             NEXT_INSN (PREV_INSN (insn)) = next;
2997           else
2998             insns = next;
2999
3000           if (next)
3001             PREV_INSN (next) = PREV_INSN (insn);
3002
3003           add_insn (insn);
3004         }
3005
3006       /* Some ports use a loop to copy large arguments onto the stack.
3007          Don't move anything outside such a loop.  */
3008       if (LABEL_P (insn))
3009         break;
3010     }
3011
3012   prev = get_last_insn ();
3013
3014   /* Write the remaining insns followed by the final copy.  */
3015
3016   for (insn = insns; insn; insn = next)
3017     {
3018       next = NEXT_INSN (insn);
3019
3020       add_insn (insn);
3021     }
3022
3023   last = emit_move_insn (target, result);
3024   if (mov_optab->handlers[(int) GET_MODE (target)].insn_code
3025       != CODE_FOR_nothing)
3026     set_unique_reg_note (last, REG_EQUAL, copy_rtx (equiv));
3027   else
3028     {
3029       /* Remove any existing REG_EQUAL note from "last", or else it will
3030          be mistaken for a note referring to the full contents of the
3031          libcall value when found together with the REG_RETVAL note added
3032          below.  An existing note can come from an insn expansion at
3033          "last".  */
3034       remove_note (last, find_reg_note (last, REG_EQUAL, NULL_RTX));
3035     }
3036
3037   if (final_dest != target)
3038     emit_move_insn (final_dest, target);
3039
3040   if (prev == 0)
3041     first = get_insns ();
3042   else
3043     first = NEXT_INSN (prev);
3044
3045   /* Encapsulate the block so it gets manipulated as a unit.  */
3046   if (!flag_non_call_exceptions || !may_trap_p (equiv))
3047     {
3048       /* We can't attach the REG_LIBCALL and REG_RETVAL notes
3049          when the encapsulated region would not be in one basic block,
3050          i.e. when there is a control_flow_insn_p insn between FIRST and LAST.
3051        */
3052       bool attach_libcall_retval_notes = true;
3053       next = NEXT_INSN (last);
3054       for (insn = first; insn != next; insn = NEXT_INSN (insn))
3055         if (control_flow_insn_p (insn))
3056           {
3057             attach_libcall_retval_notes = false;
3058             break;
3059           }
3060
3061       if (attach_libcall_retval_notes)
3062         {
3063           REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
3064                                                  REG_NOTES (first));
3065           REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first,
3066                                                 REG_NOTES (last));
3067         }
3068     }
3069 }
3070 \f
3071 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3072    PURPOSE describes how this comparison will be used.  CODE is the rtx
3073    comparison code we will be using.
3074
3075    ??? Actually, CODE is slightly weaker than that.  A target is still
3076    required to implement all of the normal bcc operations, but not
3077    required to implement all (or any) of the unordered bcc operations.  */
3078
3079 int
3080 can_compare_p (enum rtx_code code, enum machine_mode mode,
3081                enum can_compare_purpose purpose)
3082 {
3083   do
3084     {
3085       if (cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3086         {
3087           if (purpose == ccp_jump)
3088             return bcc_gen_fctn[(int) code] != NULL;
3089           else if (purpose == ccp_store_flag)
3090             return setcc_gen_code[(int) code] != CODE_FOR_nothing;
3091           else
3092             /* There's only one cmov entry point, and it's allowed to fail.  */
3093             return 1;
3094         }
3095       if (purpose == ccp_jump
3096           && cbranch_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3097         return 1;
3098       if (purpose == ccp_cmov
3099           && cmov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3100         return 1;
3101       if (purpose == ccp_store_flag
3102           && cstore_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3103         return 1;
3104       mode = GET_MODE_WIDER_MODE (mode);
3105     }
3106   while (mode != VOIDmode);
3107
3108   return 0;
3109 }
3110
3111 /* This function is called when we are going to emit a compare instruction that
3112    compares the values found in *PX and *PY, using the rtl operator COMPARISON.
3113
3114    *PMODE is the mode of the inputs (in case they are const_int).
3115    *PUNSIGNEDP nonzero says that the operands are unsigned;
3116    this matters if they need to be widened.
3117
3118    If they have mode BLKmode, then SIZE specifies the size of both operands.
3119
3120    This function performs all the setup necessary so that the caller only has
3121    to emit a single comparison insn.  This setup can involve doing a BLKmode
3122    comparison or emitting a library call to perform the comparison if no insn
3123    is available to handle it.
3124    The values which are passed in through pointers can be modified; the caller
3125    should perform the comparison on the modified values.  */
3126
3127 static void
3128 prepare_cmp_insn (rtx *px, rtx *py, enum rtx_code *pcomparison, rtx size,
3129                   enum machine_mode *pmode, int *punsignedp,
3130                   enum can_compare_purpose purpose)
3131 {
3132   enum machine_mode mode = *pmode;
3133   rtx x = *px, y = *py;
3134   int unsignedp = *punsignedp;
3135   enum mode_class class;
3136
3137   class = GET_MODE_CLASS (mode);
3138
3139   /* They could both be VOIDmode if both args are immediate constants,
3140      but we should fold that at an earlier stage.
3141      With no special code here, this will call abort,
3142      reminding the programmer to implement such folding.  */
3143
3144   if (mode != BLKmode && flag_force_mem)
3145     {
3146       /* Load duplicate non-volatile operands once.  */
3147       if (rtx_equal_p (x, y) && ! volatile_refs_p (x))
3148         {
3149           x = force_not_mem (x);
3150           y = x;
3151         }
3152       else
3153         {
3154           x = force_not_mem (x);
3155           y = force_not_mem (y);
3156         }
3157     }
3158
3159   /* If we are inside an appropriately-short loop and we are optimizing,
3160      force expensive constants into a register.  */
3161   if (CONSTANT_P (x) && optimize
3162       && rtx_cost (x, COMPARE) > COSTS_N_INSNS (1))
3163     x = force_reg (mode, x);
3164
3165   if (CONSTANT_P (y) && optimize
3166       && rtx_cost (y, COMPARE) > COSTS_N_INSNS (1))
3167     y = force_reg (mode, y);
3168
3169 #ifdef HAVE_cc0
3170   /* Abort if we have a non-canonical comparison.  The RTL documentation
3171      states that canonical comparisons are required only for targets which
3172      have cc0.  */
3173   if (CONSTANT_P (x) && ! CONSTANT_P (y))
3174     abort ();
3175 #endif
3176
3177   /* Don't let both operands fail to indicate the mode.  */
3178   if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
3179     x = force_reg (mode, x);
3180
3181   /* Handle all BLKmode compares.  */
3182
3183   if (mode == BLKmode)
3184     {
3185       enum machine_mode cmp_mode, result_mode;
3186       enum insn_code cmp_code;
3187       tree length_type;
3188       rtx libfunc;
3189       rtx result;
3190       rtx opalign
3191         = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
3192
3193       if (size == 0)
3194         abort ();
3195
3196       /* Try to use a memory block compare insn - either cmpstr
3197          or cmpmem will do.  */
3198       for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3199            cmp_mode != VOIDmode;
3200            cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
3201         {
3202           cmp_code = cmpmem_optab[cmp_mode];
3203           if (cmp_code == CODE_FOR_nothing)
3204             cmp_code = cmpstr_optab[cmp_mode];
3205           if (cmp_code == CODE_FOR_nothing)
3206             continue;
3207
3208           /* Must make sure the size fits the insn's mode.  */
3209           if ((GET_CODE (size) == CONST_INT
3210                && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
3211               || (GET_MODE_BITSIZE (GET_MODE (size))
3212                   > GET_MODE_BITSIZE (cmp_mode)))
3213             continue;
3214
3215           result_mode = insn_data[cmp_code].operand[0].mode;
3216           result = gen_reg_rtx (result_mode);
3217           size = convert_to_mode (cmp_mode, size, 1);
3218           emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
3219
3220           *px = result;
3221           *py = const0_rtx;
3222           *pmode = result_mode;
3223           return;
3224         }
3225
3226       /* Otherwise call a library function, memcmp.  */
3227       libfunc = memcmp_libfunc;
3228       length_type = sizetype;
3229       result_mode = TYPE_MODE (integer_type_node);
3230       cmp_mode = TYPE_MODE (length_type);
3231       size = convert_to_mode (TYPE_MODE (length_type), size,
3232                               TYPE_UNSIGNED (length_type));
3233
3234       result = emit_library_call_value (libfunc, 0, LCT_PURE_MAKE_BLOCK,
3235                                         result_mode, 3,
3236                                         XEXP (x, 0), Pmode,
3237                                         XEXP (y, 0), Pmode,
3238                                         size, cmp_mode);
3239       *px = result;
3240       *py = const0_rtx;
3241       *pmode = result_mode;
3242       return;
3243     }
3244
3245   /* Don't allow operands to the compare to trap, as that can put the
3246      compare and branch in different basic blocks.  */
3247   if (flag_non_call_exceptions)
3248     {
3249       if (may_trap_p (x))
3250         x = force_reg (mode, x);
3251       if (may_trap_p (y))
3252         y = force_reg (mode, y);
3253     }
3254
3255   *px = x;
3256   *py = y;
3257   if (can_compare_p (*pcomparison, mode, purpose))
3258     return;
3259
3260   /* Handle a lib call just for the mode we are using.  */
3261
3262   if (cmp_optab->handlers[(int) mode].libfunc && class != MODE_FLOAT)
3263     {
3264       rtx libfunc = cmp_optab->handlers[(int) mode].libfunc;
3265       rtx result;
3266
3267       /* If we want unsigned, and this mode has a distinct unsigned
3268          comparison routine, use that.  */
3269       if (unsignedp && ucmp_optab->handlers[(int) mode].libfunc)
3270         libfunc = ucmp_optab->handlers[(int) mode].libfunc;
3271
3272       result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST_MAKE_BLOCK,
3273                                         word_mode, 2, x, mode, y, mode);
3274
3275       *px = result;
3276       *pmode = word_mode;
3277       if (TARGET_LIB_INT_CMP_BIASED)
3278         /* Integer comparison returns a result that must be compared
3279            against 1, so that even if we do an unsigned compare
3280            afterward, there is still a value that can represent the
3281            result "less than".  */
3282         *py = const1_rtx;
3283       else
3284         {
3285           *py = const0_rtx;
3286           *punsignedp = 1;
3287         }
3288       return;
3289     }
3290
3291   if (class == MODE_FLOAT)
3292     prepare_float_lib_cmp (px, py, pcomparison, pmode, punsignedp);
3293
3294   else
3295     abort ();
3296 }
3297
3298 /* Before emitting an insn with code ICODE, make sure that X, which is going
3299    to be used for operand OPNUM of the insn, is converted from mode MODE to
3300    WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
3301    that it is accepted by the operand predicate.  Return the new value.  */
3302
3303 static rtx
3304 prepare_operand (int icode, rtx x, int opnum, enum machine_mode mode,
3305                  enum machine_mode wider_mode, int unsignedp)
3306 {
3307   if (mode != wider_mode)
3308     x = convert_modes (wider_mode, mode, x, unsignedp);
3309
3310   if (! (*insn_data[icode].operand[opnum].predicate)
3311       (x, insn_data[icode].operand[opnum].mode))
3312     {
3313       if (no_new_pseudos)
3314         return NULL_RTX;
3315       x = copy_to_mode_reg (insn_data[icode].operand[opnum].mode, x);
3316     }
3317
3318   return x;
3319 }
3320
3321 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
3322    we can do the comparison.
3323    The arguments are the same as for emit_cmp_and_jump_insns; but LABEL may
3324    be NULL_RTX which indicates that only a comparison is to be generated.  */
3325
3326 static void
3327 emit_cmp_and_jump_insn_1 (rtx x, rtx y, enum machine_mode mode,
3328                           enum rtx_code comparison, int unsignedp, rtx label)
3329 {
3330   rtx test = gen_rtx_fmt_ee (comparison, mode, x, y);
3331   enum mode_class class = GET_MODE_CLASS (mode);
3332   enum machine_mode wider_mode = mode;
3333
3334   /* Try combined insns first.  */
3335   do
3336     {
3337       enum insn_code icode;
3338       PUT_MODE (test, wider_mode);
3339
3340       if (label)
3341         {
3342           icode = cbranch_optab->handlers[(int) wider_mode].insn_code;
3343
3344           if (icode != CODE_FOR_nothing
3345               && (*insn_data[icode].operand[0].predicate) (test, wider_mode))
3346             {
3347               x = prepare_operand (icode, x, 1, mode, wider_mode, unsignedp);
3348               y = prepare_operand (icode, y, 2, mode, wider_mode, unsignedp);
3349               emit_jump_insn (GEN_FCN (icode) (test, x, y, label));
3350               return;
3351             }
3352         }
3353
3354       /* Handle some compares against zero.  */
3355       icode = (int) tst_optab->handlers[(int) wider_mode].insn_code;
3356       if (y == CONST0_RTX (mode) && icode != CODE_FOR_nothing)
3357         {
3358           x = prepare_operand (icode, x, 0, mode, wider_mode, unsignedp);
3359           emit_insn (GEN_FCN (icode) (x));
3360           if (label)
3361             emit_jump_insn ((*bcc_gen_fctn[(int) comparison]) (label));
3362           return;
3363         }
3364
3365       /* Handle compares for which there is a directly suitable insn.  */
3366
3367       icode = (int) cmp_optab->handlers[(int) wider_mode].insn_code;
3368       if (icode != CODE_FOR_nothing)
3369         {
3370           x = prepare_operand (icode, x, 0, mode, wider_mode, unsignedp);
3371           y = prepare_operand (icode, y, 1, mode, wider_mode, unsignedp);
3372           emit_insn (GEN_FCN (icode) (x, y));
3373           if (label)
3374             emit_jump_insn ((*bcc_gen_fctn[(int) comparison]) (label));
3375           return;
3376         }
3377
3378       if (class != MODE_INT && class != MODE_FLOAT
3379           && class != MODE_COMPLEX_FLOAT)
3380         break;
3381
3382       wider_mode = GET_MODE_WIDER_MODE (wider_mode);
3383     }
3384   while (wider_mode != VOIDmode);
3385
3386   abort ();
3387 }
3388
3389 /* Generate code to compare X with Y so that the condition codes are
3390    set and to jump to LABEL if the condition is true.  If X is a
3391    constant and Y is not a constant, then the comparison is swapped to
3392    ensure that the comparison RTL has the canonical form.
3393
3394    UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
3395    need to be widened by emit_cmp_insn.  UNSIGNEDP is also used to select
3396    the proper branch condition code.
3397
3398    If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
3399
3400    MODE is the mode of the inputs (in case they are const_int).
3401
3402    COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).  It will
3403    be passed unchanged to emit_cmp_insn, then potentially converted into an
3404    unsigned variant based on UNSIGNEDP to select a proper jump instruction.  */
3405
3406 void
3407 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
3408                          enum machine_mode mode, int unsignedp, rtx label)
3409 {
3410   rtx op0 = x, op1 = y;
3411
3412   /* Swap operands and condition to ensure canonical RTL.  */
3413   if (swap_commutative_operands_p (x, y))
3414     {
3415       /* If we're not emitting a branch, this means some caller
3416          is out of sync.  */
3417       if (! label)
3418         abort ();
3419
3420       op0 = y, op1 = x;
3421       comparison = swap_condition (comparison);
3422     }
3423
3424 #ifdef HAVE_cc0
3425   /* If OP0 is still a constant, then both X and Y must be constants.  Force
3426      X into a register to avoid aborting in emit_cmp_insn due to non-canonical
3427      RTL.  */
3428   if (CONSTANT_P (op0))
3429     op0 = force_reg (mode, op0);
3430 #endif
3431
3432   if (unsignedp)
3433     comparison = unsigned_condition (comparison);
3434
3435   prepare_cmp_insn (&op0, &op1, &comparison, size, &mode, &unsignedp,
3436                     ccp_jump);
3437   emit_cmp_and_jump_insn_1 (op0, op1, mode, comparison, unsignedp, label);
3438 }
3439
3440 /* Like emit_cmp_and_jump_insns, but generate only the comparison.  */
3441
3442 void
3443 emit_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
3444                enum machine_mode mode, int unsignedp)
3445 {
3446   emit_cmp_and_jump_insns (x, y, comparison, size, mode, unsignedp, 0);
3447 }
3448 \f
3449 /* Emit a library call comparison between floating point X and Y.
3450    COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).  */
3451
3452 static void
3453 prepare_float_lib_cmp (rtx *px, rtx *py, enum rtx_code *pcomparison,
3454                        enum machine_mode *pmode, int *punsignedp)
3455 {
3456   enum rtx_code comparison = *pcomparison;
3457   enum rtx_code swapped = swap_condition (comparison);
3458   enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
3459   rtx x = *px;
3460   rtx y = *py;
3461   enum machine_mode orig_mode = GET_MODE (x);
3462   enum machine_mode mode;
3463   rtx value, target, insns, equiv;
3464   rtx libfunc = 0;
3465   bool reversed_p = false;
3466
3467   for (mode = orig_mode; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3468     {
3469       if ((libfunc = code_to_optab[comparison]->handlers[mode].libfunc))
3470         break;
3471
3472       if ((libfunc = code_to_optab[swapped]->handlers[mode].libfunc))
3473         {
3474           rtx tmp;
3475           tmp = x; x = y; y = tmp;
3476           comparison = swapped;
3477           break;
3478         }
3479
3480       if ((libfunc = code_to_optab[reversed]->handlers[mode].libfunc)
3481           && FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, reversed))
3482         {
3483           comparison = reversed;
3484           reversed_p = true;
3485           break;
3486         }
3487     }
3488
3489   if (mode == VOIDmode)
3490     abort ();
3491
3492   if (mode != orig_mode)
3493     {
3494       x = convert_to_mode (mode, x, 0);
3495       y = convert_to_mode (mode, y, 0);
3496     }
3497
3498   /* Attach a REG_EQUAL note describing the semantics of the libcall to
3499      the RTL.  The allows the RTL optimizers to delete the libcall if the
3500      condition can be determined at compile-time.  */
3501   if (comparison == UNORDERED)
3502     {
3503       rtx temp = simplify_gen_relational (NE, word_mode, mode, x, x);
3504       equiv = simplify_gen_relational (NE, word_mode, mode, y, y);
3505       equiv = simplify_gen_ternary (IF_THEN_ELSE, word_mode, word_mode,
3506                                     temp, const_true_rtx, equiv);
3507     }
3508   else
3509     {
3510       equiv = simplify_gen_relational (comparison, word_mode, mode, x, y);
3511       if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
3512         {
3513           rtx true_rtx, false_rtx;
3514
3515           switch (comparison)
3516             {
3517             case EQ:
3518               true_rtx = const0_rtx;
3519               false_rtx = const_true_rtx;
3520               break;
3521
3522             case NE:
3523               true_rtx = const_true_rtx;
3524               false_rtx = const0_rtx;
3525               break;
3526
3527             case GT:
3528               true_rtx = const1_rtx;
3529               false_rtx = const0_rtx;
3530               break;
3531
3532             case GE:
3533               true_rtx = const0_rtx;
3534               false_rtx = constm1_rtx;
3535               break;
3536
3537             case LT:
3538               true_rtx = constm1_rtx;
3539               false_rtx = const0_rtx;
3540               break;
3541
3542             case LE:
3543               true_rtx = const0_rtx;
3544               false_rtx = const1_rtx;
3545               break;
3546
3547             default:
3548               abort ();
3549             }
3550           equiv = simplify_gen_ternary (IF_THEN_ELSE, word_mode, word_mode,
3551                                         equiv, true_rtx, false_rtx);
3552         }
3553     }
3554
3555   start_sequence ();
3556   value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
3557                                    word_mode, 2, x, mode, y, mode);
3558   insns = get_insns ();
3559   end_sequence ();
3560
3561   target = gen_reg_rtx (word_mode);
3562   emit_libcall_block (insns, target, value, equiv);
3563
3564   if (comparison == UNORDERED
3565       || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
3566     comparison = reversed_p ? EQ : NE;
3567
3568   *px = target;
3569   *py = const0_rtx;
3570   *pmode = word_mode;
3571   *pcomparison = comparison;
3572   *punsignedp = 0;
3573 }
3574 \f
3575 /* Generate code to indirectly jump to a location given in the rtx LOC.  */
3576
3577 void
3578 emit_indirect_jump (rtx loc)
3579 {
3580   if (! ((*insn_data[(int) CODE_FOR_indirect_jump].operand[0].predicate)
3581          (loc, Pmode)))
3582     loc = copy_to_mode_reg (Pmode, loc);
3583
3584   emit_jump_insn (gen_indirect_jump (loc));
3585   emit_barrier ();
3586 }
3587 \f
3588 #ifdef HAVE_conditional_move
3589
3590 /* Emit a conditional move instruction if the machine supports one for that
3591    condition and machine mode.
3592
3593    OP0 and OP1 are the operands that should be compared using CODE.  CMODE is
3594    the mode to use should they be constants.  If it is VOIDmode, they cannot
3595    both be constants.
3596
3597    OP2 should be stored in TARGET if the comparison is true, otherwise OP3
3598    should be stored there.  MODE is the mode to use should they be constants.
3599    If it is VOIDmode, they cannot both be constants.
3600
3601    The result is either TARGET (perhaps modified) or NULL_RTX if the operation
3602    is not supported.  */
3603
3604 rtx
3605 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
3606                        enum machine_mode cmode, rtx op2, rtx op3,
3607                        enum machine_mode mode, int unsignedp)
3608 {
3609   rtx tem, subtarget, comparison, insn;
3610   enum insn_code icode;
3611   enum rtx_code reversed;
3612
3613   /* If one operand is constant, make it the second one.  Only do this
3614      if the other operand is not constant as well.  */
3615
3616   if (swap_commutative_operands_p (op0, op1))
3617     {
3618       tem = op0;
3619       op0 = op1;
3620       op1 = tem;
3621       code = swap_condition (code);
3622     }
3623
3624   /* get_condition will prefer to generate LT and GT even if the old
3625      comparison was against zero, so undo that canonicalization here since
3626      comparisons against zero are cheaper.  */
3627   if (code == LT && op1 == const1_rtx)
3628     code = LE, op1 = const0_rtx;
3629   else if (code == GT && op1 == constm1_rtx)
3630     code = GE, op1 = const0_rtx;
3631
3632   if (cmode == VOIDmode)
3633     cmode = GET_MODE (op0);
3634
3635   if (swap_commutative_operands_p (op2, op3)
3636       && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
3637           != UNKNOWN))
3638     {
3639       tem = op2;
3640       op2 = op3;
3641       op3 = tem;
3642       code = reversed;
3643     }
3644
3645   if (mode == VOIDmode)
3646     mode = GET_MODE (op2);
3647
3648   icode = movcc_gen_code[mode];
3649
3650   if (icode == CODE_FOR_nothing)
3651     return 0;
3652
3653   if (flag_force_mem)
3654     {
3655       op2 = force_not_mem (op2);
3656       op3 = force_not_mem (op3);
3657     }
3658
3659   if (!target)
3660     target = gen_reg_rtx (mode);
3661
3662   subtarget = target;
3663
3664   /* If the insn doesn't accept these operands, put them in pseudos.  */
3665
3666   if (! (*insn_data[icode].operand[0].predicate)
3667       (subtarget, insn_data[icode].operand[0].mode))
3668     subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
3669
3670   if (! (*insn_data[icode].operand[2].predicate)
3671       (op2, insn_data[icode].operand[2].mode))
3672     op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
3673
3674   if (! (*insn_data[icode].operand[3].predicate)
3675       (op3, insn_data[icode].operand[3].mode))
3676     op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
3677
3678   /* Everything should now be in the suitable form, so emit the compare insn
3679      and then the conditional move.  */
3680
3681   comparison
3682     = compare_from_rtx (op0, op1, code, unsignedp, cmode, NULL_RTX);
3683
3684   /* ??? Watch for const0_rtx (nop) and const_true_rtx (unconditional)?  */
3685   /* We can get const0_rtx or const_true_rtx in some circumstances.  Just
3686      return NULL and let the caller figure out how best to deal with this
3687      situation.  */
3688   if (GET_CODE (comparison) != code)
3689     return NULL_RTX;
3690
3691   insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
3692
3693   /* If that failed, then give up.  */
3694   if (insn == 0)
3695     return 0;
3696
3697   emit_insn (insn);
3698
3699   if (subtarget != target)
3700     convert_move (target, subtarget, 0);
3701
3702   return target;
3703 }
3704
3705 /* Return nonzero if a conditional move of mode MODE is supported.
3706
3707    This function is for combine so it can tell whether an insn that looks
3708    like a conditional move is actually supported by the hardware.  If we
3709    guess wrong we lose a bit on optimization, but that's it.  */
3710 /* ??? sparc64 supports conditionally moving integers values based on fp
3711    comparisons, and vice versa.  How do we handle them?  */
3712
3713 int
3714 can_conditionally_move_p (enum machine_mode mode)
3715 {
3716   if (movcc_gen_code[mode] != CODE_FOR_nothing)
3717     return 1;
3718
3719   return 0;
3720 }
3721
3722 #endif /* HAVE_conditional_move */
3723
3724 /* Emit a conditional addition instruction if the machine supports one for that
3725    condition and machine mode.
3726
3727    OP0 and OP1 are the operands that should be compared using CODE.  CMODE is
3728    the mode to use should they be constants.  If it is VOIDmode, they cannot
3729    both be constants.
3730
3731    OP2 should be stored in TARGET if the comparison is true, otherwise OP2+OP3
3732    should be stored there.  MODE is the mode to use should they be constants.
3733    If it is VOIDmode, they cannot both be constants.
3734
3735    The result is either TARGET (perhaps modified) or NULL_RTX if the operation
3736    is not supported.  */
3737
3738 rtx
3739 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
3740                       enum machine_mode cmode, rtx op2, rtx op3,
3741                       enum machine_mode mode, int unsignedp)
3742 {
3743   rtx tem, subtarget, comparison, insn;
3744   enum insn_code icode;
3745   enum rtx_code reversed;
3746
3747   /* If one operand is constant, make it the second one.  Only do this
3748      if the other operand is not constant as well.  */
3749
3750   if (swap_commutative_operands_p (op0, op1))
3751     {
3752       tem = op0;
3753       op0 = op1;
3754       op1 = tem;
3755       code = swap_condition (code);
3756     }
3757
3758   /* get_condition will prefer to generate LT and GT even if the old
3759      comparison was against zero, so undo that canonicalization here since
3760      comparisons against zero are cheaper.  */
3761   if (code == LT && op1 == const1_rtx)
3762     code = LE, op1 = const0_rtx;
3763   else if (code == GT && op1 == constm1_rtx)
3764     code = GE, op1 = const0_rtx;
3765
3766   if (cmode == VOIDmode)
3767     cmode = GET_MODE (op0);
3768
3769   if (swap_commutative_operands_p (op2, op3)
3770       && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
3771           != UNKNOWN))
3772     {
3773       tem = op2;
3774       op2 = op3;
3775       op3 = tem;
3776       code = reversed;
3777     }
3778
3779   if (mode == VOIDmode)
3780     mode = GET_MODE (op2);
3781
3782   icode = addcc_optab->handlers[(int) mode].insn_code;
3783
3784   if (icode == CODE_FOR_nothing)
3785     return 0;
3786
3787   if (flag_force_mem)
3788     {
3789       op2 = force_not_mem (op2);
3790       op3 = force_not_mem (op3);
3791     }
3792
3793   if (!target)
3794     target = gen_reg_rtx (mode);
3795
3796   /* If the insn doesn't accept these operands, put them in pseudos.  */
3797
3798   if (! (*insn_data[icode].operand[0].predicate)
3799       (target, insn_data[icode].operand[0].mode))
3800     subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
3801   else
3802     subtarget = target;
3803
3804   if (! (*insn_data[icode].operand[2].predicate)
3805       (op2, insn_data[icode].operand[2].mode))
3806     op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
3807
3808   if (! (*insn_data[icode].operand[3].predicate)
3809       (op3, insn_data[icode].operand[3].mode))
3810     op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
3811
3812   /* Everything should now be in the suitable form, so emit the compare insn
3813      and then the conditional move.  */
3814
3815   comparison
3816     = compare_from_rtx (op0, op1, code, unsignedp, cmode, NULL_RTX);
3817
3818   /* ??? Watch for const0_rtx (nop) and const_true_rtx (unconditional)?  */
3819   /* We can get const0_rtx or const_true_rtx in some circumstances.  Just
3820      return NULL and let the caller figure out how best to deal with this
3821      situation.  */
3822   if (GET_CODE (comparison) != code)
3823     return NULL_RTX;
3824
3825   insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
3826
3827   /* If that failed, then give up.  */
3828   if (insn == 0)
3829     return 0;
3830
3831   emit_insn (insn);
3832
3833   if (subtarget != target)
3834     convert_move (target, subtarget, 0);
3835
3836   return target;
3837 }
3838 \f
3839 /* These functions attempt to generate an insn body, rather than
3840    emitting the insn, but if the gen function already emits them, we
3841    make no attempt to turn them back into naked patterns.  */
3842
3843 /* Generate and return an insn body to add Y to X.  */
3844
3845 rtx
3846 gen_add2_insn (rtx x, rtx y)
3847 {
3848   int icode = (int) add_optab->handlers[(int) GET_MODE (x)].insn_code;
3849
3850   if (! ((*insn_data[icode].operand[0].predicate)
3851          (x, insn_data[icode].operand[0].mode))
3852       || ! ((*insn_data[icode].operand[1].predicate)
3853             (x, insn_data[icode].operand[1].mode))
3854       || ! ((*insn_data[icode].operand[2].predicate)
3855             (y, insn_data[icode].operand[2].mode)))
3856     abort ();
3857
3858   return (GEN_FCN (icode) (x, x, y));
3859 }
3860
3861 /* Generate and return an insn body to add r1 and c,
3862    storing the result in r0.  */
3863 rtx
3864 gen_add3_insn (rtx r0, rtx r1, rtx c)
3865 {
3866   int icode = (int) add_optab->handlers[(int) GET_MODE (r0)].insn_code;
3867
3868   if (icode == CODE_FOR_nothing
3869       || ! ((*insn_data[icode].operand[0].predicate)
3870             (r0, insn_data[icode].operand[0].mode))
3871       || ! ((*insn_data[icode].operand[1].predicate)
3872             (r1, insn_data[icode].operand[1].mode))
3873       || ! ((*insn_data[icode].operand[2].predicate)
3874             (c, insn_data[icode].operand[2].mode)))
3875     return NULL_RTX;
3876
3877   return (GEN_FCN (icode) (r0, r1, c));
3878 }
3879
3880 int
3881 have_add2_insn (rtx x, rtx y)
3882 {
3883   int icode;
3884
3885   if (GET_MODE (x) == VOIDmode)
3886     abort ();
3887
3888   icode = (int) add_optab->handlers[(int) GET_MODE (x)].insn_code;
3889
3890   if (icode == CODE_FOR_nothing)
3891     return 0;
3892
3893   if (! ((*insn_data[icode].operand[0].predicate)
3894          (x, insn_data[icode].operand[0].mode))
3895       || ! ((*insn_data[icode].operand[1].predicate)
3896             (x, insn_data[icode].operand[1].mode))
3897       || ! ((*insn_data[icode].operand[2].predicate)
3898             (y, insn_data[icode].operand[2].mode)))
3899     return 0;
3900
3901   return 1;
3902 }
3903
3904 /* Generate and return an insn body to subtract Y from X.  */
3905
3906 rtx
3907 gen_sub2_insn (rtx x, rtx y)
3908 {
3909   int icode = (int) sub_optab->handlers[(int) GET_MODE (x)].insn_code;
3910
3911   if (! ((*insn_data[icode].operand[0].predicate)
3912          (x, insn_data[icode].operand[0].mode))
3913       || ! ((*insn_data[icode].operand[1].predicate)
3914             (x, insn_data[icode].operand[1].mode))
3915       || ! ((*insn_data[icode].operand[2].predicate)
3916             (y, insn_data[icode].operand[2].mode)))
3917     abort ();
3918
3919   return (GEN_FCN (icode) (x, x, y));
3920 }
3921
3922 /* Generate and return an insn body to subtract r1 and c,
3923    storing the result in r0.  */
3924 rtx
3925 gen_sub3_insn (rtx r0, rtx r1, rtx c)
3926 {
3927   int icode = (int) sub_optab->handlers[(int) GET_MODE (r0)].insn_code;
3928
3929   if (icode == CODE_FOR_nothing
3930       || ! ((*insn_data[icode].operand[0].predicate)
3931             (r0, insn_data[icode].operand[0].mode))
3932       || ! ((*insn_data[icode].operand[1].predicate)
3933             (r1, insn_data[icode].operand[1].mode))
3934       || ! ((*insn_data[icode].operand[2].predicate)
3935             (c, insn_data[icode].operand[2].mode)))
3936     return NULL_RTX;
3937
3938   return (GEN_FCN (icode) (r0, r1, c));
3939 }
3940
3941 int
3942 have_sub2_insn (rtx x, rtx y)
3943 {
3944   int icode;
3945
3946   if (GET_MODE (x) == VOIDmode)
3947     abort ();
3948
3949   icode = (int) sub_optab->handlers[(int) GET_MODE (x)].insn_code;
3950
3951   if (icode == CODE_FOR_nothing)
3952     return 0;
3953
3954   if (! ((*insn_data[icode].operand[0].predicate)
3955          (x, insn_data[icode].operand[0].mode))
3956       || ! ((*insn_data[icode].operand[1].predicate)
3957             (x, insn_data[icode].operand[1].mode))
3958       || ! ((*insn_data[icode].operand[2].predicate)
3959             (y, insn_data[icode].operand[2].mode)))
3960     return 0;
3961
3962   return 1;
3963 }
3964
3965 /* Generate the body of an instruction to copy Y into X.
3966    It may be a list of insns, if one insn isn't enough.  */
3967
3968 rtx
3969 gen_move_insn (rtx x, rtx y)
3970 {
3971   rtx seq;
3972
3973   start_sequence ();
3974   emit_move_insn_1 (x, y);
3975   seq = get_insns ();
3976   end_sequence ();
3977   return seq;
3978 }
3979 \f
3980 /* Return the insn code used to extend FROM_MODE to TO_MODE.
3981    UNSIGNEDP specifies zero-extension instead of sign-extension.  If
3982    no such operation exists, CODE_FOR_nothing will be returned.  */
3983
3984 enum insn_code
3985 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
3986               int unsignedp)
3987 {
3988   convert_optab tab;
3989 #ifdef HAVE_ptr_extend
3990   if (unsignedp < 0)
3991     return CODE_FOR_ptr_extend;
3992 #endif
3993
3994   tab = unsignedp ? zext_optab : sext_optab;
3995   return tab->handlers[to_mode][from_mode].insn_code;
3996 }
3997
3998 /* Generate the body of an insn to extend Y (with mode MFROM)
3999    into X (with mode MTO).  Do zero-extension if UNSIGNEDP is nonzero.  */
4000
4001 rtx
4002 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4003                  enum machine_mode mfrom, int unsignedp)
4004 {
4005   enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4006   return GEN_FCN (icode) (x, y);
4007 }
4008 \f
4009 /* can_fix_p and can_float_p say whether the target machine
4010    can directly convert a given fixed point type to
4011    a given floating point type, or vice versa.
4012    The returned value is the CODE_FOR_... value to use,
4013    or CODE_FOR_nothing if these modes cannot be directly converted.
4014
4015    *TRUNCP_PTR is set to 1 if it is necessary to output
4016    an explicit FTRUNC insn before the fix insn; otherwise 0.  */
4017
4018 static enum insn_code
4019 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4020            int unsignedp, int *truncp_ptr)
4021 {
4022   convert_optab tab;
4023   enum insn_code icode;
4024
4025   tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4026   icode = tab->handlers[fixmode][fltmode].insn_code;
4027   if (icode != CODE_FOR_nothing)
4028     {
4029       *truncp_ptr = 0;
4030       return icode;
4031     }
4032
4033   /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4034      for this to work. We need to rework the fix* and ftrunc* patterns
4035      and documentation.  */
4036   tab = unsignedp ? ufix_optab : sfix_optab;
4037   icode = tab->handlers[fixmode][fltmode].insn_code;
4038   if (icode != CODE_FOR_nothing
4039       && ftrunc_optab->handlers[fltmode].insn_code != CODE_FOR_nothing)
4040     {
4041       *truncp_ptr = 1;
4042       return icode;
4043     }
4044
4045   *truncp_ptr = 0;
4046   return CODE_FOR_nothing;
4047 }
4048
4049 static enum insn_code
4050 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4051              int unsignedp)
4052 {
4053   convert_optab tab;
4054
4055   tab = unsignedp ? ufloat_optab : sfloat_optab;
4056   return tab->handlers[fltmode][fixmode].insn_code;
4057 }
4058 \f
4059 /* Generate code to convert FROM to floating point
4060    and store in TO.  FROM must be fixed point and not VOIDmode.
4061    UNSIGNEDP nonzero means regard FROM as unsigned.
4062    Normally this is done by correcting the final value
4063    if it is negative.  */
4064
4065 void
4066 expand_float (rtx to, rtx from, int unsignedp)
4067 {
4068   enum insn_code icode;
4069   rtx target = to;
4070   enum machine_mode fmode, imode;
4071
4072   /* Crash now, because we won't be able to decide which mode to use.  */
4073   if (GET_MODE (from) == VOIDmode)
4074     abort ();
4075
4076   /* Look for an insn to do the conversion.  Do it in the specified
4077      modes if possible; otherwise convert either input, output or both to
4078      wider mode.  If the integer mode is wider than the mode of FROM,
4079      we can do the conversion signed even if the input is unsigned.  */
4080
4081   for (fmode = GET_MODE (to); fmode != VOIDmode;
4082        fmode = GET_MODE_WIDER_MODE (fmode))
4083     for (imode = GET_MODE (from); imode != VOIDmode;
4084          imode = GET_MODE_WIDER_MODE (imode))
4085       {
4086         int doing_unsigned = unsignedp;
4087
4088         if (fmode != GET_MODE (to)
4089             && significand_size (fmode) < GET_MODE_BITSIZE (GET_MODE (from)))
4090           continue;
4091
4092         icode = can_float_p (fmode, imode, unsignedp);
4093         if (icode == CODE_FOR_nothing && imode != GET_MODE (from) && unsignedp)
4094           icode = can_float_p (fmode, imode, 0), doing_unsigned = 0;
4095
4096         if (icode != CODE_FOR_nothing)
4097           {
4098             if (imode != GET_MODE (from))
4099               from = convert_to_mode (imode, from, unsignedp);
4100
4101             if (fmode != GET_MODE (to))
4102               target = gen_reg_rtx (fmode);
4103
4104             emit_unop_insn (icode, target, from,
4105                             doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4106
4107             if (target != to)
4108               convert_move (to, target, 0);
4109             return;
4110           }
4111       }
4112
4113   /* Unsigned integer, and no way to convert directly.
4114      Convert as signed, then conditionally adjust the result.  */
4115   if (unsignedp)
4116     {
4117       rtx label = gen_label_rtx ();
4118       rtx temp;
4119       REAL_VALUE_TYPE offset;
4120
4121       if (flag_force_mem)
4122         from = force_not_mem (from);
4123
4124       /* Look for a usable floating mode FMODE wider than the source and at
4125          least as wide as the target.  Using FMODE will avoid rounding woes
4126          with unsigned values greater than the signed maximum value.  */
4127
4128       for (fmode = GET_MODE (to);  fmode != VOIDmode;
4129            fmode = GET_MODE_WIDER_MODE (fmode))
4130         if (GET_MODE_BITSIZE (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
4131             && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
4132           break;
4133
4134       if (fmode == VOIDmode)
4135         {
4136           /* There is no such mode.  Pretend the target is wide enough.  */
4137           fmode = GET_MODE (to);
4138
4139           /* Avoid double-rounding when TO is narrower than FROM.  */
4140           if ((significand_size (fmode) + 1)
4141               < GET_MODE_BITSIZE (GET_MODE (from)))
4142             {
4143               rtx temp1;
4144               rtx neglabel = gen_label_rtx ();
4145
4146               /* Don't use TARGET if it isn't a register, is a hard register,
4147                  or is the wrong mode.  */
4148               if (!REG_P (target)
4149                   || REGNO (target) < FIRST_PSEUDO_REGISTER
4150                   || GET_MODE (target) != fmode)
4151                 target = gen_reg_rtx (fmode);
4152
4153               imode = GET_MODE (from);
4154               do_pending_stack_adjust ();
4155
4156               /* Test whether the sign bit is set.  */
4157               emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
4158                                        0, neglabel);
4159
4160               /* The sign bit is not set.  Convert as signed.  */
4161               expand_float (target, from, 0);
4162               emit_jump_insn (gen_jump (label));
4163               emit_barrier ();
4164
4165               /* The sign bit is set.
4166                  Convert to a usable (positive signed) value by shifting right
4167                  one bit, while remembering if a nonzero bit was shifted
4168                  out; i.e., compute  (from & 1) | (from >> 1).  */
4169
4170               emit_label (neglabel);
4171               temp = expand_binop (imode, and_optab, from, const1_rtx,
4172                                    NULL_RTX, 1, OPTAB_LIB_WIDEN);
4173               temp1 = expand_shift (RSHIFT_EXPR, imode, from, integer_one_node,
4174                                     NULL_RTX, 1);
4175               temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
4176                                    OPTAB_LIB_WIDEN);
4177               expand_float (target, temp, 0);
4178
4179               /* Multiply by 2 to undo the shift above.  */
4180               temp = expand_binop (fmode, add_optab, target, target,
4181                                    target, 0, OPTAB_LIB_WIDEN);
4182               if (temp != target)
4183                 emit_move_insn (target, temp);
4184
4185               do_pending_stack_adjust ();
4186               emit_label (label);
4187               goto done;
4188             }
4189         }
4190
4191       /* If we are about to do some arithmetic to correct for an
4192          unsigned operand, do it in a pseudo-register.  */
4193
4194       if (GET_MODE (to) != fmode
4195           || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
4196         target = gen_reg_rtx (fmode);
4197
4198       /* Convert as signed integer to floating.  */
4199       expand_float (target, from, 0);
4200
4201       /* If FROM is negative (and therefore TO is negative),
4202          correct its value by 2**bitwidth.  */
4203
4204       do_pending_stack_adjust ();
4205       emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
4206                                0, label);
4207
4208
4209       real_2expN (&offset, GET_MODE_BITSIZE (GET_MODE (from)));
4210       temp = expand_binop (fmode, add_optab, target,
4211                            CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
4212                            target, 0, OPTAB_LIB_WIDEN);
4213       if (temp != target)
4214         emit_move_insn (target, temp);
4215
4216       do_pending_stack_adjust ();
4217       emit_label (label);
4218       goto done;
4219     }
4220
4221   /* No hardware instruction available; call a library routine.  */
4222     {
4223       rtx libfunc;
4224       rtx insns;
4225       rtx value;
4226       convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
4227
4228       if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
4229         from = convert_to_mode (SImode, from, unsignedp);
4230
4231       if (flag_force_mem)
4232         from = force_not_mem (from);
4233
4234       libfunc = tab->handlers[GET_MODE (to)][GET_MODE (from)].libfunc;
4235       if (!libfunc)
4236         abort ();
4237
4238       start_sequence ();
4239
4240       value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4241                                        GET_MODE (to), 1, from,
4242                                        GET_MODE (from));
4243       insns = get_insns ();
4244       end_sequence ();
4245
4246       emit_libcall_block (insns, target, value,
4247                           gen_rtx_FLOAT (GET_MODE (to), from));
4248     }
4249
4250  done:
4251
4252   /* Copy result to requested destination
4253      if we have been computing in a temp location.  */
4254
4255   if (target != to)
4256     {
4257       if (GET_MODE (target) == GET_MODE (to))
4258         emit_move_insn (to, target);
4259       else
4260         convert_move (to, target, 0);
4261     }
4262 }
4263 \f
4264 /* Generate code to convert FROM to fixed point and store in TO.  FROM
4265    must be floating point.  */
4266
4267 void
4268 expand_fix (rtx to, rtx from, int unsignedp)
4269 {
4270   enum insn_code icode;
4271   rtx target = to;
4272   enum machine_mode fmode, imode;
4273   int must_trunc = 0;
4274
4275   /* We first try to find a pair of modes, one real and one integer, at
4276      least as wide as FROM and TO, respectively, in which we can open-code
4277      this conversion.  If the integer mode is wider than the mode of TO,
4278      we can do the conversion either signed or unsigned.  */
4279
4280   for (fmode = GET_MODE (from); fmode != VOIDmode;
4281        fmode = GET_MODE_WIDER_MODE (fmode))
4282     for (imode = GET_MODE (to); imode != VOIDmode;
4283          imode = GET_MODE_WIDER_MODE (imode))
4284       {
4285         int doing_unsigned = unsignedp;
4286
4287         icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
4288         if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
4289           icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
4290
4291         if (icode != CODE_FOR_nothing)
4292           {
4293             if (fmode != GET_MODE (from))
4294               from = convert_to_mode (fmode, from, 0);
4295
4296             if (must_trunc)
4297               {
4298                 rtx temp = gen_reg_rtx (GET_MODE (from));
4299                 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
4300                                     temp, 0);
4301               }
4302
4303             if (imode != GET_MODE (to))
4304               target = gen_reg_rtx (imode);
4305
4306             emit_unop_insn (icode, target, from,
4307                             doing_unsigned ? UNSIGNED_FIX : FIX);
4308             if (target != to)
4309               convert_move (to, target, unsignedp);
4310             return;
4311           }
4312       }
4313
4314   /* For an unsigned conversion, there is one more way to do it.
4315      If we have a signed conversion, we generate code that compares
4316      the real value to the largest representable positive number.  If if
4317      is smaller, the conversion is done normally.  Otherwise, subtract
4318      one plus the highest signed number, convert, and add it back.
4319
4320      We only need to check all real modes, since we know we didn't find
4321      anything with a wider integer mode.
4322
4323      This code used to extend FP value into mode wider than the destination.
4324      This is not needed.  Consider, for instance conversion from SFmode
4325      into DImode.
4326
4327      The hot path trought the code is dealing with inputs smaller than 2^63
4328      and doing just the conversion, so there is no bits to lose.
4329
4330      In the other path we know the value is positive in the range 2^63..2^64-1
4331      inclusive.  (as for other imput overflow happens and result is undefined)
4332      So we know that the most important bit set in mantissa corresponds to
4333      2^63.  The subtraction of 2^63 should not generate any rounding as it
4334      simply clears out that bit.  The rest is trivial.  */
4335
4336   if (unsignedp && GET_MODE_BITSIZE (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
4337     for (fmode = GET_MODE (from); fmode != VOIDmode;
4338          fmode = GET_MODE_WIDER_MODE (fmode))
4339       if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0,
4340                                          &must_trunc))
4341         {
4342           int bitsize;
4343           REAL_VALUE_TYPE offset;
4344           rtx limit, lab1, lab2, insn;
4345
4346           bitsize = GET_MODE_BITSIZE (GET_MODE (to));
4347           real_2expN (&offset, bitsize - 1);
4348           limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
4349           lab1 = gen_label_rtx ();
4350           lab2 = gen_label_rtx ();
4351
4352           if (flag_force_mem)
4353             from = force_not_mem (from);
4354
4355           if (fmode != GET_MODE (from))
4356             from = convert_to_mode (fmode, from, 0);
4357
4358           /* See if we need to do the subtraction.  */
4359           do_pending_stack_adjust ();
4360           emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
4361                                    0, lab1);
4362
4363           /* If not, do the signed "fix" and branch around fixup code.  */
4364           expand_fix (to, from, 0);
4365           emit_jump_insn (gen_jump (lab2));
4366           emit_barrier ();
4367
4368           /* Otherwise, subtract 2**(N-1), convert to signed number,
4369              then add 2**(N-1).  Do the addition using XOR since this
4370              will often generate better code.  */
4371           emit_label (lab1);
4372           target = expand_binop (GET_MODE (from), sub_optab, from, limit,
4373                                  NULL_RTX, 0, OPTAB_LIB_WIDEN);
4374           expand_fix (to, target, 0);
4375           target = expand_binop (GET_MODE (to), xor_optab, to,
4376                                  gen_int_mode
4377                                  ((HOST_WIDE_INT) 1 << (bitsize - 1),
4378                                   GET_MODE (to)),
4379                                  to, 1, OPTAB_LIB_WIDEN);
4380
4381           if (target != to)
4382             emit_move_insn (to, target);
4383
4384           emit_label (lab2);
4385
4386           if (mov_optab->handlers[(int) GET_MODE (to)].insn_code
4387               != CODE_FOR_nothing)
4388             {
4389               /* Make a place for a REG_NOTE and add it.  */
4390               insn = emit_move_insn (to, to);
4391               set_unique_reg_note (insn,
4392                                    REG_EQUAL,
4393                                    gen_rtx_fmt_e (UNSIGNED_FIX,
4394                                                   GET_MODE (to),
4395                                                   copy_rtx (from)));
4396             }
4397
4398           return;
4399         }
4400
4401   /* We can't do it with an insn, so use a library call.  But first ensure
4402      that the mode of TO is at least as wide as SImode, since those are the
4403      only library calls we know about.  */
4404
4405   if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
4406     {
4407       target = gen_reg_rtx (SImode);
4408
4409       expand_fix (target, from, unsignedp);
4410     }
4411   else
4412     {
4413       rtx insns;
4414       rtx value;
4415       rtx libfunc;
4416
4417       convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
4418       libfunc = tab->handlers[GET_MODE (to)][GET_MODE (from)].libfunc;
4419       if (!libfunc)
4420         abort ();
4421
4422       if (flag_force_mem)
4423         from = force_not_mem (from);
4424
4425       start_sequence ();
4426
4427       value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4428                                        GET_MODE (to), 1, from,
4429                                        GET_MODE (from));
4430       insns = get_insns ();
4431       end_sequence ();
4432
4433       emit_libcall_block (insns, target, value,
4434                           gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
4435                                          GET_MODE (to), from));
4436     }
4437
4438   if (target != to)
4439     {
4440       if (GET_MODE (to) == GET_MODE (target))
4441         emit_move_insn (to, target);
4442       else
4443         convert_move (to, target, 0);
4444     }
4445 }
4446 \f
4447 /* Report whether we have an instruction to perform the operation
4448    specified by CODE on operands of mode MODE.  */
4449 int
4450 have_insn_for (enum rtx_code code, enum machine_mode mode)
4451 {
4452   return (code_to_optab[(int) code] != 0
4453           && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
4454               != CODE_FOR_nothing));
4455 }
4456
4457 /* Create a blank optab.  */
4458 static optab
4459 new_optab (void)
4460 {
4461   int i;
4462   optab op = ggc_alloc (sizeof (struct optab));
4463   for (i = 0; i < NUM_MACHINE_MODES; i++)
4464     {
4465       op->handlers[i].insn_code = CODE_FOR_nothing;
4466       op->handlers[i].libfunc = 0;
4467     }
4468
4469   return op;
4470 }
4471
4472 static convert_optab
4473 new_convert_optab (void)
4474 {
4475   int i, j;
4476   convert_optab op = ggc_alloc (sizeof (struct convert_optab));
4477   for (i = 0; i < NUM_MACHINE_MODES; i++)
4478     for (j = 0; j < NUM_MACHINE_MODES; j++)
4479       {
4480         op->handlers[i][j].insn_code = CODE_FOR_nothing;
4481         op->handlers[i][j].libfunc = 0;
4482       }
4483   return op;
4484 }
4485
4486 /* Same, but fill in its code as CODE, and write it into the
4487    code_to_optab table.  */
4488 static inline optab
4489 init_optab (enum rtx_code code)
4490 {
4491   optab op = new_optab ();
4492   op->code = code;
4493   code_to_optab[(int) code] = op;
4494   return op;
4495 }
4496
4497 /* Same, but fill in its code as CODE, and do _not_ write it into
4498    the code_to_optab table.  */
4499 static inline optab
4500 init_optabv (enum rtx_code code)
4501 {
4502   optab op = new_optab ();
4503   op->code = code;
4504   return op;
4505 }
4506
4507 /* Conversion optabs never go in the code_to_optab table.  */
4508 static inline convert_optab
4509 init_convert_optab (enum rtx_code code)
4510 {
4511   convert_optab op = new_convert_optab ();
4512   op->code = code;
4513   return op;
4514 }
4515
4516 /* Initialize the libfunc fields of an entire group of entries in some
4517    optab.  Each entry is set equal to a string consisting of a leading
4518    pair of underscores followed by a generic operation name followed by
4519    a mode name (downshifted to lowercase) followed by a single character
4520    representing the number of operands for the given operation (which is
4521    usually one of the characters '2', '3', or '4').
4522
4523    OPTABLE is the table in which libfunc fields are to be initialized.
4524    FIRST_MODE is the first machine mode index in the given optab to
4525      initialize.
4526    LAST_MODE is the last machine mode index in the given optab to
4527      initialize.
4528    OPNAME is the generic (string) name of the operation.
4529    SUFFIX is the character which specifies the number of operands for
4530      the given generic operation.
4531 */
4532
4533 static void
4534 init_libfuncs (optab optable, int first_mode, int last_mode,
4535                const char *opname, int suffix)
4536 {
4537   int mode;
4538   unsigned opname_len = strlen (opname);
4539
4540   for (mode = first_mode; (int) mode <= (int) last_mode;
4541        mode = (enum machine_mode) ((int) mode + 1))
4542     {
4543       const char *mname = GET_MODE_NAME (mode);
4544       unsigned mname_len = strlen (mname);
4545       char *libfunc_name = alloca (2 + opname_len + mname_len + 1 + 1);
4546       char *p;
4547       const char *q;
4548
4549       p = libfunc_name;
4550       *p++ = '_';
4551       *p++ = '_';
4552       for (q = opname; *q; )
4553         *p++ = *q++;
4554       for (q = mname; *q; q++)
4555         *p++ = TOLOWER (*q);
4556       *p++ = suffix;
4557       *p = '\0';
4558
4559       optable->handlers[(int) mode].libfunc
4560         = init_one_libfunc (ggc_alloc_string (libfunc_name, p - libfunc_name));
4561     }
4562 }
4563
4564 /* Initialize the libfunc fields of an entire group of entries in some
4565    optab which correspond to all integer mode operations.  The parameters
4566    have the same meaning as similarly named ones for the `init_libfuncs'
4567    routine.  (See above).  */
4568
4569 static void
4570 init_integral_libfuncs (optab optable, const char *opname, int suffix)
4571 {
4572   int maxsize = 2*BITS_PER_WORD;
4573   if (maxsize < LONG_LONG_TYPE_SIZE)
4574     maxsize = LONG_LONG_TYPE_SIZE;
4575   init_libfuncs (optable, word_mode,
4576                  mode_for_size (maxsize, MODE_INT, 0),
4577                  opname, suffix);
4578 }
4579
4580 /* Initialize the libfunc fields of an entire group of entries in some
4581    optab which correspond to all real mode operations.  The parameters
4582    have the same meaning as similarly named ones for the `init_libfuncs'
4583    routine.  (See above).  */
4584
4585 static void
4586 init_floating_libfuncs (optab optable, const char *opname, int suffix)
4587 {
4588   init_libfuncs (optable, MIN_MODE_FLOAT, MAX_MODE_FLOAT, opname, suffix);
4589 }
4590
4591 /* Initialize the libfunc fields of an entire group of entries of an
4592    inter-mode-class conversion optab.  The string formation rules are
4593    similar to the ones for init_libfuncs, above, but instead of having
4594    a mode name and an operand count these functions have two mode names
4595    and no operand count.  */
4596 static void
4597 init_interclass_conv_libfuncs (convert_optab tab, const char *opname,
4598                                enum mode_class from_class,
4599                                enum mode_class to_class)
4600 {
4601   enum machine_mode first_from_mode = GET_CLASS_NARROWEST_MODE (from_class);
4602   enum machine_mode first_to_mode = GET_CLASS_NARROWEST_MODE (to_class);
4603   size_t opname_len = strlen (opname);
4604   size_t max_mname_len = 0;
4605
4606   enum machine_mode fmode, tmode;
4607   const char *fname, *tname;
4608   const char *q;
4609   char *libfunc_name, *suffix;
4610   char *p;
4611
4612   for (fmode = first_from_mode;
4613        fmode != VOIDmode;
4614        fmode = GET_MODE_WIDER_MODE (fmode))
4615     max_mname_len = MAX (max_mname_len, strlen (GET_MODE_NAME (fmode)));
4616
4617   for (tmode = first_to_mode;
4618        tmode != VOIDmode;
4619        tmode = GET_MODE_WIDER_MODE (tmode))
4620     max_mname_len = MAX (max_mname_len, strlen (GET_MODE_NAME (tmode)));
4621
4622   libfunc_name = alloca (2 + opname_len + 2*max_mname_len + 1 + 1);
4623   libfunc_name[0] = '_';
4624   libfunc_name[1] = '_';
4625   memcpy (&libfunc_name[2], opname, opname_len);
4626   suffix = libfunc_name + opname_len + 2;
4627
4628   for (fmode = first_from_mode; fmode != VOIDmode;
4629        fmode = GET_MODE_WIDER_MODE (fmode))
4630     for (tmode = first_to_mode; tmode != VOIDmode;
4631          tmode = GET_MODE_WIDER_MODE (tmode))
4632       {
4633         fname = GET_MODE_NAME (fmode);
4634         tname = GET_MODE_NAME (tmode);
4635
4636         p = suffix;
4637         for (q = fname; *q; p++, q++)
4638           *p = TOLOWER (*q);
4639         for (q = tname; *q; p++, q++)
4640           *p = TOLOWER (*q);
4641
4642         *p = '\0';
4643
4644         tab->handlers[tmode][fmode].libfunc
4645           = init_one_libfunc (ggc_alloc_string (libfunc_name,
4646                                                 p - libfunc_name));
4647       }
4648 }
4649
4650 /* Initialize the libfunc fields of an entire group of entries of an
4651    intra-mode-class conversion optab.  The string formation rules are
4652    similar to the ones for init_libfunc, above.  WIDENING says whether
4653    the optab goes from narrow to wide modes or vice versa.  These functions
4654    have two mode names _and_ an operand count.  */
4655 static void
4656 init_intraclass_conv_libfuncs (convert_optab tab, const char *opname,
4657                                enum mode_class class, bool widening)
4658 {
4659   enum machine_mode first_mode = GET_CLASS_NARROWEST_MODE (class);
4660   size_t opname_len = strlen (opname);
4661   size_t max_mname_len = 0;
4662
4663   enum machine_mode nmode, wmode;
4664   const char *nname, *wname;
4665   const char *q;
4666   char *libfunc_name, *suffix;
4667   char *p;
4668
4669   for (nmode = first_mode; nmode != VOIDmode;
4670        nmode = GET_MODE_WIDER_MODE (nmode))
4671     max_mname_len = MAX (max_mname_len, strlen (GET_MODE_NAME (nmode)));
4672
4673   libfunc_name = alloca (2 + opname_len + 2*max_mname_len + 1 + 1);
4674   libfunc_name[0] = '_';
4675   libfunc_name[1] = '_';
4676   memcpy (&libfunc_name[2], opname, opname_len);
4677   suffix = libfunc_name + opname_len + 2;
4678
4679   for (nmode = first_mode; nmode != VOIDmode;
4680        nmode = GET_MODE_WIDER_MODE (nmode))
4681     for (wmode = GET_MODE_WIDER_MODE (nmode); wmode != VOIDmode;
4682          wmode = GET_MODE_WIDER_MODE (wmode))
4683       {
4684         nname = GET_MODE_NAME (nmode);
4685         wname = GET_MODE_NAME (wmode);
4686
4687         p = suffix;
4688         for (q = widening ? nname : wname; *q; p++, q++)
4689           *p = TOLOWER (*q);
4690         for (q = widening ? wname : nname; *q; p++, q++)
4691           *p = TOLOWER (*q);
4692
4693         *p++ = '2';
4694         *p = '\0';
4695
4696         tab->handlers[widening ? wmode : nmode]
4697                      [widening ? nmode : wmode].libfunc
4698           = init_one_libfunc (ggc_alloc_string (libfunc_name,
4699                                                 p - libfunc_name));
4700       }
4701 }
4702
4703
4704 rtx
4705 init_one_libfunc (const char *name)
4706 {
4707   rtx symbol;
4708
4709   /* Create a FUNCTION_DECL that can be passed to
4710      targetm.encode_section_info.  */
4711   /* ??? We don't have any type information except for this is
4712      a function.  Pretend this is "int foo()".  */
4713   tree decl = build_decl (FUNCTION_DECL, get_identifier (name),
4714                           build_function_type (integer_type_node, NULL_TREE));
4715   DECL_ARTIFICIAL (decl) = 1;
4716   DECL_EXTERNAL (decl) = 1;
4717   TREE_PUBLIC (decl) = 1;
4718
4719   symbol = XEXP (DECL_RTL (decl), 0);
4720
4721   /* Zap the nonsensical SYMBOL_REF_DECL for this.  What we're left with
4722      are the flags assigned by targetm.encode_section_info.  */
4723   SYMBOL_REF_DECL (symbol) = 0;
4724
4725   return symbol;
4726 }
4727
4728 /* Call this to reset the function entry for one optab (OPTABLE) in mode
4729    MODE to NAME, which should be either 0 or a string constant.  */
4730 void
4731 set_optab_libfunc (optab optable, enum machine_mode mode, const char *name)
4732 {
4733   if (name)
4734     optable->handlers[mode].libfunc = init_one_libfunc (name);
4735   else
4736     optable->handlers[mode].libfunc = 0;
4737 }
4738
4739 /* Call this to reset the function entry for one conversion optab
4740    (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
4741    either 0 or a string constant.  */
4742 void
4743 set_conv_libfunc (convert_optab optable, enum machine_mode tmode,
4744                   enum machine_mode fmode, const char *name)
4745 {
4746   if (name)
4747     optable->handlers[tmode][fmode].libfunc = init_one_libfunc (name);
4748   else
4749     optable->handlers[tmode][fmode].libfunc = 0;
4750 }
4751
4752 /* Call this once to initialize the contents of the optabs
4753    appropriately for the current target machine.  */
4754
4755 void
4756 init_optabs (void)
4757 {
4758   unsigned int i;
4759
4760   /* Start by initializing all tables to contain CODE_FOR_nothing.  */
4761
4762   for (i = 0; i < NUM_RTX_CODE; i++)
4763     setcc_gen_code[i] = CODE_FOR_nothing;
4764
4765 #ifdef HAVE_conditional_move
4766   for (i = 0; i < NUM_MACHINE_MODES; i++)
4767     movcc_gen_code[i] = CODE_FOR_nothing;
4768 #endif
4769
4770   for (i = 0; i < NUM_MACHINE_MODES; i++)
4771     {
4772       vcond_gen_code[i] = CODE_FOR_nothing;
4773       vcondu_gen_code[i] = CODE_FOR_nothing;
4774     }
4775
4776   add_optab = init_optab (PLUS);
4777   addv_optab = init_optabv (PLUS);
4778   sub_optab = init_optab (MINUS);
4779   subv_optab = init_optabv (MINUS);
4780   smul_optab = init_optab (MULT);
4781   smulv_optab = init_optabv (MULT);
4782   smul_highpart_optab = init_optab (UNKNOWN);
4783   umul_highpart_optab = init_optab (UNKNOWN);
4784   smul_widen_optab = init_optab (UNKNOWN);
4785   umul_widen_optab = init_optab (UNKNOWN);
4786   sdiv_optab = init_optab (DIV);
4787   sdivv_optab = init_optabv (DIV);
4788   sdivmod_optab = init_optab (UNKNOWN);
4789   udiv_optab = init_optab (UDIV);
4790   udivmod_optab = init_optab (UNKNOWN);
4791   smod_optab = init_optab (MOD);
4792   umod_optab = init_optab (UMOD);
4793   fmod_optab = init_optab (UNKNOWN);
4794   drem_optab = init_optab (UNKNOWN);
4795   ftrunc_optab = init_optab (UNKNOWN);
4796   and_optab = init_optab (AND);
4797   ior_optab = init_optab (IOR);
4798   xor_optab = init_optab (XOR);
4799   ashl_optab = init_optab (ASHIFT);
4800   ashr_optab = init_optab (ASHIFTRT);
4801   lshr_optab = init_optab (LSHIFTRT);
4802   rotl_optab = init_optab (ROTATE);
4803   rotr_optab = init_optab (ROTATERT);
4804   smin_optab = init_optab (SMIN);
4805   smax_optab = init_optab (SMAX);
4806   umin_optab = init_optab (UMIN);
4807   umax_optab = init_optab (UMAX);
4808   pow_optab = init_optab (UNKNOWN);
4809   atan2_optab = init_optab (UNKNOWN);
4810
4811   /* These three have codes assigned exclusively for the sake of
4812      have_insn_for.  */
4813   mov_optab = init_optab (SET);
4814   movstrict_optab = init_optab (STRICT_LOW_PART);
4815   cmp_optab = init_optab (COMPARE);
4816
4817   ucmp_optab = init_optab (UNKNOWN);
4818   tst_optab = init_optab (UNKNOWN);
4819
4820   eq_optab = init_optab (EQ);
4821   ne_optab = init_optab (NE);
4822   gt_optab = init_optab (GT);
4823   ge_optab = init_optab (GE);
4824   lt_optab = init_optab (LT);
4825   le_optab = init_optab (LE);
4826   unord_optab = init_optab (UNORDERED);
4827
4828   neg_optab = init_optab (NEG);
4829   negv_optab = init_optabv (NEG);
4830   abs_optab = init_optab (ABS);
4831   absv_optab = init_optabv (ABS);
4832   addcc_optab = init_optab (UNKNOWN);
4833   one_cmpl_optab = init_optab (NOT);
4834   ffs_optab = init_optab (FFS);
4835   clz_optab = init_optab (CLZ);
4836   ctz_optab = init_optab (CTZ);
4837   popcount_optab = init_optab (POPCOUNT);
4838   parity_optab = init_optab (PARITY);
4839   sqrt_optab = init_optab (SQRT);
4840   floor_optab = init_optab (UNKNOWN);
4841   ceil_optab = init_optab (UNKNOWN);
4842   round_optab = init_optab (UNKNOWN);
4843   btrunc_optab = init_optab (UNKNOWN);
4844   nearbyint_optab = init_optab (UNKNOWN);
4845   rint_optab = init_optab (UNKNOWN);
4846   sincos_optab = init_optab (UNKNOWN);
4847   sin_optab = init_optab (UNKNOWN);
4848   asin_optab = init_optab (UNKNOWN);
4849   cos_optab = init_optab (UNKNOWN);
4850   acos_optab = init_optab (UNKNOWN);
4851   exp_optab = init_optab (UNKNOWN);
4852   exp10_optab = init_optab (UNKNOWN);
4853   exp2_optab = init_optab (UNKNOWN);
4854   expm1_optab = init_optab (UNKNOWN);
4855   logb_optab = init_optab (UNKNOWN);
4856   ilogb_optab = init_optab (UNKNOWN);
4857   log_optab = init_optab (UNKNOWN);
4858   log10_optab = init_optab (UNKNOWN);
4859   log2_optab = init_optab (UNKNOWN);
4860   log1p_optab = init_optab (UNKNOWN);
4861   tan_optab = init_optab (UNKNOWN);
4862   atan_optab = init_optab (UNKNOWN);
4863   copysign_optab = init_optab (UNKNOWN);
4864
4865   strlen_optab = init_optab (UNKNOWN);
4866   cbranch_optab = init_optab (UNKNOWN);
4867   cmov_optab = init_optab (UNKNOWN);
4868   cstore_optab = init_optab (UNKNOWN);
4869   push_optab = init_optab (UNKNOWN);
4870
4871   vec_extract_optab = init_optab (UNKNOWN);
4872   vec_set_optab = init_optab (UNKNOWN);
4873   vec_init_optab = init_optab (UNKNOWN);
4874   vec_realign_load_optab = init_optab (UNKNOWN);
4875   movmisalign_optab = init_optab (UNKNOWN);
4876
4877   /* Conversions.  */
4878   sext_optab = init_convert_optab (SIGN_EXTEND);
4879   zext_optab = init_convert_optab (ZERO_EXTEND);
4880   trunc_optab = init_convert_optab (TRUNCATE);
4881   sfix_optab = init_convert_optab (FIX);
4882   ufix_optab = init_convert_optab (UNSIGNED_FIX);
4883   sfixtrunc_optab = init_convert_optab (UNKNOWN);
4884   ufixtrunc_optab = init_convert_optab (UNKNOWN);
4885   sfloat_optab = init_convert_optab (FLOAT);
4886   ufloat_optab = init_convert_optab (UNSIGNED_FLOAT);
4887
4888   for (i = 0; i < NUM_MACHINE_MODES; i++)
4889     {
4890       movmem_optab[i] = CODE_FOR_nothing;
4891       clrmem_optab[i] = CODE_FOR_nothing;
4892       cmpstr_optab[i] = CODE_FOR_nothing;
4893       cmpmem_optab[i] = CODE_FOR_nothing;
4894
4895 #ifdef HAVE_SECONDARY_RELOADS
4896       reload_in_optab[i] = reload_out_optab[i] = CODE_FOR_nothing;
4897 #endif
4898     }
4899
4900   /* Fill in the optabs with the insns we support.  */
4901   init_all_optabs ();
4902
4903   /* Initialize the optabs with the names of the library functions.  */
4904   init_integral_libfuncs (add_optab, "add", '3');
4905   init_floating_libfuncs (add_optab, "add", '3');
4906   init_integral_libfuncs (addv_optab, "addv", '3');
4907   init_floating_libfuncs (addv_optab, "add", '3');
4908   init_integral_libfuncs (sub_optab, "sub", '3');
4909   init_floating_libfuncs (sub_optab, "sub", '3');
4910   init_integral_libfuncs (subv_optab, "subv", '3');
4911   init_floating_libfuncs (subv_optab, "sub", '3');
4912   init_integral_libfuncs (smul_optab, "mul", '3');
4913   init_floating_libfuncs (smul_optab, "mul", '3');
4914   init_integral_libfuncs (smulv_optab, "mulv", '3');
4915   init_floating_libfuncs (smulv_optab, "mul", '3');
4916   init_integral_libfuncs (sdiv_optab, "div", '3');
4917   init_floating_libfuncs (sdiv_optab, "div", '3');
4918   init_integral_libfuncs (sdivv_optab, "divv", '3');
4919   init_integral_libfuncs (udiv_optab, "udiv", '3');
4920   init_integral_libfuncs (sdivmod_optab, "divmod", '4');
4921   init_integral_libfuncs (udivmod_optab, "udivmod", '4');
4922   init_integral_libfuncs (smod_optab, "mod", '3');
4923   init_integral_libfuncs (umod_optab, "umod", '3');
4924   init_floating_libfuncs (ftrunc_optab, "ftrunc", '2');
4925   init_integral_libfuncs (and_optab, "and", '3');
4926   init_integral_libfuncs (ior_optab, "ior", '3');
4927   init_integral_libfuncs (xor_optab, "xor", '3');
4928   init_integral_libfuncs (ashl_optab, "ashl", '3');
4929   init_integral_libfuncs (ashr_optab, "ashr", '3');
4930   init_integral_libfuncs (lshr_optab, "lshr", '3');
4931   init_integral_libfuncs (smin_optab, "min", '3');
4932   init_floating_libfuncs (smin_optab, "min", '3');
4933   init_integral_libfuncs (smax_optab, "max", '3');
4934   init_floating_libfuncs (smax_optab, "max", '3');
4935   init_integral_libfuncs (umin_optab, "umin", '3');
4936   init_integral_libfuncs (umax_optab, "umax", '3');
4937   init_integral_libfuncs (neg_optab, "neg", '2');
4938   init_floating_libfuncs (neg_optab, "neg", '2');
4939   init_integral_libfuncs (negv_optab, "negv", '2');
4940   init_floating_libfuncs (negv_optab, "neg", '2');
4941   init_integral_libfuncs (one_cmpl_optab, "one_cmpl", '2');
4942   init_integral_libfuncs (ffs_optab, "ffs", '2');
4943   init_integral_libfuncs (clz_optab, "clz", '2');
4944   init_integral_libfuncs (ctz_optab, "ctz", '2');
4945   init_integral_libfuncs (popcount_optab, "popcount", '2');
4946   init_integral_libfuncs (parity_optab, "parity", '2');
4947
4948   /* Comparison libcalls for integers MUST come in pairs,
4949      signed/unsigned.  */
4950   init_integral_libfuncs (cmp_optab, "cmp", '2');
4951   init_integral_libfuncs (ucmp_optab, "ucmp", '2');
4952   init_floating_libfuncs (cmp_optab, "cmp", '2');
4953
4954   /* EQ etc are floating point only.  */
4955   init_floating_libfuncs (eq_optab, "eq", '2');
4956   init_floating_libfuncs (ne_optab, "ne", '2');
4957   init_floating_libfuncs (gt_optab, "gt", '2');
4958   init_floating_libfuncs (ge_optab, "ge", '2');
4959   init_floating_libfuncs (lt_optab, "lt", '2');
4960   init_floating_libfuncs (le_optab, "le", '2');
4961   init_floating_libfuncs (unord_optab, "unord", '2');
4962
4963   /* Conversions.  */
4964   init_interclass_conv_libfuncs (sfloat_optab, "float",
4965                                  MODE_INT, MODE_FLOAT);
4966   init_interclass_conv_libfuncs (sfix_optab, "fix",
4967                                  MODE_FLOAT, MODE_INT);
4968   init_interclass_conv_libfuncs (ufix_optab, "fixuns",
4969                                  MODE_FLOAT, MODE_INT);
4970
4971   /* sext_optab is also used for FLOAT_EXTEND.  */
4972   init_intraclass_conv_libfuncs (sext_optab, "extend", MODE_FLOAT, true);
4973   init_intraclass_conv_libfuncs (trunc_optab, "trunc", MODE_FLOAT, false);
4974
4975   /* Use cabs for double complex abs, since systems generally have cabs.
4976      Don't define any libcall for float complex, so that cabs will be used.  */
4977   if (complex_double_type_node)
4978     abs_optab->handlers[TYPE_MODE (complex_double_type_node)].libfunc
4979       = init_one_libfunc ("cabs");
4980
4981   /* The ffs function operates on `int'.  */
4982   ffs_optab->handlers[(int) mode_for_size (INT_TYPE_SIZE, MODE_INT, 0)].libfunc
4983     = init_one_libfunc ("ffs");
4984
4985   abort_libfunc = init_one_libfunc ("abort");
4986   memcpy_libfunc = init_one_libfunc ("memcpy");
4987   memmove_libfunc = init_one_libfunc ("memmove");
4988   memcmp_libfunc = init_one_libfunc ("memcmp");
4989   memset_libfunc = init_one_libfunc ("memset");
4990   setbits_libfunc = init_one_libfunc ("__setbits");
4991
4992   unwind_resume_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
4993                                             ? "_Unwind_SjLj_Resume"
4994                                             : "_Unwind_Resume");
4995 #ifndef DONT_USE_BUILTIN_SETJMP
4996   setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
4997   longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
4998 #else
4999   setjmp_libfunc = init_one_libfunc ("setjmp");
5000   longjmp_libfunc = init_one_libfunc ("longjmp");
5001 #endif
5002   unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
5003   unwind_sjlj_unregister_libfunc
5004     = init_one_libfunc ("_Unwind_SjLj_Unregister");
5005
5006   /* For function entry/exit instrumentation.  */
5007   profile_function_entry_libfunc
5008     = init_one_libfunc ("__cyg_profile_func_enter");
5009   profile_function_exit_libfunc
5010     = init_one_libfunc ("__cyg_profile_func_exit");
5011
5012   gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
5013
5014   if (HAVE_conditional_trap)
5015     trap_rtx = gen_rtx_fmt_ee (EQ, VOIDmode, NULL_RTX, NULL_RTX);
5016
5017   /* Allow the target to add more libcalls or rename some, etc.  */
5018   targetm.init_libfuncs ();
5019 }
5020
5021 #ifdef DEBUG
5022
5023 /* Print information about the current contents of the optabs on
5024    STDERR.  */
5025
5026 static void
5027 debug_optab_libfuncs (void)
5028 {
5029   int i;
5030   int j;
5031   int k;
5032
5033   /* Dump the arithmetic optabs.  */
5034   for (i = 0; i != (int) OTI_MAX; i++)
5035     for (j = 0; j < NUM_MACHINE_MODES; ++j)
5036       {
5037         optab o;
5038         struct optab_handlers *h;
5039
5040         o = optab_table[i];
5041         h = &o->handlers[j];
5042         if (h->libfunc)
5043           {
5044             if (GET_CODE (h->libfunc) != SYMBOL_REF)
5045               abort ();
5046             fprintf (stderr, "%s\t%s:\t%s\n",
5047                      GET_RTX_NAME (o->code),
5048                      GET_MODE_NAME (j),
5049                      XSTR (h->libfunc, 0));
5050           }
5051       }
5052
5053   /* Dump the conversion optabs.  */
5054   for (i = 0; i < (int) CTI_MAX; ++i)
5055     for (j = 0; j < NUM_MACHINE_MODES; ++j)
5056       for (k = 0; k < NUM_MACHINE_MODES; ++k)
5057         {
5058           convert_optab o;
5059           struct optab_handlers *h;
5060
5061           o = &convert_optab_table[i];
5062           h = &o->handlers[j][k];
5063           if (h->libfunc)
5064             {
5065               if (GET_CODE (h->libfunc) != SYMBOL_REF)
5066                 abort ();
5067               fprintf (stderr, "%s\t%s\t%s:\t%s\n",
5068                        GET_RTX_NAME (o->code),
5069                        GET_MODE_NAME (j),
5070                        GET_MODE_NAME (k),
5071                        XSTR (h->libfunc, 0));
5072             }
5073         }
5074 }
5075
5076 #endif /* DEBUG */
5077
5078 \f
5079 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
5080    CODE.  Return 0 on failure.  */
5081
5082 rtx
5083 gen_cond_trap (enum rtx_code code ATTRIBUTE_UNUSED, rtx op1,
5084                rtx op2 ATTRIBUTE_UNUSED, rtx tcode ATTRIBUTE_UNUSED)
5085 {
5086   enum machine_mode mode = GET_MODE (op1);
5087   enum insn_code icode;
5088   rtx insn;
5089
5090   if (!HAVE_conditional_trap)
5091     return 0;
5092
5093   if (mode == VOIDmode)
5094     return 0;
5095
5096   icode = cmp_optab->handlers[(int) mode].insn_code;
5097   if (icode == CODE_FOR_nothing)
5098     return 0;
5099
5100   start_sequence ();
5101   op1 = prepare_operand (icode, op1, 0, mode, mode, 0);
5102   op2 = prepare_operand (icode, op2, 1, mode, mode, 0);
5103   if (!op1 || !op2)
5104     {
5105       end_sequence ();
5106       return 0;
5107     }
5108   emit_insn (GEN_FCN (icode) (op1, op2));
5109
5110   PUT_CODE (trap_rtx, code);
5111   insn = gen_conditional_trap (trap_rtx, tcode);
5112   if (insn)
5113     {
5114       emit_insn (insn);
5115       insn = get_insns ();
5116     }
5117   end_sequence ();
5118
5119   return insn;
5120 }
5121
5122 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
5123    or unsigned operation code.  */
5124
5125 static enum rtx_code
5126 get_rtx_code (enum tree_code tcode, bool unsignedp)
5127 {
5128   enum rtx_code code;
5129   switch (tcode)
5130     {
5131     case EQ_EXPR:
5132       code = EQ;
5133       break;
5134     case NE_EXPR:
5135       code = NE;
5136       break;
5137     case LT_EXPR:
5138       code = unsignedp ? LTU : LT;
5139       break;
5140     case LE_EXPR:
5141       code = unsignedp ? LEU : LE;
5142       break;
5143     case GT_EXPR:
5144       code = unsignedp ? GTU : GT;
5145       break;
5146     case GE_EXPR:
5147       code = unsignedp ? GEU : GE;
5148       break;
5149       
5150     case UNORDERED_EXPR:
5151       code = UNORDERED;
5152       break;
5153     case ORDERED_EXPR:
5154       code = ORDERED;
5155       break;
5156     case UNLT_EXPR:
5157       code = UNLT;
5158       break;
5159     case UNLE_EXPR:
5160       code = UNLE;
5161       break;
5162     case UNGT_EXPR:
5163       code = UNGT;
5164       break;
5165     case UNGE_EXPR:
5166       code = UNGE;
5167       break;
5168     case UNEQ_EXPR:
5169       code = UNEQ;
5170       break;
5171     case LTGT_EXPR:
5172       code = LTGT;
5173       break;
5174
5175     default:
5176       abort ();
5177     }
5178   return code;
5179 }
5180
5181 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
5182    unsigned operators. Do not generate compare instruction.  */
5183
5184 static rtx
5185 vector_compare_rtx (tree cond, bool unsignedp, enum insn_code icode)
5186 {
5187   enum rtx_code rcode;
5188   tree t_op0, t_op1;
5189   rtx rtx_op0, rtx_op1;
5190
5191   if (!COMPARISON_CLASS_P (cond))
5192     {
5193       /* This is unlikely. While generating VEC_COND_EXPR,
5194          auto vectorizer ensures that condition is a relational
5195          operation.  */
5196       abort ();
5197     }
5198   else
5199     {
5200       rcode = get_rtx_code (TREE_CODE (cond), unsignedp); 
5201       t_op0 = TREE_OPERAND (cond, 0);
5202       t_op1 = TREE_OPERAND (cond, 1);
5203     }
5204
5205   /* Expand operands.  */
5206   rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)), 1);
5207   rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)), 1);
5208
5209   if (!(*insn_data[icode].operand[4].predicate) (rtx_op0, GET_MODE (rtx_op0))
5210       && GET_MODE (rtx_op0) != VOIDmode)
5211     rtx_op0 = force_reg (GET_MODE (rtx_op0), rtx_op0);
5212   
5213   if (!(*insn_data[icode].operand[5].predicate) (rtx_op1, GET_MODE (rtx_op1))
5214       && GET_MODE (rtx_op1) != VOIDmode)
5215     rtx_op1 = force_reg (GET_MODE (rtx_op1), rtx_op1);
5216
5217   return gen_rtx_fmt_ee (rcode, VOIDmode, rtx_op0, rtx_op1);
5218 }
5219
5220 /* Return insn code for VEC_COND_EXPR EXPR.  */
5221   
5222 static inline enum insn_code 
5223 get_vcond_icode (tree expr, enum machine_mode mode)
5224 {
5225   enum insn_code icode = CODE_FOR_nothing;
5226
5227   if (TYPE_UNSIGNED (TREE_TYPE (expr)))
5228     icode = vcondu_gen_code[mode];
5229   else
5230     icode = vcond_gen_code[mode];
5231   return icode;
5232 }
5233
5234 /* Return TRUE iff, appropriate vector insns are available
5235    for vector cond expr expr in VMODE mode.  */
5236
5237 bool
5238 expand_vec_cond_expr_p (tree expr, enum machine_mode vmode)
5239 {
5240   if (get_vcond_icode (expr, vmode) == CODE_FOR_nothing)
5241     return false;
5242   return true;
5243 }
5244
5245 /* Generate insns for VEC_COND_EXPR.  */
5246
5247 rtx
5248 expand_vec_cond_expr (tree vec_cond_expr, rtx target)
5249 {
5250   enum insn_code icode;
5251   rtx comparison, rtx_op1, rtx_op2, cc_op0, cc_op1;
5252   enum machine_mode mode = TYPE_MODE (TREE_TYPE (vec_cond_expr));
5253   bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (vec_cond_expr));
5254
5255   icode = get_vcond_icode (vec_cond_expr, mode);
5256   if (icode == CODE_FOR_nothing)
5257     return 0;
5258
5259   if (!target)
5260     target = gen_reg_rtx (mode);
5261
5262   /* Get comparison rtx.  First expand both cond expr operands.  */
5263   comparison = vector_compare_rtx (TREE_OPERAND (vec_cond_expr, 0), 
5264                                    unsignedp, icode);
5265   cc_op0 = XEXP (comparison, 0);
5266   cc_op1 = XEXP (comparison, 1);
5267   /* Expand both operands and force them in reg, if required.  */
5268   rtx_op1 = expand_expr (TREE_OPERAND (vec_cond_expr, 1),
5269                          NULL_RTX, VOIDmode, 1);
5270   if (!(*insn_data[icode].operand[1].predicate) (rtx_op1, mode)
5271       && mode != VOIDmode)
5272     rtx_op1 = force_reg (mode, rtx_op1);
5273
5274   rtx_op2 = expand_expr (TREE_OPERAND (vec_cond_expr, 2),
5275                          NULL_RTX, VOIDmode, 1);
5276   if (!(*insn_data[icode].operand[2].predicate) (rtx_op2, mode)
5277       && mode != VOIDmode)
5278     rtx_op2 = force_reg (mode, rtx_op2);
5279
5280   /* Emit instruction! */
5281   emit_insn (GEN_FCN (icode) (target, rtx_op1, rtx_op2, 
5282                               comparison, cc_op0,  cc_op1));
5283
5284   return target;
5285 }
5286 #include "gt-optabs.h"