* dwarf2out.c (dwarf2out_finish): Set parent to comp_unit_die
[official-gcc.git] / gcc / optabs.c
blobabb69700a04a3f121f5da8dacfac0a0b5133fa84
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
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"
46 #include "target.h"
48 /* Each optab contains info on how this target machine
49 can perform a particular operation
50 for all sizes and kinds of operands.
52 The operation to be performed is often specified
53 by passing one of these optabs as an argument.
55 See expr.h for documentation of these optabs. */
57 optab optab_table[OTI_MAX];
59 rtx libfunc_table[LTI_MAX];
61 /* Tables of patterns for converting one mode to another. */
62 convert_optab convert_optab_table[CTI_MAX];
64 /* Contains the optab used for each rtx code. */
65 optab code_to_optab[NUM_RTX_CODE + 1];
67 /* Indexed by the rtx-code for a conditional (eg. EQ, LT,...)
68 gives the gen_function to make a branch to test that condition. */
70 rtxfun bcc_gen_fctn[NUM_RTX_CODE];
72 /* Indexed by the rtx-code for a conditional (eg. EQ, LT,...)
73 gives the insn code to make a store-condition insn
74 to test that condition. */
76 enum insn_code setcc_gen_code[NUM_RTX_CODE];
78 #ifdef HAVE_conditional_move
79 /* Indexed by the machine mode, gives the insn code to make a conditional
80 move insn. This is not indexed by the rtx-code like bcc_gen_fctn and
81 setcc_gen_code to cut down on the number of named patterns. Consider a day
82 when a lot more rtx codes are conditional (eg: for the ARM). */
84 enum insn_code movcc_gen_code[NUM_MACHINE_MODES];
85 #endif
87 /* The insn generating function can not take an rtx_code argument.
88 TRAP_RTX is used as an rtx argument. Its code is replaced with
89 the code to be used in the trap insn and all other fields are ignored. */
90 static GTY(()) rtx trap_rtx;
92 static int add_equal_note (rtx, rtx, enum rtx_code, rtx, rtx);
93 static rtx widen_operand (rtx, enum machine_mode, enum machine_mode, int,
94 int);
95 static void prepare_cmp_insn (rtx *, rtx *, enum rtx_code *, rtx,
96 enum machine_mode *, int *,
97 enum can_compare_purpose);
98 static enum insn_code can_fix_p (enum machine_mode, enum machine_mode, int,
99 int *);
100 static enum insn_code can_float_p (enum machine_mode, enum machine_mode, int);
101 static optab new_optab (void);
102 static convert_optab new_convert_optab (void);
103 static inline optab init_optab (enum rtx_code);
104 static inline optab init_optabv (enum rtx_code);
105 static inline convert_optab init_convert_optab (enum rtx_code);
106 static void init_libfuncs (optab, int, int, const char *, int);
107 static void init_integral_libfuncs (optab, const char *, int);
108 static void init_floating_libfuncs (optab, const char *, int);
109 static void init_interclass_conv_libfuncs (convert_optab, const char *,
110 enum mode_class, enum mode_class);
111 static void init_intraclass_conv_libfuncs (convert_optab, const char *,
112 enum mode_class, bool);
113 static void emit_cmp_and_jump_insn_1 (rtx, rtx, enum machine_mode,
114 enum rtx_code, int, rtx);
115 static void prepare_float_lib_cmp (rtx *, rtx *, enum rtx_code *,
116 enum machine_mode *, int *);
117 static rtx widen_clz (enum machine_mode, rtx, rtx);
118 static rtx expand_parity (enum machine_mode, rtx, rtx);
120 #ifndef HAVE_conditional_trap
121 #define HAVE_conditional_trap 0
122 #define gen_conditional_trap(a,b) (abort (), NULL_RTX)
123 #endif
125 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
126 the result of operation CODE applied to OP0 (and OP1 if it is a binary
127 operation).
129 If the last insn does not set TARGET, don't do anything, but return 1.
131 If a previous insn sets TARGET and TARGET is one of OP0 or OP1,
132 don't add the REG_EQUAL note but return 0. Our caller can then try
133 again, ensuring that TARGET is not one of the operands. */
135 static int
136 add_equal_note (rtx insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
138 rtx last_insn, insn, set;
139 rtx note;
141 if (! insns
142 || ! INSN_P (insns)
143 || NEXT_INSN (insns) == NULL_RTX)
144 abort ();
146 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
147 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
148 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
149 && GET_RTX_CLASS (code) != RTX_COMPARE
150 && GET_RTX_CLASS (code) != RTX_UNARY)
151 return 1;
153 if (GET_CODE (target) == ZERO_EXTRACT)
154 return 1;
156 for (last_insn = insns;
157 NEXT_INSN (last_insn) != NULL_RTX;
158 last_insn = NEXT_INSN (last_insn))
161 set = single_set (last_insn);
162 if (set == NULL_RTX)
163 return 1;
165 if (! rtx_equal_p (SET_DEST (set), target)
166 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
167 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
168 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
169 return 1;
171 /* If TARGET is in OP0 or OP1, check if anything in SEQ sets TARGET
172 besides the last insn. */
173 if (reg_overlap_mentioned_p (target, op0)
174 || (op1 && reg_overlap_mentioned_p (target, op1)))
176 insn = PREV_INSN (last_insn);
177 while (insn != NULL_RTX)
179 if (reg_set_p (target, insn))
180 return 0;
182 insn = PREV_INSN (insn);
186 if (GET_RTX_CLASS (code) == RTX_UNARY)
187 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
188 else
189 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
191 set_unique_reg_note (last_insn, REG_EQUAL, note);
193 return 1;
196 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
197 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
198 not actually do a sign-extend or zero-extend, but can leave the
199 higher-order bits of the result rtx undefined, for example, in the case
200 of logical operations, but not right shifts. */
202 static rtx
203 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
204 int unsignedp, int no_extend)
206 rtx result;
208 /* If we don't have to extend and this is a constant, return it. */
209 if (no_extend && GET_MODE (op) == VOIDmode)
210 return op;
212 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
213 extend since it will be more efficient to do so unless the signedness of
214 a promoted object differs from our extension. */
215 if (! no_extend
216 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
217 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
218 return convert_modes (mode, oldmode, op, unsignedp);
220 /* If MODE is no wider than a single word, we return a paradoxical
221 SUBREG. */
222 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
223 return gen_rtx_SUBREG (mode, force_reg (GET_MODE (op), op), 0);
225 /* Otherwise, get an object of MODE, clobber it, and set the low-order
226 part to OP. */
228 result = gen_reg_rtx (mode);
229 emit_insn (gen_rtx_CLOBBER (VOIDmode, result));
230 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
231 return result;
234 /* Return the optab used for computing the operation given by
235 the tree code, CODE. This function is not always usable (for
236 example, it cannot give complete results for multiplication
237 or division) but probably ought to be relied on more widely
238 throughout the expander. */
239 optab
240 optab_for_tree_code (enum tree_code code, tree type)
242 bool trapv;
243 switch (code)
245 case BIT_AND_EXPR:
246 return and_optab;
248 case BIT_IOR_EXPR:
249 return ior_optab;
251 case BIT_NOT_EXPR:
252 return one_cmpl_optab;
254 case BIT_XOR_EXPR:
255 return xor_optab;
257 case TRUNC_MOD_EXPR:
258 case CEIL_MOD_EXPR:
259 case FLOOR_MOD_EXPR:
260 case ROUND_MOD_EXPR:
261 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
263 case RDIV_EXPR:
264 case TRUNC_DIV_EXPR:
265 case CEIL_DIV_EXPR:
266 case FLOOR_DIV_EXPR:
267 case ROUND_DIV_EXPR:
268 case EXACT_DIV_EXPR:
269 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
271 case LSHIFT_EXPR:
272 return ashl_optab;
274 case RSHIFT_EXPR:
275 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
277 case LROTATE_EXPR:
278 return rotl_optab;
280 case RROTATE_EXPR:
281 return rotr_optab;
283 case MAX_EXPR:
284 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
286 case MIN_EXPR:
287 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
289 case REALIGN_STORE_EXPR:
290 return vec_realign_store_optab;
292 case REALIGN_LOAD_EXPR:
293 return vec_realign_load_optab;
295 default:
296 break;
299 trapv = flag_trapv && INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type);
300 switch (code)
302 case PLUS_EXPR:
303 return trapv ? addv_optab : add_optab;
305 case MINUS_EXPR:
306 return trapv ? subv_optab : sub_optab;
308 case MULT_EXPR:
309 return trapv ? smulv_optab : smul_optab;
311 case NEGATE_EXPR:
312 return trapv ? negv_optab : neg_optab;
314 case ABS_EXPR:
315 return trapv ? absv_optab : abs_optab;
317 default:
318 return NULL;
323 /* Generate code to perform an operation specified by TERNARY_OPTAB
324 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
326 UNSIGNEDP is for the case where we have to widen the operands
327 to perform the operation. It says to use zero-extension.
329 If TARGET is nonzero, the value
330 is generated there, if it is convenient to do so.
331 In all cases an rtx is returned for the locus of the value;
332 this may or may not be TARGET. */
335 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
336 rtx op1, rtx op2, rtx target, int unsignedp)
338 int icode = (int) ternary_optab->handlers[(int) mode].insn_code;
339 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
340 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
341 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
342 rtx temp;
343 rtx pat;
344 rtx xop0 = op0, xop1 = op1, xop2 = op2;
346 if (ternary_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
347 abort ();
349 if (!target
350 || ! (*insn_data[icode].operand[0].predicate) (target, mode))
351 temp = gen_reg_rtx (mode);
352 else
353 temp = target;
355 /* In case the insn wants input operands in modes different from
356 those of the actual operands, convert the operands. It would
357 seem that we don't need to convert CONST_INTs, but we do, so
358 that they're properly zero-extended, sign-extended or truncated
359 for their mode. */
361 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
362 xop0 = convert_modes (mode0,
363 GET_MODE (op0) != VOIDmode
364 ? GET_MODE (op0)
365 : mode,
366 xop0, unsignedp);
368 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
369 xop1 = convert_modes (mode1,
370 GET_MODE (op1) != VOIDmode
371 ? GET_MODE (op1)
372 : mode,
373 xop1, unsignedp);
375 if (GET_MODE (op2) != mode2 && mode2 != VOIDmode)
376 xop2 = convert_modes (mode2,
377 GET_MODE (op2) != VOIDmode
378 ? GET_MODE (op2)
379 : mode,
380 xop2, unsignedp);
382 /* Now, if insn's predicates don't allow our operands, put them into
383 pseudo regs. */
385 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0)
386 && mode0 != VOIDmode)
387 xop0 = copy_to_mode_reg (mode0, xop0);
389 if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1)
390 && mode1 != VOIDmode)
391 xop1 = copy_to_mode_reg (mode1, xop1);
393 if (! (*insn_data[icode].operand[3].predicate) (xop2, mode2)
394 && mode2 != VOIDmode)
395 xop2 = copy_to_mode_reg (mode2, xop2);
397 pat = GEN_FCN (icode) (temp, xop0, xop1, xop2);
399 emit_insn (pat);
400 return temp;
404 /* Like expand_binop, but return a constant rtx if the result can be
405 calculated at compile time. The arguments and return value are
406 otherwise the same as for expand_binop. */
408 static rtx
409 simplify_expand_binop (enum machine_mode mode, optab binoptab,
410 rtx op0, rtx op1, rtx target, int unsignedp,
411 enum optab_methods methods)
413 if (CONSTANT_P (op0) && CONSTANT_P (op1))
414 return simplify_gen_binary (binoptab->code, mode, op0, op1);
415 else
416 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
419 /* Like simplify_expand_binop, but always put the result in TARGET.
420 Return true if the expansion succeeded. */
422 static bool
423 force_expand_binop (enum machine_mode mode, optab binoptab,
424 rtx op0, rtx op1, rtx target, int unsignedp,
425 enum optab_methods methods)
427 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
428 target, unsignedp, methods);
429 if (x == 0)
430 return false;
431 if (x != target)
432 emit_move_insn (target, x);
433 return true;
436 /* This subroutine of expand_doubleword_shift handles the cases in which
437 the effective shift value is >= BITS_PER_WORD. The arguments and return
438 value are the same as for the parent routine, except that SUPERWORD_OP1
439 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
440 INTO_TARGET may be null if the caller has decided to calculate it. */
442 static bool
443 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
444 rtx outof_target, rtx into_target,
445 int unsignedp, enum optab_methods methods)
447 if (into_target != 0)
448 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
449 into_target, unsignedp, methods))
450 return false;
452 if (outof_target != 0)
454 /* For a signed right shift, we must fill OUTOF_TARGET with copies
455 of the sign bit, otherwise we must fill it with zeros. */
456 if (binoptab != ashr_optab)
457 emit_move_insn (outof_target, CONST0_RTX (word_mode));
458 else
459 if (!force_expand_binop (word_mode, binoptab,
460 outof_input, GEN_INT (BITS_PER_WORD - 1),
461 outof_target, unsignedp, methods))
462 return false;
464 return true;
467 /* This subroutine of expand_doubleword_shift handles the cases in which
468 the effective shift value is < BITS_PER_WORD. The arguments and return
469 value are the same as for the parent routine. */
471 static bool
472 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
473 rtx outof_input, rtx into_input, rtx op1,
474 rtx outof_target, rtx into_target,
475 int unsignedp, enum optab_methods methods,
476 unsigned HOST_WIDE_INT shift_mask)
478 optab reverse_unsigned_shift, unsigned_shift;
479 rtx tmp, carries;
481 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
482 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
484 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
485 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
486 the opposite direction to BINOPTAB. */
487 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
489 carries = outof_input;
490 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
491 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
492 0, true, methods);
494 else
496 /* We must avoid shifting by BITS_PER_WORD bits since that is either
497 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
498 has unknown behavior. Do a single shift first, then shift by the
499 remainder. It's OK to use ~OP1 as the remainder if shift counts
500 are truncated to the mode size. */
501 carries = expand_binop (word_mode, reverse_unsigned_shift,
502 outof_input, const1_rtx, 0, unsignedp, methods);
503 if (shift_mask == BITS_PER_WORD - 1)
505 tmp = immed_double_const (-1, -1, op1_mode);
506 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
507 0, true, methods);
509 else
511 tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
512 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
513 0, true, methods);
516 if (tmp == 0 || carries == 0)
517 return false;
518 carries = expand_binop (word_mode, reverse_unsigned_shift,
519 carries, tmp, 0, unsignedp, methods);
520 if (carries == 0)
521 return false;
523 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
524 so the result can go directly into INTO_TARGET if convenient. */
525 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
526 into_target, unsignedp, methods);
527 if (tmp == 0)
528 return false;
530 /* Now OR in the bits carried over from OUTOF_INPUT. */
531 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
532 into_target, unsignedp, methods))
533 return false;
535 /* Use a standard word_mode shift for the out-of half. */
536 if (outof_target != 0)
537 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
538 outof_target, unsignedp, methods))
539 return false;
541 return true;
545 #ifdef HAVE_conditional_move
546 /* Try implementing expand_doubleword_shift using conditional moves.
547 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
548 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
549 are the shift counts to use in the former and latter case. All other
550 arguments are the same as the parent routine. */
552 static bool
553 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
554 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
555 rtx outof_input, rtx into_input,
556 rtx subword_op1, rtx superword_op1,
557 rtx outof_target, rtx into_target,
558 int unsignedp, enum optab_methods methods,
559 unsigned HOST_WIDE_INT shift_mask)
561 rtx outof_superword, into_superword;
563 /* Put the superword version of the output into OUTOF_SUPERWORD and
564 INTO_SUPERWORD. */
565 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
566 if (outof_target != 0 && subword_op1 == superword_op1)
568 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
569 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
570 into_superword = outof_target;
571 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
572 outof_superword, 0, unsignedp, methods))
573 return false;
575 else
577 into_superword = gen_reg_rtx (word_mode);
578 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
579 outof_superword, into_superword,
580 unsignedp, methods))
581 return false;
584 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
585 if (!expand_subword_shift (op1_mode, binoptab,
586 outof_input, into_input, subword_op1,
587 outof_target, into_target,
588 unsignedp, methods, shift_mask))
589 return false;
591 /* Select between them. Do the INTO half first because INTO_SUPERWORD
592 might be the current value of OUTOF_TARGET. */
593 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
594 into_target, into_superword, word_mode, false))
595 return false;
597 if (outof_target != 0)
598 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
599 outof_target, outof_superword,
600 word_mode, false))
601 return false;
603 return true;
605 #endif
607 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
608 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
609 input operand; the shift moves bits in the direction OUTOF_INPUT->
610 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
611 of the target. OP1 is the shift count and OP1_MODE is its mode.
612 If OP1 is constant, it will have been truncated as appropriate
613 and is known to be nonzero.
615 If SHIFT_MASK is zero, the result of word shifts is undefined when the
616 shift count is outside the range [0, BITS_PER_WORD). This routine must
617 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
619 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
620 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
621 fill with zeros or sign bits as appropriate.
623 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
624 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
625 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
626 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
627 are undefined.
629 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
630 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
631 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
632 function wants to calculate it itself.
634 Return true if the shift could be successfully synthesized. */
636 static bool
637 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
638 rtx outof_input, rtx into_input, rtx op1,
639 rtx outof_target, rtx into_target,
640 int unsignedp, enum optab_methods methods,
641 unsigned HOST_WIDE_INT shift_mask)
643 rtx superword_op1, tmp, cmp1, cmp2;
644 rtx subword_label, done_label;
645 enum rtx_code cmp_code;
647 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
648 fill the result with sign or zero bits as appropriate. If so, the value
649 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
650 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
651 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
653 This isn't worthwhile for constant shifts since the optimizers will
654 cope better with in-range shift counts. */
655 if (shift_mask >= BITS_PER_WORD
656 && outof_target != 0
657 && !CONSTANT_P (op1))
659 if (!expand_doubleword_shift (op1_mode, binoptab,
660 outof_input, into_input, op1,
661 0, into_target,
662 unsignedp, methods, shift_mask))
663 return false;
664 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
665 outof_target, unsignedp, methods))
666 return false;
667 return true;
670 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
671 is true when the effective shift value is less than BITS_PER_WORD.
672 Set SUPERWORD_OP1 to the shift count that should be used to shift
673 OUTOF_INPUT into INTO_TARGET when the condition is false. */
674 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
675 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
677 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
678 is a subword shift count. */
679 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
680 0, true, methods);
681 cmp2 = CONST0_RTX (op1_mode);
682 cmp_code = EQ;
683 superword_op1 = op1;
685 else
687 /* Set CMP1 to OP1 - BITS_PER_WORD. */
688 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
689 0, true, methods);
690 cmp2 = CONST0_RTX (op1_mode);
691 cmp_code = LT;
692 superword_op1 = cmp1;
694 if (cmp1 == 0)
695 return false;
697 /* If we can compute the condition at compile time, pick the
698 appropriate subroutine. */
699 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
700 if (tmp != 0 && GET_CODE (tmp) == CONST_INT)
702 if (tmp == const0_rtx)
703 return expand_superword_shift (binoptab, outof_input, superword_op1,
704 outof_target, into_target,
705 unsignedp, methods);
706 else
707 return expand_subword_shift (op1_mode, binoptab,
708 outof_input, into_input, op1,
709 outof_target, into_target,
710 unsignedp, methods, shift_mask);
713 #ifdef HAVE_conditional_move
714 /* Try using conditional moves to generate straight-line code. */
716 rtx start = get_last_insn ();
717 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
718 cmp_code, cmp1, cmp2,
719 outof_input, into_input,
720 op1, superword_op1,
721 outof_target, into_target,
722 unsignedp, methods, shift_mask))
723 return true;
724 delete_insns_since (start);
726 #endif
728 /* As a last resort, use branches to select the correct alternative. */
729 subword_label = gen_label_rtx ();
730 done_label = gen_label_rtx ();
732 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
733 0, 0, subword_label);
735 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
736 outof_target, into_target,
737 unsignedp, methods))
738 return false;
740 emit_jump_insn (gen_jump (done_label));
741 emit_barrier ();
742 emit_label (subword_label);
744 if (!expand_subword_shift (op1_mode, binoptab,
745 outof_input, into_input, op1,
746 outof_target, into_target,
747 unsignedp, methods, shift_mask))
748 return false;
750 emit_label (done_label);
751 return true;
754 /* Wrapper around expand_binop which takes an rtx code to specify
755 the operation to perform, not an optab pointer. All other
756 arguments are the same. */
758 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
759 rtx op1, rtx target, int unsignedp,
760 enum optab_methods methods)
762 optab binop = code_to_optab[(int) code];
763 if (binop == 0)
764 abort ();
766 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
769 /* Generate code to perform an operation specified by BINOPTAB
770 on operands OP0 and OP1, with result having machine-mode MODE.
772 UNSIGNEDP is for the case where we have to widen the operands
773 to perform the operation. It says to use zero-extension.
775 If TARGET is nonzero, the value
776 is generated there, if it is convenient to do so.
777 In all cases an rtx is returned for the locus of the value;
778 this may or may not be TARGET. */
781 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
782 rtx target, int unsignedp, enum optab_methods methods)
784 enum optab_methods next_methods
785 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
786 ? OPTAB_WIDEN : methods);
787 enum mode_class class;
788 enum machine_mode wider_mode;
789 rtx temp;
790 int commutative_op = 0;
791 int shift_op = (binoptab->code == ASHIFT
792 || binoptab->code == ASHIFTRT
793 || binoptab->code == LSHIFTRT
794 || binoptab->code == ROTATE
795 || binoptab->code == ROTATERT);
796 rtx entry_last = get_last_insn ();
797 rtx last;
799 class = GET_MODE_CLASS (mode);
801 if (flag_force_mem)
803 /* Load duplicate non-volatile operands once. */
804 if (rtx_equal_p (op0, op1) && ! volatile_refs_p (op0))
806 op0 = force_not_mem (op0);
807 op1 = op0;
809 else
811 op0 = force_not_mem (op0);
812 op1 = force_not_mem (op1);
816 /* If subtracting an integer constant, convert this into an addition of
817 the negated constant. */
819 if (binoptab == sub_optab && GET_CODE (op1) == CONST_INT)
821 op1 = negate_rtx (mode, op1);
822 binoptab = add_optab;
825 /* If we are inside an appropriately-short loop and we are optimizing,
826 force expensive constants into a register. */
827 if (CONSTANT_P (op0) && optimize
828 && rtx_cost (op0, binoptab->code) > COSTS_N_INSNS (1))
829 op0 = force_reg (mode, op0);
831 if (CONSTANT_P (op1) && optimize
832 && ! shift_op && rtx_cost (op1, binoptab->code) > COSTS_N_INSNS (1))
833 op1 = force_reg (mode, op1);
835 /* Record where to delete back to if we backtrack. */
836 last = get_last_insn ();
838 /* If operation is commutative,
839 try to make the first operand a register.
840 Even better, try to make it the same as the target.
841 Also try to make the last operand a constant. */
842 if (GET_RTX_CLASS (binoptab->code) == RTX_COMM_ARITH
843 || binoptab == smul_widen_optab
844 || binoptab == umul_widen_optab
845 || binoptab == smul_highpart_optab
846 || binoptab == umul_highpart_optab)
848 commutative_op = 1;
850 if (((target == 0 || REG_P (target))
851 ? ((REG_P (op1)
852 && !REG_P (op0))
853 || target == op1)
854 : rtx_equal_p (op1, target))
855 || GET_CODE (op0) == CONST_INT)
857 temp = op1;
858 op1 = op0;
859 op0 = temp;
863 /* If we can do it with a three-operand insn, do so. */
865 if (methods != OPTAB_MUST_WIDEN
866 && binoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
868 int icode = (int) binoptab->handlers[(int) mode].insn_code;
869 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
870 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
871 rtx pat;
872 rtx xop0 = op0, xop1 = op1;
874 if (target)
875 temp = target;
876 else
877 temp = gen_reg_rtx (mode);
879 /* If it is a commutative operator and the modes would match
880 if we would swap the operands, we can save the conversions. */
881 if (commutative_op)
883 if (GET_MODE (op0) != mode0 && GET_MODE (op1) != mode1
884 && GET_MODE (op0) == mode1 && GET_MODE (op1) == mode0)
886 rtx tmp;
888 tmp = op0; op0 = op1; op1 = tmp;
889 tmp = xop0; xop0 = xop1; xop1 = tmp;
893 /* In case the insn wants input operands in modes different from
894 those of the actual operands, convert the operands. It would
895 seem that we don't need to convert CONST_INTs, but we do, so
896 that they're properly zero-extended, sign-extended or truncated
897 for their mode. */
899 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
900 xop0 = convert_modes (mode0,
901 GET_MODE (op0) != VOIDmode
902 ? GET_MODE (op0)
903 : mode,
904 xop0, unsignedp);
906 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
907 xop1 = convert_modes (mode1,
908 GET_MODE (op1) != VOIDmode
909 ? GET_MODE (op1)
910 : mode,
911 xop1, unsignedp);
913 /* Now, if insn's predicates don't allow our operands, put them into
914 pseudo regs. */
916 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0)
917 && mode0 != VOIDmode)
918 xop0 = copy_to_mode_reg (mode0, xop0);
920 if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1)
921 && mode1 != VOIDmode)
922 xop1 = copy_to_mode_reg (mode1, xop1);
924 if (! (*insn_data[icode].operand[0].predicate) (temp, mode))
925 temp = gen_reg_rtx (mode);
927 pat = GEN_FCN (icode) (temp, xop0, xop1);
928 if (pat)
930 /* If PAT is composed of more than one insn, try to add an appropriate
931 REG_EQUAL note to it. If we can't because TEMP conflicts with an
932 operand, call ourselves again, this time without a target. */
933 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
934 && ! add_equal_note (pat, temp, binoptab->code, xop0, xop1))
936 delete_insns_since (last);
937 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
938 unsignedp, methods);
941 emit_insn (pat);
942 return temp;
944 else
945 delete_insns_since (last);
948 /* If this is a multiply, see if we can do a widening operation that
949 takes operands of this mode and makes a wider mode. */
951 if (binoptab == smul_optab && GET_MODE_WIDER_MODE (mode) != VOIDmode
952 && (((unsignedp ? umul_widen_optab : smul_widen_optab)
953 ->handlers[(int) GET_MODE_WIDER_MODE (mode)].insn_code)
954 != CODE_FOR_nothing))
956 temp = expand_binop (GET_MODE_WIDER_MODE (mode),
957 unsignedp ? umul_widen_optab : smul_widen_optab,
958 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
960 if (temp != 0)
962 if (GET_MODE_CLASS (mode) == MODE_INT)
963 return gen_lowpart (mode, temp);
964 else
965 return convert_to_mode (mode, temp, unsignedp);
969 /* Look for a wider mode of the same class for which we think we
970 can open-code the operation. Check for a widening multiply at the
971 wider mode as well. */
973 if ((class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
974 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
975 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
976 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
978 if (binoptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
979 || (binoptab == smul_optab
980 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
981 && (((unsignedp ? umul_widen_optab : smul_widen_optab)
982 ->handlers[(int) GET_MODE_WIDER_MODE (wider_mode)].insn_code)
983 != CODE_FOR_nothing)))
985 rtx xop0 = op0, xop1 = op1;
986 int no_extend = 0;
988 /* For certain integer operations, we need not actually extend
989 the narrow operands, as long as we will truncate
990 the results to the same narrowness. */
992 if ((binoptab == ior_optab || binoptab == and_optab
993 || binoptab == xor_optab
994 || binoptab == add_optab || binoptab == sub_optab
995 || binoptab == smul_optab || binoptab == ashl_optab)
996 && class == MODE_INT)
997 no_extend = 1;
999 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1001 /* The second operand of a shift must always be extended. */
1002 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1003 no_extend && binoptab != ashl_optab);
1005 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1006 unsignedp, OPTAB_DIRECT);
1007 if (temp)
1009 if (class != MODE_INT)
1011 if (target == 0)
1012 target = gen_reg_rtx (mode);
1013 convert_move (target, temp, 0);
1014 return target;
1016 else
1017 return gen_lowpart (mode, temp);
1019 else
1020 delete_insns_since (last);
1024 /* These can be done a word at a time. */
1025 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1026 && class == MODE_INT
1027 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1028 && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1030 int i;
1031 rtx insns;
1032 rtx equiv_value;
1034 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1035 won't be accurate, so use a new target. */
1036 if (target == 0 || target == op0 || target == op1)
1037 target = gen_reg_rtx (mode);
1039 start_sequence ();
1041 /* Do the actual arithmetic. */
1042 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1044 rtx target_piece = operand_subword (target, i, 1, mode);
1045 rtx x = expand_binop (word_mode, binoptab,
1046 operand_subword_force (op0, i, mode),
1047 operand_subword_force (op1, i, mode),
1048 target_piece, unsignedp, next_methods);
1050 if (x == 0)
1051 break;
1053 if (target_piece != x)
1054 emit_move_insn (target_piece, x);
1057 insns = get_insns ();
1058 end_sequence ();
1060 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1062 if (binoptab->code != UNKNOWN)
1063 equiv_value
1064 = gen_rtx_fmt_ee (binoptab->code, mode,
1065 copy_rtx (op0), copy_rtx (op1));
1066 else
1067 equiv_value = 0;
1069 emit_no_conflict_block (insns, target, op0, op1, equiv_value);
1070 return target;
1074 /* Synthesize double word shifts from single word shifts. */
1075 if ((binoptab == lshr_optab || binoptab == ashl_optab
1076 || binoptab == ashr_optab)
1077 && class == MODE_INT
1078 && (GET_CODE (op1) == CONST_INT || !optimize_size)
1079 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1080 && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1081 && ashl_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1082 && lshr_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1084 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1085 enum machine_mode op1_mode;
1087 double_shift_mask = targetm.shift_truncation_mask (mode);
1088 shift_mask = targetm.shift_truncation_mask (word_mode);
1089 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1091 /* Apply the truncation to constant shifts. */
1092 if (double_shift_mask > 0 && GET_CODE (op1) == CONST_INT)
1093 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1095 if (op1 == CONST0_RTX (op1_mode))
1096 return op0;
1098 /* Make sure that this is a combination that expand_doubleword_shift
1099 can handle. See the comments there for details. */
1100 if (double_shift_mask == 0
1101 || (shift_mask == BITS_PER_WORD - 1
1102 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1104 rtx insns, equiv_value;
1105 rtx into_target, outof_target;
1106 rtx into_input, outof_input;
1107 int left_shift, outof_word;
1109 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1110 won't be accurate, so use a new target. */
1111 if (target == 0 || target == op0 || target == op1)
1112 target = gen_reg_rtx (mode);
1114 start_sequence ();
1116 /* OUTOF_* is the word we are shifting bits away from, and
1117 INTO_* is the word that we are shifting bits towards, thus
1118 they differ depending on the direction of the shift and
1119 WORDS_BIG_ENDIAN. */
1121 left_shift = binoptab == ashl_optab;
1122 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1124 outof_target = operand_subword (target, outof_word, 1, mode);
1125 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1127 outof_input = operand_subword_force (op0, outof_word, mode);
1128 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1130 if (expand_doubleword_shift (op1_mode, binoptab,
1131 outof_input, into_input, op1,
1132 outof_target, into_target,
1133 unsignedp, methods, shift_mask))
1135 insns = get_insns ();
1136 end_sequence ();
1138 equiv_value = gen_rtx_fmt_ee (binoptab->code, mode, op0, op1);
1139 emit_no_conflict_block (insns, target, op0, op1, equiv_value);
1140 return target;
1142 end_sequence ();
1146 /* Synthesize double word rotates from single word shifts. */
1147 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1148 && class == MODE_INT
1149 && GET_CODE (op1) == CONST_INT
1150 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1151 && ashl_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1152 && lshr_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1154 rtx insns, equiv_value;
1155 rtx into_target, outof_target;
1156 rtx into_input, outof_input;
1157 rtx inter;
1158 int shift_count, left_shift, outof_word;
1160 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1161 won't be accurate, so use a new target. Do this also if target is not
1162 a REG, first because having a register instead may open optimization
1163 opportunities, and second because if target and op0 happen to be MEMs
1164 designating the same location, we would risk clobbering it too early
1165 in the code sequence we generate below. */
1166 if (target == 0 || target == op0 || target == op1 || ! REG_P (target))
1167 target = gen_reg_rtx (mode);
1169 start_sequence ();
1171 shift_count = INTVAL (op1);
1173 /* OUTOF_* is the word we are shifting bits away from, and
1174 INTO_* is the word that we are shifting bits towards, thus
1175 they differ depending on the direction of the shift and
1176 WORDS_BIG_ENDIAN. */
1178 left_shift = (binoptab == rotl_optab);
1179 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1181 outof_target = operand_subword (target, outof_word, 1, mode);
1182 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1184 outof_input = operand_subword_force (op0, outof_word, mode);
1185 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1187 if (shift_count == BITS_PER_WORD)
1189 /* This is just a word swap. */
1190 emit_move_insn (outof_target, into_input);
1191 emit_move_insn (into_target, outof_input);
1192 inter = const0_rtx;
1194 else
1196 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1197 rtx first_shift_count, second_shift_count;
1198 optab reverse_unsigned_shift, unsigned_shift;
1200 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1201 ? lshr_optab : ashl_optab);
1203 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1204 ? ashl_optab : lshr_optab);
1206 if (shift_count > BITS_PER_WORD)
1208 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1209 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1211 else
1213 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1214 second_shift_count = GEN_INT (shift_count);
1217 into_temp1 = expand_binop (word_mode, unsigned_shift,
1218 outof_input, first_shift_count,
1219 NULL_RTX, unsignedp, next_methods);
1220 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1221 into_input, second_shift_count,
1222 NULL_RTX, unsignedp, next_methods);
1224 if (into_temp1 != 0 && into_temp2 != 0)
1225 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1226 into_target, unsignedp, next_methods);
1227 else
1228 inter = 0;
1230 if (inter != 0 && inter != into_target)
1231 emit_move_insn (into_target, inter);
1233 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1234 into_input, first_shift_count,
1235 NULL_RTX, unsignedp, next_methods);
1236 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1237 outof_input, second_shift_count,
1238 NULL_RTX, unsignedp, next_methods);
1240 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1241 inter = expand_binop (word_mode, ior_optab,
1242 outof_temp1, outof_temp2,
1243 outof_target, unsignedp, next_methods);
1245 if (inter != 0 && inter != outof_target)
1246 emit_move_insn (outof_target, inter);
1249 insns = get_insns ();
1250 end_sequence ();
1252 if (inter != 0)
1254 if (binoptab->code != UNKNOWN)
1255 equiv_value = gen_rtx_fmt_ee (binoptab->code, mode, op0, op1);
1256 else
1257 equiv_value = 0;
1259 /* We can't make this a no conflict block if this is a word swap,
1260 because the word swap case fails if the input and output values
1261 are in the same register. */
1262 if (shift_count != BITS_PER_WORD)
1263 emit_no_conflict_block (insns, target, op0, op1, equiv_value);
1264 else
1265 emit_insn (insns);
1268 return target;
1272 /* These can be done a word at a time by propagating carries. */
1273 if ((binoptab == add_optab || binoptab == sub_optab)
1274 && class == MODE_INT
1275 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1276 && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1278 unsigned int i;
1279 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1280 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1281 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1282 rtx xop0, xop1, xtarget;
1284 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1285 value is one of those, use it. Otherwise, use 1 since it is the
1286 one easiest to get. */
1287 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1288 int normalizep = STORE_FLAG_VALUE;
1289 #else
1290 int normalizep = 1;
1291 #endif
1293 /* Prepare the operands. */
1294 xop0 = force_reg (mode, op0);
1295 xop1 = force_reg (mode, op1);
1297 xtarget = gen_reg_rtx (mode);
1299 if (target == 0 || !REG_P (target))
1300 target = xtarget;
1302 /* Indicate for flow that the entire target reg is being set. */
1303 if (REG_P (target))
1304 emit_insn (gen_rtx_CLOBBER (VOIDmode, xtarget));
1306 /* Do the actual arithmetic. */
1307 for (i = 0; i < nwords; i++)
1309 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1310 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1311 rtx op0_piece = operand_subword_force (xop0, index, mode);
1312 rtx op1_piece = operand_subword_force (xop1, index, mode);
1313 rtx x;
1315 /* Main add/subtract of the input operands. */
1316 x = expand_binop (word_mode, binoptab,
1317 op0_piece, op1_piece,
1318 target_piece, unsignedp, next_methods);
1319 if (x == 0)
1320 break;
1322 if (i + 1 < nwords)
1324 /* Store carry from main add/subtract. */
1325 carry_out = gen_reg_rtx (word_mode);
1326 carry_out = emit_store_flag_force (carry_out,
1327 (binoptab == add_optab
1328 ? LT : GT),
1329 x, op0_piece,
1330 word_mode, 1, normalizep);
1333 if (i > 0)
1335 rtx newx;
1337 /* Add/subtract previous carry to main result. */
1338 newx = expand_binop (word_mode,
1339 normalizep == 1 ? binoptab : otheroptab,
1340 x, carry_in,
1341 NULL_RTX, 1, next_methods);
1343 if (i + 1 < nwords)
1345 /* Get out carry from adding/subtracting carry in. */
1346 rtx carry_tmp = gen_reg_rtx (word_mode);
1347 carry_tmp = emit_store_flag_force (carry_tmp,
1348 (binoptab == add_optab
1349 ? LT : GT),
1350 newx, x,
1351 word_mode, 1, normalizep);
1353 /* Logical-ior the two poss. carry together. */
1354 carry_out = expand_binop (word_mode, ior_optab,
1355 carry_out, carry_tmp,
1356 carry_out, 0, next_methods);
1357 if (carry_out == 0)
1358 break;
1360 emit_move_insn (target_piece, newx);
1363 carry_in = carry_out;
1366 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
1368 if (mov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
1369 || ! rtx_equal_p (target, xtarget))
1371 rtx temp = emit_move_insn (target, xtarget);
1373 set_unique_reg_note (temp,
1374 REG_EQUAL,
1375 gen_rtx_fmt_ee (binoptab->code, mode,
1376 copy_rtx (xop0),
1377 copy_rtx (xop1)));
1379 else
1380 target = xtarget;
1382 return target;
1385 else
1386 delete_insns_since (last);
1389 /* If we want to multiply two two-word values and have normal and widening
1390 multiplies of single-word values, we can do this with three smaller
1391 multiplications. Note that we do not make a REG_NO_CONFLICT block here
1392 because we are not operating on one word at a time.
1394 The multiplication proceeds as follows:
1395 _______________________
1396 [__op0_high_|__op0_low__]
1397 _______________________
1398 * [__op1_high_|__op1_low__]
1399 _______________________________________________
1400 _______________________
1401 (1) [__op0_low__*__op1_low__]
1402 _______________________
1403 (2a) [__op0_low__*__op1_high_]
1404 _______________________
1405 (2b) [__op0_high_*__op1_low__]
1406 _______________________
1407 (3) [__op0_high_*__op1_high_]
1410 This gives a 4-word result. Since we are only interested in the
1411 lower 2 words, partial result (3) and the upper words of (2a) and
1412 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1413 calculated using non-widening multiplication.
1415 (1), however, needs to be calculated with an unsigned widening
1416 multiplication. If this operation is not directly supported we
1417 try using a signed widening multiplication and adjust the result.
1418 This adjustment works as follows:
1420 If both operands are positive then no adjustment is needed.
1422 If the operands have different signs, for example op0_low < 0 and
1423 op1_low >= 0, the instruction treats the most significant bit of
1424 op0_low as a sign bit instead of a bit with significance
1425 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1426 with 2**BITS_PER_WORD - op0_low, and two's complements the
1427 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1428 the result.
1430 Similarly, if both operands are negative, we need to add
1431 (op0_low + op1_low) * 2**BITS_PER_WORD.
1433 We use a trick to adjust quickly. We logically shift op0_low right
1434 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1435 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1436 logical shift exists, we do an arithmetic right shift and subtract
1437 the 0 or -1. */
1439 if (binoptab == smul_optab
1440 && class == MODE_INT
1441 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1442 && smul_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1443 && add_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1444 && ((umul_widen_optab->handlers[(int) mode].insn_code
1445 != CODE_FOR_nothing)
1446 || (smul_widen_optab->handlers[(int) mode].insn_code
1447 != CODE_FOR_nothing)))
1449 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1450 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1451 rtx op0_high = operand_subword_force (op0, high, mode);
1452 rtx op0_low = operand_subword_force (op0, low, mode);
1453 rtx op1_high = operand_subword_force (op1, high, mode);
1454 rtx op1_low = operand_subword_force (op1, low, mode);
1455 rtx product = 0;
1456 rtx op0_xhigh = NULL_RTX;
1457 rtx op1_xhigh = NULL_RTX;
1459 /* If the target is the same as one of the inputs, don't use it. This
1460 prevents problems with the REG_EQUAL note. */
1461 if (target == op0 || target == op1
1462 || (target != 0 && !REG_P (target)))
1463 target = 0;
1465 /* Multiply the two lower words to get a double-word product.
1466 If unsigned widening multiplication is available, use that;
1467 otherwise use the signed form and compensate. */
1469 if (umul_widen_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1471 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1472 target, 1, OPTAB_DIRECT);
1474 /* If we didn't succeed, delete everything we did so far. */
1475 if (product == 0)
1476 delete_insns_since (last);
1477 else
1478 op0_xhigh = op0_high, op1_xhigh = op1_high;
1481 if (product == 0
1482 && smul_widen_optab->handlers[(int) mode].insn_code
1483 != CODE_FOR_nothing)
1485 rtx wordm1 = GEN_INT (BITS_PER_WORD - 1);
1486 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1487 target, 1, OPTAB_DIRECT);
1488 op0_xhigh = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1489 NULL_RTX, 1, next_methods);
1490 if (op0_xhigh)
1491 op0_xhigh = expand_binop (word_mode, add_optab, op0_high,
1492 op0_xhigh, op0_xhigh, 0, next_methods);
1493 else
1495 op0_xhigh = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1496 NULL_RTX, 0, next_methods);
1497 if (op0_xhigh)
1498 op0_xhigh = expand_binop (word_mode, sub_optab, op0_high,
1499 op0_xhigh, op0_xhigh, 0,
1500 next_methods);
1503 op1_xhigh = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1504 NULL_RTX, 1, next_methods);
1505 if (op1_xhigh)
1506 op1_xhigh = expand_binop (word_mode, add_optab, op1_high,
1507 op1_xhigh, op1_xhigh, 0, next_methods);
1508 else
1510 op1_xhigh = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1511 NULL_RTX, 0, next_methods);
1512 if (op1_xhigh)
1513 op1_xhigh = expand_binop (word_mode, sub_optab, op1_high,
1514 op1_xhigh, op1_xhigh, 0,
1515 next_methods);
1519 /* If we have been able to directly compute the product of the
1520 low-order words of the operands and perform any required adjustments
1521 of the operands, we proceed by trying two more multiplications
1522 and then computing the appropriate sum.
1524 We have checked above that the required addition is provided.
1525 Full-word addition will normally always succeed, especially if
1526 it is provided at all, so we don't worry about its failure. The
1527 multiplication may well fail, however, so we do handle that. */
1529 if (product && op0_xhigh && op1_xhigh)
1531 rtx product_high = operand_subword (product, high, 1, mode);
1532 rtx temp = expand_binop (word_mode, binoptab, op0_low, op1_xhigh,
1533 NULL_RTX, 0, OPTAB_DIRECT);
1535 if (!REG_P (product_high))
1536 product_high = force_reg (word_mode, product_high);
1538 if (temp != 0)
1539 temp = expand_binop (word_mode, add_optab, temp, product_high,
1540 product_high, 0, next_methods);
1542 if (temp != 0 && temp != product_high)
1543 emit_move_insn (product_high, temp);
1545 if (temp != 0)
1546 temp = expand_binop (word_mode, binoptab, op1_low, op0_xhigh,
1547 NULL_RTX, 0, OPTAB_DIRECT);
1549 if (temp != 0)
1550 temp = expand_binop (word_mode, add_optab, temp,
1551 product_high, product_high,
1552 0, next_methods);
1554 if (temp != 0 && temp != product_high)
1555 emit_move_insn (product_high, temp);
1557 emit_move_insn (operand_subword (product, high, 1, mode), product_high);
1559 if (temp != 0)
1561 if (mov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1563 temp = emit_move_insn (product, product);
1564 set_unique_reg_note (temp,
1565 REG_EQUAL,
1566 gen_rtx_fmt_ee (MULT, mode,
1567 copy_rtx (op0),
1568 copy_rtx (op1)));
1571 return product;
1575 /* If we get here, we couldn't do it for some reason even though we
1576 originally thought we could. Delete anything we've emitted in
1577 trying to do it. */
1579 delete_insns_since (last);
1582 /* It can't be open-coded in this mode.
1583 Use a library call if one is available and caller says that's ok. */
1585 if (binoptab->handlers[(int) mode].libfunc
1586 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
1588 rtx insns;
1589 rtx op1x = op1;
1590 enum machine_mode op1_mode = mode;
1591 rtx value;
1593 start_sequence ();
1595 if (shift_op)
1597 op1_mode = word_mode;
1598 /* Specify unsigned here,
1599 since negative shift counts are meaningless. */
1600 op1x = convert_to_mode (word_mode, op1, 1);
1603 if (GET_MODE (op0) != VOIDmode
1604 && GET_MODE (op0) != mode)
1605 op0 = convert_to_mode (mode, op0, unsignedp);
1607 /* Pass 1 for NO_QUEUE so we don't lose any increments
1608 if the libcall is cse'd or moved. */
1609 value = emit_library_call_value (binoptab->handlers[(int) mode].libfunc,
1610 NULL_RTX, LCT_CONST, mode, 2,
1611 op0, mode, op1x, op1_mode);
1613 insns = get_insns ();
1614 end_sequence ();
1616 target = gen_reg_rtx (mode);
1617 emit_libcall_block (insns, target, value,
1618 gen_rtx_fmt_ee (binoptab->code, mode, op0, op1));
1620 return target;
1623 delete_insns_since (last);
1625 /* It can't be done in this mode. Can we do it in a wider mode? */
1627 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
1628 || methods == OPTAB_MUST_WIDEN))
1630 /* Caller says, don't even try. */
1631 delete_insns_since (entry_last);
1632 return 0;
1635 /* Compute the value of METHODS to pass to recursive calls.
1636 Don't allow widening to be tried recursively. */
1638 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
1640 /* Look for a wider mode of the same class for which it appears we can do
1641 the operation. */
1643 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1645 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1646 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1648 if ((binoptab->handlers[(int) wider_mode].insn_code
1649 != CODE_FOR_nothing)
1650 || (methods == OPTAB_LIB
1651 && binoptab->handlers[(int) wider_mode].libfunc))
1653 rtx xop0 = op0, xop1 = op1;
1654 int no_extend = 0;
1656 /* For certain integer operations, we need not actually extend
1657 the narrow operands, as long as we will truncate
1658 the results to the same narrowness. */
1660 if ((binoptab == ior_optab || binoptab == and_optab
1661 || binoptab == xor_optab
1662 || binoptab == add_optab || binoptab == sub_optab
1663 || binoptab == smul_optab || binoptab == ashl_optab)
1664 && class == MODE_INT)
1665 no_extend = 1;
1667 xop0 = widen_operand (xop0, wider_mode, mode,
1668 unsignedp, no_extend);
1670 /* The second operand of a shift must always be extended. */
1671 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1672 no_extend && binoptab != ashl_optab);
1674 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1675 unsignedp, methods);
1676 if (temp)
1678 if (class != MODE_INT)
1680 if (target == 0)
1681 target = gen_reg_rtx (mode);
1682 convert_move (target, temp, 0);
1683 return target;
1685 else
1686 return gen_lowpart (mode, temp);
1688 else
1689 delete_insns_since (last);
1694 delete_insns_since (entry_last);
1695 return 0;
1698 /* Expand a binary operator which has both signed and unsigned forms.
1699 UOPTAB is the optab for unsigned operations, and SOPTAB is for
1700 signed operations.
1702 If we widen unsigned operands, we may use a signed wider operation instead
1703 of an unsigned wider operation, since the result would be the same. */
1706 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
1707 rtx op0, rtx op1, rtx target, int unsignedp,
1708 enum optab_methods methods)
1710 rtx temp;
1711 optab direct_optab = unsignedp ? uoptab : soptab;
1712 struct optab wide_soptab;
1714 /* Do it without widening, if possible. */
1715 temp = expand_binop (mode, direct_optab, op0, op1, target,
1716 unsignedp, OPTAB_DIRECT);
1717 if (temp || methods == OPTAB_DIRECT)
1718 return temp;
1720 /* Try widening to a signed int. Make a fake signed optab that
1721 hides any signed insn for direct use. */
1722 wide_soptab = *soptab;
1723 wide_soptab.handlers[(int) mode].insn_code = CODE_FOR_nothing;
1724 wide_soptab.handlers[(int) mode].libfunc = 0;
1726 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
1727 unsignedp, OPTAB_WIDEN);
1729 /* For unsigned operands, try widening to an unsigned int. */
1730 if (temp == 0 && unsignedp)
1731 temp = expand_binop (mode, uoptab, op0, op1, target,
1732 unsignedp, OPTAB_WIDEN);
1733 if (temp || methods == OPTAB_WIDEN)
1734 return temp;
1736 /* Use the right width lib call if that exists. */
1737 temp = expand_binop (mode, direct_optab, op0, op1, target, unsignedp, OPTAB_LIB);
1738 if (temp || methods == OPTAB_LIB)
1739 return temp;
1741 /* Must widen and use a lib call, use either signed or unsigned. */
1742 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
1743 unsignedp, methods);
1744 if (temp != 0)
1745 return temp;
1746 if (unsignedp)
1747 return expand_binop (mode, uoptab, op0, op1, target,
1748 unsignedp, methods);
1749 return 0;
1752 /* Generate code to perform an operation specified by UNOPPTAB
1753 on operand OP0, with two results to TARG0 and TARG1.
1754 We assume that the order of the operands for the instruction
1755 is TARG0, TARG1, OP0.
1757 Either TARG0 or TARG1 may be zero, but what that means is that
1758 the result is not actually wanted. We will generate it into
1759 a dummy pseudo-reg and discard it. They may not both be zero.
1761 Returns 1 if this operation can be performed; 0 if not. */
1764 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
1765 int unsignedp)
1767 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1768 enum mode_class class;
1769 enum machine_mode wider_mode;
1770 rtx entry_last = get_last_insn ();
1771 rtx last;
1773 class = GET_MODE_CLASS (mode);
1775 if (flag_force_mem)
1776 op0 = force_not_mem (op0);
1778 if (!targ0)
1779 targ0 = gen_reg_rtx (mode);
1780 if (!targ1)
1781 targ1 = gen_reg_rtx (mode);
1783 /* Record where to go back to if we fail. */
1784 last = get_last_insn ();
1786 if (unoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1788 int icode = (int) unoptab->handlers[(int) mode].insn_code;
1789 enum machine_mode mode0 = insn_data[icode].operand[2].mode;
1790 rtx pat;
1791 rtx xop0 = op0;
1793 if (GET_MODE (xop0) != VOIDmode
1794 && GET_MODE (xop0) != mode0)
1795 xop0 = convert_to_mode (mode0, xop0, unsignedp);
1797 /* Now, if insn doesn't accept these operands, put them into pseudos. */
1798 if (! (*insn_data[icode].operand[2].predicate) (xop0, mode0))
1799 xop0 = copy_to_mode_reg (mode0, xop0);
1801 /* We could handle this, but we should always be called with a pseudo
1802 for our targets and all insns should take them as outputs. */
1803 if (! (*insn_data[icode].operand[0].predicate) (targ0, mode)
1804 || ! (*insn_data[icode].operand[1].predicate) (targ1, mode))
1805 abort ();
1807 pat = GEN_FCN (icode) (targ0, targ1, xop0);
1808 if (pat)
1810 emit_insn (pat);
1811 return 1;
1813 else
1814 delete_insns_since (last);
1817 /* It can't be done in this mode. Can we do it in a wider mode? */
1819 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1821 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1822 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1824 if (unoptab->handlers[(int) wider_mode].insn_code
1825 != CODE_FOR_nothing)
1827 rtx t0 = gen_reg_rtx (wider_mode);
1828 rtx t1 = gen_reg_rtx (wider_mode);
1829 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
1831 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
1833 convert_move (targ0, t0, unsignedp);
1834 convert_move (targ1, t1, unsignedp);
1835 return 1;
1837 else
1838 delete_insns_since (last);
1843 delete_insns_since (entry_last);
1844 return 0;
1847 /* Generate code to perform an operation specified by BINOPTAB
1848 on operands OP0 and OP1, with two results to TARG1 and TARG2.
1849 We assume that the order of the operands for the instruction
1850 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
1851 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
1853 Either TARG0 or TARG1 may be zero, but what that means is that
1854 the result is not actually wanted. We will generate it into
1855 a dummy pseudo-reg and discard it. They may not both be zero.
1857 Returns 1 if this operation can be performed; 0 if not. */
1860 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
1861 int unsignedp)
1863 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1864 enum mode_class class;
1865 enum machine_mode wider_mode;
1866 rtx entry_last = get_last_insn ();
1867 rtx last;
1869 class = GET_MODE_CLASS (mode);
1871 if (flag_force_mem)
1873 op0 = force_not_mem (op0);
1874 op1 = force_not_mem (op1);
1877 /* If we are inside an appropriately-short loop and we are optimizing,
1878 force expensive constants into a register. */
1879 if (CONSTANT_P (op0) && optimize
1880 && rtx_cost (op0, binoptab->code) > COSTS_N_INSNS (1))
1881 op0 = force_reg (mode, op0);
1883 if (CONSTANT_P (op1) && optimize
1884 && rtx_cost (op1, binoptab->code) > COSTS_N_INSNS (1))
1885 op1 = force_reg (mode, op1);
1887 if (!targ0)
1888 targ0 = gen_reg_rtx (mode);
1889 if (!targ1)
1890 targ1 = gen_reg_rtx (mode);
1892 /* Record where to go back to if we fail. */
1893 last = get_last_insn ();
1895 if (binoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1897 int icode = (int) binoptab->handlers[(int) mode].insn_code;
1898 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
1899 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
1900 rtx pat;
1901 rtx xop0 = op0, xop1 = op1;
1903 /* In case the insn wants input operands in modes different from
1904 those of the actual operands, convert the operands. It would
1905 seem that we don't need to convert CONST_INTs, but we do, so
1906 that they're properly zero-extended, sign-extended or truncated
1907 for their mode. */
1909 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
1910 xop0 = convert_modes (mode0,
1911 GET_MODE (op0) != VOIDmode
1912 ? GET_MODE (op0)
1913 : mode,
1914 xop0, unsignedp);
1916 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
1917 xop1 = convert_modes (mode1,
1918 GET_MODE (op1) != VOIDmode
1919 ? GET_MODE (op1)
1920 : mode,
1921 xop1, unsignedp);
1923 /* Now, if insn doesn't accept these operands, put them into pseudos. */
1924 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0))
1925 xop0 = copy_to_mode_reg (mode0, xop0);
1927 if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1))
1928 xop1 = copy_to_mode_reg (mode1, xop1);
1930 /* We could handle this, but we should always be called with a pseudo
1931 for our targets and all insns should take them as outputs. */
1932 if (! (*insn_data[icode].operand[0].predicate) (targ0, mode)
1933 || ! (*insn_data[icode].operand[3].predicate) (targ1, mode))
1934 abort ();
1936 pat = GEN_FCN (icode) (targ0, xop0, xop1, targ1);
1937 if (pat)
1939 emit_insn (pat);
1940 return 1;
1942 else
1943 delete_insns_since (last);
1946 /* It can't be done in this mode. Can we do it in a wider mode? */
1948 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1950 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1951 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1953 if (binoptab->handlers[(int) wider_mode].insn_code
1954 != CODE_FOR_nothing)
1956 rtx t0 = gen_reg_rtx (wider_mode);
1957 rtx t1 = gen_reg_rtx (wider_mode);
1958 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
1959 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
1961 if (expand_twoval_binop (binoptab, cop0, cop1,
1962 t0, t1, unsignedp))
1964 convert_move (targ0, t0, unsignedp);
1965 convert_move (targ1, t1, unsignedp);
1966 return 1;
1968 else
1969 delete_insns_since (last);
1974 delete_insns_since (entry_last);
1975 return 0;
1978 /* Expand the two-valued library call indicated by BINOPTAB, but
1979 preserve only one of the values. If TARG0 is non-NULL, the first
1980 value is placed into TARG0; otherwise the second value is placed
1981 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
1982 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
1983 This routine assumes that the value returned by the library call is
1984 as if the return value was of an integral mode twice as wide as the
1985 mode of OP0. Returns 1 if the call was successful. */
1987 bool
1988 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
1989 rtx targ0, rtx targ1, enum rtx_code code)
1991 enum machine_mode mode;
1992 enum machine_mode libval_mode;
1993 rtx libval;
1994 rtx insns;
1996 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
1997 if (!((targ0 != NULL_RTX) ^ (targ1 != NULL_RTX)))
1998 abort ();
2000 mode = GET_MODE (op0);
2001 if (!binoptab->handlers[(int) mode].libfunc)
2002 return false;
2004 /* The value returned by the library function will have twice as
2005 many bits as the nominal MODE. */
2006 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2007 MODE_INT);
2008 start_sequence ();
2009 libval = emit_library_call_value (binoptab->handlers[(int) mode].libfunc,
2010 NULL_RTX, LCT_CONST,
2011 libval_mode, 2,
2012 op0, mode,
2013 op1, mode);
2014 /* Get the part of VAL containing the value that we want. */
2015 libval = simplify_gen_subreg (mode, libval, libval_mode,
2016 targ0 ? 0 : GET_MODE_SIZE (mode));
2017 insns = get_insns ();
2018 end_sequence ();
2019 /* Move the into the desired location. */
2020 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2021 gen_rtx_fmt_ee (code, mode, op0, op1));
2023 return true;
2027 /* Wrapper around expand_unop which takes an rtx code to specify
2028 the operation to perform, not an optab pointer. All other
2029 arguments are the same. */
2031 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2032 rtx target, int unsignedp)
2034 optab unop = code_to_optab[(int) code];
2035 if (unop == 0)
2036 abort ();
2038 return expand_unop (mode, unop, op0, target, unsignedp);
2041 /* Try calculating
2042 (clz:narrow x)
2044 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)). */
2045 static rtx
2046 widen_clz (enum machine_mode mode, rtx op0, rtx target)
2048 enum mode_class class = GET_MODE_CLASS (mode);
2049 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2051 enum machine_mode wider_mode;
2052 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2053 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2055 if (clz_optab->handlers[(int) wider_mode].insn_code
2056 != CODE_FOR_nothing)
2058 rtx xop0, temp, last;
2060 last = get_last_insn ();
2062 if (target == 0)
2063 target = gen_reg_rtx (mode);
2064 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2065 temp = expand_unop (wider_mode, clz_optab, xop0, NULL_RTX, true);
2066 if (temp != 0)
2067 temp = expand_binop (wider_mode, sub_optab, temp,
2068 GEN_INT (GET_MODE_BITSIZE (wider_mode)
2069 - GET_MODE_BITSIZE (mode)),
2070 target, true, OPTAB_DIRECT);
2071 if (temp == 0)
2072 delete_insns_since (last);
2074 return temp;
2078 return 0;
2081 /* Try calculating (parity x) as (and (popcount x) 1), where
2082 popcount can also be done in a wider mode. */
2083 static rtx
2084 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2086 enum mode_class class = GET_MODE_CLASS (mode);
2087 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2089 enum machine_mode wider_mode;
2090 for (wider_mode = mode; wider_mode != VOIDmode;
2091 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2093 if (popcount_optab->handlers[(int) wider_mode].insn_code
2094 != CODE_FOR_nothing)
2096 rtx xop0, temp, last;
2098 last = get_last_insn ();
2100 if (target == 0)
2101 target = gen_reg_rtx (mode);
2102 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2103 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2104 true);
2105 if (temp != 0)
2106 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2107 target, true, OPTAB_DIRECT);
2108 if (temp == 0)
2109 delete_insns_since (last);
2111 return temp;
2115 return 0;
2118 /* Generate code to perform an operation specified by UNOPTAB
2119 on operand OP0, with result having machine-mode MODE.
2121 UNSIGNEDP is for the case where we have to widen the operands
2122 to perform the operation. It says to use zero-extension.
2124 If TARGET is nonzero, the value
2125 is generated there, if it is convenient to do so.
2126 In all cases an rtx is returned for the locus of the value;
2127 this may or may not be TARGET. */
2130 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2131 int unsignedp)
2133 enum mode_class class;
2134 enum machine_mode wider_mode;
2135 rtx temp;
2136 rtx last = get_last_insn ();
2137 rtx pat;
2139 class = GET_MODE_CLASS (mode);
2141 if (flag_force_mem)
2142 op0 = force_not_mem (op0);
2144 if (unoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2146 int icode = (int) unoptab->handlers[(int) mode].insn_code;
2147 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2148 rtx xop0 = op0;
2150 if (target)
2151 temp = target;
2152 else
2153 temp = gen_reg_rtx (mode);
2155 if (GET_MODE (xop0) != VOIDmode
2156 && GET_MODE (xop0) != mode0)
2157 xop0 = convert_to_mode (mode0, xop0, unsignedp);
2159 /* Now, if insn doesn't accept our operand, put it into a pseudo. */
2161 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0))
2162 xop0 = copy_to_mode_reg (mode0, xop0);
2164 if (! (*insn_data[icode].operand[0].predicate) (temp, mode))
2165 temp = gen_reg_rtx (mode);
2167 pat = GEN_FCN (icode) (temp, xop0);
2168 if (pat)
2170 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2171 && ! add_equal_note (pat, temp, unoptab->code, xop0, NULL_RTX))
2173 delete_insns_since (last);
2174 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
2177 emit_insn (pat);
2179 return temp;
2181 else
2182 delete_insns_since (last);
2185 /* It can't be done in this mode. Can we open-code it in a wider mode? */
2187 /* Widening clz needs special treatment. */
2188 if (unoptab == clz_optab)
2190 temp = widen_clz (mode, op0, target);
2191 if (temp)
2192 return temp;
2193 else
2194 goto try_libcall;
2197 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2198 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2199 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2201 if (unoptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing)
2203 rtx xop0 = op0;
2205 /* For certain operations, we need not actually extend
2206 the narrow operand, as long as we will truncate the
2207 results to the same narrowness. */
2209 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2210 (unoptab == neg_optab
2211 || unoptab == one_cmpl_optab)
2212 && class == MODE_INT);
2214 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2215 unsignedp);
2217 if (temp)
2219 if (class != MODE_INT)
2221 if (target == 0)
2222 target = gen_reg_rtx (mode);
2223 convert_move (target, temp, 0);
2224 return target;
2226 else
2227 return gen_lowpart (mode, temp);
2229 else
2230 delete_insns_since (last);
2234 /* These can be done a word at a time. */
2235 if (unoptab == one_cmpl_optab
2236 && class == MODE_INT
2237 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
2238 && unoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
2240 int i;
2241 rtx insns;
2243 if (target == 0 || target == op0)
2244 target = gen_reg_rtx (mode);
2246 start_sequence ();
2248 /* Do the actual arithmetic. */
2249 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
2251 rtx target_piece = operand_subword (target, i, 1, mode);
2252 rtx x = expand_unop (word_mode, unoptab,
2253 operand_subword_force (op0, i, mode),
2254 target_piece, unsignedp);
2256 if (target_piece != x)
2257 emit_move_insn (target_piece, x);
2260 insns = get_insns ();
2261 end_sequence ();
2263 emit_no_conflict_block (insns, target, op0, NULL_RTX,
2264 gen_rtx_fmt_e (unoptab->code, mode,
2265 copy_rtx (op0)));
2266 return target;
2269 /* Try negating floating point values by flipping the sign bit. */
2270 if (unoptab->code == NEG && class == MODE_FLOAT
2271 && GET_MODE_BITSIZE (mode) <= 2 * HOST_BITS_PER_WIDE_INT)
2273 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
2274 enum machine_mode imode = int_mode_for_mode (mode);
2275 int bitpos = (fmt != 0) ? fmt->signbit : -1;
2277 if (imode != BLKmode && bitpos >= 0 && fmt->has_signed_zero)
2279 HOST_WIDE_INT hi, lo;
2280 rtx last = get_last_insn ();
2282 /* Handle targets with different FP word orders. */
2283 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2285 int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
2286 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
2287 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
2290 if (bitpos < HOST_BITS_PER_WIDE_INT)
2292 hi = 0;
2293 lo = (HOST_WIDE_INT) 1 << bitpos;
2295 else
2297 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
2298 lo = 0;
2300 temp = expand_binop (imode, xor_optab,
2301 gen_lowpart (imode, op0),
2302 immed_double_const (lo, hi, imode),
2303 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2304 if (temp != 0)
2306 rtx insn;
2307 if (target == 0)
2308 target = gen_reg_rtx (mode);
2309 insn = emit_move_insn (target, gen_lowpart (mode, temp));
2310 set_unique_reg_note (insn, REG_EQUAL,
2311 gen_rtx_fmt_e (NEG, mode,
2312 copy_rtx (op0)));
2313 return target;
2315 delete_insns_since (last);
2319 /* Try calculating parity (x) as popcount (x) % 2. */
2320 if (unoptab == parity_optab)
2322 temp = expand_parity (mode, op0, target);
2323 if (temp)
2324 return temp;
2327 /* If there is no negation pattern, try subtracting from zero. */
2328 if (unoptab == neg_optab && class == MODE_INT)
2330 temp = expand_binop (mode, sub_optab, CONST0_RTX (mode), op0,
2331 target, unsignedp, OPTAB_DIRECT);
2332 if (temp)
2333 return temp;
2336 try_libcall:
2337 /* Now try a library call in this mode. */
2338 if (unoptab->handlers[(int) mode].libfunc)
2340 rtx insns;
2341 rtx value;
2342 enum machine_mode outmode = mode;
2344 /* All of these functions return small values. Thus we choose to
2345 have them return something that isn't a double-word. */
2346 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
2347 || unoptab == popcount_optab || unoptab == parity_optab)
2348 outmode
2349 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node)));
2351 start_sequence ();
2353 /* Pass 1 for NO_QUEUE so we don't lose any increments
2354 if the libcall is cse'd or moved. */
2355 value = emit_library_call_value (unoptab->handlers[(int) mode].libfunc,
2356 NULL_RTX, LCT_CONST, outmode,
2357 1, op0, mode);
2358 insns = get_insns ();
2359 end_sequence ();
2361 target = gen_reg_rtx (outmode);
2362 emit_libcall_block (insns, target, value,
2363 gen_rtx_fmt_e (unoptab->code, mode, op0));
2365 return target;
2368 /* It can't be done in this mode. Can we do it in a wider mode? */
2370 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2372 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2373 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2375 if ((unoptab->handlers[(int) wider_mode].insn_code
2376 != CODE_FOR_nothing)
2377 || unoptab->handlers[(int) wider_mode].libfunc)
2379 rtx xop0 = op0;
2381 /* For certain operations, we need not actually extend
2382 the narrow operand, as long as we will truncate the
2383 results to the same narrowness. */
2385 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2386 (unoptab == neg_optab
2387 || unoptab == one_cmpl_optab)
2388 && class == MODE_INT);
2390 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2391 unsignedp);
2393 /* If we are generating clz using wider mode, adjust the
2394 result. */
2395 if (unoptab == clz_optab && temp != 0)
2396 temp = expand_binop (wider_mode, sub_optab, temp,
2397 GEN_INT (GET_MODE_BITSIZE (wider_mode)
2398 - GET_MODE_BITSIZE (mode)),
2399 target, true, OPTAB_DIRECT);
2401 if (temp)
2403 if (class != MODE_INT)
2405 if (target == 0)
2406 target = gen_reg_rtx (mode);
2407 convert_move (target, temp, 0);
2408 return target;
2410 else
2411 return gen_lowpart (mode, temp);
2413 else
2414 delete_insns_since (last);
2419 /* If there is no negate operation, try doing a subtract from zero.
2420 The US Software GOFAST library needs this. FIXME: This is *wrong*
2421 for floating-point operations due to negative zeros! */
2422 if (unoptab->code == NEG)
2424 rtx temp;
2425 temp = expand_binop (mode,
2426 unoptab == negv_optab ? subv_optab : sub_optab,
2427 CONST0_RTX (mode), op0,
2428 target, unsignedp, OPTAB_LIB_WIDEN);
2429 if (temp)
2430 return temp;
2433 return 0;
2436 /* Emit code to compute the absolute value of OP0, with result to
2437 TARGET if convenient. (TARGET may be 0.) The return value says
2438 where the result actually is to be found.
2440 MODE is the mode of the operand; the mode of the result is
2441 different but can be deduced from MODE.
2446 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
2447 int result_unsignedp)
2449 rtx temp;
2451 if (! flag_trapv)
2452 result_unsignedp = 1;
2454 /* First try to do it with a special abs instruction. */
2455 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
2456 op0, target, 0);
2457 if (temp != 0)
2458 return temp;
2460 /* For floating point modes, try clearing the sign bit. */
2461 if (GET_MODE_CLASS (mode) == MODE_FLOAT
2462 && GET_MODE_BITSIZE (mode) <= 2 * HOST_BITS_PER_WIDE_INT)
2464 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
2465 enum machine_mode imode = int_mode_for_mode (mode);
2466 int bitpos = (fmt != 0) ? fmt->signbit : -1;
2468 if (imode != BLKmode && bitpos >= 0)
2470 HOST_WIDE_INT hi, lo;
2471 rtx last = get_last_insn ();
2473 /* Handle targets with different FP word orders. */
2474 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2476 int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
2477 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
2478 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
2481 if (bitpos < HOST_BITS_PER_WIDE_INT)
2483 hi = 0;
2484 lo = (HOST_WIDE_INT) 1 << bitpos;
2486 else
2488 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
2489 lo = 0;
2491 temp = expand_binop (imode, and_optab,
2492 gen_lowpart (imode, op0),
2493 immed_double_const (~lo, ~hi, imode),
2494 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2495 if (temp != 0)
2497 rtx insn;
2498 if (target == 0)
2499 target = gen_reg_rtx (mode);
2500 insn = emit_move_insn (target, gen_lowpart (mode, temp));
2501 set_unique_reg_note (insn, REG_EQUAL,
2502 gen_rtx_fmt_e (ABS, mode,
2503 copy_rtx (op0)));
2504 return target;
2506 delete_insns_since (last);
2510 /* If we have a MAX insn, we can do this as MAX (x, -x). */
2511 if (smax_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2513 rtx last = get_last_insn ();
2515 temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
2516 if (temp != 0)
2517 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
2518 OPTAB_WIDEN);
2520 if (temp != 0)
2521 return temp;
2523 delete_insns_since (last);
2526 /* If this machine has expensive jumps, we can do integer absolute
2527 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
2528 where W is the width of MODE. */
2530 if (GET_MODE_CLASS (mode) == MODE_INT && BRANCH_COST >= 2)
2532 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
2533 size_int (GET_MODE_BITSIZE (mode) - 1),
2534 NULL_RTX, 0);
2536 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
2537 OPTAB_LIB_WIDEN);
2538 if (temp != 0)
2539 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
2540 temp, extended, target, 0, OPTAB_LIB_WIDEN);
2542 if (temp != 0)
2543 return temp;
2546 return NULL_RTX;
2550 expand_abs (enum machine_mode mode, rtx op0, rtx target,
2551 int result_unsignedp, int safe)
2553 rtx temp, op1;
2555 if (! flag_trapv)
2556 result_unsignedp = 1;
2558 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
2559 if (temp != 0)
2560 return temp;
2562 /* If that does not win, use conditional jump and negate. */
2564 /* It is safe to use the target if it is the same
2565 as the source if this is also a pseudo register */
2566 if (op0 == target && REG_P (op0)
2567 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
2568 safe = 1;
2570 op1 = gen_label_rtx ();
2571 if (target == 0 || ! safe
2572 || GET_MODE (target) != mode
2573 || (MEM_P (target) && MEM_VOLATILE_P (target))
2574 || (REG_P (target)
2575 && REGNO (target) < FIRST_PSEUDO_REGISTER))
2576 target = gen_reg_rtx (mode);
2578 emit_move_insn (target, op0);
2579 NO_DEFER_POP;
2581 /* If this mode is an integer too wide to compare properly,
2582 compare word by word. Rely on CSE to optimize constant cases. */
2583 if (GET_MODE_CLASS (mode) == MODE_INT
2584 && ! can_compare_p (GE, mode, ccp_jump))
2585 do_jump_by_parts_greater_rtx (mode, 0, target, const0_rtx,
2586 NULL_RTX, op1);
2587 else
2588 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
2589 NULL_RTX, NULL_RTX, op1);
2591 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
2592 target, target, 0);
2593 if (op0 != target)
2594 emit_move_insn (target, op0);
2595 emit_label (op1);
2596 OK_DEFER_POP;
2597 return target;
2600 /* Generate an instruction whose insn-code is INSN_CODE,
2601 with two operands: an output TARGET and an input OP0.
2602 TARGET *must* be nonzero, and the output is always stored there.
2603 CODE is an rtx code such that (CODE OP0) is an rtx that describes
2604 the value that is stored into TARGET. */
2606 void
2607 emit_unop_insn (int icode, rtx target, rtx op0, enum rtx_code code)
2609 rtx temp;
2610 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2611 rtx pat;
2613 temp = target;
2615 /* Sign and zero extension from memory is often done specially on
2616 RISC machines, so forcing into a register here can pessimize
2617 code. */
2618 if (flag_force_mem && code != SIGN_EXTEND && code != ZERO_EXTEND)
2619 op0 = force_not_mem (op0);
2621 /* Now, if insn does not accept our operands, put them into pseudos. */
2623 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
2624 op0 = copy_to_mode_reg (mode0, op0);
2626 if (! (*insn_data[icode].operand[0].predicate) (temp, GET_MODE (temp))
2627 || (flag_force_mem && MEM_P (temp)))
2628 temp = gen_reg_rtx (GET_MODE (temp));
2630 pat = GEN_FCN (icode) (temp, op0);
2632 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
2633 add_equal_note (pat, temp, code, op0, NULL_RTX);
2635 emit_insn (pat);
2637 if (temp != target)
2638 emit_move_insn (target, temp);
2641 /* Emit code to perform a series of operations on a multi-word quantity, one
2642 word at a time.
2644 Such a block is preceded by a CLOBBER of the output, consists of multiple
2645 insns, each setting one word of the output, and followed by a SET copying
2646 the output to itself.
2648 Each of the insns setting words of the output receives a REG_NO_CONFLICT
2649 note indicating that it doesn't conflict with the (also multi-word)
2650 inputs. The entire block is surrounded by REG_LIBCALL and REG_RETVAL
2651 notes.
2653 INSNS is a block of code generated to perform the operation, not including
2654 the CLOBBER and final copy. All insns that compute intermediate values
2655 are first emitted, followed by the block as described above.
2657 TARGET, OP0, and OP1 are the output and inputs of the operations,
2658 respectively. OP1 may be zero for a unary operation.
2660 EQUIV, if nonzero, is an expression to be placed into a REG_EQUAL note
2661 on the last insn.
2663 If TARGET is not a register, INSNS is simply emitted with no special
2664 processing. Likewise if anything in INSNS is not an INSN or if
2665 there is a libcall block inside INSNS.
2667 The final insn emitted is returned. */
2670 emit_no_conflict_block (rtx insns, rtx target, rtx op0, rtx op1, rtx equiv)
2672 rtx prev, next, first, last, insn;
2674 if (!REG_P (target) || reload_in_progress)
2675 return emit_insn (insns);
2676 else
2677 for (insn = insns; insn; insn = NEXT_INSN (insn))
2678 if (!NONJUMP_INSN_P (insn)
2679 || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
2680 return emit_insn (insns);
2682 /* First emit all insns that do not store into words of the output and remove
2683 these from the list. */
2684 for (insn = insns; insn; insn = next)
2686 rtx set = 0, note;
2687 int i;
2689 next = NEXT_INSN (insn);
2691 /* Some ports (cris) create a libcall regions at their own. We must
2692 avoid any potential nesting of LIBCALLs. */
2693 if ((note = find_reg_note (insn, REG_LIBCALL, NULL)) != NULL)
2694 remove_note (insn, note);
2695 if ((note = find_reg_note (insn, REG_RETVAL, NULL)) != NULL)
2696 remove_note (insn, note);
2698 if (GET_CODE (PATTERN (insn)) == SET || GET_CODE (PATTERN (insn)) == USE
2699 || GET_CODE (PATTERN (insn)) == CLOBBER)
2700 set = PATTERN (insn);
2701 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2703 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2704 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
2706 set = XVECEXP (PATTERN (insn), 0, i);
2707 break;
2711 if (set == 0)
2712 abort ();
2714 if (! reg_overlap_mentioned_p (target, SET_DEST (set)))
2716 if (PREV_INSN (insn))
2717 NEXT_INSN (PREV_INSN (insn)) = next;
2718 else
2719 insns = next;
2721 if (next)
2722 PREV_INSN (next) = PREV_INSN (insn);
2724 add_insn (insn);
2728 prev = get_last_insn ();
2730 /* Now write the CLOBBER of the output, followed by the setting of each
2731 of the words, followed by the final copy. */
2732 if (target != op0 && target != op1)
2733 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
2735 for (insn = insns; insn; insn = next)
2737 next = NEXT_INSN (insn);
2738 add_insn (insn);
2740 if (op1 && REG_P (op1))
2741 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_NO_CONFLICT, op1,
2742 REG_NOTES (insn));
2744 if (op0 && REG_P (op0))
2745 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_NO_CONFLICT, op0,
2746 REG_NOTES (insn));
2749 if (mov_optab->handlers[(int) GET_MODE (target)].insn_code
2750 != CODE_FOR_nothing)
2752 last = emit_move_insn (target, target);
2753 if (equiv)
2754 set_unique_reg_note (last, REG_EQUAL, equiv);
2756 else
2758 last = get_last_insn ();
2760 /* Remove any existing REG_EQUAL note from "last", or else it will
2761 be mistaken for a note referring to the full contents of the
2762 alleged libcall value when found together with the REG_RETVAL
2763 note added below. An existing note can come from an insn
2764 expansion at "last". */
2765 remove_note (last, find_reg_note (last, REG_EQUAL, NULL_RTX));
2768 if (prev == 0)
2769 first = get_insns ();
2770 else
2771 first = NEXT_INSN (prev);
2773 /* Encapsulate the block so it gets manipulated as a unit. */
2774 REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
2775 REG_NOTES (first));
2776 REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
2778 return last;
2781 /* Emit code to make a call to a constant function or a library call.
2783 INSNS is a list containing all insns emitted in the call.
2784 These insns leave the result in RESULT. Our block is to copy RESULT
2785 to TARGET, which is logically equivalent to EQUIV.
2787 We first emit any insns that set a pseudo on the assumption that these are
2788 loading constants into registers; doing so allows them to be safely cse'ed
2789 between blocks. Then we emit all the other insns in the block, followed by
2790 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
2791 note with an operand of EQUIV.
2793 Moving assignments to pseudos outside of the block is done to improve
2794 the generated code, but is not required to generate correct code,
2795 hence being unable to move an assignment is not grounds for not making
2796 a libcall block. There are two reasons why it is safe to leave these
2797 insns inside the block: First, we know that these pseudos cannot be
2798 used in generated RTL outside the block since they are created for
2799 temporary purposes within the block. Second, CSE will not record the
2800 values of anything set inside a libcall block, so we know they must
2801 be dead at the end of the block.
2803 Except for the first group of insns (the ones setting pseudos), the
2804 block is delimited by REG_RETVAL and REG_LIBCALL notes. */
2806 void
2807 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
2809 rtx final_dest = target;
2810 rtx prev, next, first, last, insn;
2812 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
2813 into a MEM later. Protect the libcall block from this change. */
2814 if (! REG_P (target) || REG_USERVAR_P (target))
2815 target = gen_reg_rtx (GET_MODE (target));
2817 /* If we're using non-call exceptions, a libcall corresponding to an
2818 operation that may trap may also trap. */
2819 if (flag_non_call_exceptions && may_trap_p (equiv))
2821 for (insn = insns; insn; insn = NEXT_INSN (insn))
2822 if (CALL_P (insn))
2824 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2826 if (note != 0 && INTVAL (XEXP (note, 0)) <= 0)
2827 remove_note (insn, note);
2830 else
2831 /* look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
2832 reg note to indicate that this call cannot throw or execute a nonlocal
2833 goto (unless there is already a REG_EH_REGION note, in which case
2834 we update it). */
2835 for (insn = insns; insn; insn = NEXT_INSN (insn))
2836 if (CALL_P (insn))
2838 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2840 if (note != 0)
2841 XEXP (note, 0) = constm1_rtx;
2842 else
2843 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, constm1_rtx,
2844 REG_NOTES (insn));
2847 /* First emit all insns that set pseudos. Remove them from the list as
2848 we go. Avoid insns that set pseudos which were referenced in previous
2849 insns. These can be generated by move_by_pieces, for example,
2850 to update an address. Similarly, avoid insns that reference things
2851 set in previous insns. */
2853 for (insn = insns; insn; insn = next)
2855 rtx set = single_set (insn);
2856 rtx note;
2858 /* Some ports (cris) create a libcall regions at their own. We must
2859 avoid any potential nesting of LIBCALLs. */
2860 if ((note = find_reg_note (insn, REG_LIBCALL, NULL)) != NULL)
2861 remove_note (insn, note);
2862 if ((note = find_reg_note (insn, REG_RETVAL, NULL)) != NULL)
2863 remove_note (insn, note);
2865 next = NEXT_INSN (insn);
2867 if (set != 0 && REG_P (SET_DEST (set))
2868 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
2869 && (insn == insns
2870 || ((! INSN_P(insns)
2871 || ! reg_mentioned_p (SET_DEST (set), PATTERN (insns)))
2872 && ! reg_used_between_p (SET_DEST (set), insns, insn)
2873 && ! modified_in_p (SET_SRC (set), insns)
2874 && ! modified_between_p (SET_SRC (set), insns, insn))))
2876 if (PREV_INSN (insn))
2877 NEXT_INSN (PREV_INSN (insn)) = next;
2878 else
2879 insns = next;
2881 if (next)
2882 PREV_INSN (next) = PREV_INSN (insn);
2884 add_insn (insn);
2887 /* Some ports use a loop to copy large arguments onto the stack.
2888 Don't move anything outside such a loop. */
2889 if (LABEL_P (insn))
2890 break;
2893 prev = get_last_insn ();
2895 /* Write the remaining insns followed by the final copy. */
2897 for (insn = insns; insn; insn = next)
2899 next = NEXT_INSN (insn);
2901 add_insn (insn);
2904 last = emit_move_insn (target, result);
2905 if (mov_optab->handlers[(int) GET_MODE (target)].insn_code
2906 != CODE_FOR_nothing)
2907 set_unique_reg_note (last, REG_EQUAL, copy_rtx (equiv));
2908 else
2910 /* Remove any existing REG_EQUAL note from "last", or else it will
2911 be mistaken for a note referring to the full contents of the
2912 libcall value when found together with the REG_RETVAL note added
2913 below. An existing note can come from an insn expansion at
2914 "last". */
2915 remove_note (last, find_reg_note (last, REG_EQUAL, NULL_RTX));
2918 if (final_dest != target)
2919 emit_move_insn (final_dest, target);
2921 if (prev == 0)
2922 first = get_insns ();
2923 else
2924 first = NEXT_INSN (prev);
2926 /* Encapsulate the block so it gets manipulated as a unit. */
2927 if (!flag_non_call_exceptions || !may_trap_p (equiv))
2929 /* We can't attach the REG_LIBCALL and REG_RETVAL notes
2930 when the encapsulated region would not be in one basic block,
2931 i.e. when there is a control_flow_insn_p insn between FIRST and LAST.
2933 bool attach_libcall_retval_notes = true;
2934 next = NEXT_INSN (last);
2935 for (insn = first; insn != next; insn = NEXT_INSN (insn))
2936 if (control_flow_insn_p (insn))
2938 attach_libcall_retval_notes = false;
2939 break;
2942 if (attach_libcall_retval_notes)
2944 REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
2945 REG_NOTES (first));
2946 REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first,
2947 REG_NOTES (last));
2952 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
2953 PURPOSE describes how this comparison will be used. CODE is the rtx
2954 comparison code we will be using.
2956 ??? Actually, CODE is slightly weaker than that. A target is still
2957 required to implement all of the normal bcc operations, but not
2958 required to implement all (or any) of the unordered bcc operations. */
2961 can_compare_p (enum rtx_code code, enum machine_mode mode,
2962 enum can_compare_purpose purpose)
2966 if (cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2968 if (purpose == ccp_jump)
2969 return bcc_gen_fctn[(int) code] != NULL;
2970 else if (purpose == ccp_store_flag)
2971 return setcc_gen_code[(int) code] != CODE_FOR_nothing;
2972 else
2973 /* There's only one cmov entry point, and it's allowed to fail. */
2974 return 1;
2976 if (purpose == ccp_jump
2977 && cbranch_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2978 return 1;
2979 if (purpose == ccp_cmov
2980 && cmov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2981 return 1;
2982 if (purpose == ccp_store_flag
2983 && cstore_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2984 return 1;
2986 mode = GET_MODE_WIDER_MODE (mode);
2988 while (mode != VOIDmode);
2990 return 0;
2993 /* This function is called when we are going to emit a compare instruction that
2994 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
2996 *PMODE is the mode of the inputs (in case they are const_int).
2997 *PUNSIGNEDP nonzero says that the operands are unsigned;
2998 this matters if they need to be widened.
3000 If they have mode BLKmode, then SIZE specifies the size of both operands.
3002 This function performs all the setup necessary so that the caller only has
3003 to emit a single comparison insn. This setup can involve doing a BLKmode
3004 comparison or emitting a library call to perform the comparison if no insn
3005 is available to handle it.
3006 The values which are passed in through pointers can be modified; the caller
3007 should perform the comparison on the modified values. */
3009 static void
3010 prepare_cmp_insn (rtx *px, rtx *py, enum rtx_code *pcomparison, rtx size,
3011 enum machine_mode *pmode, int *punsignedp,
3012 enum can_compare_purpose purpose)
3014 enum machine_mode mode = *pmode;
3015 rtx x = *px, y = *py;
3016 int unsignedp = *punsignedp;
3017 enum mode_class class;
3019 class = GET_MODE_CLASS (mode);
3021 /* They could both be VOIDmode if both args are immediate constants,
3022 but we should fold that at an earlier stage.
3023 With no special code here, this will call abort,
3024 reminding the programmer to implement such folding. */
3026 if (mode != BLKmode && flag_force_mem)
3028 /* Load duplicate non-volatile operands once. */
3029 if (rtx_equal_p (x, y) && ! volatile_refs_p (x))
3031 x = force_not_mem (x);
3032 y = x;
3034 else
3036 x = force_not_mem (x);
3037 y = force_not_mem (y);
3041 /* If we are inside an appropriately-short loop and we are optimizing,
3042 force expensive constants into a register. */
3043 if (CONSTANT_P (x) && optimize
3044 && rtx_cost (x, COMPARE) > COSTS_N_INSNS (1))
3045 x = force_reg (mode, x);
3047 if (CONSTANT_P (y) && optimize
3048 && rtx_cost (y, COMPARE) > COSTS_N_INSNS (1))
3049 y = force_reg (mode, y);
3051 #ifdef HAVE_cc0
3052 /* Abort if we have a non-canonical comparison. The RTL documentation
3053 states that canonical comparisons are required only for targets which
3054 have cc0. */
3055 if (CONSTANT_P (x) && ! CONSTANT_P (y))
3056 abort ();
3057 #endif
3059 /* Don't let both operands fail to indicate the mode. */
3060 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
3061 x = force_reg (mode, x);
3063 /* Handle all BLKmode compares. */
3065 if (mode == BLKmode)
3067 enum machine_mode cmp_mode, result_mode;
3068 enum insn_code cmp_code;
3069 tree length_type;
3070 rtx libfunc;
3071 rtx result;
3072 rtx opalign
3073 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
3075 if (size == 0)
3076 abort ();
3078 /* Try to use a memory block compare insn - either cmpstr
3079 or cmpmem will do. */
3080 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3081 cmp_mode != VOIDmode;
3082 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
3084 cmp_code = cmpmem_optab[cmp_mode];
3085 if (cmp_code == CODE_FOR_nothing)
3086 cmp_code = cmpstr_optab[cmp_mode];
3087 if (cmp_code == CODE_FOR_nothing)
3088 continue;
3090 /* Must make sure the size fits the insn's mode. */
3091 if ((GET_CODE (size) == CONST_INT
3092 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
3093 || (GET_MODE_BITSIZE (GET_MODE (size))
3094 > GET_MODE_BITSIZE (cmp_mode)))
3095 continue;
3097 result_mode = insn_data[cmp_code].operand[0].mode;
3098 result = gen_reg_rtx (result_mode);
3099 size = convert_to_mode (cmp_mode, size, 1);
3100 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
3102 *px = result;
3103 *py = const0_rtx;
3104 *pmode = result_mode;
3105 return;
3108 /* Otherwise call a library function, memcmp. */
3109 libfunc = memcmp_libfunc;
3110 length_type = sizetype;
3111 result_mode = TYPE_MODE (integer_type_node);
3112 cmp_mode = TYPE_MODE (length_type);
3113 size = convert_to_mode (TYPE_MODE (length_type), size,
3114 TYPE_UNSIGNED (length_type));
3116 result = emit_library_call_value (libfunc, 0, LCT_PURE_MAKE_BLOCK,
3117 result_mode, 3,
3118 XEXP (x, 0), Pmode,
3119 XEXP (y, 0), Pmode,
3120 size, cmp_mode);
3121 *px = result;
3122 *py = const0_rtx;
3123 *pmode = result_mode;
3124 return;
3127 /* Don't allow operands to the compare to trap, as that can put the
3128 compare and branch in different basic blocks. */
3129 if (flag_non_call_exceptions)
3131 if (may_trap_p (x))
3132 x = force_reg (mode, x);
3133 if (may_trap_p (y))
3134 y = force_reg (mode, y);
3137 *px = x;
3138 *py = y;
3139 if (can_compare_p (*pcomparison, mode, purpose))
3140 return;
3142 /* Handle a lib call just for the mode we are using. */
3144 if (cmp_optab->handlers[(int) mode].libfunc && class != MODE_FLOAT)
3146 rtx libfunc = cmp_optab->handlers[(int) mode].libfunc;
3147 rtx result;
3149 /* If we want unsigned, and this mode has a distinct unsigned
3150 comparison routine, use that. */
3151 if (unsignedp && ucmp_optab->handlers[(int) mode].libfunc)
3152 libfunc = ucmp_optab->handlers[(int) mode].libfunc;
3154 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST_MAKE_BLOCK,
3155 word_mode, 2, x, mode, y, mode);
3157 *px = result;
3158 *pmode = word_mode;
3159 if (TARGET_LIB_INT_CMP_BIASED)
3160 /* Integer comparison returns a result that must be compared
3161 against 1, so that even if we do an unsigned compare
3162 afterward, there is still a value that can represent the
3163 result "less than". */
3164 *py = const1_rtx;
3165 else
3167 *py = const0_rtx;
3168 *punsignedp = 1;
3170 return;
3173 if (class == MODE_FLOAT)
3174 prepare_float_lib_cmp (px, py, pcomparison, pmode, punsignedp);
3176 else
3177 abort ();
3180 /* Before emitting an insn with code ICODE, make sure that X, which is going
3181 to be used for operand OPNUM of the insn, is converted from mode MODE to
3182 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
3183 that it is accepted by the operand predicate. Return the new value. */
3186 prepare_operand (int icode, rtx x, int opnum, enum machine_mode mode,
3187 enum machine_mode wider_mode, int unsignedp)
3189 if (mode != wider_mode)
3190 x = convert_modes (wider_mode, mode, x, unsignedp);
3192 if (! (*insn_data[icode].operand[opnum].predicate)
3193 (x, insn_data[icode].operand[opnum].mode))
3195 if (no_new_pseudos)
3196 return NULL_RTX;
3197 x = copy_to_mode_reg (insn_data[icode].operand[opnum].mode, x);
3200 return x;
3203 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
3204 we can do the comparison.
3205 The arguments are the same as for emit_cmp_and_jump_insns; but LABEL may
3206 be NULL_RTX which indicates that only a comparison is to be generated. */
3208 static void
3209 emit_cmp_and_jump_insn_1 (rtx x, rtx y, enum machine_mode mode,
3210 enum rtx_code comparison, int unsignedp, rtx label)
3212 rtx test = gen_rtx_fmt_ee (comparison, mode, x, y);
3213 enum mode_class class = GET_MODE_CLASS (mode);
3214 enum machine_mode wider_mode = mode;
3216 /* Try combined insns first. */
3219 enum insn_code icode;
3220 PUT_MODE (test, wider_mode);
3222 if (label)
3224 icode = cbranch_optab->handlers[(int) wider_mode].insn_code;
3226 if (icode != CODE_FOR_nothing
3227 && (*insn_data[icode].operand[0].predicate) (test, wider_mode))
3229 x = prepare_operand (icode, x, 1, mode, wider_mode, unsignedp);
3230 y = prepare_operand (icode, y, 2, mode, wider_mode, unsignedp);
3231 emit_jump_insn (GEN_FCN (icode) (test, x, y, label));
3232 return;
3236 /* Handle some compares against zero. */
3237 icode = (int) tst_optab->handlers[(int) wider_mode].insn_code;
3238 if (y == CONST0_RTX (mode) && icode != CODE_FOR_nothing)
3240 x = prepare_operand (icode, x, 0, mode, wider_mode, unsignedp);
3241 emit_insn (GEN_FCN (icode) (x));
3242 if (label)
3243 emit_jump_insn ((*bcc_gen_fctn[(int) comparison]) (label));
3244 return;
3247 /* Handle compares for which there is a directly suitable insn. */
3249 icode = (int) cmp_optab->handlers[(int) wider_mode].insn_code;
3250 if (icode != CODE_FOR_nothing)
3252 x = prepare_operand (icode, x, 0, mode, wider_mode, unsignedp);
3253 y = prepare_operand (icode, y, 1, mode, wider_mode, unsignedp);
3254 emit_insn (GEN_FCN (icode) (x, y));
3255 if (label)
3256 emit_jump_insn ((*bcc_gen_fctn[(int) comparison]) (label));
3257 return;
3260 if (class != MODE_INT && class != MODE_FLOAT
3261 && class != MODE_COMPLEX_FLOAT)
3262 break;
3264 wider_mode = GET_MODE_WIDER_MODE (wider_mode);
3266 while (wider_mode != VOIDmode);
3268 abort ();
3271 /* Generate code to compare X with Y so that the condition codes are
3272 set and to jump to LABEL if the condition is true. If X is a
3273 constant and Y is not a constant, then the comparison is swapped to
3274 ensure that the comparison RTL has the canonical form.
3276 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
3277 need to be widened by emit_cmp_insn. UNSIGNEDP is also used to select
3278 the proper branch condition code.
3280 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
3282 MODE is the mode of the inputs (in case they are const_int).
3284 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). It will
3285 be passed unchanged to emit_cmp_insn, then potentially converted into an
3286 unsigned variant based on UNSIGNEDP to select a proper jump instruction. */
3288 void
3289 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
3290 enum machine_mode mode, int unsignedp, rtx label)
3292 rtx op0 = x, op1 = y;
3294 /* Swap operands and condition to ensure canonical RTL. */
3295 if (swap_commutative_operands_p (x, y))
3297 /* If we're not emitting a branch, this means some caller
3298 is out of sync. */
3299 if (! label)
3300 abort ();
3302 op0 = y, op1 = x;
3303 comparison = swap_condition (comparison);
3306 #ifdef HAVE_cc0
3307 /* If OP0 is still a constant, then both X and Y must be constants. Force
3308 X into a register to avoid aborting in emit_cmp_insn due to non-canonical
3309 RTL. */
3310 if (CONSTANT_P (op0))
3311 op0 = force_reg (mode, op0);
3312 #endif
3314 if (unsignedp)
3315 comparison = unsigned_condition (comparison);
3317 prepare_cmp_insn (&op0, &op1, &comparison, size, &mode, &unsignedp,
3318 ccp_jump);
3319 emit_cmp_and_jump_insn_1 (op0, op1, mode, comparison, unsignedp, label);
3322 /* Like emit_cmp_and_jump_insns, but generate only the comparison. */
3324 void
3325 emit_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
3326 enum machine_mode mode, int unsignedp)
3328 emit_cmp_and_jump_insns (x, y, comparison, size, mode, unsignedp, 0);
3331 /* Emit a library call comparison between floating point X and Y.
3332 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
3334 static void
3335 prepare_float_lib_cmp (rtx *px, rtx *py, enum rtx_code *pcomparison,
3336 enum machine_mode *pmode, int *punsignedp)
3338 enum rtx_code comparison = *pcomparison;
3339 enum rtx_code swapped = swap_condition (comparison);
3340 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
3341 rtx x = *px;
3342 rtx y = *py;
3343 enum machine_mode orig_mode = GET_MODE (x);
3344 enum machine_mode mode;
3345 rtx value, target, insns, equiv;
3346 rtx libfunc = 0;
3347 bool reversed_p = false;
3349 for (mode = orig_mode; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3351 if ((libfunc = code_to_optab[comparison]->handlers[mode].libfunc))
3352 break;
3354 if ((libfunc = code_to_optab[swapped]->handlers[mode].libfunc))
3356 rtx tmp;
3357 tmp = x; x = y; y = tmp;
3358 comparison = swapped;
3359 break;
3362 if ((libfunc = code_to_optab[reversed]->handlers[mode].libfunc)
3363 && FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, reversed))
3365 comparison = reversed;
3366 reversed_p = true;
3367 break;
3371 if (mode == VOIDmode)
3372 abort ();
3374 if (mode != orig_mode)
3376 x = convert_to_mode (mode, x, 0);
3377 y = convert_to_mode (mode, y, 0);
3380 /* Attach a REG_EQUAL note describing the semantics of the libcall to
3381 the RTL. The allows the RTL optimizers to delete the libcall if the
3382 condition can be determined at compile-time. */
3383 if (comparison == UNORDERED)
3385 rtx temp = simplify_gen_relational (NE, word_mode, mode, x, x);
3386 equiv = simplify_gen_relational (NE, word_mode, mode, y, y);
3387 equiv = simplify_gen_ternary (IF_THEN_ELSE, word_mode, word_mode,
3388 temp, const_true_rtx, equiv);
3390 else
3392 equiv = simplify_gen_relational (comparison, word_mode, mode, x, y);
3393 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
3395 rtx true_rtx, false_rtx;
3397 switch (comparison)
3399 case EQ:
3400 true_rtx = const0_rtx;
3401 false_rtx = const_true_rtx;
3402 break;
3404 case NE:
3405 true_rtx = const_true_rtx;
3406 false_rtx = const0_rtx;
3407 break;
3409 case GT:
3410 true_rtx = const1_rtx;
3411 false_rtx = const0_rtx;
3412 break;
3414 case GE:
3415 true_rtx = const0_rtx;
3416 false_rtx = constm1_rtx;
3417 break;
3419 case LT:
3420 true_rtx = constm1_rtx;
3421 false_rtx = const0_rtx;
3422 break;
3424 case LE:
3425 true_rtx = const0_rtx;
3426 false_rtx = const1_rtx;
3427 break;
3429 default:
3430 abort ();
3432 equiv = simplify_gen_ternary (IF_THEN_ELSE, word_mode, word_mode,
3433 equiv, true_rtx, false_rtx);
3437 start_sequence ();
3438 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
3439 word_mode, 2, x, mode, y, mode);
3440 insns = get_insns ();
3441 end_sequence ();
3443 target = gen_reg_rtx (word_mode);
3444 emit_libcall_block (insns, target, value, equiv);
3446 if (comparison == UNORDERED
3447 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
3448 comparison = reversed_p ? EQ : NE;
3450 *px = target;
3451 *py = const0_rtx;
3452 *pmode = word_mode;
3453 *pcomparison = comparison;
3454 *punsignedp = 0;
3457 /* Generate code to indirectly jump to a location given in the rtx LOC. */
3459 void
3460 emit_indirect_jump (rtx loc)
3462 if (! ((*insn_data[(int) CODE_FOR_indirect_jump].operand[0].predicate)
3463 (loc, Pmode)))
3464 loc = copy_to_mode_reg (Pmode, loc);
3466 emit_jump_insn (gen_indirect_jump (loc));
3467 emit_barrier ();
3470 #ifdef HAVE_conditional_move
3472 /* Emit a conditional move instruction if the machine supports one for that
3473 condition and machine mode.
3475 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
3476 the mode to use should they be constants. If it is VOIDmode, they cannot
3477 both be constants.
3479 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
3480 should be stored there. MODE is the mode to use should they be constants.
3481 If it is VOIDmode, they cannot both be constants.
3483 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
3484 is not supported. */
3487 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
3488 enum machine_mode cmode, rtx op2, rtx op3,
3489 enum machine_mode mode, int unsignedp)
3491 rtx tem, subtarget, comparison, insn;
3492 enum insn_code icode;
3493 enum rtx_code reversed;
3495 /* If one operand is constant, make it the second one. Only do this
3496 if the other operand is not constant as well. */
3498 if (swap_commutative_operands_p (op0, op1))
3500 tem = op0;
3501 op0 = op1;
3502 op1 = tem;
3503 code = swap_condition (code);
3506 /* get_condition will prefer to generate LT and GT even if the old
3507 comparison was against zero, so undo that canonicalization here since
3508 comparisons against zero are cheaper. */
3509 if (code == LT && op1 == const1_rtx)
3510 code = LE, op1 = const0_rtx;
3511 else if (code == GT && op1 == constm1_rtx)
3512 code = GE, op1 = const0_rtx;
3514 if (cmode == VOIDmode)
3515 cmode = GET_MODE (op0);
3517 if (swap_commutative_operands_p (op2, op3)
3518 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
3519 != UNKNOWN))
3521 tem = op2;
3522 op2 = op3;
3523 op3 = tem;
3524 code = reversed;
3527 if (mode == VOIDmode)
3528 mode = GET_MODE (op2);
3530 icode = movcc_gen_code[mode];
3532 if (icode == CODE_FOR_nothing)
3533 return 0;
3535 if (flag_force_mem)
3537 op2 = force_not_mem (op2);
3538 op3 = force_not_mem (op3);
3541 if (!target)
3542 target = gen_reg_rtx (mode);
3544 subtarget = target;
3546 /* If the insn doesn't accept these operands, put them in pseudos. */
3548 if (! (*insn_data[icode].operand[0].predicate)
3549 (subtarget, insn_data[icode].operand[0].mode))
3550 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
3552 if (! (*insn_data[icode].operand[2].predicate)
3553 (op2, insn_data[icode].operand[2].mode))
3554 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
3556 if (! (*insn_data[icode].operand[3].predicate)
3557 (op3, insn_data[icode].operand[3].mode))
3558 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
3560 /* Everything should now be in the suitable form, so emit the compare insn
3561 and then the conditional move. */
3563 comparison
3564 = compare_from_rtx (op0, op1, code, unsignedp, cmode, NULL_RTX);
3566 /* ??? Watch for const0_rtx (nop) and const_true_rtx (unconditional)? */
3567 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
3568 return NULL and let the caller figure out how best to deal with this
3569 situation. */
3570 if (GET_CODE (comparison) != code)
3571 return NULL_RTX;
3573 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
3575 /* If that failed, then give up. */
3576 if (insn == 0)
3577 return 0;
3579 emit_insn (insn);
3581 if (subtarget != target)
3582 convert_move (target, subtarget, 0);
3584 return target;
3587 /* Return nonzero if a conditional move of mode MODE is supported.
3589 This function is for combine so it can tell whether an insn that looks
3590 like a conditional move is actually supported by the hardware. If we
3591 guess wrong we lose a bit on optimization, but that's it. */
3592 /* ??? sparc64 supports conditionally moving integers values based on fp
3593 comparisons, and vice versa. How do we handle them? */
3596 can_conditionally_move_p (enum machine_mode mode)
3598 if (movcc_gen_code[mode] != CODE_FOR_nothing)
3599 return 1;
3601 return 0;
3604 #endif /* HAVE_conditional_move */
3606 /* Emit a conditional addition instruction if the machine supports one for that
3607 condition and machine mode.
3609 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
3610 the mode to use should they be constants. If it is VOIDmode, they cannot
3611 both be constants.
3613 OP2 should be stored in TARGET if the comparison is true, otherwise OP2+OP3
3614 should be stored there. MODE is the mode to use should they be constants.
3615 If it is VOIDmode, they cannot both be constants.
3617 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
3618 is not supported. */
3621 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
3622 enum machine_mode cmode, rtx op2, rtx op3,
3623 enum machine_mode mode, int unsignedp)
3625 rtx tem, subtarget, comparison, insn;
3626 enum insn_code icode;
3627 enum rtx_code reversed;
3629 /* If one operand is constant, make it the second one. Only do this
3630 if the other operand is not constant as well. */
3632 if (swap_commutative_operands_p (op0, op1))
3634 tem = op0;
3635 op0 = op1;
3636 op1 = tem;
3637 code = swap_condition (code);
3640 /* get_condition will prefer to generate LT and GT even if the old
3641 comparison was against zero, so undo that canonicalization here since
3642 comparisons against zero are cheaper. */
3643 if (code == LT && op1 == const1_rtx)
3644 code = LE, op1 = const0_rtx;
3645 else if (code == GT && op1 == constm1_rtx)
3646 code = GE, op1 = const0_rtx;
3648 if (cmode == VOIDmode)
3649 cmode = GET_MODE (op0);
3651 if (swap_commutative_operands_p (op2, op3)
3652 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
3653 != UNKNOWN))
3655 tem = op2;
3656 op2 = op3;
3657 op3 = tem;
3658 code = reversed;
3661 if (mode == VOIDmode)
3662 mode = GET_MODE (op2);
3664 icode = addcc_optab->handlers[(int) mode].insn_code;
3666 if (icode == CODE_FOR_nothing)
3667 return 0;
3669 if (flag_force_mem)
3671 op2 = force_not_mem (op2);
3672 op3 = force_not_mem (op3);
3675 if (!target)
3676 target = gen_reg_rtx (mode);
3678 /* If the insn doesn't accept these operands, put them in pseudos. */
3680 if (! (*insn_data[icode].operand[0].predicate)
3681 (target, insn_data[icode].operand[0].mode))
3682 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
3683 else
3684 subtarget = target;
3686 if (! (*insn_data[icode].operand[2].predicate)
3687 (op2, insn_data[icode].operand[2].mode))
3688 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
3690 if (! (*insn_data[icode].operand[3].predicate)
3691 (op3, insn_data[icode].operand[3].mode))
3692 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
3694 /* Everything should now be in the suitable form, so emit the compare insn
3695 and then the conditional move. */
3697 comparison
3698 = compare_from_rtx (op0, op1, code, unsignedp, cmode, NULL_RTX);
3700 /* ??? Watch for const0_rtx (nop) and const_true_rtx (unconditional)? */
3701 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
3702 return NULL and let the caller figure out how best to deal with this
3703 situation. */
3704 if (GET_CODE (comparison) != code)
3705 return NULL_RTX;
3707 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
3709 /* If that failed, then give up. */
3710 if (insn == 0)
3711 return 0;
3713 emit_insn (insn);
3715 if (subtarget != target)
3716 convert_move (target, subtarget, 0);
3718 return target;
3721 /* These functions attempt to generate an insn body, rather than
3722 emitting the insn, but if the gen function already emits them, we
3723 make no attempt to turn them back into naked patterns. */
3725 /* Generate and return an insn body to add Y to X. */
3728 gen_add2_insn (rtx x, rtx y)
3730 int icode = (int) add_optab->handlers[(int) GET_MODE (x)].insn_code;
3732 if (! ((*insn_data[icode].operand[0].predicate)
3733 (x, insn_data[icode].operand[0].mode))
3734 || ! ((*insn_data[icode].operand[1].predicate)
3735 (x, insn_data[icode].operand[1].mode))
3736 || ! ((*insn_data[icode].operand[2].predicate)
3737 (y, insn_data[icode].operand[2].mode)))
3738 abort ();
3740 return (GEN_FCN (icode) (x, x, y));
3743 /* Generate and return an insn body to add r1 and c,
3744 storing the result in r0. */
3746 gen_add3_insn (rtx r0, rtx r1, rtx c)
3748 int icode = (int) add_optab->handlers[(int) GET_MODE (r0)].insn_code;
3750 if (icode == CODE_FOR_nothing
3751 || ! ((*insn_data[icode].operand[0].predicate)
3752 (r0, insn_data[icode].operand[0].mode))
3753 || ! ((*insn_data[icode].operand[1].predicate)
3754 (r1, insn_data[icode].operand[1].mode))
3755 || ! ((*insn_data[icode].operand[2].predicate)
3756 (c, insn_data[icode].operand[2].mode)))
3757 return NULL_RTX;
3759 return (GEN_FCN (icode) (r0, r1, c));
3763 have_add2_insn (rtx x, rtx y)
3765 int icode;
3767 if (GET_MODE (x) == VOIDmode)
3768 abort ();
3770 icode = (int) add_optab->handlers[(int) GET_MODE (x)].insn_code;
3772 if (icode == CODE_FOR_nothing)
3773 return 0;
3775 if (! ((*insn_data[icode].operand[0].predicate)
3776 (x, insn_data[icode].operand[0].mode))
3777 || ! ((*insn_data[icode].operand[1].predicate)
3778 (x, insn_data[icode].operand[1].mode))
3779 || ! ((*insn_data[icode].operand[2].predicate)
3780 (y, insn_data[icode].operand[2].mode)))
3781 return 0;
3783 return 1;
3786 /* Generate and return an insn body to subtract Y from X. */
3789 gen_sub2_insn (rtx x, rtx y)
3791 int icode = (int) sub_optab->handlers[(int) GET_MODE (x)].insn_code;
3793 if (! ((*insn_data[icode].operand[0].predicate)
3794 (x, insn_data[icode].operand[0].mode))
3795 || ! ((*insn_data[icode].operand[1].predicate)
3796 (x, insn_data[icode].operand[1].mode))
3797 || ! ((*insn_data[icode].operand[2].predicate)
3798 (y, insn_data[icode].operand[2].mode)))
3799 abort ();
3801 return (GEN_FCN (icode) (x, x, y));
3804 /* Generate and return an insn body to subtract r1 and c,
3805 storing the result in r0. */
3807 gen_sub3_insn (rtx r0, rtx r1, rtx c)
3809 int icode = (int) sub_optab->handlers[(int) GET_MODE (r0)].insn_code;
3811 if (icode == CODE_FOR_nothing
3812 || ! ((*insn_data[icode].operand[0].predicate)
3813 (r0, insn_data[icode].operand[0].mode))
3814 || ! ((*insn_data[icode].operand[1].predicate)
3815 (r1, insn_data[icode].operand[1].mode))
3816 || ! ((*insn_data[icode].operand[2].predicate)
3817 (c, insn_data[icode].operand[2].mode)))
3818 return NULL_RTX;
3820 return (GEN_FCN (icode) (r0, r1, c));
3824 have_sub2_insn (rtx x, rtx y)
3826 int icode;
3828 if (GET_MODE (x) == VOIDmode)
3829 abort ();
3831 icode = (int) sub_optab->handlers[(int) GET_MODE (x)].insn_code;
3833 if (icode == CODE_FOR_nothing)
3834 return 0;
3836 if (! ((*insn_data[icode].operand[0].predicate)
3837 (x, insn_data[icode].operand[0].mode))
3838 || ! ((*insn_data[icode].operand[1].predicate)
3839 (x, insn_data[icode].operand[1].mode))
3840 || ! ((*insn_data[icode].operand[2].predicate)
3841 (y, insn_data[icode].operand[2].mode)))
3842 return 0;
3844 return 1;
3847 /* Generate the body of an instruction to copy Y into X.
3848 It may be a list of insns, if one insn isn't enough. */
3851 gen_move_insn (rtx x, rtx y)
3853 rtx seq;
3855 start_sequence ();
3856 emit_move_insn_1 (x, y);
3857 seq = get_insns ();
3858 end_sequence ();
3859 return seq;
3862 /* Return the insn code used to extend FROM_MODE to TO_MODE.
3863 UNSIGNEDP specifies zero-extension instead of sign-extension. If
3864 no such operation exists, CODE_FOR_nothing will be returned. */
3866 enum insn_code
3867 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
3868 int unsignedp)
3870 convert_optab tab;
3871 #ifdef HAVE_ptr_extend
3872 if (unsignedp < 0)
3873 return CODE_FOR_ptr_extend;
3874 #endif
3876 tab = unsignedp ? zext_optab : sext_optab;
3877 return tab->handlers[to_mode][from_mode].insn_code;
3880 /* Generate the body of an insn to extend Y (with mode MFROM)
3881 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
3884 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
3885 enum machine_mode mfrom, int unsignedp)
3887 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
3888 return GEN_FCN (icode) (x, y);
3891 /* can_fix_p and can_float_p say whether the target machine
3892 can directly convert a given fixed point type to
3893 a given floating point type, or vice versa.
3894 The returned value is the CODE_FOR_... value to use,
3895 or CODE_FOR_nothing if these modes cannot be directly converted.
3897 *TRUNCP_PTR is set to 1 if it is necessary to output
3898 an explicit FTRUNC insn before the fix insn; otherwise 0. */
3900 static enum insn_code
3901 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
3902 int unsignedp, int *truncp_ptr)
3904 convert_optab tab;
3905 enum insn_code icode;
3907 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
3908 icode = tab->handlers[fixmode][fltmode].insn_code;
3909 if (icode != CODE_FOR_nothing)
3911 *truncp_ptr = 0;
3912 return icode;
3915 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
3916 for this to work. We need to rework the fix* and ftrunc* patterns
3917 and documentation. */
3918 tab = unsignedp ? ufix_optab : sfix_optab;
3919 icode = tab->handlers[fixmode][fltmode].insn_code;
3920 if (icode != CODE_FOR_nothing
3921 && ftrunc_optab->handlers[fltmode].insn_code != CODE_FOR_nothing)
3923 *truncp_ptr = 1;
3924 return icode;
3927 *truncp_ptr = 0;
3928 return CODE_FOR_nothing;
3931 static enum insn_code
3932 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
3933 int unsignedp)
3935 convert_optab tab;
3937 tab = unsignedp ? ufloat_optab : sfloat_optab;
3938 return tab->handlers[fltmode][fixmode].insn_code;
3941 /* Generate code to convert FROM to floating point
3942 and store in TO. FROM must be fixed point and not VOIDmode.
3943 UNSIGNEDP nonzero means regard FROM as unsigned.
3944 Normally this is done by correcting the final value
3945 if it is negative. */
3947 void
3948 expand_float (rtx to, rtx from, int unsignedp)
3950 enum insn_code icode;
3951 rtx target = to;
3952 enum machine_mode fmode, imode;
3954 /* Crash now, because we won't be able to decide which mode to use. */
3955 if (GET_MODE (from) == VOIDmode)
3956 abort ();
3958 /* Look for an insn to do the conversion. Do it in the specified
3959 modes if possible; otherwise convert either input, output or both to
3960 wider mode. If the integer mode is wider than the mode of FROM,
3961 we can do the conversion signed even if the input is unsigned. */
3963 for (fmode = GET_MODE (to); fmode != VOIDmode;
3964 fmode = GET_MODE_WIDER_MODE (fmode))
3965 for (imode = GET_MODE (from); imode != VOIDmode;
3966 imode = GET_MODE_WIDER_MODE (imode))
3968 int doing_unsigned = unsignedp;
3970 if (fmode != GET_MODE (to)
3971 && significand_size (fmode) < GET_MODE_BITSIZE (GET_MODE (from)))
3972 continue;
3974 icode = can_float_p (fmode, imode, unsignedp);
3975 if (icode == CODE_FOR_nothing && imode != GET_MODE (from) && unsignedp)
3976 icode = can_float_p (fmode, imode, 0), doing_unsigned = 0;
3978 if (icode != CODE_FOR_nothing)
3980 if (imode != GET_MODE (from))
3981 from = convert_to_mode (imode, from, unsignedp);
3983 if (fmode != GET_MODE (to))
3984 target = gen_reg_rtx (fmode);
3986 emit_unop_insn (icode, target, from,
3987 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
3989 if (target != to)
3990 convert_move (to, target, 0);
3991 return;
3995 /* Unsigned integer, and no way to convert directly.
3996 Convert as signed, then conditionally adjust the result. */
3997 if (unsignedp)
3999 rtx label = gen_label_rtx ();
4000 rtx temp;
4001 REAL_VALUE_TYPE offset;
4003 if (flag_force_mem)
4004 from = force_not_mem (from);
4006 /* Look for a usable floating mode FMODE wider than the source and at
4007 least as wide as the target. Using FMODE will avoid rounding woes
4008 with unsigned values greater than the signed maximum value. */
4010 for (fmode = GET_MODE (to); fmode != VOIDmode;
4011 fmode = GET_MODE_WIDER_MODE (fmode))
4012 if (GET_MODE_BITSIZE (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
4013 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
4014 break;
4016 if (fmode == VOIDmode)
4018 /* There is no such mode. Pretend the target is wide enough. */
4019 fmode = GET_MODE (to);
4021 /* Avoid double-rounding when TO is narrower than FROM. */
4022 if ((significand_size (fmode) + 1)
4023 < GET_MODE_BITSIZE (GET_MODE (from)))
4025 rtx temp1;
4026 rtx neglabel = gen_label_rtx ();
4028 /* Don't use TARGET if it isn't a register, is a hard register,
4029 or is the wrong mode. */
4030 if (!REG_P (target)
4031 || REGNO (target) < FIRST_PSEUDO_REGISTER
4032 || GET_MODE (target) != fmode)
4033 target = gen_reg_rtx (fmode);
4035 imode = GET_MODE (from);
4036 do_pending_stack_adjust ();
4038 /* Test whether the sign bit is set. */
4039 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
4040 0, neglabel);
4042 /* The sign bit is not set. Convert as signed. */
4043 expand_float (target, from, 0);
4044 emit_jump_insn (gen_jump (label));
4045 emit_barrier ();
4047 /* The sign bit is set.
4048 Convert to a usable (positive signed) value by shifting right
4049 one bit, while remembering if a nonzero bit was shifted
4050 out; i.e., compute (from & 1) | (from >> 1). */
4052 emit_label (neglabel);
4053 temp = expand_binop (imode, and_optab, from, const1_rtx,
4054 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4055 temp1 = expand_shift (RSHIFT_EXPR, imode, from, integer_one_node,
4056 NULL_RTX, 1);
4057 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
4058 OPTAB_LIB_WIDEN);
4059 expand_float (target, temp, 0);
4061 /* Multiply by 2 to undo the shift above. */
4062 temp = expand_binop (fmode, add_optab, target, target,
4063 target, 0, OPTAB_LIB_WIDEN);
4064 if (temp != target)
4065 emit_move_insn (target, temp);
4067 do_pending_stack_adjust ();
4068 emit_label (label);
4069 goto done;
4073 /* If we are about to do some arithmetic to correct for an
4074 unsigned operand, do it in a pseudo-register. */
4076 if (GET_MODE (to) != fmode
4077 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
4078 target = gen_reg_rtx (fmode);
4080 /* Convert as signed integer to floating. */
4081 expand_float (target, from, 0);
4083 /* If FROM is negative (and therefore TO is negative),
4084 correct its value by 2**bitwidth. */
4086 do_pending_stack_adjust ();
4087 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
4088 0, label);
4091 real_2expN (&offset, GET_MODE_BITSIZE (GET_MODE (from)));
4092 temp = expand_binop (fmode, add_optab, target,
4093 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
4094 target, 0, OPTAB_LIB_WIDEN);
4095 if (temp != target)
4096 emit_move_insn (target, temp);
4098 do_pending_stack_adjust ();
4099 emit_label (label);
4100 goto done;
4103 /* No hardware instruction available; call a library routine. */
4105 rtx libfunc;
4106 rtx insns;
4107 rtx value;
4108 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
4110 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
4111 from = convert_to_mode (SImode, from, unsignedp);
4113 if (flag_force_mem)
4114 from = force_not_mem (from);
4116 libfunc = tab->handlers[GET_MODE (to)][GET_MODE (from)].libfunc;
4117 if (!libfunc)
4118 abort ();
4120 start_sequence ();
4122 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4123 GET_MODE (to), 1, from,
4124 GET_MODE (from));
4125 insns = get_insns ();
4126 end_sequence ();
4128 emit_libcall_block (insns, target, value,
4129 gen_rtx_FLOAT (GET_MODE (to), from));
4132 done:
4134 /* Copy result to requested destination
4135 if we have been computing in a temp location. */
4137 if (target != to)
4139 if (GET_MODE (target) == GET_MODE (to))
4140 emit_move_insn (to, target);
4141 else
4142 convert_move (to, target, 0);
4146 /* Generate code to convert FROM to fixed point and store in TO. FROM
4147 must be floating point. */
4149 void
4150 expand_fix (rtx to, rtx from, int unsignedp)
4152 enum insn_code icode;
4153 rtx target = to;
4154 enum machine_mode fmode, imode;
4155 int must_trunc = 0;
4157 /* We first try to find a pair of modes, one real and one integer, at
4158 least as wide as FROM and TO, respectively, in which we can open-code
4159 this conversion. If the integer mode is wider than the mode of TO,
4160 we can do the conversion either signed or unsigned. */
4162 for (fmode = GET_MODE (from); fmode != VOIDmode;
4163 fmode = GET_MODE_WIDER_MODE (fmode))
4164 for (imode = GET_MODE (to); imode != VOIDmode;
4165 imode = GET_MODE_WIDER_MODE (imode))
4167 int doing_unsigned = unsignedp;
4169 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
4170 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
4171 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
4173 if (icode != CODE_FOR_nothing)
4175 if (fmode != GET_MODE (from))
4176 from = convert_to_mode (fmode, from, 0);
4178 if (must_trunc)
4180 rtx temp = gen_reg_rtx (GET_MODE (from));
4181 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
4182 temp, 0);
4185 if (imode != GET_MODE (to))
4186 target = gen_reg_rtx (imode);
4188 emit_unop_insn (icode, target, from,
4189 doing_unsigned ? UNSIGNED_FIX : FIX);
4190 if (target != to)
4191 convert_move (to, target, unsignedp);
4192 return;
4196 /* For an unsigned conversion, there is one more way to do it.
4197 If we have a signed conversion, we generate code that compares
4198 the real value to the largest representable positive number. If if
4199 is smaller, the conversion is done normally. Otherwise, subtract
4200 one plus the highest signed number, convert, and add it back.
4202 We only need to check all real modes, since we know we didn't find
4203 anything with a wider integer mode.
4205 This code used to extend FP value into mode wider than the destination.
4206 This is not needed. Consider, for instance conversion from SFmode
4207 into DImode.
4209 The hot path trought the code is dealing with inputs smaller than 2^63
4210 and doing just the conversion, so there is no bits to lose.
4212 In the other path we know the value is positive in the range 2^63..2^64-1
4213 inclusive. (as for other imput overflow happens and result is undefined)
4214 So we know that the most important bit set in mantissa corresponds to
4215 2^63. The subtraction of 2^63 should not generate any rounding as it
4216 simply clears out that bit. The rest is trivial. */
4218 if (unsignedp && GET_MODE_BITSIZE (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
4219 for (fmode = GET_MODE (from); fmode != VOIDmode;
4220 fmode = GET_MODE_WIDER_MODE (fmode))
4221 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0,
4222 &must_trunc))
4224 int bitsize;
4225 REAL_VALUE_TYPE offset;
4226 rtx limit, lab1, lab2, insn;
4228 bitsize = GET_MODE_BITSIZE (GET_MODE (to));
4229 real_2expN (&offset, bitsize - 1);
4230 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
4231 lab1 = gen_label_rtx ();
4232 lab2 = gen_label_rtx ();
4234 if (flag_force_mem)
4235 from = force_not_mem (from);
4237 if (fmode != GET_MODE (from))
4238 from = convert_to_mode (fmode, from, 0);
4240 /* See if we need to do the subtraction. */
4241 do_pending_stack_adjust ();
4242 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
4243 0, lab1);
4245 /* If not, do the signed "fix" and branch around fixup code. */
4246 expand_fix (to, from, 0);
4247 emit_jump_insn (gen_jump (lab2));
4248 emit_barrier ();
4250 /* Otherwise, subtract 2**(N-1), convert to signed number,
4251 then add 2**(N-1). Do the addition using XOR since this
4252 will often generate better code. */
4253 emit_label (lab1);
4254 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
4255 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4256 expand_fix (to, target, 0);
4257 target = expand_binop (GET_MODE (to), xor_optab, to,
4258 gen_int_mode
4259 ((HOST_WIDE_INT) 1 << (bitsize - 1),
4260 GET_MODE (to)),
4261 to, 1, OPTAB_LIB_WIDEN);
4263 if (target != to)
4264 emit_move_insn (to, target);
4266 emit_label (lab2);
4268 if (mov_optab->handlers[(int) GET_MODE (to)].insn_code
4269 != CODE_FOR_nothing)
4271 /* Make a place for a REG_NOTE and add it. */
4272 insn = emit_move_insn (to, to);
4273 set_unique_reg_note (insn,
4274 REG_EQUAL,
4275 gen_rtx_fmt_e (UNSIGNED_FIX,
4276 GET_MODE (to),
4277 copy_rtx (from)));
4280 return;
4283 /* We can't do it with an insn, so use a library call. But first ensure
4284 that the mode of TO is at least as wide as SImode, since those are the
4285 only library calls we know about. */
4287 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
4289 target = gen_reg_rtx (SImode);
4291 expand_fix (target, from, unsignedp);
4293 else
4295 rtx insns;
4296 rtx value;
4297 rtx libfunc;
4299 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
4300 libfunc = tab->handlers[GET_MODE (to)][GET_MODE (from)].libfunc;
4301 if (!libfunc)
4302 abort ();
4304 if (flag_force_mem)
4305 from = force_not_mem (from);
4307 start_sequence ();
4309 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4310 GET_MODE (to), 1, from,
4311 GET_MODE (from));
4312 insns = get_insns ();
4313 end_sequence ();
4315 emit_libcall_block (insns, target, value,
4316 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
4317 GET_MODE (to), from));
4320 if (target != to)
4322 if (GET_MODE (to) == GET_MODE (target))
4323 emit_move_insn (to, target);
4324 else
4325 convert_move (to, target, 0);
4329 /* Report whether we have an instruction to perform the operation
4330 specified by CODE on operands of mode MODE. */
4332 have_insn_for (enum rtx_code code, enum machine_mode mode)
4334 return (code_to_optab[(int) code] != 0
4335 && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
4336 != CODE_FOR_nothing));
4339 /* Create a blank optab. */
4340 static optab
4341 new_optab (void)
4343 int i;
4344 optab op = ggc_alloc (sizeof (struct optab));
4345 for (i = 0; i < NUM_MACHINE_MODES; i++)
4347 op->handlers[i].insn_code = CODE_FOR_nothing;
4348 op->handlers[i].libfunc = 0;
4351 return op;
4354 static convert_optab
4355 new_convert_optab (void)
4357 int i, j;
4358 convert_optab op = ggc_alloc (sizeof (struct convert_optab));
4359 for (i = 0; i < NUM_MACHINE_MODES; i++)
4360 for (j = 0; j < NUM_MACHINE_MODES; j++)
4362 op->handlers[i][j].insn_code = CODE_FOR_nothing;
4363 op->handlers[i][j].libfunc = 0;
4365 return op;
4368 /* Same, but fill in its code as CODE, and write it into the
4369 code_to_optab table. */
4370 static inline optab
4371 init_optab (enum rtx_code code)
4373 optab op = new_optab ();
4374 op->code = code;
4375 code_to_optab[(int) code] = op;
4376 return op;
4379 /* Same, but fill in its code as CODE, and do _not_ write it into
4380 the code_to_optab table. */
4381 static inline optab
4382 init_optabv (enum rtx_code code)
4384 optab op = new_optab ();
4385 op->code = code;
4386 return op;
4389 /* Conversion optabs never go in the code_to_optab table. */
4390 static inline convert_optab
4391 init_convert_optab (enum rtx_code code)
4393 convert_optab op = new_convert_optab ();
4394 op->code = code;
4395 return op;
4398 /* Initialize the libfunc fields of an entire group of entries in some
4399 optab. Each entry is set equal to a string consisting of a leading
4400 pair of underscores followed by a generic operation name followed by
4401 a mode name (downshifted to lowercase) followed by a single character
4402 representing the number of operands for the given operation (which is
4403 usually one of the characters '2', '3', or '4').
4405 OPTABLE is the table in which libfunc fields are to be initialized.
4406 FIRST_MODE is the first machine mode index in the given optab to
4407 initialize.
4408 LAST_MODE is the last machine mode index in the given optab to
4409 initialize.
4410 OPNAME is the generic (string) name of the operation.
4411 SUFFIX is the character which specifies the number of operands for
4412 the given generic operation.
4415 static void
4416 init_libfuncs (optab optable, int first_mode, int last_mode,
4417 const char *opname, int suffix)
4419 int mode;
4420 unsigned opname_len = strlen (opname);
4422 for (mode = first_mode; (int) mode <= (int) last_mode;
4423 mode = (enum machine_mode) ((int) mode + 1))
4425 const char *mname = GET_MODE_NAME (mode);
4426 unsigned mname_len = strlen (mname);
4427 char *libfunc_name = alloca (2 + opname_len + mname_len + 1 + 1);
4428 char *p;
4429 const char *q;
4431 p = libfunc_name;
4432 *p++ = '_';
4433 *p++ = '_';
4434 for (q = opname; *q; )
4435 *p++ = *q++;
4436 for (q = mname; *q; q++)
4437 *p++ = TOLOWER (*q);
4438 *p++ = suffix;
4439 *p = '\0';
4441 optable->handlers[(int) mode].libfunc
4442 = init_one_libfunc (ggc_alloc_string (libfunc_name, p - libfunc_name));
4446 /* Initialize the libfunc fields of an entire group of entries in some
4447 optab which correspond to all integer mode operations. The parameters
4448 have the same meaning as similarly named ones for the `init_libfuncs'
4449 routine. (See above). */
4451 static void
4452 init_integral_libfuncs (optab optable, const char *opname, int suffix)
4454 int maxsize = 2*BITS_PER_WORD;
4455 if (maxsize < LONG_LONG_TYPE_SIZE)
4456 maxsize = LONG_LONG_TYPE_SIZE;
4457 init_libfuncs (optable, word_mode,
4458 mode_for_size (maxsize, MODE_INT, 0),
4459 opname, suffix);
4462 /* Initialize the libfunc fields of an entire group of entries in some
4463 optab which correspond to all real mode operations. The parameters
4464 have the same meaning as similarly named ones for the `init_libfuncs'
4465 routine. (See above). */
4467 static void
4468 init_floating_libfuncs (optab optable, const char *opname, int suffix)
4470 init_libfuncs (optable, MIN_MODE_FLOAT, MAX_MODE_FLOAT, opname, suffix);
4473 /* Initialize the libfunc fields of an entire group of entries of an
4474 inter-mode-class conversion optab. The string formation rules are
4475 similar to the ones for init_libfuncs, above, but instead of having
4476 a mode name and an operand count these functions have two mode names
4477 and no operand count. */
4478 static void
4479 init_interclass_conv_libfuncs (convert_optab tab, const char *opname,
4480 enum mode_class from_class,
4481 enum mode_class to_class)
4483 enum machine_mode first_from_mode = GET_CLASS_NARROWEST_MODE (from_class);
4484 enum machine_mode first_to_mode = GET_CLASS_NARROWEST_MODE (to_class);
4485 size_t opname_len = strlen (opname);
4486 size_t max_mname_len = 0;
4488 enum machine_mode fmode, tmode;
4489 const char *fname, *tname;
4490 const char *q;
4491 char *libfunc_name, *suffix;
4492 char *p;
4494 for (fmode = first_from_mode;
4495 fmode != VOIDmode;
4496 fmode = GET_MODE_WIDER_MODE (fmode))
4497 max_mname_len = MAX (max_mname_len, strlen (GET_MODE_NAME (fmode)));
4499 for (tmode = first_to_mode;
4500 tmode != VOIDmode;
4501 tmode = GET_MODE_WIDER_MODE (tmode))
4502 max_mname_len = MAX (max_mname_len, strlen (GET_MODE_NAME (tmode)));
4504 libfunc_name = alloca (2 + opname_len + 2*max_mname_len + 1 + 1);
4505 libfunc_name[0] = '_';
4506 libfunc_name[1] = '_';
4507 memcpy (&libfunc_name[2], opname, opname_len);
4508 suffix = libfunc_name + opname_len + 2;
4510 for (fmode = first_from_mode; fmode != VOIDmode;
4511 fmode = GET_MODE_WIDER_MODE (fmode))
4512 for (tmode = first_to_mode; tmode != VOIDmode;
4513 tmode = GET_MODE_WIDER_MODE (tmode))
4515 fname = GET_MODE_NAME (fmode);
4516 tname = GET_MODE_NAME (tmode);
4518 p = suffix;
4519 for (q = fname; *q; p++, q++)
4520 *p = TOLOWER (*q);
4521 for (q = tname; *q; p++, q++)
4522 *p = TOLOWER (*q);
4524 *p = '\0';
4526 tab->handlers[tmode][fmode].libfunc
4527 = init_one_libfunc (ggc_alloc_string (libfunc_name,
4528 p - libfunc_name));
4532 /* Initialize the libfunc fields of an entire group of entries of an
4533 intra-mode-class conversion optab. The string formation rules are
4534 similar to the ones for init_libfunc, above. WIDENING says whether
4535 the optab goes from narrow to wide modes or vice versa. These functions
4536 have two mode names _and_ an operand count. */
4537 static void
4538 init_intraclass_conv_libfuncs (convert_optab tab, const char *opname,
4539 enum mode_class class, bool widening)
4541 enum machine_mode first_mode = GET_CLASS_NARROWEST_MODE (class);
4542 size_t opname_len = strlen (opname);
4543 size_t max_mname_len = 0;
4545 enum machine_mode nmode, wmode;
4546 const char *nname, *wname;
4547 const char *q;
4548 char *libfunc_name, *suffix;
4549 char *p;
4551 for (nmode = first_mode; nmode != VOIDmode;
4552 nmode = GET_MODE_WIDER_MODE (nmode))
4553 max_mname_len = MAX (max_mname_len, strlen (GET_MODE_NAME (nmode)));
4555 libfunc_name = alloca (2 + opname_len + 2*max_mname_len + 1 + 1);
4556 libfunc_name[0] = '_';
4557 libfunc_name[1] = '_';
4558 memcpy (&libfunc_name[2], opname, opname_len);
4559 suffix = libfunc_name + opname_len + 2;
4561 for (nmode = first_mode; nmode != VOIDmode;
4562 nmode = GET_MODE_WIDER_MODE (nmode))
4563 for (wmode = GET_MODE_WIDER_MODE (nmode); wmode != VOIDmode;
4564 wmode = GET_MODE_WIDER_MODE (wmode))
4566 nname = GET_MODE_NAME (nmode);
4567 wname = GET_MODE_NAME (wmode);
4569 p = suffix;
4570 for (q = widening ? nname : wname; *q; p++, q++)
4571 *p = TOLOWER (*q);
4572 for (q = widening ? wname : nname; *q; p++, q++)
4573 *p = TOLOWER (*q);
4575 *p++ = '2';
4576 *p = '\0';
4578 tab->handlers[widening ? wmode : nmode]
4579 [widening ? nmode : wmode].libfunc
4580 = init_one_libfunc (ggc_alloc_string (libfunc_name,
4581 p - libfunc_name));
4587 init_one_libfunc (const char *name)
4589 rtx symbol;
4591 /* Create a FUNCTION_DECL that can be passed to
4592 targetm.encode_section_info. */
4593 /* ??? We don't have any type information except for this is
4594 a function. Pretend this is "int foo()". */
4595 tree decl = build_decl (FUNCTION_DECL, get_identifier (name),
4596 build_function_type (integer_type_node, NULL_TREE));
4597 DECL_ARTIFICIAL (decl) = 1;
4598 DECL_EXTERNAL (decl) = 1;
4599 TREE_PUBLIC (decl) = 1;
4601 symbol = XEXP (DECL_RTL (decl), 0);
4603 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
4604 are the flags assigned by targetm.encode_section_info. */
4605 SYMBOL_REF_DECL (symbol) = 0;
4607 return symbol;
4610 /* Call this to reset the function entry for one optab (OPTABLE) in mode
4611 MODE to NAME, which should be either 0 or a string constant. */
4612 void
4613 set_optab_libfunc (optab optable, enum machine_mode mode, const char *name)
4615 if (name)
4616 optable->handlers[mode].libfunc = init_one_libfunc (name);
4617 else
4618 optable->handlers[mode].libfunc = 0;
4621 /* Call this to reset the function entry for one conversion optab
4622 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
4623 either 0 or a string constant. */
4624 void
4625 set_conv_libfunc (convert_optab optable, enum machine_mode tmode,
4626 enum machine_mode fmode, const char *name)
4628 if (name)
4629 optable->handlers[tmode][fmode].libfunc = init_one_libfunc (name);
4630 else
4631 optable->handlers[tmode][fmode].libfunc = 0;
4634 /* Call this once to initialize the contents of the optabs
4635 appropriately for the current target machine. */
4637 void
4638 init_optabs (void)
4640 unsigned int i;
4642 /* Start by initializing all tables to contain CODE_FOR_nothing. */
4644 for (i = 0; i < NUM_RTX_CODE; i++)
4645 setcc_gen_code[i] = CODE_FOR_nothing;
4647 #ifdef HAVE_conditional_move
4648 for (i = 0; i < NUM_MACHINE_MODES; i++)
4649 movcc_gen_code[i] = CODE_FOR_nothing;
4650 #endif
4652 add_optab = init_optab (PLUS);
4653 addv_optab = init_optabv (PLUS);
4654 sub_optab = init_optab (MINUS);
4655 subv_optab = init_optabv (MINUS);
4656 smul_optab = init_optab (MULT);
4657 smulv_optab = init_optabv (MULT);
4658 smul_highpart_optab = init_optab (UNKNOWN);
4659 umul_highpart_optab = init_optab (UNKNOWN);
4660 smul_widen_optab = init_optab (UNKNOWN);
4661 umul_widen_optab = init_optab (UNKNOWN);
4662 sdiv_optab = init_optab (DIV);
4663 sdivv_optab = init_optabv (DIV);
4664 sdivmod_optab = init_optab (UNKNOWN);
4665 udiv_optab = init_optab (UDIV);
4666 udivmod_optab = init_optab (UNKNOWN);
4667 smod_optab = init_optab (MOD);
4668 umod_optab = init_optab (UMOD);
4669 fmod_optab = init_optab (UNKNOWN);
4670 drem_optab = init_optab (UNKNOWN);
4671 ftrunc_optab = init_optab (UNKNOWN);
4672 and_optab = init_optab (AND);
4673 ior_optab = init_optab (IOR);
4674 xor_optab = init_optab (XOR);
4675 ashl_optab = init_optab (ASHIFT);
4676 ashr_optab = init_optab (ASHIFTRT);
4677 lshr_optab = init_optab (LSHIFTRT);
4678 rotl_optab = init_optab (ROTATE);
4679 rotr_optab = init_optab (ROTATERT);
4680 smin_optab = init_optab (SMIN);
4681 smax_optab = init_optab (SMAX);
4682 umin_optab = init_optab (UMIN);
4683 umax_optab = init_optab (UMAX);
4684 pow_optab = init_optab (UNKNOWN);
4685 atan2_optab = init_optab (UNKNOWN);
4687 /* These three have codes assigned exclusively for the sake of
4688 have_insn_for. */
4689 mov_optab = init_optab (SET);
4690 movstrict_optab = init_optab (STRICT_LOW_PART);
4691 cmp_optab = init_optab (COMPARE);
4693 ucmp_optab = init_optab (UNKNOWN);
4694 tst_optab = init_optab (UNKNOWN);
4696 eq_optab = init_optab (EQ);
4697 ne_optab = init_optab (NE);
4698 gt_optab = init_optab (GT);
4699 ge_optab = init_optab (GE);
4700 lt_optab = init_optab (LT);
4701 le_optab = init_optab (LE);
4702 unord_optab = init_optab (UNORDERED);
4704 neg_optab = init_optab (NEG);
4705 negv_optab = init_optabv (NEG);
4706 abs_optab = init_optab (ABS);
4707 absv_optab = init_optabv (ABS);
4708 addcc_optab = init_optab (UNKNOWN);
4709 one_cmpl_optab = init_optab (NOT);
4710 ffs_optab = init_optab (FFS);
4711 clz_optab = init_optab (CLZ);
4712 ctz_optab = init_optab (CTZ);
4713 popcount_optab = init_optab (POPCOUNT);
4714 parity_optab = init_optab (PARITY);
4715 sqrt_optab = init_optab (SQRT);
4716 floor_optab = init_optab (UNKNOWN);
4717 ceil_optab = init_optab (UNKNOWN);
4718 round_optab = init_optab (UNKNOWN);
4719 btrunc_optab = init_optab (UNKNOWN);
4720 nearbyint_optab = init_optab (UNKNOWN);
4721 rint_optab = init_optab (UNKNOWN);
4722 sincos_optab = init_optab (UNKNOWN);
4723 sin_optab = init_optab (UNKNOWN);
4724 asin_optab = init_optab (UNKNOWN);
4725 cos_optab = init_optab (UNKNOWN);
4726 acos_optab = init_optab (UNKNOWN);
4727 exp_optab = init_optab (UNKNOWN);
4728 exp10_optab = init_optab (UNKNOWN);
4729 exp2_optab = init_optab (UNKNOWN);
4730 expm1_optab = init_optab (UNKNOWN);
4731 logb_optab = init_optab (UNKNOWN);
4732 ilogb_optab = init_optab (UNKNOWN);
4733 log_optab = init_optab (UNKNOWN);
4734 log10_optab = init_optab (UNKNOWN);
4735 log2_optab = init_optab (UNKNOWN);
4736 log1p_optab = init_optab (UNKNOWN);
4737 tan_optab = init_optab (UNKNOWN);
4738 atan_optab = init_optab (UNKNOWN);
4739 strlen_optab = init_optab (UNKNOWN);
4740 cbranch_optab = init_optab (UNKNOWN);
4741 cmov_optab = init_optab (UNKNOWN);
4742 cstore_optab = init_optab (UNKNOWN);
4743 push_optab = init_optab (UNKNOWN);
4745 vec_extract_optab = init_optab (UNKNOWN);
4746 vec_set_optab = init_optab (UNKNOWN);
4747 vec_init_optab = init_optab (UNKNOWN);
4748 vec_realign_load_optab = init_optab (UNKNOWN);
4750 /* Conversions. */
4751 sext_optab = init_convert_optab (SIGN_EXTEND);
4752 zext_optab = init_convert_optab (ZERO_EXTEND);
4753 trunc_optab = init_convert_optab (TRUNCATE);
4754 sfix_optab = init_convert_optab (FIX);
4755 ufix_optab = init_convert_optab (UNSIGNED_FIX);
4756 sfixtrunc_optab = init_convert_optab (UNKNOWN);
4757 ufixtrunc_optab = init_convert_optab (UNKNOWN);
4758 sfloat_optab = init_convert_optab (FLOAT);
4759 ufloat_optab = init_convert_optab (UNSIGNED_FLOAT);
4761 for (i = 0; i < NUM_MACHINE_MODES; i++)
4763 movmem_optab[i] = CODE_FOR_nothing;
4764 clrmem_optab[i] = CODE_FOR_nothing;
4765 cmpstr_optab[i] = CODE_FOR_nothing;
4766 cmpmem_optab[i] = CODE_FOR_nothing;
4768 #ifdef HAVE_SECONDARY_RELOADS
4769 reload_in_optab[i] = reload_out_optab[i] = CODE_FOR_nothing;
4770 #endif
4773 /* Fill in the optabs with the insns we support. */
4774 init_all_optabs ();
4776 /* Initialize the optabs with the names of the library functions. */
4777 init_integral_libfuncs (add_optab, "add", '3');
4778 init_floating_libfuncs (add_optab, "add", '3');
4779 init_integral_libfuncs (addv_optab, "addv", '3');
4780 init_floating_libfuncs (addv_optab, "add", '3');
4781 init_integral_libfuncs (sub_optab, "sub", '3');
4782 init_floating_libfuncs (sub_optab, "sub", '3');
4783 init_integral_libfuncs (subv_optab, "subv", '3');
4784 init_floating_libfuncs (subv_optab, "sub", '3');
4785 init_integral_libfuncs (smul_optab, "mul", '3');
4786 init_floating_libfuncs (smul_optab, "mul", '3');
4787 init_integral_libfuncs (smulv_optab, "mulv", '3');
4788 init_floating_libfuncs (smulv_optab, "mul", '3');
4789 init_integral_libfuncs (sdiv_optab, "div", '3');
4790 init_floating_libfuncs (sdiv_optab, "div", '3');
4791 init_integral_libfuncs (sdivv_optab, "divv", '3');
4792 init_integral_libfuncs (udiv_optab, "udiv", '3');
4793 init_integral_libfuncs (sdivmod_optab, "divmod", '4');
4794 init_integral_libfuncs (udivmod_optab, "udivmod", '4');
4795 init_integral_libfuncs (smod_optab, "mod", '3');
4796 init_integral_libfuncs (umod_optab, "umod", '3');
4797 init_floating_libfuncs (ftrunc_optab, "ftrunc", '2');
4798 init_integral_libfuncs (and_optab, "and", '3');
4799 init_integral_libfuncs (ior_optab, "ior", '3');
4800 init_integral_libfuncs (xor_optab, "xor", '3');
4801 init_integral_libfuncs (ashl_optab, "ashl", '3');
4802 init_integral_libfuncs (ashr_optab, "ashr", '3');
4803 init_integral_libfuncs (lshr_optab, "lshr", '3');
4804 init_integral_libfuncs (smin_optab, "min", '3');
4805 init_floating_libfuncs (smin_optab, "min", '3');
4806 init_integral_libfuncs (smax_optab, "max", '3');
4807 init_floating_libfuncs (smax_optab, "max", '3');
4808 init_integral_libfuncs (umin_optab, "umin", '3');
4809 init_integral_libfuncs (umax_optab, "umax", '3');
4810 init_integral_libfuncs (neg_optab, "neg", '2');
4811 init_floating_libfuncs (neg_optab, "neg", '2');
4812 init_integral_libfuncs (negv_optab, "negv", '2');
4813 init_floating_libfuncs (negv_optab, "neg", '2');
4814 init_integral_libfuncs (one_cmpl_optab, "one_cmpl", '2');
4815 init_integral_libfuncs (ffs_optab, "ffs", '2');
4816 init_integral_libfuncs (clz_optab, "clz", '2');
4817 init_integral_libfuncs (ctz_optab, "ctz", '2');
4818 init_integral_libfuncs (popcount_optab, "popcount", '2');
4819 init_integral_libfuncs (parity_optab, "parity", '2');
4821 /* Comparison libcalls for integers MUST come in pairs,
4822 signed/unsigned. */
4823 init_integral_libfuncs (cmp_optab, "cmp", '2');
4824 init_integral_libfuncs (ucmp_optab, "ucmp", '2');
4825 init_floating_libfuncs (cmp_optab, "cmp", '2');
4827 /* EQ etc are floating point only. */
4828 init_floating_libfuncs (eq_optab, "eq", '2');
4829 init_floating_libfuncs (ne_optab, "ne", '2');
4830 init_floating_libfuncs (gt_optab, "gt", '2');
4831 init_floating_libfuncs (ge_optab, "ge", '2');
4832 init_floating_libfuncs (lt_optab, "lt", '2');
4833 init_floating_libfuncs (le_optab, "le", '2');
4834 init_floating_libfuncs (unord_optab, "unord", '2');
4836 /* Conversions. */
4837 init_interclass_conv_libfuncs (sfloat_optab, "float",
4838 MODE_INT, MODE_FLOAT);
4839 init_interclass_conv_libfuncs (sfix_optab, "fix",
4840 MODE_FLOAT, MODE_INT);
4841 init_interclass_conv_libfuncs (ufix_optab, "fixuns",
4842 MODE_FLOAT, MODE_INT);
4844 /* sext_optab is also used for FLOAT_EXTEND. */
4845 init_intraclass_conv_libfuncs (sext_optab, "extend", MODE_FLOAT, true);
4846 init_intraclass_conv_libfuncs (trunc_optab, "trunc", MODE_FLOAT, false);
4848 /* Use cabs for double complex abs, since systems generally have cabs.
4849 Don't define any libcall for float complex, so that cabs will be used. */
4850 if (complex_double_type_node)
4851 abs_optab->handlers[TYPE_MODE (complex_double_type_node)].libfunc
4852 = init_one_libfunc ("cabs");
4854 /* The ffs function operates on `int'. */
4855 ffs_optab->handlers[(int) mode_for_size (INT_TYPE_SIZE, MODE_INT, 0)].libfunc
4856 = init_one_libfunc ("ffs");
4858 abort_libfunc = init_one_libfunc ("abort");
4859 memcpy_libfunc = init_one_libfunc ("memcpy");
4860 memmove_libfunc = init_one_libfunc ("memmove");
4861 memcmp_libfunc = init_one_libfunc ("memcmp");
4862 memset_libfunc = init_one_libfunc ("memset");
4863 setbits_libfunc = init_one_libfunc ("__setbits");
4865 unwind_resume_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
4866 ? "_Unwind_SjLj_Resume"
4867 : "_Unwind_Resume");
4868 #ifndef DONT_USE_BUILTIN_SETJMP
4869 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
4870 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
4871 #else
4872 setjmp_libfunc = init_one_libfunc ("setjmp");
4873 longjmp_libfunc = init_one_libfunc ("longjmp");
4874 #endif
4875 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
4876 unwind_sjlj_unregister_libfunc
4877 = init_one_libfunc ("_Unwind_SjLj_Unregister");
4879 /* For function entry/exit instrumentation. */
4880 profile_function_entry_libfunc
4881 = init_one_libfunc ("__cyg_profile_func_enter");
4882 profile_function_exit_libfunc
4883 = init_one_libfunc ("__cyg_profile_func_exit");
4885 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
4887 if (HAVE_conditional_trap)
4888 trap_rtx = gen_rtx_fmt_ee (EQ, VOIDmode, NULL_RTX, NULL_RTX);
4890 /* Allow the target to add more libcalls or rename some, etc. */
4891 targetm.init_libfuncs ();
4894 #ifdef DEBUG
4896 /* Print information about the current contents of the optabs on
4897 STDERR. */
4899 static void
4900 debug_optab_libfuncs (void)
4902 int i;
4903 int j;
4904 int k;
4906 /* Dump the arithmetic optabs. */
4907 for (i = 0; i != (int) OTI_MAX; i++)
4908 for (j = 0; j < NUM_MACHINE_MODES; ++j)
4910 optab o;
4911 struct optab_handlers *h;
4913 o = optab_table[i];
4914 h = &o->handlers[j];
4915 if (h->libfunc)
4917 if (GET_CODE (h->libfunc) != SYMBOL_REF)
4918 abort ();
4919 fprintf (stderr, "%s\t%s:\t%s\n",
4920 GET_RTX_NAME (o->code),
4921 GET_MODE_NAME (j),
4922 XSTR (h->libfunc, 0));
4926 /* Dump the conversion optabs. */
4927 for (i = 0; i < (int) CTI_MAX; ++i)
4928 for (j = 0; j < NUM_MACHINE_MODES; ++j)
4929 for (k = 0; k < NUM_MACHINE_MODES; ++k)
4931 convert_optab o;
4932 struct optab_handlers *h;
4934 o = &convert_optab_table[i];
4935 h = &o->handlers[j][k];
4936 if (h->libfunc)
4938 if (GET_CODE (h->libfunc) != SYMBOL_REF)
4939 abort ();
4940 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
4941 GET_RTX_NAME (o->code),
4942 GET_MODE_NAME (j),
4943 GET_MODE_NAME (k),
4944 XSTR (h->libfunc, 0));
4949 #endif /* DEBUG */
4952 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
4953 CODE. Return 0 on failure. */
4956 gen_cond_trap (enum rtx_code code ATTRIBUTE_UNUSED, rtx op1,
4957 rtx op2 ATTRIBUTE_UNUSED, rtx tcode ATTRIBUTE_UNUSED)
4959 enum machine_mode mode = GET_MODE (op1);
4960 enum insn_code icode;
4961 rtx insn;
4963 if (!HAVE_conditional_trap)
4964 return 0;
4966 if (mode == VOIDmode)
4967 return 0;
4969 icode = cmp_optab->handlers[(int) mode].insn_code;
4970 if (icode == CODE_FOR_nothing)
4971 return 0;
4973 start_sequence ();
4974 op1 = prepare_operand (icode, op1, 0, mode, mode, 0);
4975 op2 = prepare_operand (icode, op2, 1, mode, mode, 0);
4976 if (!op1 || !op2)
4978 end_sequence ();
4979 return 0;
4981 emit_insn (GEN_FCN (icode) (op1, op2));
4983 PUT_CODE (trap_rtx, code);
4984 insn = gen_conditional_trap (trap_rtx, tcode);
4985 if (insn)
4987 emit_insn (insn);
4988 insn = get_insns ();
4990 end_sequence ();
4992 return insn;
4995 #include "gt-optabs.h"