ipa-inline.c (edge_badness): Reduce precision; use scc hints.
[official-gcc.git] / gcc / optabs.c
blobe22031615fac49016df4a5219841edea76d7c661
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, 2012 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 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
58 enum machine_mode *);
59 static rtx expand_unop_direct (enum machine_mode, optab, rtx, rtx, int);
60 static void emit_libcall_block_1 (rtx, rtx, rtx, rtx, bool);
62 /* Debug facility for use in GDB. */
63 void debug_optab_libfuncs (void);
65 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
66 #if ENABLE_DECIMAL_BID_FORMAT
67 #define DECIMAL_PREFIX "bid_"
68 #else
69 #define DECIMAL_PREFIX "dpd_"
70 #endif
72 /* Used for libfunc_hash. */
74 static hashval_t
75 hash_libfunc (const void *p)
77 const struct libfunc_entry *const e = (const struct libfunc_entry *) p;
78 return ((e->mode1 + e->mode2 * NUM_MACHINE_MODES) ^ e->op);
81 /* Used for libfunc_hash. */
83 static int
84 eq_libfunc (const void *p, const void *q)
86 const struct libfunc_entry *const e1 = (const struct libfunc_entry *) p;
87 const struct libfunc_entry *const e2 = (const struct libfunc_entry *) q;
88 return e1->op == e2->op && e1->mode1 == e2->mode1 && e1->mode2 == e2->mode2;
91 /* Return libfunc corresponding operation defined by OPTAB converting
92 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
93 if no libfunc is available. */
94 rtx
95 convert_optab_libfunc (convert_optab optab, enum machine_mode mode1,
96 enum machine_mode mode2)
98 struct libfunc_entry e;
99 struct libfunc_entry **slot;
101 /* ??? This ought to be an assert, but not all of the places
102 that we expand optabs know about the optabs that got moved
103 to being direct. */
104 if (!(optab >= FIRST_CONV_OPTAB && optab <= LAST_CONVLIB_OPTAB))
105 return NULL_RTX;
107 e.op = optab;
108 e.mode1 = mode1;
109 e.mode2 = mode2;
110 slot = (struct libfunc_entry **)
111 htab_find_slot (libfunc_hash, &e, NO_INSERT);
112 if (!slot)
114 const struct convert_optab_libcall_d *d
115 = &convlib_def[optab - FIRST_CONV_OPTAB];
117 if (d->libcall_gen == NULL)
118 return NULL;
120 d->libcall_gen (optab, d->libcall_basename, mode1, mode2);
121 slot = (struct libfunc_entry **)
122 htab_find_slot (libfunc_hash, &e, NO_INSERT);
123 if (!slot)
124 return NULL;
126 return (*slot)->libfunc;
129 /* Return libfunc corresponding operation defined by OPTAB in MODE.
130 Trigger lazy initialization if needed, return NULL if no libfunc is
131 available. */
133 optab_libfunc (optab optab, enum machine_mode mode)
135 struct libfunc_entry e;
136 struct libfunc_entry **slot;
138 /* ??? This ought to be an assert, but not all of the places
139 that we expand optabs know about the optabs that got moved
140 to being direct. */
141 if (!(optab >= FIRST_NORM_OPTAB && optab <= LAST_NORMLIB_OPTAB))
142 return NULL_RTX;
144 e.op = optab;
145 e.mode1 = mode;
146 e.mode2 = VOIDmode;
147 slot = (struct libfunc_entry **)
148 htab_find_slot (libfunc_hash, &e, NO_INSERT);
149 if (!slot)
151 const struct optab_libcall_d *d
152 = &normlib_def[optab - FIRST_NORM_OPTAB];
154 if (d->libcall_gen == NULL)
155 return NULL;
157 d->libcall_gen (optab, d->libcall_basename, d->libcall_suffix, mode);
158 slot = (struct libfunc_entry **)
159 htab_find_slot (libfunc_hash, &e, NO_INSERT);
160 if (!slot)
161 return NULL;
163 return (*slot)->libfunc;
167 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
168 the result of operation CODE applied to OP0 (and OP1 if it is a binary
169 operation).
171 If the last insn does not set TARGET, don't do anything, but return 1.
173 If a previous insn sets TARGET and TARGET is one of OP0 or OP1,
174 don't add the REG_EQUAL note but return 0. Our caller can then try
175 again, ensuring that TARGET is not one of the operands. */
177 static int
178 add_equal_note (rtx insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
180 rtx last_insn, insn, set;
181 rtx note;
183 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
185 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
186 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
187 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
188 && GET_RTX_CLASS (code) != RTX_COMPARE
189 && GET_RTX_CLASS (code) != RTX_UNARY)
190 return 1;
192 if (GET_CODE (target) == ZERO_EXTRACT)
193 return 1;
195 for (last_insn = insns;
196 NEXT_INSN (last_insn) != NULL_RTX;
197 last_insn = NEXT_INSN (last_insn))
200 set = single_set (last_insn);
201 if (set == NULL_RTX)
202 return 1;
204 if (! rtx_equal_p (SET_DEST (set), target)
205 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
206 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
207 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
208 return 1;
210 /* If TARGET is in OP0 or OP1, check if anything in SEQ sets TARGET
211 besides the last insn. */
212 if (reg_overlap_mentioned_p (target, op0)
213 || (op1 && reg_overlap_mentioned_p (target, op1)))
215 insn = PREV_INSN (last_insn);
216 while (insn != NULL_RTX)
218 if (reg_set_p (target, insn))
219 return 0;
221 insn = PREV_INSN (insn);
225 if (GET_RTX_CLASS (code) == RTX_UNARY)
226 switch (code)
228 case FFS:
229 case CLZ:
230 case CTZ:
231 case CLRSB:
232 case POPCOUNT:
233 case PARITY:
234 case BSWAP:
235 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
237 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
238 if (GET_MODE_SIZE (GET_MODE (op0))
239 > GET_MODE_SIZE (GET_MODE (target)))
240 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
241 note, GET_MODE (op0));
242 else
243 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
244 note, GET_MODE (op0));
245 break;
247 /* FALLTHRU */
248 default:
249 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
250 break;
252 else
253 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
255 set_unique_reg_note (last_insn, REG_EQUAL, note);
257 return 1;
260 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
261 for a widening operation would be. In most cases this would be OP0, but if
262 that's a constant it'll be VOIDmode, which isn't useful. */
264 static enum machine_mode
265 widened_mode (enum machine_mode to_mode, rtx op0, rtx op1)
267 enum machine_mode m0 = GET_MODE (op0);
268 enum machine_mode m1 = GET_MODE (op1);
269 enum machine_mode result;
271 if (m0 == VOIDmode && m1 == VOIDmode)
272 return to_mode;
273 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
274 result = m1;
275 else
276 result = m0;
278 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
279 return to_mode;
281 return result;
284 /* Find a widening optab even if it doesn't widen as much as we want.
285 E.g. if from_mode is HImode, and to_mode is DImode, and there is no
286 direct HI->SI insn, then return SI->DI, if that exists.
287 If PERMIT_NON_WIDENING is non-zero then this can be used with
288 non-widening optabs also. */
290 enum insn_code
291 find_widening_optab_handler_and_mode (optab op, enum machine_mode to_mode,
292 enum machine_mode from_mode,
293 int permit_non_widening,
294 enum machine_mode *found_mode)
296 for (; (permit_non_widening || from_mode != to_mode)
297 && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
298 && from_mode != VOIDmode;
299 from_mode = GET_MODE_WIDER_MODE (from_mode))
301 enum insn_code handler = widening_optab_handler (op, to_mode,
302 from_mode);
304 if (handler != CODE_FOR_nothing)
306 if (found_mode)
307 *found_mode = from_mode;
308 return handler;
312 return CODE_FOR_nothing;
315 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
316 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
317 not actually do a sign-extend or zero-extend, but can leave the
318 higher-order bits of the result rtx undefined, for example, in the case
319 of logical operations, but not right shifts. */
321 static rtx
322 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
323 int unsignedp, int no_extend)
325 rtx result;
327 /* If we don't have to extend and this is a constant, return it. */
328 if (no_extend && GET_MODE (op) == VOIDmode)
329 return op;
331 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
332 extend since it will be more efficient to do so unless the signedness of
333 a promoted object differs from our extension. */
334 if (! no_extend
335 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
336 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
337 return convert_modes (mode, oldmode, op, unsignedp);
339 /* If MODE is no wider than a single word, we return a paradoxical
340 SUBREG. */
341 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
342 return gen_rtx_SUBREG (mode, force_reg (GET_MODE (op), op), 0);
344 /* Otherwise, get an object of MODE, clobber it, and set the low-order
345 part to OP. */
347 result = gen_reg_rtx (mode);
348 emit_clobber (result);
349 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
350 return result;
353 /* Return the optab used for computing the operation given by the tree code,
354 CODE and the tree EXP. This function is not always usable (for example, it
355 cannot give complete results for multiplication or division) but probably
356 ought to be relied on more widely throughout the expander. */
357 optab
358 optab_for_tree_code (enum tree_code code, const_tree type,
359 enum optab_subtype subtype)
361 bool trapv;
362 switch (code)
364 case BIT_AND_EXPR:
365 return and_optab;
367 case BIT_IOR_EXPR:
368 return ior_optab;
370 case BIT_NOT_EXPR:
371 return one_cmpl_optab;
373 case BIT_XOR_EXPR:
374 return xor_optab;
376 case MULT_HIGHPART_EXPR:
377 return TYPE_UNSIGNED (type) ? umul_highpart_optab : smul_highpart_optab;
379 case TRUNC_MOD_EXPR:
380 case CEIL_MOD_EXPR:
381 case FLOOR_MOD_EXPR:
382 case ROUND_MOD_EXPR:
383 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
385 case RDIV_EXPR:
386 case TRUNC_DIV_EXPR:
387 case CEIL_DIV_EXPR:
388 case FLOOR_DIV_EXPR:
389 case ROUND_DIV_EXPR:
390 case EXACT_DIV_EXPR:
391 if (TYPE_SATURATING(type))
392 return TYPE_UNSIGNED(type) ? usdiv_optab : ssdiv_optab;
393 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
395 case LSHIFT_EXPR:
396 if (TREE_CODE (type) == VECTOR_TYPE)
398 if (subtype == optab_vector)
399 return TYPE_SATURATING (type) ? unknown_optab : vashl_optab;
401 gcc_assert (subtype == optab_scalar);
403 if (TYPE_SATURATING(type))
404 return TYPE_UNSIGNED(type) ? usashl_optab : ssashl_optab;
405 return ashl_optab;
407 case RSHIFT_EXPR:
408 if (TREE_CODE (type) == VECTOR_TYPE)
410 if (subtype == optab_vector)
411 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
413 gcc_assert (subtype == optab_scalar);
415 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
417 case LROTATE_EXPR:
418 if (TREE_CODE (type) == VECTOR_TYPE)
420 if (subtype == optab_vector)
421 return vrotl_optab;
423 gcc_assert (subtype == optab_scalar);
425 return rotl_optab;
427 case RROTATE_EXPR:
428 if (TREE_CODE (type) == VECTOR_TYPE)
430 if (subtype == optab_vector)
431 return vrotr_optab;
433 gcc_assert (subtype == optab_scalar);
435 return rotr_optab;
437 case MAX_EXPR:
438 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
440 case MIN_EXPR:
441 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
443 case REALIGN_LOAD_EXPR:
444 return vec_realign_load_optab;
446 case WIDEN_SUM_EXPR:
447 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
449 case DOT_PROD_EXPR:
450 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
452 case WIDEN_MULT_PLUS_EXPR:
453 return (TYPE_UNSIGNED (type)
454 ? (TYPE_SATURATING (type)
455 ? usmadd_widen_optab : umadd_widen_optab)
456 : (TYPE_SATURATING (type)
457 ? ssmadd_widen_optab : smadd_widen_optab));
459 case WIDEN_MULT_MINUS_EXPR:
460 return (TYPE_UNSIGNED (type)
461 ? (TYPE_SATURATING (type)
462 ? usmsub_widen_optab : umsub_widen_optab)
463 : (TYPE_SATURATING (type)
464 ? ssmsub_widen_optab : smsub_widen_optab));
466 case FMA_EXPR:
467 return fma_optab;
469 case REDUC_MAX_EXPR:
470 return TYPE_UNSIGNED (type) ? reduc_umax_optab : reduc_smax_optab;
472 case REDUC_MIN_EXPR:
473 return TYPE_UNSIGNED (type) ? reduc_umin_optab : reduc_smin_optab;
475 case REDUC_PLUS_EXPR:
476 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
478 case VEC_LSHIFT_EXPR:
479 return vec_shl_optab;
481 case VEC_RSHIFT_EXPR:
482 return vec_shr_optab;
484 case VEC_WIDEN_MULT_HI_EXPR:
485 return TYPE_UNSIGNED (type) ?
486 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
488 case VEC_WIDEN_MULT_LO_EXPR:
489 return TYPE_UNSIGNED (type) ?
490 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
492 case VEC_WIDEN_MULT_EVEN_EXPR:
493 return TYPE_UNSIGNED (type) ?
494 vec_widen_umult_even_optab : vec_widen_smult_even_optab;
496 case VEC_WIDEN_MULT_ODD_EXPR:
497 return TYPE_UNSIGNED (type) ?
498 vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
500 case VEC_WIDEN_LSHIFT_HI_EXPR:
501 return TYPE_UNSIGNED (type) ?
502 vec_widen_ushiftl_hi_optab : vec_widen_sshiftl_hi_optab;
504 case VEC_WIDEN_LSHIFT_LO_EXPR:
505 return TYPE_UNSIGNED (type) ?
506 vec_widen_ushiftl_lo_optab : vec_widen_sshiftl_lo_optab;
508 case VEC_UNPACK_HI_EXPR:
509 return TYPE_UNSIGNED (type) ?
510 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
512 case VEC_UNPACK_LO_EXPR:
513 return TYPE_UNSIGNED (type) ?
514 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
516 case VEC_UNPACK_FLOAT_HI_EXPR:
517 /* The signedness is determined from input operand. */
518 return TYPE_UNSIGNED (type) ?
519 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
521 case VEC_UNPACK_FLOAT_LO_EXPR:
522 /* The signedness is determined from input operand. */
523 return TYPE_UNSIGNED (type) ?
524 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
526 case VEC_PACK_TRUNC_EXPR:
527 return vec_pack_trunc_optab;
529 case VEC_PACK_SAT_EXPR:
530 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
532 case VEC_PACK_FIX_TRUNC_EXPR:
533 /* The signedness is determined from output operand. */
534 return TYPE_UNSIGNED (type) ?
535 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
537 default:
538 break;
541 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
542 switch (code)
544 case POINTER_PLUS_EXPR:
545 case PLUS_EXPR:
546 if (TYPE_SATURATING(type))
547 return TYPE_UNSIGNED(type) ? usadd_optab : ssadd_optab;
548 return trapv ? addv_optab : add_optab;
550 case MINUS_EXPR:
551 if (TYPE_SATURATING(type))
552 return TYPE_UNSIGNED(type) ? ussub_optab : sssub_optab;
553 return trapv ? subv_optab : sub_optab;
555 case MULT_EXPR:
556 if (TYPE_SATURATING(type))
557 return TYPE_UNSIGNED(type) ? usmul_optab : ssmul_optab;
558 return trapv ? smulv_optab : smul_optab;
560 case NEGATE_EXPR:
561 if (TYPE_SATURATING(type))
562 return TYPE_UNSIGNED(type) ? usneg_optab : ssneg_optab;
563 return trapv ? negv_optab : neg_optab;
565 case ABS_EXPR:
566 return trapv ? absv_optab : abs_optab;
568 default:
569 return unknown_optab;
574 /* Expand vector widening operations.
576 There are two different classes of operations handled here:
577 1) Operations whose result is wider than all the arguments to the operation.
578 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
579 In this case OP0 and optionally OP1 would be initialized,
580 but WIDE_OP wouldn't (not relevant for this case).
581 2) Operations whose result is of the same size as the last argument to the
582 operation, but wider than all the other arguments to the operation.
583 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
584 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
586 E.g, when called to expand the following operations, this is how
587 the arguments will be initialized:
588 nops OP0 OP1 WIDE_OP
589 widening-sum 2 oprnd0 - oprnd1
590 widening-dot-product 3 oprnd0 oprnd1 oprnd2
591 widening-mult 2 oprnd0 oprnd1 -
592 type-promotion (vec-unpack) 1 oprnd0 - - */
595 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
596 rtx target, int unsignedp)
598 struct expand_operand eops[4];
599 tree oprnd0, oprnd1, oprnd2;
600 enum machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
601 optab widen_pattern_optab;
602 enum insn_code icode;
603 int nops = TREE_CODE_LENGTH (ops->code);
604 int op;
606 oprnd0 = ops->op0;
607 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
608 widen_pattern_optab =
609 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
610 if (ops->code == WIDEN_MULT_PLUS_EXPR
611 || ops->code == WIDEN_MULT_MINUS_EXPR)
612 icode = find_widening_optab_handler (widen_pattern_optab,
613 TYPE_MODE (TREE_TYPE (ops->op2)),
614 tmode0, 0);
615 else
616 icode = optab_handler (widen_pattern_optab, tmode0);
617 gcc_assert (icode != CODE_FOR_nothing);
619 if (nops >= 2)
621 oprnd1 = ops->op1;
622 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
625 /* The last operand is of a wider mode than the rest of the operands. */
626 if (nops == 2)
627 wmode = tmode1;
628 else if (nops == 3)
630 gcc_assert (tmode1 == tmode0);
631 gcc_assert (op1);
632 oprnd2 = ops->op2;
633 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
636 op = 0;
637 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
638 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
639 if (op1)
640 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
641 if (wide_op)
642 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
643 expand_insn (icode, op, eops);
644 return eops[0].value;
647 /* Generate code to perform an operation specified by TERNARY_OPTAB
648 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
650 UNSIGNEDP is for the case where we have to widen the operands
651 to perform the operation. It says to use zero-extension.
653 If TARGET is nonzero, the value
654 is generated there, if it is convenient to do so.
655 In all cases an rtx is returned for the locus of the value;
656 this may or may not be TARGET. */
659 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
660 rtx op1, rtx op2, rtx target, int unsignedp)
662 struct expand_operand ops[4];
663 enum insn_code icode = optab_handler (ternary_optab, mode);
665 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
667 create_output_operand (&ops[0], target, mode);
668 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
669 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
670 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
671 expand_insn (icode, 4, ops);
672 return ops[0].value;
676 /* Like expand_binop, but return a constant rtx if the result can be
677 calculated at compile time. The arguments and return value are
678 otherwise the same as for expand_binop. */
681 simplify_expand_binop (enum machine_mode mode, optab binoptab,
682 rtx op0, rtx op1, rtx target, int unsignedp,
683 enum optab_methods methods)
685 if (CONSTANT_P (op0) && CONSTANT_P (op1))
687 rtx x = simplify_binary_operation (optab_to_code (binoptab),
688 mode, op0, op1);
689 if (x)
690 return x;
693 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
696 /* Like simplify_expand_binop, but always put the result in TARGET.
697 Return true if the expansion succeeded. */
699 bool
700 force_expand_binop (enum machine_mode mode, optab binoptab,
701 rtx op0, rtx op1, rtx target, int unsignedp,
702 enum optab_methods methods)
704 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
705 target, unsignedp, methods);
706 if (x == 0)
707 return false;
708 if (x != target)
709 emit_move_insn (target, x);
710 return true;
713 /* Generate insns for VEC_LSHIFT_EXPR, VEC_RSHIFT_EXPR. */
716 expand_vec_shift_expr (sepops ops, rtx target)
718 struct expand_operand eops[3];
719 enum insn_code icode;
720 rtx rtx_op1, rtx_op2;
721 enum machine_mode mode = TYPE_MODE (ops->type);
722 tree vec_oprnd = ops->op0;
723 tree shift_oprnd = ops->op1;
724 optab shift_optab;
726 switch (ops->code)
728 case VEC_RSHIFT_EXPR:
729 shift_optab = vec_shr_optab;
730 break;
731 case VEC_LSHIFT_EXPR:
732 shift_optab = vec_shl_optab;
733 break;
734 default:
735 gcc_unreachable ();
738 icode = optab_handler (shift_optab, mode);
739 gcc_assert (icode != CODE_FOR_nothing);
741 rtx_op1 = expand_normal (vec_oprnd);
742 rtx_op2 = expand_normal (shift_oprnd);
744 create_output_operand (&eops[0], target, mode);
745 create_input_operand (&eops[1], rtx_op1, GET_MODE (rtx_op1));
746 create_convert_operand_from_type (&eops[2], rtx_op2, TREE_TYPE (shift_oprnd));
747 expand_insn (icode, 3, eops);
749 return eops[0].value;
752 /* Create a new vector value in VMODE with all elements set to OP. The
753 mode of OP must be the element mode of VMODE. If OP is a constant,
754 then the return value will be a constant. */
756 static rtx
757 expand_vector_broadcast (enum machine_mode vmode, rtx op)
759 enum insn_code icode;
760 rtvec vec;
761 rtx ret;
762 int i, n;
764 gcc_checking_assert (VECTOR_MODE_P (vmode));
766 n = GET_MODE_NUNITS (vmode);
767 vec = rtvec_alloc (n);
768 for (i = 0; i < n; ++i)
769 RTVEC_ELT (vec, i) = op;
771 if (CONSTANT_P (op))
772 return gen_rtx_CONST_VECTOR (vmode, vec);
774 /* ??? If the target doesn't have a vec_init, then we have no easy way
775 of performing this operation. Most of this sort of generic support
776 is hidden away in the vector lowering support in gimple. */
777 icode = optab_handler (vec_init_optab, vmode);
778 if (icode == CODE_FOR_nothing)
779 return NULL;
781 ret = gen_reg_rtx (vmode);
782 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
784 return ret;
787 /* This subroutine of expand_doubleword_shift handles the cases in which
788 the effective shift value is >= BITS_PER_WORD. The arguments and return
789 value are the same as for the parent routine, except that SUPERWORD_OP1
790 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
791 INTO_TARGET may be null if the caller has decided to calculate it. */
793 static bool
794 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
795 rtx outof_target, rtx into_target,
796 int unsignedp, enum optab_methods methods)
798 if (into_target != 0)
799 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
800 into_target, unsignedp, methods))
801 return false;
803 if (outof_target != 0)
805 /* For a signed right shift, we must fill OUTOF_TARGET with copies
806 of the sign bit, otherwise we must fill it with zeros. */
807 if (binoptab != ashr_optab)
808 emit_move_insn (outof_target, CONST0_RTX (word_mode));
809 else
810 if (!force_expand_binop (word_mode, binoptab,
811 outof_input, GEN_INT (BITS_PER_WORD - 1),
812 outof_target, unsignedp, methods))
813 return false;
815 return true;
818 /* This subroutine of expand_doubleword_shift handles the cases in which
819 the effective shift value is < BITS_PER_WORD. The arguments and return
820 value are the same as for the parent routine. */
822 static bool
823 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
824 rtx outof_input, rtx into_input, rtx op1,
825 rtx outof_target, rtx into_target,
826 int unsignedp, enum optab_methods methods,
827 unsigned HOST_WIDE_INT shift_mask)
829 optab reverse_unsigned_shift, unsigned_shift;
830 rtx tmp, carries;
832 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
833 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
835 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
836 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
837 the opposite direction to BINOPTAB. */
838 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
840 carries = outof_input;
841 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
842 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
843 0, true, methods);
845 else
847 /* We must avoid shifting by BITS_PER_WORD bits since that is either
848 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
849 has unknown behavior. Do a single shift first, then shift by the
850 remainder. It's OK to use ~OP1 as the remainder if shift counts
851 are truncated to the mode size. */
852 carries = expand_binop (word_mode, reverse_unsigned_shift,
853 outof_input, const1_rtx, 0, unsignedp, methods);
854 if (shift_mask == BITS_PER_WORD - 1)
856 tmp = immed_double_const (-1, -1, op1_mode);
857 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
858 0, true, methods);
860 else
862 tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
863 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
864 0, true, methods);
867 if (tmp == 0 || carries == 0)
868 return false;
869 carries = expand_binop (word_mode, reverse_unsigned_shift,
870 carries, tmp, 0, unsignedp, methods);
871 if (carries == 0)
872 return false;
874 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
875 so the result can go directly into INTO_TARGET if convenient. */
876 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
877 into_target, unsignedp, methods);
878 if (tmp == 0)
879 return false;
881 /* Now OR in the bits carried over from OUTOF_INPUT. */
882 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
883 into_target, unsignedp, methods))
884 return false;
886 /* Use a standard word_mode shift for the out-of half. */
887 if (outof_target != 0)
888 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
889 outof_target, unsignedp, methods))
890 return false;
892 return true;
896 #ifdef HAVE_conditional_move
897 /* Try implementing expand_doubleword_shift using conditional moves.
898 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
899 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
900 are the shift counts to use in the former and latter case. All other
901 arguments are the same as the parent routine. */
903 static bool
904 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
905 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
906 rtx outof_input, rtx into_input,
907 rtx subword_op1, rtx superword_op1,
908 rtx outof_target, rtx into_target,
909 int unsignedp, enum optab_methods methods,
910 unsigned HOST_WIDE_INT shift_mask)
912 rtx outof_superword, into_superword;
914 /* Put the superword version of the output into OUTOF_SUPERWORD and
915 INTO_SUPERWORD. */
916 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
917 if (outof_target != 0 && subword_op1 == superword_op1)
919 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
920 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
921 into_superword = outof_target;
922 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
923 outof_superword, 0, unsignedp, methods))
924 return false;
926 else
928 into_superword = gen_reg_rtx (word_mode);
929 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
930 outof_superword, into_superword,
931 unsignedp, methods))
932 return false;
935 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
936 if (!expand_subword_shift (op1_mode, binoptab,
937 outof_input, into_input, subword_op1,
938 outof_target, into_target,
939 unsignedp, methods, shift_mask))
940 return false;
942 /* Select between them. Do the INTO half first because INTO_SUPERWORD
943 might be the current value of OUTOF_TARGET. */
944 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
945 into_target, into_superword, word_mode, false))
946 return false;
948 if (outof_target != 0)
949 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
950 outof_target, outof_superword,
951 word_mode, false))
952 return false;
954 return true;
956 #endif
958 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
959 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
960 input operand; the shift moves bits in the direction OUTOF_INPUT->
961 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
962 of the target. OP1 is the shift count and OP1_MODE is its mode.
963 If OP1 is constant, it will have been truncated as appropriate
964 and is known to be nonzero.
966 If SHIFT_MASK is zero, the result of word shifts is undefined when the
967 shift count is outside the range [0, BITS_PER_WORD). This routine must
968 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
970 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
971 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
972 fill with zeros or sign bits as appropriate.
974 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
975 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
976 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
977 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
978 are undefined.
980 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
981 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
982 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
983 function wants to calculate it itself.
985 Return true if the shift could be successfully synthesized. */
987 static bool
988 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
989 rtx outof_input, rtx into_input, rtx op1,
990 rtx outof_target, rtx into_target,
991 int unsignedp, enum optab_methods methods,
992 unsigned HOST_WIDE_INT shift_mask)
994 rtx superword_op1, tmp, cmp1, cmp2;
995 rtx subword_label, done_label;
996 enum rtx_code cmp_code;
998 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
999 fill the result with sign or zero bits as appropriate. If so, the value
1000 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1001 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1002 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1004 This isn't worthwhile for constant shifts since the optimizers will
1005 cope better with in-range shift counts. */
1006 if (shift_mask >= BITS_PER_WORD
1007 && outof_target != 0
1008 && !CONSTANT_P (op1))
1010 if (!expand_doubleword_shift (op1_mode, binoptab,
1011 outof_input, into_input, op1,
1012 0, into_target,
1013 unsignedp, methods, shift_mask))
1014 return false;
1015 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1016 outof_target, unsignedp, methods))
1017 return false;
1018 return true;
1021 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1022 is true when the effective shift value is less than BITS_PER_WORD.
1023 Set SUPERWORD_OP1 to the shift count that should be used to shift
1024 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1025 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
1026 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1028 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1029 is a subword shift count. */
1030 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1031 0, true, methods);
1032 cmp2 = CONST0_RTX (op1_mode);
1033 cmp_code = EQ;
1034 superword_op1 = op1;
1036 else
1038 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1039 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1040 0, true, methods);
1041 cmp2 = CONST0_RTX (op1_mode);
1042 cmp_code = LT;
1043 superword_op1 = cmp1;
1045 if (cmp1 == 0)
1046 return false;
1048 /* If we can compute the condition at compile time, pick the
1049 appropriate subroutine. */
1050 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1051 if (tmp != 0 && CONST_INT_P (tmp))
1053 if (tmp == const0_rtx)
1054 return expand_superword_shift (binoptab, outof_input, superword_op1,
1055 outof_target, into_target,
1056 unsignedp, methods);
1057 else
1058 return expand_subword_shift (op1_mode, binoptab,
1059 outof_input, into_input, op1,
1060 outof_target, into_target,
1061 unsignedp, methods, shift_mask);
1064 #ifdef HAVE_conditional_move
1065 /* Try using conditional moves to generate straight-line code. */
1067 rtx start = get_last_insn ();
1068 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1069 cmp_code, cmp1, cmp2,
1070 outof_input, into_input,
1071 op1, superword_op1,
1072 outof_target, into_target,
1073 unsignedp, methods, shift_mask))
1074 return true;
1075 delete_insns_since (start);
1077 #endif
1079 /* As a last resort, use branches to select the correct alternative. */
1080 subword_label = gen_label_rtx ();
1081 done_label = gen_label_rtx ();
1083 NO_DEFER_POP;
1084 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1085 0, 0, subword_label, -1);
1086 OK_DEFER_POP;
1088 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1089 outof_target, into_target,
1090 unsignedp, methods))
1091 return false;
1093 emit_jump_insn (gen_jump (done_label));
1094 emit_barrier ();
1095 emit_label (subword_label);
1097 if (!expand_subword_shift (op1_mode, binoptab,
1098 outof_input, into_input, op1,
1099 outof_target, into_target,
1100 unsignedp, methods, shift_mask))
1101 return false;
1103 emit_label (done_label);
1104 return true;
1107 /* Subroutine of expand_binop. Perform a double word multiplication of
1108 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1109 as the target's word_mode. This function return NULL_RTX if anything
1110 goes wrong, in which case it may have already emitted instructions
1111 which need to be deleted.
1113 If we want to multiply two two-word values and have normal and widening
1114 multiplies of single-word values, we can do this with three smaller
1115 multiplications.
1117 The multiplication proceeds as follows:
1118 _______________________
1119 [__op0_high_|__op0_low__]
1120 _______________________
1121 * [__op1_high_|__op1_low__]
1122 _______________________________________________
1123 _______________________
1124 (1) [__op0_low__*__op1_low__]
1125 _______________________
1126 (2a) [__op0_low__*__op1_high_]
1127 _______________________
1128 (2b) [__op0_high_*__op1_low__]
1129 _______________________
1130 (3) [__op0_high_*__op1_high_]
1133 This gives a 4-word result. Since we are only interested in the
1134 lower 2 words, partial result (3) and the upper words of (2a) and
1135 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1136 calculated using non-widening multiplication.
1138 (1), however, needs to be calculated with an unsigned widening
1139 multiplication. If this operation is not directly supported we
1140 try using a signed widening multiplication and adjust the result.
1141 This adjustment works as follows:
1143 If both operands are positive then no adjustment is needed.
1145 If the operands have different signs, for example op0_low < 0 and
1146 op1_low >= 0, the instruction treats the most significant bit of
1147 op0_low as a sign bit instead of a bit with significance
1148 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1149 with 2**BITS_PER_WORD - op0_low, and two's complements the
1150 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1151 the result.
1153 Similarly, if both operands are negative, we need to add
1154 (op0_low + op1_low) * 2**BITS_PER_WORD.
1156 We use a trick to adjust quickly. We logically shift op0_low right
1157 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1158 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1159 logical shift exists, we do an arithmetic right shift and subtract
1160 the 0 or -1. */
1162 static rtx
1163 expand_doubleword_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
1164 bool umulp, enum optab_methods methods)
1166 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1167 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1168 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1169 rtx product, adjust, product_high, temp;
1171 rtx op0_high = operand_subword_force (op0, high, mode);
1172 rtx op0_low = operand_subword_force (op0, low, mode);
1173 rtx op1_high = operand_subword_force (op1, high, mode);
1174 rtx op1_low = operand_subword_force (op1, low, mode);
1176 /* If we're using an unsigned multiply to directly compute the product
1177 of the low-order words of the operands and perform any required
1178 adjustments of the operands, we begin by trying two more multiplications
1179 and then computing the appropriate sum.
1181 We have checked above that the required addition is provided.
1182 Full-word addition will normally always succeed, especially if
1183 it is provided at all, so we don't worry about its failure. The
1184 multiplication may well fail, however, so we do handle that. */
1186 if (!umulp)
1188 /* ??? This could be done with emit_store_flag where available. */
1189 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1190 NULL_RTX, 1, methods);
1191 if (temp)
1192 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1193 NULL_RTX, 0, OPTAB_DIRECT);
1194 else
1196 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1197 NULL_RTX, 0, methods);
1198 if (!temp)
1199 return NULL_RTX;
1200 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1201 NULL_RTX, 0, OPTAB_DIRECT);
1204 if (!op0_high)
1205 return NULL_RTX;
1208 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1209 NULL_RTX, 0, OPTAB_DIRECT);
1210 if (!adjust)
1211 return NULL_RTX;
1213 /* OP0_HIGH should now be dead. */
1215 if (!umulp)
1217 /* ??? This could be done with emit_store_flag where available. */
1218 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1219 NULL_RTX, 1, methods);
1220 if (temp)
1221 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1222 NULL_RTX, 0, OPTAB_DIRECT);
1223 else
1225 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1226 NULL_RTX, 0, methods);
1227 if (!temp)
1228 return NULL_RTX;
1229 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1230 NULL_RTX, 0, OPTAB_DIRECT);
1233 if (!op1_high)
1234 return NULL_RTX;
1237 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1238 NULL_RTX, 0, OPTAB_DIRECT);
1239 if (!temp)
1240 return NULL_RTX;
1242 /* OP1_HIGH should now be dead. */
1244 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1245 NULL_RTX, 0, OPTAB_DIRECT);
1247 if (target && !REG_P (target))
1248 target = NULL_RTX;
1250 if (umulp)
1251 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1252 target, 1, OPTAB_DIRECT);
1253 else
1254 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1255 target, 1, OPTAB_DIRECT);
1257 if (!product)
1258 return NULL_RTX;
1260 product_high = operand_subword (product, high, 1, mode);
1261 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1262 NULL_RTX, 0, OPTAB_DIRECT);
1263 emit_move_insn (product_high, adjust);
1264 return product;
1267 /* Wrapper around expand_binop which takes an rtx code to specify
1268 the operation to perform, not an optab pointer. All other
1269 arguments are the same. */
1271 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
1272 rtx op1, rtx target, int unsignedp,
1273 enum optab_methods methods)
1275 optab binop = code_to_optab (code);
1276 gcc_assert (binop);
1278 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1281 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1282 binop. Order them according to commutative_operand_precedence and, if
1283 possible, try to put TARGET or a pseudo first. */
1284 static bool
1285 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1287 int op0_prec = commutative_operand_precedence (op0);
1288 int op1_prec = commutative_operand_precedence (op1);
1290 if (op0_prec < op1_prec)
1291 return true;
1293 if (op0_prec > op1_prec)
1294 return false;
1296 /* With equal precedence, both orders are ok, but it is better if the
1297 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1298 if (target == 0 || REG_P (target))
1299 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1300 else
1301 return rtx_equal_p (op1, target);
1304 /* Return true if BINOPTAB implements a shift operation. */
1306 static bool
1307 shift_optab_p (optab binoptab)
1309 switch (optab_to_code (binoptab))
1311 case ASHIFT:
1312 case SS_ASHIFT:
1313 case US_ASHIFT:
1314 case ASHIFTRT:
1315 case LSHIFTRT:
1316 case ROTATE:
1317 case ROTATERT:
1318 return true;
1320 default:
1321 return false;
1325 /* Return true if BINOPTAB implements a commutative binary operation. */
1327 static bool
1328 commutative_optab_p (optab binoptab)
1330 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1331 || binoptab == smul_widen_optab
1332 || binoptab == umul_widen_optab
1333 || binoptab == smul_highpart_optab
1334 || binoptab == umul_highpart_optab);
1337 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1338 optimizing, and if the operand is a constant that costs more than
1339 1 instruction, force the constant into a register and return that
1340 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1342 static rtx
1343 avoid_expensive_constant (enum machine_mode mode, optab binoptab,
1344 int opn, rtx x, bool unsignedp)
1346 bool speed = optimize_insn_for_speed_p ();
1348 if (mode != VOIDmode
1349 && optimize
1350 && CONSTANT_P (x)
1351 && (rtx_cost (x, optab_to_code (binoptab), opn, speed)
1352 > set_src_cost (x, speed)))
1354 if (CONST_INT_P (x))
1356 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1357 if (intval != INTVAL (x))
1358 x = GEN_INT (intval);
1360 else
1361 x = convert_modes (mode, VOIDmode, x, unsignedp);
1362 x = force_reg (mode, x);
1364 return x;
1367 /* Helper function for expand_binop: handle the case where there
1368 is an insn that directly implements the indicated operation.
1369 Returns null if this is not possible. */
1370 static rtx
1371 expand_binop_directly (enum machine_mode mode, optab binoptab,
1372 rtx op0, rtx op1,
1373 rtx target, int unsignedp, enum optab_methods methods,
1374 rtx last)
1376 enum machine_mode from_mode = widened_mode (mode, op0, op1);
1377 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1378 from_mode, 1);
1379 enum machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1380 enum machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1381 enum machine_mode mode0, mode1, tmp_mode;
1382 struct expand_operand ops[3];
1383 bool commutative_p;
1384 rtx pat;
1385 rtx xop0 = op0, xop1 = op1;
1386 rtx swap;
1388 /* If it is a commutative operator and the modes would match
1389 if we would swap the operands, we can save the conversions. */
1390 commutative_p = commutative_optab_p (binoptab);
1391 if (commutative_p
1392 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1393 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1395 swap = xop0;
1396 xop0 = xop1;
1397 xop1 = swap;
1400 /* If we are optimizing, force expensive constants into a register. */
1401 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1402 if (!shift_optab_p (binoptab))
1403 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1405 /* In case the insn wants input operands in modes different from
1406 those of the actual operands, convert the operands. It would
1407 seem that we don't need to convert CONST_INTs, but we do, so
1408 that they're properly zero-extended, sign-extended or truncated
1409 for their mode. */
1411 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1412 if (xmode0 != VOIDmode && xmode0 != mode0)
1414 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1415 mode0 = xmode0;
1418 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1419 if (xmode1 != VOIDmode && xmode1 != mode1)
1421 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1422 mode1 = xmode1;
1425 /* If operation is commutative,
1426 try to make the first operand a register.
1427 Even better, try to make it the same as the target.
1428 Also try to make the last operand a constant. */
1429 if (commutative_p
1430 && swap_commutative_operands_with_target (target, xop0, xop1))
1432 swap = xop1;
1433 xop1 = xop0;
1434 xop0 = swap;
1437 /* Now, if insn's predicates don't allow our operands, put them into
1438 pseudo regs. */
1440 if (binoptab == vec_pack_trunc_optab
1441 || binoptab == vec_pack_usat_optab
1442 || binoptab == vec_pack_ssat_optab
1443 || binoptab == vec_pack_ufix_trunc_optab
1444 || binoptab == vec_pack_sfix_trunc_optab)
1446 /* The mode of the result is different then the mode of the
1447 arguments. */
1448 tmp_mode = insn_data[(int) icode].operand[0].mode;
1449 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1451 delete_insns_since (last);
1452 return NULL_RTX;
1455 else
1456 tmp_mode = mode;
1458 create_output_operand (&ops[0], target, tmp_mode);
1459 create_input_operand (&ops[1], xop0, mode0);
1460 create_input_operand (&ops[2], xop1, mode1);
1461 pat = maybe_gen_insn (icode, 3, ops);
1462 if (pat)
1464 /* If PAT is composed of more than one insn, try to add an appropriate
1465 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1466 operand, call expand_binop again, this time without a target. */
1467 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1468 && ! add_equal_note (pat, ops[0].value, optab_to_code (binoptab),
1469 ops[1].value, ops[2].value))
1471 delete_insns_since (last);
1472 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1473 unsignedp, methods);
1476 emit_insn (pat);
1477 return ops[0].value;
1479 delete_insns_since (last);
1480 return NULL_RTX;
1483 /* Generate code to perform an operation specified by BINOPTAB
1484 on operands OP0 and OP1, with result having machine-mode MODE.
1486 UNSIGNEDP is for the case where we have to widen the operands
1487 to perform the operation. It says to use zero-extension.
1489 If TARGET is nonzero, the value
1490 is generated there, if it is convenient to do so.
1491 In all cases an rtx is returned for the locus of the value;
1492 this may or may not be TARGET. */
1495 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
1496 rtx target, int unsignedp, enum optab_methods methods)
1498 enum optab_methods next_methods
1499 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1500 ? OPTAB_WIDEN : methods);
1501 enum mode_class mclass;
1502 enum machine_mode wider_mode;
1503 rtx libfunc;
1504 rtx temp;
1505 rtx entry_last = get_last_insn ();
1506 rtx last;
1508 mclass = GET_MODE_CLASS (mode);
1510 /* If subtracting an integer constant, convert this into an addition of
1511 the negated constant. */
1513 if (binoptab == sub_optab && CONST_INT_P (op1))
1515 op1 = negate_rtx (mode, op1);
1516 binoptab = add_optab;
1519 /* Record where to delete back to if we backtrack. */
1520 last = get_last_insn ();
1522 /* If we can do it with a three-operand insn, do so. */
1524 if (methods != OPTAB_MUST_WIDEN
1525 && find_widening_optab_handler (binoptab, mode,
1526 widened_mode (mode, op0, op1), 1)
1527 != CODE_FOR_nothing)
1529 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1530 unsignedp, methods, last);
1531 if (temp)
1532 return temp;
1535 /* If we were trying to rotate, and that didn't work, try rotating
1536 the other direction before falling back to shifts and bitwise-or. */
1537 if (((binoptab == rotl_optab
1538 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1539 || (binoptab == rotr_optab
1540 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1541 && mclass == MODE_INT)
1543 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1544 rtx newop1;
1545 unsigned int bits = GET_MODE_PRECISION (mode);
1547 if (CONST_INT_P (op1))
1548 newop1 = GEN_INT (bits - INTVAL (op1));
1549 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1550 newop1 = negate_rtx (GET_MODE (op1), op1);
1551 else
1552 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1553 GEN_INT (bits), op1,
1554 NULL_RTX, unsignedp, OPTAB_DIRECT);
1556 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1557 target, unsignedp, methods, last);
1558 if (temp)
1559 return temp;
1562 /* If this is a multiply, see if we can do a widening operation that
1563 takes operands of this mode and makes a wider mode. */
1565 if (binoptab == smul_optab
1566 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1567 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1568 : smul_widen_optab),
1569 GET_MODE_2XWIDER_MODE (mode), mode)
1570 != CODE_FOR_nothing))
1572 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1573 unsignedp ? umul_widen_optab : smul_widen_optab,
1574 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1576 if (temp != 0)
1578 if (GET_MODE_CLASS (mode) == MODE_INT
1579 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1580 return gen_lowpart (mode, temp);
1581 else
1582 return convert_to_mode (mode, temp, unsignedp);
1586 /* If this is a vector shift by a scalar, see if we can do a vector
1587 shift by a vector. If so, broadcast the scalar into a vector. */
1588 if (mclass == MODE_VECTOR_INT)
1590 optab otheroptab = unknown_optab;
1592 if (binoptab == ashl_optab)
1593 otheroptab = vashl_optab;
1594 else if (binoptab == ashr_optab)
1595 otheroptab = vashr_optab;
1596 else if (binoptab == lshr_optab)
1597 otheroptab = vlshr_optab;
1598 else if (binoptab == rotl_optab)
1599 otheroptab = vrotl_optab;
1600 else if (binoptab == rotr_optab)
1601 otheroptab = vrotr_optab;
1603 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1605 rtx vop1 = expand_vector_broadcast (mode, op1);
1606 if (vop1)
1608 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1609 target, unsignedp, methods, last);
1610 if (temp)
1611 return temp;
1616 /* Look for a wider mode of the same class for which we think we
1617 can open-code the operation. Check for a widening multiply at the
1618 wider mode as well. */
1620 if (CLASS_HAS_WIDER_MODES_P (mclass)
1621 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1622 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1623 wider_mode != VOIDmode;
1624 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1626 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1627 || (binoptab == smul_optab
1628 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1629 && (find_widening_optab_handler ((unsignedp
1630 ? umul_widen_optab
1631 : smul_widen_optab),
1632 GET_MODE_WIDER_MODE (wider_mode),
1633 mode, 0)
1634 != CODE_FOR_nothing)))
1636 rtx xop0 = op0, xop1 = op1;
1637 int no_extend = 0;
1639 /* For certain integer operations, we need not actually extend
1640 the narrow operands, as long as we will truncate
1641 the results to the same narrowness. */
1643 if ((binoptab == ior_optab || binoptab == and_optab
1644 || binoptab == xor_optab
1645 || binoptab == add_optab || binoptab == sub_optab
1646 || binoptab == smul_optab || binoptab == ashl_optab)
1647 && mclass == MODE_INT)
1649 no_extend = 1;
1650 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1651 xop0, unsignedp);
1652 if (binoptab != ashl_optab)
1653 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1654 xop1, unsignedp);
1657 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1659 /* The second operand of a shift must always be extended. */
1660 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1661 no_extend && binoptab != ashl_optab);
1663 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1664 unsignedp, OPTAB_DIRECT);
1665 if (temp)
1667 if (mclass != MODE_INT
1668 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1670 if (target == 0)
1671 target = gen_reg_rtx (mode);
1672 convert_move (target, temp, 0);
1673 return target;
1675 else
1676 return gen_lowpart (mode, temp);
1678 else
1679 delete_insns_since (last);
1683 /* If operation is commutative,
1684 try to make the first operand a register.
1685 Even better, try to make it the same as the target.
1686 Also try to make the last operand a constant. */
1687 if (commutative_optab_p (binoptab)
1688 && swap_commutative_operands_with_target (target, op0, op1))
1690 temp = op1;
1691 op1 = op0;
1692 op0 = temp;
1695 /* These can be done a word at a time. */
1696 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1697 && mclass == MODE_INT
1698 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1699 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1701 int i;
1702 rtx insns;
1704 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1705 won't be accurate, so use a new target. */
1706 if (target == 0
1707 || target == op0
1708 || target == op1
1709 || !valid_multiword_target_p (target))
1710 target = gen_reg_rtx (mode);
1712 start_sequence ();
1714 /* Do the actual arithmetic. */
1715 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1717 rtx target_piece = operand_subword (target, i, 1, mode);
1718 rtx x = expand_binop (word_mode, binoptab,
1719 operand_subword_force (op0, i, mode),
1720 operand_subword_force (op1, i, mode),
1721 target_piece, unsignedp, next_methods);
1723 if (x == 0)
1724 break;
1726 if (target_piece != x)
1727 emit_move_insn (target_piece, x);
1730 insns = get_insns ();
1731 end_sequence ();
1733 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1735 emit_insn (insns);
1736 return target;
1740 /* Synthesize double word shifts from single word shifts. */
1741 if ((binoptab == lshr_optab || binoptab == ashl_optab
1742 || binoptab == ashr_optab)
1743 && mclass == MODE_INT
1744 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1745 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1746 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1747 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1748 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1749 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1751 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1752 enum machine_mode op1_mode;
1754 double_shift_mask = targetm.shift_truncation_mask (mode);
1755 shift_mask = targetm.shift_truncation_mask (word_mode);
1756 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1758 /* Apply the truncation to constant shifts. */
1759 if (double_shift_mask > 0 && CONST_INT_P (op1))
1760 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1762 if (op1 == CONST0_RTX (op1_mode))
1763 return op0;
1765 /* Make sure that this is a combination that expand_doubleword_shift
1766 can handle. See the comments there for details. */
1767 if (double_shift_mask == 0
1768 || (shift_mask == BITS_PER_WORD - 1
1769 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1771 rtx insns;
1772 rtx into_target, outof_target;
1773 rtx into_input, outof_input;
1774 int left_shift, outof_word;
1776 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1777 won't be accurate, so use a new target. */
1778 if (target == 0
1779 || target == op0
1780 || target == op1
1781 || !valid_multiword_target_p (target))
1782 target = gen_reg_rtx (mode);
1784 start_sequence ();
1786 /* OUTOF_* is the word we are shifting bits away from, and
1787 INTO_* is the word that we are shifting bits towards, thus
1788 they differ depending on the direction of the shift and
1789 WORDS_BIG_ENDIAN. */
1791 left_shift = binoptab == ashl_optab;
1792 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1794 outof_target = operand_subword (target, outof_word, 1, mode);
1795 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1797 outof_input = operand_subword_force (op0, outof_word, mode);
1798 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1800 if (expand_doubleword_shift (op1_mode, binoptab,
1801 outof_input, into_input, op1,
1802 outof_target, into_target,
1803 unsignedp, next_methods, shift_mask))
1805 insns = get_insns ();
1806 end_sequence ();
1808 emit_insn (insns);
1809 return target;
1811 end_sequence ();
1815 /* Synthesize double word rotates from single word shifts. */
1816 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1817 && mclass == MODE_INT
1818 && CONST_INT_P (op1)
1819 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1820 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1821 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1823 rtx insns;
1824 rtx into_target, outof_target;
1825 rtx into_input, outof_input;
1826 rtx inter;
1827 int shift_count, left_shift, outof_word;
1829 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1830 won't be accurate, so use a new target. Do this also if target is not
1831 a REG, first because having a register instead may open optimization
1832 opportunities, and second because if target and op0 happen to be MEMs
1833 designating the same location, we would risk clobbering it too early
1834 in the code sequence we generate below. */
1835 if (target == 0
1836 || target == op0
1837 || target == op1
1838 || !REG_P (target)
1839 || !valid_multiword_target_p (target))
1840 target = gen_reg_rtx (mode);
1842 start_sequence ();
1844 shift_count = INTVAL (op1);
1846 /* OUTOF_* is the word we are shifting bits away from, and
1847 INTO_* is the word that we are shifting bits towards, thus
1848 they differ depending on the direction of the shift and
1849 WORDS_BIG_ENDIAN. */
1851 left_shift = (binoptab == rotl_optab);
1852 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1854 outof_target = operand_subword (target, outof_word, 1, mode);
1855 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1857 outof_input = operand_subword_force (op0, outof_word, mode);
1858 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1860 if (shift_count == BITS_PER_WORD)
1862 /* This is just a word swap. */
1863 emit_move_insn (outof_target, into_input);
1864 emit_move_insn (into_target, outof_input);
1865 inter = const0_rtx;
1867 else
1869 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1870 rtx first_shift_count, second_shift_count;
1871 optab reverse_unsigned_shift, unsigned_shift;
1873 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1874 ? lshr_optab : ashl_optab);
1876 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1877 ? ashl_optab : lshr_optab);
1879 if (shift_count > BITS_PER_WORD)
1881 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1882 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1884 else
1886 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1887 second_shift_count = GEN_INT (shift_count);
1890 into_temp1 = expand_binop (word_mode, unsigned_shift,
1891 outof_input, first_shift_count,
1892 NULL_RTX, unsignedp, next_methods);
1893 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1894 into_input, second_shift_count,
1895 NULL_RTX, unsignedp, next_methods);
1897 if (into_temp1 != 0 && into_temp2 != 0)
1898 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1899 into_target, unsignedp, next_methods);
1900 else
1901 inter = 0;
1903 if (inter != 0 && inter != into_target)
1904 emit_move_insn (into_target, inter);
1906 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1907 into_input, first_shift_count,
1908 NULL_RTX, unsignedp, next_methods);
1909 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1910 outof_input, second_shift_count,
1911 NULL_RTX, unsignedp, next_methods);
1913 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1914 inter = expand_binop (word_mode, ior_optab,
1915 outof_temp1, outof_temp2,
1916 outof_target, unsignedp, next_methods);
1918 if (inter != 0 && inter != outof_target)
1919 emit_move_insn (outof_target, inter);
1922 insns = get_insns ();
1923 end_sequence ();
1925 if (inter != 0)
1927 emit_insn (insns);
1928 return target;
1932 /* These can be done a word at a time by propagating carries. */
1933 if ((binoptab == add_optab || binoptab == sub_optab)
1934 && mclass == MODE_INT
1935 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1936 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1938 unsigned int i;
1939 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1940 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1941 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1942 rtx xop0, xop1, xtarget;
1944 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1945 value is one of those, use it. Otherwise, use 1 since it is the
1946 one easiest to get. */
1947 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1948 int normalizep = STORE_FLAG_VALUE;
1949 #else
1950 int normalizep = 1;
1951 #endif
1953 /* Prepare the operands. */
1954 xop0 = force_reg (mode, op0);
1955 xop1 = force_reg (mode, op1);
1957 xtarget = gen_reg_rtx (mode);
1959 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1960 target = xtarget;
1962 /* Indicate for flow that the entire target reg is being set. */
1963 if (REG_P (target))
1964 emit_clobber (xtarget);
1966 /* Do the actual arithmetic. */
1967 for (i = 0; i < nwords; i++)
1969 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1970 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1971 rtx op0_piece = operand_subword_force (xop0, index, mode);
1972 rtx op1_piece = operand_subword_force (xop1, index, mode);
1973 rtx x;
1975 /* Main add/subtract of the input operands. */
1976 x = expand_binop (word_mode, binoptab,
1977 op0_piece, op1_piece,
1978 target_piece, unsignedp, next_methods);
1979 if (x == 0)
1980 break;
1982 if (i + 1 < nwords)
1984 /* Store carry from main add/subtract. */
1985 carry_out = gen_reg_rtx (word_mode);
1986 carry_out = emit_store_flag_force (carry_out,
1987 (binoptab == add_optab
1988 ? LT : GT),
1989 x, op0_piece,
1990 word_mode, 1, normalizep);
1993 if (i > 0)
1995 rtx newx;
1997 /* Add/subtract previous carry to main result. */
1998 newx = expand_binop (word_mode,
1999 normalizep == 1 ? binoptab : otheroptab,
2000 x, carry_in,
2001 NULL_RTX, 1, next_methods);
2003 if (i + 1 < nwords)
2005 /* Get out carry from adding/subtracting carry in. */
2006 rtx carry_tmp = gen_reg_rtx (word_mode);
2007 carry_tmp = emit_store_flag_force (carry_tmp,
2008 (binoptab == add_optab
2009 ? LT : GT),
2010 newx, x,
2011 word_mode, 1, normalizep);
2013 /* Logical-ior the two poss. carry together. */
2014 carry_out = expand_binop (word_mode, ior_optab,
2015 carry_out, carry_tmp,
2016 carry_out, 0, next_methods);
2017 if (carry_out == 0)
2018 break;
2020 emit_move_insn (target_piece, newx);
2022 else
2024 if (x != target_piece)
2025 emit_move_insn (target_piece, x);
2028 carry_in = carry_out;
2031 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2033 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2034 || ! rtx_equal_p (target, xtarget))
2036 rtx temp = emit_move_insn (target, xtarget);
2038 set_dst_reg_note (temp, REG_EQUAL,
2039 gen_rtx_fmt_ee (optab_to_code (binoptab),
2040 mode, copy_rtx (xop0),
2041 copy_rtx (xop1)),
2042 target);
2044 else
2045 target = xtarget;
2047 return target;
2050 else
2051 delete_insns_since (last);
2054 /* Attempt to synthesize double word multiplies using a sequence of word
2055 mode multiplications. We first attempt to generate a sequence using a
2056 more efficient unsigned widening multiply, and if that fails we then
2057 try using a signed widening multiply. */
2059 if (binoptab == smul_optab
2060 && mclass == MODE_INT
2061 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2062 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2063 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2065 rtx product = NULL_RTX;
2066 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
2067 != CODE_FOR_nothing)
2069 product = expand_doubleword_mult (mode, op0, op1, target,
2070 true, methods);
2071 if (!product)
2072 delete_insns_since (last);
2075 if (product == NULL_RTX
2076 && widening_optab_handler (smul_widen_optab, mode, word_mode)
2077 != CODE_FOR_nothing)
2079 product = expand_doubleword_mult (mode, op0, op1, target,
2080 false, methods);
2081 if (!product)
2082 delete_insns_since (last);
2085 if (product != NULL_RTX)
2087 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2089 temp = emit_move_insn (target ? target : product, product);
2090 set_dst_reg_note (temp,
2091 REG_EQUAL,
2092 gen_rtx_fmt_ee (MULT, mode,
2093 copy_rtx (op0),
2094 copy_rtx (op1)),
2095 target ? target : product);
2097 return product;
2101 /* It can't be open-coded in this mode.
2102 Use a library call if one is available and caller says that's ok. */
2104 libfunc = optab_libfunc (binoptab, mode);
2105 if (libfunc
2106 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2108 rtx insns;
2109 rtx op1x = op1;
2110 enum machine_mode op1_mode = mode;
2111 rtx value;
2113 start_sequence ();
2115 if (shift_optab_p (binoptab))
2117 op1_mode = targetm.libgcc_shift_count_mode ();
2118 /* Specify unsigned here,
2119 since negative shift counts are meaningless. */
2120 op1x = convert_to_mode (op1_mode, op1, 1);
2123 if (GET_MODE (op0) != VOIDmode
2124 && GET_MODE (op0) != mode)
2125 op0 = convert_to_mode (mode, op0, unsignedp);
2127 /* Pass 1 for NO_QUEUE so we don't lose any increments
2128 if the libcall is cse'd or moved. */
2129 value = emit_library_call_value (libfunc,
2130 NULL_RTX, LCT_CONST, mode, 2,
2131 op0, mode, op1x, op1_mode);
2133 insns = get_insns ();
2134 end_sequence ();
2136 target = gen_reg_rtx (mode);
2137 emit_libcall_block_1 (insns, target, value,
2138 gen_rtx_fmt_ee (optab_to_code (binoptab),
2139 mode, op0, op1),
2140 trapv_binoptab_p (binoptab));
2142 return target;
2145 delete_insns_since (last);
2147 /* It can't be done in this mode. Can we do it in a wider mode? */
2149 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2150 || methods == OPTAB_MUST_WIDEN))
2152 /* Caller says, don't even try. */
2153 delete_insns_since (entry_last);
2154 return 0;
2157 /* Compute the value of METHODS to pass to recursive calls.
2158 Don't allow widening to be tried recursively. */
2160 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2162 /* Look for a wider mode of the same class for which it appears we can do
2163 the operation. */
2165 if (CLASS_HAS_WIDER_MODES_P (mclass))
2167 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2168 wider_mode != VOIDmode;
2169 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2171 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2172 != CODE_FOR_nothing
2173 || (methods == OPTAB_LIB
2174 && optab_libfunc (binoptab, wider_mode)))
2176 rtx xop0 = op0, xop1 = op1;
2177 int no_extend = 0;
2179 /* For certain integer operations, we need not actually extend
2180 the narrow operands, as long as we will truncate
2181 the results to the same narrowness. */
2183 if ((binoptab == ior_optab || binoptab == and_optab
2184 || binoptab == xor_optab
2185 || binoptab == add_optab || binoptab == sub_optab
2186 || binoptab == smul_optab || binoptab == ashl_optab)
2187 && mclass == MODE_INT)
2188 no_extend = 1;
2190 xop0 = widen_operand (xop0, wider_mode, mode,
2191 unsignedp, no_extend);
2193 /* The second operand of a shift must always be extended. */
2194 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2195 no_extend && binoptab != ashl_optab);
2197 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2198 unsignedp, methods);
2199 if (temp)
2201 if (mclass != MODE_INT
2202 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2204 if (target == 0)
2205 target = gen_reg_rtx (mode);
2206 convert_move (target, temp, 0);
2207 return target;
2209 else
2210 return gen_lowpart (mode, temp);
2212 else
2213 delete_insns_since (last);
2218 delete_insns_since (entry_last);
2219 return 0;
2222 /* Expand a binary operator which has both signed and unsigned forms.
2223 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2224 signed operations.
2226 If we widen unsigned operands, we may use a signed wider operation instead
2227 of an unsigned wider operation, since the result would be the same. */
2230 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
2231 rtx op0, rtx op1, rtx target, int unsignedp,
2232 enum optab_methods methods)
2234 rtx temp;
2235 optab direct_optab = unsignedp ? uoptab : soptab;
2236 bool save_enable;
2238 /* Do it without widening, if possible. */
2239 temp = expand_binop (mode, direct_optab, op0, op1, target,
2240 unsignedp, OPTAB_DIRECT);
2241 if (temp || methods == OPTAB_DIRECT)
2242 return temp;
2244 /* Try widening to a signed int. Disable any direct use of any
2245 signed insn in the current mode. */
2246 save_enable = swap_optab_enable (soptab, mode, false);
2248 temp = expand_binop (mode, soptab, op0, op1, target,
2249 unsignedp, OPTAB_WIDEN);
2251 /* For unsigned operands, try widening to an unsigned int. */
2252 if (!temp && unsignedp)
2253 temp = expand_binop (mode, uoptab, op0, op1, target,
2254 unsignedp, OPTAB_WIDEN);
2255 if (temp || methods == OPTAB_WIDEN)
2256 goto egress;
2258 /* Use the right width libcall if that exists. */
2259 temp = expand_binop (mode, direct_optab, op0, op1, target,
2260 unsignedp, OPTAB_LIB);
2261 if (temp || methods == OPTAB_LIB)
2262 goto egress;
2264 /* Must widen and use a libcall, use either signed or unsigned. */
2265 temp = expand_binop (mode, soptab, op0, op1, target,
2266 unsignedp, methods);
2267 if (!temp && unsignedp)
2268 temp = expand_binop (mode, uoptab, op0, op1, target,
2269 unsignedp, methods);
2271 egress:
2272 /* Undo the fiddling above. */
2273 if (save_enable)
2274 swap_optab_enable (soptab, mode, true);
2275 return temp;
2278 /* Generate code to perform an operation specified by UNOPPTAB
2279 on operand OP0, with two results to TARG0 and TARG1.
2280 We assume that the order of the operands for the instruction
2281 is TARG0, TARG1, OP0.
2283 Either TARG0 or TARG1 may be zero, but what that means is that
2284 the result is not actually wanted. We will generate it into
2285 a dummy pseudo-reg and discard it. They may not both be zero.
2287 Returns 1 if this operation can be performed; 0 if not. */
2290 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2291 int unsignedp)
2293 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2294 enum mode_class mclass;
2295 enum machine_mode wider_mode;
2296 rtx entry_last = get_last_insn ();
2297 rtx last;
2299 mclass = GET_MODE_CLASS (mode);
2301 if (!targ0)
2302 targ0 = gen_reg_rtx (mode);
2303 if (!targ1)
2304 targ1 = gen_reg_rtx (mode);
2306 /* Record where to go back to if we fail. */
2307 last = get_last_insn ();
2309 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2311 struct expand_operand ops[3];
2312 enum insn_code icode = optab_handler (unoptab, mode);
2314 create_fixed_operand (&ops[0], targ0);
2315 create_fixed_operand (&ops[1], targ1);
2316 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2317 if (maybe_expand_insn (icode, 3, ops))
2318 return 1;
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 (unoptab, 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);
2335 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2337 convert_move (targ0, t0, unsignedp);
2338 convert_move (targ1, t1, unsignedp);
2339 return 1;
2341 else
2342 delete_insns_since (last);
2347 delete_insns_since (entry_last);
2348 return 0;
2351 /* Generate code to perform an operation specified by BINOPTAB
2352 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2353 We assume that the order of the operands for the instruction
2354 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2355 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2357 Either TARG0 or TARG1 may be zero, but what that means is that
2358 the result is not actually wanted. We will generate it into
2359 a dummy pseudo-reg and discard it. They may not both be zero.
2361 Returns 1 if this operation can be performed; 0 if not. */
2364 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2365 int unsignedp)
2367 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2368 enum mode_class mclass;
2369 enum machine_mode wider_mode;
2370 rtx entry_last = get_last_insn ();
2371 rtx last;
2373 mclass = GET_MODE_CLASS (mode);
2375 if (!targ0)
2376 targ0 = gen_reg_rtx (mode);
2377 if (!targ1)
2378 targ1 = gen_reg_rtx (mode);
2380 /* Record where to go back to if we fail. */
2381 last = get_last_insn ();
2383 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2385 struct expand_operand ops[4];
2386 enum insn_code icode = optab_handler (binoptab, mode);
2387 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2388 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
2389 rtx xop0 = op0, xop1 = op1;
2391 /* If we are optimizing, force expensive constants into a register. */
2392 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2393 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2395 create_fixed_operand (&ops[0], targ0);
2396 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2397 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2398 create_fixed_operand (&ops[3], targ1);
2399 if (maybe_expand_insn (icode, 4, ops))
2400 return 1;
2401 delete_insns_since (last);
2404 /* It can't be done in this mode. Can we do it in a wider mode? */
2406 if (CLASS_HAS_WIDER_MODES_P (mclass))
2408 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2409 wider_mode != VOIDmode;
2410 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2412 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2414 rtx t0 = gen_reg_rtx (wider_mode);
2415 rtx t1 = gen_reg_rtx (wider_mode);
2416 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2417 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2419 if (expand_twoval_binop (binoptab, cop0, cop1,
2420 t0, t1, unsignedp))
2422 convert_move (targ0, t0, unsignedp);
2423 convert_move (targ1, t1, unsignedp);
2424 return 1;
2426 else
2427 delete_insns_since (last);
2432 delete_insns_since (entry_last);
2433 return 0;
2436 /* Expand the two-valued library call indicated by BINOPTAB, but
2437 preserve only one of the values. If TARG0 is non-NULL, the first
2438 value is placed into TARG0; otherwise the second value is placed
2439 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2440 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2441 This routine assumes that the value returned by the library call is
2442 as if the return value was of an integral mode twice as wide as the
2443 mode of OP0. Returns 1 if the call was successful. */
2445 bool
2446 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2447 rtx targ0, rtx targ1, enum rtx_code code)
2449 enum machine_mode mode;
2450 enum machine_mode libval_mode;
2451 rtx libval;
2452 rtx insns;
2453 rtx libfunc;
2455 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2456 gcc_assert (!targ0 != !targ1);
2458 mode = GET_MODE (op0);
2459 libfunc = optab_libfunc (binoptab, mode);
2460 if (!libfunc)
2461 return false;
2463 /* The value returned by the library function will have twice as
2464 many bits as the nominal MODE. */
2465 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2466 MODE_INT);
2467 start_sequence ();
2468 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2469 libval_mode, 2,
2470 op0, mode,
2471 op1, mode);
2472 /* Get the part of VAL containing the value that we want. */
2473 libval = simplify_gen_subreg (mode, libval, libval_mode,
2474 targ0 ? 0 : GET_MODE_SIZE (mode));
2475 insns = get_insns ();
2476 end_sequence ();
2477 /* Move the into the desired location. */
2478 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2479 gen_rtx_fmt_ee (code, mode, op0, op1));
2481 return true;
2485 /* Wrapper around expand_unop which takes an rtx code to specify
2486 the operation to perform, not an optab pointer. All other
2487 arguments are the same. */
2489 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2490 rtx target, int unsignedp)
2492 optab unop = code_to_optab (code);
2493 gcc_assert (unop);
2495 return expand_unop (mode, unop, op0, target, unsignedp);
2498 /* Try calculating
2499 (clz:narrow x)
2501 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2503 A similar operation can be used for clrsb. UNOPTAB says which operation
2504 we are trying to expand. */
2505 static rtx
2506 widen_leading (enum machine_mode mode, rtx op0, rtx target, optab unoptab)
2508 enum mode_class mclass = GET_MODE_CLASS (mode);
2509 if (CLASS_HAS_WIDER_MODES_P (mclass))
2511 enum machine_mode wider_mode;
2512 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2513 wider_mode != VOIDmode;
2514 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2516 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2518 rtx xop0, temp, last;
2520 last = get_last_insn ();
2522 if (target == 0)
2523 target = gen_reg_rtx (mode);
2524 xop0 = widen_operand (op0, wider_mode, mode,
2525 unoptab != clrsb_optab, false);
2526 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2527 unoptab != clrsb_optab);
2528 if (temp != 0)
2529 temp = expand_binop (wider_mode, sub_optab, temp,
2530 GEN_INT (GET_MODE_PRECISION (wider_mode)
2531 - GET_MODE_PRECISION (mode)),
2532 target, true, OPTAB_DIRECT);
2533 if (temp == 0)
2534 delete_insns_since (last);
2536 return temp;
2540 return 0;
2543 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2544 quantities, choosing which based on whether the high word is nonzero. */
2545 static rtx
2546 expand_doubleword_clz (enum machine_mode mode, rtx op0, rtx target)
2548 rtx xop0 = force_reg (mode, op0);
2549 rtx subhi = gen_highpart (word_mode, xop0);
2550 rtx sublo = gen_lowpart (word_mode, xop0);
2551 rtx hi0_label = gen_label_rtx ();
2552 rtx after_label = gen_label_rtx ();
2553 rtx seq, temp, result;
2555 /* If we were not given a target, use a word_mode register, not a
2556 'mode' register. The result will fit, and nobody is expecting
2557 anything bigger (the return type of __builtin_clz* is int). */
2558 if (!target)
2559 target = gen_reg_rtx (word_mode);
2561 /* In any case, write to a word_mode scratch in both branches of the
2562 conditional, so we can ensure there is a single move insn setting
2563 'target' to tag a REG_EQUAL note on. */
2564 result = gen_reg_rtx (word_mode);
2566 start_sequence ();
2568 /* If the high word is not equal to zero,
2569 then clz of the full value is clz of the high word. */
2570 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2571 word_mode, true, hi0_label);
2573 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2574 if (!temp)
2575 goto fail;
2577 if (temp != result)
2578 convert_move (result, temp, true);
2580 emit_jump_insn (gen_jump (after_label));
2581 emit_barrier ();
2583 /* Else clz of the full value is clz of the low word plus the number
2584 of bits in the high word. */
2585 emit_label (hi0_label);
2587 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2588 if (!temp)
2589 goto fail;
2590 temp = expand_binop (word_mode, add_optab, temp,
2591 GEN_INT (GET_MODE_BITSIZE (word_mode)),
2592 result, true, OPTAB_DIRECT);
2593 if (!temp)
2594 goto fail;
2595 if (temp != result)
2596 convert_move (result, temp, true);
2598 emit_label (after_label);
2599 convert_move (target, result, true);
2601 seq = get_insns ();
2602 end_sequence ();
2604 add_equal_note (seq, target, CLZ, xop0, 0);
2605 emit_insn (seq);
2606 return target;
2608 fail:
2609 end_sequence ();
2610 return 0;
2613 /* Try calculating
2614 (bswap:narrow x)
2616 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2617 static rtx
2618 widen_bswap (enum machine_mode mode, rtx op0, rtx target)
2620 enum mode_class mclass = GET_MODE_CLASS (mode);
2621 enum machine_mode wider_mode;
2622 rtx x, last;
2624 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2625 return NULL_RTX;
2627 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2628 wider_mode != VOIDmode;
2629 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2630 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2631 goto found;
2632 return NULL_RTX;
2634 found:
2635 last = get_last_insn ();
2637 x = widen_operand (op0, wider_mode, mode, true, true);
2638 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2640 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2641 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2642 if (x != 0)
2643 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2644 GET_MODE_BITSIZE (wider_mode)
2645 - GET_MODE_BITSIZE (mode),
2646 NULL_RTX, true);
2648 if (x != 0)
2650 if (target == 0)
2651 target = gen_reg_rtx (mode);
2652 emit_move_insn (target, gen_lowpart (mode, x));
2654 else
2655 delete_insns_since (last);
2657 return target;
2660 /* Try calculating bswap as two bswaps of two word-sized operands. */
2662 static rtx
2663 expand_doubleword_bswap (enum machine_mode mode, rtx op, rtx target)
2665 rtx t0, t1;
2667 t1 = expand_unop (word_mode, bswap_optab,
2668 operand_subword_force (op, 0, mode), NULL_RTX, true);
2669 t0 = expand_unop (word_mode, bswap_optab,
2670 operand_subword_force (op, 1, mode), NULL_RTX, true);
2672 if (target == 0 || !valid_multiword_target_p (target))
2673 target = gen_reg_rtx (mode);
2674 if (REG_P (target))
2675 emit_clobber (target);
2676 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2677 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2679 return target;
2682 /* Try calculating (parity x) as (and (popcount x) 1), where
2683 popcount can also be done in a wider mode. */
2684 static rtx
2685 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2687 enum mode_class mclass = GET_MODE_CLASS (mode);
2688 if (CLASS_HAS_WIDER_MODES_P (mclass))
2690 enum machine_mode wider_mode;
2691 for (wider_mode = mode; wider_mode != VOIDmode;
2692 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2694 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2696 rtx xop0, temp, last;
2698 last = get_last_insn ();
2700 if (target == 0)
2701 target = gen_reg_rtx (mode);
2702 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2703 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2704 true);
2705 if (temp != 0)
2706 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2707 target, true, OPTAB_DIRECT);
2708 if (temp == 0)
2709 delete_insns_since (last);
2711 return temp;
2715 return 0;
2718 /* Try calculating ctz(x) as K - clz(x & -x) ,
2719 where K is GET_MODE_PRECISION(mode) - 1.
2721 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2722 don't have to worry about what the hardware does in that case. (If
2723 the clz instruction produces the usual value at 0, which is K, the
2724 result of this code sequence will be -1; expand_ffs, below, relies
2725 on this. It might be nice to have it be K instead, for consistency
2726 with the (very few) processors that provide a ctz with a defined
2727 value, but that would take one more instruction, and it would be
2728 less convenient for expand_ffs anyway. */
2730 static rtx
2731 expand_ctz (enum machine_mode mode, rtx op0, rtx target)
2733 rtx seq, temp;
2735 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2736 return 0;
2738 start_sequence ();
2740 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2741 if (temp)
2742 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2743 true, OPTAB_DIRECT);
2744 if (temp)
2745 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2746 if (temp)
2747 temp = expand_binop (mode, sub_optab, GEN_INT (GET_MODE_PRECISION (mode) - 1),
2748 temp, target,
2749 true, OPTAB_DIRECT);
2750 if (temp == 0)
2752 end_sequence ();
2753 return 0;
2756 seq = get_insns ();
2757 end_sequence ();
2759 add_equal_note (seq, temp, CTZ, op0, 0);
2760 emit_insn (seq);
2761 return temp;
2765 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2766 else with the sequence used by expand_clz.
2768 The ffs builtin promises to return zero for a zero value and ctz/clz
2769 may have an undefined value in that case. If they do not give us a
2770 convenient value, we have to generate a test and branch. */
2771 static rtx
2772 expand_ffs (enum machine_mode mode, rtx op0, rtx target)
2774 HOST_WIDE_INT val = 0;
2775 bool defined_at_zero = false;
2776 rtx temp, seq;
2778 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2780 start_sequence ();
2782 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2783 if (!temp)
2784 goto fail;
2786 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2788 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2790 start_sequence ();
2791 temp = expand_ctz (mode, op0, 0);
2792 if (!temp)
2793 goto fail;
2795 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2797 defined_at_zero = true;
2798 val = (GET_MODE_PRECISION (mode) - 1) - val;
2801 else
2802 return 0;
2804 if (defined_at_zero && val == -1)
2805 /* No correction needed at zero. */;
2806 else
2808 /* We don't try to do anything clever with the situation found
2809 on some processors (eg Alpha) where ctz(0:mode) ==
2810 bitsize(mode). If someone can think of a way to send N to -1
2811 and leave alone all values in the range 0..N-1 (where N is a
2812 power of two), cheaper than this test-and-branch, please add it.
2814 The test-and-branch is done after the operation itself, in case
2815 the operation sets condition codes that can be recycled for this.
2816 (This is true on i386, for instance.) */
2818 rtx nonzero_label = gen_label_rtx ();
2819 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2820 mode, true, nonzero_label);
2822 convert_move (temp, GEN_INT (-1), false);
2823 emit_label (nonzero_label);
2826 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2827 to produce a value in the range 0..bitsize. */
2828 temp = expand_binop (mode, add_optab, temp, GEN_INT (1),
2829 target, false, OPTAB_DIRECT);
2830 if (!temp)
2831 goto fail;
2833 seq = get_insns ();
2834 end_sequence ();
2836 add_equal_note (seq, temp, FFS, op0, 0);
2837 emit_insn (seq);
2838 return temp;
2840 fail:
2841 end_sequence ();
2842 return 0;
2845 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2846 conditions, VAL may already be a SUBREG against which we cannot generate
2847 a further SUBREG. In this case, we expect forcing the value into a
2848 register will work around the situation. */
2850 static rtx
2851 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2852 enum machine_mode imode)
2854 rtx ret;
2855 ret = lowpart_subreg (omode, val, imode);
2856 if (ret == NULL)
2858 val = force_reg (imode, val);
2859 ret = lowpart_subreg (omode, val, imode);
2860 gcc_assert (ret != NULL);
2862 return ret;
2865 /* Expand a floating point absolute value or negation operation via a
2866 logical operation on the sign bit. */
2868 static rtx
2869 expand_absneg_bit (enum rtx_code code, enum machine_mode mode,
2870 rtx op0, rtx target)
2872 const struct real_format *fmt;
2873 int bitpos, word, nwords, i;
2874 enum machine_mode imode;
2875 double_int mask;
2876 rtx temp, insns;
2878 /* The format has to have a simple sign bit. */
2879 fmt = REAL_MODE_FORMAT (mode);
2880 if (fmt == NULL)
2881 return NULL_RTX;
2883 bitpos = fmt->signbit_rw;
2884 if (bitpos < 0)
2885 return NULL_RTX;
2887 /* Don't create negative zeros if the format doesn't support them. */
2888 if (code == NEG && !fmt->has_signed_zero)
2889 return NULL_RTX;
2891 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2893 imode = int_mode_for_mode (mode);
2894 if (imode == BLKmode)
2895 return NULL_RTX;
2896 word = 0;
2897 nwords = 1;
2899 else
2901 imode = word_mode;
2903 if (FLOAT_WORDS_BIG_ENDIAN)
2904 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2905 else
2906 word = bitpos / BITS_PER_WORD;
2907 bitpos = bitpos % BITS_PER_WORD;
2908 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2911 mask = double_int_zero.set_bit (bitpos);
2912 if (code == ABS)
2913 mask = ~mask;
2915 if (target == 0
2916 || target == op0
2917 || (nwords > 1 && !valid_multiword_target_p (target)))
2918 target = gen_reg_rtx (mode);
2920 if (nwords > 1)
2922 start_sequence ();
2924 for (i = 0; i < nwords; ++i)
2926 rtx targ_piece = operand_subword (target, i, 1, mode);
2927 rtx op0_piece = operand_subword_force (op0, i, mode);
2929 if (i == word)
2931 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2932 op0_piece,
2933 immed_double_int_const (mask, imode),
2934 targ_piece, 1, OPTAB_LIB_WIDEN);
2935 if (temp != targ_piece)
2936 emit_move_insn (targ_piece, temp);
2938 else
2939 emit_move_insn (targ_piece, op0_piece);
2942 insns = get_insns ();
2943 end_sequence ();
2945 emit_insn (insns);
2947 else
2949 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2950 gen_lowpart (imode, op0),
2951 immed_double_int_const (mask, imode),
2952 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2953 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2955 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2956 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2957 target);
2960 return target;
2963 /* As expand_unop, but will fail rather than attempt the operation in a
2964 different mode or with a libcall. */
2965 static rtx
2966 expand_unop_direct (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2967 int unsignedp)
2969 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2971 struct expand_operand ops[2];
2972 enum insn_code icode = optab_handler (unoptab, mode);
2973 rtx last = get_last_insn ();
2974 rtx pat;
2976 create_output_operand (&ops[0], target, mode);
2977 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2978 pat = maybe_gen_insn (icode, 2, ops);
2979 if (pat)
2981 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2982 && ! add_equal_note (pat, ops[0].value, optab_to_code (unoptab),
2983 ops[1].value, NULL_RTX))
2985 delete_insns_since (last);
2986 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
2989 emit_insn (pat);
2991 return ops[0].value;
2994 return 0;
2997 /* Generate code to perform an operation specified by UNOPTAB
2998 on operand OP0, with result having machine-mode MODE.
3000 UNSIGNEDP is for the case where we have to widen the operands
3001 to perform the operation. It says to use zero-extension.
3003 If TARGET is nonzero, the value
3004 is generated there, if it is convenient to do so.
3005 In all cases an rtx is returned for the locus of the value;
3006 this may or may not be TARGET. */
3009 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
3010 int unsignedp)
3012 enum mode_class mclass = GET_MODE_CLASS (mode);
3013 enum machine_mode wider_mode;
3014 rtx temp;
3015 rtx libfunc;
3017 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3018 if (temp)
3019 return temp;
3021 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3023 /* Widening (or narrowing) clz needs special treatment. */
3024 if (unoptab == clz_optab)
3026 temp = widen_leading (mode, op0, target, unoptab);
3027 if (temp)
3028 return temp;
3030 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3031 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3033 temp = expand_doubleword_clz (mode, op0, target);
3034 if (temp)
3035 return temp;
3038 goto try_libcall;
3041 if (unoptab == clrsb_optab)
3043 temp = widen_leading (mode, op0, target, unoptab);
3044 if (temp)
3045 return temp;
3046 goto try_libcall;
3049 /* Widening (or narrowing) bswap needs special treatment. */
3050 if (unoptab == bswap_optab)
3052 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3053 or ROTATERT. First try these directly; if this fails, then try the
3054 obvious pair of shifts with allowed widening, as this will probably
3055 be always more efficient than the other fallback methods. */
3056 if (mode == HImode)
3058 rtx last, temp1, temp2;
3060 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3062 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
3063 unsignedp, OPTAB_DIRECT);
3064 if (temp)
3065 return temp;
3068 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3070 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
3071 unsignedp, OPTAB_DIRECT);
3072 if (temp)
3073 return temp;
3076 last = get_last_insn ();
3078 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
3079 unsignedp, OPTAB_WIDEN);
3080 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
3081 unsignedp, OPTAB_WIDEN);
3082 if (temp1 && temp2)
3084 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3085 unsignedp, OPTAB_WIDEN);
3086 if (temp)
3087 return temp;
3090 delete_insns_since (last);
3093 temp = widen_bswap (mode, op0, target);
3094 if (temp)
3095 return temp;
3097 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3098 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3100 temp = expand_doubleword_bswap (mode, op0, target);
3101 if (temp)
3102 return temp;
3105 goto try_libcall;
3108 if (CLASS_HAS_WIDER_MODES_P (mclass))
3109 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3110 wider_mode != VOIDmode;
3111 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3113 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3115 rtx xop0 = op0;
3116 rtx last = get_last_insn ();
3118 /* For certain operations, we need not actually extend
3119 the narrow operand, as long as we will truncate the
3120 results to the same narrowness. */
3122 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3123 (unoptab == neg_optab
3124 || unoptab == one_cmpl_optab)
3125 && mclass == MODE_INT);
3127 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3128 unsignedp);
3130 if (temp)
3132 if (mclass != MODE_INT
3133 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3135 if (target == 0)
3136 target = gen_reg_rtx (mode);
3137 convert_move (target, temp, 0);
3138 return target;
3140 else
3141 return gen_lowpart (mode, temp);
3143 else
3144 delete_insns_since (last);
3148 /* These can be done a word at a time. */
3149 if (unoptab == one_cmpl_optab
3150 && mclass == MODE_INT
3151 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3152 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3154 int i;
3155 rtx insns;
3157 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3158 target = gen_reg_rtx (mode);
3160 start_sequence ();
3162 /* Do the actual arithmetic. */
3163 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3165 rtx target_piece = operand_subword (target, i, 1, mode);
3166 rtx x = expand_unop (word_mode, unoptab,
3167 operand_subword_force (op0, i, mode),
3168 target_piece, unsignedp);
3170 if (target_piece != x)
3171 emit_move_insn (target_piece, x);
3174 insns = get_insns ();
3175 end_sequence ();
3177 emit_insn (insns);
3178 return target;
3181 if (optab_to_code (unoptab) == NEG)
3183 /* Try negating floating point values by flipping the sign bit. */
3184 if (SCALAR_FLOAT_MODE_P (mode))
3186 temp = expand_absneg_bit (NEG, mode, op0, target);
3187 if (temp)
3188 return temp;
3191 /* If there is no negation pattern, and we have no negative zero,
3192 try subtracting from zero. */
3193 if (!HONOR_SIGNED_ZEROS (mode))
3195 temp = expand_binop (mode, (unoptab == negv_optab
3196 ? subv_optab : sub_optab),
3197 CONST0_RTX (mode), op0, target,
3198 unsignedp, OPTAB_DIRECT);
3199 if (temp)
3200 return temp;
3204 /* Try calculating parity (x) as popcount (x) % 2. */
3205 if (unoptab == parity_optab)
3207 temp = expand_parity (mode, op0, target);
3208 if (temp)
3209 return temp;
3212 /* Try implementing ffs (x) in terms of clz (x). */
3213 if (unoptab == ffs_optab)
3215 temp = expand_ffs (mode, op0, target);
3216 if (temp)
3217 return temp;
3220 /* Try implementing ctz (x) in terms of clz (x). */
3221 if (unoptab == ctz_optab)
3223 temp = expand_ctz (mode, op0, target);
3224 if (temp)
3225 return temp;
3228 try_libcall:
3229 /* Now try a library call in this mode. */
3230 libfunc = optab_libfunc (unoptab, mode);
3231 if (libfunc)
3233 rtx insns;
3234 rtx value;
3235 rtx eq_value;
3236 enum machine_mode outmode = mode;
3238 /* All of these functions return small values. Thus we choose to
3239 have them return something that isn't a double-word. */
3240 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3241 || unoptab == clrsb_optab || unoptab == popcount_optab
3242 || unoptab == parity_optab)
3243 outmode
3244 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3245 optab_libfunc (unoptab, mode)));
3247 start_sequence ();
3249 /* Pass 1 for NO_QUEUE so we don't lose any increments
3250 if the libcall is cse'd or moved. */
3251 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3252 1, op0, mode);
3253 insns = get_insns ();
3254 end_sequence ();
3256 target = gen_reg_rtx (outmode);
3257 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3258 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3259 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3260 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3261 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3262 emit_libcall_block_1 (insns, target, value, eq_value,
3263 trapv_unoptab_p (unoptab));
3265 return target;
3268 /* It can't be done in this mode. Can we do it in a wider mode? */
3270 if (CLASS_HAS_WIDER_MODES_P (mclass))
3272 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3273 wider_mode != VOIDmode;
3274 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3276 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3277 || optab_libfunc (unoptab, wider_mode))
3279 rtx xop0 = op0;
3280 rtx last = get_last_insn ();
3282 /* For certain operations, we need not actually extend
3283 the narrow operand, as long as we will truncate the
3284 results to the same narrowness. */
3285 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3286 (unoptab == neg_optab
3287 || unoptab == one_cmpl_optab
3288 || unoptab == bswap_optab)
3289 && mclass == MODE_INT);
3291 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3292 unsignedp);
3294 /* If we are generating clz using wider mode, adjust the
3295 result. Similarly for clrsb. */
3296 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3297 && temp != 0)
3298 temp = expand_binop (wider_mode, sub_optab, temp,
3299 GEN_INT (GET_MODE_PRECISION (wider_mode)
3300 - GET_MODE_PRECISION (mode)),
3301 target, true, OPTAB_DIRECT);
3303 /* Likewise for bswap. */
3304 if (unoptab == bswap_optab && temp != 0)
3306 gcc_assert (GET_MODE_PRECISION (wider_mode)
3307 == GET_MODE_BITSIZE (wider_mode)
3308 && GET_MODE_PRECISION (mode)
3309 == GET_MODE_BITSIZE (mode));
3311 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3312 GET_MODE_BITSIZE (wider_mode)
3313 - GET_MODE_BITSIZE (mode),
3314 NULL_RTX, true);
3317 if (temp)
3319 if (mclass != MODE_INT)
3321 if (target == 0)
3322 target = gen_reg_rtx (mode);
3323 convert_move (target, temp, 0);
3324 return target;
3326 else
3327 return gen_lowpart (mode, temp);
3329 else
3330 delete_insns_since (last);
3335 /* One final attempt at implementing negation via subtraction,
3336 this time allowing widening of the operand. */
3337 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3339 rtx temp;
3340 temp = expand_binop (mode,
3341 unoptab == negv_optab ? subv_optab : sub_optab,
3342 CONST0_RTX (mode), op0,
3343 target, unsignedp, OPTAB_LIB_WIDEN);
3344 if (temp)
3345 return temp;
3348 return 0;
3351 /* Emit code to compute the absolute value of OP0, with result to
3352 TARGET if convenient. (TARGET may be 0.) The return value says
3353 where the result actually is to be found.
3355 MODE is the mode of the operand; the mode of the result is
3356 different but can be deduced from MODE.
3361 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
3362 int result_unsignedp)
3364 rtx temp;
3366 if (! flag_trapv)
3367 result_unsignedp = 1;
3369 /* First try to do it with a special abs instruction. */
3370 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3371 op0, target, 0);
3372 if (temp != 0)
3373 return temp;
3375 /* For floating point modes, try clearing the sign bit. */
3376 if (SCALAR_FLOAT_MODE_P (mode))
3378 temp = expand_absneg_bit (ABS, mode, op0, target);
3379 if (temp)
3380 return temp;
3383 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3384 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3385 && !HONOR_SIGNED_ZEROS (mode))
3387 rtx last = get_last_insn ();
3389 temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
3390 if (temp != 0)
3391 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3392 OPTAB_WIDEN);
3394 if (temp != 0)
3395 return temp;
3397 delete_insns_since (last);
3400 /* If this machine has expensive jumps, we can do integer absolute
3401 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3402 where W is the width of MODE. */
3404 if (GET_MODE_CLASS (mode) == MODE_INT
3405 && BRANCH_COST (optimize_insn_for_speed_p (),
3406 false) >= 2)
3408 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3409 GET_MODE_PRECISION (mode) - 1,
3410 NULL_RTX, 0);
3412 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3413 OPTAB_LIB_WIDEN);
3414 if (temp != 0)
3415 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3416 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3418 if (temp != 0)
3419 return temp;
3422 return NULL_RTX;
3426 expand_abs (enum machine_mode mode, rtx op0, rtx target,
3427 int result_unsignedp, int safe)
3429 rtx temp, op1;
3431 if (! flag_trapv)
3432 result_unsignedp = 1;
3434 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3435 if (temp != 0)
3436 return temp;
3438 /* If that does not win, use conditional jump and negate. */
3440 /* It is safe to use the target if it is the same
3441 as the source if this is also a pseudo register */
3442 if (op0 == target && REG_P (op0)
3443 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3444 safe = 1;
3446 op1 = gen_label_rtx ();
3447 if (target == 0 || ! safe
3448 || GET_MODE (target) != mode
3449 || (MEM_P (target) && MEM_VOLATILE_P (target))
3450 || (REG_P (target)
3451 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3452 target = gen_reg_rtx (mode);
3454 emit_move_insn (target, op0);
3455 NO_DEFER_POP;
3457 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3458 NULL_RTX, NULL_RTX, op1, -1);
3460 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3461 target, target, 0);
3462 if (op0 != target)
3463 emit_move_insn (target, op0);
3464 emit_label (op1);
3465 OK_DEFER_POP;
3466 return target;
3469 /* Emit code to compute the one's complement absolute value of OP0
3470 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3471 (TARGET may be NULL_RTX.) The return value says where the result
3472 actually is to be found.
3474 MODE is the mode of the operand; the mode of the result is
3475 different but can be deduced from MODE. */
3478 expand_one_cmpl_abs_nojump (enum machine_mode mode, rtx op0, rtx target)
3480 rtx temp;
3482 /* Not applicable for floating point modes. */
3483 if (FLOAT_MODE_P (mode))
3484 return NULL_RTX;
3486 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3487 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3489 rtx last = get_last_insn ();
3491 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3492 if (temp != 0)
3493 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3494 OPTAB_WIDEN);
3496 if (temp != 0)
3497 return temp;
3499 delete_insns_since (last);
3502 /* If this machine has expensive jumps, we can do one's complement
3503 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3505 if (GET_MODE_CLASS (mode) == MODE_INT
3506 && BRANCH_COST (optimize_insn_for_speed_p (),
3507 false) >= 2)
3509 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3510 GET_MODE_PRECISION (mode) - 1,
3511 NULL_RTX, 0);
3513 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3514 OPTAB_LIB_WIDEN);
3516 if (temp != 0)
3517 return temp;
3520 return NULL_RTX;
3523 /* A subroutine of expand_copysign, perform the copysign operation using the
3524 abs and neg primitives advertised to exist on the target. The assumption
3525 is that we have a split register file, and leaving op0 in fp registers,
3526 and not playing with subregs so much, will help the register allocator. */
3528 static rtx
3529 expand_copysign_absneg (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3530 int bitpos, bool op0_is_abs)
3532 enum machine_mode imode;
3533 enum insn_code icode;
3534 rtx sign, label;
3536 if (target == op1)
3537 target = NULL_RTX;
3539 /* Check if the back end provides an insn that handles signbit for the
3540 argument's mode. */
3541 icode = optab_handler (signbit_optab, mode);
3542 if (icode != CODE_FOR_nothing)
3544 imode = insn_data[(int) icode].operand[0].mode;
3545 sign = gen_reg_rtx (imode);
3546 emit_unop_insn (icode, sign, op1, UNKNOWN);
3548 else
3550 double_int mask;
3552 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3554 imode = int_mode_for_mode (mode);
3555 if (imode == BLKmode)
3556 return NULL_RTX;
3557 op1 = gen_lowpart (imode, op1);
3559 else
3561 int word;
3563 imode = word_mode;
3564 if (FLOAT_WORDS_BIG_ENDIAN)
3565 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3566 else
3567 word = bitpos / BITS_PER_WORD;
3568 bitpos = bitpos % BITS_PER_WORD;
3569 op1 = operand_subword_force (op1, word, mode);
3572 mask = double_int_zero.set_bit (bitpos);
3574 sign = expand_binop (imode, and_optab, op1,
3575 immed_double_int_const (mask, imode),
3576 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3579 if (!op0_is_abs)
3581 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3582 if (op0 == NULL)
3583 return NULL_RTX;
3584 target = op0;
3586 else
3588 if (target == NULL_RTX)
3589 target = copy_to_reg (op0);
3590 else
3591 emit_move_insn (target, op0);
3594 label = gen_label_rtx ();
3595 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3597 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3598 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3599 else
3600 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3601 if (op0 != target)
3602 emit_move_insn (target, op0);
3604 emit_label (label);
3606 return target;
3610 /* A subroutine of expand_copysign, perform the entire copysign operation
3611 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3612 is true if op0 is known to have its sign bit clear. */
3614 static rtx
3615 expand_copysign_bit (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3616 int bitpos, bool op0_is_abs)
3618 enum machine_mode imode;
3619 double_int mask;
3620 int word, nwords, i;
3621 rtx temp, insns;
3623 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3625 imode = int_mode_for_mode (mode);
3626 if (imode == BLKmode)
3627 return NULL_RTX;
3628 word = 0;
3629 nwords = 1;
3631 else
3633 imode = word_mode;
3635 if (FLOAT_WORDS_BIG_ENDIAN)
3636 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3637 else
3638 word = bitpos / BITS_PER_WORD;
3639 bitpos = bitpos % BITS_PER_WORD;
3640 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3643 mask = double_int_zero.set_bit (bitpos);
3645 if (target == 0
3646 || target == op0
3647 || target == op1
3648 || (nwords > 1 && !valid_multiword_target_p (target)))
3649 target = gen_reg_rtx (mode);
3651 if (nwords > 1)
3653 start_sequence ();
3655 for (i = 0; i < nwords; ++i)
3657 rtx targ_piece = operand_subword (target, i, 1, mode);
3658 rtx op0_piece = operand_subword_force (op0, i, mode);
3660 if (i == word)
3662 if (!op0_is_abs)
3663 op0_piece
3664 = expand_binop (imode, and_optab, op0_piece,
3665 immed_double_int_const (~mask, imode),
3666 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3668 op1 = expand_binop (imode, and_optab,
3669 operand_subword_force (op1, i, mode),
3670 immed_double_int_const (mask, imode),
3671 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3673 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3674 targ_piece, 1, OPTAB_LIB_WIDEN);
3675 if (temp != targ_piece)
3676 emit_move_insn (targ_piece, temp);
3678 else
3679 emit_move_insn (targ_piece, op0_piece);
3682 insns = get_insns ();
3683 end_sequence ();
3685 emit_insn (insns);
3687 else
3689 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3690 immed_double_int_const (mask, imode),
3691 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3693 op0 = gen_lowpart (imode, op0);
3694 if (!op0_is_abs)
3695 op0 = expand_binop (imode, and_optab, op0,
3696 immed_double_int_const (~mask, imode),
3697 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3699 temp = expand_binop (imode, ior_optab, op0, op1,
3700 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3701 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3704 return target;
3707 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3708 scalar floating point mode. Return NULL if we do not know how to
3709 expand the operation inline. */
3712 expand_copysign (rtx op0, rtx op1, rtx target)
3714 enum machine_mode mode = GET_MODE (op0);
3715 const struct real_format *fmt;
3716 bool op0_is_abs;
3717 rtx temp;
3719 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3720 gcc_assert (GET_MODE (op1) == mode);
3722 /* First try to do it with a special instruction. */
3723 temp = expand_binop (mode, copysign_optab, op0, op1,
3724 target, 0, OPTAB_DIRECT);
3725 if (temp)
3726 return temp;
3728 fmt = REAL_MODE_FORMAT (mode);
3729 if (fmt == NULL || !fmt->has_signed_zero)
3730 return NULL_RTX;
3732 op0_is_abs = false;
3733 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3735 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3736 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3737 op0_is_abs = true;
3740 if (fmt->signbit_ro >= 0
3741 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3742 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3743 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3745 temp = expand_copysign_absneg (mode, op0, op1, target,
3746 fmt->signbit_ro, op0_is_abs);
3747 if (temp)
3748 return temp;
3751 if (fmt->signbit_rw < 0)
3752 return NULL_RTX;
3753 return expand_copysign_bit (mode, op0, op1, target,
3754 fmt->signbit_rw, op0_is_abs);
3757 /* Generate an instruction whose insn-code is INSN_CODE,
3758 with two operands: an output TARGET and an input OP0.
3759 TARGET *must* be nonzero, and the output is always stored there.
3760 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3761 the value that is stored into TARGET.
3763 Return false if expansion failed. */
3765 bool
3766 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3767 enum rtx_code code)
3769 struct expand_operand ops[2];
3770 rtx pat;
3772 create_output_operand (&ops[0], target, GET_MODE (target));
3773 create_input_operand (&ops[1], op0, GET_MODE (op0));
3774 pat = maybe_gen_insn (icode, 2, ops);
3775 if (!pat)
3776 return false;
3778 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
3779 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3781 emit_insn (pat);
3783 if (ops[0].value != target)
3784 emit_move_insn (target, ops[0].value);
3785 return true;
3787 /* Generate an instruction whose insn-code is INSN_CODE,
3788 with two operands: an output TARGET and an input OP0.
3789 TARGET *must* be nonzero, and the output is always stored there.
3790 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3791 the value that is stored into TARGET. */
3793 void
3794 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3796 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3797 gcc_assert (ok);
3800 struct no_conflict_data
3802 rtx target, first, insn;
3803 bool must_stay;
3806 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3807 the currently examined clobber / store has to stay in the list of
3808 insns that constitute the actual libcall block. */
3809 static void
3810 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3812 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3814 /* If this inns directly contributes to setting the target, it must stay. */
3815 if (reg_overlap_mentioned_p (p->target, dest))
3816 p->must_stay = true;
3817 /* If we haven't committed to keeping any other insns in the list yet,
3818 there is nothing more to check. */
3819 else if (p->insn == p->first)
3820 return;
3821 /* If this insn sets / clobbers a register that feeds one of the insns
3822 already in the list, this insn has to stay too. */
3823 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3824 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3825 || reg_used_between_p (dest, p->first, p->insn)
3826 /* Likewise if this insn depends on a register set by a previous
3827 insn in the list, or if it sets a result (presumably a hard
3828 register) that is set or clobbered by a previous insn.
3829 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3830 SET_DEST perform the former check on the address, and the latter
3831 check on the MEM. */
3832 || (GET_CODE (set) == SET
3833 && (modified_in_p (SET_SRC (set), p->first)
3834 || modified_in_p (SET_DEST (set), p->first)
3835 || modified_between_p (SET_SRC (set), p->first, p->insn)
3836 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3837 p->must_stay = true;
3841 /* Emit code to make a call to a constant function or a library call.
3843 INSNS is a list containing all insns emitted in the call.
3844 These insns leave the result in RESULT. Our block is to copy RESULT
3845 to TARGET, which is logically equivalent to EQUIV.
3847 We first emit any insns that set a pseudo on the assumption that these are
3848 loading constants into registers; doing so allows them to be safely cse'ed
3849 between blocks. Then we emit all the other insns in the block, followed by
3850 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3851 note with an operand of EQUIV. */
3853 static void
3854 emit_libcall_block_1 (rtx insns, rtx target, rtx result, rtx equiv,
3855 bool equiv_may_trap)
3857 rtx final_dest = target;
3858 rtx next, last, insn;
3860 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3861 into a MEM later. Protect the libcall block from this change. */
3862 if (! REG_P (target) || REG_USERVAR_P (target))
3863 target = gen_reg_rtx (GET_MODE (target));
3865 /* If we're using non-call exceptions, a libcall corresponding to an
3866 operation that may trap may also trap. */
3867 /* ??? See the comment in front of make_reg_eh_region_note. */
3868 if (cfun->can_throw_non_call_exceptions
3869 && (equiv_may_trap || may_trap_p (equiv)))
3871 for (insn = insns; insn; insn = NEXT_INSN (insn))
3872 if (CALL_P (insn))
3874 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3875 if (note)
3877 int lp_nr = INTVAL (XEXP (note, 0));
3878 if (lp_nr == 0 || lp_nr == INT_MIN)
3879 remove_note (insn, note);
3883 else
3885 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3886 reg note to indicate that this call cannot throw or execute a nonlocal
3887 goto (unless there is already a REG_EH_REGION note, in which case
3888 we update it). */
3889 for (insn = insns; insn; insn = NEXT_INSN (insn))
3890 if (CALL_P (insn))
3891 make_reg_eh_region_note_nothrow_nononlocal (insn);
3894 /* First emit all insns that set pseudos. Remove them from the list as
3895 we go. Avoid insns that set pseudos which were referenced in previous
3896 insns. These can be generated by move_by_pieces, for example,
3897 to update an address. Similarly, avoid insns that reference things
3898 set in previous insns. */
3900 for (insn = insns; insn; insn = next)
3902 rtx set = single_set (insn);
3904 next = NEXT_INSN (insn);
3906 if (set != 0 && REG_P (SET_DEST (set))
3907 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3909 struct no_conflict_data data;
3911 data.target = const0_rtx;
3912 data.first = insns;
3913 data.insn = insn;
3914 data.must_stay = 0;
3915 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3916 if (! data.must_stay)
3918 if (PREV_INSN (insn))
3919 NEXT_INSN (PREV_INSN (insn)) = next;
3920 else
3921 insns = next;
3923 if (next)
3924 PREV_INSN (next) = PREV_INSN (insn);
3926 add_insn (insn);
3930 /* Some ports use a loop to copy large arguments onto the stack.
3931 Don't move anything outside such a loop. */
3932 if (LABEL_P (insn))
3933 break;
3936 /* Write the remaining insns followed by the final copy. */
3937 for (insn = insns; insn; insn = next)
3939 next = NEXT_INSN (insn);
3941 add_insn (insn);
3944 last = emit_move_insn (target, result);
3945 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3947 if (final_dest != target)
3948 emit_move_insn (final_dest, target);
3951 void
3952 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3954 emit_libcall_block_1 (insns, target, result, equiv, false);
3957 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3958 PURPOSE describes how this comparison will be used. CODE is the rtx
3959 comparison code we will be using.
3961 ??? Actually, CODE is slightly weaker than that. A target is still
3962 required to implement all of the normal bcc operations, but not
3963 required to implement all (or any) of the unordered bcc operations. */
3966 can_compare_p (enum rtx_code code, enum machine_mode mode,
3967 enum can_compare_purpose purpose)
3969 rtx test;
3970 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3973 enum insn_code icode;
3975 if (purpose == ccp_jump
3976 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
3977 && insn_operand_matches (icode, 0, test))
3978 return 1;
3979 if (purpose == ccp_store_flag
3980 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
3981 && insn_operand_matches (icode, 1, test))
3982 return 1;
3983 if (purpose == ccp_cmov
3984 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
3985 return 1;
3987 mode = GET_MODE_WIDER_MODE (mode);
3988 PUT_MODE (test, mode);
3990 while (mode != VOIDmode);
3992 return 0;
3995 /* This function is called when we are going to emit a compare instruction that
3996 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
3998 *PMODE is the mode of the inputs (in case they are const_int).
3999 *PUNSIGNEDP nonzero says that the operands are unsigned;
4000 this matters if they need to be widened (as given by METHODS).
4002 If they have mode BLKmode, then SIZE specifies the size of both operands.
4004 This function performs all the setup necessary so that the caller only has
4005 to emit a single comparison insn. This setup can involve doing a BLKmode
4006 comparison or emitting a library call to perform the comparison if no insn
4007 is available to handle it.
4008 The values which are passed in through pointers can be modified; the caller
4009 should perform the comparison on the modified values. Constant
4010 comparisons must have already been folded. */
4012 static void
4013 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4014 int unsignedp, enum optab_methods methods,
4015 rtx *ptest, enum machine_mode *pmode)
4017 enum machine_mode mode = *pmode;
4018 rtx libfunc, test;
4019 enum machine_mode cmp_mode;
4020 enum mode_class mclass;
4022 /* The other methods are not needed. */
4023 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4024 || methods == OPTAB_LIB_WIDEN);
4026 /* If we are optimizing, force expensive constants into a register. */
4027 if (CONSTANT_P (x) && optimize
4028 && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
4029 > COSTS_N_INSNS (1)))
4030 x = force_reg (mode, x);
4032 if (CONSTANT_P (y) && optimize
4033 && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
4034 > COSTS_N_INSNS (1)))
4035 y = force_reg (mode, y);
4037 #ifdef HAVE_cc0
4038 /* Make sure if we have a canonical comparison. The RTL
4039 documentation states that canonical comparisons are required only
4040 for targets which have cc0. */
4041 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4042 #endif
4044 /* Don't let both operands fail to indicate the mode. */
4045 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4046 x = force_reg (mode, x);
4047 if (mode == VOIDmode)
4048 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4050 /* Handle all BLKmode compares. */
4052 if (mode == BLKmode)
4054 enum machine_mode result_mode;
4055 enum insn_code cmp_code;
4056 tree length_type;
4057 rtx libfunc;
4058 rtx result;
4059 rtx opalign
4060 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4062 gcc_assert (size);
4064 /* Try to use a memory block compare insn - either cmpstr
4065 or cmpmem will do. */
4066 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4067 cmp_mode != VOIDmode;
4068 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4070 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4071 if (cmp_code == CODE_FOR_nothing)
4072 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4073 if (cmp_code == CODE_FOR_nothing)
4074 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4075 if (cmp_code == CODE_FOR_nothing)
4076 continue;
4078 /* Must make sure the size fits the insn's mode. */
4079 if ((CONST_INT_P (size)
4080 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4081 || (GET_MODE_BITSIZE (GET_MODE (size))
4082 > GET_MODE_BITSIZE (cmp_mode)))
4083 continue;
4085 result_mode = insn_data[cmp_code].operand[0].mode;
4086 result = gen_reg_rtx (result_mode);
4087 size = convert_to_mode (cmp_mode, size, 1);
4088 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4090 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4091 *pmode = result_mode;
4092 return;
4095 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4096 goto fail;
4098 /* Otherwise call a library function, memcmp. */
4099 libfunc = memcmp_libfunc;
4100 length_type = sizetype;
4101 result_mode = TYPE_MODE (integer_type_node);
4102 cmp_mode = TYPE_MODE (length_type);
4103 size = convert_to_mode (TYPE_MODE (length_type), size,
4104 TYPE_UNSIGNED (length_type));
4106 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4107 result_mode, 3,
4108 XEXP (x, 0), Pmode,
4109 XEXP (y, 0), Pmode,
4110 size, cmp_mode);
4111 x = result;
4112 y = const0_rtx;
4113 mode = result_mode;
4114 methods = OPTAB_LIB_WIDEN;
4115 unsignedp = false;
4118 /* Don't allow operands to the compare to trap, as that can put the
4119 compare and branch in different basic blocks. */
4120 if (cfun->can_throw_non_call_exceptions)
4122 if (may_trap_p (x))
4123 x = force_reg (mode, x);
4124 if (may_trap_p (y))
4125 y = force_reg (mode, y);
4128 if (GET_MODE_CLASS (mode) == MODE_CC)
4130 gcc_assert (can_compare_p (comparison, CCmode, ccp_jump));
4131 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4132 return;
4135 mclass = GET_MODE_CLASS (mode);
4136 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4137 cmp_mode = mode;
4140 enum insn_code icode;
4141 icode = optab_handler (cbranch_optab, cmp_mode);
4142 if (icode != CODE_FOR_nothing
4143 && insn_operand_matches (icode, 0, test))
4145 rtx last = get_last_insn ();
4146 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4147 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4148 if (op0 && op1
4149 && insn_operand_matches (icode, 1, op0)
4150 && insn_operand_matches (icode, 2, op1))
4152 XEXP (test, 0) = op0;
4153 XEXP (test, 1) = op1;
4154 *ptest = test;
4155 *pmode = cmp_mode;
4156 return;
4158 delete_insns_since (last);
4161 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4162 break;
4163 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4165 while (cmp_mode != VOIDmode);
4167 if (methods != OPTAB_LIB_WIDEN)
4168 goto fail;
4170 if (!SCALAR_FLOAT_MODE_P (mode))
4172 rtx result;
4173 enum machine_mode ret_mode;
4175 /* Handle a libcall just for the mode we are using. */
4176 libfunc = optab_libfunc (cmp_optab, mode);
4177 gcc_assert (libfunc);
4179 /* If we want unsigned, and this mode has a distinct unsigned
4180 comparison routine, use that. */
4181 if (unsignedp)
4183 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4184 if (ulibfunc)
4185 libfunc = ulibfunc;
4188 ret_mode = targetm.libgcc_cmp_return_mode ();
4189 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4190 ret_mode, 2, x, mode, y, mode);
4192 /* There are two kinds of comparison routines. Biased routines
4193 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4194 of gcc expect that the comparison operation is equivalent
4195 to the modified comparison. For signed comparisons compare the
4196 result against 1 in the biased case, and zero in the unbiased
4197 case. For unsigned comparisons always compare against 1 after
4198 biasing the unbiased result by adding 1. This gives us a way to
4199 represent LTU.
4200 The comparisons in the fixed-point helper library are always
4201 biased. */
4202 x = result;
4203 y = const1_rtx;
4205 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4207 if (unsignedp)
4208 x = plus_constant (ret_mode, result, 1);
4209 else
4210 y = const0_rtx;
4213 *pmode = word_mode;
4214 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4215 ptest, pmode);
4217 else
4218 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4220 return;
4222 fail:
4223 *ptest = NULL_RTX;
4226 /* Before emitting an insn with code ICODE, make sure that X, which is going
4227 to be used for operand OPNUM of the insn, is converted from mode MODE to
4228 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4229 that it is accepted by the operand predicate. Return the new value. */
4232 prepare_operand (enum insn_code icode, rtx x, int opnum, enum machine_mode mode,
4233 enum machine_mode wider_mode, int unsignedp)
4235 if (mode != wider_mode)
4236 x = convert_modes (wider_mode, mode, x, unsignedp);
4238 if (!insn_operand_matches (icode, opnum, x))
4240 if (reload_completed)
4241 return NULL_RTX;
4242 x = copy_to_mode_reg (insn_data[(int) icode].operand[opnum].mode, x);
4245 return x;
4248 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4249 we can do the branch. */
4251 static void
4252 emit_cmp_and_jump_insn_1 (rtx test, enum machine_mode mode, rtx label, int prob)
4254 enum machine_mode optab_mode;
4255 enum mode_class mclass;
4256 enum insn_code icode;
4257 rtx insn;
4259 mclass = GET_MODE_CLASS (mode);
4260 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4261 icode = optab_handler (cbranch_optab, optab_mode);
4263 gcc_assert (icode != CODE_FOR_nothing);
4264 gcc_assert (insn_operand_matches (icode, 0, test));
4265 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4266 XEXP (test, 1), label));
4267 if (prob != -1
4268 && profile_status != PROFILE_ABSENT
4269 && insn
4270 && JUMP_P (insn)
4271 && any_condjump_p (insn))
4273 gcc_assert (!find_reg_note (insn, REG_BR_PROB, 0));
4274 add_reg_note (insn, REG_BR_PROB, GEN_INT (prob));
4278 /* Generate code to compare X with Y so that the condition codes are
4279 set and to jump to LABEL if the condition is true. If X is a
4280 constant and Y is not a constant, then the comparison is swapped to
4281 ensure that the comparison RTL has the canonical form.
4283 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4284 need to be widened. UNSIGNEDP is also used to select the proper
4285 branch condition code.
4287 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4289 MODE is the mode of the inputs (in case they are const_int).
4291 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4292 It will be potentially converted into an unsigned variant based on
4293 UNSIGNEDP to select a proper jump instruction.
4295 PROB is the probability of jumping to LABEL. */
4297 void
4298 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4299 enum machine_mode mode, int unsignedp, rtx label,
4300 int prob)
4302 rtx op0 = x, op1 = y;
4303 rtx test;
4305 /* Swap operands and condition to ensure canonical RTL. */
4306 if (swap_commutative_operands_p (x, y)
4307 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4309 op0 = y, op1 = x;
4310 comparison = swap_condition (comparison);
4313 /* If OP0 is still a constant, then both X and Y must be constants
4314 or the opposite comparison is not supported. Force X into a register
4315 to create canonical RTL. */
4316 if (CONSTANT_P (op0))
4317 op0 = force_reg (mode, op0);
4319 if (unsignedp)
4320 comparison = unsigned_condition (comparison);
4322 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4323 &test, &mode);
4324 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4328 /* Emit a library call comparison between floating point X and Y.
4329 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4331 static void
4332 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4333 rtx *ptest, enum machine_mode *pmode)
4335 enum rtx_code swapped = swap_condition (comparison);
4336 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4337 enum machine_mode orig_mode = GET_MODE (x);
4338 enum machine_mode mode, cmp_mode;
4339 rtx true_rtx, false_rtx;
4340 rtx value, target, insns, equiv;
4341 rtx libfunc = 0;
4342 bool reversed_p = false;
4343 cmp_mode = targetm.libgcc_cmp_return_mode ();
4345 for (mode = orig_mode;
4346 mode != VOIDmode;
4347 mode = GET_MODE_WIDER_MODE (mode))
4349 if (code_to_optab (comparison)
4350 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4351 break;
4353 if (code_to_optab (swapped)
4354 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4356 rtx tmp;
4357 tmp = x; x = y; y = tmp;
4358 comparison = swapped;
4359 break;
4362 if (code_to_optab (reversed)
4363 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4365 comparison = reversed;
4366 reversed_p = true;
4367 break;
4371 gcc_assert (mode != VOIDmode);
4373 if (mode != orig_mode)
4375 x = convert_to_mode (mode, x, 0);
4376 y = convert_to_mode (mode, y, 0);
4379 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4380 the RTL. The allows the RTL optimizers to delete the libcall if the
4381 condition can be determined at compile-time. */
4382 if (comparison == UNORDERED
4383 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4385 true_rtx = const_true_rtx;
4386 false_rtx = const0_rtx;
4388 else
4390 switch (comparison)
4392 case EQ:
4393 true_rtx = const0_rtx;
4394 false_rtx = const_true_rtx;
4395 break;
4397 case NE:
4398 true_rtx = const_true_rtx;
4399 false_rtx = const0_rtx;
4400 break;
4402 case GT:
4403 true_rtx = const1_rtx;
4404 false_rtx = const0_rtx;
4405 break;
4407 case GE:
4408 true_rtx = const0_rtx;
4409 false_rtx = constm1_rtx;
4410 break;
4412 case LT:
4413 true_rtx = constm1_rtx;
4414 false_rtx = const0_rtx;
4415 break;
4417 case LE:
4418 true_rtx = const0_rtx;
4419 false_rtx = const1_rtx;
4420 break;
4422 default:
4423 gcc_unreachable ();
4427 if (comparison == UNORDERED)
4429 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4430 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4431 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4432 temp, const_true_rtx, equiv);
4434 else
4436 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4437 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4438 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4439 equiv, true_rtx, false_rtx);
4442 start_sequence ();
4443 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4444 cmp_mode, 2, x, mode, y, mode);
4445 insns = get_insns ();
4446 end_sequence ();
4448 target = gen_reg_rtx (cmp_mode);
4449 emit_libcall_block (insns, target, value, equiv);
4451 if (comparison == UNORDERED
4452 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4453 || reversed_p)
4454 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4455 else
4456 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4458 *pmode = cmp_mode;
4461 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4463 void
4464 emit_indirect_jump (rtx loc)
4466 struct expand_operand ops[1];
4468 create_address_operand (&ops[0], loc);
4469 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4470 emit_barrier ();
4473 #ifdef HAVE_conditional_move
4475 /* Emit a conditional move instruction if the machine supports one for that
4476 condition and machine mode.
4478 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4479 the mode to use should they be constants. If it is VOIDmode, they cannot
4480 both be constants.
4482 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4483 should be stored there. MODE is the mode to use should they be constants.
4484 If it is VOIDmode, they cannot both be constants.
4486 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4487 is not supported. */
4490 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4491 enum machine_mode cmode, rtx op2, rtx op3,
4492 enum machine_mode mode, int unsignedp)
4494 rtx tem, comparison, last;
4495 enum insn_code icode;
4496 enum rtx_code reversed;
4498 /* If one operand is constant, make it the second one. Only do this
4499 if the other operand is not constant as well. */
4501 if (swap_commutative_operands_p (op0, op1))
4503 tem = op0;
4504 op0 = op1;
4505 op1 = tem;
4506 code = swap_condition (code);
4509 /* get_condition will prefer to generate LT and GT even if the old
4510 comparison was against zero, so undo that canonicalization here since
4511 comparisons against zero are cheaper. */
4512 if (code == LT && op1 == const1_rtx)
4513 code = LE, op1 = const0_rtx;
4514 else if (code == GT && op1 == constm1_rtx)
4515 code = GE, op1 = const0_rtx;
4517 if (cmode == VOIDmode)
4518 cmode = GET_MODE (op0);
4520 if (swap_commutative_operands_p (op2, op3)
4521 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4522 != UNKNOWN))
4524 tem = op2;
4525 op2 = op3;
4526 op3 = tem;
4527 code = reversed;
4530 if (mode == VOIDmode)
4531 mode = GET_MODE (op2);
4533 icode = direct_optab_handler (movcc_optab, mode);
4535 if (icode == CODE_FOR_nothing)
4536 return 0;
4538 if (!target)
4539 target = gen_reg_rtx (mode);
4541 code = unsignedp ? unsigned_condition (code) : code;
4542 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4544 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4545 return NULL and let the caller figure out how best to deal with this
4546 situation. */
4547 if (!COMPARISON_P (comparison))
4548 return NULL_RTX;
4550 do_pending_stack_adjust ();
4551 last = get_last_insn ();
4552 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4553 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4554 &comparison, &cmode);
4555 if (comparison)
4557 struct expand_operand ops[4];
4559 create_output_operand (&ops[0], target, mode);
4560 create_fixed_operand (&ops[1], comparison);
4561 create_input_operand (&ops[2], op2, mode);
4562 create_input_operand (&ops[3], op3, mode);
4563 if (maybe_expand_insn (icode, 4, ops))
4565 if (ops[0].value != target)
4566 convert_move (target, ops[0].value, false);
4567 return target;
4570 delete_insns_since (last);
4571 return NULL_RTX;
4574 /* Return nonzero if a conditional move of mode MODE is supported.
4576 This function is for combine so it can tell whether an insn that looks
4577 like a conditional move is actually supported by the hardware. If we
4578 guess wrong we lose a bit on optimization, but that's it. */
4579 /* ??? sparc64 supports conditionally moving integers values based on fp
4580 comparisons, and vice versa. How do we handle them? */
4583 can_conditionally_move_p (enum machine_mode mode)
4585 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4586 return 1;
4588 return 0;
4591 #endif /* HAVE_conditional_move */
4593 /* Emit a conditional addition instruction if the machine supports one for that
4594 condition and machine mode.
4596 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4597 the mode to use should they be constants. If it is VOIDmode, they cannot
4598 both be constants.
4600 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4601 should be stored there. MODE is the mode to use should they be constants.
4602 If it is VOIDmode, they cannot both be constants.
4604 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4605 is not supported. */
4608 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4609 enum machine_mode cmode, rtx op2, rtx op3,
4610 enum machine_mode mode, int unsignedp)
4612 rtx tem, comparison, last;
4613 enum insn_code icode;
4615 /* If one operand is constant, make it the second one. Only do this
4616 if the other operand is not constant as well. */
4618 if (swap_commutative_operands_p (op0, op1))
4620 tem = op0;
4621 op0 = op1;
4622 op1 = tem;
4623 code = swap_condition (code);
4626 /* get_condition will prefer to generate LT and GT even if the old
4627 comparison was against zero, so undo that canonicalization here since
4628 comparisons against zero are cheaper. */
4629 if (code == LT && op1 == const1_rtx)
4630 code = LE, op1 = const0_rtx;
4631 else if (code == GT && op1 == constm1_rtx)
4632 code = GE, op1 = const0_rtx;
4634 if (cmode == VOIDmode)
4635 cmode = GET_MODE (op0);
4637 if (mode == VOIDmode)
4638 mode = GET_MODE (op2);
4640 icode = optab_handler (addcc_optab, mode);
4642 if (icode == CODE_FOR_nothing)
4643 return 0;
4645 if (!target)
4646 target = gen_reg_rtx (mode);
4648 code = unsignedp ? unsigned_condition (code) : code;
4649 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4651 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4652 return NULL and let the caller figure out how best to deal with this
4653 situation. */
4654 if (!COMPARISON_P (comparison))
4655 return NULL_RTX;
4657 do_pending_stack_adjust ();
4658 last = get_last_insn ();
4659 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4660 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4661 &comparison, &cmode);
4662 if (comparison)
4664 struct expand_operand ops[4];
4666 create_output_operand (&ops[0], target, mode);
4667 create_fixed_operand (&ops[1], comparison);
4668 create_input_operand (&ops[2], op2, mode);
4669 create_input_operand (&ops[3], op3, mode);
4670 if (maybe_expand_insn (icode, 4, ops))
4672 if (ops[0].value != target)
4673 convert_move (target, ops[0].value, false);
4674 return target;
4677 delete_insns_since (last);
4678 return NULL_RTX;
4681 /* These functions attempt to generate an insn body, rather than
4682 emitting the insn, but if the gen function already emits them, we
4683 make no attempt to turn them back into naked patterns. */
4685 /* Generate and return an insn body to add Y to X. */
4688 gen_add2_insn (rtx x, rtx y)
4690 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4692 gcc_assert (insn_operand_matches (icode, 0, x));
4693 gcc_assert (insn_operand_matches (icode, 1, x));
4694 gcc_assert (insn_operand_matches (icode, 2, y));
4696 return GEN_FCN (icode) (x, x, y);
4699 /* Generate and return an insn body to add r1 and c,
4700 storing the result in r0. */
4703 gen_add3_insn (rtx r0, rtx r1, rtx c)
4705 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4707 if (icode == CODE_FOR_nothing
4708 || !insn_operand_matches (icode, 0, r0)
4709 || !insn_operand_matches (icode, 1, r1)
4710 || !insn_operand_matches (icode, 2, c))
4711 return NULL_RTX;
4713 return GEN_FCN (icode) (r0, r1, c);
4717 have_add2_insn (rtx x, rtx y)
4719 enum insn_code icode;
4721 gcc_assert (GET_MODE (x) != VOIDmode);
4723 icode = optab_handler (add_optab, GET_MODE (x));
4725 if (icode == CODE_FOR_nothing)
4726 return 0;
4728 if (!insn_operand_matches (icode, 0, x)
4729 || !insn_operand_matches (icode, 1, x)
4730 || !insn_operand_matches (icode, 2, y))
4731 return 0;
4733 return 1;
4736 /* Generate and return an insn body to subtract Y from X. */
4739 gen_sub2_insn (rtx x, rtx y)
4741 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4743 gcc_assert (insn_operand_matches (icode, 0, x));
4744 gcc_assert (insn_operand_matches (icode, 1, x));
4745 gcc_assert (insn_operand_matches (icode, 2, y));
4747 return GEN_FCN (icode) (x, x, y);
4750 /* Generate and return an insn body to subtract r1 and c,
4751 storing the result in r0. */
4754 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4756 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4758 if (icode == CODE_FOR_nothing
4759 || !insn_operand_matches (icode, 0, r0)
4760 || !insn_operand_matches (icode, 1, r1)
4761 || !insn_operand_matches (icode, 2, c))
4762 return NULL_RTX;
4764 return GEN_FCN (icode) (r0, r1, c);
4768 have_sub2_insn (rtx x, rtx y)
4770 enum insn_code icode;
4772 gcc_assert (GET_MODE (x) != VOIDmode);
4774 icode = optab_handler (sub_optab, GET_MODE (x));
4776 if (icode == CODE_FOR_nothing)
4777 return 0;
4779 if (!insn_operand_matches (icode, 0, x)
4780 || !insn_operand_matches (icode, 1, x)
4781 || !insn_operand_matches (icode, 2, y))
4782 return 0;
4784 return 1;
4787 /* Generate the body of an instruction to copy Y into X.
4788 It may be a list of insns, if one insn isn't enough. */
4791 gen_move_insn (rtx x, rtx y)
4793 rtx seq;
4795 start_sequence ();
4796 emit_move_insn_1 (x, y);
4797 seq = get_insns ();
4798 end_sequence ();
4799 return seq;
4802 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4803 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4804 no such operation exists, CODE_FOR_nothing will be returned. */
4806 enum insn_code
4807 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
4808 int unsignedp)
4810 convert_optab tab;
4811 #ifdef HAVE_ptr_extend
4812 if (unsignedp < 0)
4813 return CODE_FOR_ptr_extend;
4814 #endif
4816 tab = unsignedp ? zext_optab : sext_optab;
4817 return convert_optab_handler (tab, to_mode, from_mode);
4820 /* Generate the body of an insn to extend Y (with mode MFROM)
4821 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4824 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4825 enum machine_mode mfrom, int unsignedp)
4827 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4828 return GEN_FCN (icode) (x, y);
4831 /* can_fix_p and can_float_p say whether the target machine
4832 can directly convert a given fixed point type to
4833 a given floating point type, or vice versa.
4834 The returned value is the CODE_FOR_... value to use,
4835 or CODE_FOR_nothing if these modes cannot be directly converted.
4837 *TRUNCP_PTR is set to 1 if it is necessary to output
4838 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4840 static enum insn_code
4841 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4842 int unsignedp, int *truncp_ptr)
4844 convert_optab tab;
4845 enum insn_code icode;
4847 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4848 icode = convert_optab_handler (tab, fixmode, fltmode);
4849 if (icode != CODE_FOR_nothing)
4851 *truncp_ptr = 0;
4852 return icode;
4855 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4856 for this to work. We need to rework the fix* and ftrunc* patterns
4857 and documentation. */
4858 tab = unsignedp ? ufix_optab : sfix_optab;
4859 icode = convert_optab_handler (tab, fixmode, fltmode);
4860 if (icode != CODE_FOR_nothing
4861 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4863 *truncp_ptr = 1;
4864 return icode;
4867 *truncp_ptr = 0;
4868 return CODE_FOR_nothing;
4871 enum insn_code
4872 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4873 int unsignedp)
4875 convert_optab tab;
4877 tab = unsignedp ? ufloat_optab : sfloat_optab;
4878 return convert_optab_handler (tab, fltmode, fixmode);
4881 /* Function supportable_convert_operation
4883 Check whether an operation represented by the code CODE is a
4884 convert operation that is supported by the target platform in
4885 vector form (i.e., when operating on arguments of type VECTYPE_IN
4886 producing a result of type VECTYPE_OUT).
4888 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4889 This function checks if these operations are supported
4890 by the target platform either directly (via vector tree-codes), or via
4891 target builtins.
4893 Output:
4894 - CODE1 is code of vector operation to be used when
4895 vectorizing the operation, if available.
4896 - DECL is decl of target builtin functions to be used
4897 when vectorizing the operation, if available. In this case,
4898 CODE1 is CALL_EXPR. */
4900 bool
4901 supportable_convert_operation (enum tree_code code,
4902 tree vectype_out, tree vectype_in,
4903 tree *decl, enum tree_code *code1)
4905 enum machine_mode m1,m2;
4906 int truncp;
4908 m1 = TYPE_MODE (vectype_out);
4909 m2 = TYPE_MODE (vectype_in);
4911 /* First check if we can done conversion directly. */
4912 if ((code == FIX_TRUNC_EXPR
4913 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
4914 != CODE_FOR_nothing)
4915 || (code == FLOAT_EXPR
4916 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
4917 != CODE_FOR_nothing))
4919 *code1 = code;
4920 return true;
4923 /* Now check for builtin. */
4924 if (targetm.vectorize.builtin_conversion
4925 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
4927 *code1 = CALL_EXPR;
4928 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
4929 return true;
4931 return false;
4935 /* Generate code to convert FROM to floating point
4936 and store in TO. FROM must be fixed point and not VOIDmode.
4937 UNSIGNEDP nonzero means regard FROM as unsigned.
4938 Normally this is done by correcting the final value
4939 if it is negative. */
4941 void
4942 expand_float (rtx to, rtx from, int unsignedp)
4944 enum insn_code icode;
4945 rtx target = to;
4946 enum machine_mode fmode, imode;
4947 bool can_do_signed = false;
4949 /* Crash now, because we won't be able to decide which mode to use. */
4950 gcc_assert (GET_MODE (from) != VOIDmode);
4952 /* Look for an insn to do the conversion. Do it in the specified
4953 modes if possible; otherwise convert either input, output or both to
4954 wider mode. If the integer mode is wider than the mode of FROM,
4955 we can do the conversion signed even if the input is unsigned. */
4957 for (fmode = GET_MODE (to); fmode != VOIDmode;
4958 fmode = GET_MODE_WIDER_MODE (fmode))
4959 for (imode = GET_MODE (from); imode != VOIDmode;
4960 imode = GET_MODE_WIDER_MODE (imode))
4962 int doing_unsigned = unsignedp;
4964 if (fmode != GET_MODE (to)
4965 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
4966 continue;
4968 icode = can_float_p (fmode, imode, unsignedp);
4969 if (icode == CODE_FOR_nothing && unsignedp)
4971 enum insn_code scode = can_float_p (fmode, imode, 0);
4972 if (scode != CODE_FOR_nothing)
4973 can_do_signed = true;
4974 if (imode != GET_MODE (from))
4975 icode = scode, doing_unsigned = 0;
4978 if (icode != CODE_FOR_nothing)
4980 if (imode != GET_MODE (from))
4981 from = convert_to_mode (imode, from, unsignedp);
4983 if (fmode != GET_MODE (to))
4984 target = gen_reg_rtx (fmode);
4986 emit_unop_insn (icode, target, from,
4987 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4989 if (target != to)
4990 convert_move (to, target, 0);
4991 return;
4995 /* Unsigned integer, and no way to convert directly. Convert as signed,
4996 then unconditionally adjust the result. */
4997 if (unsignedp && can_do_signed)
4999 rtx label = gen_label_rtx ();
5000 rtx temp;
5001 REAL_VALUE_TYPE offset;
5003 /* Look for a usable floating mode FMODE wider than the source and at
5004 least as wide as the target. Using FMODE will avoid rounding woes
5005 with unsigned values greater than the signed maximum value. */
5007 for (fmode = GET_MODE (to); fmode != VOIDmode;
5008 fmode = GET_MODE_WIDER_MODE (fmode))
5009 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
5010 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
5011 break;
5013 if (fmode == VOIDmode)
5015 /* There is no such mode. Pretend the target is wide enough. */
5016 fmode = GET_MODE (to);
5018 /* Avoid double-rounding when TO is narrower than FROM. */
5019 if ((significand_size (fmode) + 1)
5020 < GET_MODE_PRECISION (GET_MODE (from)))
5022 rtx temp1;
5023 rtx neglabel = gen_label_rtx ();
5025 /* Don't use TARGET if it isn't a register, is a hard register,
5026 or is the wrong mode. */
5027 if (!REG_P (target)
5028 || REGNO (target) < FIRST_PSEUDO_REGISTER
5029 || GET_MODE (target) != fmode)
5030 target = gen_reg_rtx (fmode);
5032 imode = GET_MODE (from);
5033 do_pending_stack_adjust ();
5035 /* Test whether the sign bit is set. */
5036 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5037 0, neglabel);
5039 /* The sign bit is not set. Convert as signed. */
5040 expand_float (target, from, 0);
5041 emit_jump_insn (gen_jump (label));
5042 emit_barrier ();
5044 /* The sign bit is set.
5045 Convert to a usable (positive signed) value by shifting right
5046 one bit, while remembering if a nonzero bit was shifted
5047 out; i.e., compute (from & 1) | (from >> 1). */
5049 emit_label (neglabel);
5050 temp = expand_binop (imode, and_optab, from, const1_rtx,
5051 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5052 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5053 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5054 OPTAB_LIB_WIDEN);
5055 expand_float (target, temp, 0);
5057 /* Multiply by 2 to undo the shift above. */
5058 temp = expand_binop (fmode, add_optab, target, target,
5059 target, 0, OPTAB_LIB_WIDEN);
5060 if (temp != target)
5061 emit_move_insn (target, temp);
5063 do_pending_stack_adjust ();
5064 emit_label (label);
5065 goto done;
5069 /* If we are about to do some arithmetic to correct for an
5070 unsigned operand, do it in a pseudo-register. */
5072 if (GET_MODE (to) != fmode
5073 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5074 target = gen_reg_rtx (fmode);
5076 /* Convert as signed integer to floating. */
5077 expand_float (target, from, 0);
5079 /* If FROM is negative (and therefore TO is negative),
5080 correct its value by 2**bitwidth. */
5082 do_pending_stack_adjust ();
5083 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5084 0, label);
5087 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5088 temp = expand_binop (fmode, add_optab, target,
5089 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5090 target, 0, OPTAB_LIB_WIDEN);
5091 if (temp != target)
5092 emit_move_insn (target, temp);
5094 do_pending_stack_adjust ();
5095 emit_label (label);
5096 goto done;
5099 /* No hardware instruction available; call a library routine. */
5101 rtx libfunc;
5102 rtx insns;
5103 rtx value;
5104 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5106 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
5107 from = convert_to_mode (SImode, from, unsignedp);
5109 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5110 gcc_assert (libfunc);
5112 start_sequence ();
5114 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5115 GET_MODE (to), 1, from,
5116 GET_MODE (from));
5117 insns = get_insns ();
5118 end_sequence ();
5120 emit_libcall_block (insns, target, value,
5121 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5122 GET_MODE (to), from));
5125 done:
5127 /* Copy result to requested destination
5128 if we have been computing in a temp location. */
5130 if (target != to)
5132 if (GET_MODE (target) == GET_MODE (to))
5133 emit_move_insn (to, target);
5134 else
5135 convert_move (to, target, 0);
5139 /* Generate code to convert FROM to fixed point and store in TO. FROM
5140 must be floating point. */
5142 void
5143 expand_fix (rtx to, rtx from, int unsignedp)
5145 enum insn_code icode;
5146 rtx target = to;
5147 enum machine_mode fmode, imode;
5148 int must_trunc = 0;
5150 /* We first try to find a pair of modes, one real and one integer, at
5151 least as wide as FROM and TO, respectively, in which we can open-code
5152 this conversion. If the integer mode is wider than the mode of TO,
5153 we can do the conversion either signed or unsigned. */
5155 for (fmode = GET_MODE (from); fmode != VOIDmode;
5156 fmode = GET_MODE_WIDER_MODE (fmode))
5157 for (imode = GET_MODE (to); imode != VOIDmode;
5158 imode = GET_MODE_WIDER_MODE (imode))
5160 int doing_unsigned = unsignedp;
5162 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5163 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5164 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5166 if (icode != CODE_FOR_nothing)
5168 rtx last = get_last_insn ();
5169 if (fmode != GET_MODE (from))
5170 from = convert_to_mode (fmode, from, 0);
5172 if (must_trunc)
5174 rtx temp = gen_reg_rtx (GET_MODE (from));
5175 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5176 temp, 0);
5179 if (imode != GET_MODE (to))
5180 target = gen_reg_rtx (imode);
5182 if (maybe_emit_unop_insn (icode, target, from,
5183 doing_unsigned ? UNSIGNED_FIX : FIX))
5185 if (target != to)
5186 convert_move (to, target, unsignedp);
5187 return;
5189 delete_insns_since (last);
5193 /* For an unsigned conversion, there is one more way to do it.
5194 If we have a signed conversion, we generate code that compares
5195 the real value to the largest representable positive number. If if
5196 is smaller, the conversion is done normally. Otherwise, subtract
5197 one plus the highest signed number, convert, and add it back.
5199 We only need to check all real modes, since we know we didn't find
5200 anything with a wider integer mode.
5202 This code used to extend FP value into mode wider than the destination.
5203 This is needed for decimal float modes which cannot accurately
5204 represent one plus the highest signed number of the same size, but
5205 not for binary modes. Consider, for instance conversion from SFmode
5206 into DImode.
5208 The hot path through the code is dealing with inputs smaller than 2^63
5209 and doing just the conversion, so there is no bits to lose.
5211 In the other path we know the value is positive in the range 2^63..2^64-1
5212 inclusive. (as for other input overflow happens and result is undefined)
5213 So we know that the most important bit set in mantissa corresponds to
5214 2^63. The subtraction of 2^63 should not generate any rounding as it
5215 simply clears out that bit. The rest is trivial. */
5217 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5218 for (fmode = GET_MODE (from); fmode != VOIDmode;
5219 fmode = GET_MODE_WIDER_MODE (fmode))
5220 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5221 && (!DECIMAL_FLOAT_MODE_P (fmode)
5222 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5224 int bitsize;
5225 REAL_VALUE_TYPE offset;
5226 rtx limit, lab1, lab2, insn;
5228 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5229 real_2expN (&offset, bitsize - 1, fmode);
5230 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5231 lab1 = gen_label_rtx ();
5232 lab2 = gen_label_rtx ();
5234 if (fmode != GET_MODE (from))
5235 from = convert_to_mode (fmode, from, 0);
5237 /* See if we need to do the subtraction. */
5238 do_pending_stack_adjust ();
5239 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5240 0, lab1);
5242 /* If not, do the signed "fix" and branch around fixup code. */
5243 expand_fix (to, from, 0);
5244 emit_jump_insn (gen_jump (lab2));
5245 emit_barrier ();
5247 /* Otherwise, subtract 2**(N-1), convert to signed number,
5248 then add 2**(N-1). Do the addition using XOR since this
5249 will often generate better code. */
5250 emit_label (lab1);
5251 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5252 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5253 expand_fix (to, target, 0);
5254 target = expand_binop (GET_MODE (to), xor_optab, to,
5255 gen_int_mode
5256 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5257 GET_MODE (to)),
5258 to, 1, OPTAB_LIB_WIDEN);
5260 if (target != to)
5261 emit_move_insn (to, target);
5263 emit_label (lab2);
5265 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5267 /* Make a place for a REG_NOTE and add it. */
5268 insn = emit_move_insn (to, to);
5269 set_dst_reg_note (insn, REG_EQUAL,
5270 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5271 copy_rtx (from)),
5272 to);
5275 return;
5278 /* We can't do it with an insn, so use a library call. But first ensure
5279 that the mode of TO is at least as wide as SImode, since those are the
5280 only library calls we know about. */
5282 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
5284 target = gen_reg_rtx (SImode);
5286 expand_fix (target, from, unsignedp);
5288 else
5290 rtx insns;
5291 rtx value;
5292 rtx libfunc;
5294 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5295 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5296 gcc_assert (libfunc);
5298 start_sequence ();
5300 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5301 GET_MODE (to), 1, from,
5302 GET_MODE (from));
5303 insns = get_insns ();
5304 end_sequence ();
5306 emit_libcall_block (insns, target, value,
5307 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5308 GET_MODE (to), from));
5311 if (target != to)
5313 if (GET_MODE (to) == GET_MODE (target))
5314 emit_move_insn (to, target);
5315 else
5316 convert_move (to, target, 0);
5320 /* Generate code to convert FROM or TO a fixed-point.
5321 If UINTP is true, either TO or FROM is an unsigned integer.
5322 If SATP is true, we need to saturate the result. */
5324 void
5325 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5327 enum machine_mode to_mode = GET_MODE (to);
5328 enum machine_mode from_mode = GET_MODE (from);
5329 convert_optab tab;
5330 enum rtx_code this_code;
5331 enum insn_code code;
5332 rtx insns, value;
5333 rtx libfunc;
5335 if (to_mode == from_mode)
5337 emit_move_insn (to, from);
5338 return;
5341 if (uintp)
5343 tab = satp ? satfractuns_optab : fractuns_optab;
5344 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5346 else
5348 tab = satp ? satfract_optab : fract_optab;
5349 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5351 code = convert_optab_handler (tab, to_mode, from_mode);
5352 if (code != CODE_FOR_nothing)
5354 emit_unop_insn (code, to, from, this_code);
5355 return;
5358 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5359 gcc_assert (libfunc);
5361 start_sequence ();
5362 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5363 1, from, from_mode);
5364 insns = get_insns ();
5365 end_sequence ();
5367 emit_libcall_block (insns, to, value,
5368 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5371 /* Generate code to convert FROM to fixed point and store in TO. FROM
5372 must be floating point, TO must be signed. Use the conversion optab
5373 TAB to do the conversion. */
5375 bool
5376 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5378 enum insn_code icode;
5379 rtx target = to;
5380 enum machine_mode fmode, imode;
5382 /* We first try to find a pair of modes, one real and one integer, at
5383 least as wide as FROM and TO, respectively, in which we can open-code
5384 this conversion. If the integer mode is wider than the mode of TO,
5385 we can do the conversion either signed or unsigned. */
5387 for (fmode = GET_MODE (from); fmode != VOIDmode;
5388 fmode = GET_MODE_WIDER_MODE (fmode))
5389 for (imode = GET_MODE (to); imode != VOIDmode;
5390 imode = GET_MODE_WIDER_MODE (imode))
5392 icode = convert_optab_handler (tab, imode, fmode);
5393 if (icode != CODE_FOR_nothing)
5395 rtx last = get_last_insn ();
5396 if (fmode != GET_MODE (from))
5397 from = convert_to_mode (fmode, from, 0);
5399 if (imode != GET_MODE (to))
5400 target = gen_reg_rtx (imode);
5402 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5404 delete_insns_since (last);
5405 continue;
5407 if (target != to)
5408 convert_move (to, target, 0);
5409 return true;
5413 return false;
5416 /* Report whether we have an instruction to perform the operation
5417 specified by CODE on operands of mode MODE. */
5419 have_insn_for (enum rtx_code code, enum machine_mode mode)
5421 return (code_to_optab (code)
5422 && (optab_handler (code_to_optab (code), mode)
5423 != CODE_FOR_nothing));
5426 /* Initialize the libfunc fields of an entire group of entries in some
5427 optab. Each entry is set equal to a string consisting of a leading
5428 pair of underscores followed by a generic operation name followed by
5429 a mode name (downshifted to lowercase) followed by a single character
5430 representing the number of operands for the given operation (which is
5431 usually one of the characters '2', '3', or '4').
5433 OPTABLE is the table in which libfunc fields are to be initialized.
5434 OPNAME is the generic (string) name of the operation.
5435 SUFFIX is the character which specifies the number of operands for
5436 the given generic operation.
5437 MODE is the mode to generate for.
5440 static void
5441 gen_libfunc (optab optable, const char *opname, int suffix,
5442 enum machine_mode mode)
5444 unsigned opname_len = strlen (opname);
5445 const char *mname = GET_MODE_NAME (mode);
5446 unsigned mname_len = strlen (mname);
5447 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5448 int len = prefix_len + opname_len + mname_len + 1 + 1;
5449 char *libfunc_name = XALLOCAVEC (char, len);
5450 char *p;
5451 const char *q;
5453 p = libfunc_name;
5454 *p++ = '_';
5455 *p++ = '_';
5456 if (targetm.libfunc_gnu_prefix)
5458 *p++ = 'g';
5459 *p++ = 'n';
5460 *p++ = 'u';
5461 *p++ = '_';
5463 for (q = opname; *q; )
5464 *p++ = *q++;
5465 for (q = mname; *q; q++)
5466 *p++ = TOLOWER (*q);
5467 *p++ = suffix;
5468 *p = '\0';
5470 set_optab_libfunc (optable, mode,
5471 ggc_alloc_string (libfunc_name, p - libfunc_name));
5474 /* Like gen_libfunc, but verify that integer operation is involved. */
5476 void
5477 gen_int_libfunc (optab optable, const char *opname, char suffix,
5478 enum machine_mode mode)
5480 int maxsize = 2 * BITS_PER_WORD;
5482 if (GET_MODE_CLASS (mode) != MODE_INT)
5483 return;
5484 if (maxsize < LONG_LONG_TYPE_SIZE)
5485 maxsize = LONG_LONG_TYPE_SIZE;
5486 if (GET_MODE_CLASS (mode) != MODE_INT
5487 || mode < word_mode || GET_MODE_BITSIZE (mode) > maxsize)
5488 return;
5489 gen_libfunc (optable, opname, suffix, mode);
5492 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5494 void
5495 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5496 enum machine_mode mode)
5498 char *dec_opname;
5500 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5501 gen_libfunc (optable, opname, suffix, mode);
5502 if (DECIMAL_FLOAT_MODE_P (mode))
5504 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5505 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5506 depending on the low level floating format used. */
5507 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5508 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5509 gen_libfunc (optable, dec_opname, suffix, mode);
5513 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5515 void
5516 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5517 enum machine_mode mode)
5519 if (!ALL_FIXED_POINT_MODE_P (mode))
5520 return;
5521 gen_libfunc (optable, opname, suffix, mode);
5524 /* Like gen_libfunc, but verify that signed fixed-point operation is
5525 involved. */
5527 void
5528 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5529 enum machine_mode mode)
5531 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5532 return;
5533 gen_libfunc (optable, opname, suffix, mode);
5536 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5537 involved. */
5539 void
5540 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5541 enum machine_mode mode)
5543 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5544 return;
5545 gen_libfunc (optable, opname, suffix, mode);
5548 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5550 void
5551 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5552 enum machine_mode mode)
5554 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5555 gen_fp_libfunc (optable, name, suffix, mode);
5556 if (INTEGRAL_MODE_P (mode))
5557 gen_int_libfunc (optable, name, suffix, mode);
5560 /* Like gen_libfunc, but verify that FP or INT operation is involved
5561 and add 'v' suffix for integer operation. */
5563 void
5564 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5565 enum machine_mode mode)
5567 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5568 gen_fp_libfunc (optable, name, suffix, mode);
5569 if (GET_MODE_CLASS (mode) == MODE_INT)
5571 int len = strlen (name);
5572 char *v_name = XALLOCAVEC (char, len + 2);
5573 strcpy (v_name, name);
5574 v_name[len] = 'v';
5575 v_name[len + 1] = 0;
5576 gen_int_libfunc (optable, v_name, suffix, mode);
5580 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5581 involved. */
5583 void
5584 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5585 enum machine_mode mode)
5587 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5588 gen_fp_libfunc (optable, name, suffix, mode);
5589 if (INTEGRAL_MODE_P (mode))
5590 gen_int_libfunc (optable, name, suffix, mode);
5591 if (ALL_FIXED_POINT_MODE_P (mode))
5592 gen_fixed_libfunc (optable, name, suffix, mode);
5595 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5596 involved. */
5598 void
5599 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5600 enum machine_mode mode)
5602 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5603 gen_fp_libfunc (optable, name, suffix, mode);
5604 if (INTEGRAL_MODE_P (mode))
5605 gen_int_libfunc (optable, name, suffix, mode);
5606 if (SIGNED_FIXED_POINT_MODE_P (mode))
5607 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5610 /* Like gen_libfunc, but verify that INT or FIXED operation is
5611 involved. */
5613 void
5614 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5615 enum machine_mode mode)
5617 if (INTEGRAL_MODE_P (mode))
5618 gen_int_libfunc (optable, name, suffix, mode);
5619 if (ALL_FIXED_POINT_MODE_P (mode))
5620 gen_fixed_libfunc (optable, name, suffix, mode);
5623 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5624 involved. */
5626 void
5627 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5628 enum machine_mode mode)
5630 if (INTEGRAL_MODE_P (mode))
5631 gen_int_libfunc (optable, name, suffix, mode);
5632 if (SIGNED_FIXED_POINT_MODE_P (mode))
5633 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5636 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5637 involved. */
5639 void
5640 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5641 enum machine_mode mode)
5643 if (INTEGRAL_MODE_P (mode))
5644 gen_int_libfunc (optable, name, suffix, mode);
5645 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5646 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5649 /* Initialize the libfunc fields of an entire group of entries of an
5650 inter-mode-class conversion optab. The string formation rules are
5651 similar to the ones for init_libfuncs, above, but instead of having
5652 a mode name and an operand count these functions have two mode names
5653 and no operand count. */
5655 void
5656 gen_interclass_conv_libfunc (convert_optab tab,
5657 const char *opname,
5658 enum machine_mode tmode,
5659 enum machine_mode fmode)
5661 size_t opname_len = strlen (opname);
5662 size_t mname_len = 0;
5664 const char *fname, *tname;
5665 const char *q;
5666 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5667 char *libfunc_name, *suffix;
5668 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5669 char *p;
5671 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5672 depends on which underlying decimal floating point format is used. */
5673 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5675 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5677 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5678 nondec_name[0] = '_';
5679 nondec_name[1] = '_';
5680 if (targetm.libfunc_gnu_prefix)
5682 nondec_name[2] = 'g';
5683 nondec_name[3] = 'n';
5684 nondec_name[4] = 'u';
5685 nondec_name[5] = '_';
5688 memcpy (&nondec_name[prefix_len], opname, opname_len);
5689 nondec_suffix = nondec_name + opname_len + prefix_len;
5691 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5692 dec_name[0] = '_';
5693 dec_name[1] = '_';
5694 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5695 memcpy (&dec_name[2+dec_len], opname, opname_len);
5696 dec_suffix = dec_name + dec_len + opname_len + 2;
5698 fname = GET_MODE_NAME (fmode);
5699 tname = GET_MODE_NAME (tmode);
5701 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5703 libfunc_name = dec_name;
5704 suffix = dec_suffix;
5706 else
5708 libfunc_name = nondec_name;
5709 suffix = nondec_suffix;
5712 p = suffix;
5713 for (q = fname; *q; p++, q++)
5714 *p = TOLOWER (*q);
5715 for (q = tname; *q; p++, q++)
5716 *p = TOLOWER (*q);
5718 *p = '\0';
5720 set_conv_libfunc (tab, tmode, fmode,
5721 ggc_alloc_string (libfunc_name, p - libfunc_name));
5724 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5725 int->fp conversion. */
5727 void
5728 gen_int_to_fp_conv_libfunc (convert_optab tab,
5729 const char *opname,
5730 enum machine_mode tmode,
5731 enum machine_mode fmode)
5733 if (GET_MODE_CLASS (fmode) != MODE_INT)
5734 return;
5735 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5736 return;
5737 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5740 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5741 naming scheme. */
5743 void
5744 gen_ufloat_conv_libfunc (convert_optab tab,
5745 const char *opname ATTRIBUTE_UNUSED,
5746 enum machine_mode tmode,
5747 enum machine_mode fmode)
5749 if (DECIMAL_FLOAT_MODE_P (tmode))
5750 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5751 else
5752 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5755 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5756 fp->int conversion. */
5758 void
5759 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5760 const char *opname,
5761 enum machine_mode tmode,
5762 enum machine_mode fmode)
5764 if (GET_MODE_CLASS (fmode) != MODE_INT)
5765 return;
5766 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5767 return;
5768 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5771 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5772 fp->int conversion with no decimal floating point involved. */
5774 void
5775 gen_fp_to_int_conv_libfunc (convert_optab tab,
5776 const char *opname,
5777 enum machine_mode tmode,
5778 enum machine_mode fmode)
5780 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5781 return;
5782 if (GET_MODE_CLASS (tmode) != MODE_INT)
5783 return;
5784 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5787 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5788 The string formation rules are
5789 similar to the ones for init_libfunc, above. */
5791 void
5792 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5793 enum machine_mode tmode, enum machine_mode fmode)
5795 size_t opname_len = strlen (opname);
5796 size_t mname_len = 0;
5798 const char *fname, *tname;
5799 const char *q;
5800 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5801 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5802 char *libfunc_name, *suffix;
5803 char *p;
5805 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5806 depends on which underlying decimal floating point format is used. */
5807 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5809 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5811 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5812 nondec_name[0] = '_';
5813 nondec_name[1] = '_';
5814 if (targetm.libfunc_gnu_prefix)
5816 nondec_name[2] = 'g';
5817 nondec_name[3] = 'n';
5818 nondec_name[4] = 'u';
5819 nondec_name[5] = '_';
5821 memcpy (&nondec_name[prefix_len], opname, opname_len);
5822 nondec_suffix = nondec_name + opname_len + prefix_len;
5824 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5825 dec_name[0] = '_';
5826 dec_name[1] = '_';
5827 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5828 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5829 dec_suffix = dec_name + dec_len + opname_len + 2;
5831 fname = GET_MODE_NAME (fmode);
5832 tname = GET_MODE_NAME (tmode);
5834 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5836 libfunc_name = dec_name;
5837 suffix = dec_suffix;
5839 else
5841 libfunc_name = nondec_name;
5842 suffix = nondec_suffix;
5845 p = suffix;
5846 for (q = fname; *q; p++, q++)
5847 *p = TOLOWER (*q);
5848 for (q = tname; *q; p++, q++)
5849 *p = TOLOWER (*q);
5851 *p++ = '2';
5852 *p = '\0';
5854 set_conv_libfunc (tab, tmode, fmode,
5855 ggc_alloc_string (libfunc_name, p - libfunc_name));
5858 /* Pick proper libcall for trunc_optab. We need to chose if we do
5859 truncation or extension and interclass or intraclass. */
5861 void
5862 gen_trunc_conv_libfunc (convert_optab tab,
5863 const char *opname,
5864 enum machine_mode tmode,
5865 enum machine_mode fmode)
5867 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5868 return;
5869 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5870 return;
5871 if (tmode == fmode)
5872 return;
5874 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5875 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5876 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5878 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5879 return;
5881 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5882 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5883 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5884 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5887 /* Pick proper libcall for extend_optab. We need to chose if we do
5888 truncation or extension and interclass or intraclass. */
5890 void
5891 gen_extend_conv_libfunc (convert_optab tab,
5892 const char *opname ATTRIBUTE_UNUSED,
5893 enum machine_mode tmode,
5894 enum machine_mode fmode)
5896 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5897 return;
5898 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5899 return;
5900 if (tmode == fmode)
5901 return;
5903 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5904 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5905 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5907 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5908 return;
5910 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5911 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5912 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5913 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5916 /* Pick proper libcall for fract_optab. We need to chose if we do
5917 interclass or intraclass. */
5919 void
5920 gen_fract_conv_libfunc (convert_optab tab,
5921 const char *opname,
5922 enum machine_mode tmode,
5923 enum machine_mode fmode)
5925 if (tmode == fmode)
5926 return;
5927 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5928 return;
5930 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5931 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5932 else
5933 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5936 /* Pick proper libcall for fractuns_optab. */
5938 void
5939 gen_fractuns_conv_libfunc (convert_optab tab,
5940 const char *opname,
5941 enum machine_mode tmode,
5942 enum machine_mode fmode)
5944 if (tmode == fmode)
5945 return;
5946 /* One mode must be a fixed-point mode, and the other must be an integer
5947 mode. */
5948 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
5949 || (ALL_FIXED_POINT_MODE_P (fmode)
5950 && GET_MODE_CLASS (tmode) == MODE_INT)))
5951 return;
5953 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5956 /* Pick proper libcall for satfract_optab. We need to chose if we do
5957 interclass or intraclass. */
5959 void
5960 gen_satfract_conv_libfunc (convert_optab tab,
5961 const char *opname,
5962 enum machine_mode tmode,
5963 enum machine_mode fmode)
5965 if (tmode == fmode)
5966 return;
5967 /* TMODE must be a fixed-point mode. */
5968 if (!ALL_FIXED_POINT_MODE_P (tmode))
5969 return;
5971 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5972 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5973 else
5974 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5977 /* Pick proper libcall for satfractuns_optab. */
5979 void
5980 gen_satfractuns_conv_libfunc (convert_optab tab,
5981 const char *opname,
5982 enum machine_mode tmode,
5983 enum machine_mode fmode)
5985 if (tmode == fmode)
5986 return;
5987 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
5988 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
5989 return;
5991 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5994 /* A table of previously-created libfuncs, hashed by name. */
5995 static GTY ((param_is (union tree_node))) htab_t libfunc_decls;
5997 /* Hashtable callbacks for libfunc_decls. */
5999 static hashval_t
6000 libfunc_decl_hash (const void *entry)
6002 return IDENTIFIER_HASH_VALUE (DECL_NAME ((const_tree) entry));
6005 static int
6006 libfunc_decl_eq (const void *entry1, const void *entry2)
6008 return DECL_NAME ((const_tree) entry1) == (const_tree) entry2;
6011 /* Build a decl for a libfunc named NAME. */
6013 tree
6014 build_libfunc_function (const char *name)
6016 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6017 get_identifier (name),
6018 build_function_type (integer_type_node, NULL_TREE));
6019 /* ??? We don't have any type information except for this is
6020 a function. Pretend this is "int foo()". */
6021 DECL_ARTIFICIAL (decl) = 1;
6022 DECL_EXTERNAL (decl) = 1;
6023 TREE_PUBLIC (decl) = 1;
6024 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6026 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6027 are the flags assigned by targetm.encode_section_info. */
6028 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6030 return decl;
6034 init_one_libfunc (const char *name)
6036 tree id, decl;
6037 void **slot;
6038 hashval_t hash;
6040 if (libfunc_decls == NULL)
6041 libfunc_decls = htab_create_ggc (37, libfunc_decl_hash,
6042 libfunc_decl_eq, NULL);
6044 /* See if we have already created a libfunc decl for this function. */
6045 id = get_identifier (name);
6046 hash = IDENTIFIER_HASH_VALUE (id);
6047 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, INSERT);
6048 decl = (tree) *slot;
6049 if (decl == NULL)
6051 /* Create a new decl, so that it can be passed to
6052 targetm.encode_section_info. */
6053 decl = build_libfunc_function (name);
6054 *slot = decl;
6056 return XEXP (DECL_RTL (decl), 0);
6059 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6062 set_user_assembler_libfunc (const char *name, const char *asmspec)
6064 tree id, decl;
6065 void **slot;
6066 hashval_t hash;
6068 id = get_identifier (name);
6069 hash = IDENTIFIER_HASH_VALUE (id);
6070 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, NO_INSERT);
6071 gcc_assert (slot);
6072 decl = (tree) *slot;
6073 set_user_assembler_name (decl, asmspec);
6074 return XEXP (DECL_RTL (decl), 0);
6077 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6078 MODE to NAME, which should be either 0 or a string constant. */
6079 void
6080 set_optab_libfunc (optab op, enum machine_mode mode, const char *name)
6082 rtx val;
6083 struct libfunc_entry e;
6084 struct libfunc_entry **slot;
6086 e.op = op;
6087 e.mode1 = mode;
6088 e.mode2 = VOIDmode;
6090 if (name)
6091 val = init_one_libfunc (name);
6092 else
6093 val = 0;
6094 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6095 if (*slot == NULL)
6096 *slot = ggc_alloc_libfunc_entry ();
6097 (*slot)->op = op;
6098 (*slot)->mode1 = mode;
6099 (*slot)->mode2 = VOIDmode;
6100 (*slot)->libfunc = val;
6103 /* Call this to reset the function entry for one conversion optab
6104 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6105 either 0 or a string constant. */
6106 void
6107 set_conv_libfunc (convert_optab optab, enum machine_mode tmode,
6108 enum machine_mode fmode, const char *name)
6110 rtx val;
6111 struct libfunc_entry e;
6112 struct libfunc_entry **slot;
6114 e.op = optab;
6115 e.mode1 = tmode;
6116 e.mode2 = fmode;
6118 if (name)
6119 val = init_one_libfunc (name);
6120 else
6121 val = 0;
6122 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6123 if (*slot == NULL)
6124 *slot = ggc_alloc_libfunc_entry ();
6125 (*slot)->op = optab;
6126 (*slot)->mode1 = tmode;
6127 (*slot)->mode2 = fmode;
6128 (*slot)->libfunc = val;
6131 /* Call this to initialize the contents of the optabs
6132 appropriately for the current target machine. */
6134 void
6135 init_optabs (void)
6137 if (libfunc_hash)
6138 htab_empty (libfunc_hash);
6139 else
6140 libfunc_hash = htab_create_ggc (10, hash_libfunc, eq_libfunc, NULL);
6142 /* Fill in the optabs with the insns we support. */
6143 init_all_optabs ();
6145 /* The ffs function operates on `int'. Fall back on it if we do not
6146 have a libgcc2 function for that width. */
6147 if (INT_TYPE_SIZE < BITS_PER_WORD)
6148 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6149 "ffs");
6151 /* Explicitly initialize the bswap libfuncs since we need them to be
6152 valid for things other than word_mode. */
6153 if (targetm.libfunc_gnu_prefix)
6155 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6156 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6158 else
6160 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6161 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6164 /* Use cabs for double complex abs, since systems generally have cabs.
6165 Don't define any libcall for float complex, so that cabs will be used. */
6166 if (complex_double_type_node)
6167 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node),
6168 "cabs");
6170 abort_libfunc = init_one_libfunc ("abort");
6171 memcpy_libfunc = init_one_libfunc ("memcpy");
6172 memmove_libfunc = init_one_libfunc ("memmove");
6173 memcmp_libfunc = init_one_libfunc ("memcmp");
6174 memset_libfunc = init_one_libfunc ("memset");
6175 setbits_libfunc = init_one_libfunc ("__setbits");
6177 #ifndef DONT_USE_BUILTIN_SETJMP
6178 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6179 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6180 #else
6181 setjmp_libfunc = init_one_libfunc ("setjmp");
6182 longjmp_libfunc = init_one_libfunc ("longjmp");
6183 #endif
6184 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6185 unwind_sjlj_unregister_libfunc
6186 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6188 /* For function entry/exit instrumentation. */
6189 profile_function_entry_libfunc
6190 = init_one_libfunc ("__cyg_profile_func_enter");
6191 profile_function_exit_libfunc
6192 = init_one_libfunc ("__cyg_profile_func_exit");
6194 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6196 /* Allow the target to add more libcalls or rename some, etc. */
6197 targetm.init_libfuncs ();
6200 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6201 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6203 static void
6204 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6206 enum machine_mode mode;
6207 char buf[64];
6208 size_t len = strlen (base);
6209 int i;
6211 gcc_assert (max <= 8);
6212 gcc_assert (len + 3 < sizeof (buf));
6214 memcpy (buf, base, len);
6215 buf[len] = '_';
6216 buf[len + 1] = '0';
6217 buf[len + 2] = '\0';
6219 mode = QImode;
6220 for (i = 1; i <= max; i *= 2)
6222 buf[len + 1] = '0' + i;
6223 set_optab_libfunc (tab, mode, buf);
6224 mode = GET_MODE_2XWIDER_MODE (mode);
6228 void
6229 init_sync_libfuncs (int max)
6231 if (!flag_sync_libcalls)
6232 return;
6234 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6235 "__sync_val_compare_and_swap", max);
6236 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6237 "__sync_lock_test_and_set", max);
6239 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6240 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6241 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6242 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6243 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6244 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6246 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6247 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6248 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6249 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6250 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6251 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6254 /* Print information about the current contents of the optabs on
6255 STDERR. */
6257 DEBUG_FUNCTION void
6258 debug_optab_libfuncs (void)
6260 int i, j, k;
6262 /* Dump the arithmetic optabs. */
6263 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
6264 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6266 rtx l = optab_libfunc ((optab) i, (enum machine_mode) j);
6267 if (l)
6269 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6270 fprintf (stderr, "%s\t%s:\t%s\n",
6271 GET_RTX_NAME (optab_to_code ((optab) i)),
6272 GET_MODE_NAME (j),
6273 XSTR (l, 0));
6277 /* Dump the conversion optabs. */
6278 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
6279 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6280 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6282 rtx l = convert_optab_libfunc ((optab) i, (enum machine_mode) j,
6283 (enum machine_mode) k);
6284 if (l)
6286 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6287 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6288 GET_RTX_NAME (optab_to_code ((optab) i)),
6289 GET_MODE_NAME (j),
6290 GET_MODE_NAME (k),
6291 XSTR (l, 0));
6297 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6298 CODE. Return 0 on failure. */
6301 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6303 enum machine_mode mode = GET_MODE (op1);
6304 enum insn_code icode;
6305 rtx insn;
6306 rtx trap_rtx;
6308 if (mode == VOIDmode)
6309 return 0;
6311 icode = optab_handler (ctrap_optab, mode);
6312 if (icode == CODE_FOR_nothing)
6313 return 0;
6315 /* Some targets only accept a zero trap code. */
6316 if (!insn_operand_matches (icode, 3, tcode))
6317 return 0;
6319 do_pending_stack_adjust ();
6320 start_sequence ();
6321 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6322 &trap_rtx, &mode);
6323 if (!trap_rtx)
6324 insn = NULL_RTX;
6325 else
6326 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6327 tcode);
6329 /* If that failed, then give up. */
6330 if (insn == 0)
6332 end_sequence ();
6333 return 0;
6336 emit_insn (insn);
6337 insn = get_insns ();
6338 end_sequence ();
6339 return insn;
6342 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6343 or unsigned operation code. */
6345 static enum rtx_code
6346 get_rtx_code (enum tree_code tcode, bool unsignedp)
6348 enum rtx_code code;
6349 switch (tcode)
6351 case EQ_EXPR:
6352 code = EQ;
6353 break;
6354 case NE_EXPR:
6355 code = NE;
6356 break;
6357 case LT_EXPR:
6358 code = unsignedp ? LTU : LT;
6359 break;
6360 case LE_EXPR:
6361 code = unsignedp ? LEU : LE;
6362 break;
6363 case GT_EXPR:
6364 code = unsignedp ? GTU : GT;
6365 break;
6366 case GE_EXPR:
6367 code = unsignedp ? GEU : GE;
6368 break;
6370 case UNORDERED_EXPR:
6371 code = UNORDERED;
6372 break;
6373 case ORDERED_EXPR:
6374 code = ORDERED;
6375 break;
6376 case UNLT_EXPR:
6377 code = UNLT;
6378 break;
6379 case UNLE_EXPR:
6380 code = UNLE;
6381 break;
6382 case UNGT_EXPR:
6383 code = UNGT;
6384 break;
6385 case UNGE_EXPR:
6386 code = UNGE;
6387 break;
6388 case UNEQ_EXPR:
6389 code = UNEQ;
6390 break;
6391 case LTGT_EXPR:
6392 code = LTGT;
6393 break;
6395 default:
6396 gcc_unreachable ();
6398 return code;
6401 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6402 unsigned operators. Do not generate compare instruction. */
6404 static rtx
6405 vector_compare_rtx (enum tree_code tcode, tree t_op0, tree t_op1,
6406 bool unsignedp, enum insn_code icode)
6408 struct expand_operand ops[2];
6409 rtx rtx_op0, rtx_op1;
6410 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
6412 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
6414 /* Expand operands. */
6415 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6416 EXPAND_STACK_PARM);
6417 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6418 EXPAND_STACK_PARM);
6420 create_input_operand (&ops[0], rtx_op0, GET_MODE (rtx_op0));
6421 create_input_operand (&ops[1], rtx_op1, GET_MODE (rtx_op1));
6422 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6423 gcc_unreachable ();
6424 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6427 /* Return true if VEC_PERM_EXPR can be expanded using SIMD extensions
6428 of the CPU. SEL may be NULL, which stands for an unknown constant. */
6430 bool
6431 can_vec_perm_p (enum machine_mode mode, bool variable,
6432 const unsigned char *sel)
6434 enum machine_mode qimode;
6436 /* If the target doesn't implement a vector mode for the vector type,
6437 then no operations are supported. */
6438 if (!VECTOR_MODE_P (mode))
6439 return false;
6441 if (!variable)
6443 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6444 && (sel == NULL
6445 || targetm.vectorize.vec_perm_const_ok == NULL
6446 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6447 return true;
6450 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6451 return true;
6453 /* We allow fallback to a QI vector mode, and adjust the mask. */
6454 if (GET_MODE_INNER (mode) == QImode)
6455 return false;
6456 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6457 if (!VECTOR_MODE_P (qimode))
6458 return false;
6460 /* ??? For completeness, we ought to check the QImode version of
6461 vec_perm_const_optab. But all users of this implicit lowering
6462 feature implement the variable vec_perm_optab. */
6463 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6464 return false;
6466 /* In order to support the lowering of variable permutations,
6467 we need to support shifts and adds. */
6468 if (variable)
6470 if (GET_MODE_UNIT_SIZE (mode) > 2
6471 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6472 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6473 return false;
6474 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6475 return false;
6478 return true;
6481 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
6483 static rtx
6484 expand_vec_perm_1 (enum insn_code icode, rtx target,
6485 rtx v0, rtx v1, rtx sel)
6487 enum machine_mode tmode = GET_MODE (target);
6488 enum machine_mode smode = GET_MODE (sel);
6489 struct expand_operand ops[4];
6491 create_output_operand (&ops[0], target, tmode);
6492 create_input_operand (&ops[3], sel, smode);
6494 /* Make an effort to preserve v0 == v1. The target expander is able to
6495 rely on this to determine if we're permuting a single input operand. */
6496 if (rtx_equal_p (v0, v1))
6498 if (!insn_operand_matches (icode, 1, v0))
6499 v0 = force_reg (tmode, v0);
6500 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6501 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6503 create_fixed_operand (&ops[1], v0);
6504 create_fixed_operand (&ops[2], v0);
6506 else
6508 create_input_operand (&ops[1], v0, tmode);
6509 create_input_operand (&ops[2], v1, tmode);
6512 if (maybe_expand_insn (icode, 4, ops))
6513 return ops[0].value;
6514 return NULL_RTX;
6517 /* Generate instructions for vec_perm optab given its mode
6518 and three operands. */
6521 expand_vec_perm (enum machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6523 enum insn_code icode;
6524 enum machine_mode qimode;
6525 unsigned int i, w, e, u;
6526 rtx tmp, sel_qi = NULL;
6527 rtvec vec;
6529 if (!target || GET_MODE (target) != mode)
6530 target = gen_reg_rtx (mode);
6532 w = GET_MODE_SIZE (mode);
6533 e = GET_MODE_NUNITS (mode);
6534 u = GET_MODE_UNIT_SIZE (mode);
6536 /* Set QIMODE to a different vector mode with byte elements.
6537 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6538 qimode = VOIDmode;
6539 if (GET_MODE_INNER (mode) != QImode)
6541 qimode = mode_for_vector (QImode, w);
6542 if (!VECTOR_MODE_P (qimode))
6543 qimode = VOIDmode;
6546 /* If the input is a constant, expand it specially. */
6547 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6548 if (GET_CODE (sel) == CONST_VECTOR)
6550 icode = direct_optab_handler (vec_perm_const_optab, mode);
6551 if (icode != CODE_FOR_nothing)
6553 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6554 if (tmp)
6555 return tmp;
6558 /* Fall back to a constant byte-based permutation. */
6559 if (qimode != VOIDmode)
6561 vec = rtvec_alloc (w);
6562 for (i = 0; i < e; ++i)
6564 unsigned int j, this_e;
6566 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6567 this_e &= 2 * e - 1;
6568 this_e *= u;
6570 for (j = 0; j < u; ++j)
6571 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
6573 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
6575 icode = direct_optab_handler (vec_perm_const_optab, qimode);
6576 if (icode != CODE_FOR_nothing)
6578 tmp = expand_vec_perm_1 (icode, gen_lowpart (qimode, target),
6579 gen_lowpart (qimode, v0),
6580 gen_lowpart (qimode, v1), sel_qi);
6581 if (tmp)
6582 return gen_lowpart (mode, tmp);
6587 /* Otherwise expand as a fully variable permuation. */
6588 icode = direct_optab_handler (vec_perm_optab, mode);
6589 if (icode != CODE_FOR_nothing)
6591 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6592 if (tmp)
6593 return tmp;
6596 /* As a special case to aid several targets, lower the element-based
6597 permutation to a byte-based permutation and try again. */
6598 if (qimode == VOIDmode)
6599 return NULL_RTX;
6600 icode = direct_optab_handler (vec_perm_optab, qimode);
6601 if (icode == CODE_FOR_nothing)
6602 return NULL_RTX;
6604 if (sel_qi == NULL)
6606 /* Multiply each element by its byte size. */
6607 enum machine_mode selmode = GET_MODE (sel);
6608 if (u == 2)
6609 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6610 sel, 0, OPTAB_DIRECT);
6611 else
6612 sel = expand_simple_binop (selmode, ASHIFT, sel,
6613 GEN_INT (exact_log2 (u)),
6614 sel, 0, OPTAB_DIRECT);
6615 gcc_assert (sel != NULL);
6617 /* Broadcast the low byte each element into each of its bytes. */
6618 vec = rtvec_alloc (w);
6619 for (i = 0; i < w; ++i)
6621 int this_e = i / u * u;
6622 if (BYTES_BIG_ENDIAN)
6623 this_e += u - 1;
6624 RTVEC_ELT (vec, i) = GEN_INT (this_e);
6626 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6627 sel = gen_lowpart (qimode, sel);
6628 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
6629 gcc_assert (sel != NULL);
6631 /* Add the byte offset to each byte element. */
6632 /* Note that the definition of the indicies here is memory ordering,
6633 so there should be no difference between big and little endian. */
6634 vec = rtvec_alloc (w);
6635 for (i = 0; i < w; ++i)
6636 RTVEC_ELT (vec, i) = GEN_INT (i % u);
6637 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6638 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6639 sel, 0, OPTAB_DIRECT);
6640 gcc_assert (sel_qi != NULL);
6643 tmp = expand_vec_perm_1 (icode, gen_lowpart (qimode, target),
6644 gen_lowpart (qimode, v0),
6645 gen_lowpart (qimode, v1), sel_qi);
6646 if (tmp)
6647 tmp = gen_lowpart (mode, tmp);
6648 return tmp;
6651 /* Return insn code for a conditional operator with a comparison in
6652 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
6654 static inline enum insn_code
6655 get_vcond_icode (enum machine_mode vmode, enum machine_mode cmode, bool uns)
6657 enum insn_code icode = CODE_FOR_nothing;
6658 if (uns)
6659 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
6660 else
6661 icode = convert_optab_handler (vcond_optab, vmode, cmode);
6662 return icode;
6665 /* Return TRUE iff, appropriate vector insns are available
6666 for vector cond expr with vector type VALUE_TYPE and a comparison
6667 with operand vector types in CMP_OP_TYPE. */
6669 bool
6670 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
6672 enum machine_mode value_mode = TYPE_MODE (value_type);
6673 enum machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
6674 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
6675 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
6676 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
6677 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
6678 return false;
6679 return true;
6682 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6683 three operands. */
6686 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6687 rtx target)
6689 struct expand_operand ops[6];
6690 enum insn_code icode;
6691 rtx comparison, rtx_op1, rtx_op2;
6692 enum machine_mode mode = TYPE_MODE (vec_cond_type);
6693 enum machine_mode cmp_op_mode;
6694 bool unsignedp;
6695 tree op0a, op0b;
6696 enum tree_code tcode;
6698 if (COMPARISON_CLASS_P (op0))
6700 op0a = TREE_OPERAND (op0, 0);
6701 op0b = TREE_OPERAND (op0, 1);
6702 tcode = TREE_CODE (op0);
6704 else
6706 /* Fake op0 < 0. */
6707 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (op0)));
6708 op0a = op0;
6709 op0b = build_zero_cst (TREE_TYPE (op0));
6710 tcode = LT_EXPR;
6712 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6713 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
6716 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
6717 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
6719 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
6720 if (icode == CODE_FOR_nothing)
6721 return 0;
6723 comparison = vector_compare_rtx (tcode, op0a, op0b, unsignedp, icode);
6724 rtx_op1 = expand_normal (op1);
6725 rtx_op2 = expand_normal (op2);
6727 create_output_operand (&ops[0], target, mode);
6728 create_input_operand (&ops[1], rtx_op1, mode);
6729 create_input_operand (&ops[2], rtx_op2, mode);
6730 create_fixed_operand (&ops[3], comparison);
6731 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6732 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6733 expand_insn (icode, 6, ops);
6734 return ops[0].value;
6737 /* Return non-zero if a highpart multiply is supported of can be synthisized.
6738 For the benefit of expand_mult_highpart, the return value is 1 for direct,
6739 2 for even/odd widening, and 3 for hi/lo widening. */
6742 can_mult_highpart_p (enum machine_mode mode, bool uns_p)
6744 optab op;
6745 unsigned char *sel;
6746 unsigned i, nunits;
6748 op = uns_p ? umul_highpart_optab : smul_highpart_optab;
6749 if (optab_handler (op, mode) != CODE_FOR_nothing)
6750 return 1;
6752 /* If the mode is an integral vector, synth from widening operations. */
6753 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
6754 return 0;
6756 nunits = GET_MODE_NUNITS (mode);
6757 sel = XALLOCAVEC (unsigned char, nunits);
6759 op = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6760 if (optab_handler (op, mode) != CODE_FOR_nothing)
6762 op = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6763 if (optab_handler (op, mode) != CODE_FOR_nothing)
6765 for (i = 0; i < nunits; ++i)
6766 sel[i] = !BYTES_BIG_ENDIAN + (i & ~1) + ((i & 1) ? nunits : 0);
6767 if (can_vec_perm_p (mode, false, sel))
6768 return 2;
6772 op = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6773 if (optab_handler (op, mode) != CODE_FOR_nothing)
6775 op = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6776 if (optab_handler (op, mode) != CODE_FOR_nothing)
6778 for (i = 0; i < nunits; ++i)
6779 sel[i] = 2 * i + (BYTES_BIG_ENDIAN ? 0 : 1);
6780 if (can_vec_perm_p (mode, false, sel))
6781 return 3;
6785 return 0;
6788 /* Expand a highpart multiply. */
6791 expand_mult_highpart (enum machine_mode mode, rtx op0, rtx op1,
6792 rtx target, bool uns_p)
6794 struct expand_operand eops[3];
6795 enum insn_code icode;
6796 int method, i, nunits;
6797 enum machine_mode wmode;
6798 rtx m1, m2, perm;
6799 optab tab1, tab2;
6800 rtvec v;
6802 method = can_mult_highpart_p (mode, uns_p);
6803 switch (method)
6805 case 0:
6806 return NULL_RTX;
6807 case 1:
6808 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6809 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6810 OPTAB_LIB_WIDEN);
6811 case 2:
6812 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6813 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6814 break;
6815 case 3:
6816 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6817 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6818 if (BYTES_BIG_ENDIAN)
6820 optab t = tab1;
6821 tab1 = tab2;
6822 tab2 = t;
6824 break;
6825 default:
6826 gcc_unreachable ();
6829 icode = optab_handler (tab1, mode);
6830 nunits = GET_MODE_NUNITS (mode);
6831 wmode = insn_data[icode].operand[0].mode;
6832 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
6833 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
6835 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6836 create_input_operand (&eops[1], op0, mode);
6837 create_input_operand (&eops[2], op1, mode);
6838 expand_insn (icode, 3, eops);
6839 m1 = gen_lowpart (mode, eops[0].value);
6841 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6842 create_input_operand (&eops[1], op0, mode);
6843 create_input_operand (&eops[2], op1, mode);
6844 expand_insn (optab_handler (tab2, mode), 3, eops);
6845 m2 = gen_lowpart (mode, eops[0].value);
6847 v = rtvec_alloc (nunits);
6848 if (method == 2)
6850 for (i = 0; i < nunits; ++i)
6851 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
6852 + ((i & 1) ? nunits : 0));
6854 else
6856 for (i = 0; i < nunits; ++i)
6857 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
6859 perm = gen_rtx_CONST_VECTOR (mode, v);
6861 return expand_vec_perm (mode, m1, m2, perm, target);
6864 /* Return true if there is a compare_and_swap pattern. */
6866 bool
6867 can_compare_and_swap_p (enum machine_mode mode, bool allow_libcall)
6869 enum insn_code icode;
6871 /* Check for __atomic_compare_and_swap. */
6872 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
6873 if (icode != CODE_FOR_nothing)
6874 return true;
6876 /* Check for __sync_compare_and_swap. */
6877 icode = optab_handler (sync_compare_and_swap_optab, mode);
6878 if (icode != CODE_FOR_nothing)
6879 return true;
6880 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
6881 return true;
6883 /* No inline compare and swap. */
6884 return false;
6887 /* Return true if an atomic exchange can be performed. */
6889 bool
6890 can_atomic_exchange_p (enum machine_mode mode, bool allow_libcall)
6892 enum insn_code icode;
6894 /* Check for __atomic_exchange. */
6895 icode = direct_optab_handler (atomic_exchange_optab, mode);
6896 if (icode != CODE_FOR_nothing)
6897 return true;
6899 /* Don't check __sync_test_and_set, as on some platforms that
6900 has reduced functionality. Targets that really do support
6901 a proper exchange should simply be updated to the __atomics. */
6903 return can_compare_and_swap_p (mode, allow_libcall);
6907 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
6908 pattern. */
6910 static void
6911 find_cc_set (rtx x, const_rtx pat, void *data)
6913 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
6914 && GET_CODE (pat) == SET)
6916 rtx *p_cc_reg = (rtx *) data;
6917 gcc_assert (!*p_cc_reg);
6918 *p_cc_reg = x;
6922 /* This is a helper function for the other atomic operations. This function
6923 emits a loop that contains SEQ that iterates until a compare-and-swap
6924 operation at the end succeeds. MEM is the memory to be modified. SEQ is
6925 a set of instructions that takes a value from OLD_REG as an input and
6926 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
6927 set to the current contents of MEM. After SEQ, a compare-and-swap will
6928 attempt to update MEM with NEW_REG. The function returns true when the
6929 loop was generated successfully. */
6931 static bool
6932 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
6934 enum machine_mode mode = GET_MODE (mem);
6935 rtx label, cmp_reg, success, oldval;
6937 /* The loop we want to generate looks like
6939 cmp_reg = mem;
6940 label:
6941 old_reg = cmp_reg;
6942 seq;
6943 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
6944 if (success)
6945 goto label;
6947 Note that we only do the plain load from memory once. Subsequent
6948 iterations use the value loaded by the compare-and-swap pattern. */
6950 label = gen_label_rtx ();
6951 cmp_reg = gen_reg_rtx (mode);
6953 emit_move_insn (cmp_reg, mem);
6954 emit_label (label);
6955 emit_move_insn (old_reg, cmp_reg);
6956 if (seq)
6957 emit_insn (seq);
6959 success = NULL_RTX;
6960 oldval = cmp_reg;
6961 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
6962 new_reg, false, MEMMODEL_SEQ_CST,
6963 MEMMODEL_RELAXED))
6964 return false;
6966 if (oldval != cmp_reg)
6967 emit_move_insn (cmp_reg, oldval);
6969 /* Mark this jump predicted not taken. */
6970 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
6971 GET_MODE (success), 1, label, 0);
6972 return true;
6976 /* This function tries to emit an atomic_exchange intruction. VAL is written
6977 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
6978 using TARGET if possible. */
6980 static rtx
6981 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
6983 enum machine_mode mode = GET_MODE (mem);
6984 enum insn_code icode;
6986 /* If the target supports the exchange directly, great. */
6987 icode = direct_optab_handler (atomic_exchange_optab, mode);
6988 if (icode != CODE_FOR_nothing)
6990 struct expand_operand ops[4];
6992 create_output_operand (&ops[0], target, mode);
6993 create_fixed_operand (&ops[1], mem);
6994 /* VAL may have been promoted to a wider mode. Shrink it if so. */
6995 create_convert_operand_to (&ops[2], val, mode, true);
6996 create_integer_operand (&ops[3], model);
6997 if (maybe_expand_insn (icode, 4, ops))
6998 return ops[0].value;
7001 return NULL_RTX;
7004 /* This function tries to implement an atomic exchange operation using
7005 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7006 The previous contents of *MEM are returned, using TARGET if possible.
7007 Since this instructionn is an acquire barrier only, stronger memory
7008 models may require additional barriers to be emitted. */
7010 static rtx
7011 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7012 enum memmodel model)
7014 enum machine_mode mode = GET_MODE (mem);
7015 enum insn_code icode;
7016 rtx last_insn = get_last_insn ();
7018 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7020 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7021 exists, and the memory model is stronger than acquire, add a release
7022 barrier before the instruction. */
7024 if (model == MEMMODEL_SEQ_CST
7025 || model == MEMMODEL_RELEASE
7026 || model == MEMMODEL_ACQ_REL)
7027 expand_mem_thread_fence (model);
7029 if (icode != CODE_FOR_nothing)
7031 struct expand_operand ops[3];
7032 create_output_operand (&ops[0], target, mode);
7033 create_fixed_operand (&ops[1], mem);
7034 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7035 create_convert_operand_to (&ops[2], val, mode, true);
7036 if (maybe_expand_insn (icode, 3, ops))
7037 return ops[0].value;
7040 /* If an external test-and-set libcall is provided, use that instead of
7041 any external compare-and-swap that we might get from the compare-and-
7042 swap-loop expansion later. */
7043 if (!can_compare_and_swap_p (mode, false))
7045 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7046 if (libfunc != NULL)
7048 rtx addr;
7050 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7051 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7052 mode, 2, addr, ptr_mode,
7053 val, mode);
7057 /* If the test_and_set can't be emitted, eliminate any barrier that might
7058 have been emitted. */
7059 delete_insns_since (last_insn);
7060 return NULL_RTX;
7063 /* This function tries to implement an atomic exchange operation using a
7064 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7065 *MEM are returned, using TARGET if possible. No memory model is required
7066 since a compare_and_swap loop is seq-cst. */
7068 static rtx
7069 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7071 enum machine_mode mode = GET_MODE (mem);
7073 if (can_compare_and_swap_p (mode, true))
7075 if (!target || !register_operand (target, mode))
7076 target = gen_reg_rtx (mode);
7077 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7078 val = convert_modes (mode, GET_MODE (val), val, 1);
7079 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7080 return target;
7083 return NULL_RTX;
7086 /* This function tries to implement an atomic test-and-set operation
7087 using the atomic_test_and_set instruction pattern. A boolean value
7088 is returned from the operation, using TARGET if possible. */
7090 #ifndef HAVE_atomic_test_and_set
7091 #define HAVE_atomic_test_and_set 0
7092 #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
7093 #endif
7095 static rtx
7096 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7098 enum machine_mode pat_bool_mode;
7099 struct expand_operand ops[3];
7101 if (!HAVE_atomic_test_and_set)
7102 return NULL_RTX;
7104 /* While we always get QImode from __atomic_test_and_set, we get
7105 other memory modes from __sync_lock_test_and_set. Note that we
7106 use no endian adjustment here. This matches the 4.6 behavior
7107 in the Sparc backend. */
7108 gcc_checking_assert
7109 (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
7110 if (GET_MODE (mem) != QImode)
7111 mem = adjust_address_nv (mem, QImode, 0);
7113 pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
7114 create_output_operand (&ops[0], target, pat_bool_mode);
7115 create_fixed_operand (&ops[1], mem);
7116 create_integer_operand (&ops[2], model);
7118 if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
7119 return ops[0].value;
7120 return NULL_RTX;
7123 /* This function expands the legacy _sync_lock test_and_set operation which is
7124 generally an atomic exchange. Some limited targets only allow the
7125 constant 1 to be stored. This is an ACQUIRE operation.
7127 TARGET is an optional place to stick the return value.
7128 MEM is where VAL is stored. */
7131 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7133 rtx ret;
7135 /* Try an atomic_exchange first. */
7136 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_ACQUIRE);
7137 if (ret)
7138 return ret;
7140 ret = maybe_emit_sync_lock_test_and_set (target, mem, val, MEMMODEL_ACQUIRE);
7141 if (ret)
7142 return ret;
7144 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7145 if (ret)
7146 return ret;
7148 /* If there are no other options, try atomic_test_and_set if the value
7149 being stored is 1. */
7150 if (val == const1_rtx)
7151 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_ACQUIRE);
7153 return ret;
7156 /* This function expands the atomic test_and_set operation:
7157 atomically store a boolean TRUE into MEM and return the previous value.
7159 MEMMODEL is the memory model variant to use.
7160 TARGET is an optional place to stick the return value. */
7163 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7165 enum machine_mode mode = GET_MODE (mem);
7166 rtx ret, trueval, subtarget;
7168 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7169 if (ret)
7170 return ret;
7172 /* Be binary compatible with non-default settings of trueval, and different
7173 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7174 another only has atomic-exchange. */
7175 if (targetm.atomic_test_and_set_trueval == 1)
7177 trueval = const1_rtx;
7178 subtarget = target ? target : gen_reg_rtx (mode);
7180 else
7182 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7183 subtarget = gen_reg_rtx (mode);
7186 /* Try the atomic-exchange optab... */
7187 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7189 /* ... then an atomic-compare-and-swap loop ... */
7190 if (!ret)
7191 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7193 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7194 if (!ret)
7195 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7197 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7198 things with the value 1. Thus we try again without trueval. */
7199 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7200 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7202 /* Failing all else, assume a single threaded environment and simply
7203 perform the operation. */
7204 if (!ret)
7206 emit_move_insn (subtarget, mem);
7207 emit_move_insn (mem, trueval);
7208 ret = subtarget;
7211 /* Recall that have to return a boolean value; rectify if trueval
7212 is not exactly one. */
7213 if (targetm.atomic_test_and_set_trueval != 1)
7214 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7216 return ret;
7219 /* This function expands the atomic exchange operation:
7220 atomically store VAL in MEM and return the previous value in MEM.
7222 MEMMODEL is the memory model variant to use.
7223 TARGET is an optional place to stick the return value. */
7226 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7228 rtx ret;
7230 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7232 /* Next try a compare-and-swap loop for the exchange. */
7233 if (!ret)
7234 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7236 return ret;
7239 /* This function expands the atomic compare exchange operation:
7241 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7242 *PTARGET_OVAL is an optional place to store the old value from memory.
7243 Both target parameters may be NULL to indicate that we do not care about
7244 that return value. Both target parameters are updated on success to
7245 the actual location of the corresponding result.
7247 MEMMODEL is the memory model variant to use.
7249 The return value of the function is true for success. */
7251 bool
7252 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7253 rtx mem, rtx expected, rtx desired,
7254 bool is_weak, enum memmodel succ_model,
7255 enum memmodel fail_model)
7257 enum machine_mode mode = GET_MODE (mem);
7258 struct expand_operand ops[8];
7259 enum insn_code icode;
7260 rtx target_oval, target_bool = NULL_RTX;
7261 rtx libfunc;
7263 /* Load expected into a register for the compare and swap. */
7264 if (MEM_P (expected))
7265 expected = copy_to_reg (expected);
7267 /* Make sure we always have some place to put the return oldval.
7268 Further, make sure that place is distinct from the input expected,
7269 just in case we need that path down below. */
7270 if (ptarget_oval == NULL
7271 || (target_oval = *ptarget_oval) == NULL
7272 || reg_overlap_mentioned_p (expected, target_oval))
7273 target_oval = gen_reg_rtx (mode);
7275 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7276 if (icode != CODE_FOR_nothing)
7278 enum machine_mode bool_mode = insn_data[icode].operand[0].mode;
7280 /* Make sure we always have a place for the bool operand. */
7281 if (ptarget_bool == NULL
7282 || (target_bool = *ptarget_bool) == NULL
7283 || GET_MODE (target_bool) != bool_mode)
7284 target_bool = gen_reg_rtx (bool_mode);
7286 /* Emit the compare_and_swap. */
7287 create_output_operand (&ops[0], target_bool, bool_mode);
7288 create_output_operand (&ops[1], target_oval, mode);
7289 create_fixed_operand (&ops[2], mem);
7290 create_convert_operand_to (&ops[3], expected, mode, true);
7291 create_convert_operand_to (&ops[4], desired, mode, true);
7292 create_integer_operand (&ops[5], is_weak);
7293 create_integer_operand (&ops[6], succ_model);
7294 create_integer_operand (&ops[7], fail_model);
7295 expand_insn (icode, 8, ops);
7297 /* Return success/failure. */
7298 target_bool = ops[0].value;
7299 target_oval = ops[1].value;
7300 goto success;
7303 /* Otherwise fall back to the original __sync_val_compare_and_swap
7304 which is always seq-cst. */
7305 icode = optab_handler (sync_compare_and_swap_optab, mode);
7306 if (icode != CODE_FOR_nothing)
7308 rtx cc_reg;
7310 create_output_operand (&ops[0], target_oval, mode);
7311 create_fixed_operand (&ops[1], mem);
7312 create_convert_operand_to (&ops[2], expected, mode, true);
7313 create_convert_operand_to (&ops[3], desired, mode, true);
7314 if (!maybe_expand_insn (icode, 4, ops))
7315 return false;
7317 target_oval = ops[0].value;
7319 /* If the caller isn't interested in the boolean return value,
7320 skip the computation of it. */
7321 if (ptarget_bool == NULL)
7322 goto success;
7324 /* Otherwise, work out if the compare-and-swap succeeded. */
7325 cc_reg = NULL_RTX;
7326 if (have_insn_for (COMPARE, CCmode))
7327 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7328 if (cc_reg)
7330 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7331 const0_rtx, VOIDmode, 0, 1);
7332 goto success;
7334 goto success_bool_from_val;
7337 /* Also check for library support for __sync_val_compare_and_swap. */
7338 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7339 if (libfunc != NULL)
7341 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7342 target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7343 mode, 3, addr, ptr_mode,
7344 expected, mode, desired, mode);
7346 /* Compute the boolean return value only if requested. */
7347 if (ptarget_bool)
7348 goto success_bool_from_val;
7349 else
7350 goto success;
7353 /* Failure. */
7354 return false;
7356 success_bool_from_val:
7357 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7358 expected, VOIDmode, 1, 1);
7359 success:
7360 /* Make sure that the oval output winds up where the caller asked. */
7361 if (ptarget_oval)
7362 *ptarget_oval = target_oval;
7363 if (ptarget_bool)
7364 *ptarget_bool = target_bool;
7365 return true;
7368 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7370 static void
7371 expand_asm_memory_barrier (void)
7373 rtx asm_op, clob;
7375 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7376 rtvec_alloc (0), rtvec_alloc (0),
7377 rtvec_alloc (0), UNKNOWN_LOCATION);
7378 MEM_VOLATILE_P (asm_op) = 1;
7380 clob = gen_rtx_SCRATCH (VOIDmode);
7381 clob = gen_rtx_MEM (BLKmode, clob);
7382 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7384 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7387 /* This routine will either emit the mem_thread_fence pattern or issue a
7388 sync_synchronize to generate a fence for memory model MEMMODEL. */
7390 #ifndef HAVE_mem_thread_fence
7391 # define HAVE_mem_thread_fence 0
7392 # define gen_mem_thread_fence(x) (gcc_unreachable (), NULL_RTX)
7393 #endif
7394 #ifndef HAVE_memory_barrier
7395 # define HAVE_memory_barrier 0
7396 # define gen_memory_barrier() (gcc_unreachable (), NULL_RTX)
7397 #endif
7399 void
7400 expand_mem_thread_fence (enum memmodel model)
7402 if (HAVE_mem_thread_fence)
7403 emit_insn (gen_mem_thread_fence (GEN_INT (model)));
7404 else if (model != MEMMODEL_RELAXED)
7406 if (HAVE_memory_barrier)
7407 emit_insn (gen_memory_barrier ());
7408 else if (synchronize_libfunc != NULL_RTX)
7409 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7410 else
7411 expand_asm_memory_barrier ();
7415 /* This routine will either emit the mem_signal_fence pattern or issue a
7416 sync_synchronize to generate a fence for memory model MEMMODEL. */
7418 #ifndef HAVE_mem_signal_fence
7419 # define HAVE_mem_signal_fence 0
7420 # define gen_mem_signal_fence(x) (gcc_unreachable (), NULL_RTX)
7421 #endif
7423 void
7424 expand_mem_signal_fence (enum memmodel model)
7426 if (HAVE_mem_signal_fence)
7427 emit_insn (gen_mem_signal_fence (GEN_INT (model)));
7428 else if (model != MEMMODEL_RELAXED)
7430 /* By default targets are coherent between a thread and the signal
7431 handler running on the same thread. Thus this really becomes a
7432 compiler barrier, in that stores must not be sunk past
7433 (or raised above) a given point. */
7434 expand_asm_memory_barrier ();
7438 /* This function expands the atomic load operation:
7439 return the atomically loaded value in MEM.
7441 MEMMODEL is the memory model variant to use.
7442 TARGET is an option place to stick the return value. */
7445 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7447 enum machine_mode mode = GET_MODE (mem);
7448 enum insn_code icode;
7450 /* If the target supports the load directly, great. */
7451 icode = direct_optab_handler (atomic_load_optab, mode);
7452 if (icode != CODE_FOR_nothing)
7454 struct expand_operand ops[3];
7456 create_output_operand (&ops[0], target, mode);
7457 create_fixed_operand (&ops[1], mem);
7458 create_integer_operand (&ops[2], model);
7459 if (maybe_expand_insn (icode, 3, ops))
7460 return ops[0].value;
7463 /* If the size of the object is greater than word size on this target,
7464 then we assume that a load will not be atomic. */
7465 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7467 /* Issue val = compare_and_swap (mem, 0, 0).
7468 This may cause the occasional harmless store of 0 when the value is
7469 already 0, but it seems to be OK according to the standards guys. */
7470 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7471 const0_rtx, false, model, model))
7472 return target;
7473 else
7474 /* Otherwise there is no atomic load, leave the library call. */
7475 return NULL_RTX;
7478 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7479 if (!target || target == const0_rtx)
7480 target = gen_reg_rtx (mode);
7482 /* Emit the appropriate barrier before the load. */
7483 expand_mem_thread_fence (model);
7485 emit_move_insn (target, mem);
7487 /* For SEQ_CST, also emit a barrier after the load. */
7488 if (model == MEMMODEL_SEQ_CST)
7489 expand_mem_thread_fence (model);
7491 return target;
7494 /* This function expands the atomic store operation:
7495 Atomically store VAL in MEM.
7496 MEMMODEL is the memory model variant to use.
7497 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7498 function returns const0_rtx if a pattern was emitted. */
7501 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7503 enum machine_mode mode = GET_MODE (mem);
7504 enum insn_code icode;
7505 struct expand_operand ops[3];
7507 /* If the target supports the store directly, great. */
7508 icode = direct_optab_handler (atomic_store_optab, mode);
7509 if (icode != CODE_FOR_nothing)
7511 create_fixed_operand (&ops[0], mem);
7512 create_input_operand (&ops[1], val, mode);
7513 create_integer_operand (&ops[2], model);
7514 if (maybe_expand_insn (icode, 3, ops))
7515 return const0_rtx;
7518 /* If using __sync_lock_release is a viable alternative, try it. */
7519 if (use_release)
7521 icode = direct_optab_handler (sync_lock_release_optab, mode);
7522 if (icode != CODE_FOR_nothing)
7524 create_fixed_operand (&ops[0], mem);
7525 create_input_operand (&ops[1], const0_rtx, mode);
7526 if (maybe_expand_insn (icode, 2, ops))
7528 /* lock_release is only a release barrier. */
7529 if (model == MEMMODEL_SEQ_CST)
7530 expand_mem_thread_fence (model);
7531 return const0_rtx;
7536 /* If the size of the object is greater than word size on this target,
7537 a default store will not be atomic, Try a mem_exchange and throw away
7538 the result. If that doesn't work, don't do anything. */
7539 if (GET_MODE_PRECISION(mode) > BITS_PER_WORD)
7541 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7542 if (!target)
7543 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7544 if (target)
7545 return const0_rtx;
7546 else
7547 return NULL_RTX;
7550 /* If there is no mem_store, default to a move with barriers */
7551 if (model == MEMMODEL_SEQ_CST || model == MEMMODEL_RELEASE)
7552 expand_mem_thread_fence (model);
7554 emit_move_insn (mem, val);
7556 /* For SEQ_CST, also emit a barrier after the load. */
7557 if (model == MEMMODEL_SEQ_CST)
7558 expand_mem_thread_fence (model);
7560 return const0_rtx;
7564 /* Structure containing the pointers and values required to process the
7565 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7567 struct atomic_op_functions
7569 direct_optab mem_fetch_before;
7570 direct_optab mem_fetch_after;
7571 direct_optab mem_no_result;
7572 optab fetch_before;
7573 optab fetch_after;
7574 direct_optab no_result;
7575 enum rtx_code reverse_code;
7579 /* Fill in structure pointed to by OP with the various optab entries for an
7580 operation of type CODE. */
7582 static void
7583 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7585 gcc_assert (op!= NULL);
7587 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7588 in the source code during compilation, and the optab entries are not
7589 computable until runtime. Fill in the values at runtime. */
7590 switch (code)
7592 case PLUS:
7593 op->mem_fetch_before = atomic_fetch_add_optab;
7594 op->mem_fetch_after = atomic_add_fetch_optab;
7595 op->mem_no_result = atomic_add_optab;
7596 op->fetch_before = sync_old_add_optab;
7597 op->fetch_after = sync_new_add_optab;
7598 op->no_result = sync_add_optab;
7599 op->reverse_code = MINUS;
7600 break;
7601 case MINUS:
7602 op->mem_fetch_before = atomic_fetch_sub_optab;
7603 op->mem_fetch_after = atomic_sub_fetch_optab;
7604 op->mem_no_result = atomic_sub_optab;
7605 op->fetch_before = sync_old_sub_optab;
7606 op->fetch_after = sync_new_sub_optab;
7607 op->no_result = sync_sub_optab;
7608 op->reverse_code = PLUS;
7609 break;
7610 case XOR:
7611 op->mem_fetch_before = atomic_fetch_xor_optab;
7612 op->mem_fetch_after = atomic_xor_fetch_optab;
7613 op->mem_no_result = atomic_xor_optab;
7614 op->fetch_before = sync_old_xor_optab;
7615 op->fetch_after = sync_new_xor_optab;
7616 op->no_result = sync_xor_optab;
7617 op->reverse_code = XOR;
7618 break;
7619 case AND:
7620 op->mem_fetch_before = atomic_fetch_and_optab;
7621 op->mem_fetch_after = atomic_and_fetch_optab;
7622 op->mem_no_result = atomic_and_optab;
7623 op->fetch_before = sync_old_and_optab;
7624 op->fetch_after = sync_new_and_optab;
7625 op->no_result = sync_and_optab;
7626 op->reverse_code = UNKNOWN;
7627 break;
7628 case IOR:
7629 op->mem_fetch_before = atomic_fetch_or_optab;
7630 op->mem_fetch_after = atomic_or_fetch_optab;
7631 op->mem_no_result = atomic_or_optab;
7632 op->fetch_before = sync_old_ior_optab;
7633 op->fetch_after = sync_new_ior_optab;
7634 op->no_result = sync_ior_optab;
7635 op->reverse_code = UNKNOWN;
7636 break;
7637 case NOT:
7638 op->mem_fetch_before = atomic_fetch_nand_optab;
7639 op->mem_fetch_after = atomic_nand_fetch_optab;
7640 op->mem_no_result = atomic_nand_optab;
7641 op->fetch_before = sync_old_nand_optab;
7642 op->fetch_after = sync_new_nand_optab;
7643 op->no_result = sync_nand_optab;
7644 op->reverse_code = UNKNOWN;
7645 break;
7646 default:
7647 gcc_unreachable ();
7651 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7652 using memory order MODEL. If AFTER is true the operation needs to return
7653 the value of *MEM after the operation, otherwise the previous value.
7654 TARGET is an optional place to place the result. The result is unused if
7655 it is const0_rtx.
7656 Return the result if there is a better sequence, otherwise NULL_RTX. */
7658 static rtx
7659 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7660 enum memmodel model, bool after)
7662 /* If the value is prefetched, or not used, it may be possible to replace
7663 the sequence with a native exchange operation. */
7664 if (!after || target == const0_rtx)
7666 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7667 if (code == AND && val == const0_rtx)
7669 if (target == const0_rtx)
7670 target = gen_reg_rtx (GET_MODE (mem));
7671 return maybe_emit_atomic_exchange (target, mem, val, model);
7674 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7675 if (code == IOR && val == constm1_rtx)
7677 if (target == const0_rtx)
7678 target = gen_reg_rtx (GET_MODE (mem));
7679 return maybe_emit_atomic_exchange (target, mem, val, model);
7683 return NULL_RTX;
7686 /* Try to emit an instruction for a specific operation varaition.
7687 OPTAB contains the OP functions.
7688 TARGET is an optional place to return the result. const0_rtx means unused.
7689 MEM is the memory location to operate on.
7690 VAL is the value to use in the operation.
7691 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7692 MODEL is the memory model, if used.
7693 AFTER is true if the returned result is the value after the operation. */
7695 static rtx
7696 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7697 rtx val, bool use_memmodel, enum memmodel model, bool after)
7699 enum machine_mode mode = GET_MODE (mem);
7700 struct expand_operand ops[4];
7701 enum insn_code icode;
7702 int op_counter = 0;
7703 int num_ops;
7705 /* Check to see if there is a result returned. */
7706 if (target == const0_rtx)
7708 if (use_memmodel)
7710 icode = direct_optab_handler (optab->mem_no_result, mode);
7711 create_integer_operand (&ops[2], model);
7712 num_ops = 3;
7714 else
7716 icode = direct_optab_handler (optab->no_result, mode);
7717 num_ops = 2;
7720 /* Otherwise, we need to generate a result. */
7721 else
7723 if (use_memmodel)
7725 icode = direct_optab_handler (after ? optab->mem_fetch_after
7726 : optab->mem_fetch_before, mode);
7727 create_integer_operand (&ops[3], model);
7728 num_ops = 4;
7730 else
7732 icode = optab_handler (after ? optab->fetch_after
7733 : optab->fetch_before, mode);
7734 num_ops = 3;
7736 create_output_operand (&ops[op_counter++], target, mode);
7738 if (icode == CODE_FOR_nothing)
7739 return NULL_RTX;
7741 create_fixed_operand (&ops[op_counter++], mem);
7742 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7743 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7745 if (maybe_expand_insn (icode, num_ops, ops))
7746 return (target == const0_rtx ? const0_rtx : ops[0].value);
7748 return NULL_RTX;
7752 /* This function expands an atomic fetch_OP or OP_fetch operation:
7753 TARGET is an option place to stick the return value. const0_rtx indicates
7754 the result is unused.
7755 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7756 CODE is the operation being performed (OP)
7757 MEMMODEL is the memory model variant to use.
7758 AFTER is true to return the result of the operation (OP_fetch).
7759 AFTER is false to return the value before the operation (fetch_OP).
7761 This function will *only* generate instructions if there is a direct
7762 optab. No compare and swap loops or libcalls will be generated. */
7764 static rtx
7765 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7766 enum rtx_code code, enum memmodel model,
7767 bool after)
7769 enum machine_mode mode = GET_MODE (mem);
7770 struct atomic_op_functions optab;
7771 rtx result;
7772 bool unused_result = (target == const0_rtx);
7774 get_atomic_op_for_code (&optab, code);
7776 /* Check to see if there are any better instructions. */
7777 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7778 if (result)
7779 return result;
7781 /* Check for the case where the result isn't used and try those patterns. */
7782 if (unused_result)
7784 /* Try the memory model variant first. */
7785 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7786 if (result)
7787 return result;
7789 /* Next try the old style withuot a memory model. */
7790 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7791 if (result)
7792 return result;
7794 /* There is no no-result pattern, so try patterns with a result. */
7795 target = NULL_RTX;
7798 /* Try the __atomic version. */
7799 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
7800 if (result)
7801 return result;
7803 /* Try the older __sync version. */
7804 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
7805 if (result)
7806 return result;
7808 /* If the fetch value can be calculated from the other variation of fetch,
7809 try that operation. */
7810 if (after || unused_result || optab.reverse_code != UNKNOWN)
7812 /* Try the __atomic version, then the older __sync version. */
7813 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
7814 if (!result)
7815 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
7817 if (result)
7819 /* If the result isn't used, no need to do compensation code. */
7820 if (unused_result)
7821 return result;
7823 /* Issue compensation code. Fetch_after == fetch_before OP val.
7824 Fetch_before == after REVERSE_OP val. */
7825 if (!after)
7826 code = optab.reverse_code;
7827 if (code == NOT)
7829 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
7830 true, OPTAB_LIB_WIDEN);
7831 result = expand_simple_unop (mode, NOT, result, target, true);
7833 else
7834 result = expand_simple_binop (mode, code, result, val, target,
7835 true, OPTAB_LIB_WIDEN);
7836 return result;
7840 /* No direct opcode can be generated. */
7841 return NULL_RTX;
7846 /* This function expands an atomic fetch_OP or OP_fetch operation:
7847 TARGET is an option place to stick the return value. const0_rtx indicates
7848 the result is unused.
7849 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7850 CODE is the operation being performed (OP)
7851 MEMMODEL is the memory model variant to use.
7852 AFTER is true to return the result of the operation (OP_fetch).
7853 AFTER is false to return the value before the operation (fetch_OP). */
7855 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7856 enum memmodel model, bool after)
7858 enum machine_mode mode = GET_MODE (mem);
7859 rtx result;
7860 bool unused_result = (target == const0_rtx);
7862 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
7863 after);
7865 if (result)
7866 return result;
7868 /* Add/sub can be implemented by doing the reverse operation with -(val). */
7869 if (code == PLUS || code == MINUS)
7871 rtx tmp;
7872 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
7874 start_sequence ();
7875 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
7876 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
7877 model, after);
7878 if (result)
7880 /* PLUS worked so emit the insns and return. */
7881 tmp = get_insns ();
7882 end_sequence ();
7883 emit_insn (tmp);
7884 return result;
7887 /* PLUS did not work, so throw away the negation code and continue. */
7888 end_sequence ();
7891 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
7892 if (!can_compare_and_swap_p (mode, false))
7894 rtx libfunc;
7895 bool fixup = false;
7896 enum rtx_code orig_code = code;
7897 struct atomic_op_functions optab;
7899 get_atomic_op_for_code (&optab, code);
7900 libfunc = optab_libfunc (after ? optab.fetch_after
7901 : optab.fetch_before, mode);
7902 if (libfunc == NULL
7903 && (after || unused_result || optab.reverse_code != UNKNOWN))
7905 fixup = true;
7906 if (!after)
7907 code = optab.reverse_code;
7908 libfunc = optab_libfunc (after ? optab.fetch_before
7909 : optab.fetch_after, mode);
7911 if (libfunc != NULL)
7913 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7914 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
7915 2, addr, ptr_mode, val, mode);
7917 if (!unused_result && fixup)
7918 result = expand_simple_binop (mode, code, result, val, target,
7919 true, OPTAB_LIB_WIDEN);
7920 return result;
7923 /* We need the original code for any further attempts. */
7924 code = orig_code;
7927 /* If nothing else has succeeded, default to a compare and swap loop. */
7928 if (can_compare_and_swap_p (mode, true))
7930 rtx insn;
7931 rtx t0 = gen_reg_rtx (mode), t1;
7933 start_sequence ();
7935 /* If the result is used, get a register for it. */
7936 if (!unused_result)
7938 if (!target || !register_operand (target, mode))
7939 target = gen_reg_rtx (mode);
7940 /* If fetch_before, copy the value now. */
7941 if (!after)
7942 emit_move_insn (target, t0);
7944 else
7945 target = const0_rtx;
7947 t1 = t0;
7948 if (code == NOT)
7950 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
7951 true, OPTAB_LIB_WIDEN);
7952 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
7954 else
7955 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
7956 OPTAB_LIB_WIDEN);
7958 /* For after, copy the value now. */
7959 if (!unused_result && after)
7960 emit_move_insn (target, t1);
7961 insn = get_insns ();
7962 end_sequence ();
7964 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
7965 return target;
7968 return NULL_RTX;
7971 /* Return true if OPERAND is suitable for operand number OPNO of
7972 instruction ICODE. */
7974 bool
7975 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
7977 return (!insn_data[(int) icode].operand[opno].predicate
7978 || (insn_data[(int) icode].operand[opno].predicate
7979 (operand, insn_data[(int) icode].operand[opno].mode)));
7982 /* TARGET is a target of a multiword operation that we are going to
7983 implement as a series of word-mode operations. Return true if
7984 TARGET is suitable for this purpose. */
7986 bool
7987 valid_multiword_target_p (rtx target)
7989 enum machine_mode mode;
7990 int i;
7992 mode = GET_MODE (target);
7993 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
7994 if (!validate_subreg (word_mode, mode, target, i))
7995 return false;
7996 return true;
7999 /* Like maybe_legitimize_operand, but do not change the code of the
8000 current rtx value. */
8002 static bool
8003 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8004 struct expand_operand *op)
8006 /* See if the operand matches in its current form. */
8007 if (insn_operand_matches (icode, opno, op->value))
8008 return true;
8010 /* If the operand is a memory whose address has no side effects,
8011 try forcing the address into a non-virtual pseudo register.
8012 The check for side effects is important because copy_to_mode_reg
8013 cannot handle things like auto-modified addresses. */
8014 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8016 rtx addr, mem;
8018 mem = op->value;
8019 addr = XEXP (mem, 0);
8020 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8021 && !side_effects_p (addr))
8023 rtx last;
8024 enum machine_mode mode;
8026 last = get_last_insn ();
8027 mode = get_address_mode (mem);
8028 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8029 if (insn_operand_matches (icode, opno, mem))
8031 op->value = mem;
8032 return true;
8034 delete_insns_since (last);
8038 return false;
8041 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8042 on success, storing the new operand value back in OP. */
8044 static bool
8045 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8046 struct expand_operand *op)
8048 enum machine_mode mode, imode;
8049 bool old_volatile_ok, result;
8051 mode = op->mode;
8052 switch (op->type)
8054 case EXPAND_FIXED:
8055 old_volatile_ok = volatile_ok;
8056 volatile_ok = true;
8057 result = maybe_legitimize_operand_same_code (icode, opno, op);
8058 volatile_ok = old_volatile_ok;
8059 return result;
8061 case EXPAND_OUTPUT:
8062 gcc_assert (mode != VOIDmode);
8063 if (op->value
8064 && op->value != const0_rtx
8065 && GET_MODE (op->value) == mode
8066 && maybe_legitimize_operand_same_code (icode, opno, op))
8067 return true;
8069 op->value = gen_reg_rtx (mode);
8070 break;
8072 case EXPAND_INPUT:
8073 input:
8074 gcc_assert (mode != VOIDmode);
8075 gcc_assert (GET_MODE (op->value) == VOIDmode
8076 || GET_MODE (op->value) == mode);
8077 if (maybe_legitimize_operand_same_code (icode, opno, op))
8078 return true;
8080 op->value = copy_to_mode_reg (mode, op->value);
8081 break;
8083 case EXPAND_CONVERT_TO:
8084 gcc_assert (mode != VOIDmode);
8085 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8086 goto input;
8088 case EXPAND_CONVERT_FROM:
8089 if (GET_MODE (op->value) != VOIDmode)
8090 mode = GET_MODE (op->value);
8091 else
8092 /* The caller must tell us what mode this value has. */
8093 gcc_assert (mode != VOIDmode);
8095 imode = insn_data[(int) icode].operand[opno].mode;
8096 if (imode != VOIDmode && imode != mode)
8098 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8099 mode = imode;
8101 goto input;
8103 case EXPAND_ADDRESS:
8104 gcc_assert (mode != VOIDmode);
8105 op->value = convert_memory_address (mode, op->value);
8106 goto input;
8108 case EXPAND_INTEGER:
8109 mode = insn_data[(int) icode].operand[opno].mode;
8110 if (mode != VOIDmode && const_int_operand (op->value, mode))
8111 goto input;
8112 break;
8114 return insn_operand_matches (icode, opno, op->value);
8117 /* Make OP describe an input operand that should have the same value
8118 as VALUE, after any mode conversion that the target might request.
8119 TYPE is the type of VALUE. */
8121 void
8122 create_convert_operand_from_type (struct expand_operand *op,
8123 rtx value, tree type)
8125 create_convert_operand_from (op, value, TYPE_MODE (type),
8126 TYPE_UNSIGNED (type));
8129 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8130 of instruction ICODE. Return true on success, leaving the new operand
8131 values in the OPS themselves. Emit no code on failure. */
8133 bool
8134 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8135 unsigned int nops, struct expand_operand *ops)
8137 rtx last;
8138 unsigned int i;
8140 last = get_last_insn ();
8141 for (i = 0; i < nops; i++)
8142 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8144 delete_insns_since (last);
8145 return false;
8147 return true;
8150 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8151 as its operands. Return the instruction pattern on success,
8152 and emit any necessary set-up code. Return null and emit no
8153 code on failure. */
8156 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8157 struct expand_operand *ops)
8159 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8160 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8161 return NULL_RTX;
8163 switch (nops)
8165 case 1:
8166 return GEN_FCN (icode) (ops[0].value);
8167 case 2:
8168 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8169 case 3:
8170 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8171 case 4:
8172 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8173 ops[3].value);
8174 case 5:
8175 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8176 ops[3].value, ops[4].value);
8177 case 6:
8178 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8179 ops[3].value, ops[4].value, ops[5].value);
8180 case 7:
8181 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8182 ops[3].value, ops[4].value, ops[5].value,
8183 ops[6].value);
8184 case 8:
8185 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8186 ops[3].value, ops[4].value, ops[5].value,
8187 ops[6].value, ops[7].value);
8189 gcc_unreachable ();
8192 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8193 as its operands. Return true on success and emit no code on failure. */
8195 bool
8196 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8197 struct expand_operand *ops)
8199 rtx pat = maybe_gen_insn (icode, nops, ops);
8200 if (pat)
8202 emit_insn (pat);
8203 return true;
8205 return false;
8208 /* Like maybe_expand_insn, but for jumps. */
8210 bool
8211 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8212 struct expand_operand *ops)
8214 rtx pat = maybe_gen_insn (icode, nops, ops);
8215 if (pat)
8217 emit_jump_insn (pat);
8218 return true;
8220 return false;
8223 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8224 as its operands. */
8226 void
8227 expand_insn (enum insn_code icode, unsigned int nops,
8228 struct expand_operand *ops)
8230 if (!maybe_expand_insn (icode, nops, ops))
8231 gcc_unreachable ();
8234 /* Like expand_insn, but for jumps. */
8236 void
8237 expand_jump_insn (enum insn_code icode, unsigned int nops,
8238 struct expand_operand *ops)
8240 if (!maybe_expand_jump_insn (icode, nops, ops))
8241 gcc_unreachable ();
8244 #include "gt-optabs.h"