OSDN Git Service

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