OSDN Git Service

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