2011-08-31 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / gcc / optabs.c
blob886b259ce3472c94e0f9efb4ef2386eefcceb135
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 2011 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 switch (code)
221 case FFS:
222 case CLZ:
223 case CTZ:
224 case CLRSB:
225 case POPCOUNT:
226 case PARITY:
227 case BSWAP:
228 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
230 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
231 if (GET_MODE_SIZE (GET_MODE (op0))
232 > GET_MODE_SIZE (GET_MODE (target)))
233 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
234 note, GET_MODE (op0));
235 else
236 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
237 note, GET_MODE (op0));
238 break;
240 /* FALLTHRU */
241 default:
242 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
243 break;
245 else
246 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
248 set_unique_reg_note (last_insn, REG_EQUAL, note);
250 return 1;
253 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
254 for a widening operation would be. In most cases this would be OP0, but if
255 that's a constant it'll be VOIDmode, which isn't useful. */
257 static enum machine_mode
258 widened_mode (enum machine_mode to_mode, rtx op0, rtx op1)
260 enum machine_mode m0 = GET_MODE (op0);
261 enum machine_mode m1 = GET_MODE (op1);
262 enum machine_mode result;
264 if (m0 == VOIDmode && m1 == VOIDmode)
265 return to_mode;
266 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
267 result = m1;
268 else
269 result = m0;
271 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
272 return to_mode;
274 return result;
277 /* Find a widening optab even if it doesn't widen as much as we want.
278 E.g. if from_mode is HImode, and to_mode is DImode, and there is no
279 direct HI->SI insn, then return SI->DI, if that exists.
280 If PERMIT_NON_WIDENING is non-zero then this can be used with
281 non-widening optabs also. */
283 enum insn_code
284 find_widening_optab_handler_and_mode (optab op, enum machine_mode to_mode,
285 enum machine_mode from_mode,
286 int permit_non_widening,
287 enum machine_mode *found_mode)
289 for (; (permit_non_widening || from_mode != to_mode)
290 && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
291 && from_mode != VOIDmode;
292 from_mode = GET_MODE_WIDER_MODE (from_mode))
294 enum insn_code handler = widening_optab_handler (op, to_mode,
295 from_mode);
297 if (handler != CODE_FOR_nothing)
299 if (found_mode)
300 *found_mode = from_mode;
301 return handler;
305 return CODE_FOR_nothing;
308 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
309 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
310 not actually do a sign-extend or zero-extend, but can leave the
311 higher-order bits of the result rtx undefined, for example, in the case
312 of logical operations, but not right shifts. */
314 static rtx
315 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
316 int unsignedp, int no_extend)
318 rtx result;
320 /* If we don't have to extend and this is a constant, return it. */
321 if (no_extend && GET_MODE (op) == VOIDmode)
322 return op;
324 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
325 extend since it will be more efficient to do so unless the signedness of
326 a promoted object differs from our extension. */
327 if (! no_extend
328 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
329 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
330 return convert_modes (mode, oldmode, op, unsignedp);
332 /* If MODE is no wider than a single word, we return a paradoxical
333 SUBREG. */
334 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
335 return gen_rtx_SUBREG (mode, force_reg (GET_MODE (op), op), 0);
337 /* Otherwise, get an object of MODE, clobber it, and set the low-order
338 part to OP. */
340 result = gen_reg_rtx (mode);
341 emit_clobber (result);
342 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
343 return result;
346 /* Return the optab used for computing the operation given by the tree code,
347 CODE and the tree EXP. This function is not always usable (for example, it
348 cannot give complete results for multiplication or division) but probably
349 ought to be relied on more widely throughout the expander. */
350 optab
351 optab_for_tree_code (enum tree_code code, const_tree type,
352 enum optab_subtype subtype)
354 bool trapv;
355 switch (code)
357 case BIT_AND_EXPR:
358 return and_optab;
360 case BIT_IOR_EXPR:
361 return ior_optab;
363 case BIT_NOT_EXPR:
364 return one_cmpl_optab;
366 case BIT_XOR_EXPR:
367 return xor_optab;
369 case TRUNC_MOD_EXPR:
370 case CEIL_MOD_EXPR:
371 case FLOOR_MOD_EXPR:
372 case ROUND_MOD_EXPR:
373 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
375 case RDIV_EXPR:
376 case TRUNC_DIV_EXPR:
377 case CEIL_DIV_EXPR:
378 case FLOOR_DIV_EXPR:
379 case ROUND_DIV_EXPR:
380 case EXACT_DIV_EXPR:
381 if (TYPE_SATURATING(type))
382 return TYPE_UNSIGNED(type) ? usdiv_optab : ssdiv_optab;
383 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
385 case LSHIFT_EXPR:
386 if (TREE_CODE (type) == VECTOR_TYPE)
388 if (subtype == optab_vector)
389 return TYPE_SATURATING (type) ? NULL : vashl_optab;
391 gcc_assert (subtype == optab_scalar);
393 if (TYPE_SATURATING(type))
394 return TYPE_UNSIGNED(type) ? usashl_optab : ssashl_optab;
395 return ashl_optab;
397 case RSHIFT_EXPR:
398 if (TREE_CODE (type) == VECTOR_TYPE)
400 if (subtype == optab_vector)
401 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
403 gcc_assert (subtype == optab_scalar);
405 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
407 case LROTATE_EXPR:
408 if (TREE_CODE (type) == VECTOR_TYPE)
410 if (subtype == optab_vector)
411 return vrotl_optab;
413 gcc_assert (subtype == optab_scalar);
415 return rotl_optab;
417 case RROTATE_EXPR:
418 if (TREE_CODE (type) == VECTOR_TYPE)
420 if (subtype == optab_vector)
421 return vrotr_optab;
423 gcc_assert (subtype == optab_scalar);
425 return rotr_optab;
427 case MAX_EXPR:
428 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
430 case MIN_EXPR:
431 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
433 case REALIGN_LOAD_EXPR:
434 return vec_realign_load_optab;
436 case WIDEN_SUM_EXPR:
437 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
439 case DOT_PROD_EXPR:
440 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
442 case WIDEN_MULT_PLUS_EXPR:
443 return (TYPE_UNSIGNED (type)
444 ? (TYPE_SATURATING (type)
445 ? usmadd_widen_optab : umadd_widen_optab)
446 : (TYPE_SATURATING (type)
447 ? ssmadd_widen_optab : smadd_widen_optab));
449 case WIDEN_MULT_MINUS_EXPR:
450 return (TYPE_UNSIGNED (type)
451 ? (TYPE_SATURATING (type)
452 ? usmsub_widen_optab : umsub_widen_optab)
453 : (TYPE_SATURATING (type)
454 ? ssmsub_widen_optab : smsub_widen_optab));
456 case FMA_EXPR:
457 return fma_optab;
459 case REDUC_MAX_EXPR:
460 return TYPE_UNSIGNED (type) ? reduc_umax_optab : reduc_smax_optab;
462 case REDUC_MIN_EXPR:
463 return TYPE_UNSIGNED (type) ? reduc_umin_optab : reduc_smin_optab;
465 case REDUC_PLUS_EXPR:
466 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
468 case VEC_LSHIFT_EXPR:
469 return vec_shl_optab;
471 case VEC_RSHIFT_EXPR:
472 return vec_shr_optab;
474 case VEC_WIDEN_MULT_HI_EXPR:
475 return TYPE_UNSIGNED (type) ?
476 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
478 case VEC_WIDEN_MULT_LO_EXPR:
479 return TYPE_UNSIGNED (type) ?
480 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
482 case VEC_UNPACK_HI_EXPR:
483 return TYPE_UNSIGNED (type) ?
484 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
486 case VEC_UNPACK_LO_EXPR:
487 return TYPE_UNSIGNED (type) ?
488 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
490 case VEC_UNPACK_FLOAT_HI_EXPR:
491 /* The signedness is determined from input operand. */
492 return TYPE_UNSIGNED (type) ?
493 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
495 case VEC_UNPACK_FLOAT_LO_EXPR:
496 /* The signedness is determined from input operand. */
497 return TYPE_UNSIGNED (type) ?
498 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
500 case VEC_PACK_TRUNC_EXPR:
501 return vec_pack_trunc_optab;
503 case VEC_PACK_SAT_EXPR:
504 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
506 case VEC_PACK_FIX_TRUNC_EXPR:
507 /* The signedness is determined from output operand. */
508 return TYPE_UNSIGNED (type) ?
509 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
511 default:
512 break;
515 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
516 switch (code)
518 case POINTER_PLUS_EXPR:
519 case PLUS_EXPR:
520 if (TYPE_SATURATING(type))
521 return TYPE_UNSIGNED(type) ? usadd_optab : ssadd_optab;
522 return trapv ? addv_optab : add_optab;
524 case MINUS_EXPR:
525 if (TYPE_SATURATING(type))
526 return TYPE_UNSIGNED(type) ? ussub_optab : sssub_optab;
527 return trapv ? subv_optab : sub_optab;
529 case MULT_EXPR:
530 if (TYPE_SATURATING(type))
531 return TYPE_UNSIGNED(type) ? usmul_optab : ssmul_optab;
532 return trapv ? smulv_optab : smul_optab;
534 case NEGATE_EXPR:
535 if (TYPE_SATURATING(type))
536 return TYPE_UNSIGNED(type) ? usneg_optab : ssneg_optab;
537 return trapv ? negv_optab : neg_optab;
539 case ABS_EXPR:
540 return trapv ? absv_optab : abs_optab;
542 case VEC_EXTRACT_EVEN_EXPR:
543 return vec_extract_even_optab;
545 case VEC_EXTRACT_ODD_EXPR:
546 return vec_extract_odd_optab;
548 case VEC_INTERLEAVE_HIGH_EXPR:
549 return vec_interleave_high_optab;
551 case VEC_INTERLEAVE_LOW_EXPR:
552 return vec_interleave_low_optab;
554 default:
555 return NULL;
560 /* Expand vector widening operations.
562 There are two different classes of operations handled here:
563 1) Operations whose result is wider than all the arguments to the operation.
564 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
565 In this case OP0 and optionally OP1 would be initialized,
566 but WIDE_OP wouldn't (not relevant for this case).
567 2) Operations whose result is of the same size as the last argument to the
568 operation, but wider than all the other arguments to the operation.
569 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
570 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
572 E.g, when called to expand the following operations, this is how
573 the arguments will be initialized:
574 nops OP0 OP1 WIDE_OP
575 widening-sum 2 oprnd0 - oprnd1
576 widening-dot-product 3 oprnd0 oprnd1 oprnd2
577 widening-mult 2 oprnd0 oprnd1 -
578 type-promotion (vec-unpack) 1 oprnd0 - - */
581 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
582 rtx target, int unsignedp)
584 struct expand_operand eops[4];
585 tree oprnd0, oprnd1, oprnd2;
586 enum machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
587 optab widen_pattern_optab;
588 enum insn_code icode;
589 int nops = TREE_CODE_LENGTH (ops->code);
590 int op;
592 oprnd0 = ops->op0;
593 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
594 widen_pattern_optab =
595 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
596 if (ops->code == WIDEN_MULT_PLUS_EXPR
597 || ops->code == WIDEN_MULT_MINUS_EXPR)
598 icode = find_widening_optab_handler (widen_pattern_optab,
599 TYPE_MODE (TREE_TYPE (ops->op2)),
600 tmode0, 0);
601 else
602 icode = optab_handler (widen_pattern_optab, tmode0);
603 gcc_assert (icode != CODE_FOR_nothing);
605 if (nops >= 2)
607 oprnd1 = ops->op1;
608 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
611 /* The last operand is of a wider mode than the rest of the operands. */
612 if (nops == 2)
613 wmode = tmode1;
614 else if (nops == 3)
616 gcc_assert (tmode1 == tmode0);
617 gcc_assert (op1);
618 oprnd2 = ops->op2;
619 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
622 op = 0;
623 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
624 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
625 if (op1)
626 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
627 if (wide_op)
628 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
629 expand_insn (icode, op, eops);
630 return eops[0].value;
633 /* Generate code to perform an operation specified by TERNARY_OPTAB
634 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
636 UNSIGNEDP is for the case where we have to widen the operands
637 to perform the operation. It says to use zero-extension.
639 If TARGET is nonzero, the value
640 is generated there, if it is convenient to do so.
641 In all cases an rtx is returned for the locus of the value;
642 this may or may not be TARGET. */
645 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
646 rtx op1, rtx op2, rtx target, int unsignedp)
648 struct expand_operand ops[4];
649 enum insn_code icode = optab_handler (ternary_optab, mode);
651 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
653 create_output_operand (&ops[0], target, mode);
654 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
655 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
656 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
657 expand_insn (icode, 4, ops);
658 return ops[0].value;
662 /* Like expand_binop, but return a constant rtx if the result can be
663 calculated at compile time. The arguments and return value are
664 otherwise the same as for expand_binop. */
666 static rtx
667 simplify_expand_binop (enum machine_mode mode, optab binoptab,
668 rtx op0, rtx op1, rtx target, int unsignedp,
669 enum optab_methods methods)
671 if (CONSTANT_P (op0) && CONSTANT_P (op1))
673 rtx x = simplify_binary_operation (binoptab->code, mode, op0, op1);
675 if (x)
676 return x;
679 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
682 /* Like simplify_expand_binop, but always put the result in TARGET.
683 Return true if the expansion succeeded. */
685 bool
686 force_expand_binop (enum machine_mode mode, optab binoptab,
687 rtx op0, rtx op1, rtx target, int unsignedp,
688 enum optab_methods methods)
690 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
691 target, unsignedp, methods);
692 if (x == 0)
693 return false;
694 if (x != target)
695 emit_move_insn (target, x);
696 return true;
699 /* Generate insns for VEC_LSHIFT_EXPR, VEC_RSHIFT_EXPR. */
702 expand_vec_shift_expr (sepops ops, rtx target)
704 struct expand_operand eops[3];
705 enum insn_code icode;
706 rtx rtx_op1, rtx_op2;
707 enum machine_mode mode = TYPE_MODE (ops->type);
708 tree vec_oprnd = ops->op0;
709 tree shift_oprnd = ops->op1;
710 optab shift_optab;
712 switch (ops->code)
714 case VEC_RSHIFT_EXPR:
715 shift_optab = vec_shr_optab;
716 break;
717 case VEC_LSHIFT_EXPR:
718 shift_optab = vec_shl_optab;
719 break;
720 default:
721 gcc_unreachable ();
724 icode = optab_handler (shift_optab, mode);
725 gcc_assert (icode != CODE_FOR_nothing);
727 rtx_op1 = expand_normal (vec_oprnd);
728 rtx_op2 = expand_normal (shift_oprnd);
730 create_output_operand (&eops[0], target, mode);
731 create_input_operand (&eops[1], rtx_op1, GET_MODE (rtx_op1));
732 create_convert_operand_from_type (&eops[2], rtx_op2, TREE_TYPE (shift_oprnd));
733 expand_insn (icode, 3, eops);
735 return eops[0].value;
738 /* This subroutine of expand_doubleword_shift handles the cases in which
739 the effective shift value is >= BITS_PER_WORD. The arguments and return
740 value are the same as for the parent routine, except that SUPERWORD_OP1
741 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
742 INTO_TARGET may be null if the caller has decided to calculate it. */
744 static bool
745 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
746 rtx outof_target, rtx into_target,
747 int unsignedp, enum optab_methods methods)
749 if (into_target != 0)
750 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
751 into_target, unsignedp, methods))
752 return false;
754 if (outof_target != 0)
756 /* For a signed right shift, we must fill OUTOF_TARGET with copies
757 of the sign bit, otherwise we must fill it with zeros. */
758 if (binoptab != ashr_optab)
759 emit_move_insn (outof_target, CONST0_RTX (word_mode));
760 else
761 if (!force_expand_binop (word_mode, binoptab,
762 outof_input, GEN_INT (BITS_PER_WORD - 1),
763 outof_target, unsignedp, methods))
764 return false;
766 return true;
769 /* This subroutine of expand_doubleword_shift handles the cases in which
770 the effective shift value is < BITS_PER_WORD. The arguments and return
771 value are the same as for the parent routine. */
773 static bool
774 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
775 rtx outof_input, rtx into_input, rtx op1,
776 rtx outof_target, rtx into_target,
777 int unsignedp, enum optab_methods methods,
778 unsigned HOST_WIDE_INT shift_mask)
780 optab reverse_unsigned_shift, unsigned_shift;
781 rtx tmp, carries;
783 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
784 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
786 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
787 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
788 the opposite direction to BINOPTAB. */
789 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
791 carries = outof_input;
792 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
793 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
794 0, true, methods);
796 else
798 /* We must avoid shifting by BITS_PER_WORD bits since that is either
799 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
800 has unknown behavior. Do a single shift first, then shift by the
801 remainder. It's OK to use ~OP1 as the remainder if shift counts
802 are truncated to the mode size. */
803 carries = expand_binop (word_mode, reverse_unsigned_shift,
804 outof_input, const1_rtx, 0, unsignedp, methods);
805 if (shift_mask == BITS_PER_WORD - 1)
807 tmp = immed_double_const (-1, -1, op1_mode);
808 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
809 0, true, methods);
811 else
813 tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
814 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
815 0, true, methods);
818 if (tmp == 0 || carries == 0)
819 return false;
820 carries = expand_binop (word_mode, reverse_unsigned_shift,
821 carries, tmp, 0, unsignedp, methods);
822 if (carries == 0)
823 return false;
825 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
826 so the result can go directly into INTO_TARGET if convenient. */
827 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
828 into_target, unsignedp, methods);
829 if (tmp == 0)
830 return false;
832 /* Now OR in the bits carried over from OUTOF_INPUT. */
833 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
834 into_target, unsignedp, methods))
835 return false;
837 /* Use a standard word_mode shift for the out-of half. */
838 if (outof_target != 0)
839 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
840 outof_target, unsignedp, methods))
841 return false;
843 return true;
847 #ifdef HAVE_conditional_move
848 /* Try implementing expand_doubleword_shift using conditional moves.
849 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
850 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
851 are the shift counts to use in the former and latter case. All other
852 arguments are the same as the parent routine. */
854 static bool
855 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
856 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
857 rtx outof_input, rtx into_input,
858 rtx subword_op1, rtx superword_op1,
859 rtx outof_target, rtx into_target,
860 int unsignedp, enum optab_methods methods,
861 unsigned HOST_WIDE_INT shift_mask)
863 rtx outof_superword, into_superword;
865 /* Put the superword version of the output into OUTOF_SUPERWORD and
866 INTO_SUPERWORD. */
867 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
868 if (outof_target != 0 && subword_op1 == superword_op1)
870 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
871 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
872 into_superword = outof_target;
873 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
874 outof_superword, 0, unsignedp, methods))
875 return false;
877 else
879 into_superword = gen_reg_rtx (word_mode);
880 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
881 outof_superword, into_superword,
882 unsignedp, methods))
883 return false;
886 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
887 if (!expand_subword_shift (op1_mode, binoptab,
888 outof_input, into_input, subword_op1,
889 outof_target, into_target,
890 unsignedp, methods, shift_mask))
891 return false;
893 /* Select between them. Do the INTO half first because INTO_SUPERWORD
894 might be the current value of OUTOF_TARGET. */
895 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
896 into_target, into_superword, word_mode, false))
897 return false;
899 if (outof_target != 0)
900 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
901 outof_target, outof_superword,
902 word_mode, false))
903 return false;
905 return true;
907 #endif
909 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
910 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
911 input operand; the shift moves bits in the direction OUTOF_INPUT->
912 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
913 of the target. OP1 is the shift count and OP1_MODE is its mode.
914 If OP1 is constant, it will have been truncated as appropriate
915 and is known to be nonzero.
917 If SHIFT_MASK is zero, the result of word shifts is undefined when the
918 shift count is outside the range [0, BITS_PER_WORD). This routine must
919 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
921 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
922 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
923 fill with zeros or sign bits as appropriate.
925 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
926 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
927 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
928 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
929 are undefined.
931 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
932 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
933 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
934 function wants to calculate it itself.
936 Return true if the shift could be successfully synthesized. */
938 static bool
939 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
940 rtx outof_input, rtx into_input, rtx op1,
941 rtx outof_target, rtx into_target,
942 int unsignedp, enum optab_methods methods,
943 unsigned HOST_WIDE_INT shift_mask)
945 rtx superword_op1, tmp, cmp1, cmp2;
946 rtx subword_label, done_label;
947 enum rtx_code cmp_code;
949 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
950 fill the result with sign or zero bits as appropriate. If so, the value
951 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
952 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
953 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
955 This isn't worthwhile for constant shifts since the optimizers will
956 cope better with in-range shift counts. */
957 if (shift_mask >= BITS_PER_WORD
958 && outof_target != 0
959 && !CONSTANT_P (op1))
961 if (!expand_doubleword_shift (op1_mode, binoptab,
962 outof_input, into_input, op1,
963 0, into_target,
964 unsignedp, methods, shift_mask))
965 return false;
966 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
967 outof_target, unsignedp, methods))
968 return false;
969 return true;
972 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
973 is true when the effective shift value is less than BITS_PER_WORD.
974 Set SUPERWORD_OP1 to the shift count that should be used to shift
975 OUTOF_INPUT into INTO_TARGET when the condition is false. */
976 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
977 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
979 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
980 is a subword shift count. */
981 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
982 0, true, methods);
983 cmp2 = CONST0_RTX (op1_mode);
984 cmp_code = EQ;
985 superword_op1 = op1;
987 else
989 /* Set CMP1 to OP1 - BITS_PER_WORD. */
990 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
991 0, true, methods);
992 cmp2 = CONST0_RTX (op1_mode);
993 cmp_code = LT;
994 superword_op1 = cmp1;
996 if (cmp1 == 0)
997 return false;
999 /* If we can compute the condition at compile time, pick the
1000 appropriate subroutine. */
1001 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1002 if (tmp != 0 && CONST_INT_P (tmp))
1004 if (tmp == const0_rtx)
1005 return expand_superword_shift (binoptab, outof_input, superword_op1,
1006 outof_target, into_target,
1007 unsignedp, methods);
1008 else
1009 return expand_subword_shift (op1_mode, binoptab,
1010 outof_input, into_input, op1,
1011 outof_target, into_target,
1012 unsignedp, methods, shift_mask);
1015 #ifdef HAVE_conditional_move
1016 /* Try using conditional moves to generate straight-line code. */
1018 rtx start = get_last_insn ();
1019 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1020 cmp_code, cmp1, cmp2,
1021 outof_input, into_input,
1022 op1, superword_op1,
1023 outof_target, into_target,
1024 unsignedp, methods, shift_mask))
1025 return true;
1026 delete_insns_since (start);
1028 #endif
1030 /* As a last resort, use branches to select the correct alternative. */
1031 subword_label = gen_label_rtx ();
1032 done_label = gen_label_rtx ();
1034 NO_DEFER_POP;
1035 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1036 0, 0, subword_label, -1);
1037 OK_DEFER_POP;
1039 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1040 outof_target, into_target,
1041 unsignedp, methods))
1042 return false;
1044 emit_jump_insn (gen_jump (done_label));
1045 emit_barrier ();
1046 emit_label (subword_label);
1048 if (!expand_subword_shift (op1_mode, binoptab,
1049 outof_input, into_input, op1,
1050 outof_target, into_target,
1051 unsignedp, methods, shift_mask))
1052 return false;
1054 emit_label (done_label);
1055 return true;
1058 /* Subroutine of expand_binop. Perform a double word multiplication of
1059 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1060 as the target's word_mode. This function return NULL_RTX if anything
1061 goes wrong, in which case it may have already emitted instructions
1062 which need to be deleted.
1064 If we want to multiply two two-word values and have normal and widening
1065 multiplies of single-word values, we can do this with three smaller
1066 multiplications.
1068 The multiplication proceeds as follows:
1069 _______________________
1070 [__op0_high_|__op0_low__]
1071 _______________________
1072 * [__op1_high_|__op1_low__]
1073 _______________________________________________
1074 _______________________
1075 (1) [__op0_low__*__op1_low__]
1076 _______________________
1077 (2a) [__op0_low__*__op1_high_]
1078 _______________________
1079 (2b) [__op0_high_*__op1_low__]
1080 _______________________
1081 (3) [__op0_high_*__op1_high_]
1084 This gives a 4-word result. Since we are only interested in the
1085 lower 2 words, partial result (3) and the upper words of (2a) and
1086 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1087 calculated using non-widening multiplication.
1089 (1), however, needs to be calculated with an unsigned widening
1090 multiplication. If this operation is not directly supported we
1091 try using a signed widening multiplication and adjust the result.
1092 This adjustment works as follows:
1094 If both operands are positive then no adjustment is needed.
1096 If the operands have different signs, for example op0_low < 0 and
1097 op1_low >= 0, the instruction treats the most significant bit of
1098 op0_low as a sign bit instead of a bit with significance
1099 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1100 with 2**BITS_PER_WORD - op0_low, and two's complements the
1101 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1102 the result.
1104 Similarly, if both operands are negative, we need to add
1105 (op0_low + op1_low) * 2**BITS_PER_WORD.
1107 We use a trick to adjust quickly. We logically shift op0_low right
1108 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1109 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1110 logical shift exists, we do an arithmetic right shift and subtract
1111 the 0 or -1. */
1113 static rtx
1114 expand_doubleword_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
1115 bool umulp, enum optab_methods methods)
1117 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1118 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1119 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1120 rtx product, adjust, product_high, temp;
1122 rtx op0_high = operand_subword_force (op0, high, mode);
1123 rtx op0_low = operand_subword_force (op0, low, mode);
1124 rtx op1_high = operand_subword_force (op1, high, mode);
1125 rtx op1_low = operand_subword_force (op1, low, mode);
1127 /* If we're using an unsigned multiply to directly compute the product
1128 of the low-order words of the operands and perform any required
1129 adjustments of the operands, we begin by trying two more multiplications
1130 and then computing the appropriate sum.
1132 We have checked above that the required addition is provided.
1133 Full-word addition will normally always succeed, especially if
1134 it is provided at all, so we don't worry about its failure. The
1135 multiplication may well fail, however, so we do handle that. */
1137 if (!umulp)
1139 /* ??? This could be done with emit_store_flag where available. */
1140 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1141 NULL_RTX, 1, methods);
1142 if (temp)
1143 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1144 NULL_RTX, 0, OPTAB_DIRECT);
1145 else
1147 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1148 NULL_RTX, 0, methods);
1149 if (!temp)
1150 return NULL_RTX;
1151 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1152 NULL_RTX, 0, OPTAB_DIRECT);
1155 if (!op0_high)
1156 return NULL_RTX;
1159 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1160 NULL_RTX, 0, OPTAB_DIRECT);
1161 if (!adjust)
1162 return NULL_RTX;
1164 /* OP0_HIGH should now be dead. */
1166 if (!umulp)
1168 /* ??? This could be done with emit_store_flag where available. */
1169 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1170 NULL_RTX, 1, methods);
1171 if (temp)
1172 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1173 NULL_RTX, 0, OPTAB_DIRECT);
1174 else
1176 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1177 NULL_RTX, 0, methods);
1178 if (!temp)
1179 return NULL_RTX;
1180 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1181 NULL_RTX, 0, OPTAB_DIRECT);
1184 if (!op1_high)
1185 return NULL_RTX;
1188 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1189 NULL_RTX, 0, OPTAB_DIRECT);
1190 if (!temp)
1191 return NULL_RTX;
1193 /* OP1_HIGH should now be dead. */
1195 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1196 NULL_RTX, 0, OPTAB_DIRECT);
1198 if (target && !REG_P (target))
1199 target = NULL_RTX;
1201 if (umulp)
1202 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1203 target, 1, OPTAB_DIRECT);
1204 else
1205 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1206 target, 1, OPTAB_DIRECT);
1208 if (!product)
1209 return NULL_RTX;
1211 product_high = operand_subword (product, high, 1, mode);
1212 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1213 NULL_RTX, 0, OPTAB_DIRECT);
1214 emit_move_insn (product_high, adjust);
1215 return product;
1218 /* Wrapper around expand_binop which takes an rtx code to specify
1219 the operation to perform, not an optab pointer. All other
1220 arguments are the same. */
1222 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
1223 rtx op1, rtx target, int unsignedp,
1224 enum optab_methods methods)
1226 optab binop = code_to_optab[(int) code];
1227 gcc_assert (binop);
1229 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1232 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1233 binop. Order them according to commutative_operand_precedence and, if
1234 possible, try to put TARGET or a pseudo first. */
1235 static bool
1236 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1238 int op0_prec = commutative_operand_precedence (op0);
1239 int op1_prec = commutative_operand_precedence (op1);
1241 if (op0_prec < op1_prec)
1242 return true;
1244 if (op0_prec > op1_prec)
1245 return false;
1247 /* With equal precedence, both orders are ok, but it is better if the
1248 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1249 if (target == 0 || REG_P (target))
1250 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1251 else
1252 return rtx_equal_p (op1, target);
1255 /* Return true if BINOPTAB implements a shift operation. */
1257 static bool
1258 shift_optab_p (optab binoptab)
1260 switch (binoptab->code)
1262 case ASHIFT:
1263 case SS_ASHIFT:
1264 case US_ASHIFT:
1265 case ASHIFTRT:
1266 case LSHIFTRT:
1267 case ROTATE:
1268 case ROTATERT:
1269 return true;
1271 default:
1272 return false;
1276 /* Return true if BINOPTAB implements a commutative binary operation. */
1278 static bool
1279 commutative_optab_p (optab binoptab)
1281 return (GET_RTX_CLASS (binoptab->code) == RTX_COMM_ARITH
1282 || binoptab == smul_widen_optab
1283 || binoptab == umul_widen_optab
1284 || binoptab == smul_highpart_optab
1285 || binoptab == umul_highpart_optab);
1288 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1289 optimizing, and if the operand is a constant that costs more than
1290 1 instruction, force the constant into a register and return that
1291 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1293 static rtx
1294 avoid_expensive_constant (enum machine_mode mode, optab binoptab,
1295 int opn, rtx x, bool unsignedp)
1297 bool speed = optimize_insn_for_speed_p ();
1299 if (mode != VOIDmode
1300 && optimize
1301 && CONSTANT_P (x)
1302 && rtx_cost (x, binoptab->code, opn, speed) > set_src_cost (x, speed))
1304 if (CONST_INT_P (x))
1306 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1307 if (intval != INTVAL (x))
1308 x = GEN_INT (intval);
1310 else
1311 x = convert_modes (mode, VOIDmode, x, unsignedp);
1312 x = force_reg (mode, x);
1314 return x;
1317 /* Helper function for expand_binop: handle the case where there
1318 is an insn that directly implements the indicated operation.
1319 Returns null if this is not possible. */
1320 static rtx
1321 expand_binop_directly (enum machine_mode mode, optab binoptab,
1322 rtx op0, rtx op1,
1323 rtx target, int unsignedp, enum optab_methods methods,
1324 rtx last)
1326 enum machine_mode from_mode = widened_mode (mode, op0, op1);
1327 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1328 from_mode, 1);
1329 enum machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1330 enum machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1331 enum machine_mode mode0, mode1, tmp_mode;
1332 struct expand_operand ops[3];
1333 bool commutative_p;
1334 rtx pat;
1335 rtx xop0 = op0, xop1 = op1;
1336 rtx swap;
1338 /* If it is a commutative operator and the modes would match
1339 if we would swap the operands, we can save the conversions. */
1340 commutative_p = commutative_optab_p (binoptab);
1341 if (commutative_p
1342 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1343 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1345 swap = xop0;
1346 xop0 = xop1;
1347 xop1 = swap;
1350 /* If we are optimizing, force expensive constants into a register. */
1351 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1352 if (!shift_optab_p (binoptab))
1353 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1355 /* In case the insn wants input operands in modes different from
1356 those of the actual operands, convert the operands. It would
1357 seem that we don't need to convert CONST_INTs, but we do, so
1358 that they're properly zero-extended, sign-extended or truncated
1359 for their mode. */
1361 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1362 if (xmode0 != VOIDmode && xmode0 != mode0)
1364 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1365 mode0 = xmode0;
1368 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1369 if (xmode1 != VOIDmode && xmode1 != mode1)
1371 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1372 mode1 = xmode1;
1375 /* If operation is commutative,
1376 try to make the first operand a register.
1377 Even better, try to make it the same as the target.
1378 Also try to make the last operand a constant. */
1379 if (commutative_p
1380 && swap_commutative_operands_with_target (target, xop0, xop1))
1382 swap = xop1;
1383 xop1 = xop0;
1384 xop0 = swap;
1387 /* Now, if insn's predicates don't allow our operands, put them into
1388 pseudo regs. */
1390 if (binoptab == vec_pack_trunc_optab
1391 || binoptab == vec_pack_usat_optab
1392 || binoptab == vec_pack_ssat_optab
1393 || binoptab == vec_pack_ufix_trunc_optab
1394 || binoptab == vec_pack_sfix_trunc_optab)
1396 /* The mode of the result is different then the mode of the
1397 arguments. */
1398 tmp_mode = insn_data[(int) icode].operand[0].mode;
1399 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1401 delete_insns_since (last);
1402 return NULL_RTX;
1405 else
1406 tmp_mode = mode;
1408 create_output_operand (&ops[0], target, tmp_mode);
1409 create_input_operand (&ops[1], xop0, mode0);
1410 create_input_operand (&ops[2], xop1, mode1);
1411 pat = maybe_gen_insn (icode, 3, ops);
1412 if (pat)
1414 /* If PAT is composed of more than one insn, try to add an appropriate
1415 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1416 operand, call expand_binop again, this time without a target. */
1417 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1418 && ! add_equal_note (pat, ops[0].value, binoptab->code,
1419 ops[1].value, ops[2].value))
1421 delete_insns_since (last);
1422 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1423 unsignedp, methods);
1426 emit_insn (pat);
1427 return ops[0].value;
1429 delete_insns_since (last);
1430 return NULL_RTX;
1433 /* Generate code to perform an operation specified by BINOPTAB
1434 on operands OP0 and OP1, with result having machine-mode MODE.
1436 UNSIGNEDP is for the case where we have to widen the operands
1437 to perform the operation. It says to use zero-extension.
1439 If TARGET is nonzero, the value
1440 is generated there, if it is convenient to do so.
1441 In all cases an rtx is returned for the locus of the value;
1442 this may or may not be TARGET. */
1445 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
1446 rtx target, int unsignedp, enum optab_methods methods)
1448 enum optab_methods next_methods
1449 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1450 ? OPTAB_WIDEN : methods);
1451 enum mode_class mclass;
1452 enum machine_mode wider_mode;
1453 rtx libfunc;
1454 rtx temp;
1455 rtx entry_last = get_last_insn ();
1456 rtx last;
1458 mclass = GET_MODE_CLASS (mode);
1460 /* If subtracting an integer constant, convert this into an addition of
1461 the negated constant. */
1463 if (binoptab == sub_optab && CONST_INT_P (op1))
1465 op1 = negate_rtx (mode, op1);
1466 binoptab = add_optab;
1469 /* Record where to delete back to if we backtrack. */
1470 last = get_last_insn ();
1472 /* If we can do it with a three-operand insn, do so. */
1474 if (methods != OPTAB_MUST_WIDEN
1475 && find_widening_optab_handler (binoptab, mode,
1476 widened_mode (mode, op0, op1), 1)
1477 != CODE_FOR_nothing)
1479 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1480 unsignedp, methods, last);
1481 if (temp)
1482 return temp;
1485 /* If we were trying to rotate, and that didn't work, try rotating
1486 the other direction before falling back to shifts and bitwise-or. */
1487 if (((binoptab == rotl_optab
1488 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1489 || (binoptab == rotr_optab
1490 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1491 && mclass == MODE_INT)
1493 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1494 rtx newop1;
1495 unsigned int bits = GET_MODE_PRECISION (mode);
1497 if (CONST_INT_P (op1))
1498 newop1 = GEN_INT (bits - INTVAL (op1));
1499 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1500 newop1 = negate_rtx (GET_MODE (op1), op1);
1501 else
1502 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1503 GEN_INT (bits), op1,
1504 NULL_RTX, unsignedp, OPTAB_DIRECT);
1506 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1507 target, unsignedp, methods, last);
1508 if (temp)
1509 return temp;
1512 /* If this is a multiply, see if we can do a widening operation that
1513 takes operands of this mode and makes a wider mode. */
1515 if (binoptab == smul_optab
1516 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1517 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1518 : smul_widen_optab),
1519 GET_MODE_2XWIDER_MODE (mode), mode)
1520 != CODE_FOR_nothing))
1522 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1523 unsignedp ? umul_widen_optab : smul_widen_optab,
1524 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1526 if (temp != 0)
1528 if (GET_MODE_CLASS (mode) == MODE_INT
1529 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1530 return gen_lowpart (mode, temp);
1531 else
1532 return convert_to_mode (mode, temp, unsignedp);
1536 /* Look for a wider mode of the same class for which we think we
1537 can open-code the operation. Check for a widening multiply at the
1538 wider mode as well. */
1540 if (CLASS_HAS_WIDER_MODES_P (mclass)
1541 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1542 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1543 wider_mode != VOIDmode;
1544 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1546 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1547 || (binoptab == smul_optab
1548 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1549 && (find_widening_optab_handler ((unsignedp
1550 ? umul_widen_optab
1551 : smul_widen_optab),
1552 GET_MODE_WIDER_MODE (wider_mode),
1553 mode, 0)
1554 != CODE_FOR_nothing)))
1556 rtx xop0 = op0, xop1 = op1;
1557 int no_extend = 0;
1559 /* For certain integer operations, we need not actually extend
1560 the narrow operands, as long as we will truncate
1561 the results to the same narrowness. */
1563 if ((binoptab == ior_optab || binoptab == and_optab
1564 || binoptab == xor_optab
1565 || binoptab == add_optab || binoptab == sub_optab
1566 || binoptab == smul_optab || binoptab == ashl_optab)
1567 && mclass == MODE_INT)
1569 no_extend = 1;
1570 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1571 xop0, unsignedp);
1572 if (binoptab != ashl_optab)
1573 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1574 xop1, unsignedp);
1577 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1579 /* The second operand of a shift must always be extended. */
1580 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1581 no_extend && binoptab != ashl_optab);
1583 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1584 unsignedp, OPTAB_DIRECT);
1585 if (temp)
1587 if (mclass != MODE_INT
1588 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1590 if (target == 0)
1591 target = gen_reg_rtx (mode);
1592 convert_move (target, temp, 0);
1593 return target;
1595 else
1596 return gen_lowpart (mode, temp);
1598 else
1599 delete_insns_since (last);
1603 /* If operation is commutative,
1604 try to make the first operand a register.
1605 Even better, try to make it the same as the target.
1606 Also try to make the last operand a constant. */
1607 if (commutative_optab_p (binoptab)
1608 && swap_commutative_operands_with_target (target, op0, op1))
1610 temp = op1;
1611 op1 = op0;
1612 op0 = temp;
1615 /* These can be done a word at a time. */
1616 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1617 && mclass == MODE_INT
1618 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1619 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1621 int i;
1622 rtx insns;
1624 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1625 won't be accurate, so use a new target. */
1626 if (target == 0
1627 || target == op0
1628 || target == op1
1629 || !valid_multiword_target_p (target))
1630 target = gen_reg_rtx (mode);
1632 start_sequence ();
1634 /* Do the actual arithmetic. */
1635 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1637 rtx target_piece = operand_subword (target, i, 1, mode);
1638 rtx x = expand_binop (word_mode, binoptab,
1639 operand_subword_force (op0, i, mode),
1640 operand_subword_force (op1, i, mode),
1641 target_piece, unsignedp, next_methods);
1643 if (x == 0)
1644 break;
1646 if (target_piece != x)
1647 emit_move_insn (target_piece, x);
1650 insns = get_insns ();
1651 end_sequence ();
1653 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1655 emit_insn (insns);
1656 return target;
1660 /* Synthesize double word shifts from single word shifts. */
1661 if ((binoptab == lshr_optab || binoptab == ashl_optab
1662 || binoptab == ashr_optab)
1663 && mclass == MODE_INT
1664 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1665 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1666 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1667 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1668 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1669 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1671 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1672 enum machine_mode op1_mode;
1674 double_shift_mask = targetm.shift_truncation_mask (mode);
1675 shift_mask = targetm.shift_truncation_mask (word_mode);
1676 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1678 /* Apply the truncation to constant shifts. */
1679 if (double_shift_mask > 0 && CONST_INT_P (op1))
1680 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1682 if (op1 == CONST0_RTX (op1_mode))
1683 return op0;
1685 /* Make sure that this is a combination that expand_doubleword_shift
1686 can handle. See the comments there for details. */
1687 if (double_shift_mask == 0
1688 || (shift_mask == BITS_PER_WORD - 1
1689 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1691 rtx insns;
1692 rtx into_target, outof_target;
1693 rtx into_input, outof_input;
1694 int left_shift, outof_word;
1696 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1697 won't be accurate, so use a new target. */
1698 if (target == 0
1699 || target == op0
1700 || target == op1
1701 || !valid_multiword_target_p (target))
1702 target = gen_reg_rtx (mode);
1704 start_sequence ();
1706 /* OUTOF_* is the word we are shifting bits away from, and
1707 INTO_* is the word that we are shifting bits towards, thus
1708 they differ depending on the direction of the shift and
1709 WORDS_BIG_ENDIAN. */
1711 left_shift = binoptab == ashl_optab;
1712 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1714 outof_target = operand_subword (target, outof_word, 1, mode);
1715 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1717 outof_input = operand_subword_force (op0, outof_word, mode);
1718 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1720 if (expand_doubleword_shift (op1_mode, binoptab,
1721 outof_input, into_input, op1,
1722 outof_target, into_target,
1723 unsignedp, next_methods, shift_mask))
1725 insns = get_insns ();
1726 end_sequence ();
1728 emit_insn (insns);
1729 return target;
1731 end_sequence ();
1735 /* Synthesize double word rotates from single word shifts. */
1736 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1737 && mclass == MODE_INT
1738 && CONST_INT_P (op1)
1739 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1740 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1741 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1743 rtx insns;
1744 rtx into_target, outof_target;
1745 rtx into_input, outof_input;
1746 rtx inter;
1747 int shift_count, left_shift, outof_word;
1749 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1750 won't be accurate, so use a new target. Do this also if target is not
1751 a REG, first because having a register instead may open optimization
1752 opportunities, and second because if target and op0 happen to be MEMs
1753 designating the same location, we would risk clobbering it too early
1754 in the code sequence we generate below. */
1755 if (target == 0
1756 || target == op0
1757 || target == op1
1758 || !REG_P (target)
1759 || !valid_multiword_target_p (target))
1760 target = gen_reg_rtx (mode);
1762 start_sequence ();
1764 shift_count = INTVAL (op1);
1766 /* OUTOF_* is the word we are shifting bits away from, and
1767 INTO_* is the word that we are shifting bits towards, thus
1768 they differ depending on the direction of the shift and
1769 WORDS_BIG_ENDIAN. */
1771 left_shift = (binoptab == rotl_optab);
1772 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1774 outof_target = operand_subword (target, outof_word, 1, mode);
1775 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1777 outof_input = operand_subword_force (op0, outof_word, mode);
1778 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1780 if (shift_count == BITS_PER_WORD)
1782 /* This is just a word swap. */
1783 emit_move_insn (outof_target, into_input);
1784 emit_move_insn (into_target, outof_input);
1785 inter = const0_rtx;
1787 else
1789 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1790 rtx first_shift_count, second_shift_count;
1791 optab reverse_unsigned_shift, unsigned_shift;
1793 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1794 ? lshr_optab : ashl_optab);
1796 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1797 ? ashl_optab : lshr_optab);
1799 if (shift_count > BITS_PER_WORD)
1801 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1802 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1804 else
1806 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1807 second_shift_count = GEN_INT (shift_count);
1810 into_temp1 = expand_binop (word_mode, unsigned_shift,
1811 outof_input, first_shift_count,
1812 NULL_RTX, unsignedp, next_methods);
1813 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1814 into_input, second_shift_count,
1815 NULL_RTX, unsignedp, next_methods);
1817 if (into_temp1 != 0 && into_temp2 != 0)
1818 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1819 into_target, unsignedp, next_methods);
1820 else
1821 inter = 0;
1823 if (inter != 0 && inter != into_target)
1824 emit_move_insn (into_target, inter);
1826 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1827 into_input, first_shift_count,
1828 NULL_RTX, unsignedp, next_methods);
1829 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1830 outof_input, second_shift_count,
1831 NULL_RTX, unsignedp, next_methods);
1833 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1834 inter = expand_binop (word_mode, ior_optab,
1835 outof_temp1, outof_temp2,
1836 outof_target, unsignedp, next_methods);
1838 if (inter != 0 && inter != outof_target)
1839 emit_move_insn (outof_target, inter);
1842 insns = get_insns ();
1843 end_sequence ();
1845 if (inter != 0)
1847 emit_insn (insns);
1848 return target;
1852 /* These can be done a word at a time by propagating carries. */
1853 if ((binoptab == add_optab || binoptab == sub_optab)
1854 && mclass == MODE_INT
1855 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1856 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1858 unsigned int i;
1859 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1860 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1861 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1862 rtx xop0, xop1, xtarget;
1864 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1865 value is one of those, use it. Otherwise, use 1 since it is the
1866 one easiest to get. */
1867 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1868 int normalizep = STORE_FLAG_VALUE;
1869 #else
1870 int normalizep = 1;
1871 #endif
1873 /* Prepare the operands. */
1874 xop0 = force_reg (mode, op0);
1875 xop1 = force_reg (mode, op1);
1877 xtarget = gen_reg_rtx (mode);
1879 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1880 target = xtarget;
1882 /* Indicate for flow that the entire target reg is being set. */
1883 if (REG_P (target))
1884 emit_clobber (xtarget);
1886 /* Do the actual arithmetic. */
1887 for (i = 0; i < nwords; i++)
1889 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1890 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1891 rtx op0_piece = operand_subword_force (xop0, index, mode);
1892 rtx op1_piece = operand_subword_force (xop1, index, mode);
1893 rtx x;
1895 /* Main add/subtract of the input operands. */
1896 x = expand_binop (word_mode, binoptab,
1897 op0_piece, op1_piece,
1898 target_piece, unsignedp, next_methods);
1899 if (x == 0)
1900 break;
1902 if (i + 1 < nwords)
1904 /* Store carry from main add/subtract. */
1905 carry_out = gen_reg_rtx (word_mode);
1906 carry_out = emit_store_flag_force (carry_out,
1907 (binoptab == add_optab
1908 ? LT : GT),
1909 x, op0_piece,
1910 word_mode, 1, normalizep);
1913 if (i > 0)
1915 rtx newx;
1917 /* Add/subtract previous carry to main result. */
1918 newx = expand_binop (word_mode,
1919 normalizep == 1 ? binoptab : otheroptab,
1920 x, carry_in,
1921 NULL_RTX, 1, next_methods);
1923 if (i + 1 < nwords)
1925 /* Get out carry from adding/subtracting carry in. */
1926 rtx carry_tmp = gen_reg_rtx (word_mode);
1927 carry_tmp = emit_store_flag_force (carry_tmp,
1928 (binoptab == add_optab
1929 ? LT : GT),
1930 newx, x,
1931 word_mode, 1, normalizep);
1933 /* Logical-ior the two poss. carry together. */
1934 carry_out = expand_binop (word_mode, ior_optab,
1935 carry_out, carry_tmp,
1936 carry_out, 0, next_methods);
1937 if (carry_out == 0)
1938 break;
1940 emit_move_insn (target_piece, newx);
1942 else
1944 if (x != target_piece)
1945 emit_move_insn (target_piece, x);
1948 carry_in = carry_out;
1951 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
1953 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
1954 || ! rtx_equal_p (target, xtarget))
1956 rtx temp = emit_move_insn (target, xtarget);
1958 set_unique_reg_note (temp,
1959 REG_EQUAL,
1960 gen_rtx_fmt_ee (binoptab->code, mode,
1961 copy_rtx (xop0),
1962 copy_rtx (xop1)));
1964 else
1965 target = xtarget;
1967 return target;
1970 else
1971 delete_insns_since (last);
1974 /* Attempt to synthesize double word multiplies using a sequence of word
1975 mode multiplications. We first attempt to generate a sequence using a
1976 more efficient unsigned widening multiply, and if that fails we then
1977 try using a signed widening multiply. */
1979 if (binoptab == smul_optab
1980 && mclass == MODE_INT
1981 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1982 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
1983 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
1985 rtx product = NULL_RTX;
1986 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
1987 != CODE_FOR_nothing)
1989 product = expand_doubleword_mult (mode, op0, op1, target,
1990 true, methods);
1991 if (!product)
1992 delete_insns_since (last);
1995 if (product == NULL_RTX
1996 && widening_optab_handler (smul_widen_optab, mode, word_mode)
1997 != CODE_FOR_nothing)
1999 product = expand_doubleword_mult (mode, op0, op1, target,
2000 false, methods);
2001 if (!product)
2002 delete_insns_since (last);
2005 if (product != NULL_RTX)
2007 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2009 temp = emit_move_insn (target ? target : product, product);
2010 set_unique_reg_note (temp,
2011 REG_EQUAL,
2012 gen_rtx_fmt_ee (MULT, mode,
2013 copy_rtx (op0),
2014 copy_rtx (op1)));
2016 return product;
2020 /* It can't be open-coded in this mode.
2021 Use a library call if one is available and caller says that's ok. */
2023 libfunc = optab_libfunc (binoptab, mode);
2024 if (libfunc
2025 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2027 rtx insns;
2028 rtx op1x = op1;
2029 enum machine_mode op1_mode = mode;
2030 rtx value;
2032 start_sequence ();
2034 if (shift_optab_p (binoptab))
2036 op1_mode = targetm.libgcc_shift_count_mode ();
2037 /* Specify unsigned here,
2038 since negative shift counts are meaningless. */
2039 op1x = convert_to_mode (op1_mode, op1, 1);
2042 if (GET_MODE (op0) != VOIDmode
2043 && GET_MODE (op0) != mode)
2044 op0 = convert_to_mode (mode, op0, unsignedp);
2046 /* Pass 1 for NO_QUEUE so we don't lose any increments
2047 if the libcall is cse'd or moved. */
2048 value = emit_library_call_value (libfunc,
2049 NULL_RTX, LCT_CONST, mode, 2,
2050 op0, mode, op1x, op1_mode);
2052 insns = get_insns ();
2053 end_sequence ();
2055 target = gen_reg_rtx (mode);
2056 emit_libcall_block (insns, target, value,
2057 gen_rtx_fmt_ee (binoptab->code, mode, op0, op1));
2059 return target;
2062 delete_insns_since (last);
2064 /* It can't be done in this mode. Can we do it in a wider mode? */
2066 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2067 || methods == OPTAB_MUST_WIDEN))
2069 /* Caller says, don't even try. */
2070 delete_insns_since (entry_last);
2071 return 0;
2074 /* Compute the value of METHODS to pass to recursive calls.
2075 Don't allow widening to be tried recursively. */
2077 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2079 /* Look for a wider mode of the same class for which it appears we can do
2080 the operation. */
2082 if (CLASS_HAS_WIDER_MODES_P (mclass))
2084 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2085 wider_mode != VOIDmode;
2086 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2088 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2089 != CODE_FOR_nothing
2090 || (methods == OPTAB_LIB
2091 && optab_libfunc (binoptab, wider_mode)))
2093 rtx xop0 = op0, xop1 = op1;
2094 int no_extend = 0;
2096 /* For certain integer operations, we need not actually extend
2097 the narrow operands, as long as we will truncate
2098 the results to the same narrowness. */
2100 if ((binoptab == ior_optab || binoptab == and_optab
2101 || binoptab == xor_optab
2102 || binoptab == add_optab || binoptab == sub_optab
2103 || binoptab == smul_optab || binoptab == ashl_optab)
2104 && mclass == MODE_INT)
2105 no_extend = 1;
2107 xop0 = widen_operand (xop0, wider_mode, mode,
2108 unsignedp, no_extend);
2110 /* The second operand of a shift must always be extended. */
2111 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2112 no_extend && binoptab != ashl_optab);
2114 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2115 unsignedp, methods);
2116 if (temp)
2118 if (mclass != MODE_INT
2119 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2121 if (target == 0)
2122 target = gen_reg_rtx (mode);
2123 convert_move (target, temp, 0);
2124 return target;
2126 else
2127 return gen_lowpart (mode, temp);
2129 else
2130 delete_insns_since (last);
2135 delete_insns_since (entry_last);
2136 return 0;
2139 /* Expand a binary operator which has both signed and unsigned forms.
2140 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2141 signed operations.
2143 If we widen unsigned operands, we may use a signed wider operation instead
2144 of an unsigned wider operation, since the result would be the same. */
2147 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
2148 rtx op0, rtx op1, rtx target, int unsignedp,
2149 enum optab_methods methods)
2151 rtx temp;
2152 optab direct_optab = unsignedp ? uoptab : soptab;
2153 struct optab_d wide_soptab;
2155 /* Do it without widening, if possible. */
2156 temp = expand_binop (mode, direct_optab, op0, op1, target,
2157 unsignedp, OPTAB_DIRECT);
2158 if (temp || methods == OPTAB_DIRECT)
2159 return temp;
2161 /* Try widening to a signed int. Make a fake signed optab that
2162 hides any signed insn for direct use. */
2163 wide_soptab = *soptab;
2164 set_optab_handler (&wide_soptab, mode, CODE_FOR_nothing);
2165 /* We don't want to generate new hash table entries from this fake
2166 optab. */
2167 wide_soptab.libcall_gen = NULL;
2169 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2170 unsignedp, OPTAB_WIDEN);
2172 /* For unsigned operands, try widening to an unsigned int. */
2173 if (temp == 0 && unsignedp)
2174 temp = expand_binop (mode, uoptab, op0, op1, target,
2175 unsignedp, OPTAB_WIDEN);
2176 if (temp || methods == OPTAB_WIDEN)
2177 return temp;
2179 /* Use the right width libcall if that exists. */
2180 temp = expand_binop (mode, direct_optab, op0, op1, target, unsignedp, OPTAB_LIB);
2181 if (temp || methods == OPTAB_LIB)
2182 return temp;
2184 /* Must widen and use a libcall, use either signed or unsigned. */
2185 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2186 unsignedp, methods);
2187 if (temp != 0)
2188 return temp;
2189 if (unsignedp)
2190 return expand_binop (mode, uoptab, op0, op1, target,
2191 unsignedp, methods);
2192 return 0;
2195 /* Generate code to perform an operation specified by UNOPPTAB
2196 on operand OP0, with two results to TARG0 and TARG1.
2197 We assume that the order of the operands for the instruction
2198 is TARG0, TARG1, OP0.
2200 Either TARG0 or TARG1 may be zero, but what that means is that
2201 the result is not actually wanted. We will generate it into
2202 a dummy pseudo-reg and discard it. They may not both be zero.
2204 Returns 1 if this operation can be performed; 0 if not. */
2207 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2208 int unsignedp)
2210 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2211 enum mode_class mclass;
2212 enum machine_mode wider_mode;
2213 rtx entry_last = get_last_insn ();
2214 rtx last;
2216 mclass = GET_MODE_CLASS (mode);
2218 if (!targ0)
2219 targ0 = gen_reg_rtx (mode);
2220 if (!targ1)
2221 targ1 = gen_reg_rtx (mode);
2223 /* Record where to go back to if we fail. */
2224 last = get_last_insn ();
2226 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2228 struct expand_operand ops[3];
2229 enum insn_code icode = optab_handler (unoptab, mode);
2231 create_fixed_operand (&ops[0], targ0);
2232 create_fixed_operand (&ops[1], targ1);
2233 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2234 if (maybe_expand_insn (icode, 3, ops))
2235 return 1;
2238 /* It can't be done in this mode. Can we do it in a wider mode? */
2240 if (CLASS_HAS_WIDER_MODES_P (mclass))
2242 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2243 wider_mode != VOIDmode;
2244 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2246 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2248 rtx t0 = gen_reg_rtx (wider_mode);
2249 rtx t1 = gen_reg_rtx (wider_mode);
2250 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2252 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2254 convert_move (targ0, t0, unsignedp);
2255 convert_move (targ1, t1, unsignedp);
2256 return 1;
2258 else
2259 delete_insns_since (last);
2264 delete_insns_since (entry_last);
2265 return 0;
2268 /* Generate code to perform an operation specified by BINOPTAB
2269 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2270 We assume that the order of the operands for the instruction
2271 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2272 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2274 Either TARG0 or TARG1 may be zero, but what that means is that
2275 the result is not actually wanted. We will generate it into
2276 a dummy pseudo-reg and discard it. They may not both be zero.
2278 Returns 1 if this operation can be performed; 0 if not. */
2281 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2282 int unsignedp)
2284 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2285 enum mode_class mclass;
2286 enum machine_mode wider_mode;
2287 rtx entry_last = get_last_insn ();
2288 rtx last;
2290 mclass = GET_MODE_CLASS (mode);
2292 if (!targ0)
2293 targ0 = gen_reg_rtx (mode);
2294 if (!targ1)
2295 targ1 = gen_reg_rtx (mode);
2297 /* Record where to go back to if we fail. */
2298 last = get_last_insn ();
2300 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2302 struct expand_operand ops[4];
2303 enum insn_code icode = optab_handler (binoptab, mode);
2304 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2305 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
2306 rtx xop0 = op0, xop1 = op1;
2308 /* If we are optimizing, force expensive constants into a register. */
2309 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2310 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2312 create_fixed_operand (&ops[0], targ0);
2313 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2314 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2315 create_fixed_operand (&ops[3], targ1);
2316 if (maybe_expand_insn (icode, 4, ops))
2317 return 1;
2318 delete_insns_since (last);
2321 /* It can't be done in this mode. Can we do it in a wider mode? */
2323 if (CLASS_HAS_WIDER_MODES_P (mclass))
2325 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2326 wider_mode != VOIDmode;
2327 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2329 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2331 rtx t0 = gen_reg_rtx (wider_mode);
2332 rtx t1 = gen_reg_rtx (wider_mode);
2333 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2334 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2336 if (expand_twoval_binop (binoptab, cop0, cop1,
2337 t0, t1, unsignedp))
2339 convert_move (targ0, t0, unsignedp);
2340 convert_move (targ1, t1, unsignedp);
2341 return 1;
2343 else
2344 delete_insns_since (last);
2349 delete_insns_since (entry_last);
2350 return 0;
2353 /* Expand the two-valued library call indicated by BINOPTAB, but
2354 preserve only one of the values. If TARG0 is non-NULL, the first
2355 value is placed into TARG0; otherwise the second value is placed
2356 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2357 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2358 This routine assumes that the value returned by the library call is
2359 as if the return value was of an integral mode twice as wide as the
2360 mode of OP0. Returns 1 if the call was successful. */
2362 bool
2363 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2364 rtx targ0, rtx targ1, enum rtx_code code)
2366 enum machine_mode mode;
2367 enum machine_mode libval_mode;
2368 rtx libval;
2369 rtx insns;
2370 rtx libfunc;
2372 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2373 gcc_assert (!targ0 != !targ1);
2375 mode = GET_MODE (op0);
2376 libfunc = optab_libfunc (binoptab, mode);
2377 if (!libfunc)
2378 return false;
2380 /* The value returned by the library function will have twice as
2381 many bits as the nominal MODE. */
2382 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2383 MODE_INT);
2384 start_sequence ();
2385 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2386 libval_mode, 2,
2387 op0, mode,
2388 op1, mode);
2389 /* Get the part of VAL containing the value that we want. */
2390 libval = simplify_gen_subreg (mode, libval, libval_mode,
2391 targ0 ? 0 : GET_MODE_SIZE (mode));
2392 insns = get_insns ();
2393 end_sequence ();
2394 /* Move the into the desired location. */
2395 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2396 gen_rtx_fmt_ee (code, mode, op0, op1));
2398 return true;
2402 /* Wrapper around expand_unop which takes an rtx code to specify
2403 the operation to perform, not an optab pointer. All other
2404 arguments are the same. */
2406 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2407 rtx target, int unsignedp)
2409 optab unop = code_to_optab[(int) code];
2410 gcc_assert (unop);
2412 return expand_unop (mode, unop, op0, target, unsignedp);
2415 /* Try calculating
2416 (clz:narrow x)
2418 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2420 A similar operation can be used for clrsb. UNOPTAB says which operation
2421 we are trying to expand. */
2422 static rtx
2423 widen_leading (enum machine_mode mode, rtx op0, rtx target, optab unoptab)
2425 enum mode_class mclass = GET_MODE_CLASS (mode);
2426 if (CLASS_HAS_WIDER_MODES_P (mclass))
2428 enum machine_mode wider_mode;
2429 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2430 wider_mode != VOIDmode;
2431 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2433 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2435 rtx xop0, temp, last;
2437 last = get_last_insn ();
2439 if (target == 0)
2440 target = gen_reg_rtx (mode);
2441 xop0 = widen_operand (op0, wider_mode, mode,
2442 unoptab != clrsb_optab, false);
2443 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2444 unoptab != clrsb_optab);
2445 if (temp != 0)
2446 temp = expand_binop (wider_mode, sub_optab, temp,
2447 GEN_INT (GET_MODE_PRECISION (wider_mode)
2448 - GET_MODE_PRECISION (mode)),
2449 target, true, OPTAB_DIRECT);
2450 if (temp == 0)
2451 delete_insns_since (last);
2453 return temp;
2457 return 0;
2460 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2461 quantities, choosing which based on whether the high word is nonzero. */
2462 static rtx
2463 expand_doubleword_clz (enum machine_mode mode, rtx op0, rtx target)
2465 rtx xop0 = force_reg (mode, op0);
2466 rtx subhi = gen_highpart (word_mode, xop0);
2467 rtx sublo = gen_lowpart (word_mode, xop0);
2468 rtx hi0_label = gen_label_rtx ();
2469 rtx after_label = gen_label_rtx ();
2470 rtx seq, temp, result;
2472 /* If we were not given a target, use a word_mode register, not a
2473 'mode' register. The result will fit, and nobody is expecting
2474 anything bigger (the return type of __builtin_clz* is int). */
2475 if (!target)
2476 target = gen_reg_rtx (word_mode);
2478 /* In any case, write to a word_mode scratch in both branches of the
2479 conditional, so we can ensure there is a single move insn setting
2480 'target' to tag a REG_EQUAL note on. */
2481 result = gen_reg_rtx (word_mode);
2483 start_sequence ();
2485 /* If the high word is not equal to zero,
2486 then clz of the full value is clz of the high word. */
2487 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2488 word_mode, true, hi0_label);
2490 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2491 if (!temp)
2492 goto fail;
2494 if (temp != result)
2495 convert_move (result, temp, true);
2497 emit_jump_insn (gen_jump (after_label));
2498 emit_barrier ();
2500 /* Else clz of the full value is clz of the low word plus the number
2501 of bits in the high word. */
2502 emit_label (hi0_label);
2504 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2505 if (!temp)
2506 goto fail;
2507 temp = expand_binop (word_mode, add_optab, temp,
2508 GEN_INT (GET_MODE_BITSIZE (word_mode)),
2509 result, true, OPTAB_DIRECT);
2510 if (!temp)
2511 goto fail;
2512 if (temp != result)
2513 convert_move (result, temp, true);
2515 emit_label (after_label);
2516 convert_move (target, result, true);
2518 seq = get_insns ();
2519 end_sequence ();
2521 add_equal_note (seq, target, CLZ, xop0, 0);
2522 emit_insn (seq);
2523 return target;
2525 fail:
2526 end_sequence ();
2527 return 0;
2530 /* Try calculating
2531 (bswap:narrow x)
2533 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2534 static rtx
2535 widen_bswap (enum machine_mode mode, rtx op0, rtx target)
2537 enum mode_class mclass = GET_MODE_CLASS (mode);
2538 enum machine_mode wider_mode;
2539 rtx x, last;
2541 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2542 return NULL_RTX;
2544 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2545 wider_mode != VOIDmode;
2546 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2547 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2548 goto found;
2549 return NULL_RTX;
2551 found:
2552 last = get_last_insn ();
2554 x = widen_operand (op0, wider_mode, mode, true, true);
2555 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2557 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2558 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2559 if (x != 0)
2560 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2561 GET_MODE_BITSIZE (wider_mode)
2562 - GET_MODE_BITSIZE (mode),
2563 NULL_RTX, true);
2565 if (x != 0)
2567 if (target == 0)
2568 target = gen_reg_rtx (mode);
2569 emit_move_insn (target, gen_lowpart (mode, x));
2571 else
2572 delete_insns_since (last);
2574 return target;
2577 /* Try calculating bswap as two bswaps of two word-sized operands. */
2579 static rtx
2580 expand_doubleword_bswap (enum machine_mode mode, rtx op, rtx target)
2582 rtx t0, t1;
2584 t1 = expand_unop (word_mode, bswap_optab,
2585 operand_subword_force (op, 0, mode), NULL_RTX, true);
2586 t0 = expand_unop (word_mode, bswap_optab,
2587 operand_subword_force (op, 1, mode), NULL_RTX, true);
2589 if (target == 0 || !valid_multiword_target_p (target))
2590 target = gen_reg_rtx (mode);
2591 if (REG_P (target))
2592 emit_clobber (target);
2593 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2594 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2596 return target;
2599 /* Try calculating (parity x) as (and (popcount x) 1), where
2600 popcount can also be done in a wider mode. */
2601 static rtx
2602 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2604 enum mode_class mclass = GET_MODE_CLASS (mode);
2605 if (CLASS_HAS_WIDER_MODES_P (mclass))
2607 enum machine_mode wider_mode;
2608 for (wider_mode = mode; wider_mode != VOIDmode;
2609 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2611 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2613 rtx xop0, temp, last;
2615 last = get_last_insn ();
2617 if (target == 0)
2618 target = gen_reg_rtx (mode);
2619 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2620 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2621 true);
2622 if (temp != 0)
2623 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2624 target, true, OPTAB_DIRECT);
2625 if (temp == 0)
2626 delete_insns_since (last);
2628 return temp;
2632 return 0;
2635 /* Try calculating ctz(x) as K - clz(x & -x) ,
2636 where K is GET_MODE_PRECISION(mode) - 1.
2638 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2639 don't have to worry about what the hardware does in that case. (If
2640 the clz instruction produces the usual value at 0, which is K, the
2641 result of this code sequence will be -1; expand_ffs, below, relies
2642 on this. It might be nice to have it be K instead, for consistency
2643 with the (very few) processors that provide a ctz with a defined
2644 value, but that would take one more instruction, and it would be
2645 less convenient for expand_ffs anyway. */
2647 static rtx
2648 expand_ctz (enum machine_mode mode, rtx op0, rtx target)
2650 rtx seq, temp;
2652 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2653 return 0;
2655 start_sequence ();
2657 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2658 if (temp)
2659 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2660 true, OPTAB_DIRECT);
2661 if (temp)
2662 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2663 if (temp)
2664 temp = expand_binop (mode, sub_optab, GEN_INT (GET_MODE_PRECISION (mode) - 1),
2665 temp, target,
2666 true, OPTAB_DIRECT);
2667 if (temp == 0)
2669 end_sequence ();
2670 return 0;
2673 seq = get_insns ();
2674 end_sequence ();
2676 add_equal_note (seq, temp, CTZ, op0, 0);
2677 emit_insn (seq);
2678 return temp;
2682 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2683 else with the sequence used by expand_clz.
2685 The ffs builtin promises to return zero for a zero value and ctz/clz
2686 may have an undefined value in that case. If they do not give us a
2687 convenient value, we have to generate a test and branch. */
2688 static rtx
2689 expand_ffs (enum machine_mode mode, rtx op0, rtx target)
2691 HOST_WIDE_INT val = 0;
2692 bool defined_at_zero = false;
2693 rtx temp, seq;
2695 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2697 start_sequence ();
2699 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2700 if (!temp)
2701 goto fail;
2703 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2705 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2707 start_sequence ();
2708 temp = expand_ctz (mode, op0, 0);
2709 if (!temp)
2710 goto fail;
2712 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2714 defined_at_zero = true;
2715 val = (GET_MODE_PRECISION (mode) - 1) - val;
2718 else
2719 return 0;
2721 if (defined_at_zero && val == -1)
2722 /* No correction needed at zero. */;
2723 else
2725 /* We don't try to do anything clever with the situation found
2726 on some processors (eg Alpha) where ctz(0:mode) ==
2727 bitsize(mode). If someone can think of a way to send N to -1
2728 and leave alone all values in the range 0..N-1 (where N is a
2729 power of two), cheaper than this test-and-branch, please add it.
2731 The test-and-branch is done after the operation itself, in case
2732 the operation sets condition codes that can be recycled for this.
2733 (This is true on i386, for instance.) */
2735 rtx nonzero_label = gen_label_rtx ();
2736 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2737 mode, true, nonzero_label);
2739 convert_move (temp, GEN_INT (-1), false);
2740 emit_label (nonzero_label);
2743 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2744 to produce a value in the range 0..bitsize. */
2745 temp = expand_binop (mode, add_optab, temp, GEN_INT (1),
2746 target, false, OPTAB_DIRECT);
2747 if (!temp)
2748 goto fail;
2750 seq = get_insns ();
2751 end_sequence ();
2753 add_equal_note (seq, temp, FFS, op0, 0);
2754 emit_insn (seq);
2755 return temp;
2757 fail:
2758 end_sequence ();
2759 return 0;
2762 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2763 conditions, VAL may already be a SUBREG against which we cannot generate
2764 a further SUBREG. In this case, we expect forcing the value into a
2765 register will work around the situation. */
2767 static rtx
2768 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2769 enum machine_mode imode)
2771 rtx ret;
2772 ret = lowpart_subreg (omode, val, imode);
2773 if (ret == NULL)
2775 val = force_reg (imode, val);
2776 ret = lowpart_subreg (omode, val, imode);
2777 gcc_assert (ret != NULL);
2779 return ret;
2782 /* Expand a floating point absolute value or negation operation via a
2783 logical operation on the sign bit. */
2785 static rtx
2786 expand_absneg_bit (enum rtx_code code, enum machine_mode mode,
2787 rtx op0, rtx target)
2789 const struct real_format *fmt;
2790 int bitpos, word, nwords, i;
2791 enum machine_mode imode;
2792 double_int mask;
2793 rtx temp, insns;
2795 /* The format has to have a simple sign bit. */
2796 fmt = REAL_MODE_FORMAT (mode);
2797 if (fmt == NULL)
2798 return NULL_RTX;
2800 bitpos = fmt->signbit_rw;
2801 if (bitpos < 0)
2802 return NULL_RTX;
2804 /* Don't create negative zeros if the format doesn't support them. */
2805 if (code == NEG && !fmt->has_signed_zero)
2806 return NULL_RTX;
2808 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2810 imode = int_mode_for_mode (mode);
2811 if (imode == BLKmode)
2812 return NULL_RTX;
2813 word = 0;
2814 nwords = 1;
2816 else
2818 imode = word_mode;
2820 if (FLOAT_WORDS_BIG_ENDIAN)
2821 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2822 else
2823 word = bitpos / BITS_PER_WORD;
2824 bitpos = bitpos % BITS_PER_WORD;
2825 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2828 mask = double_int_setbit (double_int_zero, bitpos);
2829 if (code == ABS)
2830 mask = double_int_not (mask);
2832 if (target == 0
2833 || target == op0
2834 || (nwords > 1 && !valid_multiword_target_p (target)))
2835 target = gen_reg_rtx (mode);
2837 if (nwords > 1)
2839 start_sequence ();
2841 for (i = 0; i < nwords; ++i)
2843 rtx targ_piece = operand_subword (target, i, 1, mode);
2844 rtx op0_piece = operand_subword_force (op0, i, mode);
2846 if (i == word)
2848 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2849 op0_piece,
2850 immed_double_int_const (mask, imode),
2851 targ_piece, 1, OPTAB_LIB_WIDEN);
2852 if (temp != targ_piece)
2853 emit_move_insn (targ_piece, temp);
2855 else
2856 emit_move_insn (targ_piece, op0_piece);
2859 insns = get_insns ();
2860 end_sequence ();
2862 emit_insn (insns);
2864 else
2866 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2867 gen_lowpart (imode, op0),
2868 immed_double_int_const (mask, imode),
2869 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2870 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2872 set_unique_reg_note (get_last_insn (), REG_EQUAL,
2873 gen_rtx_fmt_e (code, mode, copy_rtx (op0)));
2876 return target;
2879 /* As expand_unop, but will fail rather than attempt the operation in a
2880 different mode or with a libcall. */
2881 static rtx
2882 expand_unop_direct (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2883 int unsignedp)
2885 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2887 struct expand_operand ops[2];
2888 enum insn_code icode = optab_handler (unoptab, mode);
2889 rtx last = get_last_insn ();
2890 rtx pat;
2892 create_output_operand (&ops[0], target, mode);
2893 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2894 pat = maybe_gen_insn (icode, 2, ops);
2895 if (pat)
2897 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2898 && ! add_equal_note (pat, ops[0].value, unoptab->code,
2899 ops[1].value, NULL_RTX))
2901 delete_insns_since (last);
2902 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
2905 emit_insn (pat);
2907 return ops[0].value;
2910 return 0;
2913 /* Generate code to perform an operation specified by UNOPTAB
2914 on operand OP0, with result having machine-mode MODE.
2916 UNSIGNEDP is for the case where we have to widen the operands
2917 to perform the operation. It says to use zero-extension.
2919 If TARGET is nonzero, the value
2920 is generated there, if it is convenient to do so.
2921 In all cases an rtx is returned for the locus of the value;
2922 this may or may not be TARGET. */
2925 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2926 int unsignedp)
2928 enum mode_class mclass = GET_MODE_CLASS (mode);
2929 enum machine_mode wider_mode;
2930 rtx temp;
2931 rtx libfunc;
2933 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
2934 if (temp)
2935 return temp;
2937 /* It can't be done in this mode. Can we open-code it in a wider mode? */
2939 /* Widening (or narrowing) clz needs special treatment. */
2940 if (unoptab == clz_optab)
2942 temp = widen_leading (mode, op0, target, unoptab);
2943 if (temp)
2944 return temp;
2946 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2947 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2949 temp = expand_doubleword_clz (mode, op0, target);
2950 if (temp)
2951 return temp;
2954 goto try_libcall;
2957 if (unoptab == clrsb_optab)
2959 temp = widen_leading (mode, op0, target, unoptab);
2960 if (temp)
2961 return temp;
2962 goto try_libcall;
2965 /* Widening (or narrowing) bswap needs special treatment. */
2966 if (unoptab == bswap_optab)
2968 temp = widen_bswap (mode, op0, target);
2969 if (temp)
2970 return temp;
2972 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2973 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
2975 temp = expand_doubleword_bswap (mode, op0, target);
2976 if (temp)
2977 return temp;
2980 goto try_libcall;
2983 if (CLASS_HAS_WIDER_MODES_P (mclass))
2984 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2985 wider_mode != VOIDmode;
2986 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2988 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2990 rtx xop0 = op0;
2991 rtx last = get_last_insn ();
2993 /* For certain operations, we need not actually extend
2994 the narrow operand, as long as we will truncate the
2995 results to the same narrowness. */
2997 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
2998 (unoptab == neg_optab
2999 || unoptab == one_cmpl_optab)
3000 && mclass == MODE_INT);
3002 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3003 unsignedp);
3005 if (temp)
3007 if (mclass != MODE_INT
3008 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3010 if (target == 0)
3011 target = gen_reg_rtx (mode);
3012 convert_move (target, temp, 0);
3013 return target;
3015 else
3016 return gen_lowpart (mode, temp);
3018 else
3019 delete_insns_since (last);
3023 /* These can be done a word at a time. */
3024 if (unoptab == one_cmpl_optab
3025 && mclass == MODE_INT
3026 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3027 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3029 int i;
3030 rtx insns;
3032 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3033 target = gen_reg_rtx (mode);
3035 start_sequence ();
3037 /* Do the actual arithmetic. */
3038 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3040 rtx target_piece = operand_subword (target, i, 1, mode);
3041 rtx x = expand_unop (word_mode, unoptab,
3042 operand_subword_force (op0, i, mode),
3043 target_piece, unsignedp);
3045 if (target_piece != x)
3046 emit_move_insn (target_piece, x);
3049 insns = get_insns ();
3050 end_sequence ();
3052 emit_insn (insns);
3053 return target;
3056 if (unoptab->code == NEG)
3058 /* Try negating floating point values by flipping the sign bit. */
3059 if (SCALAR_FLOAT_MODE_P (mode))
3061 temp = expand_absneg_bit (NEG, mode, op0, target);
3062 if (temp)
3063 return temp;
3066 /* If there is no negation pattern, and we have no negative zero,
3067 try subtracting from zero. */
3068 if (!HONOR_SIGNED_ZEROS (mode))
3070 temp = expand_binop (mode, (unoptab == negv_optab
3071 ? subv_optab : sub_optab),
3072 CONST0_RTX (mode), op0, target,
3073 unsignedp, OPTAB_DIRECT);
3074 if (temp)
3075 return temp;
3079 /* Try calculating parity (x) as popcount (x) % 2. */
3080 if (unoptab == parity_optab)
3082 temp = expand_parity (mode, op0, target);
3083 if (temp)
3084 return temp;
3087 /* Try implementing ffs (x) in terms of clz (x). */
3088 if (unoptab == ffs_optab)
3090 temp = expand_ffs (mode, op0, target);
3091 if (temp)
3092 return temp;
3095 /* Try implementing ctz (x) in terms of clz (x). */
3096 if (unoptab == ctz_optab)
3098 temp = expand_ctz (mode, op0, target);
3099 if (temp)
3100 return temp;
3103 try_libcall:
3104 /* Now try a library call in this mode. */
3105 libfunc = optab_libfunc (unoptab, mode);
3106 if (libfunc)
3108 rtx insns;
3109 rtx value;
3110 rtx eq_value;
3111 enum machine_mode outmode = mode;
3113 /* All of these functions return small values. Thus we choose to
3114 have them return something that isn't a double-word. */
3115 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3116 || unoptab == clrsb_optab || unoptab == popcount_optab
3117 || unoptab == parity_optab)
3118 outmode
3119 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3120 optab_libfunc (unoptab, mode)));
3122 start_sequence ();
3124 /* Pass 1 for NO_QUEUE so we don't lose any increments
3125 if the libcall is cse'd or moved. */
3126 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3127 1, op0, mode);
3128 insns = get_insns ();
3129 end_sequence ();
3131 target = gen_reg_rtx (outmode);
3132 eq_value = gen_rtx_fmt_e (unoptab->code, mode, op0);
3133 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3134 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3135 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3136 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3137 emit_libcall_block (insns, target, value, eq_value);
3139 return target;
3142 /* It can't be done in this mode. Can we do it in a wider mode? */
3144 if (CLASS_HAS_WIDER_MODES_P (mclass))
3146 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3147 wider_mode != VOIDmode;
3148 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3150 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3151 || optab_libfunc (unoptab, wider_mode))
3153 rtx xop0 = op0;
3154 rtx last = get_last_insn ();
3156 /* For certain operations, we need not actually extend
3157 the narrow operand, as long as we will truncate the
3158 results to the same narrowness. */
3160 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3161 (unoptab == neg_optab
3162 || unoptab == one_cmpl_optab)
3163 && mclass == MODE_INT);
3165 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3166 unsignedp);
3168 /* If we are generating clz using wider mode, adjust the
3169 result. Similarly for clrsb. */
3170 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3171 && temp != 0)
3172 temp = expand_binop (wider_mode, sub_optab, temp,
3173 GEN_INT (GET_MODE_PRECISION (wider_mode)
3174 - GET_MODE_PRECISION (mode)),
3175 target, true, OPTAB_DIRECT);
3177 if (temp)
3179 if (mclass != MODE_INT)
3181 if (target == 0)
3182 target = gen_reg_rtx (mode);
3183 convert_move (target, temp, 0);
3184 return target;
3186 else
3187 return gen_lowpart (mode, temp);
3189 else
3190 delete_insns_since (last);
3195 /* One final attempt at implementing negation via subtraction,
3196 this time allowing widening of the operand. */
3197 if (unoptab->code == NEG && !HONOR_SIGNED_ZEROS (mode))
3199 rtx temp;
3200 temp = expand_binop (mode,
3201 unoptab == negv_optab ? subv_optab : sub_optab,
3202 CONST0_RTX (mode), op0,
3203 target, unsignedp, OPTAB_LIB_WIDEN);
3204 if (temp)
3205 return temp;
3208 return 0;
3211 /* Emit code to compute the absolute value of OP0, with result to
3212 TARGET if convenient. (TARGET may be 0.) The return value says
3213 where the result actually is to be found.
3215 MODE is the mode of the operand; the mode of the result is
3216 different but can be deduced from MODE.
3221 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
3222 int result_unsignedp)
3224 rtx temp;
3226 if (! flag_trapv)
3227 result_unsignedp = 1;
3229 /* First try to do it with a special abs instruction. */
3230 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3231 op0, target, 0);
3232 if (temp != 0)
3233 return temp;
3235 /* For floating point modes, try clearing the sign bit. */
3236 if (SCALAR_FLOAT_MODE_P (mode))
3238 temp = expand_absneg_bit (ABS, mode, op0, target);
3239 if (temp)
3240 return temp;
3243 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3244 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3245 && !HONOR_SIGNED_ZEROS (mode))
3247 rtx last = get_last_insn ();
3249 temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
3250 if (temp != 0)
3251 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3252 OPTAB_WIDEN);
3254 if (temp != 0)
3255 return temp;
3257 delete_insns_since (last);
3260 /* If this machine has expensive jumps, we can do integer absolute
3261 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3262 where W is the width of MODE. */
3264 if (GET_MODE_CLASS (mode) == MODE_INT
3265 && BRANCH_COST (optimize_insn_for_speed_p (),
3266 false) >= 2)
3268 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3269 GET_MODE_PRECISION (mode) - 1,
3270 NULL_RTX, 0);
3272 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3273 OPTAB_LIB_WIDEN);
3274 if (temp != 0)
3275 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3276 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3278 if (temp != 0)
3279 return temp;
3282 return NULL_RTX;
3286 expand_abs (enum machine_mode mode, rtx op0, rtx target,
3287 int result_unsignedp, int safe)
3289 rtx temp, op1;
3291 if (! flag_trapv)
3292 result_unsignedp = 1;
3294 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3295 if (temp != 0)
3296 return temp;
3298 /* If that does not win, use conditional jump and negate. */
3300 /* It is safe to use the target if it is the same
3301 as the source if this is also a pseudo register */
3302 if (op0 == target && REG_P (op0)
3303 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3304 safe = 1;
3306 op1 = gen_label_rtx ();
3307 if (target == 0 || ! safe
3308 || GET_MODE (target) != mode
3309 || (MEM_P (target) && MEM_VOLATILE_P (target))
3310 || (REG_P (target)
3311 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3312 target = gen_reg_rtx (mode);
3314 emit_move_insn (target, op0);
3315 NO_DEFER_POP;
3317 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3318 NULL_RTX, NULL_RTX, op1, -1);
3320 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3321 target, target, 0);
3322 if (op0 != target)
3323 emit_move_insn (target, op0);
3324 emit_label (op1);
3325 OK_DEFER_POP;
3326 return target;
3329 /* Emit code to compute the one's complement absolute value of OP0
3330 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3331 (TARGET may be NULL_RTX.) The return value says where the result
3332 actually is to be found.
3334 MODE is the mode of the operand; the mode of the result is
3335 different but can be deduced from MODE. */
3338 expand_one_cmpl_abs_nojump (enum machine_mode mode, rtx op0, rtx target)
3340 rtx temp;
3342 /* Not applicable for floating point modes. */
3343 if (FLOAT_MODE_P (mode))
3344 return NULL_RTX;
3346 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3347 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3349 rtx last = get_last_insn ();
3351 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3352 if (temp != 0)
3353 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3354 OPTAB_WIDEN);
3356 if (temp != 0)
3357 return temp;
3359 delete_insns_since (last);
3362 /* If this machine has expensive jumps, we can do one's complement
3363 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3365 if (GET_MODE_CLASS (mode) == MODE_INT
3366 && BRANCH_COST (optimize_insn_for_speed_p (),
3367 false) >= 2)
3369 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3370 GET_MODE_PRECISION (mode) - 1,
3371 NULL_RTX, 0);
3373 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3374 OPTAB_LIB_WIDEN);
3376 if (temp != 0)
3377 return temp;
3380 return NULL_RTX;
3383 /* A subroutine of expand_copysign, perform the copysign operation using the
3384 abs and neg primitives advertised to exist on the target. The assumption
3385 is that we have a split register file, and leaving op0 in fp registers,
3386 and not playing with subregs so much, will help the register allocator. */
3388 static rtx
3389 expand_copysign_absneg (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3390 int bitpos, bool op0_is_abs)
3392 enum machine_mode imode;
3393 enum insn_code icode;
3394 rtx sign, label;
3396 if (target == op1)
3397 target = NULL_RTX;
3399 /* Check if the back end provides an insn that handles signbit for the
3400 argument's mode. */
3401 icode = optab_handler (signbit_optab, mode);
3402 if (icode != CODE_FOR_nothing)
3404 imode = insn_data[(int) icode].operand[0].mode;
3405 sign = gen_reg_rtx (imode);
3406 emit_unop_insn (icode, sign, op1, UNKNOWN);
3408 else
3410 double_int mask;
3412 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3414 imode = int_mode_for_mode (mode);
3415 if (imode == BLKmode)
3416 return NULL_RTX;
3417 op1 = gen_lowpart (imode, op1);
3419 else
3421 int word;
3423 imode = word_mode;
3424 if (FLOAT_WORDS_BIG_ENDIAN)
3425 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3426 else
3427 word = bitpos / BITS_PER_WORD;
3428 bitpos = bitpos % BITS_PER_WORD;
3429 op1 = operand_subword_force (op1, word, mode);
3432 mask = double_int_setbit (double_int_zero, bitpos);
3434 sign = expand_binop (imode, and_optab, op1,
3435 immed_double_int_const (mask, imode),
3436 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3439 if (!op0_is_abs)
3441 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3442 if (op0 == NULL)
3443 return NULL_RTX;
3444 target = op0;
3446 else
3448 if (target == NULL_RTX)
3449 target = copy_to_reg (op0);
3450 else
3451 emit_move_insn (target, op0);
3454 label = gen_label_rtx ();
3455 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3457 if (GET_CODE (op0) == CONST_DOUBLE)
3458 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3459 else
3460 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3461 if (op0 != target)
3462 emit_move_insn (target, op0);
3464 emit_label (label);
3466 return target;
3470 /* A subroutine of expand_copysign, perform the entire copysign operation
3471 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3472 is true if op0 is known to have its sign bit clear. */
3474 static rtx
3475 expand_copysign_bit (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3476 int bitpos, bool op0_is_abs)
3478 enum machine_mode imode;
3479 double_int mask;
3480 int word, nwords, i;
3481 rtx temp, insns;
3483 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3485 imode = int_mode_for_mode (mode);
3486 if (imode == BLKmode)
3487 return NULL_RTX;
3488 word = 0;
3489 nwords = 1;
3491 else
3493 imode = word_mode;
3495 if (FLOAT_WORDS_BIG_ENDIAN)
3496 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3497 else
3498 word = bitpos / BITS_PER_WORD;
3499 bitpos = bitpos % BITS_PER_WORD;
3500 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3503 mask = double_int_setbit (double_int_zero, bitpos);
3505 if (target == 0
3506 || target == op0
3507 || target == op1
3508 || (nwords > 1 && !valid_multiword_target_p (target)))
3509 target = gen_reg_rtx (mode);
3511 if (nwords > 1)
3513 start_sequence ();
3515 for (i = 0; i < nwords; ++i)
3517 rtx targ_piece = operand_subword (target, i, 1, mode);
3518 rtx op0_piece = operand_subword_force (op0, i, mode);
3520 if (i == word)
3522 if (!op0_is_abs)
3523 op0_piece
3524 = expand_binop (imode, and_optab, op0_piece,
3525 immed_double_int_const (double_int_not (mask),
3526 imode),
3527 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3529 op1 = expand_binop (imode, and_optab,
3530 operand_subword_force (op1, i, mode),
3531 immed_double_int_const (mask, imode),
3532 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3534 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3535 targ_piece, 1, OPTAB_LIB_WIDEN);
3536 if (temp != targ_piece)
3537 emit_move_insn (targ_piece, temp);
3539 else
3540 emit_move_insn (targ_piece, op0_piece);
3543 insns = get_insns ();
3544 end_sequence ();
3546 emit_insn (insns);
3548 else
3550 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3551 immed_double_int_const (mask, imode),
3552 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3554 op0 = gen_lowpart (imode, op0);
3555 if (!op0_is_abs)
3556 op0 = expand_binop (imode, and_optab, op0,
3557 immed_double_int_const (double_int_not (mask),
3558 imode),
3559 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3561 temp = expand_binop (imode, ior_optab, op0, op1,
3562 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3563 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3566 return target;
3569 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3570 scalar floating point mode. Return NULL if we do not know how to
3571 expand the operation inline. */
3574 expand_copysign (rtx op0, rtx op1, rtx target)
3576 enum machine_mode mode = GET_MODE (op0);
3577 const struct real_format *fmt;
3578 bool op0_is_abs;
3579 rtx temp;
3581 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3582 gcc_assert (GET_MODE (op1) == mode);
3584 /* First try to do it with a special instruction. */
3585 temp = expand_binop (mode, copysign_optab, op0, op1,
3586 target, 0, OPTAB_DIRECT);
3587 if (temp)
3588 return temp;
3590 fmt = REAL_MODE_FORMAT (mode);
3591 if (fmt == NULL || !fmt->has_signed_zero)
3592 return NULL_RTX;
3594 op0_is_abs = false;
3595 if (GET_CODE (op0) == CONST_DOUBLE)
3597 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3598 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3599 op0_is_abs = true;
3602 if (fmt->signbit_ro >= 0
3603 && (GET_CODE (op0) == CONST_DOUBLE
3604 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3605 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3607 temp = expand_copysign_absneg (mode, op0, op1, target,
3608 fmt->signbit_ro, op0_is_abs);
3609 if (temp)
3610 return temp;
3613 if (fmt->signbit_rw < 0)
3614 return NULL_RTX;
3615 return expand_copysign_bit (mode, op0, op1, target,
3616 fmt->signbit_rw, op0_is_abs);
3619 /* Generate an instruction whose insn-code is INSN_CODE,
3620 with two operands: an output TARGET and an input OP0.
3621 TARGET *must* be nonzero, and the output is always stored there.
3622 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3623 the value that is stored into TARGET.
3625 Return false if expansion failed. */
3627 bool
3628 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3629 enum rtx_code code)
3631 struct expand_operand ops[2];
3632 rtx pat;
3634 create_output_operand (&ops[0], target, GET_MODE (target));
3635 create_input_operand (&ops[1], op0, GET_MODE (op0));
3636 pat = maybe_gen_insn (icode, 2, ops);
3637 if (!pat)
3638 return false;
3640 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
3641 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3643 emit_insn (pat);
3645 if (ops[0].value != target)
3646 emit_move_insn (target, ops[0].value);
3647 return true;
3649 /* Generate an instruction whose insn-code is INSN_CODE,
3650 with two operands: an output TARGET and an input OP0.
3651 TARGET *must* be nonzero, and the output is always stored there.
3652 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3653 the value that is stored into TARGET. */
3655 void
3656 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3658 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3659 gcc_assert (ok);
3662 struct no_conflict_data
3664 rtx target, first, insn;
3665 bool must_stay;
3668 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3669 the currently examined clobber / store has to stay in the list of
3670 insns that constitute the actual libcall block. */
3671 static void
3672 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3674 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3676 /* If this inns directly contributes to setting the target, it must stay. */
3677 if (reg_overlap_mentioned_p (p->target, dest))
3678 p->must_stay = true;
3679 /* If we haven't committed to keeping any other insns in the list yet,
3680 there is nothing more to check. */
3681 else if (p->insn == p->first)
3682 return;
3683 /* If this insn sets / clobbers a register that feeds one of the insns
3684 already in the list, this insn has to stay too. */
3685 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3686 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3687 || reg_used_between_p (dest, p->first, p->insn)
3688 /* Likewise if this insn depends on a register set by a previous
3689 insn in the list, or if it sets a result (presumably a hard
3690 register) that is set or clobbered by a previous insn.
3691 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3692 SET_DEST perform the former check on the address, and the latter
3693 check on the MEM. */
3694 || (GET_CODE (set) == SET
3695 && (modified_in_p (SET_SRC (set), p->first)
3696 || modified_in_p (SET_DEST (set), p->first)
3697 || modified_between_p (SET_SRC (set), p->first, p->insn)
3698 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3699 p->must_stay = true;
3703 /* Emit code to make a call to a constant function or a library call.
3705 INSNS is a list containing all insns emitted in the call.
3706 These insns leave the result in RESULT. Our block is to copy RESULT
3707 to TARGET, which is logically equivalent to EQUIV.
3709 We first emit any insns that set a pseudo on the assumption that these are
3710 loading constants into registers; doing so allows them to be safely cse'ed
3711 between blocks. Then we emit all the other insns in the block, followed by
3712 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3713 note with an operand of EQUIV. */
3715 void
3716 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3718 rtx final_dest = target;
3719 rtx next, last, insn;
3721 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3722 into a MEM later. Protect the libcall block from this change. */
3723 if (! REG_P (target) || REG_USERVAR_P (target))
3724 target = gen_reg_rtx (GET_MODE (target));
3726 /* If we're using non-call exceptions, a libcall corresponding to an
3727 operation that may trap may also trap. */
3728 /* ??? See the comment in front of make_reg_eh_region_note. */
3729 if (cfun->can_throw_non_call_exceptions && may_trap_p (equiv))
3731 for (insn = insns; insn; insn = NEXT_INSN (insn))
3732 if (CALL_P (insn))
3734 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3735 if (note)
3737 int lp_nr = INTVAL (XEXP (note, 0));
3738 if (lp_nr == 0 || lp_nr == INT_MIN)
3739 remove_note (insn, note);
3743 else
3745 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3746 reg note to indicate that this call cannot throw or execute a nonlocal
3747 goto (unless there is already a REG_EH_REGION note, in which case
3748 we update it). */
3749 for (insn = insns; insn; insn = NEXT_INSN (insn))
3750 if (CALL_P (insn))
3751 make_reg_eh_region_note_nothrow_nononlocal (insn);
3754 /* First emit all insns that set pseudos. Remove them from the list as
3755 we go. Avoid insns that set pseudos which were referenced in previous
3756 insns. These can be generated by move_by_pieces, for example,
3757 to update an address. Similarly, avoid insns that reference things
3758 set in previous insns. */
3760 for (insn = insns; insn; insn = next)
3762 rtx set = single_set (insn);
3764 next = NEXT_INSN (insn);
3766 if (set != 0 && REG_P (SET_DEST (set))
3767 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3769 struct no_conflict_data data;
3771 data.target = const0_rtx;
3772 data.first = insns;
3773 data.insn = insn;
3774 data.must_stay = 0;
3775 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3776 if (! data.must_stay)
3778 if (PREV_INSN (insn))
3779 NEXT_INSN (PREV_INSN (insn)) = next;
3780 else
3781 insns = next;
3783 if (next)
3784 PREV_INSN (next) = PREV_INSN (insn);
3786 add_insn (insn);
3790 /* Some ports use a loop to copy large arguments onto the stack.
3791 Don't move anything outside such a loop. */
3792 if (LABEL_P (insn))
3793 break;
3796 /* Write the remaining insns followed by the final copy. */
3797 for (insn = insns; insn; insn = next)
3799 next = NEXT_INSN (insn);
3801 add_insn (insn);
3804 last = emit_move_insn (target, result);
3805 if (optab_handler (mov_optab, GET_MODE (target)) != CODE_FOR_nothing)
3806 set_unique_reg_note (last, REG_EQUAL, copy_rtx (equiv));
3808 if (final_dest != target)
3809 emit_move_insn (final_dest, target);
3812 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3813 PURPOSE describes how this comparison will be used. CODE is the rtx
3814 comparison code we will be using.
3816 ??? Actually, CODE is slightly weaker than that. A target is still
3817 required to implement all of the normal bcc operations, but not
3818 required to implement all (or any) of the unordered bcc operations. */
3821 can_compare_p (enum rtx_code code, enum machine_mode mode,
3822 enum can_compare_purpose purpose)
3824 rtx test;
3825 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3828 enum insn_code icode;
3830 if (purpose == ccp_jump
3831 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
3832 && insn_operand_matches (icode, 0, test))
3833 return 1;
3834 if (purpose == ccp_store_flag
3835 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
3836 && insn_operand_matches (icode, 1, test))
3837 return 1;
3838 if (purpose == ccp_cmov
3839 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
3840 return 1;
3842 mode = GET_MODE_WIDER_MODE (mode);
3843 PUT_MODE (test, mode);
3845 while (mode != VOIDmode);
3847 return 0;
3850 /* This function is called when we are going to emit a compare instruction that
3851 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
3853 *PMODE is the mode of the inputs (in case they are const_int).
3854 *PUNSIGNEDP nonzero says that the operands are unsigned;
3855 this matters if they need to be widened (as given by METHODS).
3857 If they have mode BLKmode, then SIZE specifies the size of both operands.
3859 This function performs all the setup necessary so that the caller only has
3860 to emit a single comparison insn. This setup can involve doing a BLKmode
3861 comparison or emitting a library call to perform the comparison if no insn
3862 is available to handle it.
3863 The values which are passed in through pointers can be modified; the caller
3864 should perform the comparison on the modified values. Constant
3865 comparisons must have already been folded. */
3867 static void
3868 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
3869 int unsignedp, enum optab_methods methods,
3870 rtx *ptest, enum machine_mode *pmode)
3872 enum machine_mode mode = *pmode;
3873 rtx libfunc, test;
3874 enum machine_mode cmp_mode;
3875 enum mode_class mclass;
3877 /* The other methods are not needed. */
3878 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
3879 || methods == OPTAB_LIB_WIDEN);
3881 /* If we are optimizing, force expensive constants into a register. */
3882 if (CONSTANT_P (x) && optimize
3883 && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
3884 > COSTS_N_INSNS (1)))
3885 x = force_reg (mode, x);
3887 if (CONSTANT_P (y) && optimize
3888 && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
3889 > COSTS_N_INSNS (1)))
3890 y = force_reg (mode, y);
3892 #ifdef HAVE_cc0
3893 /* Make sure if we have a canonical comparison. The RTL
3894 documentation states that canonical comparisons are required only
3895 for targets which have cc0. */
3896 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
3897 #endif
3899 /* Don't let both operands fail to indicate the mode. */
3900 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
3901 x = force_reg (mode, x);
3902 if (mode == VOIDmode)
3903 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
3905 /* Handle all BLKmode compares. */
3907 if (mode == BLKmode)
3909 enum machine_mode result_mode;
3910 enum insn_code cmp_code;
3911 tree length_type;
3912 rtx libfunc;
3913 rtx result;
3914 rtx opalign
3915 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
3917 gcc_assert (size);
3919 /* Try to use a memory block compare insn - either cmpstr
3920 or cmpmem will do. */
3921 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
3922 cmp_mode != VOIDmode;
3923 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
3925 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
3926 if (cmp_code == CODE_FOR_nothing)
3927 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
3928 if (cmp_code == CODE_FOR_nothing)
3929 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
3930 if (cmp_code == CODE_FOR_nothing)
3931 continue;
3933 /* Must make sure the size fits the insn's mode. */
3934 if ((CONST_INT_P (size)
3935 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
3936 || (GET_MODE_BITSIZE (GET_MODE (size))
3937 > GET_MODE_BITSIZE (cmp_mode)))
3938 continue;
3940 result_mode = insn_data[cmp_code].operand[0].mode;
3941 result = gen_reg_rtx (result_mode);
3942 size = convert_to_mode (cmp_mode, size, 1);
3943 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
3945 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
3946 *pmode = result_mode;
3947 return;
3950 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
3951 goto fail;
3953 /* Otherwise call a library function, memcmp. */
3954 libfunc = memcmp_libfunc;
3955 length_type = sizetype;
3956 result_mode = TYPE_MODE (integer_type_node);
3957 cmp_mode = TYPE_MODE (length_type);
3958 size = convert_to_mode (TYPE_MODE (length_type), size,
3959 TYPE_UNSIGNED (length_type));
3961 result = emit_library_call_value (libfunc, 0, LCT_PURE,
3962 result_mode, 3,
3963 XEXP (x, 0), Pmode,
3964 XEXP (y, 0), Pmode,
3965 size, cmp_mode);
3967 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
3968 *pmode = result_mode;
3969 return;
3972 /* Don't allow operands to the compare to trap, as that can put the
3973 compare and branch in different basic blocks. */
3974 if (cfun->can_throw_non_call_exceptions)
3976 if (may_trap_p (x))
3977 x = force_reg (mode, x);
3978 if (may_trap_p (y))
3979 y = force_reg (mode, y);
3982 if (GET_MODE_CLASS (mode) == MODE_CC)
3984 gcc_assert (can_compare_p (comparison, CCmode, ccp_jump));
3985 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
3986 return;
3989 mclass = GET_MODE_CLASS (mode);
3990 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
3991 cmp_mode = mode;
3994 enum insn_code icode;
3995 icode = optab_handler (cbranch_optab, cmp_mode);
3996 if (icode != CODE_FOR_nothing
3997 && insn_operand_matches (icode, 0, test))
3999 rtx last = get_last_insn ();
4000 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4001 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4002 if (op0 && op1
4003 && insn_operand_matches (icode, 1, op0)
4004 && insn_operand_matches (icode, 2, op1))
4006 XEXP (test, 0) = op0;
4007 XEXP (test, 1) = op1;
4008 *ptest = test;
4009 *pmode = cmp_mode;
4010 return;
4012 delete_insns_since (last);
4015 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4016 break;
4017 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4019 while (cmp_mode != VOIDmode);
4021 if (methods != OPTAB_LIB_WIDEN)
4022 goto fail;
4024 if (!SCALAR_FLOAT_MODE_P (mode))
4026 rtx result;
4028 /* Handle a libcall just for the mode we are using. */
4029 libfunc = optab_libfunc (cmp_optab, mode);
4030 gcc_assert (libfunc);
4032 /* If we want unsigned, and this mode has a distinct unsigned
4033 comparison routine, use that. */
4034 if (unsignedp)
4036 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4037 if (ulibfunc)
4038 libfunc = ulibfunc;
4041 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4042 targetm.libgcc_cmp_return_mode (),
4043 2, x, mode, y, mode);
4045 /* There are two kinds of comparison routines. Biased routines
4046 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4047 of gcc expect that the comparison operation is equivalent
4048 to the modified comparison. For signed comparisons compare the
4049 result against 1 in the biased case, and zero in the unbiased
4050 case. For unsigned comparisons always compare against 1 after
4051 biasing the unbiased result by adding 1. This gives us a way to
4052 represent LTU.
4053 The comparisons in the fixed-point helper library are always
4054 biased. */
4055 x = result;
4056 y = const1_rtx;
4058 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4060 if (unsignedp)
4061 x = plus_constant (result, 1);
4062 else
4063 y = const0_rtx;
4066 *pmode = word_mode;
4067 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4068 ptest, pmode);
4070 else
4071 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4073 return;
4075 fail:
4076 *ptest = NULL_RTX;
4079 /* Before emitting an insn with code ICODE, make sure that X, which is going
4080 to be used for operand OPNUM of the insn, is converted from mode MODE to
4081 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4082 that it is accepted by the operand predicate. Return the new value. */
4085 prepare_operand (enum insn_code icode, rtx x, int opnum, enum machine_mode mode,
4086 enum machine_mode wider_mode, int unsignedp)
4088 if (mode != wider_mode)
4089 x = convert_modes (wider_mode, mode, x, unsignedp);
4091 if (!insn_operand_matches (icode, opnum, x))
4093 if (reload_completed)
4094 return NULL_RTX;
4095 x = copy_to_mode_reg (insn_data[(int) icode].operand[opnum].mode, x);
4098 return x;
4101 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4102 we can do the branch. */
4104 static void
4105 emit_cmp_and_jump_insn_1 (rtx test, enum machine_mode mode, rtx label)
4107 enum machine_mode optab_mode;
4108 enum mode_class mclass;
4109 enum insn_code icode;
4111 mclass = GET_MODE_CLASS (mode);
4112 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4113 icode = optab_handler (cbranch_optab, optab_mode);
4115 gcc_assert (icode != CODE_FOR_nothing);
4116 gcc_assert (insn_operand_matches (icode, 0, test));
4117 emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0), XEXP (test, 1), label));
4120 /* Generate code to compare X with Y so that the condition codes are
4121 set and to jump to LABEL if the condition is true. If X is a
4122 constant and Y is not a constant, then the comparison is swapped to
4123 ensure that the comparison RTL has the canonical form.
4125 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4126 need to be widened. UNSIGNEDP is also used to select the proper
4127 branch condition code.
4129 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4131 MODE is the mode of the inputs (in case they are const_int).
4133 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4134 It will be potentially converted into an unsigned variant based on
4135 UNSIGNEDP to select a proper jump instruction. */
4137 void
4138 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4139 enum machine_mode mode, int unsignedp, rtx label)
4141 rtx op0 = x, op1 = y;
4142 rtx test;
4144 /* Swap operands and condition to ensure canonical RTL. */
4145 if (swap_commutative_operands_p (x, y)
4146 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4148 op0 = y, op1 = x;
4149 comparison = swap_condition (comparison);
4152 /* If OP0 is still a constant, then both X and Y must be constants
4153 or the opposite comparison is not supported. Force X into a register
4154 to create canonical RTL. */
4155 if (CONSTANT_P (op0))
4156 op0 = force_reg (mode, op0);
4158 if (unsignedp)
4159 comparison = unsigned_condition (comparison);
4161 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4162 &test, &mode);
4163 emit_cmp_and_jump_insn_1 (test, mode, label);
4167 /* Emit a library call comparison between floating point X and Y.
4168 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4170 static void
4171 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4172 rtx *ptest, enum machine_mode *pmode)
4174 enum rtx_code swapped = swap_condition (comparison);
4175 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4176 enum machine_mode orig_mode = GET_MODE (x);
4177 enum machine_mode mode, cmp_mode;
4178 rtx true_rtx, false_rtx;
4179 rtx value, target, insns, equiv;
4180 rtx libfunc = 0;
4181 bool reversed_p = false;
4182 cmp_mode = targetm.libgcc_cmp_return_mode ();
4184 for (mode = orig_mode;
4185 mode != VOIDmode;
4186 mode = GET_MODE_WIDER_MODE (mode))
4188 if (code_to_optab[comparison]
4189 && (libfunc = optab_libfunc (code_to_optab[comparison], mode)))
4190 break;
4192 if (code_to_optab[swapped]
4193 && (libfunc = optab_libfunc (code_to_optab[swapped], mode)))
4195 rtx tmp;
4196 tmp = x; x = y; y = tmp;
4197 comparison = swapped;
4198 break;
4201 if (code_to_optab[reversed]
4202 && (libfunc = optab_libfunc (code_to_optab[reversed], mode)))
4204 comparison = reversed;
4205 reversed_p = true;
4206 break;
4210 gcc_assert (mode != VOIDmode);
4212 if (mode != orig_mode)
4214 x = convert_to_mode (mode, x, 0);
4215 y = convert_to_mode (mode, y, 0);
4218 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4219 the RTL. The allows the RTL optimizers to delete the libcall if the
4220 condition can be determined at compile-time. */
4221 if (comparison == UNORDERED
4222 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4224 true_rtx = const_true_rtx;
4225 false_rtx = const0_rtx;
4227 else
4229 switch (comparison)
4231 case EQ:
4232 true_rtx = const0_rtx;
4233 false_rtx = const_true_rtx;
4234 break;
4236 case NE:
4237 true_rtx = const_true_rtx;
4238 false_rtx = const0_rtx;
4239 break;
4241 case GT:
4242 true_rtx = const1_rtx;
4243 false_rtx = const0_rtx;
4244 break;
4246 case GE:
4247 true_rtx = const0_rtx;
4248 false_rtx = constm1_rtx;
4249 break;
4251 case LT:
4252 true_rtx = constm1_rtx;
4253 false_rtx = const0_rtx;
4254 break;
4256 case LE:
4257 true_rtx = const0_rtx;
4258 false_rtx = const1_rtx;
4259 break;
4261 default:
4262 gcc_unreachable ();
4266 if (comparison == UNORDERED)
4268 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4269 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4270 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4271 temp, const_true_rtx, equiv);
4273 else
4275 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4276 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4277 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4278 equiv, true_rtx, false_rtx);
4281 start_sequence ();
4282 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4283 cmp_mode, 2, x, mode, y, mode);
4284 insns = get_insns ();
4285 end_sequence ();
4287 target = gen_reg_rtx (cmp_mode);
4288 emit_libcall_block (insns, target, value, equiv);
4290 if (comparison == UNORDERED
4291 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4292 || reversed_p)
4293 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4294 else
4295 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4297 *pmode = cmp_mode;
4300 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4302 void
4303 emit_indirect_jump (rtx loc)
4305 struct expand_operand ops[1];
4307 create_address_operand (&ops[0], loc);
4308 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4309 emit_barrier ();
4312 #ifdef HAVE_conditional_move
4314 /* Emit a conditional move instruction if the machine supports one for that
4315 condition and machine mode.
4317 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4318 the mode to use should they be constants. If it is VOIDmode, they cannot
4319 both be constants.
4321 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4322 should be stored there. MODE is the mode to use should they be constants.
4323 If it is VOIDmode, they cannot both be constants.
4325 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4326 is not supported. */
4329 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4330 enum machine_mode cmode, rtx op2, rtx op3,
4331 enum machine_mode mode, int unsignedp)
4333 rtx tem, comparison, last;
4334 enum insn_code icode;
4335 enum rtx_code reversed;
4337 /* If one operand is constant, make it the second one. Only do this
4338 if the other operand is not constant as well. */
4340 if (swap_commutative_operands_p (op0, op1))
4342 tem = op0;
4343 op0 = op1;
4344 op1 = tem;
4345 code = swap_condition (code);
4348 /* get_condition will prefer to generate LT and GT even if the old
4349 comparison was against zero, so undo that canonicalization here since
4350 comparisons against zero are cheaper. */
4351 if (code == LT && op1 == const1_rtx)
4352 code = LE, op1 = const0_rtx;
4353 else if (code == GT && op1 == constm1_rtx)
4354 code = GE, op1 = const0_rtx;
4356 if (cmode == VOIDmode)
4357 cmode = GET_MODE (op0);
4359 if (swap_commutative_operands_p (op2, op3)
4360 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4361 != UNKNOWN))
4363 tem = op2;
4364 op2 = op3;
4365 op3 = tem;
4366 code = reversed;
4369 if (mode == VOIDmode)
4370 mode = GET_MODE (op2);
4372 icode = direct_optab_handler (movcc_optab, mode);
4374 if (icode == CODE_FOR_nothing)
4375 return 0;
4377 if (!target)
4378 target = gen_reg_rtx (mode);
4380 code = unsignedp ? unsigned_condition (code) : code;
4381 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4383 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4384 return NULL and let the caller figure out how best to deal with this
4385 situation. */
4386 if (!COMPARISON_P (comparison))
4387 return NULL_RTX;
4389 do_pending_stack_adjust ();
4390 last = get_last_insn ();
4391 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4392 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4393 &comparison, &cmode);
4394 if (comparison)
4396 struct expand_operand ops[4];
4398 create_output_operand (&ops[0], target, mode);
4399 create_fixed_operand (&ops[1], comparison);
4400 create_input_operand (&ops[2], op2, mode);
4401 create_input_operand (&ops[3], op3, mode);
4402 if (maybe_expand_insn (icode, 4, ops))
4404 if (ops[0].value != target)
4405 convert_move (target, ops[0].value, false);
4406 return target;
4409 delete_insns_since (last);
4410 return NULL_RTX;
4413 /* Return nonzero if a conditional move of mode MODE is supported.
4415 This function is for combine so it can tell whether an insn that looks
4416 like a conditional move is actually supported by the hardware. If we
4417 guess wrong we lose a bit on optimization, but that's it. */
4418 /* ??? sparc64 supports conditionally moving integers values based on fp
4419 comparisons, and vice versa. How do we handle them? */
4422 can_conditionally_move_p (enum machine_mode mode)
4424 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4425 return 1;
4427 return 0;
4430 #endif /* HAVE_conditional_move */
4432 /* Emit a conditional addition instruction if the machine supports one for that
4433 condition and machine mode.
4435 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4436 the mode to use should they be constants. If it is VOIDmode, they cannot
4437 both be constants.
4439 OP2 should be stored in TARGET if the comparison is true, otherwise OP2+OP3
4440 should be stored there. MODE is the mode to use should they be constants.
4441 If it is VOIDmode, they cannot both be constants.
4443 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4444 is not supported. */
4447 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4448 enum machine_mode cmode, rtx op2, rtx op3,
4449 enum machine_mode mode, int unsignedp)
4451 rtx tem, comparison, last;
4452 enum insn_code icode;
4453 enum rtx_code reversed;
4455 /* If one operand is constant, make it the second one. Only do this
4456 if the other operand is not constant as well. */
4458 if (swap_commutative_operands_p (op0, op1))
4460 tem = op0;
4461 op0 = op1;
4462 op1 = tem;
4463 code = swap_condition (code);
4466 /* get_condition will prefer to generate LT and GT even if the old
4467 comparison was against zero, so undo that canonicalization here since
4468 comparisons against zero are cheaper. */
4469 if (code == LT && op1 == const1_rtx)
4470 code = LE, op1 = const0_rtx;
4471 else if (code == GT && op1 == constm1_rtx)
4472 code = GE, op1 = const0_rtx;
4474 if (cmode == VOIDmode)
4475 cmode = GET_MODE (op0);
4477 if (swap_commutative_operands_p (op2, op3)
4478 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4479 != UNKNOWN))
4481 tem = op2;
4482 op2 = op3;
4483 op3 = tem;
4484 code = reversed;
4487 if (mode == VOIDmode)
4488 mode = GET_MODE (op2);
4490 icode = optab_handler (addcc_optab, mode);
4492 if (icode == CODE_FOR_nothing)
4493 return 0;
4495 if (!target)
4496 target = gen_reg_rtx (mode);
4498 code = unsignedp ? unsigned_condition (code) : code;
4499 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4501 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4502 return NULL and let the caller figure out how best to deal with this
4503 situation. */
4504 if (!COMPARISON_P (comparison))
4505 return NULL_RTX;
4507 do_pending_stack_adjust ();
4508 last = get_last_insn ();
4509 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4510 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4511 &comparison, &cmode);
4512 if (comparison)
4514 struct expand_operand ops[4];
4516 create_output_operand (&ops[0], target, mode);
4517 create_fixed_operand (&ops[1], comparison);
4518 create_input_operand (&ops[2], op2, mode);
4519 create_input_operand (&ops[3], op3, mode);
4520 if (maybe_expand_insn (icode, 4, ops))
4522 if (ops[0].value != target)
4523 convert_move (target, ops[0].value, false);
4524 return target;
4527 delete_insns_since (last);
4528 return NULL_RTX;
4531 /* These functions attempt to generate an insn body, rather than
4532 emitting the insn, but if the gen function already emits them, we
4533 make no attempt to turn them back into naked patterns. */
4535 /* Generate and return an insn body to add Y to X. */
4538 gen_add2_insn (rtx x, rtx y)
4540 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4542 gcc_assert (insn_operand_matches (icode, 0, x));
4543 gcc_assert (insn_operand_matches (icode, 1, x));
4544 gcc_assert (insn_operand_matches (icode, 2, y));
4546 return GEN_FCN (icode) (x, x, y);
4549 /* Generate and return an insn body to add r1 and c,
4550 storing the result in r0. */
4553 gen_add3_insn (rtx r0, rtx r1, rtx c)
4555 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4557 if (icode == CODE_FOR_nothing
4558 || !insn_operand_matches (icode, 0, r0)
4559 || !insn_operand_matches (icode, 1, r1)
4560 || !insn_operand_matches (icode, 2, c))
4561 return NULL_RTX;
4563 return GEN_FCN (icode) (r0, r1, c);
4567 have_add2_insn (rtx x, rtx y)
4569 enum insn_code icode;
4571 gcc_assert (GET_MODE (x) != VOIDmode);
4573 icode = optab_handler (add_optab, GET_MODE (x));
4575 if (icode == CODE_FOR_nothing)
4576 return 0;
4578 if (!insn_operand_matches (icode, 0, x)
4579 || !insn_operand_matches (icode, 1, x)
4580 || !insn_operand_matches (icode, 2, y))
4581 return 0;
4583 return 1;
4586 /* Generate and return an insn body to subtract Y from X. */
4589 gen_sub2_insn (rtx x, rtx y)
4591 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4593 gcc_assert (insn_operand_matches (icode, 0, x));
4594 gcc_assert (insn_operand_matches (icode, 1, x));
4595 gcc_assert (insn_operand_matches (icode, 2, y));
4597 return GEN_FCN (icode) (x, x, y);
4600 /* Generate and return an insn body to subtract r1 and c,
4601 storing the result in r0. */
4604 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4606 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4608 if (icode == CODE_FOR_nothing
4609 || !insn_operand_matches (icode, 0, r0)
4610 || !insn_operand_matches (icode, 1, r1)
4611 || !insn_operand_matches (icode, 2, c))
4612 return NULL_RTX;
4614 return GEN_FCN (icode) (r0, r1, c);
4618 have_sub2_insn (rtx x, rtx y)
4620 enum insn_code icode;
4622 gcc_assert (GET_MODE (x) != VOIDmode);
4624 icode = optab_handler (sub_optab, GET_MODE (x));
4626 if (icode == CODE_FOR_nothing)
4627 return 0;
4629 if (!insn_operand_matches (icode, 0, x)
4630 || !insn_operand_matches (icode, 1, x)
4631 || !insn_operand_matches (icode, 2, y))
4632 return 0;
4634 return 1;
4637 /* Generate the body of an instruction to copy Y into X.
4638 It may be a list of insns, if one insn isn't enough. */
4641 gen_move_insn (rtx x, rtx y)
4643 rtx seq;
4645 start_sequence ();
4646 emit_move_insn_1 (x, y);
4647 seq = get_insns ();
4648 end_sequence ();
4649 return seq;
4652 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4653 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4654 no such operation exists, CODE_FOR_nothing will be returned. */
4656 enum insn_code
4657 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
4658 int unsignedp)
4660 convert_optab tab;
4661 #ifdef HAVE_ptr_extend
4662 if (unsignedp < 0)
4663 return CODE_FOR_ptr_extend;
4664 #endif
4666 tab = unsignedp ? zext_optab : sext_optab;
4667 return convert_optab_handler (tab, to_mode, from_mode);
4670 /* Generate the body of an insn to extend Y (with mode MFROM)
4671 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4674 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4675 enum machine_mode mfrom, int unsignedp)
4677 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4678 return GEN_FCN (icode) (x, y);
4681 /* can_fix_p and can_float_p say whether the target machine
4682 can directly convert a given fixed point type to
4683 a given floating point type, or vice versa.
4684 The returned value is the CODE_FOR_... value to use,
4685 or CODE_FOR_nothing if these modes cannot be directly converted.
4687 *TRUNCP_PTR is set to 1 if it is necessary to output
4688 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4690 static enum insn_code
4691 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4692 int unsignedp, int *truncp_ptr)
4694 convert_optab tab;
4695 enum insn_code icode;
4697 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4698 icode = convert_optab_handler (tab, fixmode, fltmode);
4699 if (icode != CODE_FOR_nothing)
4701 *truncp_ptr = 0;
4702 return icode;
4705 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4706 for this to work. We need to rework the fix* and ftrunc* patterns
4707 and documentation. */
4708 tab = unsignedp ? ufix_optab : sfix_optab;
4709 icode = convert_optab_handler (tab, fixmode, fltmode);
4710 if (icode != CODE_FOR_nothing
4711 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4713 *truncp_ptr = 1;
4714 return icode;
4717 *truncp_ptr = 0;
4718 return CODE_FOR_nothing;
4721 enum insn_code
4722 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4723 int unsignedp)
4725 convert_optab tab;
4727 tab = unsignedp ? ufloat_optab : sfloat_optab;
4728 return convert_optab_handler (tab, fltmode, fixmode);
4731 /* Generate code to convert FROM to floating point
4732 and store in TO. FROM must be fixed point and not VOIDmode.
4733 UNSIGNEDP nonzero means regard FROM as unsigned.
4734 Normally this is done by correcting the final value
4735 if it is negative. */
4737 void
4738 expand_float (rtx to, rtx from, int unsignedp)
4740 enum insn_code icode;
4741 rtx target = to;
4742 enum machine_mode fmode, imode;
4743 bool can_do_signed = false;
4745 /* Crash now, because we won't be able to decide which mode to use. */
4746 gcc_assert (GET_MODE (from) != VOIDmode);
4748 /* Look for an insn to do the conversion. Do it in the specified
4749 modes if possible; otherwise convert either input, output or both to
4750 wider mode. If the integer mode is wider than the mode of FROM,
4751 we can do the conversion signed even if the input is unsigned. */
4753 for (fmode = GET_MODE (to); fmode != VOIDmode;
4754 fmode = GET_MODE_WIDER_MODE (fmode))
4755 for (imode = GET_MODE (from); imode != VOIDmode;
4756 imode = GET_MODE_WIDER_MODE (imode))
4758 int doing_unsigned = unsignedp;
4760 if (fmode != GET_MODE (to)
4761 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
4762 continue;
4764 icode = can_float_p (fmode, imode, unsignedp);
4765 if (icode == CODE_FOR_nothing && unsignedp)
4767 enum insn_code scode = can_float_p (fmode, imode, 0);
4768 if (scode != CODE_FOR_nothing)
4769 can_do_signed = true;
4770 if (imode != GET_MODE (from))
4771 icode = scode, doing_unsigned = 0;
4774 if (icode != CODE_FOR_nothing)
4776 if (imode != GET_MODE (from))
4777 from = convert_to_mode (imode, from, unsignedp);
4779 if (fmode != GET_MODE (to))
4780 target = gen_reg_rtx (fmode);
4782 emit_unop_insn (icode, target, from,
4783 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4785 if (target != to)
4786 convert_move (to, target, 0);
4787 return;
4791 /* Unsigned integer, and no way to convert directly. Convert as signed,
4792 then unconditionally adjust the result. */
4793 if (unsignedp && can_do_signed)
4795 rtx label = gen_label_rtx ();
4796 rtx temp;
4797 REAL_VALUE_TYPE offset;
4799 /* Look for a usable floating mode FMODE wider than the source and at
4800 least as wide as the target. Using FMODE will avoid rounding woes
4801 with unsigned values greater than the signed maximum value. */
4803 for (fmode = GET_MODE (to); fmode != VOIDmode;
4804 fmode = GET_MODE_WIDER_MODE (fmode))
4805 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
4806 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
4807 break;
4809 if (fmode == VOIDmode)
4811 /* There is no such mode. Pretend the target is wide enough. */
4812 fmode = GET_MODE (to);
4814 /* Avoid double-rounding when TO is narrower than FROM. */
4815 if ((significand_size (fmode) + 1)
4816 < GET_MODE_PRECISION (GET_MODE (from)))
4818 rtx temp1;
4819 rtx neglabel = gen_label_rtx ();
4821 /* Don't use TARGET if it isn't a register, is a hard register,
4822 or is the wrong mode. */
4823 if (!REG_P (target)
4824 || REGNO (target) < FIRST_PSEUDO_REGISTER
4825 || GET_MODE (target) != fmode)
4826 target = gen_reg_rtx (fmode);
4828 imode = GET_MODE (from);
4829 do_pending_stack_adjust ();
4831 /* Test whether the sign bit is set. */
4832 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
4833 0, neglabel);
4835 /* The sign bit is not set. Convert as signed. */
4836 expand_float (target, from, 0);
4837 emit_jump_insn (gen_jump (label));
4838 emit_barrier ();
4840 /* The sign bit is set.
4841 Convert to a usable (positive signed) value by shifting right
4842 one bit, while remembering if a nonzero bit was shifted
4843 out; i.e., compute (from & 1) | (from >> 1). */
4845 emit_label (neglabel);
4846 temp = expand_binop (imode, and_optab, from, const1_rtx,
4847 NULL_RTX, 1, OPTAB_LIB_WIDEN);
4848 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
4849 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
4850 OPTAB_LIB_WIDEN);
4851 expand_float (target, temp, 0);
4853 /* Multiply by 2 to undo the shift above. */
4854 temp = expand_binop (fmode, add_optab, target, target,
4855 target, 0, OPTAB_LIB_WIDEN);
4856 if (temp != target)
4857 emit_move_insn (target, temp);
4859 do_pending_stack_adjust ();
4860 emit_label (label);
4861 goto done;
4865 /* If we are about to do some arithmetic to correct for an
4866 unsigned operand, do it in a pseudo-register. */
4868 if (GET_MODE (to) != fmode
4869 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
4870 target = gen_reg_rtx (fmode);
4872 /* Convert as signed integer to floating. */
4873 expand_float (target, from, 0);
4875 /* If FROM is negative (and therefore TO is negative),
4876 correct its value by 2**bitwidth. */
4878 do_pending_stack_adjust ();
4879 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
4880 0, label);
4883 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
4884 temp = expand_binop (fmode, add_optab, target,
4885 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
4886 target, 0, OPTAB_LIB_WIDEN);
4887 if (temp != target)
4888 emit_move_insn (target, temp);
4890 do_pending_stack_adjust ();
4891 emit_label (label);
4892 goto done;
4895 /* No hardware instruction available; call a library routine. */
4897 rtx libfunc;
4898 rtx insns;
4899 rtx value;
4900 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
4902 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
4903 from = convert_to_mode (SImode, from, unsignedp);
4905 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
4906 gcc_assert (libfunc);
4908 start_sequence ();
4910 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4911 GET_MODE (to), 1, from,
4912 GET_MODE (from));
4913 insns = get_insns ();
4914 end_sequence ();
4916 emit_libcall_block (insns, target, value,
4917 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
4918 GET_MODE (to), from));
4921 done:
4923 /* Copy result to requested destination
4924 if we have been computing in a temp location. */
4926 if (target != to)
4928 if (GET_MODE (target) == GET_MODE (to))
4929 emit_move_insn (to, target);
4930 else
4931 convert_move (to, target, 0);
4935 /* Generate code to convert FROM to fixed point and store in TO. FROM
4936 must be floating point. */
4938 void
4939 expand_fix (rtx to, rtx from, int unsignedp)
4941 enum insn_code icode;
4942 rtx target = to;
4943 enum machine_mode fmode, imode;
4944 int must_trunc = 0;
4946 /* We first try to find a pair of modes, one real and one integer, at
4947 least as wide as FROM and TO, respectively, in which we can open-code
4948 this conversion. If the integer mode is wider than the mode of TO,
4949 we can do the conversion either signed or unsigned. */
4951 for (fmode = GET_MODE (from); fmode != VOIDmode;
4952 fmode = GET_MODE_WIDER_MODE (fmode))
4953 for (imode = GET_MODE (to); imode != VOIDmode;
4954 imode = GET_MODE_WIDER_MODE (imode))
4956 int doing_unsigned = unsignedp;
4958 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
4959 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
4960 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
4962 if (icode != CODE_FOR_nothing)
4964 rtx last = get_last_insn ();
4965 if (fmode != GET_MODE (from))
4966 from = convert_to_mode (fmode, from, 0);
4968 if (must_trunc)
4970 rtx temp = gen_reg_rtx (GET_MODE (from));
4971 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
4972 temp, 0);
4975 if (imode != GET_MODE (to))
4976 target = gen_reg_rtx (imode);
4978 if (maybe_emit_unop_insn (icode, target, from,
4979 doing_unsigned ? UNSIGNED_FIX : FIX))
4981 if (target != to)
4982 convert_move (to, target, unsignedp);
4983 return;
4985 delete_insns_since (last);
4989 /* For an unsigned conversion, there is one more way to do it.
4990 If we have a signed conversion, we generate code that compares
4991 the real value to the largest representable positive number. If if
4992 is smaller, the conversion is done normally. Otherwise, subtract
4993 one plus the highest signed number, convert, and add it back.
4995 We only need to check all real modes, since we know we didn't find
4996 anything with a wider integer mode.
4998 This code used to extend FP value into mode wider than the destination.
4999 This is needed for decimal float modes which cannot accurately
5000 represent one plus the highest signed number of the same size, but
5001 not for binary modes. Consider, for instance conversion from SFmode
5002 into DImode.
5004 The hot path through the code is dealing with inputs smaller than 2^63
5005 and doing just the conversion, so there is no bits to lose.
5007 In the other path we know the value is positive in the range 2^63..2^64-1
5008 inclusive. (as for other input overflow happens and result is undefined)
5009 So we know that the most important bit set in mantissa corresponds to
5010 2^63. The subtraction of 2^63 should not generate any rounding as it
5011 simply clears out that bit. The rest is trivial. */
5013 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5014 for (fmode = GET_MODE (from); fmode != VOIDmode;
5015 fmode = GET_MODE_WIDER_MODE (fmode))
5016 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5017 && (!DECIMAL_FLOAT_MODE_P (fmode)
5018 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5020 int bitsize;
5021 REAL_VALUE_TYPE offset;
5022 rtx limit, lab1, lab2, insn;
5024 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5025 real_2expN (&offset, bitsize - 1, fmode);
5026 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5027 lab1 = gen_label_rtx ();
5028 lab2 = gen_label_rtx ();
5030 if (fmode != GET_MODE (from))
5031 from = convert_to_mode (fmode, from, 0);
5033 /* See if we need to do the subtraction. */
5034 do_pending_stack_adjust ();
5035 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5036 0, lab1);
5038 /* If not, do the signed "fix" and branch around fixup code. */
5039 expand_fix (to, from, 0);
5040 emit_jump_insn (gen_jump (lab2));
5041 emit_barrier ();
5043 /* Otherwise, subtract 2**(N-1), convert to signed number,
5044 then add 2**(N-1). Do the addition using XOR since this
5045 will often generate better code. */
5046 emit_label (lab1);
5047 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5048 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5049 expand_fix (to, target, 0);
5050 target = expand_binop (GET_MODE (to), xor_optab, to,
5051 gen_int_mode
5052 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5053 GET_MODE (to)),
5054 to, 1, OPTAB_LIB_WIDEN);
5056 if (target != to)
5057 emit_move_insn (to, target);
5059 emit_label (lab2);
5061 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5063 /* Make a place for a REG_NOTE and add it. */
5064 insn = emit_move_insn (to, to);
5065 set_unique_reg_note (insn,
5066 REG_EQUAL,
5067 gen_rtx_fmt_e (UNSIGNED_FIX,
5068 GET_MODE (to),
5069 copy_rtx (from)));
5072 return;
5075 /* We can't do it with an insn, so use a library call. But first ensure
5076 that the mode of TO is at least as wide as SImode, since those are the
5077 only library calls we know about. */
5079 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
5081 target = gen_reg_rtx (SImode);
5083 expand_fix (target, from, unsignedp);
5085 else
5087 rtx insns;
5088 rtx value;
5089 rtx libfunc;
5091 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5092 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5093 gcc_assert (libfunc);
5095 start_sequence ();
5097 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5098 GET_MODE (to), 1, from,
5099 GET_MODE (from));
5100 insns = get_insns ();
5101 end_sequence ();
5103 emit_libcall_block (insns, target, value,
5104 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5105 GET_MODE (to), from));
5108 if (target != to)
5110 if (GET_MODE (to) == GET_MODE (target))
5111 emit_move_insn (to, target);
5112 else
5113 convert_move (to, target, 0);
5117 /* Generate code to convert FROM or TO a fixed-point.
5118 If UINTP is true, either TO or FROM is an unsigned integer.
5119 If SATP is true, we need to saturate the result. */
5121 void
5122 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5124 enum machine_mode to_mode = GET_MODE (to);
5125 enum machine_mode from_mode = GET_MODE (from);
5126 convert_optab tab;
5127 enum rtx_code this_code;
5128 enum insn_code code;
5129 rtx insns, value;
5130 rtx libfunc;
5132 if (to_mode == from_mode)
5134 emit_move_insn (to, from);
5135 return;
5138 if (uintp)
5140 tab = satp ? satfractuns_optab : fractuns_optab;
5141 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5143 else
5145 tab = satp ? satfract_optab : fract_optab;
5146 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5148 code = convert_optab_handler (tab, to_mode, from_mode);
5149 if (code != CODE_FOR_nothing)
5151 emit_unop_insn (code, to, from, this_code);
5152 return;
5155 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5156 gcc_assert (libfunc);
5158 start_sequence ();
5159 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5160 1, from, from_mode);
5161 insns = get_insns ();
5162 end_sequence ();
5164 emit_libcall_block (insns, to, value,
5165 gen_rtx_fmt_e (tab->code, to_mode, from));
5168 /* Generate code to convert FROM to fixed point and store in TO. FROM
5169 must be floating point, TO must be signed. Use the conversion optab
5170 TAB to do the conversion. */
5172 bool
5173 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5175 enum insn_code icode;
5176 rtx target = to;
5177 enum machine_mode fmode, imode;
5179 /* We first try to find a pair of modes, one real and one integer, at
5180 least as wide as FROM and TO, respectively, in which we can open-code
5181 this conversion. If the integer mode is wider than the mode of TO,
5182 we can do the conversion either signed or unsigned. */
5184 for (fmode = GET_MODE (from); fmode != VOIDmode;
5185 fmode = GET_MODE_WIDER_MODE (fmode))
5186 for (imode = GET_MODE (to); imode != VOIDmode;
5187 imode = GET_MODE_WIDER_MODE (imode))
5189 icode = convert_optab_handler (tab, imode, fmode);
5190 if (icode != CODE_FOR_nothing)
5192 rtx last = get_last_insn ();
5193 if (fmode != GET_MODE (from))
5194 from = convert_to_mode (fmode, from, 0);
5196 if (imode != GET_MODE (to))
5197 target = gen_reg_rtx (imode);
5199 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5201 delete_insns_since (last);
5202 continue;
5204 if (target != to)
5205 convert_move (to, target, 0);
5206 return true;
5210 return false;
5213 /* Report whether we have an instruction to perform the operation
5214 specified by CODE on operands of mode MODE. */
5216 have_insn_for (enum rtx_code code, enum machine_mode mode)
5218 return (code_to_optab[(int) code] != 0
5219 && (optab_handler (code_to_optab[(int) code], mode)
5220 != CODE_FOR_nothing));
5223 /* Set all insn_code fields to CODE_FOR_nothing. */
5225 static void
5226 init_insn_codes (void)
5228 memset (optab_table, 0, sizeof (optab_table));
5229 memset (convert_optab_table, 0, sizeof (convert_optab_table));
5230 memset (direct_optab_table, 0, sizeof (direct_optab_table));
5233 /* Initialize OP's code to CODE, and write it into the code_to_optab table. */
5234 static inline void
5235 init_optab (optab op, enum rtx_code code)
5237 op->code = code;
5238 code_to_optab[(int) code] = op;
5241 /* Same, but fill in its code as CODE, and do _not_ write it into
5242 the code_to_optab table. */
5243 static inline void
5244 init_optabv (optab op, enum rtx_code code)
5246 op->code = code;
5249 /* Conversion optabs never go in the code_to_optab table. */
5250 static void
5251 init_convert_optab (convert_optab op, enum rtx_code code)
5253 op->code = code;
5256 /* Initialize the libfunc fields of an entire group of entries in some
5257 optab. Each entry is set equal to a string consisting of a leading
5258 pair of underscores followed by a generic operation name followed by
5259 a mode name (downshifted to lowercase) followed by a single character
5260 representing the number of operands for the given operation (which is
5261 usually one of the characters '2', '3', or '4').
5263 OPTABLE is the table in which libfunc fields are to be initialized.
5264 OPNAME is the generic (string) name of the operation.
5265 SUFFIX is the character which specifies the number of operands for
5266 the given generic operation.
5267 MODE is the mode to generate for.
5270 static void
5271 gen_libfunc (optab optable, const char *opname, int suffix, enum machine_mode mode)
5273 unsigned opname_len = strlen (opname);
5274 const char *mname = GET_MODE_NAME (mode);
5275 unsigned mname_len = strlen (mname);
5276 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5277 int len = prefix_len + opname_len + mname_len + 1 + 1;
5278 char *libfunc_name = XALLOCAVEC (char, len);
5279 char *p;
5280 const char *q;
5282 p = libfunc_name;
5283 *p++ = '_';
5284 *p++ = '_';
5285 if (targetm.libfunc_gnu_prefix)
5287 *p++ = 'g';
5288 *p++ = 'n';
5289 *p++ = 'u';
5290 *p++ = '_';
5292 for (q = opname; *q; )
5293 *p++ = *q++;
5294 for (q = mname; *q; q++)
5295 *p++ = TOLOWER (*q);
5296 *p++ = suffix;
5297 *p = '\0';
5299 set_optab_libfunc (optable, mode,
5300 ggc_alloc_string (libfunc_name, p - libfunc_name));
5303 /* Like gen_libfunc, but verify that integer operation is involved. */
5305 static void
5306 gen_int_libfunc (optab optable, const char *opname, char suffix,
5307 enum machine_mode mode)
5309 int maxsize = 2 * BITS_PER_WORD;
5311 if (GET_MODE_CLASS (mode) != MODE_INT)
5312 return;
5313 if (maxsize < LONG_LONG_TYPE_SIZE)
5314 maxsize = LONG_LONG_TYPE_SIZE;
5315 if (GET_MODE_CLASS (mode) != MODE_INT
5316 || mode < word_mode || GET_MODE_BITSIZE (mode) > maxsize)
5317 return;
5318 gen_libfunc (optable, opname, suffix, mode);
5321 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5323 static void
5324 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5325 enum machine_mode mode)
5327 char *dec_opname;
5329 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5330 gen_libfunc (optable, opname, suffix, mode);
5331 if (DECIMAL_FLOAT_MODE_P (mode))
5333 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5334 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5335 depending on the low level floating format used. */
5336 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5337 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5338 gen_libfunc (optable, dec_opname, suffix, mode);
5342 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5344 static void
5345 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5346 enum machine_mode mode)
5348 if (!ALL_FIXED_POINT_MODE_P (mode))
5349 return;
5350 gen_libfunc (optable, opname, suffix, mode);
5353 /* Like gen_libfunc, but verify that signed fixed-point operation is
5354 involved. */
5356 static void
5357 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5358 enum machine_mode mode)
5360 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5361 return;
5362 gen_libfunc (optable, opname, suffix, mode);
5365 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5366 involved. */
5368 static void
5369 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5370 enum machine_mode mode)
5372 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5373 return;
5374 gen_libfunc (optable, opname, suffix, mode);
5377 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5379 static void
5380 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5381 enum machine_mode mode)
5383 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5384 gen_fp_libfunc (optable, name, suffix, mode);
5385 if (INTEGRAL_MODE_P (mode))
5386 gen_int_libfunc (optable, name, suffix, mode);
5389 /* Like gen_libfunc, but verify that FP or INT operation is involved
5390 and add 'v' suffix for integer operation. */
5392 static void
5393 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5394 enum machine_mode mode)
5396 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5397 gen_fp_libfunc (optable, name, suffix, mode);
5398 if (GET_MODE_CLASS (mode) == MODE_INT)
5400 int len = strlen (name);
5401 char *v_name = XALLOCAVEC (char, len + 2);
5402 strcpy (v_name, name);
5403 v_name[len] = 'v';
5404 v_name[len + 1] = 0;
5405 gen_int_libfunc (optable, v_name, suffix, mode);
5409 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5410 involved. */
5412 static void
5413 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5414 enum machine_mode mode)
5416 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5417 gen_fp_libfunc (optable, name, suffix, mode);
5418 if (INTEGRAL_MODE_P (mode))
5419 gen_int_libfunc (optable, name, suffix, mode);
5420 if (ALL_FIXED_POINT_MODE_P (mode))
5421 gen_fixed_libfunc (optable, name, suffix, mode);
5424 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5425 involved. */
5427 static void
5428 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5429 enum machine_mode mode)
5431 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5432 gen_fp_libfunc (optable, name, suffix, mode);
5433 if (INTEGRAL_MODE_P (mode))
5434 gen_int_libfunc (optable, name, suffix, mode);
5435 if (SIGNED_FIXED_POINT_MODE_P (mode))
5436 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5439 /* Like gen_libfunc, but verify that INT or FIXED operation is
5440 involved. */
5442 static void
5443 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5444 enum machine_mode mode)
5446 if (INTEGRAL_MODE_P (mode))
5447 gen_int_libfunc (optable, name, suffix, mode);
5448 if (ALL_FIXED_POINT_MODE_P (mode))
5449 gen_fixed_libfunc (optable, name, suffix, mode);
5452 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5453 involved. */
5455 static void
5456 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5457 enum machine_mode mode)
5459 if (INTEGRAL_MODE_P (mode))
5460 gen_int_libfunc (optable, name, suffix, mode);
5461 if (SIGNED_FIXED_POINT_MODE_P (mode))
5462 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5465 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5466 involved. */
5468 static void
5469 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5470 enum machine_mode mode)
5472 if (INTEGRAL_MODE_P (mode))
5473 gen_int_libfunc (optable, name, suffix, mode);
5474 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5475 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5478 /* Initialize the libfunc fields of an entire group of entries of an
5479 inter-mode-class conversion optab. The string formation rules are
5480 similar to the ones for init_libfuncs, above, but instead of having
5481 a mode name and an operand count these functions have two mode names
5482 and no operand count. */
5484 static void
5485 gen_interclass_conv_libfunc (convert_optab tab,
5486 const char *opname,
5487 enum machine_mode tmode,
5488 enum machine_mode fmode)
5490 size_t opname_len = strlen (opname);
5491 size_t mname_len = 0;
5493 const char *fname, *tname;
5494 const char *q;
5495 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5496 char *libfunc_name, *suffix;
5497 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5498 char *p;
5500 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5501 depends on which underlying decimal floating point format is used. */
5502 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5504 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5506 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5507 nondec_name[0] = '_';
5508 nondec_name[1] = '_';
5509 if (targetm.libfunc_gnu_prefix)
5511 nondec_name[2] = 'g';
5512 nondec_name[3] = 'n';
5513 nondec_name[4] = 'u';
5514 nondec_name[5] = '_';
5517 memcpy (&nondec_name[prefix_len], opname, opname_len);
5518 nondec_suffix = nondec_name + opname_len + prefix_len;
5520 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5521 dec_name[0] = '_';
5522 dec_name[1] = '_';
5523 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5524 memcpy (&dec_name[2+dec_len], opname, opname_len);
5525 dec_suffix = dec_name + dec_len + opname_len + 2;
5527 fname = GET_MODE_NAME (fmode);
5528 tname = GET_MODE_NAME (tmode);
5530 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5532 libfunc_name = dec_name;
5533 suffix = dec_suffix;
5535 else
5537 libfunc_name = nondec_name;
5538 suffix = nondec_suffix;
5541 p = suffix;
5542 for (q = fname; *q; p++, q++)
5543 *p = TOLOWER (*q);
5544 for (q = tname; *q; p++, q++)
5545 *p = TOLOWER (*q);
5547 *p = '\0';
5549 set_conv_libfunc (tab, tmode, fmode,
5550 ggc_alloc_string (libfunc_name, p - libfunc_name));
5553 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5554 int->fp conversion. */
5556 static void
5557 gen_int_to_fp_conv_libfunc (convert_optab tab,
5558 const char *opname,
5559 enum machine_mode tmode,
5560 enum machine_mode fmode)
5562 if (GET_MODE_CLASS (fmode) != MODE_INT)
5563 return;
5564 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5565 return;
5566 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5569 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5570 naming scheme. */
5572 static void
5573 gen_ufloat_conv_libfunc (convert_optab tab,
5574 const char *opname ATTRIBUTE_UNUSED,
5575 enum machine_mode tmode,
5576 enum machine_mode fmode)
5578 if (DECIMAL_FLOAT_MODE_P (tmode))
5579 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5580 else
5581 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5584 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5585 fp->int conversion. */
5587 static void
5588 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5589 const char *opname,
5590 enum machine_mode tmode,
5591 enum machine_mode fmode)
5593 if (GET_MODE_CLASS (fmode) != MODE_INT)
5594 return;
5595 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5596 return;
5597 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5600 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5601 fp->int conversion with no decimal floating point involved. */
5603 static void
5604 gen_fp_to_int_conv_libfunc (convert_optab tab,
5605 const char *opname,
5606 enum machine_mode tmode,
5607 enum machine_mode fmode)
5609 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5610 return;
5611 if (GET_MODE_CLASS (tmode) != MODE_INT)
5612 return;
5613 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5616 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5617 The string formation rules are
5618 similar to the ones for init_libfunc, above. */
5620 static void
5621 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5622 enum machine_mode tmode, enum machine_mode fmode)
5624 size_t opname_len = strlen (opname);
5625 size_t mname_len = 0;
5627 const char *fname, *tname;
5628 const char *q;
5629 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5630 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5631 char *libfunc_name, *suffix;
5632 char *p;
5634 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5635 depends on which underlying decimal floating point format is used. */
5636 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5638 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5640 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5641 nondec_name[0] = '_';
5642 nondec_name[1] = '_';
5643 if (targetm.libfunc_gnu_prefix)
5645 nondec_name[2] = 'g';
5646 nondec_name[3] = 'n';
5647 nondec_name[4] = 'u';
5648 nondec_name[5] = '_';
5650 memcpy (&nondec_name[prefix_len], opname, opname_len);
5651 nondec_suffix = nondec_name + opname_len + prefix_len;
5653 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5654 dec_name[0] = '_';
5655 dec_name[1] = '_';
5656 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5657 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5658 dec_suffix = dec_name + dec_len + opname_len + 2;
5660 fname = GET_MODE_NAME (fmode);
5661 tname = GET_MODE_NAME (tmode);
5663 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5665 libfunc_name = dec_name;
5666 suffix = dec_suffix;
5668 else
5670 libfunc_name = nondec_name;
5671 suffix = nondec_suffix;
5674 p = suffix;
5675 for (q = fname; *q; p++, q++)
5676 *p = TOLOWER (*q);
5677 for (q = tname; *q; p++, q++)
5678 *p = TOLOWER (*q);
5680 *p++ = '2';
5681 *p = '\0';
5683 set_conv_libfunc (tab, tmode, fmode,
5684 ggc_alloc_string (libfunc_name, p - libfunc_name));
5687 /* Pick proper libcall for trunc_optab. We need to chose if we do
5688 truncation or extension and interclass or intraclass. */
5690 static void
5691 gen_trunc_conv_libfunc (convert_optab tab,
5692 const char *opname,
5693 enum machine_mode tmode,
5694 enum machine_mode fmode)
5696 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5697 return;
5698 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5699 return;
5700 if (tmode == fmode)
5701 return;
5703 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5704 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5705 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5707 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5708 return;
5710 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5711 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5712 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5713 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5716 /* Pick proper libcall for extend_optab. We need to chose if we do
5717 truncation or extension and interclass or intraclass. */
5719 static void
5720 gen_extend_conv_libfunc (convert_optab tab,
5721 const char *opname ATTRIBUTE_UNUSED,
5722 enum machine_mode tmode,
5723 enum machine_mode fmode)
5725 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5726 return;
5727 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5728 return;
5729 if (tmode == fmode)
5730 return;
5732 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5733 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5734 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5736 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5737 return;
5739 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5740 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5741 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5742 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5745 /* Pick proper libcall for fract_optab. We need to chose if we do
5746 interclass or intraclass. */
5748 static void
5749 gen_fract_conv_libfunc (convert_optab tab,
5750 const char *opname,
5751 enum machine_mode tmode,
5752 enum machine_mode fmode)
5754 if (tmode == fmode)
5755 return;
5756 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5757 return;
5759 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5760 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5761 else
5762 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5765 /* Pick proper libcall for fractuns_optab. */
5767 static void
5768 gen_fractuns_conv_libfunc (convert_optab tab,
5769 const char *opname,
5770 enum machine_mode tmode,
5771 enum machine_mode fmode)
5773 if (tmode == fmode)
5774 return;
5775 /* One mode must be a fixed-point mode, and the other must be an integer
5776 mode. */
5777 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
5778 || (ALL_FIXED_POINT_MODE_P (fmode)
5779 && GET_MODE_CLASS (tmode) == MODE_INT)))
5780 return;
5782 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5785 /* Pick proper libcall for satfract_optab. We need to chose if we do
5786 interclass or intraclass. */
5788 static void
5789 gen_satfract_conv_libfunc (convert_optab tab,
5790 const char *opname,
5791 enum machine_mode tmode,
5792 enum machine_mode fmode)
5794 if (tmode == fmode)
5795 return;
5796 /* TMODE must be a fixed-point mode. */
5797 if (!ALL_FIXED_POINT_MODE_P (tmode))
5798 return;
5800 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5801 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5802 else
5803 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5806 /* Pick proper libcall for satfractuns_optab. */
5808 static void
5809 gen_satfractuns_conv_libfunc (convert_optab tab,
5810 const char *opname,
5811 enum machine_mode tmode,
5812 enum machine_mode fmode)
5814 if (tmode == fmode)
5815 return;
5816 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
5817 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
5818 return;
5820 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5823 /* A table of previously-created libfuncs, hashed by name. */
5824 static GTY ((param_is (union tree_node))) htab_t libfunc_decls;
5826 /* Hashtable callbacks for libfunc_decls. */
5828 static hashval_t
5829 libfunc_decl_hash (const void *entry)
5831 return IDENTIFIER_HASH_VALUE (DECL_NAME ((const_tree) entry));
5834 static int
5835 libfunc_decl_eq (const void *entry1, const void *entry2)
5837 return DECL_NAME ((const_tree) entry1) == (const_tree) entry2;
5840 /* Build a decl for a libfunc named NAME. */
5842 tree
5843 build_libfunc_function (const char *name)
5845 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
5846 get_identifier (name),
5847 build_function_type (integer_type_node, NULL_TREE));
5848 /* ??? We don't have any type information except for this is
5849 a function. Pretend this is "int foo()". */
5850 DECL_ARTIFICIAL (decl) = 1;
5851 DECL_EXTERNAL (decl) = 1;
5852 TREE_PUBLIC (decl) = 1;
5853 gcc_assert (DECL_ASSEMBLER_NAME (decl));
5855 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
5856 are the flags assigned by targetm.encode_section_info. */
5857 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
5859 return decl;
5863 init_one_libfunc (const char *name)
5865 tree id, decl;
5866 void **slot;
5867 hashval_t hash;
5869 if (libfunc_decls == NULL)
5870 libfunc_decls = htab_create_ggc (37, libfunc_decl_hash,
5871 libfunc_decl_eq, NULL);
5873 /* See if we have already created a libfunc decl for this function. */
5874 id = get_identifier (name);
5875 hash = IDENTIFIER_HASH_VALUE (id);
5876 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, INSERT);
5877 decl = (tree) *slot;
5878 if (decl == NULL)
5880 /* Create a new decl, so that it can be passed to
5881 targetm.encode_section_info. */
5882 decl = build_libfunc_function (name);
5883 *slot = decl;
5885 return XEXP (DECL_RTL (decl), 0);
5888 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
5891 set_user_assembler_libfunc (const char *name, const char *asmspec)
5893 tree id, decl;
5894 void **slot;
5895 hashval_t hash;
5897 id = get_identifier (name);
5898 hash = IDENTIFIER_HASH_VALUE (id);
5899 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, NO_INSERT);
5900 gcc_assert (slot);
5901 decl = (tree) *slot;
5902 set_user_assembler_name (decl, asmspec);
5903 return XEXP (DECL_RTL (decl), 0);
5906 /* Call this to reset the function entry for one optab (OPTABLE) in mode
5907 MODE to NAME, which should be either 0 or a string constant. */
5908 void
5909 set_optab_libfunc (optab optable, enum machine_mode mode, const char *name)
5911 rtx val;
5912 struct libfunc_entry e;
5913 struct libfunc_entry **slot;
5914 e.optab = (size_t) (optable - &optab_table[0]);
5915 e.mode1 = mode;
5916 e.mode2 = VOIDmode;
5918 if (name)
5919 val = init_one_libfunc (name);
5920 else
5921 val = 0;
5922 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
5923 if (*slot == NULL)
5924 *slot = ggc_alloc_libfunc_entry ();
5925 (*slot)->optab = (size_t) (optable - &optab_table[0]);
5926 (*slot)->mode1 = mode;
5927 (*slot)->mode2 = VOIDmode;
5928 (*slot)->libfunc = val;
5931 /* Call this to reset the function entry for one conversion optab
5932 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
5933 either 0 or a string constant. */
5934 void
5935 set_conv_libfunc (convert_optab optable, enum machine_mode tmode,
5936 enum machine_mode fmode, const char *name)
5938 rtx val;
5939 struct libfunc_entry e;
5940 struct libfunc_entry **slot;
5941 e.optab = (size_t) (optable - &convert_optab_table[0]);
5942 e.mode1 = tmode;
5943 e.mode2 = fmode;
5945 if (name)
5946 val = init_one_libfunc (name);
5947 else
5948 val = 0;
5949 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
5950 if (*slot == NULL)
5951 *slot = ggc_alloc_libfunc_entry ();
5952 (*slot)->optab = (size_t) (optable - &convert_optab_table[0]);
5953 (*slot)->mode1 = tmode;
5954 (*slot)->mode2 = fmode;
5955 (*slot)->libfunc = val;
5958 /* Call this to initialize the contents of the optabs
5959 appropriately for the current target machine. */
5961 void
5962 init_optabs (void)
5964 if (libfunc_hash)
5966 htab_empty (libfunc_hash);
5967 /* We statically initialize the insn_codes with the equivalent of
5968 CODE_FOR_nothing. Repeat the process if reinitialising. */
5969 init_insn_codes ();
5971 else
5972 libfunc_hash = htab_create_ggc (10, hash_libfunc, eq_libfunc, NULL);
5974 init_optab (add_optab, PLUS);
5975 init_optabv (addv_optab, PLUS);
5976 init_optab (sub_optab, MINUS);
5977 init_optabv (subv_optab, MINUS);
5978 init_optab (ssadd_optab, SS_PLUS);
5979 init_optab (usadd_optab, US_PLUS);
5980 init_optab (sssub_optab, SS_MINUS);
5981 init_optab (ussub_optab, US_MINUS);
5982 init_optab (smul_optab, MULT);
5983 init_optab (ssmul_optab, SS_MULT);
5984 init_optab (usmul_optab, US_MULT);
5985 init_optabv (smulv_optab, MULT);
5986 init_optab (smul_highpart_optab, UNKNOWN);
5987 init_optab (umul_highpart_optab, UNKNOWN);
5988 init_optab (smul_widen_optab, UNKNOWN);
5989 init_optab (umul_widen_optab, UNKNOWN);
5990 init_optab (usmul_widen_optab, UNKNOWN);
5991 init_optab (smadd_widen_optab, UNKNOWN);
5992 init_optab (umadd_widen_optab, UNKNOWN);
5993 init_optab (ssmadd_widen_optab, UNKNOWN);
5994 init_optab (usmadd_widen_optab, UNKNOWN);
5995 init_optab (smsub_widen_optab, UNKNOWN);
5996 init_optab (umsub_widen_optab, UNKNOWN);
5997 init_optab (ssmsub_widen_optab, UNKNOWN);
5998 init_optab (usmsub_widen_optab, UNKNOWN);
5999 init_optab (sdiv_optab, DIV);
6000 init_optab (ssdiv_optab, SS_DIV);
6001 init_optab (usdiv_optab, US_DIV);
6002 init_optabv (sdivv_optab, DIV);
6003 init_optab (sdivmod_optab, UNKNOWN);
6004 init_optab (udiv_optab, UDIV);
6005 init_optab (udivmod_optab, UNKNOWN);
6006 init_optab (smod_optab, MOD);
6007 init_optab (umod_optab, UMOD);
6008 init_optab (fmod_optab, UNKNOWN);
6009 init_optab (remainder_optab, UNKNOWN);
6010 init_optab (ftrunc_optab, UNKNOWN);
6011 init_optab (and_optab, AND);
6012 init_optab (ior_optab, IOR);
6013 init_optab (xor_optab, XOR);
6014 init_optab (ashl_optab, ASHIFT);
6015 init_optab (ssashl_optab, SS_ASHIFT);
6016 init_optab (usashl_optab, US_ASHIFT);
6017 init_optab (ashr_optab, ASHIFTRT);
6018 init_optab (lshr_optab, LSHIFTRT);
6019 init_optabv (vashl_optab, ASHIFT);
6020 init_optabv (vashr_optab, ASHIFTRT);
6021 init_optabv (vlshr_optab, LSHIFTRT);
6022 init_optab (rotl_optab, ROTATE);
6023 init_optab (rotr_optab, ROTATERT);
6024 init_optab (smin_optab, SMIN);
6025 init_optab (smax_optab, SMAX);
6026 init_optab (umin_optab, UMIN);
6027 init_optab (umax_optab, UMAX);
6028 init_optab (pow_optab, UNKNOWN);
6029 init_optab (atan2_optab, UNKNOWN);
6030 init_optab (fma_optab, FMA);
6031 init_optab (fms_optab, UNKNOWN);
6032 init_optab (fnma_optab, UNKNOWN);
6033 init_optab (fnms_optab, UNKNOWN);
6035 /* These three have codes assigned exclusively for the sake of
6036 have_insn_for. */
6037 init_optab (mov_optab, SET);
6038 init_optab (movstrict_optab, STRICT_LOW_PART);
6039 init_optab (cbranch_optab, COMPARE);
6041 init_optab (cmov_optab, UNKNOWN);
6042 init_optab (cstore_optab, UNKNOWN);
6043 init_optab (ctrap_optab, UNKNOWN);
6045 init_optab (storent_optab, UNKNOWN);
6047 init_optab (cmp_optab, UNKNOWN);
6048 init_optab (ucmp_optab, UNKNOWN);
6050 init_optab (eq_optab, EQ);
6051 init_optab (ne_optab, NE);
6052 init_optab (gt_optab, GT);
6053 init_optab (ge_optab, GE);
6054 init_optab (lt_optab, LT);
6055 init_optab (le_optab, LE);
6056 init_optab (unord_optab, UNORDERED);
6058 init_optab (neg_optab, NEG);
6059 init_optab (ssneg_optab, SS_NEG);
6060 init_optab (usneg_optab, US_NEG);
6061 init_optabv (negv_optab, NEG);
6062 init_optab (abs_optab, ABS);
6063 init_optabv (absv_optab, ABS);
6064 init_optab (addcc_optab, UNKNOWN);
6065 init_optab (one_cmpl_optab, NOT);
6066 init_optab (bswap_optab, BSWAP);
6067 init_optab (ffs_optab, FFS);
6068 init_optab (clz_optab, CLZ);
6069 init_optab (ctz_optab, CTZ);
6070 init_optab (clrsb_optab, CLRSB);
6071 init_optab (popcount_optab, POPCOUNT);
6072 init_optab (parity_optab, PARITY);
6073 init_optab (sqrt_optab, SQRT);
6074 init_optab (floor_optab, UNKNOWN);
6075 init_optab (ceil_optab, UNKNOWN);
6076 init_optab (round_optab, UNKNOWN);
6077 init_optab (btrunc_optab, UNKNOWN);
6078 init_optab (nearbyint_optab, UNKNOWN);
6079 init_optab (rint_optab, UNKNOWN);
6080 init_optab (sincos_optab, UNKNOWN);
6081 init_optab (sin_optab, UNKNOWN);
6082 init_optab (asin_optab, UNKNOWN);
6083 init_optab (cos_optab, UNKNOWN);
6084 init_optab (acos_optab, UNKNOWN);
6085 init_optab (exp_optab, UNKNOWN);
6086 init_optab (exp10_optab, UNKNOWN);
6087 init_optab (exp2_optab, UNKNOWN);
6088 init_optab (expm1_optab, UNKNOWN);
6089 init_optab (ldexp_optab, UNKNOWN);
6090 init_optab (scalb_optab, UNKNOWN);
6091 init_optab (significand_optab, UNKNOWN);
6092 init_optab (logb_optab, UNKNOWN);
6093 init_optab (ilogb_optab, UNKNOWN);
6094 init_optab (log_optab, UNKNOWN);
6095 init_optab (log10_optab, UNKNOWN);
6096 init_optab (log2_optab, UNKNOWN);
6097 init_optab (log1p_optab, UNKNOWN);
6098 init_optab (tan_optab, UNKNOWN);
6099 init_optab (atan_optab, UNKNOWN);
6100 init_optab (copysign_optab, UNKNOWN);
6101 init_optab (signbit_optab, UNKNOWN);
6103 init_optab (isinf_optab, UNKNOWN);
6105 init_optab (strlen_optab, UNKNOWN);
6106 init_optab (push_optab, UNKNOWN);
6108 init_optab (reduc_smax_optab, UNKNOWN);
6109 init_optab (reduc_umax_optab, UNKNOWN);
6110 init_optab (reduc_smin_optab, UNKNOWN);
6111 init_optab (reduc_umin_optab, UNKNOWN);
6112 init_optab (reduc_splus_optab, UNKNOWN);
6113 init_optab (reduc_uplus_optab, UNKNOWN);
6115 init_optab (ssum_widen_optab, UNKNOWN);
6116 init_optab (usum_widen_optab, UNKNOWN);
6117 init_optab (sdot_prod_optab, UNKNOWN);
6118 init_optab (udot_prod_optab, UNKNOWN);
6120 init_optab (vec_extract_optab, UNKNOWN);
6121 init_optab (vec_extract_even_optab, UNKNOWN);
6122 init_optab (vec_extract_odd_optab, UNKNOWN);
6123 init_optab (vec_interleave_high_optab, UNKNOWN);
6124 init_optab (vec_interleave_low_optab, UNKNOWN);
6125 init_optab (vec_set_optab, UNKNOWN);
6126 init_optab (vec_init_optab, UNKNOWN);
6127 init_optab (vec_shl_optab, UNKNOWN);
6128 init_optab (vec_shr_optab, UNKNOWN);
6129 init_optab (vec_realign_load_optab, UNKNOWN);
6130 init_optab (movmisalign_optab, UNKNOWN);
6131 init_optab (vec_widen_umult_hi_optab, UNKNOWN);
6132 init_optab (vec_widen_umult_lo_optab, UNKNOWN);
6133 init_optab (vec_widen_smult_hi_optab, UNKNOWN);
6134 init_optab (vec_widen_smult_lo_optab, UNKNOWN);
6135 init_optab (vec_unpacks_hi_optab, UNKNOWN);
6136 init_optab (vec_unpacks_lo_optab, UNKNOWN);
6137 init_optab (vec_unpacku_hi_optab, UNKNOWN);
6138 init_optab (vec_unpacku_lo_optab, UNKNOWN);
6139 init_optab (vec_unpacks_float_hi_optab, UNKNOWN);
6140 init_optab (vec_unpacks_float_lo_optab, UNKNOWN);
6141 init_optab (vec_unpacku_float_hi_optab, UNKNOWN);
6142 init_optab (vec_unpacku_float_lo_optab, UNKNOWN);
6143 init_optab (vec_pack_trunc_optab, UNKNOWN);
6144 init_optab (vec_pack_usat_optab, UNKNOWN);
6145 init_optab (vec_pack_ssat_optab, UNKNOWN);
6146 init_optab (vec_pack_ufix_trunc_optab, UNKNOWN);
6147 init_optab (vec_pack_sfix_trunc_optab, UNKNOWN);
6149 init_optab (powi_optab, UNKNOWN);
6151 /* Conversions. */
6152 init_convert_optab (sext_optab, SIGN_EXTEND);
6153 init_convert_optab (zext_optab, ZERO_EXTEND);
6154 init_convert_optab (trunc_optab, TRUNCATE);
6155 init_convert_optab (sfix_optab, FIX);
6156 init_convert_optab (ufix_optab, UNSIGNED_FIX);
6157 init_convert_optab (sfixtrunc_optab, UNKNOWN);
6158 init_convert_optab (ufixtrunc_optab, UNKNOWN);
6159 init_convert_optab (sfloat_optab, FLOAT);
6160 init_convert_optab (ufloat_optab, UNSIGNED_FLOAT);
6161 init_convert_optab (lrint_optab, UNKNOWN);
6162 init_convert_optab (lround_optab, UNKNOWN);
6163 init_convert_optab (lfloor_optab, UNKNOWN);
6164 init_convert_optab (lceil_optab, UNKNOWN);
6166 init_convert_optab (fract_optab, FRACT_CONVERT);
6167 init_convert_optab (fractuns_optab, UNSIGNED_FRACT_CONVERT);
6168 init_convert_optab (satfract_optab, SAT_FRACT);
6169 init_convert_optab (satfractuns_optab, UNSIGNED_SAT_FRACT);
6171 /* Fill in the optabs with the insns we support. */
6172 init_all_optabs ();
6174 /* Initialize the optabs with the names of the library functions. */
6175 add_optab->libcall_basename = "add";
6176 add_optab->libcall_suffix = '3';
6177 add_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6178 addv_optab->libcall_basename = "add";
6179 addv_optab->libcall_suffix = '3';
6180 addv_optab->libcall_gen = gen_intv_fp_libfunc;
6181 ssadd_optab->libcall_basename = "ssadd";
6182 ssadd_optab->libcall_suffix = '3';
6183 ssadd_optab->libcall_gen = gen_signed_fixed_libfunc;
6184 usadd_optab->libcall_basename = "usadd";
6185 usadd_optab->libcall_suffix = '3';
6186 usadd_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6187 sub_optab->libcall_basename = "sub";
6188 sub_optab->libcall_suffix = '3';
6189 sub_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6190 subv_optab->libcall_basename = "sub";
6191 subv_optab->libcall_suffix = '3';
6192 subv_optab->libcall_gen = gen_intv_fp_libfunc;
6193 sssub_optab->libcall_basename = "sssub";
6194 sssub_optab->libcall_suffix = '3';
6195 sssub_optab->libcall_gen = gen_signed_fixed_libfunc;
6196 ussub_optab->libcall_basename = "ussub";
6197 ussub_optab->libcall_suffix = '3';
6198 ussub_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6199 smul_optab->libcall_basename = "mul";
6200 smul_optab->libcall_suffix = '3';
6201 smul_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6202 smulv_optab->libcall_basename = "mul";
6203 smulv_optab->libcall_suffix = '3';
6204 smulv_optab->libcall_gen = gen_intv_fp_libfunc;
6205 ssmul_optab->libcall_basename = "ssmul";
6206 ssmul_optab->libcall_suffix = '3';
6207 ssmul_optab->libcall_gen = gen_signed_fixed_libfunc;
6208 usmul_optab->libcall_basename = "usmul";
6209 usmul_optab->libcall_suffix = '3';
6210 usmul_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6211 sdiv_optab->libcall_basename = "div";
6212 sdiv_optab->libcall_suffix = '3';
6213 sdiv_optab->libcall_gen = gen_int_fp_signed_fixed_libfunc;
6214 sdivv_optab->libcall_basename = "divv";
6215 sdivv_optab->libcall_suffix = '3';
6216 sdivv_optab->libcall_gen = gen_int_libfunc;
6217 ssdiv_optab->libcall_basename = "ssdiv";
6218 ssdiv_optab->libcall_suffix = '3';
6219 ssdiv_optab->libcall_gen = gen_signed_fixed_libfunc;
6220 udiv_optab->libcall_basename = "udiv";
6221 udiv_optab->libcall_suffix = '3';
6222 udiv_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6223 usdiv_optab->libcall_basename = "usdiv";
6224 usdiv_optab->libcall_suffix = '3';
6225 usdiv_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6226 sdivmod_optab->libcall_basename = "divmod";
6227 sdivmod_optab->libcall_suffix = '4';
6228 sdivmod_optab->libcall_gen = gen_int_libfunc;
6229 udivmod_optab->libcall_basename = "udivmod";
6230 udivmod_optab->libcall_suffix = '4';
6231 udivmod_optab->libcall_gen = gen_int_libfunc;
6232 smod_optab->libcall_basename = "mod";
6233 smod_optab->libcall_suffix = '3';
6234 smod_optab->libcall_gen = gen_int_libfunc;
6235 umod_optab->libcall_basename = "umod";
6236 umod_optab->libcall_suffix = '3';
6237 umod_optab->libcall_gen = gen_int_libfunc;
6238 ftrunc_optab->libcall_basename = "ftrunc";
6239 ftrunc_optab->libcall_suffix = '2';
6240 ftrunc_optab->libcall_gen = gen_fp_libfunc;
6241 and_optab->libcall_basename = "and";
6242 and_optab->libcall_suffix = '3';
6243 and_optab->libcall_gen = gen_int_libfunc;
6244 ior_optab->libcall_basename = "ior";
6245 ior_optab->libcall_suffix = '3';
6246 ior_optab->libcall_gen = gen_int_libfunc;
6247 xor_optab->libcall_basename = "xor";
6248 xor_optab->libcall_suffix = '3';
6249 xor_optab->libcall_gen = gen_int_libfunc;
6250 ashl_optab->libcall_basename = "ashl";
6251 ashl_optab->libcall_suffix = '3';
6252 ashl_optab->libcall_gen = gen_int_fixed_libfunc;
6253 ssashl_optab->libcall_basename = "ssashl";
6254 ssashl_optab->libcall_suffix = '3';
6255 ssashl_optab->libcall_gen = gen_signed_fixed_libfunc;
6256 usashl_optab->libcall_basename = "usashl";
6257 usashl_optab->libcall_suffix = '3';
6258 usashl_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6259 ashr_optab->libcall_basename = "ashr";
6260 ashr_optab->libcall_suffix = '3';
6261 ashr_optab->libcall_gen = gen_int_signed_fixed_libfunc;
6262 lshr_optab->libcall_basename = "lshr";
6263 lshr_optab->libcall_suffix = '3';
6264 lshr_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6265 smin_optab->libcall_basename = "min";
6266 smin_optab->libcall_suffix = '3';
6267 smin_optab->libcall_gen = gen_int_fp_libfunc;
6268 smax_optab->libcall_basename = "max";
6269 smax_optab->libcall_suffix = '3';
6270 smax_optab->libcall_gen = gen_int_fp_libfunc;
6271 umin_optab->libcall_basename = "umin";
6272 umin_optab->libcall_suffix = '3';
6273 umin_optab->libcall_gen = gen_int_libfunc;
6274 umax_optab->libcall_basename = "umax";
6275 umax_optab->libcall_suffix = '3';
6276 umax_optab->libcall_gen = gen_int_libfunc;
6277 neg_optab->libcall_basename = "neg";
6278 neg_optab->libcall_suffix = '2';
6279 neg_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6280 ssneg_optab->libcall_basename = "ssneg";
6281 ssneg_optab->libcall_suffix = '2';
6282 ssneg_optab->libcall_gen = gen_signed_fixed_libfunc;
6283 usneg_optab->libcall_basename = "usneg";
6284 usneg_optab->libcall_suffix = '2';
6285 usneg_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6286 negv_optab->libcall_basename = "neg";
6287 negv_optab->libcall_suffix = '2';
6288 negv_optab->libcall_gen = gen_intv_fp_libfunc;
6289 one_cmpl_optab->libcall_basename = "one_cmpl";
6290 one_cmpl_optab->libcall_suffix = '2';
6291 one_cmpl_optab->libcall_gen = gen_int_libfunc;
6292 ffs_optab->libcall_basename = "ffs";
6293 ffs_optab->libcall_suffix = '2';
6294 ffs_optab->libcall_gen = gen_int_libfunc;
6295 clz_optab->libcall_basename = "clz";
6296 clz_optab->libcall_suffix = '2';
6297 clz_optab->libcall_gen = gen_int_libfunc;
6298 ctz_optab->libcall_basename = "ctz";
6299 ctz_optab->libcall_suffix = '2';
6300 ctz_optab->libcall_gen = gen_int_libfunc;
6301 clrsb_optab->libcall_basename = "clrsb";
6302 clrsb_optab->libcall_suffix = '2';
6303 clrsb_optab->libcall_gen = gen_int_libfunc;
6304 popcount_optab->libcall_basename = "popcount";
6305 popcount_optab->libcall_suffix = '2';
6306 popcount_optab->libcall_gen = gen_int_libfunc;
6307 parity_optab->libcall_basename = "parity";
6308 parity_optab->libcall_suffix = '2';
6309 parity_optab->libcall_gen = gen_int_libfunc;
6311 /* Comparison libcalls for integers MUST come in pairs,
6312 signed/unsigned. */
6313 cmp_optab->libcall_basename = "cmp";
6314 cmp_optab->libcall_suffix = '2';
6315 cmp_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6316 ucmp_optab->libcall_basename = "ucmp";
6317 ucmp_optab->libcall_suffix = '2';
6318 ucmp_optab->libcall_gen = gen_int_libfunc;
6320 /* EQ etc are floating point only. */
6321 eq_optab->libcall_basename = "eq";
6322 eq_optab->libcall_suffix = '2';
6323 eq_optab->libcall_gen = gen_fp_libfunc;
6324 ne_optab->libcall_basename = "ne";
6325 ne_optab->libcall_suffix = '2';
6326 ne_optab->libcall_gen = gen_fp_libfunc;
6327 gt_optab->libcall_basename = "gt";
6328 gt_optab->libcall_suffix = '2';
6329 gt_optab->libcall_gen = gen_fp_libfunc;
6330 ge_optab->libcall_basename = "ge";
6331 ge_optab->libcall_suffix = '2';
6332 ge_optab->libcall_gen = gen_fp_libfunc;
6333 lt_optab->libcall_basename = "lt";
6334 lt_optab->libcall_suffix = '2';
6335 lt_optab->libcall_gen = gen_fp_libfunc;
6336 le_optab->libcall_basename = "le";
6337 le_optab->libcall_suffix = '2';
6338 le_optab->libcall_gen = gen_fp_libfunc;
6339 unord_optab->libcall_basename = "unord";
6340 unord_optab->libcall_suffix = '2';
6341 unord_optab->libcall_gen = gen_fp_libfunc;
6343 powi_optab->libcall_basename = "powi";
6344 powi_optab->libcall_suffix = '2';
6345 powi_optab->libcall_gen = gen_fp_libfunc;
6347 /* Conversions. */
6348 sfloat_optab->libcall_basename = "float";
6349 sfloat_optab->libcall_gen = gen_int_to_fp_conv_libfunc;
6350 ufloat_optab->libcall_gen = gen_ufloat_conv_libfunc;
6351 sfix_optab->libcall_basename = "fix";
6352 sfix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6353 ufix_optab->libcall_basename = "fixuns";
6354 ufix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6355 lrint_optab->libcall_basename = "lrint";
6356 lrint_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6357 lround_optab->libcall_basename = "lround";
6358 lround_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6359 lfloor_optab->libcall_basename = "lfloor";
6360 lfloor_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6361 lceil_optab->libcall_basename = "lceil";
6362 lceil_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6364 /* trunc_optab is also used for FLOAT_EXTEND. */
6365 sext_optab->libcall_basename = "extend";
6366 sext_optab->libcall_gen = gen_extend_conv_libfunc;
6367 trunc_optab->libcall_basename = "trunc";
6368 trunc_optab->libcall_gen = gen_trunc_conv_libfunc;
6370 /* Conversions for fixed-point modes and other modes. */
6371 fract_optab->libcall_basename = "fract";
6372 fract_optab->libcall_gen = gen_fract_conv_libfunc;
6373 satfract_optab->libcall_basename = "satfract";
6374 satfract_optab->libcall_gen = gen_satfract_conv_libfunc;
6375 fractuns_optab->libcall_basename = "fractuns";
6376 fractuns_optab->libcall_gen = gen_fractuns_conv_libfunc;
6377 satfractuns_optab->libcall_basename = "satfractuns";
6378 satfractuns_optab->libcall_gen = gen_satfractuns_conv_libfunc;
6380 /* The ffs function operates on `int'. Fall back on it if we do not
6381 have a libgcc2 function for that width. */
6382 if (INT_TYPE_SIZE < BITS_PER_WORD)
6383 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6384 "ffs");
6386 /* Explicitly initialize the bswap libfuncs since we need them to be
6387 valid for things other than word_mode. */
6388 if (targetm.libfunc_gnu_prefix)
6390 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6391 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6393 else
6395 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6396 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6399 /* Use cabs for double complex abs, since systems generally have cabs.
6400 Don't define any libcall for float complex, so that cabs will be used. */
6401 if (complex_double_type_node)
6402 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node), "cabs");
6404 abort_libfunc = init_one_libfunc ("abort");
6405 memcpy_libfunc = init_one_libfunc ("memcpy");
6406 memmove_libfunc = init_one_libfunc ("memmove");
6407 memcmp_libfunc = init_one_libfunc ("memcmp");
6408 memset_libfunc = init_one_libfunc ("memset");
6409 setbits_libfunc = init_one_libfunc ("__setbits");
6411 #ifndef DONT_USE_BUILTIN_SETJMP
6412 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6413 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6414 #else
6415 setjmp_libfunc = init_one_libfunc ("setjmp");
6416 longjmp_libfunc = init_one_libfunc ("longjmp");
6417 #endif
6418 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6419 unwind_sjlj_unregister_libfunc
6420 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6422 /* For function entry/exit instrumentation. */
6423 profile_function_entry_libfunc
6424 = init_one_libfunc ("__cyg_profile_func_enter");
6425 profile_function_exit_libfunc
6426 = init_one_libfunc ("__cyg_profile_func_exit");
6428 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6430 /* Allow the target to add more libcalls or rename some, etc. */
6431 targetm.init_libfuncs ();
6434 /* Print information about the current contents of the optabs on
6435 STDERR. */
6437 DEBUG_FUNCTION void
6438 debug_optab_libfuncs (void)
6440 int i;
6441 int j;
6442 int k;
6444 /* Dump the arithmetic optabs. */
6445 for (i = 0; i != (int) OTI_MAX; i++)
6446 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6448 optab o;
6449 rtx l;
6451 o = &optab_table[i];
6452 l = optab_libfunc (o, (enum machine_mode) j);
6453 if (l)
6455 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6456 fprintf (stderr, "%s\t%s:\t%s\n",
6457 GET_RTX_NAME (o->code),
6458 GET_MODE_NAME (j),
6459 XSTR (l, 0));
6463 /* Dump the conversion optabs. */
6464 for (i = 0; i < (int) COI_MAX; ++i)
6465 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6466 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6468 convert_optab o;
6469 rtx l;
6471 o = &convert_optab_table[i];
6472 l = convert_optab_libfunc (o, (enum machine_mode) j,
6473 (enum machine_mode) k);
6474 if (l)
6476 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6477 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6478 GET_RTX_NAME (o->code),
6479 GET_MODE_NAME (j),
6480 GET_MODE_NAME (k),
6481 XSTR (l, 0));
6487 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6488 CODE. Return 0 on failure. */
6491 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6493 enum machine_mode mode = GET_MODE (op1);
6494 enum insn_code icode;
6495 rtx insn;
6496 rtx trap_rtx;
6498 if (mode == VOIDmode)
6499 return 0;
6501 icode = optab_handler (ctrap_optab, mode);
6502 if (icode == CODE_FOR_nothing)
6503 return 0;
6505 /* Some targets only accept a zero trap code. */
6506 if (!insn_operand_matches (icode, 3, tcode))
6507 return 0;
6509 do_pending_stack_adjust ();
6510 start_sequence ();
6511 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6512 &trap_rtx, &mode);
6513 if (!trap_rtx)
6514 insn = NULL_RTX;
6515 else
6516 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6517 tcode);
6519 /* If that failed, then give up. */
6520 if (insn == 0)
6522 end_sequence ();
6523 return 0;
6526 emit_insn (insn);
6527 insn = get_insns ();
6528 end_sequence ();
6529 return insn;
6532 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6533 or unsigned operation code. */
6535 static enum rtx_code
6536 get_rtx_code (enum tree_code tcode, bool unsignedp)
6538 enum rtx_code code;
6539 switch (tcode)
6541 case EQ_EXPR:
6542 code = EQ;
6543 break;
6544 case NE_EXPR:
6545 code = NE;
6546 break;
6547 case LT_EXPR:
6548 code = unsignedp ? LTU : LT;
6549 break;
6550 case LE_EXPR:
6551 code = unsignedp ? LEU : LE;
6552 break;
6553 case GT_EXPR:
6554 code = unsignedp ? GTU : GT;
6555 break;
6556 case GE_EXPR:
6557 code = unsignedp ? GEU : GE;
6558 break;
6560 case UNORDERED_EXPR:
6561 code = UNORDERED;
6562 break;
6563 case ORDERED_EXPR:
6564 code = ORDERED;
6565 break;
6566 case UNLT_EXPR:
6567 code = UNLT;
6568 break;
6569 case UNLE_EXPR:
6570 code = UNLE;
6571 break;
6572 case UNGT_EXPR:
6573 code = UNGT;
6574 break;
6575 case UNGE_EXPR:
6576 code = UNGE;
6577 break;
6578 case UNEQ_EXPR:
6579 code = UNEQ;
6580 break;
6581 case LTGT_EXPR:
6582 code = LTGT;
6583 break;
6585 default:
6586 gcc_unreachable ();
6588 return code;
6591 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6592 unsigned operators. Do not generate compare instruction. */
6594 static rtx
6595 vector_compare_rtx (tree cond, bool unsignedp, enum insn_code icode)
6597 struct expand_operand ops[2];
6598 enum rtx_code rcode;
6599 tree t_op0, t_op1;
6600 rtx rtx_op0, rtx_op1;
6602 /* This is unlikely. While generating VEC_COND_EXPR, auto vectorizer
6603 ensures that condition is a relational operation. */
6604 gcc_assert (COMPARISON_CLASS_P (cond));
6606 rcode = get_rtx_code (TREE_CODE (cond), unsignedp);
6607 t_op0 = TREE_OPERAND (cond, 0);
6608 t_op1 = TREE_OPERAND (cond, 1);
6610 /* Expand operands. */
6611 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6612 EXPAND_STACK_PARM);
6613 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6614 EXPAND_STACK_PARM);
6616 create_input_operand (&ops[0], rtx_op0, GET_MODE (rtx_op0));
6617 create_input_operand (&ops[1], rtx_op1, GET_MODE (rtx_op1));
6618 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6619 gcc_unreachable ();
6620 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6623 /* Return insn code for TYPE, the type of a VEC_COND_EXPR. */
6625 static inline enum insn_code
6626 get_vcond_icode (tree type, enum machine_mode mode)
6628 enum insn_code icode = CODE_FOR_nothing;
6630 if (TYPE_UNSIGNED (type))
6631 icode = direct_optab_handler (vcondu_optab, mode);
6632 else
6633 icode = direct_optab_handler (vcond_optab, mode);
6634 return icode;
6637 /* Return TRUE iff, appropriate vector insns are available
6638 for vector cond expr with type TYPE in VMODE mode. */
6640 bool
6641 expand_vec_cond_expr_p (tree type, enum machine_mode vmode)
6643 if (get_vcond_icode (type, vmode) == CODE_FOR_nothing)
6644 return false;
6645 return true;
6648 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6649 three operands. */
6652 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6653 rtx target)
6655 struct expand_operand ops[6];
6656 enum insn_code icode;
6657 rtx comparison, rtx_op1, rtx_op2;
6658 enum machine_mode mode = TYPE_MODE (vec_cond_type);
6659 bool unsignedp = TYPE_UNSIGNED (vec_cond_type);
6661 icode = get_vcond_icode (vec_cond_type, mode);
6662 if (icode == CODE_FOR_nothing)
6663 return 0;
6665 comparison = vector_compare_rtx (op0, unsignedp, icode);
6666 rtx_op1 = expand_normal (op1);
6667 rtx_op2 = expand_normal (op2);
6669 create_output_operand (&ops[0], target, mode);
6670 create_input_operand (&ops[1], rtx_op1, mode);
6671 create_input_operand (&ops[2], rtx_op2, mode);
6672 create_fixed_operand (&ops[3], comparison);
6673 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6674 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6675 expand_insn (icode, 6, ops);
6676 return ops[0].value;
6680 /* This is an internal subroutine of the other compare_and_swap expanders.
6681 MEM, OLD_VAL and NEW_VAL are as you'd expect for a compare-and-swap
6682 operation. TARGET is an optional place to store the value result of
6683 the operation. ICODE is the particular instruction to expand. Return
6684 the result of the operation. */
6686 static rtx
6687 expand_val_compare_and_swap_1 (rtx mem, rtx old_val, rtx new_val,
6688 rtx target, enum insn_code icode)
6690 struct expand_operand ops[4];
6691 enum machine_mode mode = GET_MODE (mem);
6693 create_output_operand (&ops[0], target, mode);
6694 create_fixed_operand (&ops[1], mem);
6695 /* OLD_VAL and NEW_VAL may have been promoted to a wider mode.
6696 Shrink them if so. */
6697 create_convert_operand_to (&ops[2], old_val, mode, true);
6698 create_convert_operand_to (&ops[3], new_val, mode, true);
6699 if (maybe_expand_insn (icode, 4, ops))
6700 return ops[0].value;
6701 return NULL_RTX;
6704 /* Expand a compare-and-swap operation and return its value. */
6707 expand_val_compare_and_swap (rtx mem, rtx old_val, rtx new_val, rtx target)
6709 enum machine_mode mode = GET_MODE (mem);
6710 enum insn_code icode
6711 = direct_optab_handler (sync_compare_and_swap_optab, mode);
6713 if (icode == CODE_FOR_nothing)
6714 return NULL_RTX;
6716 return expand_val_compare_and_swap_1 (mem, old_val, new_val, target, icode);
6719 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
6720 pattern. */
6722 static void
6723 find_cc_set (rtx x, const_rtx pat, void *data)
6725 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
6726 && GET_CODE (pat) == SET)
6728 rtx *p_cc_reg = (rtx *) data;
6729 gcc_assert (!*p_cc_reg);
6730 *p_cc_reg = x;
6734 /* Expand a compare-and-swap operation and store true into the result if
6735 the operation was successful and false otherwise. Return the result.
6736 Unlike other routines, TARGET is not optional. */
6739 expand_bool_compare_and_swap (rtx mem, rtx old_val, rtx new_val, rtx target)
6741 enum machine_mode mode = GET_MODE (mem);
6742 enum insn_code icode;
6743 rtx subtarget, seq, cc_reg;
6745 /* If the target supports a compare-and-swap pattern that simultaneously
6746 sets some flag for success, then use it. Otherwise use the regular
6747 compare-and-swap and follow that immediately with a compare insn. */
6748 icode = direct_optab_handler (sync_compare_and_swap_optab, mode);
6749 if (icode == CODE_FOR_nothing)
6750 return NULL_RTX;
6752 do_pending_stack_adjust ();
6755 start_sequence ();
6756 subtarget = expand_val_compare_and_swap_1 (mem, old_val, new_val,
6757 NULL_RTX, icode);
6758 cc_reg = NULL_RTX;
6759 if (subtarget == NULL_RTX)
6761 end_sequence ();
6762 return NULL_RTX;
6765 if (have_insn_for (COMPARE, CCmode))
6766 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
6767 seq = get_insns ();
6768 end_sequence ();
6770 /* We might be comparing against an old value. Try again. :-( */
6771 if (!cc_reg && MEM_P (old_val))
6773 seq = NULL_RTX;
6774 old_val = force_reg (mode, old_val);
6777 while (!seq);
6779 emit_insn (seq);
6780 if (cc_reg)
6781 return emit_store_flag_force (target, EQ, cc_reg, const0_rtx, VOIDmode, 0, 1);
6782 else
6783 return emit_store_flag_force (target, EQ, subtarget, old_val, VOIDmode, 1, 1);
6786 /* This is a helper function for the other atomic operations. This function
6787 emits a loop that contains SEQ that iterates until a compare-and-swap
6788 operation at the end succeeds. MEM is the memory to be modified. SEQ is
6789 a set of instructions that takes a value from OLD_REG as an input and
6790 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
6791 set to the current contents of MEM. After SEQ, a compare-and-swap will
6792 attempt to update MEM with NEW_REG. The function returns true when the
6793 loop was generated successfully. */
6795 static bool
6796 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
6798 enum machine_mode mode = GET_MODE (mem);
6799 enum insn_code icode;
6800 rtx label, cmp_reg, subtarget, cc_reg;
6802 /* The loop we want to generate looks like
6804 cmp_reg = mem;
6805 label:
6806 old_reg = cmp_reg;
6807 seq;
6808 cmp_reg = compare-and-swap(mem, old_reg, new_reg)
6809 if (cmp_reg != old_reg)
6810 goto label;
6812 Note that we only do the plain load from memory once. Subsequent
6813 iterations use the value loaded by the compare-and-swap pattern. */
6815 label = gen_label_rtx ();
6816 cmp_reg = gen_reg_rtx (mode);
6818 emit_move_insn (cmp_reg, mem);
6819 emit_label (label);
6820 emit_move_insn (old_reg, cmp_reg);
6821 if (seq)
6822 emit_insn (seq);
6824 /* If the target supports a compare-and-swap pattern that simultaneously
6825 sets some flag for success, then use it. Otherwise use the regular
6826 compare-and-swap and follow that immediately with a compare insn. */
6827 icode = direct_optab_handler (sync_compare_and_swap_optab, mode);
6828 if (icode == CODE_FOR_nothing)
6829 return false;
6831 subtarget = expand_val_compare_and_swap_1 (mem, old_reg, new_reg,
6832 cmp_reg, icode);
6833 if (subtarget == NULL_RTX)
6834 return false;
6836 cc_reg = NULL_RTX;
6837 if (have_insn_for (COMPARE, CCmode))
6838 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
6839 if (cc_reg)
6841 cmp_reg = cc_reg;
6842 old_reg = const0_rtx;
6844 else
6846 if (subtarget != cmp_reg)
6847 emit_move_insn (cmp_reg, subtarget);
6850 /* ??? Mark this jump predicted not taken? */
6851 emit_cmp_and_jump_insns (cmp_reg, old_reg, NE, const0_rtx, GET_MODE (cmp_reg), 1,
6852 label);
6853 return true;
6856 /* This function generates the atomic operation MEM CODE= VAL. In this
6857 case, we do not care about any resulting value. Returns NULL if we
6858 cannot generate the operation. */
6861 expand_sync_operation (rtx mem, rtx val, enum rtx_code code)
6863 enum machine_mode mode = GET_MODE (mem);
6864 enum insn_code icode;
6865 rtx insn;
6867 /* Look to see if the target supports the operation directly. */
6868 switch (code)
6870 case PLUS:
6871 icode = direct_optab_handler (sync_add_optab, mode);
6872 break;
6873 case IOR:
6874 icode = direct_optab_handler (sync_ior_optab, mode);
6875 break;
6876 case XOR:
6877 icode = direct_optab_handler (sync_xor_optab, mode);
6878 break;
6879 case AND:
6880 icode = direct_optab_handler (sync_and_optab, mode);
6881 break;
6882 case NOT:
6883 icode = direct_optab_handler (sync_nand_optab, mode);
6884 break;
6886 case MINUS:
6887 icode = direct_optab_handler (sync_sub_optab, mode);
6888 if (icode == CODE_FOR_nothing || CONST_INT_P (val))
6890 icode = direct_optab_handler (sync_add_optab, mode);
6891 if (icode != CODE_FOR_nothing)
6893 val = expand_simple_unop (mode, NEG, val, NULL_RTX, 1);
6894 code = PLUS;
6897 break;
6899 default:
6900 gcc_unreachable ();
6903 /* Generate the direct operation, if present. */
6904 if (icode != CODE_FOR_nothing)
6906 struct expand_operand ops[2];
6908 create_fixed_operand (&ops[0], mem);
6909 /* VAL may have been promoted to a wider mode. Shrink it if so. */
6910 create_convert_operand_to (&ops[1], val, mode, true);
6911 if (maybe_expand_insn (icode, 2, ops))
6912 return const0_rtx;
6915 /* Failing that, generate a compare-and-swap loop in which we perform the
6916 operation with normal arithmetic instructions. */
6917 if (direct_optab_handler (sync_compare_and_swap_optab, mode)
6918 != CODE_FOR_nothing)
6920 rtx t0 = gen_reg_rtx (mode), t1;
6922 start_sequence ();
6924 t1 = t0;
6925 if (code == NOT)
6927 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
6928 true, OPTAB_LIB_WIDEN);
6929 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
6931 else
6932 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX,
6933 true, OPTAB_LIB_WIDEN);
6934 insn = get_insns ();
6935 end_sequence ();
6937 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
6938 return const0_rtx;
6941 return NULL_RTX;
6944 /* This function generates the atomic operation MEM CODE= VAL. In this
6945 case, we do care about the resulting value: if AFTER is true then
6946 return the value MEM holds after the operation, if AFTER is false
6947 then return the value MEM holds before the operation. TARGET is an
6948 optional place for the result value to be stored. */
6951 expand_sync_fetch_operation (rtx mem, rtx val, enum rtx_code code,
6952 bool after, rtx target)
6954 enum machine_mode mode = GET_MODE (mem);
6955 enum insn_code old_code, new_code, icode;
6956 bool compensate;
6957 rtx insn;
6959 /* Look to see if the target supports the operation directly. */
6960 switch (code)
6962 case PLUS:
6963 old_code = direct_optab_handler (sync_old_add_optab, mode);
6964 new_code = direct_optab_handler (sync_new_add_optab, mode);
6965 break;
6966 case IOR:
6967 old_code = direct_optab_handler (sync_old_ior_optab, mode);
6968 new_code = direct_optab_handler (sync_new_ior_optab, mode);
6969 break;
6970 case XOR:
6971 old_code = direct_optab_handler (sync_old_xor_optab, mode);
6972 new_code = direct_optab_handler (sync_new_xor_optab, mode);
6973 break;
6974 case AND:
6975 old_code = direct_optab_handler (sync_old_and_optab, mode);
6976 new_code = direct_optab_handler (sync_new_and_optab, mode);
6977 break;
6978 case NOT:
6979 old_code = direct_optab_handler (sync_old_nand_optab, mode);
6980 new_code = direct_optab_handler (sync_new_nand_optab, mode);
6981 break;
6983 case MINUS:
6984 old_code = direct_optab_handler (sync_old_sub_optab, mode);
6985 new_code = direct_optab_handler (sync_new_sub_optab, mode);
6986 if ((old_code == CODE_FOR_nothing && new_code == CODE_FOR_nothing)
6987 || CONST_INT_P (val))
6989 old_code = direct_optab_handler (sync_old_add_optab, mode);
6990 new_code = direct_optab_handler (sync_new_add_optab, mode);
6991 if (old_code != CODE_FOR_nothing || new_code != CODE_FOR_nothing)
6993 val = expand_simple_unop (mode, NEG, val, NULL_RTX, 1);
6994 code = PLUS;
6997 break;
6999 default:
7000 gcc_unreachable ();
7003 /* If the target does supports the proper new/old operation, great. But
7004 if we only support the opposite old/new operation, check to see if we
7005 can compensate. In the case in which the old value is supported, then
7006 we can always perform the operation again with normal arithmetic. In
7007 the case in which the new value is supported, then we can only handle
7008 this in the case the operation is reversible. */
7009 compensate = false;
7010 if (after)
7012 icode = new_code;
7013 if (icode == CODE_FOR_nothing)
7015 icode = old_code;
7016 if (icode != CODE_FOR_nothing)
7017 compensate = true;
7020 else
7022 icode = old_code;
7023 if (icode == CODE_FOR_nothing
7024 && (code == PLUS || code == MINUS || code == XOR))
7026 icode = new_code;
7027 if (icode != CODE_FOR_nothing)
7028 compensate = true;
7032 /* If we found something supported, great. */
7033 if (icode != CODE_FOR_nothing)
7035 struct expand_operand ops[3];
7037 create_output_operand (&ops[0], target, mode);
7038 create_fixed_operand (&ops[1], mem);
7039 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7040 create_convert_operand_to (&ops[2], val, mode, true);
7041 if (maybe_expand_insn (icode, 3, ops))
7043 target = ops[0].value;
7044 val = ops[2].value;
7045 /* If we need to compensate for using an operation with the
7046 wrong return value, do so now. */
7047 if (compensate)
7049 if (!after)
7051 if (code == PLUS)
7052 code = MINUS;
7053 else if (code == MINUS)
7054 code = PLUS;
7057 if (code == NOT)
7059 target = expand_simple_binop (mode, AND, target, val,
7060 NULL_RTX, true,
7061 OPTAB_LIB_WIDEN);
7062 target = expand_simple_unop (mode, code, target,
7063 NULL_RTX, true);
7065 else
7066 target = expand_simple_binop (mode, code, target, val,
7067 NULL_RTX, true,
7068 OPTAB_LIB_WIDEN);
7071 return target;
7075 /* Failing that, generate a compare-and-swap loop in which we perform the
7076 operation with normal arithmetic instructions. */
7077 if (direct_optab_handler (sync_compare_and_swap_optab, mode)
7078 != CODE_FOR_nothing)
7080 rtx t0 = gen_reg_rtx (mode), t1;
7082 if (!target || !register_operand (target, mode))
7083 target = gen_reg_rtx (mode);
7085 start_sequence ();
7087 if (!after)
7088 emit_move_insn (target, t0);
7089 t1 = t0;
7090 if (code == NOT)
7092 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
7093 true, OPTAB_LIB_WIDEN);
7094 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
7096 else
7097 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX,
7098 true, OPTAB_LIB_WIDEN);
7099 if (after)
7100 emit_move_insn (target, t1);
7102 insn = get_insns ();
7103 end_sequence ();
7105 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
7106 return target;
7109 return NULL_RTX;
7112 /* This function expands a test-and-set operation. Ideally we atomically
7113 store VAL in MEM and return the previous value in MEM. Some targets
7114 may not support this operation and only support VAL with the constant 1;
7115 in this case while the return value will be 0/1, but the exact value
7116 stored in MEM is target defined. TARGET is an option place to stick
7117 the return value. */
7120 expand_sync_lock_test_and_set (rtx mem, rtx val, rtx target)
7122 enum machine_mode mode = GET_MODE (mem);
7123 enum insn_code icode;
7125 /* If the target supports the test-and-set directly, great. */
7126 icode = direct_optab_handler (sync_lock_test_and_set_optab, mode);
7127 if (icode != CODE_FOR_nothing)
7129 struct expand_operand ops[3];
7131 create_output_operand (&ops[0], target, mode);
7132 create_fixed_operand (&ops[1], mem);
7133 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7134 create_convert_operand_to (&ops[2], val, mode, true);
7135 if (maybe_expand_insn (icode, 3, ops))
7136 return ops[0].value;
7139 /* Otherwise, use a compare-and-swap loop for the exchange. */
7140 if (direct_optab_handler (sync_compare_and_swap_optab, mode)
7141 != CODE_FOR_nothing)
7143 if (!target || !register_operand (target, mode))
7144 target = gen_reg_rtx (mode);
7145 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7146 val = convert_modes (mode, GET_MODE (val), val, 1);
7147 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7148 return target;
7151 return NULL_RTX;
7154 /* Return true if OPERAND is suitable for operand number OPNO of
7155 instruction ICODE. */
7157 bool
7158 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
7160 return (!insn_data[(int) icode].operand[opno].predicate
7161 || (insn_data[(int) icode].operand[opno].predicate
7162 (operand, insn_data[(int) icode].operand[opno].mode)));
7165 /* TARGET is a target of a multiword operation that we are going to
7166 implement as a series of word-mode operations. Return true if
7167 TARGET is suitable for this purpose. */
7169 bool
7170 valid_multiword_target_p (rtx target)
7172 enum machine_mode mode;
7173 int i;
7175 mode = GET_MODE (target);
7176 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
7177 if (!validate_subreg (word_mode, mode, target, i))
7178 return false;
7179 return true;
7182 /* Like maybe_legitimize_operand, but do not change the code of the
7183 current rtx value. */
7185 static bool
7186 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
7187 struct expand_operand *op)
7189 /* See if the operand matches in its current form. */
7190 if (insn_operand_matches (icode, opno, op->value))
7191 return true;
7193 /* If the operand is a memory whose address has no side effects,
7194 try forcing the address into a register. The check for side
7195 effects is important because force_reg cannot handle things
7196 like auto-modified addresses. */
7197 if (insn_data[(int) icode].operand[opno].allows_mem
7198 && MEM_P (op->value)
7199 && !side_effects_p (XEXP (op->value, 0)))
7201 rtx addr, mem, last;
7203 last = get_last_insn ();
7204 addr = force_reg (Pmode, XEXP (op->value, 0));
7205 mem = replace_equiv_address (op->value, addr);
7206 if (insn_operand_matches (icode, opno, mem))
7208 op->value = mem;
7209 return true;
7211 delete_insns_since (last);
7214 return false;
7217 /* Try to make OP match operand OPNO of instruction ICODE. Return true
7218 on success, storing the new operand value back in OP. */
7220 static bool
7221 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
7222 struct expand_operand *op)
7224 enum machine_mode mode, imode;
7225 bool old_volatile_ok, result;
7227 mode = op->mode;
7228 switch (op->type)
7230 case EXPAND_FIXED:
7231 old_volatile_ok = volatile_ok;
7232 volatile_ok = true;
7233 result = maybe_legitimize_operand_same_code (icode, opno, op);
7234 volatile_ok = old_volatile_ok;
7235 return result;
7237 case EXPAND_OUTPUT:
7238 gcc_assert (mode != VOIDmode);
7239 if (op->value
7240 && op->value != const0_rtx
7241 && GET_MODE (op->value) == mode
7242 && maybe_legitimize_operand_same_code (icode, opno, op))
7243 return true;
7245 op->value = gen_reg_rtx (mode);
7246 break;
7248 case EXPAND_INPUT:
7249 input:
7250 gcc_assert (mode != VOIDmode);
7251 gcc_assert (GET_MODE (op->value) == VOIDmode
7252 || GET_MODE (op->value) == mode);
7253 if (maybe_legitimize_operand_same_code (icode, opno, op))
7254 return true;
7256 op->value = copy_to_mode_reg (mode, op->value);
7257 break;
7259 case EXPAND_CONVERT_TO:
7260 gcc_assert (mode != VOIDmode);
7261 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
7262 goto input;
7264 case EXPAND_CONVERT_FROM:
7265 if (GET_MODE (op->value) != VOIDmode)
7266 mode = GET_MODE (op->value);
7267 else
7268 /* The caller must tell us what mode this value has. */
7269 gcc_assert (mode != VOIDmode);
7271 imode = insn_data[(int) icode].operand[opno].mode;
7272 if (imode != VOIDmode && imode != mode)
7274 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
7275 mode = imode;
7277 goto input;
7279 case EXPAND_ADDRESS:
7280 gcc_assert (mode != VOIDmode);
7281 op->value = convert_memory_address (mode, op->value);
7282 goto input;
7284 case EXPAND_INTEGER:
7285 mode = insn_data[(int) icode].operand[opno].mode;
7286 if (mode != VOIDmode && const_int_operand (op->value, mode))
7287 goto input;
7288 break;
7290 return insn_operand_matches (icode, opno, op->value);
7293 /* Make OP describe an input operand that should have the same value
7294 as VALUE, after any mode conversion that the target might request.
7295 TYPE is the type of VALUE. */
7297 void
7298 create_convert_operand_from_type (struct expand_operand *op,
7299 rtx value, tree type)
7301 create_convert_operand_from (op, value, TYPE_MODE (type),
7302 TYPE_UNSIGNED (type));
7305 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
7306 of instruction ICODE. Return true on success, leaving the new operand
7307 values in the OPS themselves. Emit no code on failure. */
7309 bool
7310 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
7311 unsigned int nops, struct expand_operand *ops)
7313 rtx last;
7314 unsigned int i;
7316 last = get_last_insn ();
7317 for (i = 0; i < nops; i++)
7318 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
7320 delete_insns_since (last);
7321 return false;
7323 return true;
7326 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
7327 as its operands. Return the instruction pattern on success,
7328 and emit any necessary set-up code. Return null and emit no
7329 code on failure. */
7332 maybe_gen_insn (enum insn_code icode, unsigned int nops,
7333 struct expand_operand *ops)
7335 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
7336 if (!maybe_legitimize_operands (icode, 0, nops, ops))
7337 return NULL_RTX;
7339 switch (nops)
7341 case 1:
7342 return GEN_FCN (icode) (ops[0].value);
7343 case 2:
7344 return GEN_FCN (icode) (ops[0].value, ops[1].value);
7345 case 3:
7346 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
7347 case 4:
7348 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7349 ops[3].value);
7350 case 5:
7351 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7352 ops[3].value, ops[4].value);
7353 case 6:
7354 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
7355 ops[3].value, ops[4].value, ops[5].value);
7357 gcc_unreachable ();
7360 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
7361 as its operands. Return true on success and emit no code on failure. */
7363 bool
7364 maybe_expand_insn (enum insn_code icode, unsigned int nops,
7365 struct expand_operand *ops)
7367 rtx pat = maybe_gen_insn (icode, nops, ops);
7368 if (pat)
7370 emit_insn (pat);
7371 return true;
7373 return false;
7376 /* Like maybe_expand_insn, but for jumps. */
7378 bool
7379 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
7380 struct expand_operand *ops)
7382 rtx pat = maybe_gen_insn (icode, nops, ops);
7383 if (pat)
7385 emit_jump_insn (pat);
7386 return true;
7388 return false;
7391 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
7392 as its operands. */
7394 void
7395 expand_insn (enum insn_code icode, unsigned int nops,
7396 struct expand_operand *ops)
7398 if (!maybe_expand_insn (icode, nops, ops))
7399 gcc_unreachable ();
7402 /* Like expand_insn, but for jumps. */
7404 void
7405 expand_jump_insn (enum insn_code icode, unsigned int nops,
7406 struct expand_operand *ops)
7408 if (!maybe_expand_jump_insn (icode, nops, ops))
7409 gcc_unreachable ();
7412 #include "gt-optabs.h"