gcc/
[official-gcc.git] / gcc / optabs.c
blob163d57dae3dba6cc398b7f5fdc139fae2a828f5d
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "diagnostic-core.h"
27 /* Include insn-config.h before expr.h so that HAVE_conditional_move
28 is properly defined. */
29 #include "insn-config.h"
30 #include "rtl.h"
31 #include "alias.h"
32 #include "symtab.h"
33 #include "tree.h"
34 #include "tree-hasher.h"
35 #include "stor-layout.h"
36 #include "stringpool.h"
37 #include "varasm.h"
38 #include "tm_p.h"
39 #include "flags.h"
40 #include "hard-reg-set.h"
41 #include "function.h"
42 #include "except.h"
43 #include "expmed.h"
44 #include "dojump.h"
45 #include "explow.h"
46 #include "calls.h"
47 #include "emit-rtl.h"
48 #include "stmt.h"
49 #include "expr.h"
50 #include "insn-codes.h"
51 #include "optabs.h"
52 #include "libfuncs.h"
53 #include "recog.h"
54 #include "reload.h"
55 #include "predict.h"
56 #include "dominance.h"
57 #include "cfg.h"
58 #include "basic-block.h"
59 #include "target.h"
61 struct target_optabs default_target_optabs;
62 struct target_libfuncs default_target_libfuncs;
63 struct target_optabs *this_fn_optabs = &default_target_optabs;
64 #if SWITCHABLE_TARGET
65 struct target_optabs *this_target_optabs = &default_target_optabs;
66 struct target_libfuncs *this_target_libfuncs = &default_target_libfuncs;
67 #endif
69 #define libfunc_hash \
70 (this_target_libfuncs->x_libfunc_hash)
72 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
73 machine_mode *);
74 static rtx expand_unop_direct (machine_mode, optab, rtx, rtx, int);
75 static void emit_libcall_block_1 (rtx_insn *, rtx, rtx, rtx, bool);
77 /* Debug facility for use in GDB. */
78 void debug_optab_libfuncs (void);
80 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
81 #if ENABLE_DECIMAL_BID_FORMAT
82 #define DECIMAL_PREFIX "bid_"
83 #else
84 #define DECIMAL_PREFIX "dpd_"
85 #endif
87 /* Used for libfunc_hash. */
89 hashval_t
90 libfunc_hasher::hash (libfunc_entry *e)
92 return ((e->mode1 + e->mode2 * NUM_MACHINE_MODES) ^ e->op);
95 /* Used for libfunc_hash. */
97 bool
98 libfunc_hasher::equal (libfunc_entry *e1, libfunc_entry *e2)
100 return e1->op == e2->op && e1->mode1 == e2->mode1 && e1->mode2 == e2->mode2;
103 /* Return libfunc corresponding operation defined by OPTAB converting
104 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
105 if no libfunc is available. */
107 convert_optab_libfunc (convert_optab optab, machine_mode mode1,
108 machine_mode mode2)
110 struct libfunc_entry e;
111 struct libfunc_entry **slot;
113 /* ??? This ought to be an assert, but not all of the places
114 that we expand optabs know about the optabs that got moved
115 to being direct. */
116 if (!(optab >= FIRST_CONV_OPTAB && optab <= LAST_CONVLIB_OPTAB))
117 return NULL_RTX;
119 e.op = optab;
120 e.mode1 = mode1;
121 e.mode2 = mode2;
122 slot = libfunc_hash->find_slot (&e, NO_INSERT);
123 if (!slot)
125 const struct convert_optab_libcall_d *d
126 = &convlib_def[optab - FIRST_CONV_OPTAB];
128 if (d->libcall_gen == NULL)
129 return NULL;
131 d->libcall_gen (optab, d->libcall_basename, mode1, mode2);
132 slot = libfunc_hash->find_slot (&e, NO_INSERT);
133 if (!slot)
134 return NULL;
136 return (*slot)->libfunc;
139 /* Return libfunc corresponding operation defined by OPTAB in MODE.
140 Trigger lazy initialization if needed, return NULL if no libfunc is
141 available. */
143 optab_libfunc (optab optab, machine_mode mode)
145 struct libfunc_entry e;
146 struct libfunc_entry **slot;
148 /* ??? This ought to be an assert, but not all of the places
149 that we expand optabs know about the optabs that got moved
150 to being direct. */
151 if (!(optab >= FIRST_NORM_OPTAB && optab <= LAST_NORMLIB_OPTAB))
152 return NULL_RTX;
154 e.op = optab;
155 e.mode1 = mode;
156 e.mode2 = VOIDmode;
157 slot = libfunc_hash->find_slot (&e, NO_INSERT);
158 if (!slot)
160 const struct optab_libcall_d *d
161 = &normlib_def[optab - FIRST_NORM_OPTAB];
163 if (d->libcall_gen == NULL)
164 return NULL;
166 d->libcall_gen (optab, d->libcall_basename, d->libcall_suffix, mode);
167 slot = libfunc_hash->find_slot (&e, NO_INSERT);
168 if (!slot)
169 return NULL;
171 return (*slot)->libfunc;
175 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
176 the result of operation CODE applied to OP0 (and OP1 if it is a binary
177 operation).
179 If the last insn does not set TARGET, don't do anything, but return 1.
181 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
182 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
183 try again, ensuring that TARGET is not one of the operands. */
185 static int
186 add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
188 rtx_insn *last_insn;
189 rtx set;
190 rtx note;
192 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
194 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
195 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
196 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
197 && GET_RTX_CLASS (code) != RTX_COMPARE
198 && GET_RTX_CLASS (code) != RTX_UNARY)
199 return 1;
201 if (GET_CODE (target) == ZERO_EXTRACT)
202 return 1;
204 for (last_insn = insns;
205 NEXT_INSN (last_insn) != NULL_RTX;
206 last_insn = NEXT_INSN (last_insn))
209 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
210 a value changing in the insn, so the note would be invalid for CSE. */
211 if (reg_overlap_mentioned_p (target, op0)
212 || (op1 && reg_overlap_mentioned_p (target, op1)))
214 if (MEM_P (target)
215 && (rtx_equal_p (target, op0)
216 || (op1 && rtx_equal_p (target, op1))))
218 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
219 over expanding it as temp = MEM op X, MEM = temp. If the target
220 supports MEM = MEM op X instructions, it is sometimes too hard
221 to reconstruct that form later, especially if X is also a memory,
222 and due to multiple occurrences of addresses the address might
223 be forced into register unnecessarily.
224 Note that not emitting the REG_EQUIV note might inhibit
225 CSE in some cases. */
226 set = single_set (last_insn);
227 if (set
228 && GET_CODE (SET_SRC (set)) == code
229 && MEM_P (SET_DEST (set))
230 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
231 || (op1 && rtx_equal_p (SET_DEST (set),
232 XEXP (SET_SRC (set), 1)))))
233 return 1;
235 return 0;
238 set = set_for_reg_notes (last_insn);
239 if (set == NULL_RTX)
240 return 1;
242 if (! rtx_equal_p (SET_DEST (set), target)
243 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
244 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
245 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
246 return 1;
248 if (GET_RTX_CLASS (code) == RTX_UNARY)
249 switch (code)
251 case FFS:
252 case CLZ:
253 case CTZ:
254 case CLRSB:
255 case POPCOUNT:
256 case PARITY:
257 case BSWAP:
258 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
260 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
261 if (GET_MODE_SIZE (GET_MODE (op0))
262 > GET_MODE_SIZE (GET_MODE (target)))
263 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
264 note, GET_MODE (op0));
265 else
266 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
267 note, GET_MODE (op0));
268 break;
270 /* FALLTHRU */
271 default:
272 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
273 break;
275 else
276 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
278 set_unique_reg_note (last_insn, REG_EQUAL, note);
280 return 1;
283 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
284 for a widening operation would be. In most cases this would be OP0, but if
285 that's a constant it'll be VOIDmode, which isn't useful. */
287 static machine_mode
288 widened_mode (machine_mode to_mode, rtx op0, rtx op1)
290 machine_mode m0 = GET_MODE (op0);
291 machine_mode m1 = GET_MODE (op1);
292 machine_mode result;
294 if (m0 == VOIDmode && m1 == VOIDmode)
295 return to_mode;
296 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
297 result = m1;
298 else
299 result = m0;
301 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
302 return to_mode;
304 return result;
307 /* Like optab_handler, but for widening_operations that have a
308 TO_MODE and a FROM_MODE. */
310 enum insn_code
311 widening_optab_handler (optab op, machine_mode to_mode,
312 machine_mode from_mode)
314 unsigned scode = (op << 16) | to_mode;
315 if (to_mode != from_mode && from_mode != VOIDmode)
317 /* ??? Why does find_widening_optab_handler_and_mode attempt to
318 widen things that can't be widened? E.g. add_optab... */
319 if (op > LAST_CONV_OPTAB)
320 return CODE_FOR_nothing;
321 scode |= from_mode << 8;
323 return raw_optab_handler (scode);
326 /* Find a widening optab even if it doesn't widen as much as we want.
327 E.g. if from_mode is HImode, and to_mode is DImode, and there is no
328 direct HI->SI insn, then return SI->DI, if that exists.
329 If PERMIT_NON_WIDENING is non-zero then this can be used with
330 non-widening optabs also. */
332 enum insn_code
333 find_widening_optab_handler_and_mode (optab op, machine_mode to_mode,
334 machine_mode from_mode,
335 int permit_non_widening,
336 machine_mode *found_mode)
338 for (; (permit_non_widening || from_mode != to_mode)
339 && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
340 && from_mode != VOIDmode;
341 from_mode = GET_MODE_WIDER_MODE (from_mode))
343 enum insn_code handler = widening_optab_handler (op, to_mode,
344 from_mode);
346 if (handler != CODE_FOR_nothing)
348 if (found_mode)
349 *found_mode = from_mode;
350 return handler;
354 return CODE_FOR_nothing;
357 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
358 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
359 not actually do a sign-extend or zero-extend, but can leave the
360 higher-order bits of the result rtx undefined, for example, in the case
361 of logical operations, but not right shifts. */
363 static rtx
364 widen_operand (rtx op, machine_mode mode, machine_mode oldmode,
365 int unsignedp, int no_extend)
367 rtx result;
369 /* If we don't have to extend and this is a constant, return it. */
370 if (no_extend && GET_MODE (op) == VOIDmode)
371 return op;
373 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
374 extend since it will be more efficient to do so unless the signedness of
375 a promoted object differs from our extension. */
376 if (! no_extend
377 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
378 && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
379 return convert_modes (mode, oldmode, op, unsignedp);
381 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
382 SUBREG. */
383 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
384 return gen_lowpart (mode, force_reg (GET_MODE (op), op));
386 /* Otherwise, get an object of MODE, clobber it, and set the low-order
387 part to OP. */
389 result = gen_reg_rtx (mode);
390 emit_clobber (result);
391 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
392 return result;
395 /* Return the optab used for computing the operation given by the tree code,
396 CODE and the tree EXP. This function is not always usable (for example, it
397 cannot give complete results for multiplication or division) but probably
398 ought to be relied on more widely throughout the expander. */
399 optab
400 optab_for_tree_code (enum tree_code code, const_tree type,
401 enum optab_subtype subtype)
403 bool trapv;
404 switch (code)
406 case BIT_AND_EXPR:
407 return and_optab;
409 case BIT_IOR_EXPR:
410 return ior_optab;
412 case BIT_NOT_EXPR:
413 return one_cmpl_optab;
415 case BIT_XOR_EXPR:
416 return xor_optab;
418 case MULT_HIGHPART_EXPR:
419 return TYPE_UNSIGNED (type) ? umul_highpart_optab : smul_highpart_optab;
421 case TRUNC_MOD_EXPR:
422 case CEIL_MOD_EXPR:
423 case FLOOR_MOD_EXPR:
424 case ROUND_MOD_EXPR:
425 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
427 case RDIV_EXPR:
428 case TRUNC_DIV_EXPR:
429 case CEIL_DIV_EXPR:
430 case FLOOR_DIV_EXPR:
431 case ROUND_DIV_EXPR:
432 case EXACT_DIV_EXPR:
433 if (TYPE_SATURATING (type))
434 return TYPE_UNSIGNED (type) ? usdiv_optab : ssdiv_optab;
435 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
437 case LSHIFT_EXPR:
438 if (TREE_CODE (type) == VECTOR_TYPE)
440 if (subtype == optab_vector)
441 return TYPE_SATURATING (type) ? unknown_optab : vashl_optab;
443 gcc_assert (subtype == optab_scalar);
445 if (TYPE_SATURATING (type))
446 return TYPE_UNSIGNED (type) ? usashl_optab : ssashl_optab;
447 return ashl_optab;
449 case RSHIFT_EXPR:
450 if (TREE_CODE (type) == VECTOR_TYPE)
452 if (subtype == optab_vector)
453 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
455 gcc_assert (subtype == optab_scalar);
457 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
459 case LROTATE_EXPR:
460 if (TREE_CODE (type) == VECTOR_TYPE)
462 if (subtype == optab_vector)
463 return vrotl_optab;
465 gcc_assert (subtype == optab_scalar);
467 return rotl_optab;
469 case RROTATE_EXPR:
470 if (TREE_CODE (type) == VECTOR_TYPE)
472 if (subtype == optab_vector)
473 return vrotr_optab;
475 gcc_assert (subtype == optab_scalar);
477 return rotr_optab;
479 case MAX_EXPR:
480 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
482 case MIN_EXPR:
483 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
485 case REALIGN_LOAD_EXPR:
486 return vec_realign_load_optab;
488 case WIDEN_SUM_EXPR:
489 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
491 case DOT_PROD_EXPR:
492 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
494 case SAD_EXPR:
495 return TYPE_UNSIGNED (type) ? usad_optab : ssad_optab;
497 case WIDEN_MULT_PLUS_EXPR:
498 return (TYPE_UNSIGNED (type)
499 ? (TYPE_SATURATING (type)
500 ? usmadd_widen_optab : umadd_widen_optab)
501 : (TYPE_SATURATING (type)
502 ? ssmadd_widen_optab : smadd_widen_optab));
504 case WIDEN_MULT_MINUS_EXPR:
505 return (TYPE_UNSIGNED (type)
506 ? (TYPE_SATURATING (type)
507 ? usmsub_widen_optab : umsub_widen_optab)
508 : (TYPE_SATURATING (type)
509 ? ssmsub_widen_optab : smsub_widen_optab));
511 case FMA_EXPR:
512 return fma_optab;
514 case REDUC_MAX_EXPR:
515 return TYPE_UNSIGNED (type)
516 ? reduc_umax_scal_optab : reduc_smax_scal_optab;
518 case REDUC_MIN_EXPR:
519 return TYPE_UNSIGNED (type)
520 ? reduc_umin_scal_optab : reduc_smin_scal_optab;
522 case REDUC_PLUS_EXPR:
523 return reduc_plus_scal_optab;
525 case VEC_WIDEN_MULT_HI_EXPR:
526 return TYPE_UNSIGNED (type) ?
527 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
529 case VEC_WIDEN_MULT_LO_EXPR:
530 return TYPE_UNSIGNED (type) ?
531 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
533 case VEC_WIDEN_MULT_EVEN_EXPR:
534 return TYPE_UNSIGNED (type) ?
535 vec_widen_umult_even_optab : vec_widen_smult_even_optab;
537 case VEC_WIDEN_MULT_ODD_EXPR:
538 return TYPE_UNSIGNED (type) ?
539 vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
541 case VEC_WIDEN_LSHIFT_HI_EXPR:
542 return TYPE_UNSIGNED (type) ?
543 vec_widen_ushiftl_hi_optab : vec_widen_sshiftl_hi_optab;
545 case VEC_WIDEN_LSHIFT_LO_EXPR:
546 return TYPE_UNSIGNED (type) ?
547 vec_widen_ushiftl_lo_optab : vec_widen_sshiftl_lo_optab;
549 case VEC_UNPACK_HI_EXPR:
550 return TYPE_UNSIGNED (type) ?
551 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
553 case VEC_UNPACK_LO_EXPR:
554 return TYPE_UNSIGNED (type) ?
555 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
557 case VEC_UNPACK_FLOAT_HI_EXPR:
558 /* The signedness is determined from input operand. */
559 return TYPE_UNSIGNED (type) ?
560 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
562 case VEC_UNPACK_FLOAT_LO_EXPR:
563 /* The signedness is determined from input operand. */
564 return TYPE_UNSIGNED (type) ?
565 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
567 case VEC_PACK_TRUNC_EXPR:
568 return vec_pack_trunc_optab;
570 case VEC_PACK_SAT_EXPR:
571 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
573 case VEC_PACK_FIX_TRUNC_EXPR:
574 /* The signedness is determined from output operand. */
575 return TYPE_UNSIGNED (type) ?
576 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
578 default:
579 break;
582 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
583 switch (code)
585 case POINTER_PLUS_EXPR:
586 case PLUS_EXPR:
587 if (TYPE_SATURATING (type))
588 return TYPE_UNSIGNED (type) ? usadd_optab : ssadd_optab;
589 return trapv ? addv_optab : add_optab;
591 case MINUS_EXPR:
592 if (TYPE_SATURATING (type))
593 return TYPE_UNSIGNED (type) ? ussub_optab : sssub_optab;
594 return trapv ? subv_optab : sub_optab;
596 case MULT_EXPR:
597 if (TYPE_SATURATING (type))
598 return TYPE_UNSIGNED (type) ? usmul_optab : ssmul_optab;
599 return trapv ? smulv_optab : smul_optab;
601 case NEGATE_EXPR:
602 if (TYPE_SATURATING (type))
603 return TYPE_UNSIGNED (type) ? usneg_optab : ssneg_optab;
604 return trapv ? negv_optab : neg_optab;
606 case ABS_EXPR:
607 return trapv ? absv_optab : abs_optab;
609 default:
610 return unknown_optab;
614 /* Given optab UNOPTAB that reduces a vector to a scalar, find instead the old
615 optab that produces a vector with the reduction result in one element,
616 for a tree with type TYPE. */
618 optab
619 scalar_reduc_to_vector (optab unoptab, const_tree type)
621 switch (unoptab)
623 case reduc_plus_scal_optab:
624 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
626 case reduc_smin_scal_optab: return reduc_smin_optab;
627 case reduc_umin_scal_optab: return reduc_umin_optab;
628 case reduc_smax_scal_optab: return reduc_smax_optab;
629 case reduc_umax_scal_optab: return reduc_umax_optab;
630 default: return unknown_optab;
634 /* Expand vector widening operations.
636 There are two different classes of operations handled here:
637 1) Operations whose result is wider than all the arguments to the operation.
638 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
639 In this case OP0 and optionally OP1 would be initialized,
640 but WIDE_OP wouldn't (not relevant for this case).
641 2) Operations whose result is of the same size as the last argument to the
642 operation, but wider than all the other arguments to the operation.
643 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
644 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
646 E.g, when called to expand the following operations, this is how
647 the arguments will be initialized:
648 nops OP0 OP1 WIDE_OP
649 widening-sum 2 oprnd0 - oprnd1
650 widening-dot-product 3 oprnd0 oprnd1 oprnd2
651 widening-mult 2 oprnd0 oprnd1 -
652 type-promotion (vec-unpack) 1 oprnd0 - - */
655 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
656 rtx target, int unsignedp)
658 struct expand_operand eops[4];
659 tree oprnd0, oprnd1, oprnd2;
660 machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
661 optab widen_pattern_optab;
662 enum insn_code icode;
663 int nops = TREE_CODE_LENGTH (ops->code);
664 int op;
666 oprnd0 = ops->op0;
667 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
668 widen_pattern_optab =
669 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
670 if (ops->code == WIDEN_MULT_PLUS_EXPR
671 || ops->code == WIDEN_MULT_MINUS_EXPR)
672 icode = find_widening_optab_handler (widen_pattern_optab,
673 TYPE_MODE (TREE_TYPE (ops->op2)),
674 tmode0, 0);
675 else
676 icode = optab_handler (widen_pattern_optab, tmode0);
677 gcc_assert (icode != CODE_FOR_nothing);
679 if (nops >= 2)
681 oprnd1 = ops->op1;
682 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
685 /* The last operand is of a wider mode than the rest of the operands. */
686 if (nops == 2)
687 wmode = tmode1;
688 else if (nops == 3)
690 gcc_assert (tmode1 == tmode0);
691 gcc_assert (op1);
692 oprnd2 = ops->op2;
693 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
696 op = 0;
697 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
698 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
699 if (op1)
700 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
701 if (wide_op)
702 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
703 expand_insn (icode, op, eops);
704 return eops[0].value;
707 /* Generate code to perform an operation specified by TERNARY_OPTAB
708 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
710 UNSIGNEDP is for the case where we have to widen the operands
711 to perform the operation. It says to use zero-extension.
713 If TARGET is nonzero, the value
714 is generated there, if it is convenient to do so.
715 In all cases an rtx is returned for the locus of the value;
716 this may or may not be TARGET. */
719 expand_ternary_op (machine_mode mode, optab ternary_optab, rtx op0,
720 rtx op1, rtx op2, rtx target, int unsignedp)
722 struct expand_operand ops[4];
723 enum insn_code icode = optab_handler (ternary_optab, mode);
725 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
727 create_output_operand (&ops[0], target, mode);
728 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
729 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
730 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
731 expand_insn (icode, 4, ops);
732 return ops[0].value;
736 /* Like expand_binop, but return a constant rtx if the result can be
737 calculated at compile time. The arguments and return value are
738 otherwise the same as for expand_binop. */
741 simplify_expand_binop (machine_mode mode, optab binoptab,
742 rtx op0, rtx op1, rtx target, int unsignedp,
743 enum optab_methods methods)
745 if (CONSTANT_P (op0) && CONSTANT_P (op1))
747 rtx x = simplify_binary_operation (optab_to_code (binoptab),
748 mode, op0, op1);
749 if (x)
750 return x;
753 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
756 /* Like simplify_expand_binop, but always put the result in TARGET.
757 Return true if the expansion succeeded. */
759 bool
760 force_expand_binop (machine_mode mode, optab binoptab,
761 rtx op0, rtx op1, rtx target, int unsignedp,
762 enum optab_methods methods)
764 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
765 target, unsignedp, methods);
766 if (x == 0)
767 return false;
768 if (x != target)
769 emit_move_insn (target, x);
770 return true;
773 /* Create a new vector value in VMODE with all elements set to OP. The
774 mode of OP must be the element mode of VMODE. If OP is a constant,
775 then the return value will be a constant. */
777 static rtx
778 expand_vector_broadcast (machine_mode vmode, rtx op)
780 enum insn_code icode;
781 rtvec vec;
782 rtx ret;
783 int i, n;
785 gcc_checking_assert (VECTOR_MODE_P (vmode));
787 n = GET_MODE_NUNITS (vmode);
788 vec = rtvec_alloc (n);
789 for (i = 0; i < n; ++i)
790 RTVEC_ELT (vec, i) = op;
792 if (CONSTANT_P (op))
793 return gen_rtx_CONST_VECTOR (vmode, vec);
795 /* ??? If the target doesn't have a vec_init, then we have no easy way
796 of performing this operation. Most of this sort of generic support
797 is hidden away in the vector lowering support in gimple. */
798 icode = optab_handler (vec_init_optab, vmode);
799 if (icode == CODE_FOR_nothing)
800 return NULL;
802 ret = gen_reg_rtx (vmode);
803 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
805 return ret;
808 /* This subroutine of expand_doubleword_shift handles the cases in which
809 the effective shift value is >= BITS_PER_WORD. The arguments and return
810 value are the same as for the parent routine, except that SUPERWORD_OP1
811 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
812 INTO_TARGET may be null if the caller has decided to calculate it. */
814 static bool
815 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
816 rtx outof_target, rtx into_target,
817 int unsignedp, enum optab_methods methods)
819 if (into_target != 0)
820 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
821 into_target, unsignedp, methods))
822 return false;
824 if (outof_target != 0)
826 /* For a signed right shift, we must fill OUTOF_TARGET with copies
827 of the sign bit, otherwise we must fill it with zeros. */
828 if (binoptab != ashr_optab)
829 emit_move_insn (outof_target, CONST0_RTX (word_mode));
830 else
831 if (!force_expand_binop (word_mode, binoptab,
832 outof_input, GEN_INT (BITS_PER_WORD - 1),
833 outof_target, unsignedp, methods))
834 return false;
836 return true;
839 /* This subroutine of expand_doubleword_shift handles the cases in which
840 the effective shift value is < BITS_PER_WORD. The arguments and return
841 value are the same as for the parent routine. */
843 static bool
844 expand_subword_shift (machine_mode op1_mode, optab binoptab,
845 rtx outof_input, rtx into_input, rtx op1,
846 rtx outof_target, rtx into_target,
847 int unsignedp, enum optab_methods methods,
848 unsigned HOST_WIDE_INT shift_mask)
850 optab reverse_unsigned_shift, unsigned_shift;
851 rtx tmp, carries;
853 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
854 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
856 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
857 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
858 the opposite direction to BINOPTAB. */
859 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
861 carries = outof_input;
862 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
863 op1_mode), op1_mode);
864 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
865 0, true, methods);
867 else
869 /* We must avoid shifting by BITS_PER_WORD bits since that is either
870 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
871 has unknown behavior. Do a single shift first, then shift by the
872 remainder. It's OK to use ~OP1 as the remainder if shift counts
873 are truncated to the mode size. */
874 carries = expand_binop (word_mode, reverse_unsigned_shift,
875 outof_input, const1_rtx, 0, unsignedp, methods);
876 if (shift_mask == BITS_PER_WORD - 1)
878 tmp = immed_wide_int_const
879 (wi::minus_one (GET_MODE_PRECISION (op1_mode)), op1_mode);
880 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
881 0, true, methods);
883 else
885 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
886 op1_mode), op1_mode);
887 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
888 0, true, methods);
891 if (tmp == 0 || carries == 0)
892 return false;
893 carries = expand_binop (word_mode, reverse_unsigned_shift,
894 carries, tmp, 0, unsignedp, methods);
895 if (carries == 0)
896 return false;
898 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
899 so the result can go directly into INTO_TARGET if convenient. */
900 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
901 into_target, unsignedp, methods);
902 if (tmp == 0)
903 return false;
905 /* Now OR in the bits carried over from OUTOF_INPUT. */
906 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
907 into_target, unsignedp, methods))
908 return false;
910 /* Use a standard word_mode shift for the out-of half. */
911 if (outof_target != 0)
912 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
913 outof_target, unsignedp, methods))
914 return false;
916 return true;
920 /* Try implementing expand_doubleword_shift using conditional moves.
921 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
922 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
923 are the shift counts to use in the former and latter case. All other
924 arguments are the same as the parent routine. */
926 static bool
927 expand_doubleword_shift_condmove (machine_mode op1_mode, optab binoptab,
928 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
929 rtx outof_input, rtx into_input,
930 rtx subword_op1, rtx superword_op1,
931 rtx outof_target, rtx into_target,
932 int unsignedp, enum optab_methods methods,
933 unsigned HOST_WIDE_INT shift_mask)
935 rtx outof_superword, into_superword;
937 /* Put the superword version of the output into OUTOF_SUPERWORD and
938 INTO_SUPERWORD. */
939 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
940 if (outof_target != 0 && subword_op1 == superword_op1)
942 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
943 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
944 into_superword = outof_target;
945 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
946 outof_superword, 0, unsignedp, methods))
947 return false;
949 else
951 into_superword = gen_reg_rtx (word_mode);
952 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
953 outof_superword, into_superword,
954 unsignedp, methods))
955 return false;
958 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
959 if (!expand_subword_shift (op1_mode, binoptab,
960 outof_input, into_input, subword_op1,
961 outof_target, into_target,
962 unsignedp, methods, shift_mask))
963 return false;
965 /* Select between them. Do the INTO half first because INTO_SUPERWORD
966 might be the current value of OUTOF_TARGET. */
967 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
968 into_target, into_superword, word_mode, false))
969 return false;
971 if (outof_target != 0)
972 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
973 outof_target, outof_superword,
974 word_mode, false))
975 return false;
977 return true;
980 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
981 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
982 input operand; the shift moves bits in the direction OUTOF_INPUT->
983 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
984 of the target. OP1 is the shift count and OP1_MODE is its mode.
985 If OP1 is constant, it will have been truncated as appropriate
986 and is known to be nonzero.
988 If SHIFT_MASK is zero, the result of word shifts is undefined when the
989 shift count is outside the range [0, BITS_PER_WORD). This routine must
990 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
992 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
993 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
994 fill with zeros or sign bits as appropriate.
996 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
997 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
998 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
999 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
1000 are undefined.
1002 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
1003 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
1004 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
1005 function wants to calculate it itself.
1007 Return true if the shift could be successfully synthesized. */
1009 static bool
1010 expand_doubleword_shift (machine_mode op1_mode, optab binoptab,
1011 rtx outof_input, rtx into_input, rtx op1,
1012 rtx outof_target, rtx into_target,
1013 int unsignedp, enum optab_methods methods,
1014 unsigned HOST_WIDE_INT shift_mask)
1016 rtx superword_op1, tmp, cmp1, cmp2;
1017 enum rtx_code cmp_code;
1019 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1020 fill the result with sign or zero bits as appropriate. If so, the value
1021 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1022 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1023 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1025 This isn't worthwhile for constant shifts since the optimizers will
1026 cope better with in-range shift counts. */
1027 if (shift_mask >= BITS_PER_WORD
1028 && outof_target != 0
1029 && !CONSTANT_P (op1))
1031 if (!expand_doubleword_shift (op1_mode, binoptab,
1032 outof_input, into_input, op1,
1033 0, into_target,
1034 unsignedp, methods, shift_mask))
1035 return false;
1036 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1037 outof_target, unsignedp, methods))
1038 return false;
1039 return true;
1042 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1043 is true when the effective shift value is less than BITS_PER_WORD.
1044 Set SUPERWORD_OP1 to the shift count that should be used to shift
1045 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1046 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
1047 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1049 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1050 is a subword shift count. */
1051 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1052 0, true, methods);
1053 cmp2 = CONST0_RTX (op1_mode);
1054 cmp_code = EQ;
1055 superword_op1 = op1;
1057 else
1059 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1060 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1061 0, true, methods);
1062 cmp2 = CONST0_RTX (op1_mode);
1063 cmp_code = LT;
1064 superword_op1 = cmp1;
1066 if (cmp1 == 0)
1067 return false;
1069 /* If we can compute the condition at compile time, pick the
1070 appropriate subroutine. */
1071 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1072 if (tmp != 0 && CONST_INT_P (tmp))
1074 if (tmp == const0_rtx)
1075 return expand_superword_shift (binoptab, outof_input, superword_op1,
1076 outof_target, into_target,
1077 unsignedp, methods);
1078 else
1079 return expand_subword_shift (op1_mode, binoptab,
1080 outof_input, into_input, op1,
1081 outof_target, into_target,
1082 unsignedp, methods, shift_mask);
1085 /* Try using conditional moves to generate straight-line code. */
1086 if (HAVE_conditional_move)
1088 rtx_insn *start = get_last_insn ();
1089 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1090 cmp_code, cmp1, cmp2,
1091 outof_input, into_input,
1092 op1, superword_op1,
1093 outof_target, into_target,
1094 unsignedp, methods, shift_mask))
1095 return true;
1096 delete_insns_since (start);
1099 /* As a last resort, use branches to select the correct alternative. */
1100 rtx_code_label *subword_label = gen_label_rtx ();
1101 rtx_code_label *done_label = gen_label_rtx ();
1103 NO_DEFER_POP;
1104 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1105 0, 0, subword_label, -1);
1106 OK_DEFER_POP;
1108 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1109 outof_target, into_target,
1110 unsignedp, methods))
1111 return false;
1113 emit_jump_insn (gen_jump (done_label));
1114 emit_barrier ();
1115 emit_label (subword_label);
1117 if (!expand_subword_shift (op1_mode, binoptab,
1118 outof_input, into_input, op1,
1119 outof_target, into_target,
1120 unsignedp, methods, shift_mask))
1121 return false;
1123 emit_label (done_label);
1124 return true;
1127 /* Subroutine of expand_binop. Perform a double word multiplication of
1128 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1129 as the target's word_mode. This function return NULL_RTX if anything
1130 goes wrong, in which case it may have already emitted instructions
1131 which need to be deleted.
1133 If we want to multiply two two-word values and have normal and widening
1134 multiplies of single-word values, we can do this with three smaller
1135 multiplications.
1137 The multiplication proceeds as follows:
1138 _______________________
1139 [__op0_high_|__op0_low__]
1140 _______________________
1141 * [__op1_high_|__op1_low__]
1142 _______________________________________________
1143 _______________________
1144 (1) [__op0_low__*__op1_low__]
1145 _______________________
1146 (2a) [__op0_low__*__op1_high_]
1147 _______________________
1148 (2b) [__op0_high_*__op1_low__]
1149 _______________________
1150 (3) [__op0_high_*__op1_high_]
1153 This gives a 4-word result. Since we are only interested in the
1154 lower 2 words, partial result (3) and the upper words of (2a) and
1155 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1156 calculated using non-widening multiplication.
1158 (1), however, needs to be calculated with an unsigned widening
1159 multiplication. If this operation is not directly supported we
1160 try using a signed widening multiplication and adjust the result.
1161 This adjustment works as follows:
1163 If both operands are positive then no adjustment is needed.
1165 If the operands have different signs, for example op0_low < 0 and
1166 op1_low >= 0, the instruction treats the most significant bit of
1167 op0_low as a sign bit instead of a bit with significance
1168 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1169 with 2**BITS_PER_WORD - op0_low, and two's complements the
1170 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1171 the result.
1173 Similarly, if both operands are negative, we need to add
1174 (op0_low + op1_low) * 2**BITS_PER_WORD.
1176 We use a trick to adjust quickly. We logically shift op0_low right
1177 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1178 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1179 logical shift exists, we do an arithmetic right shift and subtract
1180 the 0 or -1. */
1182 static rtx
1183 expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
1184 bool umulp, enum optab_methods methods)
1186 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1187 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1188 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1189 rtx product, adjust, product_high, temp;
1191 rtx op0_high = operand_subword_force (op0, high, mode);
1192 rtx op0_low = operand_subword_force (op0, low, mode);
1193 rtx op1_high = operand_subword_force (op1, high, mode);
1194 rtx op1_low = operand_subword_force (op1, low, mode);
1196 /* If we're using an unsigned multiply to directly compute the product
1197 of the low-order words of the operands and perform any required
1198 adjustments of the operands, we begin by trying two more multiplications
1199 and then computing the appropriate sum.
1201 We have checked above that the required addition is provided.
1202 Full-word addition will normally always succeed, especially if
1203 it is provided at all, so we don't worry about its failure. The
1204 multiplication may well fail, however, so we do handle that. */
1206 if (!umulp)
1208 /* ??? This could be done with emit_store_flag where available. */
1209 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1210 NULL_RTX, 1, methods);
1211 if (temp)
1212 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1213 NULL_RTX, 0, OPTAB_DIRECT);
1214 else
1216 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1217 NULL_RTX, 0, methods);
1218 if (!temp)
1219 return NULL_RTX;
1220 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1221 NULL_RTX, 0, OPTAB_DIRECT);
1224 if (!op0_high)
1225 return NULL_RTX;
1228 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1229 NULL_RTX, 0, OPTAB_DIRECT);
1230 if (!adjust)
1231 return NULL_RTX;
1233 /* OP0_HIGH should now be dead. */
1235 if (!umulp)
1237 /* ??? This could be done with emit_store_flag where available. */
1238 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1239 NULL_RTX, 1, methods);
1240 if (temp)
1241 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1242 NULL_RTX, 0, OPTAB_DIRECT);
1243 else
1245 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1246 NULL_RTX, 0, methods);
1247 if (!temp)
1248 return NULL_RTX;
1249 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1250 NULL_RTX, 0, OPTAB_DIRECT);
1253 if (!op1_high)
1254 return NULL_RTX;
1257 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1258 NULL_RTX, 0, OPTAB_DIRECT);
1259 if (!temp)
1260 return NULL_RTX;
1262 /* OP1_HIGH should now be dead. */
1264 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1265 NULL_RTX, 0, OPTAB_DIRECT);
1267 if (target && !REG_P (target))
1268 target = NULL_RTX;
1270 if (umulp)
1271 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1272 target, 1, OPTAB_DIRECT);
1273 else
1274 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1275 target, 1, OPTAB_DIRECT);
1277 if (!product)
1278 return NULL_RTX;
1280 product_high = operand_subword (product, high, 1, mode);
1281 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1282 NULL_RTX, 0, OPTAB_DIRECT);
1283 emit_move_insn (product_high, adjust);
1284 return product;
1287 /* Wrapper around expand_binop which takes an rtx code to specify
1288 the operation to perform, not an optab pointer. All other
1289 arguments are the same. */
1291 expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
1292 rtx op1, rtx target, int unsignedp,
1293 enum optab_methods methods)
1295 optab binop = code_to_optab (code);
1296 gcc_assert (binop);
1298 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1301 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1302 binop. Order them according to commutative_operand_precedence and, if
1303 possible, try to put TARGET or a pseudo first. */
1304 static bool
1305 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1307 int op0_prec = commutative_operand_precedence (op0);
1308 int op1_prec = commutative_operand_precedence (op1);
1310 if (op0_prec < op1_prec)
1311 return true;
1313 if (op0_prec > op1_prec)
1314 return false;
1316 /* With equal precedence, both orders are ok, but it is better if the
1317 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1318 if (target == 0 || REG_P (target))
1319 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1320 else
1321 return rtx_equal_p (op1, target);
1324 /* Return true if BINOPTAB implements a shift operation. */
1326 static bool
1327 shift_optab_p (optab binoptab)
1329 switch (optab_to_code (binoptab))
1331 case ASHIFT:
1332 case SS_ASHIFT:
1333 case US_ASHIFT:
1334 case ASHIFTRT:
1335 case LSHIFTRT:
1336 case ROTATE:
1337 case ROTATERT:
1338 return true;
1340 default:
1341 return false;
1345 /* Return true if BINOPTAB implements a commutative binary operation. */
1347 static bool
1348 commutative_optab_p (optab binoptab)
1350 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1351 || binoptab == smul_widen_optab
1352 || binoptab == umul_widen_optab
1353 || binoptab == smul_highpart_optab
1354 || binoptab == umul_highpart_optab);
1357 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1358 optimizing, and if the operand is a constant that costs more than
1359 1 instruction, force the constant into a register and return that
1360 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1362 static rtx
1363 avoid_expensive_constant (machine_mode mode, optab binoptab,
1364 int opn, rtx x, bool unsignedp)
1366 bool speed = optimize_insn_for_speed_p ();
1368 if (mode != VOIDmode
1369 && optimize
1370 && CONSTANT_P (x)
1371 && (rtx_cost (x, optab_to_code (binoptab), opn, speed)
1372 > set_src_cost (x, speed)))
1374 if (CONST_INT_P (x))
1376 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1377 if (intval != INTVAL (x))
1378 x = GEN_INT (intval);
1380 else
1381 x = convert_modes (mode, VOIDmode, x, unsignedp);
1382 x = force_reg (mode, x);
1384 return x;
1387 /* Helper function for expand_binop: handle the case where there
1388 is an insn that directly implements the indicated operation.
1389 Returns null if this is not possible. */
1390 static rtx
1391 expand_binop_directly (machine_mode mode, optab binoptab,
1392 rtx op0, rtx op1,
1393 rtx target, int unsignedp, enum optab_methods methods,
1394 rtx_insn *last)
1396 machine_mode from_mode = widened_mode (mode, op0, op1);
1397 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1398 from_mode, 1);
1399 machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1400 machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1401 machine_mode mode0, mode1, tmp_mode;
1402 struct expand_operand ops[3];
1403 bool commutative_p;
1404 rtx_insn *pat;
1405 rtx xop0 = op0, xop1 = op1;
1407 /* If it is a commutative operator and the modes would match
1408 if we would swap the operands, we can save the conversions. */
1409 commutative_p = commutative_optab_p (binoptab);
1410 if (commutative_p
1411 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1412 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1413 std::swap (xop0, xop1);
1415 /* If we are optimizing, force expensive constants into a register. */
1416 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1417 if (!shift_optab_p (binoptab))
1418 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1420 /* In case the insn wants input operands in modes different from
1421 those of the actual operands, convert the operands. It would
1422 seem that we don't need to convert CONST_INTs, but we do, so
1423 that they're properly zero-extended, sign-extended or truncated
1424 for their mode. */
1426 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1427 if (xmode0 != VOIDmode && xmode0 != mode0)
1429 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1430 mode0 = xmode0;
1433 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1434 if (xmode1 != VOIDmode && xmode1 != mode1)
1436 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1437 mode1 = xmode1;
1440 /* If operation is commutative,
1441 try to make the first operand a register.
1442 Even better, try to make it the same as the target.
1443 Also try to make the last operand a constant. */
1444 if (commutative_p
1445 && swap_commutative_operands_with_target (target, xop0, xop1))
1446 std::swap (xop0, xop1);
1448 /* Now, if insn's predicates don't allow our operands, put them into
1449 pseudo regs. */
1451 if (binoptab == vec_pack_trunc_optab
1452 || binoptab == vec_pack_usat_optab
1453 || binoptab == vec_pack_ssat_optab
1454 || binoptab == vec_pack_ufix_trunc_optab
1455 || binoptab == vec_pack_sfix_trunc_optab)
1457 /* The mode of the result is different then the mode of the
1458 arguments. */
1459 tmp_mode = insn_data[(int) icode].operand[0].mode;
1460 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1462 delete_insns_since (last);
1463 return NULL_RTX;
1466 else
1467 tmp_mode = mode;
1469 create_output_operand (&ops[0], target, tmp_mode);
1470 create_input_operand (&ops[1], xop0, mode0);
1471 create_input_operand (&ops[2], xop1, mode1);
1472 pat = maybe_gen_insn (icode, 3, ops);
1473 if (pat)
1475 /* If PAT is composed of more than one insn, try to add an appropriate
1476 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1477 operand, call expand_binop again, this time without a target. */
1478 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1479 && ! add_equal_note (pat, ops[0].value,
1480 optab_to_code (binoptab),
1481 ops[1].value, ops[2].value))
1483 delete_insns_since (last);
1484 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1485 unsignedp, methods);
1488 emit_insn (pat);
1489 return ops[0].value;
1491 delete_insns_since (last);
1492 return NULL_RTX;
1495 /* Generate code to perform an operation specified by BINOPTAB
1496 on operands OP0 and OP1, with result having machine-mode MODE.
1498 UNSIGNEDP is for the case where we have to widen the operands
1499 to perform the operation. It says to use zero-extension.
1501 If TARGET is nonzero, the value
1502 is generated there, if it is convenient to do so.
1503 In all cases an rtx is returned for the locus of the value;
1504 this may or may not be TARGET. */
1507 expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
1508 rtx target, int unsignedp, enum optab_methods methods)
1510 enum optab_methods next_methods
1511 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1512 ? OPTAB_WIDEN : methods);
1513 enum mode_class mclass;
1514 machine_mode wider_mode;
1515 rtx libfunc;
1516 rtx temp;
1517 rtx_insn *entry_last = get_last_insn ();
1518 rtx_insn *last;
1520 mclass = GET_MODE_CLASS (mode);
1522 /* If subtracting an integer constant, convert this into an addition of
1523 the negated constant. */
1525 if (binoptab == sub_optab && CONST_INT_P (op1))
1527 op1 = negate_rtx (mode, op1);
1528 binoptab = add_optab;
1531 /* Record where to delete back to if we backtrack. */
1532 last = get_last_insn ();
1534 /* If we can do it with a three-operand insn, do so. */
1536 if (methods != OPTAB_MUST_WIDEN
1537 && find_widening_optab_handler (binoptab, mode,
1538 widened_mode (mode, op0, op1), 1)
1539 != CODE_FOR_nothing)
1541 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1542 unsignedp, methods, last);
1543 if (temp)
1544 return temp;
1547 /* If we were trying to rotate, and that didn't work, try rotating
1548 the other direction before falling back to shifts and bitwise-or. */
1549 if (((binoptab == rotl_optab
1550 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1551 || (binoptab == rotr_optab
1552 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1553 && mclass == MODE_INT)
1555 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1556 rtx newop1;
1557 unsigned int bits = GET_MODE_PRECISION (mode);
1559 if (CONST_INT_P (op1))
1560 newop1 = GEN_INT (bits - INTVAL (op1));
1561 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1562 newop1 = negate_rtx (GET_MODE (op1), op1);
1563 else
1564 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1565 gen_int_mode (bits, GET_MODE (op1)), op1,
1566 NULL_RTX, unsignedp, OPTAB_DIRECT);
1568 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1569 target, unsignedp, methods, last);
1570 if (temp)
1571 return temp;
1574 /* If this is a multiply, see if we can do a widening operation that
1575 takes operands of this mode and makes a wider mode. */
1577 if (binoptab == smul_optab
1578 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1579 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1580 : smul_widen_optab),
1581 GET_MODE_2XWIDER_MODE (mode), mode)
1582 != CODE_FOR_nothing))
1584 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1585 unsignedp ? umul_widen_optab : smul_widen_optab,
1586 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1588 if (temp != 0)
1590 if (GET_MODE_CLASS (mode) == MODE_INT
1591 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1592 return gen_lowpart (mode, temp);
1593 else
1594 return convert_to_mode (mode, temp, unsignedp);
1598 /* If this is a vector shift by a scalar, see if we can do a vector
1599 shift by a vector. If so, broadcast the scalar into a vector. */
1600 if (mclass == MODE_VECTOR_INT)
1602 optab otheroptab = unknown_optab;
1604 if (binoptab == ashl_optab)
1605 otheroptab = vashl_optab;
1606 else if (binoptab == ashr_optab)
1607 otheroptab = vashr_optab;
1608 else if (binoptab == lshr_optab)
1609 otheroptab = vlshr_optab;
1610 else if (binoptab == rotl_optab)
1611 otheroptab = vrotl_optab;
1612 else if (binoptab == rotr_optab)
1613 otheroptab = vrotr_optab;
1615 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1617 rtx vop1 = expand_vector_broadcast (mode, op1);
1618 if (vop1)
1620 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1621 target, unsignedp, methods, last);
1622 if (temp)
1623 return temp;
1628 /* Look for a wider mode of the same class for which we think we
1629 can open-code the operation. Check for a widening multiply at the
1630 wider mode as well. */
1632 if (CLASS_HAS_WIDER_MODES_P (mclass)
1633 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1634 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1635 wider_mode != VOIDmode;
1636 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1638 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1639 || (binoptab == smul_optab
1640 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1641 && (find_widening_optab_handler ((unsignedp
1642 ? umul_widen_optab
1643 : smul_widen_optab),
1644 GET_MODE_WIDER_MODE (wider_mode),
1645 mode, 0)
1646 != CODE_FOR_nothing)))
1648 rtx xop0 = op0, xop1 = op1;
1649 int no_extend = 0;
1651 /* For certain integer operations, we need not actually extend
1652 the narrow operands, as long as we will truncate
1653 the results to the same narrowness. */
1655 if ((binoptab == ior_optab || binoptab == and_optab
1656 || binoptab == xor_optab
1657 || binoptab == add_optab || binoptab == sub_optab
1658 || binoptab == smul_optab || binoptab == ashl_optab)
1659 && mclass == MODE_INT)
1661 no_extend = 1;
1662 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1663 xop0, unsignedp);
1664 if (binoptab != ashl_optab)
1665 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1666 xop1, unsignedp);
1669 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1671 /* The second operand of a shift must always be extended. */
1672 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1673 no_extend && binoptab != ashl_optab);
1675 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1676 unsignedp, OPTAB_DIRECT);
1677 if (temp)
1679 if (mclass != MODE_INT
1680 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1682 if (target == 0)
1683 target = gen_reg_rtx (mode);
1684 convert_move (target, temp, 0);
1685 return target;
1687 else
1688 return gen_lowpart (mode, temp);
1690 else
1691 delete_insns_since (last);
1695 /* If operation is commutative,
1696 try to make the first operand a register.
1697 Even better, try to make it the same as the target.
1698 Also try to make the last operand a constant. */
1699 if (commutative_optab_p (binoptab)
1700 && swap_commutative_operands_with_target (target, op0, op1))
1701 std::swap (op0, op1);
1703 /* These can be done a word at a time. */
1704 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1705 && mclass == MODE_INT
1706 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1707 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1709 int i;
1710 rtx_insn *insns;
1712 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1713 won't be accurate, so use a new target. */
1714 if (target == 0
1715 || target == op0
1716 || target == op1
1717 || !valid_multiword_target_p (target))
1718 target = gen_reg_rtx (mode);
1720 start_sequence ();
1722 /* Do the actual arithmetic. */
1723 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1725 rtx target_piece = operand_subword (target, i, 1, mode);
1726 rtx x = expand_binop (word_mode, binoptab,
1727 operand_subword_force (op0, i, mode),
1728 operand_subword_force (op1, i, mode),
1729 target_piece, unsignedp, next_methods);
1731 if (x == 0)
1732 break;
1734 if (target_piece != x)
1735 emit_move_insn (target_piece, x);
1738 insns = get_insns ();
1739 end_sequence ();
1741 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1743 emit_insn (insns);
1744 return target;
1748 /* Synthesize double word shifts from single word shifts. */
1749 if ((binoptab == lshr_optab || binoptab == ashl_optab
1750 || binoptab == ashr_optab)
1751 && mclass == MODE_INT
1752 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1753 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1754 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1755 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1756 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1757 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1759 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1760 machine_mode op1_mode;
1762 double_shift_mask = targetm.shift_truncation_mask (mode);
1763 shift_mask = targetm.shift_truncation_mask (word_mode);
1764 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1766 /* Apply the truncation to constant shifts. */
1767 if (double_shift_mask > 0 && CONST_INT_P (op1))
1768 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1770 if (op1 == CONST0_RTX (op1_mode))
1771 return op0;
1773 /* Make sure that this is a combination that expand_doubleword_shift
1774 can handle. See the comments there for details. */
1775 if (double_shift_mask == 0
1776 || (shift_mask == BITS_PER_WORD - 1
1777 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1779 rtx_insn *insns;
1780 rtx into_target, outof_target;
1781 rtx into_input, outof_input;
1782 int left_shift, outof_word;
1784 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1785 won't be accurate, so use a new target. */
1786 if (target == 0
1787 || target == op0
1788 || target == op1
1789 || !valid_multiword_target_p (target))
1790 target = gen_reg_rtx (mode);
1792 start_sequence ();
1794 /* OUTOF_* is the word we are shifting bits away from, and
1795 INTO_* is the word that we are shifting bits towards, thus
1796 they differ depending on the direction of the shift and
1797 WORDS_BIG_ENDIAN. */
1799 left_shift = binoptab == ashl_optab;
1800 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1802 outof_target = operand_subword (target, outof_word, 1, mode);
1803 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1805 outof_input = operand_subword_force (op0, outof_word, mode);
1806 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1808 if (expand_doubleword_shift (op1_mode, binoptab,
1809 outof_input, into_input, op1,
1810 outof_target, into_target,
1811 unsignedp, next_methods, shift_mask))
1813 insns = get_insns ();
1814 end_sequence ();
1816 emit_insn (insns);
1817 return target;
1819 end_sequence ();
1823 /* Synthesize double word rotates from single word shifts. */
1824 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1825 && mclass == MODE_INT
1826 && CONST_INT_P (op1)
1827 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1828 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1829 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1831 rtx_insn *insns;
1832 rtx into_target, outof_target;
1833 rtx into_input, outof_input;
1834 rtx inter;
1835 int shift_count, left_shift, outof_word;
1837 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1838 won't be accurate, so use a new target. Do this also if target is not
1839 a REG, first because having a register instead may open optimization
1840 opportunities, and second because if target and op0 happen to be MEMs
1841 designating the same location, we would risk clobbering it too early
1842 in the code sequence we generate below. */
1843 if (target == 0
1844 || target == op0
1845 || target == op1
1846 || !REG_P (target)
1847 || !valid_multiword_target_p (target))
1848 target = gen_reg_rtx (mode);
1850 start_sequence ();
1852 shift_count = INTVAL (op1);
1854 /* OUTOF_* is the word we are shifting bits away from, and
1855 INTO_* is the word that we are shifting bits towards, thus
1856 they differ depending on the direction of the shift and
1857 WORDS_BIG_ENDIAN. */
1859 left_shift = (binoptab == rotl_optab);
1860 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1862 outof_target = operand_subword (target, outof_word, 1, mode);
1863 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1865 outof_input = operand_subword_force (op0, outof_word, mode);
1866 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1868 if (shift_count == BITS_PER_WORD)
1870 /* This is just a word swap. */
1871 emit_move_insn (outof_target, into_input);
1872 emit_move_insn (into_target, outof_input);
1873 inter = const0_rtx;
1875 else
1877 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1878 rtx first_shift_count, second_shift_count;
1879 optab reverse_unsigned_shift, unsigned_shift;
1881 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1882 ? lshr_optab : ashl_optab);
1884 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1885 ? ashl_optab : lshr_optab);
1887 if (shift_count > BITS_PER_WORD)
1889 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1890 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1892 else
1894 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1895 second_shift_count = GEN_INT (shift_count);
1898 into_temp1 = expand_binop (word_mode, unsigned_shift,
1899 outof_input, first_shift_count,
1900 NULL_RTX, unsignedp, next_methods);
1901 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1902 into_input, second_shift_count,
1903 NULL_RTX, unsignedp, next_methods);
1905 if (into_temp1 != 0 && into_temp2 != 0)
1906 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1907 into_target, unsignedp, next_methods);
1908 else
1909 inter = 0;
1911 if (inter != 0 && inter != into_target)
1912 emit_move_insn (into_target, inter);
1914 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1915 into_input, first_shift_count,
1916 NULL_RTX, unsignedp, next_methods);
1917 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1918 outof_input, second_shift_count,
1919 NULL_RTX, unsignedp, next_methods);
1921 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1922 inter = expand_binop (word_mode, ior_optab,
1923 outof_temp1, outof_temp2,
1924 outof_target, unsignedp, next_methods);
1926 if (inter != 0 && inter != outof_target)
1927 emit_move_insn (outof_target, inter);
1930 insns = get_insns ();
1931 end_sequence ();
1933 if (inter != 0)
1935 emit_insn (insns);
1936 return target;
1940 /* These can be done a word at a time by propagating carries. */
1941 if ((binoptab == add_optab || binoptab == sub_optab)
1942 && mclass == MODE_INT
1943 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1944 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1946 unsigned int i;
1947 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1948 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1949 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1950 rtx xop0, xop1, xtarget;
1952 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1953 value is one of those, use it. Otherwise, use 1 since it is the
1954 one easiest to get. */
1955 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1956 int normalizep = STORE_FLAG_VALUE;
1957 #else
1958 int normalizep = 1;
1959 #endif
1961 /* Prepare the operands. */
1962 xop0 = force_reg (mode, op0);
1963 xop1 = force_reg (mode, op1);
1965 xtarget = gen_reg_rtx (mode);
1967 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1968 target = xtarget;
1970 /* Indicate for flow that the entire target reg is being set. */
1971 if (REG_P (target))
1972 emit_clobber (xtarget);
1974 /* Do the actual arithmetic. */
1975 for (i = 0; i < nwords; i++)
1977 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1978 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1979 rtx op0_piece = operand_subword_force (xop0, index, mode);
1980 rtx op1_piece = operand_subword_force (xop1, index, mode);
1981 rtx x;
1983 /* Main add/subtract of the input operands. */
1984 x = expand_binop (word_mode, binoptab,
1985 op0_piece, op1_piece,
1986 target_piece, unsignedp, next_methods);
1987 if (x == 0)
1988 break;
1990 if (i + 1 < nwords)
1992 /* Store carry from main add/subtract. */
1993 carry_out = gen_reg_rtx (word_mode);
1994 carry_out = emit_store_flag_force (carry_out,
1995 (binoptab == add_optab
1996 ? LT : GT),
1997 x, op0_piece,
1998 word_mode, 1, normalizep);
2001 if (i > 0)
2003 rtx newx;
2005 /* Add/subtract previous carry to main result. */
2006 newx = expand_binop (word_mode,
2007 normalizep == 1 ? binoptab : otheroptab,
2008 x, carry_in,
2009 NULL_RTX, 1, next_methods);
2011 if (i + 1 < nwords)
2013 /* Get out carry from adding/subtracting carry in. */
2014 rtx carry_tmp = gen_reg_rtx (word_mode);
2015 carry_tmp = emit_store_flag_force (carry_tmp,
2016 (binoptab == add_optab
2017 ? LT : GT),
2018 newx, x,
2019 word_mode, 1, normalizep);
2021 /* Logical-ior the two poss. carry together. */
2022 carry_out = expand_binop (word_mode, ior_optab,
2023 carry_out, carry_tmp,
2024 carry_out, 0, next_methods);
2025 if (carry_out == 0)
2026 break;
2028 emit_move_insn (target_piece, newx);
2030 else
2032 if (x != target_piece)
2033 emit_move_insn (target_piece, x);
2036 carry_in = carry_out;
2039 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2041 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2042 || ! rtx_equal_p (target, xtarget))
2044 rtx_insn *temp = emit_move_insn (target, xtarget);
2046 set_dst_reg_note (temp, REG_EQUAL,
2047 gen_rtx_fmt_ee (optab_to_code (binoptab),
2048 mode, copy_rtx (xop0),
2049 copy_rtx (xop1)),
2050 target);
2052 else
2053 target = xtarget;
2055 return target;
2058 else
2059 delete_insns_since (last);
2062 /* Attempt to synthesize double word multiplies using a sequence of word
2063 mode multiplications. We first attempt to generate a sequence using a
2064 more efficient unsigned widening multiply, and if that fails we then
2065 try using a signed widening multiply. */
2067 if (binoptab == smul_optab
2068 && mclass == MODE_INT
2069 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2070 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2071 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2073 rtx product = NULL_RTX;
2074 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
2075 != CODE_FOR_nothing)
2077 product = expand_doubleword_mult (mode, op0, op1, target,
2078 true, methods);
2079 if (!product)
2080 delete_insns_since (last);
2083 if (product == NULL_RTX
2084 && widening_optab_handler (smul_widen_optab, mode, word_mode)
2085 != CODE_FOR_nothing)
2087 product = expand_doubleword_mult (mode, op0, op1, target,
2088 false, methods);
2089 if (!product)
2090 delete_insns_since (last);
2093 if (product != NULL_RTX)
2095 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2097 temp = emit_move_insn (target ? target : product, product);
2098 set_dst_reg_note (temp,
2099 REG_EQUAL,
2100 gen_rtx_fmt_ee (MULT, mode,
2101 copy_rtx (op0),
2102 copy_rtx (op1)),
2103 target ? target : product);
2105 return product;
2109 /* It can't be open-coded in this mode.
2110 Use a library call if one is available and caller says that's ok. */
2112 libfunc = optab_libfunc (binoptab, mode);
2113 if (libfunc
2114 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2116 rtx_insn *insns;
2117 rtx op1x = op1;
2118 machine_mode op1_mode = mode;
2119 rtx value;
2121 start_sequence ();
2123 if (shift_optab_p (binoptab))
2125 op1_mode = targetm.libgcc_shift_count_mode ();
2126 /* Specify unsigned here,
2127 since negative shift counts are meaningless. */
2128 op1x = convert_to_mode (op1_mode, op1, 1);
2131 if (GET_MODE (op0) != VOIDmode
2132 && GET_MODE (op0) != mode)
2133 op0 = convert_to_mode (mode, op0, unsignedp);
2135 /* Pass 1 for NO_QUEUE so we don't lose any increments
2136 if the libcall is cse'd or moved. */
2137 value = emit_library_call_value (libfunc,
2138 NULL_RTX, LCT_CONST, mode, 2,
2139 op0, mode, op1x, op1_mode);
2141 insns = get_insns ();
2142 end_sequence ();
2144 target = gen_reg_rtx (mode);
2145 emit_libcall_block_1 (insns, target, value,
2146 gen_rtx_fmt_ee (optab_to_code (binoptab),
2147 mode, op0, op1),
2148 trapv_binoptab_p (binoptab));
2150 return target;
2153 delete_insns_since (last);
2155 /* It can't be done in this mode. Can we do it in a wider mode? */
2157 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2158 || methods == OPTAB_MUST_WIDEN))
2160 /* Caller says, don't even try. */
2161 delete_insns_since (entry_last);
2162 return 0;
2165 /* Compute the value of METHODS to pass to recursive calls.
2166 Don't allow widening to be tried recursively. */
2168 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2170 /* Look for a wider mode of the same class for which it appears we can do
2171 the operation. */
2173 if (CLASS_HAS_WIDER_MODES_P (mclass))
2175 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2176 wider_mode != VOIDmode;
2177 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2179 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2180 != CODE_FOR_nothing
2181 || (methods == OPTAB_LIB
2182 && optab_libfunc (binoptab, wider_mode)))
2184 rtx xop0 = op0, xop1 = op1;
2185 int no_extend = 0;
2187 /* For certain integer operations, we need not actually extend
2188 the narrow operands, as long as we will truncate
2189 the results to the same narrowness. */
2191 if ((binoptab == ior_optab || binoptab == and_optab
2192 || binoptab == xor_optab
2193 || binoptab == add_optab || binoptab == sub_optab
2194 || binoptab == smul_optab || binoptab == ashl_optab)
2195 && mclass == MODE_INT)
2196 no_extend = 1;
2198 xop0 = widen_operand (xop0, wider_mode, mode,
2199 unsignedp, no_extend);
2201 /* The second operand of a shift must always be extended. */
2202 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2203 no_extend && binoptab != ashl_optab);
2205 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2206 unsignedp, methods);
2207 if (temp)
2209 if (mclass != MODE_INT
2210 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2212 if (target == 0)
2213 target = gen_reg_rtx (mode);
2214 convert_move (target, temp, 0);
2215 return target;
2217 else
2218 return gen_lowpart (mode, temp);
2220 else
2221 delete_insns_since (last);
2226 delete_insns_since (entry_last);
2227 return 0;
2230 /* Expand a binary operator which has both signed and unsigned forms.
2231 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2232 signed operations.
2234 If we widen unsigned operands, we may use a signed wider operation instead
2235 of an unsigned wider operation, since the result would be the same. */
2238 sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
2239 rtx op0, rtx op1, rtx target, int unsignedp,
2240 enum optab_methods methods)
2242 rtx temp;
2243 optab direct_optab = unsignedp ? uoptab : soptab;
2244 bool save_enable;
2246 /* Do it without widening, if possible. */
2247 temp = expand_binop (mode, direct_optab, op0, op1, target,
2248 unsignedp, OPTAB_DIRECT);
2249 if (temp || methods == OPTAB_DIRECT)
2250 return temp;
2252 /* Try widening to a signed int. Disable any direct use of any
2253 signed insn in the current mode. */
2254 save_enable = swap_optab_enable (soptab, mode, false);
2256 temp = expand_binop (mode, soptab, op0, op1, target,
2257 unsignedp, OPTAB_WIDEN);
2259 /* For unsigned operands, try widening to an unsigned int. */
2260 if (!temp && unsignedp)
2261 temp = expand_binop (mode, uoptab, op0, op1, target,
2262 unsignedp, OPTAB_WIDEN);
2263 if (temp || methods == OPTAB_WIDEN)
2264 goto egress;
2266 /* Use the right width libcall if that exists. */
2267 temp = expand_binop (mode, direct_optab, op0, op1, target,
2268 unsignedp, OPTAB_LIB);
2269 if (temp || methods == OPTAB_LIB)
2270 goto egress;
2272 /* Must widen and use a libcall, use either signed or unsigned. */
2273 temp = expand_binop (mode, soptab, op0, op1, target,
2274 unsignedp, methods);
2275 if (!temp && unsignedp)
2276 temp = expand_binop (mode, uoptab, op0, op1, target,
2277 unsignedp, methods);
2279 egress:
2280 /* Undo the fiddling above. */
2281 if (save_enable)
2282 swap_optab_enable (soptab, mode, true);
2283 return temp;
2286 /* Generate code to perform an operation specified by UNOPPTAB
2287 on operand OP0, with two results to TARG0 and TARG1.
2288 We assume that the order of the operands for the instruction
2289 is TARG0, TARG1, OP0.
2291 Either TARG0 or TARG1 may be zero, but what that means is that
2292 the result is not actually wanted. We will generate it into
2293 a dummy pseudo-reg and discard it. They may not both be zero.
2295 Returns 1 if this operation can be performed; 0 if not. */
2298 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2299 int unsignedp)
2301 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2302 enum mode_class mclass;
2303 machine_mode wider_mode;
2304 rtx_insn *entry_last = get_last_insn ();
2305 rtx_insn *last;
2307 mclass = GET_MODE_CLASS (mode);
2309 if (!targ0)
2310 targ0 = gen_reg_rtx (mode);
2311 if (!targ1)
2312 targ1 = gen_reg_rtx (mode);
2314 /* Record where to go back to if we fail. */
2315 last = get_last_insn ();
2317 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2319 struct expand_operand ops[3];
2320 enum insn_code icode = optab_handler (unoptab, mode);
2322 create_fixed_operand (&ops[0], targ0);
2323 create_fixed_operand (&ops[1], targ1);
2324 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2325 if (maybe_expand_insn (icode, 3, ops))
2326 return 1;
2329 /* It can't be done in this mode. Can we do it in a wider mode? */
2331 if (CLASS_HAS_WIDER_MODES_P (mclass))
2333 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2334 wider_mode != VOIDmode;
2335 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2337 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2339 rtx t0 = gen_reg_rtx (wider_mode);
2340 rtx t1 = gen_reg_rtx (wider_mode);
2341 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2343 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2345 convert_move (targ0, t0, unsignedp);
2346 convert_move (targ1, t1, unsignedp);
2347 return 1;
2349 else
2350 delete_insns_since (last);
2355 delete_insns_since (entry_last);
2356 return 0;
2359 /* Generate code to perform an operation specified by BINOPTAB
2360 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2361 We assume that the order of the operands for the instruction
2362 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2363 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2365 Either TARG0 or TARG1 may be zero, but what that means is that
2366 the result is not actually wanted. We will generate it into
2367 a dummy pseudo-reg and discard it. They may not both be zero.
2369 Returns 1 if this operation can be performed; 0 if not. */
2372 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2373 int unsignedp)
2375 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2376 enum mode_class mclass;
2377 machine_mode wider_mode;
2378 rtx_insn *entry_last = get_last_insn ();
2379 rtx_insn *last;
2381 mclass = GET_MODE_CLASS (mode);
2383 if (!targ0)
2384 targ0 = gen_reg_rtx (mode);
2385 if (!targ1)
2386 targ1 = gen_reg_rtx (mode);
2388 /* Record where to go back to if we fail. */
2389 last = get_last_insn ();
2391 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2393 struct expand_operand ops[4];
2394 enum insn_code icode = optab_handler (binoptab, mode);
2395 machine_mode mode0 = insn_data[icode].operand[1].mode;
2396 machine_mode mode1 = insn_data[icode].operand[2].mode;
2397 rtx xop0 = op0, xop1 = op1;
2399 /* If we are optimizing, force expensive constants into a register. */
2400 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2401 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2403 create_fixed_operand (&ops[0], targ0);
2404 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2405 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2406 create_fixed_operand (&ops[3], targ1);
2407 if (maybe_expand_insn (icode, 4, ops))
2408 return 1;
2409 delete_insns_since (last);
2412 /* It can't be done in this mode. Can we do it in a wider mode? */
2414 if (CLASS_HAS_WIDER_MODES_P (mclass))
2416 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2417 wider_mode != VOIDmode;
2418 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2420 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2422 rtx t0 = gen_reg_rtx (wider_mode);
2423 rtx t1 = gen_reg_rtx (wider_mode);
2424 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2425 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2427 if (expand_twoval_binop (binoptab, cop0, cop1,
2428 t0, t1, unsignedp))
2430 convert_move (targ0, t0, unsignedp);
2431 convert_move (targ1, t1, unsignedp);
2432 return 1;
2434 else
2435 delete_insns_since (last);
2440 delete_insns_since (entry_last);
2441 return 0;
2444 /* Expand the two-valued library call indicated by BINOPTAB, but
2445 preserve only one of the values. If TARG0 is non-NULL, the first
2446 value is placed into TARG0; otherwise the second value is placed
2447 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2448 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2449 This routine assumes that the value returned by the library call is
2450 as if the return value was of an integral mode twice as wide as the
2451 mode of OP0. Returns 1 if the call was successful. */
2453 bool
2454 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2455 rtx targ0, rtx targ1, enum rtx_code code)
2457 machine_mode mode;
2458 machine_mode libval_mode;
2459 rtx libval;
2460 rtx_insn *insns;
2461 rtx libfunc;
2463 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2464 gcc_assert (!targ0 != !targ1);
2466 mode = GET_MODE (op0);
2467 libfunc = optab_libfunc (binoptab, mode);
2468 if (!libfunc)
2469 return false;
2471 /* The value returned by the library function will have twice as
2472 many bits as the nominal MODE. */
2473 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2474 MODE_INT);
2475 start_sequence ();
2476 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2477 libval_mode, 2,
2478 op0, mode,
2479 op1, mode);
2480 /* Get the part of VAL containing the value that we want. */
2481 libval = simplify_gen_subreg (mode, libval, libval_mode,
2482 targ0 ? 0 : GET_MODE_SIZE (mode));
2483 insns = get_insns ();
2484 end_sequence ();
2485 /* Move the into the desired location. */
2486 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2487 gen_rtx_fmt_ee (code, mode, op0, op1));
2489 return true;
2493 /* Wrapper around expand_unop which takes an rtx code to specify
2494 the operation to perform, not an optab pointer. All other
2495 arguments are the same. */
2497 expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
2498 rtx target, int unsignedp)
2500 optab unop = code_to_optab (code);
2501 gcc_assert (unop);
2503 return expand_unop (mode, unop, op0, target, unsignedp);
2506 /* Try calculating
2507 (clz:narrow x)
2509 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2511 A similar operation can be used for clrsb. UNOPTAB says which operation
2512 we are trying to expand. */
2513 static rtx
2514 widen_leading (machine_mode mode, rtx op0, rtx target, optab unoptab)
2516 enum mode_class mclass = GET_MODE_CLASS (mode);
2517 if (CLASS_HAS_WIDER_MODES_P (mclass))
2519 machine_mode wider_mode;
2520 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2521 wider_mode != VOIDmode;
2522 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2524 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2526 rtx xop0, temp;
2527 rtx_insn *last;
2529 last = get_last_insn ();
2531 if (target == 0)
2532 target = gen_reg_rtx (mode);
2533 xop0 = widen_operand (op0, wider_mode, mode,
2534 unoptab != clrsb_optab, false);
2535 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2536 unoptab != clrsb_optab);
2537 if (temp != 0)
2538 temp = expand_binop
2539 (wider_mode, sub_optab, temp,
2540 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2541 - GET_MODE_PRECISION (mode),
2542 wider_mode),
2543 target, true, OPTAB_DIRECT);
2544 if (temp == 0)
2545 delete_insns_since (last);
2547 return temp;
2551 return 0;
2554 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2555 quantities, choosing which based on whether the high word is nonzero. */
2556 static rtx
2557 expand_doubleword_clz (machine_mode mode, rtx op0, rtx target)
2559 rtx xop0 = force_reg (mode, op0);
2560 rtx subhi = gen_highpart (word_mode, xop0);
2561 rtx sublo = gen_lowpart (word_mode, xop0);
2562 rtx_code_label *hi0_label = gen_label_rtx ();
2563 rtx_code_label *after_label = gen_label_rtx ();
2564 rtx_insn *seq;
2565 rtx temp, result;
2567 /* If we were not given a target, use a word_mode register, not a
2568 'mode' register. The result will fit, and nobody is expecting
2569 anything bigger (the return type of __builtin_clz* is int). */
2570 if (!target)
2571 target = gen_reg_rtx (word_mode);
2573 /* In any case, write to a word_mode scratch in both branches of the
2574 conditional, so we can ensure there is a single move insn setting
2575 'target' to tag a REG_EQUAL note on. */
2576 result = gen_reg_rtx (word_mode);
2578 start_sequence ();
2580 /* If the high word is not equal to zero,
2581 then clz of the full value is clz of the high word. */
2582 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2583 word_mode, true, hi0_label);
2585 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2586 if (!temp)
2587 goto fail;
2589 if (temp != result)
2590 convert_move (result, temp, true);
2592 emit_jump_insn (gen_jump (after_label));
2593 emit_barrier ();
2595 /* Else clz of the full value is clz of the low word plus the number
2596 of bits in the high word. */
2597 emit_label (hi0_label);
2599 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2600 if (!temp)
2601 goto fail;
2602 temp = expand_binop (word_mode, add_optab, temp,
2603 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2604 result, true, OPTAB_DIRECT);
2605 if (!temp)
2606 goto fail;
2607 if (temp != result)
2608 convert_move (result, temp, true);
2610 emit_label (after_label);
2611 convert_move (target, result, true);
2613 seq = get_insns ();
2614 end_sequence ();
2616 add_equal_note (seq, target, CLZ, xop0, 0);
2617 emit_insn (seq);
2618 return target;
2620 fail:
2621 end_sequence ();
2622 return 0;
2625 /* Try calculating
2626 (bswap:narrow x)
2628 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2629 static rtx
2630 widen_bswap (machine_mode mode, rtx op0, rtx target)
2632 enum mode_class mclass = GET_MODE_CLASS (mode);
2633 machine_mode wider_mode;
2634 rtx x;
2635 rtx_insn *last;
2637 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2638 return NULL_RTX;
2640 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2641 wider_mode != VOIDmode;
2642 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2643 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2644 goto found;
2645 return NULL_RTX;
2647 found:
2648 last = get_last_insn ();
2650 x = widen_operand (op0, wider_mode, mode, true, true);
2651 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2653 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2654 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2655 if (x != 0)
2656 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2657 GET_MODE_BITSIZE (wider_mode)
2658 - GET_MODE_BITSIZE (mode),
2659 NULL_RTX, true);
2661 if (x != 0)
2663 if (target == 0)
2664 target = gen_reg_rtx (mode);
2665 emit_move_insn (target, gen_lowpart (mode, x));
2667 else
2668 delete_insns_since (last);
2670 return target;
2673 /* Try calculating bswap as two bswaps of two word-sized operands. */
2675 static rtx
2676 expand_doubleword_bswap (machine_mode mode, rtx op, rtx target)
2678 rtx t0, t1;
2680 t1 = expand_unop (word_mode, bswap_optab,
2681 operand_subword_force (op, 0, mode), NULL_RTX, true);
2682 t0 = expand_unop (word_mode, bswap_optab,
2683 operand_subword_force (op, 1, mode), NULL_RTX, true);
2685 if (target == 0 || !valid_multiword_target_p (target))
2686 target = gen_reg_rtx (mode);
2687 if (REG_P (target))
2688 emit_clobber (target);
2689 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2690 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2692 return target;
2695 /* Try calculating (parity x) as (and (popcount x) 1), where
2696 popcount can also be done in a wider mode. */
2697 static rtx
2698 expand_parity (machine_mode mode, rtx op0, rtx target)
2700 enum mode_class mclass = GET_MODE_CLASS (mode);
2701 if (CLASS_HAS_WIDER_MODES_P (mclass))
2703 machine_mode wider_mode;
2704 for (wider_mode = mode; wider_mode != VOIDmode;
2705 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2707 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2709 rtx xop0, temp;
2710 rtx_insn *last;
2712 last = get_last_insn ();
2714 if (target == 0)
2715 target = gen_reg_rtx (mode);
2716 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2717 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2718 true);
2719 if (temp != 0)
2720 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2721 target, true, OPTAB_DIRECT);
2722 if (temp == 0)
2723 delete_insns_since (last);
2725 return temp;
2729 return 0;
2732 /* Try calculating ctz(x) as K - clz(x & -x) ,
2733 where K is GET_MODE_PRECISION(mode) - 1.
2735 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2736 don't have to worry about what the hardware does in that case. (If
2737 the clz instruction produces the usual value at 0, which is K, the
2738 result of this code sequence will be -1; expand_ffs, below, relies
2739 on this. It might be nice to have it be K instead, for consistency
2740 with the (very few) processors that provide a ctz with a defined
2741 value, but that would take one more instruction, and it would be
2742 less convenient for expand_ffs anyway. */
2744 static rtx
2745 expand_ctz (machine_mode mode, rtx op0, rtx target)
2747 rtx_insn *seq;
2748 rtx temp;
2750 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2751 return 0;
2753 start_sequence ();
2755 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2756 if (temp)
2757 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2758 true, OPTAB_DIRECT);
2759 if (temp)
2760 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2761 if (temp)
2762 temp = expand_binop (mode, sub_optab,
2763 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2764 temp, target,
2765 true, OPTAB_DIRECT);
2766 if (temp == 0)
2768 end_sequence ();
2769 return 0;
2772 seq = get_insns ();
2773 end_sequence ();
2775 add_equal_note (seq, temp, CTZ, op0, 0);
2776 emit_insn (seq);
2777 return temp;
2781 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2782 else with the sequence used by expand_clz.
2784 The ffs builtin promises to return zero for a zero value and ctz/clz
2785 may have an undefined value in that case. If they do not give us a
2786 convenient value, we have to generate a test and branch. */
2787 static rtx
2788 expand_ffs (machine_mode mode, rtx op0, rtx target)
2790 HOST_WIDE_INT val = 0;
2791 bool defined_at_zero = false;
2792 rtx temp;
2793 rtx_insn *seq;
2795 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2797 start_sequence ();
2799 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2800 if (!temp)
2801 goto fail;
2803 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2805 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2807 start_sequence ();
2808 temp = expand_ctz (mode, op0, 0);
2809 if (!temp)
2810 goto fail;
2812 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2814 defined_at_zero = true;
2815 val = (GET_MODE_PRECISION (mode) - 1) - val;
2818 else
2819 return 0;
2821 if (defined_at_zero && val == -1)
2822 /* No correction needed at zero. */;
2823 else
2825 /* We don't try to do anything clever with the situation found
2826 on some processors (eg Alpha) where ctz(0:mode) ==
2827 bitsize(mode). If someone can think of a way to send N to -1
2828 and leave alone all values in the range 0..N-1 (where N is a
2829 power of two), cheaper than this test-and-branch, please add it.
2831 The test-and-branch is done after the operation itself, in case
2832 the operation sets condition codes that can be recycled for this.
2833 (This is true on i386, for instance.) */
2835 rtx_code_label *nonzero_label = gen_label_rtx ();
2836 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2837 mode, true, nonzero_label);
2839 convert_move (temp, GEN_INT (-1), false);
2840 emit_label (nonzero_label);
2843 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2844 to produce a value in the range 0..bitsize. */
2845 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2846 target, false, OPTAB_DIRECT);
2847 if (!temp)
2848 goto fail;
2850 seq = get_insns ();
2851 end_sequence ();
2853 add_equal_note (seq, temp, FFS, op0, 0);
2854 emit_insn (seq);
2855 return temp;
2857 fail:
2858 end_sequence ();
2859 return 0;
2862 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2863 conditions, VAL may already be a SUBREG against which we cannot generate
2864 a further SUBREG. In this case, we expect forcing the value into a
2865 register will work around the situation. */
2867 static rtx
2868 lowpart_subreg_maybe_copy (machine_mode omode, rtx val,
2869 machine_mode imode)
2871 rtx ret;
2872 ret = lowpart_subreg (omode, val, imode);
2873 if (ret == NULL)
2875 val = force_reg (imode, val);
2876 ret = lowpart_subreg (omode, val, imode);
2877 gcc_assert (ret != NULL);
2879 return ret;
2882 /* Expand a floating point absolute value or negation operation via a
2883 logical operation on the sign bit. */
2885 static rtx
2886 expand_absneg_bit (enum rtx_code code, machine_mode mode,
2887 rtx op0, rtx target)
2889 const struct real_format *fmt;
2890 int bitpos, word, nwords, i;
2891 machine_mode imode;
2892 rtx temp;
2893 rtx_insn *insns;
2895 /* The format has to have a simple sign bit. */
2896 fmt = REAL_MODE_FORMAT (mode);
2897 if (fmt == NULL)
2898 return NULL_RTX;
2900 bitpos = fmt->signbit_rw;
2901 if (bitpos < 0)
2902 return NULL_RTX;
2904 /* Don't create negative zeros if the format doesn't support them. */
2905 if (code == NEG && !fmt->has_signed_zero)
2906 return NULL_RTX;
2908 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2910 imode = int_mode_for_mode (mode);
2911 if (imode == BLKmode)
2912 return NULL_RTX;
2913 word = 0;
2914 nwords = 1;
2916 else
2918 imode = word_mode;
2920 if (FLOAT_WORDS_BIG_ENDIAN)
2921 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2922 else
2923 word = bitpos / BITS_PER_WORD;
2924 bitpos = bitpos % BITS_PER_WORD;
2925 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2928 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
2929 if (code == ABS)
2930 mask = ~mask;
2932 if (target == 0
2933 || target == op0
2934 || (nwords > 1 && !valid_multiword_target_p (target)))
2935 target = gen_reg_rtx (mode);
2937 if (nwords > 1)
2939 start_sequence ();
2941 for (i = 0; i < nwords; ++i)
2943 rtx targ_piece = operand_subword (target, i, 1, mode);
2944 rtx op0_piece = operand_subword_force (op0, i, mode);
2946 if (i == word)
2948 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2949 op0_piece,
2950 immed_wide_int_const (mask, imode),
2951 targ_piece, 1, OPTAB_LIB_WIDEN);
2952 if (temp != targ_piece)
2953 emit_move_insn (targ_piece, temp);
2955 else
2956 emit_move_insn (targ_piece, op0_piece);
2959 insns = get_insns ();
2960 end_sequence ();
2962 emit_insn (insns);
2964 else
2966 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2967 gen_lowpart (imode, op0),
2968 immed_wide_int_const (mask, imode),
2969 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2970 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2972 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2973 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2974 target);
2977 return target;
2980 /* As expand_unop, but will fail rather than attempt the operation in a
2981 different mode or with a libcall. */
2982 static rtx
2983 expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
2984 int unsignedp)
2986 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2988 struct expand_operand ops[2];
2989 enum insn_code icode = optab_handler (unoptab, mode);
2990 rtx_insn *last = get_last_insn ();
2991 rtx_insn *pat;
2993 create_output_operand (&ops[0], target, mode);
2994 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2995 pat = maybe_gen_insn (icode, 2, ops);
2996 if (pat)
2998 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2999 && ! add_equal_note (pat, ops[0].value,
3000 optab_to_code (unoptab),
3001 ops[1].value, NULL_RTX))
3003 delete_insns_since (last);
3004 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3007 emit_insn (pat);
3009 return ops[0].value;
3012 return 0;
3015 /* Generate code to perform an operation specified by UNOPTAB
3016 on operand OP0, with result having machine-mode MODE.
3018 UNSIGNEDP is for the case where we have to widen the operands
3019 to perform the operation. It says to use zero-extension.
3021 If TARGET is nonzero, the value
3022 is generated there, if it is convenient to do so.
3023 In all cases an rtx is returned for the locus of the value;
3024 this may or may not be TARGET. */
3027 expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
3028 int unsignedp)
3030 enum mode_class mclass = GET_MODE_CLASS (mode);
3031 machine_mode wider_mode;
3032 rtx temp;
3033 rtx libfunc;
3035 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3036 if (temp)
3037 return temp;
3039 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3041 /* Widening (or narrowing) clz needs special treatment. */
3042 if (unoptab == clz_optab)
3044 temp = widen_leading (mode, op0, target, unoptab);
3045 if (temp)
3046 return temp;
3048 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3049 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3051 temp = expand_doubleword_clz (mode, op0, target);
3052 if (temp)
3053 return temp;
3056 goto try_libcall;
3059 if (unoptab == clrsb_optab)
3061 temp = widen_leading (mode, op0, target, unoptab);
3062 if (temp)
3063 return temp;
3064 goto try_libcall;
3067 /* Widening (or narrowing) bswap needs special treatment. */
3068 if (unoptab == bswap_optab)
3070 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3071 or ROTATERT. First try these directly; if this fails, then try the
3072 obvious pair of shifts with allowed widening, as this will probably
3073 be always more efficient than the other fallback methods. */
3074 if (mode == HImode)
3076 rtx_insn *last;
3077 rtx temp1, temp2;
3079 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3081 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
3082 unsignedp, OPTAB_DIRECT);
3083 if (temp)
3084 return temp;
3087 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3089 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
3090 unsignedp, OPTAB_DIRECT);
3091 if (temp)
3092 return temp;
3095 last = get_last_insn ();
3097 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
3098 unsignedp, OPTAB_WIDEN);
3099 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
3100 unsignedp, OPTAB_WIDEN);
3101 if (temp1 && temp2)
3103 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3104 unsignedp, OPTAB_WIDEN);
3105 if (temp)
3106 return temp;
3109 delete_insns_since (last);
3112 temp = widen_bswap (mode, op0, target);
3113 if (temp)
3114 return temp;
3116 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3117 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3119 temp = expand_doubleword_bswap (mode, op0, target);
3120 if (temp)
3121 return temp;
3124 goto try_libcall;
3127 if (CLASS_HAS_WIDER_MODES_P (mclass))
3128 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3129 wider_mode != VOIDmode;
3130 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3132 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3134 rtx xop0 = op0;
3135 rtx_insn *last = get_last_insn ();
3137 /* For certain operations, we need not actually extend
3138 the narrow operand, as long as we will truncate the
3139 results to the same narrowness. */
3141 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3142 (unoptab == neg_optab
3143 || unoptab == one_cmpl_optab)
3144 && mclass == MODE_INT);
3146 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3147 unsignedp);
3149 if (temp)
3151 if (mclass != MODE_INT
3152 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3154 if (target == 0)
3155 target = gen_reg_rtx (mode);
3156 convert_move (target, temp, 0);
3157 return target;
3159 else
3160 return gen_lowpart (mode, temp);
3162 else
3163 delete_insns_since (last);
3167 /* These can be done a word at a time. */
3168 if (unoptab == one_cmpl_optab
3169 && mclass == MODE_INT
3170 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3171 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3173 int i;
3174 rtx_insn *insns;
3176 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3177 target = gen_reg_rtx (mode);
3179 start_sequence ();
3181 /* Do the actual arithmetic. */
3182 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3184 rtx target_piece = operand_subword (target, i, 1, mode);
3185 rtx x = expand_unop (word_mode, unoptab,
3186 operand_subword_force (op0, i, mode),
3187 target_piece, unsignedp);
3189 if (target_piece != x)
3190 emit_move_insn (target_piece, x);
3193 insns = get_insns ();
3194 end_sequence ();
3196 emit_insn (insns);
3197 return target;
3200 if (optab_to_code (unoptab) == NEG)
3202 /* Try negating floating point values by flipping the sign bit. */
3203 if (SCALAR_FLOAT_MODE_P (mode))
3205 temp = expand_absneg_bit (NEG, mode, op0, target);
3206 if (temp)
3207 return temp;
3210 /* If there is no negation pattern, and we have no negative zero,
3211 try subtracting from zero. */
3212 if (!HONOR_SIGNED_ZEROS (mode))
3214 temp = expand_binop (mode, (unoptab == negv_optab
3215 ? subv_optab : sub_optab),
3216 CONST0_RTX (mode), op0, target,
3217 unsignedp, OPTAB_DIRECT);
3218 if (temp)
3219 return temp;
3223 /* Try calculating parity (x) as popcount (x) % 2. */
3224 if (unoptab == parity_optab)
3226 temp = expand_parity (mode, op0, target);
3227 if (temp)
3228 return temp;
3231 /* Try implementing ffs (x) in terms of clz (x). */
3232 if (unoptab == ffs_optab)
3234 temp = expand_ffs (mode, op0, target);
3235 if (temp)
3236 return temp;
3239 /* Try implementing ctz (x) in terms of clz (x). */
3240 if (unoptab == ctz_optab)
3242 temp = expand_ctz (mode, op0, target);
3243 if (temp)
3244 return temp;
3247 try_libcall:
3248 /* Now try a library call in this mode. */
3249 libfunc = optab_libfunc (unoptab, mode);
3250 if (libfunc)
3252 rtx_insn *insns;
3253 rtx value;
3254 rtx eq_value;
3255 machine_mode outmode = mode;
3257 /* All of these functions return small values. Thus we choose to
3258 have them return something that isn't a double-word. */
3259 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3260 || unoptab == clrsb_optab || unoptab == popcount_optab
3261 || unoptab == parity_optab)
3262 outmode
3263 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3264 optab_libfunc (unoptab, mode)));
3266 start_sequence ();
3268 /* Pass 1 for NO_QUEUE so we don't lose any increments
3269 if the libcall is cse'd or moved. */
3270 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3271 1, op0, mode);
3272 insns = get_insns ();
3273 end_sequence ();
3275 target = gen_reg_rtx (outmode);
3276 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3277 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3278 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3279 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3280 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3281 emit_libcall_block_1 (insns, target, value, eq_value,
3282 trapv_unoptab_p (unoptab));
3284 return target;
3287 /* It can't be done in this mode. Can we do it in a wider mode? */
3289 if (CLASS_HAS_WIDER_MODES_P (mclass))
3291 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3292 wider_mode != VOIDmode;
3293 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3295 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3296 || optab_libfunc (unoptab, wider_mode))
3298 rtx xop0 = op0;
3299 rtx_insn *last = get_last_insn ();
3301 /* For certain operations, we need not actually extend
3302 the narrow operand, as long as we will truncate the
3303 results to the same narrowness. */
3304 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3305 (unoptab == neg_optab
3306 || unoptab == one_cmpl_optab
3307 || unoptab == bswap_optab)
3308 && mclass == MODE_INT);
3310 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3311 unsignedp);
3313 /* If we are generating clz using wider mode, adjust the
3314 result. Similarly for clrsb. */
3315 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3316 && temp != 0)
3317 temp = expand_binop
3318 (wider_mode, sub_optab, temp,
3319 gen_int_mode (GET_MODE_PRECISION (wider_mode)
3320 - GET_MODE_PRECISION (mode),
3321 wider_mode),
3322 target, true, OPTAB_DIRECT);
3324 /* Likewise for bswap. */
3325 if (unoptab == bswap_optab && temp != 0)
3327 gcc_assert (GET_MODE_PRECISION (wider_mode)
3328 == GET_MODE_BITSIZE (wider_mode)
3329 && GET_MODE_PRECISION (mode)
3330 == GET_MODE_BITSIZE (mode));
3332 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3333 GET_MODE_BITSIZE (wider_mode)
3334 - GET_MODE_BITSIZE (mode),
3335 NULL_RTX, true);
3338 if (temp)
3340 if (mclass != MODE_INT)
3342 if (target == 0)
3343 target = gen_reg_rtx (mode);
3344 convert_move (target, temp, 0);
3345 return target;
3347 else
3348 return gen_lowpart (mode, temp);
3350 else
3351 delete_insns_since (last);
3356 /* One final attempt at implementing negation via subtraction,
3357 this time allowing widening of the operand. */
3358 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3360 rtx temp;
3361 temp = expand_binop (mode,
3362 unoptab == negv_optab ? subv_optab : sub_optab,
3363 CONST0_RTX (mode), op0,
3364 target, unsignedp, OPTAB_LIB_WIDEN);
3365 if (temp)
3366 return temp;
3369 return 0;
3372 /* Emit code to compute the absolute value of OP0, with result to
3373 TARGET if convenient. (TARGET may be 0.) The return value says
3374 where the result actually is to be found.
3376 MODE is the mode of the operand; the mode of the result is
3377 different but can be deduced from MODE.
3382 expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
3383 int result_unsignedp)
3385 rtx temp;
3387 if (GET_MODE_CLASS (mode) != MODE_INT
3388 || ! flag_trapv)
3389 result_unsignedp = 1;
3391 /* First try to do it with a special abs instruction. */
3392 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3393 op0, target, 0);
3394 if (temp != 0)
3395 return temp;
3397 /* For floating point modes, try clearing the sign bit. */
3398 if (SCALAR_FLOAT_MODE_P (mode))
3400 temp = expand_absneg_bit (ABS, mode, op0, target);
3401 if (temp)
3402 return temp;
3405 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3406 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3407 && !HONOR_SIGNED_ZEROS (mode))
3409 rtx_insn *last = get_last_insn ();
3411 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3412 op0, NULL_RTX, 0);
3413 if (temp != 0)
3414 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3415 OPTAB_WIDEN);
3417 if (temp != 0)
3418 return temp;
3420 delete_insns_since (last);
3423 /* If this machine has expensive jumps, we can do integer absolute
3424 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3425 where W is the width of MODE. */
3427 if (GET_MODE_CLASS (mode) == MODE_INT
3428 && BRANCH_COST (optimize_insn_for_speed_p (),
3429 false) >= 2)
3431 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3432 GET_MODE_PRECISION (mode) - 1,
3433 NULL_RTX, 0);
3435 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3436 OPTAB_LIB_WIDEN);
3437 if (temp != 0)
3438 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3439 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3441 if (temp != 0)
3442 return temp;
3445 return NULL_RTX;
3449 expand_abs (machine_mode mode, rtx op0, rtx target,
3450 int result_unsignedp, int safe)
3452 rtx temp;
3453 rtx_code_label *op1;
3455 if (GET_MODE_CLASS (mode) != MODE_INT
3456 || ! flag_trapv)
3457 result_unsignedp = 1;
3459 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3460 if (temp != 0)
3461 return temp;
3463 /* If that does not win, use conditional jump and negate. */
3465 /* It is safe to use the target if it is the same
3466 as the source if this is also a pseudo register */
3467 if (op0 == target && REG_P (op0)
3468 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3469 safe = 1;
3471 op1 = gen_label_rtx ();
3472 if (target == 0 || ! safe
3473 || GET_MODE (target) != mode
3474 || (MEM_P (target) && MEM_VOLATILE_P (target))
3475 || (REG_P (target)
3476 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3477 target = gen_reg_rtx (mode);
3479 emit_move_insn (target, op0);
3480 NO_DEFER_POP;
3482 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3483 NULL_RTX, NULL, op1, -1);
3485 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3486 target, target, 0);
3487 if (op0 != target)
3488 emit_move_insn (target, op0);
3489 emit_label (op1);
3490 OK_DEFER_POP;
3491 return target;
3494 /* Emit code to compute the one's complement absolute value of OP0
3495 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3496 (TARGET may be NULL_RTX.) The return value says where the result
3497 actually is to be found.
3499 MODE is the mode of the operand; the mode of the result is
3500 different but can be deduced from MODE. */
3503 expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
3505 rtx temp;
3507 /* Not applicable for floating point modes. */
3508 if (FLOAT_MODE_P (mode))
3509 return NULL_RTX;
3511 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3512 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3514 rtx_insn *last = get_last_insn ();
3516 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3517 if (temp != 0)
3518 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3519 OPTAB_WIDEN);
3521 if (temp != 0)
3522 return temp;
3524 delete_insns_since (last);
3527 /* If this machine has expensive jumps, we can do one's complement
3528 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3530 if (GET_MODE_CLASS (mode) == MODE_INT
3531 && BRANCH_COST (optimize_insn_for_speed_p (),
3532 false) >= 2)
3534 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3535 GET_MODE_PRECISION (mode) - 1,
3536 NULL_RTX, 0);
3538 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3539 OPTAB_LIB_WIDEN);
3541 if (temp != 0)
3542 return temp;
3545 return NULL_RTX;
3548 /* A subroutine of expand_copysign, perform the copysign operation using the
3549 abs and neg primitives advertised to exist on the target. The assumption
3550 is that we have a split register file, and leaving op0 in fp registers,
3551 and not playing with subregs so much, will help the register allocator. */
3553 static rtx
3554 expand_copysign_absneg (machine_mode mode, rtx op0, rtx op1, rtx target,
3555 int bitpos, bool op0_is_abs)
3557 machine_mode imode;
3558 enum insn_code icode;
3559 rtx sign;
3560 rtx_code_label *label;
3562 if (target == op1)
3563 target = NULL_RTX;
3565 /* Check if the back end provides an insn that handles signbit for the
3566 argument's mode. */
3567 icode = optab_handler (signbit_optab, mode);
3568 if (icode != CODE_FOR_nothing)
3570 imode = insn_data[(int) icode].operand[0].mode;
3571 sign = gen_reg_rtx (imode);
3572 emit_unop_insn (icode, sign, op1, UNKNOWN);
3574 else
3576 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3578 imode = int_mode_for_mode (mode);
3579 if (imode == BLKmode)
3580 return NULL_RTX;
3581 op1 = gen_lowpart (imode, op1);
3583 else
3585 int word;
3587 imode = word_mode;
3588 if (FLOAT_WORDS_BIG_ENDIAN)
3589 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3590 else
3591 word = bitpos / BITS_PER_WORD;
3592 bitpos = bitpos % BITS_PER_WORD;
3593 op1 = operand_subword_force (op1, word, mode);
3596 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3597 sign = expand_binop (imode, and_optab, op1,
3598 immed_wide_int_const (mask, imode),
3599 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3602 if (!op0_is_abs)
3604 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3605 if (op0 == NULL)
3606 return NULL_RTX;
3607 target = op0;
3609 else
3611 if (target == NULL_RTX)
3612 target = copy_to_reg (op0);
3613 else
3614 emit_move_insn (target, op0);
3617 label = gen_label_rtx ();
3618 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3620 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3621 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3622 else
3623 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3624 if (op0 != target)
3625 emit_move_insn (target, op0);
3627 emit_label (label);
3629 return target;
3633 /* A subroutine of expand_copysign, perform the entire copysign operation
3634 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3635 is true if op0 is known to have its sign bit clear. */
3637 static rtx
3638 expand_copysign_bit (machine_mode mode, rtx op0, rtx op1, rtx target,
3639 int bitpos, bool op0_is_abs)
3641 machine_mode imode;
3642 int word, nwords, i;
3643 rtx temp;
3644 rtx_insn *insns;
3646 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3648 imode = int_mode_for_mode (mode);
3649 if (imode == BLKmode)
3650 return NULL_RTX;
3651 word = 0;
3652 nwords = 1;
3654 else
3656 imode = word_mode;
3658 if (FLOAT_WORDS_BIG_ENDIAN)
3659 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3660 else
3661 word = bitpos / BITS_PER_WORD;
3662 bitpos = bitpos % BITS_PER_WORD;
3663 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3666 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3668 if (target == 0
3669 || target == op0
3670 || target == op1
3671 || (nwords > 1 && !valid_multiword_target_p (target)))
3672 target = gen_reg_rtx (mode);
3674 if (nwords > 1)
3676 start_sequence ();
3678 for (i = 0; i < nwords; ++i)
3680 rtx targ_piece = operand_subword (target, i, 1, mode);
3681 rtx op0_piece = operand_subword_force (op0, i, mode);
3683 if (i == word)
3685 if (!op0_is_abs)
3686 op0_piece
3687 = expand_binop (imode, and_optab, op0_piece,
3688 immed_wide_int_const (~mask, imode),
3689 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3690 op1 = expand_binop (imode, and_optab,
3691 operand_subword_force (op1, i, mode),
3692 immed_wide_int_const (mask, imode),
3693 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3695 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3696 targ_piece, 1, OPTAB_LIB_WIDEN);
3697 if (temp != targ_piece)
3698 emit_move_insn (targ_piece, temp);
3700 else
3701 emit_move_insn (targ_piece, op0_piece);
3704 insns = get_insns ();
3705 end_sequence ();
3707 emit_insn (insns);
3709 else
3711 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3712 immed_wide_int_const (mask, imode),
3713 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3715 op0 = gen_lowpart (imode, op0);
3716 if (!op0_is_abs)
3717 op0 = expand_binop (imode, and_optab, op0,
3718 immed_wide_int_const (~mask, imode),
3719 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3721 temp = expand_binop (imode, ior_optab, op0, op1,
3722 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3723 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3726 return target;
3729 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3730 scalar floating point mode. Return NULL if we do not know how to
3731 expand the operation inline. */
3734 expand_copysign (rtx op0, rtx op1, rtx target)
3736 machine_mode mode = GET_MODE (op0);
3737 const struct real_format *fmt;
3738 bool op0_is_abs;
3739 rtx temp;
3741 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3742 gcc_assert (GET_MODE (op1) == mode);
3744 /* First try to do it with a special instruction. */
3745 temp = expand_binop (mode, copysign_optab, op0, op1,
3746 target, 0, OPTAB_DIRECT);
3747 if (temp)
3748 return temp;
3750 fmt = REAL_MODE_FORMAT (mode);
3751 if (fmt == NULL || !fmt->has_signed_zero)
3752 return NULL_RTX;
3754 op0_is_abs = false;
3755 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3757 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3758 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3759 op0_is_abs = true;
3762 if (fmt->signbit_ro >= 0
3763 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3764 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3765 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3767 temp = expand_copysign_absneg (mode, op0, op1, target,
3768 fmt->signbit_ro, op0_is_abs);
3769 if (temp)
3770 return temp;
3773 if (fmt->signbit_rw < 0)
3774 return NULL_RTX;
3775 return expand_copysign_bit (mode, op0, op1, target,
3776 fmt->signbit_rw, op0_is_abs);
3779 /* Generate an instruction whose insn-code is INSN_CODE,
3780 with two operands: an output TARGET and an input OP0.
3781 TARGET *must* be nonzero, and the output is always stored there.
3782 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3783 the value that is stored into TARGET.
3785 Return false if expansion failed. */
3787 bool
3788 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3789 enum rtx_code code)
3791 struct expand_operand ops[2];
3792 rtx_insn *pat;
3794 create_output_operand (&ops[0], target, GET_MODE (target));
3795 create_input_operand (&ops[1], op0, GET_MODE (op0));
3796 pat = maybe_gen_insn (icode, 2, ops);
3797 if (!pat)
3798 return false;
3800 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3801 && code != UNKNOWN)
3802 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3804 emit_insn (pat);
3806 if (ops[0].value != target)
3807 emit_move_insn (target, ops[0].value);
3808 return true;
3810 /* Generate an instruction whose insn-code is INSN_CODE,
3811 with two operands: an output TARGET and an input OP0.
3812 TARGET *must* be nonzero, and the output is always stored there.
3813 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3814 the value that is stored into TARGET. */
3816 void
3817 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3819 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3820 gcc_assert (ok);
3823 struct no_conflict_data
3825 rtx target;
3826 rtx_insn *first, *insn;
3827 bool must_stay;
3830 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3831 the currently examined clobber / store has to stay in the list of
3832 insns that constitute the actual libcall block. */
3833 static void
3834 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3836 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3838 /* If this inns directly contributes to setting the target, it must stay. */
3839 if (reg_overlap_mentioned_p (p->target, dest))
3840 p->must_stay = true;
3841 /* If we haven't committed to keeping any other insns in the list yet,
3842 there is nothing more to check. */
3843 else if (p->insn == p->first)
3844 return;
3845 /* If this insn sets / clobbers a register that feeds one of the insns
3846 already in the list, this insn has to stay too. */
3847 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3848 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3849 || reg_used_between_p (dest, p->first, p->insn)
3850 /* Likewise if this insn depends on a register set by a previous
3851 insn in the list, or if it sets a result (presumably a hard
3852 register) that is set or clobbered by a previous insn.
3853 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3854 SET_DEST perform the former check on the address, and the latter
3855 check on the MEM. */
3856 || (GET_CODE (set) == SET
3857 && (modified_in_p (SET_SRC (set), p->first)
3858 || modified_in_p (SET_DEST (set), p->first)
3859 || modified_between_p (SET_SRC (set), p->first, p->insn)
3860 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3861 p->must_stay = true;
3865 /* Emit code to make a call to a constant function or a library call.
3867 INSNS is a list containing all insns emitted in the call.
3868 These insns leave the result in RESULT. Our block is to copy RESULT
3869 to TARGET, which is logically equivalent to EQUIV.
3871 We first emit any insns that set a pseudo on the assumption that these are
3872 loading constants into registers; doing so allows them to be safely cse'ed
3873 between blocks. Then we emit all the other insns in the block, followed by
3874 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3875 note with an operand of EQUIV. */
3877 static void
3878 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
3879 bool equiv_may_trap)
3881 rtx final_dest = target;
3882 rtx_insn *next, *last, *insn;
3884 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3885 into a MEM later. Protect the libcall block from this change. */
3886 if (! REG_P (target) || REG_USERVAR_P (target))
3887 target = gen_reg_rtx (GET_MODE (target));
3889 /* If we're using non-call exceptions, a libcall corresponding to an
3890 operation that may trap may also trap. */
3891 /* ??? See the comment in front of make_reg_eh_region_note. */
3892 if (cfun->can_throw_non_call_exceptions
3893 && (equiv_may_trap || may_trap_p (equiv)))
3895 for (insn = insns; insn; insn = NEXT_INSN (insn))
3896 if (CALL_P (insn))
3898 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3899 if (note)
3901 int lp_nr = INTVAL (XEXP (note, 0));
3902 if (lp_nr == 0 || lp_nr == INT_MIN)
3903 remove_note (insn, note);
3907 else
3909 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3910 reg note to indicate that this call cannot throw or execute a nonlocal
3911 goto (unless there is already a REG_EH_REGION note, in which case
3912 we update it). */
3913 for (insn = insns; insn; insn = NEXT_INSN (insn))
3914 if (CALL_P (insn))
3915 make_reg_eh_region_note_nothrow_nononlocal (insn);
3918 /* First emit all insns that set pseudos. Remove them from the list as
3919 we go. Avoid insns that set pseudos which were referenced in previous
3920 insns. These can be generated by move_by_pieces, for example,
3921 to update an address. Similarly, avoid insns that reference things
3922 set in previous insns. */
3924 for (insn = insns; insn; insn = next)
3926 rtx set = single_set (insn);
3928 next = NEXT_INSN (insn);
3930 if (set != 0 && REG_P (SET_DEST (set))
3931 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3933 struct no_conflict_data data;
3935 data.target = const0_rtx;
3936 data.first = insns;
3937 data.insn = insn;
3938 data.must_stay = 0;
3939 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3940 if (! data.must_stay)
3942 if (PREV_INSN (insn))
3943 SET_NEXT_INSN (PREV_INSN (insn)) = next;
3944 else
3945 insns = next;
3947 if (next)
3948 SET_PREV_INSN (next) = PREV_INSN (insn);
3950 add_insn (insn);
3954 /* Some ports use a loop to copy large arguments onto the stack.
3955 Don't move anything outside such a loop. */
3956 if (LABEL_P (insn))
3957 break;
3960 /* Write the remaining insns followed by the final copy. */
3961 for (insn = insns; insn; insn = next)
3963 next = NEXT_INSN (insn);
3965 add_insn (insn);
3968 last = emit_move_insn (target, result);
3969 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3971 if (final_dest != target)
3972 emit_move_insn (final_dest, target);
3975 void
3976 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3978 emit_libcall_block_1 (safe_as_a <rtx_insn *> (insns),
3979 target, result, equiv, false);
3982 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3983 PURPOSE describes how this comparison will be used. CODE is the rtx
3984 comparison code we will be using.
3986 ??? Actually, CODE is slightly weaker than that. A target is still
3987 required to implement all of the normal bcc operations, but not
3988 required to implement all (or any) of the unordered bcc operations. */
3991 can_compare_p (enum rtx_code code, machine_mode mode,
3992 enum can_compare_purpose purpose)
3994 rtx test;
3995 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3998 enum insn_code icode;
4000 if (purpose == ccp_jump
4001 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
4002 && insn_operand_matches (icode, 0, test))
4003 return 1;
4004 if (purpose == ccp_store_flag
4005 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
4006 && insn_operand_matches (icode, 1, test))
4007 return 1;
4008 if (purpose == ccp_cmov
4009 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
4010 return 1;
4012 mode = GET_MODE_WIDER_MODE (mode);
4013 PUT_MODE (test, mode);
4015 while (mode != VOIDmode);
4017 return 0;
4020 /* This function is called when we are going to emit a compare instruction that
4021 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
4023 *PMODE is the mode of the inputs (in case they are const_int).
4024 *PUNSIGNEDP nonzero says that the operands are unsigned;
4025 this matters if they need to be widened (as given by METHODS).
4027 If they have mode BLKmode, then SIZE specifies the size of both operands.
4029 This function performs all the setup necessary so that the caller only has
4030 to emit a single comparison insn. This setup can involve doing a BLKmode
4031 comparison or emitting a library call to perform the comparison if no insn
4032 is available to handle it.
4033 The values which are passed in through pointers can be modified; the caller
4034 should perform the comparison on the modified values. Constant
4035 comparisons must have already been folded. */
4037 static void
4038 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4039 int unsignedp, enum optab_methods methods,
4040 rtx *ptest, machine_mode *pmode)
4042 machine_mode mode = *pmode;
4043 rtx libfunc, test;
4044 machine_mode cmp_mode;
4045 enum mode_class mclass;
4047 /* The other methods are not needed. */
4048 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4049 || methods == OPTAB_LIB_WIDEN);
4051 /* If we are optimizing, force expensive constants into a register. */
4052 if (CONSTANT_P (x) && optimize
4053 && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
4054 > COSTS_N_INSNS (1)))
4055 x = force_reg (mode, x);
4057 if (CONSTANT_P (y) && optimize
4058 && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
4059 > COSTS_N_INSNS (1)))
4060 y = force_reg (mode, y);
4062 #if HAVE_cc0
4063 /* Make sure if we have a canonical comparison. The RTL
4064 documentation states that canonical comparisons are required only
4065 for targets which have cc0. */
4066 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4067 #endif
4069 /* Don't let both operands fail to indicate the mode. */
4070 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4071 x = force_reg (mode, x);
4072 if (mode == VOIDmode)
4073 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4075 /* Handle all BLKmode compares. */
4077 if (mode == BLKmode)
4079 machine_mode result_mode;
4080 enum insn_code cmp_code;
4081 tree length_type;
4082 rtx libfunc;
4083 rtx result;
4084 rtx opalign
4085 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4087 gcc_assert (size);
4089 /* Try to use a memory block compare insn - either cmpstr
4090 or cmpmem will do. */
4091 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4092 cmp_mode != VOIDmode;
4093 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4095 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4096 if (cmp_code == CODE_FOR_nothing)
4097 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4098 if (cmp_code == CODE_FOR_nothing)
4099 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4100 if (cmp_code == CODE_FOR_nothing)
4101 continue;
4103 /* Must make sure the size fits the insn's mode. */
4104 if ((CONST_INT_P (size)
4105 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4106 || (GET_MODE_BITSIZE (GET_MODE (size))
4107 > GET_MODE_BITSIZE (cmp_mode)))
4108 continue;
4110 result_mode = insn_data[cmp_code].operand[0].mode;
4111 result = gen_reg_rtx (result_mode);
4112 size = convert_to_mode (cmp_mode, size, 1);
4113 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4115 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4116 *pmode = result_mode;
4117 return;
4120 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4121 goto fail;
4123 /* Otherwise call a library function, memcmp. */
4124 libfunc = memcmp_libfunc;
4125 length_type = sizetype;
4126 result_mode = TYPE_MODE (integer_type_node);
4127 cmp_mode = TYPE_MODE (length_type);
4128 size = convert_to_mode (TYPE_MODE (length_type), size,
4129 TYPE_UNSIGNED (length_type));
4131 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4132 result_mode, 3,
4133 XEXP (x, 0), Pmode,
4134 XEXP (y, 0), Pmode,
4135 size, cmp_mode);
4136 x = result;
4137 y = const0_rtx;
4138 mode = result_mode;
4139 methods = OPTAB_LIB_WIDEN;
4140 unsignedp = false;
4143 /* Don't allow operands to the compare to trap, as that can put the
4144 compare and branch in different basic blocks. */
4145 if (cfun->can_throw_non_call_exceptions)
4147 if (may_trap_p (x))
4148 x = force_reg (mode, x);
4149 if (may_trap_p (y))
4150 y = force_reg (mode, y);
4153 if (GET_MODE_CLASS (mode) == MODE_CC)
4155 enum insn_code icode = optab_handler (cbranch_optab, CCmode);
4156 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4157 gcc_assert (icode != CODE_FOR_nothing
4158 && insn_operand_matches (icode, 0, test));
4159 *ptest = test;
4160 return;
4163 mclass = GET_MODE_CLASS (mode);
4164 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4165 cmp_mode = mode;
4168 enum insn_code icode;
4169 icode = optab_handler (cbranch_optab, cmp_mode);
4170 if (icode != CODE_FOR_nothing
4171 && insn_operand_matches (icode, 0, test))
4173 rtx_insn *last = get_last_insn ();
4174 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4175 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4176 if (op0 && op1
4177 && insn_operand_matches (icode, 1, op0)
4178 && insn_operand_matches (icode, 2, op1))
4180 XEXP (test, 0) = op0;
4181 XEXP (test, 1) = op1;
4182 *ptest = test;
4183 *pmode = cmp_mode;
4184 return;
4186 delete_insns_since (last);
4189 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4190 break;
4191 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4193 while (cmp_mode != VOIDmode);
4195 if (methods != OPTAB_LIB_WIDEN)
4196 goto fail;
4198 if (!SCALAR_FLOAT_MODE_P (mode))
4200 rtx result;
4201 machine_mode ret_mode;
4203 /* Handle a libcall just for the mode we are using. */
4204 libfunc = optab_libfunc (cmp_optab, mode);
4205 gcc_assert (libfunc);
4207 /* If we want unsigned, and this mode has a distinct unsigned
4208 comparison routine, use that. */
4209 if (unsignedp)
4211 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4212 if (ulibfunc)
4213 libfunc = ulibfunc;
4216 ret_mode = targetm.libgcc_cmp_return_mode ();
4217 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4218 ret_mode, 2, x, mode, y, mode);
4220 /* There are two kinds of comparison routines. Biased routines
4221 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4222 of gcc expect that the comparison operation is equivalent
4223 to the modified comparison. For signed comparisons compare the
4224 result against 1 in the biased case, and zero in the unbiased
4225 case. For unsigned comparisons always compare against 1 after
4226 biasing the unbiased result by adding 1. This gives us a way to
4227 represent LTU.
4228 The comparisons in the fixed-point helper library are always
4229 biased. */
4230 x = result;
4231 y = const1_rtx;
4233 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4235 if (unsignedp)
4236 x = plus_constant (ret_mode, result, 1);
4237 else
4238 y = const0_rtx;
4241 *pmode = ret_mode;
4242 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4243 ptest, pmode);
4245 else
4246 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4248 return;
4250 fail:
4251 *ptest = NULL_RTX;
4254 /* Before emitting an insn with code ICODE, make sure that X, which is going
4255 to be used for operand OPNUM of the insn, is converted from mode MODE to
4256 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4257 that it is accepted by the operand predicate. Return the new value. */
4260 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
4261 machine_mode wider_mode, int unsignedp)
4263 if (mode != wider_mode)
4264 x = convert_modes (wider_mode, mode, x, unsignedp);
4266 if (!insn_operand_matches (icode, opnum, x))
4268 machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
4269 if (reload_completed)
4270 return NULL_RTX;
4271 if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
4272 return NULL_RTX;
4273 x = copy_to_mode_reg (op_mode, x);
4276 return x;
4279 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4280 we can do the branch. */
4282 static void
4283 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label, int prob)
4285 machine_mode optab_mode;
4286 enum mode_class mclass;
4287 enum insn_code icode;
4288 rtx_insn *insn;
4290 mclass = GET_MODE_CLASS (mode);
4291 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4292 icode = optab_handler (cbranch_optab, optab_mode);
4294 gcc_assert (icode != CODE_FOR_nothing);
4295 gcc_assert (insn_operand_matches (icode, 0, test));
4296 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4297 XEXP (test, 1), label));
4298 if (prob != -1
4299 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4300 && insn
4301 && JUMP_P (insn)
4302 && any_condjump_p (insn)
4303 && !find_reg_note (insn, REG_BR_PROB, 0))
4304 add_int_reg_note (insn, REG_BR_PROB, prob);
4307 /* Generate code to compare X with Y so that the condition codes are
4308 set and to jump to LABEL if the condition is true. If X is a
4309 constant and Y is not a constant, then the comparison is swapped to
4310 ensure that the comparison RTL has the canonical form.
4312 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4313 need to be widened. UNSIGNEDP is also used to select the proper
4314 branch condition code.
4316 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4318 MODE is the mode of the inputs (in case they are const_int).
4320 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4321 It will be potentially converted into an unsigned variant based on
4322 UNSIGNEDP to select a proper jump instruction.
4324 PROB is the probability of jumping to LABEL. */
4326 void
4327 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4328 machine_mode mode, int unsignedp, rtx label,
4329 int prob)
4331 rtx op0 = x, op1 = y;
4332 rtx test;
4334 /* Swap operands and condition to ensure canonical RTL. */
4335 if (swap_commutative_operands_p (x, y)
4336 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4338 op0 = y, op1 = x;
4339 comparison = swap_condition (comparison);
4342 /* If OP0 is still a constant, then both X and Y must be constants
4343 or the opposite comparison is not supported. Force X into a register
4344 to create canonical RTL. */
4345 if (CONSTANT_P (op0))
4346 op0 = force_reg (mode, op0);
4348 if (unsignedp)
4349 comparison = unsigned_condition (comparison);
4351 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4352 &test, &mode);
4353 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4357 /* Emit a library call comparison between floating point X and Y.
4358 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4360 static void
4361 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4362 rtx *ptest, machine_mode *pmode)
4364 enum rtx_code swapped = swap_condition (comparison);
4365 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4366 machine_mode orig_mode = GET_MODE (x);
4367 machine_mode mode, cmp_mode;
4368 rtx true_rtx, false_rtx;
4369 rtx value, target, equiv;
4370 rtx_insn *insns;
4371 rtx libfunc = 0;
4372 bool reversed_p = false;
4373 cmp_mode = targetm.libgcc_cmp_return_mode ();
4375 for (mode = orig_mode;
4376 mode != VOIDmode;
4377 mode = GET_MODE_WIDER_MODE (mode))
4379 if (code_to_optab (comparison)
4380 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4381 break;
4383 if (code_to_optab (swapped)
4384 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4386 std::swap (x, y);
4387 comparison = swapped;
4388 break;
4391 if (code_to_optab (reversed)
4392 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4394 comparison = reversed;
4395 reversed_p = true;
4396 break;
4400 gcc_assert (mode != VOIDmode);
4402 if (mode != orig_mode)
4404 x = convert_to_mode (mode, x, 0);
4405 y = convert_to_mode (mode, y, 0);
4408 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4409 the RTL. The allows the RTL optimizers to delete the libcall if the
4410 condition can be determined at compile-time. */
4411 if (comparison == UNORDERED
4412 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4414 true_rtx = const_true_rtx;
4415 false_rtx = const0_rtx;
4417 else
4419 switch (comparison)
4421 case EQ:
4422 true_rtx = const0_rtx;
4423 false_rtx = const_true_rtx;
4424 break;
4426 case NE:
4427 true_rtx = const_true_rtx;
4428 false_rtx = const0_rtx;
4429 break;
4431 case GT:
4432 true_rtx = const1_rtx;
4433 false_rtx = const0_rtx;
4434 break;
4436 case GE:
4437 true_rtx = const0_rtx;
4438 false_rtx = constm1_rtx;
4439 break;
4441 case LT:
4442 true_rtx = constm1_rtx;
4443 false_rtx = const0_rtx;
4444 break;
4446 case LE:
4447 true_rtx = const0_rtx;
4448 false_rtx = const1_rtx;
4449 break;
4451 default:
4452 gcc_unreachable ();
4456 if (comparison == UNORDERED)
4458 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4459 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4460 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4461 temp, const_true_rtx, equiv);
4463 else
4465 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4466 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4467 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4468 equiv, true_rtx, false_rtx);
4471 start_sequence ();
4472 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4473 cmp_mode, 2, x, mode, y, mode);
4474 insns = get_insns ();
4475 end_sequence ();
4477 target = gen_reg_rtx (cmp_mode);
4478 emit_libcall_block (insns, target, value, equiv);
4480 if (comparison == UNORDERED
4481 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4482 || reversed_p)
4483 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4484 else
4485 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4487 *pmode = cmp_mode;
4490 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4492 void
4493 emit_indirect_jump (rtx loc ATTRIBUTE_UNUSED)
4495 #ifndef HAVE_indirect_jump
4496 sorry ("indirect jumps are not available on this target");
4497 #else
4498 struct expand_operand ops[1];
4499 create_address_operand (&ops[0], loc);
4500 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4501 emit_barrier ();
4502 #endif
4506 /* Emit a conditional move instruction if the machine supports one for that
4507 condition and machine mode.
4509 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4510 the mode to use should they be constants. If it is VOIDmode, they cannot
4511 both be constants.
4513 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4514 should be stored there. MODE is the mode to use should they be constants.
4515 If it is VOIDmode, they cannot both be constants.
4517 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4518 is not supported. */
4521 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4522 machine_mode cmode, rtx op2, rtx op3,
4523 machine_mode mode, int unsignedp)
4525 rtx comparison;
4526 rtx_insn *last;
4527 enum insn_code icode;
4528 enum rtx_code reversed;
4530 /* If one operand is constant, make it the second one. Only do this
4531 if the other operand is not constant as well. */
4533 if (swap_commutative_operands_p (op0, op1))
4535 std::swap (op0, op1);
4536 code = swap_condition (code);
4539 /* get_condition will prefer to generate LT and GT even if the old
4540 comparison was against zero, so undo that canonicalization here since
4541 comparisons against zero are cheaper. */
4542 if (code == LT && op1 == const1_rtx)
4543 code = LE, op1 = const0_rtx;
4544 else if (code == GT && op1 == constm1_rtx)
4545 code = GE, op1 = const0_rtx;
4547 if (cmode == VOIDmode)
4548 cmode = GET_MODE (op0);
4550 if (swap_commutative_operands_p (op2, op3)
4551 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4552 != UNKNOWN))
4554 std::swap (op2, op3);
4555 code = reversed;
4558 if (mode == VOIDmode)
4559 mode = GET_MODE (op2);
4561 icode = direct_optab_handler (movcc_optab, mode);
4563 if (icode == CODE_FOR_nothing)
4564 return 0;
4566 if (!target)
4567 target = gen_reg_rtx (mode);
4569 code = unsignedp ? unsigned_condition (code) : code;
4570 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4572 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4573 return NULL and let the caller figure out how best to deal with this
4574 situation. */
4575 if (!COMPARISON_P (comparison))
4576 return NULL_RTX;
4578 saved_pending_stack_adjust save;
4579 save_pending_stack_adjust (&save);
4580 last = get_last_insn ();
4581 do_pending_stack_adjust ();
4582 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4583 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4584 &comparison, &cmode);
4585 if (comparison)
4587 struct expand_operand ops[4];
4589 create_output_operand (&ops[0], target, mode);
4590 create_fixed_operand (&ops[1], comparison);
4591 create_input_operand (&ops[2], op2, mode);
4592 create_input_operand (&ops[3], op3, mode);
4593 if (maybe_expand_insn (icode, 4, ops))
4595 if (ops[0].value != target)
4596 convert_move (target, ops[0].value, false);
4597 return target;
4600 delete_insns_since (last);
4601 restore_pending_stack_adjust (&save);
4602 return NULL_RTX;
4605 /* Return nonzero if a conditional move of mode MODE is supported.
4607 This function is for combine so it can tell whether an insn that looks
4608 like a conditional move is actually supported by the hardware. If we
4609 guess wrong we lose a bit on optimization, but that's it. */
4610 /* ??? sparc64 supports conditionally moving integers values based on fp
4611 comparisons, and vice versa. How do we handle them? */
4614 can_conditionally_move_p (machine_mode mode)
4616 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4617 return 1;
4619 return 0;
4622 /* Emit a conditional addition instruction if the machine supports one for that
4623 condition and machine mode.
4625 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4626 the mode to use should they be constants. If it is VOIDmode, they cannot
4627 both be constants.
4629 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4630 should be stored there. MODE is the mode to use should they be constants.
4631 If it is VOIDmode, they cannot both be constants.
4633 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4634 is not supported. */
4637 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4638 machine_mode cmode, rtx op2, rtx op3,
4639 machine_mode mode, int unsignedp)
4641 rtx comparison;
4642 rtx_insn *last;
4643 enum insn_code icode;
4645 /* If one operand is constant, make it the second one. Only do this
4646 if the other operand is not constant as well. */
4648 if (swap_commutative_operands_p (op0, op1))
4650 std::swap (op0, op1);
4651 code = swap_condition (code);
4654 /* get_condition will prefer to generate LT and GT even if the old
4655 comparison was against zero, so undo that canonicalization here since
4656 comparisons against zero are cheaper. */
4657 if (code == LT && op1 == const1_rtx)
4658 code = LE, op1 = const0_rtx;
4659 else if (code == GT && op1 == constm1_rtx)
4660 code = GE, op1 = const0_rtx;
4662 if (cmode == VOIDmode)
4663 cmode = GET_MODE (op0);
4665 if (mode == VOIDmode)
4666 mode = GET_MODE (op2);
4668 icode = optab_handler (addcc_optab, mode);
4670 if (icode == CODE_FOR_nothing)
4671 return 0;
4673 if (!target)
4674 target = gen_reg_rtx (mode);
4676 code = unsignedp ? unsigned_condition (code) : code;
4677 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4679 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4680 return NULL and let the caller figure out how best to deal with this
4681 situation. */
4682 if (!COMPARISON_P (comparison))
4683 return NULL_RTX;
4685 do_pending_stack_adjust ();
4686 last = get_last_insn ();
4687 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4688 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4689 &comparison, &cmode);
4690 if (comparison)
4692 struct expand_operand ops[4];
4694 create_output_operand (&ops[0], target, mode);
4695 create_fixed_operand (&ops[1], comparison);
4696 create_input_operand (&ops[2], op2, mode);
4697 create_input_operand (&ops[3], op3, mode);
4698 if (maybe_expand_insn (icode, 4, ops))
4700 if (ops[0].value != target)
4701 convert_move (target, ops[0].value, false);
4702 return target;
4705 delete_insns_since (last);
4706 return NULL_RTX;
4709 /* These functions attempt to generate an insn body, rather than
4710 emitting the insn, but if the gen function already emits them, we
4711 make no attempt to turn them back into naked patterns. */
4713 /* Generate and return an insn body to add Y to X. */
4715 rtx_insn *
4716 gen_add2_insn (rtx x, rtx y)
4718 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4720 gcc_assert (insn_operand_matches (icode, 0, x));
4721 gcc_assert (insn_operand_matches (icode, 1, x));
4722 gcc_assert (insn_operand_matches (icode, 2, y));
4724 return GEN_FCN (icode) (x, x, y);
4727 /* Generate and return an insn body to add r1 and c,
4728 storing the result in r0. */
4730 rtx_insn *
4731 gen_add3_insn (rtx r0, rtx r1, rtx c)
4733 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4735 if (icode == CODE_FOR_nothing
4736 || !insn_operand_matches (icode, 0, r0)
4737 || !insn_operand_matches (icode, 1, r1)
4738 || !insn_operand_matches (icode, 2, c))
4739 return NULL;
4741 return GEN_FCN (icode) (r0, r1, c);
4745 have_add2_insn (rtx x, rtx y)
4747 enum insn_code icode;
4749 gcc_assert (GET_MODE (x) != VOIDmode);
4751 icode = optab_handler (add_optab, GET_MODE (x));
4753 if (icode == CODE_FOR_nothing)
4754 return 0;
4756 if (!insn_operand_matches (icode, 0, x)
4757 || !insn_operand_matches (icode, 1, x)
4758 || !insn_operand_matches (icode, 2, y))
4759 return 0;
4761 return 1;
4764 /* Generate and return an insn body to add Y to X. */
4766 rtx_insn *
4767 gen_addptr3_insn (rtx x, rtx y, rtx z)
4769 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4771 gcc_assert (insn_operand_matches (icode, 0, x));
4772 gcc_assert (insn_operand_matches (icode, 1, y));
4773 gcc_assert (insn_operand_matches (icode, 2, z));
4775 return GEN_FCN (icode) (x, y, z);
4778 /* Return true if the target implements an addptr pattern and X, Y,
4779 and Z are valid for the pattern predicates. */
4782 have_addptr3_insn (rtx x, rtx y, rtx z)
4784 enum insn_code icode;
4786 gcc_assert (GET_MODE (x) != VOIDmode);
4788 icode = optab_handler (addptr3_optab, GET_MODE (x));
4790 if (icode == CODE_FOR_nothing)
4791 return 0;
4793 if (!insn_operand_matches (icode, 0, x)
4794 || !insn_operand_matches (icode, 1, y)
4795 || !insn_operand_matches (icode, 2, z))
4796 return 0;
4798 return 1;
4801 /* Generate and return an insn body to subtract Y from X. */
4803 rtx_insn *
4804 gen_sub2_insn (rtx x, rtx y)
4806 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4808 gcc_assert (insn_operand_matches (icode, 0, x));
4809 gcc_assert (insn_operand_matches (icode, 1, x));
4810 gcc_assert (insn_operand_matches (icode, 2, y));
4812 return GEN_FCN (icode) (x, x, y);
4815 /* Generate and return an insn body to subtract r1 and c,
4816 storing the result in r0. */
4818 rtx_insn *
4819 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4821 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4823 if (icode == CODE_FOR_nothing
4824 || !insn_operand_matches (icode, 0, r0)
4825 || !insn_operand_matches (icode, 1, r1)
4826 || !insn_operand_matches (icode, 2, c))
4827 return NULL;
4829 return GEN_FCN (icode) (r0, r1, c);
4833 have_sub2_insn (rtx x, rtx y)
4835 enum insn_code icode;
4837 gcc_assert (GET_MODE (x) != VOIDmode);
4839 icode = optab_handler (sub_optab, GET_MODE (x));
4841 if (icode == CODE_FOR_nothing)
4842 return 0;
4844 if (!insn_operand_matches (icode, 0, x)
4845 || !insn_operand_matches (icode, 1, x)
4846 || !insn_operand_matches (icode, 2, y))
4847 return 0;
4849 return 1;
4852 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4853 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4854 no such operation exists, CODE_FOR_nothing will be returned. */
4856 enum insn_code
4857 can_extend_p (machine_mode to_mode, machine_mode from_mode,
4858 int unsignedp)
4860 convert_optab tab;
4861 #ifdef HAVE_ptr_extend
4862 if (unsignedp < 0)
4863 return CODE_FOR_ptr_extend;
4864 #endif
4866 tab = unsignedp ? zext_optab : sext_optab;
4867 return convert_optab_handler (tab, to_mode, from_mode);
4870 /* Generate the body of an insn to extend Y (with mode MFROM)
4871 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4873 rtx_insn *
4874 gen_extend_insn (rtx x, rtx y, machine_mode mto,
4875 machine_mode mfrom, int unsignedp)
4877 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4878 return GEN_FCN (icode) (x, y);
4881 /* can_fix_p and can_float_p say whether the target machine
4882 can directly convert a given fixed point type to
4883 a given floating point type, or vice versa.
4884 The returned value is the CODE_FOR_... value to use,
4885 or CODE_FOR_nothing if these modes cannot be directly converted.
4887 *TRUNCP_PTR is set to 1 if it is necessary to output
4888 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4890 static enum insn_code
4891 can_fix_p (machine_mode fixmode, machine_mode fltmode,
4892 int unsignedp, int *truncp_ptr)
4894 convert_optab tab;
4895 enum insn_code icode;
4897 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4898 icode = convert_optab_handler (tab, fixmode, fltmode);
4899 if (icode != CODE_FOR_nothing)
4901 *truncp_ptr = 0;
4902 return icode;
4905 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4906 for this to work. We need to rework the fix* and ftrunc* patterns
4907 and documentation. */
4908 tab = unsignedp ? ufix_optab : sfix_optab;
4909 icode = convert_optab_handler (tab, fixmode, fltmode);
4910 if (icode != CODE_FOR_nothing
4911 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4913 *truncp_ptr = 1;
4914 return icode;
4917 *truncp_ptr = 0;
4918 return CODE_FOR_nothing;
4921 enum insn_code
4922 can_float_p (machine_mode fltmode, machine_mode fixmode,
4923 int unsignedp)
4925 convert_optab tab;
4927 tab = unsignedp ? ufloat_optab : sfloat_optab;
4928 return convert_optab_handler (tab, fltmode, fixmode);
4931 /* Function supportable_convert_operation
4933 Check whether an operation represented by the code CODE is a
4934 convert operation that is supported by the target platform in
4935 vector form (i.e., when operating on arguments of type VECTYPE_IN
4936 producing a result of type VECTYPE_OUT).
4938 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4939 This function checks if these operations are supported
4940 by the target platform either directly (via vector tree-codes), or via
4941 target builtins.
4943 Output:
4944 - CODE1 is code of vector operation to be used when
4945 vectorizing the operation, if available.
4946 - DECL is decl of target builtin functions to be used
4947 when vectorizing the operation, if available. In this case,
4948 CODE1 is CALL_EXPR. */
4950 bool
4951 supportable_convert_operation (enum tree_code code,
4952 tree vectype_out, tree vectype_in,
4953 tree *decl, enum tree_code *code1)
4955 machine_mode m1,m2;
4956 int truncp;
4958 m1 = TYPE_MODE (vectype_out);
4959 m2 = TYPE_MODE (vectype_in);
4961 /* First check if we can done conversion directly. */
4962 if ((code == FIX_TRUNC_EXPR
4963 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
4964 != CODE_FOR_nothing)
4965 || (code == FLOAT_EXPR
4966 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
4967 != CODE_FOR_nothing))
4969 *code1 = code;
4970 return true;
4973 /* Now check for builtin. */
4974 if (targetm.vectorize.builtin_conversion
4975 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
4977 *code1 = CALL_EXPR;
4978 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
4979 return true;
4981 return false;
4985 /* Generate code to convert FROM to floating point
4986 and store in TO. FROM must be fixed point and not VOIDmode.
4987 UNSIGNEDP nonzero means regard FROM as unsigned.
4988 Normally this is done by correcting the final value
4989 if it is negative. */
4991 void
4992 expand_float (rtx to, rtx from, int unsignedp)
4994 enum insn_code icode;
4995 rtx target = to;
4996 machine_mode fmode, imode;
4997 bool can_do_signed = false;
4999 /* Crash now, because we won't be able to decide which mode to use. */
5000 gcc_assert (GET_MODE (from) != VOIDmode);
5002 /* Look for an insn to do the conversion. Do it in the specified
5003 modes if possible; otherwise convert either input, output or both to
5004 wider mode. If the integer mode is wider than the mode of FROM,
5005 we can do the conversion signed even if the input is unsigned. */
5007 for (fmode = GET_MODE (to); fmode != VOIDmode;
5008 fmode = GET_MODE_WIDER_MODE (fmode))
5009 for (imode = GET_MODE (from); imode != VOIDmode;
5010 imode = GET_MODE_WIDER_MODE (imode))
5012 int doing_unsigned = unsignedp;
5014 if (fmode != GET_MODE (to)
5015 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
5016 continue;
5018 icode = can_float_p (fmode, imode, unsignedp);
5019 if (icode == CODE_FOR_nothing && unsignedp)
5021 enum insn_code scode = can_float_p (fmode, imode, 0);
5022 if (scode != CODE_FOR_nothing)
5023 can_do_signed = true;
5024 if (imode != GET_MODE (from))
5025 icode = scode, doing_unsigned = 0;
5028 if (icode != CODE_FOR_nothing)
5030 if (imode != GET_MODE (from))
5031 from = convert_to_mode (imode, from, unsignedp);
5033 if (fmode != GET_MODE (to))
5034 target = gen_reg_rtx (fmode);
5036 emit_unop_insn (icode, target, from,
5037 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
5039 if (target != to)
5040 convert_move (to, target, 0);
5041 return;
5045 /* Unsigned integer, and no way to convert directly. Convert as signed,
5046 then unconditionally adjust the result. */
5047 if (unsignedp && can_do_signed)
5049 rtx_code_label *label = gen_label_rtx ();
5050 rtx temp;
5051 REAL_VALUE_TYPE offset;
5053 /* Look for a usable floating mode FMODE wider than the source and at
5054 least as wide as the target. Using FMODE will avoid rounding woes
5055 with unsigned values greater than the signed maximum value. */
5057 for (fmode = GET_MODE (to); fmode != VOIDmode;
5058 fmode = GET_MODE_WIDER_MODE (fmode))
5059 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
5060 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
5061 break;
5063 if (fmode == VOIDmode)
5065 /* There is no such mode. Pretend the target is wide enough. */
5066 fmode = GET_MODE (to);
5068 /* Avoid double-rounding when TO is narrower than FROM. */
5069 if ((significand_size (fmode) + 1)
5070 < GET_MODE_PRECISION (GET_MODE (from)))
5072 rtx temp1;
5073 rtx_code_label *neglabel = gen_label_rtx ();
5075 /* Don't use TARGET if it isn't a register, is a hard register,
5076 or is the wrong mode. */
5077 if (!REG_P (target)
5078 || REGNO (target) < FIRST_PSEUDO_REGISTER
5079 || GET_MODE (target) != fmode)
5080 target = gen_reg_rtx (fmode);
5082 imode = GET_MODE (from);
5083 do_pending_stack_adjust ();
5085 /* Test whether the sign bit is set. */
5086 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5087 0, neglabel);
5089 /* The sign bit is not set. Convert as signed. */
5090 expand_float (target, from, 0);
5091 emit_jump_insn (gen_jump (label));
5092 emit_barrier ();
5094 /* The sign bit is set.
5095 Convert to a usable (positive signed) value by shifting right
5096 one bit, while remembering if a nonzero bit was shifted
5097 out; i.e., compute (from & 1) | (from >> 1). */
5099 emit_label (neglabel);
5100 temp = expand_binop (imode, and_optab, from, const1_rtx,
5101 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5102 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5103 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5104 OPTAB_LIB_WIDEN);
5105 expand_float (target, temp, 0);
5107 /* Multiply by 2 to undo the shift above. */
5108 temp = expand_binop (fmode, add_optab, target, target,
5109 target, 0, OPTAB_LIB_WIDEN);
5110 if (temp != target)
5111 emit_move_insn (target, temp);
5113 do_pending_stack_adjust ();
5114 emit_label (label);
5115 goto done;
5119 /* If we are about to do some arithmetic to correct for an
5120 unsigned operand, do it in a pseudo-register. */
5122 if (GET_MODE (to) != fmode
5123 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5124 target = gen_reg_rtx (fmode);
5126 /* Convert as signed integer to floating. */
5127 expand_float (target, from, 0);
5129 /* If FROM is negative (and therefore TO is negative),
5130 correct its value by 2**bitwidth. */
5132 do_pending_stack_adjust ();
5133 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5134 0, label);
5137 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5138 temp = expand_binop (fmode, add_optab, target,
5139 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5140 target, 0, OPTAB_LIB_WIDEN);
5141 if (temp != target)
5142 emit_move_insn (target, temp);
5144 do_pending_stack_adjust ();
5145 emit_label (label);
5146 goto done;
5149 /* No hardware instruction available; call a library routine. */
5151 rtx libfunc;
5152 rtx_insn *insns;
5153 rtx value;
5154 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5156 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_PRECISION (SImode))
5157 from = convert_to_mode (SImode, from, unsignedp);
5159 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5160 gcc_assert (libfunc);
5162 start_sequence ();
5164 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5165 GET_MODE (to), 1, from,
5166 GET_MODE (from));
5167 insns = get_insns ();
5168 end_sequence ();
5170 emit_libcall_block (insns, target, value,
5171 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5172 GET_MODE (to), from));
5175 done:
5177 /* Copy result to requested destination
5178 if we have been computing in a temp location. */
5180 if (target != to)
5182 if (GET_MODE (target) == GET_MODE (to))
5183 emit_move_insn (to, target);
5184 else
5185 convert_move (to, target, 0);
5189 /* Generate code to convert FROM to fixed point and store in TO. FROM
5190 must be floating point. */
5192 void
5193 expand_fix (rtx to, rtx from, int unsignedp)
5195 enum insn_code icode;
5196 rtx target = to;
5197 machine_mode fmode, imode;
5198 int must_trunc = 0;
5200 /* We first try to find a pair of modes, one real and one integer, at
5201 least as wide as FROM and TO, respectively, in which we can open-code
5202 this conversion. If the integer mode is wider than the mode of TO,
5203 we can do the conversion either signed or unsigned. */
5205 for (fmode = GET_MODE (from); fmode != VOIDmode;
5206 fmode = GET_MODE_WIDER_MODE (fmode))
5207 for (imode = GET_MODE (to); imode != VOIDmode;
5208 imode = GET_MODE_WIDER_MODE (imode))
5210 int doing_unsigned = unsignedp;
5212 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5213 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5214 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5216 if (icode != CODE_FOR_nothing)
5218 rtx_insn *last = get_last_insn ();
5219 if (fmode != GET_MODE (from))
5220 from = convert_to_mode (fmode, from, 0);
5222 if (must_trunc)
5224 rtx temp = gen_reg_rtx (GET_MODE (from));
5225 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5226 temp, 0);
5229 if (imode != GET_MODE (to))
5230 target = gen_reg_rtx (imode);
5232 if (maybe_emit_unop_insn (icode, target, from,
5233 doing_unsigned ? UNSIGNED_FIX : FIX))
5235 if (target != to)
5236 convert_move (to, target, unsignedp);
5237 return;
5239 delete_insns_since (last);
5243 /* For an unsigned conversion, there is one more way to do it.
5244 If we have a signed conversion, we generate code that compares
5245 the real value to the largest representable positive number. If if
5246 is smaller, the conversion is done normally. Otherwise, subtract
5247 one plus the highest signed number, convert, and add it back.
5249 We only need to check all real modes, since we know we didn't find
5250 anything with a wider integer mode.
5252 This code used to extend FP value into mode wider than the destination.
5253 This is needed for decimal float modes which cannot accurately
5254 represent one plus the highest signed number of the same size, but
5255 not for binary modes. Consider, for instance conversion from SFmode
5256 into DImode.
5258 The hot path through the code is dealing with inputs smaller than 2^63
5259 and doing just the conversion, so there is no bits to lose.
5261 In the other path we know the value is positive in the range 2^63..2^64-1
5262 inclusive. (as for other input overflow happens and result is undefined)
5263 So we know that the most important bit set in mantissa corresponds to
5264 2^63. The subtraction of 2^63 should not generate any rounding as it
5265 simply clears out that bit. The rest is trivial. */
5267 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5268 for (fmode = GET_MODE (from); fmode != VOIDmode;
5269 fmode = GET_MODE_WIDER_MODE (fmode))
5270 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5271 && (!DECIMAL_FLOAT_MODE_P (fmode)
5272 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5274 int bitsize;
5275 REAL_VALUE_TYPE offset;
5276 rtx limit;
5277 rtx_code_label *lab1, *lab2;
5278 rtx_insn *insn;
5280 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5281 real_2expN (&offset, bitsize - 1, fmode);
5282 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5283 lab1 = gen_label_rtx ();
5284 lab2 = gen_label_rtx ();
5286 if (fmode != GET_MODE (from))
5287 from = convert_to_mode (fmode, from, 0);
5289 /* See if we need to do the subtraction. */
5290 do_pending_stack_adjust ();
5291 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5292 0, lab1);
5294 /* If not, do the signed "fix" and branch around fixup code. */
5295 expand_fix (to, from, 0);
5296 emit_jump_insn (gen_jump (lab2));
5297 emit_barrier ();
5299 /* Otherwise, subtract 2**(N-1), convert to signed number,
5300 then add 2**(N-1). Do the addition using XOR since this
5301 will often generate better code. */
5302 emit_label (lab1);
5303 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5304 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5305 expand_fix (to, target, 0);
5306 target = expand_binop (GET_MODE (to), xor_optab, to,
5307 gen_int_mode
5308 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5309 GET_MODE (to)),
5310 to, 1, OPTAB_LIB_WIDEN);
5312 if (target != to)
5313 emit_move_insn (to, target);
5315 emit_label (lab2);
5317 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5319 /* Make a place for a REG_NOTE and add it. */
5320 insn = emit_move_insn (to, to);
5321 set_dst_reg_note (insn, REG_EQUAL,
5322 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5323 copy_rtx (from)),
5324 to);
5327 return;
5330 /* We can't do it with an insn, so use a library call. But first ensure
5331 that the mode of TO is at least as wide as SImode, since those are the
5332 only library calls we know about. */
5334 if (GET_MODE_PRECISION (GET_MODE (to)) < GET_MODE_PRECISION (SImode))
5336 target = gen_reg_rtx (SImode);
5338 expand_fix (target, from, unsignedp);
5340 else
5342 rtx_insn *insns;
5343 rtx value;
5344 rtx libfunc;
5346 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5347 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5348 gcc_assert (libfunc);
5350 start_sequence ();
5352 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5353 GET_MODE (to), 1, from,
5354 GET_MODE (from));
5355 insns = get_insns ();
5356 end_sequence ();
5358 emit_libcall_block (insns, target, value,
5359 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5360 GET_MODE (to), from));
5363 if (target != to)
5365 if (GET_MODE (to) == GET_MODE (target))
5366 emit_move_insn (to, target);
5367 else
5368 convert_move (to, target, 0);
5372 /* Generate code to convert FROM or TO a fixed-point.
5373 If UINTP is true, either TO or FROM is an unsigned integer.
5374 If SATP is true, we need to saturate the result. */
5376 void
5377 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5379 machine_mode to_mode = GET_MODE (to);
5380 machine_mode from_mode = GET_MODE (from);
5381 convert_optab tab;
5382 enum rtx_code this_code;
5383 enum insn_code code;
5384 rtx_insn *insns;
5385 rtx value;
5386 rtx libfunc;
5388 if (to_mode == from_mode)
5390 emit_move_insn (to, from);
5391 return;
5394 if (uintp)
5396 tab = satp ? satfractuns_optab : fractuns_optab;
5397 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5399 else
5401 tab = satp ? satfract_optab : fract_optab;
5402 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5404 code = convert_optab_handler (tab, to_mode, from_mode);
5405 if (code != CODE_FOR_nothing)
5407 emit_unop_insn (code, to, from, this_code);
5408 return;
5411 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5412 gcc_assert (libfunc);
5414 start_sequence ();
5415 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5416 1, from, from_mode);
5417 insns = get_insns ();
5418 end_sequence ();
5420 emit_libcall_block (insns, to, value,
5421 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5424 /* Generate code to convert FROM to fixed point and store in TO. FROM
5425 must be floating point, TO must be signed. Use the conversion optab
5426 TAB to do the conversion. */
5428 bool
5429 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5431 enum insn_code icode;
5432 rtx target = to;
5433 machine_mode fmode, imode;
5435 /* We first try to find a pair of modes, one real and one integer, at
5436 least as wide as FROM and TO, respectively, in which we can open-code
5437 this conversion. If the integer mode is wider than the mode of TO,
5438 we can do the conversion either signed or unsigned. */
5440 for (fmode = GET_MODE (from); fmode != VOIDmode;
5441 fmode = GET_MODE_WIDER_MODE (fmode))
5442 for (imode = GET_MODE (to); imode != VOIDmode;
5443 imode = GET_MODE_WIDER_MODE (imode))
5445 icode = convert_optab_handler (tab, imode, fmode);
5446 if (icode != CODE_FOR_nothing)
5448 rtx_insn *last = get_last_insn ();
5449 if (fmode != GET_MODE (from))
5450 from = convert_to_mode (fmode, from, 0);
5452 if (imode != GET_MODE (to))
5453 target = gen_reg_rtx (imode);
5455 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5457 delete_insns_since (last);
5458 continue;
5460 if (target != to)
5461 convert_move (to, target, 0);
5462 return true;
5466 return false;
5469 /* Report whether we have an instruction to perform the operation
5470 specified by CODE on operands of mode MODE. */
5472 have_insn_for (enum rtx_code code, machine_mode mode)
5474 return (code_to_optab (code)
5475 && (optab_handler (code_to_optab (code), mode)
5476 != CODE_FOR_nothing));
5479 /* Initialize the libfunc fields of an entire group of entries in some
5480 optab. Each entry is set equal to a string consisting of a leading
5481 pair of underscores followed by a generic operation name followed by
5482 a mode name (downshifted to lowercase) followed by a single character
5483 representing the number of operands for the given operation (which is
5484 usually one of the characters '2', '3', or '4').
5486 OPTABLE is the table in which libfunc fields are to be initialized.
5487 OPNAME is the generic (string) name of the operation.
5488 SUFFIX is the character which specifies the number of operands for
5489 the given generic operation.
5490 MODE is the mode to generate for.
5493 static void
5494 gen_libfunc (optab optable, const char *opname, int suffix,
5495 machine_mode mode)
5497 unsigned opname_len = strlen (opname);
5498 const char *mname = GET_MODE_NAME (mode);
5499 unsigned mname_len = strlen (mname);
5500 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5501 int len = prefix_len + opname_len + mname_len + 1 + 1;
5502 char *libfunc_name = XALLOCAVEC (char, len);
5503 char *p;
5504 const char *q;
5506 p = libfunc_name;
5507 *p++ = '_';
5508 *p++ = '_';
5509 if (targetm.libfunc_gnu_prefix)
5511 *p++ = 'g';
5512 *p++ = 'n';
5513 *p++ = 'u';
5514 *p++ = '_';
5516 for (q = opname; *q; )
5517 *p++ = *q++;
5518 for (q = mname; *q; q++)
5519 *p++ = TOLOWER (*q);
5520 *p++ = suffix;
5521 *p = '\0';
5523 set_optab_libfunc (optable, mode,
5524 ggc_alloc_string (libfunc_name, p - libfunc_name));
5527 /* Like gen_libfunc, but verify that integer operation is involved. */
5529 void
5530 gen_int_libfunc (optab optable, const char *opname, char suffix,
5531 machine_mode mode)
5533 int maxsize = 2 * BITS_PER_WORD;
5534 int minsize = BITS_PER_WORD;
5536 if (GET_MODE_CLASS (mode) != MODE_INT)
5537 return;
5538 if (maxsize < LONG_LONG_TYPE_SIZE)
5539 maxsize = LONG_LONG_TYPE_SIZE;
5540 if (minsize > INT_TYPE_SIZE
5541 && (trapv_binoptab_p (optable)
5542 || trapv_unoptab_p (optable)))
5543 minsize = INT_TYPE_SIZE;
5544 if (GET_MODE_BITSIZE (mode) < minsize
5545 || GET_MODE_BITSIZE (mode) > maxsize)
5546 return;
5547 gen_libfunc (optable, opname, suffix, mode);
5550 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5552 void
5553 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5554 machine_mode mode)
5556 char *dec_opname;
5558 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5559 gen_libfunc (optable, opname, suffix, mode);
5560 if (DECIMAL_FLOAT_MODE_P (mode))
5562 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5563 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5564 depending on the low level floating format used. */
5565 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5566 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5567 gen_libfunc (optable, dec_opname, suffix, mode);
5571 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5573 void
5574 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5575 machine_mode mode)
5577 if (!ALL_FIXED_POINT_MODE_P (mode))
5578 return;
5579 gen_libfunc (optable, opname, suffix, mode);
5582 /* Like gen_libfunc, but verify that signed fixed-point operation is
5583 involved. */
5585 void
5586 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5587 machine_mode mode)
5589 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5590 return;
5591 gen_libfunc (optable, opname, suffix, mode);
5594 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5595 involved. */
5597 void
5598 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5599 machine_mode mode)
5601 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5602 return;
5603 gen_libfunc (optable, opname, suffix, mode);
5606 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5608 void
5609 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5610 machine_mode mode)
5612 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5613 gen_fp_libfunc (optable, name, suffix, mode);
5614 if (INTEGRAL_MODE_P (mode))
5615 gen_int_libfunc (optable, name, suffix, mode);
5618 /* Like gen_libfunc, but verify that FP or INT operation is involved
5619 and add 'v' suffix for integer operation. */
5621 void
5622 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5623 machine_mode mode)
5625 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5626 gen_fp_libfunc (optable, name, suffix, mode);
5627 if (GET_MODE_CLASS (mode) == MODE_INT)
5629 int len = strlen (name);
5630 char *v_name = XALLOCAVEC (char, len + 2);
5631 strcpy (v_name, name);
5632 v_name[len] = 'v';
5633 v_name[len + 1] = 0;
5634 gen_int_libfunc (optable, v_name, suffix, mode);
5638 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5639 involved. */
5641 void
5642 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5643 machine_mode mode)
5645 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5646 gen_fp_libfunc (optable, name, suffix, mode);
5647 if (INTEGRAL_MODE_P (mode))
5648 gen_int_libfunc (optable, name, suffix, mode);
5649 if (ALL_FIXED_POINT_MODE_P (mode))
5650 gen_fixed_libfunc (optable, name, suffix, mode);
5653 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5654 involved. */
5656 void
5657 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5658 machine_mode mode)
5660 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5661 gen_fp_libfunc (optable, name, suffix, mode);
5662 if (INTEGRAL_MODE_P (mode))
5663 gen_int_libfunc (optable, name, suffix, mode);
5664 if (SIGNED_FIXED_POINT_MODE_P (mode))
5665 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5668 /* Like gen_libfunc, but verify that INT or FIXED operation is
5669 involved. */
5671 void
5672 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5673 machine_mode mode)
5675 if (INTEGRAL_MODE_P (mode))
5676 gen_int_libfunc (optable, name, suffix, mode);
5677 if (ALL_FIXED_POINT_MODE_P (mode))
5678 gen_fixed_libfunc (optable, name, suffix, mode);
5681 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5682 involved. */
5684 void
5685 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5686 machine_mode mode)
5688 if (INTEGRAL_MODE_P (mode))
5689 gen_int_libfunc (optable, name, suffix, mode);
5690 if (SIGNED_FIXED_POINT_MODE_P (mode))
5691 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5694 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5695 involved. */
5697 void
5698 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5699 machine_mode mode)
5701 if (INTEGRAL_MODE_P (mode))
5702 gen_int_libfunc (optable, name, suffix, mode);
5703 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5704 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5707 /* Initialize the libfunc fields of an entire group of entries of an
5708 inter-mode-class conversion optab. The string formation rules are
5709 similar to the ones for init_libfuncs, above, but instead of having
5710 a mode name and an operand count these functions have two mode names
5711 and no operand count. */
5713 void
5714 gen_interclass_conv_libfunc (convert_optab tab,
5715 const char *opname,
5716 machine_mode tmode,
5717 machine_mode fmode)
5719 size_t opname_len = strlen (opname);
5720 size_t mname_len = 0;
5722 const char *fname, *tname;
5723 const char *q;
5724 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5725 char *libfunc_name, *suffix;
5726 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5727 char *p;
5729 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5730 depends on which underlying decimal floating point format is used. */
5731 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5733 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5735 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5736 nondec_name[0] = '_';
5737 nondec_name[1] = '_';
5738 if (targetm.libfunc_gnu_prefix)
5740 nondec_name[2] = 'g';
5741 nondec_name[3] = 'n';
5742 nondec_name[4] = 'u';
5743 nondec_name[5] = '_';
5746 memcpy (&nondec_name[prefix_len], opname, opname_len);
5747 nondec_suffix = nondec_name + opname_len + prefix_len;
5749 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5750 dec_name[0] = '_';
5751 dec_name[1] = '_';
5752 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5753 memcpy (&dec_name[2+dec_len], opname, opname_len);
5754 dec_suffix = dec_name + dec_len + opname_len + 2;
5756 fname = GET_MODE_NAME (fmode);
5757 tname = GET_MODE_NAME (tmode);
5759 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5761 libfunc_name = dec_name;
5762 suffix = dec_suffix;
5764 else
5766 libfunc_name = nondec_name;
5767 suffix = nondec_suffix;
5770 p = suffix;
5771 for (q = fname; *q; p++, q++)
5772 *p = TOLOWER (*q);
5773 for (q = tname; *q; p++, q++)
5774 *p = TOLOWER (*q);
5776 *p = '\0';
5778 set_conv_libfunc (tab, tmode, fmode,
5779 ggc_alloc_string (libfunc_name, p - libfunc_name));
5782 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5783 int->fp conversion. */
5785 void
5786 gen_int_to_fp_conv_libfunc (convert_optab tab,
5787 const char *opname,
5788 machine_mode tmode,
5789 machine_mode fmode)
5791 if (GET_MODE_CLASS (fmode) != MODE_INT)
5792 return;
5793 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5794 return;
5795 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5798 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5799 naming scheme. */
5801 void
5802 gen_ufloat_conv_libfunc (convert_optab tab,
5803 const char *opname ATTRIBUTE_UNUSED,
5804 machine_mode tmode,
5805 machine_mode fmode)
5807 if (DECIMAL_FLOAT_MODE_P (tmode))
5808 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5809 else
5810 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5813 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5814 fp->int conversion. */
5816 void
5817 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5818 const char *opname,
5819 machine_mode tmode,
5820 machine_mode fmode)
5822 if (GET_MODE_CLASS (fmode) != MODE_INT)
5823 return;
5824 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5825 return;
5826 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5829 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5830 fp->int conversion with no decimal floating point involved. */
5832 void
5833 gen_fp_to_int_conv_libfunc (convert_optab tab,
5834 const char *opname,
5835 machine_mode tmode,
5836 machine_mode fmode)
5838 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5839 return;
5840 if (GET_MODE_CLASS (tmode) != MODE_INT)
5841 return;
5842 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5845 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5846 The string formation rules are
5847 similar to the ones for init_libfunc, above. */
5849 void
5850 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5851 machine_mode tmode, machine_mode fmode)
5853 size_t opname_len = strlen (opname);
5854 size_t mname_len = 0;
5856 const char *fname, *tname;
5857 const char *q;
5858 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5859 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5860 char *libfunc_name, *suffix;
5861 char *p;
5863 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5864 depends on which underlying decimal floating point format is used. */
5865 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5867 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5869 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5870 nondec_name[0] = '_';
5871 nondec_name[1] = '_';
5872 if (targetm.libfunc_gnu_prefix)
5874 nondec_name[2] = 'g';
5875 nondec_name[3] = 'n';
5876 nondec_name[4] = 'u';
5877 nondec_name[5] = '_';
5879 memcpy (&nondec_name[prefix_len], opname, opname_len);
5880 nondec_suffix = nondec_name + opname_len + prefix_len;
5882 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5883 dec_name[0] = '_';
5884 dec_name[1] = '_';
5885 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5886 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5887 dec_suffix = dec_name + dec_len + opname_len + 2;
5889 fname = GET_MODE_NAME (fmode);
5890 tname = GET_MODE_NAME (tmode);
5892 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5894 libfunc_name = dec_name;
5895 suffix = dec_suffix;
5897 else
5899 libfunc_name = nondec_name;
5900 suffix = nondec_suffix;
5903 p = suffix;
5904 for (q = fname; *q; p++, q++)
5905 *p = TOLOWER (*q);
5906 for (q = tname; *q; p++, q++)
5907 *p = TOLOWER (*q);
5909 *p++ = '2';
5910 *p = '\0';
5912 set_conv_libfunc (tab, tmode, fmode,
5913 ggc_alloc_string (libfunc_name, p - libfunc_name));
5916 /* Pick proper libcall for trunc_optab. We need to chose if we do
5917 truncation or extension and interclass or intraclass. */
5919 void
5920 gen_trunc_conv_libfunc (convert_optab tab,
5921 const char *opname,
5922 machine_mode tmode,
5923 machine_mode fmode)
5925 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5926 return;
5927 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5928 return;
5929 if (tmode == fmode)
5930 return;
5932 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5933 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5934 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5936 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5937 return;
5939 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5940 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5941 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5942 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5945 /* Pick proper libcall for extend_optab. We need to chose if we do
5946 truncation or extension and interclass or intraclass. */
5948 void
5949 gen_extend_conv_libfunc (convert_optab tab,
5950 const char *opname ATTRIBUTE_UNUSED,
5951 machine_mode tmode,
5952 machine_mode fmode)
5954 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5955 return;
5956 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5957 return;
5958 if (tmode == fmode)
5959 return;
5961 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5962 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5963 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5965 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5966 return;
5968 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5969 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5970 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5971 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5974 /* Pick proper libcall for fract_optab. We need to chose if we do
5975 interclass or intraclass. */
5977 void
5978 gen_fract_conv_libfunc (convert_optab tab,
5979 const char *opname,
5980 machine_mode tmode,
5981 machine_mode fmode)
5983 if (tmode == fmode)
5984 return;
5985 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5986 return;
5988 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5989 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5990 else
5991 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5994 /* Pick proper libcall for fractuns_optab. */
5996 void
5997 gen_fractuns_conv_libfunc (convert_optab tab,
5998 const char *opname,
5999 machine_mode tmode,
6000 machine_mode fmode)
6002 if (tmode == fmode)
6003 return;
6004 /* One mode must be a fixed-point mode, and the other must be an integer
6005 mode. */
6006 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
6007 || (ALL_FIXED_POINT_MODE_P (fmode)
6008 && GET_MODE_CLASS (tmode) == MODE_INT)))
6009 return;
6011 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6014 /* Pick proper libcall for satfract_optab. We need to chose if we do
6015 interclass or intraclass. */
6017 void
6018 gen_satfract_conv_libfunc (convert_optab tab,
6019 const char *opname,
6020 machine_mode tmode,
6021 machine_mode fmode)
6023 if (tmode == fmode)
6024 return;
6025 /* TMODE must be a fixed-point mode. */
6026 if (!ALL_FIXED_POINT_MODE_P (tmode))
6027 return;
6029 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6030 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6031 else
6032 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6035 /* Pick proper libcall for satfractuns_optab. */
6037 void
6038 gen_satfractuns_conv_libfunc (convert_optab tab,
6039 const char *opname,
6040 machine_mode tmode,
6041 machine_mode fmode)
6043 if (tmode == fmode)
6044 return;
6045 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
6046 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
6047 return;
6049 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6052 /* Hashtable callbacks for libfunc_decls. */
6054 struct libfunc_decl_hasher : ggc_ptr_hash<tree_node>
6056 static hashval_t
6057 hash (tree entry)
6059 return IDENTIFIER_HASH_VALUE (DECL_NAME (entry));
6062 static bool
6063 equal (tree decl, tree name)
6065 return DECL_NAME (decl) == name;
6069 /* A table of previously-created libfuncs, hashed by name. */
6070 static GTY (()) hash_table<libfunc_decl_hasher> *libfunc_decls;
6072 /* Build a decl for a libfunc named NAME. */
6074 tree
6075 build_libfunc_function (const char *name)
6077 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6078 get_identifier (name),
6079 build_function_type (integer_type_node, NULL_TREE));
6080 /* ??? We don't have any type information except for this is
6081 a function. Pretend this is "int foo()". */
6082 DECL_ARTIFICIAL (decl) = 1;
6083 DECL_EXTERNAL (decl) = 1;
6084 TREE_PUBLIC (decl) = 1;
6085 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6087 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6088 are the flags assigned by targetm.encode_section_info. */
6089 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6091 return decl;
6095 init_one_libfunc (const char *name)
6097 tree id, decl;
6098 hashval_t hash;
6100 if (libfunc_decls == NULL)
6101 libfunc_decls = hash_table<libfunc_decl_hasher>::create_ggc (37);
6103 /* See if we have already created a libfunc decl for this function. */
6104 id = get_identifier (name);
6105 hash = IDENTIFIER_HASH_VALUE (id);
6106 tree *slot = libfunc_decls->find_slot_with_hash (id, hash, INSERT);
6107 decl = *slot;
6108 if (decl == NULL)
6110 /* Create a new decl, so that it can be passed to
6111 targetm.encode_section_info. */
6112 decl = build_libfunc_function (name);
6113 *slot = decl;
6115 return XEXP (DECL_RTL (decl), 0);
6118 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6121 set_user_assembler_libfunc (const char *name, const char *asmspec)
6123 tree id, decl;
6124 hashval_t hash;
6126 id = get_identifier (name);
6127 hash = IDENTIFIER_HASH_VALUE (id);
6128 tree *slot = libfunc_decls->find_slot_with_hash (id, hash, NO_INSERT);
6129 gcc_assert (slot);
6130 decl = (tree) *slot;
6131 set_user_assembler_name (decl, asmspec);
6132 return XEXP (DECL_RTL (decl), 0);
6135 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6136 MODE to NAME, which should be either 0 or a string constant. */
6137 void
6138 set_optab_libfunc (optab op, machine_mode mode, const char *name)
6140 rtx val;
6141 struct libfunc_entry e;
6142 struct libfunc_entry **slot;
6144 e.op = op;
6145 e.mode1 = mode;
6146 e.mode2 = VOIDmode;
6148 if (name)
6149 val = init_one_libfunc (name);
6150 else
6151 val = 0;
6152 slot = libfunc_hash->find_slot (&e, INSERT);
6153 if (*slot == NULL)
6154 *slot = ggc_alloc<libfunc_entry> ();
6155 (*slot)->op = op;
6156 (*slot)->mode1 = mode;
6157 (*slot)->mode2 = VOIDmode;
6158 (*slot)->libfunc = val;
6161 /* Call this to reset the function entry for one conversion optab
6162 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6163 either 0 or a string constant. */
6164 void
6165 set_conv_libfunc (convert_optab optab, machine_mode tmode,
6166 machine_mode fmode, const char *name)
6168 rtx val;
6169 struct libfunc_entry e;
6170 struct libfunc_entry **slot;
6172 e.op = optab;
6173 e.mode1 = tmode;
6174 e.mode2 = fmode;
6176 if (name)
6177 val = init_one_libfunc (name);
6178 else
6179 val = 0;
6180 slot = libfunc_hash->find_slot (&e, INSERT);
6181 if (*slot == NULL)
6182 *slot = ggc_alloc<libfunc_entry> ();
6183 (*slot)->op = optab;
6184 (*slot)->mode1 = tmode;
6185 (*slot)->mode2 = fmode;
6186 (*slot)->libfunc = val;
6189 /* Call this to initialize the contents of the optabs
6190 appropriately for the current target machine. */
6192 void
6193 init_optabs (void)
6195 if (libfunc_hash)
6196 libfunc_hash->empty ();
6197 else
6198 libfunc_hash = hash_table<libfunc_hasher>::create_ggc (10);
6200 /* Fill in the optabs with the insns we support. */
6201 init_all_optabs (this_fn_optabs);
6203 /* The ffs function operates on `int'. Fall back on it if we do not
6204 have a libgcc2 function for that width. */
6205 if (INT_TYPE_SIZE < BITS_PER_WORD)
6206 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6207 "ffs");
6209 /* Explicitly initialize the bswap libfuncs since we need them to be
6210 valid for things other than word_mode. */
6211 if (targetm.libfunc_gnu_prefix)
6213 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6214 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6216 else
6218 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6219 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6222 /* Use cabs for double complex abs, since systems generally have cabs.
6223 Don't define any libcall for float complex, so that cabs will be used. */
6224 if (complex_double_type_node)
6225 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node),
6226 "cabs");
6228 abort_libfunc = init_one_libfunc ("abort");
6229 memcpy_libfunc = init_one_libfunc ("memcpy");
6230 memmove_libfunc = init_one_libfunc ("memmove");
6231 memcmp_libfunc = init_one_libfunc ("memcmp");
6232 memset_libfunc = init_one_libfunc ("memset");
6233 setbits_libfunc = init_one_libfunc ("__setbits");
6235 #ifndef DONT_USE_BUILTIN_SETJMP
6236 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6237 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6238 #else
6239 setjmp_libfunc = init_one_libfunc ("setjmp");
6240 longjmp_libfunc = init_one_libfunc ("longjmp");
6241 #endif
6242 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6243 unwind_sjlj_unregister_libfunc
6244 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6246 /* For function entry/exit instrumentation. */
6247 profile_function_entry_libfunc
6248 = init_one_libfunc ("__cyg_profile_func_enter");
6249 profile_function_exit_libfunc
6250 = init_one_libfunc ("__cyg_profile_func_exit");
6252 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6254 /* Allow the target to add more libcalls or rename some, etc. */
6255 targetm.init_libfuncs ();
6258 /* Use the current target and options to initialize
6259 TREE_OPTIMIZATION_OPTABS (OPTNODE). */
6261 void
6262 init_tree_optimization_optabs (tree optnode)
6264 /* Quick exit if we have already computed optabs for this target. */
6265 if (TREE_OPTIMIZATION_BASE_OPTABS (optnode) == this_target_optabs)
6266 return;
6268 /* Forget any previous information and set up for the current target. */
6269 TREE_OPTIMIZATION_BASE_OPTABS (optnode) = this_target_optabs;
6270 struct target_optabs *tmp_optabs = (struct target_optabs *)
6271 TREE_OPTIMIZATION_OPTABS (optnode);
6272 if (tmp_optabs)
6273 memset (tmp_optabs, 0, sizeof (struct target_optabs));
6274 else
6275 tmp_optabs = ggc_alloc<target_optabs> ();
6277 /* Generate a new set of optabs into tmp_optabs. */
6278 init_all_optabs (tmp_optabs);
6280 /* If the optabs changed, record it. */
6281 if (memcmp (tmp_optabs, this_target_optabs, sizeof (struct target_optabs)))
6282 TREE_OPTIMIZATION_OPTABS (optnode) = tmp_optabs;
6283 else
6285 TREE_OPTIMIZATION_OPTABS (optnode) = NULL;
6286 ggc_free (tmp_optabs);
6290 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6291 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6293 static void
6294 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6296 machine_mode mode;
6297 char buf[64];
6298 size_t len = strlen (base);
6299 int i;
6301 gcc_assert (max <= 8);
6302 gcc_assert (len + 3 < sizeof (buf));
6304 memcpy (buf, base, len);
6305 buf[len] = '_';
6306 buf[len + 1] = '0';
6307 buf[len + 2] = '\0';
6309 mode = QImode;
6310 for (i = 1; i <= max; i *= 2)
6312 buf[len + 1] = '0' + i;
6313 set_optab_libfunc (tab, mode, buf);
6314 mode = GET_MODE_2XWIDER_MODE (mode);
6318 void
6319 init_sync_libfuncs (int max)
6321 if (!flag_sync_libcalls)
6322 return;
6324 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6325 "__sync_val_compare_and_swap", max);
6326 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6327 "__sync_lock_test_and_set", max);
6329 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6330 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6331 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6332 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6333 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6334 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6336 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6337 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6338 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6339 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6340 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6341 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6344 /* Print information about the current contents of the optabs on
6345 STDERR. */
6347 DEBUG_FUNCTION void
6348 debug_optab_libfuncs (void)
6350 int i, j, k;
6352 /* Dump the arithmetic optabs. */
6353 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
6354 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6356 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
6357 if (l)
6359 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6360 fprintf (stderr, "%s\t%s:\t%s\n",
6361 GET_RTX_NAME (optab_to_code ((optab) i)),
6362 GET_MODE_NAME (j),
6363 XSTR (l, 0));
6367 /* Dump the conversion optabs. */
6368 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
6369 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6370 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6372 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
6373 (machine_mode) k);
6374 if (l)
6376 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6377 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6378 GET_RTX_NAME (optab_to_code ((optab) i)),
6379 GET_MODE_NAME (j),
6380 GET_MODE_NAME (k),
6381 XSTR (l, 0));
6387 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6388 CODE. Return 0 on failure. */
6390 rtx_insn *
6391 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6393 machine_mode mode = GET_MODE (op1);
6394 enum insn_code icode;
6395 rtx_insn *insn;
6396 rtx trap_rtx;
6398 if (mode == VOIDmode)
6399 return 0;
6401 icode = optab_handler (ctrap_optab, mode);
6402 if (icode == CODE_FOR_nothing)
6403 return 0;
6405 /* Some targets only accept a zero trap code. */
6406 if (!insn_operand_matches (icode, 3, tcode))
6407 return 0;
6409 do_pending_stack_adjust ();
6410 start_sequence ();
6411 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6412 &trap_rtx, &mode);
6413 if (!trap_rtx)
6414 insn = NULL;
6415 else
6416 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6417 tcode);
6419 /* If that failed, then give up. */
6420 if (insn == 0)
6422 end_sequence ();
6423 return 0;
6426 emit_insn (insn);
6427 insn = get_insns ();
6428 end_sequence ();
6429 return insn;
6432 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6433 or unsigned operation code. */
6435 enum rtx_code
6436 get_rtx_code (enum tree_code tcode, bool unsignedp)
6438 enum rtx_code code;
6439 switch (tcode)
6441 case EQ_EXPR:
6442 code = EQ;
6443 break;
6444 case NE_EXPR:
6445 code = NE;
6446 break;
6447 case LT_EXPR:
6448 code = unsignedp ? LTU : LT;
6449 break;
6450 case LE_EXPR:
6451 code = unsignedp ? LEU : LE;
6452 break;
6453 case GT_EXPR:
6454 code = unsignedp ? GTU : GT;
6455 break;
6456 case GE_EXPR:
6457 code = unsignedp ? GEU : GE;
6458 break;
6460 case UNORDERED_EXPR:
6461 code = UNORDERED;
6462 break;
6463 case ORDERED_EXPR:
6464 code = ORDERED;
6465 break;
6466 case UNLT_EXPR:
6467 code = UNLT;
6468 break;
6469 case UNLE_EXPR:
6470 code = UNLE;
6471 break;
6472 case UNGT_EXPR:
6473 code = UNGT;
6474 break;
6475 case UNGE_EXPR:
6476 code = UNGE;
6477 break;
6478 case UNEQ_EXPR:
6479 code = UNEQ;
6480 break;
6481 case LTGT_EXPR:
6482 code = LTGT;
6483 break;
6485 case BIT_AND_EXPR:
6486 code = AND;
6487 break;
6489 case BIT_IOR_EXPR:
6490 code = IOR;
6491 break;
6493 default:
6494 gcc_unreachable ();
6496 return code;
6499 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6500 unsigned operators. Do not generate compare instruction. */
6502 static rtx
6503 vector_compare_rtx (enum tree_code tcode, tree t_op0, tree t_op1,
6504 bool unsignedp, enum insn_code icode)
6506 struct expand_operand ops[2];
6507 rtx rtx_op0, rtx_op1;
6508 machine_mode m0, m1;
6509 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
6511 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
6513 /* Expand operands. For vector types with scalar modes, e.g. where int64x1_t
6514 has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
6515 cases, use the original mode. */
6516 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6517 EXPAND_STACK_PARM);
6518 m0 = GET_MODE (rtx_op0);
6519 if (m0 == VOIDmode)
6520 m0 = TYPE_MODE (TREE_TYPE (t_op0));
6522 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6523 EXPAND_STACK_PARM);
6524 m1 = GET_MODE (rtx_op1);
6525 if (m1 == VOIDmode)
6526 m1 = TYPE_MODE (TREE_TYPE (t_op1));
6528 create_input_operand (&ops[0], rtx_op0, m0);
6529 create_input_operand (&ops[1], rtx_op1, m1);
6530 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6531 gcc_unreachable ();
6532 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6535 /* Return true if VEC_PERM_EXPR of arbitrary input vectors can be expanded using
6536 SIMD extensions of the CPU. SEL may be NULL, which stands for an unknown
6537 constant. Note that additional permutations representing whole-vector shifts
6538 may also be handled via the vec_shr optab, but only where the second input
6539 vector is entirely constant zeroes; this case is not dealt with here. */
6541 bool
6542 can_vec_perm_p (machine_mode mode, bool variable,
6543 const unsigned char *sel)
6545 machine_mode qimode;
6547 /* If the target doesn't implement a vector mode for the vector type,
6548 then no operations are supported. */
6549 if (!VECTOR_MODE_P (mode))
6550 return false;
6552 if (!variable)
6554 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6555 && (sel == NULL
6556 || targetm.vectorize.vec_perm_const_ok == NULL
6557 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6558 return true;
6561 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6562 return true;
6564 /* We allow fallback to a QI vector mode, and adjust the mask. */
6565 if (GET_MODE_INNER (mode) == QImode)
6566 return false;
6567 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6568 if (!VECTOR_MODE_P (qimode))
6569 return false;
6571 /* ??? For completeness, we ought to check the QImode version of
6572 vec_perm_const_optab. But all users of this implicit lowering
6573 feature implement the variable vec_perm_optab. */
6574 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6575 return false;
6577 /* In order to support the lowering of variable permutations,
6578 we need to support shifts and adds. */
6579 if (variable)
6581 if (GET_MODE_UNIT_SIZE (mode) > 2
6582 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6583 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6584 return false;
6585 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6586 return false;
6589 return true;
6592 /* Checks if vec_perm mask SEL is a constant equivalent to a shift of the first
6593 vec_perm operand, assuming the second operand is a constant vector of zeroes.
6594 Return the shift distance in bits if so, or NULL_RTX if the vec_perm is not a
6595 shift. */
6596 static rtx
6597 shift_amt_for_vec_perm_mask (rtx sel)
6599 unsigned int i, first, nelt = GET_MODE_NUNITS (GET_MODE (sel));
6600 unsigned int bitsize = GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (sel)));
6602 if (GET_CODE (sel) != CONST_VECTOR)
6603 return NULL_RTX;
6605 first = INTVAL (CONST_VECTOR_ELT (sel, 0));
6606 if (first >= 2*nelt)
6607 return NULL_RTX;
6608 for (i = 1; i < nelt; i++)
6610 int idx = INTVAL (CONST_VECTOR_ELT (sel, i));
6611 unsigned int expected = (i + first) & (2 * nelt - 1);
6612 /* Indices into the second vector are all equivalent. */
6613 if (idx < 0 || (MIN (nelt, (unsigned) idx) != MIN (nelt, expected)))
6614 return NULL_RTX;
6617 return GEN_INT (first * bitsize);
6620 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
6622 static rtx
6623 expand_vec_perm_1 (enum insn_code icode, rtx target,
6624 rtx v0, rtx v1, rtx sel)
6626 machine_mode tmode = GET_MODE (target);
6627 machine_mode smode = GET_MODE (sel);
6628 struct expand_operand ops[4];
6630 create_output_operand (&ops[0], target, tmode);
6631 create_input_operand (&ops[3], sel, smode);
6633 /* Make an effort to preserve v0 == v1. The target expander is able to
6634 rely on this to determine if we're permuting a single input operand. */
6635 if (rtx_equal_p (v0, v1))
6637 if (!insn_operand_matches (icode, 1, v0))
6638 v0 = force_reg (tmode, v0);
6639 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6640 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6642 create_fixed_operand (&ops[1], v0);
6643 create_fixed_operand (&ops[2], v0);
6645 else
6647 create_input_operand (&ops[1], v0, tmode);
6648 /* See if this can be handled with a vec_shr. We only do this if the
6649 second vector is all zeroes. */
6650 enum insn_code shift_code = optab_handler (vec_shr_optab, GET_MODE (v0));
6651 if (v1 == CONST0_RTX (GET_MODE (v1)) && shift_code)
6652 if (rtx shift_amt = shift_amt_for_vec_perm_mask (sel))
6654 create_convert_operand_from_type (&ops[2], shift_amt,
6655 sizetype_tab[(int) stk_sizetype]);
6656 if (maybe_expand_insn (shift_code, 3, ops))
6657 return ops[0].value;
6659 create_input_operand (&ops[2], v1, tmode);
6662 if (maybe_expand_insn (icode, 4, ops))
6663 return ops[0].value;
6664 return NULL_RTX;
6667 /* Generate instructions for vec_perm optab given its mode
6668 and three operands. */
6671 expand_vec_perm (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6673 enum insn_code icode;
6674 machine_mode qimode;
6675 unsigned int i, w, e, u;
6676 rtx tmp, sel_qi = NULL;
6677 rtvec vec;
6679 if (!target || GET_MODE (target) != mode)
6680 target = gen_reg_rtx (mode);
6682 w = GET_MODE_SIZE (mode);
6683 e = GET_MODE_NUNITS (mode);
6684 u = GET_MODE_UNIT_SIZE (mode);
6686 /* Set QIMODE to a different vector mode with byte elements.
6687 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6688 qimode = VOIDmode;
6689 if (GET_MODE_INNER (mode) != QImode)
6691 qimode = mode_for_vector (QImode, w);
6692 if (!VECTOR_MODE_P (qimode))
6693 qimode = VOIDmode;
6696 /* If the input is a constant, expand it specially. */
6697 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6698 if (GET_CODE (sel) == CONST_VECTOR)
6700 icode = direct_optab_handler (vec_perm_const_optab, mode);
6701 if (icode != CODE_FOR_nothing)
6703 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6704 if (tmp)
6705 return tmp;
6708 /* Fall back to a constant byte-based permutation. */
6709 if (qimode != VOIDmode)
6711 vec = rtvec_alloc (w);
6712 for (i = 0; i < e; ++i)
6714 unsigned int j, this_e;
6716 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6717 this_e &= 2 * e - 1;
6718 this_e *= u;
6720 for (j = 0; j < u; ++j)
6721 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
6723 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
6725 icode = direct_optab_handler (vec_perm_const_optab, qimode);
6726 if (icode != CODE_FOR_nothing)
6728 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6729 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6730 gen_lowpart (qimode, v1), sel_qi);
6731 if (tmp)
6732 return gen_lowpart (mode, tmp);
6737 /* Otherwise expand as a fully variable permuation. */
6738 icode = direct_optab_handler (vec_perm_optab, mode);
6739 if (icode != CODE_FOR_nothing)
6741 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6742 if (tmp)
6743 return tmp;
6746 /* As a special case to aid several targets, lower the element-based
6747 permutation to a byte-based permutation and try again. */
6748 if (qimode == VOIDmode)
6749 return NULL_RTX;
6750 icode = direct_optab_handler (vec_perm_optab, qimode);
6751 if (icode == CODE_FOR_nothing)
6752 return NULL_RTX;
6754 if (sel_qi == NULL)
6756 /* Multiply each element by its byte size. */
6757 machine_mode selmode = GET_MODE (sel);
6758 if (u == 2)
6759 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6760 NULL, 0, OPTAB_DIRECT);
6761 else
6762 sel = expand_simple_binop (selmode, ASHIFT, sel,
6763 GEN_INT (exact_log2 (u)),
6764 NULL, 0, OPTAB_DIRECT);
6765 gcc_assert (sel != NULL);
6767 /* Broadcast the low byte each element into each of its bytes. */
6768 vec = rtvec_alloc (w);
6769 for (i = 0; i < w; ++i)
6771 int this_e = i / u * u;
6772 if (BYTES_BIG_ENDIAN)
6773 this_e += u - 1;
6774 RTVEC_ELT (vec, i) = GEN_INT (this_e);
6776 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6777 sel = gen_lowpart (qimode, sel);
6778 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
6779 gcc_assert (sel != NULL);
6781 /* Add the byte offset to each byte element. */
6782 /* Note that the definition of the indicies here is memory ordering,
6783 so there should be no difference between big and little endian. */
6784 vec = rtvec_alloc (w);
6785 for (i = 0; i < w; ++i)
6786 RTVEC_ELT (vec, i) = GEN_INT (i % u);
6787 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6788 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6789 sel, 0, OPTAB_DIRECT);
6790 gcc_assert (sel_qi != NULL);
6793 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6794 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6795 gen_lowpart (qimode, v1), sel_qi);
6796 if (tmp)
6797 tmp = gen_lowpart (mode, tmp);
6798 return tmp;
6801 /* Return insn code for a conditional operator with a comparison in
6802 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
6804 static inline enum insn_code
6805 get_vcond_icode (machine_mode vmode, machine_mode cmode, bool uns)
6807 enum insn_code icode = CODE_FOR_nothing;
6808 if (uns)
6809 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
6810 else
6811 icode = convert_optab_handler (vcond_optab, vmode, cmode);
6812 return icode;
6815 /* Return TRUE iff, appropriate vector insns are available
6816 for vector cond expr with vector type VALUE_TYPE and a comparison
6817 with operand vector types in CMP_OP_TYPE. */
6819 bool
6820 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
6822 machine_mode value_mode = TYPE_MODE (value_type);
6823 machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
6824 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
6825 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
6826 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
6827 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
6828 return false;
6829 return true;
6832 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6833 three operands. */
6836 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6837 rtx target)
6839 struct expand_operand ops[6];
6840 enum insn_code icode;
6841 rtx comparison, rtx_op1, rtx_op2;
6842 machine_mode mode = TYPE_MODE (vec_cond_type);
6843 machine_mode cmp_op_mode;
6844 bool unsignedp;
6845 tree op0a, op0b;
6846 enum tree_code tcode;
6848 if (COMPARISON_CLASS_P (op0))
6850 op0a = TREE_OPERAND (op0, 0);
6851 op0b = TREE_OPERAND (op0, 1);
6852 tcode = TREE_CODE (op0);
6854 else
6856 /* Fake op0 < 0. */
6857 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (op0)));
6858 op0a = op0;
6859 op0b = build_zero_cst (TREE_TYPE (op0));
6860 tcode = LT_EXPR;
6862 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6863 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
6866 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
6867 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
6869 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
6870 if (icode == CODE_FOR_nothing)
6871 return 0;
6873 comparison = vector_compare_rtx (tcode, op0a, op0b, unsignedp, icode);
6874 rtx_op1 = expand_normal (op1);
6875 rtx_op2 = expand_normal (op2);
6877 create_output_operand (&ops[0], target, mode);
6878 create_input_operand (&ops[1], rtx_op1, mode);
6879 create_input_operand (&ops[2], rtx_op2, mode);
6880 create_fixed_operand (&ops[3], comparison);
6881 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6882 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6883 expand_insn (icode, 6, ops);
6884 return ops[0].value;
6887 /* Return non-zero if a highpart multiply is supported of can be synthisized.
6888 For the benefit of expand_mult_highpart, the return value is 1 for direct,
6889 2 for even/odd widening, and 3 for hi/lo widening. */
6892 can_mult_highpart_p (machine_mode mode, bool uns_p)
6894 optab op;
6895 unsigned char *sel;
6896 unsigned i, nunits;
6898 op = uns_p ? umul_highpart_optab : smul_highpart_optab;
6899 if (optab_handler (op, mode) != CODE_FOR_nothing)
6900 return 1;
6902 /* If the mode is an integral vector, synth from widening operations. */
6903 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
6904 return 0;
6906 nunits = GET_MODE_NUNITS (mode);
6907 sel = XALLOCAVEC (unsigned char, nunits);
6909 op = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6910 if (optab_handler (op, mode) != CODE_FOR_nothing)
6912 op = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6913 if (optab_handler (op, mode) != CODE_FOR_nothing)
6915 for (i = 0; i < nunits; ++i)
6916 sel[i] = !BYTES_BIG_ENDIAN + (i & ~1) + ((i & 1) ? nunits : 0);
6917 if (can_vec_perm_p (mode, false, sel))
6918 return 2;
6922 op = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6923 if (optab_handler (op, mode) != CODE_FOR_nothing)
6925 op = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6926 if (optab_handler (op, mode) != CODE_FOR_nothing)
6928 for (i = 0; i < nunits; ++i)
6929 sel[i] = 2 * i + (BYTES_BIG_ENDIAN ? 0 : 1);
6930 if (can_vec_perm_p (mode, false, sel))
6931 return 3;
6935 return 0;
6938 /* Expand a highpart multiply. */
6941 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
6942 rtx target, bool uns_p)
6944 struct expand_operand eops[3];
6945 enum insn_code icode;
6946 int method, i, nunits;
6947 machine_mode wmode;
6948 rtx m1, m2, perm;
6949 optab tab1, tab2;
6950 rtvec v;
6952 method = can_mult_highpart_p (mode, uns_p);
6953 switch (method)
6955 case 0:
6956 return NULL_RTX;
6957 case 1:
6958 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6959 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6960 OPTAB_LIB_WIDEN);
6961 case 2:
6962 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6963 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6964 break;
6965 case 3:
6966 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6967 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6968 if (BYTES_BIG_ENDIAN)
6969 std::swap (tab1, tab2);
6970 break;
6971 default:
6972 gcc_unreachable ();
6975 icode = optab_handler (tab1, mode);
6976 nunits = GET_MODE_NUNITS (mode);
6977 wmode = insn_data[icode].operand[0].mode;
6978 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
6979 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
6981 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6982 create_input_operand (&eops[1], op0, mode);
6983 create_input_operand (&eops[2], op1, mode);
6984 expand_insn (icode, 3, eops);
6985 m1 = gen_lowpart (mode, eops[0].value);
6987 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6988 create_input_operand (&eops[1], op0, mode);
6989 create_input_operand (&eops[2], op1, mode);
6990 expand_insn (optab_handler (tab2, mode), 3, eops);
6991 m2 = gen_lowpart (mode, eops[0].value);
6993 v = rtvec_alloc (nunits);
6994 if (method == 2)
6996 for (i = 0; i < nunits; ++i)
6997 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
6998 + ((i & 1) ? nunits : 0));
7000 else
7002 for (i = 0; i < nunits; ++i)
7003 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
7005 perm = gen_rtx_CONST_VECTOR (mode, v);
7007 return expand_vec_perm (mode, m1, m2, perm, target);
7010 /* Return true if target supports vector masked load/store for mode. */
7011 bool
7012 can_vec_mask_load_store_p (machine_mode mode, bool is_load)
7014 optab op = is_load ? maskload_optab : maskstore_optab;
7015 machine_mode vmode;
7016 unsigned int vector_sizes;
7018 /* If mode is vector mode, check it directly. */
7019 if (VECTOR_MODE_P (mode))
7020 return optab_handler (op, mode) != CODE_FOR_nothing;
7022 /* Otherwise, return true if there is some vector mode with
7023 the mask load/store supported. */
7025 /* See if there is any chance the mask load or store might be
7026 vectorized. If not, punt. */
7027 vmode = targetm.vectorize.preferred_simd_mode (mode);
7028 if (!VECTOR_MODE_P (vmode))
7029 return false;
7031 if (optab_handler (op, vmode) != CODE_FOR_nothing)
7032 return true;
7034 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
7035 while (vector_sizes != 0)
7037 unsigned int cur = 1 << floor_log2 (vector_sizes);
7038 vector_sizes &= ~cur;
7039 if (cur <= GET_MODE_SIZE (mode))
7040 continue;
7041 vmode = mode_for_vector (mode, cur / GET_MODE_SIZE (mode));
7042 if (VECTOR_MODE_P (vmode)
7043 && optab_handler (op, vmode) != CODE_FOR_nothing)
7044 return true;
7046 return false;
7049 /* Return true if there is a compare_and_swap pattern. */
7051 bool
7052 can_compare_and_swap_p (machine_mode mode, bool allow_libcall)
7054 enum insn_code icode;
7056 /* Check for __atomic_compare_and_swap. */
7057 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7058 if (icode != CODE_FOR_nothing)
7059 return true;
7061 /* Check for __sync_compare_and_swap. */
7062 icode = optab_handler (sync_compare_and_swap_optab, mode);
7063 if (icode != CODE_FOR_nothing)
7064 return true;
7065 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
7066 return true;
7068 /* No inline compare and swap. */
7069 return false;
7072 /* Return true if an atomic exchange can be performed. */
7074 bool
7075 can_atomic_exchange_p (machine_mode mode, bool allow_libcall)
7077 enum insn_code icode;
7079 /* Check for __atomic_exchange. */
7080 icode = direct_optab_handler (atomic_exchange_optab, mode);
7081 if (icode != CODE_FOR_nothing)
7082 return true;
7084 /* Don't check __sync_test_and_set, as on some platforms that
7085 has reduced functionality. Targets that really do support
7086 a proper exchange should simply be updated to the __atomics. */
7088 return can_compare_and_swap_p (mode, allow_libcall);
7092 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
7093 pattern. */
7095 static void
7096 find_cc_set (rtx x, const_rtx pat, void *data)
7098 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
7099 && GET_CODE (pat) == SET)
7101 rtx *p_cc_reg = (rtx *) data;
7102 gcc_assert (!*p_cc_reg);
7103 *p_cc_reg = x;
7107 /* This is a helper function for the other atomic operations. This function
7108 emits a loop that contains SEQ that iterates until a compare-and-swap
7109 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7110 a set of instructions that takes a value from OLD_REG as an input and
7111 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7112 set to the current contents of MEM. After SEQ, a compare-and-swap will
7113 attempt to update MEM with NEW_REG. The function returns true when the
7114 loop was generated successfully. */
7116 static bool
7117 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7119 machine_mode mode = GET_MODE (mem);
7120 rtx_code_label *label;
7121 rtx cmp_reg, success, oldval;
7123 /* The loop we want to generate looks like
7125 cmp_reg = mem;
7126 label:
7127 old_reg = cmp_reg;
7128 seq;
7129 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
7130 if (success)
7131 goto label;
7133 Note that we only do the plain load from memory once. Subsequent
7134 iterations use the value loaded by the compare-and-swap pattern. */
7136 label = gen_label_rtx ();
7137 cmp_reg = gen_reg_rtx (mode);
7139 emit_move_insn (cmp_reg, mem);
7140 emit_label (label);
7141 emit_move_insn (old_reg, cmp_reg);
7142 if (seq)
7143 emit_insn (seq);
7145 success = NULL_RTX;
7146 oldval = cmp_reg;
7147 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
7148 new_reg, false, MEMMODEL_SYNC_SEQ_CST,
7149 MEMMODEL_RELAXED))
7150 return false;
7152 if (oldval != cmp_reg)
7153 emit_move_insn (cmp_reg, oldval);
7155 /* Mark this jump predicted not taken. */
7156 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
7157 GET_MODE (success), 1, label, 0);
7158 return true;
7162 /* This function tries to emit an atomic_exchange intruction. VAL is written
7163 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
7164 using TARGET if possible. */
7166 static rtx
7167 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7169 machine_mode mode = GET_MODE (mem);
7170 enum insn_code icode;
7172 /* If the target supports the exchange directly, great. */
7173 icode = direct_optab_handler (atomic_exchange_optab, mode);
7174 if (icode != CODE_FOR_nothing)
7176 struct expand_operand ops[4];
7178 create_output_operand (&ops[0], target, mode);
7179 create_fixed_operand (&ops[1], mem);
7180 create_input_operand (&ops[2], val, mode);
7181 create_integer_operand (&ops[3], model);
7182 if (maybe_expand_insn (icode, 4, ops))
7183 return ops[0].value;
7186 return NULL_RTX;
7189 /* This function tries to implement an atomic exchange operation using
7190 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7191 The previous contents of *MEM are returned, using TARGET if possible.
7192 Since this instructionn is an acquire barrier only, stronger memory
7193 models may require additional barriers to be emitted. */
7195 static rtx
7196 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7197 enum memmodel model)
7199 machine_mode mode = GET_MODE (mem);
7200 enum insn_code icode;
7201 rtx_insn *last_insn = get_last_insn ();
7203 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7205 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7206 exists, and the memory model is stronger than acquire, add a release
7207 barrier before the instruction. */
7209 if (is_mm_seq_cst (model) || is_mm_release (model) || is_mm_acq_rel (model))
7210 expand_mem_thread_fence (model);
7212 if (icode != CODE_FOR_nothing)
7214 struct expand_operand ops[3];
7215 create_output_operand (&ops[0], target, mode);
7216 create_fixed_operand (&ops[1], mem);
7217 create_input_operand (&ops[2], val, mode);
7218 if (maybe_expand_insn (icode, 3, ops))
7219 return ops[0].value;
7222 /* If an external test-and-set libcall is provided, use that instead of
7223 any external compare-and-swap that we might get from the compare-and-
7224 swap-loop expansion later. */
7225 if (!can_compare_and_swap_p (mode, false))
7227 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7228 if (libfunc != NULL)
7230 rtx addr;
7232 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7233 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7234 mode, 2, addr, ptr_mode,
7235 val, mode);
7239 /* If the test_and_set can't be emitted, eliminate any barrier that might
7240 have been emitted. */
7241 delete_insns_since (last_insn);
7242 return NULL_RTX;
7245 /* This function tries to implement an atomic exchange operation using a
7246 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7247 *MEM are returned, using TARGET if possible. No memory model is required
7248 since a compare_and_swap loop is seq-cst. */
7250 static rtx
7251 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7253 machine_mode mode = GET_MODE (mem);
7255 if (can_compare_and_swap_p (mode, true))
7257 if (!target || !register_operand (target, mode))
7258 target = gen_reg_rtx (mode);
7259 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7260 return target;
7263 return NULL_RTX;
7266 /* This function tries to implement an atomic test-and-set operation
7267 using the atomic_test_and_set instruction pattern. A boolean value
7268 is returned from the operation, using TARGET if possible. */
7270 #ifndef HAVE_atomic_test_and_set
7271 #define HAVE_atomic_test_and_set 0
7272 #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
7273 #endif
7275 static rtx
7276 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7278 machine_mode pat_bool_mode;
7279 struct expand_operand ops[3];
7281 if (!HAVE_atomic_test_and_set)
7282 return NULL_RTX;
7284 /* While we always get QImode from __atomic_test_and_set, we get
7285 other memory modes from __sync_lock_test_and_set. Note that we
7286 use no endian adjustment here. This matches the 4.6 behavior
7287 in the Sparc backend. */
7288 gcc_checking_assert
7289 (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
7290 if (GET_MODE (mem) != QImode)
7291 mem = adjust_address_nv (mem, QImode, 0);
7293 pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
7294 create_output_operand (&ops[0], target, pat_bool_mode);
7295 create_fixed_operand (&ops[1], mem);
7296 create_integer_operand (&ops[2], model);
7298 if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
7299 return ops[0].value;
7300 return NULL_RTX;
7303 /* This function expands the legacy _sync_lock test_and_set operation which is
7304 generally an atomic exchange. Some limited targets only allow the
7305 constant 1 to be stored. This is an ACQUIRE operation.
7307 TARGET is an optional place to stick the return value.
7308 MEM is where VAL is stored. */
7311 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7313 rtx ret;
7315 /* Try an atomic_exchange first. */
7316 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_SYNC_ACQUIRE);
7317 if (ret)
7318 return ret;
7320 ret = maybe_emit_sync_lock_test_and_set (target, mem, val,
7321 MEMMODEL_SYNC_ACQUIRE);
7322 if (ret)
7323 return ret;
7325 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7326 if (ret)
7327 return ret;
7329 /* If there are no other options, try atomic_test_and_set if the value
7330 being stored is 1. */
7331 if (val == const1_rtx)
7332 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_SYNC_ACQUIRE);
7334 return ret;
7337 /* This function expands the atomic test_and_set operation:
7338 atomically store a boolean TRUE into MEM and return the previous value.
7340 MEMMODEL is the memory model variant to use.
7341 TARGET is an optional place to stick the return value. */
7344 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7346 machine_mode mode = GET_MODE (mem);
7347 rtx ret, trueval, subtarget;
7349 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7350 if (ret)
7351 return ret;
7353 /* Be binary compatible with non-default settings of trueval, and different
7354 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7355 another only has atomic-exchange. */
7356 if (targetm.atomic_test_and_set_trueval == 1)
7358 trueval = const1_rtx;
7359 subtarget = target ? target : gen_reg_rtx (mode);
7361 else
7363 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7364 subtarget = gen_reg_rtx (mode);
7367 /* Try the atomic-exchange optab... */
7368 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7370 /* ... then an atomic-compare-and-swap loop ... */
7371 if (!ret)
7372 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7374 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7375 if (!ret)
7376 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7378 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7379 things with the value 1. Thus we try again without trueval. */
7380 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7381 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7383 /* Failing all else, assume a single threaded environment and simply
7384 perform the operation. */
7385 if (!ret)
7387 /* If the result is ignored skip the move to target. */
7388 if (subtarget != const0_rtx)
7389 emit_move_insn (subtarget, mem);
7391 emit_move_insn (mem, trueval);
7392 ret = subtarget;
7395 /* Recall that have to return a boolean value; rectify if trueval
7396 is not exactly one. */
7397 if (targetm.atomic_test_and_set_trueval != 1)
7398 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7400 return ret;
7403 /* This function expands the atomic exchange operation:
7404 atomically store VAL in MEM and return the previous value in MEM.
7406 MEMMODEL is the memory model variant to use.
7407 TARGET is an optional place to stick the return value. */
7410 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7412 rtx ret;
7414 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7416 /* Next try a compare-and-swap loop for the exchange. */
7417 if (!ret)
7418 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7420 return ret;
7423 /* This function expands the atomic compare exchange operation:
7425 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7426 *PTARGET_OVAL is an optional place to store the old value from memory.
7427 Both target parameters may be NULL to indicate that we do not care about
7428 that return value. Both target parameters are updated on success to
7429 the actual location of the corresponding result.
7431 MEMMODEL is the memory model variant to use.
7433 The return value of the function is true for success. */
7435 bool
7436 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7437 rtx mem, rtx expected, rtx desired,
7438 bool is_weak, enum memmodel succ_model,
7439 enum memmodel fail_model)
7441 machine_mode mode = GET_MODE (mem);
7442 struct expand_operand ops[8];
7443 enum insn_code icode;
7444 rtx target_oval, target_bool = NULL_RTX;
7445 rtx libfunc;
7447 /* Load expected into a register for the compare and swap. */
7448 if (MEM_P (expected))
7449 expected = copy_to_reg (expected);
7451 /* Make sure we always have some place to put the return oldval.
7452 Further, make sure that place is distinct from the input expected,
7453 just in case we need that path down below. */
7454 if (ptarget_oval == NULL
7455 || (target_oval = *ptarget_oval) == NULL
7456 || reg_overlap_mentioned_p (expected, target_oval))
7457 target_oval = gen_reg_rtx (mode);
7459 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7460 if (icode != CODE_FOR_nothing)
7462 machine_mode bool_mode = insn_data[icode].operand[0].mode;
7464 /* Make sure we always have a place for the bool operand. */
7465 if (ptarget_bool == NULL
7466 || (target_bool = *ptarget_bool) == NULL
7467 || GET_MODE (target_bool) != bool_mode)
7468 target_bool = gen_reg_rtx (bool_mode);
7470 /* Emit the compare_and_swap. */
7471 create_output_operand (&ops[0], target_bool, bool_mode);
7472 create_output_operand (&ops[1], target_oval, mode);
7473 create_fixed_operand (&ops[2], mem);
7474 create_input_operand (&ops[3], expected, mode);
7475 create_input_operand (&ops[4], desired, mode);
7476 create_integer_operand (&ops[5], is_weak);
7477 create_integer_operand (&ops[6], succ_model);
7478 create_integer_operand (&ops[7], fail_model);
7479 if (maybe_expand_insn (icode, 8, ops))
7481 /* Return success/failure. */
7482 target_bool = ops[0].value;
7483 target_oval = ops[1].value;
7484 goto success;
7488 /* Otherwise fall back to the original __sync_val_compare_and_swap
7489 which is always seq-cst. */
7490 icode = optab_handler (sync_compare_and_swap_optab, mode);
7491 if (icode != CODE_FOR_nothing)
7493 rtx cc_reg;
7495 create_output_operand (&ops[0], target_oval, mode);
7496 create_fixed_operand (&ops[1], mem);
7497 create_input_operand (&ops[2], expected, mode);
7498 create_input_operand (&ops[3], desired, mode);
7499 if (!maybe_expand_insn (icode, 4, ops))
7500 return false;
7502 target_oval = ops[0].value;
7504 /* If the caller isn't interested in the boolean return value,
7505 skip the computation of it. */
7506 if (ptarget_bool == NULL)
7507 goto success;
7509 /* Otherwise, work out if the compare-and-swap succeeded. */
7510 cc_reg = NULL_RTX;
7511 if (have_insn_for (COMPARE, CCmode))
7512 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7513 if (cc_reg)
7515 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7516 const0_rtx, VOIDmode, 0, 1);
7517 goto success;
7519 goto success_bool_from_val;
7522 /* Also check for library support for __sync_val_compare_and_swap. */
7523 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7524 if (libfunc != NULL)
7526 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7527 target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7528 mode, 3, addr, ptr_mode,
7529 expected, mode, desired, mode);
7531 /* Compute the boolean return value only if requested. */
7532 if (ptarget_bool)
7533 goto success_bool_from_val;
7534 else
7535 goto success;
7538 /* Failure. */
7539 return false;
7541 success_bool_from_val:
7542 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7543 expected, VOIDmode, 1, 1);
7544 success:
7545 /* Make sure that the oval output winds up where the caller asked. */
7546 if (ptarget_oval)
7547 *ptarget_oval = target_oval;
7548 if (ptarget_bool)
7549 *ptarget_bool = target_bool;
7550 return true;
7553 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7555 static void
7556 expand_asm_memory_barrier (void)
7558 rtx asm_op, clob;
7560 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7561 rtvec_alloc (0), rtvec_alloc (0),
7562 rtvec_alloc (0), UNKNOWN_LOCATION);
7563 MEM_VOLATILE_P (asm_op) = 1;
7565 clob = gen_rtx_SCRATCH (VOIDmode);
7566 clob = gen_rtx_MEM (BLKmode, clob);
7567 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7569 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7572 /* This routine will either emit the mem_thread_fence pattern or issue a
7573 sync_synchronize to generate a fence for memory model MEMMODEL. */
7575 void
7576 expand_mem_thread_fence (enum memmodel model)
7578 if (targetm.have_mem_thread_fence ())
7579 emit_insn (targetm.gen_mem_thread_fence (GEN_INT (model)));
7580 else if (!is_mm_relaxed (model))
7582 if (targetm.have_memory_barrier ())
7583 emit_insn (targetm.gen_memory_barrier ());
7584 else if (synchronize_libfunc != NULL_RTX)
7585 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7586 else
7587 expand_asm_memory_barrier ();
7591 /* This routine will either emit the mem_signal_fence pattern or issue a
7592 sync_synchronize to generate a fence for memory model MEMMODEL. */
7594 void
7595 expand_mem_signal_fence (enum memmodel model)
7597 if (targetm.have_mem_signal_fence ())
7598 emit_insn (targetm.gen_mem_signal_fence (GEN_INT (model)));
7599 else if (!is_mm_relaxed (model))
7601 /* By default targets are coherent between a thread and the signal
7602 handler running on the same thread. Thus this really becomes a
7603 compiler barrier, in that stores must not be sunk past
7604 (or raised above) a given point. */
7605 expand_asm_memory_barrier ();
7609 /* This function expands the atomic load operation:
7610 return the atomically loaded value in MEM.
7612 MEMMODEL is the memory model variant to use.
7613 TARGET is an option place to stick the return value. */
7616 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7618 machine_mode mode = GET_MODE (mem);
7619 enum insn_code icode;
7621 /* If the target supports the load directly, great. */
7622 icode = direct_optab_handler (atomic_load_optab, mode);
7623 if (icode != CODE_FOR_nothing)
7625 struct expand_operand ops[3];
7627 create_output_operand (&ops[0], target, mode);
7628 create_fixed_operand (&ops[1], mem);
7629 create_integer_operand (&ops[2], model);
7630 if (maybe_expand_insn (icode, 3, ops))
7631 return ops[0].value;
7634 /* If the size of the object is greater than word size on this target,
7635 then we assume that a load will not be atomic. */
7636 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7638 /* Issue val = compare_and_swap (mem, 0, 0).
7639 This may cause the occasional harmless store of 0 when the value is
7640 already 0, but it seems to be OK according to the standards guys. */
7641 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7642 const0_rtx, false, model, model))
7643 return target;
7644 else
7645 /* Otherwise there is no atomic load, leave the library call. */
7646 return NULL_RTX;
7649 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7650 if (!target || target == const0_rtx)
7651 target = gen_reg_rtx (mode);
7653 /* For SEQ_CST, emit a barrier before the load. */
7654 if (is_mm_seq_cst (model))
7655 expand_mem_thread_fence (model);
7657 emit_move_insn (target, mem);
7659 /* Emit the appropriate barrier after the load. */
7660 expand_mem_thread_fence (model);
7662 return target;
7665 /* This function expands the atomic store operation:
7666 Atomically store VAL in MEM.
7667 MEMMODEL is the memory model variant to use.
7668 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7669 function returns const0_rtx if a pattern was emitted. */
7672 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7674 machine_mode mode = GET_MODE (mem);
7675 enum insn_code icode;
7676 struct expand_operand ops[3];
7678 /* If the target supports the store directly, great. */
7679 icode = direct_optab_handler (atomic_store_optab, mode);
7680 if (icode != CODE_FOR_nothing)
7682 create_fixed_operand (&ops[0], mem);
7683 create_input_operand (&ops[1], val, mode);
7684 create_integer_operand (&ops[2], model);
7685 if (maybe_expand_insn (icode, 3, ops))
7686 return const0_rtx;
7689 /* If using __sync_lock_release is a viable alternative, try it. */
7690 if (use_release)
7692 icode = direct_optab_handler (sync_lock_release_optab, mode);
7693 if (icode != CODE_FOR_nothing)
7695 create_fixed_operand (&ops[0], mem);
7696 create_input_operand (&ops[1], const0_rtx, mode);
7697 if (maybe_expand_insn (icode, 2, ops))
7699 /* lock_release is only a release barrier. */
7700 if (is_mm_seq_cst (model))
7701 expand_mem_thread_fence (model);
7702 return const0_rtx;
7707 /* If the size of the object is greater than word size on this target,
7708 a default store will not be atomic, Try a mem_exchange and throw away
7709 the result. If that doesn't work, don't do anything. */
7710 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7712 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7713 if (!target)
7714 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7715 if (target)
7716 return const0_rtx;
7717 else
7718 return NULL_RTX;
7721 /* Otherwise assume stores are atomic, and emit the proper barriers. */
7722 expand_mem_thread_fence (model);
7724 emit_move_insn (mem, val);
7726 /* For SEQ_CST, also emit a barrier after the store. */
7727 if (is_mm_seq_cst (model))
7728 expand_mem_thread_fence (model);
7730 return const0_rtx;
7734 /* Structure containing the pointers and values required to process the
7735 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7737 struct atomic_op_functions
7739 direct_optab mem_fetch_before;
7740 direct_optab mem_fetch_after;
7741 direct_optab mem_no_result;
7742 optab fetch_before;
7743 optab fetch_after;
7744 direct_optab no_result;
7745 enum rtx_code reverse_code;
7749 /* Fill in structure pointed to by OP with the various optab entries for an
7750 operation of type CODE. */
7752 static void
7753 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7755 gcc_assert (op!= NULL);
7757 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7758 in the source code during compilation, and the optab entries are not
7759 computable until runtime. Fill in the values at runtime. */
7760 switch (code)
7762 case PLUS:
7763 op->mem_fetch_before = atomic_fetch_add_optab;
7764 op->mem_fetch_after = atomic_add_fetch_optab;
7765 op->mem_no_result = atomic_add_optab;
7766 op->fetch_before = sync_old_add_optab;
7767 op->fetch_after = sync_new_add_optab;
7768 op->no_result = sync_add_optab;
7769 op->reverse_code = MINUS;
7770 break;
7771 case MINUS:
7772 op->mem_fetch_before = atomic_fetch_sub_optab;
7773 op->mem_fetch_after = atomic_sub_fetch_optab;
7774 op->mem_no_result = atomic_sub_optab;
7775 op->fetch_before = sync_old_sub_optab;
7776 op->fetch_after = sync_new_sub_optab;
7777 op->no_result = sync_sub_optab;
7778 op->reverse_code = PLUS;
7779 break;
7780 case XOR:
7781 op->mem_fetch_before = atomic_fetch_xor_optab;
7782 op->mem_fetch_after = atomic_xor_fetch_optab;
7783 op->mem_no_result = atomic_xor_optab;
7784 op->fetch_before = sync_old_xor_optab;
7785 op->fetch_after = sync_new_xor_optab;
7786 op->no_result = sync_xor_optab;
7787 op->reverse_code = XOR;
7788 break;
7789 case AND:
7790 op->mem_fetch_before = atomic_fetch_and_optab;
7791 op->mem_fetch_after = atomic_and_fetch_optab;
7792 op->mem_no_result = atomic_and_optab;
7793 op->fetch_before = sync_old_and_optab;
7794 op->fetch_after = sync_new_and_optab;
7795 op->no_result = sync_and_optab;
7796 op->reverse_code = UNKNOWN;
7797 break;
7798 case IOR:
7799 op->mem_fetch_before = atomic_fetch_or_optab;
7800 op->mem_fetch_after = atomic_or_fetch_optab;
7801 op->mem_no_result = atomic_or_optab;
7802 op->fetch_before = sync_old_ior_optab;
7803 op->fetch_after = sync_new_ior_optab;
7804 op->no_result = sync_ior_optab;
7805 op->reverse_code = UNKNOWN;
7806 break;
7807 case NOT:
7808 op->mem_fetch_before = atomic_fetch_nand_optab;
7809 op->mem_fetch_after = atomic_nand_fetch_optab;
7810 op->mem_no_result = atomic_nand_optab;
7811 op->fetch_before = sync_old_nand_optab;
7812 op->fetch_after = sync_new_nand_optab;
7813 op->no_result = sync_nand_optab;
7814 op->reverse_code = UNKNOWN;
7815 break;
7816 default:
7817 gcc_unreachable ();
7821 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7822 using memory order MODEL. If AFTER is true the operation needs to return
7823 the value of *MEM after the operation, otherwise the previous value.
7824 TARGET is an optional place to place the result. The result is unused if
7825 it is const0_rtx.
7826 Return the result if there is a better sequence, otherwise NULL_RTX. */
7828 static rtx
7829 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7830 enum memmodel model, bool after)
7832 /* If the value is prefetched, or not used, it may be possible to replace
7833 the sequence with a native exchange operation. */
7834 if (!after || target == const0_rtx)
7836 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7837 if (code == AND && val == const0_rtx)
7839 if (target == const0_rtx)
7840 target = gen_reg_rtx (GET_MODE (mem));
7841 return maybe_emit_atomic_exchange (target, mem, val, model);
7844 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7845 if (code == IOR && val == constm1_rtx)
7847 if (target == const0_rtx)
7848 target = gen_reg_rtx (GET_MODE (mem));
7849 return maybe_emit_atomic_exchange (target, mem, val, model);
7853 return NULL_RTX;
7856 /* Try to emit an instruction for a specific operation varaition.
7857 OPTAB contains the OP functions.
7858 TARGET is an optional place to return the result. const0_rtx means unused.
7859 MEM is the memory location to operate on.
7860 VAL is the value to use in the operation.
7861 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7862 MODEL is the memory model, if used.
7863 AFTER is true if the returned result is the value after the operation. */
7865 static rtx
7866 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7867 rtx val, bool use_memmodel, enum memmodel model, bool after)
7869 machine_mode mode = GET_MODE (mem);
7870 struct expand_operand ops[4];
7871 enum insn_code icode;
7872 int op_counter = 0;
7873 int num_ops;
7875 /* Check to see if there is a result returned. */
7876 if (target == const0_rtx)
7878 if (use_memmodel)
7880 icode = direct_optab_handler (optab->mem_no_result, mode);
7881 create_integer_operand (&ops[2], model);
7882 num_ops = 3;
7884 else
7886 icode = direct_optab_handler (optab->no_result, mode);
7887 num_ops = 2;
7890 /* Otherwise, we need to generate a result. */
7891 else
7893 if (use_memmodel)
7895 icode = direct_optab_handler (after ? optab->mem_fetch_after
7896 : optab->mem_fetch_before, mode);
7897 create_integer_operand (&ops[3], model);
7898 num_ops = 4;
7900 else
7902 icode = optab_handler (after ? optab->fetch_after
7903 : optab->fetch_before, mode);
7904 num_ops = 3;
7906 create_output_operand (&ops[op_counter++], target, mode);
7908 if (icode == CODE_FOR_nothing)
7909 return NULL_RTX;
7911 create_fixed_operand (&ops[op_counter++], mem);
7912 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7913 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7915 if (maybe_expand_insn (icode, num_ops, ops))
7916 return (target == const0_rtx ? const0_rtx : ops[0].value);
7918 return NULL_RTX;
7922 /* This function expands an atomic fetch_OP or OP_fetch operation:
7923 TARGET is an option place to stick the return value. const0_rtx indicates
7924 the result is unused.
7925 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7926 CODE is the operation being performed (OP)
7927 MEMMODEL is the memory model variant to use.
7928 AFTER is true to return the result of the operation (OP_fetch).
7929 AFTER is false to return the value before the operation (fetch_OP).
7931 This function will *only* generate instructions if there is a direct
7932 optab. No compare and swap loops or libcalls will be generated. */
7934 static rtx
7935 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7936 enum rtx_code code, enum memmodel model,
7937 bool after)
7939 machine_mode mode = GET_MODE (mem);
7940 struct atomic_op_functions optab;
7941 rtx result;
7942 bool unused_result = (target == const0_rtx);
7944 get_atomic_op_for_code (&optab, code);
7946 /* Check to see if there are any better instructions. */
7947 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7948 if (result)
7949 return result;
7951 /* Check for the case where the result isn't used and try those patterns. */
7952 if (unused_result)
7954 /* Try the memory model variant first. */
7955 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7956 if (result)
7957 return result;
7959 /* Next try the old style withuot a memory model. */
7960 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7961 if (result)
7962 return result;
7964 /* There is no no-result pattern, so try patterns with a result. */
7965 target = NULL_RTX;
7968 /* Try the __atomic version. */
7969 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
7970 if (result)
7971 return result;
7973 /* Try the older __sync version. */
7974 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
7975 if (result)
7976 return result;
7978 /* If the fetch value can be calculated from the other variation of fetch,
7979 try that operation. */
7980 if (after || unused_result || optab.reverse_code != UNKNOWN)
7982 /* Try the __atomic version, then the older __sync version. */
7983 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
7984 if (!result)
7985 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
7987 if (result)
7989 /* If the result isn't used, no need to do compensation code. */
7990 if (unused_result)
7991 return result;
7993 /* Issue compensation code. Fetch_after == fetch_before OP val.
7994 Fetch_before == after REVERSE_OP val. */
7995 if (!after)
7996 code = optab.reverse_code;
7997 if (code == NOT)
7999 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
8000 true, OPTAB_LIB_WIDEN);
8001 result = expand_simple_unop (mode, NOT, result, target, true);
8003 else
8004 result = expand_simple_binop (mode, code, result, val, target,
8005 true, OPTAB_LIB_WIDEN);
8006 return result;
8010 /* No direct opcode can be generated. */
8011 return NULL_RTX;
8016 /* This function expands an atomic fetch_OP or OP_fetch operation:
8017 TARGET is an option place to stick the return value. const0_rtx indicates
8018 the result is unused.
8019 atomically fetch MEM, perform the operation with VAL and return it to MEM.
8020 CODE is the operation being performed (OP)
8021 MEMMODEL is the memory model variant to use.
8022 AFTER is true to return the result of the operation (OP_fetch).
8023 AFTER is false to return the value before the operation (fetch_OP). */
8025 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
8026 enum memmodel model, bool after)
8028 machine_mode mode = GET_MODE (mem);
8029 rtx result;
8030 bool unused_result = (target == const0_rtx);
8032 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
8033 after);
8035 if (result)
8036 return result;
8038 /* Add/sub can be implemented by doing the reverse operation with -(val). */
8039 if (code == PLUS || code == MINUS)
8041 rtx tmp;
8042 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
8044 start_sequence ();
8045 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
8046 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
8047 model, after);
8048 if (result)
8050 /* PLUS worked so emit the insns and return. */
8051 tmp = get_insns ();
8052 end_sequence ();
8053 emit_insn (tmp);
8054 return result;
8057 /* PLUS did not work, so throw away the negation code and continue. */
8058 end_sequence ();
8061 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
8062 if (!can_compare_and_swap_p (mode, false))
8064 rtx libfunc;
8065 bool fixup = false;
8066 enum rtx_code orig_code = code;
8067 struct atomic_op_functions optab;
8069 get_atomic_op_for_code (&optab, code);
8070 libfunc = optab_libfunc (after ? optab.fetch_after
8071 : optab.fetch_before, mode);
8072 if (libfunc == NULL
8073 && (after || unused_result || optab.reverse_code != UNKNOWN))
8075 fixup = true;
8076 if (!after)
8077 code = optab.reverse_code;
8078 libfunc = optab_libfunc (after ? optab.fetch_before
8079 : optab.fetch_after, mode);
8081 if (libfunc != NULL)
8083 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
8084 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
8085 2, addr, ptr_mode, val, mode);
8087 if (!unused_result && fixup)
8088 result = expand_simple_binop (mode, code, result, val, target,
8089 true, OPTAB_LIB_WIDEN);
8090 return result;
8093 /* We need the original code for any further attempts. */
8094 code = orig_code;
8097 /* If nothing else has succeeded, default to a compare and swap loop. */
8098 if (can_compare_and_swap_p (mode, true))
8100 rtx_insn *insn;
8101 rtx t0 = gen_reg_rtx (mode), t1;
8103 start_sequence ();
8105 /* If the result is used, get a register for it. */
8106 if (!unused_result)
8108 if (!target || !register_operand (target, mode))
8109 target = gen_reg_rtx (mode);
8110 /* If fetch_before, copy the value now. */
8111 if (!after)
8112 emit_move_insn (target, t0);
8114 else
8115 target = const0_rtx;
8117 t1 = t0;
8118 if (code == NOT)
8120 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
8121 true, OPTAB_LIB_WIDEN);
8122 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
8124 else
8125 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
8126 OPTAB_LIB_WIDEN);
8128 /* For after, copy the value now. */
8129 if (!unused_result && after)
8130 emit_move_insn (target, t1);
8131 insn = get_insns ();
8132 end_sequence ();
8134 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
8135 return target;
8138 return NULL_RTX;
8141 /* Return true if OPERAND is suitable for operand number OPNO of
8142 instruction ICODE. */
8144 bool
8145 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
8147 return (!insn_data[(int) icode].operand[opno].predicate
8148 || (insn_data[(int) icode].operand[opno].predicate
8149 (operand, insn_data[(int) icode].operand[opno].mode)));
8152 /* TARGET is a target of a multiword operation that we are going to
8153 implement as a series of word-mode operations. Return true if
8154 TARGET is suitable for this purpose. */
8156 bool
8157 valid_multiword_target_p (rtx target)
8159 machine_mode mode;
8160 int i;
8162 mode = GET_MODE (target);
8163 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
8164 if (!validate_subreg (word_mode, mode, target, i))
8165 return false;
8166 return true;
8169 /* Like maybe_legitimize_operand, but do not change the code of the
8170 current rtx value. */
8172 static bool
8173 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8174 struct expand_operand *op)
8176 /* See if the operand matches in its current form. */
8177 if (insn_operand_matches (icode, opno, op->value))
8178 return true;
8180 /* If the operand is a memory whose address has no side effects,
8181 try forcing the address into a non-virtual pseudo register.
8182 The check for side effects is important because copy_to_mode_reg
8183 cannot handle things like auto-modified addresses. */
8184 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8186 rtx addr, mem;
8188 mem = op->value;
8189 addr = XEXP (mem, 0);
8190 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8191 && !side_effects_p (addr))
8193 rtx_insn *last;
8194 machine_mode mode;
8196 last = get_last_insn ();
8197 mode = get_address_mode (mem);
8198 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8199 if (insn_operand_matches (icode, opno, mem))
8201 op->value = mem;
8202 return true;
8204 delete_insns_since (last);
8208 return false;
8211 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8212 on success, storing the new operand value back in OP. */
8214 static bool
8215 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8216 struct expand_operand *op)
8218 machine_mode mode, imode;
8219 bool old_volatile_ok, result;
8221 mode = op->mode;
8222 switch (op->type)
8224 case EXPAND_FIXED:
8225 old_volatile_ok = volatile_ok;
8226 volatile_ok = true;
8227 result = maybe_legitimize_operand_same_code (icode, opno, op);
8228 volatile_ok = old_volatile_ok;
8229 return result;
8231 case EXPAND_OUTPUT:
8232 gcc_assert (mode != VOIDmode);
8233 if (op->value
8234 && op->value != const0_rtx
8235 && GET_MODE (op->value) == mode
8236 && maybe_legitimize_operand_same_code (icode, opno, op))
8237 return true;
8239 op->value = gen_reg_rtx (mode);
8240 break;
8242 case EXPAND_INPUT:
8243 input:
8244 gcc_assert (mode != VOIDmode);
8245 gcc_assert (GET_MODE (op->value) == VOIDmode
8246 || GET_MODE (op->value) == mode);
8247 if (maybe_legitimize_operand_same_code (icode, opno, op))
8248 return true;
8250 op->value = copy_to_mode_reg (mode, op->value);
8251 break;
8253 case EXPAND_CONVERT_TO:
8254 gcc_assert (mode != VOIDmode);
8255 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8256 goto input;
8258 case EXPAND_CONVERT_FROM:
8259 if (GET_MODE (op->value) != VOIDmode)
8260 mode = GET_MODE (op->value);
8261 else
8262 /* The caller must tell us what mode this value has. */
8263 gcc_assert (mode != VOIDmode);
8265 imode = insn_data[(int) icode].operand[opno].mode;
8266 if (imode != VOIDmode && imode != mode)
8268 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8269 mode = imode;
8271 goto input;
8273 case EXPAND_ADDRESS:
8274 gcc_assert (mode != VOIDmode);
8275 op->value = convert_memory_address (mode, op->value);
8276 goto input;
8278 case EXPAND_INTEGER:
8279 mode = insn_data[(int) icode].operand[opno].mode;
8280 if (mode != VOIDmode && const_int_operand (op->value, mode))
8281 goto input;
8282 break;
8284 return insn_operand_matches (icode, opno, op->value);
8287 /* Make OP describe an input operand that should have the same value
8288 as VALUE, after any mode conversion that the target might request.
8289 TYPE is the type of VALUE. */
8291 void
8292 create_convert_operand_from_type (struct expand_operand *op,
8293 rtx value, tree type)
8295 create_convert_operand_from (op, value, TYPE_MODE (type),
8296 TYPE_UNSIGNED (type));
8299 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8300 of instruction ICODE. Return true on success, leaving the new operand
8301 values in the OPS themselves. Emit no code on failure. */
8303 bool
8304 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8305 unsigned int nops, struct expand_operand *ops)
8307 rtx_insn *last;
8308 unsigned int i;
8310 last = get_last_insn ();
8311 for (i = 0; i < nops; i++)
8312 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8314 delete_insns_since (last);
8315 return false;
8317 return true;
8320 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8321 as its operands. Return the instruction pattern on success,
8322 and emit any necessary set-up code. Return null and emit no
8323 code on failure. */
8325 rtx_insn *
8326 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8327 struct expand_operand *ops)
8329 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8330 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8331 return NULL;
8333 switch (nops)
8335 case 1:
8336 return GEN_FCN (icode) (ops[0].value);
8337 case 2:
8338 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8339 case 3:
8340 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8341 case 4:
8342 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8343 ops[3].value);
8344 case 5:
8345 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8346 ops[3].value, ops[4].value);
8347 case 6:
8348 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8349 ops[3].value, ops[4].value, ops[5].value);
8350 case 7:
8351 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8352 ops[3].value, ops[4].value, ops[5].value,
8353 ops[6].value);
8354 case 8:
8355 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8356 ops[3].value, ops[4].value, ops[5].value,
8357 ops[6].value, ops[7].value);
8358 case 9:
8359 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8360 ops[3].value, ops[4].value, ops[5].value,
8361 ops[6].value, ops[7].value, ops[8].value);
8363 gcc_unreachable ();
8366 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8367 as its operands. Return true on success and emit no code on failure. */
8369 bool
8370 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8371 struct expand_operand *ops)
8373 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
8374 if (pat)
8376 emit_insn (pat);
8377 return true;
8379 return false;
8382 /* Like maybe_expand_insn, but for jumps. */
8384 bool
8385 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8386 struct expand_operand *ops)
8388 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
8389 if (pat)
8391 emit_jump_insn (pat);
8392 return true;
8394 return false;
8397 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8398 as its operands. */
8400 void
8401 expand_insn (enum insn_code icode, unsigned int nops,
8402 struct expand_operand *ops)
8404 if (!maybe_expand_insn (icode, nops, ops))
8405 gcc_unreachable ();
8408 /* Like expand_insn, but for jumps. */
8410 void
8411 expand_jump_insn (enum insn_code icode, unsigned int nops,
8412 struct expand_operand *ops)
8414 if (!maybe_expand_jump_insn (icode, nops, ops))
8415 gcc_unreachable ();
8418 /* Reduce conditional compilation elsewhere. */
8419 #ifndef HAVE_insv
8420 #define HAVE_insv 0
8421 #define CODE_FOR_insv CODE_FOR_nothing
8422 #endif
8423 #ifndef HAVE_extv
8424 #define HAVE_extv 0
8425 #define CODE_FOR_extv CODE_FOR_nothing
8426 #endif
8427 #ifndef HAVE_extzv
8428 #define HAVE_extzv 0
8429 #define CODE_FOR_extzv CODE_FOR_nothing
8430 #endif
8432 /* Enumerates the possible types of structure operand to an
8433 extraction_insn. */
8434 enum extraction_type { ET_unaligned_mem, ET_reg };
8436 /* Check whether insv, extv or extzv pattern ICODE can be used for an
8437 insertion or extraction of type TYPE on a structure of mode MODE.
8438 Return true if so and fill in *INSN accordingly. STRUCT_OP is the
8439 operand number of the structure (the first sign_extract or zero_extract
8440 operand) and FIELD_OP is the operand number of the field (the other
8441 side of the set from the sign_extract or zero_extract). */
8443 static bool
8444 get_traditional_extraction_insn (extraction_insn *insn,
8445 enum extraction_type type,
8446 machine_mode mode,
8447 enum insn_code icode,
8448 int struct_op, int field_op)
8450 const struct insn_data_d *data = &insn_data[icode];
8452 machine_mode struct_mode = data->operand[struct_op].mode;
8453 if (struct_mode == VOIDmode)
8454 struct_mode = word_mode;
8455 if (mode != struct_mode)
8456 return false;
8458 machine_mode field_mode = data->operand[field_op].mode;
8459 if (field_mode == VOIDmode)
8460 field_mode = word_mode;
8462 machine_mode pos_mode = data->operand[struct_op + 2].mode;
8463 if (pos_mode == VOIDmode)
8464 pos_mode = word_mode;
8466 insn->icode = icode;
8467 insn->field_mode = field_mode;
8468 insn->struct_mode = (type == ET_unaligned_mem ? byte_mode : struct_mode);
8469 insn->pos_mode = pos_mode;
8470 return true;
8473 /* Return true if an optab exists to perform an insertion or extraction
8474 of type TYPE in mode MODE. Describe the instruction in *INSN if so.
8476 REG_OPTAB is the optab to use for register structures and
8477 MISALIGN_OPTAB is the optab to use for misaligned memory structures.
8478 POS_OP is the operand number of the bit position. */
8480 static bool
8481 get_optab_extraction_insn (struct extraction_insn *insn,
8482 enum extraction_type type,
8483 machine_mode mode, direct_optab reg_optab,
8484 direct_optab misalign_optab, int pos_op)
8486 direct_optab optab = (type == ET_unaligned_mem ? misalign_optab : reg_optab);
8487 enum insn_code icode = direct_optab_handler (optab, mode);
8488 if (icode == CODE_FOR_nothing)
8489 return false;
8491 const struct insn_data_d *data = &insn_data[icode];
8493 insn->icode = icode;
8494 insn->field_mode = mode;
8495 insn->struct_mode = (type == ET_unaligned_mem ? BLKmode : mode);
8496 insn->pos_mode = data->operand[pos_op].mode;
8497 if (insn->pos_mode == VOIDmode)
8498 insn->pos_mode = word_mode;
8499 return true;
8502 /* Return true if an instruction exists to perform an insertion or
8503 extraction (PATTERN says which) of type TYPE in mode MODE.
8504 Describe the instruction in *INSN if so. */
8506 static bool
8507 get_extraction_insn (extraction_insn *insn,
8508 enum extraction_pattern pattern,
8509 enum extraction_type type,
8510 machine_mode mode)
8512 switch (pattern)
8514 case EP_insv:
8515 if (HAVE_insv
8516 && get_traditional_extraction_insn (insn, type, mode,
8517 CODE_FOR_insv, 0, 3))
8518 return true;
8519 return get_optab_extraction_insn (insn, type, mode, insv_optab,
8520 insvmisalign_optab, 2);
8522 case EP_extv:
8523 if (HAVE_extv
8524 && get_traditional_extraction_insn (insn, type, mode,
8525 CODE_FOR_extv, 1, 0))
8526 return true;
8527 return get_optab_extraction_insn (insn, type, mode, extv_optab,
8528 extvmisalign_optab, 3);
8530 case EP_extzv:
8531 if (HAVE_extzv
8532 && get_traditional_extraction_insn (insn, type, mode,
8533 CODE_FOR_extzv, 1, 0))
8534 return true;
8535 return get_optab_extraction_insn (insn, type, mode, extzv_optab,
8536 extzvmisalign_optab, 3);
8538 default:
8539 gcc_unreachable ();
8543 /* Return true if an instruction exists to access a field of mode
8544 FIELDMODE in a structure that has STRUCT_BITS significant bits.
8545 Describe the "best" such instruction in *INSN if so. PATTERN and
8546 TYPE describe the type of insertion or extraction we want to perform.
8548 For an insertion, the number of significant structure bits includes
8549 all bits of the target. For an extraction, it need only include the
8550 most significant bit of the field. Larger widths are acceptable
8551 in both cases. */
8553 static bool
8554 get_best_extraction_insn (extraction_insn *insn,
8555 enum extraction_pattern pattern,
8556 enum extraction_type type,
8557 unsigned HOST_WIDE_INT struct_bits,
8558 machine_mode field_mode)
8560 machine_mode mode = smallest_mode_for_size (struct_bits, MODE_INT);
8561 while (mode != VOIDmode)
8563 if (get_extraction_insn (insn, pattern, type, mode))
8565 while (mode != VOIDmode
8566 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (field_mode)
8567 && !TRULY_NOOP_TRUNCATION_MODES_P (insn->field_mode,
8568 field_mode))
8570 get_extraction_insn (insn, pattern, type, mode);
8571 mode = GET_MODE_WIDER_MODE (mode);
8573 return true;
8575 mode = GET_MODE_WIDER_MODE (mode);
8577 return false;
8580 /* Return true if an instruction exists to access a field of mode
8581 FIELDMODE in a register structure that has STRUCT_BITS significant bits.
8582 Describe the "best" such instruction in *INSN if so. PATTERN describes
8583 the type of insertion or extraction we want to perform.
8585 For an insertion, the number of significant structure bits includes
8586 all bits of the target. For an extraction, it need only include the
8587 most significant bit of the field. Larger widths are acceptable
8588 in both cases. */
8590 bool
8591 get_best_reg_extraction_insn (extraction_insn *insn,
8592 enum extraction_pattern pattern,
8593 unsigned HOST_WIDE_INT struct_bits,
8594 machine_mode field_mode)
8596 return get_best_extraction_insn (insn, pattern, ET_reg, struct_bits,
8597 field_mode);
8600 /* Return true if an instruction exists to access a field of BITSIZE
8601 bits starting BITNUM bits into a memory structure. Describe the
8602 "best" such instruction in *INSN if so. PATTERN describes the type
8603 of insertion or extraction we want to perform and FIELDMODE is the
8604 natural mode of the extracted field.
8606 The instructions considered here only access bytes that overlap
8607 the bitfield; they do not touch any surrounding bytes. */
8609 bool
8610 get_best_mem_extraction_insn (extraction_insn *insn,
8611 enum extraction_pattern pattern,
8612 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitnum,
8613 machine_mode field_mode)
8615 unsigned HOST_WIDE_INT struct_bits = (bitnum % BITS_PER_UNIT
8616 + bitsize
8617 + BITS_PER_UNIT - 1);
8618 struct_bits -= struct_bits % BITS_PER_UNIT;
8619 return get_best_extraction_insn (insn, pattern, ET_unaligned_mem,
8620 struct_bits, field_mode);
8623 /* Determine whether "1 << x" is relatively cheap in word_mode. */
8625 bool
8626 lshift_cheap_p (bool speed_p)
8628 /* FIXME: This should be made target dependent via this "this_target"
8629 mechanism, similar to e.g. can_copy_init_p in gcse.c. */
8630 static bool init[2] = { false, false };
8631 static bool cheap[2] = { true, true };
8633 /* If the targer has no lshift in word_mode, the operation will most
8634 probably not be cheap. ??? Does GCC even work for such targets? */
8635 if (optab_handler (ashl_optab, word_mode) == CODE_FOR_nothing)
8636 return false;
8638 if (!init[speed_p])
8640 rtx reg = gen_raw_REG (word_mode, 10000);
8641 int cost = set_src_cost (gen_rtx_ASHIFT (word_mode, const1_rtx, reg),
8642 speed_p);
8643 cheap[speed_p] = cost < COSTS_N_INSNS (3);
8644 init[speed_p] = true;
8647 return cheap[speed_p];
8650 #include "gt-optabs.h"