re checking -fdump-passes
[official-gcc.git] / gcc / optabs.c
blob224fb11b59b1210170b86a6fc5cb34c0df59c712
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "diagnostic-core.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 "basic-block.h"
45 #include "target.h"
47 struct target_optabs default_target_optabs;
48 struct target_libfuncs default_target_libfuncs;
49 #if SWITCHABLE_TARGET
50 struct target_optabs *this_target_optabs = &default_target_optabs;
51 struct target_libfuncs *this_target_libfuncs = &default_target_libfuncs;
52 #endif
54 #define libfunc_hash \
55 (this_target_libfuncs->x_libfunc_hash)
57 /* Contains the optab used for each rtx code. */
58 optab code_to_optab[NUM_RTX_CODE + 1];
60 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
61 enum machine_mode *);
62 static rtx expand_unop_direct (enum machine_mode, optab, rtx, rtx, int);
64 /* Debug facility for use in GDB. */
65 void debug_optab_libfuncs (void);
67 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
68 #if ENABLE_DECIMAL_BID_FORMAT
69 #define DECIMAL_PREFIX "bid_"
70 #else
71 #define DECIMAL_PREFIX "dpd_"
72 #endif
74 /* Used for libfunc_hash. */
76 static hashval_t
77 hash_libfunc (const void *p)
79 const struct libfunc_entry *const e = (const struct libfunc_entry *) p;
81 return (((int) e->mode1 + (int) e->mode2 * NUM_MACHINE_MODES)
82 ^ e->optab);
85 /* Used for libfunc_hash. */
87 static int
88 eq_libfunc (const void *p, const void *q)
90 const struct libfunc_entry *const e1 = (const struct libfunc_entry *) p;
91 const struct libfunc_entry *const e2 = (const struct libfunc_entry *) q;
93 return (e1->optab == e2->optab
94 && e1->mode1 == e2->mode1
95 && e1->mode2 == e2->mode2);
98 /* Return libfunc corresponding operation defined by OPTAB converting
99 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
100 if no libfunc is available. */
102 convert_optab_libfunc (convert_optab optab, enum machine_mode mode1,
103 enum machine_mode mode2)
105 struct libfunc_entry e;
106 struct libfunc_entry **slot;
108 e.optab = (size_t) (optab - &convert_optab_table[0]);
109 e.mode1 = mode1;
110 e.mode2 = mode2;
111 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
112 if (!slot)
114 if (optab->libcall_gen)
116 optab->libcall_gen (optab, optab->libcall_basename, mode1, mode2);
117 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
118 if (slot)
119 return (*slot)->libfunc;
120 else
121 return NULL;
123 return NULL;
125 return (*slot)->libfunc;
128 /* Return libfunc corresponding operation defined by OPTAB in MODE.
129 Trigger lazy initialization if needed, return NULL if no libfunc is
130 available. */
132 optab_libfunc (optab optab, enum machine_mode mode)
134 struct libfunc_entry e;
135 struct libfunc_entry **slot;
137 e.optab = (size_t) (optab - &optab_table[0]);
138 e.mode1 = mode;
139 e.mode2 = VOIDmode;
140 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
141 if (!slot)
143 if (optab->libcall_gen)
145 optab->libcall_gen (optab, optab->libcall_basename,
146 optab->libcall_suffix, mode);
147 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash,
148 &e, NO_INSERT);
149 if (slot)
150 return (*slot)->libfunc;
151 else
152 return NULL;
154 return NULL;
156 return (*slot)->libfunc;
160 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
161 the result of operation CODE applied to OP0 (and OP1 if it is a binary
162 operation).
164 If the last insn does not set TARGET, don't do anything, but return 1.
166 If a previous insn sets TARGET and TARGET is one of OP0 or OP1,
167 don't add the REG_EQUAL note but return 0. Our caller can then try
168 again, ensuring that TARGET is not one of the operands. */
170 static int
171 add_equal_note (rtx insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
173 rtx last_insn, insn, set;
174 rtx note;
176 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
178 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
179 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
180 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
181 && GET_RTX_CLASS (code) != RTX_COMPARE
182 && GET_RTX_CLASS (code) != RTX_UNARY)
183 return 1;
185 if (GET_CODE (target) == ZERO_EXTRACT)
186 return 1;
188 for (last_insn = insns;
189 NEXT_INSN (last_insn) != NULL_RTX;
190 last_insn = NEXT_INSN (last_insn))
193 set = single_set (last_insn);
194 if (set == NULL_RTX)
195 return 1;
197 if (! rtx_equal_p (SET_DEST (set), target)
198 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
199 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
200 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
201 return 1;
203 /* If TARGET is in OP0 or OP1, check if anything in SEQ sets TARGET
204 besides the last insn. */
205 if (reg_overlap_mentioned_p (target, op0)
206 || (op1 && reg_overlap_mentioned_p (target, op1)))
208 insn = PREV_INSN (last_insn);
209 while (insn != NULL_RTX)
211 if (reg_set_p (target, insn))
212 return 0;
214 insn = PREV_INSN (insn);
218 if (GET_RTX_CLASS (code) == RTX_UNARY)
219 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
220 else
221 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
223 set_unique_reg_note (last_insn, REG_EQUAL, note);
225 return 1;
228 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
229 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
230 not actually do a sign-extend or zero-extend, but can leave the
231 higher-order bits of the result rtx undefined, for example, in the case
232 of logical operations, but not right shifts. */
234 static rtx
235 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
236 int unsignedp, int no_extend)
238 rtx result;
240 /* If we don't have to extend and this is a constant, return it. */
241 if (no_extend && GET_MODE (op) == VOIDmode)
242 return op;
244 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
245 extend since it will be more efficient to do so unless the signedness of
246 a promoted object differs from our extension. */
247 if (! no_extend
248 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
249 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
250 return convert_modes (mode, oldmode, op, unsignedp);
252 /* If MODE is no wider than a single word, we return a paradoxical
253 SUBREG. */
254 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
255 return gen_rtx_SUBREG (mode, force_reg (GET_MODE (op), op), 0);
257 /* Otherwise, get an object of MODE, clobber it, and set the low-order
258 part to OP. */
260 result = gen_reg_rtx (mode);
261 emit_clobber (result);
262 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
263 return result;
266 /* Return the optab used for computing the operation given by the tree code,
267 CODE and the tree EXP. This function is not always usable (for example, it
268 cannot give complete results for multiplication or division) but probably
269 ought to be relied on more widely throughout the expander. */
270 optab
271 optab_for_tree_code (enum tree_code code, const_tree type,
272 enum optab_subtype subtype)
274 bool trapv;
275 switch (code)
277 case BIT_AND_EXPR:
278 return and_optab;
280 case BIT_IOR_EXPR:
281 return ior_optab;
283 case BIT_NOT_EXPR:
284 return one_cmpl_optab;
286 case BIT_XOR_EXPR:
287 return xor_optab;
289 case TRUNC_MOD_EXPR:
290 case CEIL_MOD_EXPR:
291 case FLOOR_MOD_EXPR:
292 case ROUND_MOD_EXPR:
293 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
295 case RDIV_EXPR:
296 case TRUNC_DIV_EXPR:
297 case CEIL_DIV_EXPR:
298 case FLOOR_DIV_EXPR:
299 case ROUND_DIV_EXPR:
300 case EXACT_DIV_EXPR:
301 if (TYPE_SATURATING(type))
302 return TYPE_UNSIGNED(type) ? usdiv_optab : ssdiv_optab;
303 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
305 case LSHIFT_EXPR:
306 if (TREE_CODE (type) == VECTOR_TYPE)
308 if (subtype == optab_vector)
309 return TYPE_SATURATING (type) ? NULL : vashl_optab;
311 gcc_assert (subtype == optab_scalar);
313 if (TYPE_SATURATING(type))
314 return TYPE_UNSIGNED(type) ? usashl_optab : ssashl_optab;
315 return ashl_optab;
317 case RSHIFT_EXPR:
318 if (TREE_CODE (type) == VECTOR_TYPE)
320 if (subtype == optab_vector)
321 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
323 gcc_assert (subtype == optab_scalar);
325 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
327 case LROTATE_EXPR:
328 if (TREE_CODE (type) == VECTOR_TYPE)
330 if (subtype == optab_vector)
331 return vrotl_optab;
333 gcc_assert (subtype == optab_scalar);
335 return rotl_optab;
337 case RROTATE_EXPR:
338 if (TREE_CODE (type) == VECTOR_TYPE)
340 if (subtype == optab_vector)
341 return vrotr_optab;
343 gcc_assert (subtype == optab_scalar);
345 return rotr_optab;
347 case MAX_EXPR:
348 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
350 case MIN_EXPR:
351 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
353 case REALIGN_LOAD_EXPR:
354 return vec_realign_load_optab;
356 case WIDEN_SUM_EXPR:
357 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
359 case DOT_PROD_EXPR:
360 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
362 case WIDEN_MULT_PLUS_EXPR:
363 return (TYPE_UNSIGNED (type)
364 ? (TYPE_SATURATING (type)
365 ? usmadd_widen_optab : umadd_widen_optab)
366 : (TYPE_SATURATING (type)
367 ? ssmadd_widen_optab : smadd_widen_optab));
369 case WIDEN_MULT_MINUS_EXPR:
370 return (TYPE_UNSIGNED (type)
371 ? (TYPE_SATURATING (type)
372 ? usmsub_widen_optab : umsub_widen_optab)
373 : (TYPE_SATURATING (type)
374 ? ssmsub_widen_optab : smsub_widen_optab));
376 case FMA_EXPR:
377 return fma_optab;
379 case REDUC_MAX_EXPR:
380 return TYPE_UNSIGNED (type) ? reduc_umax_optab : reduc_smax_optab;
382 case REDUC_MIN_EXPR:
383 return TYPE_UNSIGNED (type) ? reduc_umin_optab : reduc_smin_optab;
385 case REDUC_PLUS_EXPR:
386 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
388 case VEC_LSHIFT_EXPR:
389 return vec_shl_optab;
391 case VEC_RSHIFT_EXPR:
392 return vec_shr_optab;
394 case VEC_WIDEN_MULT_HI_EXPR:
395 return TYPE_UNSIGNED (type) ?
396 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
398 case VEC_WIDEN_MULT_LO_EXPR:
399 return TYPE_UNSIGNED (type) ?
400 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
402 case VEC_UNPACK_HI_EXPR:
403 return TYPE_UNSIGNED (type) ?
404 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
406 case VEC_UNPACK_LO_EXPR:
407 return TYPE_UNSIGNED (type) ?
408 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
410 case VEC_UNPACK_FLOAT_HI_EXPR:
411 /* The signedness is determined from input operand. */
412 return TYPE_UNSIGNED (type) ?
413 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
415 case VEC_UNPACK_FLOAT_LO_EXPR:
416 /* The signedness is determined from input operand. */
417 return TYPE_UNSIGNED (type) ?
418 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
420 case VEC_PACK_TRUNC_EXPR:
421 return vec_pack_trunc_optab;
423 case VEC_PACK_SAT_EXPR:
424 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
426 case VEC_PACK_FIX_TRUNC_EXPR:
427 /* The signedness is determined from output operand. */
428 return TYPE_UNSIGNED (type) ?
429 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
431 default:
432 break;
435 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
436 switch (code)
438 case POINTER_PLUS_EXPR:
439 case PLUS_EXPR:
440 if (TYPE_SATURATING(type))
441 return TYPE_UNSIGNED(type) ? usadd_optab : ssadd_optab;
442 return trapv ? addv_optab : add_optab;
444 case MINUS_EXPR:
445 if (TYPE_SATURATING(type))
446 return TYPE_UNSIGNED(type) ? ussub_optab : sssub_optab;
447 return trapv ? subv_optab : sub_optab;
449 case MULT_EXPR:
450 if (TYPE_SATURATING(type))
451 return TYPE_UNSIGNED(type) ? usmul_optab : ssmul_optab;
452 return trapv ? smulv_optab : smul_optab;
454 case NEGATE_EXPR:
455 if (TYPE_SATURATING(type))
456 return TYPE_UNSIGNED(type) ? usneg_optab : ssneg_optab;
457 return trapv ? negv_optab : neg_optab;
459 case ABS_EXPR:
460 return trapv ? absv_optab : abs_optab;
462 case VEC_EXTRACT_EVEN_EXPR:
463 return vec_extract_even_optab;
465 case VEC_EXTRACT_ODD_EXPR:
466 return vec_extract_odd_optab;
468 case VEC_INTERLEAVE_HIGH_EXPR:
469 return vec_interleave_high_optab;
471 case VEC_INTERLEAVE_LOW_EXPR:
472 return vec_interleave_low_optab;
474 default:
475 return NULL;
480 /* Expand vector widening operations.
482 There are two different classes of operations handled here:
483 1) Operations whose result is wider than all the arguments to the operation.
484 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
485 In this case OP0 and optionally OP1 would be initialized,
486 but WIDE_OP wouldn't (not relevant for this case).
487 2) Operations whose result is of the same size as the last argument to the
488 operation, but wider than all the other arguments to the operation.
489 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
490 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
492 E.g, when called to expand the following operations, this is how
493 the arguments will be initialized:
494 nops OP0 OP1 WIDE_OP
495 widening-sum 2 oprnd0 - oprnd1
496 widening-dot-product 3 oprnd0 oprnd1 oprnd2
497 widening-mult 2 oprnd0 oprnd1 -
498 type-promotion (vec-unpack) 1 oprnd0 - - */
501 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
502 rtx target, int unsignedp)
504 struct expand_operand eops[4];
505 tree oprnd0, oprnd1, oprnd2;
506 enum machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
507 optab widen_pattern_optab;
508 enum insn_code icode;
509 int nops = TREE_CODE_LENGTH (ops->code);
510 int op;
512 oprnd0 = ops->op0;
513 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
514 widen_pattern_optab =
515 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
516 if (ops->code == WIDEN_MULT_PLUS_EXPR
517 || ops->code == WIDEN_MULT_MINUS_EXPR)
518 icode = optab_handler (widen_pattern_optab,
519 TYPE_MODE (TREE_TYPE (ops->op2)));
520 else
521 icode = optab_handler (widen_pattern_optab, tmode0);
522 gcc_assert (icode != CODE_FOR_nothing);
524 if (nops >= 2)
526 oprnd1 = ops->op1;
527 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
530 /* The last operand is of a wider mode than the rest of the operands. */
531 if (nops == 2)
532 wmode = tmode1;
533 else if (nops == 3)
535 gcc_assert (tmode1 == tmode0);
536 gcc_assert (op1);
537 oprnd2 = ops->op2;
538 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
541 op = 0;
542 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
543 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
544 if (op1)
545 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
546 if (wide_op)
547 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
548 expand_insn (icode, op, eops);
549 return eops[0].value;
552 /* Generate code to perform an operation specified by TERNARY_OPTAB
553 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
555 UNSIGNEDP is for the case where we have to widen the operands
556 to perform the operation. It says to use zero-extension.
558 If TARGET is nonzero, the value
559 is generated there, if it is convenient to do so.
560 In all cases an rtx is returned for the locus of the value;
561 this may or may not be TARGET. */
564 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
565 rtx op1, rtx op2, rtx target, int unsignedp)
567 struct expand_operand ops[4];
568 enum insn_code icode = optab_handler (ternary_optab, mode);
570 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
572 create_output_operand (&ops[0], target, mode);
573 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
574 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
575 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
576 expand_insn (icode, 4, ops);
577 return ops[0].value;
581 /* Like expand_binop, but return a constant rtx if the result can be
582 calculated at compile time. The arguments and return value are
583 otherwise the same as for expand_binop. */
585 static rtx
586 simplify_expand_binop (enum machine_mode mode, optab binoptab,
587 rtx op0, rtx op1, rtx target, int unsignedp,
588 enum optab_methods methods)
590 if (CONSTANT_P (op0) && CONSTANT_P (op1))
592 rtx x = simplify_binary_operation (binoptab->code, mode, op0, op1);
594 if (x)
595 return x;
598 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
601 /* Like simplify_expand_binop, but always put the result in TARGET.
602 Return true if the expansion succeeded. */
604 bool
605 force_expand_binop (enum machine_mode mode, optab binoptab,
606 rtx op0, rtx op1, rtx target, int unsignedp,
607 enum optab_methods methods)
609 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
610 target, unsignedp, methods);
611 if (x == 0)
612 return false;
613 if (x != target)
614 emit_move_insn (target, x);
615 return true;
618 /* Generate insns for VEC_LSHIFT_EXPR, VEC_RSHIFT_EXPR. */
621 expand_vec_shift_expr (sepops ops, rtx target)
623 struct expand_operand eops[3];
624 enum insn_code icode;
625 rtx rtx_op1, rtx_op2;
626 enum machine_mode mode = TYPE_MODE (ops->type);
627 tree vec_oprnd = ops->op0;
628 tree shift_oprnd = ops->op1;
629 optab shift_optab;
631 switch (ops->code)
633 case VEC_RSHIFT_EXPR:
634 shift_optab = vec_shr_optab;
635 break;
636 case VEC_LSHIFT_EXPR:
637 shift_optab = vec_shl_optab;
638 break;
639 default:
640 gcc_unreachable ();
643 icode = optab_handler (shift_optab, mode);
644 gcc_assert (icode != CODE_FOR_nothing);
646 rtx_op1 = expand_normal (vec_oprnd);
647 rtx_op2 = expand_normal (shift_oprnd);
649 create_output_operand (&eops[0], target, mode);
650 create_input_operand (&eops[1], rtx_op1, GET_MODE (rtx_op1));
651 create_convert_operand_from_type (&eops[2], rtx_op2, TREE_TYPE (shift_oprnd));
652 expand_insn (icode, 3, eops);
654 return eops[0].value;
657 /* This subroutine of expand_doubleword_shift handles the cases in which
658 the effective shift value is >= BITS_PER_WORD. The arguments and return
659 value are the same as for the parent routine, except that SUPERWORD_OP1
660 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
661 INTO_TARGET may be null if the caller has decided to calculate it. */
663 static bool
664 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
665 rtx outof_target, rtx into_target,
666 int unsignedp, enum optab_methods methods)
668 if (into_target != 0)
669 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
670 into_target, unsignedp, methods))
671 return false;
673 if (outof_target != 0)
675 /* For a signed right shift, we must fill OUTOF_TARGET with copies
676 of the sign bit, otherwise we must fill it with zeros. */
677 if (binoptab != ashr_optab)
678 emit_move_insn (outof_target, CONST0_RTX (word_mode));
679 else
680 if (!force_expand_binop (word_mode, binoptab,
681 outof_input, GEN_INT (BITS_PER_WORD - 1),
682 outof_target, unsignedp, methods))
683 return false;
685 return true;
688 /* This subroutine of expand_doubleword_shift handles the cases in which
689 the effective shift value is < BITS_PER_WORD. The arguments and return
690 value are the same as for the parent routine. */
692 static bool
693 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
694 rtx outof_input, rtx into_input, rtx op1,
695 rtx outof_target, rtx into_target,
696 int unsignedp, enum optab_methods methods,
697 unsigned HOST_WIDE_INT shift_mask)
699 optab reverse_unsigned_shift, unsigned_shift;
700 rtx tmp, carries;
702 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
703 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
705 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
706 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
707 the opposite direction to BINOPTAB. */
708 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
710 carries = outof_input;
711 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
712 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
713 0, true, methods);
715 else
717 /* We must avoid shifting by BITS_PER_WORD bits since that is either
718 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
719 has unknown behavior. Do a single shift first, then shift by the
720 remainder. It's OK to use ~OP1 as the remainder if shift counts
721 are truncated to the mode size. */
722 carries = expand_binop (word_mode, reverse_unsigned_shift,
723 outof_input, const1_rtx, 0, unsignedp, methods);
724 if (shift_mask == BITS_PER_WORD - 1)
726 tmp = immed_double_const (-1, -1, op1_mode);
727 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
728 0, true, methods);
730 else
732 tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
733 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
734 0, true, methods);
737 if (tmp == 0 || carries == 0)
738 return false;
739 carries = expand_binop (word_mode, reverse_unsigned_shift,
740 carries, tmp, 0, unsignedp, methods);
741 if (carries == 0)
742 return false;
744 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
745 so the result can go directly into INTO_TARGET if convenient. */
746 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
747 into_target, unsignedp, methods);
748 if (tmp == 0)
749 return false;
751 /* Now OR in the bits carried over from OUTOF_INPUT. */
752 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
753 into_target, unsignedp, methods))
754 return false;
756 /* Use a standard word_mode shift for the out-of half. */
757 if (outof_target != 0)
758 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
759 outof_target, unsignedp, methods))
760 return false;
762 return true;
766 #ifdef HAVE_conditional_move
767 /* Try implementing expand_doubleword_shift using conditional moves.
768 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
769 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
770 are the shift counts to use in the former and latter case. All other
771 arguments are the same as the parent routine. */
773 static bool
774 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
775 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
776 rtx outof_input, rtx into_input,
777 rtx subword_op1, rtx superword_op1,
778 rtx outof_target, rtx into_target,
779 int unsignedp, enum optab_methods methods,
780 unsigned HOST_WIDE_INT shift_mask)
782 rtx outof_superword, into_superword;
784 /* Put the superword version of the output into OUTOF_SUPERWORD and
785 INTO_SUPERWORD. */
786 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
787 if (outof_target != 0 && subword_op1 == superword_op1)
789 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
790 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
791 into_superword = outof_target;
792 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
793 outof_superword, 0, unsignedp, methods))
794 return false;
796 else
798 into_superword = gen_reg_rtx (word_mode);
799 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
800 outof_superword, into_superword,
801 unsignedp, methods))
802 return false;
805 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
806 if (!expand_subword_shift (op1_mode, binoptab,
807 outof_input, into_input, subword_op1,
808 outof_target, into_target,
809 unsignedp, methods, shift_mask))
810 return false;
812 /* Select between them. Do the INTO half first because INTO_SUPERWORD
813 might be the current value of OUTOF_TARGET. */
814 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
815 into_target, into_superword, word_mode, false))
816 return false;
818 if (outof_target != 0)
819 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
820 outof_target, outof_superword,
821 word_mode, false))
822 return false;
824 return true;
826 #endif
828 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
829 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
830 input operand; the shift moves bits in the direction OUTOF_INPUT->
831 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
832 of the target. OP1 is the shift count and OP1_MODE is its mode.
833 If OP1 is constant, it will have been truncated as appropriate
834 and is known to be nonzero.
836 If SHIFT_MASK is zero, the result of word shifts is undefined when the
837 shift count is outside the range [0, BITS_PER_WORD). This routine must
838 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
840 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
841 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
842 fill with zeros or sign bits as appropriate.
844 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
845 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
846 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
847 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
848 are undefined.
850 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
851 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
852 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
853 function wants to calculate it itself.
855 Return true if the shift could be successfully synthesized. */
857 static bool
858 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
859 rtx outof_input, rtx into_input, rtx op1,
860 rtx outof_target, rtx into_target,
861 int unsignedp, enum optab_methods methods,
862 unsigned HOST_WIDE_INT shift_mask)
864 rtx superword_op1, tmp, cmp1, cmp2;
865 rtx subword_label, done_label;
866 enum rtx_code cmp_code;
868 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
869 fill the result with sign or zero bits as appropriate. If so, the value
870 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
871 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
872 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
874 This isn't worthwhile for constant shifts since the optimizers will
875 cope better with in-range shift counts. */
876 if (shift_mask >= BITS_PER_WORD
877 && outof_target != 0
878 && !CONSTANT_P (op1))
880 if (!expand_doubleword_shift (op1_mode, binoptab,
881 outof_input, into_input, op1,
882 0, into_target,
883 unsignedp, methods, shift_mask))
884 return false;
885 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
886 outof_target, unsignedp, methods))
887 return false;
888 return true;
891 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
892 is true when the effective shift value is less than BITS_PER_WORD.
893 Set SUPERWORD_OP1 to the shift count that should be used to shift
894 OUTOF_INPUT into INTO_TARGET when the condition is false. */
895 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
896 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
898 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
899 is a subword shift count. */
900 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
901 0, true, methods);
902 cmp2 = CONST0_RTX (op1_mode);
903 cmp_code = EQ;
904 superword_op1 = op1;
906 else
908 /* Set CMP1 to OP1 - BITS_PER_WORD. */
909 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
910 0, true, methods);
911 cmp2 = CONST0_RTX (op1_mode);
912 cmp_code = LT;
913 superword_op1 = cmp1;
915 if (cmp1 == 0)
916 return false;
918 /* If we can compute the condition at compile time, pick the
919 appropriate subroutine. */
920 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
921 if (tmp != 0 && CONST_INT_P (tmp))
923 if (tmp == const0_rtx)
924 return expand_superword_shift (binoptab, outof_input, superword_op1,
925 outof_target, into_target,
926 unsignedp, methods);
927 else
928 return expand_subword_shift (op1_mode, binoptab,
929 outof_input, into_input, op1,
930 outof_target, into_target,
931 unsignedp, methods, shift_mask);
934 #ifdef HAVE_conditional_move
935 /* Try using conditional moves to generate straight-line code. */
937 rtx start = get_last_insn ();
938 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
939 cmp_code, cmp1, cmp2,
940 outof_input, into_input,
941 op1, superword_op1,
942 outof_target, into_target,
943 unsignedp, methods, shift_mask))
944 return true;
945 delete_insns_since (start);
947 #endif
949 /* As a last resort, use branches to select the correct alternative. */
950 subword_label = gen_label_rtx ();
951 done_label = gen_label_rtx ();
953 NO_DEFER_POP;
954 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
955 0, 0, subword_label, -1);
956 OK_DEFER_POP;
958 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
959 outof_target, into_target,
960 unsignedp, methods))
961 return false;
963 emit_jump_insn (gen_jump (done_label));
964 emit_barrier ();
965 emit_label (subword_label);
967 if (!expand_subword_shift (op1_mode, binoptab,
968 outof_input, into_input, op1,
969 outof_target, into_target,
970 unsignedp, methods, shift_mask))
971 return false;
973 emit_label (done_label);
974 return true;
977 /* Subroutine of expand_binop. Perform a double word multiplication of
978 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
979 as the target's word_mode. This function return NULL_RTX if anything
980 goes wrong, in which case it may have already emitted instructions
981 which need to be deleted.
983 If we want to multiply two two-word values and have normal and widening
984 multiplies of single-word values, we can do this with three smaller
985 multiplications.
987 The multiplication proceeds as follows:
988 _______________________
989 [__op0_high_|__op0_low__]
990 _______________________
991 * [__op1_high_|__op1_low__]
992 _______________________________________________
993 _______________________
994 (1) [__op0_low__*__op1_low__]
995 _______________________
996 (2a) [__op0_low__*__op1_high_]
997 _______________________
998 (2b) [__op0_high_*__op1_low__]
999 _______________________
1000 (3) [__op0_high_*__op1_high_]
1003 This gives a 4-word result. Since we are only interested in the
1004 lower 2 words, partial result (3) and the upper words of (2a) and
1005 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1006 calculated using non-widening multiplication.
1008 (1), however, needs to be calculated with an unsigned widening
1009 multiplication. If this operation is not directly supported we
1010 try using a signed widening multiplication and adjust the result.
1011 This adjustment works as follows:
1013 If both operands are positive then no adjustment is needed.
1015 If the operands have different signs, for example op0_low < 0 and
1016 op1_low >= 0, the instruction treats the most significant bit of
1017 op0_low as a sign bit instead of a bit with significance
1018 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1019 with 2**BITS_PER_WORD - op0_low, and two's complements the
1020 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1021 the result.
1023 Similarly, if both operands are negative, we need to add
1024 (op0_low + op1_low) * 2**BITS_PER_WORD.
1026 We use a trick to adjust quickly. We logically shift op0_low right
1027 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1028 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1029 logical shift exists, we do an arithmetic right shift and subtract
1030 the 0 or -1. */
1032 static rtx
1033 expand_doubleword_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
1034 bool umulp, enum optab_methods methods)
1036 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1037 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1038 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1039 rtx product, adjust, product_high, temp;
1041 rtx op0_high = operand_subword_force (op0, high, mode);
1042 rtx op0_low = operand_subword_force (op0, low, mode);
1043 rtx op1_high = operand_subword_force (op1, high, mode);
1044 rtx op1_low = operand_subword_force (op1, low, mode);
1046 /* If we're using an unsigned multiply to directly compute the product
1047 of the low-order words of the operands and perform any required
1048 adjustments of the operands, we begin by trying two more multiplications
1049 and then computing the appropriate sum.
1051 We have checked above that the required addition is provided.
1052 Full-word addition will normally always succeed, especially if
1053 it is provided at all, so we don't worry about its failure. The
1054 multiplication may well fail, however, so we do handle that. */
1056 if (!umulp)
1058 /* ??? This could be done with emit_store_flag where available. */
1059 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1060 NULL_RTX, 1, methods);
1061 if (temp)
1062 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1063 NULL_RTX, 0, OPTAB_DIRECT);
1064 else
1066 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1067 NULL_RTX, 0, methods);
1068 if (!temp)
1069 return NULL_RTX;
1070 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1071 NULL_RTX, 0, OPTAB_DIRECT);
1074 if (!op0_high)
1075 return NULL_RTX;
1078 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1079 NULL_RTX, 0, OPTAB_DIRECT);
1080 if (!adjust)
1081 return NULL_RTX;
1083 /* OP0_HIGH should now be dead. */
1085 if (!umulp)
1087 /* ??? This could be done with emit_store_flag where available. */
1088 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1089 NULL_RTX, 1, methods);
1090 if (temp)
1091 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1092 NULL_RTX, 0, OPTAB_DIRECT);
1093 else
1095 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1096 NULL_RTX, 0, methods);
1097 if (!temp)
1098 return NULL_RTX;
1099 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1100 NULL_RTX, 0, OPTAB_DIRECT);
1103 if (!op1_high)
1104 return NULL_RTX;
1107 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1108 NULL_RTX, 0, OPTAB_DIRECT);
1109 if (!temp)
1110 return NULL_RTX;
1112 /* OP1_HIGH should now be dead. */
1114 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1115 NULL_RTX, 0, OPTAB_DIRECT);
1117 if (target && !REG_P (target))
1118 target = NULL_RTX;
1120 if (umulp)
1121 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1122 target, 1, OPTAB_DIRECT);
1123 else
1124 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1125 target, 1, OPTAB_DIRECT);
1127 if (!product)
1128 return NULL_RTX;
1130 product_high = operand_subword (product, high, 1, mode);
1131 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1132 NULL_RTX, 0, OPTAB_DIRECT);
1133 emit_move_insn (product_high, adjust);
1134 return product;
1137 /* Wrapper around expand_binop which takes an rtx code to specify
1138 the operation to perform, not an optab pointer. All other
1139 arguments are the same. */
1141 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
1142 rtx op1, rtx target, int unsignedp,
1143 enum optab_methods methods)
1145 optab binop = code_to_optab[(int) code];
1146 gcc_assert (binop);
1148 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1151 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1152 binop. Order them according to commutative_operand_precedence and, if
1153 possible, try to put TARGET or a pseudo first. */
1154 static bool
1155 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1157 int op0_prec = commutative_operand_precedence (op0);
1158 int op1_prec = commutative_operand_precedence (op1);
1160 if (op0_prec < op1_prec)
1161 return true;
1163 if (op0_prec > op1_prec)
1164 return false;
1166 /* With equal precedence, both orders are ok, but it is better if the
1167 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1168 if (target == 0 || REG_P (target))
1169 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1170 else
1171 return rtx_equal_p (op1, target);
1174 /* Return true if BINOPTAB implements a shift operation. */
1176 static bool
1177 shift_optab_p (optab binoptab)
1179 switch (binoptab->code)
1181 case ASHIFT:
1182 case SS_ASHIFT:
1183 case US_ASHIFT:
1184 case ASHIFTRT:
1185 case LSHIFTRT:
1186 case ROTATE:
1187 case ROTATERT:
1188 return true;
1190 default:
1191 return false;
1195 /* Return true if BINOPTAB implements a commutative binary operation. */
1197 static bool
1198 commutative_optab_p (optab binoptab)
1200 return (GET_RTX_CLASS (binoptab->code) == RTX_COMM_ARITH
1201 || binoptab == smul_widen_optab
1202 || binoptab == umul_widen_optab
1203 || binoptab == smul_highpart_optab
1204 || binoptab == umul_highpart_optab);
1207 /* X is to be used in mode MODE as an operand to BINOPTAB. If we're
1208 optimizing, and if the operand is a constant that costs more than
1209 1 instruction, force the constant into a register and return that
1210 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1212 static rtx
1213 avoid_expensive_constant (enum machine_mode mode, optab binoptab,
1214 rtx x, bool unsignedp)
1216 bool speed = optimize_insn_for_speed_p ();
1218 if (mode != VOIDmode
1219 && optimize
1220 && CONSTANT_P (x)
1221 && rtx_cost (x, binoptab->code, speed) > rtx_cost (x, SET, speed))
1223 if (CONST_INT_P (x))
1225 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1226 if (intval != INTVAL (x))
1227 x = GEN_INT (intval);
1229 else
1230 x = convert_modes (mode, VOIDmode, x, unsignedp);
1231 x = force_reg (mode, x);
1233 return x;
1236 /* Helper function for expand_binop: handle the case where there
1237 is an insn that directly implements the indicated operation.
1238 Returns null if this is not possible. */
1239 static rtx
1240 expand_binop_directly (enum machine_mode mode, optab binoptab,
1241 rtx op0, rtx op1,
1242 rtx target, int unsignedp, enum optab_methods methods,
1243 rtx last)
1245 enum insn_code icode = optab_handler (binoptab, mode);
1246 enum machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1247 enum machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1248 enum machine_mode mode0, mode1, tmp_mode;
1249 struct expand_operand ops[3];
1250 bool commutative_p;
1251 rtx pat;
1252 rtx xop0 = op0, xop1 = op1;
1253 rtx swap;
1255 /* If it is a commutative operator and the modes would match
1256 if we would swap the operands, we can save the conversions. */
1257 commutative_p = commutative_optab_p (binoptab);
1258 if (commutative_p
1259 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1260 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1262 swap = xop0;
1263 xop0 = xop1;
1264 xop1 = swap;
1267 /* If we are optimizing, force expensive constants into a register. */
1268 xop0 = avoid_expensive_constant (xmode0, binoptab, xop0, unsignedp);
1269 if (!shift_optab_p (binoptab))
1270 xop1 = avoid_expensive_constant (xmode1, binoptab, xop1, unsignedp);
1272 /* In case the insn wants input operands in modes different from
1273 those of the actual operands, convert the operands. It would
1274 seem that we don't need to convert CONST_INTs, but we do, so
1275 that they're properly zero-extended, sign-extended or truncated
1276 for their mode. */
1278 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1279 if (xmode0 != VOIDmode && xmode0 != mode0)
1281 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1282 mode0 = xmode0;
1285 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1286 if (xmode1 != VOIDmode && xmode1 != mode1)
1288 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1289 mode1 = xmode1;
1292 /* If operation is commutative,
1293 try to make the first operand a register.
1294 Even better, try to make it the same as the target.
1295 Also try to make the last operand a constant. */
1296 if (commutative_p
1297 && swap_commutative_operands_with_target (target, xop0, xop1))
1299 swap = xop1;
1300 xop1 = xop0;
1301 xop0 = swap;
1304 /* Now, if insn's predicates don't allow our operands, put them into
1305 pseudo regs. */
1307 if (binoptab == vec_pack_trunc_optab
1308 || binoptab == vec_pack_usat_optab
1309 || binoptab == vec_pack_ssat_optab
1310 || binoptab == vec_pack_ufix_trunc_optab
1311 || binoptab == vec_pack_sfix_trunc_optab)
1313 /* The mode of the result is different then the mode of the
1314 arguments. */
1315 tmp_mode = insn_data[(int) icode].operand[0].mode;
1316 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1318 delete_insns_since (last);
1319 return NULL_RTX;
1322 else
1323 tmp_mode = mode;
1325 create_output_operand (&ops[0], target, tmp_mode);
1326 create_input_operand (&ops[1], xop0, mode0);
1327 create_input_operand (&ops[2], xop1, mode1);
1328 pat = maybe_gen_insn (icode, 3, ops);
1329 if (pat)
1331 /* If PAT is composed of more than one insn, try to add an appropriate
1332 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1333 operand, call expand_binop again, this time without a target. */
1334 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1335 && ! add_equal_note (pat, ops[0].value, binoptab->code,
1336 ops[1].value, ops[2].value))
1338 delete_insns_since (last);
1339 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1340 unsignedp, methods);
1343 emit_insn (pat);
1344 return ops[0].value;
1346 delete_insns_since (last);
1347 return NULL_RTX;
1350 /* Generate code to perform an operation specified by BINOPTAB
1351 on operands OP0 and OP1, with result having machine-mode MODE.
1353 UNSIGNEDP is for the case where we have to widen the operands
1354 to perform the operation. It says to use zero-extension.
1356 If TARGET is nonzero, the value
1357 is generated there, if it is convenient to do so.
1358 In all cases an rtx is returned for the locus of the value;
1359 this may or may not be TARGET. */
1362 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
1363 rtx target, int unsignedp, enum optab_methods methods)
1365 enum optab_methods next_methods
1366 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1367 ? OPTAB_WIDEN : methods);
1368 enum mode_class mclass;
1369 enum machine_mode wider_mode;
1370 rtx libfunc;
1371 rtx temp;
1372 rtx entry_last = get_last_insn ();
1373 rtx last;
1375 mclass = GET_MODE_CLASS (mode);
1377 /* If subtracting an integer constant, convert this into an addition of
1378 the negated constant. */
1380 if (binoptab == sub_optab && CONST_INT_P (op1))
1382 op1 = negate_rtx (mode, op1);
1383 binoptab = add_optab;
1386 /* Record where to delete back to if we backtrack. */
1387 last = get_last_insn ();
1389 /* If we can do it with a three-operand insn, do so. */
1391 if (methods != OPTAB_MUST_WIDEN
1392 && optab_handler (binoptab, mode) != CODE_FOR_nothing)
1394 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1395 unsignedp, methods, last);
1396 if (temp)
1397 return temp;
1400 /* If we were trying to rotate, and that didn't work, try rotating
1401 the other direction before falling back to shifts and bitwise-or. */
1402 if (((binoptab == rotl_optab
1403 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1404 || (binoptab == rotr_optab
1405 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1406 && mclass == MODE_INT)
1408 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1409 rtx newop1;
1410 unsigned int bits = GET_MODE_BITSIZE (mode);
1412 if (CONST_INT_P (op1))
1413 newop1 = GEN_INT (bits - INTVAL (op1));
1414 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1415 newop1 = negate_rtx (GET_MODE (op1), op1);
1416 else
1417 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1418 GEN_INT (bits), op1,
1419 NULL_RTX, unsignedp, OPTAB_DIRECT);
1421 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1422 target, unsignedp, methods, last);
1423 if (temp)
1424 return temp;
1427 /* If this is a multiply, see if we can do a widening operation that
1428 takes operands of this mode and makes a wider mode. */
1430 if (binoptab == smul_optab
1431 && GET_MODE_WIDER_MODE (mode) != VOIDmode
1432 && (optab_handler ((unsignedp ? umul_widen_optab : smul_widen_optab),
1433 GET_MODE_WIDER_MODE (mode))
1434 != CODE_FOR_nothing))
1436 temp = expand_binop (GET_MODE_WIDER_MODE (mode),
1437 unsignedp ? umul_widen_optab : smul_widen_optab,
1438 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1440 if (temp != 0)
1442 if (GET_MODE_CLASS (mode) == MODE_INT
1443 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1444 GET_MODE_BITSIZE (GET_MODE (temp))))
1445 return gen_lowpart (mode, temp);
1446 else
1447 return convert_to_mode (mode, temp, unsignedp);
1451 /* Look for a wider mode of the same class for which we think we
1452 can open-code the operation. Check for a widening multiply at the
1453 wider mode as well. */
1455 if (CLASS_HAS_WIDER_MODES_P (mclass)
1456 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1457 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1458 wider_mode != VOIDmode;
1459 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1461 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1462 || (binoptab == smul_optab
1463 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1464 && (optab_handler ((unsignedp ? umul_widen_optab
1465 : smul_widen_optab),
1466 GET_MODE_WIDER_MODE (wider_mode))
1467 != CODE_FOR_nothing)))
1469 rtx xop0 = op0, xop1 = op1;
1470 int no_extend = 0;
1472 /* For certain integer operations, we need not actually extend
1473 the narrow operands, as long as we will truncate
1474 the results to the same narrowness. */
1476 if ((binoptab == ior_optab || binoptab == and_optab
1477 || binoptab == xor_optab
1478 || binoptab == add_optab || binoptab == sub_optab
1479 || binoptab == smul_optab || binoptab == ashl_optab)
1480 && mclass == MODE_INT)
1482 no_extend = 1;
1483 xop0 = avoid_expensive_constant (mode, binoptab,
1484 xop0, unsignedp);
1485 if (binoptab != ashl_optab)
1486 xop1 = avoid_expensive_constant (mode, binoptab,
1487 xop1, unsignedp);
1490 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1492 /* The second operand of a shift must always be extended. */
1493 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1494 no_extend && binoptab != ashl_optab);
1496 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1497 unsignedp, OPTAB_DIRECT);
1498 if (temp)
1500 if (mclass != MODE_INT
1501 || !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1502 GET_MODE_BITSIZE (wider_mode)))
1504 if (target == 0)
1505 target = gen_reg_rtx (mode);
1506 convert_move (target, temp, 0);
1507 return target;
1509 else
1510 return gen_lowpart (mode, temp);
1512 else
1513 delete_insns_since (last);
1517 /* If operation is commutative,
1518 try to make the first operand a register.
1519 Even better, try to make it the same as the target.
1520 Also try to make the last operand a constant. */
1521 if (commutative_optab_p (binoptab)
1522 && swap_commutative_operands_with_target (target, op0, op1))
1524 temp = op1;
1525 op1 = op0;
1526 op0 = temp;
1529 /* These can be done a word at a time. */
1530 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1531 && mclass == MODE_INT
1532 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1533 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1535 int i;
1536 rtx insns;
1538 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1539 won't be accurate, so use a new target. */
1540 if (target == 0
1541 || target == op0
1542 || target == op1
1543 || !valid_multiword_target_p (target))
1544 target = gen_reg_rtx (mode);
1546 start_sequence ();
1548 /* Do the actual arithmetic. */
1549 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1551 rtx target_piece = operand_subword (target, i, 1, mode);
1552 rtx x = expand_binop (word_mode, binoptab,
1553 operand_subword_force (op0, i, mode),
1554 operand_subword_force (op1, i, mode),
1555 target_piece, unsignedp, next_methods);
1557 if (x == 0)
1558 break;
1560 if (target_piece != x)
1561 emit_move_insn (target_piece, x);
1564 insns = get_insns ();
1565 end_sequence ();
1567 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1569 emit_insn (insns);
1570 return target;
1574 /* Synthesize double word shifts from single word shifts. */
1575 if ((binoptab == lshr_optab || binoptab == ashl_optab
1576 || binoptab == ashr_optab)
1577 && mclass == MODE_INT
1578 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1579 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1580 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1581 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1582 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1584 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1585 enum machine_mode op1_mode;
1587 double_shift_mask = targetm.shift_truncation_mask (mode);
1588 shift_mask = targetm.shift_truncation_mask (word_mode);
1589 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1591 /* Apply the truncation to constant shifts. */
1592 if (double_shift_mask > 0 && CONST_INT_P (op1))
1593 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1595 if (op1 == CONST0_RTX (op1_mode))
1596 return op0;
1598 /* Make sure that this is a combination that expand_doubleword_shift
1599 can handle. See the comments there for details. */
1600 if (double_shift_mask == 0
1601 || (shift_mask == BITS_PER_WORD - 1
1602 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1604 rtx insns;
1605 rtx into_target, outof_target;
1606 rtx into_input, outof_input;
1607 int left_shift, outof_word;
1609 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1610 won't be accurate, so use a new target. */
1611 if (target == 0
1612 || target == op0
1613 || target == op1
1614 || !valid_multiword_target_p (target))
1615 target = gen_reg_rtx (mode);
1617 start_sequence ();
1619 /* OUTOF_* is the word we are shifting bits away from, and
1620 INTO_* is the word that we are shifting bits towards, thus
1621 they differ depending on the direction of the shift and
1622 WORDS_BIG_ENDIAN. */
1624 left_shift = binoptab == ashl_optab;
1625 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1627 outof_target = operand_subword (target, outof_word, 1, mode);
1628 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1630 outof_input = operand_subword_force (op0, outof_word, mode);
1631 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1633 if (expand_doubleword_shift (op1_mode, binoptab,
1634 outof_input, into_input, op1,
1635 outof_target, into_target,
1636 unsignedp, next_methods, shift_mask))
1638 insns = get_insns ();
1639 end_sequence ();
1641 emit_insn (insns);
1642 return target;
1644 end_sequence ();
1648 /* Synthesize double word rotates from single word shifts. */
1649 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1650 && mclass == MODE_INT
1651 && CONST_INT_P (op1)
1652 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1653 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1654 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1656 rtx insns;
1657 rtx into_target, outof_target;
1658 rtx into_input, outof_input;
1659 rtx inter;
1660 int shift_count, left_shift, outof_word;
1662 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1663 won't be accurate, so use a new target. Do this also if target is not
1664 a REG, first because having a register instead may open optimization
1665 opportunities, and second because if target and op0 happen to be MEMs
1666 designating the same location, we would risk clobbering it too early
1667 in the code sequence we generate below. */
1668 if (target == 0
1669 || target == op0
1670 || target == op1
1671 || !REG_P (target)
1672 || !valid_multiword_target_p (target))
1673 target = gen_reg_rtx (mode);
1675 start_sequence ();
1677 shift_count = INTVAL (op1);
1679 /* OUTOF_* is the word we are shifting bits away from, and
1680 INTO_* is the word that we are shifting bits towards, thus
1681 they differ depending on the direction of the shift and
1682 WORDS_BIG_ENDIAN. */
1684 left_shift = (binoptab == rotl_optab);
1685 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1687 outof_target = operand_subword (target, outof_word, 1, mode);
1688 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1690 outof_input = operand_subword_force (op0, outof_word, mode);
1691 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1693 if (shift_count == BITS_PER_WORD)
1695 /* This is just a word swap. */
1696 emit_move_insn (outof_target, into_input);
1697 emit_move_insn (into_target, outof_input);
1698 inter = const0_rtx;
1700 else
1702 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1703 rtx first_shift_count, second_shift_count;
1704 optab reverse_unsigned_shift, unsigned_shift;
1706 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1707 ? lshr_optab : ashl_optab);
1709 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1710 ? ashl_optab : lshr_optab);
1712 if (shift_count > BITS_PER_WORD)
1714 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1715 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1717 else
1719 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1720 second_shift_count = GEN_INT (shift_count);
1723 into_temp1 = expand_binop (word_mode, unsigned_shift,
1724 outof_input, first_shift_count,
1725 NULL_RTX, unsignedp, next_methods);
1726 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1727 into_input, second_shift_count,
1728 NULL_RTX, unsignedp, next_methods);
1730 if (into_temp1 != 0 && into_temp2 != 0)
1731 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1732 into_target, unsignedp, next_methods);
1733 else
1734 inter = 0;
1736 if (inter != 0 && inter != into_target)
1737 emit_move_insn (into_target, inter);
1739 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1740 into_input, first_shift_count,
1741 NULL_RTX, unsignedp, next_methods);
1742 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1743 outof_input, second_shift_count,
1744 NULL_RTX, unsignedp, next_methods);
1746 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1747 inter = expand_binop (word_mode, ior_optab,
1748 outof_temp1, outof_temp2,
1749 outof_target, unsignedp, next_methods);
1751 if (inter != 0 && inter != outof_target)
1752 emit_move_insn (outof_target, inter);
1755 insns = get_insns ();
1756 end_sequence ();
1758 if (inter != 0)
1760 emit_insn (insns);
1761 return target;
1765 /* These can be done a word at a time by propagating carries. */
1766 if ((binoptab == add_optab || binoptab == sub_optab)
1767 && mclass == MODE_INT
1768 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1769 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1771 unsigned int i;
1772 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1773 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1774 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1775 rtx xop0, xop1, xtarget;
1777 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1778 value is one of those, use it. Otherwise, use 1 since it is the
1779 one easiest to get. */
1780 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1781 int normalizep = STORE_FLAG_VALUE;
1782 #else
1783 int normalizep = 1;
1784 #endif
1786 /* Prepare the operands. */
1787 xop0 = force_reg (mode, op0);
1788 xop1 = force_reg (mode, op1);
1790 xtarget = gen_reg_rtx (mode);
1792 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1793 target = xtarget;
1795 /* Indicate for flow that the entire target reg is being set. */
1796 if (REG_P (target))
1797 emit_clobber (xtarget);
1799 /* Do the actual arithmetic. */
1800 for (i = 0; i < nwords; i++)
1802 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1803 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1804 rtx op0_piece = operand_subword_force (xop0, index, mode);
1805 rtx op1_piece = operand_subword_force (xop1, index, mode);
1806 rtx x;
1808 /* Main add/subtract of the input operands. */
1809 x = expand_binop (word_mode, binoptab,
1810 op0_piece, op1_piece,
1811 target_piece, unsignedp, next_methods);
1812 if (x == 0)
1813 break;
1815 if (i + 1 < nwords)
1817 /* Store carry from main add/subtract. */
1818 carry_out = gen_reg_rtx (word_mode);
1819 carry_out = emit_store_flag_force (carry_out,
1820 (binoptab == add_optab
1821 ? LT : GT),
1822 x, op0_piece,
1823 word_mode, 1, normalizep);
1826 if (i > 0)
1828 rtx newx;
1830 /* Add/subtract previous carry to main result. */
1831 newx = expand_binop (word_mode,
1832 normalizep == 1 ? binoptab : otheroptab,
1833 x, carry_in,
1834 NULL_RTX, 1, next_methods);
1836 if (i + 1 < nwords)
1838 /* Get out carry from adding/subtracting carry in. */
1839 rtx carry_tmp = gen_reg_rtx (word_mode);
1840 carry_tmp = emit_store_flag_force (carry_tmp,
1841 (binoptab == add_optab
1842 ? LT : GT),
1843 newx, x,
1844 word_mode, 1, normalizep);
1846 /* Logical-ior the two poss. carry together. */
1847 carry_out = expand_binop (word_mode, ior_optab,
1848 carry_out, carry_tmp,
1849 carry_out, 0, next_methods);
1850 if (carry_out == 0)
1851 break;
1853 emit_move_insn (target_piece, newx);
1855 else
1857 if (x != target_piece)
1858 emit_move_insn (target_piece, x);
1861 carry_in = carry_out;
1864 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
1866 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
1867 || ! rtx_equal_p (target, xtarget))
1869 rtx temp = emit_move_insn (target, xtarget);
1871 set_unique_reg_note (temp,
1872 REG_EQUAL,
1873 gen_rtx_fmt_ee (binoptab->code, mode,
1874 copy_rtx (xop0),
1875 copy_rtx (xop1)));
1877 else
1878 target = xtarget;
1880 return target;
1883 else
1884 delete_insns_since (last);
1887 /* Attempt to synthesize double word multiplies using a sequence of word
1888 mode multiplications. We first attempt to generate a sequence using a
1889 more efficient unsigned widening multiply, and if that fails we then
1890 try using a signed widening multiply. */
1892 if (binoptab == smul_optab
1893 && mclass == MODE_INT
1894 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1895 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
1896 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
1898 rtx product = NULL_RTX;
1900 if (optab_handler (umul_widen_optab, mode) != CODE_FOR_nothing)
1902 product = expand_doubleword_mult (mode, op0, op1, target,
1903 true, methods);
1904 if (!product)
1905 delete_insns_since (last);
1908 if (product == NULL_RTX
1909 && optab_handler (smul_widen_optab, mode) != CODE_FOR_nothing)
1911 product = expand_doubleword_mult (mode, op0, op1, target,
1912 false, methods);
1913 if (!product)
1914 delete_insns_since (last);
1917 if (product != NULL_RTX)
1919 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
1921 temp = emit_move_insn (target ? target : product, product);
1922 set_unique_reg_note (temp,
1923 REG_EQUAL,
1924 gen_rtx_fmt_ee (MULT, mode,
1925 copy_rtx (op0),
1926 copy_rtx (op1)));
1928 return product;
1932 /* It can't be open-coded in this mode.
1933 Use a library call if one is available and caller says that's ok. */
1935 libfunc = optab_libfunc (binoptab, mode);
1936 if (libfunc
1937 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
1939 rtx insns;
1940 rtx op1x = op1;
1941 enum machine_mode op1_mode = mode;
1942 rtx value;
1944 start_sequence ();
1946 if (shift_optab_p (binoptab))
1948 op1_mode = targetm.libgcc_shift_count_mode ();
1949 /* Specify unsigned here,
1950 since negative shift counts are meaningless. */
1951 op1x = convert_to_mode (op1_mode, op1, 1);
1954 if (GET_MODE (op0) != VOIDmode
1955 && GET_MODE (op0) != mode)
1956 op0 = convert_to_mode (mode, op0, unsignedp);
1958 /* Pass 1 for NO_QUEUE so we don't lose any increments
1959 if the libcall is cse'd or moved. */
1960 value = emit_library_call_value (libfunc,
1961 NULL_RTX, LCT_CONST, mode, 2,
1962 op0, mode, op1x, op1_mode);
1964 insns = get_insns ();
1965 end_sequence ();
1967 target = gen_reg_rtx (mode);
1968 emit_libcall_block (insns, target, value,
1969 gen_rtx_fmt_ee (binoptab->code, mode, op0, op1));
1971 return target;
1974 delete_insns_since (last);
1976 /* It can't be done in this mode. Can we do it in a wider mode? */
1978 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
1979 || methods == OPTAB_MUST_WIDEN))
1981 /* Caller says, don't even try. */
1982 delete_insns_since (entry_last);
1983 return 0;
1986 /* Compute the value of METHODS to pass to recursive calls.
1987 Don't allow widening to be tried recursively. */
1989 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
1991 /* Look for a wider mode of the same class for which it appears we can do
1992 the operation. */
1994 if (CLASS_HAS_WIDER_MODES_P (mclass))
1996 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1997 wider_mode != VOIDmode;
1998 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2000 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
2001 || (methods == OPTAB_LIB
2002 && optab_libfunc (binoptab, wider_mode)))
2004 rtx xop0 = op0, xop1 = op1;
2005 int no_extend = 0;
2007 /* For certain integer operations, we need not actually extend
2008 the narrow operands, as long as we will truncate
2009 the results to the same narrowness. */
2011 if ((binoptab == ior_optab || binoptab == and_optab
2012 || binoptab == xor_optab
2013 || binoptab == add_optab || binoptab == sub_optab
2014 || binoptab == smul_optab || binoptab == ashl_optab)
2015 && mclass == MODE_INT)
2016 no_extend = 1;
2018 xop0 = widen_operand (xop0, wider_mode, mode,
2019 unsignedp, no_extend);
2021 /* The second operand of a shift must always be extended. */
2022 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2023 no_extend && binoptab != ashl_optab);
2025 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2026 unsignedp, methods);
2027 if (temp)
2029 if (mclass != MODE_INT
2030 || !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
2031 GET_MODE_BITSIZE (wider_mode)))
2033 if (target == 0)
2034 target = gen_reg_rtx (mode);
2035 convert_move (target, temp, 0);
2036 return target;
2038 else
2039 return gen_lowpart (mode, temp);
2041 else
2042 delete_insns_since (last);
2047 delete_insns_since (entry_last);
2048 return 0;
2051 /* Expand a binary operator which has both signed and unsigned forms.
2052 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2053 signed operations.
2055 If we widen unsigned operands, we may use a signed wider operation instead
2056 of an unsigned wider operation, since the result would be the same. */
2059 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
2060 rtx op0, rtx op1, rtx target, int unsignedp,
2061 enum optab_methods methods)
2063 rtx temp;
2064 optab direct_optab = unsignedp ? uoptab : soptab;
2065 struct optab_d wide_soptab;
2067 /* Do it without widening, if possible. */
2068 temp = expand_binop (mode, direct_optab, op0, op1, target,
2069 unsignedp, OPTAB_DIRECT);
2070 if (temp || methods == OPTAB_DIRECT)
2071 return temp;
2073 /* Try widening to a signed int. Make a fake signed optab that
2074 hides any signed insn for direct use. */
2075 wide_soptab = *soptab;
2076 set_optab_handler (&wide_soptab, mode, CODE_FOR_nothing);
2077 /* We don't want to generate new hash table entries from this fake
2078 optab. */
2079 wide_soptab.libcall_gen = NULL;
2081 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2082 unsignedp, OPTAB_WIDEN);
2084 /* For unsigned operands, try widening to an unsigned int. */
2085 if (temp == 0 && unsignedp)
2086 temp = expand_binop (mode, uoptab, op0, op1, target,
2087 unsignedp, OPTAB_WIDEN);
2088 if (temp || methods == OPTAB_WIDEN)
2089 return temp;
2091 /* Use the right width libcall if that exists. */
2092 temp = expand_binop (mode, direct_optab, op0, op1, target, unsignedp, OPTAB_LIB);
2093 if (temp || methods == OPTAB_LIB)
2094 return temp;
2096 /* Must widen and use a libcall, use either signed or unsigned. */
2097 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2098 unsignedp, methods);
2099 if (temp != 0)
2100 return temp;
2101 if (unsignedp)
2102 return expand_binop (mode, uoptab, op0, op1, target,
2103 unsignedp, methods);
2104 return 0;
2107 /* Generate code to perform an operation specified by UNOPPTAB
2108 on operand OP0, with two results to TARG0 and TARG1.
2109 We assume that the order of the operands for the instruction
2110 is TARG0, TARG1, OP0.
2112 Either TARG0 or TARG1 may be zero, but what that means is that
2113 the result is not actually wanted. We will generate it into
2114 a dummy pseudo-reg and discard it. They may not both be zero.
2116 Returns 1 if this operation can be performed; 0 if not. */
2119 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2120 int unsignedp)
2122 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2123 enum mode_class mclass;
2124 enum machine_mode wider_mode;
2125 rtx entry_last = get_last_insn ();
2126 rtx last;
2128 mclass = GET_MODE_CLASS (mode);
2130 if (!targ0)
2131 targ0 = gen_reg_rtx (mode);
2132 if (!targ1)
2133 targ1 = gen_reg_rtx (mode);
2135 /* Record where to go back to if we fail. */
2136 last = get_last_insn ();
2138 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2140 struct expand_operand ops[3];
2141 enum insn_code icode = optab_handler (unoptab, mode);
2143 create_fixed_operand (&ops[0], targ0);
2144 create_fixed_operand (&ops[1], targ1);
2145 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2146 if (maybe_expand_insn (icode, 3, ops))
2147 return 1;
2150 /* It can't be done in this mode. Can we do it in a wider mode? */
2152 if (CLASS_HAS_WIDER_MODES_P (mclass))
2154 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2155 wider_mode != VOIDmode;
2156 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2158 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2160 rtx t0 = gen_reg_rtx (wider_mode);
2161 rtx t1 = gen_reg_rtx (wider_mode);
2162 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2164 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2166 convert_move (targ0, t0, unsignedp);
2167 convert_move (targ1, t1, unsignedp);
2168 return 1;
2170 else
2171 delete_insns_since (last);
2176 delete_insns_since (entry_last);
2177 return 0;
2180 /* Generate code to perform an operation specified by BINOPTAB
2181 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2182 We assume that the order of the operands for the instruction
2183 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2184 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2186 Either TARG0 or TARG1 may be zero, but what that means is that
2187 the result is not actually wanted. We will generate it into
2188 a dummy pseudo-reg and discard it. They may not both be zero.
2190 Returns 1 if this operation can be performed; 0 if not. */
2193 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2194 int unsignedp)
2196 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2197 enum mode_class mclass;
2198 enum machine_mode wider_mode;
2199 rtx entry_last = get_last_insn ();
2200 rtx last;
2202 mclass = GET_MODE_CLASS (mode);
2204 if (!targ0)
2205 targ0 = gen_reg_rtx (mode);
2206 if (!targ1)
2207 targ1 = gen_reg_rtx (mode);
2209 /* Record where to go back to if we fail. */
2210 last = get_last_insn ();
2212 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2214 struct expand_operand ops[4];
2215 enum insn_code icode = optab_handler (binoptab, mode);
2216 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2217 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
2218 rtx xop0 = op0, xop1 = op1;
2220 /* If we are optimizing, force expensive constants into a register. */
2221 xop0 = avoid_expensive_constant (mode0, binoptab, xop0, unsignedp);
2222 xop1 = avoid_expensive_constant (mode1, binoptab, xop1, unsignedp);
2224 create_fixed_operand (&ops[0], targ0);
2225 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2226 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2227 create_fixed_operand (&ops[3], targ1);
2228 if (maybe_expand_insn (icode, 4, ops))
2229 return 1;
2230 delete_insns_since (last);
2233 /* It can't be done in this mode. Can we do it in a wider mode? */
2235 if (CLASS_HAS_WIDER_MODES_P (mclass))
2237 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2238 wider_mode != VOIDmode;
2239 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2241 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2243 rtx t0 = gen_reg_rtx (wider_mode);
2244 rtx t1 = gen_reg_rtx (wider_mode);
2245 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2246 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2248 if (expand_twoval_binop (binoptab, cop0, cop1,
2249 t0, t1, unsignedp))
2251 convert_move (targ0, t0, unsignedp);
2252 convert_move (targ1, t1, unsignedp);
2253 return 1;
2255 else
2256 delete_insns_since (last);
2261 delete_insns_since (entry_last);
2262 return 0;
2265 /* Expand the two-valued library call indicated by BINOPTAB, but
2266 preserve only one of the values. If TARG0 is non-NULL, the first
2267 value is placed into TARG0; otherwise the second value is placed
2268 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2269 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2270 This routine assumes that the value returned by the library call is
2271 as if the return value was of an integral mode twice as wide as the
2272 mode of OP0. Returns 1 if the call was successful. */
2274 bool
2275 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2276 rtx targ0, rtx targ1, enum rtx_code code)
2278 enum machine_mode mode;
2279 enum machine_mode libval_mode;
2280 rtx libval;
2281 rtx insns;
2282 rtx libfunc;
2284 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2285 gcc_assert (!targ0 != !targ1);
2287 mode = GET_MODE (op0);
2288 libfunc = optab_libfunc (binoptab, mode);
2289 if (!libfunc)
2290 return false;
2292 /* The value returned by the library function will have twice as
2293 many bits as the nominal MODE. */
2294 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2295 MODE_INT);
2296 start_sequence ();
2297 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2298 libval_mode, 2,
2299 op0, mode,
2300 op1, mode);
2301 /* Get the part of VAL containing the value that we want. */
2302 libval = simplify_gen_subreg (mode, libval, libval_mode,
2303 targ0 ? 0 : GET_MODE_SIZE (mode));
2304 insns = get_insns ();
2305 end_sequence ();
2306 /* Move the into the desired location. */
2307 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2308 gen_rtx_fmt_ee (code, mode, op0, op1));
2310 return true;
2314 /* Wrapper around expand_unop which takes an rtx code to specify
2315 the operation to perform, not an optab pointer. All other
2316 arguments are the same. */
2318 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2319 rtx target, int unsignedp)
2321 optab unop = code_to_optab[(int) code];
2322 gcc_assert (unop);
2324 return expand_unop (mode, unop, op0, target, unsignedp);
2327 /* Try calculating
2328 (clz:narrow x)
2330 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)). */
2331 static rtx
2332 widen_clz (enum machine_mode mode, rtx op0, rtx target)
2334 enum mode_class mclass = GET_MODE_CLASS (mode);
2335 if (CLASS_HAS_WIDER_MODES_P (mclass))
2337 enum machine_mode wider_mode;
2338 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2339 wider_mode != VOIDmode;
2340 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2342 if (optab_handler (clz_optab, wider_mode) != CODE_FOR_nothing)
2344 rtx xop0, temp, last;
2346 last = get_last_insn ();
2348 if (target == 0)
2349 target = gen_reg_rtx (mode);
2350 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2351 temp = expand_unop (wider_mode, clz_optab, xop0, NULL_RTX, true);
2352 if (temp != 0)
2353 temp = expand_binop (wider_mode, sub_optab, temp,
2354 GEN_INT (GET_MODE_BITSIZE (wider_mode)
2355 - GET_MODE_BITSIZE (mode)),
2356 target, true, OPTAB_DIRECT);
2357 if (temp == 0)
2358 delete_insns_since (last);
2360 return temp;
2364 return 0;
2367 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2368 quantities, choosing which based on whether the high word is nonzero. */
2369 static rtx
2370 expand_doubleword_clz (enum machine_mode mode, rtx op0, rtx target)
2372 rtx xop0 = force_reg (mode, op0);
2373 rtx subhi = gen_highpart (word_mode, xop0);
2374 rtx sublo = gen_lowpart (word_mode, xop0);
2375 rtx hi0_label = gen_label_rtx ();
2376 rtx after_label = gen_label_rtx ();
2377 rtx seq, temp, result;
2379 /* If we were not given a target, use a word_mode register, not a
2380 'mode' register. The result will fit, and nobody is expecting
2381 anything bigger (the return type of __builtin_clz* is int). */
2382 if (!target)
2383 target = gen_reg_rtx (word_mode);
2385 /* In any case, write to a word_mode scratch in both branches of the
2386 conditional, so we can ensure there is a single move insn setting
2387 'target' to tag a REG_EQUAL note on. */
2388 result = gen_reg_rtx (word_mode);
2390 start_sequence ();
2392 /* If the high word is not equal to zero,
2393 then clz of the full value is clz of the high word. */
2394 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2395 word_mode, true, hi0_label);
2397 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2398 if (!temp)
2399 goto fail;
2401 if (temp != result)
2402 convert_move (result, temp, true);
2404 emit_jump_insn (gen_jump (after_label));
2405 emit_barrier ();
2407 /* Else clz of the full value is clz of the low word plus the number
2408 of bits in the high word. */
2409 emit_label (hi0_label);
2411 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2412 if (!temp)
2413 goto fail;
2414 temp = expand_binop (word_mode, add_optab, temp,
2415 GEN_INT (GET_MODE_BITSIZE (word_mode)),
2416 result, true, OPTAB_DIRECT);
2417 if (!temp)
2418 goto fail;
2419 if (temp != result)
2420 convert_move (result, temp, true);
2422 emit_label (after_label);
2423 convert_move (target, result, true);
2425 seq = get_insns ();
2426 end_sequence ();
2428 add_equal_note (seq, target, CLZ, xop0, 0);
2429 emit_insn (seq);
2430 return target;
2432 fail:
2433 end_sequence ();
2434 return 0;
2437 /* Try calculating
2438 (bswap:narrow x)
2440 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2441 static rtx
2442 widen_bswap (enum machine_mode mode, rtx op0, rtx target)
2444 enum mode_class mclass = GET_MODE_CLASS (mode);
2445 enum machine_mode wider_mode;
2446 rtx x, last;
2448 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2449 return NULL_RTX;
2451 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2452 wider_mode != VOIDmode;
2453 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2454 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2455 goto found;
2456 return NULL_RTX;
2458 found:
2459 last = get_last_insn ();
2461 x = widen_operand (op0, wider_mode, mode, true, true);
2462 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2464 if (x != 0)
2465 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2466 GET_MODE_BITSIZE (wider_mode)
2467 - GET_MODE_BITSIZE (mode),
2468 NULL_RTX, true);
2470 if (x != 0)
2472 if (target == 0)
2473 target = gen_reg_rtx (mode);
2474 emit_move_insn (target, gen_lowpart (mode, x));
2476 else
2477 delete_insns_since (last);
2479 return target;
2482 /* Try calculating bswap as two bswaps of two word-sized operands. */
2484 static rtx
2485 expand_doubleword_bswap (enum machine_mode mode, rtx op, rtx target)
2487 rtx t0, t1;
2489 t1 = expand_unop (word_mode, bswap_optab,
2490 operand_subword_force (op, 0, mode), NULL_RTX, true);
2491 t0 = expand_unop (word_mode, bswap_optab,
2492 operand_subword_force (op, 1, mode), NULL_RTX, true);
2494 if (target == 0 || !valid_multiword_target_p (target))
2495 target = gen_reg_rtx (mode);
2496 if (REG_P (target))
2497 emit_clobber (target);
2498 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2499 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2501 return target;
2504 /* Try calculating (parity x) as (and (popcount x) 1), where
2505 popcount can also be done in a wider mode. */
2506 static rtx
2507 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2509 enum mode_class mclass = GET_MODE_CLASS (mode);
2510 if (CLASS_HAS_WIDER_MODES_P (mclass))
2512 enum machine_mode wider_mode;
2513 for (wider_mode = mode; wider_mode != VOIDmode;
2514 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2516 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2518 rtx xop0, temp, last;
2520 last = get_last_insn ();
2522 if (target == 0)
2523 target = gen_reg_rtx (mode);
2524 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2525 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2526 true);
2527 if (temp != 0)
2528 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2529 target, true, OPTAB_DIRECT);
2530 if (temp == 0)
2531 delete_insns_since (last);
2533 return temp;
2537 return 0;
2540 /* Try calculating ctz(x) as K - clz(x & -x) ,
2541 where K is GET_MODE_BITSIZE(mode) - 1.
2543 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2544 don't have to worry about what the hardware does in that case. (If
2545 the clz instruction produces the usual value at 0, which is K, the
2546 result of this code sequence will be -1; expand_ffs, below, relies
2547 on this. It might be nice to have it be K instead, for consistency
2548 with the (very few) processors that provide a ctz with a defined
2549 value, but that would take one more instruction, and it would be
2550 less convenient for expand_ffs anyway. */
2552 static rtx
2553 expand_ctz (enum machine_mode mode, rtx op0, rtx target)
2555 rtx seq, temp;
2557 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2558 return 0;
2560 start_sequence ();
2562 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2563 if (temp)
2564 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2565 true, OPTAB_DIRECT);
2566 if (temp)
2567 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2568 if (temp)
2569 temp = expand_binop (mode, sub_optab, GEN_INT (GET_MODE_BITSIZE (mode) - 1),
2570 temp, target,
2571 true, OPTAB_DIRECT);
2572 if (temp == 0)
2574 end_sequence ();
2575 return 0;
2578 seq = get_insns ();
2579 end_sequence ();
2581 add_equal_note (seq, temp, CTZ, op0, 0);
2582 emit_insn (seq);
2583 return temp;
2587 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2588 else with the sequence used by expand_clz.
2590 The ffs builtin promises to return zero for a zero value and ctz/clz
2591 may have an undefined value in that case. If they do not give us a
2592 convenient value, we have to generate a test and branch. */
2593 static rtx
2594 expand_ffs (enum machine_mode mode, rtx op0, rtx target)
2596 HOST_WIDE_INT val = 0;
2597 bool defined_at_zero = false;
2598 rtx temp, seq;
2600 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2602 start_sequence ();
2604 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2605 if (!temp)
2606 goto fail;
2608 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2610 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2612 start_sequence ();
2613 temp = expand_ctz (mode, op0, 0);
2614 if (!temp)
2615 goto fail;
2617 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2619 defined_at_zero = true;
2620 val = (GET_MODE_BITSIZE (mode) - 1) - val;
2623 else
2624 return 0;
2626 if (defined_at_zero && val == -1)
2627 /* No correction needed at zero. */;
2628 else
2630 /* We don't try to do anything clever with the situation found
2631 on some processors (eg Alpha) where ctz(0:mode) ==
2632 bitsize(mode). If someone can think of a way to send N to -1
2633 and leave alone all values in the range 0..N-1 (where N is a
2634 power of two), cheaper than this test-and-branch, please add it.
2636 The test-and-branch is done after the operation itself, in case
2637 the operation sets condition codes that can be recycled for this.
2638 (This is true on i386, for instance.) */
2640 rtx nonzero_label = gen_label_rtx ();
2641 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2642 mode, true, nonzero_label);
2644 convert_move (temp, GEN_INT (-1), false);
2645 emit_label (nonzero_label);
2648 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2649 to produce a value in the range 0..bitsize. */
2650 temp = expand_binop (mode, add_optab, temp, GEN_INT (1),
2651 target, false, OPTAB_DIRECT);
2652 if (!temp)
2653 goto fail;
2655 seq = get_insns ();
2656 end_sequence ();
2658 add_equal_note (seq, temp, FFS, op0, 0);
2659 emit_insn (seq);
2660 return temp;
2662 fail:
2663 end_sequence ();
2664 return 0;
2667 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2668 conditions, VAL may already be a SUBREG against which we cannot generate
2669 a further SUBREG. In this case, we expect forcing the value into a
2670 register will work around the situation. */
2672 static rtx
2673 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2674 enum machine_mode imode)
2676 rtx ret;
2677 ret = lowpart_subreg (omode, val, imode);
2678 if (ret == NULL)
2680 val = force_reg (imode, val);
2681 ret = lowpart_subreg (omode, val, imode);
2682 gcc_assert (ret != NULL);
2684 return ret;
2687 /* Expand a floating point absolute value or negation operation via a
2688 logical operation on the sign bit. */
2690 static rtx
2691 expand_absneg_bit (enum rtx_code code, enum machine_mode mode,
2692 rtx op0, rtx target)
2694 const struct real_format *fmt;
2695 int bitpos, word, nwords, i;
2696 enum machine_mode imode;
2697 double_int mask;
2698 rtx temp, insns;
2700 /* The format has to have a simple sign bit. */
2701 fmt = REAL_MODE_FORMAT (mode);
2702 if (fmt == NULL)
2703 return NULL_RTX;
2705 bitpos = fmt->signbit_rw;
2706 if (bitpos < 0)
2707 return NULL_RTX;
2709 /* Don't create negative zeros if the format doesn't support them. */
2710 if (code == NEG && !fmt->has_signed_zero)
2711 return NULL_RTX;
2713 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2715 imode = int_mode_for_mode (mode);
2716 if (imode == BLKmode)
2717 return NULL_RTX;
2718 word = 0;
2719 nwords = 1;
2721 else
2723 imode = word_mode;
2725 if (FLOAT_WORDS_BIG_ENDIAN)
2726 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2727 else
2728 word = bitpos / BITS_PER_WORD;
2729 bitpos = bitpos % BITS_PER_WORD;
2730 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2733 mask = double_int_setbit (double_int_zero, bitpos);
2734 if (code == ABS)
2735 mask = double_int_not (mask);
2737 if (target == 0
2738 || target == op0
2739 || (nwords > 1 && !valid_multiword_target_p (target)))
2740 target = gen_reg_rtx (mode);
2742 if (nwords > 1)
2744 start_sequence ();
2746 for (i = 0; i < nwords; ++i)
2748 rtx targ_piece = operand_subword (target, i, 1, mode);
2749 rtx op0_piece = operand_subword_force (op0, i, mode);
2751 if (i == word)
2753 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2754 op0_piece,
2755 immed_double_int_const (mask, imode),
2756 targ_piece, 1, OPTAB_LIB_WIDEN);
2757 if (temp != targ_piece)
2758 emit_move_insn (targ_piece, temp);
2760 else
2761 emit_move_insn (targ_piece, op0_piece);
2764 insns = get_insns ();
2765 end_sequence ();
2767 emit_insn (insns);
2769 else
2771 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2772 gen_lowpart (imode, op0),
2773 immed_double_int_const (mask, imode),
2774 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2775 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2777 set_unique_reg_note (get_last_insn (), REG_EQUAL,
2778 gen_rtx_fmt_e (code, mode, copy_rtx (op0)));
2781 return target;
2784 /* As expand_unop, but will fail rather than attempt the operation in a
2785 different mode or with a libcall. */
2786 static rtx
2787 expand_unop_direct (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2788 int unsignedp)
2790 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2792 struct expand_operand ops[2];
2793 enum insn_code icode = optab_handler (unoptab, mode);
2794 rtx last = get_last_insn ();
2795 rtx pat;
2797 create_output_operand (&ops[0], target, mode);
2798 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2799 pat = maybe_gen_insn (icode, 2, ops);
2800 if (pat)
2802 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2803 && ! add_equal_note (pat, ops[0].value, unoptab->code,
2804 ops[1].value, NULL_RTX))
2806 delete_insns_since (last);
2807 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
2810 emit_insn (pat);
2812 return ops[0].value;
2815 return 0;
2818 /* Generate code to perform an operation specified by UNOPTAB
2819 on operand OP0, with result having machine-mode MODE.
2821 UNSIGNEDP is for the case where we have to widen the operands
2822 to perform the operation. It says to use zero-extension.
2824 If TARGET is nonzero, the value
2825 is generated there, if it is convenient to do so.
2826 In all cases an rtx is returned for the locus of the value;
2827 this may or may not be TARGET. */
2830 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2831 int unsignedp)
2833 enum mode_class mclass = GET_MODE_CLASS (mode);
2834 enum machine_mode wider_mode;
2835 rtx temp;
2836 rtx libfunc;
2838 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
2839 if (temp)
2840 return temp;
2842 /* It can't be done in this mode. Can we open-code it in a wider mode? */
2844 /* Widening (or narrowing) clz needs special treatment. */
2845 if (unoptab == clz_optab)
2847 temp = widen_clz (mode, op0, target);
2848 if (temp)
2849 return temp;
2851 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2852 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2854 temp = expand_doubleword_clz (mode, op0, target);
2855 if (temp)
2856 return temp;
2859 goto try_libcall;
2862 /* Widening (or narrowing) bswap needs special treatment. */
2863 if (unoptab == bswap_optab)
2865 temp = widen_bswap (mode, op0, target);
2866 if (temp)
2867 return temp;
2869 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2870 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2872 temp = expand_doubleword_bswap (mode, op0, target);
2873 if (temp)
2874 return temp;
2877 goto try_libcall;
2880 if (CLASS_HAS_WIDER_MODES_P (mclass))
2881 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2882 wider_mode != VOIDmode;
2883 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2885 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2887 rtx xop0 = op0;
2888 rtx last = get_last_insn ();
2890 /* For certain operations, we need not actually extend
2891 the narrow operand, as long as we will truncate the
2892 results to the same narrowness. */
2894 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2895 (unoptab == neg_optab
2896 || unoptab == one_cmpl_optab)
2897 && mclass == MODE_INT);
2899 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2900 unsignedp);
2902 if (temp)
2904 if (mclass != MODE_INT
2905 || !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
2906 GET_MODE_BITSIZE (wider_mode)))
2908 if (target == 0)
2909 target = gen_reg_rtx (mode);
2910 convert_move (target, temp, 0);
2911 return target;
2913 else
2914 return gen_lowpart (mode, temp);
2916 else
2917 delete_insns_since (last);
2921 /* These can be done a word at a time. */
2922 if (unoptab == one_cmpl_optab
2923 && mclass == MODE_INT
2924 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
2925 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2927 int i;
2928 rtx insns;
2930 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
2931 target = gen_reg_rtx (mode);
2933 start_sequence ();
2935 /* Do the actual arithmetic. */
2936 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
2938 rtx target_piece = operand_subword (target, i, 1, mode);
2939 rtx x = expand_unop (word_mode, unoptab,
2940 operand_subword_force (op0, i, mode),
2941 target_piece, unsignedp);
2943 if (target_piece != x)
2944 emit_move_insn (target_piece, x);
2947 insns = get_insns ();
2948 end_sequence ();
2950 emit_insn (insns);
2951 return target;
2954 if (unoptab->code == NEG)
2956 /* Try negating floating point values by flipping the sign bit. */
2957 if (SCALAR_FLOAT_MODE_P (mode))
2959 temp = expand_absneg_bit (NEG, mode, op0, target);
2960 if (temp)
2961 return temp;
2964 /* If there is no negation pattern, and we have no negative zero,
2965 try subtracting from zero. */
2966 if (!HONOR_SIGNED_ZEROS (mode))
2968 temp = expand_binop (mode, (unoptab == negv_optab
2969 ? subv_optab : sub_optab),
2970 CONST0_RTX (mode), op0, target,
2971 unsignedp, OPTAB_DIRECT);
2972 if (temp)
2973 return temp;
2977 /* Try calculating parity (x) as popcount (x) % 2. */
2978 if (unoptab == parity_optab)
2980 temp = expand_parity (mode, op0, target);
2981 if (temp)
2982 return temp;
2985 /* Try implementing ffs (x) in terms of clz (x). */
2986 if (unoptab == ffs_optab)
2988 temp = expand_ffs (mode, op0, target);
2989 if (temp)
2990 return temp;
2993 /* Try implementing ctz (x) in terms of clz (x). */
2994 if (unoptab == ctz_optab)
2996 temp = expand_ctz (mode, op0, target);
2997 if (temp)
2998 return temp;
3001 try_libcall:
3002 /* Now try a library call in this mode. */
3003 libfunc = optab_libfunc (unoptab, mode);
3004 if (libfunc)
3006 rtx insns;
3007 rtx value;
3008 rtx eq_value;
3009 enum machine_mode outmode = mode;
3011 /* All of these functions return small values. Thus we choose to
3012 have them return something that isn't a double-word. */
3013 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3014 || unoptab == popcount_optab || unoptab == parity_optab)
3015 outmode
3016 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3017 optab_libfunc (unoptab, mode)));
3019 start_sequence ();
3021 /* Pass 1 for NO_QUEUE so we don't lose any increments
3022 if the libcall is cse'd or moved. */
3023 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3024 1, op0, mode);
3025 insns = get_insns ();
3026 end_sequence ();
3028 target = gen_reg_rtx (outmode);
3029 eq_value = gen_rtx_fmt_e (unoptab->code, mode, op0);
3030 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3031 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3032 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3033 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3034 emit_libcall_block (insns, target, value, eq_value);
3036 return target;
3039 /* It can't be done in this mode. Can we do it in a wider mode? */
3041 if (CLASS_HAS_WIDER_MODES_P (mclass))
3043 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3044 wider_mode != VOIDmode;
3045 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3047 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3048 || optab_libfunc (unoptab, wider_mode))
3050 rtx xop0 = op0;
3051 rtx last = get_last_insn ();
3053 /* For certain operations, we need not actually extend
3054 the narrow operand, as long as we will truncate the
3055 results to the same narrowness. */
3057 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3058 (unoptab == neg_optab
3059 || unoptab == one_cmpl_optab)
3060 && mclass == MODE_INT);
3062 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3063 unsignedp);
3065 /* If we are generating clz using wider mode, adjust the
3066 result. */
3067 if (unoptab == clz_optab && temp != 0)
3068 temp = expand_binop (wider_mode, sub_optab, temp,
3069 GEN_INT (GET_MODE_BITSIZE (wider_mode)
3070 - GET_MODE_BITSIZE (mode)),
3071 target, true, OPTAB_DIRECT);
3073 if (temp)
3075 if (mclass != MODE_INT)
3077 if (target == 0)
3078 target = gen_reg_rtx (mode);
3079 convert_move (target, temp, 0);
3080 return target;
3082 else
3083 return gen_lowpart (mode, temp);
3085 else
3086 delete_insns_since (last);
3091 /* One final attempt at implementing negation via subtraction,
3092 this time allowing widening of the operand. */
3093 if (unoptab->code == NEG && !HONOR_SIGNED_ZEROS (mode))
3095 rtx temp;
3096 temp = expand_binop (mode,
3097 unoptab == negv_optab ? subv_optab : sub_optab,
3098 CONST0_RTX (mode), op0,
3099 target, unsignedp, OPTAB_LIB_WIDEN);
3100 if (temp)
3101 return temp;
3104 return 0;
3107 /* Emit code to compute the absolute value of OP0, with result to
3108 TARGET if convenient. (TARGET may be 0.) The return value says
3109 where the result actually is to be found.
3111 MODE is the mode of the operand; the mode of the result is
3112 different but can be deduced from MODE.
3117 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
3118 int result_unsignedp)
3120 rtx temp;
3122 if (! flag_trapv)
3123 result_unsignedp = 1;
3125 /* First try to do it with a special abs instruction. */
3126 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3127 op0, target, 0);
3128 if (temp != 0)
3129 return temp;
3131 /* For floating point modes, try clearing the sign bit. */
3132 if (SCALAR_FLOAT_MODE_P (mode))
3134 temp = expand_absneg_bit (ABS, mode, op0, target);
3135 if (temp)
3136 return temp;
3139 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3140 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3141 && !HONOR_SIGNED_ZEROS (mode))
3143 rtx last = get_last_insn ();
3145 temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
3146 if (temp != 0)
3147 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3148 OPTAB_WIDEN);
3150 if (temp != 0)
3151 return temp;
3153 delete_insns_since (last);
3156 /* If this machine has expensive jumps, we can do integer absolute
3157 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3158 where W is the width of MODE. */
3160 if (GET_MODE_CLASS (mode) == MODE_INT
3161 && BRANCH_COST (optimize_insn_for_speed_p (),
3162 false) >= 2)
3164 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3165 GET_MODE_BITSIZE (mode) - 1,
3166 NULL_RTX, 0);
3168 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3169 OPTAB_LIB_WIDEN);
3170 if (temp != 0)
3171 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3172 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3174 if (temp != 0)
3175 return temp;
3178 return NULL_RTX;
3182 expand_abs (enum machine_mode mode, rtx op0, rtx target,
3183 int result_unsignedp, int safe)
3185 rtx temp, op1;
3187 if (! flag_trapv)
3188 result_unsignedp = 1;
3190 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3191 if (temp != 0)
3192 return temp;
3194 /* If that does not win, use conditional jump and negate. */
3196 /* It is safe to use the target if it is the same
3197 as the source if this is also a pseudo register */
3198 if (op0 == target && REG_P (op0)
3199 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3200 safe = 1;
3202 op1 = gen_label_rtx ();
3203 if (target == 0 || ! safe
3204 || GET_MODE (target) != mode
3205 || (MEM_P (target) && MEM_VOLATILE_P (target))
3206 || (REG_P (target)
3207 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3208 target = gen_reg_rtx (mode);
3210 emit_move_insn (target, op0);
3211 NO_DEFER_POP;
3213 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3214 NULL_RTX, NULL_RTX, op1, -1);
3216 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3217 target, target, 0);
3218 if (op0 != target)
3219 emit_move_insn (target, op0);
3220 emit_label (op1);
3221 OK_DEFER_POP;
3222 return target;
3225 /* Emit code to compute the one's complement absolute value of OP0
3226 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3227 (TARGET may be NULL_RTX.) The return value says where the result
3228 actually is to be found.
3230 MODE is the mode of the operand; the mode of the result is
3231 different but can be deduced from MODE. */
3234 expand_one_cmpl_abs_nojump (enum machine_mode mode, rtx op0, rtx target)
3236 rtx temp;
3238 /* Not applicable for floating point modes. */
3239 if (FLOAT_MODE_P (mode))
3240 return NULL_RTX;
3242 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3243 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3245 rtx last = get_last_insn ();
3247 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3248 if (temp != 0)
3249 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3250 OPTAB_WIDEN);
3252 if (temp != 0)
3253 return temp;
3255 delete_insns_since (last);
3258 /* If this machine has expensive jumps, we can do one's complement
3259 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3261 if (GET_MODE_CLASS (mode) == MODE_INT
3262 && BRANCH_COST (optimize_insn_for_speed_p (),
3263 false) >= 2)
3265 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3266 GET_MODE_BITSIZE (mode) - 1,
3267 NULL_RTX, 0);
3269 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3270 OPTAB_LIB_WIDEN);
3272 if (temp != 0)
3273 return temp;
3276 return NULL_RTX;
3279 /* A subroutine of expand_copysign, perform the copysign operation using the
3280 abs and neg primitives advertised to exist on the target. The assumption
3281 is that we have a split register file, and leaving op0 in fp registers,
3282 and not playing with subregs so much, will help the register allocator. */
3284 static rtx
3285 expand_copysign_absneg (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3286 int bitpos, bool op0_is_abs)
3288 enum machine_mode imode;
3289 enum insn_code icode;
3290 rtx sign, label;
3292 if (target == op1)
3293 target = NULL_RTX;
3295 /* Check if the back end provides an insn that handles signbit for the
3296 argument's mode. */
3297 icode = optab_handler (signbit_optab, mode);
3298 if (icode != CODE_FOR_nothing)
3300 imode = insn_data[(int) icode].operand[0].mode;
3301 sign = gen_reg_rtx (imode);
3302 emit_unop_insn (icode, sign, op1, UNKNOWN);
3304 else
3306 double_int mask;
3308 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3310 imode = int_mode_for_mode (mode);
3311 if (imode == BLKmode)
3312 return NULL_RTX;
3313 op1 = gen_lowpart (imode, op1);
3315 else
3317 int word;
3319 imode = word_mode;
3320 if (FLOAT_WORDS_BIG_ENDIAN)
3321 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3322 else
3323 word = bitpos / BITS_PER_WORD;
3324 bitpos = bitpos % BITS_PER_WORD;
3325 op1 = operand_subword_force (op1, word, mode);
3328 mask = double_int_setbit (double_int_zero, bitpos);
3330 sign = expand_binop (imode, and_optab, op1,
3331 immed_double_int_const (mask, imode),
3332 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3335 if (!op0_is_abs)
3337 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3338 if (op0 == NULL)
3339 return NULL_RTX;
3340 target = op0;
3342 else
3344 if (target == NULL_RTX)
3345 target = copy_to_reg (op0);
3346 else
3347 emit_move_insn (target, op0);
3350 label = gen_label_rtx ();
3351 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3353 if (GET_CODE (op0) == CONST_DOUBLE)
3354 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3355 else
3356 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3357 if (op0 != target)
3358 emit_move_insn (target, op0);
3360 emit_label (label);
3362 return target;
3366 /* A subroutine of expand_copysign, perform the entire copysign operation
3367 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3368 is true if op0 is known to have its sign bit clear. */
3370 static rtx
3371 expand_copysign_bit (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3372 int bitpos, bool op0_is_abs)
3374 enum machine_mode imode;
3375 double_int mask;
3376 int word, nwords, i;
3377 rtx temp, insns;
3379 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3381 imode = int_mode_for_mode (mode);
3382 if (imode == BLKmode)
3383 return NULL_RTX;
3384 word = 0;
3385 nwords = 1;
3387 else
3389 imode = word_mode;
3391 if (FLOAT_WORDS_BIG_ENDIAN)
3392 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3393 else
3394 word = bitpos / BITS_PER_WORD;
3395 bitpos = bitpos % BITS_PER_WORD;
3396 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3399 mask = double_int_setbit (double_int_zero, bitpos);
3401 if (target == 0
3402 || target == op0
3403 || target == op1
3404 || (nwords > 1 && !valid_multiword_target_p (target)))
3405 target = gen_reg_rtx (mode);
3407 if (nwords > 1)
3409 start_sequence ();
3411 for (i = 0; i < nwords; ++i)
3413 rtx targ_piece = operand_subword (target, i, 1, mode);
3414 rtx op0_piece = operand_subword_force (op0, i, mode);
3416 if (i == word)
3418 if (!op0_is_abs)
3419 op0_piece
3420 = expand_binop (imode, and_optab, op0_piece,
3421 immed_double_int_const (double_int_not (mask),
3422 imode),
3423 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3425 op1 = expand_binop (imode, and_optab,
3426 operand_subword_force (op1, i, mode),
3427 immed_double_int_const (mask, imode),
3428 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3430 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3431 targ_piece, 1, OPTAB_LIB_WIDEN);
3432 if (temp != targ_piece)
3433 emit_move_insn (targ_piece, temp);
3435 else
3436 emit_move_insn (targ_piece, op0_piece);
3439 insns = get_insns ();
3440 end_sequence ();
3442 emit_insn (insns);
3444 else
3446 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3447 immed_double_int_const (mask, imode),
3448 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3450 op0 = gen_lowpart (imode, op0);
3451 if (!op0_is_abs)
3452 op0 = expand_binop (imode, and_optab, op0,
3453 immed_double_int_const (double_int_not (mask),
3454 imode),
3455 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3457 temp = expand_binop (imode, ior_optab, op0, op1,
3458 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3459 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3462 return target;
3465 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3466 scalar floating point mode. Return NULL if we do not know how to
3467 expand the operation inline. */
3470 expand_copysign (rtx op0, rtx op1, rtx target)
3472 enum machine_mode mode = GET_MODE (op0);
3473 const struct real_format *fmt;
3474 bool op0_is_abs;
3475 rtx temp;
3477 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3478 gcc_assert (GET_MODE (op1) == mode);
3480 /* First try to do it with a special instruction. */
3481 temp = expand_binop (mode, copysign_optab, op0, op1,
3482 target, 0, OPTAB_DIRECT);
3483 if (temp)
3484 return temp;
3486 fmt = REAL_MODE_FORMAT (mode);
3487 if (fmt == NULL || !fmt->has_signed_zero)
3488 return NULL_RTX;
3490 op0_is_abs = false;
3491 if (GET_CODE (op0) == CONST_DOUBLE)
3493 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3494 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3495 op0_is_abs = true;
3498 if (fmt->signbit_ro >= 0
3499 && (GET_CODE (op0) == CONST_DOUBLE
3500 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3501 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3503 temp = expand_copysign_absneg (mode, op0, op1, target,
3504 fmt->signbit_ro, op0_is_abs);
3505 if (temp)
3506 return temp;
3509 if (fmt->signbit_rw < 0)
3510 return NULL_RTX;
3511 return expand_copysign_bit (mode, op0, op1, target,
3512 fmt->signbit_rw, op0_is_abs);
3515 /* Generate an instruction whose insn-code is INSN_CODE,
3516 with two operands: an output TARGET and an input OP0.
3517 TARGET *must* be nonzero, and the output is always stored there.
3518 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3519 the value that is stored into TARGET.
3521 Return false if expansion failed. */
3523 bool
3524 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3525 enum rtx_code code)
3527 struct expand_operand ops[2];
3528 rtx pat;
3530 create_output_operand (&ops[0], target, GET_MODE (target));
3531 create_input_operand (&ops[1], op0, GET_MODE (op0));
3532 pat = maybe_gen_insn (icode, 2, ops);
3533 if (!pat)
3534 return false;
3536 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
3537 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3539 emit_insn (pat);
3541 if (ops[0].value != target)
3542 emit_move_insn (target, ops[0].value);
3543 return true;
3545 /* Generate an instruction whose insn-code is INSN_CODE,
3546 with two operands: an output TARGET and an input OP0.
3547 TARGET *must* be nonzero, and the output is always stored there.
3548 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3549 the value that is stored into TARGET. */
3551 void
3552 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3554 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3555 gcc_assert (ok);
3558 struct no_conflict_data
3560 rtx target, first, insn;
3561 bool must_stay;
3564 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3565 the currently examined clobber / store has to stay in the list of
3566 insns that constitute the actual libcall block. */
3567 static void
3568 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3570 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3572 /* If this inns directly contributes to setting the target, it must stay. */
3573 if (reg_overlap_mentioned_p (p->target, dest))
3574 p->must_stay = true;
3575 /* If we haven't committed to keeping any other insns in the list yet,
3576 there is nothing more to check. */
3577 else if (p->insn == p->first)
3578 return;
3579 /* If this insn sets / clobbers a register that feeds one of the insns
3580 already in the list, this insn has to stay too. */
3581 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3582 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3583 || reg_used_between_p (dest, p->first, p->insn)
3584 /* Likewise if this insn depends on a register set by a previous
3585 insn in the list, or if it sets a result (presumably a hard
3586 register) that is set or clobbered by a previous insn.
3587 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3588 SET_DEST perform the former check on the address, and the latter
3589 check on the MEM. */
3590 || (GET_CODE (set) == SET
3591 && (modified_in_p (SET_SRC (set), p->first)
3592 || modified_in_p (SET_DEST (set), p->first)
3593 || modified_between_p (SET_SRC (set), p->first, p->insn)
3594 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3595 p->must_stay = true;
3599 /* Emit code to make a call to a constant function or a library call.
3601 INSNS is a list containing all insns emitted in the call.
3602 These insns leave the result in RESULT. Our block is to copy RESULT
3603 to TARGET, which is logically equivalent to EQUIV.
3605 We first emit any insns that set a pseudo on the assumption that these are
3606 loading constants into registers; doing so allows them to be safely cse'ed
3607 between blocks. Then we emit all the other insns in the block, followed by
3608 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3609 note with an operand of EQUIV. */
3611 void
3612 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3614 rtx final_dest = target;
3615 rtx next, last, insn;
3617 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3618 into a MEM later. Protect the libcall block from this change. */
3619 if (! REG_P (target) || REG_USERVAR_P (target))
3620 target = gen_reg_rtx (GET_MODE (target));
3622 /* If we're using non-call exceptions, a libcall corresponding to an
3623 operation that may trap may also trap. */
3624 /* ??? See the comment in front of make_reg_eh_region_note. */
3625 if (cfun->can_throw_non_call_exceptions && may_trap_p (equiv))
3627 for (insn = insns; insn; insn = NEXT_INSN (insn))
3628 if (CALL_P (insn))
3630 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3631 if (note)
3633 int lp_nr = INTVAL (XEXP (note, 0));
3634 if (lp_nr == 0 || lp_nr == INT_MIN)
3635 remove_note (insn, note);
3639 else
3641 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3642 reg note to indicate that this call cannot throw or execute a nonlocal
3643 goto (unless there is already a REG_EH_REGION note, in which case
3644 we update it). */
3645 for (insn = insns; insn; insn = NEXT_INSN (insn))
3646 if (CALL_P (insn))
3647 make_reg_eh_region_note_nothrow_nononlocal (insn);
3650 /* First emit all insns that set pseudos. Remove them from the list as
3651 we go. Avoid insns that set pseudos which were referenced in previous
3652 insns. These can be generated by move_by_pieces, for example,
3653 to update an address. Similarly, avoid insns that reference things
3654 set in previous insns. */
3656 for (insn = insns; insn; insn = next)
3658 rtx set = single_set (insn);
3660 next = NEXT_INSN (insn);
3662 if (set != 0 && REG_P (SET_DEST (set))
3663 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3665 struct no_conflict_data data;
3667 data.target = const0_rtx;
3668 data.first = insns;
3669 data.insn = insn;
3670 data.must_stay = 0;
3671 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3672 if (! data.must_stay)
3674 if (PREV_INSN (insn))
3675 NEXT_INSN (PREV_INSN (insn)) = next;
3676 else
3677 insns = next;
3679 if (next)
3680 PREV_INSN (next) = PREV_INSN (insn);
3682 add_insn (insn);
3686 /* Some ports use a loop to copy large arguments onto the stack.
3687 Don't move anything outside such a loop. */
3688 if (LABEL_P (insn))
3689 break;
3692 /* Write the remaining insns followed by the final copy. */
3693 for (insn = insns; insn; insn = next)
3695 next = NEXT_INSN (insn);
3697 add_insn (insn);
3700 last = emit_move_insn (target, result);
3701 if (optab_handler (mov_optab, GET_MODE (target)) != CODE_FOR_nothing)
3702 set_unique_reg_note (last, REG_EQUAL, copy_rtx (equiv));
3704 if (final_dest != target)
3705 emit_move_insn (final_dest, target);
3708 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3709 PURPOSE describes how this comparison will be used. CODE is the rtx
3710 comparison code we will be using.
3712 ??? Actually, CODE is slightly weaker than that. A target is still
3713 required to implement all of the normal bcc operations, but not
3714 required to implement all (or any) of the unordered bcc operations. */
3717 can_compare_p (enum rtx_code code, enum machine_mode mode,
3718 enum can_compare_purpose purpose)
3720 rtx test;
3721 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3724 enum insn_code icode;
3726 if (purpose == ccp_jump
3727 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
3728 && insn_operand_matches (icode, 0, test))
3729 return 1;
3730 if (purpose == ccp_store_flag
3731 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
3732 && insn_operand_matches (icode, 1, test))
3733 return 1;
3734 if (purpose == ccp_cmov
3735 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
3736 return 1;
3738 mode = GET_MODE_WIDER_MODE (mode);
3739 PUT_MODE (test, mode);
3741 while (mode != VOIDmode);
3743 return 0;
3746 /* This function is called when we are going to emit a compare instruction that
3747 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
3749 *PMODE is the mode of the inputs (in case they are const_int).
3750 *PUNSIGNEDP nonzero says that the operands are unsigned;
3751 this matters if they need to be widened (as given by METHODS).
3753 If they have mode BLKmode, then SIZE specifies the size of both operands.
3755 This function performs all the setup necessary so that the caller only has
3756 to emit a single comparison insn. This setup can involve doing a BLKmode
3757 comparison or emitting a library call to perform the comparison if no insn
3758 is available to handle it.
3759 The values which are passed in through pointers can be modified; the caller
3760 should perform the comparison on the modified values. Constant
3761 comparisons must have already been folded. */
3763 static void
3764 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
3765 int unsignedp, enum optab_methods methods,
3766 rtx *ptest, enum machine_mode *pmode)
3768 enum machine_mode mode = *pmode;
3769 rtx libfunc, test;
3770 enum machine_mode cmp_mode;
3771 enum mode_class mclass;
3773 /* The other methods are not needed. */
3774 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
3775 || methods == OPTAB_LIB_WIDEN);
3777 /* If we are optimizing, force expensive constants into a register. */
3778 if (CONSTANT_P (x) && optimize
3779 && (rtx_cost (x, COMPARE, optimize_insn_for_speed_p ())
3780 > COSTS_N_INSNS (1)))
3781 x = force_reg (mode, x);
3783 if (CONSTANT_P (y) && optimize
3784 && (rtx_cost (y, COMPARE, optimize_insn_for_speed_p ())
3785 > COSTS_N_INSNS (1)))
3786 y = force_reg (mode, y);
3788 #ifdef HAVE_cc0
3789 /* Make sure if we have a canonical comparison. The RTL
3790 documentation states that canonical comparisons are required only
3791 for targets which have cc0. */
3792 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
3793 #endif
3795 /* Don't let both operands fail to indicate the mode. */
3796 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
3797 x = force_reg (mode, x);
3798 if (mode == VOIDmode)
3799 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
3801 /* Handle all BLKmode compares. */
3803 if (mode == BLKmode)
3805 enum machine_mode result_mode;
3806 enum insn_code cmp_code;
3807 tree length_type;
3808 rtx libfunc;
3809 rtx result;
3810 rtx opalign
3811 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
3813 gcc_assert (size);
3815 /* Try to use a memory block compare insn - either cmpstr
3816 or cmpmem will do. */
3817 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3818 cmp_mode != VOIDmode;
3819 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
3821 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
3822 if (cmp_code == CODE_FOR_nothing)
3823 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
3824 if (cmp_code == CODE_FOR_nothing)
3825 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
3826 if (cmp_code == CODE_FOR_nothing)
3827 continue;
3829 /* Must make sure the size fits the insn's mode. */
3830 if ((CONST_INT_P (size)
3831 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
3832 || (GET_MODE_BITSIZE (GET_MODE (size))
3833 > GET_MODE_BITSIZE (cmp_mode)))
3834 continue;
3836 result_mode = insn_data[cmp_code].operand[0].mode;
3837 result = gen_reg_rtx (result_mode);
3838 size = convert_to_mode (cmp_mode, size, 1);
3839 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
3841 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
3842 *pmode = result_mode;
3843 return;
3846 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
3847 goto fail;
3849 /* Otherwise call a library function, memcmp. */
3850 libfunc = memcmp_libfunc;
3851 length_type = sizetype;
3852 result_mode = TYPE_MODE (integer_type_node);
3853 cmp_mode = TYPE_MODE (length_type);
3854 size = convert_to_mode (TYPE_MODE (length_type), size,
3855 TYPE_UNSIGNED (length_type));
3857 result = emit_library_call_value (libfunc, 0, LCT_PURE,
3858 result_mode, 3,
3859 XEXP (x, 0), Pmode,
3860 XEXP (y, 0), Pmode,
3861 size, cmp_mode);
3863 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
3864 *pmode = result_mode;
3865 return;
3868 /* Don't allow operands to the compare to trap, as that can put the
3869 compare and branch in different basic blocks. */
3870 if (cfun->can_throw_non_call_exceptions)
3872 if (may_trap_p (x))
3873 x = force_reg (mode, x);
3874 if (may_trap_p (y))
3875 y = force_reg (mode, y);
3878 if (GET_MODE_CLASS (mode) == MODE_CC)
3880 gcc_assert (can_compare_p (comparison, CCmode, ccp_jump));
3881 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
3882 return;
3885 mclass = GET_MODE_CLASS (mode);
3886 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
3887 cmp_mode = mode;
3890 enum insn_code icode;
3891 icode = optab_handler (cbranch_optab, cmp_mode);
3892 if (icode != CODE_FOR_nothing
3893 && insn_operand_matches (icode, 0, test))
3895 rtx last = get_last_insn ();
3896 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
3897 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
3898 if (op0 && op1
3899 && insn_operand_matches (icode, 1, op0)
3900 && insn_operand_matches (icode, 2, op1))
3902 XEXP (test, 0) = op0;
3903 XEXP (test, 1) = op1;
3904 *ptest = test;
3905 *pmode = cmp_mode;
3906 return;
3908 delete_insns_since (last);
3911 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
3912 break;
3913 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
3915 while (cmp_mode != VOIDmode);
3917 if (methods != OPTAB_LIB_WIDEN)
3918 goto fail;
3920 if (!SCALAR_FLOAT_MODE_P (mode))
3922 rtx result;
3924 /* Handle a libcall just for the mode we are using. */
3925 libfunc = optab_libfunc (cmp_optab, mode);
3926 gcc_assert (libfunc);
3928 /* If we want unsigned, and this mode has a distinct unsigned
3929 comparison routine, use that. */
3930 if (unsignedp)
3932 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
3933 if (ulibfunc)
3934 libfunc = ulibfunc;
3937 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
3938 targetm.libgcc_cmp_return_mode (),
3939 2, x, mode, y, mode);
3941 /* There are two kinds of comparison routines. Biased routines
3942 return 0/1/2, and unbiased routines return -1/0/1. Other parts
3943 of gcc expect that the comparison operation is equivalent
3944 to the modified comparison. For signed comparisons compare the
3945 result against 1 in the biased case, and zero in the unbiased
3946 case. For unsigned comparisons always compare against 1 after
3947 biasing the unbiased result by adding 1. This gives us a way to
3948 represent LTU. */
3949 x = result;
3950 y = const1_rtx;
3952 if (!TARGET_LIB_INT_CMP_BIASED)
3954 if (unsignedp)
3955 x = plus_constant (result, 1);
3956 else
3957 y = const0_rtx;
3960 *pmode = word_mode;
3961 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
3962 ptest, pmode);
3964 else
3965 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
3967 return;
3969 fail:
3970 *ptest = NULL_RTX;
3973 /* Before emitting an insn with code ICODE, make sure that X, which is going
3974 to be used for operand OPNUM of the insn, is converted from mode MODE to
3975 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
3976 that it is accepted by the operand predicate. Return the new value. */
3979 prepare_operand (enum insn_code icode, rtx x, int opnum, enum machine_mode mode,
3980 enum machine_mode wider_mode, int unsignedp)
3982 if (mode != wider_mode)
3983 x = convert_modes (wider_mode, mode, x, unsignedp);
3985 if (!insn_operand_matches (icode, opnum, x))
3987 if (reload_completed)
3988 return NULL_RTX;
3989 x = copy_to_mode_reg (insn_data[(int) icode].operand[opnum].mode, x);
3992 return x;
3995 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
3996 we can do the branch. */
3998 static void
3999 emit_cmp_and_jump_insn_1 (rtx test, enum machine_mode mode, rtx label)
4001 enum machine_mode optab_mode;
4002 enum mode_class mclass;
4003 enum insn_code icode;
4005 mclass = GET_MODE_CLASS (mode);
4006 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4007 icode = optab_handler (cbranch_optab, optab_mode);
4009 gcc_assert (icode != CODE_FOR_nothing);
4010 gcc_assert (insn_operand_matches (icode, 0, test));
4011 emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0), XEXP (test, 1), label));
4014 /* Generate code to compare X with Y so that the condition codes are
4015 set and to jump to LABEL if the condition is true. If X is a
4016 constant and Y is not a constant, then the comparison is swapped to
4017 ensure that the comparison RTL has the canonical form.
4019 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4020 need to be widened. UNSIGNEDP is also used to select the proper
4021 branch condition code.
4023 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4025 MODE is the mode of the inputs (in case they are const_int).
4027 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4028 It will be potentially converted into an unsigned variant based on
4029 UNSIGNEDP to select a proper jump instruction. */
4031 void
4032 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4033 enum machine_mode mode, int unsignedp, rtx label)
4035 rtx op0 = x, op1 = y;
4036 rtx test;
4038 /* Swap operands and condition to ensure canonical RTL. */
4039 if (swap_commutative_operands_p (x, y)
4040 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4042 op0 = y, op1 = x;
4043 comparison = swap_condition (comparison);
4046 /* If OP0 is still a constant, then both X and Y must be constants
4047 or the opposite comparison is not supported. Force X into a register
4048 to create canonical RTL. */
4049 if (CONSTANT_P (op0))
4050 op0 = force_reg (mode, op0);
4052 if (unsignedp)
4053 comparison = unsigned_condition (comparison);
4055 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4056 &test, &mode);
4057 emit_cmp_and_jump_insn_1 (test, mode, label);
4061 /* Emit a library call comparison between floating point X and Y.
4062 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4064 static void
4065 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4066 rtx *ptest, enum machine_mode *pmode)
4068 enum rtx_code swapped = swap_condition (comparison);
4069 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4070 enum machine_mode orig_mode = GET_MODE (x);
4071 enum machine_mode mode, cmp_mode;
4072 rtx true_rtx, false_rtx;
4073 rtx value, target, insns, equiv;
4074 rtx libfunc = 0;
4075 bool reversed_p = false;
4076 cmp_mode = targetm.libgcc_cmp_return_mode ();
4078 for (mode = orig_mode;
4079 mode != VOIDmode;
4080 mode = GET_MODE_WIDER_MODE (mode))
4082 if (code_to_optab[comparison]
4083 && (libfunc = optab_libfunc (code_to_optab[comparison], mode)))
4084 break;
4086 if (code_to_optab[swapped]
4087 && (libfunc = optab_libfunc (code_to_optab[swapped], mode)))
4089 rtx tmp;
4090 tmp = x; x = y; y = tmp;
4091 comparison = swapped;
4092 break;
4095 if (code_to_optab[reversed]
4096 && (libfunc = optab_libfunc (code_to_optab[reversed], mode)))
4098 comparison = reversed;
4099 reversed_p = true;
4100 break;
4104 gcc_assert (mode != VOIDmode);
4106 if (mode != orig_mode)
4108 x = convert_to_mode (mode, x, 0);
4109 y = convert_to_mode (mode, y, 0);
4112 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4113 the RTL. The allows the RTL optimizers to delete the libcall if the
4114 condition can be determined at compile-time. */
4115 if (comparison == UNORDERED
4116 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4118 true_rtx = const_true_rtx;
4119 false_rtx = const0_rtx;
4121 else
4123 switch (comparison)
4125 case EQ:
4126 true_rtx = const0_rtx;
4127 false_rtx = const_true_rtx;
4128 break;
4130 case NE:
4131 true_rtx = const_true_rtx;
4132 false_rtx = const0_rtx;
4133 break;
4135 case GT:
4136 true_rtx = const1_rtx;
4137 false_rtx = const0_rtx;
4138 break;
4140 case GE:
4141 true_rtx = const0_rtx;
4142 false_rtx = constm1_rtx;
4143 break;
4145 case LT:
4146 true_rtx = constm1_rtx;
4147 false_rtx = const0_rtx;
4148 break;
4150 case LE:
4151 true_rtx = const0_rtx;
4152 false_rtx = const1_rtx;
4153 break;
4155 default:
4156 gcc_unreachable ();
4160 if (comparison == UNORDERED)
4162 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4163 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4164 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4165 temp, const_true_rtx, equiv);
4167 else
4169 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4170 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4171 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4172 equiv, true_rtx, false_rtx);
4175 start_sequence ();
4176 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4177 cmp_mode, 2, x, mode, y, mode);
4178 insns = get_insns ();
4179 end_sequence ();
4181 target = gen_reg_rtx (cmp_mode);
4182 emit_libcall_block (insns, target, value, equiv);
4184 if (comparison == UNORDERED
4185 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4186 || reversed_p)
4187 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4188 else
4189 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4191 *pmode = cmp_mode;
4194 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4196 void
4197 emit_indirect_jump (rtx loc)
4199 struct expand_operand ops[1];
4201 create_address_operand (&ops[0], loc);
4202 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4203 emit_barrier ();
4206 #ifdef HAVE_conditional_move
4208 /* Emit a conditional move instruction if the machine supports one for that
4209 condition and machine mode.
4211 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4212 the mode to use should they be constants. If it is VOIDmode, they cannot
4213 both be constants.
4215 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4216 should be stored there. MODE is the mode to use should they be constants.
4217 If it is VOIDmode, they cannot both be constants.
4219 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4220 is not supported. */
4223 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4224 enum machine_mode cmode, rtx op2, rtx op3,
4225 enum machine_mode mode, int unsignedp)
4227 rtx tem, comparison, last;
4228 enum insn_code icode;
4229 enum rtx_code reversed;
4231 /* If one operand is constant, make it the second one. Only do this
4232 if the other operand is not constant as well. */
4234 if (swap_commutative_operands_p (op0, op1))
4236 tem = op0;
4237 op0 = op1;
4238 op1 = tem;
4239 code = swap_condition (code);
4242 /* get_condition will prefer to generate LT and GT even if the old
4243 comparison was against zero, so undo that canonicalization here since
4244 comparisons against zero are cheaper. */
4245 if (code == LT && op1 == const1_rtx)
4246 code = LE, op1 = const0_rtx;
4247 else if (code == GT && op1 == constm1_rtx)
4248 code = GE, op1 = const0_rtx;
4250 if (cmode == VOIDmode)
4251 cmode = GET_MODE (op0);
4253 if (swap_commutative_operands_p (op2, op3)
4254 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4255 != UNKNOWN))
4257 tem = op2;
4258 op2 = op3;
4259 op3 = tem;
4260 code = reversed;
4263 if (mode == VOIDmode)
4264 mode = GET_MODE (op2);
4266 icode = direct_optab_handler (movcc_optab, mode);
4268 if (icode == CODE_FOR_nothing)
4269 return 0;
4271 if (!target)
4272 target = gen_reg_rtx (mode);
4274 code = unsignedp ? unsigned_condition (code) : code;
4275 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4277 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4278 return NULL and let the caller figure out how best to deal with this
4279 situation. */
4280 if (!COMPARISON_P (comparison))
4281 return NULL_RTX;
4283 do_pending_stack_adjust ();
4284 last = get_last_insn ();
4285 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4286 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4287 &comparison, &cmode);
4288 if (comparison)
4290 struct expand_operand ops[4];
4292 create_output_operand (&ops[0], target, mode);
4293 create_fixed_operand (&ops[1], comparison);
4294 create_input_operand (&ops[2], op2, mode);
4295 create_input_operand (&ops[3], op3, mode);
4296 if (maybe_expand_insn (icode, 4, ops))
4298 if (ops[0].value != target)
4299 convert_move (target, ops[0].value, false);
4300 return target;
4303 delete_insns_since (last);
4304 return NULL_RTX;
4307 /* Return nonzero if a conditional move of mode MODE is supported.
4309 This function is for combine so it can tell whether an insn that looks
4310 like a conditional move is actually supported by the hardware. If we
4311 guess wrong we lose a bit on optimization, but that's it. */
4312 /* ??? sparc64 supports conditionally moving integers values based on fp
4313 comparisons, and vice versa. How do we handle them? */
4316 can_conditionally_move_p (enum machine_mode mode)
4318 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4319 return 1;
4321 return 0;
4324 #endif /* HAVE_conditional_move */
4326 /* Emit a conditional addition instruction if the machine supports one for that
4327 condition and machine mode.
4329 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4330 the mode to use should they be constants. If it is VOIDmode, they cannot
4331 both be constants.
4333 OP2 should be stored in TARGET if the comparison is true, otherwise OP2+OP3
4334 should be stored there. MODE is the mode to use should they be constants.
4335 If it is VOIDmode, they cannot both be constants.
4337 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4338 is not supported. */
4341 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4342 enum machine_mode cmode, rtx op2, rtx op3,
4343 enum machine_mode mode, int unsignedp)
4345 rtx tem, comparison, last;
4346 enum insn_code icode;
4347 enum rtx_code reversed;
4349 /* If one operand is constant, make it the second one. Only do this
4350 if the other operand is not constant as well. */
4352 if (swap_commutative_operands_p (op0, op1))
4354 tem = op0;
4355 op0 = op1;
4356 op1 = tem;
4357 code = swap_condition (code);
4360 /* get_condition will prefer to generate LT and GT even if the old
4361 comparison was against zero, so undo that canonicalization here since
4362 comparisons against zero are cheaper. */
4363 if (code == LT && op1 == const1_rtx)
4364 code = LE, op1 = const0_rtx;
4365 else if (code == GT && op1 == constm1_rtx)
4366 code = GE, op1 = const0_rtx;
4368 if (cmode == VOIDmode)
4369 cmode = GET_MODE (op0);
4371 if (swap_commutative_operands_p (op2, op3)
4372 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4373 != UNKNOWN))
4375 tem = op2;
4376 op2 = op3;
4377 op3 = tem;
4378 code = reversed;
4381 if (mode == VOIDmode)
4382 mode = GET_MODE (op2);
4384 icode = optab_handler (addcc_optab, mode);
4386 if (icode == CODE_FOR_nothing)
4387 return 0;
4389 if (!target)
4390 target = gen_reg_rtx (mode);
4392 code = unsignedp ? unsigned_condition (code) : code;
4393 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4395 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4396 return NULL and let the caller figure out how best to deal with this
4397 situation. */
4398 if (!COMPARISON_P (comparison))
4399 return NULL_RTX;
4401 do_pending_stack_adjust ();
4402 last = get_last_insn ();
4403 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4404 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4405 &comparison, &cmode);
4406 if (comparison)
4408 struct expand_operand ops[4];
4410 create_output_operand (&ops[0], target, mode);
4411 create_fixed_operand (&ops[1], comparison);
4412 create_input_operand (&ops[2], op2, mode);
4413 create_input_operand (&ops[3], op3, mode);
4414 if (maybe_expand_insn (icode, 4, ops))
4416 if (ops[0].value != target)
4417 convert_move (target, ops[0].value, false);
4418 return target;
4421 delete_insns_since (last);
4422 return NULL_RTX;
4425 /* These functions attempt to generate an insn body, rather than
4426 emitting the insn, but if the gen function already emits them, we
4427 make no attempt to turn them back into naked patterns. */
4429 /* Generate and return an insn body to add Y to X. */
4432 gen_add2_insn (rtx x, rtx y)
4434 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4436 gcc_assert (insn_operand_matches (icode, 0, x));
4437 gcc_assert (insn_operand_matches (icode, 1, x));
4438 gcc_assert (insn_operand_matches (icode, 2, y));
4440 return GEN_FCN (icode) (x, x, y);
4443 /* Generate and return an insn body to add r1 and c,
4444 storing the result in r0. */
4447 gen_add3_insn (rtx r0, rtx r1, rtx c)
4449 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4451 if (icode == CODE_FOR_nothing
4452 || !insn_operand_matches (icode, 0, r0)
4453 || !insn_operand_matches (icode, 1, r1)
4454 || !insn_operand_matches (icode, 2, c))
4455 return NULL_RTX;
4457 return GEN_FCN (icode) (r0, r1, c);
4461 have_add2_insn (rtx x, rtx y)
4463 enum insn_code icode;
4465 gcc_assert (GET_MODE (x) != VOIDmode);
4467 icode = optab_handler (add_optab, GET_MODE (x));
4469 if (icode == CODE_FOR_nothing)
4470 return 0;
4472 if (!insn_operand_matches (icode, 0, x)
4473 || !insn_operand_matches (icode, 1, x)
4474 || !insn_operand_matches (icode, 2, y))
4475 return 0;
4477 return 1;
4480 /* Generate and return an insn body to subtract Y from X. */
4483 gen_sub2_insn (rtx x, rtx y)
4485 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4487 gcc_assert (insn_operand_matches (icode, 0, x));
4488 gcc_assert (insn_operand_matches (icode, 1, x));
4489 gcc_assert (insn_operand_matches (icode, 2, y));
4491 return GEN_FCN (icode) (x, x, y);
4494 /* Generate and return an insn body to subtract r1 and c,
4495 storing the result in r0. */
4498 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4500 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4502 if (icode == CODE_FOR_nothing
4503 || !insn_operand_matches (icode, 0, r0)
4504 || !insn_operand_matches (icode, 1, r1)
4505 || !insn_operand_matches (icode, 2, c))
4506 return NULL_RTX;
4508 return GEN_FCN (icode) (r0, r1, c);
4512 have_sub2_insn (rtx x, rtx y)
4514 enum insn_code icode;
4516 gcc_assert (GET_MODE (x) != VOIDmode);
4518 icode = optab_handler (sub_optab, GET_MODE (x));
4520 if (icode == CODE_FOR_nothing)
4521 return 0;
4523 if (!insn_operand_matches (icode, 0, x)
4524 || !insn_operand_matches (icode, 1, x)
4525 || !insn_operand_matches (icode, 2, y))
4526 return 0;
4528 return 1;
4531 /* Generate the body of an instruction to copy Y into X.
4532 It may be a list of insns, if one insn isn't enough. */
4535 gen_move_insn (rtx x, rtx y)
4537 rtx seq;
4539 start_sequence ();
4540 emit_move_insn_1 (x, y);
4541 seq = get_insns ();
4542 end_sequence ();
4543 return seq;
4546 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4547 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4548 no such operation exists, CODE_FOR_nothing will be returned. */
4550 enum insn_code
4551 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
4552 int unsignedp)
4554 convert_optab tab;
4555 #ifdef HAVE_ptr_extend
4556 if (unsignedp < 0)
4557 return CODE_FOR_ptr_extend;
4558 #endif
4560 tab = unsignedp ? zext_optab : sext_optab;
4561 return convert_optab_handler (tab, to_mode, from_mode);
4564 /* Generate the body of an insn to extend Y (with mode MFROM)
4565 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4568 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4569 enum machine_mode mfrom, int unsignedp)
4571 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4572 return GEN_FCN (icode) (x, y);
4575 /* can_fix_p and can_float_p say whether the target machine
4576 can directly convert a given fixed point type to
4577 a given floating point type, or vice versa.
4578 The returned value is the CODE_FOR_... value to use,
4579 or CODE_FOR_nothing if these modes cannot be directly converted.
4581 *TRUNCP_PTR is set to 1 if it is necessary to output
4582 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4584 static enum insn_code
4585 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4586 int unsignedp, int *truncp_ptr)
4588 convert_optab tab;
4589 enum insn_code icode;
4591 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4592 icode = convert_optab_handler (tab, fixmode, fltmode);
4593 if (icode != CODE_FOR_nothing)
4595 *truncp_ptr = 0;
4596 return icode;
4599 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4600 for this to work. We need to rework the fix* and ftrunc* patterns
4601 and documentation. */
4602 tab = unsignedp ? ufix_optab : sfix_optab;
4603 icode = convert_optab_handler (tab, fixmode, fltmode);
4604 if (icode != CODE_FOR_nothing
4605 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4607 *truncp_ptr = 1;
4608 return icode;
4611 *truncp_ptr = 0;
4612 return CODE_FOR_nothing;
4615 static enum insn_code
4616 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4617 int unsignedp)
4619 convert_optab tab;
4621 tab = unsignedp ? ufloat_optab : sfloat_optab;
4622 return convert_optab_handler (tab, fltmode, fixmode);
4625 /* Generate code to convert FROM to floating point
4626 and store in TO. FROM must be fixed point and not VOIDmode.
4627 UNSIGNEDP nonzero means regard FROM as unsigned.
4628 Normally this is done by correcting the final value
4629 if it is negative. */
4631 void
4632 expand_float (rtx to, rtx from, int unsignedp)
4634 enum insn_code icode;
4635 rtx target = to;
4636 enum machine_mode fmode, imode;
4637 bool can_do_signed = false;
4639 /* Crash now, because we won't be able to decide which mode to use. */
4640 gcc_assert (GET_MODE (from) != VOIDmode);
4642 /* Look for an insn to do the conversion. Do it in the specified
4643 modes if possible; otherwise convert either input, output or both to
4644 wider mode. If the integer mode is wider than the mode of FROM,
4645 we can do the conversion signed even if the input is unsigned. */
4647 for (fmode = GET_MODE (to); fmode != VOIDmode;
4648 fmode = GET_MODE_WIDER_MODE (fmode))
4649 for (imode = GET_MODE (from); imode != VOIDmode;
4650 imode = GET_MODE_WIDER_MODE (imode))
4652 int doing_unsigned = unsignedp;
4654 if (fmode != GET_MODE (to)
4655 && significand_size (fmode) < GET_MODE_BITSIZE (GET_MODE (from)))
4656 continue;
4658 icode = can_float_p (fmode, imode, unsignedp);
4659 if (icode == CODE_FOR_nothing && unsignedp)
4661 enum insn_code scode = can_float_p (fmode, imode, 0);
4662 if (scode != CODE_FOR_nothing)
4663 can_do_signed = true;
4664 if (imode != GET_MODE (from))
4665 icode = scode, doing_unsigned = 0;
4668 if (icode != CODE_FOR_nothing)
4670 if (imode != GET_MODE (from))
4671 from = convert_to_mode (imode, from, unsignedp);
4673 if (fmode != GET_MODE (to))
4674 target = gen_reg_rtx (fmode);
4676 emit_unop_insn (icode, target, from,
4677 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4679 if (target != to)
4680 convert_move (to, target, 0);
4681 return;
4685 /* Unsigned integer, and no way to convert directly. Convert as signed,
4686 then unconditionally adjust the result. */
4687 if (unsignedp && can_do_signed)
4689 rtx label = gen_label_rtx ();
4690 rtx temp;
4691 REAL_VALUE_TYPE offset;
4693 /* Look for a usable floating mode FMODE wider than the source and at
4694 least as wide as the target. Using FMODE will avoid rounding woes
4695 with unsigned values greater than the signed maximum value. */
4697 for (fmode = GET_MODE (to); fmode != VOIDmode;
4698 fmode = GET_MODE_WIDER_MODE (fmode))
4699 if (GET_MODE_BITSIZE (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
4700 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
4701 break;
4703 if (fmode == VOIDmode)
4705 /* There is no such mode. Pretend the target is wide enough. */
4706 fmode = GET_MODE (to);
4708 /* Avoid double-rounding when TO is narrower than FROM. */
4709 if ((significand_size (fmode) + 1)
4710 < GET_MODE_BITSIZE (GET_MODE (from)))
4712 rtx temp1;
4713 rtx neglabel = gen_label_rtx ();
4715 /* Don't use TARGET if it isn't a register, is a hard register,
4716 or is the wrong mode. */
4717 if (!REG_P (target)
4718 || REGNO (target) < FIRST_PSEUDO_REGISTER
4719 || GET_MODE (target) != fmode)
4720 target = gen_reg_rtx (fmode);
4722 imode = GET_MODE (from);
4723 do_pending_stack_adjust ();
4725 /* Test whether the sign bit is set. */
4726 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
4727 0, neglabel);
4729 /* The sign bit is not set. Convert as signed. */
4730 expand_float (target, from, 0);
4731 emit_jump_insn (gen_jump (label));
4732 emit_barrier ();
4734 /* The sign bit is set.
4735 Convert to a usable (positive signed) value by shifting right
4736 one bit, while remembering if a nonzero bit was shifted
4737 out; i.e., compute (from & 1) | (from >> 1). */
4739 emit_label (neglabel);
4740 temp = expand_binop (imode, and_optab, from, const1_rtx,
4741 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4742 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
4743 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
4744 OPTAB_LIB_WIDEN);
4745 expand_float (target, temp, 0);
4747 /* Multiply by 2 to undo the shift above. */
4748 temp = expand_binop (fmode, add_optab, target, target,
4749 target, 0, OPTAB_LIB_WIDEN);
4750 if (temp != target)
4751 emit_move_insn (target, temp);
4753 do_pending_stack_adjust ();
4754 emit_label (label);
4755 goto done;
4759 /* If we are about to do some arithmetic to correct for an
4760 unsigned operand, do it in a pseudo-register. */
4762 if (GET_MODE (to) != fmode
4763 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
4764 target = gen_reg_rtx (fmode);
4766 /* Convert as signed integer to floating. */
4767 expand_float (target, from, 0);
4769 /* If FROM is negative (and therefore TO is negative),
4770 correct its value by 2**bitwidth. */
4772 do_pending_stack_adjust ();
4773 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
4774 0, label);
4777 real_2expN (&offset, GET_MODE_BITSIZE (GET_MODE (from)), fmode);
4778 temp = expand_binop (fmode, add_optab, target,
4779 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
4780 target, 0, OPTAB_LIB_WIDEN);
4781 if (temp != target)
4782 emit_move_insn (target, temp);
4784 do_pending_stack_adjust ();
4785 emit_label (label);
4786 goto done;
4789 /* No hardware instruction available; call a library routine. */
4791 rtx libfunc;
4792 rtx insns;
4793 rtx value;
4794 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
4796 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
4797 from = convert_to_mode (SImode, from, unsignedp);
4799 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
4800 gcc_assert (libfunc);
4802 start_sequence ();
4804 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4805 GET_MODE (to), 1, from,
4806 GET_MODE (from));
4807 insns = get_insns ();
4808 end_sequence ();
4810 emit_libcall_block (insns, target, value,
4811 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
4812 GET_MODE (to), from));
4815 done:
4817 /* Copy result to requested destination
4818 if we have been computing in a temp location. */
4820 if (target != to)
4822 if (GET_MODE (target) == GET_MODE (to))
4823 emit_move_insn (to, target);
4824 else
4825 convert_move (to, target, 0);
4829 /* Generate code to convert FROM to fixed point and store in TO. FROM
4830 must be floating point. */
4832 void
4833 expand_fix (rtx to, rtx from, int unsignedp)
4835 enum insn_code icode;
4836 rtx target = to;
4837 enum machine_mode fmode, imode;
4838 int must_trunc = 0;
4840 /* We first try to find a pair of modes, one real and one integer, at
4841 least as wide as FROM and TO, respectively, in which we can open-code
4842 this conversion. If the integer mode is wider than the mode of TO,
4843 we can do the conversion either signed or unsigned. */
4845 for (fmode = GET_MODE (from); fmode != VOIDmode;
4846 fmode = GET_MODE_WIDER_MODE (fmode))
4847 for (imode = GET_MODE (to); imode != VOIDmode;
4848 imode = GET_MODE_WIDER_MODE (imode))
4850 int doing_unsigned = unsignedp;
4852 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
4853 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
4854 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
4856 if (icode != CODE_FOR_nothing)
4858 rtx last = get_last_insn ();
4859 if (fmode != GET_MODE (from))
4860 from = convert_to_mode (fmode, from, 0);
4862 if (must_trunc)
4864 rtx temp = gen_reg_rtx (GET_MODE (from));
4865 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
4866 temp, 0);
4869 if (imode != GET_MODE (to))
4870 target = gen_reg_rtx (imode);
4872 if (maybe_emit_unop_insn (icode, target, from,
4873 doing_unsigned ? UNSIGNED_FIX : FIX))
4875 if (target != to)
4876 convert_move (to, target, unsignedp);
4877 return;
4879 delete_insns_since (last);
4883 /* For an unsigned conversion, there is one more way to do it.
4884 If we have a signed conversion, we generate code that compares
4885 the real value to the largest representable positive number. If if
4886 is smaller, the conversion is done normally. Otherwise, subtract
4887 one plus the highest signed number, convert, and add it back.
4889 We only need to check all real modes, since we know we didn't find
4890 anything with a wider integer mode.
4892 This code used to extend FP value into mode wider than the destination.
4893 This is needed for decimal float modes which cannot accurately
4894 represent one plus the highest signed number of the same size, but
4895 not for binary modes. Consider, for instance conversion from SFmode
4896 into DImode.
4898 The hot path through the code is dealing with inputs smaller than 2^63
4899 and doing just the conversion, so there is no bits to lose.
4901 In the other path we know the value is positive in the range 2^63..2^64-1
4902 inclusive. (as for other input overflow happens and result is undefined)
4903 So we know that the most important bit set in mantissa corresponds to
4904 2^63. The subtraction of 2^63 should not generate any rounding as it
4905 simply clears out that bit. The rest is trivial. */
4907 if (unsignedp && GET_MODE_BITSIZE (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
4908 for (fmode = GET_MODE (from); fmode != VOIDmode;
4909 fmode = GET_MODE_WIDER_MODE (fmode))
4910 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
4911 && (!DECIMAL_FLOAT_MODE_P (fmode)
4912 || GET_MODE_BITSIZE (fmode) > GET_MODE_BITSIZE (GET_MODE (to))))
4914 int bitsize;
4915 REAL_VALUE_TYPE offset;
4916 rtx limit, lab1, lab2, insn;
4918 bitsize = GET_MODE_BITSIZE (GET_MODE (to));
4919 real_2expN (&offset, bitsize - 1, fmode);
4920 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
4921 lab1 = gen_label_rtx ();
4922 lab2 = gen_label_rtx ();
4924 if (fmode != GET_MODE (from))
4925 from = convert_to_mode (fmode, from, 0);
4927 /* See if we need to do the subtraction. */
4928 do_pending_stack_adjust ();
4929 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
4930 0, lab1);
4932 /* If not, do the signed "fix" and branch around fixup code. */
4933 expand_fix (to, from, 0);
4934 emit_jump_insn (gen_jump (lab2));
4935 emit_barrier ();
4937 /* Otherwise, subtract 2**(N-1), convert to signed number,
4938 then add 2**(N-1). Do the addition using XOR since this
4939 will often generate better code. */
4940 emit_label (lab1);
4941 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
4942 NULL_RTX, 0, OPTAB_LIB_WIDEN);
4943 expand_fix (to, target, 0);
4944 target = expand_binop (GET_MODE (to), xor_optab, to,
4945 gen_int_mode
4946 ((HOST_WIDE_INT) 1 << (bitsize - 1),
4947 GET_MODE (to)),
4948 to, 1, OPTAB_LIB_WIDEN);
4950 if (target != to)
4951 emit_move_insn (to, target);
4953 emit_label (lab2);
4955 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
4957 /* Make a place for a REG_NOTE and add it. */
4958 insn = emit_move_insn (to, to);
4959 set_unique_reg_note (insn,
4960 REG_EQUAL,
4961 gen_rtx_fmt_e (UNSIGNED_FIX,
4962 GET_MODE (to),
4963 copy_rtx (from)));
4966 return;
4969 /* We can't do it with an insn, so use a library call. But first ensure
4970 that the mode of TO is at least as wide as SImode, since those are the
4971 only library calls we know about. */
4973 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
4975 target = gen_reg_rtx (SImode);
4977 expand_fix (target, from, unsignedp);
4979 else
4981 rtx insns;
4982 rtx value;
4983 rtx libfunc;
4985 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
4986 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
4987 gcc_assert (libfunc);
4989 start_sequence ();
4991 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4992 GET_MODE (to), 1, from,
4993 GET_MODE (from));
4994 insns = get_insns ();
4995 end_sequence ();
4997 emit_libcall_block (insns, target, value,
4998 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
4999 GET_MODE (to), from));
5002 if (target != to)
5004 if (GET_MODE (to) == GET_MODE (target))
5005 emit_move_insn (to, target);
5006 else
5007 convert_move (to, target, 0);
5011 /* Generate code to convert FROM or TO a fixed-point.
5012 If UINTP is true, either TO or FROM is an unsigned integer.
5013 If SATP is true, we need to saturate the result. */
5015 void
5016 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5018 enum machine_mode to_mode = GET_MODE (to);
5019 enum machine_mode from_mode = GET_MODE (from);
5020 convert_optab tab;
5021 enum rtx_code this_code;
5022 enum insn_code code;
5023 rtx insns, value;
5024 rtx libfunc;
5026 if (to_mode == from_mode)
5028 emit_move_insn (to, from);
5029 return;
5032 if (uintp)
5034 tab = satp ? satfractuns_optab : fractuns_optab;
5035 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5037 else
5039 tab = satp ? satfract_optab : fract_optab;
5040 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5042 code = convert_optab_handler (tab, to_mode, from_mode);
5043 if (code != CODE_FOR_nothing)
5045 emit_unop_insn (code, to, from, this_code);
5046 return;
5049 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5050 gcc_assert (libfunc);
5052 start_sequence ();
5053 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5054 1, from, from_mode);
5055 insns = get_insns ();
5056 end_sequence ();
5058 emit_libcall_block (insns, to, value,
5059 gen_rtx_fmt_e (tab->code, to_mode, from));
5062 /* Generate code to convert FROM to fixed point and store in TO. FROM
5063 must be floating point, TO must be signed. Use the conversion optab
5064 TAB to do the conversion. */
5066 bool
5067 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5069 enum insn_code icode;
5070 rtx target = to;
5071 enum machine_mode fmode, imode;
5073 /* We first try to find a pair of modes, one real and one integer, at
5074 least as wide as FROM and TO, respectively, in which we can open-code
5075 this conversion. If the integer mode is wider than the mode of TO,
5076 we can do the conversion either signed or unsigned. */
5078 for (fmode = GET_MODE (from); fmode != VOIDmode;
5079 fmode = GET_MODE_WIDER_MODE (fmode))
5080 for (imode = GET_MODE (to); imode != VOIDmode;
5081 imode = GET_MODE_WIDER_MODE (imode))
5083 icode = convert_optab_handler (tab, imode, fmode);
5084 if (icode != CODE_FOR_nothing)
5086 rtx last = get_last_insn ();
5087 if (fmode != GET_MODE (from))
5088 from = convert_to_mode (fmode, from, 0);
5090 if (imode != GET_MODE (to))
5091 target = gen_reg_rtx (imode);
5093 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5095 delete_insns_since (last);
5096 continue;
5098 if (target != to)
5099 convert_move (to, target, 0);
5100 return true;
5104 return false;
5107 /* Report whether we have an instruction to perform the operation
5108 specified by CODE on operands of mode MODE. */
5110 have_insn_for (enum rtx_code code, enum machine_mode mode)
5112 return (code_to_optab[(int) code] != 0
5113 && (optab_handler (code_to_optab[(int) code], mode)
5114 != CODE_FOR_nothing));
5117 /* Set all insn_code fields to CODE_FOR_nothing. */
5119 static void
5120 init_insn_codes (void)
5122 memset (optab_table, 0, sizeof (optab_table));
5123 memset (convert_optab_table, 0, sizeof (convert_optab_table));
5124 memset (direct_optab_table, 0, sizeof (direct_optab_table));
5127 /* Initialize OP's code to CODE, and write it into the code_to_optab table. */
5128 static inline void
5129 init_optab (optab op, enum rtx_code code)
5131 op->code = code;
5132 code_to_optab[(int) code] = op;
5135 /* Same, but fill in its code as CODE, and do _not_ write it into
5136 the code_to_optab table. */
5137 static inline void
5138 init_optabv (optab op, enum rtx_code code)
5140 op->code = code;
5143 /* Conversion optabs never go in the code_to_optab table. */
5144 static void
5145 init_convert_optab (convert_optab op, enum rtx_code code)
5147 op->code = code;
5150 /* Initialize the libfunc fields of an entire group of entries in some
5151 optab. Each entry is set equal to a string consisting of a leading
5152 pair of underscores followed by a generic operation name followed by
5153 a mode name (downshifted to lowercase) followed by a single character
5154 representing the number of operands for the given operation (which is
5155 usually one of the characters '2', '3', or '4').
5157 OPTABLE is the table in which libfunc fields are to be initialized.
5158 OPNAME is the generic (string) name of the operation.
5159 SUFFIX is the character which specifies the number of operands for
5160 the given generic operation.
5161 MODE is the mode to generate for.
5164 static void
5165 gen_libfunc (optab optable, const char *opname, int suffix, enum machine_mode mode)
5167 unsigned opname_len = strlen (opname);
5168 const char *mname = GET_MODE_NAME (mode);
5169 unsigned mname_len = strlen (mname);
5170 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5171 int len = prefix_len + opname_len + mname_len + 1 + 1;
5172 char *libfunc_name = XALLOCAVEC (char, len);
5173 char *p;
5174 const char *q;
5176 p = libfunc_name;
5177 *p++ = '_';
5178 *p++ = '_';
5179 if (targetm.libfunc_gnu_prefix)
5181 *p++ = 'g';
5182 *p++ = 'n';
5183 *p++ = 'u';
5184 *p++ = '_';
5186 for (q = opname; *q; )
5187 *p++ = *q++;
5188 for (q = mname; *q; q++)
5189 *p++ = TOLOWER (*q);
5190 *p++ = suffix;
5191 *p = '\0';
5193 set_optab_libfunc (optable, mode,
5194 ggc_alloc_string (libfunc_name, p - libfunc_name));
5197 /* Like gen_libfunc, but verify that integer operation is involved. */
5199 static void
5200 gen_int_libfunc (optab optable, const char *opname, char suffix,
5201 enum machine_mode mode)
5203 int maxsize = 2 * BITS_PER_WORD;
5205 if (GET_MODE_CLASS (mode) != MODE_INT)
5206 return;
5207 if (maxsize < LONG_LONG_TYPE_SIZE)
5208 maxsize = LONG_LONG_TYPE_SIZE;
5209 if (GET_MODE_CLASS (mode) != MODE_INT
5210 || mode < word_mode || GET_MODE_BITSIZE (mode) > maxsize)
5211 return;
5212 gen_libfunc (optable, opname, suffix, mode);
5215 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5217 static void
5218 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5219 enum machine_mode mode)
5221 char *dec_opname;
5223 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5224 gen_libfunc (optable, opname, suffix, mode);
5225 if (DECIMAL_FLOAT_MODE_P (mode))
5227 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5228 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5229 depending on the low level floating format used. */
5230 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5231 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5232 gen_libfunc (optable, dec_opname, suffix, mode);
5236 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5238 static void
5239 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5240 enum machine_mode mode)
5242 if (!ALL_FIXED_POINT_MODE_P (mode))
5243 return;
5244 gen_libfunc (optable, opname, suffix, mode);
5247 /* Like gen_libfunc, but verify that signed fixed-point operation is
5248 involved. */
5250 static void
5251 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5252 enum machine_mode mode)
5254 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5255 return;
5256 gen_libfunc (optable, opname, suffix, mode);
5259 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5260 involved. */
5262 static void
5263 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5264 enum machine_mode mode)
5266 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5267 return;
5268 gen_libfunc (optable, opname, suffix, mode);
5271 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5273 static void
5274 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5275 enum machine_mode mode)
5277 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5278 gen_fp_libfunc (optable, name, suffix, mode);
5279 if (INTEGRAL_MODE_P (mode))
5280 gen_int_libfunc (optable, name, suffix, mode);
5283 /* Like gen_libfunc, but verify that FP or INT operation is involved
5284 and add 'v' suffix for integer operation. */
5286 static void
5287 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5288 enum machine_mode mode)
5290 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5291 gen_fp_libfunc (optable, name, suffix, mode);
5292 if (GET_MODE_CLASS (mode) == MODE_INT)
5294 int len = strlen (name);
5295 char *v_name = XALLOCAVEC (char, len + 2);
5296 strcpy (v_name, name);
5297 v_name[len] = 'v';
5298 v_name[len + 1] = 0;
5299 gen_int_libfunc (optable, v_name, suffix, mode);
5303 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5304 involved. */
5306 static void
5307 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5308 enum machine_mode mode)
5310 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5311 gen_fp_libfunc (optable, name, suffix, mode);
5312 if (INTEGRAL_MODE_P (mode))
5313 gen_int_libfunc (optable, name, suffix, mode);
5314 if (ALL_FIXED_POINT_MODE_P (mode))
5315 gen_fixed_libfunc (optable, name, suffix, mode);
5318 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5319 involved. */
5321 static void
5322 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5323 enum machine_mode mode)
5325 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5326 gen_fp_libfunc (optable, name, suffix, mode);
5327 if (INTEGRAL_MODE_P (mode))
5328 gen_int_libfunc (optable, name, suffix, mode);
5329 if (SIGNED_FIXED_POINT_MODE_P (mode))
5330 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5333 /* Like gen_libfunc, but verify that INT or FIXED operation is
5334 involved. */
5336 static void
5337 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5338 enum machine_mode mode)
5340 if (INTEGRAL_MODE_P (mode))
5341 gen_int_libfunc (optable, name, suffix, mode);
5342 if (ALL_FIXED_POINT_MODE_P (mode))
5343 gen_fixed_libfunc (optable, name, suffix, mode);
5346 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5347 involved. */
5349 static void
5350 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5351 enum machine_mode mode)
5353 if (INTEGRAL_MODE_P (mode))
5354 gen_int_libfunc (optable, name, suffix, mode);
5355 if (SIGNED_FIXED_POINT_MODE_P (mode))
5356 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5359 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5360 involved. */
5362 static void
5363 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5364 enum machine_mode mode)
5366 if (INTEGRAL_MODE_P (mode))
5367 gen_int_libfunc (optable, name, suffix, mode);
5368 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5369 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5372 /* Initialize the libfunc fields of an entire group of entries of an
5373 inter-mode-class conversion optab. The string formation rules are
5374 similar to the ones for init_libfuncs, above, but instead of having
5375 a mode name and an operand count these functions have two mode names
5376 and no operand count. */
5378 static void
5379 gen_interclass_conv_libfunc (convert_optab tab,
5380 const char *opname,
5381 enum machine_mode tmode,
5382 enum machine_mode fmode)
5384 size_t opname_len = strlen (opname);
5385 size_t mname_len = 0;
5387 const char *fname, *tname;
5388 const char *q;
5389 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5390 char *libfunc_name, *suffix;
5391 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5392 char *p;
5394 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5395 depends on which underlying decimal floating point format is used. */
5396 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5398 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5400 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5401 nondec_name[0] = '_';
5402 nondec_name[1] = '_';
5403 if (targetm.libfunc_gnu_prefix)
5405 nondec_name[2] = 'g';
5406 nondec_name[3] = 'n';
5407 nondec_name[4] = 'u';
5408 nondec_name[5] = '_';
5411 memcpy (&nondec_name[prefix_len], opname, opname_len);
5412 nondec_suffix = nondec_name + opname_len + prefix_len;
5414 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5415 dec_name[0] = '_';
5416 dec_name[1] = '_';
5417 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5418 memcpy (&dec_name[2+dec_len], opname, opname_len);
5419 dec_suffix = dec_name + dec_len + opname_len + 2;
5421 fname = GET_MODE_NAME (fmode);
5422 tname = GET_MODE_NAME (tmode);
5424 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5426 libfunc_name = dec_name;
5427 suffix = dec_suffix;
5429 else
5431 libfunc_name = nondec_name;
5432 suffix = nondec_suffix;
5435 p = suffix;
5436 for (q = fname; *q; p++, q++)
5437 *p = TOLOWER (*q);
5438 for (q = tname; *q; p++, q++)
5439 *p = TOLOWER (*q);
5441 *p = '\0';
5443 set_conv_libfunc (tab, tmode, fmode,
5444 ggc_alloc_string (libfunc_name, p - libfunc_name));
5447 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5448 int->fp conversion. */
5450 static void
5451 gen_int_to_fp_conv_libfunc (convert_optab tab,
5452 const char *opname,
5453 enum machine_mode tmode,
5454 enum machine_mode fmode)
5456 if (GET_MODE_CLASS (fmode) != MODE_INT)
5457 return;
5458 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5459 return;
5460 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5463 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5464 naming scheme. */
5466 static void
5467 gen_ufloat_conv_libfunc (convert_optab tab,
5468 const char *opname ATTRIBUTE_UNUSED,
5469 enum machine_mode tmode,
5470 enum machine_mode fmode)
5472 if (DECIMAL_FLOAT_MODE_P (tmode))
5473 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5474 else
5475 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5478 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5479 fp->int conversion. */
5481 static void
5482 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5483 const char *opname,
5484 enum machine_mode tmode,
5485 enum machine_mode fmode)
5487 if (GET_MODE_CLASS (fmode) != MODE_INT)
5488 return;
5489 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5490 return;
5491 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5494 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5495 fp->int conversion with no decimal floating point involved. */
5497 static void
5498 gen_fp_to_int_conv_libfunc (convert_optab tab,
5499 const char *opname,
5500 enum machine_mode tmode,
5501 enum machine_mode fmode)
5503 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5504 return;
5505 if (GET_MODE_CLASS (tmode) != MODE_INT)
5506 return;
5507 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5510 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5511 The string formation rules are
5512 similar to the ones for init_libfunc, above. */
5514 static void
5515 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5516 enum machine_mode tmode, enum machine_mode fmode)
5518 size_t opname_len = strlen (opname);
5519 size_t mname_len = 0;
5521 const char *fname, *tname;
5522 const char *q;
5523 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5524 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5525 char *libfunc_name, *suffix;
5526 char *p;
5528 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5529 depends on which underlying decimal floating point format is used. */
5530 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5532 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5534 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5535 nondec_name[0] = '_';
5536 nondec_name[1] = '_';
5537 if (targetm.libfunc_gnu_prefix)
5539 nondec_name[2] = 'g';
5540 nondec_name[3] = 'n';
5541 nondec_name[4] = 'u';
5542 nondec_name[5] = '_';
5544 memcpy (&nondec_name[prefix_len], opname, opname_len);
5545 nondec_suffix = nondec_name + opname_len + prefix_len;
5547 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5548 dec_name[0] = '_';
5549 dec_name[1] = '_';
5550 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5551 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5552 dec_suffix = dec_name + dec_len + opname_len + 2;
5554 fname = GET_MODE_NAME (fmode);
5555 tname = GET_MODE_NAME (tmode);
5557 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5559 libfunc_name = dec_name;
5560 suffix = dec_suffix;
5562 else
5564 libfunc_name = nondec_name;
5565 suffix = nondec_suffix;
5568 p = suffix;
5569 for (q = fname; *q; p++, q++)
5570 *p = TOLOWER (*q);
5571 for (q = tname; *q; p++, q++)
5572 *p = TOLOWER (*q);
5574 *p++ = '2';
5575 *p = '\0';
5577 set_conv_libfunc (tab, tmode, fmode,
5578 ggc_alloc_string (libfunc_name, p - libfunc_name));
5581 /* Pick proper libcall for trunc_optab. We need to chose if we do
5582 truncation or extension and interclass or intraclass. */
5584 static void
5585 gen_trunc_conv_libfunc (convert_optab tab,
5586 const char *opname,
5587 enum machine_mode tmode,
5588 enum machine_mode fmode)
5590 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5591 return;
5592 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5593 return;
5594 if (tmode == fmode)
5595 return;
5597 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5598 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5599 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5601 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5602 return;
5604 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5605 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5606 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5607 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5610 /* Pick proper libcall for extend_optab. We need to chose if we do
5611 truncation or extension and interclass or intraclass. */
5613 static void
5614 gen_extend_conv_libfunc (convert_optab tab,
5615 const char *opname ATTRIBUTE_UNUSED,
5616 enum machine_mode tmode,
5617 enum machine_mode fmode)
5619 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5620 return;
5621 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5622 return;
5623 if (tmode == fmode)
5624 return;
5626 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5627 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5628 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5630 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5631 return;
5633 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5634 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5635 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5636 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5639 /* Pick proper libcall for fract_optab. We need to chose if we do
5640 interclass or intraclass. */
5642 static void
5643 gen_fract_conv_libfunc (convert_optab tab,
5644 const char *opname,
5645 enum machine_mode tmode,
5646 enum machine_mode fmode)
5648 if (tmode == fmode)
5649 return;
5650 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5651 return;
5653 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5654 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5655 else
5656 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5659 /* Pick proper libcall for fractuns_optab. */
5661 static void
5662 gen_fractuns_conv_libfunc (convert_optab tab,
5663 const char *opname,
5664 enum machine_mode tmode,
5665 enum machine_mode fmode)
5667 if (tmode == fmode)
5668 return;
5669 /* One mode must be a fixed-point mode, and the other must be an integer
5670 mode. */
5671 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
5672 || (ALL_FIXED_POINT_MODE_P (fmode)
5673 && GET_MODE_CLASS (tmode) == MODE_INT)))
5674 return;
5676 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5679 /* Pick proper libcall for satfract_optab. We need to chose if we do
5680 interclass or intraclass. */
5682 static void
5683 gen_satfract_conv_libfunc (convert_optab tab,
5684 const char *opname,
5685 enum machine_mode tmode,
5686 enum machine_mode fmode)
5688 if (tmode == fmode)
5689 return;
5690 /* TMODE must be a fixed-point mode. */
5691 if (!ALL_FIXED_POINT_MODE_P (tmode))
5692 return;
5694 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5695 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5696 else
5697 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5700 /* Pick proper libcall for satfractuns_optab. */
5702 static void
5703 gen_satfractuns_conv_libfunc (convert_optab tab,
5704 const char *opname,
5705 enum machine_mode tmode,
5706 enum machine_mode fmode)
5708 if (tmode == fmode)
5709 return;
5710 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
5711 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
5712 return;
5714 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5717 /* A table of previously-created libfuncs, hashed by name. */
5718 static GTY ((param_is (union tree_node))) htab_t libfunc_decls;
5720 /* Hashtable callbacks for libfunc_decls. */
5722 static hashval_t
5723 libfunc_decl_hash (const void *entry)
5725 return IDENTIFIER_HASH_VALUE (DECL_NAME ((const_tree) entry));
5728 static int
5729 libfunc_decl_eq (const void *entry1, const void *entry2)
5731 return DECL_NAME ((const_tree) entry1) == (const_tree) entry2;
5734 /* Build a decl for a libfunc named NAME. */
5736 tree
5737 build_libfunc_function (const char *name)
5739 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
5740 get_identifier (name),
5741 build_function_type (integer_type_node, NULL_TREE));
5742 /* ??? We don't have any type information except for this is
5743 a function. Pretend this is "int foo()". */
5744 DECL_ARTIFICIAL (decl) = 1;
5745 DECL_EXTERNAL (decl) = 1;
5746 TREE_PUBLIC (decl) = 1;
5747 gcc_assert (DECL_ASSEMBLER_NAME (decl));
5749 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
5750 are the flags assigned by targetm.encode_section_info. */
5751 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
5753 return decl;
5757 init_one_libfunc (const char *name)
5759 tree id, decl;
5760 void **slot;
5761 hashval_t hash;
5763 if (libfunc_decls == NULL)
5764 libfunc_decls = htab_create_ggc (37, libfunc_decl_hash,
5765 libfunc_decl_eq, NULL);
5767 /* See if we have already created a libfunc decl for this function. */
5768 id = get_identifier (name);
5769 hash = IDENTIFIER_HASH_VALUE (id);
5770 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, INSERT);
5771 decl = (tree) *slot;
5772 if (decl == NULL)
5774 /* Create a new decl, so that it can be passed to
5775 targetm.encode_section_info. */
5776 decl = build_libfunc_function (name);
5777 *slot = decl;
5779 return XEXP (DECL_RTL (decl), 0);
5782 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
5785 set_user_assembler_libfunc (const char *name, const char *asmspec)
5787 tree id, decl;
5788 void **slot;
5789 hashval_t hash;
5791 id = get_identifier (name);
5792 hash = IDENTIFIER_HASH_VALUE (id);
5793 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, NO_INSERT);
5794 gcc_assert (slot);
5795 decl = (tree) *slot;
5796 set_user_assembler_name (decl, asmspec);
5797 return XEXP (DECL_RTL (decl), 0);
5800 /* Call this to reset the function entry for one optab (OPTABLE) in mode
5801 MODE to NAME, which should be either 0 or a string constant. */
5802 void
5803 set_optab_libfunc (optab optable, enum machine_mode mode, const char *name)
5805 rtx val;
5806 struct libfunc_entry e;
5807 struct libfunc_entry **slot;
5808 e.optab = (size_t) (optable - &optab_table[0]);
5809 e.mode1 = mode;
5810 e.mode2 = VOIDmode;
5812 if (name)
5813 val = init_one_libfunc (name);
5814 else
5815 val = 0;
5816 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
5817 if (*slot == NULL)
5818 *slot = ggc_alloc_libfunc_entry ();
5819 (*slot)->optab = (size_t) (optable - &optab_table[0]);
5820 (*slot)->mode1 = mode;
5821 (*slot)->mode2 = VOIDmode;
5822 (*slot)->libfunc = val;
5825 /* Call this to reset the function entry for one conversion optab
5826 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
5827 either 0 or a string constant. */
5828 void
5829 set_conv_libfunc (convert_optab optable, enum machine_mode tmode,
5830 enum machine_mode fmode, const char *name)
5832 rtx val;
5833 struct libfunc_entry e;
5834 struct libfunc_entry **slot;
5835 e.optab = (size_t) (optable - &convert_optab_table[0]);
5836 e.mode1 = tmode;
5837 e.mode2 = fmode;
5839 if (name)
5840 val = init_one_libfunc (name);
5841 else
5842 val = 0;
5843 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
5844 if (*slot == NULL)
5845 *slot = ggc_alloc_libfunc_entry ();
5846 (*slot)->optab = (size_t) (optable - &convert_optab_table[0]);
5847 (*slot)->mode1 = tmode;
5848 (*slot)->mode2 = fmode;
5849 (*slot)->libfunc = val;
5852 /* Call this to initialize the contents of the optabs
5853 appropriately for the current target machine. */
5855 void
5856 init_optabs (void)
5858 if (libfunc_hash)
5860 htab_empty (libfunc_hash);
5861 /* We statically initialize the insn_codes with the equivalent of
5862 CODE_FOR_nothing. Repeat the process if reinitialising. */
5863 init_insn_codes ();
5865 else
5866 libfunc_hash = htab_create_ggc (10, hash_libfunc, eq_libfunc, NULL);
5868 init_optab (add_optab, PLUS);
5869 init_optabv (addv_optab, PLUS);
5870 init_optab (sub_optab, MINUS);
5871 init_optabv (subv_optab, MINUS);
5872 init_optab (ssadd_optab, SS_PLUS);
5873 init_optab (usadd_optab, US_PLUS);
5874 init_optab (sssub_optab, SS_MINUS);
5875 init_optab (ussub_optab, US_MINUS);
5876 init_optab (smul_optab, MULT);
5877 init_optab (ssmul_optab, SS_MULT);
5878 init_optab (usmul_optab, US_MULT);
5879 init_optabv (smulv_optab, MULT);
5880 init_optab (smul_highpart_optab, UNKNOWN);
5881 init_optab (umul_highpart_optab, UNKNOWN);
5882 init_optab (smul_widen_optab, UNKNOWN);
5883 init_optab (umul_widen_optab, UNKNOWN);
5884 init_optab (usmul_widen_optab, UNKNOWN);
5885 init_optab (smadd_widen_optab, UNKNOWN);
5886 init_optab (umadd_widen_optab, UNKNOWN);
5887 init_optab (ssmadd_widen_optab, UNKNOWN);
5888 init_optab (usmadd_widen_optab, UNKNOWN);
5889 init_optab (smsub_widen_optab, UNKNOWN);
5890 init_optab (umsub_widen_optab, UNKNOWN);
5891 init_optab (ssmsub_widen_optab, UNKNOWN);
5892 init_optab (usmsub_widen_optab, UNKNOWN);
5893 init_optab (sdiv_optab, DIV);
5894 init_optab (ssdiv_optab, SS_DIV);
5895 init_optab (usdiv_optab, US_DIV);
5896 init_optabv (sdivv_optab, DIV);
5897 init_optab (sdivmod_optab, UNKNOWN);
5898 init_optab (udiv_optab, UDIV);
5899 init_optab (udivmod_optab, UNKNOWN);
5900 init_optab (smod_optab, MOD);
5901 init_optab (umod_optab, UMOD);
5902 init_optab (fmod_optab, UNKNOWN);
5903 init_optab (remainder_optab, UNKNOWN);
5904 init_optab (ftrunc_optab, UNKNOWN);
5905 init_optab (and_optab, AND);
5906 init_optab (ior_optab, IOR);
5907 init_optab (xor_optab, XOR);
5908 init_optab (ashl_optab, ASHIFT);
5909 init_optab (ssashl_optab, SS_ASHIFT);
5910 init_optab (usashl_optab, US_ASHIFT);
5911 init_optab (ashr_optab, ASHIFTRT);
5912 init_optab (lshr_optab, LSHIFTRT);
5913 init_optab (rotl_optab, ROTATE);
5914 init_optab (rotr_optab, ROTATERT);
5915 init_optab (smin_optab, SMIN);
5916 init_optab (smax_optab, SMAX);
5917 init_optab (umin_optab, UMIN);
5918 init_optab (umax_optab, UMAX);
5919 init_optab (pow_optab, UNKNOWN);
5920 init_optab (atan2_optab, UNKNOWN);
5921 init_optab (fma_optab, FMA);
5922 init_optab (fms_optab, UNKNOWN);
5923 init_optab (fnma_optab, UNKNOWN);
5924 init_optab (fnms_optab, UNKNOWN);
5926 /* These three have codes assigned exclusively for the sake of
5927 have_insn_for. */
5928 init_optab (mov_optab, SET);
5929 init_optab (movstrict_optab, STRICT_LOW_PART);
5930 init_optab (cbranch_optab, COMPARE);
5932 init_optab (cmov_optab, UNKNOWN);
5933 init_optab (cstore_optab, UNKNOWN);
5934 init_optab (ctrap_optab, UNKNOWN);
5936 init_optab (storent_optab, UNKNOWN);
5938 init_optab (cmp_optab, UNKNOWN);
5939 init_optab (ucmp_optab, UNKNOWN);
5941 init_optab (eq_optab, EQ);
5942 init_optab (ne_optab, NE);
5943 init_optab (gt_optab, GT);
5944 init_optab (ge_optab, GE);
5945 init_optab (lt_optab, LT);
5946 init_optab (le_optab, LE);
5947 init_optab (unord_optab, UNORDERED);
5949 init_optab (neg_optab, NEG);
5950 init_optab (ssneg_optab, SS_NEG);
5951 init_optab (usneg_optab, US_NEG);
5952 init_optabv (negv_optab, NEG);
5953 init_optab (abs_optab, ABS);
5954 init_optabv (absv_optab, ABS);
5955 init_optab (addcc_optab, UNKNOWN);
5956 init_optab (one_cmpl_optab, NOT);
5957 init_optab (bswap_optab, BSWAP);
5958 init_optab (ffs_optab, FFS);
5959 init_optab (clz_optab, CLZ);
5960 init_optab (ctz_optab, CTZ);
5961 init_optab (popcount_optab, POPCOUNT);
5962 init_optab (parity_optab, PARITY);
5963 init_optab (sqrt_optab, SQRT);
5964 init_optab (floor_optab, UNKNOWN);
5965 init_optab (ceil_optab, UNKNOWN);
5966 init_optab (round_optab, UNKNOWN);
5967 init_optab (btrunc_optab, UNKNOWN);
5968 init_optab (nearbyint_optab, UNKNOWN);
5969 init_optab (rint_optab, UNKNOWN);
5970 init_optab (sincos_optab, UNKNOWN);
5971 init_optab (sin_optab, UNKNOWN);
5972 init_optab (asin_optab, UNKNOWN);
5973 init_optab (cos_optab, UNKNOWN);
5974 init_optab (acos_optab, UNKNOWN);
5975 init_optab (exp_optab, UNKNOWN);
5976 init_optab (exp10_optab, UNKNOWN);
5977 init_optab (exp2_optab, UNKNOWN);
5978 init_optab (expm1_optab, UNKNOWN);
5979 init_optab (ldexp_optab, UNKNOWN);
5980 init_optab (scalb_optab, UNKNOWN);
5981 init_optab (significand_optab, UNKNOWN);
5982 init_optab (logb_optab, UNKNOWN);
5983 init_optab (ilogb_optab, UNKNOWN);
5984 init_optab (log_optab, UNKNOWN);
5985 init_optab (log10_optab, UNKNOWN);
5986 init_optab (log2_optab, UNKNOWN);
5987 init_optab (log1p_optab, UNKNOWN);
5988 init_optab (tan_optab, UNKNOWN);
5989 init_optab (atan_optab, UNKNOWN);
5990 init_optab (copysign_optab, UNKNOWN);
5991 init_optab (signbit_optab, UNKNOWN);
5993 init_optab (isinf_optab, UNKNOWN);
5995 init_optab (strlen_optab, UNKNOWN);
5996 init_optab (push_optab, UNKNOWN);
5998 init_optab (reduc_smax_optab, UNKNOWN);
5999 init_optab (reduc_umax_optab, UNKNOWN);
6000 init_optab (reduc_smin_optab, UNKNOWN);
6001 init_optab (reduc_umin_optab, UNKNOWN);
6002 init_optab (reduc_splus_optab, UNKNOWN);
6003 init_optab (reduc_uplus_optab, UNKNOWN);
6005 init_optab (ssum_widen_optab, UNKNOWN);
6006 init_optab (usum_widen_optab, UNKNOWN);
6007 init_optab (sdot_prod_optab, UNKNOWN);
6008 init_optab (udot_prod_optab, UNKNOWN);
6010 init_optab (vec_extract_optab, UNKNOWN);
6011 init_optab (vec_extract_even_optab, UNKNOWN);
6012 init_optab (vec_extract_odd_optab, UNKNOWN);
6013 init_optab (vec_interleave_high_optab, UNKNOWN);
6014 init_optab (vec_interleave_low_optab, UNKNOWN);
6015 init_optab (vec_set_optab, UNKNOWN);
6016 init_optab (vec_init_optab, UNKNOWN);
6017 init_optab (vec_shl_optab, UNKNOWN);
6018 init_optab (vec_shr_optab, UNKNOWN);
6019 init_optab (vec_realign_load_optab, UNKNOWN);
6020 init_optab (movmisalign_optab, UNKNOWN);
6021 init_optab (vec_widen_umult_hi_optab, UNKNOWN);
6022 init_optab (vec_widen_umult_lo_optab, UNKNOWN);
6023 init_optab (vec_widen_smult_hi_optab, UNKNOWN);
6024 init_optab (vec_widen_smult_lo_optab, UNKNOWN);
6025 init_optab (vec_unpacks_hi_optab, UNKNOWN);
6026 init_optab (vec_unpacks_lo_optab, UNKNOWN);
6027 init_optab (vec_unpacku_hi_optab, UNKNOWN);
6028 init_optab (vec_unpacku_lo_optab, UNKNOWN);
6029 init_optab (vec_unpacks_float_hi_optab, UNKNOWN);
6030 init_optab (vec_unpacks_float_lo_optab, UNKNOWN);
6031 init_optab (vec_unpacku_float_hi_optab, UNKNOWN);
6032 init_optab (vec_unpacku_float_lo_optab, UNKNOWN);
6033 init_optab (vec_pack_trunc_optab, UNKNOWN);
6034 init_optab (vec_pack_usat_optab, UNKNOWN);
6035 init_optab (vec_pack_ssat_optab, UNKNOWN);
6036 init_optab (vec_pack_ufix_trunc_optab, UNKNOWN);
6037 init_optab (vec_pack_sfix_trunc_optab, UNKNOWN);
6039 init_optab (powi_optab, UNKNOWN);
6041 /* Conversions. */
6042 init_convert_optab (sext_optab, SIGN_EXTEND);
6043 init_convert_optab (zext_optab, ZERO_EXTEND);
6044 init_convert_optab (trunc_optab, TRUNCATE);
6045 init_convert_optab (sfix_optab, FIX);
6046 init_convert_optab (ufix_optab, UNSIGNED_FIX);
6047 init_convert_optab (sfixtrunc_optab, UNKNOWN);
6048 init_convert_optab (ufixtrunc_optab, UNKNOWN);
6049 init_convert_optab (sfloat_optab, FLOAT);
6050 init_convert_optab (ufloat_optab, UNSIGNED_FLOAT);
6051 init_convert_optab (lrint_optab, UNKNOWN);
6052 init_convert_optab (lround_optab, UNKNOWN);
6053 init_convert_optab (lfloor_optab, UNKNOWN);
6054 init_convert_optab (lceil_optab, UNKNOWN);
6056 init_convert_optab (fract_optab, FRACT_CONVERT);
6057 init_convert_optab (fractuns_optab, UNSIGNED_FRACT_CONVERT);
6058 init_convert_optab (satfract_optab, SAT_FRACT);
6059 init_convert_optab (satfractuns_optab, UNSIGNED_SAT_FRACT);
6061 /* Fill in the optabs with the insns we support. */
6062 init_all_optabs ();
6064 /* Initialize the optabs with the names of the library functions. */
6065 add_optab->libcall_basename = "add";
6066 add_optab->libcall_suffix = '3';
6067 add_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6068 addv_optab->libcall_basename = "add";
6069 addv_optab->libcall_suffix = '3';
6070 addv_optab->libcall_gen = gen_intv_fp_libfunc;
6071 ssadd_optab->libcall_basename = "ssadd";
6072 ssadd_optab->libcall_suffix = '3';
6073 ssadd_optab->libcall_gen = gen_signed_fixed_libfunc;
6074 usadd_optab->libcall_basename = "usadd";
6075 usadd_optab->libcall_suffix = '3';
6076 usadd_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6077 sub_optab->libcall_basename = "sub";
6078 sub_optab->libcall_suffix = '3';
6079 sub_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6080 subv_optab->libcall_basename = "sub";
6081 subv_optab->libcall_suffix = '3';
6082 subv_optab->libcall_gen = gen_intv_fp_libfunc;
6083 sssub_optab->libcall_basename = "sssub";
6084 sssub_optab->libcall_suffix = '3';
6085 sssub_optab->libcall_gen = gen_signed_fixed_libfunc;
6086 ussub_optab->libcall_basename = "ussub";
6087 ussub_optab->libcall_suffix = '3';
6088 ussub_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6089 smul_optab->libcall_basename = "mul";
6090 smul_optab->libcall_suffix = '3';
6091 smul_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6092 smulv_optab->libcall_basename = "mul";
6093 smulv_optab->libcall_suffix = '3';
6094 smulv_optab->libcall_gen = gen_intv_fp_libfunc;
6095 ssmul_optab->libcall_basename = "ssmul";
6096 ssmul_optab->libcall_suffix = '3';
6097 ssmul_optab->libcall_gen = gen_signed_fixed_libfunc;
6098 usmul_optab->libcall_basename = "usmul";
6099 usmul_optab->libcall_suffix = '3';
6100 usmul_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6101 sdiv_optab->libcall_basename = "div";
6102 sdiv_optab->libcall_suffix = '3';
6103 sdiv_optab->libcall_gen = gen_int_fp_signed_fixed_libfunc;
6104 sdivv_optab->libcall_basename = "divv";
6105 sdivv_optab->libcall_suffix = '3';
6106 sdivv_optab->libcall_gen = gen_int_libfunc;
6107 ssdiv_optab->libcall_basename = "ssdiv";
6108 ssdiv_optab->libcall_suffix = '3';
6109 ssdiv_optab->libcall_gen = gen_signed_fixed_libfunc;
6110 udiv_optab->libcall_basename = "udiv";
6111 udiv_optab->libcall_suffix = '3';
6112 udiv_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6113 usdiv_optab->libcall_basename = "usdiv";
6114 usdiv_optab->libcall_suffix = '3';
6115 usdiv_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6116 sdivmod_optab->libcall_basename = "divmod";
6117 sdivmod_optab->libcall_suffix = '4';
6118 sdivmod_optab->libcall_gen = gen_int_libfunc;
6119 udivmod_optab->libcall_basename = "udivmod";
6120 udivmod_optab->libcall_suffix = '4';
6121 udivmod_optab->libcall_gen = gen_int_libfunc;
6122 smod_optab->libcall_basename = "mod";
6123 smod_optab->libcall_suffix = '3';
6124 smod_optab->libcall_gen = gen_int_libfunc;
6125 umod_optab->libcall_basename = "umod";
6126 umod_optab->libcall_suffix = '3';
6127 umod_optab->libcall_gen = gen_int_libfunc;
6128 ftrunc_optab->libcall_basename = "ftrunc";
6129 ftrunc_optab->libcall_suffix = '2';
6130 ftrunc_optab->libcall_gen = gen_fp_libfunc;
6131 and_optab->libcall_basename = "and";
6132 and_optab->libcall_suffix = '3';
6133 and_optab->libcall_gen = gen_int_libfunc;
6134 ior_optab->libcall_basename = "ior";
6135 ior_optab->libcall_suffix = '3';
6136 ior_optab->libcall_gen = gen_int_libfunc;
6137 xor_optab->libcall_basename = "xor";
6138 xor_optab->libcall_suffix = '3';
6139 xor_optab->libcall_gen = gen_int_libfunc;
6140 ashl_optab->libcall_basename = "ashl";
6141 ashl_optab->libcall_suffix = '3';
6142 ashl_optab->libcall_gen = gen_int_fixed_libfunc;
6143 ssashl_optab->libcall_basename = "ssashl";
6144 ssashl_optab->libcall_suffix = '3';
6145 ssashl_optab->libcall_gen = gen_signed_fixed_libfunc;
6146 usashl_optab->libcall_basename = "usashl";
6147 usashl_optab->libcall_suffix = '3';
6148 usashl_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6149 ashr_optab->libcall_basename = "ashr";
6150 ashr_optab->libcall_suffix = '3';
6151 ashr_optab->libcall_gen = gen_int_signed_fixed_libfunc;
6152 lshr_optab->libcall_basename = "lshr";
6153 lshr_optab->libcall_suffix = '3';
6154 lshr_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6155 smin_optab->libcall_basename = "min";
6156 smin_optab->libcall_suffix = '3';
6157 smin_optab->libcall_gen = gen_int_fp_libfunc;
6158 smax_optab->libcall_basename = "max";
6159 smax_optab->libcall_suffix = '3';
6160 smax_optab->libcall_gen = gen_int_fp_libfunc;
6161 umin_optab->libcall_basename = "umin";
6162 umin_optab->libcall_suffix = '3';
6163 umin_optab->libcall_gen = gen_int_libfunc;
6164 umax_optab->libcall_basename = "umax";
6165 umax_optab->libcall_suffix = '3';
6166 umax_optab->libcall_gen = gen_int_libfunc;
6167 neg_optab->libcall_basename = "neg";
6168 neg_optab->libcall_suffix = '2';
6169 neg_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6170 ssneg_optab->libcall_basename = "ssneg";
6171 ssneg_optab->libcall_suffix = '2';
6172 ssneg_optab->libcall_gen = gen_signed_fixed_libfunc;
6173 usneg_optab->libcall_basename = "usneg";
6174 usneg_optab->libcall_suffix = '2';
6175 usneg_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6176 negv_optab->libcall_basename = "neg";
6177 negv_optab->libcall_suffix = '2';
6178 negv_optab->libcall_gen = gen_intv_fp_libfunc;
6179 one_cmpl_optab->libcall_basename = "one_cmpl";
6180 one_cmpl_optab->libcall_suffix = '2';
6181 one_cmpl_optab->libcall_gen = gen_int_libfunc;
6182 ffs_optab->libcall_basename = "ffs";
6183 ffs_optab->libcall_suffix = '2';
6184 ffs_optab->libcall_gen = gen_int_libfunc;
6185 clz_optab->libcall_basename = "clz";
6186 clz_optab->libcall_suffix = '2';
6187 clz_optab->libcall_gen = gen_int_libfunc;
6188 ctz_optab->libcall_basename = "ctz";
6189 ctz_optab->libcall_suffix = '2';
6190 ctz_optab->libcall_gen = gen_int_libfunc;
6191 popcount_optab->libcall_basename = "popcount";
6192 popcount_optab->libcall_suffix = '2';
6193 popcount_optab->libcall_gen = gen_int_libfunc;
6194 parity_optab->libcall_basename = "parity";
6195 parity_optab->libcall_suffix = '2';
6196 parity_optab->libcall_gen = gen_int_libfunc;
6198 /* Comparison libcalls for integers MUST come in pairs,
6199 signed/unsigned. */
6200 cmp_optab->libcall_basename = "cmp";
6201 cmp_optab->libcall_suffix = '2';
6202 cmp_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6203 ucmp_optab->libcall_basename = "ucmp";
6204 ucmp_optab->libcall_suffix = '2';
6205 ucmp_optab->libcall_gen = gen_int_libfunc;
6207 /* EQ etc are floating point only. */
6208 eq_optab->libcall_basename = "eq";
6209 eq_optab->libcall_suffix = '2';
6210 eq_optab->libcall_gen = gen_fp_libfunc;
6211 ne_optab->libcall_basename = "ne";
6212 ne_optab->libcall_suffix = '2';
6213 ne_optab->libcall_gen = gen_fp_libfunc;
6214 gt_optab->libcall_basename = "gt";
6215 gt_optab->libcall_suffix = '2';
6216 gt_optab->libcall_gen = gen_fp_libfunc;
6217 ge_optab->libcall_basename = "ge";
6218 ge_optab->libcall_suffix = '2';
6219 ge_optab->libcall_gen = gen_fp_libfunc;
6220 lt_optab->libcall_basename = "lt";
6221 lt_optab->libcall_suffix = '2';
6222 lt_optab->libcall_gen = gen_fp_libfunc;
6223 le_optab->libcall_basename = "le";
6224 le_optab->libcall_suffix = '2';
6225 le_optab->libcall_gen = gen_fp_libfunc;
6226 unord_optab->libcall_basename = "unord";
6227 unord_optab->libcall_suffix = '2';
6228 unord_optab->libcall_gen = gen_fp_libfunc;
6230 powi_optab->libcall_basename = "powi";
6231 powi_optab->libcall_suffix = '2';
6232 powi_optab->libcall_gen = gen_fp_libfunc;
6234 /* Conversions. */
6235 sfloat_optab->libcall_basename = "float";
6236 sfloat_optab->libcall_gen = gen_int_to_fp_conv_libfunc;
6237 ufloat_optab->libcall_gen = gen_ufloat_conv_libfunc;
6238 sfix_optab->libcall_basename = "fix";
6239 sfix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6240 ufix_optab->libcall_basename = "fixuns";
6241 ufix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6242 lrint_optab->libcall_basename = "lrint";
6243 lrint_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6244 lround_optab->libcall_basename = "lround";
6245 lround_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6246 lfloor_optab->libcall_basename = "lfloor";
6247 lfloor_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6248 lceil_optab->libcall_basename = "lceil";
6249 lceil_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6251 /* trunc_optab is also used for FLOAT_EXTEND. */
6252 sext_optab->libcall_basename = "extend";
6253 sext_optab->libcall_gen = gen_extend_conv_libfunc;
6254 trunc_optab->libcall_basename = "trunc";
6255 trunc_optab->libcall_gen = gen_trunc_conv_libfunc;
6257 /* Conversions for fixed-point modes and other modes. */
6258 fract_optab->libcall_basename = "fract";
6259 fract_optab->libcall_gen = gen_fract_conv_libfunc;
6260 satfract_optab->libcall_basename = "satfract";
6261 satfract_optab->libcall_gen = gen_satfract_conv_libfunc;
6262 fractuns_optab->libcall_basename = "fractuns";
6263 fractuns_optab->libcall_gen = gen_fractuns_conv_libfunc;
6264 satfractuns_optab->libcall_basename = "satfractuns";
6265 satfractuns_optab->libcall_gen = gen_satfractuns_conv_libfunc;
6267 /* The ffs function operates on `int'. Fall back on it if we do not
6268 have a libgcc2 function for that width. */
6269 if (INT_TYPE_SIZE < BITS_PER_WORD)
6270 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6271 "ffs");
6273 /* Explicitly initialize the bswap libfuncs since we need them to be
6274 valid for things other than word_mode. */
6275 if (targetm.libfunc_gnu_prefix)
6277 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6278 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6280 else
6282 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6283 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6286 /* Use cabs for double complex abs, since systems generally have cabs.
6287 Don't define any libcall for float complex, so that cabs will be used. */
6288 if (complex_double_type_node)
6289 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node), "cabs");
6291 abort_libfunc = init_one_libfunc ("abort");
6292 memcpy_libfunc = init_one_libfunc ("memcpy");
6293 memmove_libfunc = init_one_libfunc ("memmove");
6294 memcmp_libfunc = init_one_libfunc ("memcmp");
6295 memset_libfunc = init_one_libfunc ("memset");
6296 setbits_libfunc = init_one_libfunc ("__setbits");
6298 #ifndef DONT_USE_BUILTIN_SETJMP
6299 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6300 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6301 #else
6302 setjmp_libfunc = init_one_libfunc ("setjmp");
6303 longjmp_libfunc = init_one_libfunc ("longjmp");
6304 #endif
6305 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6306 unwind_sjlj_unregister_libfunc
6307 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6309 /* For function entry/exit instrumentation. */
6310 profile_function_entry_libfunc
6311 = init_one_libfunc ("__cyg_profile_func_enter");
6312 profile_function_exit_libfunc
6313 = init_one_libfunc ("__cyg_profile_func_exit");
6315 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6317 /* Allow the target to add more libcalls or rename some, etc. */
6318 targetm.init_libfuncs ();
6321 /* Print information about the current contents of the optabs on
6322 STDERR. */
6324 DEBUG_FUNCTION void
6325 debug_optab_libfuncs (void)
6327 int i;
6328 int j;
6329 int k;
6331 /* Dump the arithmetic optabs. */
6332 for (i = 0; i != (int) OTI_MAX; i++)
6333 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6335 optab o;
6336 rtx l;
6338 o = &optab_table[i];
6339 l = optab_libfunc (o, (enum machine_mode) j);
6340 if (l)
6342 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6343 fprintf (stderr, "%s\t%s:\t%s\n",
6344 GET_RTX_NAME (o->code),
6345 GET_MODE_NAME (j),
6346 XSTR (l, 0));
6350 /* Dump the conversion optabs. */
6351 for (i = 0; i < (int) COI_MAX; ++i)
6352 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6353 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6355 convert_optab o;
6356 rtx l;
6358 o = &convert_optab_table[i];
6359 l = convert_optab_libfunc (o, (enum machine_mode) j,
6360 (enum machine_mode) k);
6361 if (l)
6363 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6364 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6365 GET_RTX_NAME (o->code),
6366 GET_MODE_NAME (j),
6367 GET_MODE_NAME (k),
6368 XSTR (l, 0));
6374 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6375 CODE. Return 0 on failure. */
6378 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6380 enum machine_mode mode = GET_MODE (op1);
6381 enum insn_code icode;
6382 rtx insn;
6383 rtx trap_rtx;
6385 if (mode == VOIDmode)
6386 return 0;
6388 icode = optab_handler (ctrap_optab, mode);
6389 if (icode == CODE_FOR_nothing)
6390 return 0;
6392 /* Some targets only accept a zero trap code. */
6393 if (!insn_operand_matches (icode, 3, tcode))
6394 return 0;
6396 do_pending_stack_adjust ();
6397 start_sequence ();
6398 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6399 &trap_rtx, &mode);
6400 if (!trap_rtx)
6401 insn = NULL_RTX;
6402 else
6403 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6404 tcode);
6406 /* If that failed, then give up. */
6407 if (insn == 0)
6409 end_sequence ();
6410 return 0;
6413 emit_insn (insn);
6414 insn = get_insns ();
6415 end_sequence ();
6416 return insn;
6419 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6420 or unsigned operation code. */
6422 static enum rtx_code
6423 get_rtx_code (enum tree_code tcode, bool unsignedp)
6425 enum rtx_code code;
6426 switch (tcode)
6428 case EQ_EXPR:
6429 code = EQ;
6430 break;
6431 case NE_EXPR:
6432 code = NE;
6433 break;
6434 case LT_EXPR:
6435 code = unsignedp ? LTU : LT;
6436 break;
6437 case LE_EXPR:
6438 code = unsignedp ? LEU : LE;
6439 break;
6440 case GT_EXPR:
6441 code = unsignedp ? GTU : GT;
6442 break;
6443 case GE_EXPR:
6444 code = unsignedp ? GEU : GE;
6445 break;
6447 case UNORDERED_EXPR:
6448 code = UNORDERED;
6449 break;
6450 case ORDERED_EXPR:
6451 code = ORDERED;
6452 break;
6453 case UNLT_EXPR:
6454 code = UNLT;
6455 break;
6456 case UNLE_EXPR:
6457 code = UNLE;
6458 break;
6459 case UNGT_EXPR:
6460 code = UNGT;
6461 break;
6462 case UNGE_EXPR:
6463 code = UNGE;
6464 break;
6465 case UNEQ_EXPR:
6466 code = UNEQ;
6467 break;
6468 case LTGT_EXPR:
6469 code = LTGT;
6470 break;
6472 default:
6473 gcc_unreachable ();
6475 return code;
6478 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6479 unsigned operators. Do not generate compare instruction. */
6481 static rtx
6482 vector_compare_rtx (tree cond, bool unsignedp, enum insn_code icode)
6484 struct expand_operand ops[2];
6485 enum rtx_code rcode;
6486 tree t_op0, t_op1;
6487 rtx rtx_op0, rtx_op1;
6489 /* This is unlikely. While generating VEC_COND_EXPR, auto vectorizer
6490 ensures that condition is a relational operation. */
6491 gcc_assert (COMPARISON_CLASS_P (cond));
6493 rcode = get_rtx_code (TREE_CODE (cond), unsignedp);
6494 t_op0 = TREE_OPERAND (cond, 0);
6495 t_op1 = TREE_OPERAND (cond, 1);
6497 /* Expand operands. */
6498 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6499 EXPAND_STACK_PARM);
6500 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6501 EXPAND_STACK_PARM);
6503 create_input_operand (&ops[0], rtx_op0, GET_MODE (rtx_op0));
6504 create_input_operand (&ops[1], rtx_op1, GET_MODE (rtx_op1));
6505 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6506 gcc_unreachable ();
6507 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6510 /* Return insn code for TYPE, the type of a VEC_COND_EXPR. */
6512 static inline enum insn_code
6513 get_vcond_icode (tree type, enum machine_mode mode)
6515 enum insn_code icode = CODE_FOR_nothing;
6517 if (TYPE_UNSIGNED (type))
6518 icode = direct_optab_handler (vcondu_optab, mode);
6519 else
6520 icode = direct_optab_handler (vcond_optab, mode);
6521 return icode;
6524 /* Return TRUE iff, appropriate vector insns are available
6525 for vector cond expr with type TYPE in VMODE mode. */
6527 bool
6528 expand_vec_cond_expr_p (tree type, enum machine_mode vmode)
6530 if (get_vcond_icode (type, vmode) == CODE_FOR_nothing)
6531 return false;
6532 return true;
6535 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6536 three operands. */
6539 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6540 rtx target)
6542 struct expand_operand ops[6];
6543 enum insn_code icode;
6544 rtx comparison, rtx_op1, rtx_op2;
6545 enum machine_mode mode = TYPE_MODE (vec_cond_type);
6546 bool unsignedp = TYPE_UNSIGNED (vec_cond_type);
6548 icode = get_vcond_icode (vec_cond_type, mode);
6549 if (icode == CODE_FOR_nothing)
6550 return 0;
6552 comparison = vector_compare_rtx (op0, unsignedp, icode);
6553 rtx_op1 = expand_normal (op1);
6554 rtx_op2 = expand_normal (op2);
6556 create_output_operand (&ops[0], target, mode);
6557 create_input_operand (&ops[1], rtx_op1, mode);
6558 create_input_operand (&ops[2], rtx_op2, mode);
6559 create_fixed_operand (&ops[3], comparison);
6560 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6561 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6562 expand_insn (icode, 6, ops);
6563 return ops[0].value;
6567 /* This is an internal subroutine of the other compare_and_swap expanders.
6568 MEM, OLD_VAL and NEW_VAL are as you'd expect for a compare-and-swap
6569 operation. TARGET is an optional place to store the value result of
6570 the operation. ICODE is the particular instruction to expand. Return
6571 the result of the operation. */
6573 static rtx
6574 expand_val_compare_and_swap_1 (rtx mem, rtx old_val, rtx new_val,
6575 rtx target, enum insn_code icode)
6577 struct expand_operand ops[4];
6578 enum machine_mode mode = GET_MODE (mem);
6580 create_output_operand (&ops[0], target, mode);
6581 create_fixed_operand (&ops[1], mem);
6582 /* OLD_VAL and NEW_VAL may have been promoted to a wider mode.
6583 Shrink them if so. */
6584 create_convert_operand_to (&ops[2], old_val, mode, true);
6585 create_convert_operand_to (&ops[3], new_val, mode, true);
6586 if (maybe_expand_insn (icode, 4, ops))
6587 return ops[0].value;
6588 return NULL_RTX;
6591 /* Expand a compare-and-swap operation and return its value. */
6594 expand_val_compare_and_swap (rtx mem, rtx old_val, rtx new_val, rtx target)
6596 enum machine_mode mode = GET_MODE (mem);
6597 enum insn_code icode
6598 = direct_optab_handler (sync_compare_and_swap_optab, mode);
6600 if (icode == CODE_FOR_nothing)
6601 return NULL_RTX;
6603 return expand_val_compare_and_swap_1 (mem, old_val, new_val, target, icode);
6606 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
6607 pattern. */
6609 static void
6610 find_cc_set (rtx x, const_rtx pat, void *data)
6612 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
6613 && GET_CODE (pat) == SET)
6615 rtx *p_cc_reg = (rtx *) data;
6616 gcc_assert (!*p_cc_reg);
6617 *p_cc_reg = x;
6621 /* Expand a compare-and-swap operation and store true into the result if
6622 the operation was successful and false otherwise. Return the result.
6623 Unlike other routines, TARGET is not optional. */
6626 expand_bool_compare_and_swap (rtx mem, rtx old_val, rtx new_val, rtx target)
6628 enum machine_mode mode = GET_MODE (mem);
6629 enum insn_code icode;
6630 rtx subtarget, seq, cc_reg;
6632 /* If the target supports a compare-and-swap pattern that simultaneously
6633 sets some flag for success, then use it. Otherwise use the regular
6634 compare-and-swap and follow that immediately with a compare insn. */
6635 icode = direct_optab_handler (sync_compare_and_swap_optab, mode);
6636 if (icode == CODE_FOR_nothing)
6637 return NULL_RTX;
6639 do_pending_stack_adjust ();
6642 start_sequence ();
6643 subtarget = expand_val_compare_and_swap_1 (mem, old_val, new_val,
6644 NULL_RTX, icode);
6645 cc_reg = NULL_RTX;
6646 if (subtarget == NULL_RTX)
6648 end_sequence ();
6649 return NULL_RTX;
6652 if (have_insn_for (COMPARE, CCmode))
6653 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
6654 seq = get_insns ();
6655 end_sequence ();
6657 /* We might be comparing against an old value. Try again. :-( */
6658 if (!cc_reg && MEM_P (old_val))
6660 seq = NULL_RTX;
6661 old_val = force_reg (mode, old_val);
6664 while (!seq);
6666 emit_insn (seq);
6667 if (cc_reg)
6668 return emit_store_flag_force (target, EQ, cc_reg, const0_rtx, VOIDmode, 0, 1);
6669 else
6670 return emit_store_flag_force (target, EQ, subtarget, old_val, VOIDmode, 1, 1);
6673 /* This is a helper function for the other atomic operations. This function
6674 emits a loop that contains SEQ that iterates until a compare-and-swap
6675 operation at the end succeeds. MEM is the memory to be modified. SEQ is
6676 a set of instructions that takes a value from OLD_REG as an input and
6677 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
6678 set to the current contents of MEM. After SEQ, a compare-and-swap will
6679 attempt to update MEM with NEW_REG. The function returns true when the
6680 loop was generated successfully. */
6682 static bool
6683 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
6685 enum machine_mode mode = GET_MODE (mem);
6686 enum insn_code icode;
6687 rtx label, cmp_reg, subtarget, cc_reg;
6689 /* The loop we want to generate looks like
6691 cmp_reg = mem;
6692 label:
6693 old_reg = cmp_reg;
6694 seq;
6695 cmp_reg = compare-and-swap(mem, old_reg, new_reg)
6696 if (cmp_reg != old_reg)
6697 goto label;
6699 Note that we only do the plain load from memory once. Subsequent
6700 iterations use the value loaded by the compare-and-swap pattern. */
6702 label = gen_label_rtx ();
6703 cmp_reg = gen_reg_rtx (mode);
6705 emit_move_insn (cmp_reg, mem);
6706 emit_label (label);
6707 emit_move_insn (old_reg, cmp_reg);
6708 if (seq)
6709 emit_insn (seq);
6711 /* If the target supports a compare-and-swap pattern that simultaneously
6712 sets some flag for success, then use it. Otherwise use the regular
6713 compare-and-swap and follow that immediately with a compare insn. */
6714 icode = direct_optab_handler (sync_compare_and_swap_optab, mode);
6715 if (icode == CODE_FOR_nothing)
6716 return false;
6718 subtarget = expand_val_compare_and_swap_1 (mem, old_reg, new_reg,
6719 cmp_reg, icode);
6720 if (subtarget == NULL_RTX)
6721 return false;
6723 cc_reg = NULL_RTX;
6724 if (have_insn_for (COMPARE, CCmode))
6725 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
6726 if (cc_reg)
6728 cmp_reg = cc_reg;
6729 old_reg = const0_rtx;
6731 else
6733 if (subtarget != cmp_reg)
6734 emit_move_insn (cmp_reg, subtarget);
6737 /* ??? Mark this jump predicted not taken? */
6738 emit_cmp_and_jump_insns (cmp_reg, old_reg, NE, const0_rtx, GET_MODE (cmp_reg), 1,
6739 label);
6740 return true;
6743 /* This function generates the atomic operation MEM CODE= VAL. In this
6744 case, we do not care about any resulting value. Returns NULL if we
6745 cannot generate the operation. */
6748 expand_sync_operation (rtx mem, rtx val, enum rtx_code code)
6750 enum machine_mode mode = GET_MODE (mem);
6751 enum insn_code icode;
6752 rtx insn;
6754 /* Look to see if the target supports the operation directly. */
6755 switch (code)
6757 case PLUS:
6758 icode = direct_optab_handler (sync_add_optab, mode);
6759 break;
6760 case IOR:
6761 icode = direct_optab_handler (sync_ior_optab, mode);
6762 break;
6763 case XOR:
6764 icode = direct_optab_handler (sync_xor_optab, mode);
6765 break;
6766 case AND:
6767 icode = direct_optab_handler (sync_and_optab, mode);
6768 break;
6769 case NOT:
6770 icode = direct_optab_handler (sync_nand_optab, mode);
6771 break;
6773 case MINUS:
6774 icode = direct_optab_handler (sync_sub_optab, mode);
6775 if (icode == CODE_FOR_nothing || CONST_INT_P (val))
6777 icode = direct_optab_handler (sync_add_optab, mode);
6778 if (icode != CODE_FOR_nothing)
6780 val = expand_simple_unop (mode, NEG, val, NULL_RTX, 1);
6781 code = PLUS;
6784 break;
6786 default:
6787 gcc_unreachable ();
6790 /* Generate the direct operation, if present. */
6791 if (icode != CODE_FOR_nothing)
6793 struct expand_operand ops[2];
6795 create_fixed_operand (&ops[0], mem);
6796 /* VAL may have been promoted to a wider mode. Shrink it if so. */
6797 create_convert_operand_to (&ops[1], val, mode, true);
6798 if (maybe_expand_insn (icode, 2, ops))
6799 return const0_rtx;
6802 /* Failing that, generate a compare-and-swap loop in which we perform the
6803 operation with normal arithmetic instructions. */
6804 if (direct_optab_handler (sync_compare_and_swap_optab, mode)
6805 != CODE_FOR_nothing)
6807 rtx t0 = gen_reg_rtx (mode), t1;
6809 start_sequence ();
6811 t1 = t0;
6812 if (code == NOT)
6814 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
6815 true, OPTAB_LIB_WIDEN);
6816 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
6818 else
6819 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX,
6820 true, OPTAB_LIB_WIDEN);
6821 insn = get_insns ();
6822 end_sequence ();
6824 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
6825 return const0_rtx;
6828 return NULL_RTX;
6831 /* This function generates the atomic operation MEM CODE= VAL. In this
6832 case, we do care about the resulting value: if AFTER is true then
6833 return the value MEM holds after the operation, if AFTER is false
6834 then return the value MEM holds before the operation. TARGET is an
6835 optional place for the result value to be stored. */
6838 expand_sync_fetch_operation (rtx mem, rtx val, enum rtx_code code,
6839 bool after, rtx target)
6841 enum machine_mode mode = GET_MODE (mem);
6842 enum insn_code old_code, new_code, icode;
6843 bool compensate;
6844 rtx insn;
6846 /* Look to see if the target supports the operation directly. */
6847 switch (code)
6849 case PLUS:
6850 old_code = direct_optab_handler (sync_old_add_optab, mode);
6851 new_code = direct_optab_handler (sync_new_add_optab, mode);
6852 break;
6853 case IOR:
6854 old_code = direct_optab_handler (sync_old_ior_optab, mode);
6855 new_code = direct_optab_handler (sync_new_ior_optab, mode);
6856 break;
6857 case XOR:
6858 old_code = direct_optab_handler (sync_old_xor_optab, mode);
6859 new_code = direct_optab_handler (sync_new_xor_optab, mode);
6860 break;
6861 case AND:
6862 old_code = direct_optab_handler (sync_old_and_optab, mode);
6863 new_code = direct_optab_handler (sync_new_and_optab, mode);
6864 break;
6865 case NOT:
6866 old_code = direct_optab_handler (sync_old_nand_optab, mode);
6867 new_code = direct_optab_handler (sync_new_nand_optab, mode);
6868 break;
6870 case MINUS:
6871 old_code = direct_optab_handler (sync_old_sub_optab, mode);
6872 new_code = direct_optab_handler (sync_new_sub_optab, mode);
6873 if ((old_code == CODE_FOR_nothing && new_code == CODE_FOR_nothing)
6874 || CONST_INT_P (val))
6876 old_code = direct_optab_handler (sync_old_add_optab, mode);
6877 new_code = direct_optab_handler (sync_new_add_optab, mode);
6878 if (old_code != CODE_FOR_nothing || new_code != CODE_FOR_nothing)
6880 val = expand_simple_unop (mode, NEG, val, NULL_RTX, 1);
6881 code = PLUS;
6884 break;
6886 default:
6887 gcc_unreachable ();
6890 /* If the target does supports the proper new/old operation, great. But
6891 if we only support the opposite old/new operation, check to see if we
6892 can compensate. In the case in which the old value is supported, then
6893 we can always perform the operation again with normal arithmetic. In
6894 the case in which the new value is supported, then we can only handle
6895 this in the case the operation is reversible. */
6896 compensate = false;
6897 if (after)
6899 icode = new_code;
6900 if (icode == CODE_FOR_nothing)
6902 icode = old_code;
6903 if (icode != CODE_FOR_nothing)
6904 compensate = true;
6907 else
6909 icode = old_code;
6910 if (icode == CODE_FOR_nothing
6911 && (code == PLUS || code == MINUS || code == XOR))
6913 icode = new_code;
6914 if (icode != CODE_FOR_nothing)
6915 compensate = true;
6919 /* If we found something supported, great. */
6920 if (icode != CODE_FOR_nothing)
6922 struct expand_operand ops[3];
6924 create_output_operand (&ops[0], target, mode);
6925 create_fixed_operand (&ops[1], mem);
6926 /* VAL may have been promoted to a wider mode. Shrink it if so. */
6927 create_convert_operand_to (&ops[2], val, mode, true);
6928 if (maybe_expand_insn (icode, 3, ops))
6930 target = ops[0].value;
6931 val = ops[2].value;
6932 /* If we need to compensate for using an operation with the
6933 wrong return value, do so now. */
6934 if (compensate)
6936 if (!after)
6938 if (code == PLUS)
6939 code = MINUS;
6940 else if (code == MINUS)
6941 code = PLUS;
6944 if (code == NOT)
6946 target = expand_simple_binop (mode, AND, target, val,
6947 NULL_RTX, true,
6948 OPTAB_LIB_WIDEN);
6949 target = expand_simple_unop (mode, code, target,
6950 NULL_RTX, true);
6952 else
6953 target = expand_simple_binop (mode, code, target, val,
6954 NULL_RTX, true,
6955 OPTAB_LIB_WIDEN);
6958 return target;
6962 /* Failing that, generate a compare-and-swap loop in which we perform the
6963 operation with normal arithmetic instructions. */
6964 if (direct_optab_handler (sync_compare_and_swap_optab, mode)
6965 != CODE_FOR_nothing)
6967 rtx t0 = gen_reg_rtx (mode), t1;
6969 if (!target || !register_operand (target, mode))
6970 target = gen_reg_rtx (mode);
6972 start_sequence ();
6974 if (!after)
6975 emit_move_insn (target, t0);
6976 t1 = t0;
6977 if (code == NOT)
6979 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
6980 true, OPTAB_LIB_WIDEN);
6981 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
6983 else
6984 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX,
6985 true, OPTAB_LIB_WIDEN);
6986 if (after)
6987 emit_move_insn (target, t1);
6989 insn = get_insns ();
6990 end_sequence ();
6992 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
6993 return target;
6996 return NULL_RTX;
6999 /* This function expands a test-and-set operation. Ideally we atomically
7000 store VAL in MEM and return the previous value in MEM. Some targets
7001 may not support this operation and only support VAL with the constant 1;
7002 in this case while the return value will be 0/1, but the exact value
7003 stored in MEM is target defined. TARGET is an option place to stick
7004 the return value. */
7007 expand_sync_lock_test_and_set (rtx mem, rtx val, rtx target)
7009 enum machine_mode mode = GET_MODE (mem);
7010 enum insn_code icode;
7012 /* If the target supports the test-and-set directly, great. */
7013 icode = direct_optab_handler (sync_lock_test_and_set_optab, mode);
7014 if (icode != CODE_FOR_nothing)
7016 struct expand_operand ops[3];
7018 create_output_operand (&ops[0], target, mode);
7019 create_fixed_operand (&ops[1], mem);
7020 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7021 create_convert_operand_to (&ops[2], val, mode, true);
7022 if (maybe_expand_insn (icode, 3, ops))
7023 return ops[0].value;
7026 /* Otherwise, use a compare-and-swap loop for the exchange. */
7027 if (direct_optab_handler (sync_compare_and_swap_optab, mode)
7028 != CODE_FOR_nothing)
7030 if (!target || !register_operand (target, mode))
7031 target = gen_reg_rtx (mode);
7032 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7033 val = convert_modes (mode, GET_MODE (val), val, 1);
7034 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7035 return target;
7038 return NULL_RTX;
7041 /* Return true if OPERAND is suitable for operand number OPNO of
7042 instruction ICODE. */
7044 bool
7045 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
7047 return (!insn_data[(int) icode].operand[opno].predicate
7048 || (insn_data[(int) icode].operand[opno].predicate
7049 (operand, insn_data[(int) icode].operand[opno].mode)));
7052 /* TARGET is a target of a multiword operation that we are going to
7053 implement as a series of word-mode operations. Return true if
7054 TARGET is suitable for this purpose. */
7056 bool
7057 valid_multiword_target_p (rtx target)
7059 enum machine_mode mode;
7060 int i;
7062 mode = GET_MODE (target);
7063 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
7064 if (!validate_subreg (word_mode, mode, target, i))
7065 return false;
7066 return true;
7069 /* Like maybe_legitimize_operand, but do not change the code of the
7070 current rtx value. */
7072 static bool
7073 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
7074 struct expand_operand *op)
7076 /* See if the operand matches in its current form. */
7077 if (insn_operand_matches (icode, opno, op->value))
7078 return true;
7080 /* If the operand is a memory whose address has no side effects,
7081 try forcing the address into a register. The check for side
7082 effects is important because force_reg cannot handle things
7083 like auto-modified addresses. */
7084 if (insn_data[(int) icode].operand[opno].allows_mem
7085 && MEM_P (op->value)
7086 && !side_effects_p (XEXP (op->value, 0)))
7088 rtx addr, mem, last;
7090 last = get_last_insn ();
7091 addr = force_reg (Pmode, XEXP (op->value, 0));
7092 mem = replace_equiv_address (op->value, addr);
7093 if (insn_operand_matches (icode, opno, mem))
7095 op->value = mem;
7096 return true;
7098 delete_insns_since (last);
7101 return false;
7104 /* Try to make OP match operand OPNO of instruction ICODE. Return true
7105 on success, storing the new operand value back in OP. */
7107 static bool
7108 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
7109 struct expand_operand *op)
7111 enum machine_mode mode, imode;
7112 bool old_volatile_ok, result;
7114 mode = op->mode;
7115 switch (op->type)
7117 case EXPAND_FIXED:
7118 old_volatile_ok = volatile_ok;
7119 volatile_ok = true;
7120 result = maybe_legitimize_operand_same_code (icode, opno, op);
7121 volatile_ok = old_volatile_ok;
7122 return result;
7124 case EXPAND_OUTPUT:
7125 gcc_assert (mode != VOIDmode);
7126 if (op->value
7127 && op->value != const0_rtx
7128 && GET_MODE (op->value) == mode
7129 && maybe_legitimize_operand_same_code (icode, opno, op))
7130 return true;
7132 op->value = gen_reg_rtx (mode);
7133 break;
7135 case EXPAND_INPUT:
7136 input:
7137 gcc_assert (mode != VOIDmode);
7138 gcc_assert (GET_MODE (op->value) == VOIDmode
7139 || GET_MODE (op->value) == mode);
7140 if (maybe_legitimize_operand_same_code (icode, opno, op))
7141 return true;
7143 op->value = copy_to_mode_reg (mode, op->value);
7144 break;
7146 case EXPAND_CONVERT_TO:
7147 gcc_assert (mode != VOIDmode);
7148 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
7149 goto input;
7151 case EXPAND_CONVERT_FROM:
7152 if (GET_MODE (op->value) != VOIDmode)
7153 mode = GET_MODE (op->value);
7154 else
7155 /* The caller must tell us what mode this value has. */
7156 gcc_assert (mode != VOIDmode);
7158 imode = insn_data[(int) icode].operand[opno].mode;
7159 if (imode != VOIDmode && imode != mode)
7161 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
7162 mode = imode;
7164 goto input;
7166 case EXPAND_ADDRESS:
7167 gcc_assert (mode != VOIDmode);
7168 op->value = convert_memory_address (mode, op->value);
7169 goto input;
7171 case EXPAND_INTEGER:
7172 mode = insn_data[(int) icode].operand[opno].mode;
7173 if (mode != VOIDmode && const_int_operand (op->value, mode))
7174 goto input;
7175 break;
7177 return insn_operand_matches (icode, opno, op->value);
7180 /* Make OP describe an input operand that should have the same value
7181 as VALUE, after any mode conversion that the target might request.
7182 TYPE is the type of VALUE. */
7184 void
7185 create_convert_operand_from_type (struct expand_operand *op,
7186 rtx value, tree type)
7188 create_convert_operand_from (op, value, TYPE_MODE (type),
7189 TYPE_UNSIGNED (type));
7192 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
7193 of instruction ICODE. Return true on success, leaving the new operand
7194 values in the OPS themselves. Emit no code on failure. */
7196 bool
7197 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
7198 unsigned int nops, struct expand_operand *ops)
7200 rtx last;
7201 unsigned int i;
7203 last = get_last_insn ();
7204 for (i = 0; i < nops; i++)
7205 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
7207 delete_insns_since (last);
7208 return false;
7210 return true;
7213 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
7214 as its operands. Return the instruction pattern on success,
7215 and emit any necessary set-up code. Return null and emit no
7216 code on failure. */
7219 maybe_gen_insn (enum insn_code icode, unsigned int nops,
7220 struct expand_operand *ops)
7222 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
7223 if (!maybe_legitimize_operands (icode, 0, nops, ops))
7224 return NULL_RTX;
7226 switch (nops)
7228 case 1:
7229 return GEN_FCN (icode) (ops[0].value);
7230 case 2:
7231 return GEN_FCN (icode) (ops[0].value, ops[1].value);
7232 case 3:
7233 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
7234 case 4:
7235 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7236 ops[3].value);
7237 case 5:
7238 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7239 ops[3].value, ops[4].value);
7240 case 6:
7241 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7242 ops[3].value, ops[4].value, ops[5].value);
7244 gcc_unreachable ();
7247 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
7248 as its operands. Return true on success and emit no code on failure. */
7250 bool
7251 maybe_expand_insn (enum insn_code icode, unsigned int nops,
7252 struct expand_operand *ops)
7254 rtx pat = maybe_gen_insn (icode, nops, ops);
7255 if (pat)
7257 emit_insn (pat);
7258 return true;
7260 return false;
7263 /* Like maybe_expand_insn, but for jumps. */
7265 bool
7266 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
7267 struct expand_operand *ops)
7269 rtx pat = maybe_gen_insn (icode, nops, ops);
7270 if (pat)
7272 emit_jump_insn (pat);
7273 return true;
7275 return false;
7278 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
7279 as its operands. */
7281 void
7282 expand_insn (enum insn_code icode, unsigned int nops,
7283 struct expand_operand *ops)
7285 if (!maybe_expand_insn (icode, nops, ops))
7286 gcc_unreachable ();
7289 /* Like expand_insn, but for jumps. */
7291 void
7292 expand_jump_insn (enum insn_code icode, unsigned int nops,
7293 struct expand_operand *ops)
7295 if (!maybe_expand_jump_insn (icode, nops, ops))
7296 gcc_unreachable ();
7299 #include "gt-optabs.h"