OSDN Git Service

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