2003-07-04 Toon Moene <toon@moene.indiv.nluug.nl>
[official-gcc.git] / gcc / optabs.c
blobedd4eaeb6d620aacf5e99dc885eac02f3d4e7bc5
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
5 This file is part of GCC.
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.
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.
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. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "toplev.h"
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"
47 /* Each optab contains info on how this target machine
48 can perform a particular operation
49 for all sizes and kinds of operands.
51 The operation to be performed is often specified
52 by passing one of these optabs as an argument.
54 See expr.h for documentation of these optabs. */
56 optab optab_table[OTI_MAX];
58 rtx libfunc_table[LTI_MAX];
60 /* Tables of patterns for extending one integer mode to another. */
61 enum insn_code extendtab[MAX_MACHINE_MODE][MAX_MACHINE_MODE][2];
63 /* Tables of patterns for converting between fixed and floating point. */
64 enum insn_code fixtab[NUM_MACHINE_MODES][NUM_MACHINE_MODES][2];
65 enum insn_code fixtrunctab[NUM_MACHINE_MODES][NUM_MACHINE_MODES][2];
66 enum insn_code floattab[NUM_MACHINE_MODES][NUM_MACHINE_MODES][2];
68 /* Contains the optab used for each rtx code. */
69 optab code_to_optab[NUM_RTX_CODE + 1];
71 /* Indexed by the rtx-code for a conditional (eg. EQ, LT,...)
72 gives the gen_function to make a branch to test that condition. */
74 rtxfun bcc_gen_fctn[NUM_RTX_CODE];
76 /* Indexed by the rtx-code for a conditional (eg. EQ, LT,...)
77 gives the insn code to make a store-condition insn
78 to test that condition. */
80 enum insn_code setcc_gen_code[NUM_RTX_CODE];
82 #ifdef HAVE_conditional_move
83 /* Indexed by the machine mode, gives the insn code to make a conditional
84 move insn. This is not indexed by the rtx-code like bcc_gen_fctn and
85 setcc_gen_code to cut down on the number of named patterns. Consider a day
86 when a lot more rtx codes are conditional (eg: for the ARM). */
88 enum insn_code movcc_gen_code[NUM_MACHINE_MODES];
89 #endif
91 /* The insn generating function can not take an rtx_code argument.
92 TRAP_RTX is used as an rtx argument. Its code is replaced with
93 the code to be used in the trap insn and all other fields are ignored. */
94 static GTY(()) rtx trap_rtx;
96 static int add_equal_note PARAMS ((rtx, rtx, enum rtx_code, rtx, rtx));
97 static rtx widen_operand PARAMS ((rtx, enum machine_mode,
98 enum machine_mode, int, int));
99 static int expand_cmplxdiv_straight PARAMS ((rtx, rtx, rtx, rtx,
100 rtx, rtx, enum machine_mode,
101 int, enum optab_methods,
102 enum mode_class, optab));
103 static int expand_cmplxdiv_wide PARAMS ((rtx, rtx, rtx, rtx,
104 rtx, rtx, enum machine_mode,
105 int, enum optab_methods,
106 enum mode_class, optab));
107 static void prepare_cmp_insn PARAMS ((rtx *, rtx *, enum rtx_code *, rtx,
108 enum machine_mode *, int *,
109 enum can_compare_purpose));
110 static enum insn_code can_fix_p PARAMS ((enum machine_mode, enum machine_mode,
111 int, int *));
112 static enum insn_code can_float_p PARAMS ((enum machine_mode,
113 enum machine_mode,
114 int));
115 static rtx ftruncify PARAMS ((rtx));
116 static optab new_optab PARAMS ((void));
117 static inline optab init_optab PARAMS ((enum rtx_code));
118 static inline optab init_optabv PARAMS ((enum rtx_code));
119 static void init_libfuncs PARAMS ((optab, int, int, const char *, int));
120 static void init_integral_libfuncs PARAMS ((optab, const char *, int));
121 static void init_floating_libfuncs PARAMS ((optab, const char *, int));
122 static void emit_cmp_and_jump_insn_1 PARAMS ((rtx, rtx, enum machine_mode,
123 enum rtx_code, int, rtx));
124 static void prepare_float_lib_cmp PARAMS ((rtx *, rtx *, enum rtx_code *,
125 enum machine_mode *, int *));
126 static rtx expand_vector_binop PARAMS ((enum machine_mode, optab,
127 rtx, rtx, rtx, int,
128 enum optab_methods));
129 static rtx expand_vector_unop PARAMS ((enum machine_mode, optab, rtx, rtx,
130 int));
131 static rtx widen_clz PARAMS ((enum machine_mode, rtx, rtx));
132 static rtx expand_parity PARAMS ((enum machine_mode, rtx, rtx));
134 #ifndef HAVE_conditional_trap
135 #define HAVE_conditional_trap 0
136 #define gen_conditional_trap(a,b) (abort (), NULL_RTX)
137 #endif
139 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
140 the result of operation CODE applied to OP0 (and OP1 if it is a binary
141 operation).
143 If the last insn does not set TARGET, don't do anything, but return 1.
145 If a previous insn sets TARGET and TARGET is one of OP0 or OP1,
146 don't add the REG_EQUAL note but return 0. Our caller can then try
147 again, ensuring that TARGET is not one of the operands. */
149 static int
150 add_equal_note (insns, target, code, op0, op1)
151 rtx insns;
152 rtx target;
153 enum rtx_code code;
154 rtx op0, op1;
156 rtx last_insn, insn, set;
157 rtx note;
159 if (! insns
160 || ! INSN_P (insns)
161 || NEXT_INSN (insns) == NULL_RTX)
162 abort ();
164 if (GET_RTX_CLASS (code) != '1' && GET_RTX_CLASS (code) != '2'
165 && GET_RTX_CLASS (code) != 'c' && GET_RTX_CLASS (code) != '<')
166 return 1;
168 if (GET_CODE (target) == ZERO_EXTRACT)
169 return 1;
171 for (last_insn = insns;
172 NEXT_INSN (last_insn) != NULL_RTX;
173 last_insn = NEXT_INSN (last_insn))
176 set = single_set (last_insn);
177 if (set == NULL_RTX)
178 return 1;
180 if (! rtx_equal_p (SET_DEST (set), target)
181 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
182 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
183 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
184 return 1;
186 /* If TARGET is in OP0 or OP1, check if anything in SEQ sets TARGET
187 besides the last insn. */
188 if (reg_overlap_mentioned_p (target, op0)
189 || (op1 && reg_overlap_mentioned_p (target, op1)))
191 insn = PREV_INSN (last_insn);
192 while (insn != NULL_RTX)
194 if (reg_set_p (target, insn))
195 return 0;
197 insn = PREV_INSN (insn);
201 if (GET_RTX_CLASS (code) == '1')
202 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
203 else
204 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
206 set_unique_reg_note (last_insn, REG_EQUAL, note);
208 return 1;
211 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
212 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
213 not actually do a sign-extend or zero-extend, but can leave the
214 higher-order bits of the result rtx undefined, for example, in the case
215 of logical operations, but not right shifts. */
217 static rtx
218 widen_operand (op, mode, oldmode, unsignedp, no_extend)
219 rtx op;
220 enum machine_mode mode, oldmode;
221 int unsignedp;
222 int no_extend;
224 rtx result;
226 /* If we don't have to extend and this is a constant, return it. */
227 if (no_extend && GET_MODE (op) == VOIDmode)
228 return op;
230 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
231 extend since it will be more efficient to do so unless the signedness of
232 a promoted object differs from our extension. */
233 if (! no_extend
234 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
235 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
236 return convert_modes (mode, oldmode, op, unsignedp);
238 /* If MODE is no wider than a single word, we return a paradoxical
239 SUBREG. */
240 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
241 return gen_rtx_SUBREG (mode, force_reg (GET_MODE (op), op), 0);
243 /* Otherwise, get an object of MODE, clobber it, and set the low-order
244 part to OP. */
246 result = gen_reg_rtx (mode);
247 emit_insn (gen_rtx_CLOBBER (VOIDmode, result));
248 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
249 return result;
252 /* Generate code to perform a straightforward complex divide. */
254 static int
255 expand_cmplxdiv_straight (real0, real1, imag0, imag1, realr, imagr, submode,
256 unsignedp, methods, class, binoptab)
257 rtx real0, real1, imag0, imag1, realr, imagr;
258 enum machine_mode submode;
259 int unsignedp;
260 enum optab_methods methods;
261 enum mode_class class;
262 optab binoptab;
264 rtx divisor;
265 rtx real_t, imag_t;
266 rtx temp1, temp2;
267 rtx res;
268 optab this_add_optab = add_optab;
269 optab this_sub_optab = sub_optab;
270 optab this_neg_optab = neg_optab;
271 optab this_mul_optab = smul_optab;
273 if (binoptab == sdivv_optab)
275 this_add_optab = addv_optab;
276 this_sub_optab = subv_optab;
277 this_neg_optab = negv_optab;
278 this_mul_optab = smulv_optab;
281 /* Don't fetch these from memory more than once. */
282 real0 = force_reg (submode, real0);
283 real1 = force_reg (submode, real1);
285 if (imag0 != 0)
286 imag0 = force_reg (submode, imag0);
288 imag1 = force_reg (submode, imag1);
290 /* Divisor: c*c + d*d. */
291 temp1 = expand_binop (submode, this_mul_optab, real1, real1,
292 NULL_RTX, unsignedp, methods);
294 temp2 = expand_binop (submode, this_mul_optab, imag1, imag1,
295 NULL_RTX, unsignedp, methods);
297 if (temp1 == 0 || temp2 == 0)
298 return 0;
300 divisor = expand_binop (submode, this_add_optab, temp1, temp2,
301 NULL_RTX, unsignedp, methods);
302 if (divisor == 0)
303 return 0;
305 if (imag0 == 0)
307 /* Mathematically, ((a)(c-id))/divisor. */
308 /* Computationally, (a+i0) / (c+id) = (ac/(cc+dd)) + i(-ad/(cc+dd)). */
310 /* Calculate the dividend. */
311 real_t = expand_binop (submode, this_mul_optab, real0, real1,
312 NULL_RTX, unsignedp, methods);
314 imag_t = expand_binop (submode, this_mul_optab, real0, imag1,
315 NULL_RTX, unsignedp, methods);
317 if (real_t == 0 || imag_t == 0)
318 return 0;
320 imag_t = expand_unop (submode, this_neg_optab, imag_t,
321 NULL_RTX, unsignedp);
323 else
325 /* Mathematically, ((a+ib)(c-id))/divider. */
326 /* Calculate the dividend. */
327 temp1 = expand_binop (submode, this_mul_optab, real0, real1,
328 NULL_RTX, unsignedp, methods);
330 temp2 = expand_binop (submode, this_mul_optab, imag0, imag1,
331 NULL_RTX, unsignedp, methods);
333 if (temp1 == 0 || temp2 == 0)
334 return 0;
336 real_t = expand_binop (submode, this_add_optab, temp1, temp2,
337 NULL_RTX, unsignedp, methods);
339 temp1 = expand_binop (submode, this_mul_optab, imag0, real1,
340 NULL_RTX, unsignedp, methods);
342 temp2 = expand_binop (submode, this_mul_optab, real0, imag1,
343 NULL_RTX, unsignedp, methods);
345 if (temp1 == 0 || temp2 == 0)
346 return 0;
348 imag_t = expand_binop (submode, this_sub_optab, temp1, temp2,
349 NULL_RTX, unsignedp, methods);
351 if (real_t == 0 || imag_t == 0)
352 return 0;
355 if (class == MODE_COMPLEX_FLOAT)
356 res = expand_binop (submode, binoptab, real_t, divisor,
357 realr, unsignedp, methods);
358 else
359 res = expand_divmod (0, TRUNC_DIV_EXPR, submode,
360 real_t, divisor, realr, unsignedp);
362 if (res == 0)
363 return 0;
365 if (res != realr)
366 emit_move_insn (realr, res);
368 if (class == MODE_COMPLEX_FLOAT)
369 res = expand_binop (submode, binoptab, imag_t, divisor,
370 imagr, unsignedp, methods);
371 else
372 res = expand_divmod (0, TRUNC_DIV_EXPR, submode,
373 imag_t, divisor, imagr, unsignedp);
375 if (res == 0)
376 return 0;
378 if (res != imagr)
379 emit_move_insn (imagr, res);
381 return 1;
384 /* Generate code to perform a wide-input-range-acceptable complex divide. */
386 static int
387 expand_cmplxdiv_wide (real0, real1, imag0, imag1, realr, imagr, submode,
388 unsignedp, methods, class, binoptab)
389 rtx real0, real1, imag0, imag1, realr, imagr;
390 enum machine_mode submode;
391 int unsignedp;
392 enum optab_methods methods;
393 enum mode_class class;
394 optab binoptab;
396 rtx ratio, divisor;
397 rtx real_t, imag_t;
398 rtx temp1, temp2, lab1, lab2;
399 enum machine_mode mode;
400 rtx res;
401 optab this_add_optab = add_optab;
402 optab this_sub_optab = sub_optab;
403 optab this_neg_optab = neg_optab;
404 optab this_mul_optab = smul_optab;
406 if (binoptab == sdivv_optab)
408 this_add_optab = addv_optab;
409 this_sub_optab = subv_optab;
410 this_neg_optab = negv_optab;
411 this_mul_optab = smulv_optab;
414 /* Don't fetch these from memory more than once. */
415 real0 = force_reg (submode, real0);
416 real1 = force_reg (submode, real1);
418 if (imag0 != 0)
419 imag0 = force_reg (submode, imag0);
421 imag1 = force_reg (submode, imag1);
423 /* XXX What's an "unsigned" complex number? */
424 if (unsignedp)
426 temp1 = real1;
427 temp2 = imag1;
429 else
431 temp1 = expand_abs (submode, real1, NULL_RTX, unsignedp, 1);
432 temp2 = expand_abs (submode, imag1, NULL_RTX, unsignedp, 1);
435 if (temp1 == 0 || temp2 == 0)
436 return 0;
438 mode = GET_MODE (temp1);
439 lab1 = gen_label_rtx ();
440 emit_cmp_and_jump_insns (temp1, temp2, LT, NULL_RTX,
441 mode, unsignedp, lab1);
443 /* |c| >= |d|; use ratio d/c to scale dividend and divisor. */
445 if (class == MODE_COMPLEX_FLOAT)
446 ratio = expand_binop (submode, binoptab, imag1, real1,
447 NULL_RTX, unsignedp, methods);
448 else
449 ratio = expand_divmod (0, TRUNC_DIV_EXPR, submode,
450 imag1, real1, NULL_RTX, unsignedp);
452 if (ratio == 0)
453 return 0;
455 /* Calculate divisor. */
457 temp1 = expand_binop (submode, this_mul_optab, imag1, ratio,
458 NULL_RTX, unsignedp, methods);
460 if (temp1 == 0)
461 return 0;
463 divisor = expand_binop (submode, this_add_optab, temp1, real1,
464 NULL_RTX, unsignedp, methods);
466 if (divisor == 0)
467 return 0;
469 /* Calculate dividend. */
471 if (imag0 == 0)
473 real_t = real0;
475 /* Compute a / (c+id) as a / (c+d(d/c)) + i (-a(d/c)) / (c+d(d/c)). */
477 imag_t = expand_binop (submode, this_mul_optab, real0, ratio,
478 NULL_RTX, unsignedp, methods);
480 if (imag_t == 0)
481 return 0;
483 imag_t = expand_unop (submode, this_neg_optab, imag_t,
484 NULL_RTX, unsignedp);
486 if (real_t == 0 || imag_t == 0)
487 return 0;
489 else
491 /* Compute (a+ib)/(c+id) as
492 (a+b(d/c))/(c+d(d/c) + i(b-a(d/c))/(c+d(d/c)). */
494 temp1 = expand_binop (submode, this_mul_optab, imag0, ratio,
495 NULL_RTX, unsignedp, methods);
497 if (temp1 == 0)
498 return 0;
500 real_t = expand_binop (submode, this_add_optab, temp1, real0,
501 NULL_RTX, unsignedp, methods);
503 temp1 = expand_binop (submode, this_mul_optab, real0, ratio,
504 NULL_RTX, unsignedp, methods);
506 if (temp1 == 0)
507 return 0;
509 imag_t = expand_binop (submode, this_sub_optab, imag0, temp1,
510 NULL_RTX, unsignedp, methods);
512 if (real_t == 0 || imag_t == 0)
513 return 0;
516 if (class == MODE_COMPLEX_FLOAT)
517 res = expand_binop (submode, binoptab, real_t, divisor,
518 realr, unsignedp, methods);
519 else
520 res = expand_divmod (0, TRUNC_DIV_EXPR, submode,
521 real_t, divisor, realr, unsignedp);
523 if (res == 0)
524 return 0;
526 if (res != realr)
527 emit_move_insn (realr, res);
529 if (class == MODE_COMPLEX_FLOAT)
530 res = expand_binop (submode, binoptab, imag_t, divisor,
531 imagr, unsignedp, methods);
532 else
533 res = expand_divmod (0, TRUNC_DIV_EXPR, submode,
534 imag_t, divisor, imagr, unsignedp);
536 if (res == 0)
537 return 0;
539 if (res != imagr)
540 emit_move_insn (imagr, res);
542 lab2 = gen_label_rtx ();
543 emit_jump_insn (gen_jump (lab2));
544 emit_barrier ();
546 emit_label (lab1);
548 /* |d| > |c|; use ratio c/d to scale dividend and divisor. */
550 if (class == MODE_COMPLEX_FLOAT)
551 ratio = expand_binop (submode, binoptab, real1, imag1,
552 NULL_RTX, unsignedp, methods);
553 else
554 ratio = expand_divmod (0, TRUNC_DIV_EXPR, submode,
555 real1, imag1, NULL_RTX, unsignedp);
557 if (ratio == 0)
558 return 0;
560 /* Calculate divisor. */
562 temp1 = expand_binop (submode, this_mul_optab, real1, ratio,
563 NULL_RTX, unsignedp, methods);
565 if (temp1 == 0)
566 return 0;
568 divisor = expand_binop (submode, this_add_optab, temp1, imag1,
569 NULL_RTX, unsignedp, methods);
571 if (divisor == 0)
572 return 0;
574 /* Calculate dividend. */
576 if (imag0 == 0)
578 /* Compute a / (c+id) as a(c/d) / (c(c/d)+d) + i (-a) / (c(c/d)+d). */
580 real_t = expand_binop (submode, this_mul_optab, real0, ratio,
581 NULL_RTX, unsignedp, methods);
583 imag_t = expand_unop (submode, this_neg_optab, real0,
584 NULL_RTX, unsignedp);
586 if (real_t == 0 || imag_t == 0)
587 return 0;
589 else
591 /* Compute (a+ib)/(c+id) as
592 (a(c/d)+b)/(c(c/d)+d) + i (b(c/d)-a)/(c(c/d)+d). */
594 temp1 = expand_binop (submode, this_mul_optab, real0, ratio,
595 NULL_RTX, unsignedp, methods);
597 if (temp1 == 0)
598 return 0;
600 real_t = expand_binop (submode, this_add_optab, temp1, imag0,
601 NULL_RTX, unsignedp, methods);
603 temp1 = expand_binop (submode, this_mul_optab, imag0, ratio,
604 NULL_RTX, unsignedp, methods);
606 if (temp1 == 0)
607 return 0;
609 imag_t = expand_binop (submode, this_sub_optab, temp1, real0,
610 NULL_RTX, unsignedp, methods);
612 if (real_t == 0 || imag_t == 0)
613 return 0;
616 if (class == MODE_COMPLEX_FLOAT)
617 res = expand_binop (submode, binoptab, real_t, divisor,
618 realr, unsignedp, methods);
619 else
620 res = expand_divmod (0, TRUNC_DIV_EXPR, submode,
621 real_t, divisor, realr, unsignedp);
623 if (res == 0)
624 return 0;
626 if (res != realr)
627 emit_move_insn (realr, res);
629 if (class == MODE_COMPLEX_FLOAT)
630 res = expand_binop (submode, binoptab, imag_t, divisor,
631 imagr, unsignedp, methods);
632 else
633 res = expand_divmod (0, TRUNC_DIV_EXPR, submode,
634 imag_t, divisor, imagr, unsignedp);
636 if (res == 0)
637 return 0;
639 if (res != imagr)
640 emit_move_insn (imagr, res);
642 emit_label (lab2);
644 return 1;
647 /* Wrapper around expand_binop which takes an rtx code to specify
648 the operation to perform, not an optab pointer. All other
649 arguments are the same. */
651 expand_simple_binop (mode, code, op0, op1, target, unsignedp, methods)
652 enum machine_mode mode;
653 enum rtx_code code;
654 rtx op0, op1;
655 rtx target;
656 int unsignedp;
657 enum optab_methods methods;
659 optab binop = code_to_optab[(int) code];
660 if (binop == 0)
661 abort ();
663 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
666 /* Generate code to perform an operation specified by BINOPTAB
667 on operands OP0 and OP1, with result having machine-mode MODE.
669 UNSIGNEDP is for the case where we have to widen the operands
670 to perform the operation. It says to use zero-extension.
672 If TARGET is nonzero, the value
673 is generated there, if it is convenient to do so.
674 In all cases an rtx is returned for the locus of the value;
675 this may or may not be TARGET. */
678 expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods)
679 enum machine_mode mode;
680 optab binoptab;
681 rtx op0, op1;
682 rtx target;
683 int unsignedp;
684 enum optab_methods methods;
686 enum optab_methods next_methods
687 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
688 ? OPTAB_WIDEN : methods);
689 enum mode_class class;
690 enum machine_mode wider_mode;
691 rtx temp;
692 int commutative_op = 0;
693 int shift_op = (binoptab->code == ASHIFT
694 || binoptab->code == ASHIFTRT
695 || binoptab->code == LSHIFTRT
696 || binoptab->code == ROTATE
697 || binoptab->code == ROTATERT);
698 rtx entry_last = get_last_insn ();
699 rtx last;
701 class = GET_MODE_CLASS (mode);
703 op0 = protect_from_queue (op0, 0);
704 op1 = protect_from_queue (op1, 0);
705 if (target)
706 target = protect_from_queue (target, 1);
708 if (flag_force_mem)
710 /* Load duplicate non-volatile operands once. */
711 if (rtx_equal_p (op0, op1) && ! volatile_refs_p (op0))
713 op0 = force_not_mem (op0);
714 op1 = op0;
716 else
718 op0 = force_not_mem (op0);
719 op1 = force_not_mem (op1);
723 /* If subtracting an integer constant, convert this into an addition of
724 the negated constant. */
726 if (binoptab == sub_optab && GET_CODE (op1) == CONST_INT)
728 op1 = negate_rtx (mode, op1);
729 binoptab = add_optab;
732 /* If we are inside an appropriately-short loop and one operand is an
733 expensive constant, force it into a register. */
734 if (CONSTANT_P (op0) && preserve_subexpressions_p ()
735 && rtx_cost (op0, binoptab->code) > COSTS_N_INSNS (1))
736 op0 = force_reg (mode, op0);
738 if (CONSTANT_P (op1) && preserve_subexpressions_p ()
739 && ! shift_op && rtx_cost (op1, binoptab->code) > COSTS_N_INSNS (1))
740 op1 = force_reg (mode, op1);
742 /* Record where to delete back to if we backtrack. */
743 last = get_last_insn ();
745 /* If operation is commutative,
746 try to make the first operand a register.
747 Even better, try to make it the same as the target.
748 Also try to make the last operand a constant. */
749 if (GET_RTX_CLASS (binoptab->code) == 'c'
750 || binoptab == smul_widen_optab
751 || binoptab == umul_widen_optab
752 || binoptab == smul_highpart_optab
753 || binoptab == umul_highpart_optab)
755 commutative_op = 1;
757 if (((target == 0 || GET_CODE (target) == REG)
758 ? ((GET_CODE (op1) == REG
759 && GET_CODE (op0) != REG)
760 || target == op1)
761 : rtx_equal_p (op1, target))
762 || GET_CODE (op0) == CONST_INT)
764 temp = op1;
765 op1 = op0;
766 op0 = temp;
770 /* If we can do it with a three-operand insn, do so. */
772 if (methods != OPTAB_MUST_WIDEN
773 && binoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
775 int icode = (int) binoptab->handlers[(int) mode].insn_code;
776 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
777 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
778 rtx pat;
779 rtx xop0 = op0, xop1 = op1;
781 if (target)
782 temp = target;
783 else
784 temp = gen_reg_rtx (mode);
786 /* If it is a commutative operator and the modes would match
787 if we would swap the operands, we can save the conversions. */
788 if (commutative_op)
790 if (GET_MODE (op0) != mode0 && GET_MODE (op1) != mode1
791 && GET_MODE (op0) == mode1 && GET_MODE (op1) == mode0)
793 rtx tmp;
795 tmp = op0; op0 = op1; op1 = tmp;
796 tmp = xop0; xop0 = xop1; xop1 = tmp;
800 /* In case the insn wants input operands in modes different from
801 those of the actual operands, convert the operands. It would
802 seem that we don't need to convert CONST_INTs, but we do, so
803 that they're properly zero-extended, sign-extended or truncated
804 for their mode. */
806 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
807 xop0 = convert_modes (mode0,
808 GET_MODE (op0) != VOIDmode
809 ? GET_MODE (op0)
810 : mode,
811 xop0, unsignedp);
813 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
814 xop1 = convert_modes (mode1,
815 GET_MODE (op1) != VOIDmode
816 ? GET_MODE (op1)
817 : mode,
818 xop1, unsignedp);
820 /* Now, if insn's predicates don't allow our operands, put them into
821 pseudo regs. */
823 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0)
824 && mode0 != VOIDmode)
825 xop0 = copy_to_mode_reg (mode0, xop0);
827 if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1)
828 && mode1 != VOIDmode)
829 xop1 = copy_to_mode_reg (mode1, xop1);
831 if (! (*insn_data[icode].operand[0].predicate) (temp, mode))
832 temp = gen_reg_rtx (mode);
834 pat = GEN_FCN (icode) (temp, xop0, xop1);
835 if (pat)
837 /* If PAT is composed of more than one insn, try to add an appropriate
838 REG_EQUAL note to it. If we can't because TEMP conflicts with an
839 operand, call ourselves again, this time without a target. */
840 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
841 && ! add_equal_note (pat, temp, binoptab->code, xop0, xop1))
843 delete_insns_since (last);
844 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
845 unsignedp, methods);
848 emit_insn (pat);
849 return temp;
851 else
852 delete_insns_since (last);
855 /* If this is a multiply, see if we can do a widening operation that
856 takes operands of this mode and makes a wider mode. */
858 if (binoptab == smul_optab && GET_MODE_WIDER_MODE (mode) != VOIDmode
859 && (((unsignedp ? umul_widen_optab : smul_widen_optab)
860 ->handlers[(int) GET_MODE_WIDER_MODE (mode)].insn_code)
861 != CODE_FOR_nothing))
863 temp = expand_binop (GET_MODE_WIDER_MODE (mode),
864 unsignedp ? umul_widen_optab : smul_widen_optab,
865 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
867 if (temp != 0)
869 if (GET_MODE_CLASS (mode) == MODE_INT)
870 return gen_lowpart (mode, temp);
871 else
872 return convert_to_mode (mode, temp, unsignedp);
876 /* Look for a wider mode of the same class for which we think we
877 can open-code the operation. Check for a widening multiply at the
878 wider mode as well. */
880 if ((class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
881 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
882 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
883 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
885 if (binoptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
886 || (binoptab == smul_optab
887 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
888 && (((unsignedp ? umul_widen_optab : smul_widen_optab)
889 ->handlers[(int) GET_MODE_WIDER_MODE (wider_mode)].insn_code)
890 != CODE_FOR_nothing)))
892 rtx xop0 = op0, xop1 = op1;
893 int no_extend = 0;
895 /* For certain integer operations, we need not actually extend
896 the narrow operands, as long as we will truncate
897 the results to the same narrowness. */
899 if ((binoptab == ior_optab || binoptab == and_optab
900 || binoptab == xor_optab
901 || binoptab == add_optab || binoptab == sub_optab
902 || binoptab == smul_optab || binoptab == ashl_optab)
903 && class == MODE_INT)
904 no_extend = 1;
906 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
908 /* The second operand of a shift must always be extended. */
909 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
910 no_extend && binoptab != ashl_optab);
912 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
913 unsignedp, OPTAB_DIRECT);
914 if (temp)
916 if (class != MODE_INT)
918 if (target == 0)
919 target = gen_reg_rtx (mode);
920 convert_move (target, temp, 0);
921 return target;
923 else
924 return gen_lowpart (mode, temp);
926 else
927 delete_insns_since (last);
931 /* These can be done a word at a time. */
932 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
933 && class == MODE_INT
934 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
935 && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
937 int i;
938 rtx insns;
939 rtx equiv_value;
941 /* If TARGET is the same as one of the operands, the REG_EQUAL note
942 won't be accurate, so use a new target. */
943 if (target == 0 || target == op0 || target == op1)
944 target = gen_reg_rtx (mode);
946 start_sequence ();
948 /* Do the actual arithmetic. */
949 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
951 rtx target_piece = operand_subword (target, i, 1, mode);
952 rtx x = expand_binop (word_mode, binoptab,
953 operand_subword_force (op0, i, mode),
954 operand_subword_force (op1, i, mode),
955 target_piece, unsignedp, next_methods);
957 if (x == 0)
958 break;
960 if (target_piece != x)
961 emit_move_insn (target_piece, x);
964 insns = get_insns ();
965 end_sequence ();
967 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
969 if (binoptab->code != UNKNOWN)
970 equiv_value
971 = gen_rtx_fmt_ee (binoptab->code, mode,
972 copy_rtx (op0), copy_rtx (op1));
973 else
974 equiv_value = 0;
976 emit_no_conflict_block (insns, target, op0, op1, equiv_value);
977 return target;
981 /* Synthesize double word shifts from single word shifts. */
982 if ((binoptab == lshr_optab || binoptab == ashl_optab
983 || binoptab == ashr_optab)
984 && class == MODE_INT
985 && GET_CODE (op1) == CONST_INT
986 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
987 && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
988 && ashl_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
989 && lshr_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
991 rtx insns, inter, equiv_value;
992 rtx into_target, outof_target;
993 rtx into_input, outof_input;
994 int shift_count, left_shift, outof_word;
996 /* If TARGET is the same as one of the operands, the REG_EQUAL note
997 won't be accurate, so use a new target. */
998 if (target == 0 || target == op0 || target == op1)
999 target = gen_reg_rtx (mode);
1001 start_sequence ();
1003 shift_count = INTVAL (op1);
1005 /* OUTOF_* is the word we are shifting bits away from, and
1006 INTO_* is the word that we are shifting bits towards, thus
1007 they differ depending on the direction of the shift and
1008 WORDS_BIG_ENDIAN. */
1010 left_shift = binoptab == ashl_optab;
1011 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1013 outof_target = operand_subword (target, outof_word, 1, mode);
1014 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1016 outof_input = operand_subword_force (op0, outof_word, mode);
1017 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1019 if (shift_count >= BITS_PER_WORD)
1021 inter = expand_binop (word_mode, binoptab,
1022 outof_input,
1023 GEN_INT (shift_count - BITS_PER_WORD),
1024 into_target, unsignedp, next_methods);
1026 if (inter != 0 && inter != into_target)
1027 emit_move_insn (into_target, inter);
1029 /* For a signed right shift, we must fill the word we are shifting
1030 out of with copies of the sign bit. Otherwise it is zeroed. */
1031 if (inter != 0 && binoptab != ashr_optab)
1032 inter = CONST0_RTX (word_mode);
1033 else if (inter != 0)
1034 inter = expand_binop (word_mode, binoptab,
1035 outof_input,
1036 GEN_INT (BITS_PER_WORD - 1),
1037 outof_target, unsignedp, next_methods);
1039 if (inter != 0 && inter != outof_target)
1040 emit_move_insn (outof_target, inter);
1042 else
1044 rtx carries;
1045 optab reverse_unsigned_shift, unsigned_shift;
1047 /* For a shift of less then BITS_PER_WORD, to compute the carry,
1048 we must do a logical shift in the opposite direction of the
1049 desired shift. */
1051 reverse_unsigned_shift = (left_shift ? lshr_optab : ashl_optab);
1053 /* For a shift of less than BITS_PER_WORD, to compute the word
1054 shifted towards, we need to unsigned shift the orig value of
1055 that word. */
1057 unsigned_shift = (left_shift ? ashl_optab : lshr_optab);
1059 carries = expand_binop (word_mode, reverse_unsigned_shift,
1060 outof_input,
1061 GEN_INT (BITS_PER_WORD - shift_count),
1062 0, unsignedp, next_methods);
1064 if (carries == 0)
1065 inter = 0;
1066 else
1067 inter = expand_binop (word_mode, unsigned_shift, into_input,
1068 op1, 0, unsignedp, next_methods);
1070 if (inter != 0)
1071 inter = expand_binop (word_mode, ior_optab, carries, inter,
1072 into_target, unsignedp, next_methods);
1074 if (inter != 0 && inter != into_target)
1075 emit_move_insn (into_target, inter);
1077 if (inter != 0)
1078 inter = expand_binop (word_mode, binoptab, outof_input,
1079 op1, outof_target, unsignedp, next_methods);
1081 if (inter != 0 && inter != outof_target)
1082 emit_move_insn (outof_target, inter);
1085 insns = get_insns ();
1086 end_sequence ();
1088 if (inter != 0)
1090 if (binoptab->code != UNKNOWN)
1091 equiv_value = gen_rtx_fmt_ee (binoptab->code, mode, op0, op1);
1092 else
1093 equiv_value = 0;
1095 emit_no_conflict_block (insns, target, op0, op1, equiv_value);
1096 return target;
1100 /* Synthesize double word rotates from single word shifts. */
1101 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1102 && class == MODE_INT
1103 && GET_CODE (op1) == CONST_INT
1104 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1105 && ashl_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1106 && lshr_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1108 rtx insns, equiv_value;
1109 rtx into_target, outof_target;
1110 rtx into_input, outof_input;
1111 rtx inter;
1112 int shift_count, left_shift, outof_word;
1114 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1115 won't be accurate, so use a new target. */
1116 if (target == 0 || target == op0 || target == op1)
1117 target = gen_reg_rtx (mode);
1119 start_sequence ();
1121 shift_count = INTVAL (op1);
1123 /* OUTOF_* is the word we are shifting bits away from, and
1124 INTO_* is the word that we are shifting bits towards, thus
1125 they differ depending on the direction of the shift and
1126 WORDS_BIG_ENDIAN. */
1128 left_shift = (binoptab == rotl_optab);
1129 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1131 outof_target = operand_subword (target, outof_word, 1, mode);
1132 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1134 outof_input = operand_subword_force (op0, outof_word, mode);
1135 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1137 if (shift_count == BITS_PER_WORD)
1139 /* This is just a word swap. */
1140 emit_move_insn (outof_target, into_input);
1141 emit_move_insn (into_target, outof_input);
1142 inter = const0_rtx;
1144 else
1146 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1147 rtx first_shift_count, second_shift_count;
1148 optab reverse_unsigned_shift, unsigned_shift;
1150 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1151 ? lshr_optab : ashl_optab);
1153 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1154 ? ashl_optab : lshr_optab);
1156 if (shift_count > BITS_PER_WORD)
1158 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1159 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1161 else
1163 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1164 second_shift_count = GEN_INT (shift_count);
1167 into_temp1 = expand_binop (word_mode, unsigned_shift,
1168 outof_input, first_shift_count,
1169 NULL_RTX, unsignedp, next_methods);
1170 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1171 into_input, second_shift_count,
1172 NULL_RTX, unsignedp, next_methods);
1174 if (into_temp1 != 0 && into_temp2 != 0)
1175 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1176 into_target, unsignedp, next_methods);
1177 else
1178 inter = 0;
1180 if (inter != 0 && inter != into_target)
1181 emit_move_insn (into_target, inter);
1183 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1184 into_input, first_shift_count,
1185 NULL_RTX, unsignedp, next_methods);
1186 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1187 outof_input, second_shift_count,
1188 NULL_RTX, unsignedp, next_methods);
1190 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1191 inter = expand_binop (word_mode, ior_optab,
1192 outof_temp1, outof_temp2,
1193 outof_target, unsignedp, next_methods);
1195 if (inter != 0 && inter != outof_target)
1196 emit_move_insn (outof_target, inter);
1199 insns = get_insns ();
1200 end_sequence ();
1202 if (inter != 0)
1204 if (binoptab->code != UNKNOWN)
1205 equiv_value = gen_rtx_fmt_ee (binoptab->code, mode, op0, op1);
1206 else
1207 equiv_value = 0;
1209 /* We can't make this a no conflict block if this is a word swap,
1210 because the word swap case fails if the input and output values
1211 are in the same register. */
1212 if (shift_count != BITS_PER_WORD)
1213 emit_no_conflict_block (insns, target, op0, op1, equiv_value);
1214 else
1215 emit_insn (insns);
1218 return target;
1222 /* These can be done a word at a time by propagating carries. */
1223 if ((binoptab == add_optab || binoptab == sub_optab)
1224 && class == MODE_INT
1225 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1226 && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1228 unsigned int i;
1229 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1230 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1231 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1232 rtx xop0, xop1, xtarget;
1234 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1235 value is one of those, use it. Otherwise, use 1 since it is the
1236 one easiest to get. */
1237 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1238 int normalizep = STORE_FLAG_VALUE;
1239 #else
1240 int normalizep = 1;
1241 #endif
1243 /* Prepare the operands. */
1244 xop0 = force_reg (mode, op0);
1245 xop1 = force_reg (mode, op1);
1247 xtarget = gen_reg_rtx (mode);
1249 if (target == 0 || GET_CODE (target) != REG)
1250 target = xtarget;
1252 /* Indicate for flow that the entire target reg is being set. */
1253 if (GET_CODE (target) == REG)
1254 emit_insn (gen_rtx_CLOBBER (VOIDmode, xtarget));
1256 /* Do the actual arithmetic. */
1257 for (i = 0; i < nwords; i++)
1259 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1260 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1261 rtx op0_piece = operand_subword_force (xop0, index, mode);
1262 rtx op1_piece = operand_subword_force (xop1, index, mode);
1263 rtx x;
1265 /* Main add/subtract of the input operands. */
1266 x = expand_binop (word_mode, binoptab,
1267 op0_piece, op1_piece,
1268 target_piece, unsignedp, next_methods);
1269 if (x == 0)
1270 break;
1272 if (i + 1 < nwords)
1274 /* Store carry from main add/subtract. */
1275 carry_out = gen_reg_rtx (word_mode);
1276 carry_out = emit_store_flag_force (carry_out,
1277 (binoptab == add_optab
1278 ? LT : GT),
1279 x, op0_piece,
1280 word_mode, 1, normalizep);
1283 if (i > 0)
1285 rtx newx;
1287 /* Add/subtract previous carry to main result. */
1288 newx = expand_binop (word_mode,
1289 normalizep == 1 ? binoptab : otheroptab,
1290 x, carry_in,
1291 NULL_RTX, 1, next_methods);
1293 if (i + 1 < nwords)
1295 /* Get out carry from adding/subtracting carry in. */
1296 rtx carry_tmp = gen_reg_rtx (word_mode);
1297 carry_tmp = emit_store_flag_force (carry_tmp,
1298 (binoptab == add_optab
1299 ? LT : GT),
1300 newx, x,
1301 word_mode, 1, normalizep);
1303 /* Logical-ior the two poss. carry together. */
1304 carry_out = expand_binop (word_mode, ior_optab,
1305 carry_out, carry_tmp,
1306 carry_out, 0, next_methods);
1307 if (carry_out == 0)
1308 break;
1310 emit_move_insn (target_piece, newx);
1313 carry_in = carry_out;
1316 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
1318 if (mov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
1319 || ! rtx_equal_p (target, xtarget))
1321 rtx temp = emit_move_insn (target, xtarget);
1323 set_unique_reg_note (temp,
1324 REG_EQUAL,
1325 gen_rtx_fmt_ee (binoptab->code, mode,
1326 copy_rtx (xop0),
1327 copy_rtx (xop1)));
1329 else
1330 target = xtarget;
1332 return target;
1335 else
1336 delete_insns_since (last);
1339 /* If we want to multiply two two-word values and have normal and widening
1340 multiplies of single-word values, we can do this with three smaller
1341 multiplications. Note that we do not make a REG_NO_CONFLICT block here
1342 because we are not operating on one word at a time.
1344 The multiplication proceeds as follows:
1345 _______________________
1346 [__op0_high_|__op0_low__]
1347 _______________________
1348 * [__op1_high_|__op1_low__]
1349 _______________________________________________
1350 _______________________
1351 (1) [__op0_low__*__op1_low__]
1352 _______________________
1353 (2a) [__op0_low__*__op1_high_]
1354 _______________________
1355 (2b) [__op0_high_*__op1_low__]
1356 _______________________
1357 (3) [__op0_high_*__op1_high_]
1360 This gives a 4-word result. Since we are only interested in the
1361 lower 2 words, partial result (3) and the upper words of (2a) and
1362 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1363 calculated using non-widening multiplication.
1365 (1), however, needs to be calculated with an unsigned widening
1366 multiplication. If this operation is not directly supported we
1367 try using a signed widening multiplication and adjust the result.
1368 This adjustment works as follows:
1370 If both operands are positive then no adjustment is needed.
1372 If the operands have different signs, for example op0_low < 0 and
1373 op1_low >= 0, the instruction treats the most significant bit of
1374 op0_low as a sign bit instead of a bit with significance
1375 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1376 with 2**BITS_PER_WORD - op0_low, and two's complements the
1377 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1378 the result.
1380 Similarly, if both operands are negative, we need to add
1381 (op0_low + op1_low) * 2**BITS_PER_WORD.
1383 We use a trick to adjust quickly. We logically shift op0_low right
1384 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1385 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1386 logical shift exists, we do an arithmetic right shift and subtract
1387 the 0 or -1. */
1389 if (binoptab == smul_optab
1390 && class == MODE_INT
1391 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1392 && smul_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1393 && add_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1394 && ((umul_widen_optab->handlers[(int) mode].insn_code
1395 != CODE_FOR_nothing)
1396 || (smul_widen_optab->handlers[(int) mode].insn_code
1397 != CODE_FOR_nothing)))
1399 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1400 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1401 rtx op0_high = operand_subword_force (op0, high, mode);
1402 rtx op0_low = operand_subword_force (op0, low, mode);
1403 rtx op1_high = operand_subword_force (op1, high, mode);
1404 rtx op1_low = operand_subword_force (op1, low, mode);
1405 rtx product = 0;
1406 rtx op0_xhigh = NULL_RTX;
1407 rtx op1_xhigh = NULL_RTX;
1409 /* If the target is the same as one of the inputs, don't use it. This
1410 prevents problems with the REG_EQUAL note. */
1411 if (target == op0 || target == op1
1412 || (target != 0 && GET_CODE (target) != REG))
1413 target = 0;
1415 /* Multiply the two lower words to get a double-word product.
1416 If unsigned widening multiplication is available, use that;
1417 otherwise use the signed form and compensate. */
1419 if (umul_widen_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1421 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1422 target, 1, OPTAB_DIRECT);
1424 /* If we didn't succeed, delete everything we did so far. */
1425 if (product == 0)
1426 delete_insns_since (last);
1427 else
1428 op0_xhigh = op0_high, op1_xhigh = op1_high;
1431 if (product == 0
1432 && smul_widen_optab->handlers[(int) mode].insn_code
1433 != CODE_FOR_nothing)
1435 rtx wordm1 = GEN_INT (BITS_PER_WORD - 1);
1436 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1437 target, 1, OPTAB_DIRECT);
1438 op0_xhigh = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1439 NULL_RTX, 1, next_methods);
1440 if (op0_xhigh)
1441 op0_xhigh = expand_binop (word_mode, add_optab, op0_high,
1442 op0_xhigh, op0_xhigh, 0, next_methods);
1443 else
1445 op0_xhigh = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1446 NULL_RTX, 0, next_methods);
1447 if (op0_xhigh)
1448 op0_xhigh = expand_binop (word_mode, sub_optab, op0_high,
1449 op0_xhigh, op0_xhigh, 0,
1450 next_methods);
1453 op1_xhigh = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1454 NULL_RTX, 1, next_methods);
1455 if (op1_xhigh)
1456 op1_xhigh = expand_binop (word_mode, add_optab, op1_high,
1457 op1_xhigh, op1_xhigh, 0, next_methods);
1458 else
1460 op1_xhigh = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1461 NULL_RTX, 0, next_methods);
1462 if (op1_xhigh)
1463 op1_xhigh = expand_binop (word_mode, sub_optab, op1_high,
1464 op1_xhigh, op1_xhigh, 0,
1465 next_methods);
1469 /* If we have been able to directly compute the product of the
1470 low-order words of the operands and perform any required adjustments
1471 of the operands, we proceed by trying two more multiplications
1472 and then computing the appropriate sum.
1474 We have checked above that the required addition is provided.
1475 Full-word addition will normally always succeed, especially if
1476 it is provided at all, so we don't worry about its failure. The
1477 multiplication may well fail, however, so we do handle that. */
1479 if (product && op0_xhigh && op1_xhigh)
1481 rtx product_high = operand_subword (product, high, 1, mode);
1482 rtx temp = expand_binop (word_mode, binoptab, op0_low, op1_xhigh,
1483 NULL_RTX, 0, OPTAB_DIRECT);
1485 if (!REG_P (product_high))
1486 product_high = force_reg (word_mode, product_high);
1488 if (temp != 0)
1489 temp = expand_binop (word_mode, add_optab, temp, product_high,
1490 product_high, 0, next_methods);
1492 if (temp != 0 && temp != product_high)
1493 emit_move_insn (product_high, temp);
1495 if (temp != 0)
1496 temp = expand_binop (word_mode, binoptab, op1_low, op0_xhigh,
1497 NULL_RTX, 0, OPTAB_DIRECT);
1499 if (temp != 0)
1500 temp = expand_binop (word_mode, add_optab, temp,
1501 product_high, product_high,
1502 0, next_methods);
1504 if (temp != 0 && temp != product_high)
1505 emit_move_insn (product_high, temp);
1507 emit_move_insn (operand_subword (product, high, 1, mode), product_high);
1509 if (temp != 0)
1511 if (mov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1513 temp = emit_move_insn (product, product);
1514 set_unique_reg_note (temp,
1515 REG_EQUAL,
1516 gen_rtx_fmt_ee (MULT, mode,
1517 copy_rtx (op0),
1518 copy_rtx (op1)));
1521 return product;
1525 /* If we get here, we couldn't do it for some reason even though we
1526 originally thought we could. Delete anything we've emitted in
1527 trying to do it. */
1529 delete_insns_since (last);
1532 /* Open-code the vector operations if we have no hardware support
1533 for them. */
1534 if (class == MODE_VECTOR_INT || class == MODE_VECTOR_FLOAT)
1535 return expand_vector_binop (mode, binoptab, op0, op1, target,
1536 unsignedp, methods);
1538 /* We need to open-code the complex type operations: '+, -, * and /' */
1540 /* At this point we allow operations between two similar complex
1541 numbers, and also if one of the operands is not a complex number
1542 but rather of MODE_FLOAT or MODE_INT. However, the caller
1543 must make sure that the MODE of the non-complex operand matches
1544 the SUBMODE of the complex operand. */
1546 if (class == MODE_COMPLEX_FLOAT || class == MODE_COMPLEX_INT)
1548 rtx real0 = 0, imag0 = 0;
1549 rtx real1 = 0, imag1 = 0;
1550 rtx realr, imagr, res;
1551 rtx seq;
1552 rtx equiv_value;
1553 int ok = 0;
1555 /* Find the correct mode for the real and imaginary parts. */
1556 enum machine_mode submode = GET_MODE_INNER(mode);
1558 if (submode == BLKmode)
1559 abort ();
1561 if (! target)
1562 target = gen_reg_rtx (mode);
1564 start_sequence ();
1566 realr = gen_realpart (submode, target);
1567 imagr = gen_imagpart (submode, target);
1569 if (GET_MODE (op0) == mode)
1571 real0 = gen_realpart (submode, op0);
1572 imag0 = gen_imagpart (submode, op0);
1574 else
1575 real0 = op0;
1577 if (GET_MODE (op1) == mode)
1579 real1 = gen_realpart (submode, op1);
1580 imag1 = gen_imagpart (submode, op1);
1582 else
1583 real1 = op1;
1585 if (real0 == 0 || real1 == 0 || ! (imag0 != 0 || imag1 != 0))
1586 abort ();
1588 switch (binoptab->code)
1590 case PLUS:
1591 /* (a+ib) + (c+id) = (a+c) + i(b+d) */
1592 case MINUS:
1593 /* (a+ib) - (c+id) = (a-c) + i(b-d) */
1594 res = expand_binop (submode, binoptab, real0, real1,
1595 realr, unsignedp, methods);
1597 if (res == 0)
1598 break;
1599 else if (res != realr)
1600 emit_move_insn (realr, res);
1602 if (imag0 != 0 && imag1 != 0)
1603 res = expand_binop (submode, binoptab, imag0, imag1,
1604 imagr, unsignedp, methods);
1605 else if (imag0 != 0)
1606 res = imag0;
1607 else if (binoptab->code == MINUS)
1608 res = expand_unop (submode,
1609 binoptab == subv_optab ? negv_optab : neg_optab,
1610 imag1, imagr, unsignedp);
1611 else
1612 res = imag1;
1614 if (res == 0)
1615 break;
1616 else if (res != imagr)
1617 emit_move_insn (imagr, res);
1619 ok = 1;
1620 break;
1622 case MULT:
1623 /* (a+ib) * (c+id) = (ac-bd) + i(ad+cb) */
1625 if (imag0 != 0 && imag1 != 0)
1627 rtx temp1, temp2;
1629 /* Don't fetch these from memory more than once. */
1630 real0 = force_reg (submode, real0);
1631 real1 = force_reg (submode, real1);
1632 imag0 = force_reg (submode, imag0);
1633 imag1 = force_reg (submode, imag1);
1635 temp1 = expand_binop (submode, binoptab, real0, real1, NULL_RTX,
1636 unsignedp, methods);
1638 temp2 = expand_binop (submode, binoptab, imag0, imag1, NULL_RTX,
1639 unsignedp, methods);
1641 if (temp1 == 0 || temp2 == 0)
1642 break;
1644 res = (expand_binop
1645 (submode,
1646 binoptab == smulv_optab ? subv_optab : sub_optab,
1647 temp1, temp2, realr, unsignedp, methods));
1649 if (res == 0)
1650 break;
1651 else if (res != realr)
1652 emit_move_insn (realr, res);
1654 temp1 = expand_binop (submode, binoptab, real0, imag1,
1655 NULL_RTX, unsignedp, methods);
1657 /* Avoid expanding redundant multiplication for the common
1658 case of squaring a complex number. */
1659 if (rtx_equal_p (real0, real1) && rtx_equal_p (imag0, imag1))
1660 temp2 = temp1;
1661 else
1662 temp2 = expand_binop (submode, binoptab, real1, imag0,
1663 NULL_RTX, unsignedp, methods);
1665 if (temp1 == 0 || temp2 == 0)
1666 break;
1668 res = (expand_binop
1669 (submode,
1670 binoptab == smulv_optab ? addv_optab : add_optab,
1671 temp1, temp2, imagr, unsignedp, methods));
1673 if (res == 0)
1674 break;
1675 else if (res != imagr)
1676 emit_move_insn (imagr, res);
1678 ok = 1;
1680 else
1682 /* Don't fetch these from memory more than once. */
1683 real0 = force_reg (submode, real0);
1684 real1 = force_reg (submode, real1);
1686 res = expand_binop (submode, binoptab, real0, real1,
1687 realr, unsignedp, methods);
1688 if (res == 0)
1689 break;
1690 else if (res != realr)
1691 emit_move_insn (realr, res);
1693 if (imag0 != 0)
1694 res = expand_binop (submode, binoptab,
1695 real1, imag0, imagr, unsignedp, methods);
1696 else
1697 res = expand_binop (submode, binoptab,
1698 real0, imag1, imagr, unsignedp, methods);
1700 if (res == 0)
1701 break;
1702 else if (res != imagr)
1703 emit_move_insn (imagr, res);
1705 ok = 1;
1707 break;
1709 case DIV:
1710 /* (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd)) */
1712 if (imag1 == 0)
1714 /* (a+ib) / (c+i0) = (a/c) + i(b/c) */
1716 /* Don't fetch these from memory more than once. */
1717 real1 = force_reg (submode, real1);
1719 /* Simply divide the real and imaginary parts by `c' */
1720 if (class == MODE_COMPLEX_FLOAT)
1721 res = expand_binop (submode, binoptab, real0, real1,
1722 realr, unsignedp, methods);
1723 else
1724 res = expand_divmod (0, TRUNC_DIV_EXPR, submode,
1725 real0, real1, realr, unsignedp);
1727 if (res == 0)
1728 break;
1729 else if (res != realr)
1730 emit_move_insn (realr, res);
1732 if (class == MODE_COMPLEX_FLOAT)
1733 res = expand_binop (submode, binoptab, imag0, real1,
1734 imagr, unsignedp, methods);
1735 else
1736 res = expand_divmod (0, TRUNC_DIV_EXPR, submode,
1737 imag0, real1, imagr, unsignedp);
1739 if (res == 0)
1740 break;
1741 else if (res != imagr)
1742 emit_move_insn (imagr, res);
1744 ok = 1;
1746 else
1748 switch (flag_complex_divide_method)
1750 case 0:
1751 ok = expand_cmplxdiv_straight (real0, real1, imag0, imag1,
1752 realr, imagr, submode,
1753 unsignedp, methods,
1754 class, binoptab);
1755 break;
1757 case 1:
1758 ok = expand_cmplxdiv_wide (real0, real1, imag0, imag1,
1759 realr, imagr, submode,
1760 unsignedp, methods,
1761 class, binoptab);
1762 break;
1764 default:
1765 abort ();
1768 break;
1770 default:
1771 abort ();
1774 seq = get_insns ();
1775 end_sequence ();
1777 if (ok)
1779 if (binoptab->code != UNKNOWN)
1780 equiv_value
1781 = gen_rtx_fmt_ee (binoptab->code, mode,
1782 copy_rtx (op0), copy_rtx (op1));
1783 else
1784 equiv_value = 0;
1786 emit_no_conflict_block (seq, target, op0, op1, equiv_value);
1788 return target;
1792 /* It can't be open-coded in this mode.
1793 Use a library call if one is available and caller says that's ok. */
1795 if (binoptab->handlers[(int) mode].libfunc
1796 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
1798 rtx insns;
1799 rtx op1x = op1;
1800 enum machine_mode op1_mode = mode;
1801 rtx value;
1803 start_sequence ();
1805 if (shift_op)
1807 op1_mode = word_mode;
1808 /* Specify unsigned here,
1809 since negative shift counts are meaningless. */
1810 op1x = convert_to_mode (word_mode, op1, 1);
1813 if (GET_MODE (op0) != VOIDmode
1814 && GET_MODE (op0) != mode)
1815 op0 = convert_to_mode (mode, op0, unsignedp);
1817 /* Pass 1 for NO_QUEUE so we don't lose any increments
1818 if the libcall is cse'd or moved. */
1819 value = emit_library_call_value (binoptab->handlers[(int) mode].libfunc,
1820 NULL_RTX, LCT_CONST, mode, 2,
1821 op0, mode, op1x, op1_mode);
1823 insns = get_insns ();
1824 end_sequence ();
1826 target = gen_reg_rtx (mode);
1827 emit_libcall_block (insns, target, value,
1828 gen_rtx_fmt_ee (binoptab->code, mode, op0, op1));
1830 return target;
1833 delete_insns_since (last);
1835 /* It can't be done in this mode. Can we do it in a wider mode? */
1837 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
1838 || methods == OPTAB_MUST_WIDEN))
1840 /* Caller says, don't even try. */
1841 delete_insns_since (entry_last);
1842 return 0;
1845 /* Compute the value of METHODS to pass to recursive calls.
1846 Don't allow widening to be tried recursively. */
1848 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
1850 /* Look for a wider mode of the same class for which it appears we can do
1851 the operation. */
1853 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1855 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1856 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1858 if ((binoptab->handlers[(int) wider_mode].insn_code
1859 != CODE_FOR_nothing)
1860 || (methods == OPTAB_LIB
1861 && binoptab->handlers[(int) wider_mode].libfunc))
1863 rtx xop0 = op0, xop1 = op1;
1864 int no_extend = 0;
1866 /* For certain integer operations, we need not actually extend
1867 the narrow operands, as long as we will truncate
1868 the results to the same narrowness. */
1870 if ((binoptab == ior_optab || binoptab == and_optab
1871 || binoptab == xor_optab
1872 || binoptab == add_optab || binoptab == sub_optab
1873 || binoptab == smul_optab || binoptab == ashl_optab)
1874 && class == MODE_INT)
1875 no_extend = 1;
1877 xop0 = widen_operand (xop0, wider_mode, mode,
1878 unsignedp, no_extend);
1880 /* The second operand of a shift must always be extended. */
1881 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1882 no_extend && binoptab != ashl_optab);
1884 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1885 unsignedp, methods);
1886 if (temp)
1888 if (class != MODE_INT)
1890 if (target == 0)
1891 target = gen_reg_rtx (mode);
1892 convert_move (target, temp, 0);
1893 return target;
1895 else
1896 return gen_lowpart (mode, temp);
1898 else
1899 delete_insns_since (last);
1904 delete_insns_since (entry_last);
1905 return 0;
1908 /* Like expand_binop, but for open-coding vectors binops. */
1910 static rtx
1911 expand_vector_binop (mode, binoptab, op0, op1, target, unsignedp, methods)
1912 enum machine_mode mode;
1913 optab binoptab;
1914 rtx op0, op1;
1915 rtx target;
1916 int unsignedp;
1917 enum optab_methods methods;
1919 enum machine_mode submode, tmode;
1920 int size, elts, subsize, subbitsize, i;
1921 rtx t, a, b, res, seq;
1922 enum mode_class class;
1924 class = GET_MODE_CLASS (mode);
1926 size = GET_MODE_SIZE (mode);
1927 submode = GET_MODE_INNER (mode);
1929 /* Search for the widest vector mode with the same inner mode that is
1930 still narrower than MODE and that allows to open-code this operator.
1931 Note, if we find such a mode and the handler later decides it can't
1932 do the expansion, we'll be called recursively with the narrower mode. */
1933 for (tmode = GET_CLASS_NARROWEST_MODE (class);
1934 GET_MODE_SIZE (tmode) < GET_MODE_SIZE (mode);
1935 tmode = GET_MODE_WIDER_MODE (tmode))
1937 if (GET_MODE_INNER (tmode) == GET_MODE_INNER (mode)
1938 && binoptab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
1939 submode = tmode;
1942 switch (binoptab->code)
1944 case AND:
1945 case IOR:
1946 case XOR:
1947 tmode = int_mode_for_mode (mode);
1948 if (tmode != BLKmode)
1949 submode = tmode;
1950 case PLUS:
1951 case MINUS:
1952 case MULT:
1953 case DIV:
1954 subsize = GET_MODE_SIZE (submode);
1955 subbitsize = GET_MODE_BITSIZE (submode);
1956 elts = size / subsize;
1958 /* If METHODS is OPTAB_DIRECT, we don't insist on the exact mode,
1959 but that we operate on more than one element at a time. */
1960 if (subsize == GET_MODE_UNIT_SIZE (mode) && methods == OPTAB_DIRECT)
1961 return 0;
1963 start_sequence ();
1965 /* Errors can leave us with a const0_rtx as operand. */
1966 if (GET_MODE (op0) != mode)
1967 op0 = copy_to_mode_reg (mode, op0);
1968 if (GET_MODE (op1) != mode)
1969 op1 = copy_to_mode_reg (mode, op1);
1971 if (!target)
1972 target = gen_reg_rtx (mode);
1974 for (i = 0; i < elts; ++i)
1976 /* If this is part of a register, and not the first item in the
1977 word, we can't store using a SUBREG - that would clobber
1978 previous results.
1979 And storing with a SUBREG is only possible for the least
1980 significant part, hence we can't do it for big endian
1981 (unless we want to permute the evaluation order. */
1982 if (GET_CODE (target) == REG
1983 && (BYTES_BIG_ENDIAN
1984 ? subsize < UNITS_PER_WORD
1985 : ((i * subsize) % UNITS_PER_WORD) != 0))
1986 t = NULL_RTX;
1987 else
1988 t = simplify_gen_subreg (submode, target, mode, i * subsize);
1989 if (CONSTANT_P (op0))
1990 a = simplify_gen_subreg (submode, op0, mode, i * subsize);
1991 else
1992 a = extract_bit_field (op0, subbitsize, i * subbitsize, unsignedp,
1993 NULL_RTX, submode, submode, size);
1994 if (CONSTANT_P (op1))
1995 b = simplify_gen_subreg (submode, op1, mode, i * subsize);
1996 else
1997 b = extract_bit_field (op1, subbitsize, i * subbitsize, unsignedp,
1998 NULL_RTX, submode, submode, size);
2000 if (binoptab->code == DIV)
2002 if (class == MODE_VECTOR_FLOAT)
2003 res = expand_binop (submode, binoptab, a, b, t,
2004 unsignedp, methods);
2005 else
2006 res = expand_divmod (0, TRUNC_DIV_EXPR, submode,
2007 a, b, t, unsignedp);
2009 else
2010 res = expand_binop (submode, binoptab, a, b, t,
2011 unsignedp, methods);
2013 if (res == 0)
2014 break;
2016 if (t)
2017 emit_move_insn (t, res);
2018 else
2019 store_bit_field (target, subbitsize, i * subbitsize, submode, res,
2020 size);
2022 break;
2024 default:
2025 abort ();
2028 seq = get_insns ();
2029 end_sequence ();
2030 emit_insn (seq);
2032 return target;
2035 /* Like expand_unop but for open-coding vector unops. */
2037 static rtx
2038 expand_vector_unop (mode, unoptab, op0, target, unsignedp)
2039 enum machine_mode mode;
2040 optab unoptab;
2041 rtx op0;
2042 rtx target;
2043 int unsignedp;
2045 enum machine_mode submode, tmode;
2046 int size, elts, subsize, subbitsize, i;
2047 rtx t, a, res, seq;
2049 size = GET_MODE_SIZE (mode);
2050 submode = GET_MODE_INNER (mode);
2052 /* Search for the widest vector mode with the same inner mode that is
2053 still narrower than MODE and that allows to open-code this operator.
2054 Note, if we find such a mode and the handler later decides it can't
2055 do the expansion, we'll be called recursively with the narrower mode. */
2056 for (tmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (mode));
2057 GET_MODE_SIZE (tmode) < GET_MODE_SIZE (mode);
2058 tmode = GET_MODE_WIDER_MODE (tmode))
2060 if (GET_MODE_INNER (tmode) == GET_MODE_INNER (mode)
2061 && unoptab->handlers[(int) tmode].insn_code != CODE_FOR_nothing)
2062 submode = tmode;
2064 /* If there is no negate operation, try doing a subtract from zero. */
2065 if (unoptab == neg_optab && GET_MODE_CLASS (submode) == MODE_INT
2066 /* Avoid infinite recursion when an
2067 error has left us with the wrong mode. */
2068 && GET_MODE (op0) == mode)
2070 rtx temp;
2071 temp = expand_binop (mode, sub_optab, CONST0_RTX (mode), op0,
2072 target, unsignedp, OPTAB_DIRECT);
2073 if (temp)
2074 return temp;
2077 if (unoptab == one_cmpl_optab)
2079 tmode = int_mode_for_mode (mode);
2080 if (tmode != BLKmode)
2081 submode = tmode;
2084 subsize = GET_MODE_SIZE (submode);
2085 subbitsize = GET_MODE_BITSIZE (submode);
2086 elts = size / subsize;
2088 /* Errors can leave us with a const0_rtx as operand. */
2089 if (GET_MODE (op0) != mode)
2090 op0 = copy_to_mode_reg (mode, op0);
2092 if (!target)
2093 target = gen_reg_rtx (mode);
2095 start_sequence ();
2097 for (i = 0; i < elts; ++i)
2099 /* If this is part of a register, and not the first item in the
2100 word, we can't store using a SUBREG - that would clobber
2101 previous results.
2102 And storing with a SUBREG is only possible for the least
2103 significant part, hence we can't do it for big endian
2104 (unless we want to permute the evaluation order. */
2105 if (GET_CODE (target) == REG
2106 && (BYTES_BIG_ENDIAN
2107 ? subsize < UNITS_PER_WORD
2108 : ((i * subsize) % UNITS_PER_WORD) != 0))
2109 t = NULL_RTX;
2110 else
2111 t = simplify_gen_subreg (submode, target, mode, i * subsize);
2112 if (CONSTANT_P (op0))
2113 a = simplify_gen_subreg (submode, op0, mode, i * subsize);
2114 else
2115 a = extract_bit_field (op0, subbitsize, i * subbitsize, unsignedp,
2116 t, submode, submode, size);
2118 res = expand_unop (submode, unoptab, a, t, unsignedp);
2120 if (t)
2121 emit_move_insn (t, res);
2122 else
2123 store_bit_field (target, subbitsize, i * subbitsize, submode, res,
2124 size);
2127 seq = get_insns ();
2128 end_sequence ();
2129 emit_insn (seq);
2131 return target;
2134 /* Expand a binary operator which has both signed and unsigned forms.
2135 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2136 signed operations.
2138 If we widen unsigned operands, we may use a signed wider operation instead
2139 of an unsigned wider operation, since the result would be the same. */
2142 sign_expand_binop (mode, uoptab, soptab, op0, op1, target, unsignedp, methods)
2143 enum machine_mode mode;
2144 optab uoptab, soptab;
2145 rtx op0, op1, target;
2146 int unsignedp;
2147 enum optab_methods methods;
2149 rtx temp;
2150 optab direct_optab = unsignedp ? uoptab : soptab;
2151 struct optab wide_soptab;
2153 /* Do it without widening, if possible. */
2154 temp = expand_binop (mode, direct_optab, op0, op1, target,
2155 unsignedp, OPTAB_DIRECT);
2156 if (temp || methods == OPTAB_DIRECT)
2157 return temp;
2159 /* Try widening to a signed int. Make a fake signed optab that
2160 hides any signed insn for direct use. */
2161 wide_soptab = *soptab;
2162 wide_soptab.handlers[(int) mode].insn_code = CODE_FOR_nothing;
2163 wide_soptab.handlers[(int) mode].libfunc = 0;
2165 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2166 unsignedp, OPTAB_WIDEN);
2168 /* For unsigned operands, try widening to an unsigned int. */
2169 if (temp == 0 && unsignedp)
2170 temp = expand_binop (mode, uoptab, op0, op1, target,
2171 unsignedp, OPTAB_WIDEN);
2172 if (temp || methods == OPTAB_WIDEN)
2173 return temp;
2175 /* Use the right width lib call if that exists. */
2176 temp = expand_binop (mode, direct_optab, op0, op1, target, unsignedp, OPTAB_LIB);
2177 if (temp || methods == OPTAB_LIB)
2178 return temp;
2180 /* Must widen and use a lib call, use either signed or unsigned. */
2181 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2182 unsignedp, methods);
2183 if (temp != 0)
2184 return temp;
2185 if (unsignedp)
2186 return expand_binop (mode, uoptab, op0, op1, target,
2187 unsignedp, methods);
2188 return 0;
2191 /* Generate code to perform an operation specified by BINOPTAB
2192 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2193 We assume that the order of the operands for the instruction
2194 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2195 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2197 Either TARG0 or TARG1 may be zero, but what that means is that
2198 the result is not actually wanted. We will generate it into
2199 a dummy pseudo-reg and discard it. They may not both be zero.
2201 Returns 1 if this operation can be performed; 0 if not. */
2204 expand_twoval_binop (binoptab, op0, op1, targ0, targ1, unsignedp)
2205 optab binoptab;
2206 rtx op0, op1;
2207 rtx targ0, targ1;
2208 int unsignedp;
2210 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2211 enum mode_class class;
2212 enum machine_mode wider_mode;
2213 rtx entry_last = get_last_insn ();
2214 rtx last;
2216 class = GET_MODE_CLASS (mode);
2218 op0 = protect_from_queue (op0, 0);
2219 op1 = protect_from_queue (op1, 0);
2221 if (flag_force_mem)
2223 op0 = force_not_mem (op0);
2224 op1 = force_not_mem (op1);
2227 /* If we are inside an appropriately-short loop and one operand is an
2228 expensive constant, force it into a register. */
2229 if (CONSTANT_P (op0) && preserve_subexpressions_p ()
2230 && rtx_cost (op0, binoptab->code) > COSTS_N_INSNS (1))
2231 op0 = force_reg (mode, op0);
2233 if (CONSTANT_P (op1) && preserve_subexpressions_p ()
2234 && rtx_cost (op1, binoptab->code) > COSTS_N_INSNS (1))
2235 op1 = force_reg (mode, op1);
2237 if (targ0)
2238 targ0 = protect_from_queue (targ0, 1);
2239 else
2240 targ0 = gen_reg_rtx (mode);
2241 if (targ1)
2242 targ1 = protect_from_queue (targ1, 1);
2243 else
2244 targ1 = gen_reg_rtx (mode);
2246 /* Record where to go back to if we fail. */
2247 last = get_last_insn ();
2249 if (binoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2251 int icode = (int) binoptab->handlers[(int) mode].insn_code;
2252 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2253 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
2254 rtx pat;
2255 rtx xop0 = op0, xop1 = op1;
2257 /* In case the insn wants input operands in modes different from
2258 those of the actual operands, convert the operands. It would
2259 seem that we don't need to convert CONST_INTs, but we do, so
2260 that they're properly zero-extended, sign-extended or truncated
2261 for their mode. */
2263 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
2264 xop0 = convert_modes (mode0,
2265 GET_MODE (op0) != VOIDmode
2266 ? GET_MODE (op0)
2267 : mode,
2268 xop0, unsignedp);
2270 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
2271 xop1 = convert_modes (mode1,
2272 GET_MODE (op1) != VOIDmode
2273 ? GET_MODE (op1)
2274 : mode,
2275 xop1, unsignedp);
2277 /* Now, if insn doesn't accept these operands, put them into pseudos. */
2278 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0))
2279 xop0 = copy_to_mode_reg (mode0, xop0);
2281 if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1))
2282 xop1 = copy_to_mode_reg (mode1, xop1);
2284 /* We could handle this, but we should always be called with a pseudo
2285 for our targets and all insns should take them as outputs. */
2286 if (! (*insn_data[icode].operand[0].predicate) (targ0, mode)
2287 || ! (*insn_data[icode].operand[3].predicate) (targ1, mode))
2288 abort ();
2290 pat = GEN_FCN (icode) (targ0, xop0, xop1, targ1);
2291 if (pat)
2293 emit_insn (pat);
2294 return 1;
2296 else
2297 delete_insns_since (last);
2300 /* It can't be done in this mode. Can we do it in a wider mode? */
2302 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2304 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2305 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2307 if (binoptab->handlers[(int) wider_mode].insn_code
2308 != CODE_FOR_nothing)
2310 rtx t0 = gen_reg_rtx (wider_mode);
2311 rtx t1 = gen_reg_rtx (wider_mode);
2312 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2313 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2315 if (expand_twoval_binop (binoptab, cop0, cop1,
2316 t0, t1, unsignedp))
2318 convert_move (targ0, t0, unsignedp);
2319 convert_move (targ1, t1, unsignedp);
2320 return 1;
2322 else
2323 delete_insns_since (last);
2328 delete_insns_since (entry_last);
2329 return 0;
2332 /* Wrapper around expand_unop which takes an rtx code to specify
2333 the operation to perform, not an optab pointer. All other
2334 arguments are the same. */
2336 expand_simple_unop (mode, code, op0, target, unsignedp)
2337 enum machine_mode mode;
2338 enum rtx_code code;
2339 rtx op0;
2340 rtx target;
2341 int unsignedp;
2343 optab unop = code_to_optab[(int) code];
2344 if (unop == 0)
2345 abort ();
2347 return expand_unop (mode, unop, op0, target, unsignedp);
2350 /* Try calculating
2351 (clz:narrow x)
2353 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)). */
2354 static rtx
2355 widen_clz (mode, op0, target)
2356 enum machine_mode mode;
2357 rtx op0;
2358 rtx target;
2360 enum mode_class class = GET_MODE_CLASS (mode);
2361 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2363 enum machine_mode wider_mode;
2364 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2365 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2367 if (clz_optab->handlers[(int) wider_mode].insn_code
2368 != CODE_FOR_nothing)
2370 rtx xop0, temp, last;
2372 last = get_last_insn ();
2374 if (target == 0)
2375 target = gen_reg_rtx (mode);
2376 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2377 temp = expand_unop (wider_mode, clz_optab, xop0, NULL_RTX, true);
2378 if (temp != 0)
2379 temp = expand_binop (wider_mode, sub_optab, temp,
2380 GEN_INT (GET_MODE_BITSIZE (wider_mode)
2381 - GET_MODE_BITSIZE (mode)),
2382 target, true, OPTAB_DIRECT);
2383 if (temp == 0)
2384 delete_insns_since (last);
2386 return temp;
2390 return 0;
2393 /* Try calculating (parity x) as (and (popcount x) 1), where
2394 popcount can also be done in a wider mode. */
2395 static rtx
2396 expand_parity (mode, op0, target)
2397 enum machine_mode mode;
2398 rtx op0;
2399 rtx target;
2401 enum mode_class class = GET_MODE_CLASS (mode);
2402 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2404 enum machine_mode wider_mode;
2405 for (wider_mode = mode; wider_mode != VOIDmode;
2406 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2408 if (popcount_optab->handlers[(int) wider_mode].insn_code
2409 != CODE_FOR_nothing)
2411 rtx xop0, temp, last;
2413 last = get_last_insn ();
2415 if (target == 0)
2416 target = gen_reg_rtx (mode);
2417 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2418 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2419 true);
2420 if (temp != 0)
2421 temp = expand_binop (wider_mode, and_optab, temp, GEN_INT (1),
2422 target, true, OPTAB_DIRECT);
2423 if (temp == 0)
2424 delete_insns_since (last);
2426 return temp;
2430 return 0;
2433 /* Generate code to perform an operation specified by UNOPTAB
2434 on operand OP0, with result having machine-mode MODE.
2436 UNSIGNEDP is for the case where we have to widen the operands
2437 to perform the operation. It says to use zero-extension.
2439 If TARGET is nonzero, the value
2440 is generated there, if it is convenient to do so.
2441 In all cases an rtx is returned for the locus of the value;
2442 this may or may not be TARGET. */
2445 expand_unop (mode, unoptab, op0, target, unsignedp)
2446 enum machine_mode mode;
2447 optab unoptab;
2448 rtx op0;
2449 rtx target;
2450 int unsignedp;
2452 enum mode_class class;
2453 enum machine_mode wider_mode;
2454 rtx temp;
2455 rtx last = get_last_insn ();
2456 rtx pat;
2458 class = GET_MODE_CLASS (mode);
2460 op0 = protect_from_queue (op0, 0);
2462 if (flag_force_mem)
2464 op0 = force_not_mem (op0);
2467 if (target)
2468 target = protect_from_queue (target, 1);
2470 if (unoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2472 int icode = (int) unoptab->handlers[(int) mode].insn_code;
2473 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2474 rtx xop0 = op0;
2476 if (target)
2477 temp = target;
2478 else
2479 temp = gen_reg_rtx (mode);
2481 if (GET_MODE (xop0) != VOIDmode
2482 && GET_MODE (xop0) != mode0)
2483 xop0 = convert_to_mode (mode0, xop0, unsignedp);
2485 /* Now, if insn doesn't accept our operand, put it into a pseudo. */
2487 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0))
2488 xop0 = copy_to_mode_reg (mode0, xop0);
2490 if (! (*insn_data[icode].operand[0].predicate) (temp, mode))
2491 temp = gen_reg_rtx (mode);
2493 pat = GEN_FCN (icode) (temp, xop0);
2494 if (pat)
2496 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2497 && ! add_equal_note (pat, temp, unoptab->code, xop0, NULL_RTX))
2499 delete_insns_since (last);
2500 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
2503 emit_insn (pat);
2505 return temp;
2507 else
2508 delete_insns_since (last);
2511 /* It can't be done in this mode. Can we open-code it in a wider mode? */
2513 /* Widening clz needs special treatment. */
2514 if (unoptab == clz_optab)
2516 temp = widen_clz (mode, op0, target);
2517 if (temp)
2518 return temp;
2519 else
2520 goto try_libcall;
2523 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2524 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2525 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2527 if (unoptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing)
2529 rtx xop0 = op0;
2531 /* For certain operations, we need not actually extend
2532 the narrow operand, as long as we will truncate the
2533 results to the same narrowness. */
2535 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2536 (unoptab == neg_optab
2537 || unoptab == one_cmpl_optab)
2538 && class == MODE_INT);
2540 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2541 unsignedp);
2543 if (temp)
2545 if (class != MODE_INT)
2547 if (target == 0)
2548 target = gen_reg_rtx (mode);
2549 convert_move (target, temp, 0);
2550 return target;
2552 else
2553 return gen_lowpart (mode, temp);
2555 else
2556 delete_insns_since (last);
2560 /* These can be done a word at a time. */
2561 if (unoptab == one_cmpl_optab
2562 && class == MODE_INT
2563 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
2564 && unoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
2566 int i;
2567 rtx insns;
2569 if (target == 0 || target == op0)
2570 target = gen_reg_rtx (mode);
2572 start_sequence ();
2574 /* Do the actual arithmetic. */
2575 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
2577 rtx target_piece = operand_subword (target, i, 1, mode);
2578 rtx x = expand_unop (word_mode, unoptab,
2579 operand_subword_force (op0, i, mode),
2580 target_piece, unsignedp);
2582 if (target_piece != x)
2583 emit_move_insn (target_piece, x);
2586 insns = get_insns ();
2587 end_sequence ();
2589 emit_no_conflict_block (insns, target, op0, NULL_RTX,
2590 gen_rtx_fmt_e (unoptab->code, mode,
2591 copy_rtx (op0)));
2592 return target;
2595 /* Open-code the complex negation operation. */
2596 else if (unoptab->code == NEG
2597 && (class == MODE_COMPLEX_FLOAT || class == MODE_COMPLEX_INT))
2599 rtx target_piece;
2600 rtx x;
2601 rtx seq;
2603 /* Find the correct mode for the real and imaginary parts. */
2604 enum machine_mode submode = GET_MODE_INNER (mode);
2606 if (submode == BLKmode)
2607 abort ();
2609 if (target == 0)
2610 target = gen_reg_rtx (mode);
2612 start_sequence ();
2614 target_piece = gen_imagpart (submode, target);
2615 x = expand_unop (submode, unoptab,
2616 gen_imagpart (submode, op0),
2617 target_piece, unsignedp);
2618 if (target_piece != x)
2619 emit_move_insn (target_piece, x);
2621 target_piece = gen_realpart (submode, target);
2622 x = expand_unop (submode, unoptab,
2623 gen_realpart (submode, op0),
2624 target_piece, unsignedp);
2625 if (target_piece != x)
2626 emit_move_insn (target_piece, x);
2628 seq = get_insns ();
2629 end_sequence ();
2631 emit_no_conflict_block (seq, target, op0, 0,
2632 gen_rtx_fmt_e (unoptab->code, mode,
2633 copy_rtx (op0)));
2634 return target;
2637 /* Try negating floating point values by flipping the sign bit. */
2638 if (unoptab->code == NEG && class == MODE_FLOAT
2639 && GET_MODE_BITSIZE (mode) <= 2 * HOST_BITS_PER_WIDE_INT)
2641 const struct real_format *fmt = real_format_for_mode[mode - QFmode];
2642 enum machine_mode imode = int_mode_for_mode (mode);
2643 int bitpos = (fmt != 0) ? fmt->signbit : -1;
2645 if (imode != BLKmode && bitpos >= 0 && fmt->has_signed_zero)
2647 HOST_WIDE_INT hi, lo;
2648 rtx last = get_last_insn ();
2650 /* Handle targets with different FP word orders. */
2651 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2653 int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
2654 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
2655 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
2658 if (bitpos < HOST_BITS_PER_WIDE_INT)
2660 hi = 0;
2661 lo = (HOST_WIDE_INT) 1 << bitpos;
2663 else
2665 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
2666 lo = 0;
2668 temp = expand_binop (imode, xor_optab,
2669 gen_lowpart (imode, op0),
2670 immed_double_const (lo, hi, imode),
2671 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2672 if (temp != 0)
2673 return gen_lowpart (mode, temp);
2674 delete_insns_since (last);
2678 /* Try calculating parity (x) as popcount (x) % 2. */
2679 if (unoptab == parity_optab)
2681 temp = expand_parity (mode, op0, target);
2682 if (temp)
2683 return temp;
2686 try_libcall:
2687 /* Now try a library call in this mode. */
2688 if (unoptab->handlers[(int) mode].libfunc)
2690 rtx insns;
2691 rtx value;
2692 enum machine_mode outmode = mode;
2694 /* All of these functions return small values. Thus we choose to
2695 have them return something that isn't a double-word. */
2696 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
2697 || unoptab == popcount_optab || unoptab == parity_optab)
2698 outmode = TYPE_MODE (integer_type_node);
2700 start_sequence ();
2702 /* Pass 1 for NO_QUEUE so we don't lose any increments
2703 if the libcall is cse'd or moved. */
2704 value = emit_library_call_value (unoptab->handlers[(int) mode].libfunc,
2705 NULL_RTX, LCT_CONST, outmode,
2706 1, op0, mode);
2707 insns = get_insns ();
2708 end_sequence ();
2710 target = gen_reg_rtx (outmode);
2711 emit_libcall_block (insns, target, value,
2712 gen_rtx_fmt_e (unoptab->code, mode, op0));
2714 return target;
2717 if (class == MODE_VECTOR_FLOAT || class == MODE_VECTOR_INT)
2718 return expand_vector_unop (mode, unoptab, op0, target, unsignedp);
2720 /* It can't be done in this mode. Can we do it in a wider mode? */
2722 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2724 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2725 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2727 if ((unoptab->handlers[(int) wider_mode].insn_code
2728 != CODE_FOR_nothing)
2729 || unoptab->handlers[(int) wider_mode].libfunc)
2731 rtx xop0 = op0;
2733 /* For certain operations, we need not actually extend
2734 the narrow operand, as long as we will truncate the
2735 results to the same narrowness. */
2737 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2738 (unoptab == neg_optab
2739 || unoptab == one_cmpl_optab)
2740 && class == MODE_INT);
2742 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2743 unsignedp);
2745 /* If we are generating clz using wider mode, adjust the
2746 result. */
2747 if (unoptab == clz_optab && temp != 0)
2748 temp = expand_binop (wider_mode, sub_optab, temp,
2749 GEN_INT (GET_MODE_BITSIZE (wider_mode)
2750 - GET_MODE_BITSIZE (mode)),
2751 target, true, OPTAB_DIRECT);
2753 if (temp)
2755 if (class != MODE_INT)
2757 if (target == 0)
2758 target = gen_reg_rtx (mode);
2759 convert_move (target, temp, 0);
2760 return target;
2762 else
2763 return gen_lowpart (mode, temp);
2765 else
2766 delete_insns_since (last);
2771 /* If there is no negate operation, try doing a subtract from zero.
2772 The US Software GOFAST library needs this. */
2773 if (unoptab->code == NEG)
2775 rtx temp;
2776 temp = expand_binop (mode,
2777 unoptab == negv_optab ? subv_optab : sub_optab,
2778 CONST0_RTX (mode), op0,
2779 target, unsignedp, OPTAB_LIB_WIDEN);
2780 if (temp)
2781 return temp;
2784 return 0;
2787 /* Emit code to compute the absolute value of OP0, with result to
2788 TARGET if convenient. (TARGET may be 0.) The return value says
2789 where the result actually is to be found.
2791 MODE is the mode of the operand; the mode of the result is
2792 different but can be deduced from MODE.
2797 expand_abs_nojump (mode, op0, target, result_unsignedp)
2798 enum machine_mode mode;
2799 rtx op0;
2800 rtx target;
2801 int result_unsignedp;
2803 rtx temp;
2805 if (! flag_trapv)
2806 result_unsignedp = 1;
2808 /* First try to do it with a special abs instruction. */
2809 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
2810 op0, target, 0);
2811 if (temp != 0)
2812 return temp;
2814 /* For floating point modes, try clearing the sign bit. */
2815 if (GET_MODE_CLASS (mode) == MODE_FLOAT
2816 && GET_MODE_BITSIZE (mode) <= 2 * HOST_BITS_PER_WIDE_INT)
2818 const struct real_format *fmt = real_format_for_mode[mode - QFmode];
2819 enum machine_mode imode = int_mode_for_mode (mode);
2820 int bitpos = (fmt != 0) ? fmt->signbit : -1;
2822 if (imode != BLKmode && bitpos >= 0)
2824 HOST_WIDE_INT hi, lo;
2825 rtx last = get_last_insn ();
2827 /* Handle targets with different FP word orders. */
2828 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2830 int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
2831 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
2832 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
2835 if (bitpos < HOST_BITS_PER_WIDE_INT)
2837 hi = 0;
2838 lo = (HOST_WIDE_INT) 1 << bitpos;
2840 else
2842 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
2843 lo = 0;
2845 temp = expand_binop (imode, and_optab,
2846 gen_lowpart (imode, op0),
2847 immed_double_const (~lo, ~hi, imode),
2848 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2849 if (temp != 0)
2850 return gen_lowpart (mode, temp);
2851 delete_insns_since (last);
2855 /* If we have a MAX insn, we can do this as MAX (x, -x). */
2856 if (smax_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2858 rtx last = get_last_insn ();
2860 temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
2861 if (temp != 0)
2862 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
2863 OPTAB_WIDEN);
2865 if (temp != 0)
2866 return temp;
2868 delete_insns_since (last);
2871 /* If this machine has expensive jumps, we can do integer absolute
2872 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
2873 where W is the width of MODE. */
2875 if (GET_MODE_CLASS (mode) == MODE_INT && BRANCH_COST >= 2)
2877 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
2878 size_int (GET_MODE_BITSIZE (mode) - 1),
2879 NULL_RTX, 0);
2881 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
2882 OPTAB_LIB_WIDEN);
2883 if (temp != 0)
2884 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
2885 temp, extended, target, 0, OPTAB_LIB_WIDEN);
2887 if (temp != 0)
2888 return temp;
2891 return NULL_RTX;
2895 expand_abs (mode, op0, target, result_unsignedp, safe)
2896 enum machine_mode mode;
2897 rtx op0;
2898 rtx target;
2899 int result_unsignedp;
2900 int safe;
2902 rtx temp, op1;
2904 if (! flag_trapv)
2905 result_unsignedp = 1;
2907 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
2908 if (temp != 0)
2909 return temp;
2911 /* If that does not win, use conditional jump and negate. */
2913 /* It is safe to use the target if it is the same
2914 as the source if this is also a pseudo register */
2915 if (op0 == target && GET_CODE (op0) == REG
2916 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
2917 safe = 1;
2919 op1 = gen_label_rtx ();
2920 if (target == 0 || ! safe
2921 || GET_MODE (target) != mode
2922 || (GET_CODE (target) == MEM && MEM_VOLATILE_P (target))
2923 || (GET_CODE (target) == REG
2924 && REGNO (target) < FIRST_PSEUDO_REGISTER))
2925 target = gen_reg_rtx (mode);
2927 emit_move_insn (target, op0);
2928 NO_DEFER_POP;
2930 /* If this mode is an integer too wide to compare properly,
2931 compare word by word. Rely on CSE to optimize constant cases. */
2932 if (GET_MODE_CLASS (mode) == MODE_INT
2933 && ! can_compare_p (GE, mode, ccp_jump))
2934 do_jump_by_parts_greater_rtx (mode, 0, target, const0_rtx,
2935 NULL_RTX, op1);
2936 else
2937 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
2938 NULL_RTX, NULL_RTX, op1);
2940 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
2941 target, target, 0);
2942 if (op0 != target)
2943 emit_move_insn (target, op0);
2944 emit_label (op1);
2945 OK_DEFER_POP;
2946 return target;
2949 /* Emit code to compute the absolute value of OP0, with result to
2950 TARGET if convenient. (TARGET may be 0.) The return value says
2951 where the result actually is to be found.
2953 MODE is the mode of the operand; the mode of the result is
2954 different but can be deduced from MODE.
2956 UNSIGNEDP is relevant for complex integer modes. */
2959 expand_complex_abs (mode, op0, target, unsignedp)
2960 enum machine_mode mode;
2961 rtx op0;
2962 rtx target;
2963 int unsignedp;
2965 enum mode_class class = GET_MODE_CLASS (mode);
2966 enum machine_mode wider_mode;
2967 rtx temp;
2968 rtx entry_last = get_last_insn ();
2969 rtx last;
2970 rtx pat;
2971 optab this_abs_optab;
2973 /* Find the correct mode for the real and imaginary parts. */
2974 enum machine_mode submode = GET_MODE_INNER (mode);
2976 if (submode == BLKmode)
2977 abort ();
2979 op0 = protect_from_queue (op0, 0);
2981 if (flag_force_mem)
2983 op0 = force_not_mem (op0);
2986 last = get_last_insn ();
2988 if (target)
2989 target = protect_from_queue (target, 1);
2991 this_abs_optab = ! unsignedp && flag_trapv
2992 && (GET_MODE_CLASS(mode) == MODE_INT)
2993 ? absv_optab : abs_optab;
2995 if (this_abs_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2997 int icode = (int) this_abs_optab->handlers[(int) mode].insn_code;
2998 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2999 rtx xop0 = op0;
3001 if (target)
3002 temp = target;
3003 else
3004 temp = gen_reg_rtx (submode);
3006 if (GET_MODE (xop0) != VOIDmode
3007 && GET_MODE (xop0) != mode0)
3008 xop0 = convert_to_mode (mode0, xop0, unsignedp);
3010 /* Now, if insn doesn't accept our operand, put it into a pseudo. */
3012 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0))
3013 xop0 = copy_to_mode_reg (mode0, xop0);
3015 if (! (*insn_data[icode].operand[0].predicate) (temp, submode))
3016 temp = gen_reg_rtx (submode);
3018 pat = GEN_FCN (icode) (temp, xop0);
3019 if (pat)
3021 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3022 && ! add_equal_note (pat, temp, this_abs_optab->code, xop0,
3023 NULL_RTX))
3025 delete_insns_since (last);
3026 return expand_unop (mode, this_abs_optab, op0, NULL_RTX,
3027 unsignedp);
3030 emit_insn (pat);
3032 return temp;
3034 else
3035 delete_insns_since (last);
3038 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3040 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
3041 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3043 if (this_abs_optab->handlers[(int) wider_mode].insn_code
3044 != CODE_FOR_nothing)
3046 rtx xop0 = op0;
3048 xop0 = convert_modes (wider_mode, mode, xop0, unsignedp);
3049 temp = expand_complex_abs (wider_mode, xop0, NULL_RTX, unsignedp);
3051 if (temp)
3053 if (class != MODE_COMPLEX_INT)
3055 if (target == 0)
3056 target = gen_reg_rtx (submode);
3057 convert_move (target, temp, 0);
3058 return target;
3060 else
3061 return gen_lowpart (submode, temp);
3063 else
3064 delete_insns_since (last);
3068 /* Open-code the complex absolute-value operation
3069 if we can open-code sqrt. Otherwise it's not worth while. */
3070 if (sqrt_optab->handlers[(int) submode].insn_code != CODE_FOR_nothing
3071 && ! flag_trapv)
3073 rtx real, imag, total;
3075 real = gen_realpart (submode, op0);
3076 imag = gen_imagpart (submode, op0);
3078 /* Square both parts. */
3079 real = expand_mult (submode, real, real, NULL_RTX, 0);
3080 imag = expand_mult (submode, imag, imag, NULL_RTX, 0);
3082 /* Sum the parts. */
3083 total = expand_binop (submode, add_optab, real, imag, NULL_RTX,
3084 0, OPTAB_LIB_WIDEN);
3086 /* Get sqrt in TARGET. Set TARGET to where the result is. */
3087 target = expand_unop (submode, sqrt_optab, total, target, 0);
3088 if (target == 0)
3089 delete_insns_since (last);
3090 else
3091 return target;
3094 /* Now try a library call in this mode. */
3095 if (this_abs_optab->handlers[(int) mode].libfunc)
3097 rtx insns;
3098 rtx value;
3100 start_sequence ();
3102 /* Pass 1 for NO_QUEUE so we don't lose any increments
3103 if the libcall is cse'd or moved. */
3104 value = emit_library_call_value (abs_optab->handlers[(int) mode].libfunc,
3105 NULL_RTX, LCT_CONST, submode, 1, op0, mode);
3106 insns = get_insns ();
3107 end_sequence ();
3109 target = gen_reg_rtx (submode);
3110 emit_libcall_block (insns, target, value,
3111 gen_rtx_fmt_e (this_abs_optab->code, mode, op0));
3113 return target;
3116 /* It can't be done in this mode. Can we do it in a wider mode? */
3118 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
3119 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3121 if ((this_abs_optab->handlers[(int) wider_mode].insn_code
3122 != CODE_FOR_nothing)
3123 || this_abs_optab->handlers[(int) wider_mode].libfunc)
3125 rtx xop0 = op0;
3127 xop0 = convert_modes (wider_mode, mode, xop0, unsignedp);
3129 temp = expand_complex_abs (wider_mode, xop0, NULL_RTX, unsignedp);
3131 if (temp)
3133 if (class != MODE_COMPLEX_INT)
3135 if (target == 0)
3136 target = gen_reg_rtx (submode);
3137 convert_move (target, temp, 0);
3138 return target;
3140 else
3141 return gen_lowpart (submode, temp);
3143 else
3144 delete_insns_since (last);
3148 delete_insns_since (entry_last);
3149 return 0;
3152 /* Generate an instruction whose insn-code is INSN_CODE,
3153 with two operands: an output TARGET and an input OP0.
3154 TARGET *must* be nonzero, and the output is always stored there.
3155 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3156 the value that is stored into TARGET. */
3158 void
3159 emit_unop_insn (icode, target, op0, code)
3160 int icode;
3161 rtx target;
3162 rtx op0;
3163 enum rtx_code code;
3165 rtx temp;
3166 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3167 rtx pat;
3169 temp = target = protect_from_queue (target, 1);
3171 op0 = protect_from_queue (op0, 0);
3173 /* Sign and zero extension from memory is often done specially on
3174 RISC machines, so forcing into a register here can pessimize
3175 code. */
3176 if (flag_force_mem && code != SIGN_EXTEND && code != ZERO_EXTEND)
3177 op0 = force_not_mem (op0);
3179 /* Now, if insn does not accept our operands, put them into pseudos. */
3181 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
3182 op0 = copy_to_mode_reg (mode0, op0);
3184 if (! (*insn_data[icode].operand[0].predicate) (temp, GET_MODE (temp))
3185 || (flag_force_mem && GET_CODE (temp) == MEM))
3186 temp = gen_reg_rtx (GET_MODE (temp));
3188 pat = GEN_FCN (icode) (temp, op0);
3190 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
3191 add_equal_note (pat, temp, code, op0, NULL_RTX);
3193 emit_insn (pat);
3195 if (temp != target)
3196 emit_move_insn (target, temp);
3199 /* Emit code to perform a series of operations on a multi-word quantity, one
3200 word at a time.
3202 Such a block is preceded by a CLOBBER of the output, consists of multiple
3203 insns, each setting one word of the output, and followed by a SET copying
3204 the output to itself.
3206 Each of the insns setting words of the output receives a REG_NO_CONFLICT
3207 note indicating that it doesn't conflict with the (also multi-word)
3208 inputs. The entire block is surrounded by REG_LIBCALL and REG_RETVAL
3209 notes.
3211 INSNS is a block of code generated to perform the operation, not including
3212 the CLOBBER and final copy. All insns that compute intermediate values
3213 are first emitted, followed by the block as described above.
3215 TARGET, OP0, and OP1 are the output and inputs of the operations,
3216 respectively. OP1 may be zero for a unary operation.
3218 EQUIV, if nonzero, is an expression to be placed into a REG_EQUAL note
3219 on the last insn.
3221 If TARGET is not a register, INSNS is simply emitted with no special
3222 processing. Likewise if anything in INSNS is not an INSN or if
3223 there is a libcall block inside INSNS.
3225 The final insn emitted is returned. */
3228 emit_no_conflict_block (insns, target, op0, op1, equiv)
3229 rtx insns;
3230 rtx target;
3231 rtx op0, op1;
3232 rtx equiv;
3234 rtx prev, next, first, last, insn;
3236 if (GET_CODE (target) != REG || reload_in_progress)
3237 return emit_insn (insns);
3238 else
3239 for (insn = insns; insn; insn = NEXT_INSN (insn))
3240 if (GET_CODE (insn) != INSN
3241 || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
3242 return emit_insn (insns);
3244 /* First emit all insns that do not store into words of the output and remove
3245 these from the list. */
3246 for (insn = insns; insn; insn = next)
3248 rtx set = 0, note;
3249 int i;
3251 next = NEXT_INSN (insn);
3253 /* Some ports (cris) create an libcall regions at their own. We must
3254 avoid any potential nesting of LIBCALLs. */
3255 if ((note = find_reg_note (insn, REG_LIBCALL, NULL)) != NULL)
3256 remove_note (insn, note);
3257 if ((note = find_reg_note (insn, REG_RETVAL, NULL)) != NULL)
3258 remove_note (insn, note);
3260 if (GET_CODE (PATTERN (insn)) == SET || GET_CODE (PATTERN (insn)) == USE
3261 || GET_CODE (PATTERN (insn)) == CLOBBER)
3262 set = PATTERN (insn);
3263 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3265 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
3266 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
3268 set = XVECEXP (PATTERN (insn), 0, i);
3269 break;
3273 if (set == 0)
3274 abort ();
3276 if (! reg_overlap_mentioned_p (target, SET_DEST (set)))
3278 if (PREV_INSN (insn))
3279 NEXT_INSN (PREV_INSN (insn)) = next;
3280 else
3281 insns = next;
3283 if (next)
3284 PREV_INSN (next) = PREV_INSN (insn);
3286 add_insn (insn);
3290 prev = get_last_insn ();
3292 /* Now write the CLOBBER of the output, followed by the setting of each
3293 of the words, followed by the final copy. */
3294 if (target != op0 && target != op1)
3295 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
3297 for (insn = insns; insn; insn = next)
3299 next = NEXT_INSN (insn);
3300 add_insn (insn);
3302 if (op1 && GET_CODE (op1) == REG)
3303 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_NO_CONFLICT, op1,
3304 REG_NOTES (insn));
3306 if (op0 && GET_CODE (op0) == REG)
3307 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_NO_CONFLICT, op0,
3308 REG_NOTES (insn));
3311 if (mov_optab->handlers[(int) GET_MODE (target)].insn_code
3312 != CODE_FOR_nothing)
3314 last = emit_move_insn (target, target);
3315 if (equiv)
3316 set_unique_reg_note (last, REG_EQUAL, equiv);
3318 else
3320 last = get_last_insn ();
3322 /* Remove any existing REG_EQUAL note from "last", or else it will
3323 be mistaken for a note referring to the full contents of the
3324 alleged libcall value when found together with the REG_RETVAL
3325 note added below. An existing note can come from an insn
3326 expansion at "last". */
3327 remove_note (last, find_reg_note (last, REG_EQUAL, NULL_RTX));
3330 if (prev == 0)
3331 first = get_insns ();
3332 else
3333 first = NEXT_INSN (prev);
3335 /* Encapsulate the block so it gets manipulated as a unit. */
3336 REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
3337 REG_NOTES (first));
3338 REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
3340 return last;
3343 /* Emit code to make a call to a constant function or a library call.
3345 INSNS is a list containing all insns emitted in the call.
3346 These insns leave the result in RESULT. Our block is to copy RESULT
3347 to TARGET, which is logically equivalent to EQUIV.
3349 We first emit any insns that set a pseudo on the assumption that these are
3350 loading constants into registers; doing so allows them to be safely cse'ed
3351 between blocks. Then we emit all the other insns in the block, followed by
3352 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3353 note with an operand of EQUIV.
3355 Moving assignments to pseudos outside of the block is done to improve
3356 the generated code, but is not required to generate correct code,
3357 hence being unable to move an assignment is not grounds for not making
3358 a libcall block. There are two reasons why it is safe to leave these
3359 insns inside the block: First, we know that these pseudos cannot be
3360 used in generated RTL outside the block since they are created for
3361 temporary purposes within the block. Second, CSE will not record the
3362 values of anything set inside a libcall block, so we know they must
3363 be dead at the end of the block.
3365 Except for the first group of insns (the ones setting pseudos), the
3366 block is delimited by REG_RETVAL and REG_LIBCALL notes. */
3368 void
3369 emit_libcall_block (insns, target, result, equiv)
3370 rtx insns;
3371 rtx target;
3372 rtx result;
3373 rtx equiv;
3375 rtx final_dest = target;
3376 rtx prev, next, first, last, insn;
3378 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3379 into a MEM later. Protect the libcall block from this change. */
3380 if (! REG_P (target) || REG_USERVAR_P (target))
3381 target = gen_reg_rtx (GET_MODE (target));
3383 /* If we're using non-call exceptions, a libcall corresponding to an
3384 operation that may trap may also trap. */
3385 if (flag_non_call_exceptions && may_trap_p (equiv))
3387 for (insn = insns; insn; insn = NEXT_INSN (insn))
3388 if (GET_CODE (insn) == CALL_INSN)
3390 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3392 if (note != 0 && INTVAL (XEXP (note, 0)) <= 0)
3393 remove_note (insn, note);
3396 else
3397 /* look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3398 reg note to indicate that this call cannot throw or execute a nonlocal
3399 goto (unless there is already a REG_EH_REGION note, in which case
3400 we update it). */
3401 for (insn = insns; insn; insn = NEXT_INSN (insn))
3402 if (GET_CODE (insn) == CALL_INSN)
3404 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3406 if (note != 0)
3407 XEXP (note, 0) = GEN_INT (-1);
3408 else
3409 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, GEN_INT (-1),
3410 REG_NOTES (insn));
3413 /* First emit all insns that set pseudos. Remove them from the list as
3414 we go. Avoid insns that set pseudos which were referenced in previous
3415 insns. These can be generated by move_by_pieces, for example,
3416 to update an address. Similarly, avoid insns that reference things
3417 set in previous insns. */
3419 for (insn = insns; insn; insn = next)
3421 rtx set = single_set (insn);
3422 rtx note;
3424 /* Some ports (cris) create an libcall regions at their own. We must
3425 avoid any potential nesting of LIBCALLs. */
3426 if ((note = find_reg_note (insn, REG_LIBCALL, NULL)) != NULL)
3427 remove_note (insn, note);
3428 if ((note = find_reg_note (insn, REG_RETVAL, NULL)) != NULL)
3429 remove_note (insn, note);
3431 next = NEXT_INSN (insn);
3433 if (set != 0 && GET_CODE (SET_DEST (set)) == REG
3434 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
3435 && (insn == insns
3436 || ((! INSN_P(insns)
3437 || ! reg_mentioned_p (SET_DEST (set), PATTERN (insns)))
3438 && ! reg_used_between_p (SET_DEST (set), insns, insn)
3439 && ! modified_in_p (SET_SRC (set), insns)
3440 && ! modified_between_p (SET_SRC (set), insns, insn))))
3442 if (PREV_INSN (insn))
3443 NEXT_INSN (PREV_INSN (insn)) = next;
3444 else
3445 insns = next;
3447 if (next)
3448 PREV_INSN (next) = PREV_INSN (insn);
3450 add_insn (insn);
3453 /* Some ports use a loop to copy large arguments onto the stack.
3454 Don't move anything outside such a loop. */
3455 if (GET_CODE (insn) == CODE_LABEL)
3456 break;
3459 prev = get_last_insn ();
3461 /* Write the remaining insns followed by the final copy. */
3463 for (insn = insns; insn; insn = next)
3465 next = NEXT_INSN (insn);
3467 add_insn (insn);
3470 last = emit_move_insn (target, result);
3471 if (mov_optab->handlers[(int) GET_MODE (target)].insn_code
3472 != CODE_FOR_nothing)
3473 set_unique_reg_note (last, REG_EQUAL, copy_rtx (equiv));
3474 else
3476 /* Remove any existing REG_EQUAL note from "last", or else it will
3477 be mistaken for a note referring to the full contents of the
3478 libcall value when found together with the REG_RETVAL note added
3479 below. An existing note can come from an insn expansion at
3480 "last". */
3481 remove_note (last, find_reg_note (last, REG_EQUAL, NULL_RTX));
3484 if (final_dest != target)
3485 emit_move_insn (final_dest, target);
3487 if (prev == 0)
3488 first = get_insns ();
3489 else
3490 first = NEXT_INSN (prev);
3492 /* Encapsulate the block so it gets manipulated as a unit. */
3493 if (!flag_non_call_exceptions || !may_trap_p (equiv))
3495 /* We can't attach the REG_LIBCALL and REG_RETVAL notes
3496 when the encapsulated region would not be in one basic block,
3497 i.e. when there is a control_flow_insn_p insn between FIRST and LAST.
3499 bool attach_libcall_retval_notes = true;
3500 next = NEXT_INSN (last);
3501 for (insn = first; insn != next; insn = NEXT_INSN (insn))
3502 if (control_flow_insn_p (insn))
3504 attach_libcall_retval_notes = false;
3505 break;
3508 if (attach_libcall_retval_notes)
3510 REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
3511 REG_NOTES (first));
3512 REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first,
3513 REG_NOTES (last));
3518 /* Generate code to store zero in X. */
3520 void
3521 emit_clr_insn (x)
3522 rtx x;
3524 emit_move_insn (x, const0_rtx);
3527 /* Generate code to store 1 in X
3528 assuming it contains zero beforehand. */
3530 void
3531 emit_0_to_1_insn (x)
3532 rtx x;
3534 emit_move_insn (x, const1_rtx);
3537 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3538 PURPOSE describes how this comparison will be used. CODE is the rtx
3539 comparison code we will be using.
3541 ??? Actually, CODE is slightly weaker than that. A target is still
3542 required to implement all of the normal bcc operations, but not
3543 required to implement all (or any) of the unordered bcc operations. */
3546 can_compare_p (code, mode, purpose)
3547 enum rtx_code code;
3548 enum machine_mode mode;
3549 enum can_compare_purpose purpose;
3553 if (cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3555 if (purpose == ccp_jump)
3556 return bcc_gen_fctn[(int) code] != NULL;
3557 else if (purpose == ccp_store_flag)
3558 return setcc_gen_code[(int) code] != CODE_FOR_nothing;
3559 else
3560 /* There's only one cmov entry point, and it's allowed to fail. */
3561 return 1;
3563 if (purpose == ccp_jump
3564 && cbranch_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3565 return 1;
3566 if (purpose == ccp_cmov
3567 && cmov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3568 return 1;
3569 if (purpose == ccp_store_flag
3570 && cstore_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3571 return 1;
3573 mode = GET_MODE_WIDER_MODE (mode);
3575 while (mode != VOIDmode);
3577 return 0;
3580 /* This function is called when we are going to emit a compare instruction that
3581 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
3583 *PMODE is the mode of the inputs (in case they are const_int).
3584 *PUNSIGNEDP nonzero says that the operands are unsigned;
3585 this matters if they need to be widened.
3587 If they have mode BLKmode, then SIZE specifies the size of both operands.
3589 This function performs all the setup necessary so that the caller only has
3590 to emit a single comparison insn. This setup can involve doing a BLKmode
3591 comparison or emitting a library call to perform the comparison if no insn
3592 is available to handle it.
3593 The values which are passed in through pointers can be modified; the caller
3594 should perform the comparison on the modified values. */
3596 static void
3597 prepare_cmp_insn (px, py, pcomparison, size, pmode, punsignedp, purpose)
3598 rtx *px, *py;
3599 enum rtx_code *pcomparison;
3600 rtx size;
3601 enum machine_mode *pmode;
3602 int *punsignedp;
3603 enum can_compare_purpose purpose;
3605 enum machine_mode mode = *pmode;
3606 rtx x = *px, y = *py;
3607 int unsignedp = *punsignedp;
3608 enum mode_class class;
3610 class = GET_MODE_CLASS (mode);
3612 /* They could both be VOIDmode if both args are immediate constants,
3613 but we should fold that at an earlier stage.
3614 With no special code here, this will call abort,
3615 reminding the programmer to implement such folding. */
3617 if (mode != BLKmode && flag_force_mem)
3619 /* Load duplicate non-volatile operands once. */
3620 if (rtx_equal_p (x, y) && ! volatile_refs_p (x))
3622 x = force_not_mem (x);
3623 y = x;
3625 else
3627 x = force_not_mem (x);
3628 y = force_not_mem (y);
3632 /* If we are inside an appropriately-short loop and one operand is an
3633 expensive constant, force it into a register. */
3634 if (CONSTANT_P (x) && preserve_subexpressions_p ()
3635 && rtx_cost (x, COMPARE) > COSTS_N_INSNS (1))
3636 x = force_reg (mode, x);
3638 if (CONSTANT_P (y) && preserve_subexpressions_p ()
3639 && rtx_cost (y, COMPARE) > COSTS_N_INSNS (1))
3640 y = force_reg (mode, y);
3642 #ifdef HAVE_cc0
3643 /* Abort if we have a non-canonical comparison. The RTL documentation
3644 states that canonical comparisons are required only for targets which
3645 have cc0. */
3646 if (CONSTANT_P (x) && ! CONSTANT_P (y))
3647 abort ();
3648 #endif
3650 /* Don't let both operands fail to indicate the mode. */
3651 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
3652 x = force_reg (mode, x);
3654 /* Handle all BLKmode compares. */
3656 if (mode == BLKmode)
3658 rtx result;
3659 enum machine_mode result_mode;
3660 rtx opalign ATTRIBUTE_UNUSED
3661 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
3663 emit_queue ();
3664 x = protect_from_queue (x, 0);
3665 y = protect_from_queue (y, 0);
3667 if (size == 0)
3668 abort ();
3669 #ifdef HAVE_cmpstrqi
3670 if (HAVE_cmpstrqi
3671 && GET_CODE (size) == CONST_INT
3672 && INTVAL (size) < (1 << GET_MODE_BITSIZE (QImode)))
3674 result_mode = insn_data[(int) CODE_FOR_cmpstrqi].operand[0].mode;
3675 result = gen_reg_rtx (result_mode);
3676 emit_insn (gen_cmpstrqi (result, x, y, size, opalign));
3678 else
3679 #endif
3680 #ifdef HAVE_cmpstrhi
3681 if (HAVE_cmpstrhi
3682 && GET_CODE (size) == CONST_INT
3683 && INTVAL (size) < (1 << GET_MODE_BITSIZE (HImode)))
3685 result_mode = insn_data[(int) CODE_FOR_cmpstrhi].operand[0].mode;
3686 result = gen_reg_rtx (result_mode);
3687 emit_insn (gen_cmpstrhi (result, x, y, size, opalign));
3689 else
3690 #endif
3691 #ifdef HAVE_cmpstrsi
3692 if (HAVE_cmpstrsi)
3694 result_mode = insn_data[(int) CODE_FOR_cmpstrsi].operand[0].mode;
3695 result = gen_reg_rtx (result_mode);
3696 size = protect_from_queue (size, 0);
3697 emit_insn (gen_cmpstrsi (result, x, y,
3698 convert_to_mode (SImode, size, 1),
3699 opalign));
3701 else
3702 #endif
3704 #ifdef TARGET_MEM_FUNCTIONS
3705 result = emit_library_call_value (memcmp_libfunc, NULL_RTX, LCT_PURE_MAKE_BLOCK,
3706 TYPE_MODE (integer_type_node), 3,
3707 XEXP (x, 0), Pmode, XEXP (y, 0), Pmode,
3708 convert_to_mode (TYPE_MODE (sizetype), size,
3709 TREE_UNSIGNED (sizetype)),
3710 TYPE_MODE (sizetype));
3711 #else
3712 result = emit_library_call_value (bcmp_libfunc, NULL_RTX, LCT_PURE_MAKE_BLOCK,
3713 TYPE_MODE (integer_type_node), 3,
3714 XEXP (x, 0), Pmode, XEXP (y, 0), Pmode,
3715 convert_to_mode (TYPE_MODE (integer_type_node),
3716 size,
3717 TREE_UNSIGNED (integer_type_node)),
3718 TYPE_MODE (integer_type_node));
3719 #endif
3721 result_mode = TYPE_MODE (integer_type_node);
3723 *px = result;
3724 *py = const0_rtx;
3725 *pmode = result_mode;
3726 return;
3729 *px = x;
3730 *py = y;
3731 if (can_compare_p (*pcomparison, mode, purpose))
3732 return;
3734 /* Handle a lib call just for the mode we are using. */
3736 if (cmp_optab->handlers[(int) mode].libfunc && class != MODE_FLOAT)
3738 rtx libfunc = cmp_optab->handlers[(int) mode].libfunc;
3739 rtx result;
3741 /* If we want unsigned, and this mode has a distinct unsigned
3742 comparison routine, use that. */
3743 if (unsignedp && ucmp_optab->handlers[(int) mode].libfunc)
3744 libfunc = ucmp_optab->handlers[(int) mode].libfunc;
3746 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST_MAKE_BLOCK,
3747 word_mode, 2, x, mode, y, mode);
3749 /* Integer comparison returns a result that must be compared against 1,
3750 so that even if we do an unsigned compare afterward,
3751 there is still a value that can represent the result "less than". */
3752 *px = result;
3753 *py = const1_rtx;
3754 *pmode = word_mode;
3755 return;
3758 if (class == MODE_FLOAT)
3759 prepare_float_lib_cmp (px, py, pcomparison, pmode, punsignedp);
3761 else
3762 abort ();
3765 /* Before emitting an insn with code ICODE, make sure that X, which is going
3766 to be used for operand OPNUM of the insn, is converted from mode MODE to
3767 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
3768 that it is accepted by the operand predicate. Return the new value. */
3771 prepare_operand (icode, x, opnum, mode, wider_mode, unsignedp)
3772 int icode;
3773 rtx x;
3774 int opnum;
3775 enum machine_mode mode, wider_mode;
3776 int unsignedp;
3778 x = protect_from_queue (x, 0);
3780 if (mode != wider_mode)
3781 x = convert_modes (wider_mode, mode, x, unsignedp);
3783 if (! (*insn_data[icode].operand[opnum].predicate)
3784 (x, insn_data[icode].operand[opnum].mode))
3785 x = copy_to_mode_reg (insn_data[icode].operand[opnum].mode, x);
3786 return x;
3789 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
3790 we can do the comparison.
3791 The arguments are the same as for emit_cmp_and_jump_insns; but LABEL may
3792 be NULL_RTX which indicates that only a comparison is to be generated. */
3794 static void
3795 emit_cmp_and_jump_insn_1 (x, y, mode, comparison, unsignedp, label)
3796 rtx x, y;
3797 enum machine_mode mode;
3798 enum rtx_code comparison;
3799 int unsignedp;
3800 rtx label;
3802 rtx test = gen_rtx_fmt_ee (comparison, mode, x, y);
3803 enum mode_class class = GET_MODE_CLASS (mode);
3804 enum machine_mode wider_mode = mode;
3806 /* Try combined insns first. */
3809 enum insn_code icode;
3810 PUT_MODE (test, wider_mode);
3812 if (label)
3814 icode = cbranch_optab->handlers[(int) wider_mode].insn_code;
3816 if (icode != CODE_FOR_nothing
3817 && (*insn_data[icode].operand[0].predicate) (test, wider_mode))
3819 x = prepare_operand (icode, x, 1, mode, wider_mode, unsignedp);
3820 y = prepare_operand (icode, y, 2, mode, wider_mode, unsignedp);
3821 emit_jump_insn (GEN_FCN (icode) (test, x, y, label));
3822 return;
3826 /* Handle some compares against zero. */
3827 icode = (int) tst_optab->handlers[(int) wider_mode].insn_code;
3828 if (y == CONST0_RTX (mode) && icode != CODE_FOR_nothing)
3830 x = prepare_operand (icode, x, 0, mode, wider_mode, unsignedp);
3831 emit_insn (GEN_FCN (icode) (x));
3832 if (label)
3833 emit_jump_insn ((*bcc_gen_fctn[(int) comparison]) (label));
3834 return;
3837 /* Handle compares for which there is a directly suitable insn. */
3839 icode = (int) cmp_optab->handlers[(int) wider_mode].insn_code;
3840 if (icode != CODE_FOR_nothing)
3842 x = prepare_operand (icode, x, 0, mode, wider_mode, unsignedp);
3843 y = prepare_operand (icode, y, 1, mode, wider_mode, unsignedp);
3844 emit_insn (GEN_FCN (icode) (x, y));
3845 if (label)
3846 emit_jump_insn ((*bcc_gen_fctn[(int) comparison]) (label));
3847 return;
3850 if (class != MODE_INT && class != MODE_FLOAT
3851 && class != MODE_COMPLEX_FLOAT)
3852 break;
3854 wider_mode = GET_MODE_WIDER_MODE (wider_mode);
3856 while (wider_mode != VOIDmode);
3858 abort ();
3861 /* Generate code to compare X with Y so that the condition codes are
3862 set and to jump to LABEL if the condition is true. If X is a
3863 constant and Y is not a constant, then the comparison is swapped to
3864 ensure that the comparison RTL has the canonical form.
3866 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
3867 need to be widened by emit_cmp_insn. UNSIGNEDP is also used to select
3868 the proper branch condition code.
3870 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
3872 MODE is the mode of the inputs (in case they are const_int).
3874 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). It will
3875 be passed unchanged to emit_cmp_insn, then potentially converted into an
3876 unsigned variant based on UNSIGNEDP to select a proper jump instruction. */
3878 void
3879 emit_cmp_and_jump_insns (x, y, comparison, size, mode, unsignedp, label)
3880 rtx x, y;
3881 enum rtx_code comparison;
3882 rtx size;
3883 enum machine_mode mode;
3884 int unsignedp;
3885 rtx label;
3887 rtx op0 = x, op1 = y;
3889 /* Swap operands and condition to ensure canonical RTL. */
3890 if (swap_commutative_operands_p (x, y))
3892 /* If we're not emitting a branch, this means some caller
3893 is out of sync. */
3894 if (! label)
3895 abort ();
3897 op0 = y, op1 = x;
3898 comparison = swap_condition (comparison);
3901 #ifdef HAVE_cc0
3902 /* If OP0 is still a constant, then both X and Y must be constants. Force
3903 X into a register to avoid aborting in emit_cmp_insn due to non-canonical
3904 RTL. */
3905 if (CONSTANT_P (op0))
3906 op0 = force_reg (mode, op0);
3907 #endif
3909 emit_queue ();
3910 if (unsignedp)
3911 comparison = unsigned_condition (comparison);
3913 prepare_cmp_insn (&op0, &op1, &comparison, size, &mode, &unsignedp,
3914 ccp_jump);
3915 emit_cmp_and_jump_insn_1 (op0, op1, mode, comparison, unsignedp, label);
3918 /* Like emit_cmp_and_jump_insns, but generate only the comparison. */
3920 void
3921 emit_cmp_insn (x, y, comparison, size, mode, unsignedp)
3922 rtx x, y;
3923 enum rtx_code comparison;
3924 rtx size;
3925 enum machine_mode mode;
3926 int unsignedp;
3928 emit_cmp_and_jump_insns (x, y, comparison, size, mode, unsignedp, 0);
3931 /* Emit a library call comparison between floating point X and Y.
3932 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
3934 static void
3935 prepare_float_lib_cmp (px, py, pcomparison, pmode, punsignedp)
3936 rtx *px, *py;
3937 enum rtx_code *pcomparison;
3938 enum machine_mode *pmode;
3939 int *punsignedp;
3941 enum rtx_code comparison = *pcomparison;
3942 rtx tmp;
3943 rtx x = *px = protect_from_queue (*px, 0);
3944 rtx y = *py = protect_from_queue (*py, 0);
3945 enum machine_mode mode = GET_MODE (x);
3946 rtx libfunc = 0;
3947 rtx result;
3949 if (mode == HFmode)
3950 switch (comparison)
3952 case EQ:
3953 libfunc = eqhf2_libfunc;
3954 break;
3956 case NE:
3957 libfunc = nehf2_libfunc;
3958 break;
3960 case GT:
3961 libfunc = gthf2_libfunc;
3962 if (libfunc == NULL_RTX)
3964 tmp = x; x = y; y = tmp;
3965 *pcomparison = LT;
3966 libfunc = lthf2_libfunc;
3968 break;
3970 case GE:
3971 libfunc = gehf2_libfunc;
3972 if (libfunc == NULL_RTX)
3974 tmp = x; x = y; y = tmp;
3975 *pcomparison = LE;
3976 libfunc = lehf2_libfunc;
3978 break;
3980 case LT:
3981 libfunc = lthf2_libfunc;
3982 if (libfunc == NULL_RTX)
3984 tmp = x; x = y; y = tmp;
3985 *pcomparison = GT;
3986 libfunc = gthf2_libfunc;
3988 break;
3990 case LE:
3991 libfunc = lehf2_libfunc;
3992 if (libfunc == NULL_RTX)
3994 tmp = x; x = y; y = tmp;
3995 *pcomparison = GE;
3996 libfunc = gehf2_libfunc;
3998 break;
4000 case UNORDERED:
4001 libfunc = unordhf2_libfunc;
4002 break;
4004 default:
4005 break;
4007 else if (mode == SFmode)
4008 switch (comparison)
4010 case EQ:
4011 libfunc = eqsf2_libfunc;
4012 break;
4014 case NE:
4015 libfunc = nesf2_libfunc;
4016 break;
4018 case GT:
4019 libfunc = gtsf2_libfunc;
4020 if (libfunc == NULL_RTX)
4022 tmp = x; x = y; y = tmp;
4023 *pcomparison = LT;
4024 libfunc = ltsf2_libfunc;
4026 break;
4028 case GE:
4029 libfunc = gesf2_libfunc;
4030 if (libfunc == NULL_RTX)
4032 tmp = x; x = y; y = tmp;
4033 *pcomparison = LE;
4034 libfunc = lesf2_libfunc;
4036 break;
4038 case LT:
4039 libfunc = ltsf2_libfunc;
4040 if (libfunc == NULL_RTX)
4042 tmp = x; x = y; y = tmp;
4043 *pcomparison = GT;
4044 libfunc = gtsf2_libfunc;
4046 break;
4048 case LE:
4049 libfunc = lesf2_libfunc;
4050 if (libfunc == NULL_RTX)
4052 tmp = x; x = y; y = tmp;
4053 *pcomparison = GE;
4054 libfunc = gesf2_libfunc;
4056 break;
4058 case UNORDERED:
4059 libfunc = unordsf2_libfunc;
4060 break;
4062 default:
4063 break;
4065 else if (mode == DFmode)
4066 switch (comparison)
4068 case EQ:
4069 libfunc = eqdf2_libfunc;
4070 break;
4072 case NE:
4073 libfunc = nedf2_libfunc;
4074 break;
4076 case GT:
4077 libfunc = gtdf2_libfunc;
4078 if (libfunc == NULL_RTX)
4080 tmp = x; x = y; y = tmp;
4081 *pcomparison = LT;
4082 libfunc = ltdf2_libfunc;
4084 break;
4086 case GE:
4087 libfunc = gedf2_libfunc;
4088 if (libfunc == NULL_RTX)
4090 tmp = x; x = y; y = tmp;
4091 *pcomparison = LE;
4092 libfunc = ledf2_libfunc;
4094 break;
4096 case LT:
4097 libfunc = ltdf2_libfunc;
4098 if (libfunc == NULL_RTX)
4100 tmp = x; x = y; y = tmp;
4101 *pcomparison = GT;
4102 libfunc = gtdf2_libfunc;
4104 break;
4106 case LE:
4107 libfunc = ledf2_libfunc;
4108 if (libfunc == NULL_RTX)
4110 tmp = x; x = y; y = tmp;
4111 *pcomparison = GE;
4112 libfunc = gedf2_libfunc;
4114 break;
4116 case UNORDERED:
4117 libfunc = unorddf2_libfunc;
4118 break;
4120 default:
4121 break;
4123 else if (mode == XFmode)
4124 switch (comparison)
4126 case EQ:
4127 libfunc = eqxf2_libfunc;
4128 break;
4130 case NE:
4131 libfunc = nexf2_libfunc;
4132 break;
4134 case GT:
4135 libfunc = gtxf2_libfunc;
4136 if (libfunc == NULL_RTX)
4138 tmp = x; x = y; y = tmp;
4139 *pcomparison = LT;
4140 libfunc = ltxf2_libfunc;
4142 break;
4144 case GE:
4145 libfunc = gexf2_libfunc;
4146 if (libfunc == NULL_RTX)
4148 tmp = x; x = y; y = tmp;
4149 *pcomparison = LE;
4150 libfunc = lexf2_libfunc;
4152 break;
4154 case LT:
4155 libfunc = ltxf2_libfunc;
4156 if (libfunc == NULL_RTX)
4158 tmp = x; x = y; y = tmp;
4159 *pcomparison = GT;
4160 libfunc = gtxf2_libfunc;
4162 break;
4164 case LE:
4165 libfunc = lexf2_libfunc;
4166 if (libfunc == NULL_RTX)
4168 tmp = x; x = y; y = tmp;
4169 *pcomparison = GE;
4170 libfunc = gexf2_libfunc;
4172 break;
4174 case UNORDERED:
4175 libfunc = unordxf2_libfunc;
4176 break;
4178 default:
4179 break;
4181 else if (mode == TFmode)
4182 switch (comparison)
4184 case EQ:
4185 libfunc = eqtf2_libfunc;
4186 break;
4188 case NE:
4189 libfunc = netf2_libfunc;
4190 break;
4192 case GT:
4193 libfunc = gttf2_libfunc;
4194 if (libfunc == NULL_RTX)
4196 tmp = x; x = y; y = tmp;
4197 *pcomparison = LT;
4198 libfunc = lttf2_libfunc;
4200 break;
4202 case GE:
4203 libfunc = getf2_libfunc;
4204 if (libfunc == NULL_RTX)
4206 tmp = x; x = y; y = tmp;
4207 *pcomparison = LE;
4208 libfunc = letf2_libfunc;
4210 break;
4212 case LT:
4213 libfunc = lttf2_libfunc;
4214 if (libfunc == NULL_RTX)
4216 tmp = x; x = y; y = tmp;
4217 *pcomparison = GT;
4218 libfunc = gttf2_libfunc;
4220 break;
4222 case LE:
4223 libfunc = letf2_libfunc;
4224 if (libfunc == NULL_RTX)
4226 tmp = x; x = y; y = tmp;
4227 *pcomparison = GE;
4228 libfunc = getf2_libfunc;
4230 break;
4232 case UNORDERED:
4233 libfunc = unordtf2_libfunc;
4234 break;
4236 default:
4237 break;
4239 else
4241 enum machine_mode wider_mode;
4243 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
4244 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
4246 if ((cmp_optab->handlers[(int) wider_mode].insn_code
4247 != CODE_FOR_nothing)
4248 || (cmp_optab->handlers[(int) wider_mode].libfunc != 0))
4250 x = protect_from_queue (x, 0);
4251 y = protect_from_queue (y, 0);
4252 *px = convert_to_mode (wider_mode, x, 0);
4253 *py = convert_to_mode (wider_mode, y, 0);
4254 prepare_float_lib_cmp (px, py, pcomparison, pmode, punsignedp);
4255 return;
4258 abort ();
4261 if (libfunc == 0)
4262 abort ();
4264 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST_MAKE_BLOCK,
4265 word_mode, 2, x, mode, y, mode);
4266 *px = result;
4267 *py = const0_rtx;
4268 *pmode = word_mode;
4269 if (comparison == UNORDERED)
4270 *pcomparison = NE;
4271 #ifdef FLOAT_LIB_COMPARE_RETURNS_BOOL
4272 else if (FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4273 *pcomparison = NE;
4274 #endif
4275 *punsignedp = 0;
4278 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4280 void
4281 emit_indirect_jump (loc)
4282 rtx loc;
4284 if (! ((*insn_data[(int) CODE_FOR_indirect_jump].operand[0].predicate)
4285 (loc, Pmode)))
4286 loc = copy_to_mode_reg (Pmode, loc);
4288 emit_jump_insn (gen_indirect_jump (loc));
4289 emit_barrier ();
4292 #ifdef HAVE_conditional_move
4294 /* Emit a conditional move instruction if the machine supports one for that
4295 condition and machine mode.
4297 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4298 the mode to use should they be constants. If it is VOIDmode, they cannot
4299 both be constants.
4301 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4302 should be stored there. MODE is the mode to use should they be constants.
4303 If it is VOIDmode, they cannot both be constants.
4305 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4306 is not supported. */
4309 emit_conditional_move (target, code, op0, op1, cmode, op2, op3, mode,
4310 unsignedp)
4311 rtx target;
4312 enum rtx_code code;
4313 rtx op0, op1;
4314 enum machine_mode cmode;
4315 rtx op2, op3;
4316 enum machine_mode mode;
4317 int unsignedp;
4319 rtx tem, subtarget, comparison, insn;
4320 enum insn_code icode;
4321 enum rtx_code reversed;
4323 /* If one operand is constant, make it the second one. Only do this
4324 if the other operand is not constant as well. */
4326 if (swap_commutative_operands_p (op0, op1))
4328 tem = op0;
4329 op0 = op1;
4330 op1 = tem;
4331 code = swap_condition (code);
4334 /* get_condition will prefer to generate LT and GT even if the old
4335 comparison was against zero, so undo that canonicalization here since
4336 comparisons against zero are cheaper. */
4337 if (code == LT && GET_CODE (op1) == CONST_INT && INTVAL (op1) == 1)
4338 code = LE, op1 = const0_rtx;
4339 else if (code == GT && GET_CODE (op1) == CONST_INT && INTVAL (op1) == -1)
4340 code = GE, op1 = const0_rtx;
4342 if (cmode == VOIDmode)
4343 cmode = GET_MODE (op0);
4345 if (swap_commutative_operands_p (op2, op3)
4346 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4347 != UNKNOWN))
4349 tem = op2;
4350 op2 = op3;
4351 op3 = tem;
4352 code = reversed;
4355 if (mode == VOIDmode)
4356 mode = GET_MODE (op2);
4358 icode = movcc_gen_code[mode];
4360 if (icode == CODE_FOR_nothing)
4361 return 0;
4363 if (flag_force_mem)
4365 op2 = force_not_mem (op2);
4366 op3 = force_not_mem (op3);
4369 if (target)
4370 target = protect_from_queue (target, 1);
4371 else
4372 target = gen_reg_rtx (mode);
4374 subtarget = target;
4376 emit_queue ();
4378 op2 = protect_from_queue (op2, 0);
4379 op3 = protect_from_queue (op3, 0);
4381 /* If the insn doesn't accept these operands, put them in pseudos. */
4383 if (! (*insn_data[icode].operand[0].predicate)
4384 (subtarget, insn_data[icode].operand[0].mode))
4385 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
4387 if (! (*insn_data[icode].operand[2].predicate)
4388 (op2, insn_data[icode].operand[2].mode))
4389 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
4391 if (! (*insn_data[icode].operand[3].predicate)
4392 (op3, insn_data[icode].operand[3].mode))
4393 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
4395 /* Everything should now be in the suitable form, so emit the compare insn
4396 and then the conditional move. */
4398 comparison
4399 = compare_from_rtx (op0, op1, code, unsignedp, cmode, NULL_RTX);
4401 /* ??? Watch for const0_rtx (nop) and const_true_rtx (unconditional)? */
4402 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4403 return NULL and let the caller figure out how best to deal with this
4404 situation. */
4405 if (GET_CODE (comparison) != code)
4406 return NULL_RTX;
4408 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
4410 /* If that failed, then give up. */
4411 if (insn == 0)
4412 return 0;
4414 emit_insn (insn);
4416 if (subtarget != target)
4417 convert_move (target, subtarget, 0);
4419 return target;
4422 /* Return nonzero if a conditional move of mode MODE is supported.
4424 This function is for combine so it can tell whether an insn that looks
4425 like a conditional move is actually supported by the hardware. If we
4426 guess wrong we lose a bit on optimization, but that's it. */
4427 /* ??? sparc64 supports conditionally moving integers values based on fp
4428 comparisons, and vice versa. How do we handle them? */
4431 can_conditionally_move_p (mode)
4432 enum machine_mode mode;
4434 if (movcc_gen_code[mode] != CODE_FOR_nothing)
4435 return 1;
4437 return 0;
4440 #endif /* HAVE_conditional_move */
4442 /* Emit a conditional addition instruction if the machine supports one for that
4443 condition and machine mode.
4445 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4446 the mode to use should they be constants. If it is VOIDmode, they cannot
4447 both be constants.
4449 OP2 should be stored in TARGET if the comparison is true, otherwise OP2+OP3
4450 should be stored there. MODE is the mode to use should they be constants.
4451 If it is VOIDmode, they cannot both be constants.
4453 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4454 is not supported. */
4457 emit_conditional_add (target, code, op0, op1, cmode, op2, op3, mode,
4458 unsignedp)
4459 rtx target;
4460 enum rtx_code code;
4461 rtx op0, op1;
4462 enum machine_mode cmode;
4463 rtx op2, op3;
4464 enum machine_mode mode;
4465 int unsignedp;
4467 rtx tem, subtarget, comparison, insn;
4468 enum insn_code icode;
4469 enum rtx_code reversed;
4471 /* If one operand is constant, make it the second one. Only do this
4472 if the other operand is not constant as well. */
4474 if (swap_commutative_operands_p (op0, op1))
4476 tem = op0;
4477 op0 = op1;
4478 op1 = tem;
4479 code = swap_condition (code);
4482 /* get_condition will prefer to generate LT and GT even if the old
4483 comparison was against zero, so undo that canonicalization here since
4484 comparisons against zero are cheaper. */
4485 if (code == LT && GET_CODE (op1) == CONST_INT && INTVAL (op1) == 1)
4486 code = LE, op1 = const0_rtx;
4487 else if (code == GT && GET_CODE (op1) == CONST_INT && INTVAL (op1) == -1)
4488 code = GE, op1 = const0_rtx;
4490 if (cmode == VOIDmode)
4491 cmode = GET_MODE (op0);
4493 if (swap_commutative_operands_p (op2, op3)
4494 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4495 != UNKNOWN))
4497 tem = op2;
4498 op2 = op3;
4499 op3 = tem;
4500 code = reversed;
4503 if (mode == VOIDmode)
4504 mode = GET_MODE (op2);
4506 icode = addcc_optab->handlers[(int) mode].insn_code;
4508 if (icode == CODE_FOR_nothing)
4509 return 0;
4511 if (flag_force_mem)
4513 op2 = force_not_mem (op2);
4514 op3 = force_not_mem (op3);
4517 if (target)
4518 target = protect_from_queue (target, 1);
4519 else
4520 target = gen_reg_rtx (mode);
4522 subtarget = target;
4524 emit_queue ();
4526 op2 = protect_from_queue (op2, 0);
4527 op3 = protect_from_queue (op3, 0);
4529 /* If the insn doesn't accept these operands, put them in pseudos. */
4531 if (! (*insn_data[icode].operand[0].predicate)
4532 (subtarget, insn_data[icode].operand[0].mode))
4533 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
4535 if (! (*insn_data[icode].operand[2].predicate)
4536 (op2, insn_data[icode].operand[2].mode))
4537 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
4539 if (! (*insn_data[icode].operand[3].predicate)
4540 (op3, insn_data[icode].operand[3].mode))
4541 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
4543 /* Everything should now be in the suitable form, so emit the compare insn
4544 and then the conditional move. */
4546 comparison
4547 = compare_from_rtx (op0, op1, code, unsignedp, cmode, NULL_RTX);
4549 /* ??? Watch for const0_rtx (nop) and const_true_rtx (unconditional)? */
4550 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4551 return NULL and let the caller figure out how best to deal with this
4552 situation. */
4553 if (GET_CODE (comparison) != code)
4554 return NULL_RTX;
4556 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
4558 /* If that failed, then give up. */
4559 if (insn == 0)
4560 return 0;
4562 emit_insn (insn);
4564 if (subtarget != target)
4565 convert_move (target, subtarget, 0);
4567 return target;
4570 /* These functions attempt to generate an insn body, rather than
4571 emitting the insn, but if the gen function already emits them, we
4572 make no attempt to turn them back into naked patterns.
4574 They do not protect from queued increments,
4575 because they may be used 1) in protect_from_queue itself
4576 and 2) in other passes where there is no queue. */
4578 /* Generate and return an insn body to add Y to X. */
4581 gen_add2_insn (x, y)
4582 rtx x, y;
4584 int icode = (int) add_optab->handlers[(int) GET_MODE (x)].insn_code;
4586 if (! ((*insn_data[icode].operand[0].predicate)
4587 (x, insn_data[icode].operand[0].mode))
4588 || ! ((*insn_data[icode].operand[1].predicate)
4589 (x, insn_data[icode].operand[1].mode))
4590 || ! ((*insn_data[icode].operand[2].predicate)
4591 (y, insn_data[icode].operand[2].mode)))
4592 abort ();
4594 return (GEN_FCN (icode) (x, x, y));
4597 /* Generate and return an insn body to add r1 and c,
4598 storing the result in r0. */
4600 gen_add3_insn (r0, r1, c)
4601 rtx r0, r1, c;
4603 int icode = (int) add_optab->handlers[(int) GET_MODE (r0)].insn_code;
4605 if (icode == CODE_FOR_nothing
4606 || ! ((*insn_data[icode].operand[0].predicate)
4607 (r0, insn_data[icode].operand[0].mode))
4608 || ! ((*insn_data[icode].operand[1].predicate)
4609 (r1, insn_data[icode].operand[1].mode))
4610 || ! ((*insn_data[icode].operand[2].predicate)
4611 (c, insn_data[icode].operand[2].mode)))
4612 return NULL_RTX;
4614 return (GEN_FCN (icode) (r0, r1, c));
4618 have_add2_insn (x, y)
4619 rtx x, y;
4621 int icode;
4623 if (GET_MODE (x) == VOIDmode)
4624 abort ();
4626 icode = (int) add_optab->handlers[(int) GET_MODE (x)].insn_code;
4628 if (icode == CODE_FOR_nothing)
4629 return 0;
4631 if (! ((*insn_data[icode].operand[0].predicate)
4632 (x, insn_data[icode].operand[0].mode))
4633 || ! ((*insn_data[icode].operand[1].predicate)
4634 (x, insn_data[icode].operand[1].mode))
4635 || ! ((*insn_data[icode].operand[2].predicate)
4636 (y, insn_data[icode].operand[2].mode)))
4637 return 0;
4639 return 1;
4642 /* Generate and return an insn body to subtract Y from X. */
4645 gen_sub2_insn (x, y)
4646 rtx x, y;
4648 int icode = (int) sub_optab->handlers[(int) GET_MODE (x)].insn_code;
4650 if (! ((*insn_data[icode].operand[0].predicate)
4651 (x, insn_data[icode].operand[0].mode))
4652 || ! ((*insn_data[icode].operand[1].predicate)
4653 (x, insn_data[icode].operand[1].mode))
4654 || ! ((*insn_data[icode].operand[2].predicate)
4655 (y, insn_data[icode].operand[2].mode)))
4656 abort ();
4658 return (GEN_FCN (icode) (x, x, y));
4661 /* Generate and return an insn body to subtract r1 and c,
4662 storing the result in r0. */
4664 gen_sub3_insn (r0, r1, c)
4665 rtx r0, r1, c;
4667 int icode = (int) sub_optab->handlers[(int) GET_MODE (r0)].insn_code;
4669 if (icode == CODE_FOR_nothing
4670 || ! ((*insn_data[icode].operand[0].predicate)
4671 (r0, insn_data[icode].operand[0].mode))
4672 || ! ((*insn_data[icode].operand[1].predicate)
4673 (r1, insn_data[icode].operand[1].mode))
4674 || ! ((*insn_data[icode].operand[2].predicate)
4675 (c, insn_data[icode].operand[2].mode)))
4676 return NULL_RTX;
4678 return (GEN_FCN (icode) (r0, r1, c));
4682 have_sub2_insn (x, y)
4683 rtx x, y;
4685 int icode;
4687 if (GET_MODE (x) == VOIDmode)
4688 abort ();
4690 icode = (int) sub_optab->handlers[(int) GET_MODE (x)].insn_code;
4692 if (icode == CODE_FOR_nothing)
4693 return 0;
4695 if (! ((*insn_data[icode].operand[0].predicate)
4696 (x, insn_data[icode].operand[0].mode))
4697 || ! ((*insn_data[icode].operand[1].predicate)
4698 (x, insn_data[icode].operand[1].mode))
4699 || ! ((*insn_data[icode].operand[2].predicate)
4700 (y, insn_data[icode].operand[2].mode)))
4701 return 0;
4703 return 1;
4706 /* Generate the body of an instruction to copy Y into X.
4707 It may be a list of insns, if one insn isn't enough. */
4710 gen_move_insn (x, y)
4711 rtx x, y;
4713 rtx seq;
4715 start_sequence ();
4716 emit_move_insn_1 (x, y);
4717 seq = get_insns ();
4718 end_sequence ();
4719 return seq;
4722 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4723 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4724 no such operation exists, CODE_FOR_nothing will be returned. */
4726 enum insn_code
4727 can_extend_p (to_mode, from_mode, unsignedp)
4728 enum machine_mode to_mode, from_mode;
4729 int unsignedp;
4731 #ifdef HAVE_ptr_extend
4732 if (unsignedp < 0)
4733 return CODE_FOR_ptr_extend;
4734 else
4735 #endif
4736 return extendtab[(int) to_mode][(int) from_mode][unsignedp != 0];
4739 /* Generate the body of an insn to extend Y (with mode MFROM)
4740 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4743 gen_extend_insn (x, y, mto, mfrom, unsignedp)
4744 rtx x, y;
4745 enum machine_mode mto, mfrom;
4746 int unsignedp;
4748 return (GEN_FCN (extendtab[(int) mto][(int) mfrom][unsignedp != 0]) (x, y));
4751 /* can_fix_p and can_float_p say whether the target machine
4752 can directly convert a given fixed point type to
4753 a given floating point type, or vice versa.
4754 The returned value is the CODE_FOR_... value to use,
4755 or CODE_FOR_nothing if these modes cannot be directly converted.
4757 *TRUNCP_PTR is set to 1 if it is necessary to output
4758 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4760 static enum insn_code
4761 can_fix_p (fixmode, fltmode, unsignedp, truncp_ptr)
4762 enum machine_mode fltmode, fixmode;
4763 int unsignedp;
4764 int *truncp_ptr;
4766 *truncp_ptr = 0;
4767 if (fixtrunctab[(int) fltmode][(int) fixmode][unsignedp != 0]
4768 != CODE_FOR_nothing)
4769 return fixtrunctab[(int) fltmode][(int) fixmode][unsignedp != 0];
4771 if (ftrunc_optab->handlers[(int) fltmode].insn_code != CODE_FOR_nothing)
4773 *truncp_ptr = 1;
4774 return fixtab[(int) fltmode][(int) fixmode][unsignedp != 0];
4776 return CODE_FOR_nothing;
4779 static enum insn_code
4780 can_float_p (fltmode, fixmode, unsignedp)
4781 enum machine_mode fixmode, fltmode;
4782 int unsignedp;
4784 return floattab[(int) fltmode][(int) fixmode][unsignedp != 0];
4787 /* Generate code to convert FROM to floating point
4788 and store in TO. FROM must be fixed point and not VOIDmode.
4789 UNSIGNEDP nonzero means regard FROM as unsigned.
4790 Normally this is done by correcting the final value
4791 if it is negative. */
4793 void
4794 expand_float (to, from, unsignedp)
4795 rtx to, from;
4796 int unsignedp;
4798 enum insn_code icode;
4799 rtx target = to;
4800 enum machine_mode fmode, imode;
4802 /* Crash now, because we won't be able to decide which mode to use. */
4803 if (GET_MODE (from) == VOIDmode)
4804 abort ();
4806 /* Look for an insn to do the conversion. Do it in the specified
4807 modes if possible; otherwise convert either input, output or both to
4808 wider mode. If the integer mode is wider than the mode of FROM,
4809 we can do the conversion signed even if the input is unsigned. */
4811 for (fmode = GET_MODE (to); fmode != VOIDmode;
4812 fmode = GET_MODE_WIDER_MODE (fmode))
4813 for (imode = GET_MODE (from); imode != VOIDmode;
4814 imode = GET_MODE_WIDER_MODE (imode))
4816 int doing_unsigned = unsignedp;
4818 if (fmode != GET_MODE (to)
4819 && significand_size (fmode) < GET_MODE_BITSIZE (GET_MODE (from)))
4820 continue;
4822 icode = can_float_p (fmode, imode, unsignedp);
4823 if (icode == CODE_FOR_nothing && imode != GET_MODE (from) && unsignedp)
4824 icode = can_float_p (fmode, imode, 0), doing_unsigned = 0;
4826 if (icode != CODE_FOR_nothing)
4828 to = protect_from_queue (to, 1);
4829 from = protect_from_queue (from, 0);
4831 if (imode != GET_MODE (from))
4832 from = convert_to_mode (imode, from, unsignedp);
4834 if (fmode != GET_MODE (to))
4835 target = gen_reg_rtx (fmode);
4837 emit_unop_insn (icode, target, from,
4838 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4840 if (target != to)
4841 convert_move (to, target, 0);
4842 return;
4846 /* Unsigned integer, and no way to convert directly.
4847 Convert as signed, then conditionally adjust the result. */
4848 if (unsignedp)
4850 rtx label = gen_label_rtx ();
4851 rtx temp;
4852 REAL_VALUE_TYPE offset;
4854 emit_queue ();
4856 to = protect_from_queue (to, 1);
4857 from = protect_from_queue (from, 0);
4859 if (flag_force_mem)
4860 from = force_not_mem (from);
4862 /* Look for a usable floating mode FMODE wider than the source and at
4863 least as wide as the target. Using FMODE will avoid rounding woes
4864 with unsigned values greater than the signed maximum value. */
4866 for (fmode = GET_MODE (to); fmode != VOIDmode;
4867 fmode = GET_MODE_WIDER_MODE (fmode))
4868 if (GET_MODE_BITSIZE (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
4869 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
4870 break;
4872 if (fmode == VOIDmode)
4874 /* There is no such mode. Pretend the target is wide enough. */
4875 fmode = GET_MODE (to);
4877 /* Avoid double-rounding when TO is narrower than FROM. */
4878 if ((significand_size (fmode) + 1)
4879 < GET_MODE_BITSIZE (GET_MODE (from)))
4881 rtx temp1;
4882 rtx neglabel = gen_label_rtx ();
4884 /* Don't use TARGET if it isn't a register, is a hard register,
4885 or is the wrong mode. */
4886 if (GET_CODE (target) != REG
4887 || REGNO (target) < FIRST_PSEUDO_REGISTER
4888 || GET_MODE (target) != fmode)
4889 target = gen_reg_rtx (fmode);
4891 imode = GET_MODE (from);
4892 do_pending_stack_adjust ();
4894 /* Test whether the sign bit is set. */
4895 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
4896 0, neglabel);
4898 /* The sign bit is not set. Convert as signed. */
4899 expand_float (target, from, 0);
4900 emit_jump_insn (gen_jump (label));
4901 emit_barrier ();
4903 /* The sign bit is set.
4904 Convert to a usable (positive signed) value by shifting right
4905 one bit, while remembering if a nonzero bit was shifted
4906 out; i.e., compute (from & 1) | (from >> 1). */
4908 emit_label (neglabel);
4909 temp = expand_binop (imode, and_optab, from, const1_rtx,
4910 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4911 temp1 = expand_shift (RSHIFT_EXPR, imode, from, integer_one_node,
4912 NULL_RTX, 1);
4913 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
4914 OPTAB_LIB_WIDEN);
4915 expand_float (target, temp, 0);
4917 /* Multiply by 2 to undo the shift above. */
4918 temp = expand_binop (fmode, add_optab, target, target,
4919 target, 0, OPTAB_LIB_WIDEN);
4920 if (temp != target)
4921 emit_move_insn (target, temp);
4923 do_pending_stack_adjust ();
4924 emit_label (label);
4925 goto done;
4929 /* If we are about to do some arithmetic to correct for an
4930 unsigned operand, do it in a pseudo-register. */
4932 if (GET_MODE (to) != fmode
4933 || GET_CODE (to) != REG || REGNO (to) < FIRST_PSEUDO_REGISTER)
4934 target = gen_reg_rtx (fmode);
4936 /* Convert as signed integer to floating. */
4937 expand_float (target, from, 0);
4939 /* If FROM is negative (and therefore TO is negative),
4940 correct its value by 2**bitwidth. */
4942 do_pending_stack_adjust ();
4943 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
4944 0, label);
4947 real_2expN (&offset, GET_MODE_BITSIZE (GET_MODE (from)));
4948 temp = expand_binop (fmode, add_optab, target,
4949 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
4950 target, 0, OPTAB_LIB_WIDEN);
4951 if (temp != target)
4952 emit_move_insn (target, temp);
4954 do_pending_stack_adjust ();
4955 emit_label (label);
4956 goto done;
4959 /* No hardware instruction available; call a library routine to convert from
4960 SImode, DImode, or TImode into SFmode, DFmode, XFmode, or TFmode. */
4962 rtx libfcn;
4963 rtx insns;
4964 rtx value;
4966 to = protect_from_queue (to, 1);
4967 from = protect_from_queue (from, 0);
4969 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
4970 from = convert_to_mode (SImode, from, unsignedp);
4972 if (flag_force_mem)
4973 from = force_not_mem (from);
4975 if (GET_MODE (to) == SFmode)
4977 if (GET_MODE (from) == SImode)
4978 libfcn = floatsisf_libfunc;
4979 else if (GET_MODE (from) == DImode)
4980 libfcn = floatdisf_libfunc;
4981 else if (GET_MODE (from) == TImode)
4982 libfcn = floattisf_libfunc;
4983 else
4984 abort ();
4986 else if (GET_MODE (to) == DFmode)
4988 if (GET_MODE (from) == SImode)
4989 libfcn = floatsidf_libfunc;
4990 else if (GET_MODE (from) == DImode)
4991 libfcn = floatdidf_libfunc;
4992 else if (GET_MODE (from) == TImode)
4993 libfcn = floattidf_libfunc;
4994 else
4995 abort ();
4997 else if (GET_MODE (to) == XFmode)
4999 if (GET_MODE (from) == SImode)
5000 libfcn = floatsixf_libfunc;
5001 else if (GET_MODE (from) == DImode)
5002 libfcn = floatdixf_libfunc;
5003 else if (GET_MODE (from) == TImode)
5004 libfcn = floattixf_libfunc;
5005 else
5006 abort ();
5008 else if (GET_MODE (to) == TFmode)
5010 if (GET_MODE (from) == SImode)
5011 libfcn = floatsitf_libfunc;
5012 else if (GET_MODE (from) == DImode)
5013 libfcn = floatditf_libfunc;
5014 else if (GET_MODE (from) == TImode)
5015 libfcn = floattitf_libfunc;
5016 else
5017 abort ();
5019 else
5020 abort ();
5022 start_sequence ();
5024 value = emit_library_call_value (libfcn, NULL_RTX, LCT_CONST,
5025 GET_MODE (to), 1, from,
5026 GET_MODE (from));
5027 insns = get_insns ();
5028 end_sequence ();
5030 emit_libcall_block (insns, target, value,
5031 gen_rtx_FLOAT (GET_MODE (to), from));
5034 done:
5036 /* Copy result to requested destination
5037 if we have been computing in a temp location. */
5039 if (target != to)
5041 if (GET_MODE (target) == GET_MODE (to))
5042 emit_move_insn (to, target);
5043 else
5044 convert_move (to, target, 0);
5048 /* expand_fix: generate code to convert FROM to fixed point
5049 and store in TO. FROM must be floating point. */
5051 static rtx
5052 ftruncify (x)
5053 rtx x;
5055 rtx temp = gen_reg_rtx (GET_MODE (x));
5056 return expand_unop (GET_MODE (x), ftrunc_optab, x, temp, 0);
5059 void
5060 expand_fix (to, from, unsignedp)
5061 rtx to, from;
5062 int unsignedp;
5064 enum insn_code icode;
5065 rtx target = to;
5066 enum machine_mode fmode, imode;
5067 int must_trunc = 0;
5068 rtx libfcn = 0;
5070 /* We first try to find a pair of modes, one real and one integer, at
5071 least as wide as FROM and TO, respectively, in which we can open-code
5072 this conversion. If the integer mode is wider than the mode of TO,
5073 we can do the conversion either signed or unsigned. */
5075 for (fmode = GET_MODE (from); fmode != VOIDmode;
5076 fmode = GET_MODE_WIDER_MODE (fmode))
5077 for (imode = GET_MODE (to); imode != VOIDmode;
5078 imode = GET_MODE_WIDER_MODE (imode))
5080 int doing_unsigned = unsignedp;
5082 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5083 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5084 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5086 if (icode != CODE_FOR_nothing)
5088 to = protect_from_queue (to, 1);
5089 from = protect_from_queue (from, 0);
5091 if (fmode != GET_MODE (from))
5092 from = convert_to_mode (fmode, from, 0);
5094 if (must_trunc)
5095 from = ftruncify (from);
5097 if (imode != GET_MODE (to))
5098 target = gen_reg_rtx (imode);
5100 emit_unop_insn (icode, target, from,
5101 doing_unsigned ? UNSIGNED_FIX : FIX);
5102 if (target != to)
5103 convert_move (to, target, unsignedp);
5104 return;
5108 /* For an unsigned conversion, there is one more way to do it.
5109 If we have a signed conversion, we generate code that compares
5110 the real value to the largest representable positive number. If if
5111 is smaller, the conversion is done normally. Otherwise, subtract
5112 one plus the highest signed number, convert, and add it back.
5114 We only need to check all real modes, since we know we didn't find
5115 anything with a wider integer mode.
5117 This code used to extend FP value into mode wider than the destination.
5118 This is not needed. Consider, for instance conversion from SFmode
5119 into DImode.
5121 The hot path trought the code is dealing with inputs smaller than 2^63
5122 and doing just the conversion, so there is no bits to lose.
5124 In the other path we know the value is positive in the range 2^63..2^64-1
5125 inclusive. (as for other imput overflow happens and result is undefined)
5126 So we know that the most important bit set in mantissa corresponds to
5127 2^63. The subtraction of 2^63 should not generate any rounding as it
5128 simply clears out that bit. The rest is trivial. */
5130 if (unsignedp && GET_MODE_BITSIZE (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5131 for (fmode = GET_MODE (from); fmode != VOIDmode;
5132 fmode = GET_MODE_WIDER_MODE (fmode))
5133 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0,
5134 &must_trunc))
5136 int bitsize;
5137 REAL_VALUE_TYPE offset;
5138 rtx limit, lab1, lab2, insn;
5140 bitsize = GET_MODE_BITSIZE (GET_MODE (to));
5141 real_2expN (&offset, bitsize - 1);
5142 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5143 lab1 = gen_label_rtx ();
5144 lab2 = gen_label_rtx ();
5146 emit_queue ();
5147 to = protect_from_queue (to, 1);
5148 from = protect_from_queue (from, 0);
5150 if (flag_force_mem)
5151 from = force_not_mem (from);
5153 if (fmode != GET_MODE (from))
5154 from = convert_to_mode (fmode, from, 0);
5156 /* See if we need to do the subtraction. */
5157 do_pending_stack_adjust ();
5158 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5159 0, lab1);
5161 /* If not, do the signed "fix" and branch around fixup code. */
5162 expand_fix (to, from, 0);
5163 emit_jump_insn (gen_jump (lab2));
5164 emit_barrier ();
5166 /* Otherwise, subtract 2**(N-1), convert to signed number,
5167 then add 2**(N-1). Do the addition using XOR since this
5168 will often generate better code. */
5169 emit_label (lab1);
5170 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5171 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5172 expand_fix (to, target, 0);
5173 target = expand_binop (GET_MODE (to), xor_optab, to,
5174 gen_int_mode
5175 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5176 GET_MODE (to)),
5177 to, 1, OPTAB_LIB_WIDEN);
5179 if (target != to)
5180 emit_move_insn (to, target);
5182 emit_label (lab2);
5184 if (mov_optab->handlers[(int) GET_MODE (to)].insn_code
5185 != CODE_FOR_nothing)
5187 /* Make a place for a REG_NOTE and add it. */
5188 insn = emit_move_insn (to, to);
5189 set_unique_reg_note (insn,
5190 REG_EQUAL,
5191 gen_rtx_fmt_e (UNSIGNED_FIX,
5192 GET_MODE (to),
5193 copy_rtx (from)));
5196 return;
5199 /* We can't do it with an insn, so use a library call. But first ensure
5200 that the mode of TO is at least as wide as SImode, since those are the
5201 only library calls we know about. */
5203 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
5205 target = gen_reg_rtx (SImode);
5207 expand_fix (target, from, unsignedp);
5209 else if (GET_MODE (from) == SFmode)
5211 if (GET_MODE (to) == SImode)
5212 libfcn = unsignedp ? fixunssfsi_libfunc : fixsfsi_libfunc;
5213 else if (GET_MODE (to) == DImode)
5214 libfcn = unsignedp ? fixunssfdi_libfunc : fixsfdi_libfunc;
5215 else if (GET_MODE (to) == TImode)
5216 libfcn = unsignedp ? fixunssfti_libfunc : fixsfti_libfunc;
5217 else
5218 abort ();
5220 else if (GET_MODE (from) == DFmode)
5222 if (GET_MODE (to) == SImode)
5223 libfcn = unsignedp ? fixunsdfsi_libfunc : fixdfsi_libfunc;
5224 else if (GET_MODE (to) == DImode)
5225 libfcn = unsignedp ? fixunsdfdi_libfunc : fixdfdi_libfunc;
5226 else if (GET_MODE (to) == TImode)
5227 libfcn = unsignedp ? fixunsdfti_libfunc : fixdfti_libfunc;
5228 else
5229 abort ();
5231 else if (GET_MODE (from) == XFmode)
5233 if (GET_MODE (to) == SImode)
5234 libfcn = unsignedp ? fixunsxfsi_libfunc : fixxfsi_libfunc;
5235 else if (GET_MODE (to) == DImode)
5236 libfcn = unsignedp ? fixunsxfdi_libfunc : fixxfdi_libfunc;
5237 else if (GET_MODE (to) == TImode)
5238 libfcn = unsignedp ? fixunsxfti_libfunc : fixxfti_libfunc;
5239 else
5240 abort ();
5242 else if (GET_MODE (from) == TFmode)
5244 if (GET_MODE (to) == SImode)
5245 libfcn = unsignedp ? fixunstfsi_libfunc : fixtfsi_libfunc;
5246 else if (GET_MODE (to) == DImode)
5247 libfcn = unsignedp ? fixunstfdi_libfunc : fixtfdi_libfunc;
5248 else if (GET_MODE (to) == TImode)
5249 libfcn = unsignedp ? fixunstfti_libfunc : fixtfti_libfunc;
5250 else
5251 abort ();
5253 else
5254 abort ();
5256 if (libfcn)
5258 rtx insns;
5259 rtx value;
5261 to = protect_from_queue (to, 1);
5262 from = protect_from_queue (from, 0);
5264 if (flag_force_mem)
5265 from = force_not_mem (from);
5267 start_sequence ();
5269 value = emit_library_call_value (libfcn, NULL_RTX, LCT_CONST,
5270 GET_MODE (to), 1, from,
5271 GET_MODE (from));
5272 insns = get_insns ();
5273 end_sequence ();
5275 emit_libcall_block (insns, target, value,
5276 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5277 GET_MODE (to), from));
5280 if (target != to)
5282 if (GET_MODE (to) == GET_MODE (target))
5283 emit_move_insn (to, target);
5284 else
5285 convert_move (to, target, 0);
5289 /* Report whether we have an instruction to perform the operation
5290 specified by CODE on operands of mode MODE. */
5292 have_insn_for (code, mode)
5293 enum rtx_code code;
5294 enum machine_mode mode;
5296 return (code_to_optab[(int) code] != 0
5297 && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
5298 != CODE_FOR_nothing));
5301 /* Create a blank optab. */
5302 static optab
5303 new_optab ()
5305 int i;
5306 optab op = (optab) ggc_alloc (sizeof (struct optab));
5307 for (i = 0; i < NUM_MACHINE_MODES; i++)
5309 op->handlers[i].insn_code = CODE_FOR_nothing;
5310 op->handlers[i].libfunc = 0;
5313 return op;
5316 /* Same, but fill in its code as CODE, and write it into the
5317 code_to_optab table. */
5318 static inline optab
5319 init_optab (code)
5320 enum rtx_code code;
5322 optab op = new_optab ();
5323 op->code = code;
5324 code_to_optab[(int) code] = op;
5325 return op;
5328 /* Same, but fill in its code as CODE, and do _not_ write it into
5329 the code_to_optab table. */
5330 static inline optab
5331 init_optabv (code)
5332 enum rtx_code code;
5334 optab op = new_optab ();
5335 op->code = code;
5336 return op;
5339 /* Initialize the libfunc fields of an entire group of entries in some
5340 optab. Each entry is set equal to a string consisting of a leading
5341 pair of underscores followed by a generic operation name followed by
5342 a mode name (downshifted to lower case) followed by a single character
5343 representing the number of operands for the given operation (which is
5344 usually one of the characters '2', '3', or '4').
5346 OPTABLE is the table in which libfunc fields are to be initialized.
5347 FIRST_MODE is the first machine mode index in the given optab to
5348 initialize.
5349 LAST_MODE is the last machine mode index in the given optab to
5350 initialize.
5351 OPNAME is the generic (string) name of the operation.
5352 SUFFIX is the character which specifies the number of operands for
5353 the given generic operation.
5356 static void
5357 init_libfuncs (optable, first_mode, last_mode, opname, suffix)
5358 optab optable;
5359 int first_mode;
5360 int last_mode;
5361 const char *opname;
5362 int suffix;
5364 int mode;
5365 unsigned opname_len = strlen (opname);
5367 for (mode = first_mode; (int) mode <= (int) last_mode;
5368 mode = (enum machine_mode) ((int) mode + 1))
5370 const char *mname = GET_MODE_NAME (mode);
5371 unsigned mname_len = strlen (mname);
5372 char *libfunc_name = alloca (2 + opname_len + mname_len + 1 + 1);
5373 char *p;
5374 const char *q;
5376 p = libfunc_name;
5377 *p++ = '_';
5378 *p++ = '_';
5379 for (q = opname; *q; )
5380 *p++ = *q++;
5381 for (q = mname; *q; q++)
5382 *p++ = TOLOWER (*q);
5383 *p++ = suffix;
5384 *p = '\0';
5386 optable->handlers[(int) mode].libfunc
5387 = init_one_libfunc (ggc_alloc_string (libfunc_name, p - libfunc_name));
5391 /* Initialize the libfunc fields of an entire group of entries in some
5392 optab which correspond to all integer mode operations. The parameters
5393 have the same meaning as similarly named ones for the `init_libfuncs'
5394 routine. (See above). */
5396 static void
5397 init_integral_libfuncs (optable, opname, suffix)
5398 optab optable;
5399 const char *opname;
5400 int suffix;
5402 int maxsize = 2*BITS_PER_WORD;
5403 if (maxsize < LONG_LONG_TYPE_SIZE)
5404 maxsize = LONG_LONG_TYPE_SIZE;
5405 init_libfuncs (optable, word_mode,
5406 mode_for_size (maxsize, MODE_INT, 0),
5407 opname, suffix);
5410 /* Initialize the libfunc fields of an entire group of entries in some
5411 optab which correspond to all real mode operations. The parameters
5412 have the same meaning as similarly named ones for the `init_libfuncs'
5413 routine. (See above). */
5415 static void
5416 init_floating_libfuncs (optable, opname, suffix)
5417 optab optable;
5418 const char *opname;
5419 int suffix;
5421 enum machine_mode fmode, dmode, lmode;
5423 fmode = float_type_node ? TYPE_MODE (float_type_node) : VOIDmode;
5424 dmode = double_type_node ? TYPE_MODE (double_type_node) : VOIDmode;
5425 lmode = long_double_type_node ? TYPE_MODE (long_double_type_node) : VOIDmode;
5427 if (fmode != VOIDmode)
5428 init_libfuncs (optable, fmode, fmode, opname, suffix);
5429 if (dmode != fmode && dmode != VOIDmode)
5430 init_libfuncs (optable, dmode, dmode, opname, suffix);
5431 if (lmode != dmode && lmode != VOIDmode)
5432 init_libfuncs (optable, lmode, lmode, opname, suffix);
5436 init_one_libfunc (name)
5437 const char *name;
5439 rtx symbol;
5441 /* Create a FUNCTION_DECL that can be passed to
5442 targetm.encode_section_info. */
5443 /* ??? We don't have any type information except for this is
5444 a function. Pretend this is "int foo()". */
5445 tree decl = build_decl (FUNCTION_DECL, get_identifier (name),
5446 build_function_type (integer_type_node, NULL_TREE));
5447 DECL_ARTIFICIAL (decl) = 1;
5448 DECL_EXTERNAL (decl) = 1;
5449 TREE_PUBLIC (decl) = 1;
5451 symbol = XEXP (DECL_RTL (decl), 0);
5453 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
5454 are the flags assigned by targetm.encode_section_info. */
5455 SYMBOL_REF_DECL (symbol) = 0;
5457 return symbol;
5460 /* Call this once to initialize the contents of the optabs
5461 appropriately for the current target machine. */
5463 void
5464 init_optabs ()
5466 unsigned int i, j, k;
5468 /* Start by initializing all tables to contain CODE_FOR_nothing. */
5470 for (i = 0; i < ARRAY_SIZE (fixtab); i++)
5471 for (j = 0; j < ARRAY_SIZE (fixtab[0]); j++)
5472 for (k = 0; k < ARRAY_SIZE (fixtab[0][0]); k++)
5473 fixtab[i][j][k] = CODE_FOR_nothing;
5475 for (i = 0; i < ARRAY_SIZE (fixtrunctab); i++)
5476 for (j = 0; j < ARRAY_SIZE (fixtrunctab[0]); j++)
5477 for (k = 0; k < ARRAY_SIZE (fixtrunctab[0][0]); k++)
5478 fixtrunctab[i][j][k] = CODE_FOR_nothing;
5480 for (i = 0; i < ARRAY_SIZE (floattab); i++)
5481 for (j = 0; j < ARRAY_SIZE (floattab[0]); j++)
5482 for (k = 0; k < ARRAY_SIZE (floattab[0][0]); k++)
5483 floattab[i][j][k] = CODE_FOR_nothing;
5485 for (i = 0; i < ARRAY_SIZE (extendtab); i++)
5486 for (j = 0; j < ARRAY_SIZE (extendtab[0]); j++)
5487 for (k = 0; k < ARRAY_SIZE (extendtab[0][0]); k++)
5488 extendtab[i][j][k] = CODE_FOR_nothing;
5490 for (i = 0; i < NUM_RTX_CODE; i++)
5491 setcc_gen_code[i] = CODE_FOR_nothing;
5493 #ifdef HAVE_conditional_move
5494 for (i = 0; i < NUM_MACHINE_MODES; i++)
5495 movcc_gen_code[i] = CODE_FOR_nothing;
5496 #endif
5498 add_optab = init_optab (PLUS);
5499 addv_optab = init_optabv (PLUS);
5500 sub_optab = init_optab (MINUS);
5501 subv_optab = init_optabv (MINUS);
5502 smul_optab = init_optab (MULT);
5503 smulv_optab = init_optabv (MULT);
5504 smul_highpart_optab = init_optab (UNKNOWN);
5505 umul_highpart_optab = init_optab (UNKNOWN);
5506 smul_widen_optab = init_optab (UNKNOWN);
5507 umul_widen_optab = init_optab (UNKNOWN);
5508 sdiv_optab = init_optab (DIV);
5509 sdivv_optab = init_optabv (DIV);
5510 sdivmod_optab = init_optab (UNKNOWN);
5511 udiv_optab = init_optab (UDIV);
5512 udivmod_optab = init_optab (UNKNOWN);
5513 smod_optab = init_optab (MOD);
5514 umod_optab = init_optab (UMOD);
5515 ftrunc_optab = init_optab (UNKNOWN);
5516 and_optab = init_optab (AND);
5517 ior_optab = init_optab (IOR);
5518 xor_optab = init_optab (XOR);
5519 ashl_optab = init_optab (ASHIFT);
5520 ashr_optab = init_optab (ASHIFTRT);
5521 lshr_optab = init_optab (LSHIFTRT);
5522 rotl_optab = init_optab (ROTATE);
5523 rotr_optab = init_optab (ROTATERT);
5524 smin_optab = init_optab (SMIN);
5525 smax_optab = init_optab (SMAX);
5526 umin_optab = init_optab (UMIN);
5527 umax_optab = init_optab (UMAX);
5528 pow_optab = init_optab (UNKNOWN);
5529 atan2_optab = init_optab (UNKNOWN);
5531 /* These three have codes assigned exclusively for the sake of
5532 have_insn_for. */
5533 mov_optab = init_optab (SET);
5534 movstrict_optab = init_optab (STRICT_LOW_PART);
5535 cmp_optab = init_optab (COMPARE);
5537 ucmp_optab = init_optab (UNKNOWN);
5538 tst_optab = init_optab (UNKNOWN);
5539 neg_optab = init_optab (NEG);
5540 negv_optab = init_optabv (NEG);
5541 abs_optab = init_optab (ABS);
5542 absv_optab = init_optabv (ABS);
5543 addcc_optab = init_optab (UNKNOWN);
5544 one_cmpl_optab = init_optab (NOT);
5545 ffs_optab = init_optab (FFS);
5546 clz_optab = init_optab (CLZ);
5547 ctz_optab = init_optab (CTZ);
5548 popcount_optab = init_optab (POPCOUNT);
5549 parity_optab = init_optab (PARITY);
5550 sqrt_optab = init_optab (SQRT);
5551 floor_optab = init_optab (UNKNOWN);
5552 ceil_optab = init_optab (UNKNOWN);
5553 round_optab = init_optab (UNKNOWN);
5554 trunc_optab = init_optab (UNKNOWN);
5555 nearbyint_optab = init_optab (UNKNOWN);
5556 sin_optab = init_optab (UNKNOWN);
5557 cos_optab = init_optab (UNKNOWN);
5558 exp_optab = init_optab (UNKNOWN);
5559 log_optab = init_optab (UNKNOWN);
5560 tan_optab = init_optab (UNKNOWN);
5561 atan_optab = init_optab (UNKNOWN);
5562 strlen_optab = init_optab (UNKNOWN);
5563 cbranch_optab = init_optab (UNKNOWN);
5564 cmov_optab = init_optab (UNKNOWN);
5565 cstore_optab = init_optab (UNKNOWN);
5566 push_optab = init_optab (UNKNOWN);
5568 for (i = 0; i < NUM_MACHINE_MODES; i++)
5570 movstr_optab[i] = CODE_FOR_nothing;
5571 clrstr_optab[i] = CODE_FOR_nothing;
5573 #ifdef HAVE_SECONDARY_RELOADS
5574 reload_in_optab[i] = reload_out_optab[i] = CODE_FOR_nothing;
5575 #endif
5578 /* Fill in the optabs with the insns we support. */
5579 init_all_optabs ();
5581 #ifdef FIXUNS_TRUNC_LIKE_FIX_TRUNC
5582 /* This flag says the same insns that convert to a signed fixnum
5583 also convert validly to an unsigned one. */
5584 for (i = 0; i < NUM_MACHINE_MODES; i++)
5585 for (j = 0; j < NUM_MACHINE_MODES; j++)
5586 fixtrunctab[i][j][1] = fixtrunctab[i][j][0];
5587 #endif
5589 /* Initialize the optabs with the names of the library functions. */
5590 init_integral_libfuncs (add_optab, "add", '3');
5591 init_floating_libfuncs (add_optab, "add", '3');
5592 init_integral_libfuncs (addv_optab, "addv", '3');
5593 init_floating_libfuncs (addv_optab, "add", '3');
5594 init_integral_libfuncs (sub_optab, "sub", '3');
5595 init_floating_libfuncs (sub_optab, "sub", '3');
5596 init_integral_libfuncs (subv_optab, "subv", '3');
5597 init_floating_libfuncs (subv_optab, "sub", '3');
5598 init_integral_libfuncs (smul_optab, "mul", '3');
5599 init_floating_libfuncs (smul_optab, "mul", '3');
5600 init_integral_libfuncs (smulv_optab, "mulv", '3');
5601 init_floating_libfuncs (smulv_optab, "mul", '3');
5602 init_integral_libfuncs (sdiv_optab, "div", '3');
5603 init_floating_libfuncs (sdiv_optab, "div", '3');
5604 init_integral_libfuncs (sdivv_optab, "divv", '3');
5605 init_integral_libfuncs (udiv_optab, "udiv", '3');
5606 init_integral_libfuncs (sdivmod_optab, "divmod", '4');
5607 init_integral_libfuncs (udivmod_optab, "udivmod", '4');
5608 init_integral_libfuncs (smod_optab, "mod", '3');
5609 init_integral_libfuncs (umod_optab, "umod", '3');
5610 init_floating_libfuncs (ftrunc_optab, "ftrunc", '2');
5611 init_integral_libfuncs (and_optab, "and", '3');
5612 init_integral_libfuncs (ior_optab, "ior", '3');
5613 init_integral_libfuncs (xor_optab, "xor", '3');
5614 init_integral_libfuncs (ashl_optab, "ashl", '3');
5615 init_integral_libfuncs (ashr_optab, "ashr", '3');
5616 init_integral_libfuncs (lshr_optab, "lshr", '3');
5617 init_integral_libfuncs (smin_optab, "min", '3');
5618 init_floating_libfuncs (smin_optab, "min", '3');
5619 init_integral_libfuncs (smax_optab, "max", '3');
5620 init_floating_libfuncs (smax_optab, "max", '3');
5621 init_integral_libfuncs (umin_optab, "umin", '3');
5622 init_integral_libfuncs (umax_optab, "umax", '3');
5623 init_integral_libfuncs (neg_optab, "neg", '2');
5624 init_floating_libfuncs (neg_optab, "neg", '2');
5625 init_integral_libfuncs (negv_optab, "negv", '2');
5626 init_floating_libfuncs (negv_optab, "neg", '2');
5627 init_integral_libfuncs (one_cmpl_optab, "one_cmpl", '2');
5628 init_integral_libfuncs (ffs_optab, "ffs", '2');
5629 init_integral_libfuncs (clz_optab, "clz", '2');
5630 init_integral_libfuncs (ctz_optab, "ctz", '2');
5631 init_integral_libfuncs (popcount_optab, "popcount", '2');
5632 init_integral_libfuncs (parity_optab, "parity", '2');
5634 /* Comparison libcalls for integers MUST come in pairs, signed/unsigned. */
5635 init_integral_libfuncs (cmp_optab, "cmp", '2');
5636 init_integral_libfuncs (ucmp_optab, "ucmp", '2');
5637 init_floating_libfuncs (cmp_optab, "cmp", '2');
5639 #ifdef MULSI3_LIBCALL
5640 smul_optab->handlers[(int) SImode].libfunc
5641 = init_one_libfunc (MULSI3_LIBCALL);
5642 #endif
5643 #ifdef MULDI3_LIBCALL
5644 smul_optab->handlers[(int) DImode].libfunc
5645 = init_one_libfunc (MULDI3_LIBCALL);
5646 #endif
5648 #ifdef DIVSI3_LIBCALL
5649 sdiv_optab->handlers[(int) SImode].libfunc
5650 = init_one_libfunc (DIVSI3_LIBCALL);
5651 #endif
5652 #ifdef DIVDI3_LIBCALL
5653 sdiv_optab->handlers[(int) DImode].libfunc
5654 = init_one_libfunc (DIVDI3_LIBCALL);
5655 #endif
5657 #ifdef UDIVSI3_LIBCALL
5658 udiv_optab->handlers[(int) SImode].libfunc
5659 = init_one_libfunc (UDIVSI3_LIBCALL);
5660 #endif
5661 #ifdef UDIVDI3_LIBCALL
5662 udiv_optab->handlers[(int) DImode].libfunc
5663 = init_one_libfunc (UDIVDI3_LIBCALL);
5664 #endif
5666 #ifdef MODSI3_LIBCALL
5667 smod_optab->handlers[(int) SImode].libfunc
5668 = init_one_libfunc (MODSI3_LIBCALL);
5669 #endif
5670 #ifdef MODDI3_LIBCALL
5671 smod_optab->handlers[(int) DImode].libfunc
5672 = init_one_libfunc (MODDI3_LIBCALL);
5673 #endif
5675 #ifdef UMODSI3_LIBCALL
5676 umod_optab->handlers[(int) SImode].libfunc
5677 = init_one_libfunc (UMODSI3_LIBCALL);
5678 #endif
5679 #ifdef UMODDI3_LIBCALL
5680 umod_optab->handlers[(int) DImode].libfunc
5681 = init_one_libfunc (UMODDI3_LIBCALL);
5682 #endif
5684 /* Use cabs for DC complex abs, since systems generally have cabs.
5685 Don't define any libcall for SCmode, so that cabs will be used. */
5686 abs_optab->handlers[(int) DCmode].libfunc
5687 = init_one_libfunc ("cabs");
5689 /* The ffs function operates on `int'. */
5690 ffs_optab->handlers[(int) mode_for_size (INT_TYPE_SIZE, MODE_INT, 0)].libfunc
5691 = init_one_libfunc ("ffs");
5693 extendsfdf2_libfunc = init_one_libfunc ("__extendsfdf2");
5694 extendsfxf2_libfunc = init_one_libfunc ("__extendsfxf2");
5695 extendsftf2_libfunc = init_one_libfunc ("__extendsftf2");
5696 extenddfxf2_libfunc = init_one_libfunc ("__extenddfxf2");
5697 extenddftf2_libfunc = init_one_libfunc ("__extenddftf2");
5699 truncdfsf2_libfunc = init_one_libfunc ("__truncdfsf2");
5700 truncxfsf2_libfunc = init_one_libfunc ("__truncxfsf2");
5701 trunctfsf2_libfunc = init_one_libfunc ("__trunctfsf2");
5702 truncxfdf2_libfunc = init_one_libfunc ("__truncxfdf2");
5703 trunctfdf2_libfunc = init_one_libfunc ("__trunctfdf2");
5705 abort_libfunc = init_one_libfunc ("abort");
5706 memcpy_libfunc = init_one_libfunc ("memcpy");
5707 memmove_libfunc = init_one_libfunc ("memmove");
5708 bcopy_libfunc = init_one_libfunc ("bcopy");
5709 memcmp_libfunc = init_one_libfunc ("memcmp");
5710 bcmp_libfunc = init_one_libfunc ("__gcc_bcmp");
5711 memset_libfunc = init_one_libfunc ("memset");
5712 bzero_libfunc = init_one_libfunc ("bzero");
5713 setbits_libfunc = init_one_libfunc ("__setbits");
5715 unwind_resume_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
5716 ? "_Unwind_SjLj_Resume"
5717 : "_Unwind_Resume");
5718 #ifndef DONT_USE_BUILTIN_SETJMP
5719 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
5720 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
5721 #else
5722 setjmp_libfunc = init_one_libfunc ("setjmp");
5723 longjmp_libfunc = init_one_libfunc ("longjmp");
5724 #endif
5725 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
5726 unwind_sjlj_unregister_libfunc
5727 = init_one_libfunc ("_Unwind_SjLj_Unregister");
5729 eqhf2_libfunc = init_one_libfunc ("__eqhf2");
5730 nehf2_libfunc = init_one_libfunc ("__nehf2");
5731 gthf2_libfunc = init_one_libfunc ("__gthf2");
5732 gehf2_libfunc = init_one_libfunc ("__gehf2");
5733 lthf2_libfunc = init_one_libfunc ("__lthf2");
5734 lehf2_libfunc = init_one_libfunc ("__lehf2");
5735 unordhf2_libfunc = init_one_libfunc ("__unordhf2");
5737 eqsf2_libfunc = init_one_libfunc ("__eqsf2");
5738 nesf2_libfunc = init_one_libfunc ("__nesf2");
5739 gtsf2_libfunc = init_one_libfunc ("__gtsf2");
5740 gesf2_libfunc = init_one_libfunc ("__gesf2");
5741 ltsf2_libfunc = init_one_libfunc ("__ltsf2");
5742 lesf2_libfunc = init_one_libfunc ("__lesf2");
5743 unordsf2_libfunc = init_one_libfunc ("__unordsf2");
5745 eqdf2_libfunc = init_one_libfunc ("__eqdf2");
5746 nedf2_libfunc = init_one_libfunc ("__nedf2");
5747 gtdf2_libfunc = init_one_libfunc ("__gtdf2");
5748 gedf2_libfunc = init_one_libfunc ("__gedf2");
5749 ltdf2_libfunc = init_one_libfunc ("__ltdf2");
5750 ledf2_libfunc = init_one_libfunc ("__ledf2");
5751 unorddf2_libfunc = init_one_libfunc ("__unorddf2");
5753 eqxf2_libfunc = init_one_libfunc ("__eqxf2");
5754 nexf2_libfunc = init_one_libfunc ("__nexf2");
5755 gtxf2_libfunc = init_one_libfunc ("__gtxf2");
5756 gexf2_libfunc = init_one_libfunc ("__gexf2");
5757 ltxf2_libfunc = init_one_libfunc ("__ltxf2");
5758 lexf2_libfunc = init_one_libfunc ("__lexf2");
5759 unordxf2_libfunc = init_one_libfunc ("__unordxf2");
5761 eqtf2_libfunc = init_one_libfunc ("__eqtf2");
5762 netf2_libfunc = init_one_libfunc ("__netf2");
5763 gttf2_libfunc = init_one_libfunc ("__gttf2");
5764 getf2_libfunc = init_one_libfunc ("__getf2");
5765 lttf2_libfunc = init_one_libfunc ("__lttf2");
5766 letf2_libfunc = init_one_libfunc ("__letf2");
5767 unordtf2_libfunc = init_one_libfunc ("__unordtf2");
5769 floatsisf_libfunc = init_one_libfunc ("__floatsisf");
5770 floatdisf_libfunc = init_one_libfunc ("__floatdisf");
5771 floattisf_libfunc = init_one_libfunc ("__floattisf");
5773 floatsidf_libfunc = init_one_libfunc ("__floatsidf");
5774 floatdidf_libfunc = init_one_libfunc ("__floatdidf");
5775 floattidf_libfunc = init_one_libfunc ("__floattidf");
5777 floatsixf_libfunc = init_one_libfunc ("__floatsixf");
5778 floatdixf_libfunc = init_one_libfunc ("__floatdixf");
5779 floattixf_libfunc = init_one_libfunc ("__floattixf");
5781 floatsitf_libfunc = init_one_libfunc ("__floatsitf");
5782 floatditf_libfunc = init_one_libfunc ("__floatditf");
5783 floattitf_libfunc = init_one_libfunc ("__floattitf");
5785 fixsfsi_libfunc = init_one_libfunc ("__fixsfsi");
5786 fixsfdi_libfunc = init_one_libfunc ("__fixsfdi");
5787 fixsfti_libfunc = init_one_libfunc ("__fixsfti");
5789 fixdfsi_libfunc = init_one_libfunc ("__fixdfsi");
5790 fixdfdi_libfunc = init_one_libfunc ("__fixdfdi");
5791 fixdfti_libfunc = init_one_libfunc ("__fixdfti");
5793 fixxfsi_libfunc = init_one_libfunc ("__fixxfsi");
5794 fixxfdi_libfunc = init_one_libfunc ("__fixxfdi");
5795 fixxfti_libfunc = init_one_libfunc ("__fixxfti");
5797 fixtfsi_libfunc = init_one_libfunc ("__fixtfsi");
5798 fixtfdi_libfunc = init_one_libfunc ("__fixtfdi");
5799 fixtfti_libfunc = init_one_libfunc ("__fixtfti");
5801 fixunssfsi_libfunc = init_one_libfunc ("__fixunssfsi");
5802 fixunssfdi_libfunc = init_one_libfunc ("__fixunssfdi");
5803 fixunssfti_libfunc = init_one_libfunc ("__fixunssfti");
5805 fixunsdfsi_libfunc = init_one_libfunc ("__fixunsdfsi");
5806 fixunsdfdi_libfunc = init_one_libfunc ("__fixunsdfdi");
5807 fixunsdfti_libfunc = init_one_libfunc ("__fixunsdfti");
5809 fixunsxfsi_libfunc = init_one_libfunc ("__fixunsxfsi");
5810 fixunsxfdi_libfunc = init_one_libfunc ("__fixunsxfdi");
5811 fixunsxfti_libfunc = init_one_libfunc ("__fixunsxfti");
5813 fixunstfsi_libfunc = init_one_libfunc ("__fixunstfsi");
5814 fixunstfdi_libfunc = init_one_libfunc ("__fixunstfdi");
5815 fixunstfti_libfunc = init_one_libfunc ("__fixunstfti");
5817 /* For function entry/exit instrumentation. */
5818 profile_function_entry_libfunc
5819 = init_one_libfunc ("__cyg_profile_func_enter");
5820 profile_function_exit_libfunc
5821 = init_one_libfunc ("__cyg_profile_func_exit");
5823 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
5824 gcov_init_libfunc = init_one_libfunc ("__gcov_init");
5826 if (HAVE_conditional_trap)
5827 trap_rtx = gen_rtx_fmt_ee (EQ, VOIDmode, NULL_RTX, NULL_RTX);
5829 #ifdef INIT_TARGET_OPTABS
5830 /* Allow the target to add more libcalls or rename some, etc. */
5831 INIT_TARGET_OPTABS;
5832 #endif
5835 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
5836 CODE. Return 0 on failure. */
5839 gen_cond_trap (code, op1, op2, tcode)
5840 enum rtx_code code ATTRIBUTE_UNUSED;
5841 rtx op1, op2 ATTRIBUTE_UNUSED, tcode ATTRIBUTE_UNUSED;
5843 enum machine_mode mode = GET_MODE (op1);
5844 enum insn_code icode;
5845 rtx insn;
5847 if (!HAVE_conditional_trap)
5848 return 0;
5850 if (mode == VOIDmode)
5851 return 0;
5853 icode = cmp_optab->handlers[(int) mode].insn_code;
5854 if (icode == CODE_FOR_nothing)
5855 return 0;
5857 start_sequence ();
5858 op1 = prepare_operand (icode, op1, 0, mode, mode, 0);
5859 op2 = prepare_operand (icode, op2, 1, mode, mode, 0);
5860 emit_insn (GEN_FCN (icode) (op1, op2));
5862 PUT_CODE (trap_rtx, code);
5863 insn = gen_conditional_trap (trap_rtx, tcode);
5864 if (insn)
5866 emit_insn (insn);
5867 insn = get_insns ();
5869 end_sequence ();
5871 return insn;
5874 #include "gt-optabs.h"