2004-09-04 Uros Bizjak <uros@kss-loka.si>
[official-gcc.git] / gcc / optabs.c
blobde7f4dc2caa117f66d0dd511c28c48809b06950c
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "toplev.h"
29 /* Include insn-config.h before expr.h so that HAVE_conditional_move
30 is properly defined. */
31 #include "insn-config.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "tm_p.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "except.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "libfuncs.h"
41 #include "recog.h"
42 #include "reload.h"
43 #include "ggc.h"
44 #include "real.h"
45 #include "basic-block.h"
46 #include "target.h"
48 /* Each optab contains info on how this target machine
49 can perform a particular operation
50 for all sizes and kinds of operands.
52 The operation to be performed is often specified
53 by passing one of these optabs as an argument.
55 See expr.h for documentation of these optabs. */
57 optab optab_table[OTI_MAX];
59 rtx libfunc_table[LTI_MAX];
61 /* Tables of patterns for converting one mode to another. */
62 convert_optab convert_optab_table[CTI_MAX];
64 /* Contains the optab used for each rtx code. */
65 optab code_to_optab[NUM_RTX_CODE + 1];
67 /* Indexed by the rtx-code for a conditional (eg. EQ, LT,...)
68 gives the gen_function to make a branch to test that condition. */
70 rtxfun bcc_gen_fctn[NUM_RTX_CODE];
72 /* Indexed by the rtx-code for a conditional (eg. EQ, LT,...)
73 gives the insn code to make a store-condition insn
74 to test that condition. */
76 enum insn_code setcc_gen_code[NUM_RTX_CODE];
78 #ifdef HAVE_conditional_move
79 /* Indexed by the machine mode, gives the insn code to make a conditional
80 move insn. This is not indexed by the rtx-code like bcc_gen_fctn and
81 setcc_gen_code to cut down on the number of named patterns. Consider a day
82 when a lot more rtx codes are conditional (eg: for the ARM). */
84 enum insn_code movcc_gen_code[NUM_MACHINE_MODES];
85 #endif
87 /* The insn generating function can not take an rtx_code argument.
88 TRAP_RTX is used as an rtx argument. Its code is replaced with
89 the code to be used in the trap insn and all other fields are ignored. */
90 static GTY(()) rtx trap_rtx;
92 static int add_equal_note (rtx, rtx, enum rtx_code, rtx, rtx);
93 static rtx widen_operand (rtx, enum machine_mode, enum machine_mode, int,
94 int);
95 static void prepare_cmp_insn (rtx *, rtx *, enum rtx_code *, rtx,
96 enum machine_mode *, int *,
97 enum can_compare_purpose);
98 static enum insn_code can_fix_p (enum machine_mode, enum machine_mode, int,
99 int *);
100 static enum insn_code can_float_p (enum machine_mode, enum machine_mode, int);
101 static optab new_optab (void);
102 static convert_optab new_convert_optab (void);
103 static inline optab init_optab (enum rtx_code);
104 static inline optab init_optabv (enum rtx_code);
105 static inline convert_optab init_convert_optab (enum rtx_code);
106 static void init_libfuncs (optab, int, int, const char *, int);
107 static void init_integral_libfuncs (optab, const char *, int);
108 static void init_floating_libfuncs (optab, const char *, int);
109 static void init_interclass_conv_libfuncs (convert_optab, const char *,
110 enum mode_class, enum mode_class);
111 static void init_intraclass_conv_libfuncs (convert_optab, const char *,
112 enum mode_class, bool);
113 static void emit_cmp_and_jump_insn_1 (rtx, rtx, enum machine_mode,
114 enum rtx_code, int, rtx);
115 static void prepare_float_lib_cmp (rtx *, rtx *, enum rtx_code *,
116 enum machine_mode *, int *);
117 static rtx widen_clz (enum machine_mode, rtx, rtx);
118 static rtx expand_parity (enum machine_mode, rtx, rtx);
120 #ifndef HAVE_conditional_trap
121 #define HAVE_conditional_trap 0
122 #define gen_conditional_trap(a,b) (abort (), NULL_RTX)
123 #endif
125 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
126 the result of operation CODE applied to OP0 (and OP1 if it is a binary
127 operation).
129 If the last insn does not set TARGET, don't do anything, but return 1.
131 If a previous insn sets TARGET and TARGET is one of OP0 or OP1,
132 don't add the REG_EQUAL note but return 0. Our caller can then try
133 again, ensuring that TARGET is not one of the operands. */
135 static int
136 add_equal_note (rtx insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
138 rtx last_insn, insn, set;
139 rtx note;
141 if (! insns
142 || ! INSN_P (insns)
143 || NEXT_INSN (insns) == NULL_RTX)
144 abort ();
146 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
147 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
148 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
149 && GET_RTX_CLASS (code) != RTX_COMPARE
150 && GET_RTX_CLASS (code) != RTX_UNARY)
151 return 1;
153 if (GET_CODE (target) == ZERO_EXTRACT)
154 return 1;
156 for (last_insn = insns;
157 NEXT_INSN (last_insn) != NULL_RTX;
158 last_insn = NEXT_INSN (last_insn))
161 set = single_set (last_insn);
162 if (set == NULL_RTX)
163 return 1;
165 if (! rtx_equal_p (SET_DEST (set), target)
166 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
167 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
168 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
169 return 1;
171 /* If TARGET is in OP0 or OP1, check if anything in SEQ sets TARGET
172 besides the last insn. */
173 if (reg_overlap_mentioned_p (target, op0)
174 || (op1 && reg_overlap_mentioned_p (target, op1)))
176 insn = PREV_INSN (last_insn);
177 while (insn != NULL_RTX)
179 if (reg_set_p (target, insn))
180 return 0;
182 insn = PREV_INSN (insn);
186 if (GET_RTX_CLASS (code) == RTX_UNARY)
187 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
188 else
189 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
191 set_unique_reg_note (last_insn, REG_EQUAL, note);
193 return 1;
196 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
197 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
198 not actually do a sign-extend or zero-extend, but can leave the
199 higher-order bits of the result rtx undefined, for example, in the case
200 of logical operations, but not right shifts. */
202 static rtx
203 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
204 int unsignedp, int no_extend)
206 rtx result;
208 /* If we don't have to extend and this is a constant, return it. */
209 if (no_extend && GET_MODE (op) == VOIDmode)
210 return op;
212 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
213 extend since it will be more efficient to do so unless the signedness of
214 a promoted object differs from our extension. */
215 if (! no_extend
216 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
217 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
218 return convert_modes (mode, oldmode, op, unsignedp);
220 /* If MODE is no wider than a single word, we return a paradoxical
221 SUBREG. */
222 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
223 return gen_rtx_SUBREG (mode, force_reg (GET_MODE (op), op), 0);
225 /* Otherwise, get an object of MODE, clobber it, and set the low-order
226 part to OP. */
228 result = gen_reg_rtx (mode);
229 emit_insn (gen_rtx_CLOBBER (VOIDmode, result));
230 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
231 return result;
234 /* Return the optab used for computing the operation given by
235 the tree code, CODE. This function is not always usable (for
236 example, it cannot give complete results for multiplication
237 or division) but probably ought to be relied on more widely
238 throughout the expander. */
239 optab
240 optab_for_tree_code (enum tree_code code, tree type)
242 bool trapv;
243 switch (code)
245 case BIT_AND_EXPR:
246 return and_optab;
248 case BIT_IOR_EXPR:
249 return ior_optab;
251 case BIT_NOT_EXPR:
252 return one_cmpl_optab;
254 case BIT_XOR_EXPR:
255 return xor_optab;
257 case TRUNC_MOD_EXPR:
258 case CEIL_MOD_EXPR:
259 case FLOOR_MOD_EXPR:
260 case ROUND_MOD_EXPR:
261 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
263 case RDIV_EXPR:
264 case TRUNC_DIV_EXPR:
265 case CEIL_DIV_EXPR:
266 case FLOOR_DIV_EXPR:
267 case ROUND_DIV_EXPR:
268 case EXACT_DIV_EXPR:
269 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
271 case LSHIFT_EXPR:
272 return ashl_optab;
274 case RSHIFT_EXPR:
275 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
277 case LROTATE_EXPR:
278 return rotl_optab;
280 case RROTATE_EXPR:
281 return rotr_optab;
283 case MAX_EXPR:
284 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
286 case MIN_EXPR:
287 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
289 default:
290 break;
293 trapv = flag_trapv && INTEGRAL_TYPE_P (type) && !TYPE_UNSIGNED (type);
294 switch (code)
296 case PLUS_EXPR:
297 return trapv ? addv_optab : add_optab;
299 case MINUS_EXPR:
300 return trapv ? subv_optab : sub_optab;
302 case MULT_EXPR:
303 return trapv ? smulv_optab : smul_optab;
305 case NEGATE_EXPR:
306 return trapv ? negv_optab : neg_optab;
308 case ABS_EXPR:
309 return trapv ? absv_optab : abs_optab;
311 default:
312 return NULL;
317 /* Wrapper around expand_binop which takes an rtx code to specify
318 the operation to perform, not an optab pointer. All other
319 arguments are the same. */
321 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
322 rtx op1, rtx target, int unsignedp,
323 enum optab_methods methods)
325 optab binop = code_to_optab[(int) code];
326 if (binop == 0)
327 abort ();
329 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
332 /* Generate code to perform an operation specified by BINOPTAB
333 on operands OP0 and OP1, with result having machine-mode MODE.
335 UNSIGNEDP is for the case where we have to widen the operands
336 to perform the operation. It says to use zero-extension.
338 If TARGET is nonzero, the value
339 is generated there, if it is convenient to do so.
340 In all cases an rtx is returned for the locus of the value;
341 this may or may not be TARGET. */
344 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
345 rtx target, int unsignedp, enum optab_methods methods)
347 enum optab_methods next_methods
348 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
349 ? OPTAB_WIDEN : methods);
350 enum mode_class class;
351 enum machine_mode wider_mode;
352 rtx temp;
353 int commutative_op = 0;
354 int shift_op = (binoptab->code == ASHIFT
355 || binoptab->code == ASHIFTRT
356 || binoptab->code == LSHIFTRT
357 || binoptab->code == ROTATE
358 || binoptab->code == ROTATERT);
359 rtx entry_last = get_last_insn ();
360 rtx last;
362 class = GET_MODE_CLASS (mode);
364 if (flag_force_mem)
366 /* Load duplicate non-volatile operands once. */
367 if (rtx_equal_p (op0, op1) && ! volatile_refs_p (op0))
369 op0 = force_not_mem (op0);
370 op1 = op0;
372 else
374 op0 = force_not_mem (op0);
375 op1 = force_not_mem (op1);
379 /* If subtracting an integer constant, convert this into an addition of
380 the negated constant. */
382 if (binoptab == sub_optab && GET_CODE (op1) == CONST_INT)
384 op1 = negate_rtx (mode, op1);
385 binoptab = add_optab;
388 /* If we are inside an appropriately-short loop and we are optimizing,
389 force expensive constants into a register. */
390 if (CONSTANT_P (op0) && optimize
391 && rtx_cost (op0, binoptab->code) > COSTS_N_INSNS (1))
392 op0 = force_reg (mode, op0);
394 if (CONSTANT_P (op1) && optimize
395 && ! shift_op && rtx_cost (op1, binoptab->code) > COSTS_N_INSNS (1))
396 op1 = force_reg (mode, op1);
398 /* Record where to delete back to if we backtrack. */
399 last = get_last_insn ();
401 /* If operation is commutative,
402 try to make the first operand a register.
403 Even better, try to make it the same as the target.
404 Also try to make the last operand a constant. */
405 if (GET_RTX_CLASS (binoptab->code) == RTX_COMM_ARITH
406 || binoptab == smul_widen_optab
407 || binoptab == umul_widen_optab
408 || binoptab == smul_highpart_optab
409 || binoptab == umul_highpart_optab)
411 commutative_op = 1;
413 if (((target == 0 || REG_P (target))
414 ? ((REG_P (op1)
415 && !REG_P (op0))
416 || target == op1)
417 : rtx_equal_p (op1, target))
418 || GET_CODE (op0) == CONST_INT)
420 temp = op1;
421 op1 = op0;
422 op0 = temp;
426 /* If we can do it with a three-operand insn, do so. */
428 if (methods != OPTAB_MUST_WIDEN
429 && binoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
431 int icode = (int) binoptab->handlers[(int) mode].insn_code;
432 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
433 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
434 rtx pat;
435 rtx xop0 = op0, xop1 = op1;
437 if (target)
438 temp = target;
439 else
440 temp = gen_reg_rtx (mode);
442 /* If it is a commutative operator and the modes would match
443 if we would swap the operands, we can save the conversions. */
444 if (commutative_op)
446 if (GET_MODE (op0) != mode0 && GET_MODE (op1) != mode1
447 && GET_MODE (op0) == mode1 && GET_MODE (op1) == mode0)
449 rtx tmp;
451 tmp = op0; op0 = op1; op1 = tmp;
452 tmp = xop0; xop0 = xop1; xop1 = tmp;
456 /* In case the insn wants input operands in modes different from
457 those of the actual operands, convert the operands. It would
458 seem that we don't need to convert CONST_INTs, but we do, so
459 that they're properly zero-extended, sign-extended or truncated
460 for their mode. */
462 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
463 xop0 = convert_modes (mode0,
464 GET_MODE (op0) != VOIDmode
465 ? GET_MODE (op0)
466 : mode,
467 xop0, unsignedp);
469 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
470 xop1 = convert_modes (mode1,
471 GET_MODE (op1) != VOIDmode
472 ? GET_MODE (op1)
473 : mode,
474 xop1, unsignedp);
476 /* Now, if insn's predicates don't allow our operands, put them into
477 pseudo regs. */
479 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0)
480 && mode0 != VOIDmode)
481 xop0 = copy_to_mode_reg (mode0, xop0);
483 if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1)
484 && mode1 != VOIDmode)
485 xop1 = copy_to_mode_reg (mode1, xop1);
487 if (! (*insn_data[icode].operand[0].predicate) (temp, mode))
488 temp = gen_reg_rtx (mode);
490 pat = GEN_FCN (icode) (temp, xop0, xop1);
491 if (pat)
493 /* If PAT is composed of more than one insn, try to add an appropriate
494 REG_EQUAL note to it. If we can't because TEMP conflicts with an
495 operand, call ourselves again, this time without a target. */
496 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
497 && ! add_equal_note (pat, temp, binoptab->code, xop0, xop1))
499 delete_insns_since (last);
500 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
501 unsignedp, methods);
504 emit_insn (pat);
505 return temp;
507 else
508 delete_insns_since (last);
511 /* If this is a multiply, see if we can do a widening operation that
512 takes operands of this mode and makes a wider mode. */
514 if (binoptab == smul_optab && GET_MODE_WIDER_MODE (mode) != VOIDmode
515 && (((unsignedp ? umul_widen_optab : smul_widen_optab)
516 ->handlers[(int) GET_MODE_WIDER_MODE (mode)].insn_code)
517 != CODE_FOR_nothing))
519 temp = expand_binop (GET_MODE_WIDER_MODE (mode),
520 unsignedp ? umul_widen_optab : smul_widen_optab,
521 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
523 if (temp != 0)
525 if (GET_MODE_CLASS (mode) == MODE_INT)
526 return gen_lowpart (mode, temp);
527 else
528 return convert_to_mode (mode, temp, unsignedp);
532 /* Look for a wider mode of the same class for which we think we
533 can open-code the operation. Check for a widening multiply at the
534 wider mode as well. */
536 if ((class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
537 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
538 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
539 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
541 if (binoptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing
542 || (binoptab == smul_optab
543 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
544 && (((unsignedp ? umul_widen_optab : smul_widen_optab)
545 ->handlers[(int) GET_MODE_WIDER_MODE (wider_mode)].insn_code)
546 != CODE_FOR_nothing)))
548 rtx xop0 = op0, xop1 = op1;
549 int no_extend = 0;
551 /* For certain integer operations, we need not actually extend
552 the narrow operands, as long as we will truncate
553 the results to the same narrowness. */
555 if ((binoptab == ior_optab || binoptab == and_optab
556 || binoptab == xor_optab
557 || binoptab == add_optab || binoptab == sub_optab
558 || binoptab == smul_optab || binoptab == ashl_optab)
559 && class == MODE_INT)
560 no_extend = 1;
562 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
564 /* The second operand of a shift must always be extended. */
565 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
566 no_extend && binoptab != ashl_optab);
568 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
569 unsignedp, OPTAB_DIRECT);
570 if (temp)
572 if (class != MODE_INT)
574 if (target == 0)
575 target = gen_reg_rtx (mode);
576 convert_move (target, temp, 0);
577 return target;
579 else
580 return gen_lowpart (mode, temp);
582 else
583 delete_insns_since (last);
587 /* These can be done a word at a time. */
588 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
589 && class == MODE_INT
590 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
591 && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
593 int i;
594 rtx insns;
595 rtx equiv_value;
597 /* If TARGET is the same as one of the operands, the REG_EQUAL note
598 won't be accurate, so use a new target. */
599 if (target == 0 || target == op0 || target == op1)
600 target = gen_reg_rtx (mode);
602 start_sequence ();
604 /* Do the actual arithmetic. */
605 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
607 rtx target_piece = operand_subword (target, i, 1, mode);
608 rtx x = expand_binop (word_mode, binoptab,
609 operand_subword_force (op0, i, mode),
610 operand_subword_force (op1, i, mode),
611 target_piece, unsignedp, next_methods);
613 if (x == 0)
614 break;
616 if (target_piece != x)
617 emit_move_insn (target_piece, x);
620 insns = get_insns ();
621 end_sequence ();
623 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
625 if (binoptab->code != UNKNOWN)
626 equiv_value
627 = gen_rtx_fmt_ee (binoptab->code, mode,
628 copy_rtx (op0), copy_rtx (op1));
629 else
630 equiv_value = 0;
632 emit_no_conflict_block (insns, target, op0, op1, equiv_value);
633 return target;
637 /* Synthesize double word shifts from single word shifts. */
638 if ((binoptab == lshr_optab || binoptab == ashl_optab
639 || binoptab == ashr_optab)
640 && class == MODE_INT
641 && GET_CODE (op1) == CONST_INT
642 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
643 && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
644 && ashl_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
645 && lshr_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
647 rtx insns, inter, equiv_value;
648 rtx into_target, outof_target;
649 rtx into_input, outof_input;
650 int shift_count, left_shift, outof_word;
652 /* If TARGET is the same as one of the operands, the REG_EQUAL note
653 won't be accurate, so use a new target. */
654 if (target == 0 || target == op0 || target == op1)
655 target = gen_reg_rtx (mode);
657 start_sequence ();
659 shift_count = INTVAL (op1);
661 /* OUTOF_* is the word we are shifting bits away from, and
662 INTO_* is the word that we are shifting bits towards, thus
663 they differ depending on the direction of the shift and
664 WORDS_BIG_ENDIAN. */
666 left_shift = binoptab == ashl_optab;
667 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
669 outof_target = operand_subword (target, outof_word, 1, mode);
670 into_target = operand_subword (target, 1 - outof_word, 1, mode);
672 outof_input = operand_subword_force (op0, outof_word, mode);
673 into_input = operand_subword_force (op0, 1 - outof_word, mode);
675 if (shift_count >= BITS_PER_WORD)
677 inter = expand_binop (word_mode, binoptab,
678 outof_input,
679 GEN_INT (shift_count - BITS_PER_WORD),
680 into_target, unsignedp, next_methods);
682 if (inter != 0 && inter != into_target)
683 emit_move_insn (into_target, inter);
685 /* For a signed right shift, we must fill the word we are shifting
686 out of with copies of the sign bit. Otherwise it is zeroed. */
687 if (inter != 0 && binoptab != ashr_optab)
688 inter = CONST0_RTX (word_mode);
689 else if (inter != 0)
690 inter = expand_binop (word_mode, binoptab,
691 outof_input,
692 GEN_INT (BITS_PER_WORD - 1),
693 outof_target, unsignedp, next_methods);
695 if (inter != 0 && inter != outof_target)
696 emit_move_insn (outof_target, inter);
698 else
700 rtx carries;
701 optab reverse_unsigned_shift, unsigned_shift;
703 /* For a shift of less then BITS_PER_WORD, to compute the carry,
704 we must do a logical shift in the opposite direction of the
705 desired shift. */
707 reverse_unsigned_shift = (left_shift ? lshr_optab : ashl_optab);
709 /* For a shift of less than BITS_PER_WORD, to compute the word
710 shifted towards, we need to unsigned shift the orig value of
711 that word. */
713 unsigned_shift = (left_shift ? ashl_optab : lshr_optab);
715 carries = expand_binop (word_mode, reverse_unsigned_shift,
716 outof_input,
717 GEN_INT (BITS_PER_WORD - shift_count),
718 0, unsignedp, next_methods);
720 if (carries == 0)
721 inter = 0;
722 else
723 inter = expand_binop (word_mode, unsigned_shift, into_input,
724 op1, 0, unsignedp, next_methods);
726 if (inter != 0)
727 inter = expand_binop (word_mode, ior_optab, carries, inter,
728 into_target, unsignedp, next_methods);
730 if (inter != 0 && inter != into_target)
731 emit_move_insn (into_target, inter);
733 if (inter != 0)
734 inter = expand_binop (word_mode, binoptab, outof_input,
735 op1, outof_target, unsignedp, next_methods);
737 if (inter != 0 && inter != outof_target)
738 emit_move_insn (outof_target, inter);
741 insns = get_insns ();
742 end_sequence ();
744 if (inter != 0)
746 if (binoptab->code != UNKNOWN)
747 equiv_value = gen_rtx_fmt_ee (binoptab->code, mode, op0, op1);
748 else
749 equiv_value = 0;
751 emit_no_conflict_block (insns, target, op0, op1, equiv_value);
752 return target;
756 /* Synthesize double word rotates from single word shifts. */
757 if ((binoptab == rotl_optab || binoptab == rotr_optab)
758 && class == MODE_INT
759 && GET_CODE (op1) == CONST_INT
760 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
761 && ashl_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
762 && lshr_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
764 rtx insns, equiv_value;
765 rtx into_target, outof_target;
766 rtx into_input, outof_input;
767 rtx inter;
768 int shift_count, left_shift, outof_word;
770 /* If TARGET is the same as one of the operands, the REG_EQUAL note
771 won't be accurate, so use a new target. Do this also if target is not
772 a REG, first because having a register instead may open optimization
773 opportunities, and second because if target and op0 happen to be MEMs
774 designating the same location, we would risk clobbering it too early
775 in the code sequence we generate below. */
776 if (target == 0 || target == op0 || target == op1 || ! REG_P (target))
777 target = gen_reg_rtx (mode);
779 start_sequence ();
781 shift_count = INTVAL (op1);
783 /* OUTOF_* is the word we are shifting bits away from, and
784 INTO_* is the word that we are shifting bits towards, thus
785 they differ depending on the direction of the shift and
786 WORDS_BIG_ENDIAN. */
788 left_shift = (binoptab == rotl_optab);
789 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
791 outof_target = operand_subword (target, outof_word, 1, mode);
792 into_target = operand_subword (target, 1 - outof_word, 1, mode);
794 outof_input = operand_subword_force (op0, outof_word, mode);
795 into_input = operand_subword_force (op0, 1 - outof_word, mode);
797 if (shift_count == BITS_PER_WORD)
799 /* This is just a word swap. */
800 emit_move_insn (outof_target, into_input);
801 emit_move_insn (into_target, outof_input);
802 inter = const0_rtx;
804 else
806 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
807 rtx first_shift_count, second_shift_count;
808 optab reverse_unsigned_shift, unsigned_shift;
810 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
811 ? lshr_optab : ashl_optab);
813 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
814 ? ashl_optab : lshr_optab);
816 if (shift_count > BITS_PER_WORD)
818 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
819 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
821 else
823 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
824 second_shift_count = GEN_INT (shift_count);
827 into_temp1 = expand_binop (word_mode, unsigned_shift,
828 outof_input, first_shift_count,
829 NULL_RTX, unsignedp, next_methods);
830 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
831 into_input, second_shift_count,
832 NULL_RTX, unsignedp, next_methods);
834 if (into_temp1 != 0 && into_temp2 != 0)
835 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
836 into_target, unsignedp, next_methods);
837 else
838 inter = 0;
840 if (inter != 0 && inter != into_target)
841 emit_move_insn (into_target, inter);
843 outof_temp1 = expand_binop (word_mode, unsigned_shift,
844 into_input, first_shift_count,
845 NULL_RTX, unsignedp, next_methods);
846 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
847 outof_input, second_shift_count,
848 NULL_RTX, unsignedp, next_methods);
850 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
851 inter = expand_binop (word_mode, ior_optab,
852 outof_temp1, outof_temp2,
853 outof_target, unsignedp, next_methods);
855 if (inter != 0 && inter != outof_target)
856 emit_move_insn (outof_target, inter);
859 insns = get_insns ();
860 end_sequence ();
862 if (inter != 0)
864 if (binoptab->code != UNKNOWN)
865 equiv_value = gen_rtx_fmt_ee (binoptab->code, mode, op0, op1);
866 else
867 equiv_value = 0;
869 /* We can't make this a no conflict block if this is a word swap,
870 because the word swap case fails if the input and output values
871 are in the same register. */
872 if (shift_count != BITS_PER_WORD)
873 emit_no_conflict_block (insns, target, op0, op1, equiv_value);
874 else
875 emit_insn (insns);
878 return target;
882 /* These can be done a word at a time by propagating carries. */
883 if ((binoptab == add_optab || binoptab == sub_optab)
884 && class == MODE_INT
885 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
886 && binoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
888 unsigned int i;
889 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
890 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
891 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
892 rtx xop0, xop1, xtarget;
894 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
895 value is one of those, use it. Otherwise, use 1 since it is the
896 one easiest to get. */
897 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
898 int normalizep = STORE_FLAG_VALUE;
899 #else
900 int normalizep = 1;
901 #endif
903 /* Prepare the operands. */
904 xop0 = force_reg (mode, op0);
905 xop1 = force_reg (mode, op1);
907 xtarget = gen_reg_rtx (mode);
909 if (target == 0 || !REG_P (target))
910 target = xtarget;
912 /* Indicate for flow that the entire target reg is being set. */
913 if (REG_P (target))
914 emit_insn (gen_rtx_CLOBBER (VOIDmode, xtarget));
916 /* Do the actual arithmetic. */
917 for (i = 0; i < nwords; i++)
919 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
920 rtx target_piece = operand_subword (xtarget, index, 1, mode);
921 rtx op0_piece = operand_subword_force (xop0, index, mode);
922 rtx op1_piece = operand_subword_force (xop1, index, mode);
923 rtx x;
925 /* Main add/subtract of the input operands. */
926 x = expand_binop (word_mode, binoptab,
927 op0_piece, op1_piece,
928 target_piece, unsignedp, next_methods);
929 if (x == 0)
930 break;
932 if (i + 1 < nwords)
934 /* Store carry from main add/subtract. */
935 carry_out = gen_reg_rtx (word_mode);
936 carry_out = emit_store_flag_force (carry_out,
937 (binoptab == add_optab
938 ? LT : GT),
939 x, op0_piece,
940 word_mode, 1, normalizep);
943 if (i > 0)
945 rtx newx;
947 /* Add/subtract previous carry to main result. */
948 newx = expand_binop (word_mode,
949 normalizep == 1 ? binoptab : otheroptab,
950 x, carry_in,
951 NULL_RTX, 1, next_methods);
953 if (i + 1 < nwords)
955 /* Get out carry from adding/subtracting carry in. */
956 rtx carry_tmp = gen_reg_rtx (word_mode);
957 carry_tmp = emit_store_flag_force (carry_tmp,
958 (binoptab == add_optab
959 ? LT : GT),
960 newx, x,
961 word_mode, 1, normalizep);
963 /* Logical-ior the two poss. carry together. */
964 carry_out = expand_binop (word_mode, ior_optab,
965 carry_out, carry_tmp,
966 carry_out, 0, next_methods);
967 if (carry_out == 0)
968 break;
970 emit_move_insn (target_piece, newx);
973 carry_in = carry_out;
976 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
978 if (mov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing
979 || ! rtx_equal_p (target, xtarget))
981 rtx temp = emit_move_insn (target, xtarget);
983 set_unique_reg_note (temp,
984 REG_EQUAL,
985 gen_rtx_fmt_ee (binoptab->code, mode,
986 copy_rtx (xop0),
987 copy_rtx (xop1)));
989 else
990 target = xtarget;
992 return target;
995 else
996 delete_insns_since (last);
999 /* If we want to multiply two two-word values and have normal and widening
1000 multiplies of single-word values, we can do this with three smaller
1001 multiplications. Note that we do not make a REG_NO_CONFLICT block here
1002 because we are not operating on one word at a time.
1004 The multiplication proceeds as follows:
1005 _______________________
1006 [__op0_high_|__op0_low__]
1007 _______________________
1008 * [__op1_high_|__op1_low__]
1009 _______________________________________________
1010 _______________________
1011 (1) [__op0_low__*__op1_low__]
1012 _______________________
1013 (2a) [__op0_low__*__op1_high_]
1014 _______________________
1015 (2b) [__op0_high_*__op1_low__]
1016 _______________________
1017 (3) [__op0_high_*__op1_high_]
1020 This gives a 4-word result. Since we are only interested in the
1021 lower 2 words, partial result (3) and the upper words of (2a) and
1022 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1023 calculated using non-widening multiplication.
1025 (1), however, needs to be calculated with an unsigned widening
1026 multiplication. If this operation is not directly supported we
1027 try using a signed widening multiplication and adjust the result.
1028 This adjustment works as follows:
1030 If both operands are positive then no adjustment is needed.
1032 If the operands have different signs, for example op0_low < 0 and
1033 op1_low >= 0, the instruction treats the most significant bit of
1034 op0_low as a sign bit instead of a bit with significance
1035 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1036 with 2**BITS_PER_WORD - op0_low, and two's complements the
1037 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1038 the result.
1040 Similarly, if both operands are negative, we need to add
1041 (op0_low + op1_low) * 2**BITS_PER_WORD.
1043 We use a trick to adjust quickly. We logically shift op0_low right
1044 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1045 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1046 logical shift exists, we do an arithmetic right shift and subtract
1047 the 0 or -1. */
1049 if (binoptab == smul_optab
1050 && class == MODE_INT
1051 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1052 && smul_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1053 && add_optab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing
1054 && ((umul_widen_optab->handlers[(int) mode].insn_code
1055 != CODE_FOR_nothing)
1056 || (smul_widen_optab->handlers[(int) mode].insn_code
1057 != CODE_FOR_nothing)))
1059 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1060 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1061 rtx op0_high = operand_subword_force (op0, high, mode);
1062 rtx op0_low = operand_subword_force (op0, low, mode);
1063 rtx op1_high = operand_subword_force (op1, high, mode);
1064 rtx op1_low = operand_subword_force (op1, low, mode);
1065 rtx product = 0;
1066 rtx op0_xhigh = NULL_RTX;
1067 rtx op1_xhigh = NULL_RTX;
1069 /* If the target is the same as one of the inputs, don't use it. This
1070 prevents problems with the REG_EQUAL note. */
1071 if (target == op0 || target == op1
1072 || (target != 0 && !REG_P (target)))
1073 target = 0;
1075 /* Multiply the two lower words to get a double-word product.
1076 If unsigned widening multiplication is available, use that;
1077 otherwise use the signed form and compensate. */
1079 if (umul_widen_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1081 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1082 target, 1, OPTAB_DIRECT);
1084 /* If we didn't succeed, delete everything we did so far. */
1085 if (product == 0)
1086 delete_insns_since (last);
1087 else
1088 op0_xhigh = op0_high, op1_xhigh = op1_high;
1091 if (product == 0
1092 && smul_widen_optab->handlers[(int) mode].insn_code
1093 != CODE_FOR_nothing)
1095 rtx wordm1 = GEN_INT (BITS_PER_WORD - 1);
1096 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1097 target, 1, OPTAB_DIRECT);
1098 op0_xhigh = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1099 NULL_RTX, 1, next_methods);
1100 if (op0_xhigh)
1101 op0_xhigh = expand_binop (word_mode, add_optab, op0_high,
1102 op0_xhigh, op0_xhigh, 0, next_methods);
1103 else
1105 op0_xhigh = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1106 NULL_RTX, 0, next_methods);
1107 if (op0_xhigh)
1108 op0_xhigh = expand_binop (word_mode, sub_optab, op0_high,
1109 op0_xhigh, op0_xhigh, 0,
1110 next_methods);
1113 op1_xhigh = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1114 NULL_RTX, 1, next_methods);
1115 if (op1_xhigh)
1116 op1_xhigh = expand_binop (word_mode, add_optab, op1_high,
1117 op1_xhigh, op1_xhigh, 0, next_methods);
1118 else
1120 op1_xhigh = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1121 NULL_RTX, 0, next_methods);
1122 if (op1_xhigh)
1123 op1_xhigh = expand_binop (word_mode, sub_optab, op1_high,
1124 op1_xhigh, op1_xhigh, 0,
1125 next_methods);
1129 /* If we have been able to directly compute the product of the
1130 low-order words of the operands and perform any required adjustments
1131 of the operands, we proceed by trying two more multiplications
1132 and then computing the appropriate sum.
1134 We have checked above that the required addition is provided.
1135 Full-word addition will normally always succeed, especially if
1136 it is provided at all, so we don't worry about its failure. The
1137 multiplication may well fail, however, so we do handle that. */
1139 if (product && op0_xhigh && op1_xhigh)
1141 rtx product_high = operand_subword (product, high, 1, mode);
1142 rtx temp = expand_binop (word_mode, binoptab, op0_low, op1_xhigh,
1143 NULL_RTX, 0, OPTAB_DIRECT);
1145 if (!REG_P (product_high))
1146 product_high = force_reg (word_mode, product_high);
1148 if (temp != 0)
1149 temp = expand_binop (word_mode, add_optab, temp, product_high,
1150 product_high, 0, next_methods);
1152 if (temp != 0 && temp != product_high)
1153 emit_move_insn (product_high, temp);
1155 if (temp != 0)
1156 temp = expand_binop (word_mode, binoptab, op1_low, op0_xhigh,
1157 NULL_RTX, 0, OPTAB_DIRECT);
1159 if (temp != 0)
1160 temp = expand_binop (word_mode, add_optab, temp,
1161 product_high, product_high,
1162 0, next_methods);
1164 if (temp != 0 && temp != product_high)
1165 emit_move_insn (product_high, temp);
1167 emit_move_insn (operand_subword (product, high, 1, mode), product_high);
1169 if (temp != 0)
1171 if (mov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1173 temp = emit_move_insn (product, product);
1174 set_unique_reg_note (temp,
1175 REG_EQUAL,
1176 gen_rtx_fmt_ee (MULT, mode,
1177 copy_rtx (op0),
1178 copy_rtx (op1)));
1181 return product;
1185 /* If we get here, we couldn't do it for some reason even though we
1186 originally thought we could. Delete anything we've emitted in
1187 trying to do it. */
1189 delete_insns_since (last);
1192 /* It can't be open-coded in this mode.
1193 Use a library call if one is available and caller says that's ok. */
1195 if (binoptab->handlers[(int) mode].libfunc
1196 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
1198 rtx insns;
1199 rtx op1x = op1;
1200 enum machine_mode op1_mode = mode;
1201 rtx value;
1203 start_sequence ();
1205 if (shift_op)
1207 op1_mode = word_mode;
1208 /* Specify unsigned here,
1209 since negative shift counts are meaningless. */
1210 op1x = convert_to_mode (word_mode, op1, 1);
1213 if (GET_MODE (op0) != VOIDmode
1214 && GET_MODE (op0) != mode)
1215 op0 = convert_to_mode (mode, op0, unsignedp);
1217 /* Pass 1 for NO_QUEUE so we don't lose any increments
1218 if the libcall is cse'd or moved. */
1219 value = emit_library_call_value (binoptab->handlers[(int) mode].libfunc,
1220 NULL_RTX, LCT_CONST, mode, 2,
1221 op0, mode, op1x, op1_mode);
1223 insns = get_insns ();
1224 end_sequence ();
1226 target = gen_reg_rtx (mode);
1227 emit_libcall_block (insns, target, value,
1228 gen_rtx_fmt_ee (binoptab->code, mode, op0, op1));
1230 return target;
1233 delete_insns_since (last);
1235 /* It can't be done in this mode. Can we do it in a wider mode? */
1237 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
1238 || methods == OPTAB_MUST_WIDEN))
1240 /* Caller says, don't even try. */
1241 delete_insns_since (entry_last);
1242 return 0;
1245 /* Compute the value of METHODS to pass to recursive calls.
1246 Don't allow widening to be tried recursively. */
1248 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
1250 /* Look for a wider mode of the same class for which it appears we can do
1251 the operation. */
1253 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1255 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1256 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1258 if ((binoptab->handlers[(int) wider_mode].insn_code
1259 != CODE_FOR_nothing)
1260 || (methods == OPTAB_LIB
1261 && binoptab->handlers[(int) wider_mode].libfunc))
1263 rtx xop0 = op0, xop1 = op1;
1264 int no_extend = 0;
1266 /* For certain integer operations, we need not actually extend
1267 the narrow operands, as long as we will truncate
1268 the results to the same narrowness. */
1270 if ((binoptab == ior_optab || binoptab == and_optab
1271 || binoptab == xor_optab
1272 || binoptab == add_optab || binoptab == sub_optab
1273 || binoptab == smul_optab || binoptab == ashl_optab)
1274 && class == MODE_INT)
1275 no_extend = 1;
1277 xop0 = widen_operand (xop0, wider_mode, mode,
1278 unsignedp, no_extend);
1280 /* The second operand of a shift must always be extended. */
1281 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1282 no_extend && binoptab != ashl_optab);
1284 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1285 unsignedp, methods);
1286 if (temp)
1288 if (class != MODE_INT)
1290 if (target == 0)
1291 target = gen_reg_rtx (mode);
1292 convert_move (target, temp, 0);
1293 return target;
1295 else
1296 return gen_lowpart (mode, temp);
1298 else
1299 delete_insns_since (last);
1304 delete_insns_since (entry_last);
1305 return 0;
1308 /* Expand a binary operator which has both signed and unsigned forms.
1309 UOPTAB is the optab for unsigned operations, and SOPTAB is for
1310 signed operations.
1312 If we widen unsigned operands, we may use a signed wider operation instead
1313 of an unsigned wider operation, since the result would be the same. */
1316 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
1317 rtx op0, rtx op1, rtx target, int unsignedp,
1318 enum optab_methods methods)
1320 rtx temp;
1321 optab direct_optab = unsignedp ? uoptab : soptab;
1322 struct optab wide_soptab;
1324 /* Do it without widening, if possible. */
1325 temp = expand_binop (mode, direct_optab, op0, op1, target,
1326 unsignedp, OPTAB_DIRECT);
1327 if (temp || methods == OPTAB_DIRECT)
1328 return temp;
1330 /* Try widening to a signed int. Make a fake signed optab that
1331 hides any signed insn for direct use. */
1332 wide_soptab = *soptab;
1333 wide_soptab.handlers[(int) mode].insn_code = CODE_FOR_nothing;
1334 wide_soptab.handlers[(int) mode].libfunc = 0;
1336 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
1337 unsignedp, OPTAB_WIDEN);
1339 /* For unsigned operands, try widening to an unsigned int. */
1340 if (temp == 0 && unsignedp)
1341 temp = expand_binop (mode, uoptab, op0, op1, target,
1342 unsignedp, OPTAB_WIDEN);
1343 if (temp || methods == OPTAB_WIDEN)
1344 return temp;
1346 /* Use the right width lib call if that exists. */
1347 temp = expand_binop (mode, direct_optab, op0, op1, target, unsignedp, OPTAB_LIB);
1348 if (temp || methods == OPTAB_LIB)
1349 return temp;
1351 /* Must widen and use a lib call, use either signed or unsigned. */
1352 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
1353 unsignedp, methods);
1354 if (temp != 0)
1355 return temp;
1356 if (unsignedp)
1357 return expand_binop (mode, uoptab, op0, op1, target,
1358 unsignedp, methods);
1359 return 0;
1362 /* Generate code to perform an operation specified by UNOPPTAB
1363 on operand OP0, with two results to TARG0 and TARG1.
1364 We assume that the order of the operands for the instruction
1365 is TARG0, TARG1, OP0.
1367 Either TARG0 or TARG1 may be zero, but what that means is that
1368 the result is not actually wanted. We will generate it into
1369 a dummy pseudo-reg and discard it. They may not both be zero.
1371 Returns 1 if this operation can be performed; 0 if not. */
1374 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
1375 int unsignedp)
1377 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1378 enum mode_class class;
1379 enum machine_mode wider_mode;
1380 rtx entry_last = get_last_insn ();
1381 rtx last;
1383 class = GET_MODE_CLASS (mode);
1385 if (flag_force_mem)
1386 op0 = force_not_mem (op0);
1388 if (!targ0)
1389 targ0 = gen_reg_rtx (mode);
1390 if (!targ1)
1391 targ1 = gen_reg_rtx (mode);
1393 /* Record where to go back to if we fail. */
1394 last = get_last_insn ();
1396 if (unoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1398 int icode = (int) unoptab->handlers[(int) mode].insn_code;
1399 enum machine_mode mode0 = insn_data[icode].operand[2].mode;
1400 rtx pat;
1401 rtx xop0 = op0;
1403 if (GET_MODE (xop0) != VOIDmode
1404 && GET_MODE (xop0) != mode0)
1405 xop0 = convert_to_mode (mode0, xop0, unsignedp);
1407 /* Now, if insn doesn't accept these operands, put them into pseudos. */
1408 if (! (*insn_data[icode].operand[2].predicate) (xop0, mode0))
1409 xop0 = copy_to_mode_reg (mode0, xop0);
1411 /* We could handle this, but we should always be called with a pseudo
1412 for our targets and all insns should take them as outputs. */
1413 if (! (*insn_data[icode].operand[0].predicate) (targ0, mode)
1414 || ! (*insn_data[icode].operand[1].predicate) (targ1, mode))
1415 abort ();
1417 pat = GEN_FCN (icode) (targ0, targ1, xop0);
1418 if (pat)
1420 emit_insn (pat);
1421 return 1;
1423 else
1424 delete_insns_since (last);
1427 /* It can't be done in this mode. Can we do it in a wider mode? */
1429 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1431 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1432 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1434 if (unoptab->handlers[(int) wider_mode].insn_code
1435 != CODE_FOR_nothing)
1437 rtx t0 = gen_reg_rtx (wider_mode);
1438 rtx t1 = gen_reg_rtx (wider_mode);
1439 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
1441 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
1443 convert_move (targ0, t0, unsignedp);
1444 convert_move (targ1, t1, unsignedp);
1445 return 1;
1447 else
1448 delete_insns_since (last);
1453 delete_insns_since (entry_last);
1454 return 0;
1457 /* Generate code to perform an operation specified by BINOPTAB
1458 on operands OP0 and OP1, with two results to TARG1 and TARG2.
1459 We assume that the order of the operands for the instruction
1460 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
1461 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
1463 Either TARG0 or TARG1 may be zero, but what that means is that
1464 the result is not actually wanted. We will generate it into
1465 a dummy pseudo-reg and discard it. They may not both be zero.
1467 Returns 1 if this operation can be performed; 0 if not. */
1470 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
1471 int unsignedp)
1473 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
1474 enum mode_class class;
1475 enum machine_mode wider_mode;
1476 rtx entry_last = get_last_insn ();
1477 rtx last;
1479 class = GET_MODE_CLASS (mode);
1481 if (flag_force_mem)
1483 op0 = force_not_mem (op0);
1484 op1 = force_not_mem (op1);
1487 /* If we are inside an appropriately-short loop and we are optimizing,
1488 force expensive constants into a register. */
1489 if (CONSTANT_P (op0) && optimize
1490 && rtx_cost (op0, binoptab->code) > COSTS_N_INSNS (1))
1491 op0 = force_reg (mode, op0);
1493 if (CONSTANT_P (op1) && optimize
1494 && rtx_cost (op1, binoptab->code) > COSTS_N_INSNS (1))
1495 op1 = force_reg (mode, op1);
1497 if (!targ0)
1498 targ0 = gen_reg_rtx (mode);
1499 if (!targ1)
1500 targ1 = gen_reg_rtx (mode);
1502 /* Record where to go back to if we fail. */
1503 last = get_last_insn ();
1505 if (binoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1507 int icode = (int) binoptab->handlers[(int) mode].insn_code;
1508 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
1509 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
1510 rtx pat;
1511 rtx xop0 = op0, xop1 = op1;
1513 /* In case the insn wants input operands in modes different from
1514 those of the actual operands, convert the operands. It would
1515 seem that we don't need to convert CONST_INTs, but we do, so
1516 that they're properly zero-extended, sign-extended or truncated
1517 for their mode. */
1519 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
1520 xop0 = convert_modes (mode0,
1521 GET_MODE (op0) != VOIDmode
1522 ? GET_MODE (op0)
1523 : mode,
1524 xop0, unsignedp);
1526 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
1527 xop1 = convert_modes (mode1,
1528 GET_MODE (op1) != VOIDmode
1529 ? GET_MODE (op1)
1530 : mode,
1531 xop1, unsignedp);
1533 /* Now, if insn doesn't accept these operands, put them into pseudos. */
1534 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0))
1535 xop0 = copy_to_mode_reg (mode0, xop0);
1537 if (! (*insn_data[icode].operand[2].predicate) (xop1, mode1))
1538 xop1 = copy_to_mode_reg (mode1, xop1);
1540 /* We could handle this, but we should always be called with a pseudo
1541 for our targets and all insns should take them as outputs. */
1542 if (! (*insn_data[icode].operand[0].predicate) (targ0, mode)
1543 || ! (*insn_data[icode].operand[3].predicate) (targ1, mode))
1544 abort ();
1546 pat = GEN_FCN (icode) (targ0, xop0, xop1, targ1);
1547 if (pat)
1549 emit_insn (pat);
1550 return 1;
1552 else
1553 delete_insns_since (last);
1556 /* It can't be done in this mode. Can we do it in a wider mode? */
1558 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1560 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1561 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1563 if (binoptab->handlers[(int) wider_mode].insn_code
1564 != CODE_FOR_nothing)
1566 rtx t0 = gen_reg_rtx (wider_mode);
1567 rtx t1 = gen_reg_rtx (wider_mode);
1568 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
1569 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
1571 if (expand_twoval_binop (binoptab, cop0, cop1,
1572 t0, t1, unsignedp))
1574 convert_move (targ0, t0, unsignedp);
1575 convert_move (targ1, t1, unsignedp);
1576 return 1;
1578 else
1579 delete_insns_since (last);
1584 delete_insns_since (entry_last);
1585 return 0;
1588 /* Expand the two-valued library call indicated by BINOPTAB, but
1589 preserve only one of the values. If TARG0 is non-NULL, the first
1590 value is placed into TARG0; otherwise the second value is placed
1591 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
1592 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
1593 This routine assumes that the value returned by the library call is
1594 as if the return value was of an integral mode twice as wide as the
1595 mode of OP0. Returns 1 if the call was successful. */
1597 bool
1598 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
1599 rtx targ0, rtx targ1, enum rtx_code code)
1601 enum machine_mode mode;
1602 enum machine_mode libval_mode;
1603 rtx libval;
1604 rtx insns;
1606 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
1607 if (!((targ0 != NULL_RTX) ^ (targ1 != NULL_RTX)))
1608 abort ();
1610 mode = GET_MODE (op0);
1611 if (!binoptab->handlers[(int) mode].libfunc)
1612 return false;
1614 /* The value returned by the library function will have twice as
1615 many bits as the nominal MODE. */
1616 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
1617 MODE_INT);
1618 start_sequence ();
1619 libval = emit_library_call_value (binoptab->handlers[(int) mode].libfunc,
1620 NULL_RTX, LCT_CONST,
1621 libval_mode, 2,
1622 op0, mode,
1623 op1, mode);
1624 /* Get the part of VAL containing the value that we want. */
1625 libval = simplify_gen_subreg (mode, libval, libval_mode,
1626 targ0 ? 0 : GET_MODE_SIZE (mode));
1627 insns = get_insns ();
1628 end_sequence ();
1629 /* Move the into the desired location. */
1630 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
1631 gen_rtx_fmt_ee (code, mode, op0, op1));
1633 return true;
1637 /* Wrapper around expand_unop which takes an rtx code to specify
1638 the operation to perform, not an optab pointer. All other
1639 arguments are the same. */
1641 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
1642 rtx target, int unsignedp)
1644 optab unop = code_to_optab[(int) code];
1645 if (unop == 0)
1646 abort ();
1648 return expand_unop (mode, unop, op0, target, unsignedp);
1651 /* Try calculating
1652 (clz:narrow x)
1654 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)). */
1655 static rtx
1656 widen_clz (enum machine_mode mode, rtx op0, rtx target)
1658 enum mode_class class = GET_MODE_CLASS (mode);
1659 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1661 enum machine_mode wider_mode;
1662 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1663 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1665 if (clz_optab->handlers[(int) wider_mode].insn_code
1666 != CODE_FOR_nothing)
1668 rtx xop0, temp, last;
1670 last = get_last_insn ();
1672 if (target == 0)
1673 target = gen_reg_rtx (mode);
1674 xop0 = widen_operand (op0, wider_mode, mode, true, false);
1675 temp = expand_unop (wider_mode, clz_optab, xop0, NULL_RTX, true);
1676 if (temp != 0)
1677 temp = expand_binop (wider_mode, sub_optab, temp,
1678 GEN_INT (GET_MODE_BITSIZE (wider_mode)
1679 - GET_MODE_BITSIZE (mode)),
1680 target, true, OPTAB_DIRECT);
1681 if (temp == 0)
1682 delete_insns_since (last);
1684 return temp;
1688 return 0;
1691 /* Try calculating (parity x) as (and (popcount x) 1), where
1692 popcount can also be done in a wider mode. */
1693 static rtx
1694 expand_parity (enum machine_mode mode, rtx op0, rtx target)
1696 enum mode_class class = GET_MODE_CLASS (mode);
1697 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1699 enum machine_mode wider_mode;
1700 for (wider_mode = mode; wider_mode != VOIDmode;
1701 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1703 if (popcount_optab->handlers[(int) wider_mode].insn_code
1704 != CODE_FOR_nothing)
1706 rtx xop0, temp, last;
1708 last = get_last_insn ();
1710 if (target == 0)
1711 target = gen_reg_rtx (mode);
1712 xop0 = widen_operand (op0, wider_mode, mode, true, false);
1713 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
1714 true);
1715 if (temp != 0)
1716 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
1717 target, true, OPTAB_DIRECT);
1718 if (temp == 0)
1719 delete_insns_since (last);
1721 return temp;
1725 return 0;
1728 /* Generate code to perform an operation specified by UNOPTAB
1729 on operand OP0, with result having machine-mode MODE.
1731 UNSIGNEDP is for the case where we have to widen the operands
1732 to perform the operation. It says to use zero-extension.
1734 If TARGET is nonzero, the value
1735 is generated there, if it is convenient to do so.
1736 In all cases an rtx is returned for the locus of the value;
1737 this may or may not be TARGET. */
1740 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
1741 int unsignedp)
1743 enum mode_class class;
1744 enum machine_mode wider_mode;
1745 rtx temp;
1746 rtx last = get_last_insn ();
1747 rtx pat;
1749 class = GET_MODE_CLASS (mode);
1751 if (flag_force_mem)
1752 op0 = force_not_mem (op0);
1754 if (unoptab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
1756 int icode = (int) unoptab->handlers[(int) mode].insn_code;
1757 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
1758 rtx xop0 = op0;
1760 if (target)
1761 temp = target;
1762 else
1763 temp = gen_reg_rtx (mode);
1765 if (GET_MODE (xop0) != VOIDmode
1766 && GET_MODE (xop0) != mode0)
1767 xop0 = convert_to_mode (mode0, xop0, unsignedp);
1769 /* Now, if insn doesn't accept our operand, put it into a pseudo. */
1771 if (! (*insn_data[icode].operand[1].predicate) (xop0, mode0))
1772 xop0 = copy_to_mode_reg (mode0, xop0);
1774 if (! (*insn_data[icode].operand[0].predicate) (temp, mode))
1775 temp = gen_reg_rtx (mode);
1777 pat = GEN_FCN (icode) (temp, xop0);
1778 if (pat)
1780 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1781 && ! add_equal_note (pat, temp, unoptab->code, xop0, NULL_RTX))
1783 delete_insns_since (last);
1784 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
1787 emit_insn (pat);
1789 return temp;
1791 else
1792 delete_insns_since (last);
1795 /* It can't be done in this mode. Can we open-code it in a wider mode? */
1797 /* Widening clz needs special treatment. */
1798 if (unoptab == clz_optab)
1800 temp = widen_clz (mode, op0, target);
1801 if (temp)
1802 return temp;
1803 else
1804 goto try_libcall;
1807 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1808 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1809 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1811 if (unoptab->handlers[(int) wider_mode].insn_code != CODE_FOR_nothing)
1813 rtx xop0 = op0;
1815 /* For certain operations, we need not actually extend
1816 the narrow operand, as long as we will truncate the
1817 results to the same narrowness. */
1819 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
1820 (unoptab == neg_optab
1821 || unoptab == one_cmpl_optab)
1822 && class == MODE_INT);
1824 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
1825 unsignedp);
1827 if (temp)
1829 if (class != MODE_INT)
1831 if (target == 0)
1832 target = gen_reg_rtx (mode);
1833 convert_move (target, temp, 0);
1834 return target;
1836 else
1837 return gen_lowpart (mode, temp);
1839 else
1840 delete_insns_since (last);
1844 /* These can be done a word at a time. */
1845 if (unoptab == one_cmpl_optab
1846 && class == MODE_INT
1847 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1848 && unoptab->handlers[(int) word_mode].insn_code != CODE_FOR_nothing)
1850 int i;
1851 rtx insns;
1853 if (target == 0 || target == op0)
1854 target = gen_reg_rtx (mode);
1856 start_sequence ();
1858 /* Do the actual arithmetic. */
1859 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1861 rtx target_piece = operand_subword (target, i, 1, mode);
1862 rtx x = expand_unop (word_mode, unoptab,
1863 operand_subword_force (op0, i, mode),
1864 target_piece, unsignedp);
1866 if (target_piece != x)
1867 emit_move_insn (target_piece, x);
1870 insns = get_insns ();
1871 end_sequence ();
1873 emit_no_conflict_block (insns, target, op0, NULL_RTX,
1874 gen_rtx_fmt_e (unoptab->code, mode,
1875 copy_rtx (op0)));
1876 return target;
1879 /* Try negating floating point values by flipping the sign bit. */
1880 if (unoptab->code == NEG && class == MODE_FLOAT
1881 && GET_MODE_BITSIZE (mode) <= 2 * HOST_BITS_PER_WIDE_INT)
1883 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
1884 enum machine_mode imode = int_mode_for_mode (mode);
1885 int bitpos = (fmt != 0) ? fmt->signbit : -1;
1887 if (imode != BLKmode && bitpos >= 0 && fmt->has_signed_zero)
1889 HOST_WIDE_INT hi, lo;
1890 rtx last = get_last_insn ();
1892 /* Handle targets with different FP word orders. */
1893 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
1895 int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1896 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
1897 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
1900 if (bitpos < HOST_BITS_PER_WIDE_INT)
1902 hi = 0;
1903 lo = (HOST_WIDE_INT) 1 << bitpos;
1905 else
1907 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
1908 lo = 0;
1910 temp = expand_binop (imode, xor_optab,
1911 gen_lowpart (imode, op0),
1912 immed_double_const (lo, hi, imode),
1913 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1914 if (temp != 0)
1916 rtx insn;
1917 if (target == 0)
1918 target = gen_reg_rtx (mode);
1919 insn = emit_move_insn (target, gen_lowpart (mode, temp));
1920 set_unique_reg_note (insn, REG_EQUAL,
1921 gen_rtx_fmt_e (NEG, mode,
1922 copy_rtx (op0)));
1923 return target;
1925 delete_insns_since (last);
1929 /* Try calculating parity (x) as popcount (x) % 2. */
1930 if (unoptab == parity_optab)
1932 temp = expand_parity (mode, op0, target);
1933 if (temp)
1934 return temp;
1937 /* If there is no negation pattern, try subtracting from zero. */
1938 if (unoptab == neg_optab && class == MODE_INT)
1940 temp = expand_binop (mode, sub_optab, CONST0_RTX (mode), op0,
1941 target, unsignedp, OPTAB_DIRECT);
1942 if (temp)
1943 return temp;
1946 try_libcall:
1947 /* Now try a library call in this mode. */
1948 if (unoptab->handlers[(int) mode].libfunc)
1950 rtx insns;
1951 rtx value;
1952 enum machine_mode outmode = mode;
1954 /* All of these functions return small values. Thus we choose to
1955 have them return something that isn't a double-word. */
1956 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
1957 || unoptab == popcount_optab || unoptab == parity_optab)
1958 outmode
1959 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node)));
1961 start_sequence ();
1963 /* Pass 1 for NO_QUEUE so we don't lose any increments
1964 if the libcall is cse'd or moved. */
1965 value = emit_library_call_value (unoptab->handlers[(int) mode].libfunc,
1966 NULL_RTX, LCT_CONST, outmode,
1967 1, op0, mode);
1968 insns = get_insns ();
1969 end_sequence ();
1971 target = gen_reg_rtx (outmode);
1972 emit_libcall_block (insns, target, value,
1973 gen_rtx_fmt_e (unoptab->code, mode, op0));
1975 return target;
1978 /* It can't be done in this mode. Can we do it in a wider mode? */
1980 if (class == MODE_INT || class == MODE_FLOAT || class == MODE_COMPLEX_FLOAT)
1982 for (wider_mode = GET_MODE_WIDER_MODE (mode); wider_mode != VOIDmode;
1983 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1985 if ((unoptab->handlers[(int) wider_mode].insn_code
1986 != CODE_FOR_nothing)
1987 || unoptab->handlers[(int) wider_mode].libfunc)
1989 rtx xop0 = op0;
1991 /* For certain operations, we need not actually extend
1992 the narrow operand, as long as we will truncate the
1993 results to the same narrowness. */
1995 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
1996 (unoptab == neg_optab
1997 || unoptab == one_cmpl_optab)
1998 && class == MODE_INT);
2000 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2001 unsignedp);
2003 /* If we are generating clz using wider mode, adjust the
2004 result. */
2005 if (unoptab == clz_optab && temp != 0)
2006 temp = expand_binop (wider_mode, sub_optab, temp,
2007 GEN_INT (GET_MODE_BITSIZE (wider_mode)
2008 - GET_MODE_BITSIZE (mode)),
2009 target, true, OPTAB_DIRECT);
2011 if (temp)
2013 if (class != MODE_INT)
2015 if (target == 0)
2016 target = gen_reg_rtx (mode);
2017 convert_move (target, temp, 0);
2018 return target;
2020 else
2021 return gen_lowpart (mode, temp);
2023 else
2024 delete_insns_since (last);
2029 /* If there is no negate operation, try doing a subtract from zero.
2030 The US Software GOFAST library needs this. FIXME: This is *wrong*
2031 for floating-point operations due to negative zeros! */
2032 if (unoptab->code == NEG)
2034 rtx temp;
2035 temp = expand_binop (mode,
2036 unoptab == negv_optab ? subv_optab : sub_optab,
2037 CONST0_RTX (mode), op0,
2038 target, unsignedp, OPTAB_LIB_WIDEN);
2039 if (temp)
2040 return temp;
2043 return 0;
2046 /* Emit code to compute the absolute value of OP0, with result to
2047 TARGET if convenient. (TARGET may be 0.) The return value says
2048 where the result actually is to be found.
2050 MODE is the mode of the operand; the mode of the result is
2051 different but can be deduced from MODE.
2056 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
2057 int result_unsignedp)
2059 rtx temp;
2061 if (! flag_trapv)
2062 result_unsignedp = 1;
2064 /* First try to do it with a special abs instruction. */
2065 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
2066 op0, target, 0);
2067 if (temp != 0)
2068 return temp;
2070 /* For floating point modes, try clearing the sign bit. */
2071 if (GET_MODE_CLASS (mode) == MODE_FLOAT
2072 && GET_MODE_BITSIZE (mode) <= 2 * HOST_BITS_PER_WIDE_INT)
2074 const struct real_format *fmt = REAL_MODE_FORMAT (mode);
2075 enum machine_mode imode = int_mode_for_mode (mode);
2076 int bitpos = (fmt != 0) ? fmt->signbit : -1;
2078 if (imode != BLKmode && bitpos >= 0)
2080 HOST_WIDE_INT hi, lo;
2081 rtx last = get_last_insn ();
2083 /* Handle targets with different FP word orders. */
2084 if (FLOAT_WORDS_BIG_ENDIAN != WORDS_BIG_ENDIAN)
2086 int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
2087 int word = nwords - (bitpos / BITS_PER_WORD) - 1;
2088 bitpos = word * BITS_PER_WORD + bitpos % BITS_PER_WORD;
2091 if (bitpos < HOST_BITS_PER_WIDE_INT)
2093 hi = 0;
2094 lo = (HOST_WIDE_INT) 1 << bitpos;
2096 else
2098 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
2099 lo = 0;
2101 temp = expand_binop (imode, and_optab,
2102 gen_lowpart (imode, op0),
2103 immed_double_const (~lo, ~hi, imode),
2104 NULL_RTX, 1, OPTAB_LIB_WIDEN);
2105 if (temp != 0)
2107 rtx insn;
2108 if (target == 0)
2109 target = gen_reg_rtx (mode);
2110 insn = emit_move_insn (target, gen_lowpart (mode, temp));
2111 set_unique_reg_note (insn, REG_EQUAL,
2112 gen_rtx_fmt_e (ABS, mode,
2113 copy_rtx (op0)));
2114 return target;
2116 delete_insns_since (last);
2120 /* If we have a MAX insn, we can do this as MAX (x, -x). */
2121 if (smax_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2123 rtx last = get_last_insn ();
2125 temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
2126 if (temp != 0)
2127 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
2128 OPTAB_WIDEN);
2130 if (temp != 0)
2131 return temp;
2133 delete_insns_since (last);
2136 /* If this machine has expensive jumps, we can do integer absolute
2137 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
2138 where W is the width of MODE. */
2140 if (GET_MODE_CLASS (mode) == MODE_INT && BRANCH_COST >= 2)
2142 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
2143 size_int (GET_MODE_BITSIZE (mode) - 1),
2144 NULL_RTX, 0);
2146 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
2147 OPTAB_LIB_WIDEN);
2148 if (temp != 0)
2149 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
2150 temp, extended, target, 0, OPTAB_LIB_WIDEN);
2152 if (temp != 0)
2153 return temp;
2156 return NULL_RTX;
2160 expand_abs (enum machine_mode mode, rtx op0, rtx target,
2161 int result_unsignedp, int safe)
2163 rtx temp, op1;
2165 if (! flag_trapv)
2166 result_unsignedp = 1;
2168 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
2169 if (temp != 0)
2170 return temp;
2172 /* If that does not win, use conditional jump and negate. */
2174 /* It is safe to use the target if it is the same
2175 as the source if this is also a pseudo register */
2176 if (op0 == target && REG_P (op0)
2177 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
2178 safe = 1;
2180 op1 = gen_label_rtx ();
2181 if (target == 0 || ! safe
2182 || GET_MODE (target) != mode
2183 || (MEM_P (target) && MEM_VOLATILE_P (target))
2184 || (REG_P (target)
2185 && REGNO (target) < FIRST_PSEUDO_REGISTER))
2186 target = gen_reg_rtx (mode);
2188 emit_move_insn (target, op0);
2189 NO_DEFER_POP;
2191 /* If this mode is an integer too wide to compare properly,
2192 compare word by word. Rely on CSE to optimize constant cases. */
2193 if (GET_MODE_CLASS (mode) == MODE_INT
2194 && ! can_compare_p (GE, mode, ccp_jump))
2195 do_jump_by_parts_greater_rtx (mode, 0, target, const0_rtx,
2196 NULL_RTX, op1);
2197 else
2198 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
2199 NULL_RTX, NULL_RTX, op1);
2201 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
2202 target, target, 0);
2203 if (op0 != target)
2204 emit_move_insn (target, op0);
2205 emit_label (op1);
2206 OK_DEFER_POP;
2207 return target;
2210 /* Generate an instruction whose insn-code is INSN_CODE,
2211 with two operands: an output TARGET and an input OP0.
2212 TARGET *must* be nonzero, and the output is always stored there.
2213 CODE is an rtx code such that (CODE OP0) is an rtx that describes
2214 the value that is stored into TARGET. */
2216 void
2217 emit_unop_insn (int icode, rtx target, rtx op0, enum rtx_code code)
2219 rtx temp;
2220 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2221 rtx pat;
2223 temp = target;
2225 /* Sign and zero extension from memory is often done specially on
2226 RISC machines, so forcing into a register here can pessimize
2227 code. */
2228 if (flag_force_mem && code != SIGN_EXTEND && code != ZERO_EXTEND)
2229 op0 = force_not_mem (op0);
2231 /* Now, if insn does not accept our operands, put them into pseudos. */
2233 if (! (*insn_data[icode].operand[1].predicate) (op0, mode0))
2234 op0 = copy_to_mode_reg (mode0, op0);
2236 if (! (*insn_data[icode].operand[0].predicate) (temp, GET_MODE (temp))
2237 || (flag_force_mem && MEM_P (temp)))
2238 temp = gen_reg_rtx (GET_MODE (temp));
2240 pat = GEN_FCN (icode) (temp, op0);
2242 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
2243 add_equal_note (pat, temp, code, op0, NULL_RTX);
2245 emit_insn (pat);
2247 if (temp != target)
2248 emit_move_insn (target, temp);
2251 /* Emit code to perform a series of operations on a multi-word quantity, one
2252 word at a time.
2254 Such a block is preceded by a CLOBBER of the output, consists of multiple
2255 insns, each setting one word of the output, and followed by a SET copying
2256 the output to itself.
2258 Each of the insns setting words of the output receives a REG_NO_CONFLICT
2259 note indicating that it doesn't conflict with the (also multi-word)
2260 inputs. The entire block is surrounded by REG_LIBCALL and REG_RETVAL
2261 notes.
2263 INSNS is a block of code generated to perform the operation, not including
2264 the CLOBBER and final copy. All insns that compute intermediate values
2265 are first emitted, followed by the block as described above.
2267 TARGET, OP0, and OP1 are the output and inputs of the operations,
2268 respectively. OP1 may be zero for a unary operation.
2270 EQUIV, if nonzero, is an expression to be placed into a REG_EQUAL note
2271 on the last insn.
2273 If TARGET is not a register, INSNS is simply emitted with no special
2274 processing. Likewise if anything in INSNS is not an INSN or if
2275 there is a libcall block inside INSNS.
2277 The final insn emitted is returned. */
2280 emit_no_conflict_block (rtx insns, rtx target, rtx op0, rtx op1, rtx equiv)
2282 rtx prev, next, first, last, insn;
2284 if (!REG_P (target) || reload_in_progress)
2285 return emit_insn (insns);
2286 else
2287 for (insn = insns; insn; insn = NEXT_INSN (insn))
2288 if (!NONJUMP_INSN_P (insn)
2289 || find_reg_note (insn, REG_LIBCALL, NULL_RTX))
2290 return emit_insn (insns);
2292 /* First emit all insns that do not store into words of the output and remove
2293 these from the list. */
2294 for (insn = insns; insn; insn = next)
2296 rtx set = 0, note;
2297 int i;
2299 next = NEXT_INSN (insn);
2301 /* Some ports (cris) create a libcall regions at their own. We must
2302 avoid any potential nesting of LIBCALLs. */
2303 if ((note = find_reg_note (insn, REG_LIBCALL, NULL)) != NULL)
2304 remove_note (insn, note);
2305 if ((note = find_reg_note (insn, REG_RETVAL, NULL)) != NULL)
2306 remove_note (insn, note);
2308 if (GET_CODE (PATTERN (insn)) == SET || GET_CODE (PATTERN (insn)) == USE
2309 || GET_CODE (PATTERN (insn)) == CLOBBER)
2310 set = PATTERN (insn);
2311 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2313 for (i = 0; i < XVECLEN (PATTERN (insn), 0); i++)
2314 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
2316 set = XVECEXP (PATTERN (insn), 0, i);
2317 break;
2321 if (set == 0)
2322 abort ();
2324 if (! reg_overlap_mentioned_p (target, SET_DEST (set)))
2326 if (PREV_INSN (insn))
2327 NEXT_INSN (PREV_INSN (insn)) = next;
2328 else
2329 insns = next;
2331 if (next)
2332 PREV_INSN (next) = PREV_INSN (insn);
2334 add_insn (insn);
2338 prev = get_last_insn ();
2340 /* Now write the CLOBBER of the output, followed by the setting of each
2341 of the words, followed by the final copy. */
2342 if (target != op0 && target != op1)
2343 emit_insn (gen_rtx_CLOBBER (VOIDmode, target));
2345 for (insn = insns; insn; insn = next)
2347 next = NEXT_INSN (insn);
2348 add_insn (insn);
2350 if (op1 && REG_P (op1))
2351 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_NO_CONFLICT, op1,
2352 REG_NOTES (insn));
2354 if (op0 && REG_P (op0))
2355 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_NO_CONFLICT, op0,
2356 REG_NOTES (insn));
2359 if (mov_optab->handlers[(int) GET_MODE (target)].insn_code
2360 != CODE_FOR_nothing)
2362 last = emit_move_insn (target, target);
2363 if (equiv)
2364 set_unique_reg_note (last, REG_EQUAL, equiv);
2366 else
2368 last = get_last_insn ();
2370 /* Remove any existing REG_EQUAL note from "last", or else it will
2371 be mistaken for a note referring to the full contents of the
2372 alleged libcall value when found together with the REG_RETVAL
2373 note added below. An existing note can come from an insn
2374 expansion at "last". */
2375 remove_note (last, find_reg_note (last, REG_EQUAL, NULL_RTX));
2378 if (prev == 0)
2379 first = get_insns ();
2380 else
2381 first = NEXT_INSN (prev);
2383 /* Encapsulate the block so it gets manipulated as a unit. */
2384 REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
2385 REG_NOTES (first));
2386 REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first, REG_NOTES (last));
2388 return last;
2391 /* Emit code to make a call to a constant function or a library call.
2393 INSNS is a list containing all insns emitted in the call.
2394 These insns leave the result in RESULT. Our block is to copy RESULT
2395 to TARGET, which is logically equivalent to EQUIV.
2397 We first emit any insns that set a pseudo on the assumption that these are
2398 loading constants into registers; doing so allows them to be safely cse'ed
2399 between blocks. Then we emit all the other insns in the block, followed by
2400 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
2401 note with an operand of EQUIV.
2403 Moving assignments to pseudos outside of the block is done to improve
2404 the generated code, but is not required to generate correct code,
2405 hence being unable to move an assignment is not grounds for not making
2406 a libcall block. There are two reasons why it is safe to leave these
2407 insns inside the block: First, we know that these pseudos cannot be
2408 used in generated RTL outside the block since they are created for
2409 temporary purposes within the block. Second, CSE will not record the
2410 values of anything set inside a libcall block, so we know they must
2411 be dead at the end of the block.
2413 Except for the first group of insns (the ones setting pseudos), the
2414 block is delimited by REG_RETVAL and REG_LIBCALL notes. */
2416 void
2417 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
2419 rtx final_dest = target;
2420 rtx prev, next, first, last, insn;
2422 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
2423 into a MEM later. Protect the libcall block from this change. */
2424 if (! REG_P (target) || REG_USERVAR_P (target))
2425 target = gen_reg_rtx (GET_MODE (target));
2427 /* If we're using non-call exceptions, a libcall corresponding to an
2428 operation that may trap may also trap. */
2429 if (flag_non_call_exceptions && may_trap_p (equiv))
2431 for (insn = insns; insn; insn = NEXT_INSN (insn))
2432 if (CALL_P (insn))
2434 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2436 if (note != 0 && INTVAL (XEXP (note, 0)) <= 0)
2437 remove_note (insn, note);
2440 else
2441 /* look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
2442 reg note to indicate that this call cannot throw or execute a nonlocal
2443 goto (unless there is already a REG_EH_REGION note, in which case
2444 we update it). */
2445 for (insn = insns; insn; insn = NEXT_INSN (insn))
2446 if (CALL_P (insn))
2448 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
2450 if (note != 0)
2451 XEXP (note, 0) = constm1_rtx;
2452 else
2453 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EH_REGION, constm1_rtx,
2454 REG_NOTES (insn));
2457 /* First emit all insns that set pseudos. Remove them from the list as
2458 we go. Avoid insns that set pseudos which were referenced in previous
2459 insns. These can be generated by move_by_pieces, for example,
2460 to update an address. Similarly, avoid insns that reference things
2461 set in previous insns. */
2463 for (insn = insns; insn; insn = next)
2465 rtx set = single_set (insn);
2466 rtx note;
2468 /* Some ports (cris) create a libcall regions at their own. We must
2469 avoid any potential nesting of LIBCALLs. */
2470 if ((note = find_reg_note (insn, REG_LIBCALL, NULL)) != NULL)
2471 remove_note (insn, note);
2472 if ((note = find_reg_note (insn, REG_RETVAL, NULL)) != NULL)
2473 remove_note (insn, note);
2475 next = NEXT_INSN (insn);
2477 if (set != 0 && REG_P (SET_DEST (set))
2478 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
2479 && (insn == insns
2480 || ((! INSN_P(insns)
2481 || ! reg_mentioned_p (SET_DEST (set), PATTERN (insns)))
2482 && ! reg_used_between_p (SET_DEST (set), insns, insn)
2483 && ! modified_in_p (SET_SRC (set), insns)
2484 && ! modified_between_p (SET_SRC (set), insns, insn))))
2486 if (PREV_INSN (insn))
2487 NEXT_INSN (PREV_INSN (insn)) = next;
2488 else
2489 insns = next;
2491 if (next)
2492 PREV_INSN (next) = PREV_INSN (insn);
2494 add_insn (insn);
2497 /* Some ports use a loop to copy large arguments onto the stack.
2498 Don't move anything outside such a loop. */
2499 if (LABEL_P (insn))
2500 break;
2503 prev = get_last_insn ();
2505 /* Write the remaining insns followed by the final copy. */
2507 for (insn = insns; insn; insn = next)
2509 next = NEXT_INSN (insn);
2511 add_insn (insn);
2514 last = emit_move_insn (target, result);
2515 if (mov_optab->handlers[(int) GET_MODE (target)].insn_code
2516 != CODE_FOR_nothing)
2517 set_unique_reg_note (last, REG_EQUAL, copy_rtx (equiv));
2518 else
2520 /* Remove any existing REG_EQUAL note from "last", or else it will
2521 be mistaken for a note referring to the full contents of the
2522 libcall value when found together with the REG_RETVAL note added
2523 below. An existing note can come from an insn expansion at
2524 "last". */
2525 remove_note (last, find_reg_note (last, REG_EQUAL, NULL_RTX));
2528 if (final_dest != target)
2529 emit_move_insn (final_dest, target);
2531 if (prev == 0)
2532 first = get_insns ();
2533 else
2534 first = NEXT_INSN (prev);
2536 /* Encapsulate the block so it gets manipulated as a unit. */
2537 if (!flag_non_call_exceptions || !may_trap_p (equiv))
2539 /* We can't attach the REG_LIBCALL and REG_RETVAL notes
2540 when the encapsulated region would not be in one basic block,
2541 i.e. when there is a control_flow_insn_p insn between FIRST and LAST.
2543 bool attach_libcall_retval_notes = true;
2544 next = NEXT_INSN (last);
2545 for (insn = first; insn != next; insn = NEXT_INSN (insn))
2546 if (control_flow_insn_p (insn))
2548 attach_libcall_retval_notes = false;
2549 break;
2552 if (attach_libcall_retval_notes)
2554 REG_NOTES (first) = gen_rtx_INSN_LIST (REG_LIBCALL, last,
2555 REG_NOTES (first));
2556 REG_NOTES (last) = gen_rtx_INSN_LIST (REG_RETVAL, first,
2557 REG_NOTES (last));
2562 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
2563 PURPOSE describes how this comparison will be used. CODE is the rtx
2564 comparison code we will be using.
2566 ??? Actually, CODE is slightly weaker than that. A target is still
2567 required to implement all of the normal bcc operations, but not
2568 required to implement all (or any) of the unordered bcc operations. */
2571 can_compare_p (enum rtx_code code, enum machine_mode mode,
2572 enum can_compare_purpose purpose)
2576 if (cmp_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2578 if (purpose == ccp_jump)
2579 return bcc_gen_fctn[(int) code] != NULL;
2580 else if (purpose == ccp_store_flag)
2581 return setcc_gen_code[(int) code] != CODE_FOR_nothing;
2582 else
2583 /* There's only one cmov entry point, and it's allowed to fail. */
2584 return 1;
2586 if (purpose == ccp_jump
2587 && cbranch_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2588 return 1;
2589 if (purpose == ccp_cmov
2590 && cmov_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2591 return 1;
2592 if (purpose == ccp_store_flag
2593 && cstore_optab->handlers[(int) mode].insn_code != CODE_FOR_nothing)
2594 return 1;
2596 mode = GET_MODE_WIDER_MODE (mode);
2598 while (mode != VOIDmode);
2600 return 0;
2603 /* This function is called when we are going to emit a compare instruction that
2604 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
2606 *PMODE is the mode of the inputs (in case they are const_int).
2607 *PUNSIGNEDP nonzero says that the operands are unsigned;
2608 this matters if they need to be widened.
2610 If they have mode BLKmode, then SIZE specifies the size of both operands.
2612 This function performs all the setup necessary so that the caller only has
2613 to emit a single comparison insn. This setup can involve doing a BLKmode
2614 comparison or emitting a library call to perform the comparison if no insn
2615 is available to handle it.
2616 The values which are passed in through pointers can be modified; the caller
2617 should perform the comparison on the modified values. */
2619 static void
2620 prepare_cmp_insn (rtx *px, rtx *py, enum rtx_code *pcomparison, rtx size,
2621 enum machine_mode *pmode, int *punsignedp,
2622 enum can_compare_purpose purpose)
2624 enum machine_mode mode = *pmode;
2625 rtx x = *px, y = *py;
2626 int unsignedp = *punsignedp;
2627 enum mode_class class;
2629 class = GET_MODE_CLASS (mode);
2631 /* They could both be VOIDmode if both args are immediate constants,
2632 but we should fold that at an earlier stage.
2633 With no special code here, this will call abort,
2634 reminding the programmer to implement such folding. */
2636 if (mode != BLKmode && flag_force_mem)
2638 /* Load duplicate non-volatile operands once. */
2639 if (rtx_equal_p (x, y) && ! volatile_refs_p (x))
2641 x = force_not_mem (x);
2642 y = x;
2644 else
2646 x = force_not_mem (x);
2647 y = force_not_mem (y);
2651 /* If we are inside an appropriately-short loop and we are optimizing,
2652 force expensive constants into a register. */
2653 if (CONSTANT_P (x) && optimize
2654 && rtx_cost (x, COMPARE) > COSTS_N_INSNS (1))
2655 x = force_reg (mode, x);
2657 if (CONSTANT_P (y) && optimize
2658 && rtx_cost (y, COMPARE) > COSTS_N_INSNS (1))
2659 y = force_reg (mode, y);
2661 #ifdef HAVE_cc0
2662 /* Abort if we have a non-canonical comparison. The RTL documentation
2663 states that canonical comparisons are required only for targets which
2664 have cc0. */
2665 if (CONSTANT_P (x) && ! CONSTANT_P (y))
2666 abort ();
2667 #endif
2669 /* Don't let both operands fail to indicate the mode. */
2670 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
2671 x = force_reg (mode, x);
2673 /* Handle all BLKmode compares. */
2675 if (mode == BLKmode)
2677 enum machine_mode cmp_mode, result_mode;
2678 enum insn_code cmp_code;
2679 tree length_type;
2680 rtx libfunc;
2681 rtx result;
2682 rtx opalign
2683 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
2685 if (size == 0)
2686 abort ();
2688 /* Try to use a memory block compare insn - either cmpstr
2689 or cmpmem will do. */
2690 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2691 cmp_mode != VOIDmode;
2692 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
2694 cmp_code = cmpmem_optab[cmp_mode];
2695 if (cmp_code == CODE_FOR_nothing)
2696 cmp_code = cmpstr_optab[cmp_mode];
2697 if (cmp_code == CODE_FOR_nothing)
2698 continue;
2700 /* Must make sure the size fits the insn's mode. */
2701 if ((GET_CODE (size) == CONST_INT
2702 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
2703 || (GET_MODE_BITSIZE (GET_MODE (size))
2704 > GET_MODE_BITSIZE (cmp_mode)))
2705 continue;
2707 result_mode = insn_data[cmp_code].operand[0].mode;
2708 result = gen_reg_rtx (result_mode);
2709 size = convert_to_mode (cmp_mode, size, 1);
2710 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
2712 *px = result;
2713 *py = const0_rtx;
2714 *pmode = result_mode;
2715 return;
2718 /* Otherwise call a library function, memcmp. */
2719 libfunc = memcmp_libfunc;
2720 length_type = sizetype;
2721 result_mode = TYPE_MODE (integer_type_node);
2722 cmp_mode = TYPE_MODE (length_type);
2723 size = convert_to_mode (TYPE_MODE (length_type), size,
2724 TYPE_UNSIGNED (length_type));
2726 result = emit_library_call_value (libfunc, 0, LCT_PURE_MAKE_BLOCK,
2727 result_mode, 3,
2728 XEXP (x, 0), Pmode,
2729 XEXP (y, 0), Pmode,
2730 size, cmp_mode);
2731 *px = result;
2732 *py = const0_rtx;
2733 *pmode = result_mode;
2734 return;
2737 /* Don't allow operands to the compare to trap, as that can put the
2738 compare and branch in different basic blocks. */
2739 if (flag_non_call_exceptions)
2741 if (may_trap_p (x))
2742 x = force_reg (mode, x);
2743 if (may_trap_p (y))
2744 y = force_reg (mode, y);
2747 *px = x;
2748 *py = y;
2749 if (can_compare_p (*pcomparison, mode, purpose))
2750 return;
2752 /* Handle a lib call just for the mode we are using. */
2754 if (cmp_optab->handlers[(int) mode].libfunc && class != MODE_FLOAT)
2756 rtx libfunc = cmp_optab->handlers[(int) mode].libfunc;
2757 rtx result;
2759 /* If we want unsigned, and this mode has a distinct unsigned
2760 comparison routine, use that. */
2761 if (unsignedp && ucmp_optab->handlers[(int) mode].libfunc)
2762 libfunc = ucmp_optab->handlers[(int) mode].libfunc;
2764 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST_MAKE_BLOCK,
2765 word_mode, 2, x, mode, y, mode);
2767 *px = result;
2768 *pmode = word_mode;
2769 if (TARGET_LIB_INT_CMP_BIASED)
2770 /* Integer comparison returns a result that must be compared
2771 against 1, so that even if we do an unsigned compare
2772 afterward, there is still a value that can represent the
2773 result "less than". */
2774 *py = const1_rtx;
2775 else
2777 *py = const0_rtx;
2778 *punsignedp = 1;
2780 return;
2783 if (class == MODE_FLOAT)
2784 prepare_float_lib_cmp (px, py, pcomparison, pmode, punsignedp);
2786 else
2787 abort ();
2790 /* Before emitting an insn with code ICODE, make sure that X, which is going
2791 to be used for operand OPNUM of the insn, is converted from mode MODE to
2792 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
2793 that it is accepted by the operand predicate. Return the new value. */
2796 prepare_operand (int icode, rtx x, int opnum, enum machine_mode mode,
2797 enum machine_mode wider_mode, int unsignedp)
2799 if (mode != wider_mode)
2800 x = convert_modes (wider_mode, mode, x, unsignedp);
2802 if (! (*insn_data[icode].operand[opnum].predicate)
2803 (x, insn_data[icode].operand[opnum].mode))
2805 if (no_new_pseudos)
2806 return NULL_RTX;
2807 x = copy_to_mode_reg (insn_data[icode].operand[opnum].mode, x);
2810 return x;
2813 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
2814 we can do the comparison.
2815 The arguments are the same as for emit_cmp_and_jump_insns; but LABEL may
2816 be NULL_RTX which indicates that only a comparison is to be generated. */
2818 static void
2819 emit_cmp_and_jump_insn_1 (rtx x, rtx y, enum machine_mode mode,
2820 enum rtx_code comparison, int unsignedp, rtx label)
2822 rtx test = gen_rtx_fmt_ee (comparison, mode, x, y);
2823 enum mode_class class = GET_MODE_CLASS (mode);
2824 enum machine_mode wider_mode = mode;
2826 /* Try combined insns first. */
2829 enum insn_code icode;
2830 PUT_MODE (test, wider_mode);
2832 if (label)
2834 icode = cbranch_optab->handlers[(int) wider_mode].insn_code;
2836 if (icode != CODE_FOR_nothing
2837 && (*insn_data[icode].operand[0].predicate) (test, wider_mode))
2839 x = prepare_operand (icode, x, 1, mode, wider_mode, unsignedp);
2840 y = prepare_operand (icode, y, 2, mode, wider_mode, unsignedp);
2841 emit_jump_insn (GEN_FCN (icode) (test, x, y, label));
2842 return;
2846 /* Handle some compares against zero. */
2847 icode = (int) tst_optab->handlers[(int) wider_mode].insn_code;
2848 if (y == CONST0_RTX (mode) && icode != CODE_FOR_nothing)
2850 x = prepare_operand (icode, x, 0, mode, wider_mode, unsignedp);
2851 emit_insn (GEN_FCN (icode) (x));
2852 if (label)
2853 emit_jump_insn ((*bcc_gen_fctn[(int) comparison]) (label));
2854 return;
2857 /* Handle compares for which there is a directly suitable insn. */
2859 icode = (int) cmp_optab->handlers[(int) wider_mode].insn_code;
2860 if (icode != CODE_FOR_nothing)
2862 x = prepare_operand (icode, x, 0, mode, wider_mode, unsignedp);
2863 y = prepare_operand (icode, y, 1, mode, wider_mode, unsignedp);
2864 emit_insn (GEN_FCN (icode) (x, y));
2865 if (label)
2866 emit_jump_insn ((*bcc_gen_fctn[(int) comparison]) (label));
2867 return;
2870 if (class != MODE_INT && class != MODE_FLOAT
2871 && class != MODE_COMPLEX_FLOAT)
2872 break;
2874 wider_mode = GET_MODE_WIDER_MODE (wider_mode);
2876 while (wider_mode != VOIDmode);
2878 abort ();
2881 /* Generate code to compare X with Y so that the condition codes are
2882 set and to jump to LABEL if the condition is true. If X is a
2883 constant and Y is not a constant, then the comparison is swapped to
2884 ensure that the comparison RTL has the canonical form.
2886 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
2887 need to be widened by emit_cmp_insn. UNSIGNEDP is also used to select
2888 the proper branch condition code.
2890 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
2892 MODE is the mode of the inputs (in case they are const_int).
2894 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). It will
2895 be passed unchanged to emit_cmp_insn, then potentially converted into an
2896 unsigned variant based on UNSIGNEDP to select a proper jump instruction. */
2898 void
2899 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
2900 enum machine_mode mode, int unsignedp, rtx label)
2902 rtx op0 = x, op1 = y;
2904 /* Swap operands and condition to ensure canonical RTL. */
2905 if (swap_commutative_operands_p (x, y))
2907 /* If we're not emitting a branch, this means some caller
2908 is out of sync. */
2909 if (! label)
2910 abort ();
2912 op0 = y, op1 = x;
2913 comparison = swap_condition (comparison);
2916 #ifdef HAVE_cc0
2917 /* If OP0 is still a constant, then both X and Y must be constants. Force
2918 X into a register to avoid aborting in emit_cmp_insn due to non-canonical
2919 RTL. */
2920 if (CONSTANT_P (op0))
2921 op0 = force_reg (mode, op0);
2922 #endif
2924 if (unsignedp)
2925 comparison = unsigned_condition (comparison);
2927 prepare_cmp_insn (&op0, &op1, &comparison, size, &mode, &unsignedp,
2928 ccp_jump);
2929 emit_cmp_and_jump_insn_1 (op0, op1, mode, comparison, unsignedp, label);
2932 /* Like emit_cmp_and_jump_insns, but generate only the comparison. */
2934 void
2935 emit_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
2936 enum machine_mode mode, int unsignedp)
2938 emit_cmp_and_jump_insns (x, y, comparison, size, mode, unsignedp, 0);
2941 /* Emit a library call comparison between floating point X and Y.
2942 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
2944 static void
2945 prepare_float_lib_cmp (rtx *px, rtx *py, enum rtx_code *pcomparison,
2946 enum machine_mode *pmode, int *punsignedp)
2948 enum rtx_code comparison = *pcomparison;
2949 enum rtx_code swapped = swap_condition (comparison);
2950 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
2951 rtx x = *px;
2952 rtx y = *py;
2953 enum machine_mode orig_mode = GET_MODE (x);
2954 enum machine_mode mode;
2955 rtx value, target, insns, equiv;
2956 rtx libfunc = 0;
2957 bool reversed_p = false;
2959 for (mode = orig_mode; mode != VOIDmode; mode = GET_MODE_WIDER_MODE (mode))
2961 if ((libfunc = code_to_optab[comparison]->handlers[mode].libfunc))
2962 break;
2964 if ((libfunc = code_to_optab[swapped]->handlers[mode].libfunc))
2966 rtx tmp;
2967 tmp = x; x = y; y = tmp;
2968 comparison = swapped;
2969 break;
2972 if ((libfunc = code_to_optab[reversed]->handlers[mode].libfunc)
2973 && FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, reversed))
2975 comparison = reversed;
2976 reversed_p = true;
2977 break;
2981 if (mode == VOIDmode)
2982 abort ();
2984 if (mode != orig_mode)
2986 x = convert_to_mode (mode, x, 0);
2987 y = convert_to_mode (mode, y, 0);
2990 /* Attach a REG_EQUAL note describing the semantics of the libcall to
2991 the RTL. The allows the RTL optimizers to delete the libcall if the
2992 condition can be determined at compile-time. */
2993 if (comparison == UNORDERED)
2995 rtx temp = simplify_gen_relational (NE, word_mode, mode, x, x);
2996 equiv = simplify_gen_relational (NE, word_mode, mode, y, y);
2997 equiv = simplify_gen_ternary (IF_THEN_ELSE, word_mode, word_mode,
2998 temp, const_true_rtx, equiv);
3000 else
3002 equiv = simplify_gen_relational (comparison, word_mode, mode, x, y);
3003 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
3005 rtx true_rtx, false_rtx;
3007 switch (comparison)
3009 case EQ:
3010 true_rtx = const0_rtx;
3011 false_rtx = const_true_rtx;
3012 break;
3014 case NE:
3015 true_rtx = const_true_rtx;
3016 false_rtx = const0_rtx;
3017 break;
3019 case GT:
3020 true_rtx = const1_rtx;
3021 false_rtx = const0_rtx;
3022 break;
3024 case GE:
3025 true_rtx = const0_rtx;
3026 false_rtx = constm1_rtx;
3027 break;
3029 case LT:
3030 true_rtx = constm1_rtx;
3031 false_rtx = const0_rtx;
3032 break;
3034 case LE:
3035 true_rtx = const0_rtx;
3036 false_rtx = const1_rtx;
3037 break;
3039 default:
3040 abort ();
3042 equiv = simplify_gen_ternary (IF_THEN_ELSE, word_mode, word_mode,
3043 equiv, true_rtx, false_rtx);
3047 start_sequence ();
3048 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
3049 word_mode, 2, x, mode, y, mode);
3050 insns = get_insns ();
3051 end_sequence ();
3053 target = gen_reg_rtx (word_mode);
3054 emit_libcall_block (insns, target, value, equiv);
3056 if (comparison == UNORDERED
3057 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
3058 comparison = reversed_p ? EQ : NE;
3060 *px = target;
3061 *py = const0_rtx;
3062 *pmode = word_mode;
3063 *pcomparison = comparison;
3064 *punsignedp = 0;
3067 /* Generate code to indirectly jump to a location given in the rtx LOC. */
3069 void
3070 emit_indirect_jump (rtx loc)
3072 if (! ((*insn_data[(int) CODE_FOR_indirect_jump].operand[0].predicate)
3073 (loc, Pmode)))
3074 loc = copy_to_mode_reg (Pmode, loc);
3076 emit_jump_insn (gen_indirect_jump (loc));
3077 emit_barrier ();
3080 #ifdef HAVE_conditional_move
3082 /* Emit a conditional move instruction if the machine supports one for that
3083 condition and machine mode.
3085 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
3086 the mode to use should they be constants. If it is VOIDmode, they cannot
3087 both be constants.
3089 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
3090 should be stored there. MODE is the mode to use should they be constants.
3091 If it is VOIDmode, they cannot both be constants.
3093 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
3094 is not supported. */
3097 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
3098 enum machine_mode cmode, rtx op2, rtx op3,
3099 enum machine_mode mode, int unsignedp)
3101 rtx tem, subtarget, comparison, insn;
3102 enum insn_code icode;
3103 enum rtx_code reversed;
3105 /* If one operand is constant, make it the second one. Only do this
3106 if the other operand is not constant as well. */
3108 if (swap_commutative_operands_p (op0, op1))
3110 tem = op0;
3111 op0 = op1;
3112 op1 = tem;
3113 code = swap_condition (code);
3116 /* get_condition will prefer to generate LT and GT even if the old
3117 comparison was against zero, so undo that canonicalization here since
3118 comparisons against zero are cheaper. */
3119 if (code == LT && op1 == const1_rtx)
3120 code = LE, op1 = const0_rtx;
3121 else if (code == GT && op1 == constm1_rtx)
3122 code = GE, op1 = const0_rtx;
3124 if (cmode == VOIDmode)
3125 cmode = GET_MODE (op0);
3127 if (swap_commutative_operands_p (op2, op3)
3128 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
3129 != UNKNOWN))
3131 tem = op2;
3132 op2 = op3;
3133 op3 = tem;
3134 code = reversed;
3137 if (mode == VOIDmode)
3138 mode = GET_MODE (op2);
3140 icode = movcc_gen_code[mode];
3142 if (icode == CODE_FOR_nothing)
3143 return 0;
3145 if (flag_force_mem)
3147 op2 = force_not_mem (op2);
3148 op3 = force_not_mem (op3);
3151 if (!target)
3152 target = gen_reg_rtx (mode);
3154 subtarget = target;
3156 /* If the insn doesn't accept these operands, put them in pseudos. */
3158 if (! (*insn_data[icode].operand[0].predicate)
3159 (subtarget, insn_data[icode].operand[0].mode))
3160 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
3162 if (! (*insn_data[icode].operand[2].predicate)
3163 (op2, insn_data[icode].operand[2].mode))
3164 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
3166 if (! (*insn_data[icode].operand[3].predicate)
3167 (op3, insn_data[icode].operand[3].mode))
3168 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
3170 /* Everything should now be in the suitable form, so emit the compare insn
3171 and then the conditional move. */
3173 comparison
3174 = compare_from_rtx (op0, op1, code, unsignedp, cmode, NULL_RTX);
3176 /* ??? Watch for const0_rtx (nop) and const_true_rtx (unconditional)? */
3177 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
3178 return NULL and let the caller figure out how best to deal with this
3179 situation. */
3180 if (GET_CODE (comparison) != code)
3181 return NULL_RTX;
3183 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
3185 /* If that failed, then give up. */
3186 if (insn == 0)
3187 return 0;
3189 emit_insn (insn);
3191 if (subtarget != target)
3192 convert_move (target, subtarget, 0);
3194 return target;
3197 /* Return nonzero if a conditional move of mode MODE is supported.
3199 This function is for combine so it can tell whether an insn that looks
3200 like a conditional move is actually supported by the hardware. If we
3201 guess wrong we lose a bit on optimization, but that's it. */
3202 /* ??? sparc64 supports conditionally moving integers values based on fp
3203 comparisons, and vice versa. How do we handle them? */
3206 can_conditionally_move_p (enum machine_mode mode)
3208 if (movcc_gen_code[mode] != CODE_FOR_nothing)
3209 return 1;
3211 return 0;
3214 #endif /* HAVE_conditional_move */
3216 /* Emit a conditional addition instruction if the machine supports one for that
3217 condition and machine mode.
3219 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
3220 the mode to use should they be constants. If it is VOIDmode, they cannot
3221 both be constants.
3223 OP2 should be stored in TARGET if the comparison is true, otherwise OP2+OP3
3224 should be stored there. MODE is the mode to use should they be constants.
3225 If it is VOIDmode, they cannot both be constants.
3227 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
3228 is not supported. */
3231 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
3232 enum machine_mode cmode, rtx op2, rtx op3,
3233 enum machine_mode mode, int unsignedp)
3235 rtx tem, subtarget, comparison, insn;
3236 enum insn_code icode;
3237 enum rtx_code reversed;
3239 /* If one operand is constant, make it the second one. Only do this
3240 if the other operand is not constant as well. */
3242 if (swap_commutative_operands_p (op0, op1))
3244 tem = op0;
3245 op0 = op1;
3246 op1 = tem;
3247 code = swap_condition (code);
3250 /* get_condition will prefer to generate LT and GT even if the old
3251 comparison was against zero, so undo that canonicalization here since
3252 comparisons against zero are cheaper. */
3253 if (code == LT && op1 == const1_rtx)
3254 code = LE, op1 = const0_rtx;
3255 else if (code == GT && op1 == constm1_rtx)
3256 code = GE, op1 = const0_rtx;
3258 if (cmode == VOIDmode)
3259 cmode = GET_MODE (op0);
3261 if (swap_commutative_operands_p (op2, op3)
3262 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
3263 != UNKNOWN))
3265 tem = op2;
3266 op2 = op3;
3267 op3 = tem;
3268 code = reversed;
3271 if (mode == VOIDmode)
3272 mode = GET_MODE (op2);
3274 icode = addcc_optab->handlers[(int) mode].insn_code;
3276 if (icode == CODE_FOR_nothing)
3277 return 0;
3279 if (flag_force_mem)
3281 op2 = force_not_mem (op2);
3282 op3 = force_not_mem (op3);
3285 if (!target)
3286 target = gen_reg_rtx (mode);
3288 /* If the insn doesn't accept these operands, put them in pseudos. */
3290 if (! (*insn_data[icode].operand[0].predicate)
3291 (target, insn_data[icode].operand[0].mode))
3292 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
3293 else
3294 subtarget = target;
3296 if (! (*insn_data[icode].operand[2].predicate)
3297 (op2, insn_data[icode].operand[2].mode))
3298 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
3300 if (! (*insn_data[icode].operand[3].predicate)
3301 (op3, insn_data[icode].operand[3].mode))
3302 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
3304 /* Everything should now be in the suitable form, so emit the compare insn
3305 and then the conditional move. */
3307 comparison
3308 = compare_from_rtx (op0, op1, code, unsignedp, cmode, NULL_RTX);
3310 /* ??? Watch for const0_rtx (nop) and const_true_rtx (unconditional)? */
3311 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
3312 return NULL and let the caller figure out how best to deal with this
3313 situation. */
3314 if (GET_CODE (comparison) != code)
3315 return NULL_RTX;
3317 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
3319 /* If that failed, then give up. */
3320 if (insn == 0)
3321 return 0;
3323 emit_insn (insn);
3325 if (subtarget != target)
3326 convert_move (target, subtarget, 0);
3328 return target;
3331 /* These functions attempt to generate an insn body, rather than
3332 emitting the insn, but if the gen function already emits them, we
3333 make no attempt to turn them back into naked patterns. */
3335 /* Generate and return an insn body to add Y to X. */
3338 gen_add2_insn (rtx x, rtx y)
3340 int icode = (int) add_optab->handlers[(int) GET_MODE (x)].insn_code;
3342 if (! ((*insn_data[icode].operand[0].predicate)
3343 (x, insn_data[icode].operand[0].mode))
3344 || ! ((*insn_data[icode].operand[1].predicate)
3345 (x, insn_data[icode].operand[1].mode))
3346 || ! ((*insn_data[icode].operand[2].predicate)
3347 (y, insn_data[icode].operand[2].mode)))
3348 abort ();
3350 return (GEN_FCN (icode) (x, x, y));
3353 /* Generate and return an insn body to add r1 and c,
3354 storing the result in r0. */
3356 gen_add3_insn (rtx r0, rtx r1, rtx c)
3358 int icode = (int) add_optab->handlers[(int) GET_MODE (r0)].insn_code;
3360 if (icode == CODE_FOR_nothing
3361 || ! ((*insn_data[icode].operand[0].predicate)
3362 (r0, insn_data[icode].operand[0].mode))
3363 || ! ((*insn_data[icode].operand[1].predicate)
3364 (r1, insn_data[icode].operand[1].mode))
3365 || ! ((*insn_data[icode].operand[2].predicate)
3366 (c, insn_data[icode].operand[2].mode)))
3367 return NULL_RTX;
3369 return (GEN_FCN (icode) (r0, r1, c));
3373 have_add2_insn (rtx x, rtx y)
3375 int icode;
3377 if (GET_MODE (x) == VOIDmode)
3378 abort ();
3380 icode = (int) add_optab->handlers[(int) GET_MODE (x)].insn_code;
3382 if (icode == CODE_FOR_nothing)
3383 return 0;
3385 if (! ((*insn_data[icode].operand[0].predicate)
3386 (x, insn_data[icode].operand[0].mode))
3387 || ! ((*insn_data[icode].operand[1].predicate)
3388 (x, insn_data[icode].operand[1].mode))
3389 || ! ((*insn_data[icode].operand[2].predicate)
3390 (y, insn_data[icode].operand[2].mode)))
3391 return 0;
3393 return 1;
3396 /* Generate and return an insn body to subtract Y from X. */
3399 gen_sub2_insn (rtx x, rtx y)
3401 int icode = (int) sub_optab->handlers[(int) GET_MODE (x)].insn_code;
3403 if (! ((*insn_data[icode].operand[0].predicate)
3404 (x, insn_data[icode].operand[0].mode))
3405 || ! ((*insn_data[icode].operand[1].predicate)
3406 (x, insn_data[icode].operand[1].mode))
3407 || ! ((*insn_data[icode].operand[2].predicate)
3408 (y, insn_data[icode].operand[2].mode)))
3409 abort ();
3411 return (GEN_FCN (icode) (x, x, y));
3414 /* Generate and return an insn body to subtract r1 and c,
3415 storing the result in r0. */
3417 gen_sub3_insn (rtx r0, rtx r1, rtx c)
3419 int icode = (int) sub_optab->handlers[(int) GET_MODE (r0)].insn_code;
3421 if (icode == CODE_FOR_nothing
3422 || ! ((*insn_data[icode].operand[0].predicate)
3423 (r0, insn_data[icode].operand[0].mode))
3424 || ! ((*insn_data[icode].operand[1].predicate)
3425 (r1, insn_data[icode].operand[1].mode))
3426 || ! ((*insn_data[icode].operand[2].predicate)
3427 (c, insn_data[icode].operand[2].mode)))
3428 return NULL_RTX;
3430 return (GEN_FCN (icode) (r0, r1, c));
3434 have_sub2_insn (rtx x, rtx y)
3436 int icode;
3438 if (GET_MODE (x) == VOIDmode)
3439 abort ();
3441 icode = (int) sub_optab->handlers[(int) GET_MODE (x)].insn_code;
3443 if (icode == CODE_FOR_nothing)
3444 return 0;
3446 if (! ((*insn_data[icode].operand[0].predicate)
3447 (x, insn_data[icode].operand[0].mode))
3448 || ! ((*insn_data[icode].operand[1].predicate)
3449 (x, insn_data[icode].operand[1].mode))
3450 || ! ((*insn_data[icode].operand[2].predicate)
3451 (y, insn_data[icode].operand[2].mode)))
3452 return 0;
3454 return 1;
3457 /* Generate the body of an instruction to copy Y into X.
3458 It may be a list of insns, if one insn isn't enough. */
3461 gen_move_insn (rtx x, rtx y)
3463 rtx seq;
3465 start_sequence ();
3466 emit_move_insn_1 (x, y);
3467 seq = get_insns ();
3468 end_sequence ();
3469 return seq;
3472 /* Return the insn code used to extend FROM_MODE to TO_MODE.
3473 UNSIGNEDP specifies zero-extension instead of sign-extension. If
3474 no such operation exists, CODE_FOR_nothing will be returned. */
3476 enum insn_code
3477 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
3478 int unsignedp)
3480 convert_optab tab;
3481 #ifdef HAVE_ptr_extend
3482 if (unsignedp < 0)
3483 return CODE_FOR_ptr_extend;
3484 #endif
3486 tab = unsignedp ? zext_optab : sext_optab;
3487 return tab->handlers[to_mode][from_mode].insn_code;
3490 /* Generate the body of an insn to extend Y (with mode MFROM)
3491 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
3494 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
3495 enum machine_mode mfrom, int unsignedp)
3497 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
3498 return GEN_FCN (icode) (x, y);
3501 /* can_fix_p and can_float_p say whether the target machine
3502 can directly convert a given fixed point type to
3503 a given floating point type, or vice versa.
3504 The returned value is the CODE_FOR_... value to use,
3505 or CODE_FOR_nothing if these modes cannot be directly converted.
3507 *TRUNCP_PTR is set to 1 if it is necessary to output
3508 an explicit FTRUNC insn before the fix insn; otherwise 0. */
3510 static enum insn_code
3511 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
3512 int unsignedp, int *truncp_ptr)
3514 convert_optab tab;
3515 enum insn_code icode;
3517 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
3518 icode = tab->handlers[fixmode][fltmode].insn_code;
3519 if (icode != CODE_FOR_nothing)
3521 *truncp_ptr = 0;
3522 return icode;
3525 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
3526 for this to work. We need to rework the fix* and ftrunc* patterns
3527 and documentation. */
3528 tab = unsignedp ? ufix_optab : sfix_optab;
3529 icode = tab->handlers[fixmode][fltmode].insn_code;
3530 if (icode != CODE_FOR_nothing
3531 && ftrunc_optab->handlers[fltmode].insn_code != CODE_FOR_nothing)
3533 *truncp_ptr = 1;
3534 return icode;
3537 *truncp_ptr = 0;
3538 return CODE_FOR_nothing;
3541 static enum insn_code
3542 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
3543 int unsignedp)
3545 convert_optab tab;
3547 tab = unsignedp ? ufloat_optab : sfloat_optab;
3548 return tab->handlers[fltmode][fixmode].insn_code;
3551 /* Generate code to convert FROM to floating point
3552 and store in TO. FROM must be fixed point and not VOIDmode.
3553 UNSIGNEDP nonzero means regard FROM as unsigned.
3554 Normally this is done by correcting the final value
3555 if it is negative. */
3557 void
3558 expand_float (rtx to, rtx from, int unsignedp)
3560 enum insn_code icode;
3561 rtx target = to;
3562 enum machine_mode fmode, imode;
3564 /* Crash now, because we won't be able to decide which mode to use. */
3565 if (GET_MODE (from) == VOIDmode)
3566 abort ();
3568 /* Look for an insn to do the conversion. Do it in the specified
3569 modes if possible; otherwise convert either input, output or both to
3570 wider mode. If the integer mode is wider than the mode of FROM,
3571 we can do the conversion signed even if the input is unsigned. */
3573 for (fmode = GET_MODE (to); fmode != VOIDmode;
3574 fmode = GET_MODE_WIDER_MODE (fmode))
3575 for (imode = GET_MODE (from); imode != VOIDmode;
3576 imode = GET_MODE_WIDER_MODE (imode))
3578 int doing_unsigned = unsignedp;
3580 if (fmode != GET_MODE (to)
3581 && significand_size (fmode) < GET_MODE_BITSIZE (GET_MODE (from)))
3582 continue;
3584 icode = can_float_p (fmode, imode, unsignedp);
3585 if (icode == CODE_FOR_nothing && imode != GET_MODE (from) && unsignedp)
3586 icode = can_float_p (fmode, imode, 0), doing_unsigned = 0;
3588 if (icode != CODE_FOR_nothing)
3590 if (imode != GET_MODE (from))
3591 from = convert_to_mode (imode, from, unsignedp);
3593 if (fmode != GET_MODE (to))
3594 target = gen_reg_rtx (fmode);
3596 emit_unop_insn (icode, target, from,
3597 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
3599 if (target != to)
3600 convert_move (to, target, 0);
3601 return;
3605 /* Unsigned integer, and no way to convert directly.
3606 Convert as signed, then conditionally adjust the result. */
3607 if (unsignedp)
3609 rtx label = gen_label_rtx ();
3610 rtx temp;
3611 REAL_VALUE_TYPE offset;
3613 if (flag_force_mem)
3614 from = force_not_mem (from);
3616 /* Look for a usable floating mode FMODE wider than the source and at
3617 least as wide as the target. Using FMODE will avoid rounding woes
3618 with unsigned values greater than the signed maximum value. */
3620 for (fmode = GET_MODE (to); fmode != VOIDmode;
3621 fmode = GET_MODE_WIDER_MODE (fmode))
3622 if (GET_MODE_BITSIZE (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
3623 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
3624 break;
3626 if (fmode == VOIDmode)
3628 /* There is no such mode. Pretend the target is wide enough. */
3629 fmode = GET_MODE (to);
3631 /* Avoid double-rounding when TO is narrower than FROM. */
3632 if ((significand_size (fmode) + 1)
3633 < GET_MODE_BITSIZE (GET_MODE (from)))
3635 rtx temp1;
3636 rtx neglabel = gen_label_rtx ();
3638 /* Don't use TARGET if it isn't a register, is a hard register,
3639 or is the wrong mode. */
3640 if (!REG_P (target)
3641 || REGNO (target) < FIRST_PSEUDO_REGISTER
3642 || GET_MODE (target) != fmode)
3643 target = gen_reg_rtx (fmode);
3645 imode = GET_MODE (from);
3646 do_pending_stack_adjust ();
3648 /* Test whether the sign bit is set. */
3649 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
3650 0, neglabel);
3652 /* The sign bit is not set. Convert as signed. */
3653 expand_float (target, from, 0);
3654 emit_jump_insn (gen_jump (label));
3655 emit_barrier ();
3657 /* The sign bit is set.
3658 Convert to a usable (positive signed) value by shifting right
3659 one bit, while remembering if a nonzero bit was shifted
3660 out; i.e., compute (from & 1) | (from >> 1). */
3662 emit_label (neglabel);
3663 temp = expand_binop (imode, and_optab, from, const1_rtx,
3664 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3665 temp1 = expand_shift (RSHIFT_EXPR, imode, from, integer_one_node,
3666 NULL_RTX, 1);
3667 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
3668 OPTAB_LIB_WIDEN);
3669 expand_float (target, temp, 0);
3671 /* Multiply by 2 to undo the shift above. */
3672 temp = expand_binop (fmode, add_optab, target, target,
3673 target, 0, OPTAB_LIB_WIDEN);
3674 if (temp != target)
3675 emit_move_insn (target, temp);
3677 do_pending_stack_adjust ();
3678 emit_label (label);
3679 goto done;
3683 /* If we are about to do some arithmetic to correct for an
3684 unsigned operand, do it in a pseudo-register. */
3686 if (GET_MODE (to) != fmode
3687 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
3688 target = gen_reg_rtx (fmode);
3690 /* Convert as signed integer to floating. */
3691 expand_float (target, from, 0);
3693 /* If FROM is negative (and therefore TO is negative),
3694 correct its value by 2**bitwidth. */
3696 do_pending_stack_adjust ();
3697 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
3698 0, label);
3701 real_2expN (&offset, GET_MODE_BITSIZE (GET_MODE (from)));
3702 temp = expand_binop (fmode, add_optab, target,
3703 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
3704 target, 0, OPTAB_LIB_WIDEN);
3705 if (temp != target)
3706 emit_move_insn (target, temp);
3708 do_pending_stack_adjust ();
3709 emit_label (label);
3710 goto done;
3713 /* No hardware instruction available; call a library routine. */
3715 rtx libfunc;
3716 rtx insns;
3717 rtx value;
3718 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
3720 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
3721 from = convert_to_mode (SImode, from, unsignedp);
3723 if (flag_force_mem)
3724 from = force_not_mem (from);
3726 libfunc = tab->handlers[GET_MODE (to)][GET_MODE (from)].libfunc;
3727 if (!libfunc)
3728 abort ();
3730 start_sequence ();
3732 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
3733 GET_MODE (to), 1, from,
3734 GET_MODE (from));
3735 insns = get_insns ();
3736 end_sequence ();
3738 emit_libcall_block (insns, target, value,
3739 gen_rtx_FLOAT (GET_MODE (to), from));
3742 done:
3744 /* Copy result to requested destination
3745 if we have been computing in a temp location. */
3747 if (target != to)
3749 if (GET_MODE (target) == GET_MODE (to))
3750 emit_move_insn (to, target);
3751 else
3752 convert_move (to, target, 0);
3756 /* Generate code to convert FROM to fixed point and store in TO. FROM
3757 must be floating point. */
3759 void
3760 expand_fix (rtx to, rtx from, int unsignedp)
3762 enum insn_code icode;
3763 rtx target = to;
3764 enum machine_mode fmode, imode;
3765 int must_trunc = 0;
3767 /* We first try to find a pair of modes, one real and one integer, at
3768 least as wide as FROM and TO, respectively, in which we can open-code
3769 this conversion. If the integer mode is wider than the mode of TO,
3770 we can do the conversion either signed or unsigned. */
3772 for (fmode = GET_MODE (from); fmode != VOIDmode;
3773 fmode = GET_MODE_WIDER_MODE (fmode))
3774 for (imode = GET_MODE (to); imode != VOIDmode;
3775 imode = GET_MODE_WIDER_MODE (imode))
3777 int doing_unsigned = unsignedp;
3779 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
3780 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
3781 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
3783 if (icode != CODE_FOR_nothing)
3785 if (fmode != GET_MODE (from))
3786 from = convert_to_mode (fmode, from, 0);
3788 if (must_trunc)
3790 rtx temp = gen_reg_rtx (GET_MODE (from));
3791 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
3792 temp, 0);
3795 if (imode != GET_MODE (to))
3796 target = gen_reg_rtx (imode);
3798 emit_unop_insn (icode, target, from,
3799 doing_unsigned ? UNSIGNED_FIX : FIX);
3800 if (target != to)
3801 convert_move (to, target, unsignedp);
3802 return;
3806 /* For an unsigned conversion, there is one more way to do it.
3807 If we have a signed conversion, we generate code that compares
3808 the real value to the largest representable positive number. If if
3809 is smaller, the conversion is done normally. Otherwise, subtract
3810 one plus the highest signed number, convert, and add it back.
3812 We only need to check all real modes, since we know we didn't find
3813 anything with a wider integer mode.
3815 This code used to extend FP value into mode wider than the destination.
3816 This is not needed. Consider, for instance conversion from SFmode
3817 into DImode.
3819 The hot path trought the code is dealing with inputs smaller than 2^63
3820 and doing just the conversion, so there is no bits to lose.
3822 In the other path we know the value is positive in the range 2^63..2^64-1
3823 inclusive. (as for other imput overflow happens and result is undefined)
3824 So we know that the most important bit set in mantissa corresponds to
3825 2^63. The subtraction of 2^63 should not generate any rounding as it
3826 simply clears out that bit. The rest is trivial. */
3828 if (unsignedp && GET_MODE_BITSIZE (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
3829 for (fmode = GET_MODE (from); fmode != VOIDmode;
3830 fmode = GET_MODE_WIDER_MODE (fmode))
3831 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0,
3832 &must_trunc))
3834 int bitsize;
3835 REAL_VALUE_TYPE offset;
3836 rtx limit, lab1, lab2, insn;
3838 bitsize = GET_MODE_BITSIZE (GET_MODE (to));
3839 real_2expN (&offset, bitsize - 1);
3840 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
3841 lab1 = gen_label_rtx ();
3842 lab2 = gen_label_rtx ();
3844 if (flag_force_mem)
3845 from = force_not_mem (from);
3847 if (fmode != GET_MODE (from))
3848 from = convert_to_mode (fmode, from, 0);
3850 /* See if we need to do the subtraction. */
3851 do_pending_stack_adjust ();
3852 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
3853 0, lab1);
3855 /* If not, do the signed "fix" and branch around fixup code. */
3856 expand_fix (to, from, 0);
3857 emit_jump_insn (gen_jump (lab2));
3858 emit_barrier ();
3860 /* Otherwise, subtract 2**(N-1), convert to signed number,
3861 then add 2**(N-1). Do the addition using XOR since this
3862 will often generate better code. */
3863 emit_label (lab1);
3864 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
3865 NULL_RTX, 0, OPTAB_LIB_WIDEN);
3866 expand_fix (to, target, 0);
3867 target = expand_binop (GET_MODE (to), xor_optab, to,
3868 gen_int_mode
3869 ((HOST_WIDE_INT) 1 << (bitsize - 1),
3870 GET_MODE (to)),
3871 to, 1, OPTAB_LIB_WIDEN);
3873 if (target != to)
3874 emit_move_insn (to, target);
3876 emit_label (lab2);
3878 if (mov_optab->handlers[(int) GET_MODE (to)].insn_code
3879 != CODE_FOR_nothing)
3881 /* Make a place for a REG_NOTE and add it. */
3882 insn = emit_move_insn (to, to);
3883 set_unique_reg_note (insn,
3884 REG_EQUAL,
3885 gen_rtx_fmt_e (UNSIGNED_FIX,
3886 GET_MODE (to),
3887 copy_rtx (from)));
3890 return;
3893 /* We can't do it with an insn, so use a library call. But first ensure
3894 that the mode of TO is at least as wide as SImode, since those are the
3895 only library calls we know about. */
3897 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
3899 target = gen_reg_rtx (SImode);
3901 expand_fix (target, from, unsignedp);
3903 else
3905 rtx insns;
3906 rtx value;
3907 rtx libfunc;
3909 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
3910 libfunc = tab->handlers[GET_MODE (to)][GET_MODE (from)].libfunc;
3911 if (!libfunc)
3912 abort ();
3914 if (flag_force_mem)
3915 from = force_not_mem (from);
3917 start_sequence ();
3919 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
3920 GET_MODE (to), 1, from,
3921 GET_MODE (from));
3922 insns = get_insns ();
3923 end_sequence ();
3925 emit_libcall_block (insns, target, value,
3926 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
3927 GET_MODE (to), from));
3930 if (target != to)
3932 if (GET_MODE (to) == GET_MODE (target))
3933 emit_move_insn (to, target);
3934 else
3935 convert_move (to, target, 0);
3939 /* Report whether we have an instruction to perform the operation
3940 specified by CODE on operands of mode MODE. */
3942 have_insn_for (enum rtx_code code, enum machine_mode mode)
3944 return (code_to_optab[(int) code] != 0
3945 && (code_to_optab[(int) code]->handlers[(int) mode].insn_code
3946 != CODE_FOR_nothing));
3949 /* Create a blank optab. */
3950 static optab
3951 new_optab (void)
3953 int i;
3954 optab op = ggc_alloc (sizeof (struct optab));
3955 for (i = 0; i < NUM_MACHINE_MODES; i++)
3957 op->handlers[i].insn_code = CODE_FOR_nothing;
3958 op->handlers[i].libfunc = 0;
3961 return op;
3964 static convert_optab
3965 new_convert_optab (void)
3967 int i, j;
3968 convert_optab op = ggc_alloc (sizeof (struct convert_optab));
3969 for (i = 0; i < NUM_MACHINE_MODES; i++)
3970 for (j = 0; j < NUM_MACHINE_MODES; j++)
3972 op->handlers[i][j].insn_code = CODE_FOR_nothing;
3973 op->handlers[i][j].libfunc = 0;
3975 return op;
3978 /* Same, but fill in its code as CODE, and write it into the
3979 code_to_optab table. */
3980 static inline optab
3981 init_optab (enum rtx_code code)
3983 optab op = new_optab ();
3984 op->code = code;
3985 code_to_optab[(int) code] = op;
3986 return op;
3989 /* Same, but fill in its code as CODE, and do _not_ write it into
3990 the code_to_optab table. */
3991 static inline optab
3992 init_optabv (enum rtx_code code)
3994 optab op = new_optab ();
3995 op->code = code;
3996 return op;
3999 /* Conversion optabs never go in the code_to_optab table. */
4000 static inline convert_optab
4001 init_convert_optab (enum rtx_code code)
4003 convert_optab op = new_convert_optab ();
4004 op->code = code;
4005 return op;
4008 /* Initialize the libfunc fields of an entire group of entries in some
4009 optab. Each entry is set equal to a string consisting of a leading
4010 pair of underscores followed by a generic operation name followed by
4011 a mode name (downshifted to lowercase) followed by a single character
4012 representing the number of operands for the given operation (which is
4013 usually one of the characters '2', '3', or '4').
4015 OPTABLE is the table in which libfunc fields are to be initialized.
4016 FIRST_MODE is the first machine mode index in the given optab to
4017 initialize.
4018 LAST_MODE is the last machine mode index in the given optab to
4019 initialize.
4020 OPNAME is the generic (string) name of the operation.
4021 SUFFIX is the character which specifies the number of operands for
4022 the given generic operation.
4025 static void
4026 init_libfuncs (optab optable, int first_mode, int last_mode,
4027 const char *opname, int suffix)
4029 int mode;
4030 unsigned opname_len = strlen (opname);
4032 for (mode = first_mode; (int) mode <= (int) last_mode;
4033 mode = (enum machine_mode) ((int) mode + 1))
4035 const char *mname = GET_MODE_NAME (mode);
4036 unsigned mname_len = strlen (mname);
4037 char *libfunc_name = alloca (2 + opname_len + mname_len + 1 + 1);
4038 char *p;
4039 const char *q;
4041 p = libfunc_name;
4042 *p++ = '_';
4043 *p++ = '_';
4044 for (q = opname; *q; )
4045 *p++ = *q++;
4046 for (q = mname; *q; q++)
4047 *p++ = TOLOWER (*q);
4048 *p++ = suffix;
4049 *p = '\0';
4051 optable->handlers[(int) mode].libfunc
4052 = init_one_libfunc (ggc_alloc_string (libfunc_name, p - libfunc_name));
4056 /* Initialize the libfunc fields of an entire group of entries in some
4057 optab which correspond to all integer mode operations. The parameters
4058 have the same meaning as similarly named ones for the `init_libfuncs'
4059 routine. (See above). */
4061 static void
4062 init_integral_libfuncs (optab optable, const char *opname, int suffix)
4064 int maxsize = 2*BITS_PER_WORD;
4065 if (maxsize < LONG_LONG_TYPE_SIZE)
4066 maxsize = LONG_LONG_TYPE_SIZE;
4067 init_libfuncs (optable, word_mode,
4068 mode_for_size (maxsize, MODE_INT, 0),
4069 opname, suffix);
4072 /* Initialize the libfunc fields of an entire group of entries in some
4073 optab which correspond to all real mode operations. The parameters
4074 have the same meaning as similarly named ones for the `init_libfuncs'
4075 routine. (See above). */
4077 static void
4078 init_floating_libfuncs (optab optable, const char *opname, int suffix)
4080 init_libfuncs (optable, MIN_MODE_FLOAT, MAX_MODE_FLOAT, opname, suffix);
4083 /* Initialize the libfunc fields of an entire group of entries of an
4084 inter-mode-class conversion optab. The string formation rules are
4085 similar to the ones for init_libfuncs, above, but instead of having
4086 a mode name and an operand count these functions have two mode names
4087 and no operand count. */
4088 static void
4089 init_interclass_conv_libfuncs (convert_optab tab, const char *opname,
4090 enum mode_class from_class,
4091 enum mode_class to_class)
4093 enum machine_mode first_from_mode = GET_CLASS_NARROWEST_MODE (from_class);
4094 enum machine_mode first_to_mode = GET_CLASS_NARROWEST_MODE (to_class);
4095 size_t opname_len = strlen (opname);
4096 size_t max_mname_len = 0;
4098 enum machine_mode fmode, tmode;
4099 const char *fname, *tname;
4100 const char *q;
4101 char *libfunc_name, *suffix;
4102 char *p;
4104 for (fmode = first_from_mode;
4105 fmode != VOIDmode;
4106 fmode = GET_MODE_WIDER_MODE (fmode))
4107 max_mname_len = MAX (max_mname_len, strlen (GET_MODE_NAME (fmode)));
4109 for (tmode = first_to_mode;
4110 tmode != VOIDmode;
4111 tmode = GET_MODE_WIDER_MODE (tmode))
4112 max_mname_len = MAX (max_mname_len, strlen (GET_MODE_NAME (tmode)));
4114 libfunc_name = alloca (2 + opname_len + 2*max_mname_len + 1 + 1);
4115 libfunc_name[0] = '_';
4116 libfunc_name[1] = '_';
4117 memcpy (&libfunc_name[2], opname, opname_len);
4118 suffix = libfunc_name + opname_len + 2;
4120 for (fmode = first_from_mode; fmode != VOIDmode;
4121 fmode = GET_MODE_WIDER_MODE (fmode))
4122 for (tmode = first_to_mode; tmode != VOIDmode;
4123 tmode = GET_MODE_WIDER_MODE (tmode))
4125 fname = GET_MODE_NAME (fmode);
4126 tname = GET_MODE_NAME (tmode);
4128 p = suffix;
4129 for (q = fname; *q; p++, q++)
4130 *p = TOLOWER (*q);
4131 for (q = tname; *q; p++, q++)
4132 *p = TOLOWER (*q);
4134 *p = '\0';
4136 tab->handlers[tmode][fmode].libfunc
4137 = init_one_libfunc (ggc_alloc_string (libfunc_name,
4138 p - libfunc_name));
4142 /* Initialize the libfunc fields of an entire group of entries of an
4143 intra-mode-class conversion optab. The string formation rules are
4144 similar to the ones for init_libfunc, above. WIDENING says whether
4145 the optab goes from narrow to wide modes or vice versa. These functions
4146 have two mode names _and_ an operand count. */
4147 static void
4148 init_intraclass_conv_libfuncs (convert_optab tab, const char *opname,
4149 enum mode_class class, bool widening)
4151 enum machine_mode first_mode = GET_CLASS_NARROWEST_MODE (class);
4152 size_t opname_len = strlen (opname);
4153 size_t max_mname_len = 0;
4155 enum machine_mode nmode, wmode;
4156 const char *nname, *wname;
4157 const char *q;
4158 char *libfunc_name, *suffix;
4159 char *p;
4161 for (nmode = first_mode; nmode != VOIDmode;
4162 nmode = GET_MODE_WIDER_MODE (nmode))
4163 max_mname_len = MAX (max_mname_len, strlen (GET_MODE_NAME (nmode)));
4165 libfunc_name = alloca (2 + opname_len + 2*max_mname_len + 1 + 1);
4166 libfunc_name[0] = '_';
4167 libfunc_name[1] = '_';
4168 memcpy (&libfunc_name[2], opname, opname_len);
4169 suffix = libfunc_name + opname_len + 2;
4171 for (nmode = first_mode; nmode != VOIDmode;
4172 nmode = GET_MODE_WIDER_MODE (nmode))
4173 for (wmode = GET_MODE_WIDER_MODE (nmode); wmode != VOIDmode;
4174 wmode = GET_MODE_WIDER_MODE (wmode))
4176 nname = GET_MODE_NAME (nmode);
4177 wname = GET_MODE_NAME (wmode);
4179 p = suffix;
4180 for (q = widening ? nname : wname; *q; p++, q++)
4181 *p = TOLOWER (*q);
4182 for (q = widening ? wname : nname; *q; p++, q++)
4183 *p = TOLOWER (*q);
4185 *p++ = '2';
4186 *p = '\0';
4188 tab->handlers[widening ? wmode : nmode]
4189 [widening ? nmode : wmode].libfunc
4190 = init_one_libfunc (ggc_alloc_string (libfunc_name,
4191 p - libfunc_name));
4197 init_one_libfunc (const char *name)
4199 rtx symbol;
4201 /* Create a FUNCTION_DECL that can be passed to
4202 targetm.encode_section_info. */
4203 /* ??? We don't have any type information except for this is
4204 a function. Pretend this is "int foo()". */
4205 tree decl = build_decl (FUNCTION_DECL, get_identifier (name),
4206 build_function_type (integer_type_node, NULL_TREE));
4207 DECL_ARTIFICIAL (decl) = 1;
4208 DECL_EXTERNAL (decl) = 1;
4209 TREE_PUBLIC (decl) = 1;
4211 symbol = XEXP (DECL_RTL (decl), 0);
4213 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
4214 are the flags assigned by targetm.encode_section_info. */
4215 SYMBOL_REF_DECL (symbol) = 0;
4217 return symbol;
4220 /* Call this to reset the function entry for one optab (OPTABLE) in mode
4221 MODE to NAME, which should be either 0 or a string constant. */
4222 void
4223 set_optab_libfunc (optab optable, enum machine_mode mode, const char *name)
4225 if (name)
4226 optable->handlers[mode].libfunc = init_one_libfunc (name);
4227 else
4228 optable->handlers[mode].libfunc = 0;
4231 /* Call this to reset the function entry for one conversion optab
4232 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
4233 either 0 or a string constant. */
4234 void
4235 set_conv_libfunc (convert_optab optable, enum machine_mode tmode,
4236 enum machine_mode fmode, const char *name)
4238 if (name)
4239 optable->handlers[tmode][fmode].libfunc = init_one_libfunc (name);
4240 else
4241 optable->handlers[tmode][fmode].libfunc = 0;
4244 /* Call this once to initialize the contents of the optabs
4245 appropriately for the current target machine. */
4247 void
4248 init_optabs (void)
4250 unsigned int i;
4252 /* Start by initializing all tables to contain CODE_FOR_nothing. */
4254 for (i = 0; i < NUM_RTX_CODE; i++)
4255 setcc_gen_code[i] = CODE_FOR_nothing;
4257 #ifdef HAVE_conditional_move
4258 for (i = 0; i < NUM_MACHINE_MODES; i++)
4259 movcc_gen_code[i] = CODE_FOR_nothing;
4260 #endif
4262 add_optab = init_optab (PLUS);
4263 addv_optab = init_optabv (PLUS);
4264 sub_optab = init_optab (MINUS);
4265 subv_optab = init_optabv (MINUS);
4266 smul_optab = init_optab (MULT);
4267 smulv_optab = init_optabv (MULT);
4268 smul_highpart_optab = init_optab (UNKNOWN);
4269 umul_highpart_optab = init_optab (UNKNOWN);
4270 smul_widen_optab = init_optab (UNKNOWN);
4271 umul_widen_optab = init_optab (UNKNOWN);
4272 sdiv_optab = init_optab (DIV);
4273 sdivv_optab = init_optabv (DIV);
4274 sdivmod_optab = init_optab (UNKNOWN);
4275 udiv_optab = init_optab (UDIV);
4276 udivmod_optab = init_optab (UNKNOWN);
4277 smod_optab = init_optab (MOD);
4278 umod_optab = init_optab (UMOD);
4279 fmod_optab = init_optab (UNKNOWN);
4280 drem_optab = init_optab (UNKNOWN);
4281 ftrunc_optab = init_optab (UNKNOWN);
4282 and_optab = init_optab (AND);
4283 ior_optab = init_optab (IOR);
4284 xor_optab = init_optab (XOR);
4285 ashl_optab = init_optab (ASHIFT);
4286 ashr_optab = init_optab (ASHIFTRT);
4287 lshr_optab = init_optab (LSHIFTRT);
4288 rotl_optab = init_optab (ROTATE);
4289 rotr_optab = init_optab (ROTATERT);
4290 smin_optab = init_optab (SMIN);
4291 smax_optab = init_optab (SMAX);
4292 umin_optab = init_optab (UMIN);
4293 umax_optab = init_optab (UMAX);
4294 pow_optab = init_optab (UNKNOWN);
4295 atan2_optab = init_optab (UNKNOWN);
4297 /* These three have codes assigned exclusively for the sake of
4298 have_insn_for. */
4299 mov_optab = init_optab (SET);
4300 movstrict_optab = init_optab (STRICT_LOW_PART);
4301 cmp_optab = init_optab (COMPARE);
4303 ucmp_optab = init_optab (UNKNOWN);
4304 tst_optab = init_optab (UNKNOWN);
4306 eq_optab = init_optab (EQ);
4307 ne_optab = init_optab (NE);
4308 gt_optab = init_optab (GT);
4309 ge_optab = init_optab (GE);
4310 lt_optab = init_optab (LT);
4311 le_optab = init_optab (LE);
4312 unord_optab = init_optab (UNORDERED);
4314 neg_optab = init_optab (NEG);
4315 negv_optab = init_optabv (NEG);
4316 abs_optab = init_optab (ABS);
4317 absv_optab = init_optabv (ABS);
4318 addcc_optab = init_optab (UNKNOWN);
4319 one_cmpl_optab = init_optab (NOT);
4320 ffs_optab = init_optab (FFS);
4321 clz_optab = init_optab (CLZ);
4322 ctz_optab = init_optab (CTZ);
4323 popcount_optab = init_optab (POPCOUNT);
4324 parity_optab = init_optab (PARITY);
4325 sqrt_optab = init_optab (SQRT);
4326 floor_optab = init_optab (UNKNOWN);
4327 ceil_optab = init_optab (UNKNOWN);
4328 round_optab = init_optab (UNKNOWN);
4329 btrunc_optab = init_optab (UNKNOWN);
4330 nearbyint_optab = init_optab (UNKNOWN);
4331 rint_optab = init_optab (UNKNOWN);
4332 sincos_optab = init_optab (UNKNOWN);
4333 sin_optab = init_optab (UNKNOWN);
4334 asin_optab = init_optab (UNKNOWN);
4335 cos_optab = init_optab (UNKNOWN);
4336 acos_optab = init_optab (UNKNOWN);
4337 exp_optab = init_optab (UNKNOWN);
4338 exp10_optab = init_optab (UNKNOWN);
4339 exp2_optab = init_optab (UNKNOWN);
4340 expm1_optab = init_optab (UNKNOWN);
4341 logb_optab = init_optab (UNKNOWN);
4342 ilogb_optab = init_optab (UNKNOWN);
4343 log_optab = init_optab (UNKNOWN);
4344 log10_optab = init_optab (UNKNOWN);
4345 log2_optab = init_optab (UNKNOWN);
4346 log1p_optab = init_optab (UNKNOWN);
4347 tan_optab = init_optab (UNKNOWN);
4348 atan_optab = init_optab (UNKNOWN);
4349 strlen_optab = init_optab (UNKNOWN);
4350 cbranch_optab = init_optab (UNKNOWN);
4351 cmov_optab = init_optab (UNKNOWN);
4352 cstore_optab = init_optab (UNKNOWN);
4353 push_optab = init_optab (UNKNOWN);
4355 vec_extract_optab = init_optab (UNKNOWN);
4356 vec_set_optab = init_optab (UNKNOWN);
4357 vec_init_optab = init_optab (UNKNOWN);
4358 /* Conversions. */
4359 sext_optab = init_convert_optab (SIGN_EXTEND);
4360 zext_optab = init_convert_optab (ZERO_EXTEND);
4361 trunc_optab = init_convert_optab (TRUNCATE);
4362 sfix_optab = init_convert_optab (FIX);
4363 ufix_optab = init_convert_optab (UNSIGNED_FIX);
4364 sfixtrunc_optab = init_convert_optab (UNKNOWN);
4365 ufixtrunc_optab = init_convert_optab (UNKNOWN);
4366 sfloat_optab = init_convert_optab (FLOAT);
4367 ufloat_optab = init_convert_optab (UNSIGNED_FLOAT);
4369 for (i = 0; i < NUM_MACHINE_MODES; i++)
4371 movmem_optab[i] = CODE_FOR_nothing;
4372 clrmem_optab[i] = CODE_FOR_nothing;
4373 cmpstr_optab[i] = CODE_FOR_nothing;
4374 cmpmem_optab[i] = CODE_FOR_nothing;
4376 #ifdef HAVE_SECONDARY_RELOADS
4377 reload_in_optab[i] = reload_out_optab[i] = CODE_FOR_nothing;
4378 #endif
4381 /* Fill in the optabs with the insns we support. */
4382 init_all_optabs ();
4384 /* Initialize the optabs with the names of the library functions. */
4385 init_integral_libfuncs (add_optab, "add", '3');
4386 init_floating_libfuncs (add_optab, "add", '3');
4387 init_integral_libfuncs (addv_optab, "addv", '3');
4388 init_floating_libfuncs (addv_optab, "add", '3');
4389 init_integral_libfuncs (sub_optab, "sub", '3');
4390 init_floating_libfuncs (sub_optab, "sub", '3');
4391 init_integral_libfuncs (subv_optab, "subv", '3');
4392 init_floating_libfuncs (subv_optab, "sub", '3');
4393 init_integral_libfuncs (smul_optab, "mul", '3');
4394 init_floating_libfuncs (smul_optab, "mul", '3');
4395 init_integral_libfuncs (smulv_optab, "mulv", '3');
4396 init_floating_libfuncs (smulv_optab, "mul", '3');
4397 init_integral_libfuncs (sdiv_optab, "div", '3');
4398 init_floating_libfuncs (sdiv_optab, "div", '3');
4399 init_integral_libfuncs (sdivv_optab, "divv", '3');
4400 init_integral_libfuncs (udiv_optab, "udiv", '3');
4401 init_integral_libfuncs (sdivmod_optab, "divmod", '4');
4402 init_integral_libfuncs (udivmod_optab, "udivmod", '4');
4403 init_integral_libfuncs (smod_optab, "mod", '3');
4404 init_integral_libfuncs (umod_optab, "umod", '3');
4405 init_floating_libfuncs (ftrunc_optab, "ftrunc", '2');
4406 init_integral_libfuncs (and_optab, "and", '3');
4407 init_integral_libfuncs (ior_optab, "ior", '3');
4408 init_integral_libfuncs (xor_optab, "xor", '3');
4409 init_integral_libfuncs (ashl_optab, "ashl", '3');
4410 init_integral_libfuncs (ashr_optab, "ashr", '3');
4411 init_integral_libfuncs (lshr_optab, "lshr", '3');
4412 init_integral_libfuncs (smin_optab, "min", '3');
4413 init_floating_libfuncs (smin_optab, "min", '3');
4414 init_integral_libfuncs (smax_optab, "max", '3');
4415 init_floating_libfuncs (smax_optab, "max", '3');
4416 init_integral_libfuncs (umin_optab, "umin", '3');
4417 init_integral_libfuncs (umax_optab, "umax", '3');
4418 init_integral_libfuncs (neg_optab, "neg", '2');
4419 init_floating_libfuncs (neg_optab, "neg", '2');
4420 init_integral_libfuncs (negv_optab, "negv", '2');
4421 init_floating_libfuncs (negv_optab, "neg", '2');
4422 init_integral_libfuncs (one_cmpl_optab, "one_cmpl", '2');
4423 init_integral_libfuncs (ffs_optab, "ffs", '2');
4424 init_integral_libfuncs (clz_optab, "clz", '2');
4425 init_integral_libfuncs (ctz_optab, "ctz", '2');
4426 init_integral_libfuncs (popcount_optab, "popcount", '2');
4427 init_integral_libfuncs (parity_optab, "parity", '2');
4429 /* Comparison libcalls for integers MUST come in pairs,
4430 signed/unsigned. */
4431 init_integral_libfuncs (cmp_optab, "cmp", '2');
4432 init_integral_libfuncs (ucmp_optab, "ucmp", '2');
4433 init_floating_libfuncs (cmp_optab, "cmp", '2');
4435 /* EQ etc are floating point only. */
4436 init_floating_libfuncs (eq_optab, "eq", '2');
4437 init_floating_libfuncs (ne_optab, "ne", '2');
4438 init_floating_libfuncs (gt_optab, "gt", '2');
4439 init_floating_libfuncs (ge_optab, "ge", '2');
4440 init_floating_libfuncs (lt_optab, "lt", '2');
4441 init_floating_libfuncs (le_optab, "le", '2');
4442 init_floating_libfuncs (unord_optab, "unord", '2');
4444 /* Conversions. */
4445 init_interclass_conv_libfuncs (sfloat_optab, "float",
4446 MODE_INT, MODE_FLOAT);
4447 init_interclass_conv_libfuncs (sfix_optab, "fix",
4448 MODE_FLOAT, MODE_INT);
4449 init_interclass_conv_libfuncs (ufix_optab, "fixuns",
4450 MODE_FLOAT, MODE_INT);
4452 /* sext_optab is also used for FLOAT_EXTEND. */
4453 init_intraclass_conv_libfuncs (sext_optab, "extend", MODE_FLOAT, true);
4454 init_intraclass_conv_libfuncs (trunc_optab, "trunc", MODE_FLOAT, false);
4456 /* Use cabs for double complex abs, since systems generally have cabs.
4457 Don't define any libcall for float complex, so that cabs will be used. */
4458 if (complex_double_type_node)
4459 abs_optab->handlers[TYPE_MODE (complex_double_type_node)].libfunc
4460 = init_one_libfunc ("cabs");
4462 /* The ffs function operates on `int'. */
4463 ffs_optab->handlers[(int) mode_for_size (INT_TYPE_SIZE, MODE_INT, 0)].libfunc
4464 = init_one_libfunc ("ffs");
4466 abort_libfunc = init_one_libfunc ("abort");
4467 memcpy_libfunc = init_one_libfunc ("memcpy");
4468 memmove_libfunc = init_one_libfunc ("memmove");
4469 memcmp_libfunc = init_one_libfunc ("memcmp");
4470 memset_libfunc = init_one_libfunc ("memset");
4471 setbits_libfunc = init_one_libfunc ("__setbits");
4473 unwind_resume_libfunc = init_one_libfunc (USING_SJLJ_EXCEPTIONS
4474 ? "_Unwind_SjLj_Resume"
4475 : "_Unwind_Resume");
4476 #ifndef DONT_USE_BUILTIN_SETJMP
4477 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
4478 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
4479 #else
4480 setjmp_libfunc = init_one_libfunc ("setjmp");
4481 longjmp_libfunc = init_one_libfunc ("longjmp");
4482 #endif
4483 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
4484 unwind_sjlj_unregister_libfunc
4485 = init_one_libfunc ("_Unwind_SjLj_Unregister");
4487 /* For function entry/exit instrumentation. */
4488 profile_function_entry_libfunc
4489 = init_one_libfunc ("__cyg_profile_func_enter");
4490 profile_function_exit_libfunc
4491 = init_one_libfunc ("__cyg_profile_func_exit");
4493 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
4495 if (HAVE_conditional_trap)
4496 trap_rtx = gen_rtx_fmt_ee (EQ, VOIDmode, NULL_RTX, NULL_RTX);
4498 /* Allow the target to add more libcalls or rename some, etc. */
4499 targetm.init_libfuncs ();
4502 #ifdef DEBUG
4504 /* Print information about the current contents of the optabs on
4505 STDERR. */
4507 static void
4508 debug_optab_libfuncs (void)
4510 int i;
4511 int j;
4512 int k;
4514 /* Dump the arithmetic optabs. */
4515 for (i = 0; i != (int) OTI_MAX; i++)
4516 for (j = 0; j < NUM_MACHINE_MODES; ++j)
4518 optab o;
4519 struct optab_handlers *h;
4521 o = optab_table[i];
4522 h = &o->handlers[j];
4523 if (h->libfunc)
4525 if (GET_CODE (h->libfunc) != SYMBOL_REF)
4526 abort ();
4527 fprintf (stderr, "%s\t%s:\t%s\n",
4528 GET_RTX_NAME (o->code),
4529 GET_MODE_NAME (j),
4530 XSTR (h->libfunc, 0));
4534 /* Dump the conversion optabs. */
4535 for (i = 0; i < (int) CTI_MAX; ++i)
4536 for (j = 0; j < NUM_MACHINE_MODES; ++j)
4537 for (k = 0; k < NUM_MACHINE_MODES; ++k)
4539 convert_optab o;
4540 struct optab_handlers *h;
4542 o = &convert_optab_table[i];
4543 h = &o->handlers[j][k];
4544 if (h->libfunc)
4546 if (GET_CODE (h->libfunc) != SYMBOL_REF)
4547 abort ();
4548 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
4549 GET_RTX_NAME (o->code),
4550 GET_MODE_NAME (j),
4551 GET_MODE_NAME (k),
4552 XSTR (h->libfunc, 0));
4557 #endif /* DEBUG */
4560 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
4561 CODE. Return 0 on failure. */
4564 gen_cond_trap (enum rtx_code code ATTRIBUTE_UNUSED, rtx op1,
4565 rtx op2 ATTRIBUTE_UNUSED, rtx tcode ATTRIBUTE_UNUSED)
4567 enum machine_mode mode = GET_MODE (op1);
4568 enum insn_code icode;
4569 rtx insn;
4571 if (!HAVE_conditional_trap)
4572 return 0;
4574 if (mode == VOIDmode)
4575 return 0;
4577 icode = cmp_optab->handlers[(int) mode].insn_code;
4578 if (icode == CODE_FOR_nothing)
4579 return 0;
4581 start_sequence ();
4582 op1 = prepare_operand (icode, op1, 0, mode, mode, 0);
4583 op2 = prepare_operand (icode, op2, 1, mode, mode, 0);
4584 if (!op1 || !op2)
4586 end_sequence ();
4587 return 0;
4589 emit_insn (GEN_FCN (icode) (op1, op2));
4591 PUT_CODE (trap_rtx, code);
4592 insn = gen_conditional_trap (trap_rtx, tcode);
4593 if (insn)
4595 emit_insn (insn);
4596 insn = get_insns ();
4598 end_sequence ();
4600 return insn;
4603 #include "gt-optabs.h"