OSDN Git Service

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