* optabs.c, doc/c-tree.texi, doc/install.texi, doc/md.texi,
[official-gcc.git] / gcc / optabs.c
blob4a94869b30098e28ddb3da0aa9fa9d8d2642802c
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, 2005 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 /* Indexed by the machine mode, gives the insn code for vector conditional
88 operation. */
90 enum insn_code vcond_gen_code[NUM_MACHINE_MODES];
91 enum insn_code vcondu_gen_code[NUM_MACHINE_MODES];
93 /* The insn generating function can not take an rtx_code argument.
94 TRAP_RTX is used as an rtx argument. Its code is replaced with
95 the code to be used in the trap insn and all other fields are ignored. */
96 static GTY(()) rtx trap_rtx;
98 static int add_equal_note (rtx, rtx, enum rtx_code, rtx, rtx);
99 static rtx widen_operand (rtx, enum machine_mode, enum machine_mode, int,
100 int);
101 static void prepare_cmp_insn (rtx *, rtx *, enum rtx_code *, rtx,
102 enum machine_mode *, int *,
103 enum can_compare_purpose);
104 static enum insn_code can_fix_p (enum machine_mode, enum machine_mode, int,
105 int *);
106 static enum insn_code can_float_p (enum machine_mode, enum machine_mode, int);
107 static optab new_optab (void);
108 static convert_optab new_convert_optab (void);
109 static inline optab init_optab (enum rtx_code);
110 static inline optab init_optabv (enum rtx_code);
111 static inline convert_optab init_convert_optab (enum rtx_code);
112 static void init_libfuncs (optab, int, int, const char *, int);
113 static void init_integral_libfuncs (optab, const char *, int);
114 static void init_floating_libfuncs (optab, const char *, int);
115 static void init_interclass_conv_libfuncs (convert_optab, const char *,
116 enum mode_class, enum mode_class);
117 static void init_intraclass_conv_libfuncs (convert_optab, const char *,
118 enum mode_class, bool);
119 static void emit_cmp_and_jump_insn_1 (rtx, rtx, enum machine_mode,
120 enum rtx_code, int, rtx);
121 static void prepare_float_lib_cmp (rtx *, rtx *, enum rtx_code *,
122 enum machine_mode *, int *);
123 static rtx widen_clz (enum machine_mode, rtx, rtx);
124 static rtx expand_parity (enum machine_mode, rtx, rtx);
125 static enum rtx_code get_rtx_code (enum tree_code, bool);
126 static rtx vector_compare_rtx (tree, bool, enum insn_code);
128 #ifndef HAVE_conditional_trap
129 #define HAVE_conditional_trap 0
130 #define gen_conditional_trap(a,b) (abort (), NULL_RTX)
131 #endif
133 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
134 the result of operation CODE applied to OP0 (and OP1 if it is a binary
135 operation).
137 If the last insn does not set TARGET, don't do anything, but return 1.
139 If a previous insn sets TARGET and TARGET is one of OP0 or OP1,
140 don't add the REG_EQUAL note but return 0. Our caller can then try
141 again, ensuring that TARGET is not one of the operands. */
143 static int
144 add_equal_note (rtx insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
146 rtx last_insn, insn, set;
147 rtx note;
149 if (! insns
150 || ! INSN_P (insns)
151 || NEXT_INSN (insns) == NULL_RTX)
152 abort ();
154 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
155 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
156 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
157 && GET_RTX_CLASS (code) != RTX_COMPARE
158 && GET_RTX_CLASS (code) != RTX_UNARY)
159 return 1;
161 if (GET_CODE (target) == ZERO_EXTRACT)
162 return 1;
164 for (last_insn = insns;
165 NEXT_INSN (last_insn) != NULL_RTX;
166 last_insn = NEXT_INSN (last_insn))
169 set = single_set (last_insn);
170 if (set == NULL_RTX)
171 return 1;
173 if (! rtx_equal_p (SET_DEST (set), target)
174 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
175 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
176 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
177 return 1;
179 /* If TARGET is in OP0 or OP1, check if anything in SEQ sets TARGET
180 besides the last insn. */
181 if (reg_overlap_mentioned_p (target, op0)
182 || (op1 && reg_overlap_mentioned_p (target, op1)))
184 insn = PREV_INSN (last_insn);
185 while (insn != NULL_RTX)
187 if (reg_set_p (target, insn))
188 return 0;
190 insn = PREV_INSN (insn);
194 if (GET_RTX_CLASS (code) == RTX_UNARY)
195 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
196 else
197 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
199 set_unique_reg_note (last_insn, REG_EQUAL, note);
201 return 1;
204 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
205 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
206 not actually do a sign-extend or zero-extend, but can leave the
207 higher-order bits of the result rtx undefined, for example, in the case
208 of logical operations, but not right shifts. */
210 static rtx
211 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
212 int unsignedp, int no_extend)
214 rtx result;
216 /* If we don't have to extend and this is a constant, return it. */
217 if (no_extend && GET_MODE (op) == VOIDmode)
218 return op;
220 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
221 extend since it will be more efficient to do so unless the signedness of
222 a promoted object differs from our extension. */
223 if (! no_extend
224 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
225 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
226 return convert_modes (mode, oldmode, op, unsignedp);
228 /* If MODE is no wider than a single word, we return a paradoxical
229 SUBREG. */
230 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
231 return gen_rtx_SUBREG (mode, force_reg (GET_MODE (op), op), 0);
233 /* Otherwise, get an object of MODE, clobber it, and set the low-order
234 part to OP. */
236 result = gen_reg_rtx (mode);
237 emit_insn (gen_rtx_CLOBBER (VOIDmode, result));
238 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
239 return result;
242 /* Return the optab used for computing the operation given by
243 the tree code, CODE. This function is not always usable (for
244 example, it cannot give complete results for multiplication
245 or division) but probably ought to be relied on more widely
246 throughout the expander. */
247 optab
248 optab_for_tree_code (enum tree_code code, tree type)
250 bool trapv;
251 switch (code)
253 case BIT_AND_EXPR:
254 return and_optab;
256 case BIT_IOR_EXPR:
257 return ior_optab;
259 case BIT_NOT_EXPR:
260 return one_cmpl_optab;
262 case BIT_XOR_EXPR:
263 return xor_optab;
265 case TRUNC_MOD_EXPR:
266 case CEIL_MOD_EXPR:
267 case FLOOR_MOD_EXPR:
268 case ROUND_MOD_EXPR:
269 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
271 case RDIV_EXPR:
272 case TRUNC_DIV_EXPR:
273 case CEIL_DIV_EXPR:
274 case FLOOR_DIV_EXPR:
275 case ROUND_DIV_EXPR:
276 case EXACT_DIV_EXPR:
277 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
279 case LSHIFT_EXPR:
280 return ashl_optab;
282 case RSHIFT_EXPR:
283 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
285 case LROTATE_EXPR:
286 return rotl_optab;
288 case RROTATE_EXPR:
289 return rotr_optab;
291 case MAX_EXPR:
292 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
294 case MIN_EXPR:
295 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
297 case REALIGN_LOAD_EXPR:
298 return vec_realign_load_optab;
300 default:
301 break;
304 trapv = flag_trapv && INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type);
305 switch (code)
307 case PLUS_EXPR:
308 return trapv ? addv_optab : add_optab;
310 case MINUS_EXPR:
311 return trapv ? subv_optab : sub_optab;
313 case MULT_EXPR:
314 return trapv ? smulv_optab : smul_optab;
316 case NEGATE_EXPR:
317 return trapv ? negv_optab : neg_optab;
319 case ABS_EXPR:
320 return trapv ? absv_optab : abs_optab;
322 default:
323 return NULL;
328 /* Generate code to perform an operation specified by TERNARY_OPTAB
329 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
331 UNSIGNEDP is for the case where we have to widen the operands
332 to perform the operation. It says to use zero-extension.
334 If TARGET is nonzero, the value
335 is generated there, if it is convenient to do so.
336 In all cases an rtx is returned for the locus of the value;
337 this may or may not be TARGET. */
340 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
341 rtx op1, rtx op2, rtx target, int unsignedp)
343 int icode = (int) ternary_optab->handlers[(int) mode].insn_code;
344 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
345 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
346 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
347 rtx temp;
348 rtx pat;
349 rtx xop0 = op0, xop1 = op1, xop2 = op2;
351 if (ternary_optab->handlers[(int) mode].insn_code == CODE_FOR_nothing)
352 abort ();
354 if (!target
355 || ! (*insn_data[icode].operand[0].predicate) (target, mode))
356 temp = gen_reg_rtx (mode);
357 else
358 temp = target;
360 /* In case the insn wants input operands in modes different from
361 those of the actual operands, convert the operands. It would
362 seem that we don't need to convert CONST_INTs, but we do, so
363 that they're properly zero-extended, sign-extended or truncated
364 for their mode. */
366 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
367 xop0 = convert_modes (mode0,
368 GET_MODE (op0) != VOIDmode
369 ? GET_MODE (op0)
370 : mode,
371 xop0, unsignedp);
373 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
374 xop1 = convert_modes (mode1,
375 GET_MODE (op1) != VOIDmode
376 ? GET_MODE (op1)
377 : mode,
378 xop1, unsignedp);
380 if (GET_MODE (op2) != mode2 && mode2 != VOIDmode)
381 xop2 = convert_modes (mode2,
382 GET_MODE (op2) != VOIDmode
383 ? GET_MODE (op2)
384 : mode,
385 xop2, unsignedp);
387 /* Now, if insn's predicates don't allow our operands, put them into
388 pseudo regs. */
390 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0)
391 && mode0 != VOIDmode)
392 xop0 = copy_to_mode_reg (mode0, xop0);
394 if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1)
395 && mode1 != VOIDmode)
396 xop1 = copy_to_mode_reg (mode1, xop1);
398 if (! (*insn_data[icode].operand[3].predicate) (xop2, mode2)
399 && mode2 != VOIDmode)
400 xop2 = copy_to_mode_reg (mode2, xop2);
402 pat = GEN_FCN (icode) (temp, xop0, xop1, xop2);
404 emit_insn (pat);
405 return temp;
409 /* Like expand_binop, but return a constant rtx if the result can be
410 calculated at compile time. The arguments and return value are
411 otherwise the same as for expand_binop. */
413 static rtx
414 simplify_expand_binop (enum machine_mode mode, optab binoptab,
415 rtx op0, rtx op1, rtx target, int unsignedp,
416 enum optab_methods methods)
418 if (CONSTANT_P (op0) && CONSTANT_P (op1))
419 return simplify_gen_binary (binoptab->code, mode, op0, op1);
420 else
421 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
424 /* Like simplify_expand_binop, but always put the result in TARGET.
425 Return true if the expansion succeeded. */
427 bool
428 force_expand_binop (enum machine_mode mode, optab binoptab,
429 rtx op0, rtx op1, rtx target, int unsignedp,
430 enum optab_methods methods)
432 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
433 target, unsignedp, methods);
434 if (x == 0)
435 return false;
436 if (x != target)
437 emit_move_insn (target, x);
438 return true;
441 /* This subroutine of expand_doubleword_shift handles the cases in which
442 the effective shift value is >= BITS_PER_WORD. The arguments and return
443 value are the same as for the parent routine, except that SUPERWORD_OP1
444 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
445 INTO_TARGET may be null if the caller has decided to calculate it. */
447 static bool
448 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
449 rtx outof_target, rtx into_target,
450 int unsignedp, enum optab_methods methods)
452 if (into_target != 0)
453 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
454 into_target, unsignedp, methods))
455 return false;
457 if (outof_target != 0)
459 /* For a signed right shift, we must fill OUTOF_TARGET with copies
460 of the sign bit, otherwise we must fill it with zeros. */
461 if (binoptab != ashr_optab)
462 emit_move_insn (outof_target, CONST0_RTX (word_mode));
463 else
464 if (!force_expand_binop (word_mode, binoptab,
465 outof_input, GEN_INT (BITS_PER_WORD - 1),
466 outof_target, unsignedp, methods))
467 return false;
469 return true;
472 /* This subroutine of expand_doubleword_shift handles the cases in which
473 the effective shift value is < BITS_PER_WORD. The arguments and return
474 value are the same as for the parent routine. */
476 static bool
477 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
478 rtx outof_input, rtx into_input, rtx op1,
479 rtx outof_target, rtx into_target,
480 int unsignedp, enum optab_methods methods,
481 unsigned HOST_WIDE_INT shift_mask)
483 optab reverse_unsigned_shift, unsigned_shift;
484 rtx tmp, carries;
486 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
487 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
489 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
490 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
491 the opposite direction to BINOPTAB. */
492 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
494 carries = outof_input;
495 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
496 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
497 0, true, methods);
499 else
501 /* We must avoid shifting by BITS_PER_WORD bits since that is either
502 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
503 has unknown behavior. Do a single shift first, then shift by the
504 remainder. It's OK to use ~OP1 as the remainder if shift counts
505 are truncated to the mode size. */
506 carries = expand_binop (word_mode, reverse_unsigned_shift,
507 outof_input, const1_rtx, 0, unsignedp, methods);
508 if (shift_mask == BITS_PER_WORD - 1)
510 tmp = immed_double_const (-1, -1, op1_mode);
511 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
512 0, true, methods);
514 else
516 tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
517 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
518 0, true, methods);
521 if (tmp == 0 || carries == 0)
522 return false;
523 carries = expand_binop (word_mode, reverse_unsigned_shift,
524 carries, tmp, 0, unsignedp, methods);
525 if (carries == 0)
526 return false;
528 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
529 so the result can go directly into INTO_TARGET if convenient. */
530 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
531 into_target, unsignedp, methods);
532 if (tmp == 0)
533 return false;
535 /* Now OR in the bits carried over from OUTOF_INPUT. */
536 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
537 into_target, unsignedp, methods))
538 return false;
540 /* Use a standard word_mode shift for the out-of half. */
541 if (outof_target != 0)
542 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
543 outof_target, unsignedp, methods))
544 return false;
546 return true;
550 #ifdef HAVE_conditional_move
551 /* Try implementing expand_doubleword_shift using conditional moves.
552 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
553 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
554 are the shift counts to use in the former and latter case. All other
555 arguments are the same as the parent routine. */
557 static bool
558 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
559 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
560 rtx outof_input, rtx into_input,
561 rtx subword_op1, rtx superword_op1,
562 rtx outof_target, rtx into_target,
563 int unsignedp, enum optab_methods methods,
564 unsigned HOST_WIDE_INT shift_mask)
566 rtx outof_superword, into_superword;
568 /* Put the superword version of the output into OUTOF_SUPERWORD and
569 INTO_SUPERWORD. */
570 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
571 if (outof_target != 0 && subword_op1 == superword_op1)
573 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
574 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
575 into_superword = outof_target;
576 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
577 outof_superword, 0, unsignedp, methods))
578 return false;
580 else
582 into_superword = gen_reg_rtx (word_mode);
583 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
584 outof_superword, into_superword,
585 unsignedp, methods))
586 return false;
589 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
590 if (!expand_subword_shift (op1_mode, binoptab,
591 outof_input, into_input, subword_op1,
592 outof_target, into_target,
593 unsignedp, methods, shift_mask))
594 return false;
596 /* Select between them. Do the INTO half first because INTO_SUPERWORD
597 might be the current value of OUTOF_TARGET. */
598 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
599 into_target, into_superword, word_mode, false))
600 return false;
602 if (outof_target != 0)
603 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
604 outof_target, outof_superword,
605 word_mode, false))
606 return false;
608 return true;
610 #endif
612 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
613 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
614 input operand; the shift moves bits in the direction OUTOF_INPUT->
615 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
616 of the target. OP1 is the shift count and OP1_MODE is its mode.
617 If OP1 is constant, it will have been truncated as appropriate
618 and is known to be nonzero.
620 If SHIFT_MASK is zero, the result of word shifts is undefined when the
621 shift count is outside the range [0, BITS_PER_WORD). This routine must
622 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
624 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
625 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
626 fill with zeros or sign bits as appropriate.
628 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
629 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
630 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
631 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
632 are undefined.
634 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
635 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
636 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
637 function wants to calculate it itself.
639 Return true if the shift could be successfully synthesized. */
641 static bool
642 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
643 rtx outof_input, rtx into_input, rtx op1,
644 rtx outof_target, rtx into_target,
645 int unsignedp, enum optab_methods methods,
646 unsigned HOST_WIDE_INT shift_mask)
648 rtx superword_op1, tmp, cmp1, cmp2;
649 rtx subword_label, done_label;
650 enum rtx_code cmp_code;
652 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
653 fill the result with sign or zero bits as appropriate. If so, the value
654 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
655 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
656 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
658 This isn't worthwhile for constant shifts since the optimizers will
659 cope better with in-range shift counts. */
660 if (shift_mask >= BITS_PER_WORD
661 && outof_target != 0
662 && !CONSTANT_P (op1))
664 if (!expand_doubleword_shift (op1_mode, binoptab,
665 outof_input, into_input, op1,
666 0, into_target,
667 unsignedp, methods, shift_mask))
668 return false;
669 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
670 outof_target, unsignedp, methods))
671 return false;
672 return true;
675 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
676 is true when the effective shift value is less than BITS_PER_WORD.
677 Set SUPERWORD_OP1 to the shift count that should be used to shift
678 OUTOF_INPUT into INTO_TARGET when the condition is false. */
679 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
680 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
682 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
683 is a subword shift count. */
684 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
685 0, true, methods);
686 cmp2 = CONST0_RTX (op1_mode);
687 cmp_code = EQ;
688 superword_op1 = op1;
690 else
692 /* Set CMP1 to OP1 - BITS_PER_WORD. */
693 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
694 0, true, methods);
695 cmp2 = CONST0_RTX (op1_mode);
696 cmp_code = LT;
697 superword_op1 = cmp1;
699 if (cmp1 == 0)
700 return false;
702 /* If we can compute the condition at compile time, pick the
703 appropriate subroutine. */
704 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
705 if (tmp != 0 && GET_CODE (tmp) == CONST_INT)
707 if (tmp == const0_rtx)
708 return expand_superword_shift (binoptab, outof_input, superword_op1,
709 outof_target, into_target,
710 unsignedp, methods);
711 else
712 return expand_subword_shift (op1_mode, binoptab,
713 outof_input, into_input, op1,
714 outof_target, into_target,
715 unsignedp, methods, shift_mask);
718 #ifdef HAVE_conditional_move
719 /* Try using conditional moves to generate straight-line code. */
721 rtx start = get_last_insn ();
722 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
723 cmp_code, cmp1, cmp2,
724 outof_input, into_input,
725 op1, superword_op1,
726 outof_target, into_target,
727 unsignedp, methods, shift_mask))
728 return true;
729 delete_insns_since (start);
731 #endif
733 /* As a last resort, use branches to select the correct alternative. */
734 subword_label = gen_label_rtx ();
735 done_label = gen_label_rtx ();
737 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
738 0, 0, subword_label);
740 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
741 outof_target, into_target,
742 unsignedp, methods))
743 return false;
745 emit_jump_insn (gen_jump (done_label));
746 emit_barrier ();
747 emit_label (subword_label);
749 if (!expand_subword_shift (op1_mode, binoptab,
750 outof_input, into_input, op1,
751 outof_target, into_target,
752 unsignedp, methods, shift_mask))
753 return false;
755 emit_label (done_label);
756 return true;
759 /* Wrapper around expand_binop which takes an rtx code to specify
760 the operation to perform, not an optab pointer. All other
761 arguments are the same. */
763 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
764 rtx op1, rtx target, int unsignedp,
765 enum optab_methods methods)
767 optab binop = code_to_optab[(int) code];
768 if (binop == 0)
769 abort ();
771 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
774 /* Generate code to perform an operation specified by BINOPTAB
775 on operands OP0 and OP1, with result having machine-mode MODE.
777 UNSIGNEDP is for the case where we have to widen the operands
778 to perform the operation. It says to use zero-extension.
780 If TARGET is nonzero, the value
781 is generated there, if it is convenient to do so.
782 In all cases an rtx is returned for the locus of the value;
783 this may or may not be TARGET. */
786 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
787 rtx target, int unsignedp, enum optab_methods methods)
789 enum optab_methods next_methods
790 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
791 ? OPTAB_WIDEN : methods);
792 enum mode_class class;
793 enum machine_mode wider_mode;
794 rtx temp;
795 int commutative_op = 0;
796 int shift_op = (binoptab->code == ASHIFT
797 || binoptab->code == ASHIFTRT
798 || binoptab->code == LSHIFTRT
799 || binoptab->code == ROTATE
800 || binoptab->code == ROTATERT);
801 rtx entry_last = get_last_insn ();
802 rtx last;
804 class = GET_MODE_CLASS (mode);
806 if (flag_force_mem)
808 /* Load duplicate non-volatile operands once. */
809 if (rtx_equal_p (op0, op1) && ! volatile_refs_p (op0))
811 op0 = force_not_mem (op0);
812 op1 = op0;
814 else
816 op0 = force_not_mem (op0);
817 op1 = force_not_mem (op1);
821 /* If subtracting an integer constant, convert this into an addition of
822 the negated constant. */
824 if (binoptab == sub_optab && GET_CODE (op1) == CONST_INT)
826 op1 = negate_rtx (mode, op1);
827 binoptab = add_optab;
830 /* If we are inside an appropriately-short loop and we are optimizing,
831 force expensive constants into a register. */
832 if (CONSTANT_P (op0) && optimize
833 && rtx_cost (op0, binoptab->code) > COSTS_N_INSNS (1))
835 if (GET_MODE (op0) != VOIDmode)
836 op0 = convert_modes (mode, VOIDmode, op0, unsignedp);
837 op0 = force_reg (mode, op0);
840 if (CONSTANT_P (op1) && optimize
841 && ! shift_op && rtx_cost (op1, binoptab->code) > COSTS_N_INSNS (1))
843 if (GET_MODE (op1) != VOIDmode)
844 op1 = convert_modes (mode, VOIDmode, op1, unsignedp);
845 op1 = force_reg (mode, op1);
848 /* Record where to delete back to if we backtrack. */
849 last = get_last_insn ();
851 /* If operation is commutative,
852 try to make the first operand a register.
853 Even better, try to make it the same as the target.
854 Also try to make the last operand a constant. */
855 if (GET_RTX_CLASS (binoptab->code) == RTX_COMM_ARITH
856 || binoptab == smul_widen_optab
857 || binoptab == umul_widen_optab
858 || binoptab == smul_highpart_optab
859 || binoptab == umul_highpart_optab)
861 commutative_op = 1;
863 if (((target == 0 || REG_P (target))
864 ? ((REG_P (op1)
865 && !REG_P (op0))
866 || target == op1)
867 : rtx_equal_p (op1, target))
868 || GET_CODE (op0) == CONST_INT)
870 temp = op1;
871 op1 = op0;
872 op0 = temp;
876 /* If we can do it with a three-operand insn, do so. */
878 if (methods != OPTAB_MUST_WIDEN
879 && binoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
881 int icode = (int) binoptab->handlers[(int) mode].insn_code;
882 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
883 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
884 rtx pat;
885 rtx xop0 = op0, xop1 = op1;
887 if (target)
888 temp = target;
889 else
890 temp = gen_reg_rtx (mode);
892 /* If it is a commutative operator and the modes would match
893 if we would swap the operands, we can save the conversions. */
894 if (commutative_op)
896 if (GET_MODE (op0) != mode0 && GET_MODE (op1) != mode1
897 && GET_MODE (op0) == mode1 && GET_MODE (op1) == mode0)
899 rtx tmp;
901 tmp = op0; op0 = op1; op1 = tmp;
902 tmp = xop0; xop0 = xop1; xop1 = tmp;
906 /* In case the insn wants input operands in modes different from
907 those of the actual operands, convert the operands. It would
908 seem that we don't need to convert CONST_INTs, but we do, so
909 that they're properly zero-extended, sign-extended or truncated
910 for their mode. */
912 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
913 xop0 = convert_modes (mode0,
914 GET_MODE (op0) != VOIDmode
915 ? GET_MODE (op0)
916 : mode,
917 xop0, unsignedp);
919 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
920 xop1 = convert_modes (mode1,
921 GET_MODE (op1) != VOIDmode
922 ? GET_MODE (op1)
923 : mode,
924 xop1, unsignedp);
926 /* Now, if insn's predicates don't allow our operands, put them into
927 pseudo regs. */
929 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0)
930 && mode0 != VOIDmode)
931 xop0 = copy_to_mode_reg (mode0, xop0);
933 if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1)
934 && mode1 != VOIDmode)
935 xop1 = copy_to_mode_reg (mode1, xop1);
937 if (! (*insn_data[icode].operand[0].predicate) (temp, mode))
938 temp = gen_reg_rtx (mode);
940 pat = GEN_FCN (icode) (temp, xop0, xop1);
941 if (pat)
943 /* If PAT is composed of more than one insn, try to add an appropriate
944 REG_EQUAL note to it. If we can't because TEMP conflicts with an
945 operand, call ourselves again, this time without a target. */
946 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
947 && ! add_equal_note (pat, temp, binoptab->code, xop0, xop1))
949 delete_insns_since (last);
950 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
951 unsignedp, methods);
954 emit_insn (pat);
955 return temp;
957 else
958 delete_insns_since (last);
961 /* If this is a multiply, see if we can do a widening operation that
962 takes operands of this mode and makes a wider mode. */
964 if (binoptab == smul_optab && GET_MODE_WIDER_MODE (mode) != VOIDmode
965 && (((unsignedp ? umul_widen_optab : smul_widen_optab)
966 ->handlers[(int) GET_MODE_WIDER_MODE (mode)].insn_code)
967 != CODE_FOR_nothing))
969 temp = expand_binop (GET_MODE_WIDER_MODE (mode),
970 unsignedp ? umul_widen_optab : smul_widen_optab,
971 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
973 if (temp != 0)
975 if (GET_MODE_CLASS (mode) == MODE_INT)
976 return gen_lowpart (mode, temp);
977 else
978 return convert_to_mode (mode, temp, unsignedp);
982 /* Look for a wider mode of the same class for which we think we
983 can open-code the operation. Check for a widening multiply at the
984 wider mode as well. */
986 if ((class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
987 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
988 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
989 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
991 if (binoptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
992 || (binoptab == smul_optab
993 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
994 && (((unsignedp ? umul_widen_optab : smul_widen_optab)
995 ->handlers[(int) GET_MODE_WIDER_MODE (wider_mode)].insn_code)
996 != CODE_FOR_nothing)))
998 rtx xop0 = op0, xop1 = op1;
999 int no_extend = 0;
1001 /* For certain integer operations, we need not actually extend
1002 the narrow operands, as long as we will truncate
1003 the results to the same narrowness. */
1005 if ((binoptab == ior_optab || binoptab == and_optab
1006 || binoptab == xor_optab
1007 || binoptab == add_optab || binoptab == sub_optab
1008 || binoptab == smul_optab || binoptab == ashl_optab)
1009 && class == MODE_INT)
1010 no_extend = 1;
1012 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1014 /* The second operand of a shift must always be extended. */
1015 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1016 no_extend && binoptab != ashl_optab);
1018 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1019 unsignedp, OPTAB_DIRECT);
1020 if (temp)
1022 if (class != MODE_INT)
1024 if (target == 0)
1025 target = gen_reg_rtx (mode);
1026 convert_move (target, temp, 0);
1027 return target;
1029 else
1030 return gen_lowpart (mode, temp);
1032 else
1033 delete_insns_since (last);
1037 /* These can be done a word at a time. */
1038 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1039 && class == MODE_INT
1040 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1041 && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1043 int i;
1044 rtx insns;
1045 rtx equiv_value;
1047 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1048 won't be accurate, so use a new target. */
1049 if (target == 0 || target == op0 || target == op1)
1050 target = gen_reg_rtx (mode);
1052 start_sequence ();
1054 /* Do the actual arithmetic. */
1055 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1057 rtx target_piece = operand_subword (target, i, 1, mode);
1058 rtx x = expand_binop (word_mode, binoptab,
1059 operand_subword_force (op0, i, mode),
1060 operand_subword_force (op1, i, mode),
1061 target_piece, unsignedp, next_methods);
1063 if (x == 0)
1064 break;
1066 if (target_piece != x)
1067 emit_move_insn (target_piece, x);
1070 insns = get_insns ();
1071 end_sequence ();
1073 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1075 if (binoptab->code != UNKNOWN)
1076 equiv_value
1077 = gen_rtx_fmt_ee (binoptab->code, mode,
1078 copy_rtx (op0), copy_rtx (op1));
1079 else
1080 equiv_value = 0;
1082 emit_no_conflict_block (insns, target, op0, op1, equiv_value);
1083 return target;
1087 /* Synthesize double word shifts from single word shifts. */
1088 if ((binoptab == lshr_optab || binoptab == ashl_optab
1089 || binoptab == ashr_optab)
1090 && class == MODE_INT
1091 && (GET_CODE (op1) == CONST_INT || !optimize_size)
1092 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1093 && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1094 && ashl_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1095 && lshr_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1097 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1098 enum machine_mode op1_mode;
1100 double_shift_mask = targetm.shift_truncation_mask (mode);
1101 shift_mask = targetm.shift_truncation_mask (word_mode);
1102 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1104 /* Apply the truncation to constant shifts. */
1105 if (double_shift_mask > 0 && GET_CODE (op1) == CONST_INT)
1106 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1108 if (op1 == CONST0_RTX (op1_mode))
1109 return op0;
1111 /* Make sure that this is a combination that expand_doubleword_shift
1112 can handle. See the comments there for details. */
1113 if (double_shift_mask == 0
1114 || (shift_mask == BITS_PER_WORD - 1
1115 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1117 rtx insns, equiv_value;
1118 rtx into_target, outof_target;
1119 rtx into_input, outof_input;
1120 int left_shift, outof_word;
1122 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1123 won't be accurate, so use a new target. */
1124 if (target == 0 || target == op0 || target == op1)
1125 target = gen_reg_rtx (mode);
1127 start_sequence ();
1129 /* OUTOF_* is the word we are shifting bits away from, and
1130 INTO_* is the word that we are shifting bits towards, thus
1131 they differ depending on the direction of the shift and
1132 WORDS_BIG_ENDIAN. */
1134 left_shift = binoptab == ashl_optab;
1135 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1137 outof_target = operand_subword (target, outof_word, 1, mode);
1138 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1140 outof_input = operand_subword_force (op0, outof_word, mode);
1141 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1143 if (expand_doubleword_shift (op1_mode, binoptab,
1144 outof_input, into_input, op1,
1145 outof_target, into_target,
1146 unsignedp, methods, shift_mask))
1148 insns = get_insns ();
1149 end_sequence ();
1151 equiv_value = gen_rtx_fmt_ee (binoptab->code, mode, op0, op1);
1152 emit_no_conflict_block (insns, target, op0, op1, equiv_value);
1153 return target;
1155 end_sequence ();
1159 /* Synthesize double word rotates from single word shifts. */
1160 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1161 && class == MODE_INT
1162 && GET_CODE (op1) == CONST_INT
1163 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1164 && ashl_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1165 && lshr_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1167 rtx insns, equiv_value;
1168 rtx into_target, outof_target;
1169 rtx into_input, outof_input;
1170 rtx inter;
1171 int shift_count, left_shift, outof_word;
1173 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1174 won't be accurate, so use a new target. Do this also if target is not
1175 a REG, first because having a register instead may open optimization
1176 opportunities, and second because if target and op0 happen to be MEMs
1177 designating the same location, we would risk clobbering it too early
1178 in the code sequence we generate below. */
1179 if (target == 0 || target == op0 || target == op1 || ! REG_P (target))
1180 target = gen_reg_rtx (mode);
1182 start_sequence ();
1184 shift_count = INTVAL (op1);
1186 /* OUTOF_* is the word we are shifting bits away from, and
1187 INTO_* is the word that we are shifting bits towards, thus
1188 they differ depending on the direction of the shift and
1189 WORDS_BIG_ENDIAN. */
1191 left_shift = (binoptab == rotl_optab);
1192 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1194 outof_target = operand_subword (target, outof_word, 1, mode);
1195 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1197 outof_input = operand_subword_force (op0, outof_word, mode);
1198 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1200 if (shift_count == BITS_PER_WORD)
1202 /* This is just a word swap. */
1203 emit_move_insn (outof_target, into_input);
1204 emit_move_insn (into_target, outof_input);
1205 inter = const0_rtx;
1207 else
1209 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1210 rtx first_shift_count, second_shift_count;
1211 optab reverse_unsigned_shift, unsigned_shift;
1213 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1214 ? lshr_optab : ashl_optab);
1216 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1217 ? ashl_optab : lshr_optab);
1219 if (shift_count > BITS_PER_WORD)
1221 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1222 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1224 else
1226 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1227 second_shift_count = GEN_INT (shift_count);
1230 into_temp1 = expand_binop (word_mode, unsigned_shift,
1231 outof_input, first_shift_count,
1232 NULL_RTX, unsignedp, next_methods);
1233 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1234 into_input, second_shift_count,
1235 NULL_RTX, unsignedp, next_methods);
1237 if (into_temp1 != 0 && into_temp2 != 0)
1238 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1239 into_target, unsignedp, next_methods);
1240 else
1241 inter = 0;
1243 if (inter != 0 && inter != into_target)
1244 emit_move_insn (into_target, inter);
1246 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1247 into_input, first_shift_count,
1248 NULL_RTX, unsignedp, next_methods);
1249 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1250 outof_input, second_shift_count,
1251 NULL_RTX, unsignedp, next_methods);
1253 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1254 inter = expand_binop (word_mode, ior_optab,
1255 outof_temp1, outof_temp2,
1256 outof_target, unsignedp, next_methods);
1258 if (inter != 0 && inter != outof_target)
1259 emit_move_insn (outof_target, inter);
1262 insns = get_insns ();
1263 end_sequence ();
1265 if (inter != 0)
1267 if (binoptab->code != UNKNOWN)
1268 equiv_value = gen_rtx_fmt_ee (binoptab->code, mode, op0, op1);
1269 else
1270 equiv_value = 0;
1272 /* We can't make this a no conflict block if this is a word swap,
1273 because the word swap case fails if the input and output values
1274 are in the same register. */
1275 if (shift_count != BITS_PER_WORD)
1276 emit_no_conflict_block (insns, target, op0, op1, equiv_value);
1277 else
1278 emit_insn (insns);
1281 return target;
1285 /* These can be done a word at a time by propagating carries. */
1286 if ((binoptab == add_optab || binoptab == sub_optab)
1287 && class == MODE_INT
1288 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1289 && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1291 unsigned int i;
1292 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1293 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1294 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1295 rtx xop0, xop1, xtarget;
1297 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1298 value is one of those, use it. Otherwise, use 1 since it is the
1299 one easiest to get. */
1300 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1301 int normalizep = STORE_FLAG_VALUE;
1302 #else
1303 int normalizep = 1;
1304 #endif
1306 /* Prepare the operands. */
1307 xop0 = force_reg (mode, op0);
1308 xop1 = force_reg (mode, op1);
1310 xtarget = gen_reg_rtx (mode);
1312 if (target == 0 || !REG_P (target))
1313 target = xtarget;
1315 /* Indicate for flow that the entire target reg is being set. */
1316 if (REG_P (target))
1317 emit_insn (gen_rtx_CLOBBER (VOIDmode, xtarget));
1319 /* Do the actual arithmetic. */
1320 for (i = 0; i < nwords; i++)
1322 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1323 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1324 rtx op0_piece = operand_subword_force (xop0, index, mode);
1325 rtx op1_piece = operand_subword_force (xop1, index, mode);
1326 rtx x;
1328 /* Main add/subtract of the input operands. */
1329 x = expand_binop (word_mode, binoptab,
1330 op0_piece, op1_piece,
1331 target_piece, unsignedp, next_methods);
1332 if (x == 0)
1333 break;
1335 if (i + 1 < nwords)
1337 /* Store carry from main add/subtract. */
1338 carry_out = gen_reg_rtx (word_mode);
1339 carry_out = emit_store_flag_force (carry_out,
1340 (binoptab == add_optab
1341 ? LT : GT),
1342 x, op0_piece,
1343 word_mode, 1, normalizep);
1346 if (i > 0)
1348 rtx newx;
1350 /* Add/subtract previous carry to main result. */
1351 newx = expand_binop (word_mode,
1352 normalizep == 1 ? binoptab : otheroptab,
1353 x, carry_in,
1354 NULL_RTX, 1, next_methods);
1356 if (i + 1 < nwords)
1358 /* Get out carry from adding/subtracting carry in. */
1359 rtx carry_tmp = gen_reg_rtx (word_mode);
1360 carry_tmp = emit_store_flag_force (carry_tmp,
1361 (binoptab == add_optab
1362 ? LT : GT),
1363 newx, x,
1364 word_mode, 1, normalizep);
1366 /* Logical-ior the two poss. carry together. */
1367 carry_out = expand_binop (word_mode, ior_optab,
1368 carry_out, carry_tmp,
1369 carry_out, 0, next_methods);
1370 if (carry_out == 0)
1371 break;
1373 emit_move_insn (target_piece, newx);
1376 carry_in = carry_out;
1379 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
1381 if (mov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
1382 || ! rtx_equal_p (target, xtarget))
1384 rtx temp = emit_move_insn (target, xtarget);
1386 set_unique_reg_note (temp,
1387 REG_EQUAL,
1388 gen_rtx_fmt_ee (binoptab->code, mode,
1389 copy_rtx (xop0),
1390 copy_rtx (xop1)));
1392 else
1393 target = xtarget;
1395 return target;
1398 else
1399 delete_insns_since (last);
1402 /* If we want to multiply two two-word values and have normal and widening
1403 multiplies of single-word values, we can do this with three smaller
1404 multiplications. Note that we do not make a REG_NO_CONFLICT block here
1405 because we are not operating on one word at a time.
1407 The multiplication proceeds as follows:
1408 _______________________
1409 [__op0_high_|__op0_low__]
1410 _______________________
1411 * [__op1_high_|__op1_low__]
1412 _______________________________________________
1413 _______________________
1414 (1) [__op0_low__*__op1_low__]
1415 _______________________
1416 (2a) [__op0_low__*__op1_high_]
1417 _______________________
1418 (2b) [__op0_high_*__op1_low__]
1419 _______________________
1420 (3) [__op0_high_*__op1_high_]
1423 This gives a 4-word result. Since we are only interested in the
1424 lower 2 words, partial result (3) and the upper words of (2a) and
1425 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1426 calculated using non-widening multiplication.
1428 (1), however, needs to be calculated with an unsigned widening
1429 multiplication. If this operation is not directly supported we
1430 try using a signed widening multiplication and adjust the result.
1431 This adjustment works as follows:
1433 If both operands are positive then no adjustment is needed.
1435 If the operands have different signs, for example op0_low < 0 and
1436 op1_low >= 0, the instruction treats the most significant bit of
1437 op0_low as a sign bit instead of a bit with significance
1438 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1439 with 2**BITS_PER_WORD - op0_low, and two's complements the
1440 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1441 the result.
1443 Similarly, if both operands are negative, we need to add
1444 (op0_low + op1_low) * 2**BITS_PER_WORD.
1446 We use a trick to adjust quickly. We logically shift op0_low right
1447 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1448 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1449 logical shift exists, we do an arithmetic right shift and subtract
1450 the 0 or -1. */
1452 if (binoptab == smul_optab
1453 && class == MODE_INT
1454 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1455 && smul_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1456 && add_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1457 && ((umul_widen_optab->handlers[(int) mode].insn_code
1458 != CODE_FOR_nothing)
1459 || (smul_widen_optab->handlers[(int) mode].insn_code
1460 != CODE_FOR_nothing)))
1462 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1463 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1464 rtx op0_high = operand_subword_force (op0, high, mode);
1465 rtx op0_low = operand_subword_force (op0, low, mode);
1466 rtx op1_high = operand_subword_force (op1, high, mode);
1467 rtx op1_low = operand_subword_force (op1, low, mode);
1468 rtx product = 0;
1469 rtx op0_xhigh = NULL_RTX;
1470 rtx op1_xhigh = NULL_RTX;
1472 /* If the target is the same as one of the inputs, don't use it. This
1473 prevents problems with the REG_EQUAL note. */
1474 if (target == op0 || target == op1
1475 || (target != 0 && !REG_P (target)))
1476 target = 0;
1478 /* Multiply the two lower words to get a double-word product.
1479 If unsigned widening multiplication is available, use that;
1480 otherwise use the signed form and compensate. */
1482 if (umul_widen_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1484 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1485 target, 1, OPTAB_DIRECT);
1487 /* If we didn't succeed, delete everything we did so far. */
1488 if (product == 0)
1489 delete_insns_since (last);
1490 else
1491 op0_xhigh = op0_high, op1_xhigh = op1_high;
1494 if (product == 0
1495 && smul_widen_optab->handlers[(int) mode].insn_code
1496 != CODE_FOR_nothing)
1498 rtx wordm1 = GEN_INT (BITS_PER_WORD - 1);
1499 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1500 target, 1, OPTAB_DIRECT);
1501 op0_xhigh = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1502 NULL_RTX, 1, next_methods);
1503 if (op0_xhigh)
1504 op0_xhigh = expand_binop (word_mode, add_optab, op0_high,
1505 op0_xhigh, op0_xhigh, 0, next_methods);
1506 else
1508 op0_xhigh = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1509 NULL_RTX, 0, next_methods);
1510 if (op0_xhigh)
1511 op0_xhigh = expand_binop (word_mode, sub_optab, op0_high,
1512 op0_xhigh, op0_xhigh, 0,
1513 next_methods);
1516 op1_xhigh = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1517 NULL_RTX, 1, next_methods);
1518 if (op1_xhigh)
1519 op1_xhigh = expand_binop (word_mode, add_optab, op1_high,
1520 op1_xhigh, op1_xhigh, 0, next_methods);
1521 else
1523 op1_xhigh = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1524 NULL_RTX, 0, next_methods);
1525 if (op1_xhigh)
1526 op1_xhigh = expand_binop (word_mode, sub_optab, op1_high,
1527 op1_xhigh, op1_xhigh, 0,
1528 next_methods);
1532 /* If we have been able to directly compute the product of the
1533 low-order words of the operands and perform any required adjustments
1534 of the operands, we proceed by trying two more multiplications
1535 and then computing the appropriate sum.
1537 We have checked above that the required addition is provided.
1538 Full-word addition will normally always succeed, especially if
1539 it is provided at all, so we don't worry about its failure. The
1540 multiplication may well fail, however, so we do handle that. */
1542 if (product && op0_xhigh && op1_xhigh)
1544 rtx product_high = operand_subword (product, high, 1, mode);
1545 rtx temp = expand_binop (word_mode, binoptab, op0_low, op1_xhigh,
1546 NULL_RTX, 0, OPTAB_DIRECT);
1548 if (!REG_P (product_high))
1549 product_high = force_reg (word_mode, product_high);
1551 if (temp != 0)
1552 temp = expand_binop (word_mode, add_optab, temp, product_high,
1553 product_high, 0, next_methods);
1555 if (temp != 0 && temp != product_high)
1556 emit_move_insn (product_high, temp);
1558 if (temp != 0)
1559 temp = expand_binop (word_mode, binoptab, op1_low, op0_xhigh,
1560 NULL_RTX, 0, OPTAB_DIRECT);
1562 if (temp != 0)
1563 temp = expand_binop (word_mode, add_optab, temp,
1564 product_high, product_high,
1565 0, next_methods);
1567 if (temp != 0 && temp != product_high)
1568 emit_move_insn (product_high, temp);
1570 emit_move_insn (operand_subword (product, high, 1, mode), product_high);
1572 if (temp != 0)
1574 if (mov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1576 temp = emit_move_insn (product, product);
1577 set_unique_reg_note (temp,
1578 REG_EQUAL,
1579 gen_rtx_fmt_ee (MULT, mode,
1580 copy_rtx (op0),
1581 copy_rtx (op1)));
1584 return product;
1588 /* If we get here, we couldn't do it for some reason even though we
1589 originally thought we could. Delete anything we've emitted in
1590 trying to do it. */
1592 delete_insns_since (last);
1595 /* It can't be open-coded in this mode.
1596 Use a library call if one is available and caller says that's ok. */
1598 if (binoptab->handlers[(int) mode].libfunc
1599 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
1601 rtx insns;
1602 rtx op1x = op1;
1603 enum machine_mode op1_mode = mode;
1604 rtx value;
1606 start_sequence ();
1608 if (shift_op)
1610 op1_mode = word_mode;
1611 /* Specify unsigned here,
1612 since negative shift counts are meaningless. */
1613 op1x = convert_to_mode (word_mode, op1, 1);
1616 if (GET_MODE (op0) != VOIDmode
1617 && GET_MODE (op0) != mode)
1618 op0 = convert_to_mode (mode, op0, unsignedp);
1620 /* Pass 1 for NO_QUEUE so we don't lose any increments
1621 if the libcall is cse'd or moved. */
1622 value = emit_library_call_value (binoptab->handlers[(int) mode].libfunc,
1623 NULL_RTX, LCT_CONST, mode, 2,
1624 op0, mode, op1x, op1_mode);
1626 insns = get_insns ();
1627 end_sequence ();
1629 target = gen_reg_rtx (mode);
1630 emit_libcall_block (insns, target, value,
1631 gen_rtx_fmt_ee (binoptab->code, mode, op0, op1));
1633 return target;
1636 delete_insns_since (last);
1638 /* It can't be done in this mode. Can we do it in a wider mode? */
1640 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
1641 || methods == OPTAB_MUST_WIDEN))
1643 /* Caller says, don't even try. */
1644 delete_insns_since (entry_last);
1645 return 0;
1648 /* Compute the value of METHODS to pass to recursive calls.
1649 Don't allow widening to be tried recursively. */
1651 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
1653 /* Look for a wider mode of the same class for which it appears we can do
1654 the operation. */
1656 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1658 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1659 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1661 if ((binoptab->handlers[(int) wider_mode].insn_code
1662 != CODE_FOR_nothing)
1663 || (methods == OPTAB_LIB
1664 && binoptab->handlers[(int) wider_mode].libfunc))
1666 rtx xop0 = op0, xop1 = op1;
1667 int no_extend = 0;
1669 /* For certain integer operations, we need not actually extend
1670 the narrow operands, as long as we will truncate
1671 the results to the same narrowness. */
1673 if ((binoptab == ior_optab || binoptab == and_optab
1674 || binoptab == xor_optab
1675 || binoptab == add_optab || binoptab == sub_optab
1676 || binoptab == smul_optab || binoptab == ashl_optab)
1677 && class == MODE_INT)
1678 no_extend = 1;
1680 xop0 = widen_operand (xop0, wider_mode, mode,
1681 unsignedp, no_extend);
1683 /* The second operand of a shift must always be extended. */
1684 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1685 no_extend && binoptab != ashl_optab);
1687 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1688 unsignedp, methods);
1689 if (temp)
1691 if (class != MODE_INT)
1693 if (target == 0)
1694 target = gen_reg_rtx (mode);
1695 convert_move (target, temp, 0);
1696 return target;
1698 else
1699 return gen_lowpart (mode, temp);
1701 else
1702 delete_insns_since (last);
1707 delete_insns_since (entry_last);
1708 return 0;
1711 /* Expand a binary operator which has both signed and unsigned forms.
1712 UOPTAB is the optab for unsigned operations, and SOPTAB is for
1713 signed operations.
1715 If we widen unsigned operands, we may use a signed wider operation instead
1716 of an unsigned wider operation, since the result would be the same. */
1719 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
1720 rtx op0, rtx op1, rtx target, int unsignedp,
1721 enum optab_methods methods)
1723 rtx temp;
1724 optab direct_optab = unsignedp ? uoptab : soptab;
1725 struct optab wide_soptab;
1727 /* Do it without widening, if possible. */
1728 temp = expand_binop (mode, direct_optab, op0, op1, target,
1729 unsignedp, OPTAB_DIRECT);
1730 if (temp || methods == OPTAB_DIRECT)
1731 return temp;
1733 /* Try widening to a signed int. Make a fake signed optab that
1734 hides any signed insn for direct use. */
1735 wide_soptab = *soptab;
1736 wide_soptab.handlers[(int) mode].insn_code = CODE_FOR_nothing;
1737 wide_soptab.handlers[(int) mode].libfunc = 0;
1739 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
1740 unsignedp, OPTAB_WIDEN);
1742 /* For unsigned operands, try widening to an unsigned int. */
1743 if (temp == 0 && unsignedp)
1744 temp = expand_binop (mode, uoptab, op0, op1, target,
1745 unsignedp, OPTAB_WIDEN);
1746 if (temp || methods == OPTAB_WIDEN)
1747 return temp;
1749 /* Use the right width lib call if that exists. */
1750 temp = expand_binop (mode, direct_optab, op0, op1, target, unsignedp, OPTAB_LIB);
1751 if (temp || methods == OPTAB_LIB)
1752 return temp;
1754 /* Must widen and use a lib call, use either signed or unsigned. */
1755 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
1756 unsignedp, methods);
1757 if (temp != 0)
1758 return temp;
1759 if (unsignedp)
1760 return expand_binop (mode, uoptab, op0, op1, target,
1761 unsignedp, methods);
1762 return 0;
1765 /* Generate code to perform an operation specified by UNOPPTAB
1766 on operand OP0, with two results to TARG0 and TARG1.
1767 We assume that the order of the operands for the instruction
1768 is TARG0, TARG1, OP0.
1770 Either TARG0 or TARG1 may be zero, but what that means is that
1771 the result is not actually wanted. We will generate it into
1772 a dummy pseudo-reg and discard it. They may not both be zero.
1774 Returns 1 if this operation can be performed; 0 if not. */
1777 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
1778 int unsignedp)
1780 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1781 enum mode_class class;
1782 enum machine_mode wider_mode;
1783 rtx entry_last = get_last_insn ();
1784 rtx last;
1786 class = GET_MODE_CLASS (mode);
1788 if (flag_force_mem)
1789 op0 = force_not_mem (op0);
1791 if (!targ0)
1792 targ0 = gen_reg_rtx (mode);
1793 if (!targ1)
1794 targ1 = gen_reg_rtx (mode);
1796 /* Record where to go back to if we fail. */
1797 last = get_last_insn ();
1799 if (unoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1801 int icode = (int) unoptab->handlers[(int) mode].insn_code;
1802 enum machine_mode mode0 = insn_data[icode].operand[2].mode;
1803 rtx pat;
1804 rtx xop0 = op0;
1806 if (GET_MODE (xop0) != VOIDmode
1807 && GET_MODE (xop0) != mode0)
1808 xop0 = convert_to_mode (mode0, xop0, unsignedp);
1810 /* Now, if insn doesn't accept these operands, put them into pseudos. */
1811 if (! (*insn_data[icode].operand[2].predicate) (xop0, mode0))
1812 xop0 = copy_to_mode_reg (mode0, xop0);
1814 /* We could handle this, but we should always be called with a pseudo
1815 for our targets and all insns should take them as outputs. */
1816 if (! (*insn_data[icode].operand[0].predicate) (targ0, mode)
1817 || ! (*insn_data[icode].operand[1].predicate) (targ1, mode))
1818 abort ();
1820 pat = GEN_FCN (icode) (targ0, targ1, xop0);
1821 if (pat)
1823 emit_insn (pat);
1824 return 1;
1826 else
1827 delete_insns_since (last);
1830 /* It can't be done in this mode. Can we do it in a wider mode? */
1832 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1834 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1835 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1837 if (unoptab->handlers[(int) wider_mode].insn_code
1838 != CODE_FOR_nothing)
1840 rtx t0 = gen_reg_rtx (wider_mode);
1841 rtx t1 = gen_reg_rtx (wider_mode);
1842 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
1844 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
1846 convert_move (targ0, t0, unsignedp);
1847 convert_move (targ1, t1, unsignedp);
1848 return 1;
1850 else
1851 delete_insns_since (last);
1856 delete_insns_since (entry_last);
1857 return 0;
1860 /* Generate code to perform an operation specified by BINOPTAB
1861 on operands OP0 and OP1, with two results to TARG1 and TARG2.
1862 We assume that the order of the operands for the instruction
1863 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
1864 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
1866 Either TARG0 or TARG1 may be zero, but what that means is that
1867 the result is not actually wanted. We will generate it into
1868 a dummy pseudo-reg and discard it. They may not both be zero.
1870 Returns 1 if this operation can be performed; 0 if not. */
1873 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
1874 int unsignedp)
1876 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1877 enum mode_class class;
1878 enum machine_mode wider_mode;
1879 rtx entry_last = get_last_insn ();
1880 rtx last;
1882 class = GET_MODE_CLASS (mode);
1884 if (flag_force_mem)
1886 op0 = force_not_mem (op0);
1887 op1 = force_not_mem (op1);
1890 /* If we are inside an appropriately-short loop and we are optimizing,
1891 force expensive constants into a register. */
1892 if (CONSTANT_P (op0) && optimize
1893 && rtx_cost (op0, binoptab->code) > COSTS_N_INSNS (1))
1894 op0 = force_reg (mode, op0);
1896 if (CONSTANT_P (op1) && optimize
1897 && rtx_cost (op1, binoptab->code) > COSTS_N_INSNS (1))
1898 op1 = force_reg (mode, op1);
1900 if (!targ0)
1901 targ0 = gen_reg_rtx (mode);
1902 if (!targ1)
1903 targ1 = gen_reg_rtx (mode);
1905 /* Record where to go back to if we fail. */
1906 last = get_last_insn ();
1908 if (binoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1910 int icode = (int) binoptab->handlers[(int) mode].insn_code;
1911 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
1912 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
1913 rtx pat;
1914 rtx xop0 = op0, xop1 = op1;
1916 /* In case the insn wants input operands in modes different from
1917 those of the actual operands, convert the operands. It would
1918 seem that we don't need to convert CONST_INTs, but we do, so
1919 that they're properly zero-extended, sign-extended or truncated
1920 for their mode. */
1922 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
1923 xop0 = convert_modes (mode0,
1924 GET_MODE (op0) != VOIDmode
1925 ? GET_MODE (op0)
1926 : mode,
1927 xop0, unsignedp);
1929 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
1930 xop1 = convert_modes (mode1,
1931 GET_MODE (op1) != VOIDmode
1932 ? GET_MODE (op1)
1933 : mode,
1934 xop1, unsignedp);
1936 /* Now, if insn doesn't accept these operands, put them into pseudos. */
1937 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0))
1938 xop0 = copy_to_mode_reg (mode0, xop0);
1940 if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1))
1941 xop1 = copy_to_mode_reg (mode1, xop1);
1943 /* We could handle this, but we should always be called with a pseudo
1944 for our targets and all insns should take them as outputs. */
1945 if (! (*insn_data[icode].operand[0].predicate) (targ0, mode)
1946 || ! (*insn_data[icode].operand[3].predicate) (targ1, mode))
1947 abort ();
1949 pat = GEN_FCN (icode) (targ0, xop0, xop1, targ1);
1950 if (pat)
1952 emit_insn (pat);
1953 return 1;
1955 else
1956 delete_insns_since (last);
1959 /* It can't be done in this mode. Can we do it in a wider mode? */
1961 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1963 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1964 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1966 if (binoptab->handlers[(int) wider_mode].insn_code
1967 != CODE_FOR_nothing)
1969 rtx t0 = gen_reg_rtx (wider_mode);
1970 rtx t1 = gen_reg_rtx (wider_mode);
1971 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
1972 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
1974 if (expand_twoval_binop (binoptab, cop0, cop1,
1975 t0, t1, unsignedp))
1977 convert_move (targ0, t0, unsignedp);
1978 convert_move (targ1, t1, unsignedp);
1979 return 1;
1981 else
1982 delete_insns_since (last);
1987 delete_insns_since (entry_last);
1988 return 0;
1991 /* Expand the two-valued library call indicated by BINOPTAB, but
1992 preserve only one of the values. If TARG0 is non-NULL, the first
1993 value is placed into TARG0; otherwise the second value is placed
1994 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
1995 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
1996 This routine assumes that the value returned by the library call is
1997 as if the return value was of an integral mode twice as wide as the
1998 mode of OP0. Returns 1 if the call was successful. */
2000 bool
2001 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2002 rtx targ0, rtx targ1, enum rtx_code code)
2004 enum machine_mode mode;
2005 enum machine_mode libval_mode;
2006 rtx libval;
2007 rtx insns;
2009 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2010 if (!((targ0 != NULL_RTX) ^ (targ1 != NULL_RTX)))
2011 abort ();
2013 mode = GET_MODE (op0);
2014 if (!binoptab->handlers[(int) mode].libfunc)
2015 return false;
2017 /* The value returned by the library function will have twice as
2018 many bits as the nominal MODE. */
2019 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2020 MODE_INT);
2021 start_sequence ();
2022 libval = emit_library_call_value (binoptab->handlers[(int) mode].libfunc,
2023 NULL_RTX, LCT_CONST,
2024 libval_mode, 2,
2025 op0, mode,
2026 op1, mode);
2027 /* Get the part of VAL containing the value that we want. */
2028 libval = simplify_gen_subreg (mode, libval, libval_mode,
2029 targ0 ? 0 : GET_MODE_SIZE (mode));
2030 insns = get_insns ();
2031 end_sequence ();
2032 /* Move the into the desired location. */
2033 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2034 gen_rtx_fmt_ee (code, mode, op0, op1));
2036 return true;
2040 /* Wrapper around expand_unop which takes an rtx code to specify
2041 the operation to perform, not an optab pointer. All other
2042 arguments are the same. */
2044 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2045 rtx target, int unsignedp)
2047 optab unop = code_to_optab[(int) code];
2048 if (unop == 0)
2049 abort ();
2051 return expand_unop (mode, unop, op0, target, unsignedp);
2054 /* Try calculating
2055 (clz:narrow x)
2057 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)). */
2058 static rtx
2059 widen_clz (enum machine_mode mode, rtx op0, rtx target)
2061 enum mode_class class = GET_MODE_CLASS (mode);
2062 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2064 enum machine_mode wider_mode;
2065 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2066 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2068 if (clz_optab->handlers[(int) wider_mode].insn_code
2069 != CODE_FOR_nothing)
2071 rtx xop0, temp, last;
2073 last = get_last_insn ();
2075 if (target == 0)
2076 target = gen_reg_rtx (mode);
2077 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2078 temp = expand_unop (wider_mode, clz_optab, xop0, NULL_RTX, true);
2079 if (temp != 0)
2080 temp = expand_binop (wider_mode, sub_optab, temp,
2081 GEN_INT (GET_MODE_BITSIZE (wider_mode)
2082 - GET_MODE_BITSIZE (mode)),
2083 target, true, OPTAB_DIRECT);
2084 if (temp == 0)
2085 delete_insns_since (last);
2087 return temp;
2091 return 0;
2094 /* Try calculating (parity x) as (and (popcount x) 1), where
2095 popcount can also be done in a wider mode. */
2096 static rtx
2097 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2099 enum mode_class class = GET_MODE_CLASS (mode);
2100 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2102 enum machine_mode wider_mode;
2103 for (wider_mode = mode; wider_mode != VOIDmode;
2104 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2106 if (popcount_optab->handlers[(int) wider_mode].insn_code
2107 != CODE_FOR_nothing)
2109 rtx xop0, temp, last;
2111 last = get_last_insn ();
2113 if (target == 0)
2114 target = gen_reg_rtx (mode);
2115 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2116 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2117 true);
2118 if (temp != 0)
2119 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2120 target, true, OPTAB_DIRECT);
2121 if (temp == 0)
2122 delete_insns_since (last);
2124 return temp;
2128 return 0;
2131 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2132 conditions, VAL may already be a SUBREG against which we cannot generate
2133 a further SUBREG. In this case, we expect forcing the value into a
2134 register will work around the situation. */
2136 static rtx
2137 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2138 enum machine_mode imode)
2140 rtx ret;
2141 ret = lowpart_subreg (omode, val, imode);
2142 if (ret == NULL)
2144 val = force_reg (imode, val);
2145 ret = lowpart_subreg (omode, val, imode);
2146 gcc_assert (ret != NULL);
2148 return ret;
2151 /* Generate code to perform an operation specified by UNOPTAB
2152 on operand OP0, with result having machine-mode MODE.
2154 UNSIGNEDP is for the case where we have to widen the operands
2155 to perform the operation. It says to use zero-extension.
2157 If TARGET is nonzero, the value
2158 is generated there, if it is convenient to do so.
2159 In all cases an rtx is returned for the locus of the value;
2160 this may or may not be TARGET. */
2163 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2164 int unsignedp)
2166 enum mode_class class;
2167 enum machine_mode wider_mode;
2168 rtx temp;
2169 rtx last = get_last_insn ();
2170 rtx pat;
2172 class = GET_MODE_CLASS (mode);
2174 if (flag_force_mem)
2175 op0 = force_not_mem (op0);
2177 if (unoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2179 int icode = (int) unoptab->handlers[(int) mode].insn_code;
2180 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2181 rtx xop0 = op0;
2183 if (target)
2184 temp = target;
2185 else
2186 temp = gen_reg_rtx (mode);
2188 if (GET_MODE (xop0) != VOIDmode
2189 && GET_MODE (xop0) != mode0)
2190 xop0 = convert_to_mode (mode0, xop0, unsignedp);
2192 /* Now, if insn doesn't accept our operand, put it into a pseudo. */
2194 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0))
2195 xop0 = copy_to_mode_reg (mode0, xop0);
2197 if (! (*insn_data[icode].operand[0].predicate) (temp, mode))
2198 temp = gen_reg_rtx (mode);
2200 pat = GEN_FCN (icode) (temp, xop0);
2201 if (pat)
2203 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2204 && ! add_equal_note (pat, temp, unoptab->code, xop0, NULL_RTX))
2206 delete_insns_since (last);
2207 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
2210 emit_insn (pat);
2212 return temp;
2214 else
2215 delete_insns_since (last);
2218 /* It can't be done in this mode. Can we open-code it in a wider mode? */
2220 /* Widening clz needs special treatment. */
2221 if (unoptab == clz_optab)
2223 temp = widen_clz (mode, op0, target);
2224 if (temp)
2225 return temp;
2226 else
2227 goto try_libcall;
2230 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2231 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2232 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2234 if (unoptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing)
2236 rtx xop0 = op0;
2238 /* For certain operations, we need not actually extend
2239 the narrow operand, as long as we will truncate the
2240 results to the same narrowness. */
2242 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2243 (unoptab == neg_optab
2244 || unoptab == one_cmpl_optab)
2245 && class == MODE_INT);
2247 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2248 unsignedp);
2250 if (temp)
2252 if (class != MODE_INT)
2254 if (target == 0)
2255 target = gen_reg_rtx (mode);
2256 convert_move (target, temp, 0);
2257 return target;
2259 else
2260 return gen_lowpart (mode, temp);
2262 else
2263 delete_insns_since (last);
2267 /* These can be done a word at a time. */
2268 if (unoptab == one_cmpl_optab
2269 && class == MODE_INT
2270 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
2271 && unoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
2273 int i;
2274 rtx insns;
2276 if (target == 0 || target == op0)
2277 target = gen_reg_rtx (mode);
2279 start_sequence ();
2281 /* Do the actual arithmetic. */
2282 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
2284 rtx target_piece = operand_subword (target, i, 1, mode);
2285 rtx x = expand_unop (word_mode, unoptab,
2286 operand_subword_force (op0, i, mode),
2287 target_piece, unsignedp);
2289 if (target_piece != x)
2290 emit_move_insn (target_piece, x);
2293 insns = get_insns ();
2294 end_sequence ();
2296 emit_no_conflict_block (insns, target, op0, NULL_RTX,
2297 gen_rtx_fmt_e (unoptab->code, mode,
2298 copy_rtx (op0)));
2299 return target;
2302 /* Try negating floating point values by flipping the sign bit. */
2303 if (unoptab->code == NEG && class == MODE_FLOAT
2304 && GET_MODE_BITSIZE (mode) <= 2 * HOST_BITS_PER_WIDE_INT)
2306 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
2307 enum machine_mode imode = int_mode_for_mode (mode);
2308 int bitpos = (fmt != 0) ? fmt->signbit : -1;
2310 if (imode != BLKmode && bitpos >= 0 && fmt->has_signed_zero)
2312 HOST_WIDE_INT hi, lo;
2313 rtx last = get_last_insn ();
2315 /* Handle targets with different FP word orders. */
2316 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2318 int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
2319 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
2320 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
2323 if (bitpos < HOST_BITS_PER_WIDE_INT)
2325 hi = 0;
2326 lo = (HOST_WIDE_INT) 1 << bitpos;
2328 else
2330 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
2331 lo = 0;
2333 temp = expand_binop (imode, xor_optab,
2334 gen_lowpart (imode, op0),
2335 immed_double_const (lo, hi, imode),
2336 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2337 if (temp != 0)
2339 rtx insn;
2340 if (target == 0)
2341 target = gen_reg_rtx (mode);
2342 temp = lowpart_subreg_maybe_copy (mode, temp, imode);
2343 insn = emit_move_insn (target, temp);
2344 set_unique_reg_note (insn, REG_EQUAL,
2345 gen_rtx_fmt_e (NEG, mode,
2346 copy_rtx (op0)));
2347 return target;
2349 delete_insns_since (last);
2353 /* Try calculating parity (x) as popcount (x) % 2. */
2354 if (unoptab == parity_optab)
2356 temp = expand_parity (mode, op0, target);
2357 if (temp)
2358 return temp;
2361 /* If there is no negation pattern, try subtracting from zero. */
2362 if (unoptab == neg_optab && class == MODE_INT)
2364 temp = expand_binop (mode, sub_optab, CONST0_RTX (mode), op0,
2365 target, unsignedp, OPTAB_DIRECT);
2366 if (temp)
2367 return temp;
2370 try_libcall:
2371 /* Now try a library call in this mode. */
2372 if (unoptab->handlers[(int) mode].libfunc)
2374 rtx insns;
2375 rtx value;
2376 enum machine_mode outmode = mode;
2378 /* All of these functions return small values. Thus we choose to
2379 have them return something that isn't a double-word. */
2380 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
2381 || unoptab == popcount_optab || unoptab == parity_optab)
2382 outmode
2383 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node)));
2385 start_sequence ();
2387 /* Pass 1 for NO_QUEUE so we don't lose any increments
2388 if the libcall is cse'd or moved. */
2389 value = emit_library_call_value (unoptab->handlers[(int) mode].libfunc,
2390 NULL_RTX, LCT_CONST, outmode,
2391 1, op0, mode);
2392 insns = get_insns ();
2393 end_sequence ();
2395 target = gen_reg_rtx (outmode);
2396 emit_libcall_block (insns, target, value,
2397 gen_rtx_fmt_e (unoptab->code, mode, op0));
2399 return target;
2402 /* It can't be done in this mode. Can we do it in a wider mode? */
2404 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
2406 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
2407 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2409 if ((unoptab->handlers[(int) wider_mode].insn_code
2410 != CODE_FOR_nothing)
2411 || unoptab->handlers[(int) wider_mode].libfunc)
2413 rtx xop0 = op0;
2415 /* For certain operations, we need not actually extend
2416 the narrow operand, as long as we will truncate the
2417 results to the same narrowness. */
2419 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2420 (unoptab == neg_optab
2421 || unoptab == one_cmpl_optab)
2422 && class == MODE_INT);
2424 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2425 unsignedp);
2427 /* If we are generating clz using wider mode, adjust the
2428 result. */
2429 if (unoptab == clz_optab && temp != 0)
2430 temp = expand_binop (wider_mode, sub_optab, temp,
2431 GEN_INT (GET_MODE_BITSIZE (wider_mode)
2432 - GET_MODE_BITSIZE (mode)),
2433 target, true, OPTAB_DIRECT);
2435 if (temp)
2437 if (class != MODE_INT)
2439 if (target == 0)
2440 target = gen_reg_rtx (mode);
2441 convert_move (target, temp, 0);
2442 return target;
2444 else
2445 return gen_lowpart (mode, temp);
2447 else
2448 delete_insns_since (last);
2453 /* If there is no negate operation, try doing a subtract from zero.
2454 The US Software GOFAST library needs this. FIXME: This is *wrong*
2455 for floating-point operations due to negative zeros! */
2456 if (unoptab->code == NEG)
2458 rtx temp;
2459 temp = expand_binop (mode,
2460 unoptab == negv_optab ? subv_optab : sub_optab,
2461 CONST0_RTX (mode), op0,
2462 target, unsignedp, OPTAB_LIB_WIDEN);
2463 if (temp)
2464 return temp;
2467 return 0;
2470 /* Emit code to compute the absolute value of OP0, with result to
2471 TARGET if convenient. (TARGET may be 0.) The return value says
2472 where the result actually is to be found.
2474 MODE is the mode of the operand; the mode of the result is
2475 different but can be deduced from MODE.
2480 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
2481 int result_unsignedp)
2483 rtx temp;
2485 if (! flag_trapv)
2486 result_unsignedp = 1;
2488 /* First try to do it with a special abs instruction. */
2489 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
2490 op0, target, 0);
2491 if (temp != 0)
2492 return temp;
2494 /* For floating point modes, try clearing the sign bit. */
2495 if (GET_MODE_CLASS (mode) == MODE_FLOAT
2496 && GET_MODE_BITSIZE (mode) <= 2 * HOST_BITS_PER_WIDE_INT)
2498 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
2499 enum machine_mode imode = int_mode_for_mode (mode);
2500 int bitpos = (fmt != 0) ? fmt->signbit : -1;
2502 if (imode != BLKmode && bitpos >= 0)
2504 HOST_WIDE_INT hi, lo;
2505 rtx last = get_last_insn ();
2507 /* Handle targets with different FP word orders. */
2508 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2510 int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
2511 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
2512 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
2515 if (bitpos < HOST_BITS_PER_WIDE_INT)
2517 hi = 0;
2518 lo = (HOST_WIDE_INT) 1 << bitpos;
2520 else
2522 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
2523 lo = 0;
2525 temp = expand_binop (imode, and_optab,
2526 gen_lowpart (imode, op0),
2527 immed_double_const (~lo, ~hi, imode),
2528 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2529 if (temp != 0)
2531 rtx insn;
2532 if (target == 0)
2533 target = gen_reg_rtx (mode);
2534 temp = lowpart_subreg_maybe_copy (mode, temp, imode);
2535 insn = emit_move_insn (target, temp);
2536 set_unique_reg_note (insn, REG_EQUAL,
2537 gen_rtx_fmt_e (ABS, mode,
2538 copy_rtx (op0)));
2539 return target;
2541 delete_insns_since (last);
2545 /* If we have a MAX insn, we can do this as MAX (x, -x). */
2546 if (smax_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2548 rtx last = get_last_insn ();
2550 temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
2551 if (temp != 0)
2552 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
2553 OPTAB_WIDEN);
2555 if (temp != 0)
2556 return temp;
2558 delete_insns_since (last);
2561 /* If this machine has expensive jumps, we can do integer absolute
2562 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
2563 where W is the width of MODE. */
2565 if (GET_MODE_CLASS (mode) == MODE_INT && BRANCH_COST >= 2)
2567 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
2568 size_int (GET_MODE_BITSIZE (mode) - 1),
2569 NULL_RTX, 0);
2571 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
2572 OPTAB_LIB_WIDEN);
2573 if (temp != 0)
2574 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
2575 temp, extended, target, 0, OPTAB_LIB_WIDEN);
2577 if (temp != 0)
2578 return temp;
2581 return NULL_RTX;
2585 expand_abs (enum machine_mode mode, rtx op0, rtx target,
2586 int result_unsignedp, int safe)
2588 rtx temp, op1;
2590 if (! flag_trapv)
2591 result_unsignedp = 1;
2593 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
2594 if (temp != 0)
2595 return temp;
2597 /* If that does not win, use conditional jump and negate. */
2599 /* It is safe to use the target if it is the same
2600 as the source if this is also a pseudo register */
2601 if (op0 == target && REG_P (op0)
2602 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
2603 safe = 1;
2605 op1 = gen_label_rtx ();
2606 if (target == 0 || ! safe
2607 || GET_MODE (target) != mode
2608 || (MEM_P (target) && MEM_VOLATILE_P (target))
2609 || (REG_P (target)
2610 && REGNO (target) < FIRST_PSEUDO_REGISTER))
2611 target = gen_reg_rtx (mode);
2613 emit_move_insn (target, op0);
2614 NO_DEFER_POP;
2616 /* If this mode is an integer too wide to compare properly,
2617 compare word by word. Rely on CSE to optimize constant cases. */
2618 if (GET_MODE_CLASS (mode) == MODE_INT
2619 && ! can_compare_p (GE, mode, ccp_jump))
2620 do_jump_by_parts_greater_rtx (mode, 0, target, const0_rtx,
2621 NULL_RTX, op1);
2622 else
2623 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
2624 NULL_RTX, NULL_RTX, op1);
2626 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
2627 target, target, 0);
2628 if (op0 != target)
2629 emit_move_insn (target, op0);
2630 emit_label (op1);
2631 OK_DEFER_POP;
2632 return target;
2635 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
2636 scalar floating point mode. Return NULL if we do not know how to
2637 expand the operation inline. */
2640 expand_copysign (rtx op0, rtx op1, rtx target)
2642 enum machine_mode mode = GET_MODE (op0);
2643 const struct real_format *fmt;
2644 enum machine_mode imode;
2645 int bitpos;
2646 HOST_WIDE_INT hi, lo;
2647 rtx last, temp;
2649 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
2650 gcc_assert (GET_MODE (op1) == mode);
2652 /* First try to do it with a special instruction. */
2653 temp = expand_binop (mode, copysign_optab, op0, op1,
2654 target, 0, OPTAB_DIRECT);
2655 if (temp)
2656 return temp;
2658 /* Otherwise, use bit operations to move the sign from one to the other. */
2659 if (GET_MODE_BITSIZE (mode) > 2 * HOST_BITS_PER_WIDE_INT)
2660 return NULL_RTX;
2662 imode = int_mode_for_mode (mode);
2663 if (imode == BLKmode)
2664 return NULL_RTX;
2666 fmt = REAL_MODE_FORMAT (mode);
2667 bitpos = (fmt != 0) ? fmt->signbit : -1;
2668 if (bitpos < 0)
2669 return NULL_RTX;
2671 last = get_last_insn ();
2673 /* Handle targets with different FP word orders. */
2674 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2676 int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
2677 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
2678 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
2681 if (bitpos < HOST_BITS_PER_WIDE_INT)
2683 hi = 0;
2684 lo = (HOST_WIDE_INT) 1 << bitpos;
2686 else
2688 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
2689 lo = 0;
2692 op0 = expand_binop (imode, and_optab,
2693 gen_lowpart (imode, op0),
2694 immed_double_const (~lo, ~hi, imode),
2695 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2696 op1 = expand_binop (imode, and_optab,
2697 gen_lowpart (imode, op1),
2698 immed_double_const (lo, hi, imode),
2699 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2700 if (op0 && op1)
2702 target = expand_binop (imode, ior_optab, op0, op1, NULL,
2703 1, OPTAB_LIB_WIDEN);
2704 if (target)
2706 target = force_reg (imode, target);
2707 target = gen_lowpart (mode, target);
2710 else
2711 target = NULL_RTX;
2713 if (!target)
2714 delete_insns_since (last);
2716 return target;
2719 /* Generate an instruction whose insn-code is INSN_CODE,
2720 with two operands: an output TARGET and an input OP0.
2721 TARGET *must* be nonzero, and the output is always stored there.
2722 CODE is an rtx code such that (CODE OP0) is an rtx that describes
2723 the value that is stored into TARGET. */
2725 void
2726 emit_unop_insn (int icode, rtx target, rtx op0, enum rtx_code code)
2728 rtx temp;
2729 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2730 rtx pat;
2732 temp = target;
2734 /* Sign and zero extension from memory is often done specially on
2735 RISC machines, so forcing into a register here can pessimize
2736 code. */
2737 if (flag_force_mem && code != SIGN_EXTEND && code != ZERO_EXTEND)
2738 op0 = force_not_mem (op0);
2740 /* Now, if insn does not accept our operands, put them into pseudos. */
2742 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
2743 op0 = copy_to_mode_reg (mode0, op0);
2745 if (! (*insn_data[icode].operand[0].predicate) (temp, GET_MODE (temp))
2746 || (flag_force_mem && MEM_P (temp)))
2747 temp = gen_reg_rtx (GET_MODE (temp));
2749 pat = GEN_FCN (icode) (temp, op0);
2751 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
2752 add_equal_note (pat, temp, code, op0, NULL_RTX);
2754 emit_insn (pat);
2756 if (temp != target)
2757 emit_move_insn (target, temp);
2760 /* Emit code to perform a series of operations on a multi-word quantity, one
2761 word at a time.
2763 Such a block is preceded by a CLOBBER of the output, consists of multiple
2764 insns, each setting one word of the output, and followed by a SET copying
2765 the output to itself.
2767 Each of the insns setting words of the output receives a REG_NO_CONFLICT
2768 note indicating that it doesn't conflict with the (also multi-word)
2769 inputs. The entire block is surrounded by REG_LIBCALL and REG_RETVAL
2770 notes.
2772 INSNS is a block of code generated to perform the operation, not including
2773 the CLOBBER and final copy. All insns that compute intermediate values
2774 are first emitted, followed by the block as described above.
2776 TARGET, OP0, and OP1 are the output and inputs of the operations,
2777 respectively. OP1 may be zero for a unary operation.
2779 EQUIV, if nonzero, is an expression to be placed into a REG_EQUAL note
2780 on the last insn.
2782 If TARGET is not a register, INSNS is simply emitted with no special
2783 processing. Likewise if anything in INSNS is not an INSN or if
2784 there is a libcall block inside INSNS.
2786 The final insn emitted is returned. */
2789 emit_no_conflict_block (rtx insns, rtx target, rtx op0, rtx op1, rtx equiv)
2791 rtx prev, next, first, last, insn;
2793 if (!REG_P (target) || reload_in_progress)
2794 return emit_insn (insns);
2795 else
2796 for (insn = insns; insn; insn = NEXT_INSN (insn))
2797 if (!NONJUMP_INSN_P (insn)
2798 || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
2799 return emit_insn (insns);
2801 /* First emit all insns that do not store into words of the output and remove
2802 these from the list. */
2803 for (insn = insns; insn; insn = next)
2805 rtx set = 0, note;
2806 int i;
2808 next = NEXT_INSN (insn);
2810 /* Some ports (cris) create a libcall regions at their own. We must
2811 avoid any potential nesting of LIBCALLs. */
2812 if ((note = find_reg_note (insn, REG_LIBCALL, NULL)) != NULL)
2813 remove_note (insn, note);
2814 if ((note = find_reg_note (insn, REG_RETVAL, NULL)) != NULL)
2815 remove_note (insn, note);
2817 if (GET_CODE (PATTERN (insn)) == SET || GET_CODE (PATTERN (insn)) == USE
2818 || GET_CODE (PATTERN (insn)) == CLOBBER)
2819 set = PATTERN (insn);
2820 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2822 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2823 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
2825 set = XVECEXP (PATTERN (insn), 0, i);
2826 break;
2830 if (set == 0)
2831 abort ();
2833 if (! reg_overlap_mentioned_p (target, SET_DEST (set)))
2835 if (PREV_INSN (insn))
2836 NEXT_INSN (PREV_INSN (insn)) = next;
2837 else
2838 insns = next;
2840 if (next)
2841 PREV_INSN (next) = PREV_INSN (insn);
2843 add_insn (insn);
2847 prev = get_last_insn ();
2849 /* Now write the CLOBBER of the output, followed by the setting of each
2850 of the words, followed by the final copy. */
2851 if (target != op0 && target != op1)
2852 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
2854 for (insn = insns; insn; insn = next)
2856 next = NEXT_INSN (insn);
2857 add_insn (insn);
2859 if (op1 && REG_P (op1))
2860 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_NO_CONFLICT, op1,
2861 REG_NOTES (insn));
2863 if (op0 && REG_P (op0))
2864 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_NO_CONFLICT, op0,
2865 REG_NOTES (insn));
2868 if (mov_optab->handlers[(int) GET_MODE (target)].insn_code
2869 != CODE_FOR_nothing)
2871 last = emit_move_insn (target, target);
2872 if (equiv)
2873 set_unique_reg_note (last, REG_EQUAL, equiv);
2875 else
2877 last = get_last_insn ();
2879 /* Remove any existing REG_EQUAL note from "last", or else it will
2880 be mistaken for a note referring to the full contents of the
2881 alleged libcall value when found together with the REG_RETVAL
2882 note added below. An existing note can come from an insn
2883 expansion at "last". */
2884 remove_note (last, find_reg_note (last, REG_EQUAL, NULL_RTX));
2887 if (prev == 0)
2888 first = get_insns ();
2889 else
2890 first = NEXT_INSN (prev);
2892 /* Encapsulate the block so it gets manipulated as a unit. */
2893 REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
2894 REG_NOTES (first));
2895 REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
2897 return last;
2900 /* Emit code to make a call to a constant function or a library call.
2902 INSNS is a list containing all insns emitted in the call.
2903 These insns leave the result in RESULT. Our block is to copy RESULT
2904 to TARGET, which is logically equivalent to EQUIV.
2906 We first emit any insns that set a pseudo on the assumption that these are
2907 loading constants into registers; doing so allows them to be safely cse'ed
2908 between blocks. Then we emit all the other insns in the block, followed by
2909 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
2910 note with an operand of EQUIV.
2912 Moving assignments to pseudos outside of the block is done to improve
2913 the generated code, but is not required to generate correct code,
2914 hence being unable to move an assignment is not grounds for not making
2915 a libcall block. There are two reasons why it is safe to leave these
2916 insns inside the block: First, we know that these pseudos cannot be
2917 used in generated RTL outside the block since they are created for
2918 temporary purposes within the block. Second, CSE will not record the
2919 values of anything set inside a libcall block, so we know they must
2920 be dead at the end of the block.
2922 Except for the first group of insns (the ones setting pseudos), the
2923 block is delimited by REG_RETVAL and REG_LIBCALL notes. */
2925 void
2926 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
2928 rtx final_dest = target;
2929 rtx prev, next, first, last, insn;
2931 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
2932 into a MEM later. Protect the libcall block from this change. */
2933 if (! REG_P (target) || REG_USERVAR_P (target))
2934 target = gen_reg_rtx (GET_MODE (target));
2936 /* If we're using non-call exceptions, a libcall corresponding to an
2937 operation that may trap may also trap. */
2938 if (flag_non_call_exceptions && may_trap_p (equiv))
2940 for (insn = insns; insn; insn = NEXT_INSN (insn))
2941 if (CALL_P (insn))
2943 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2945 if (note != 0 && INTVAL (XEXP (note, 0)) <= 0)
2946 remove_note (insn, note);
2949 else
2950 /* look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
2951 reg note to indicate that this call cannot throw or execute a nonlocal
2952 goto (unless there is already a REG_EH_REGION note, in which case
2953 we update it). */
2954 for (insn = insns; insn; insn = NEXT_INSN (insn))
2955 if (CALL_P (insn))
2957 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2959 if (note != 0)
2960 XEXP (note, 0) = constm1_rtx;
2961 else
2962 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, constm1_rtx,
2963 REG_NOTES (insn));
2966 /* First emit all insns that set pseudos. Remove them from the list as
2967 we go. Avoid insns that set pseudos which were referenced in previous
2968 insns. These can be generated by move_by_pieces, for example,
2969 to update an address. Similarly, avoid insns that reference things
2970 set in previous insns. */
2972 for (insn = insns; insn; insn = next)
2974 rtx set = single_set (insn);
2975 rtx note;
2977 /* Some ports (cris) create a libcall regions at their own. We must
2978 avoid any potential nesting of LIBCALLs. */
2979 if ((note = find_reg_note (insn, REG_LIBCALL, NULL)) != NULL)
2980 remove_note (insn, note);
2981 if ((note = find_reg_note (insn, REG_RETVAL, NULL)) != NULL)
2982 remove_note (insn, note);
2984 next = NEXT_INSN (insn);
2986 if (set != 0 && REG_P (SET_DEST (set))
2987 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
2988 && (insn == insns
2989 || ((! INSN_P(insns)
2990 || ! reg_mentioned_p (SET_DEST (set), PATTERN (insns)))
2991 && ! reg_used_between_p (SET_DEST (set), insns, insn)
2992 && ! modified_in_p (SET_SRC (set), insns)
2993 && ! modified_between_p (SET_SRC (set), insns, insn))))
2995 if (PREV_INSN (insn))
2996 NEXT_INSN (PREV_INSN (insn)) = next;
2997 else
2998 insns = next;
3000 if (next)
3001 PREV_INSN (next) = PREV_INSN (insn);
3003 add_insn (insn);
3006 /* Some ports use a loop to copy large arguments onto the stack.
3007 Don't move anything outside such a loop. */
3008 if (LABEL_P (insn))
3009 break;
3012 prev = get_last_insn ();
3014 /* Write the remaining insns followed by the final copy. */
3016 for (insn = insns; insn; insn = next)
3018 next = NEXT_INSN (insn);
3020 add_insn (insn);
3023 last = emit_move_insn (target, result);
3024 if (mov_optab->handlers[(int) GET_MODE (target)].insn_code
3025 != CODE_FOR_nothing)
3026 set_unique_reg_note (last, REG_EQUAL, copy_rtx (equiv));
3027 else
3029 /* Remove any existing REG_EQUAL note from "last", or else it will
3030 be mistaken for a note referring to the full contents of the
3031 libcall value when found together with the REG_RETVAL note added
3032 below. An existing note can come from an insn expansion at
3033 "last". */
3034 remove_note (last, find_reg_note (last, REG_EQUAL, NULL_RTX));
3037 if (final_dest != target)
3038 emit_move_insn (final_dest, target);
3040 if (prev == 0)
3041 first = get_insns ();
3042 else
3043 first = NEXT_INSN (prev);
3045 /* Encapsulate the block so it gets manipulated as a unit. */
3046 if (!flag_non_call_exceptions || !may_trap_p (equiv))
3048 /* We can't attach the REG_LIBCALL and REG_RETVAL notes
3049 when the encapsulated region would not be in one basic block,
3050 i.e. when there is a control_flow_insn_p insn between FIRST and LAST.
3052 bool attach_libcall_retval_notes = true;
3053 next = NEXT_INSN (last);
3054 for (insn = first; insn != next; insn = NEXT_INSN (insn))
3055 if (control_flow_insn_p (insn))
3057 attach_libcall_retval_notes = false;
3058 break;
3061 if (attach_libcall_retval_notes)
3063 REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
3064 REG_NOTES (first));
3065 REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first,
3066 REG_NOTES (last));
3071 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3072 PURPOSE describes how this comparison will be used. CODE is the rtx
3073 comparison code we will be using.
3075 ??? Actually, CODE is slightly weaker than that. A target is still
3076 required to implement all of the normal bcc operations, but not
3077 required to implement all (or any) of the unordered bcc operations. */
3080 can_compare_p (enum rtx_code code, enum machine_mode mode,
3081 enum can_compare_purpose purpose)
3085 if (cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3087 if (purpose == ccp_jump)
3088 return bcc_gen_fctn[(int) code] != NULL;
3089 else if (purpose == ccp_store_flag)
3090 return setcc_gen_code[(int) code] != CODE_FOR_nothing;
3091 else
3092 /* There's only one cmov entry point, and it's allowed to fail. */
3093 return 1;
3095 if (purpose == ccp_jump
3096 && cbranch_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3097 return 1;
3098 if (purpose == ccp_cmov
3099 && cmov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3100 return 1;
3101 if (purpose == ccp_store_flag
3102 && cstore_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
3103 return 1;
3104 mode = GET_MODE_WIDER_MODE (mode);
3106 while (mode != VOIDmode);
3108 return 0;
3111 /* This function is called when we are going to emit a compare instruction that
3112 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
3114 *PMODE is the mode of the inputs (in case they are const_int).
3115 *PUNSIGNEDP nonzero says that the operands are unsigned;
3116 this matters if they need to be widened.
3118 If they have mode BLKmode, then SIZE specifies the size of both operands.
3120 This function performs all the setup necessary so that the caller only has
3121 to emit a single comparison insn. This setup can involve doing a BLKmode
3122 comparison or emitting a library call to perform the comparison if no insn
3123 is available to handle it.
3124 The values which are passed in through pointers can be modified; the caller
3125 should perform the comparison on the modified values. */
3127 static void
3128 prepare_cmp_insn (rtx *px, rtx *py, enum rtx_code *pcomparison, rtx size,
3129 enum machine_mode *pmode, int *punsignedp,
3130 enum can_compare_purpose purpose)
3132 enum machine_mode mode = *pmode;
3133 rtx x = *px, y = *py;
3134 int unsignedp = *punsignedp;
3135 enum mode_class class;
3137 class = GET_MODE_CLASS (mode);
3139 /* They could both be VOIDmode if both args are immediate constants,
3140 but we should fold that at an earlier stage.
3141 With no special code here, this will call abort,
3142 reminding the programmer to implement such folding. */
3144 if (mode != BLKmode && flag_force_mem)
3146 /* Load duplicate non-volatile operands once. */
3147 if (rtx_equal_p (x, y) && ! volatile_refs_p (x))
3149 x = force_not_mem (x);
3150 y = x;
3152 else
3154 x = force_not_mem (x);
3155 y = force_not_mem (y);
3159 /* If we are inside an appropriately-short loop and we are optimizing,
3160 force expensive constants into a register. */
3161 if (CONSTANT_P (x) && optimize
3162 && rtx_cost (x, COMPARE) > COSTS_N_INSNS (1))
3163 x = force_reg (mode, x);
3165 if (CONSTANT_P (y) && optimize
3166 && rtx_cost (y, COMPARE) > COSTS_N_INSNS (1))
3167 y = force_reg (mode, y);
3169 #ifdef HAVE_cc0
3170 /* Abort if we have a non-canonical comparison. The RTL documentation
3171 states that canonical comparisons are required only for targets which
3172 have cc0. */
3173 if (CONSTANT_P (x) && ! CONSTANT_P (y))
3174 abort ();
3175 #endif
3177 /* Don't let both operands fail to indicate the mode. */
3178 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
3179 x = force_reg (mode, x);
3181 /* Handle all BLKmode compares. */
3183 if (mode == BLKmode)
3185 enum machine_mode cmp_mode, result_mode;
3186 enum insn_code cmp_code;
3187 tree length_type;
3188 rtx libfunc;
3189 rtx result;
3190 rtx opalign
3191 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
3193 if (size == 0)
3194 abort ();
3196 /* Try to use a memory block compare insn - either cmpstr
3197 or cmpmem will do. */
3198 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3199 cmp_mode != VOIDmode;
3200 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
3202 cmp_code = cmpmem_optab[cmp_mode];
3203 if (cmp_code == CODE_FOR_nothing)
3204 cmp_code = cmpstr_optab[cmp_mode];
3205 if (cmp_code == CODE_FOR_nothing)
3206 continue;
3208 /* Must make sure the size fits the insn's mode. */
3209 if ((GET_CODE (size) == CONST_INT
3210 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
3211 || (GET_MODE_BITSIZE (GET_MODE (size))
3212 > GET_MODE_BITSIZE (cmp_mode)))
3213 continue;
3215 result_mode = insn_data[cmp_code].operand[0].mode;
3216 result = gen_reg_rtx (result_mode);
3217 size = convert_to_mode (cmp_mode, size, 1);
3218 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
3220 *px = result;
3221 *py = const0_rtx;
3222 *pmode = result_mode;
3223 return;
3226 /* Otherwise call a library function, memcmp. */
3227 libfunc = memcmp_libfunc;
3228 length_type = sizetype;
3229 result_mode = TYPE_MODE (integer_type_node);
3230 cmp_mode = TYPE_MODE (length_type);
3231 size = convert_to_mode (TYPE_MODE (length_type), size,
3232 TYPE_UNSIGNED (length_type));
3234 result = emit_library_call_value (libfunc, 0, LCT_PURE_MAKE_BLOCK,
3235 result_mode, 3,
3236 XEXP (x, 0), Pmode,
3237 XEXP (y, 0), Pmode,
3238 size, cmp_mode);
3239 *px = result;
3240 *py = const0_rtx;
3241 *pmode = result_mode;
3242 return;
3245 /* Don't allow operands to the compare to trap, as that can put the
3246 compare and branch in different basic blocks. */
3247 if (flag_non_call_exceptions)
3249 if (may_trap_p (x))
3250 x = force_reg (mode, x);
3251 if (may_trap_p (y))
3252 y = force_reg (mode, y);
3255 *px = x;
3256 *py = y;
3257 if (can_compare_p (*pcomparison, mode, purpose))
3258 return;
3260 /* Handle a lib call just for the mode we are using. */
3262 if (cmp_optab->handlers[(int) mode].libfunc && class != MODE_FLOAT)
3264 rtx libfunc = cmp_optab->handlers[(int) mode].libfunc;
3265 rtx result;
3267 /* If we want unsigned, and this mode has a distinct unsigned
3268 comparison routine, use that. */
3269 if (unsignedp && ucmp_optab->handlers[(int) mode].libfunc)
3270 libfunc = ucmp_optab->handlers[(int) mode].libfunc;
3272 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST_MAKE_BLOCK,
3273 word_mode, 2, x, mode, y, mode);
3275 *px = result;
3276 *pmode = word_mode;
3277 if (TARGET_LIB_INT_CMP_BIASED)
3278 /* Integer comparison returns a result that must be compared
3279 against 1, so that even if we do an unsigned compare
3280 afterward, there is still a value that can represent the
3281 result "less than". */
3282 *py = const1_rtx;
3283 else
3285 *py = const0_rtx;
3286 *punsignedp = 1;
3288 return;
3291 if (class == MODE_FLOAT)
3292 prepare_float_lib_cmp (px, py, pcomparison, pmode, punsignedp);
3294 else
3295 abort ();
3298 /* Before emitting an insn with code ICODE, make sure that X, which is going
3299 to be used for operand OPNUM of the insn, is converted from mode MODE to
3300 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
3301 that it is accepted by the operand predicate. Return the new value. */
3303 static rtx
3304 prepare_operand (int icode, rtx x, int opnum, enum machine_mode mode,
3305 enum machine_mode wider_mode, int unsignedp)
3307 if (mode != wider_mode)
3308 x = convert_modes (wider_mode, mode, x, unsignedp);
3310 if (! (*insn_data[icode].operand[opnum].predicate)
3311 (x, insn_data[icode].operand[opnum].mode))
3313 if (no_new_pseudos)
3314 return NULL_RTX;
3315 x = copy_to_mode_reg (insn_data[icode].operand[opnum].mode, x);
3318 return x;
3321 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
3322 we can do the comparison.
3323 The arguments are the same as for emit_cmp_and_jump_insns; but LABEL may
3324 be NULL_RTX which indicates that only a comparison is to be generated. */
3326 static void
3327 emit_cmp_and_jump_insn_1 (rtx x, rtx y, enum machine_mode mode,
3328 enum rtx_code comparison, int unsignedp, rtx label)
3330 rtx test = gen_rtx_fmt_ee (comparison, mode, x, y);
3331 enum mode_class class = GET_MODE_CLASS (mode);
3332 enum machine_mode wider_mode = mode;
3334 /* Try combined insns first. */
3337 enum insn_code icode;
3338 PUT_MODE (test, wider_mode);
3340 if (label)
3342 icode = cbranch_optab->handlers[(int) wider_mode].insn_code;
3344 if (icode != CODE_FOR_nothing
3345 && (*insn_data[icode].operand[0].predicate) (test, wider_mode))
3347 x = prepare_operand (icode, x, 1, mode, wider_mode, unsignedp);
3348 y = prepare_operand (icode, y, 2, mode, wider_mode, unsignedp);
3349 emit_jump_insn (GEN_FCN (icode) (test, x, y, label));
3350 return;
3354 /* Handle some compares against zero. */
3355 icode = (int) tst_optab->handlers[(int) wider_mode].insn_code;
3356 if (y == CONST0_RTX (mode) && icode != CODE_FOR_nothing)
3358 x = prepare_operand (icode, x, 0, mode, wider_mode, unsignedp);
3359 emit_insn (GEN_FCN (icode) (x));
3360 if (label)
3361 emit_jump_insn ((*bcc_gen_fctn[(int) comparison]) (label));
3362 return;
3365 /* Handle compares for which there is a directly suitable insn. */
3367 icode = (int) cmp_optab->handlers[(int) wider_mode].insn_code;
3368 if (icode != CODE_FOR_nothing)
3370 x = prepare_operand (icode, x, 0, mode, wider_mode, unsignedp);
3371 y = prepare_operand (icode, y, 1, mode, wider_mode, unsignedp);
3372 emit_insn (GEN_FCN (icode) (x, y));
3373 if (label)
3374 emit_jump_insn ((*bcc_gen_fctn[(int) comparison]) (label));
3375 return;
3378 if (class != MODE_INT && class != MODE_FLOAT
3379 && class != MODE_COMPLEX_FLOAT)
3380 break;
3382 wider_mode = GET_MODE_WIDER_MODE (wider_mode);
3384 while (wider_mode != VOIDmode);
3386 abort ();
3389 /* Generate code to compare X with Y so that the condition codes are
3390 set and to jump to LABEL if the condition is true. If X is a
3391 constant and Y is not a constant, then the comparison is swapped to
3392 ensure that the comparison RTL has the canonical form.
3394 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
3395 need to be widened by emit_cmp_insn. UNSIGNEDP is also used to select
3396 the proper branch condition code.
3398 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
3400 MODE is the mode of the inputs (in case they are const_int).
3402 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). It will
3403 be passed unchanged to emit_cmp_insn, then potentially converted into an
3404 unsigned variant based on UNSIGNEDP to select a proper jump instruction. */
3406 void
3407 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
3408 enum machine_mode mode, int unsignedp, rtx label)
3410 rtx op0 = x, op1 = y;
3412 /* Swap operands and condition to ensure canonical RTL. */
3413 if (swap_commutative_operands_p (x, y))
3415 /* If we're not emitting a branch, this means some caller
3416 is out of sync. */
3417 if (! label)
3418 abort ();
3420 op0 = y, op1 = x;
3421 comparison = swap_condition (comparison);
3424 #ifdef HAVE_cc0
3425 /* If OP0 is still a constant, then both X and Y must be constants. Force
3426 X into a register to avoid aborting in emit_cmp_insn due to non-canonical
3427 RTL. */
3428 if (CONSTANT_P (op0))
3429 op0 = force_reg (mode, op0);
3430 #endif
3432 if (unsignedp)
3433 comparison = unsigned_condition (comparison);
3435 prepare_cmp_insn (&op0, &op1, &comparison, size, &mode, &unsignedp,
3436 ccp_jump);
3437 emit_cmp_and_jump_insn_1 (op0, op1, mode, comparison, unsignedp, label);
3440 /* Like emit_cmp_and_jump_insns, but generate only the comparison. */
3442 void
3443 emit_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
3444 enum machine_mode mode, int unsignedp)
3446 emit_cmp_and_jump_insns (x, y, comparison, size, mode, unsignedp, 0);
3449 /* Emit a library call comparison between floating point X and Y.
3450 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
3452 static void
3453 prepare_float_lib_cmp (rtx *px, rtx *py, enum rtx_code *pcomparison,
3454 enum machine_mode *pmode, int *punsignedp)
3456 enum rtx_code comparison = *pcomparison;
3457 enum rtx_code swapped = swap_condition (comparison);
3458 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
3459 rtx x = *px;
3460 rtx y = *py;
3461 enum machine_mode orig_mode = GET_MODE (x);
3462 enum machine_mode mode;
3463 rtx value, target, insns, equiv;
3464 rtx libfunc = 0;
3465 bool reversed_p = false;
3467 for (mode = orig_mode; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
3469 if ((libfunc = code_to_optab[comparison]->handlers[mode].libfunc))
3470 break;
3472 if ((libfunc = code_to_optab[swapped]->handlers[mode].libfunc))
3474 rtx tmp;
3475 tmp = x; x = y; y = tmp;
3476 comparison = swapped;
3477 break;
3480 if ((libfunc = code_to_optab[reversed]->handlers[mode].libfunc)
3481 && FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, reversed))
3483 comparison = reversed;
3484 reversed_p = true;
3485 break;
3489 if (mode == VOIDmode)
3490 abort ();
3492 if (mode != orig_mode)
3494 x = convert_to_mode (mode, x, 0);
3495 y = convert_to_mode (mode, y, 0);
3498 /* Attach a REG_EQUAL note describing the semantics of the libcall to
3499 the RTL. The allows the RTL optimizers to delete the libcall if the
3500 condition can be determined at compile-time. */
3501 if (comparison == UNORDERED)
3503 rtx temp = simplify_gen_relational (NE, word_mode, mode, x, x);
3504 equiv = simplify_gen_relational (NE, word_mode, mode, y, y);
3505 equiv = simplify_gen_ternary (IF_THEN_ELSE, word_mode, word_mode,
3506 temp, const_true_rtx, equiv);
3508 else
3510 equiv = simplify_gen_relational (comparison, word_mode, mode, x, y);
3511 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
3513 rtx true_rtx, false_rtx;
3515 switch (comparison)
3517 case EQ:
3518 true_rtx = const0_rtx;
3519 false_rtx = const_true_rtx;
3520 break;
3522 case NE:
3523 true_rtx = const_true_rtx;
3524 false_rtx = const0_rtx;
3525 break;
3527 case GT:
3528 true_rtx = const1_rtx;
3529 false_rtx = const0_rtx;
3530 break;
3532 case GE:
3533 true_rtx = const0_rtx;
3534 false_rtx = constm1_rtx;
3535 break;
3537 case LT:
3538 true_rtx = constm1_rtx;
3539 false_rtx = const0_rtx;
3540 break;
3542 case LE:
3543 true_rtx = const0_rtx;
3544 false_rtx = const1_rtx;
3545 break;
3547 default:
3548 abort ();
3550 equiv = simplify_gen_ternary (IF_THEN_ELSE, word_mode, word_mode,
3551 equiv, true_rtx, false_rtx);
3555 start_sequence ();
3556 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
3557 word_mode, 2, x, mode, y, mode);
3558 insns = get_insns ();
3559 end_sequence ();
3561 target = gen_reg_rtx (word_mode);
3562 emit_libcall_block (insns, target, value, equiv);
3564 if (comparison == UNORDERED
3565 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
3566 comparison = reversed_p ? EQ : NE;
3568 *px = target;
3569 *py = const0_rtx;
3570 *pmode = word_mode;
3571 *pcomparison = comparison;
3572 *punsignedp = 0;
3575 /* Generate code to indirectly jump to a location given in the rtx LOC. */
3577 void
3578 emit_indirect_jump (rtx loc)
3580 if (! ((*insn_data[(int) CODE_FOR_indirect_jump].operand[0].predicate)
3581 (loc, Pmode)))
3582 loc = copy_to_mode_reg (Pmode, loc);
3584 emit_jump_insn (gen_indirect_jump (loc));
3585 emit_barrier ();
3588 #ifdef HAVE_conditional_move
3590 /* Emit a conditional move instruction if the machine supports one for that
3591 condition and machine mode.
3593 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
3594 the mode to use should they be constants. If it is VOIDmode, they cannot
3595 both be constants.
3597 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
3598 should be stored there. MODE is the mode to use should they be constants.
3599 If it is VOIDmode, they cannot both be constants.
3601 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
3602 is not supported. */
3605 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
3606 enum machine_mode cmode, rtx op2, rtx op3,
3607 enum machine_mode mode, int unsignedp)
3609 rtx tem, subtarget, comparison, insn;
3610 enum insn_code icode;
3611 enum rtx_code reversed;
3613 /* If one operand is constant, make it the second one. Only do this
3614 if the other operand is not constant as well. */
3616 if (swap_commutative_operands_p (op0, op1))
3618 tem = op0;
3619 op0 = op1;
3620 op1 = tem;
3621 code = swap_condition (code);
3624 /* get_condition will prefer to generate LT and GT even if the old
3625 comparison was against zero, so undo that canonicalization here since
3626 comparisons against zero are cheaper. */
3627 if (code == LT && op1 == const1_rtx)
3628 code = LE, op1 = const0_rtx;
3629 else if (code == GT && op1 == constm1_rtx)
3630 code = GE, op1 = const0_rtx;
3632 if (cmode == VOIDmode)
3633 cmode = GET_MODE (op0);
3635 if (swap_commutative_operands_p (op2, op3)
3636 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
3637 != UNKNOWN))
3639 tem = op2;
3640 op2 = op3;
3641 op3 = tem;
3642 code = reversed;
3645 if (mode == VOIDmode)
3646 mode = GET_MODE (op2);
3648 icode = movcc_gen_code[mode];
3650 if (icode == CODE_FOR_nothing)
3651 return 0;
3653 if (flag_force_mem)
3655 op2 = force_not_mem (op2);
3656 op3 = force_not_mem (op3);
3659 if (!target)
3660 target = gen_reg_rtx (mode);
3662 subtarget = target;
3664 /* If the insn doesn't accept these operands, put them in pseudos. */
3666 if (! (*insn_data[icode].operand[0].predicate)
3667 (subtarget, insn_data[icode].operand[0].mode))
3668 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
3670 if (! (*insn_data[icode].operand[2].predicate)
3671 (op2, insn_data[icode].operand[2].mode))
3672 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
3674 if (! (*insn_data[icode].operand[3].predicate)
3675 (op3, insn_data[icode].operand[3].mode))
3676 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
3678 /* Everything should now be in the suitable form, so emit the compare insn
3679 and then the conditional move. */
3681 comparison
3682 = compare_from_rtx (op0, op1, code, unsignedp, cmode, NULL_RTX);
3684 /* ??? Watch for const0_rtx (nop) and const_true_rtx (unconditional)? */
3685 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
3686 return NULL and let the caller figure out how best to deal with this
3687 situation. */
3688 if (GET_CODE (comparison) != code)
3689 return NULL_RTX;
3691 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
3693 /* If that failed, then give up. */
3694 if (insn == 0)
3695 return 0;
3697 emit_insn (insn);
3699 if (subtarget != target)
3700 convert_move (target, subtarget, 0);
3702 return target;
3705 /* Return nonzero if a conditional move of mode MODE is supported.
3707 This function is for combine so it can tell whether an insn that looks
3708 like a conditional move is actually supported by the hardware. If we
3709 guess wrong we lose a bit on optimization, but that's it. */
3710 /* ??? sparc64 supports conditionally moving integers values based on fp
3711 comparisons, and vice versa. How do we handle them? */
3714 can_conditionally_move_p (enum machine_mode mode)
3716 if (movcc_gen_code[mode] != CODE_FOR_nothing)
3717 return 1;
3719 return 0;
3722 #endif /* HAVE_conditional_move */
3724 /* Emit a conditional addition instruction if the machine supports one for that
3725 condition and machine mode.
3727 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
3728 the mode to use should they be constants. If it is VOIDmode, they cannot
3729 both be constants.
3731 OP2 should be stored in TARGET if the comparison is true, otherwise OP2+OP3
3732 should be stored there. MODE is the mode to use should they be constants.
3733 If it is VOIDmode, they cannot both be constants.
3735 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
3736 is not supported. */
3739 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
3740 enum machine_mode cmode, rtx op2, rtx op3,
3741 enum machine_mode mode, int unsignedp)
3743 rtx tem, subtarget, comparison, insn;
3744 enum insn_code icode;
3745 enum rtx_code reversed;
3747 /* If one operand is constant, make it the second one. Only do this
3748 if the other operand is not constant as well. */
3750 if (swap_commutative_operands_p (op0, op1))
3752 tem = op0;
3753 op0 = op1;
3754 op1 = tem;
3755 code = swap_condition (code);
3758 /* get_condition will prefer to generate LT and GT even if the old
3759 comparison was against zero, so undo that canonicalization here since
3760 comparisons against zero are cheaper. */
3761 if (code == LT && op1 == const1_rtx)
3762 code = LE, op1 = const0_rtx;
3763 else if (code == GT && op1 == constm1_rtx)
3764 code = GE, op1 = const0_rtx;
3766 if (cmode == VOIDmode)
3767 cmode = GET_MODE (op0);
3769 if (swap_commutative_operands_p (op2, op3)
3770 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
3771 != UNKNOWN))
3773 tem = op2;
3774 op2 = op3;
3775 op3 = tem;
3776 code = reversed;
3779 if (mode == VOIDmode)
3780 mode = GET_MODE (op2);
3782 icode = addcc_optab->handlers[(int) mode].insn_code;
3784 if (icode == CODE_FOR_nothing)
3785 return 0;
3787 if (flag_force_mem)
3789 op2 = force_not_mem (op2);
3790 op3 = force_not_mem (op3);
3793 if (!target)
3794 target = gen_reg_rtx (mode);
3796 /* If the insn doesn't accept these operands, put them in pseudos. */
3798 if (! (*insn_data[icode].operand[0].predicate)
3799 (target, insn_data[icode].operand[0].mode))
3800 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
3801 else
3802 subtarget = target;
3804 if (! (*insn_data[icode].operand[2].predicate)
3805 (op2, insn_data[icode].operand[2].mode))
3806 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
3808 if (! (*insn_data[icode].operand[3].predicate)
3809 (op3, insn_data[icode].operand[3].mode))
3810 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
3812 /* Everything should now be in the suitable form, so emit the compare insn
3813 and then the conditional move. */
3815 comparison
3816 = compare_from_rtx (op0, op1, code, unsignedp, cmode, NULL_RTX);
3818 /* ??? Watch for const0_rtx (nop) and const_true_rtx (unconditional)? */
3819 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
3820 return NULL and let the caller figure out how best to deal with this
3821 situation. */
3822 if (GET_CODE (comparison) != code)
3823 return NULL_RTX;
3825 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
3827 /* If that failed, then give up. */
3828 if (insn == 0)
3829 return 0;
3831 emit_insn (insn);
3833 if (subtarget != target)
3834 convert_move (target, subtarget, 0);
3836 return target;
3839 /* These functions attempt to generate an insn body, rather than
3840 emitting the insn, but if the gen function already emits them, we
3841 make no attempt to turn them back into naked patterns. */
3843 /* Generate and return an insn body to add Y to X. */
3846 gen_add2_insn (rtx x, rtx y)
3848 int icode = (int) add_optab->handlers[(int) GET_MODE (x)].insn_code;
3850 if (! ((*insn_data[icode].operand[0].predicate)
3851 (x, insn_data[icode].operand[0].mode))
3852 || ! ((*insn_data[icode].operand[1].predicate)
3853 (x, insn_data[icode].operand[1].mode))
3854 || ! ((*insn_data[icode].operand[2].predicate)
3855 (y, insn_data[icode].operand[2].mode)))
3856 abort ();
3858 return (GEN_FCN (icode) (x, x, y));
3861 /* Generate and return an insn body to add r1 and c,
3862 storing the result in r0. */
3864 gen_add3_insn (rtx r0, rtx r1, rtx c)
3866 int icode = (int) add_optab->handlers[(int) GET_MODE (r0)].insn_code;
3868 if (icode == CODE_FOR_nothing
3869 || ! ((*insn_data[icode].operand[0].predicate)
3870 (r0, insn_data[icode].operand[0].mode))
3871 || ! ((*insn_data[icode].operand[1].predicate)
3872 (r1, insn_data[icode].operand[1].mode))
3873 || ! ((*insn_data[icode].operand[2].predicate)
3874 (c, insn_data[icode].operand[2].mode)))
3875 return NULL_RTX;
3877 return (GEN_FCN (icode) (r0, r1, c));
3881 have_add2_insn (rtx x, rtx y)
3883 int icode;
3885 if (GET_MODE (x) == VOIDmode)
3886 abort ();
3888 icode = (int) add_optab->handlers[(int) GET_MODE (x)].insn_code;
3890 if (icode == CODE_FOR_nothing)
3891 return 0;
3893 if (! ((*insn_data[icode].operand[0].predicate)
3894 (x, insn_data[icode].operand[0].mode))
3895 || ! ((*insn_data[icode].operand[1].predicate)
3896 (x, insn_data[icode].operand[1].mode))
3897 || ! ((*insn_data[icode].operand[2].predicate)
3898 (y, insn_data[icode].operand[2].mode)))
3899 return 0;
3901 return 1;
3904 /* Generate and return an insn body to subtract Y from X. */
3907 gen_sub2_insn (rtx x, rtx y)
3909 int icode = (int) sub_optab->handlers[(int) GET_MODE (x)].insn_code;
3911 if (! ((*insn_data[icode].operand[0].predicate)
3912 (x, insn_data[icode].operand[0].mode))
3913 || ! ((*insn_data[icode].operand[1].predicate)
3914 (x, insn_data[icode].operand[1].mode))
3915 || ! ((*insn_data[icode].operand[2].predicate)
3916 (y, insn_data[icode].operand[2].mode)))
3917 abort ();
3919 return (GEN_FCN (icode) (x, x, y));
3922 /* Generate and return an insn body to subtract r1 and c,
3923 storing the result in r0. */
3925 gen_sub3_insn (rtx r0, rtx r1, rtx c)
3927 int icode = (int) sub_optab->handlers[(int) GET_MODE (r0)].insn_code;
3929 if (icode == CODE_FOR_nothing
3930 || ! ((*insn_data[icode].operand[0].predicate)
3931 (r0, insn_data[icode].operand[0].mode))
3932 || ! ((*insn_data[icode].operand[1].predicate)
3933 (r1, insn_data[icode].operand[1].mode))
3934 || ! ((*insn_data[icode].operand[2].predicate)
3935 (c, insn_data[icode].operand[2].mode)))
3936 return NULL_RTX;
3938 return (GEN_FCN (icode) (r0, r1, c));
3942 have_sub2_insn (rtx x, rtx y)
3944 int icode;
3946 if (GET_MODE (x) == VOIDmode)
3947 abort ();
3949 icode = (int) sub_optab->handlers[(int) GET_MODE (x)].insn_code;
3951 if (icode == CODE_FOR_nothing)
3952 return 0;
3954 if (! ((*insn_data[icode].operand[0].predicate)
3955 (x, insn_data[icode].operand[0].mode))
3956 || ! ((*insn_data[icode].operand[1].predicate)
3957 (x, insn_data[icode].operand[1].mode))
3958 || ! ((*insn_data[icode].operand[2].predicate)
3959 (y, insn_data[icode].operand[2].mode)))
3960 return 0;
3962 return 1;
3965 /* Generate the body of an instruction to copy Y into X.
3966 It may be a list of insns, if one insn isn't enough. */
3969 gen_move_insn (rtx x, rtx y)
3971 rtx seq;
3973 start_sequence ();
3974 emit_move_insn_1 (x, y);
3975 seq = get_insns ();
3976 end_sequence ();
3977 return seq;
3980 /* Return the insn code used to extend FROM_MODE to TO_MODE.
3981 UNSIGNEDP specifies zero-extension instead of sign-extension. If
3982 no such operation exists, CODE_FOR_nothing will be returned. */
3984 enum insn_code
3985 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
3986 int unsignedp)
3988 convert_optab tab;
3989 #ifdef HAVE_ptr_extend
3990 if (unsignedp < 0)
3991 return CODE_FOR_ptr_extend;
3992 #endif
3994 tab = unsignedp ? zext_optab : sext_optab;
3995 return tab->handlers[to_mode][from_mode].insn_code;
3998 /* Generate the body of an insn to extend Y (with mode MFROM)
3999 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4002 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4003 enum machine_mode mfrom, int unsignedp)
4005 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4006 return GEN_FCN (icode) (x, y);
4009 /* can_fix_p and can_float_p say whether the target machine
4010 can directly convert a given fixed point type to
4011 a given floating point type, or vice versa.
4012 The returned value is the CODE_FOR_... value to use,
4013 or CODE_FOR_nothing if these modes cannot be directly converted.
4015 *TRUNCP_PTR is set to 1 if it is necessary to output
4016 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4018 static enum insn_code
4019 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4020 int unsignedp, int *truncp_ptr)
4022 convert_optab tab;
4023 enum insn_code icode;
4025 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4026 icode = tab->handlers[fixmode][fltmode].insn_code;
4027 if (icode != CODE_FOR_nothing)
4029 *truncp_ptr = 0;
4030 return icode;
4033 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4034 for this to work. We need to rework the fix* and ftrunc* patterns
4035 and documentation. */
4036 tab = unsignedp ? ufix_optab : sfix_optab;
4037 icode = tab->handlers[fixmode][fltmode].insn_code;
4038 if (icode != CODE_FOR_nothing
4039 && ftrunc_optab->handlers[fltmode].insn_code != CODE_FOR_nothing)
4041 *truncp_ptr = 1;
4042 return icode;
4045 *truncp_ptr = 0;
4046 return CODE_FOR_nothing;
4049 static enum insn_code
4050 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4051 int unsignedp)
4053 convert_optab tab;
4055 tab = unsignedp ? ufloat_optab : sfloat_optab;
4056 return tab->handlers[fltmode][fixmode].insn_code;
4059 /* Generate code to convert FROM to floating point
4060 and store in TO. FROM must be fixed point and not VOIDmode.
4061 UNSIGNEDP nonzero means regard FROM as unsigned.
4062 Normally this is done by correcting the final value
4063 if it is negative. */
4065 void
4066 expand_float (rtx to, rtx from, int unsignedp)
4068 enum insn_code icode;
4069 rtx target = to;
4070 enum machine_mode fmode, imode;
4072 /* Crash now, because we won't be able to decide which mode to use. */
4073 if (GET_MODE (from) == VOIDmode)
4074 abort ();
4076 /* Look for an insn to do the conversion. Do it in the specified
4077 modes if possible; otherwise convert either input, output or both to
4078 wider mode. If the integer mode is wider than the mode of FROM,
4079 we can do the conversion signed even if the input is unsigned. */
4081 for (fmode = GET_MODE (to); fmode != VOIDmode;
4082 fmode = GET_MODE_WIDER_MODE (fmode))
4083 for (imode = GET_MODE (from); imode != VOIDmode;
4084 imode = GET_MODE_WIDER_MODE (imode))
4086 int doing_unsigned = unsignedp;
4088 if (fmode != GET_MODE (to)
4089 && significand_size (fmode) < GET_MODE_BITSIZE (GET_MODE (from)))
4090 continue;
4092 icode = can_float_p (fmode, imode, unsignedp);
4093 if (icode == CODE_FOR_nothing && imode != GET_MODE (from) && unsignedp)
4094 icode = can_float_p (fmode, imode, 0), doing_unsigned = 0;
4096 if (icode != CODE_FOR_nothing)
4098 if (imode != GET_MODE (from))
4099 from = convert_to_mode (imode, from, unsignedp);
4101 if (fmode != GET_MODE (to))
4102 target = gen_reg_rtx (fmode);
4104 emit_unop_insn (icode, target, from,
4105 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4107 if (target != to)
4108 convert_move (to, target, 0);
4109 return;
4113 /* Unsigned integer, and no way to convert directly.
4114 Convert as signed, then conditionally adjust the result. */
4115 if (unsignedp)
4117 rtx label = gen_label_rtx ();
4118 rtx temp;
4119 REAL_VALUE_TYPE offset;
4121 if (flag_force_mem)
4122 from = force_not_mem (from);
4124 /* Look for a usable floating mode FMODE wider than the source and at
4125 least as wide as the target. Using FMODE will avoid rounding woes
4126 with unsigned values greater than the signed maximum value. */
4128 for (fmode = GET_MODE (to); fmode != VOIDmode;
4129 fmode = GET_MODE_WIDER_MODE (fmode))
4130 if (GET_MODE_BITSIZE (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
4131 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
4132 break;
4134 if (fmode == VOIDmode)
4136 /* There is no such mode. Pretend the target is wide enough. */
4137 fmode = GET_MODE (to);
4139 /* Avoid double-rounding when TO is narrower than FROM. */
4140 if ((significand_size (fmode) + 1)
4141 < GET_MODE_BITSIZE (GET_MODE (from)))
4143 rtx temp1;
4144 rtx neglabel = gen_label_rtx ();
4146 /* Don't use TARGET if it isn't a register, is a hard register,
4147 or is the wrong mode. */
4148 if (!REG_P (target)
4149 || REGNO (target) < FIRST_PSEUDO_REGISTER
4150 || GET_MODE (target) != fmode)
4151 target = gen_reg_rtx (fmode);
4153 imode = GET_MODE (from);
4154 do_pending_stack_adjust ();
4156 /* Test whether the sign bit is set. */
4157 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
4158 0, neglabel);
4160 /* The sign bit is not set. Convert as signed. */
4161 expand_float (target, from, 0);
4162 emit_jump_insn (gen_jump (label));
4163 emit_barrier ();
4165 /* The sign bit is set.
4166 Convert to a usable (positive signed) value by shifting right
4167 one bit, while remembering if a nonzero bit was shifted
4168 out; i.e., compute (from & 1) | (from >> 1). */
4170 emit_label (neglabel);
4171 temp = expand_binop (imode, and_optab, from, const1_rtx,
4172 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4173 temp1 = expand_shift (RSHIFT_EXPR, imode, from, integer_one_node,
4174 NULL_RTX, 1);
4175 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
4176 OPTAB_LIB_WIDEN);
4177 expand_float (target, temp, 0);
4179 /* Multiply by 2 to undo the shift above. */
4180 temp = expand_binop (fmode, add_optab, target, target,
4181 target, 0, OPTAB_LIB_WIDEN);
4182 if (temp != target)
4183 emit_move_insn (target, temp);
4185 do_pending_stack_adjust ();
4186 emit_label (label);
4187 goto done;
4191 /* If we are about to do some arithmetic to correct for an
4192 unsigned operand, do it in a pseudo-register. */
4194 if (GET_MODE (to) != fmode
4195 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
4196 target = gen_reg_rtx (fmode);
4198 /* Convert as signed integer to floating. */
4199 expand_float (target, from, 0);
4201 /* If FROM is negative (and therefore TO is negative),
4202 correct its value by 2**bitwidth. */
4204 do_pending_stack_adjust ();
4205 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
4206 0, label);
4209 real_2expN (&offset, GET_MODE_BITSIZE (GET_MODE (from)));
4210 temp = expand_binop (fmode, add_optab, target,
4211 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
4212 target, 0, OPTAB_LIB_WIDEN);
4213 if (temp != target)
4214 emit_move_insn (target, temp);
4216 do_pending_stack_adjust ();
4217 emit_label (label);
4218 goto done;
4221 /* No hardware instruction available; call a library routine. */
4223 rtx libfunc;
4224 rtx insns;
4225 rtx value;
4226 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
4228 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
4229 from = convert_to_mode (SImode, from, unsignedp);
4231 if (flag_force_mem)
4232 from = force_not_mem (from);
4234 libfunc = tab->handlers[GET_MODE (to)][GET_MODE (from)].libfunc;
4235 if (!libfunc)
4236 abort ();
4238 start_sequence ();
4240 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4241 GET_MODE (to), 1, from,
4242 GET_MODE (from));
4243 insns = get_insns ();
4244 end_sequence ();
4246 emit_libcall_block (insns, target, value,
4247 gen_rtx_FLOAT (GET_MODE (to), from));
4250 done:
4252 /* Copy result to requested destination
4253 if we have been computing in a temp location. */
4255 if (target != to)
4257 if (GET_MODE (target) == GET_MODE (to))
4258 emit_move_insn (to, target);
4259 else
4260 convert_move (to, target, 0);
4264 /* Generate code to convert FROM to fixed point and store in TO. FROM
4265 must be floating point. */
4267 void
4268 expand_fix (rtx to, rtx from, int unsignedp)
4270 enum insn_code icode;
4271 rtx target = to;
4272 enum machine_mode fmode, imode;
4273 int must_trunc = 0;
4275 /* We first try to find a pair of modes, one real and one integer, at
4276 least as wide as FROM and TO, respectively, in which we can open-code
4277 this conversion. If the integer mode is wider than the mode of TO,
4278 we can do the conversion either signed or unsigned. */
4280 for (fmode = GET_MODE (from); fmode != VOIDmode;
4281 fmode = GET_MODE_WIDER_MODE (fmode))
4282 for (imode = GET_MODE (to); imode != VOIDmode;
4283 imode = GET_MODE_WIDER_MODE (imode))
4285 int doing_unsigned = unsignedp;
4287 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
4288 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
4289 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
4291 if (icode != CODE_FOR_nothing)
4293 if (fmode != GET_MODE (from))
4294 from = convert_to_mode (fmode, from, 0);
4296 if (must_trunc)
4298 rtx temp = gen_reg_rtx (GET_MODE (from));
4299 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
4300 temp, 0);
4303 if (imode != GET_MODE (to))
4304 target = gen_reg_rtx (imode);
4306 emit_unop_insn (icode, target, from,
4307 doing_unsigned ? UNSIGNED_FIX : FIX);
4308 if (target != to)
4309 convert_move (to, target, unsignedp);
4310 return;
4314 /* For an unsigned conversion, there is one more way to do it.
4315 If we have a signed conversion, we generate code that compares
4316 the real value to the largest representable positive number. If if
4317 is smaller, the conversion is done normally. Otherwise, subtract
4318 one plus the highest signed number, convert, and add it back.
4320 We only need to check all real modes, since we know we didn't find
4321 anything with a wider integer mode.
4323 This code used to extend FP value into mode wider than the destination.
4324 This is not needed. Consider, for instance conversion from SFmode
4325 into DImode.
4327 The hot path trought the code is dealing with inputs smaller than 2^63
4328 and doing just the conversion, so there is no bits to lose.
4330 In the other path we know the value is positive in the range 2^63..2^64-1
4331 inclusive. (as for other imput overflow happens and result is undefined)
4332 So we know that the most important bit set in mantissa corresponds to
4333 2^63. The subtraction of 2^63 should not generate any rounding as it
4334 simply clears out that bit. The rest is trivial. */
4336 if (unsignedp && GET_MODE_BITSIZE (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
4337 for (fmode = GET_MODE (from); fmode != VOIDmode;
4338 fmode = GET_MODE_WIDER_MODE (fmode))
4339 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0,
4340 &must_trunc))
4342 int bitsize;
4343 REAL_VALUE_TYPE offset;
4344 rtx limit, lab1, lab2, insn;
4346 bitsize = GET_MODE_BITSIZE (GET_MODE (to));
4347 real_2expN (&offset, bitsize - 1);
4348 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
4349 lab1 = gen_label_rtx ();
4350 lab2 = gen_label_rtx ();
4352 if (flag_force_mem)
4353 from = force_not_mem (from);
4355 if (fmode != GET_MODE (from))
4356 from = convert_to_mode (fmode, from, 0);
4358 /* See if we need to do the subtraction. */
4359 do_pending_stack_adjust ();
4360 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
4361 0, lab1);
4363 /* If not, do the signed "fix" and branch around fixup code. */
4364 expand_fix (to, from, 0);
4365 emit_jump_insn (gen_jump (lab2));
4366 emit_barrier ();
4368 /* Otherwise, subtract 2**(N-1), convert to signed number,
4369 then add 2**(N-1). Do the addition using XOR since this
4370 will often generate better code. */
4371 emit_label (lab1);
4372 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
4373 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4374 expand_fix (to, target, 0);
4375 target = expand_binop (GET_MODE (to), xor_optab, to,
4376 gen_int_mode
4377 ((HOST_WIDE_INT) 1 << (bitsize - 1),
4378 GET_MODE (to)),
4379 to, 1, OPTAB_LIB_WIDEN);
4381 if (target != to)
4382 emit_move_insn (to, target);
4384 emit_label (lab2);
4386 if (mov_optab->handlers[(int) GET_MODE (to)].insn_code
4387 != CODE_FOR_nothing)
4389 /* Make a place for a REG_NOTE and add it. */
4390 insn = emit_move_insn (to, to);
4391 set_unique_reg_note (insn,
4392 REG_EQUAL,
4393 gen_rtx_fmt_e (UNSIGNED_FIX,
4394 GET_MODE (to),
4395 copy_rtx (from)));
4398 return;
4401 /* We can't do it with an insn, so use a library call. But first ensure
4402 that the mode of TO is at least as wide as SImode, since those are the
4403 only library calls we know about. */
4405 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
4407 target = gen_reg_rtx (SImode);
4409 expand_fix (target, from, unsignedp);
4411 else
4413 rtx insns;
4414 rtx value;
4415 rtx libfunc;
4417 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
4418 libfunc = tab->handlers[GET_MODE (to)][GET_MODE (from)].libfunc;
4419 if (!libfunc)
4420 abort ();
4422 if (flag_force_mem)
4423 from = force_not_mem (from);
4425 start_sequence ();
4427 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4428 GET_MODE (to), 1, from,
4429 GET_MODE (from));
4430 insns = get_insns ();
4431 end_sequence ();
4433 emit_libcall_block (insns, target, value,
4434 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
4435 GET_MODE (to), from));
4438 if (target != to)
4440 if (GET_MODE (to) == GET_MODE (target))
4441 emit_move_insn (to, target);
4442 else
4443 convert_move (to, target, 0);
4447 /* Report whether we have an instruction to perform the operation
4448 specified by CODE on operands of mode MODE. */
4450 have_insn_for (enum rtx_code code, enum machine_mode mode)
4452 return (code_to_optab[(int) code] != 0
4453 && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
4454 != CODE_FOR_nothing));
4457 /* Create a blank optab. */
4458 static optab
4459 new_optab (void)
4461 int i;
4462 optab op = ggc_alloc (sizeof (struct optab));
4463 for (i = 0; i < NUM_MACHINE_MODES; i++)
4465 op->handlers[i].insn_code = CODE_FOR_nothing;
4466 op->handlers[i].libfunc = 0;
4469 return op;
4472 static convert_optab
4473 new_convert_optab (void)
4475 int i, j;
4476 convert_optab op = ggc_alloc (sizeof (struct convert_optab));
4477 for (i = 0; i < NUM_MACHINE_MODES; i++)
4478 for (j = 0; j < NUM_MACHINE_MODES; j++)
4480 op->handlers[i][j].insn_code = CODE_FOR_nothing;
4481 op->handlers[i][j].libfunc = 0;
4483 return op;
4486 /* Same, but fill in its code as CODE, and write it into the
4487 code_to_optab table. */
4488 static inline optab
4489 init_optab (enum rtx_code code)
4491 optab op = new_optab ();
4492 op->code = code;
4493 code_to_optab[(int) code] = op;
4494 return op;
4497 /* Same, but fill in its code as CODE, and do _not_ write it into
4498 the code_to_optab table. */
4499 static inline optab
4500 init_optabv (enum rtx_code code)
4502 optab op = new_optab ();
4503 op->code = code;
4504 return op;
4507 /* Conversion optabs never go in the code_to_optab table. */
4508 static inline convert_optab
4509 init_convert_optab (enum rtx_code code)
4511 convert_optab op = new_convert_optab ();
4512 op->code = code;
4513 return op;
4516 /* Initialize the libfunc fields of an entire group of entries in some
4517 optab. Each entry is set equal to a string consisting of a leading
4518 pair of underscores followed by a generic operation name followed by
4519 a mode name (downshifted to lowercase) followed by a single character
4520 representing the number of operands for the given operation (which is
4521 usually one of the characters '2', '3', or '4').
4523 OPTABLE is the table in which libfunc fields are to be initialized.
4524 FIRST_MODE is the first machine mode index in the given optab to
4525 initialize.
4526 LAST_MODE is the last machine mode index in the given optab to
4527 initialize.
4528 OPNAME is the generic (string) name of the operation.
4529 SUFFIX is the character which specifies the number of operands for
4530 the given generic operation.
4533 static void
4534 init_libfuncs (optab optable, int first_mode, int last_mode,
4535 const char *opname, int suffix)
4537 int mode;
4538 unsigned opname_len = strlen (opname);
4540 for (mode = first_mode; (int) mode <= (int) last_mode;
4541 mode = (enum machine_mode) ((int) mode + 1))
4543 const char *mname = GET_MODE_NAME (mode);
4544 unsigned mname_len = strlen (mname);
4545 char *libfunc_name = alloca (2 + opname_len + mname_len + 1 + 1);
4546 char *p;
4547 const char *q;
4549 p = libfunc_name;
4550 *p++ = '_';
4551 *p++ = '_';
4552 for (q = opname; *q; )
4553 *p++ = *q++;
4554 for (q = mname; *q; q++)
4555 *p++ = TOLOWER (*q);
4556 *p++ = suffix;
4557 *p = '\0';
4559 optable->handlers[(int) mode].libfunc
4560 = init_one_libfunc (ggc_alloc_string (libfunc_name, p - libfunc_name));
4564 /* Initialize the libfunc fields of an entire group of entries in some
4565 optab which correspond to all integer mode operations. The parameters
4566 have the same meaning as similarly named ones for the `init_libfuncs'
4567 routine. (See above). */
4569 static void
4570 init_integral_libfuncs (optab optable, const char *opname, int suffix)
4572 int maxsize = 2*BITS_PER_WORD;
4573 if (maxsize < LONG_LONG_TYPE_SIZE)
4574 maxsize = LONG_LONG_TYPE_SIZE;
4575 init_libfuncs (optable, word_mode,
4576 mode_for_size (maxsize, MODE_INT, 0),
4577 opname, suffix);
4580 /* Initialize the libfunc fields of an entire group of entries in some
4581 optab which correspond to all real mode operations. The parameters
4582 have the same meaning as similarly named ones for the `init_libfuncs'
4583 routine. (See above). */
4585 static void
4586 init_floating_libfuncs (optab optable, const char *opname, int suffix)
4588 init_libfuncs (optable, MIN_MODE_FLOAT, MAX_MODE_FLOAT, opname, suffix);
4591 /* Initialize the libfunc fields of an entire group of entries of an
4592 inter-mode-class conversion optab. The string formation rules are
4593 similar to the ones for init_libfuncs, above, but instead of having
4594 a mode name and an operand count these functions have two mode names
4595 and no operand count. */
4596 static void
4597 init_interclass_conv_libfuncs (convert_optab tab, const char *opname,
4598 enum mode_class from_class,
4599 enum mode_class to_class)
4601 enum machine_mode first_from_mode = GET_CLASS_NARROWEST_MODE (from_class);
4602 enum machine_mode first_to_mode = GET_CLASS_NARROWEST_MODE (to_class);
4603 size_t opname_len = strlen (opname);
4604 size_t max_mname_len = 0;
4606 enum machine_mode fmode, tmode;
4607 const char *fname, *tname;
4608 const char *q;
4609 char *libfunc_name, *suffix;
4610 char *p;
4612 for (fmode = first_from_mode;
4613 fmode != VOIDmode;
4614 fmode = GET_MODE_WIDER_MODE (fmode))
4615 max_mname_len = MAX (max_mname_len, strlen (GET_MODE_NAME (fmode)));
4617 for (tmode = first_to_mode;
4618 tmode != VOIDmode;
4619 tmode = GET_MODE_WIDER_MODE (tmode))
4620 max_mname_len = MAX (max_mname_len, strlen (GET_MODE_NAME (tmode)));
4622 libfunc_name = alloca (2 + opname_len + 2*max_mname_len + 1 + 1);
4623 libfunc_name[0] = '_';
4624 libfunc_name[1] = '_';
4625 memcpy (&libfunc_name[2], opname, opname_len);
4626 suffix = libfunc_name + opname_len + 2;
4628 for (fmode = first_from_mode; fmode != VOIDmode;
4629 fmode = GET_MODE_WIDER_MODE (fmode))
4630 for (tmode = first_to_mode; tmode != VOIDmode;
4631 tmode = GET_MODE_WIDER_MODE (tmode))
4633 fname = GET_MODE_NAME (fmode);
4634 tname = GET_MODE_NAME (tmode);
4636 p = suffix;
4637 for (q = fname; *q; p++, q++)
4638 *p = TOLOWER (*q);
4639 for (q = tname; *q; p++, q++)
4640 *p = TOLOWER (*q);
4642 *p = '\0';
4644 tab->handlers[tmode][fmode].libfunc
4645 = init_one_libfunc (ggc_alloc_string (libfunc_name,
4646 p - libfunc_name));
4650 /* Initialize the libfunc fields of an entire group of entries of an
4651 intra-mode-class conversion optab. The string formation rules are
4652 similar to the ones for init_libfunc, above. WIDENING says whether
4653 the optab goes from narrow to wide modes or vice versa. These functions
4654 have two mode names _and_ an operand count. */
4655 static void
4656 init_intraclass_conv_libfuncs (convert_optab tab, const char *opname,
4657 enum mode_class class, bool widening)
4659 enum machine_mode first_mode = GET_CLASS_NARROWEST_MODE (class);
4660 size_t opname_len = strlen (opname);
4661 size_t max_mname_len = 0;
4663 enum machine_mode nmode, wmode;
4664 const char *nname, *wname;
4665 const char *q;
4666 char *libfunc_name, *suffix;
4667 char *p;
4669 for (nmode = first_mode; nmode != VOIDmode;
4670 nmode = GET_MODE_WIDER_MODE (nmode))
4671 max_mname_len = MAX (max_mname_len, strlen (GET_MODE_NAME (nmode)));
4673 libfunc_name = alloca (2 + opname_len + 2*max_mname_len + 1 + 1);
4674 libfunc_name[0] = '_';
4675 libfunc_name[1] = '_';
4676 memcpy (&libfunc_name[2], opname, opname_len);
4677 suffix = libfunc_name + opname_len + 2;
4679 for (nmode = first_mode; nmode != VOIDmode;
4680 nmode = GET_MODE_WIDER_MODE (nmode))
4681 for (wmode = GET_MODE_WIDER_MODE (nmode); wmode != VOIDmode;
4682 wmode = GET_MODE_WIDER_MODE (wmode))
4684 nname = GET_MODE_NAME (nmode);
4685 wname = GET_MODE_NAME (wmode);
4687 p = suffix;
4688 for (q = widening ? nname : wname; *q; p++, q++)
4689 *p = TOLOWER (*q);
4690 for (q = widening ? wname : nname; *q; p++, q++)
4691 *p = TOLOWER (*q);
4693 *p++ = '2';
4694 *p = '\0';
4696 tab->handlers[widening ? wmode : nmode]
4697 [widening ? nmode : wmode].libfunc
4698 = init_one_libfunc (ggc_alloc_string (libfunc_name,
4699 p - libfunc_name));
4705 init_one_libfunc (const char *name)
4707 rtx symbol;
4709 /* Create a FUNCTION_DECL that can be passed to
4710 targetm.encode_section_info. */
4711 /* ??? We don't have any type information except for this is
4712 a function. Pretend this is "int foo()". */
4713 tree decl = build_decl (FUNCTION_DECL, get_identifier (name),
4714 build_function_type (integer_type_node, NULL_TREE));
4715 DECL_ARTIFICIAL (decl) = 1;
4716 DECL_EXTERNAL (decl) = 1;
4717 TREE_PUBLIC (decl) = 1;
4719 symbol = XEXP (DECL_RTL (decl), 0);
4721 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
4722 are the flags assigned by targetm.encode_section_info. */
4723 SYMBOL_REF_DECL (symbol) = 0;
4725 return symbol;
4728 /* Call this to reset the function entry for one optab (OPTABLE) in mode
4729 MODE to NAME, which should be either 0 or a string constant. */
4730 void
4731 set_optab_libfunc (optab optable, enum machine_mode mode, const char *name)
4733 if (name)
4734 optable->handlers[mode].libfunc = init_one_libfunc (name);
4735 else
4736 optable->handlers[mode].libfunc = 0;
4739 /* Call this to reset the function entry for one conversion optab
4740 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
4741 either 0 or a string constant. */
4742 void
4743 set_conv_libfunc (convert_optab optable, enum machine_mode tmode,
4744 enum machine_mode fmode, const char *name)
4746 if (name)
4747 optable->handlers[tmode][fmode].libfunc = init_one_libfunc (name);
4748 else
4749 optable->handlers[tmode][fmode].libfunc = 0;
4752 /* Call this once to initialize the contents of the optabs
4753 appropriately for the current target machine. */
4755 void
4756 init_optabs (void)
4758 unsigned int i;
4760 /* Start by initializing all tables to contain CODE_FOR_nothing. */
4762 for (i = 0; i < NUM_RTX_CODE; i++)
4763 setcc_gen_code[i] = CODE_FOR_nothing;
4765 #ifdef HAVE_conditional_move
4766 for (i = 0; i < NUM_MACHINE_MODES; i++)
4767 movcc_gen_code[i] = CODE_FOR_nothing;
4768 #endif
4770 for (i = 0; i < NUM_MACHINE_MODES; i++)
4772 vcond_gen_code[i] = CODE_FOR_nothing;
4773 vcondu_gen_code[i] = CODE_FOR_nothing;
4776 add_optab = init_optab (PLUS);
4777 addv_optab = init_optabv (PLUS);
4778 sub_optab = init_optab (MINUS);
4779 subv_optab = init_optabv (MINUS);
4780 smul_optab = init_optab (MULT);
4781 smulv_optab = init_optabv (MULT);
4782 smul_highpart_optab = init_optab (UNKNOWN);
4783 umul_highpart_optab = init_optab (UNKNOWN);
4784 smul_widen_optab = init_optab (UNKNOWN);
4785 umul_widen_optab = init_optab (UNKNOWN);
4786 sdiv_optab = init_optab (DIV);
4787 sdivv_optab = init_optabv (DIV);
4788 sdivmod_optab = init_optab (UNKNOWN);
4789 udiv_optab = init_optab (UDIV);
4790 udivmod_optab = init_optab (UNKNOWN);
4791 smod_optab = init_optab (MOD);
4792 umod_optab = init_optab (UMOD);
4793 fmod_optab = init_optab (UNKNOWN);
4794 drem_optab = init_optab (UNKNOWN);
4795 ftrunc_optab = init_optab (UNKNOWN);
4796 and_optab = init_optab (AND);
4797 ior_optab = init_optab (IOR);
4798 xor_optab = init_optab (XOR);
4799 ashl_optab = init_optab (ASHIFT);
4800 ashr_optab = init_optab (ASHIFTRT);
4801 lshr_optab = init_optab (LSHIFTRT);
4802 rotl_optab = init_optab (ROTATE);
4803 rotr_optab = init_optab (ROTATERT);
4804 smin_optab = init_optab (SMIN);
4805 smax_optab = init_optab (SMAX);
4806 umin_optab = init_optab (UMIN);
4807 umax_optab = init_optab (UMAX);
4808 pow_optab = init_optab (UNKNOWN);
4809 atan2_optab = init_optab (UNKNOWN);
4811 /* These three have codes assigned exclusively for the sake of
4812 have_insn_for. */
4813 mov_optab = init_optab (SET);
4814 movstrict_optab = init_optab (STRICT_LOW_PART);
4815 cmp_optab = init_optab (COMPARE);
4817 ucmp_optab = init_optab (UNKNOWN);
4818 tst_optab = init_optab (UNKNOWN);
4820 eq_optab = init_optab (EQ);
4821 ne_optab = init_optab (NE);
4822 gt_optab = init_optab (GT);
4823 ge_optab = init_optab (GE);
4824 lt_optab = init_optab (LT);
4825 le_optab = init_optab (LE);
4826 unord_optab = init_optab (UNORDERED);
4828 neg_optab = init_optab (NEG);
4829 negv_optab = init_optabv (NEG);
4830 abs_optab = init_optab (ABS);
4831 absv_optab = init_optabv (ABS);
4832 addcc_optab = init_optab (UNKNOWN);
4833 one_cmpl_optab = init_optab (NOT);
4834 ffs_optab = init_optab (FFS);
4835 clz_optab = init_optab (CLZ);
4836 ctz_optab = init_optab (CTZ);
4837 popcount_optab = init_optab (POPCOUNT);
4838 parity_optab = init_optab (PARITY);
4839 sqrt_optab = init_optab (SQRT);
4840 floor_optab = init_optab (UNKNOWN);
4841 ceil_optab = init_optab (UNKNOWN);
4842 round_optab = init_optab (UNKNOWN);
4843 btrunc_optab = init_optab (UNKNOWN);
4844 nearbyint_optab = init_optab (UNKNOWN);
4845 rint_optab = init_optab (UNKNOWN);
4846 sincos_optab = init_optab (UNKNOWN);
4847 sin_optab = init_optab (UNKNOWN);
4848 asin_optab = init_optab (UNKNOWN);
4849 cos_optab = init_optab (UNKNOWN);
4850 acos_optab = init_optab (UNKNOWN);
4851 exp_optab = init_optab (UNKNOWN);
4852 exp10_optab = init_optab (UNKNOWN);
4853 exp2_optab = init_optab (UNKNOWN);
4854 expm1_optab = init_optab (UNKNOWN);
4855 logb_optab = init_optab (UNKNOWN);
4856 ilogb_optab = init_optab (UNKNOWN);
4857 log_optab = init_optab (UNKNOWN);
4858 log10_optab = init_optab (UNKNOWN);
4859 log2_optab = init_optab (UNKNOWN);
4860 log1p_optab = init_optab (UNKNOWN);
4861 tan_optab = init_optab (UNKNOWN);
4862 atan_optab = init_optab (UNKNOWN);
4863 copysign_optab = init_optab (UNKNOWN);
4865 strlen_optab = init_optab (UNKNOWN);
4866 cbranch_optab = init_optab (UNKNOWN);
4867 cmov_optab = init_optab (UNKNOWN);
4868 cstore_optab = init_optab (UNKNOWN);
4869 push_optab = init_optab (UNKNOWN);
4871 vec_extract_optab = init_optab (UNKNOWN);
4872 vec_set_optab = init_optab (UNKNOWN);
4873 vec_init_optab = init_optab (UNKNOWN);
4874 vec_realign_load_optab = init_optab (UNKNOWN);
4875 movmisalign_optab = init_optab (UNKNOWN);
4877 /* Conversions. */
4878 sext_optab = init_convert_optab (SIGN_EXTEND);
4879 zext_optab = init_convert_optab (ZERO_EXTEND);
4880 trunc_optab = init_convert_optab (TRUNCATE);
4881 sfix_optab = init_convert_optab (FIX);
4882 ufix_optab = init_convert_optab (UNSIGNED_FIX);
4883 sfixtrunc_optab = init_convert_optab (UNKNOWN);
4884 ufixtrunc_optab = init_convert_optab (UNKNOWN);
4885 sfloat_optab = init_convert_optab (FLOAT);
4886 ufloat_optab = init_convert_optab (UNSIGNED_FLOAT);
4888 for (i = 0; i < NUM_MACHINE_MODES; i++)
4890 movmem_optab[i] = CODE_FOR_nothing;
4891 clrmem_optab[i] = CODE_FOR_nothing;
4892 cmpstr_optab[i] = CODE_FOR_nothing;
4893 cmpmem_optab[i] = CODE_FOR_nothing;
4895 #ifdef HAVE_SECONDARY_RELOADS
4896 reload_in_optab[i] = reload_out_optab[i] = CODE_FOR_nothing;
4897 #endif
4900 /* Fill in the optabs with the insns we support. */
4901 init_all_optabs ();
4903 /* Initialize the optabs with the names of the library functions. */
4904 init_integral_libfuncs (add_optab, "add", '3');
4905 init_floating_libfuncs (add_optab, "add", '3');
4906 init_integral_libfuncs (addv_optab, "addv", '3');
4907 init_floating_libfuncs (addv_optab, "add", '3');
4908 init_integral_libfuncs (sub_optab, "sub", '3');
4909 init_floating_libfuncs (sub_optab, "sub", '3');
4910 init_integral_libfuncs (subv_optab, "subv", '3');
4911 init_floating_libfuncs (subv_optab, "sub", '3');
4912 init_integral_libfuncs (smul_optab, "mul", '3');
4913 init_floating_libfuncs (smul_optab, "mul", '3');
4914 init_integral_libfuncs (smulv_optab, "mulv", '3');
4915 init_floating_libfuncs (smulv_optab, "mul", '3');
4916 init_integral_libfuncs (sdiv_optab, "div", '3');
4917 init_floating_libfuncs (sdiv_optab, "div", '3');
4918 init_integral_libfuncs (sdivv_optab, "divv", '3');
4919 init_integral_libfuncs (udiv_optab, "udiv", '3');
4920 init_integral_libfuncs (sdivmod_optab, "divmod", '4');
4921 init_integral_libfuncs (udivmod_optab, "udivmod", '4');
4922 init_integral_libfuncs (smod_optab, "mod", '3');
4923 init_integral_libfuncs (umod_optab, "umod", '3');
4924 init_floating_libfuncs (ftrunc_optab, "ftrunc", '2');
4925 init_integral_libfuncs (and_optab, "and", '3');
4926 init_integral_libfuncs (ior_optab, "ior", '3');
4927 init_integral_libfuncs (xor_optab, "xor", '3');
4928 init_integral_libfuncs (ashl_optab, "ashl", '3');
4929 init_integral_libfuncs (ashr_optab, "ashr", '3');
4930 init_integral_libfuncs (lshr_optab, "lshr", '3');
4931 init_integral_libfuncs (smin_optab, "min", '3');
4932 init_floating_libfuncs (smin_optab, "min", '3');
4933 init_integral_libfuncs (smax_optab, "max", '3');
4934 init_floating_libfuncs (smax_optab, "max", '3');
4935 init_integral_libfuncs (umin_optab, "umin", '3');
4936 init_integral_libfuncs (umax_optab, "umax", '3');
4937 init_integral_libfuncs (neg_optab, "neg", '2');
4938 init_floating_libfuncs (neg_optab, "neg", '2');
4939 init_integral_libfuncs (negv_optab, "negv", '2');
4940 init_floating_libfuncs (negv_optab, "neg", '2');
4941 init_integral_libfuncs (one_cmpl_optab, "one_cmpl", '2');
4942 init_integral_libfuncs (ffs_optab, "ffs", '2');
4943 init_integral_libfuncs (clz_optab, "clz", '2');
4944 init_integral_libfuncs (ctz_optab, "ctz", '2');
4945 init_integral_libfuncs (popcount_optab, "popcount", '2');
4946 init_integral_libfuncs (parity_optab, "parity", '2');
4948 /* Comparison libcalls for integers MUST come in pairs,
4949 signed/unsigned. */
4950 init_integral_libfuncs (cmp_optab, "cmp", '2');
4951 init_integral_libfuncs (ucmp_optab, "ucmp", '2');
4952 init_floating_libfuncs (cmp_optab, "cmp", '2');
4954 /* EQ etc are floating point only. */
4955 init_floating_libfuncs (eq_optab, "eq", '2');
4956 init_floating_libfuncs (ne_optab, "ne", '2');
4957 init_floating_libfuncs (gt_optab, "gt", '2');
4958 init_floating_libfuncs (ge_optab, "ge", '2');
4959 init_floating_libfuncs (lt_optab, "lt", '2');
4960 init_floating_libfuncs (le_optab, "le", '2');
4961 init_floating_libfuncs (unord_optab, "unord", '2');
4963 /* Conversions. */
4964 init_interclass_conv_libfuncs (sfloat_optab, "float",
4965 MODE_INT, MODE_FLOAT);
4966 init_interclass_conv_libfuncs (sfix_optab, "fix",
4967 MODE_FLOAT, MODE_INT);
4968 init_interclass_conv_libfuncs (ufix_optab, "fixuns",
4969 MODE_FLOAT, MODE_INT);
4971 /* sext_optab is also used for FLOAT_EXTEND. */
4972 init_intraclass_conv_libfuncs (sext_optab, "extend", MODE_FLOAT, true);
4973 init_intraclass_conv_libfuncs (trunc_optab, "trunc", MODE_FLOAT, false);
4975 /* Use cabs for double complex abs, since systems generally have cabs.
4976 Don't define any libcall for float complex, so that cabs will be used. */
4977 if (complex_double_type_node)
4978 abs_optab->handlers[TYPE_MODE (complex_double_type_node)].libfunc
4979 = init_one_libfunc ("cabs");
4981 /* The ffs function operates on `int'. */
4982 ffs_optab->handlers[(int) mode_for_size (INT_TYPE_SIZE, MODE_INT, 0)].libfunc
4983 = init_one_libfunc ("ffs");
4985 abort_libfunc = init_one_libfunc ("abort");
4986 memcpy_libfunc = init_one_libfunc ("memcpy");
4987 memmove_libfunc = init_one_libfunc ("memmove");
4988 memcmp_libfunc = init_one_libfunc ("memcmp");
4989 memset_libfunc = init_one_libfunc ("memset");
4990 setbits_libfunc = init_one_libfunc ("__setbits");
4992 unwind_resume_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
4993 ? "_Unwind_SjLj_Resume"
4994 : "_Unwind_Resume");
4995 #ifndef DONT_USE_BUILTIN_SETJMP
4996 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
4997 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
4998 #else
4999 setjmp_libfunc = init_one_libfunc ("setjmp");
5000 longjmp_libfunc = init_one_libfunc ("longjmp");
5001 #endif
5002 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
5003 unwind_sjlj_unregister_libfunc
5004 = init_one_libfunc ("_Unwind_SjLj_Unregister");
5006 /* For function entry/exit instrumentation. */
5007 profile_function_entry_libfunc
5008 = init_one_libfunc ("__cyg_profile_func_enter");
5009 profile_function_exit_libfunc
5010 = init_one_libfunc ("__cyg_profile_func_exit");
5012 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
5014 if (HAVE_conditional_trap)
5015 trap_rtx = gen_rtx_fmt_ee (EQ, VOIDmode, NULL_RTX, NULL_RTX);
5017 /* Allow the target to add more libcalls or rename some, etc. */
5018 targetm.init_libfuncs ();
5021 #ifdef DEBUG
5023 /* Print information about the current contents of the optabs on
5024 STDERR. */
5026 static void
5027 debug_optab_libfuncs (void)
5029 int i;
5030 int j;
5031 int k;
5033 /* Dump the arithmetic optabs. */
5034 for (i = 0; i != (int) OTI_MAX; i++)
5035 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5037 optab o;
5038 struct optab_handlers *h;
5040 o = optab_table[i];
5041 h = &o->handlers[j];
5042 if (h->libfunc)
5044 if (GET_CODE (h->libfunc) != SYMBOL_REF)
5045 abort ();
5046 fprintf (stderr, "%s\t%s:\t%s\n",
5047 GET_RTX_NAME (o->code),
5048 GET_MODE_NAME (j),
5049 XSTR (h->libfunc, 0));
5053 /* Dump the conversion optabs. */
5054 for (i = 0; i < (int) CTI_MAX; ++i)
5055 for (j = 0; j < NUM_MACHINE_MODES; ++j)
5056 for (k = 0; k < NUM_MACHINE_MODES; ++k)
5058 convert_optab o;
5059 struct optab_handlers *h;
5061 o = &convert_optab_table[i];
5062 h = &o->handlers[j][k];
5063 if (h->libfunc)
5065 if (GET_CODE (h->libfunc) != SYMBOL_REF)
5066 abort ();
5067 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
5068 GET_RTX_NAME (o->code),
5069 GET_MODE_NAME (j),
5070 GET_MODE_NAME (k),
5071 XSTR (h->libfunc, 0));
5076 #endif /* DEBUG */
5079 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
5080 CODE. Return 0 on failure. */
5083 gen_cond_trap (enum rtx_code code ATTRIBUTE_UNUSED, rtx op1,
5084 rtx op2 ATTRIBUTE_UNUSED, rtx tcode ATTRIBUTE_UNUSED)
5086 enum machine_mode mode = GET_MODE (op1);
5087 enum insn_code icode;
5088 rtx insn;
5090 if (!HAVE_conditional_trap)
5091 return 0;
5093 if (mode == VOIDmode)
5094 return 0;
5096 icode = cmp_optab->handlers[(int) mode].insn_code;
5097 if (icode == CODE_FOR_nothing)
5098 return 0;
5100 start_sequence ();
5101 op1 = prepare_operand (icode, op1, 0, mode, mode, 0);
5102 op2 = prepare_operand (icode, op2, 1, mode, mode, 0);
5103 if (!op1 || !op2)
5105 end_sequence ();
5106 return 0;
5108 emit_insn (GEN_FCN (icode) (op1, op2));
5110 PUT_CODE (trap_rtx, code);
5111 insn = gen_conditional_trap (trap_rtx, tcode);
5112 if (insn)
5114 emit_insn (insn);
5115 insn = get_insns ();
5117 end_sequence ();
5119 return insn;
5122 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
5123 or unsigned operation code. */
5125 static enum rtx_code
5126 get_rtx_code (enum tree_code tcode, bool unsignedp)
5128 enum rtx_code code;
5129 switch (tcode)
5131 case EQ_EXPR:
5132 code = EQ;
5133 break;
5134 case NE_EXPR:
5135 code = NE;
5136 break;
5137 case LT_EXPR:
5138 code = unsignedp ? LTU : LT;
5139 break;
5140 case LE_EXPR:
5141 code = unsignedp ? LEU : LE;
5142 break;
5143 case GT_EXPR:
5144 code = unsignedp ? GTU : GT;
5145 break;
5146 case GE_EXPR:
5147 code = unsignedp ? GEU : GE;
5148 break;
5150 case UNORDERED_EXPR:
5151 code = UNORDERED;
5152 break;
5153 case ORDERED_EXPR:
5154 code = ORDERED;
5155 break;
5156 case UNLT_EXPR:
5157 code = UNLT;
5158 break;
5159 case UNLE_EXPR:
5160 code = UNLE;
5161 break;
5162 case UNGT_EXPR:
5163 code = UNGT;
5164 break;
5165 case UNGE_EXPR:
5166 code = UNGE;
5167 break;
5168 case UNEQ_EXPR:
5169 code = UNEQ;
5170 break;
5171 case LTGT_EXPR:
5172 code = LTGT;
5173 break;
5175 default:
5176 abort ();
5178 return code;
5181 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
5182 unsigned operators. Do not generate compare instruction. */
5184 static rtx
5185 vector_compare_rtx (tree cond, bool unsignedp, enum insn_code icode)
5187 enum rtx_code rcode;
5188 tree t_op0, t_op1;
5189 rtx rtx_op0, rtx_op1;
5191 if (!COMPARISON_CLASS_P (cond))
5193 /* This is unlikely. While generating VEC_COND_EXPR,
5194 auto vectorizer ensures that condition is a relational
5195 operation. */
5196 abort ();
5198 else
5200 rcode = get_rtx_code (TREE_CODE (cond), unsignedp);
5201 t_op0 = TREE_OPERAND (cond, 0);
5202 t_op1 = TREE_OPERAND (cond, 1);
5205 /* Expand operands. */
5206 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)), 1);
5207 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)), 1);
5209 if (!(*insn_data[icode].operand[4].predicate) (rtx_op0, GET_MODE (rtx_op0))
5210 && GET_MODE (rtx_op0) != VOIDmode)
5211 rtx_op0 = force_reg (GET_MODE (rtx_op0), rtx_op0);
5213 if (!(*insn_data[icode].operand[5].predicate) (rtx_op1, GET_MODE (rtx_op1))
5214 && GET_MODE (rtx_op1) != VOIDmode)
5215 rtx_op1 = force_reg (GET_MODE (rtx_op1), rtx_op1);
5217 return gen_rtx_fmt_ee (rcode, VOIDmode, rtx_op0, rtx_op1);
5220 /* Return insn code for VEC_COND_EXPR EXPR. */
5222 static inline enum insn_code
5223 get_vcond_icode (tree expr, enum machine_mode mode)
5225 enum insn_code icode = CODE_FOR_nothing;
5227 if (TYPE_UNSIGNED (TREE_TYPE (expr)))
5228 icode = vcondu_gen_code[mode];
5229 else
5230 icode = vcond_gen_code[mode];
5231 return icode;
5234 /* Return TRUE iff, appropriate vector insns are available
5235 for vector cond expr expr in VMODE mode. */
5237 bool
5238 expand_vec_cond_expr_p (tree expr, enum machine_mode vmode)
5240 if (get_vcond_icode (expr, vmode) == CODE_FOR_nothing)
5241 return false;
5242 return true;
5245 /* Generate insns for VEC_COND_EXPR. */
5248 expand_vec_cond_expr (tree vec_cond_expr, rtx target)
5250 enum insn_code icode;
5251 rtx comparison, rtx_op1, rtx_op2, cc_op0, cc_op1;
5252 enum machine_mode mode = TYPE_MODE (TREE_TYPE (vec_cond_expr));
5253 bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (vec_cond_expr));
5255 icode = get_vcond_icode (vec_cond_expr, mode);
5256 if (icode == CODE_FOR_nothing)
5257 return 0;
5259 if (!target)
5260 target = gen_reg_rtx (mode);
5262 /* Get comparison rtx. First expand both cond expr operands. */
5263 comparison = vector_compare_rtx (TREE_OPERAND (vec_cond_expr, 0),
5264 unsignedp, icode);
5265 cc_op0 = XEXP (comparison, 0);
5266 cc_op1 = XEXP (comparison, 1);
5267 /* Expand both operands and force them in reg, if required. */
5268 rtx_op1 = expand_expr (TREE_OPERAND (vec_cond_expr, 1),
5269 NULL_RTX, VOIDmode, 1);
5270 if (!(*insn_data[icode].operand[1].predicate) (rtx_op1, mode)
5271 && mode != VOIDmode)
5272 rtx_op1 = force_reg (mode, rtx_op1);
5274 rtx_op2 = expand_expr (TREE_OPERAND (vec_cond_expr, 2),
5275 NULL_RTX, VOIDmode, 1);
5276 if (!(*insn_data[icode].operand[2].predicate) (rtx_op2, mode)
5277 && mode != VOIDmode)
5278 rtx_op2 = force_reg (mode, rtx_op2);
5280 /* Emit instruction! */
5281 emit_insn (GEN_FCN (icode) (target, rtx_op1, rtx_op2,
5282 comparison, cc_op0, cc_op1));
5284 return target;
5286 #include "gt-optabs.h"