re PR middle-end/67401 (Incorrect expand of __atomic_compare_exchange_8 using __sync_...
[official-gcc.git] / gcc / optabs.c
blobc2a9b1ce9effd8bcaffc99c98d685558a41b65b7
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 "backend.h"
25 #include "predict.h"
26 #include "diagnostic-core.h"
28 /* Include insn-config.h before expr.h so that HAVE_conditional_move
29 is properly defined. */
30 #include "insn-config.h"
31 #include "rtl.h"
32 #include "alias.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 "except.h"
41 #include "expmed.h"
42 #include "dojump.h"
43 #include "explow.h"
44 #include "calls.h"
45 #include "emit-rtl.h"
46 #include "stmt.h"
47 #include "expr.h"
48 #include "insn-codes.h"
49 #include "optabs.h"
50 #include "libfuncs.h"
51 #include "recog.h"
52 #include "reload.h"
53 #include "target.h"
55 struct target_optabs default_target_optabs;
56 struct target_libfuncs default_target_libfuncs;
57 struct target_optabs *this_fn_optabs = &default_target_optabs;
58 #if SWITCHABLE_TARGET
59 struct target_optabs *this_target_optabs = &default_target_optabs;
60 struct target_libfuncs *this_target_libfuncs = &default_target_libfuncs;
61 #endif
63 #define libfunc_hash \
64 (this_target_libfuncs->x_libfunc_hash)
66 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
67 machine_mode *);
68 static rtx expand_unop_direct (machine_mode, optab, rtx, rtx, int);
69 static void emit_libcall_block_1 (rtx_insn *, rtx, rtx, rtx, bool);
71 /* Debug facility for use in GDB. */
72 void debug_optab_libfuncs (void);
74 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
75 #if ENABLE_DECIMAL_BID_FORMAT
76 #define DECIMAL_PREFIX "bid_"
77 #else
78 #define DECIMAL_PREFIX "dpd_"
79 #endif
81 /* Used for libfunc_hash. */
83 hashval_t
84 libfunc_hasher::hash (libfunc_entry *e)
86 return ((e->mode1 + e->mode2 * NUM_MACHINE_MODES) ^ e->op);
89 /* Used for libfunc_hash. */
91 bool
92 libfunc_hasher::equal (libfunc_entry *e1, libfunc_entry *e2)
94 return e1->op == e2->op && e1->mode1 == e2->mode1 && e1->mode2 == e2->mode2;
97 /* Return libfunc corresponding operation defined by OPTAB converting
98 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
99 if no libfunc is available. */
101 convert_optab_libfunc (convert_optab optab, machine_mode mode1,
102 machine_mode mode2)
104 struct libfunc_entry e;
105 struct libfunc_entry **slot;
107 /* ??? This ought to be an assert, but not all of the places
108 that we expand optabs know about the optabs that got moved
109 to being direct. */
110 if (!(optab >= FIRST_CONV_OPTAB && optab <= LAST_CONVLIB_OPTAB))
111 return NULL_RTX;
113 e.op = optab;
114 e.mode1 = mode1;
115 e.mode2 = mode2;
116 slot = libfunc_hash->find_slot (&e, NO_INSERT);
117 if (!slot)
119 const struct convert_optab_libcall_d *d
120 = &convlib_def[optab - FIRST_CONV_OPTAB];
122 if (d->libcall_gen == NULL)
123 return NULL;
125 d->libcall_gen (optab, d->libcall_basename, mode1, mode2);
126 slot = libfunc_hash->find_slot (&e, NO_INSERT);
127 if (!slot)
128 return NULL;
130 return (*slot)->libfunc;
133 /* Return libfunc corresponding operation defined by OPTAB in MODE.
134 Trigger lazy initialization if needed, return NULL if no libfunc is
135 available. */
137 optab_libfunc (optab optab, machine_mode mode)
139 struct libfunc_entry e;
140 struct libfunc_entry **slot;
142 /* ??? This ought to be an assert, but not all of the places
143 that we expand optabs know about the optabs that got moved
144 to being direct. */
145 if (!(optab >= FIRST_NORM_OPTAB && optab <= LAST_NORMLIB_OPTAB))
146 return NULL_RTX;
148 e.op = optab;
149 e.mode1 = mode;
150 e.mode2 = VOIDmode;
151 slot = libfunc_hash->find_slot (&e, NO_INSERT);
152 if (!slot)
154 const struct optab_libcall_d *d
155 = &normlib_def[optab - FIRST_NORM_OPTAB];
157 if (d->libcall_gen == NULL)
158 return NULL;
160 d->libcall_gen (optab, d->libcall_basename, d->libcall_suffix, mode);
161 slot = libfunc_hash->find_slot (&e, NO_INSERT);
162 if (!slot)
163 return NULL;
165 return (*slot)->libfunc;
169 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
170 the result of operation CODE applied to OP0 (and OP1 if it is a binary
171 operation).
173 If the last insn does not set TARGET, don't do anything, but return 1.
175 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
176 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
177 try again, ensuring that TARGET is not one of the operands. */
179 static int
180 add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
182 rtx_insn *last_insn;
183 rtx set;
184 rtx note;
186 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
188 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
189 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
190 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
191 && GET_RTX_CLASS (code) != RTX_COMPARE
192 && GET_RTX_CLASS (code) != RTX_UNARY)
193 return 1;
195 if (GET_CODE (target) == ZERO_EXTRACT)
196 return 1;
198 for (last_insn = insns;
199 NEXT_INSN (last_insn) != NULL_RTX;
200 last_insn = NEXT_INSN (last_insn))
203 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
204 a value changing in the insn, so the note would be invalid for CSE. */
205 if (reg_overlap_mentioned_p (target, op0)
206 || (op1 && reg_overlap_mentioned_p (target, op1)))
208 if (MEM_P (target)
209 && (rtx_equal_p (target, op0)
210 || (op1 && rtx_equal_p (target, op1))))
212 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
213 over expanding it as temp = MEM op X, MEM = temp. If the target
214 supports MEM = MEM op X instructions, it is sometimes too hard
215 to reconstruct that form later, especially if X is also a memory,
216 and due to multiple occurrences of addresses the address might
217 be forced into register unnecessarily.
218 Note that not emitting the REG_EQUIV note might inhibit
219 CSE in some cases. */
220 set = single_set (last_insn);
221 if (set
222 && GET_CODE (SET_SRC (set)) == code
223 && MEM_P (SET_DEST (set))
224 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
225 || (op1 && rtx_equal_p (SET_DEST (set),
226 XEXP (SET_SRC (set), 1)))))
227 return 1;
229 return 0;
232 set = set_for_reg_notes (last_insn);
233 if (set == NULL_RTX)
234 return 1;
236 if (! rtx_equal_p (SET_DEST (set), target)
237 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
238 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
239 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
240 return 1;
242 if (GET_RTX_CLASS (code) == RTX_UNARY)
243 switch (code)
245 case FFS:
246 case CLZ:
247 case CTZ:
248 case CLRSB:
249 case POPCOUNT:
250 case PARITY:
251 case BSWAP:
252 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
254 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
255 if (GET_MODE_SIZE (GET_MODE (op0))
256 > GET_MODE_SIZE (GET_MODE (target)))
257 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
258 note, GET_MODE (op0));
259 else
260 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
261 note, GET_MODE (op0));
262 break;
264 /* FALLTHRU */
265 default:
266 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
267 break;
269 else
270 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
272 set_unique_reg_note (last_insn, REG_EQUAL, note);
274 return 1;
277 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
278 for a widening operation would be. In most cases this would be OP0, but if
279 that's a constant it'll be VOIDmode, which isn't useful. */
281 static machine_mode
282 widened_mode (machine_mode to_mode, rtx op0, rtx op1)
284 machine_mode m0 = GET_MODE (op0);
285 machine_mode m1 = GET_MODE (op1);
286 machine_mode result;
288 if (m0 == VOIDmode && m1 == VOIDmode)
289 return to_mode;
290 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
291 result = m1;
292 else
293 result = m0;
295 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
296 return to_mode;
298 return result;
301 /* Like optab_handler, but for widening_operations that have a
302 TO_MODE and a FROM_MODE. */
304 enum insn_code
305 widening_optab_handler (optab op, machine_mode to_mode,
306 machine_mode from_mode)
308 unsigned scode = (op << 16) | to_mode;
309 if (to_mode != from_mode && from_mode != VOIDmode)
311 /* ??? Why does find_widening_optab_handler_and_mode attempt to
312 widen things that can't be widened? E.g. add_optab... */
313 if (op > LAST_CONV_OPTAB)
314 return CODE_FOR_nothing;
315 scode |= from_mode << 8;
317 return raw_optab_handler (scode);
320 /* Find a widening optab even if it doesn't widen as much as we want.
321 E.g. if from_mode is HImode, and to_mode is DImode, and there is no
322 direct HI->SI insn, then return SI->DI, if that exists.
323 If PERMIT_NON_WIDENING is non-zero then this can be used with
324 non-widening optabs also. */
326 enum insn_code
327 find_widening_optab_handler_and_mode (optab op, machine_mode to_mode,
328 machine_mode from_mode,
329 int permit_non_widening,
330 machine_mode *found_mode)
332 for (; (permit_non_widening || from_mode != to_mode)
333 && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
334 && from_mode != VOIDmode;
335 from_mode = GET_MODE_WIDER_MODE (from_mode))
337 enum insn_code handler = widening_optab_handler (op, to_mode,
338 from_mode);
340 if (handler != CODE_FOR_nothing)
342 if (found_mode)
343 *found_mode = from_mode;
344 return handler;
348 return CODE_FOR_nothing;
351 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
352 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
353 not actually do a sign-extend or zero-extend, but can leave the
354 higher-order bits of the result rtx undefined, for example, in the case
355 of logical operations, but not right shifts. */
357 static rtx
358 widen_operand (rtx op, machine_mode mode, machine_mode oldmode,
359 int unsignedp, int no_extend)
361 rtx result;
363 /* If we don't have to extend and this is a constant, return it. */
364 if (no_extend && GET_MODE (op) == VOIDmode)
365 return op;
367 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
368 extend since it will be more efficient to do so unless the signedness of
369 a promoted object differs from our extension. */
370 if (! no_extend
371 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
372 && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
373 return convert_modes (mode, oldmode, op, unsignedp);
375 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
376 SUBREG. */
377 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
378 return gen_lowpart (mode, force_reg (GET_MODE (op), op));
380 /* Otherwise, get an object of MODE, clobber it, and set the low-order
381 part to OP. */
383 result = gen_reg_rtx (mode);
384 emit_clobber (result);
385 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
386 return result;
389 /* Return the optab used for computing the operation given by the tree code,
390 CODE and the tree EXP. This function is not always usable (for example, it
391 cannot give complete results for multiplication or division) but probably
392 ought to be relied on more widely throughout the expander. */
393 optab
394 optab_for_tree_code (enum tree_code code, const_tree type,
395 enum optab_subtype subtype)
397 bool trapv;
398 switch (code)
400 case BIT_AND_EXPR:
401 return and_optab;
403 case BIT_IOR_EXPR:
404 return ior_optab;
406 case BIT_NOT_EXPR:
407 return one_cmpl_optab;
409 case BIT_XOR_EXPR:
410 return xor_optab;
412 case MULT_HIGHPART_EXPR:
413 return TYPE_UNSIGNED (type) ? umul_highpart_optab : smul_highpart_optab;
415 case TRUNC_MOD_EXPR:
416 case CEIL_MOD_EXPR:
417 case FLOOR_MOD_EXPR:
418 case ROUND_MOD_EXPR:
419 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
421 case RDIV_EXPR:
422 case TRUNC_DIV_EXPR:
423 case CEIL_DIV_EXPR:
424 case FLOOR_DIV_EXPR:
425 case ROUND_DIV_EXPR:
426 case EXACT_DIV_EXPR:
427 if (TYPE_SATURATING (type))
428 return TYPE_UNSIGNED (type) ? usdiv_optab : ssdiv_optab;
429 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
431 case LSHIFT_EXPR:
432 if (TREE_CODE (type) == VECTOR_TYPE)
434 if (subtype == optab_vector)
435 return TYPE_SATURATING (type) ? unknown_optab : vashl_optab;
437 gcc_assert (subtype == optab_scalar);
439 if (TYPE_SATURATING (type))
440 return TYPE_UNSIGNED (type) ? usashl_optab : ssashl_optab;
441 return ashl_optab;
443 case RSHIFT_EXPR:
444 if (TREE_CODE (type) == VECTOR_TYPE)
446 if (subtype == optab_vector)
447 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
449 gcc_assert (subtype == optab_scalar);
451 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
453 case LROTATE_EXPR:
454 if (TREE_CODE (type) == VECTOR_TYPE)
456 if (subtype == optab_vector)
457 return vrotl_optab;
459 gcc_assert (subtype == optab_scalar);
461 return rotl_optab;
463 case RROTATE_EXPR:
464 if (TREE_CODE (type) == VECTOR_TYPE)
466 if (subtype == optab_vector)
467 return vrotr_optab;
469 gcc_assert (subtype == optab_scalar);
471 return rotr_optab;
473 case MAX_EXPR:
474 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
476 case MIN_EXPR:
477 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
479 case REALIGN_LOAD_EXPR:
480 return vec_realign_load_optab;
482 case WIDEN_SUM_EXPR:
483 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
485 case DOT_PROD_EXPR:
486 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
488 case SAD_EXPR:
489 return TYPE_UNSIGNED (type) ? usad_optab : ssad_optab;
491 case WIDEN_MULT_PLUS_EXPR:
492 return (TYPE_UNSIGNED (type)
493 ? (TYPE_SATURATING (type)
494 ? usmadd_widen_optab : umadd_widen_optab)
495 : (TYPE_SATURATING (type)
496 ? ssmadd_widen_optab : smadd_widen_optab));
498 case WIDEN_MULT_MINUS_EXPR:
499 return (TYPE_UNSIGNED (type)
500 ? (TYPE_SATURATING (type)
501 ? usmsub_widen_optab : umsub_widen_optab)
502 : (TYPE_SATURATING (type)
503 ? ssmsub_widen_optab : smsub_widen_optab));
505 case FMA_EXPR:
506 return fma_optab;
508 case REDUC_MAX_EXPR:
509 return TYPE_UNSIGNED (type)
510 ? reduc_umax_scal_optab : reduc_smax_scal_optab;
512 case REDUC_MIN_EXPR:
513 return TYPE_UNSIGNED (type)
514 ? reduc_umin_scal_optab : reduc_smin_scal_optab;
516 case REDUC_PLUS_EXPR:
517 return reduc_plus_scal_optab;
519 case VEC_WIDEN_MULT_HI_EXPR:
520 return TYPE_UNSIGNED (type) ?
521 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
523 case VEC_WIDEN_MULT_LO_EXPR:
524 return TYPE_UNSIGNED (type) ?
525 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
527 case VEC_WIDEN_MULT_EVEN_EXPR:
528 return TYPE_UNSIGNED (type) ?
529 vec_widen_umult_even_optab : vec_widen_smult_even_optab;
531 case VEC_WIDEN_MULT_ODD_EXPR:
532 return TYPE_UNSIGNED (type) ?
533 vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
535 case VEC_WIDEN_LSHIFT_HI_EXPR:
536 return TYPE_UNSIGNED (type) ?
537 vec_widen_ushiftl_hi_optab : vec_widen_sshiftl_hi_optab;
539 case VEC_WIDEN_LSHIFT_LO_EXPR:
540 return TYPE_UNSIGNED (type) ?
541 vec_widen_ushiftl_lo_optab : vec_widen_sshiftl_lo_optab;
543 case VEC_UNPACK_HI_EXPR:
544 return TYPE_UNSIGNED (type) ?
545 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
547 case VEC_UNPACK_LO_EXPR:
548 return TYPE_UNSIGNED (type) ?
549 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
551 case VEC_UNPACK_FLOAT_HI_EXPR:
552 /* The signedness is determined from input operand. */
553 return TYPE_UNSIGNED (type) ?
554 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
556 case VEC_UNPACK_FLOAT_LO_EXPR:
557 /* The signedness is determined from input operand. */
558 return TYPE_UNSIGNED (type) ?
559 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
561 case VEC_PACK_TRUNC_EXPR:
562 return vec_pack_trunc_optab;
564 case VEC_PACK_SAT_EXPR:
565 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
567 case VEC_PACK_FIX_TRUNC_EXPR:
568 /* The signedness is determined from output operand. */
569 return TYPE_UNSIGNED (type) ?
570 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
572 default:
573 break;
576 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
577 switch (code)
579 case POINTER_PLUS_EXPR:
580 case PLUS_EXPR:
581 if (TYPE_SATURATING (type))
582 return TYPE_UNSIGNED (type) ? usadd_optab : ssadd_optab;
583 return trapv ? addv_optab : add_optab;
585 case MINUS_EXPR:
586 if (TYPE_SATURATING (type))
587 return TYPE_UNSIGNED (type) ? ussub_optab : sssub_optab;
588 return trapv ? subv_optab : sub_optab;
590 case MULT_EXPR:
591 if (TYPE_SATURATING (type))
592 return TYPE_UNSIGNED (type) ? usmul_optab : ssmul_optab;
593 return trapv ? smulv_optab : smul_optab;
595 case NEGATE_EXPR:
596 if (TYPE_SATURATING (type))
597 return TYPE_UNSIGNED (type) ? usneg_optab : ssneg_optab;
598 return trapv ? negv_optab : neg_optab;
600 case ABS_EXPR:
601 return trapv ? absv_optab : abs_optab;
603 default:
604 return unknown_optab;
608 /* Given optab UNOPTAB that reduces a vector to a scalar, find instead the old
609 optab that produces a vector with the reduction result in one element,
610 for a tree with type TYPE. */
612 optab
613 scalar_reduc_to_vector (optab unoptab, const_tree type)
615 switch (unoptab)
617 case reduc_plus_scal_optab:
618 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
620 case reduc_smin_scal_optab: return reduc_smin_optab;
621 case reduc_umin_scal_optab: return reduc_umin_optab;
622 case reduc_smax_scal_optab: return reduc_smax_optab;
623 case reduc_umax_scal_optab: return reduc_umax_optab;
624 default: return unknown_optab;
628 /* Expand vector widening operations.
630 There are two different classes of operations handled here:
631 1) Operations whose result is wider than all the arguments to the operation.
632 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
633 In this case OP0 and optionally OP1 would be initialized,
634 but WIDE_OP wouldn't (not relevant for this case).
635 2) Operations whose result is of the same size as the last argument to the
636 operation, but wider than all the other arguments to the operation.
637 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
638 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
640 E.g, when called to expand the following operations, this is how
641 the arguments will be initialized:
642 nops OP0 OP1 WIDE_OP
643 widening-sum 2 oprnd0 - oprnd1
644 widening-dot-product 3 oprnd0 oprnd1 oprnd2
645 widening-mult 2 oprnd0 oprnd1 -
646 type-promotion (vec-unpack) 1 oprnd0 - - */
649 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
650 rtx target, int unsignedp)
652 struct expand_operand eops[4];
653 tree oprnd0, oprnd1, oprnd2;
654 machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
655 optab widen_pattern_optab;
656 enum insn_code icode;
657 int nops = TREE_CODE_LENGTH (ops->code);
658 int op;
660 oprnd0 = ops->op0;
661 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
662 widen_pattern_optab =
663 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
664 if (ops->code == WIDEN_MULT_PLUS_EXPR
665 || ops->code == WIDEN_MULT_MINUS_EXPR)
666 icode = find_widening_optab_handler (widen_pattern_optab,
667 TYPE_MODE (TREE_TYPE (ops->op2)),
668 tmode0, 0);
669 else
670 icode = optab_handler (widen_pattern_optab, tmode0);
671 gcc_assert (icode != CODE_FOR_nothing);
673 if (nops >= 2)
675 oprnd1 = ops->op1;
676 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
679 /* The last operand is of a wider mode than the rest of the operands. */
680 if (nops == 2)
681 wmode = tmode1;
682 else if (nops == 3)
684 gcc_assert (tmode1 == tmode0);
685 gcc_assert (op1);
686 oprnd2 = ops->op2;
687 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
690 op = 0;
691 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
692 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
693 if (op1)
694 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
695 if (wide_op)
696 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
697 expand_insn (icode, op, eops);
698 return eops[0].value;
701 /* Generate code to perform an operation specified by TERNARY_OPTAB
702 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
704 UNSIGNEDP is for the case where we have to widen the operands
705 to perform the operation. It says to use zero-extension.
707 If TARGET is nonzero, the value
708 is generated there, if it is convenient to do so.
709 In all cases an rtx is returned for the locus of the value;
710 this may or may not be TARGET. */
713 expand_ternary_op (machine_mode mode, optab ternary_optab, rtx op0,
714 rtx op1, rtx op2, rtx target, int unsignedp)
716 struct expand_operand ops[4];
717 enum insn_code icode = optab_handler (ternary_optab, mode);
719 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
721 create_output_operand (&ops[0], target, mode);
722 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
723 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
724 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
725 expand_insn (icode, 4, ops);
726 return ops[0].value;
730 /* Like expand_binop, but return a constant rtx if the result can be
731 calculated at compile time. The arguments and return value are
732 otherwise the same as for expand_binop. */
735 simplify_expand_binop (machine_mode mode, optab binoptab,
736 rtx op0, rtx op1, rtx target, int unsignedp,
737 enum optab_methods methods)
739 if (CONSTANT_P (op0) && CONSTANT_P (op1))
741 rtx x = simplify_binary_operation (optab_to_code (binoptab),
742 mode, op0, op1);
743 if (x)
744 return x;
747 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
750 /* Like simplify_expand_binop, but always put the result in TARGET.
751 Return true if the expansion succeeded. */
753 bool
754 force_expand_binop (machine_mode mode, optab binoptab,
755 rtx op0, rtx op1, rtx target, int unsignedp,
756 enum optab_methods methods)
758 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
759 target, unsignedp, methods);
760 if (x == 0)
761 return false;
762 if (x != target)
763 emit_move_insn (target, x);
764 return true;
767 /* Create a new vector value in VMODE with all elements set to OP. The
768 mode of OP must be the element mode of VMODE. If OP is a constant,
769 then the return value will be a constant. */
771 static rtx
772 expand_vector_broadcast (machine_mode vmode, rtx op)
774 enum insn_code icode;
775 rtvec vec;
776 rtx ret;
777 int i, n;
779 gcc_checking_assert (VECTOR_MODE_P (vmode));
781 n = GET_MODE_NUNITS (vmode);
782 vec = rtvec_alloc (n);
783 for (i = 0; i < n; ++i)
784 RTVEC_ELT (vec, i) = op;
786 if (CONSTANT_P (op))
787 return gen_rtx_CONST_VECTOR (vmode, vec);
789 /* ??? If the target doesn't have a vec_init, then we have no easy way
790 of performing this operation. Most of this sort of generic support
791 is hidden away in the vector lowering support in gimple. */
792 icode = optab_handler (vec_init_optab, vmode);
793 if (icode == CODE_FOR_nothing)
794 return NULL;
796 ret = gen_reg_rtx (vmode);
797 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
799 return ret;
802 /* This subroutine of expand_doubleword_shift handles the cases in which
803 the effective shift value is >= BITS_PER_WORD. The arguments and return
804 value are the same as for the parent routine, except that SUPERWORD_OP1
805 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
806 INTO_TARGET may be null if the caller has decided to calculate it. */
808 static bool
809 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
810 rtx outof_target, rtx into_target,
811 int unsignedp, enum optab_methods methods)
813 if (into_target != 0)
814 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
815 into_target, unsignedp, methods))
816 return false;
818 if (outof_target != 0)
820 /* For a signed right shift, we must fill OUTOF_TARGET with copies
821 of the sign bit, otherwise we must fill it with zeros. */
822 if (binoptab != ashr_optab)
823 emit_move_insn (outof_target, CONST0_RTX (word_mode));
824 else
825 if (!force_expand_binop (word_mode, binoptab,
826 outof_input, GEN_INT (BITS_PER_WORD - 1),
827 outof_target, unsignedp, methods))
828 return false;
830 return true;
833 /* This subroutine of expand_doubleword_shift handles the cases in which
834 the effective shift value is < BITS_PER_WORD. The arguments and return
835 value are the same as for the parent routine. */
837 static bool
838 expand_subword_shift (machine_mode op1_mode, optab binoptab,
839 rtx outof_input, rtx into_input, rtx op1,
840 rtx outof_target, rtx into_target,
841 int unsignedp, enum optab_methods methods,
842 unsigned HOST_WIDE_INT shift_mask)
844 optab reverse_unsigned_shift, unsigned_shift;
845 rtx tmp, carries;
847 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
848 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
850 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
851 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
852 the opposite direction to BINOPTAB. */
853 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
855 carries = outof_input;
856 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
857 op1_mode), op1_mode);
858 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
859 0, true, methods);
861 else
863 /* We must avoid shifting by BITS_PER_WORD bits since that is either
864 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
865 has unknown behavior. Do a single shift first, then shift by the
866 remainder. It's OK to use ~OP1 as the remainder if shift counts
867 are truncated to the mode size. */
868 carries = expand_binop (word_mode, reverse_unsigned_shift,
869 outof_input, const1_rtx, 0, unsignedp, methods);
870 if (shift_mask == BITS_PER_WORD - 1)
872 tmp = immed_wide_int_const
873 (wi::minus_one (GET_MODE_PRECISION (op1_mode)), op1_mode);
874 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
875 0, true, methods);
877 else
879 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
880 op1_mode), op1_mode);
881 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
882 0, true, methods);
885 if (tmp == 0 || carries == 0)
886 return false;
887 carries = expand_binop (word_mode, reverse_unsigned_shift,
888 carries, tmp, 0, unsignedp, methods);
889 if (carries == 0)
890 return false;
892 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
893 so the result can go directly into INTO_TARGET if convenient. */
894 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
895 into_target, unsignedp, methods);
896 if (tmp == 0)
897 return false;
899 /* Now OR in the bits carried over from OUTOF_INPUT. */
900 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
901 into_target, unsignedp, methods))
902 return false;
904 /* Use a standard word_mode shift for the out-of half. */
905 if (outof_target != 0)
906 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
907 outof_target, unsignedp, methods))
908 return false;
910 return true;
914 /* Try implementing expand_doubleword_shift using conditional moves.
915 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
916 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
917 are the shift counts to use in the former and latter case. All other
918 arguments are the same as the parent routine. */
920 static bool
921 expand_doubleword_shift_condmove (machine_mode op1_mode, optab binoptab,
922 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
923 rtx outof_input, rtx into_input,
924 rtx subword_op1, rtx superword_op1,
925 rtx outof_target, rtx into_target,
926 int unsignedp, enum optab_methods methods,
927 unsigned HOST_WIDE_INT shift_mask)
929 rtx outof_superword, into_superword;
931 /* Put the superword version of the output into OUTOF_SUPERWORD and
932 INTO_SUPERWORD. */
933 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
934 if (outof_target != 0 && subword_op1 == superword_op1)
936 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
937 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
938 into_superword = outof_target;
939 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
940 outof_superword, 0, unsignedp, methods))
941 return false;
943 else
945 into_superword = gen_reg_rtx (word_mode);
946 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
947 outof_superword, into_superword,
948 unsignedp, methods))
949 return false;
952 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
953 if (!expand_subword_shift (op1_mode, binoptab,
954 outof_input, into_input, subword_op1,
955 outof_target, into_target,
956 unsignedp, methods, shift_mask))
957 return false;
959 /* Select between them. Do the INTO half first because INTO_SUPERWORD
960 might be the current value of OUTOF_TARGET. */
961 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
962 into_target, into_superword, word_mode, false))
963 return false;
965 if (outof_target != 0)
966 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
967 outof_target, outof_superword,
968 word_mode, false))
969 return false;
971 return true;
974 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
975 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
976 input operand; the shift moves bits in the direction OUTOF_INPUT->
977 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
978 of the target. OP1 is the shift count and OP1_MODE is its mode.
979 If OP1 is constant, it will have been truncated as appropriate
980 and is known to be nonzero.
982 If SHIFT_MASK is zero, the result of word shifts is undefined when the
983 shift count is outside the range [0, BITS_PER_WORD). This routine must
984 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
986 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
987 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
988 fill with zeros or sign bits as appropriate.
990 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
991 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
992 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
993 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
994 are undefined.
996 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
997 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
998 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
999 function wants to calculate it itself.
1001 Return true if the shift could be successfully synthesized. */
1003 static bool
1004 expand_doubleword_shift (machine_mode op1_mode, optab binoptab,
1005 rtx outof_input, rtx into_input, rtx op1,
1006 rtx outof_target, rtx into_target,
1007 int unsignedp, enum optab_methods methods,
1008 unsigned HOST_WIDE_INT shift_mask)
1010 rtx superword_op1, tmp, cmp1, cmp2;
1011 enum rtx_code cmp_code;
1013 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1014 fill the result with sign or zero bits as appropriate. If so, the value
1015 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1016 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1017 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1019 This isn't worthwhile for constant shifts since the optimizers will
1020 cope better with in-range shift counts. */
1021 if (shift_mask >= BITS_PER_WORD
1022 && outof_target != 0
1023 && !CONSTANT_P (op1))
1025 if (!expand_doubleword_shift (op1_mode, binoptab,
1026 outof_input, into_input, op1,
1027 0, into_target,
1028 unsignedp, methods, shift_mask))
1029 return false;
1030 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1031 outof_target, unsignedp, methods))
1032 return false;
1033 return true;
1036 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1037 is true when the effective shift value is less than BITS_PER_WORD.
1038 Set SUPERWORD_OP1 to the shift count that should be used to shift
1039 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1040 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
1041 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1043 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1044 is a subword shift count. */
1045 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1046 0, true, methods);
1047 cmp2 = CONST0_RTX (op1_mode);
1048 cmp_code = EQ;
1049 superword_op1 = op1;
1051 else
1053 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1054 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1055 0, true, methods);
1056 cmp2 = CONST0_RTX (op1_mode);
1057 cmp_code = LT;
1058 superword_op1 = cmp1;
1060 if (cmp1 == 0)
1061 return false;
1063 /* If we can compute the condition at compile time, pick the
1064 appropriate subroutine. */
1065 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1066 if (tmp != 0 && CONST_INT_P (tmp))
1068 if (tmp == const0_rtx)
1069 return expand_superword_shift (binoptab, outof_input, superword_op1,
1070 outof_target, into_target,
1071 unsignedp, methods);
1072 else
1073 return expand_subword_shift (op1_mode, binoptab,
1074 outof_input, into_input, op1,
1075 outof_target, into_target,
1076 unsignedp, methods, shift_mask);
1079 /* Try using conditional moves to generate straight-line code. */
1080 if (HAVE_conditional_move)
1082 rtx_insn *start = get_last_insn ();
1083 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1084 cmp_code, cmp1, cmp2,
1085 outof_input, into_input,
1086 op1, superword_op1,
1087 outof_target, into_target,
1088 unsignedp, methods, shift_mask))
1089 return true;
1090 delete_insns_since (start);
1093 /* As a last resort, use branches to select the correct alternative. */
1094 rtx_code_label *subword_label = gen_label_rtx ();
1095 rtx_code_label *done_label = gen_label_rtx ();
1097 NO_DEFER_POP;
1098 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1099 0, 0, subword_label, -1);
1100 OK_DEFER_POP;
1102 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1103 outof_target, into_target,
1104 unsignedp, methods))
1105 return false;
1107 emit_jump_insn (targetm.gen_jump (done_label));
1108 emit_barrier ();
1109 emit_label (subword_label);
1111 if (!expand_subword_shift (op1_mode, binoptab,
1112 outof_input, into_input, op1,
1113 outof_target, into_target,
1114 unsignedp, methods, shift_mask))
1115 return false;
1117 emit_label (done_label);
1118 return true;
1121 /* Subroutine of expand_binop. Perform a double word multiplication of
1122 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1123 as the target's word_mode. This function return NULL_RTX if anything
1124 goes wrong, in which case it may have already emitted instructions
1125 which need to be deleted.
1127 If we want to multiply two two-word values and have normal and widening
1128 multiplies of single-word values, we can do this with three smaller
1129 multiplications.
1131 The multiplication proceeds as follows:
1132 _______________________
1133 [__op0_high_|__op0_low__]
1134 _______________________
1135 * [__op1_high_|__op1_low__]
1136 _______________________________________________
1137 _______________________
1138 (1) [__op0_low__*__op1_low__]
1139 _______________________
1140 (2a) [__op0_low__*__op1_high_]
1141 _______________________
1142 (2b) [__op0_high_*__op1_low__]
1143 _______________________
1144 (3) [__op0_high_*__op1_high_]
1147 This gives a 4-word result. Since we are only interested in the
1148 lower 2 words, partial result (3) and the upper words of (2a) and
1149 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1150 calculated using non-widening multiplication.
1152 (1), however, needs to be calculated with an unsigned widening
1153 multiplication. If this operation is not directly supported we
1154 try using a signed widening multiplication and adjust the result.
1155 This adjustment works as follows:
1157 If both operands are positive then no adjustment is needed.
1159 If the operands have different signs, for example op0_low < 0 and
1160 op1_low >= 0, the instruction treats the most significant bit of
1161 op0_low as a sign bit instead of a bit with significance
1162 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1163 with 2**BITS_PER_WORD - op0_low, and two's complements the
1164 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1165 the result.
1167 Similarly, if both operands are negative, we need to add
1168 (op0_low + op1_low) * 2**BITS_PER_WORD.
1170 We use a trick to adjust quickly. We logically shift op0_low right
1171 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1172 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1173 logical shift exists, we do an arithmetic right shift and subtract
1174 the 0 or -1. */
1176 static rtx
1177 expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
1178 bool umulp, enum optab_methods methods)
1180 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1181 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1182 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1183 rtx product, adjust, product_high, temp;
1185 rtx op0_high = operand_subword_force (op0, high, mode);
1186 rtx op0_low = operand_subword_force (op0, low, mode);
1187 rtx op1_high = operand_subword_force (op1, high, mode);
1188 rtx op1_low = operand_subword_force (op1, low, mode);
1190 /* If we're using an unsigned multiply to directly compute the product
1191 of the low-order words of the operands and perform any required
1192 adjustments of the operands, we begin by trying two more multiplications
1193 and then computing the appropriate sum.
1195 We have checked above that the required addition is provided.
1196 Full-word addition will normally always succeed, especially if
1197 it is provided at all, so we don't worry about its failure. The
1198 multiplication may well fail, however, so we do handle that. */
1200 if (!umulp)
1202 /* ??? This could be done with emit_store_flag where available. */
1203 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1204 NULL_RTX, 1, methods);
1205 if (temp)
1206 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1207 NULL_RTX, 0, OPTAB_DIRECT);
1208 else
1210 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1211 NULL_RTX, 0, methods);
1212 if (!temp)
1213 return NULL_RTX;
1214 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1215 NULL_RTX, 0, OPTAB_DIRECT);
1218 if (!op0_high)
1219 return NULL_RTX;
1222 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1223 NULL_RTX, 0, OPTAB_DIRECT);
1224 if (!adjust)
1225 return NULL_RTX;
1227 /* OP0_HIGH should now be dead. */
1229 if (!umulp)
1231 /* ??? This could be done with emit_store_flag where available. */
1232 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1233 NULL_RTX, 1, methods);
1234 if (temp)
1235 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1236 NULL_RTX, 0, OPTAB_DIRECT);
1237 else
1239 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1240 NULL_RTX, 0, methods);
1241 if (!temp)
1242 return NULL_RTX;
1243 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1244 NULL_RTX, 0, OPTAB_DIRECT);
1247 if (!op1_high)
1248 return NULL_RTX;
1251 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1252 NULL_RTX, 0, OPTAB_DIRECT);
1253 if (!temp)
1254 return NULL_RTX;
1256 /* OP1_HIGH should now be dead. */
1258 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1259 NULL_RTX, 0, OPTAB_DIRECT);
1261 if (target && !REG_P (target))
1262 target = NULL_RTX;
1264 if (umulp)
1265 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1266 target, 1, OPTAB_DIRECT);
1267 else
1268 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1269 target, 1, OPTAB_DIRECT);
1271 if (!product)
1272 return NULL_RTX;
1274 product_high = operand_subword (product, high, 1, mode);
1275 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1276 NULL_RTX, 0, OPTAB_DIRECT);
1277 emit_move_insn (product_high, adjust);
1278 return product;
1281 /* Wrapper around expand_binop which takes an rtx code to specify
1282 the operation to perform, not an optab pointer. All other
1283 arguments are the same. */
1285 expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
1286 rtx op1, rtx target, int unsignedp,
1287 enum optab_methods methods)
1289 optab binop = code_to_optab (code);
1290 gcc_assert (binop);
1292 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1295 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1296 binop. Order them according to commutative_operand_precedence and, if
1297 possible, try to put TARGET or a pseudo first. */
1298 static bool
1299 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1301 int op0_prec = commutative_operand_precedence (op0);
1302 int op1_prec = commutative_operand_precedence (op1);
1304 if (op0_prec < op1_prec)
1305 return true;
1307 if (op0_prec > op1_prec)
1308 return false;
1310 /* With equal precedence, both orders are ok, but it is better if the
1311 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1312 if (target == 0 || REG_P (target))
1313 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1314 else
1315 return rtx_equal_p (op1, target);
1318 /* Return true if BINOPTAB implements a shift operation. */
1320 static bool
1321 shift_optab_p (optab binoptab)
1323 switch (optab_to_code (binoptab))
1325 case ASHIFT:
1326 case SS_ASHIFT:
1327 case US_ASHIFT:
1328 case ASHIFTRT:
1329 case LSHIFTRT:
1330 case ROTATE:
1331 case ROTATERT:
1332 return true;
1334 default:
1335 return false;
1339 /* Return true if BINOPTAB implements a commutative binary operation. */
1341 static bool
1342 commutative_optab_p (optab binoptab)
1344 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1345 || binoptab == smul_widen_optab
1346 || binoptab == umul_widen_optab
1347 || binoptab == smul_highpart_optab
1348 || binoptab == umul_highpart_optab);
1351 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1352 optimizing, and if the operand is a constant that costs more than
1353 1 instruction, force the constant into a register and return that
1354 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1356 static rtx
1357 avoid_expensive_constant (machine_mode mode, optab binoptab,
1358 int opn, rtx x, bool unsignedp)
1360 bool speed = optimize_insn_for_speed_p ();
1362 if (mode != VOIDmode
1363 && optimize
1364 && CONSTANT_P (x)
1365 && (rtx_cost (x, mode, optab_to_code (binoptab), opn, speed)
1366 > set_src_cost (x, mode, speed)))
1368 if (CONST_INT_P (x))
1370 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1371 if (intval != INTVAL (x))
1372 x = GEN_INT (intval);
1374 else
1375 x = convert_modes (mode, VOIDmode, x, unsignedp);
1376 x = force_reg (mode, x);
1378 return x;
1381 /* Helper function for expand_binop: handle the case where there
1382 is an insn that directly implements the indicated operation.
1383 Returns null if this is not possible. */
1384 static rtx
1385 expand_binop_directly (machine_mode mode, optab binoptab,
1386 rtx op0, rtx op1,
1387 rtx target, int unsignedp, enum optab_methods methods,
1388 rtx_insn *last)
1390 machine_mode from_mode = widened_mode (mode, op0, op1);
1391 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1392 from_mode, 1);
1393 machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1394 machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1395 machine_mode mode0, mode1, tmp_mode;
1396 struct expand_operand ops[3];
1397 bool commutative_p;
1398 rtx_insn *pat;
1399 rtx xop0 = op0, xop1 = op1;
1401 /* If it is a commutative operator and the modes would match
1402 if we would swap the operands, we can save the conversions. */
1403 commutative_p = commutative_optab_p (binoptab);
1404 if (commutative_p
1405 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1406 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1407 std::swap (xop0, xop1);
1409 /* If we are optimizing, force expensive constants into a register. */
1410 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1411 if (!shift_optab_p (binoptab))
1412 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1414 /* In case the insn wants input operands in modes different from
1415 those of the actual operands, convert the operands. It would
1416 seem that we don't need to convert CONST_INTs, but we do, so
1417 that they're properly zero-extended, sign-extended or truncated
1418 for their mode. */
1420 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1421 if (xmode0 != VOIDmode && xmode0 != mode0)
1423 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1424 mode0 = xmode0;
1427 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1428 if (xmode1 != VOIDmode && xmode1 != mode1)
1430 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1431 mode1 = xmode1;
1434 /* If operation is commutative,
1435 try to make the first operand a register.
1436 Even better, try to make it the same as the target.
1437 Also try to make the last operand a constant. */
1438 if (commutative_p
1439 && swap_commutative_operands_with_target (target, xop0, xop1))
1440 std::swap (xop0, xop1);
1442 /* Now, if insn's predicates don't allow our operands, put them into
1443 pseudo regs. */
1445 if (binoptab == vec_pack_trunc_optab
1446 || binoptab == vec_pack_usat_optab
1447 || binoptab == vec_pack_ssat_optab
1448 || binoptab == vec_pack_ufix_trunc_optab
1449 || binoptab == vec_pack_sfix_trunc_optab)
1451 /* The mode of the result is different then the mode of the
1452 arguments. */
1453 tmp_mode = insn_data[(int) icode].operand[0].mode;
1454 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1456 delete_insns_since (last);
1457 return NULL_RTX;
1460 else
1461 tmp_mode = mode;
1463 create_output_operand (&ops[0], target, tmp_mode);
1464 create_input_operand (&ops[1], xop0, mode0);
1465 create_input_operand (&ops[2], xop1, mode1);
1466 pat = maybe_gen_insn (icode, 3, ops);
1467 if (pat)
1469 /* If PAT is composed of more than one insn, try to add an appropriate
1470 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1471 operand, call expand_binop again, this time without a target. */
1472 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1473 && ! add_equal_note (pat, ops[0].value,
1474 optab_to_code (binoptab),
1475 ops[1].value, ops[2].value))
1477 delete_insns_since (last);
1478 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1479 unsignedp, methods);
1482 emit_insn (pat);
1483 return ops[0].value;
1485 delete_insns_since (last);
1486 return NULL_RTX;
1489 /* Generate code to perform an operation specified by BINOPTAB
1490 on operands OP0 and OP1, with result having machine-mode MODE.
1492 UNSIGNEDP is for the case where we have to widen the operands
1493 to perform the operation. It says to use zero-extension.
1495 If TARGET is nonzero, the value
1496 is generated there, if it is convenient to do so.
1497 In all cases an rtx is returned for the locus of the value;
1498 this may or may not be TARGET. */
1501 expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
1502 rtx target, int unsignedp, enum optab_methods methods)
1504 enum optab_methods next_methods
1505 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1506 ? OPTAB_WIDEN : methods);
1507 enum mode_class mclass;
1508 machine_mode wider_mode;
1509 rtx libfunc;
1510 rtx temp;
1511 rtx_insn *entry_last = get_last_insn ();
1512 rtx_insn *last;
1514 mclass = GET_MODE_CLASS (mode);
1516 /* If subtracting an integer constant, convert this into an addition of
1517 the negated constant. */
1519 if (binoptab == sub_optab && CONST_INT_P (op1))
1521 op1 = negate_rtx (mode, op1);
1522 binoptab = add_optab;
1525 /* Record where to delete back to if we backtrack. */
1526 last = get_last_insn ();
1528 /* If we can do it with a three-operand insn, do so. */
1530 if (methods != OPTAB_MUST_WIDEN
1531 && find_widening_optab_handler (binoptab, mode,
1532 widened_mode (mode, op0, op1), 1)
1533 != CODE_FOR_nothing)
1535 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1536 unsignedp, methods, last);
1537 if (temp)
1538 return temp;
1541 /* If we were trying to rotate, and that didn't work, try rotating
1542 the other direction before falling back to shifts and bitwise-or. */
1543 if (((binoptab == rotl_optab
1544 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1545 || (binoptab == rotr_optab
1546 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1547 && mclass == MODE_INT)
1549 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1550 rtx newop1;
1551 unsigned int bits = GET_MODE_PRECISION (mode);
1553 if (CONST_INT_P (op1))
1554 newop1 = GEN_INT (bits - INTVAL (op1));
1555 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1556 newop1 = negate_rtx (GET_MODE (op1), op1);
1557 else
1558 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1559 gen_int_mode (bits, GET_MODE (op1)), op1,
1560 NULL_RTX, unsignedp, OPTAB_DIRECT);
1562 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1563 target, unsignedp, methods, last);
1564 if (temp)
1565 return temp;
1568 /* If this is a multiply, see if we can do a widening operation that
1569 takes operands of this mode and makes a wider mode. */
1571 if (binoptab == smul_optab
1572 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1573 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1574 : smul_widen_optab),
1575 GET_MODE_2XWIDER_MODE (mode), mode)
1576 != CODE_FOR_nothing))
1578 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1579 unsignedp ? umul_widen_optab : smul_widen_optab,
1580 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1582 if (temp != 0)
1584 if (GET_MODE_CLASS (mode) == MODE_INT
1585 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1586 return gen_lowpart (mode, temp);
1587 else
1588 return convert_to_mode (mode, temp, unsignedp);
1592 /* If this is a vector shift by a scalar, see if we can do a vector
1593 shift by a vector. If so, broadcast the scalar into a vector. */
1594 if (mclass == MODE_VECTOR_INT)
1596 optab otheroptab = unknown_optab;
1598 if (binoptab == ashl_optab)
1599 otheroptab = vashl_optab;
1600 else if (binoptab == ashr_optab)
1601 otheroptab = vashr_optab;
1602 else if (binoptab == lshr_optab)
1603 otheroptab = vlshr_optab;
1604 else if (binoptab == rotl_optab)
1605 otheroptab = vrotl_optab;
1606 else if (binoptab == rotr_optab)
1607 otheroptab = vrotr_optab;
1609 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1611 /* The scalar may have been extended to be too wide. Truncate
1612 it back to the proper size to fit in the broadcast vector. */
1613 machine_mode inner_mode = GET_MODE_INNER (mode);
1614 if (!CONST_INT_P (op1)
1615 && (GET_MODE_BITSIZE (inner_mode)
1616 < GET_MODE_BITSIZE (GET_MODE (op1))))
1617 op1 = force_reg (inner_mode,
1618 simplify_gen_unary (TRUNCATE, inner_mode, op1,
1619 GET_MODE (op1)));
1620 rtx vop1 = expand_vector_broadcast (mode, op1);
1621 if (vop1)
1623 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1624 target, unsignedp, methods, last);
1625 if (temp)
1626 return temp;
1631 /* Look for a wider mode of the same class for which we think we
1632 can open-code the operation. Check for a widening multiply at the
1633 wider mode as well. */
1635 if (CLASS_HAS_WIDER_MODES_P (mclass)
1636 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1637 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1638 wider_mode != VOIDmode;
1639 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1641 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1642 || (binoptab == smul_optab
1643 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1644 && (find_widening_optab_handler ((unsignedp
1645 ? umul_widen_optab
1646 : smul_widen_optab),
1647 GET_MODE_WIDER_MODE (wider_mode),
1648 mode, 0)
1649 != CODE_FOR_nothing)))
1651 rtx xop0 = op0, xop1 = op1;
1652 int no_extend = 0;
1654 /* For certain integer operations, we need not actually extend
1655 the narrow operands, as long as we will truncate
1656 the results to the same narrowness. */
1658 if ((binoptab == ior_optab || binoptab == and_optab
1659 || binoptab == xor_optab
1660 || binoptab == add_optab || binoptab == sub_optab
1661 || binoptab == smul_optab || binoptab == ashl_optab)
1662 && mclass == MODE_INT)
1664 no_extend = 1;
1665 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1666 xop0, unsignedp);
1667 if (binoptab != ashl_optab)
1668 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1669 xop1, unsignedp);
1672 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1674 /* The second operand of a shift must always be extended. */
1675 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1676 no_extend && binoptab != ashl_optab);
1678 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1679 unsignedp, OPTAB_DIRECT);
1680 if (temp)
1682 if (mclass != MODE_INT
1683 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1685 if (target == 0)
1686 target = gen_reg_rtx (mode);
1687 convert_move (target, temp, 0);
1688 return target;
1690 else
1691 return gen_lowpart (mode, temp);
1693 else
1694 delete_insns_since (last);
1698 /* If operation is commutative,
1699 try to make the first operand a register.
1700 Even better, try to make it the same as the target.
1701 Also try to make the last operand a constant. */
1702 if (commutative_optab_p (binoptab)
1703 && swap_commutative_operands_with_target (target, op0, op1))
1704 std::swap (op0, op1);
1706 /* These can be done a word at a time. */
1707 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1708 && mclass == MODE_INT
1709 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1710 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1712 int i;
1713 rtx_insn *insns;
1715 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1716 won't be accurate, so use a new target. */
1717 if (target == 0
1718 || target == op0
1719 || target == op1
1720 || !valid_multiword_target_p (target))
1721 target = gen_reg_rtx (mode);
1723 start_sequence ();
1725 /* Do the actual arithmetic. */
1726 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1728 rtx target_piece = operand_subword (target, i, 1, mode);
1729 rtx x = expand_binop (word_mode, binoptab,
1730 operand_subword_force (op0, i, mode),
1731 operand_subword_force (op1, i, mode),
1732 target_piece, unsignedp, next_methods);
1734 if (x == 0)
1735 break;
1737 if (target_piece != x)
1738 emit_move_insn (target_piece, x);
1741 insns = get_insns ();
1742 end_sequence ();
1744 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1746 emit_insn (insns);
1747 return target;
1751 /* Synthesize double word shifts from single word shifts. */
1752 if ((binoptab == lshr_optab || binoptab == ashl_optab
1753 || binoptab == ashr_optab)
1754 && mclass == MODE_INT
1755 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1756 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1757 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1758 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1759 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1760 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1762 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1763 machine_mode op1_mode;
1765 double_shift_mask = targetm.shift_truncation_mask (mode);
1766 shift_mask = targetm.shift_truncation_mask (word_mode);
1767 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1769 /* Apply the truncation to constant shifts. */
1770 if (double_shift_mask > 0 && CONST_INT_P (op1))
1771 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1773 if (op1 == CONST0_RTX (op1_mode))
1774 return op0;
1776 /* Make sure that this is a combination that expand_doubleword_shift
1777 can handle. See the comments there for details. */
1778 if (double_shift_mask == 0
1779 || (shift_mask == BITS_PER_WORD - 1
1780 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1782 rtx_insn *insns;
1783 rtx into_target, outof_target;
1784 rtx into_input, outof_input;
1785 int left_shift, outof_word;
1787 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1788 won't be accurate, so use a new target. */
1789 if (target == 0
1790 || target == op0
1791 || target == op1
1792 || !valid_multiword_target_p (target))
1793 target = gen_reg_rtx (mode);
1795 start_sequence ();
1797 /* OUTOF_* is the word we are shifting bits away from, and
1798 INTO_* is the word that we are shifting bits towards, thus
1799 they differ depending on the direction of the shift and
1800 WORDS_BIG_ENDIAN. */
1802 left_shift = binoptab == ashl_optab;
1803 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1805 outof_target = operand_subword (target, outof_word, 1, mode);
1806 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1808 outof_input = operand_subword_force (op0, outof_word, mode);
1809 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1811 if (expand_doubleword_shift (op1_mode, binoptab,
1812 outof_input, into_input, op1,
1813 outof_target, into_target,
1814 unsignedp, next_methods, shift_mask))
1816 insns = get_insns ();
1817 end_sequence ();
1819 emit_insn (insns);
1820 return target;
1822 end_sequence ();
1826 /* Synthesize double word rotates from single word shifts. */
1827 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1828 && mclass == MODE_INT
1829 && CONST_INT_P (op1)
1830 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1831 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1832 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1834 rtx_insn *insns;
1835 rtx into_target, outof_target;
1836 rtx into_input, outof_input;
1837 rtx inter;
1838 int shift_count, left_shift, outof_word;
1840 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1841 won't be accurate, so use a new target. Do this also if target is not
1842 a REG, first because having a register instead may open optimization
1843 opportunities, and second because if target and op0 happen to be MEMs
1844 designating the same location, we would risk clobbering it too early
1845 in the code sequence we generate below. */
1846 if (target == 0
1847 || target == op0
1848 || target == op1
1849 || !REG_P (target)
1850 || !valid_multiword_target_p (target))
1851 target = gen_reg_rtx (mode);
1853 start_sequence ();
1855 shift_count = INTVAL (op1);
1857 /* OUTOF_* is the word we are shifting bits away from, and
1858 INTO_* is the word that we are shifting bits towards, thus
1859 they differ depending on the direction of the shift and
1860 WORDS_BIG_ENDIAN. */
1862 left_shift = (binoptab == rotl_optab);
1863 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1865 outof_target = operand_subword (target, outof_word, 1, mode);
1866 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1868 outof_input = operand_subword_force (op0, outof_word, mode);
1869 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1871 if (shift_count == BITS_PER_WORD)
1873 /* This is just a word swap. */
1874 emit_move_insn (outof_target, into_input);
1875 emit_move_insn (into_target, outof_input);
1876 inter = const0_rtx;
1878 else
1880 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1881 rtx first_shift_count, second_shift_count;
1882 optab reverse_unsigned_shift, unsigned_shift;
1884 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1885 ? lshr_optab : ashl_optab);
1887 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1888 ? ashl_optab : lshr_optab);
1890 if (shift_count > BITS_PER_WORD)
1892 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1893 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1895 else
1897 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1898 second_shift_count = GEN_INT (shift_count);
1901 into_temp1 = expand_binop (word_mode, unsigned_shift,
1902 outof_input, first_shift_count,
1903 NULL_RTX, unsignedp, next_methods);
1904 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1905 into_input, second_shift_count,
1906 NULL_RTX, unsignedp, next_methods);
1908 if (into_temp1 != 0 && into_temp2 != 0)
1909 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1910 into_target, unsignedp, next_methods);
1911 else
1912 inter = 0;
1914 if (inter != 0 && inter != into_target)
1915 emit_move_insn (into_target, inter);
1917 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1918 into_input, first_shift_count,
1919 NULL_RTX, unsignedp, next_methods);
1920 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1921 outof_input, second_shift_count,
1922 NULL_RTX, unsignedp, next_methods);
1924 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1925 inter = expand_binop (word_mode, ior_optab,
1926 outof_temp1, outof_temp2,
1927 outof_target, unsignedp, next_methods);
1929 if (inter != 0 && inter != outof_target)
1930 emit_move_insn (outof_target, inter);
1933 insns = get_insns ();
1934 end_sequence ();
1936 if (inter != 0)
1938 emit_insn (insns);
1939 return target;
1943 /* These can be done a word at a time by propagating carries. */
1944 if ((binoptab == add_optab || binoptab == sub_optab)
1945 && mclass == MODE_INT
1946 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1947 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1949 unsigned int i;
1950 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1951 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1952 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1953 rtx xop0, xop1, xtarget;
1955 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1956 value is one of those, use it. Otherwise, use 1 since it is the
1957 one easiest to get. */
1958 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1959 int normalizep = STORE_FLAG_VALUE;
1960 #else
1961 int normalizep = 1;
1962 #endif
1964 /* Prepare the operands. */
1965 xop0 = force_reg (mode, op0);
1966 xop1 = force_reg (mode, op1);
1968 xtarget = gen_reg_rtx (mode);
1970 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1971 target = xtarget;
1973 /* Indicate for flow that the entire target reg is being set. */
1974 if (REG_P (target))
1975 emit_clobber (xtarget);
1977 /* Do the actual arithmetic. */
1978 for (i = 0; i < nwords; i++)
1980 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1981 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1982 rtx op0_piece = operand_subword_force (xop0, index, mode);
1983 rtx op1_piece = operand_subword_force (xop1, index, mode);
1984 rtx x;
1986 /* Main add/subtract of the input operands. */
1987 x = expand_binop (word_mode, binoptab,
1988 op0_piece, op1_piece,
1989 target_piece, unsignedp, next_methods);
1990 if (x == 0)
1991 break;
1993 if (i + 1 < nwords)
1995 /* Store carry from main add/subtract. */
1996 carry_out = gen_reg_rtx (word_mode);
1997 carry_out = emit_store_flag_force (carry_out,
1998 (binoptab == add_optab
1999 ? LT : GT),
2000 x, op0_piece,
2001 word_mode, 1, normalizep);
2004 if (i > 0)
2006 rtx newx;
2008 /* Add/subtract previous carry to main result. */
2009 newx = expand_binop (word_mode,
2010 normalizep == 1 ? binoptab : otheroptab,
2011 x, carry_in,
2012 NULL_RTX, 1, next_methods);
2014 if (i + 1 < nwords)
2016 /* Get out carry from adding/subtracting carry in. */
2017 rtx carry_tmp = gen_reg_rtx (word_mode);
2018 carry_tmp = emit_store_flag_force (carry_tmp,
2019 (binoptab == add_optab
2020 ? LT : GT),
2021 newx, x,
2022 word_mode, 1, normalizep);
2024 /* Logical-ior the two poss. carry together. */
2025 carry_out = expand_binop (word_mode, ior_optab,
2026 carry_out, carry_tmp,
2027 carry_out, 0, next_methods);
2028 if (carry_out == 0)
2029 break;
2031 emit_move_insn (target_piece, newx);
2033 else
2035 if (x != target_piece)
2036 emit_move_insn (target_piece, x);
2039 carry_in = carry_out;
2042 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2044 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2045 || ! rtx_equal_p (target, xtarget))
2047 rtx_insn *temp = emit_move_insn (target, xtarget);
2049 set_dst_reg_note (temp, REG_EQUAL,
2050 gen_rtx_fmt_ee (optab_to_code (binoptab),
2051 mode, copy_rtx (xop0),
2052 copy_rtx (xop1)),
2053 target);
2055 else
2056 target = xtarget;
2058 return target;
2061 else
2062 delete_insns_since (last);
2065 /* Attempt to synthesize double word multiplies using a sequence of word
2066 mode multiplications. We first attempt to generate a sequence using a
2067 more efficient unsigned widening multiply, and if that fails we then
2068 try using a signed widening multiply. */
2070 if (binoptab == smul_optab
2071 && mclass == MODE_INT
2072 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2073 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2074 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2076 rtx product = NULL_RTX;
2077 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
2078 != CODE_FOR_nothing)
2080 product = expand_doubleword_mult (mode, op0, op1, target,
2081 true, methods);
2082 if (!product)
2083 delete_insns_since (last);
2086 if (product == NULL_RTX
2087 && widening_optab_handler (smul_widen_optab, mode, word_mode)
2088 != CODE_FOR_nothing)
2090 product = expand_doubleword_mult (mode, op0, op1, target,
2091 false, methods);
2092 if (!product)
2093 delete_insns_since (last);
2096 if (product != NULL_RTX)
2098 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2100 temp = emit_move_insn (target ? target : product, product);
2101 set_dst_reg_note (temp,
2102 REG_EQUAL,
2103 gen_rtx_fmt_ee (MULT, mode,
2104 copy_rtx (op0),
2105 copy_rtx (op1)),
2106 target ? target : product);
2108 return product;
2112 /* It can't be open-coded in this mode.
2113 Use a library call if one is available and caller says that's ok. */
2115 libfunc = optab_libfunc (binoptab, mode);
2116 if (libfunc
2117 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2119 rtx_insn *insns;
2120 rtx op1x = op1;
2121 machine_mode op1_mode = mode;
2122 rtx value;
2124 start_sequence ();
2126 if (shift_optab_p (binoptab))
2128 op1_mode = targetm.libgcc_shift_count_mode ();
2129 /* Specify unsigned here,
2130 since negative shift counts are meaningless. */
2131 op1x = convert_to_mode (op1_mode, op1, 1);
2134 if (GET_MODE (op0) != VOIDmode
2135 && GET_MODE (op0) != mode)
2136 op0 = convert_to_mode (mode, op0, unsignedp);
2138 /* Pass 1 for NO_QUEUE so we don't lose any increments
2139 if the libcall is cse'd or moved. */
2140 value = emit_library_call_value (libfunc,
2141 NULL_RTX, LCT_CONST, mode, 2,
2142 op0, mode, op1x, op1_mode);
2144 insns = get_insns ();
2145 end_sequence ();
2147 target = gen_reg_rtx (mode);
2148 emit_libcall_block_1 (insns, target, value,
2149 gen_rtx_fmt_ee (optab_to_code (binoptab),
2150 mode, op0, op1),
2151 trapv_binoptab_p (binoptab));
2153 return target;
2156 delete_insns_since (last);
2158 /* It can't be done in this mode. Can we do it in a wider mode? */
2160 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2161 || methods == OPTAB_MUST_WIDEN))
2163 /* Caller says, don't even try. */
2164 delete_insns_since (entry_last);
2165 return 0;
2168 /* Compute the value of METHODS to pass to recursive calls.
2169 Don't allow widening to be tried recursively. */
2171 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2173 /* Look for a wider mode of the same class for which it appears we can do
2174 the operation. */
2176 if (CLASS_HAS_WIDER_MODES_P (mclass))
2178 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2179 wider_mode != VOIDmode;
2180 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2182 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2183 != CODE_FOR_nothing
2184 || (methods == OPTAB_LIB
2185 && optab_libfunc (binoptab, wider_mode)))
2187 rtx xop0 = op0, xop1 = op1;
2188 int no_extend = 0;
2190 /* For certain integer operations, we need not actually extend
2191 the narrow operands, as long as we will truncate
2192 the results to the same narrowness. */
2194 if ((binoptab == ior_optab || binoptab == and_optab
2195 || binoptab == xor_optab
2196 || binoptab == add_optab || binoptab == sub_optab
2197 || binoptab == smul_optab || binoptab == ashl_optab)
2198 && mclass == MODE_INT)
2199 no_extend = 1;
2201 xop0 = widen_operand (xop0, wider_mode, mode,
2202 unsignedp, no_extend);
2204 /* The second operand of a shift must always be extended. */
2205 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2206 no_extend && binoptab != ashl_optab);
2208 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2209 unsignedp, methods);
2210 if (temp)
2212 if (mclass != MODE_INT
2213 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2215 if (target == 0)
2216 target = gen_reg_rtx (mode);
2217 convert_move (target, temp, 0);
2218 return target;
2220 else
2221 return gen_lowpart (mode, temp);
2223 else
2224 delete_insns_since (last);
2229 delete_insns_since (entry_last);
2230 return 0;
2233 /* Expand a binary operator which has both signed and unsigned forms.
2234 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2235 signed operations.
2237 If we widen unsigned operands, we may use a signed wider operation instead
2238 of an unsigned wider operation, since the result would be the same. */
2241 sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
2242 rtx op0, rtx op1, rtx target, int unsignedp,
2243 enum optab_methods methods)
2245 rtx temp;
2246 optab direct_optab = unsignedp ? uoptab : soptab;
2247 bool save_enable;
2249 /* Do it without widening, if possible. */
2250 temp = expand_binop (mode, direct_optab, op0, op1, target,
2251 unsignedp, OPTAB_DIRECT);
2252 if (temp || methods == OPTAB_DIRECT)
2253 return temp;
2255 /* Try widening to a signed int. Disable any direct use of any
2256 signed insn in the current mode. */
2257 save_enable = swap_optab_enable (soptab, mode, false);
2259 temp = expand_binop (mode, soptab, op0, op1, target,
2260 unsignedp, OPTAB_WIDEN);
2262 /* For unsigned operands, try widening to an unsigned int. */
2263 if (!temp && unsignedp)
2264 temp = expand_binop (mode, uoptab, op0, op1, target,
2265 unsignedp, OPTAB_WIDEN);
2266 if (temp || methods == OPTAB_WIDEN)
2267 goto egress;
2269 /* Use the right width libcall if that exists. */
2270 temp = expand_binop (mode, direct_optab, op0, op1, target,
2271 unsignedp, OPTAB_LIB);
2272 if (temp || methods == OPTAB_LIB)
2273 goto egress;
2275 /* Must widen and use a libcall, use either signed or unsigned. */
2276 temp = expand_binop (mode, soptab, op0, op1, target,
2277 unsignedp, methods);
2278 if (!temp && unsignedp)
2279 temp = expand_binop (mode, uoptab, op0, op1, target,
2280 unsignedp, methods);
2282 egress:
2283 /* Undo the fiddling above. */
2284 if (save_enable)
2285 swap_optab_enable (soptab, mode, true);
2286 return temp;
2289 /* Generate code to perform an operation specified by UNOPPTAB
2290 on operand OP0, with two results to TARG0 and TARG1.
2291 We assume that the order of the operands for the instruction
2292 is TARG0, TARG1, OP0.
2294 Either TARG0 or TARG1 may be zero, but what that means is that
2295 the result is not actually wanted. We will generate it into
2296 a dummy pseudo-reg and discard it. They may not both be zero.
2298 Returns 1 if this operation can be performed; 0 if not. */
2301 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2302 int unsignedp)
2304 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2305 enum mode_class mclass;
2306 machine_mode wider_mode;
2307 rtx_insn *entry_last = get_last_insn ();
2308 rtx_insn *last;
2310 mclass = GET_MODE_CLASS (mode);
2312 if (!targ0)
2313 targ0 = gen_reg_rtx (mode);
2314 if (!targ1)
2315 targ1 = gen_reg_rtx (mode);
2317 /* Record where to go back to if we fail. */
2318 last = get_last_insn ();
2320 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2322 struct expand_operand ops[3];
2323 enum insn_code icode = optab_handler (unoptab, mode);
2325 create_fixed_operand (&ops[0], targ0);
2326 create_fixed_operand (&ops[1], targ1);
2327 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2328 if (maybe_expand_insn (icode, 3, ops))
2329 return 1;
2332 /* It can't be done in this mode. Can we do it in a wider mode? */
2334 if (CLASS_HAS_WIDER_MODES_P (mclass))
2336 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2337 wider_mode != VOIDmode;
2338 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2340 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2342 rtx t0 = gen_reg_rtx (wider_mode);
2343 rtx t1 = gen_reg_rtx (wider_mode);
2344 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2346 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2348 convert_move (targ0, t0, unsignedp);
2349 convert_move (targ1, t1, unsignedp);
2350 return 1;
2352 else
2353 delete_insns_since (last);
2358 delete_insns_since (entry_last);
2359 return 0;
2362 /* Generate code to perform an operation specified by BINOPTAB
2363 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2364 We assume that the order of the operands for the instruction
2365 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2366 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2368 Either TARG0 or TARG1 may be zero, but what that means is that
2369 the result is not actually wanted. We will generate it into
2370 a dummy pseudo-reg and discard it. They may not both be zero.
2372 Returns 1 if this operation can be performed; 0 if not. */
2375 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2376 int unsignedp)
2378 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2379 enum mode_class mclass;
2380 machine_mode wider_mode;
2381 rtx_insn *entry_last = get_last_insn ();
2382 rtx_insn *last;
2384 mclass = GET_MODE_CLASS (mode);
2386 if (!targ0)
2387 targ0 = gen_reg_rtx (mode);
2388 if (!targ1)
2389 targ1 = gen_reg_rtx (mode);
2391 /* Record where to go back to if we fail. */
2392 last = get_last_insn ();
2394 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2396 struct expand_operand ops[4];
2397 enum insn_code icode = optab_handler (binoptab, mode);
2398 machine_mode mode0 = insn_data[icode].operand[1].mode;
2399 machine_mode mode1 = insn_data[icode].operand[2].mode;
2400 rtx xop0 = op0, xop1 = op1;
2402 /* If we are optimizing, force expensive constants into a register. */
2403 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2404 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2406 create_fixed_operand (&ops[0], targ0);
2407 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2408 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2409 create_fixed_operand (&ops[3], targ1);
2410 if (maybe_expand_insn (icode, 4, ops))
2411 return 1;
2412 delete_insns_since (last);
2415 /* It can't be done in this mode. Can we do it in a wider mode? */
2417 if (CLASS_HAS_WIDER_MODES_P (mclass))
2419 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2420 wider_mode != VOIDmode;
2421 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2423 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2425 rtx t0 = gen_reg_rtx (wider_mode);
2426 rtx t1 = gen_reg_rtx (wider_mode);
2427 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2428 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2430 if (expand_twoval_binop (binoptab, cop0, cop1,
2431 t0, t1, unsignedp))
2433 convert_move (targ0, t0, unsignedp);
2434 convert_move (targ1, t1, unsignedp);
2435 return 1;
2437 else
2438 delete_insns_since (last);
2443 delete_insns_since (entry_last);
2444 return 0;
2447 /* Expand the two-valued library call indicated by BINOPTAB, but
2448 preserve only one of the values. If TARG0 is non-NULL, the first
2449 value is placed into TARG0; otherwise the second value is placed
2450 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2451 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2452 This routine assumes that the value returned by the library call is
2453 as if the return value was of an integral mode twice as wide as the
2454 mode of OP0. Returns 1 if the call was successful. */
2456 bool
2457 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2458 rtx targ0, rtx targ1, enum rtx_code code)
2460 machine_mode mode;
2461 machine_mode libval_mode;
2462 rtx libval;
2463 rtx_insn *insns;
2464 rtx libfunc;
2466 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2467 gcc_assert (!targ0 != !targ1);
2469 mode = GET_MODE (op0);
2470 libfunc = optab_libfunc (binoptab, mode);
2471 if (!libfunc)
2472 return false;
2474 /* The value returned by the library function will have twice as
2475 many bits as the nominal MODE. */
2476 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2477 MODE_INT);
2478 start_sequence ();
2479 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2480 libval_mode, 2,
2481 op0, mode,
2482 op1, mode);
2483 /* Get the part of VAL containing the value that we want. */
2484 libval = simplify_gen_subreg (mode, libval, libval_mode,
2485 targ0 ? 0 : GET_MODE_SIZE (mode));
2486 insns = get_insns ();
2487 end_sequence ();
2488 /* Move the into the desired location. */
2489 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2490 gen_rtx_fmt_ee (code, mode, op0, op1));
2492 return true;
2496 /* Wrapper around expand_unop which takes an rtx code to specify
2497 the operation to perform, not an optab pointer. All other
2498 arguments are the same. */
2500 expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
2501 rtx target, int unsignedp)
2503 optab unop = code_to_optab (code);
2504 gcc_assert (unop);
2506 return expand_unop (mode, unop, op0, target, unsignedp);
2509 /* Try calculating
2510 (clz:narrow x)
2512 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2514 A similar operation can be used for clrsb. UNOPTAB says which operation
2515 we are trying to expand. */
2516 static rtx
2517 widen_leading (machine_mode mode, rtx op0, rtx target, optab unoptab)
2519 enum mode_class mclass = GET_MODE_CLASS (mode);
2520 if (CLASS_HAS_WIDER_MODES_P (mclass))
2522 machine_mode wider_mode;
2523 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2524 wider_mode != VOIDmode;
2525 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2527 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2529 rtx xop0, temp;
2530 rtx_insn *last;
2532 last = get_last_insn ();
2534 if (target == 0)
2535 target = gen_reg_rtx (mode);
2536 xop0 = widen_operand (op0, wider_mode, mode,
2537 unoptab != clrsb_optab, false);
2538 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2539 unoptab != clrsb_optab);
2540 if (temp != 0)
2541 temp = expand_binop
2542 (wider_mode, sub_optab, temp,
2543 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2544 - GET_MODE_PRECISION (mode),
2545 wider_mode),
2546 target, true, OPTAB_DIRECT);
2547 if (temp == 0)
2548 delete_insns_since (last);
2550 return temp;
2554 return 0;
2557 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2558 quantities, choosing which based on whether the high word is nonzero. */
2559 static rtx
2560 expand_doubleword_clz (machine_mode mode, rtx op0, rtx target)
2562 rtx xop0 = force_reg (mode, op0);
2563 rtx subhi = gen_highpart (word_mode, xop0);
2564 rtx sublo = gen_lowpart (word_mode, xop0);
2565 rtx_code_label *hi0_label = gen_label_rtx ();
2566 rtx_code_label *after_label = gen_label_rtx ();
2567 rtx_insn *seq;
2568 rtx temp, result;
2570 /* If we were not given a target, use a word_mode register, not a
2571 'mode' register. The result will fit, and nobody is expecting
2572 anything bigger (the return type of __builtin_clz* is int). */
2573 if (!target)
2574 target = gen_reg_rtx (word_mode);
2576 /* In any case, write to a word_mode scratch in both branches of the
2577 conditional, so we can ensure there is a single move insn setting
2578 'target' to tag a REG_EQUAL note on. */
2579 result = gen_reg_rtx (word_mode);
2581 start_sequence ();
2583 /* If the high word is not equal to zero,
2584 then clz of the full value is clz of the high word. */
2585 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2586 word_mode, true, hi0_label);
2588 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2589 if (!temp)
2590 goto fail;
2592 if (temp != result)
2593 convert_move (result, temp, true);
2595 emit_jump_insn (targetm.gen_jump (after_label));
2596 emit_barrier ();
2598 /* Else clz of the full value is clz of the low word plus the number
2599 of bits in the high word. */
2600 emit_label (hi0_label);
2602 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2603 if (!temp)
2604 goto fail;
2605 temp = expand_binop (word_mode, add_optab, temp,
2606 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2607 result, true, OPTAB_DIRECT);
2608 if (!temp)
2609 goto fail;
2610 if (temp != result)
2611 convert_move (result, temp, true);
2613 emit_label (after_label);
2614 convert_move (target, result, true);
2616 seq = get_insns ();
2617 end_sequence ();
2619 add_equal_note (seq, target, CLZ, xop0, 0);
2620 emit_insn (seq);
2621 return target;
2623 fail:
2624 end_sequence ();
2625 return 0;
2628 /* Try calculating
2629 (bswap:narrow x)
2631 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2632 static rtx
2633 widen_bswap (machine_mode mode, rtx op0, rtx target)
2635 enum mode_class mclass = GET_MODE_CLASS (mode);
2636 machine_mode wider_mode;
2637 rtx x;
2638 rtx_insn *last;
2640 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2641 return NULL_RTX;
2643 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2644 wider_mode != VOIDmode;
2645 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2646 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2647 goto found;
2648 return NULL_RTX;
2650 found:
2651 last = get_last_insn ();
2653 x = widen_operand (op0, wider_mode, mode, true, true);
2654 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2656 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2657 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2658 if (x != 0)
2659 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2660 GET_MODE_BITSIZE (wider_mode)
2661 - GET_MODE_BITSIZE (mode),
2662 NULL_RTX, true);
2664 if (x != 0)
2666 if (target == 0)
2667 target = gen_reg_rtx (mode);
2668 emit_move_insn (target, gen_lowpart (mode, x));
2670 else
2671 delete_insns_since (last);
2673 return target;
2676 /* Try calculating bswap as two bswaps of two word-sized operands. */
2678 static rtx
2679 expand_doubleword_bswap (machine_mode mode, rtx op, rtx target)
2681 rtx t0, t1;
2683 t1 = expand_unop (word_mode, bswap_optab,
2684 operand_subword_force (op, 0, mode), NULL_RTX, true);
2685 t0 = expand_unop (word_mode, bswap_optab,
2686 operand_subword_force (op, 1, mode), NULL_RTX, true);
2688 if (target == 0 || !valid_multiword_target_p (target))
2689 target = gen_reg_rtx (mode);
2690 if (REG_P (target))
2691 emit_clobber (target);
2692 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2693 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2695 return target;
2698 /* Try calculating (parity x) as (and (popcount x) 1), where
2699 popcount can also be done in a wider mode. */
2700 static rtx
2701 expand_parity (machine_mode mode, rtx op0, rtx target)
2703 enum mode_class mclass = GET_MODE_CLASS (mode);
2704 if (CLASS_HAS_WIDER_MODES_P (mclass))
2706 machine_mode wider_mode;
2707 for (wider_mode = mode; wider_mode != VOIDmode;
2708 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2710 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2712 rtx xop0, temp;
2713 rtx_insn *last;
2715 last = get_last_insn ();
2717 if (target == 0)
2718 target = gen_reg_rtx (mode);
2719 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2720 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2721 true);
2722 if (temp != 0)
2723 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2724 target, true, OPTAB_DIRECT);
2725 if (temp == 0)
2726 delete_insns_since (last);
2728 return temp;
2732 return 0;
2735 /* Try calculating ctz(x) as K - clz(x & -x) ,
2736 where K is GET_MODE_PRECISION(mode) - 1.
2738 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2739 don't have to worry about what the hardware does in that case. (If
2740 the clz instruction produces the usual value at 0, which is K, the
2741 result of this code sequence will be -1; expand_ffs, below, relies
2742 on this. It might be nice to have it be K instead, for consistency
2743 with the (very few) processors that provide a ctz with a defined
2744 value, but that would take one more instruction, and it would be
2745 less convenient for expand_ffs anyway. */
2747 static rtx
2748 expand_ctz (machine_mode mode, rtx op0, rtx target)
2750 rtx_insn *seq;
2751 rtx temp;
2753 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2754 return 0;
2756 start_sequence ();
2758 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2759 if (temp)
2760 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2761 true, OPTAB_DIRECT);
2762 if (temp)
2763 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2764 if (temp)
2765 temp = expand_binop (mode, sub_optab,
2766 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2767 temp, target,
2768 true, OPTAB_DIRECT);
2769 if (temp == 0)
2771 end_sequence ();
2772 return 0;
2775 seq = get_insns ();
2776 end_sequence ();
2778 add_equal_note (seq, temp, CTZ, op0, 0);
2779 emit_insn (seq);
2780 return temp;
2784 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2785 else with the sequence used by expand_clz.
2787 The ffs builtin promises to return zero for a zero value and ctz/clz
2788 may have an undefined value in that case. If they do not give us a
2789 convenient value, we have to generate a test and branch. */
2790 static rtx
2791 expand_ffs (machine_mode mode, rtx op0, rtx target)
2793 HOST_WIDE_INT val = 0;
2794 bool defined_at_zero = false;
2795 rtx temp;
2796 rtx_insn *seq;
2798 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2800 start_sequence ();
2802 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2803 if (!temp)
2804 goto fail;
2806 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2808 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2810 start_sequence ();
2811 temp = expand_ctz (mode, op0, 0);
2812 if (!temp)
2813 goto fail;
2815 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2817 defined_at_zero = true;
2818 val = (GET_MODE_PRECISION (mode) - 1) - val;
2821 else
2822 return 0;
2824 if (defined_at_zero && val == -1)
2825 /* No correction needed at zero. */;
2826 else
2828 /* We don't try to do anything clever with the situation found
2829 on some processors (eg Alpha) where ctz(0:mode) ==
2830 bitsize(mode). If someone can think of a way to send N to -1
2831 and leave alone all values in the range 0..N-1 (where N is a
2832 power of two), cheaper than this test-and-branch, please add it.
2834 The test-and-branch is done after the operation itself, in case
2835 the operation sets condition codes that can be recycled for this.
2836 (This is true on i386, for instance.) */
2838 rtx_code_label *nonzero_label = gen_label_rtx ();
2839 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2840 mode, true, nonzero_label);
2842 convert_move (temp, GEN_INT (-1), false);
2843 emit_label (nonzero_label);
2846 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2847 to produce a value in the range 0..bitsize. */
2848 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2849 target, false, OPTAB_DIRECT);
2850 if (!temp)
2851 goto fail;
2853 seq = get_insns ();
2854 end_sequence ();
2856 add_equal_note (seq, temp, FFS, op0, 0);
2857 emit_insn (seq);
2858 return temp;
2860 fail:
2861 end_sequence ();
2862 return 0;
2865 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2866 conditions, VAL may already be a SUBREG against which we cannot generate
2867 a further SUBREG. In this case, we expect forcing the value into a
2868 register will work around the situation. */
2870 static rtx
2871 lowpart_subreg_maybe_copy (machine_mode omode, rtx val,
2872 machine_mode imode)
2874 rtx ret;
2875 ret = lowpart_subreg (omode, val, imode);
2876 if (ret == NULL)
2878 val = force_reg (imode, val);
2879 ret = lowpart_subreg (omode, val, imode);
2880 gcc_assert (ret != NULL);
2882 return ret;
2885 /* Expand a floating point absolute value or negation operation via a
2886 logical operation on the sign bit. */
2888 static rtx
2889 expand_absneg_bit (enum rtx_code code, machine_mode mode,
2890 rtx op0, rtx target)
2892 const struct real_format *fmt;
2893 int bitpos, word, nwords, i;
2894 machine_mode imode;
2895 rtx temp;
2896 rtx_insn *insns;
2898 /* The format has to have a simple sign bit. */
2899 fmt = REAL_MODE_FORMAT (mode);
2900 if (fmt == NULL)
2901 return NULL_RTX;
2903 bitpos = fmt->signbit_rw;
2904 if (bitpos < 0)
2905 return NULL_RTX;
2907 /* Don't create negative zeros if the format doesn't support them. */
2908 if (code == NEG && !fmt->has_signed_zero)
2909 return NULL_RTX;
2911 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2913 imode = int_mode_for_mode (mode);
2914 if (imode == BLKmode)
2915 return NULL_RTX;
2916 word = 0;
2917 nwords = 1;
2919 else
2921 imode = word_mode;
2923 if (FLOAT_WORDS_BIG_ENDIAN)
2924 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2925 else
2926 word = bitpos / BITS_PER_WORD;
2927 bitpos = bitpos % BITS_PER_WORD;
2928 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2931 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
2932 if (code == ABS)
2933 mask = ~mask;
2935 if (target == 0
2936 || target == op0
2937 || (nwords > 1 && !valid_multiword_target_p (target)))
2938 target = gen_reg_rtx (mode);
2940 if (nwords > 1)
2942 start_sequence ();
2944 for (i = 0; i < nwords; ++i)
2946 rtx targ_piece = operand_subword (target, i, 1, mode);
2947 rtx op0_piece = operand_subword_force (op0, i, mode);
2949 if (i == word)
2951 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2952 op0_piece,
2953 immed_wide_int_const (mask, imode),
2954 targ_piece, 1, OPTAB_LIB_WIDEN);
2955 if (temp != targ_piece)
2956 emit_move_insn (targ_piece, temp);
2958 else
2959 emit_move_insn (targ_piece, op0_piece);
2962 insns = get_insns ();
2963 end_sequence ();
2965 emit_insn (insns);
2967 else
2969 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2970 gen_lowpart (imode, op0),
2971 immed_wide_int_const (mask, imode),
2972 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2973 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2975 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2976 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2977 target);
2980 return target;
2983 /* As expand_unop, but will fail rather than attempt the operation in a
2984 different mode or with a libcall. */
2985 static rtx
2986 expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
2987 int unsignedp)
2989 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2991 struct expand_operand ops[2];
2992 enum insn_code icode = optab_handler (unoptab, mode);
2993 rtx_insn *last = get_last_insn ();
2994 rtx_insn *pat;
2996 create_output_operand (&ops[0], target, mode);
2997 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2998 pat = maybe_gen_insn (icode, 2, ops);
2999 if (pat)
3001 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3002 && ! add_equal_note (pat, ops[0].value,
3003 optab_to_code (unoptab),
3004 ops[1].value, NULL_RTX))
3006 delete_insns_since (last);
3007 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3010 emit_insn (pat);
3012 return ops[0].value;
3015 return 0;
3018 /* Generate code to perform an operation specified by UNOPTAB
3019 on operand OP0, with result having machine-mode MODE.
3021 UNSIGNEDP is for the case where we have to widen the operands
3022 to perform the operation. It says to use zero-extension.
3024 If TARGET is nonzero, the value
3025 is generated there, if it is convenient to do so.
3026 In all cases an rtx is returned for the locus of the value;
3027 this may or may not be TARGET. */
3030 expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
3031 int unsignedp)
3033 enum mode_class mclass = GET_MODE_CLASS (mode);
3034 machine_mode wider_mode;
3035 rtx temp;
3036 rtx libfunc;
3038 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3039 if (temp)
3040 return temp;
3042 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3044 /* Widening (or narrowing) clz needs special treatment. */
3045 if (unoptab == clz_optab)
3047 temp = widen_leading (mode, op0, target, unoptab);
3048 if (temp)
3049 return temp;
3051 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3052 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3054 temp = expand_doubleword_clz (mode, op0, target);
3055 if (temp)
3056 return temp;
3059 goto try_libcall;
3062 if (unoptab == clrsb_optab)
3064 temp = widen_leading (mode, op0, target, unoptab);
3065 if (temp)
3066 return temp;
3067 goto try_libcall;
3070 /* Widening (or narrowing) bswap needs special treatment. */
3071 if (unoptab == bswap_optab)
3073 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3074 or ROTATERT. First try these directly; if this fails, then try the
3075 obvious pair of shifts with allowed widening, as this will probably
3076 be always more efficient than the other fallback methods. */
3077 if (mode == HImode)
3079 rtx_insn *last;
3080 rtx temp1, temp2;
3082 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3084 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
3085 unsignedp, OPTAB_DIRECT);
3086 if (temp)
3087 return temp;
3090 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3092 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
3093 unsignedp, OPTAB_DIRECT);
3094 if (temp)
3095 return temp;
3098 last = get_last_insn ();
3100 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
3101 unsignedp, OPTAB_WIDEN);
3102 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
3103 unsignedp, OPTAB_WIDEN);
3104 if (temp1 && temp2)
3106 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3107 unsignedp, OPTAB_WIDEN);
3108 if (temp)
3109 return temp;
3112 delete_insns_since (last);
3115 temp = widen_bswap (mode, op0, target);
3116 if (temp)
3117 return temp;
3119 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3120 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3122 temp = expand_doubleword_bswap (mode, op0, target);
3123 if (temp)
3124 return temp;
3127 goto try_libcall;
3130 if (CLASS_HAS_WIDER_MODES_P (mclass))
3131 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3132 wider_mode != VOIDmode;
3133 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3135 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3137 rtx xop0 = op0;
3138 rtx_insn *last = get_last_insn ();
3140 /* For certain operations, we need not actually extend
3141 the narrow operand, as long as we will truncate the
3142 results to the same narrowness. */
3144 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3145 (unoptab == neg_optab
3146 || unoptab == one_cmpl_optab)
3147 && mclass == MODE_INT);
3149 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3150 unsignedp);
3152 if (temp)
3154 if (mclass != MODE_INT
3155 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3157 if (target == 0)
3158 target = gen_reg_rtx (mode);
3159 convert_move (target, temp, 0);
3160 return target;
3162 else
3163 return gen_lowpart (mode, temp);
3165 else
3166 delete_insns_since (last);
3170 /* These can be done a word at a time. */
3171 if (unoptab == one_cmpl_optab
3172 && mclass == MODE_INT
3173 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3174 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3176 int i;
3177 rtx_insn *insns;
3179 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3180 target = gen_reg_rtx (mode);
3182 start_sequence ();
3184 /* Do the actual arithmetic. */
3185 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3187 rtx target_piece = operand_subword (target, i, 1, mode);
3188 rtx x = expand_unop (word_mode, unoptab,
3189 operand_subword_force (op0, i, mode),
3190 target_piece, unsignedp);
3192 if (target_piece != x)
3193 emit_move_insn (target_piece, x);
3196 insns = get_insns ();
3197 end_sequence ();
3199 emit_insn (insns);
3200 return target;
3203 if (optab_to_code (unoptab) == NEG)
3205 /* Try negating floating point values by flipping the sign bit. */
3206 if (SCALAR_FLOAT_MODE_P (mode))
3208 temp = expand_absneg_bit (NEG, mode, op0, target);
3209 if (temp)
3210 return temp;
3213 /* If there is no negation pattern, and we have no negative zero,
3214 try subtracting from zero. */
3215 if (!HONOR_SIGNED_ZEROS (mode))
3217 temp = expand_binop (mode, (unoptab == negv_optab
3218 ? subv_optab : sub_optab),
3219 CONST0_RTX (mode), op0, target,
3220 unsignedp, OPTAB_DIRECT);
3221 if (temp)
3222 return temp;
3226 /* Try calculating parity (x) as popcount (x) % 2. */
3227 if (unoptab == parity_optab)
3229 temp = expand_parity (mode, op0, target);
3230 if (temp)
3231 return temp;
3234 /* Try implementing ffs (x) in terms of clz (x). */
3235 if (unoptab == ffs_optab)
3237 temp = expand_ffs (mode, op0, target);
3238 if (temp)
3239 return temp;
3242 /* Try implementing ctz (x) in terms of clz (x). */
3243 if (unoptab == ctz_optab)
3245 temp = expand_ctz (mode, op0, target);
3246 if (temp)
3247 return temp;
3250 try_libcall:
3251 /* Now try a library call in this mode. */
3252 libfunc = optab_libfunc (unoptab, mode);
3253 if (libfunc)
3255 rtx_insn *insns;
3256 rtx value;
3257 rtx eq_value;
3258 machine_mode outmode = mode;
3260 /* All of these functions return small values. Thus we choose to
3261 have them return something that isn't a double-word. */
3262 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3263 || unoptab == clrsb_optab || unoptab == popcount_optab
3264 || unoptab == parity_optab)
3265 outmode
3266 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3267 optab_libfunc (unoptab, mode)));
3269 start_sequence ();
3271 /* Pass 1 for NO_QUEUE so we don't lose any increments
3272 if the libcall is cse'd or moved. */
3273 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3274 1, op0, mode);
3275 insns = get_insns ();
3276 end_sequence ();
3278 target = gen_reg_rtx (outmode);
3279 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3280 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3281 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3282 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3283 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3284 emit_libcall_block_1 (insns, target, value, eq_value,
3285 trapv_unoptab_p (unoptab));
3287 return target;
3290 /* It can't be done in this mode. Can we do it in a wider mode? */
3292 if (CLASS_HAS_WIDER_MODES_P (mclass))
3294 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3295 wider_mode != VOIDmode;
3296 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3298 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3299 || optab_libfunc (unoptab, wider_mode))
3301 rtx xop0 = op0;
3302 rtx_insn *last = get_last_insn ();
3304 /* For certain operations, we need not actually extend
3305 the narrow operand, as long as we will truncate the
3306 results to the same narrowness. */
3307 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3308 (unoptab == neg_optab
3309 || unoptab == one_cmpl_optab
3310 || unoptab == bswap_optab)
3311 && mclass == MODE_INT);
3313 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3314 unsignedp);
3316 /* If we are generating clz using wider mode, adjust the
3317 result. Similarly for clrsb. */
3318 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3319 && temp != 0)
3320 temp = expand_binop
3321 (wider_mode, sub_optab, temp,
3322 gen_int_mode (GET_MODE_PRECISION (wider_mode)
3323 - GET_MODE_PRECISION (mode),
3324 wider_mode),
3325 target, true, OPTAB_DIRECT);
3327 /* Likewise for bswap. */
3328 if (unoptab == bswap_optab && temp != 0)
3330 gcc_assert (GET_MODE_PRECISION (wider_mode)
3331 == GET_MODE_BITSIZE (wider_mode)
3332 && GET_MODE_PRECISION (mode)
3333 == GET_MODE_BITSIZE (mode));
3335 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3336 GET_MODE_BITSIZE (wider_mode)
3337 - GET_MODE_BITSIZE (mode),
3338 NULL_RTX, true);
3341 if (temp)
3343 if (mclass != MODE_INT)
3345 if (target == 0)
3346 target = gen_reg_rtx (mode);
3347 convert_move (target, temp, 0);
3348 return target;
3350 else
3351 return gen_lowpart (mode, temp);
3353 else
3354 delete_insns_since (last);
3359 /* One final attempt at implementing negation via subtraction,
3360 this time allowing widening of the operand. */
3361 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3363 rtx temp;
3364 temp = expand_binop (mode,
3365 unoptab == negv_optab ? subv_optab : sub_optab,
3366 CONST0_RTX (mode), op0,
3367 target, unsignedp, OPTAB_LIB_WIDEN);
3368 if (temp)
3369 return temp;
3372 return 0;
3375 /* Emit code to compute the absolute value of OP0, with result to
3376 TARGET if convenient. (TARGET may be 0.) The return value says
3377 where the result actually is to be found.
3379 MODE is the mode of the operand; the mode of the result is
3380 different but can be deduced from MODE.
3385 expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
3386 int result_unsignedp)
3388 rtx temp;
3390 if (GET_MODE_CLASS (mode) != MODE_INT
3391 || ! flag_trapv)
3392 result_unsignedp = 1;
3394 /* First try to do it with a special abs instruction. */
3395 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3396 op0, target, 0);
3397 if (temp != 0)
3398 return temp;
3400 /* For floating point modes, try clearing the sign bit. */
3401 if (SCALAR_FLOAT_MODE_P (mode))
3403 temp = expand_absneg_bit (ABS, mode, op0, target);
3404 if (temp)
3405 return temp;
3408 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3409 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3410 && !HONOR_SIGNED_ZEROS (mode))
3412 rtx_insn *last = get_last_insn ();
3414 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3415 op0, NULL_RTX, 0);
3416 if (temp != 0)
3417 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3418 OPTAB_WIDEN);
3420 if (temp != 0)
3421 return temp;
3423 delete_insns_since (last);
3426 /* If this machine has expensive jumps, we can do integer absolute
3427 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3428 where W is the width of MODE. */
3430 if (GET_MODE_CLASS (mode) == MODE_INT
3431 && BRANCH_COST (optimize_insn_for_speed_p (),
3432 false) >= 2)
3434 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3435 GET_MODE_PRECISION (mode) - 1,
3436 NULL_RTX, 0);
3438 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3439 OPTAB_LIB_WIDEN);
3440 if (temp != 0)
3441 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3442 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3444 if (temp != 0)
3445 return temp;
3448 return NULL_RTX;
3452 expand_abs (machine_mode mode, rtx op0, rtx target,
3453 int result_unsignedp, int safe)
3455 rtx temp;
3456 rtx_code_label *op1;
3458 if (GET_MODE_CLASS (mode) != MODE_INT
3459 || ! flag_trapv)
3460 result_unsignedp = 1;
3462 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3463 if (temp != 0)
3464 return temp;
3466 /* If that does not win, use conditional jump and negate. */
3468 /* It is safe to use the target if it is the same
3469 as the source if this is also a pseudo register */
3470 if (op0 == target && REG_P (op0)
3471 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3472 safe = 1;
3474 op1 = gen_label_rtx ();
3475 if (target == 0 || ! safe
3476 || GET_MODE (target) != mode
3477 || (MEM_P (target) && MEM_VOLATILE_P (target))
3478 || (REG_P (target)
3479 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3480 target = gen_reg_rtx (mode);
3482 emit_move_insn (target, op0);
3483 NO_DEFER_POP;
3485 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3486 NULL_RTX, NULL, op1, -1);
3488 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3489 target, target, 0);
3490 if (op0 != target)
3491 emit_move_insn (target, op0);
3492 emit_label (op1);
3493 OK_DEFER_POP;
3494 return target;
3497 /* Emit code to compute the one's complement absolute value of OP0
3498 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3499 (TARGET may be NULL_RTX.) The return value says where the result
3500 actually is to be found.
3502 MODE is the mode of the operand; the mode of the result is
3503 different but can be deduced from MODE. */
3506 expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
3508 rtx temp;
3510 /* Not applicable for floating point modes. */
3511 if (FLOAT_MODE_P (mode))
3512 return NULL_RTX;
3514 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3515 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3517 rtx_insn *last = get_last_insn ();
3519 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3520 if (temp != 0)
3521 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3522 OPTAB_WIDEN);
3524 if (temp != 0)
3525 return temp;
3527 delete_insns_since (last);
3530 /* If this machine has expensive jumps, we can do one's complement
3531 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3533 if (GET_MODE_CLASS (mode) == MODE_INT
3534 && BRANCH_COST (optimize_insn_for_speed_p (),
3535 false) >= 2)
3537 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3538 GET_MODE_PRECISION (mode) - 1,
3539 NULL_RTX, 0);
3541 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3542 OPTAB_LIB_WIDEN);
3544 if (temp != 0)
3545 return temp;
3548 return NULL_RTX;
3551 /* A subroutine of expand_copysign, perform the copysign operation using the
3552 abs and neg primitives advertised to exist on the target. The assumption
3553 is that we have a split register file, and leaving op0 in fp registers,
3554 and not playing with subregs so much, will help the register allocator. */
3556 static rtx
3557 expand_copysign_absneg (machine_mode mode, rtx op0, rtx op1, rtx target,
3558 int bitpos, bool op0_is_abs)
3560 machine_mode imode;
3561 enum insn_code icode;
3562 rtx sign;
3563 rtx_code_label *label;
3565 if (target == op1)
3566 target = NULL_RTX;
3568 /* Check if the back end provides an insn that handles signbit for the
3569 argument's mode. */
3570 icode = optab_handler (signbit_optab, mode);
3571 if (icode != CODE_FOR_nothing)
3573 imode = insn_data[(int) icode].operand[0].mode;
3574 sign = gen_reg_rtx (imode);
3575 emit_unop_insn (icode, sign, op1, UNKNOWN);
3577 else
3579 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3581 imode = int_mode_for_mode (mode);
3582 if (imode == BLKmode)
3583 return NULL_RTX;
3584 op1 = gen_lowpart (imode, op1);
3586 else
3588 int word;
3590 imode = word_mode;
3591 if (FLOAT_WORDS_BIG_ENDIAN)
3592 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3593 else
3594 word = bitpos / BITS_PER_WORD;
3595 bitpos = bitpos % BITS_PER_WORD;
3596 op1 = operand_subword_force (op1, word, mode);
3599 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3600 sign = expand_binop (imode, and_optab, op1,
3601 immed_wide_int_const (mask, imode),
3602 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3605 if (!op0_is_abs)
3607 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3608 if (op0 == NULL)
3609 return NULL_RTX;
3610 target = op0;
3612 else
3614 if (target == NULL_RTX)
3615 target = copy_to_reg (op0);
3616 else
3617 emit_move_insn (target, op0);
3620 label = gen_label_rtx ();
3621 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3623 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3624 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3625 else
3626 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3627 if (op0 != target)
3628 emit_move_insn (target, op0);
3630 emit_label (label);
3632 return target;
3636 /* A subroutine of expand_copysign, perform the entire copysign operation
3637 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3638 is true if op0 is known to have its sign bit clear. */
3640 static rtx
3641 expand_copysign_bit (machine_mode mode, rtx op0, rtx op1, rtx target,
3642 int bitpos, bool op0_is_abs)
3644 machine_mode imode;
3645 int word, nwords, i;
3646 rtx temp;
3647 rtx_insn *insns;
3649 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3651 imode = int_mode_for_mode (mode);
3652 if (imode == BLKmode)
3653 return NULL_RTX;
3654 word = 0;
3655 nwords = 1;
3657 else
3659 imode = word_mode;
3661 if (FLOAT_WORDS_BIG_ENDIAN)
3662 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3663 else
3664 word = bitpos / BITS_PER_WORD;
3665 bitpos = bitpos % BITS_PER_WORD;
3666 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3669 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3671 if (target == 0
3672 || target == op0
3673 || target == op1
3674 || (nwords > 1 && !valid_multiword_target_p (target)))
3675 target = gen_reg_rtx (mode);
3677 if (nwords > 1)
3679 start_sequence ();
3681 for (i = 0; i < nwords; ++i)
3683 rtx targ_piece = operand_subword (target, i, 1, mode);
3684 rtx op0_piece = operand_subword_force (op0, i, mode);
3686 if (i == word)
3688 if (!op0_is_abs)
3689 op0_piece
3690 = expand_binop (imode, and_optab, op0_piece,
3691 immed_wide_int_const (~mask, imode),
3692 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3693 op1 = expand_binop (imode, and_optab,
3694 operand_subword_force (op1, i, mode),
3695 immed_wide_int_const (mask, imode),
3696 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3698 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3699 targ_piece, 1, OPTAB_LIB_WIDEN);
3700 if (temp != targ_piece)
3701 emit_move_insn (targ_piece, temp);
3703 else
3704 emit_move_insn (targ_piece, op0_piece);
3707 insns = get_insns ();
3708 end_sequence ();
3710 emit_insn (insns);
3712 else
3714 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3715 immed_wide_int_const (mask, imode),
3716 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3718 op0 = gen_lowpart (imode, op0);
3719 if (!op0_is_abs)
3720 op0 = expand_binop (imode, and_optab, op0,
3721 immed_wide_int_const (~mask, imode),
3722 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3724 temp = expand_binop (imode, ior_optab, op0, op1,
3725 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3726 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3729 return target;
3732 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3733 scalar floating point mode. Return NULL if we do not know how to
3734 expand the operation inline. */
3737 expand_copysign (rtx op0, rtx op1, rtx target)
3739 machine_mode mode = GET_MODE (op0);
3740 const struct real_format *fmt;
3741 bool op0_is_abs;
3742 rtx temp;
3744 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3745 gcc_assert (GET_MODE (op1) == mode);
3747 /* First try to do it with a special instruction. */
3748 temp = expand_binop (mode, copysign_optab, op0, op1,
3749 target, 0, OPTAB_DIRECT);
3750 if (temp)
3751 return temp;
3753 fmt = REAL_MODE_FORMAT (mode);
3754 if (fmt == NULL || !fmt->has_signed_zero)
3755 return NULL_RTX;
3757 op0_is_abs = false;
3758 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3760 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3761 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3762 op0_is_abs = true;
3765 if (fmt->signbit_ro >= 0
3766 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3767 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3768 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3770 temp = expand_copysign_absneg (mode, op0, op1, target,
3771 fmt->signbit_ro, op0_is_abs);
3772 if (temp)
3773 return temp;
3776 if (fmt->signbit_rw < 0)
3777 return NULL_RTX;
3778 return expand_copysign_bit (mode, op0, op1, target,
3779 fmt->signbit_rw, op0_is_abs);
3782 /* Generate an instruction whose insn-code is INSN_CODE,
3783 with two operands: an output TARGET and an input OP0.
3784 TARGET *must* be nonzero, and the output is always stored there.
3785 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3786 the value that is stored into TARGET.
3788 Return false if expansion failed. */
3790 bool
3791 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3792 enum rtx_code code)
3794 struct expand_operand ops[2];
3795 rtx_insn *pat;
3797 create_output_operand (&ops[0], target, GET_MODE (target));
3798 create_input_operand (&ops[1], op0, GET_MODE (op0));
3799 pat = maybe_gen_insn (icode, 2, ops);
3800 if (!pat)
3801 return false;
3803 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3804 && code != UNKNOWN)
3805 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3807 emit_insn (pat);
3809 if (ops[0].value != target)
3810 emit_move_insn (target, ops[0].value);
3811 return true;
3813 /* Generate an instruction whose insn-code is INSN_CODE,
3814 with two operands: an output TARGET and an input OP0.
3815 TARGET *must* be nonzero, and the output is always stored there.
3816 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3817 the value that is stored into TARGET. */
3819 void
3820 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3822 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3823 gcc_assert (ok);
3826 struct no_conflict_data
3828 rtx target;
3829 rtx_insn *first, *insn;
3830 bool must_stay;
3833 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3834 the currently examined clobber / store has to stay in the list of
3835 insns that constitute the actual libcall block. */
3836 static void
3837 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3839 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3841 /* If this inns directly contributes to setting the target, it must stay. */
3842 if (reg_overlap_mentioned_p (p->target, dest))
3843 p->must_stay = true;
3844 /* If we haven't committed to keeping any other insns in the list yet,
3845 there is nothing more to check. */
3846 else if (p->insn == p->first)
3847 return;
3848 /* If this insn sets / clobbers a register that feeds one of the insns
3849 already in the list, this insn has to stay too. */
3850 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3851 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3852 || reg_used_between_p (dest, p->first, p->insn)
3853 /* Likewise if this insn depends on a register set by a previous
3854 insn in the list, or if it sets a result (presumably a hard
3855 register) that is set or clobbered by a previous insn.
3856 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3857 SET_DEST perform the former check on the address, and the latter
3858 check on the MEM. */
3859 || (GET_CODE (set) == SET
3860 && (modified_in_p (SET_SRC (set), p->first)
3861 || modified_in_p (SET_DEST (set), p->first)
3862 || modified_between_p (SET_SRC (set), p->first, p->insn)
3863 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3864 p->must_stay = true;
3868 /* Emit code to make a call to a constant function or a library call.
3870 INSNS is a list containing all insns emitted in the call.
3871 These insns leave the result in RESULT. Our block is to copy RESULT
3872 to TARGET, which is logically equivalent to EQUIV.
3874 We first emit any insns that set a pseudo on the assumption that these are
3875 loading constants into registers; doing so allows them to be safely cse'ed
3876 between blocks. Then we emit all the other insns in the block, followed by
3877 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3878 note with an operand of EQUIV. */
3880 static void
3881 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
3882 bool equiv_may_trap)
3884 rtx final_dest = target;
3885 rtx_insn *next, *last, *insn;
3887 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3888 into a MEM later. Protect the libcall block from this change. */
3889 if (! REG_P (target) || REG_USERVAR_P (target))
3890 target = gen_reg_rtx (GET_MODE (target));
3892 /* If we're using non-call exceptions, a libcall corresponding to an
3893 operation that may trap may also trap. */
3894 /* ??? See the comment in front of make_reg_eh_region_note. */
3895 if (cfun->can_throw_non_call_exceptions
3896 && (equiv_may_trap || may_trap_p (equiv)))
3898 for (insn = insns; insn; insn = NEXT_INSN (insn))
3899 if (CALL_P (insn))
3901 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3902 if (note)
3904 int lp_nr = INTVAL (XEXP (note, 0));
3905 if (lp_nr == 0 || lp_nr == INT_MIN)
3906 remove_note (insn, note);
3910 else
3912 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3913 reg note to indicate that this call cannot throw or execute a nonlocal
3914 goto (unless there is already a REG_EH_REGION note, in which case
3915 we update it). */
3916 for (insn = insns; insn; insn = NEXT_INSN (insn))
3917 if (CALL_P (insn))
3918 make_reg_eh_region_note_nothrow_nononlocal (insn);
3921 /* First emit all insns that set pseudos. Remove them from the list as
3922 we go. Avoid insns that set pseudos which were referenced in previous
3923 insns. These can be generated by move_by_pieces, for example,
3924 to update an address. Similarly, avoid insns that reference things
3925 set in previous insns. */
3927 for (insn = insns; insn; insn = next)
3929 rtx set = single_set (insn);
3931 next = NEXT_INSN (insn);
3933 if (set != 0 && REG_P (SET_DEST (set))
3934 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3936 struct no_conflict_data data;
3938 data.target = const0_rtx;
3939 data.first = insns;
3940 data.insn = insn;
3941 data.must_stay = 0;
3942 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3943 if (! data.must_stay)
3945 if (PREV_INSN (insn))
3946 SET_NEXT_INSN (PREV_INSN (insn)) = next;
3947 else
3948 insns = next;
3950 if (next)
3951 SET_PREV_INSN (next) = PREV_INSN (insn);
3953 add_insn (insn);
3957 /* Some ports use a loop to copy large arguments onto the stack.
3958 Don't move anything outside such a loop. */
3959 if (LABEL_P (insn))
3960 break;
3963 /* Write the remaining insns followed by the final copy. */
3964 for (insn = insns; insn; insn = next)
3966 next = NEXT_INSN (insn);
3968 add_insn (insn);
3971 last = emit_move_insn (target, result);
3972 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3974 if (final_dest != target)
3975 emit_move_insn (final_dest, target);
3978 void
3979 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3981 emit_libcall_block_1 (safe_as_a <rtx_insn *> (insns),
3982 target, result, equiv, false);
3985 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3986 PURPOSE describes how this comparison will be used. CODE is the rtx
3987 comparison code we will be using.
3989 ??? Actually, CODE is slightly weaker than that. A target is still
3990 required to implement all of the normal bcc operations, but not
3991 required to implement all (or any) of the unordered bcc operations. */
3994 can_compare_p (enum rtx_code code, machine_mode mode,
3995 enum can_compare_purpose purpose)
3997 rtx test;
3998 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
4001 enum insn_code icode;
4003 if (purpose == ccp_jump
4004 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
4005 && insn_operand_matches (icode, 0, test))
4006 return 1;
4007 if (purpose == ccp_store_flag
4008 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
4009 && insn_operand_matches (icode, 1, test))
4010 return 1;
4011 if (purpose == ccp_cmov
4012 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
4013 return 1;
4015 mode = GET_MODE_WIDER_MODE (mode);
4016 PUT_MODE (test, mode);
4018 while (mode != VOIDmode);
4020 return 0;
4023 /* This function is called when we are going to emit a compare instruction that
4024 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
4026 *PMODE is the mode of the inputs (in case they are const_int).
4027 *PUNSIGNEDP nonzero says that the operands are unsigned;
4028 this matters if they need to be widened (as given by METHODS).
4030 If they have mode BLKmode, then SIZE specifies the size of both operands.
4032 This function performs all the setup necessary so that the caller only has
4033 to emit a single comparison insn. This setup can involve doing a BLKmode
4034 comparison or emitting a library call to perform the comparison if no insn
4035 is available to handle it.
4036 The values which are passed in through pointers can be modified; the caller
4037 should perform the comparison on the modified values. Constant
4038 comparisons must have already been folded. */
4040 static void
4041 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4042 int unsignedp, enum optab_methods methods,
4043 rtx *ptest, machine_mode *pmode)
4045 machine_mode mode = *pmode;
4046 rtx libfunc, test;
4047 machine_mode cmp_mode;
4048 enum mode_class mclass;
4050 /* The other methods are not needed. */
4051 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4052 || methods == OPTAB_LIB_WIDEN);
4054 /* If we are optimizing, force expensive constants into a register. */
4055 if (CONSTANT_P (x) && optimize
4056 && (rtx_cost (x, mode, COMPARE, 0, optimize_insn_for_speed_p ())
4057 > COSTS_N_INSNS (1)))
4058 x = force_reg (mode, x);
4060 if (CONSTANT_P (y) && optimize
4061 && (rtx_cost (y, mode, COMPARE, 1, optimize_insn_for_speed_p ())
4062 > COSTS_N_INSNS (1)))
4063 y = force_reg (mode, y);
4065 #if HAVE_cc0
4066 /* Make sure if we have a canonical comparison. The RTL
4067 documentation states that canonical comparisons are required only
4068 for targets which have cc0. */
4069 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4070 #endif
4072 /* Don't let both operands fail to indicate the mode. */
4073 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4074 x = force_reg (mode, x);
4075 if (mode == VOIDmode)
4076 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4078 /* Handle all BLKmode compares. */
4080 if (mode == BLKmode)
4082 machine_mode result_mode;
4083 enum insn_code cmp_code;
4084 tree length_type;
4085 rtx libfunc;
4086 rtx result;
4087 rtx opalign
4088 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4090 gcc_assert (size);
4092 /* Try to use a memory block compare insn - either cmpstr
4093 or cmpmem will do. */
4094 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4095 cmp_mode != VOIDmode;
4096 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4098 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4099 if (cmp_code == CODE_FOR_nothing)
4100 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4101 if (cmp_code == CODE_FOR_nothing)
4102 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4103 if (cmp_code == CODE_FOR_nothing)
4104 continue;
4106 /* Must make sure the size fits the insn's mode. */
4107 if ((CONST_INT_P (size)
4108 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4109 || (GET_MODE_BITSIZE (GET_MODE (size))
4110 > GET_MODE_BITSIZE (cmp_mode)))
4111 continue;
4113 result_mode = insn_data[cmp_code].operand[0].mode;
4114 result = gen_reg_rtx (result_mode);
4115 size = convert_to_mode (cmp_mode, size, 1);
4116 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4118 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4119 *pmode = result_mode;
4120 return;
4123 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4124 goto fail;
4126 /* Otherwise call a library function, memcmp. */
4127 libfunc = memcmp_libfunc;
4128 length_type = sizetype;
4129 result_mode = TYPE_MODE (integer_type_node);
4130 cmp_mode = TYPE_MODE (length_type);
4131 size = convert_to_mode (TYPE_MODE (length_type), size,
4132 TYPE_UNSIGNED (length_type));
4134 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4135 result_mode, 3,
4136 XEXP (x, 0), Pmode,
4137 XEXP (y, 0), Pmode,
4138 size, cmp_mode);
4139 x = result;
4140 y = const0_rtx;
4141 mode = result_mode;
4142 methods = OPTAB_LIB_WIDEN;
4143 unsignedp = false;
4146 /* Don't allow operands to the compare to trap, as that can put the
4147 compare and branch in different basic blocks. */
4148 if (cfun->can_throw_non_call_exceptions)
4150 if (may_trap_p (x))
4151 x = force_reg (mode, x);
4152 if (may_trap_p (y))
4153 y = force_reg (mode, y);
4156 if (GET_MODE_CLASS (mode) == MODE_CC)
4158 enum insn_code icode = optab_handler (cbranch_optab, CCmode);
4159 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4160 gcc_assert (icode != CODE_FOR_nothing
4161 && insn_operand_matches (icode, 0, test));
4162 *ptest = test;
4163 return;
4166 mclass = GET_MODE_CLASS (mode);
4167 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4168 cmp_mode = mode;
4171 enum insn_code icode;
4172 icode = optab_handler (cbranch_optab, cmp_mode);
4173 if (icode != CODE_FOR_nothing
4174 && insn_operand_matches (icode, 0, test))
4176 rtx_insn *last = get_last_insn ();
4177 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4178 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4179 if (op0 && op1
4180 && insn_operand_matches (icode, 1, op0)
4181 && insn_operand_matches (icode, 2, op1))
4183 XEXP (test, 0) = op0;
4184 XEXP (test, 1) = op1;
4185 *ptest = test;
4186 *pmode = cmp_mode;
4187 return;
4189 delete_insns_since (last);
4192 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4193 break;
4194 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4196 while (cmp_mode != VOIDmode);
4198 if (methods != OPTAB_LIB_WIDEN)
4199 goto fail;
4201 if (!SCALAR_FLOAT_MODE_P (mode))
4203 rtx result;
4204 machine_mode ret_mode;
4206 /* Handle a libcall just for the mode we are using. */
4207 libfunc = optab_libfunc (cmp_optab, mode);
4208 gcc_assert (libfunc);
4210 /* If we want unsigned, and this mode has a distinct unsigned
4211 comparison routine, use that. */
4212 if (unsignedp)
4214 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4215 if (ulibfunc)
4216 libfunc = ulibfunc;
4219 ret_mode = targetm.libgcc_cmp_return_mode ();
4220 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4221 ret_mode, 2, x, mode, y, mode);
4223 /* There are two kinds of comparison routines. Biased routines
4224 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4225 of gcc expect that the comparison operation is equivalent
4226 to the modified comparison. For signed comparisons compare the
4227 result against 1 in the biased case, and zero in the unbiased
4228 case. For unsigned comparisons always compare against 1 after
4229 biasing the unbiased result by adding 1. This gives us a way to
4230 represent LTU.
4231 The comparisons in the fixed-point helper library are always
4232 biased. */
4233 x = result;
4234 y = const1_rtx;
4236 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4238 if (unsignedp)
4239 x = plus_constant (ret_mode, result, 1);
4240 else
4241 y = const0_rtx;
4244 *pmode = ret_mode;
4245 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4246 ptest, pmode);
4248 else
4249 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4251 return;
4253 fail:
4254 *ptest = NULL_RTX;
4257 /* Before emitting an insn with code ICODE, make sure that X, which is going
4258 to be used for operand OPNUM of the insn, is converted from mode MODE to
4259 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4260 that it is accepted by the operand predicate. Return the new value. */
4263 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
4264 machine_mode wider_mode, int unsignedp)
4266 if (mode != wider_mode)
4267 x = convert_modes (wider_mode, mode, x, unsignedp);
4269 if (!insn_operand_matches (icode, opnum, x))
4271 machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
4272 if (reload_completed)
4273 return NULL_RTX;
4274 if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
4275 return NULL_RTX;
4276 x = copy_to_mode_reg (op_mode, x);
4279 return x;
4282 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4283 we can do the branch. */
4285 static void
4286 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label, int prob)
4288 machine_mode optab_mode;
4289 enum mode_class mclass;
4290 enum insn_code icode;
4291 rtx_insn *insn;
4293 mclass = GET_MODE_CLASS (mode);
4294 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4295 icode = optab_handler (cbranch_optab, optab_mode);
4297 gcc_assert (icode != CODE_FOR_nothing);
4298 gcc_assert (insn_operand_matches (icode, 0, test));
4299 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4300 XEXP (test, 1), label));
4301 if (prob != -1
4302 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4303 && insn
4304 && JUMP_P (insn)
4305 && any_condjump_p (insn)
4306 && !find_reg_note (insn, REG_BR_PROB, 0))
4307 add_int_reg_note (insn, REG_BR_PROB, prob);
4310 /* Generate code to compare X with Y so that the condition codes are
4311 set and to jump to LABEL if the condition is true. If X is a
4312 constant and Y is not a constant, then the comparison is swapped to
4313 ensure that the comparison RTL has the canonical form.
4315 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4316 need to be widened. UNSIGNEDP is also used to select the proper
4317 branch condition code.
4319 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4321 MODE is the mode of the inputs (in case they are const_int).
4323 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4324 It will be potentially converted into an unsigned variant based on
4325 UNSIGNEDP to select a proper jump instruction.
4327 PROB is the probability of jumping to LABEL. */
4329 void
4330 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4331 machine_mode mode, int unsignedp, rtx label,
4332 int prob)
4334 rtx op0 = x, op1 = y;
4335 rtx test;
4337 /* Swap operands and condition to ensure canonical RTL. */
4338 if (swap_commutative_operands_p (x, y)
4339 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4341 op0 = y, op1 = x;
4342 comparison = swap_condition (comparison);
4345 /* If OP0 is still a constant, then both X and Y must be constants
4346 or the opposite comparison is not supported. Force X into a register
4347 to create canonical RTL. */
4348 if (CONSTANT_P (op0))
4349 op0 = force_reg (mode, op0);
4351 if (unsignedp)
4352 comparison = unsigned_condition (comparison);
4354 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4355 &test, &mode);
4356 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4360 /* Emit a library call comparison between floating point X and Y.
4361 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4363 static void
4364 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4365 rtx *ptest, machine_mode *pmode)
4367 enum rtx_code swapped = swap_condition (comparison);
4368 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4369 machine_mode orig_mode = GET_MODE (x);
4370 machine_mode mode, cmp_mode;
4371 rtx true_rtx, false_rtx;
4372 rtx value, target, equiv;
4373 rtx_insn *insns;
4374 rtx libfunc = 0;
4375 bool reversed_p = false;
4376 cmp_mode = targetm.libgcc_cmp_return_mode ();
4378 for (mode = orig_mode;
4379 mode != VOIDmode;
4380 mode = GET_MODE_WIDER_MODE (mode))
4382 if (code_to_optab (comparison)
4383 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4384 break;
4386 if (code_to_optab (swapped)
4387 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4389 std::swap (x, y);
4390 comparison = swapped;
4391 break;
4394 if (code_to_optab (reversed)
4395 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4397 comparison = reversed;
4398 reversed_p = true;
4399 break;
4403 gcc_assert (mode != VOIDmode);
4405 if (mode != orig_mode)
4407 x = convert_to_mode (mode, x, 0);
4408 y = convert_to_mode (mode, y, 0);
4411 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4412 the RTL. The allows the RTL optimizers to delete the libcall if the
4413 condition can be determined at compile-time. */
4414 if (comparison == UNORDERED
4415 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4417 true_rtx = const_true_rtx;
4418 false_rtx = const0_rtx;
4420 else
4422 switch (comparison)
4424 case EQ:
4425 true_rtx = const0_rtx;
4426 false_rtx = const_true_rtx;
4427 break;
4429 case NE:
4430 true_rtx = const_true_rtx;
4431 false_rtx = const0_rtx;
4432 break;
4434 case GT:
4435 true_rtx = const1_rtx;
4436 false_rtx = const0_rtx;
4437 break;
4439 case GE:
4440 true_rtx = const0_rtx;
4441 false_rtx = constm1_rtx;
4442 break;
4444 case LT:
4445 true_rtx = constm1_rtx;
4446 false_rtx = const0_rtx;
4447 break;
4449 case LE:
4450 true_rtx = const0_rtx;
4451 false_rtx = const1_rtx;
4452 break;
4454 default:
4455 gcc_unreachable ();
4459 if (comparison == UNORDERED)
4461 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4462 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4463 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4464 temp, const_true_rtx, equiv);
4466 else
4468 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4469 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4470 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4471 equiv, true_rtx, false_rtx);
4474 start_sequence ();
4475 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4476 cmp_mode, 2, x, mode, y, mode);
4477 insns = get_insns ();
4478 end_sequence ();
4480 target = gen_reg_rtx (cmp_mode);
4481 emit_libcall_block (insns, target, value, equiv);
4483 if (comparison == UNORDERED
4484 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4485 || reversed_p)
4486 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4487 else
4488 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4490 *pmode = cmp_mode;
4493 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4495 void
4496 emit_indirect_jump (rtx loc)
4498 if (!targetm.have_indirect_jump ())
4499 sorry ("indirect jumps are not available on this target");
4500 else
4502 struct expand_operand ops[1];
4503 create_address_operand (&ops[0], loc);
4504 expand_jump_insn (targetm.code_for_indirect_jump, 1, ops);
4505 emit_barrier ();
4510 /* Emit a conditional move instruction if the machine supports one for that
4511 condition and machine mode.
4513 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4514 the mode to use should they be constants. If it is VOIDmode, they cannot
4515 both be constants.
4517 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4518 should be stored there. MODE is the mode to use should they be constants.
4519 If it is VOIDmode, they cannot both be constants.
4521 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4522 is not supported. */
4525 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4526 machine_mode cmode, rtx op2, rtx op3,
4527 machine_mode mode, int unsignedp)
4529 rtx comparison;
4530 rtx_insn *last;
4531 enum insn_code icode;
4532 enum rtx_code reversed;
4534 /* If one operand is constant, make it the second one. Only do this
4535 if the other operand is not constant as well. */
4537 if (swap_commutative_operands_p (op0, op1))
4539 std::swap (op0, op1);
4540 code = swap_condition (code);
4543 /* get_condition will prefer to generate LT and GT even if the old
4544 comparison was against zero, so undo that canonicalization here since
4545 comparisons against zero are cheaper. */
4546 if (code == LT && op1 == const1_rtx)
4547 code = LE, op1 = const0_rtx;
4548 else if (code == GT && op1 == constm1_rtx)
4549 code = GE, op1 = const0_rtx;
4551 if (cmode == VOIDmode)
4552 cmode = GET_MODE (op0);
4554 if (swap_commutative_operands_p (op2, op3)
4555 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4556 != UNKNOWN))
4558 std::swap (op2, op3);
4559 code = reversed;
4562 if (mode == VOIDmode)
4563 mode = GET_MODE (op2);
4565 icode = direct_optab_handler (movcc_optab, mode);
4567 if (icode == CODE_FOR_nothing)
4568 return 0;
4570 if (!target)
4571 target = gen_reg_rtx (mode);
4573 code = unsignedp ? unsigned_condition (code) : code;
4574 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4576 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4577 return NULL and let the caller figure out how best to deal with this
4578 situation. */
4579 if (!COMPARISON_P (comparison))
4580 return NULL_RTX;
4582 saved_pending_stack_adjust save;
4583 save_pending_stack_adjust (&save);
4584 last = get_last_insn ();
4585 do_pending_stack_adjust ();
4586 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4587 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4588 &comparison, &cmode);
4589 if (comparison)
4591 struct expand_operand ops[4];
4593 create_output_operand (&ops[0], target, mode);
4594 create_fixed_operand (&ops[1], comparison);
4595 create_input_operand (&ops[2], op2, mode);
4596 create_input_operand (&ops[3], op3, mode);
4597 if (maybe_expand_insn (icode, 4, ops))
4599 if (ops[0].value != target)
4600 convert_move (target, ops[0].value, false);
4601 return target;
4604 delete_insns_since (last);
4605 restore_pending_stack_adjust (&save);
4606 return NULL_RTX;
4609 /* Return nonzero if a conditional move of mode MODE is supported.
4611 This function is for combine so it can tell whether an insn that looks
4612 like a conditional move is actually supported by the hardware. If we
4613 guess wrong we lose a bit on optimization, but that's it. */
4614 /* ??? sparc64 supports conditionally moving integers values based on fp
4615 comparisons, and vice versa. How do we handle them? */
4618 can_conditionally_move_p (machine_mode mode)
4620 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4621 return 1;
4623 return 0;
4626 /* Emit a conditional addition instruction if the machine supports one for that
4627 condition and machine mode.
4629 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4630 the mode to use should they be constants. If it is VOIDmode, they cannot
4631 both be constants.
4633 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4634 should be stored there. MODE is the mode to use should they be constants.
4635 If it is VOIDmode, they cannot both be constants.
4637 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4638 is not supported. */
4641 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4642 machine_mode cmode, rtx op2, rtx op3,
4643 machine_mode mode, int unsignedp)
4645 rtx comparison;
4646 rtx_insn *last;
4647 enum insn_code icode;
4649 /* If one operand is constant, make it the second one. Only do this
4650 if the other operand is not constant as well. */
4652 if (swap_commutative_operands_p (op0, op1))
4654 std::swap (op0, op1);
4655 code = swap_condition (code);
4658 /* get_condition will prefer to generate LT and GT even if the old
4659 comparison was against zero, so undo that canonicalization here since
4660 comparisons against zero are cheaper. */
4661 if (code == LT && op1 == const1_rtx)
4662 code = LE, op1 = const0_rtx;
4663 else if (code == GT && op1 == constm1_rtx)
4664 code = GE, op1 = const0_rtx;
4666 if (cmode == VOIDmode)
4667 cmode = GET_MODE (op0);
4669 if (mode == VOIDmode)
4670 mode = GET_MODE (op2);
4672 icode = optab_handler (addcc_optab, mode);
4674 if (icode == CODE_FOR_nothing)
4675 return 0;
4677 if (!target)
4678 target = gen_reg_rtx (mode);
4680 code = unsignedp ? unsigned_condition (code) : code;
4681 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4683 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4684 return NULL and let the caller figure out how best to deal with this
4685 situation. */
4686 if (!COMPARISON_P (comparison))
4687 return NULL_RTX;
4689 do_pending_stack_adjust ();
4690 last = get_last_insn ();
4691 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4692 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4693 &comparison, &cmode);
4694 if (comparison)
4696 struct expand_operand ops[4];
4698 create_output_operand (&ops[0], target, mode);
4699 create_fixed_operand (&ops[1], comparison);
4700 create_input_operand (&ops[2], op2, mode);
4701 create_input_operand (&ops[3], op3, mode);
4702 if (maybe_expand_insn (icode, 4, ops))
4704 if (ops[0].value != target)
4705 convert_move (target, ops[0].value, false);
4706 return target;
4709 delete_insns_since (last);
4710 return NULL_RTX;
4713 /* These functions attempt to generate an insn body, rather than
4714 emitting the insn, but if the gen function already emits them, we
4715 make no attempt to turn them back into naked patterns. */
4717 /* Generate and return an insn body to add Y to X. */
4719 rtx_insn *
4720 gen_add2_insn (rtx x, rtx y)
4722 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4724 gcc_assert (insn_operand_matches (icode, 0, x));
4725 gcc_assert (insn_operand_matches (icode, 1, x));
4726 gcc_assert (insn_operand_matches (icode, 2, y));
4728 return GEN_FCN (icode) (x, x, y);
4731 /* Generate and return an insn body to add r1 and c,
4732 storing the result in r0. */
4734 rtx_insn *
4735 gen_add3_insn (rtx r0, rtx r1, rtx c)
4737 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4739 if (icode == CODE_FOR_nothing
4740 || !insn_operand_matches (icode, 0, r0)
4741 || !insn_operand_matches (icode, 1, r1)
4742 || !insn_operand_matches (icode, 2, c))
4743 return NULL;
4745 return GEN_FCN (icode) (r0, r1, c);
4749 have_add2_insn (rtx x, rtx y)
4751 enum insn_code icode;
4753 gcc_assert (GET_MODE (x) != VOIDmode);
4755 icode = optab_handler (add_optab, GET_MODE (x));
4757 if (icode == CODE_FOR_nothing)
4758 return 0;
4760 if (!insn_operand_matches (icode, 0, x)
4761 || !insn_operand_matches (icode, 1, x)
4762 || !insn_operand_matches (icode, 2, y))
4763 return 0;
4765 return 1;
4768 /* Generate and return an insn body to add Y to X. */
4770 rtx_insn *
4771 gen_addptr3_insn (rtx x, rtx y, rtx z)
4773 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4775 gcc_assert (insn_operand_matches (icode, 0, x));
4776 gcc_assert (insn_operand_matches (icode, 1, y));
4777 gcc_assert (insn_operand_matches (icode, 2, z));
4779 return GEN_FCN (icode) (x, y, z);
4782 /* Return true if the target implements an addptr pattern and X, Y,
4783 and Z are valid for the pattern predicates. */
4786 have_addptr3_insn (rtx x, rtx y, rtx z)
4788 enum insn_code icode;
4790 gcc_assert (GET_MODE (x) != VOIDmode);
4792 icode = optab_handler (addptr3_optab, GET_MODE (x));
4794 if (icode == CODE_FOR_nothing)
4795 return 0;
4797 if (!insn_operand_matches (icode, 0, x)
4798 || !insn_operand_matches (icode, 1, y)
4799 || !insn_operand_matches (icode, 2, z))
4800 return 0;
4802 return 1;
4805 /* Generate and return an insn body to subtract Y from X. */
4807 rtx_insn *
4808 gen_sub2_insn (rtx x, rtx y)
4810 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4812 gcc_assert (insn_operand_matches (icode, 0, x));
4813 gcc_assert (insn_operand_matches (icode, 1, x));
4814 gcc_assert (insn_operand_matches (icode, 2, y));
4816 return GEN_FCN (icode) (x, x, y);
4819 /* Generate and return an insn body to subtract r1 and c,
4820 storing the result in r0. */
4822 rtx_insn *
4823 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4825 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4827 if (icode == CODE_FOR_nothing
4828 || !insn_operand_matches (icode, 0, r0)
4829 || !insn_operand_matches (icode, 1, r1)
4830 || !insn_operand_matches (icode, 2, c))
4831 return NULL;
4833 return GEN_FCN (icode) (r0, r1, c);
4837 have_sub2_insn (rtx x, rtx y)
4839 enum insn_code icode;
4841 gcc_assert (GET_MODE (x) != VOIDmode);
4843 icode = optab_handler (sub_optab, GET_MODE (x));
4845 if (icode == CODE_FOR_nothing)
4846 return 0;
4848 if (!insn_operand_matches (icode, 0, x)
4849 || !insn_operand_matches (icode, 1, x)
4850 || !insn_operand_matches (icode, 2, y))
4851 return 0;
4853 return 1;
4856 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4857 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4858 no such operation exists, CODE_FOR_nothing will be returned. */
4860 enum insn_code
4861 can_extend_p (machine_mode to_mode, machine_mode from_mode,
4862 int unsignedp)
4864 convert_optab tab;
4865 if (unsignedp < 0 && targetm.have_ptr_extend ())
4866 return targetm.code_for_ptr_extend;
4868 tab = unsignedp ? zext_optab : sext_optab;
4869 return convert_optab_handler (tab, to_mode, from_mode);
4872 /* Generate the body of an insn to extend Y (with mode MFROM)
4873 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4875 rtx_insn *
4876 gen_extend_insn (rtx x, rtx y, machine_mode mto,
4877 machine_mode mfrom, int unsignedp)
4879 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4880 return GEN_FCN (icode) (x, y);
4883 /* can_fix_p and can_float_p say whether the target machine
4884 can directly convert a given fixed point type to
4885 a given floating point type, or vice versa.
4886 The returned value is the CODE_FOR_... value to use,
4887 or CODE_FOR_nothing if these modes cannot be directly converted.
4889 *TRUNCP_PTR is set to 1 if it is necessary to output
4890 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4892 static enum insn_code
4893 can_fix_p (machine_mode fixmode, machine_mode fltmode,
4894 int unsignedp, int *truncp_ptr)
4896 convert_optab tab;
4897 enum insn_code icode;
4899 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4900 icode = convert_optab_handler (tab, fixmode, fltmode);
4901 if (icode != CODE_FOR_nothing)
4903 *truncp_ptr = 0;
4904 return icode;
4907 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4908 for this to work. We need to rework the fix* and ftrunc* patterns
4909 and documentation. */
4910 tab = unsignedp ? ufix_optab : sfix_optab;
4911 icode = convert_optab_handler (tab, fixmode, fltmode);
4912 if (icode != CODE_FOR_nothing
4913 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4915 *truncp_ptr = 1;
4916 return icode;
4919 *truncp_ptr = 0;
4920 return CODE_FOR_nothing;
4923 enum insn_code
4924 can_float_p (machine_mode fltmode, machine_mode fixmode,
4925 int unsignedp)
4927 convert_optab tab;
4929 tab = unsignedp ? ufloat_optab : sfloat_optab;
4930 return convert_optab_handler (tab, fltmode, fixmode);
4933 /* Function supportable_convert_operation
4935 Check whether an operation represented by the code CODE is a
4936 convert operation that is supported by the target platform in
4937 vector form (i.e., when operating on arguments of type VECTYPE_IN
4938 producing a result of type VECTYPE_OUT).
4940 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4941 This function checks if these operations are supported
4942 by the target platform either directly (via vector tree-codes), or via
4943 target builtins.
4945 Output:
4946 - CODE1 is code of vector operation to be used when
4947 vectorizing the operation, if available.
4948 - DECL is decl of target builtin functions to be used
4949 when vectorizing the operation, if available. In this case,
4950 CODE1 is CALL_EXPR. */
4952 bool
4953 supportable_convert_operation (enum tree_code code,
4954 tree vectype_out, tree vectype_in,
4955 tree *decl, enum tree_code *code1)
4957 machine_mode m1,m2;
4958 int truncp;
4960 m1 = TYPE_MODE (vectype_out);
4961 m2 = TYPE_MODE (vectype_in);
4963 /* First check if we can done conversion directly. */
4964 if ((code == FIX_TRUNC_EXPR
4965 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
4966 != CODE_FOR_nothing)
4967 || (code == FLOAT_EXPR
4968 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
4969 != CODE_FOR_nothing))
4971 *code1 = code;
4972 return true;
4975 /* Now check for builtin. */
4976 if (targetm.vectorize.builtin_conversion
4977 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
4979 *code1 = CALL_EXPR;
4980 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
4981 return true;
4983 return false;
4987 /* Generate code to convert FROM to floating point
4988 and store in TO. FROM must be fixed point and not VOIDmode.
4989 UNSIGNEDP nonzero means regard FROM as unsigned.
4990 Normally this is done by correcting the final value
4991 if it is negative. */
4993 void
4994 expand_float (rtx to, rtx from, int unsignedp)
4996 enum insn_code icode;
4997 rtx target = to;
4998 machine_mode fmode, imode;
4999 bool can_do_signed = false;
5001 /* Crash now, because we won't be able to decide which mode to use. */
5002 gcc_assert (GET_MODE (from) != VOIDmode);
5004 /* Look for an insn to do the conversion. Do it in the specified
5005 modes if possible; otherwise convert either input, output or both to
5006 wider mode. If the integer mode is wider than the mode of FROM,
5007 we can do the conversion signed even if the input is unsigned. */
5009 for (fmode = GET_MODE (to); fmode != VOIDmode;
5010 fmode = GET_MODE_WIDER_MODE (fmode))
5011 for (imode = GET_MODE (from); imode != VOIDmode;
5012 imode = GET_MODE_WIDER_MODE (imode))
5014 int doing_unsigned = unsignedp;
5016 if (fmode != GET_MODE (to)
5017 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
5018 continue;
5020 icode = can_float_p (fmode, imode, unsignedp);
5021 if (icode == CODE_FOR_nothing && unsignedp)
5023 enum insn_code scode = can_float_p (fmode, imode, 0);
5024 if (scode != CODE_FOR_nothing)
5025 can_do_signed = true;
5026 if (imode != GET_MODE (from))
5027 icode = scode, doing_unsigned = 0;
5030 if (icode != CODE_FOR_nothing)
5032 if (imode != GET_MODE (from))
5033 from = convert_to_mode (imode, from, unsignedp);
5035 if (fmode != GET_MODE (to))
5036 target = gen_reg_rtx (fmode);
5038 emit_unop_insn (icode, target, from,
5039 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
5041 if (target != to)
5042 convert_move (to, target, 0);
5043 return;
5047 /* Unsigned integer, and no way to convert directly. Convert as signed,
5048 then unconditionally adjust the result. */
5049 if (unsignedp && can_do_signed)
5051 rtx_code_label *label = gen_label_rtx ();
5052 rtx temp;
5053 REAL_VALUE_TYPE offset;
5055 /* Look for a usable floating mode FMODE wider than the source and at
5056 least as wide as the target. Using FMODE will avoid rounding woes
5057 with unsigned values greater than the signed maximum value. */
5059 for (fmode = GET_MODE (to); fmode != VOIDmode;
5060 fmode = GET_MODE_WIDER_MODE (fmode))
5061 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
5062 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
5063 break;
5065 if (fmode == VOIDmode)
5067 /* There is no such mode. Pretend the target is wide enough. */
5068 fmode = GET_MODE (to);
5070 /* Avoid double-rounding when TO is narrower than FROM. */
5071 if ((significand_size (fmode) + 1)
5072 < GET_MODE_PRECISION (GET_MODE (from)))
5074 rtx temp1;
5075 rtx_code_label *neglabel = gen_label_rtx ();
5077 /* Don't use TARGET if it isn't a register, is a hard register,
5078 or is the wrong mode. */
5079 if (!REG_P (target)
5080 || REGNO (target) < FIRST_PSEUDO_REGISTER
5081 || GET_MODE (target) != fmode)
5082 target = gen_reg_rtx (fmode);
5084 imode = GET_MODE (from);
5085 do_pending_stack_adjust ();
5087 /* Test whether the sign bit is set. */
5088 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5089 0, neglabel);
5091 /* The sign bit is not set. Convert as signed. */
5092 expand_float (target, from, 0);
5093 emit_jump_insn (targetm.gen_jump (label));
5094 emit_barrier ();
5096 /* The sign bit is set.
5097 Convert to a usable (positive signed) value by shifting right
5098 one bit, while remembering if a nonzero bit was shifted
5099 out; i.e., compute (from & 1) | (from >> 1). */
5101 emit_label (neglabel);
5102 temp = expand_binop (imode, and_optab, from, const1_rtx,
5103 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5104 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5105 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5106 OPTAB_LIB_WIDEN);
5107 expand_float (target, temp, 0);
5109 /* Multiply by 2 to undo the shift above. */
5110 temp = expand_binop (fmode, add_optab, target, target,
5111 target, 0, OPTAB_LIB_WIDEN);
5112 if (temp != target)
5113 emit_move_insn (target, temp);
5115 do_pending_stack_adjust ();
5116 emit_label (label);
5117 goto done;
5121 /* If we are about to do some arithmetic to correct for an
5122 unsigned operand, do it in a pseudo-register. */
5124 if (GET_MODE (to) != fmode
5125 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5126 target = gen_reg_rtx (fmode);
5128 /* Convert as signed integer to floating. */
5129 expand_float (target, from, 0);
5131 /* If FROM is negative (and therefore TO is negative),
5132 correct its value by 2**bitwidth. */
5134 do_pending_stack_adjust ();
5135 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5136 0, label);
5139 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5140 temp = expand_binop (fmode, add_optab, target,
5141 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5142 target, 0, OPTAB_LIB_WIDEN);
5143 if (temp != target)
5144 emit_move_insn (target, temp);
5146 do_pending_stack_adjust ();
5147 emit_label (label);
5148 goto done;
5151 /* No hardware instruction available; call a library routine. */
5153 rtx libfunc;
5154 rtx_insn *insns;
5155 rtx value;
5156 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5158 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_PRECISION (SImode))
5159 from = convert_to_mode (SImode, from, unsignedp);
5161 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5162 gcc_assert (libfunc);
5164 start_sequence ();
5166 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5167 GET_MODE (to), 1, from,
5168 GET_MODE (from));
5169 insns = get_insns ();
5170 end_sequence ();
5172 emit_libcall_block (insns, target, value,
5173 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5174 GET_MODE (to), from));
5177 done:
5179 /* Copy result to requested destination
5180 if we have been computing in a temp location. */
5182 if (target != to)
5184 if (GET_MODE (target) == GET_MODE (to))
5185 emit_move_insn (to, target);
5186 else
5187 convert_move (to, target, 0);
5191 /* Generate code to convert FROM to fixed point and store in TO. FROM
5192 must be floating point. */
5194 void
5195 expand_fix (rtx to, rtx from, int unsignedp)
5197 enum insn_code icode;
5198 rtx target = to;
5199 machine_mode fmode, imode;
5200 int must_trunc = 0;
5202 /* We first try to find a pair of modes, one real and one integer, at
5203 least as wide as FROM and TO, respectively, in which we can open-code
5204 this conversion. If the integer mode is wider than the mode of TO,
5205 we can do the conversion either signed or unsigned. */
5207 for (fmode = GET_MODE (from); fmode != VOIDmode;
5208 fmode = GET_MODE_WIDER_MODE (fmode))
5209 for (imode = GET_MODE (to); imode != VOIDmode;
5210 imode = GET_MODE_WIDER_MODE (imode))
5212 int doing_unsigned = unsignedp;
5214 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5215 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5216 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5218 if (icode != CODE_FOR_nothing)
5220 rtx_insn *last = get_last_insn ();
5221 if (fmode != GET_MODE (from))
5222 from = convert_to_mode (fmode, from, 0);
5224 if (must_trunc)
5226 rtx temp = gen_reg_rtx (GET_MODE (from));
5227 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5228 temp, 0);
5231 if (imode != GET_MODE (to))
5232 target = gen_reg_rtx (imode);
5234 if (maybe_emit_unop_insn (icode, target, from,
5235 doing_unsigned ? UNSIGNED_FIX : FIX))
5237 if (target != to)
5238 convert_move (to, target, unsignedp);
5239 return;
5241 delete_insns_since (last);
5245 /* For an unsigned conversion, there is one more way to do it.
5246 If we have a signed conversion, we generate code that compares
5247 the real value to the largest representable positive number. If if
5248 is smaller, the conversion is done normally. Otherwise, subtract
5249 one plus the highest signed number, convert, and add it back.
5251 We only need to check all real modes, since we know we didn't find
5252 anything with a wider integer mode.
5254 This code used to extend FP value into mode wider than the destination.
5255 This is needed for decimal float modes which cannot accurately
5256 represent one plus the highest signed number of the same size, but
5257 not for binary modes. Consider, for instance conversion from SFmode
5258 into DImode.
5260 The hot path through the code is dealing with inputs smaller than 2^63
5261 and doing just the conversion, so there is no bits to lose.
5263 In the other path we know the value is positive in the range 2^63..2^64-1
5264 inclusive. (as for other input overflow happens and result is undefined)
5265 So we know that the most important bit set in mantissa corresponds to
5266 2^63. The subtraction of 2^63 should not generate any rounding as it
5267 simply clears out that bit. The rest is trivial. */
5269 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5270 for (fmode = GET_MODE (from); fmode != VOIDmode;
5271 fmode = GET_MODE_WIDER_MODE (fmode))
5272 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5273 && (!DECIMAL_FLOAT_MODE_P (fmode)
5274 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5276 int bitsize;
5277 REAL_VALUE_TYPE offset;
5278 rtx limit;
5279 rtx_code_label *lab1, *lab2;
5280 rtx_insn *insn;
5282 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5283 real_2expN (&offset, bitsize - 1, fmode);
5284 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5285 lab1 = gen_label_rtx ();
5286 lab2 = gen_label_rtx ();
5288 if (fmode != GET_MODE (from))
5289 from = convert_to_mode (fmode, from, 0);
5291 /* See if we need to do the subtraction. */
5292 do_pending_stack_adjust ();
5293 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5294 0, lab1);
5296 /* If not, do the signed "fix" and branch around fixup code. */
5297 expand_fix (to, from, 0);
5298 emit_jump_insn (targetm.gen_jump (lab2));
5299 emit_barrier ();
5301 /* Otherwise, subtract 2**(N-1), convert to signed number,
5302 then add 2**(N-1). Do the addition using XOR since this
5303 will often generate better code. */
5304 emit_label (lab1);
5305 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5306 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5307 expand_fix (to, target, 0);
5308 target = expand_binop (GET_MODE (to), xor_optab, to,
5309 gen_int_mode
5310 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5311 GET_MODE (to)),
5312 to, 1, OPTAB_LIB_WIDEN);
5314 if (target != to)
5315 emit_move_insn (to, target);
5317 emit_label (lab2);
5319 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5321 /* Make a place for a REG_NOTE and add it. */
5322 insn = emit_move_insn (to, to);
5323 set_dst_reg_note (insn, REG_EQUAL,
5324 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5325 copy_rtx (from)),
5326 to);
5329 return;
5332 /* We can't do it with an insn, so use a library call. But first ensure
5333 that the mode of TO is at least as wide as SImode, since those are the
5334 only library calls we know about. */
5336 if (GET_MODE_PRECISION (GET_MODE (to)) < GET_MODE_PRECISION (SImode))
5338 target = gen_reg_rtx (SImode);
5340 expand_fix (target, from, unsignedp);
5342 else
5344 rtx_insn *insns;
5345 rtx value;
5346 rtx libfunc;
5348 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5349 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5350 gcc_assert (libfunc);
5352 start_sequence ();
5354 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5355 GET_MODE (to), 1, from,
5356 GET_MODE (from));
5357 insns = get_insns ();
5358 end_sequence ();
5360 emit_libcall_block (insns, target, value,
5361 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5362 GET_MODE (to), from));
5365 if (target != to)
5367 if (GET_MODE (to) == GET_MODE (target))
5368 emit_move_insn (to, target);
5369 else
5370 convert_move (to, target, 0);
5374 /* Generate code to convert FROM or TO a fixed-point.
5375 If UINTP is true, either TO or FROM is an unsigned integer.
5376 If SATP is true, we need to saturate the result. */
5378 void
5379 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5381 machine_mode to_mode = GET_MODE (to);
5382 machine_mode from_mode = GET_MODE (from);
5383 convert_optab tab;
5384 enum rtx_code this_code;
5385 enum insn_code code;
5386 rtx_insn *insns;
5387 rtx value;
5388 rtx libfunc;
5390 if (to_mode == from_mode)
5392 emit_move_insn (to, from);
5393 return;
5396 if (uintp)
5398 tab = satp ? satfractuns_optab : fractuns_optab;
5399 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5401 else
5403 tab = satp ? satfract_optab : fract_optab;
5404 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5406 code = convert_optab_handler (tab, to_mode, from_mode);
5407 if (code != CODE_FOR_nothing)
5409 emit_unop_insn (code, to, from, this_code);
5410 return;
5413 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5414 gcc_assert (libfunc);
5416 start_sequence ();
5417 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5418 1, from, from_mode);
5419 insns = get_insns ();
5420 end_sequence ();
5422 emit_libcall_block (insns, to, value,
5423 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5426 /* Generate code to convert FROM to fixed point and store in TO. FROM
5427 must be floating point, TO must be signed. Use the conversion optab
5428 TAB to do the conversion. */
5430 bool
5431 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5433 enum insn_code icode;
5434 rtx target = to;
5435 machine_mode fmode, imode;
5437 /* We first try to find a pair of modes, one real and one integer, at
5438 least as wide as FROM and TO, respectively, in which we can open-code
5439 this conversion. If the integer mode is wider than the mode of TO,
5440 we can do the conversion either signed or unsigned. */
5442 for (fmode = GET_MODE (from); fmode != VOIDmode;
5443 fmode = GET_MODE_WIDER_MODE (fmode))
5444 for (imode = GET_MODE (to); imode != VOIDmode;
5445 imode = GET_MODE_WIDER_MODE (imode))
5447 icode = convert_optab_handler (tab, imode, fmode);
5448 if (icode != CODE_FOR_nothing)
5450 rtx_insn *last = get_last_insn ();
5451 if (fmode != GET_MODE (from))
5452 from = convert_to_mode (fmode, from, 0);
5454 if (imode != GET_MODE (to))
5455 target = gen_reg_rtx (imode);
5457 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5459 delete_insns_since (last);
5460 continue;
5462 if (target != to)
5463 convert_move (to, target, 0);
5464 return true;
5468 return false;
5471 /* Report whether we have an instruction to perform the operation
5472 specified by CODE on operands of mode MODE. */
5474 have_insn_for (enum rtx_code code, machine_mode mode)
5476 return (code_to_optab (code)
5477 && (optab_handler (code_to_optab (code), mode)
5478 != CODE_FOR_nothing));
5481 /* Initialize the libfunc fields of an entire group of entries in some
5482 optab. Each entry is set equal to a string consisting of a leading
5483 pair of underscores followed by a generic operation name followed by
5484 a mode name (downshifted to lowercase) followed by a single character
5485 representing the number of operands for the given operation (which is
5486 usually one of the characters '2', '3', or '4').
5488 OPTABLE is the table in which libfunc fields are to be initialized.
5489 OPNAME is the generic (string) name of the operation.
5490 SUFFIX is the character which specifies the number of operands for
5491 the given generic operation.
5492 MODE is the mode to generate for.
5495 static void
5496 gen_libfunc (optab optable, const char *opname, int suffix,
5497 machine_mode mode)
5499 unsigned opname_len = strlen (opname);
5500 const char *mname = GET_MODE_NAME (mode);
5501 unsigned mname_len = strlen (mname);
5502 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5503 int len = prefix_len + opname_len + mname_len + 1 + 1;
5504 char *libfunc_name = XALLOCAVEC (char, len);
5505 char *p;
5506 const char *q;
5508 p = libfunc_name;
5509 *p++ = '_';
5510 *p++ = '_';
5511 if (targetm.libfunc_gnu_prefix)
5513 *p++ = 'g';
5514 *p++ = 'n';
5515 *p++ = 'u';
5516 *p++ = '_';
5518 for (q = opname; *q; )
5519 *p++ = *q++;
5520 for (q = mname; *q; q++)
5521 *p++ = TOLOWER (*q);
5522 *p++ = suffix;
5523 *p = '\0';
5525 set_optab_libfunc (optable, mode,
5526 ggc_alloc_string (libfunc_name, p - libfunc_name));
5529 /* Like gen_libfunc, but verify that integer operation is involved. */
5531 void
5532 gen_int_libfunc (optab optable, const char *opname, char suffix,
5533 machine_mode mode)
5535 int maxsize = 2 * BITS_PER_WORD;
5536 int minsize = BITS_PER_WORD;
5538 if (GET_MODE_CLASS (mode) != MODE_INT)
5539 return;
5540 if (maxsize < LONG_LONG_TYPE_SIZE)
5541 maxsize = LONG_LONG_TYPE_SIZE;
5542 if (minsize > INT_TYPE_SIZE
5543 && (trapv_binoptab_p (optable)
5544 || trapv_unoptab_p (optable)))
5545 minsize = INT_TYPE_SIZE;
5546 if (GET_MODE_BITSIZE (mode) < minsize
5547 || GET_MODE_BITSIZE (mode) > maxsize)
5548 return;
5549 gen_libfunc (optable, opname, suffix, mode);
5552 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5554 void
5555 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5556 machine_mode mode)
5558 char *dec_opname;
5560 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5561 gen_libfunc (optable, opname, suffix, mode);
5562 if (DECIMAL_FLOAT_MODE_P (mode))
5564 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5565 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5566 depending on the low level floating format used. */
5567 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5568 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5569 gen_libfunc (optable, dec_opname, suffix, mode);
5573 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5575 void
5576 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5577 machine_mode mode)
5579 if (!ALL_FIXED_POINT_MODE_P (mode))
5580 return;
5581 gen_libfunc (optable, opname, suffix, mode);
5584 /* Like gen_libfunc, but verify that signed fixed-point operation is
5585 involved. */
5587 void
5588 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5589 machine_mode mode)
5591 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5592 return;
5593 gen_libfunc (optable, opname, suffix, mode);
5596 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5597 involved. */
5599 void
5600 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5601 machine_mode mode)
5603 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5604 return;
5605 gen_libfunc (optable, opname, suffix, mode);
5608 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5610 void
5611 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5612 machine_mode mode)
5614 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5615 gen_fp_libfunc (optable, name, suffix, mode);
5616 if (INTEGRAL_MODE_P (mode))
5617 gen_int_libfunc (optable, name, suffix, mode);
5620 /* Like gen_libfunc, but verify that FP or INT operation is involved
5621 and add 'v' suffix for integer operation. */
5623 void
5624 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5625 machine_mode mode)
5627 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5628 gen_fp_libfunc (optable, name, suffix, mode);
5629 if (GET_MODE_CLASS (mode) == MODE_INT)
5631 int len = strlen (name);
5632 char *v_name = XALLOCAVEC (char, len + 2);
5633 strcpy (v_name, name);
5634 v_name[len] = 'v';
5635 v_name[len + 1] = 0;
5636 gen_int_libfunc (optable, v_name, suffix, mode);
5640 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5641 involved. */
5643 void
5644 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5645 machine_mode mode)
5647 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5648 gen_fp_libfunc (optable, name, suffix, mode);
5649 if (INTEGRAL_MODE_P (mode))
5650 gen_int_libfunc (optable, name, suffix, mode);
5651 if (ALL_FIXED_POINT_MODE_P (mode))
5652 gen_fixed_libfunc (optable, name, suffix, mode);
5655 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5656 involved. */
5658 void
5659 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5660 machine_mode mode)
5662 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5663 gen_fp_libfunc (optable, name, suffix, mode);
5664 if (INTEGRAL_MODE_P (mode))
5665 gen_int_libfunc (optable, name, suffix, mode);
5666 if (SIGNED_FIXED_POINT_MODE_P (mode))
5667 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5670 /* Like gen_libfunc, but verify that INT or FIXED operation is
5671 involved. */
5673 void
5674 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5675 machine_mode mode)
5677 if (INTEGRAL_MODE_P (mode))
5678 gen_int_libfunc (optable, name, suffix, mode);
5679 if (ALL_FIXED_POINT_MODE_P (mode))
5680 gen_fixed_libfunc (optable, name, suffix, mode);
5683 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5684 involved. */
5686 void
5687 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5688 machine_mode mode)
5690 if (INTEGRAL_MODE_P (mode))
5691 gen_int_libfunc (optable, name, suffix, mode);
5692 if (SIGNED_FIXED_POINT_MODE_P (mode))
5693 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5696 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5697 involved. */
5699 void
5700 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5701 machine_mode mode)
5703 if (INTEGRAL_MODE_P (mode))
5704 gen_int_libfunc (optable, name, suffix, mode);
5705 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5706 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5709 /* Initialize the libfunc fields of an entire group of entries of an
5710 inter-mode-class conversion optab. The string formation rules are
5711 similar to the ones for init_libfuncs, above, but instead of having
5712 a mode name and an operand count these functions have two mode names
5713 and no operand count. */
5715 void
5716 gen_interclass_conv_libfunc (convert_optab tab,
5717 const char *opname,
5718 machine_mode tmode,
5719 machine_mode fmode)
5721 size_t opname_len = strlen (opname);
5722 size_t mname_len = 0;
5724 const char *fname, *tname;
5725 const char *q;
5726 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5727 char *libfunc_name, *suffix;
5728 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5729 char *p;
5731 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5732 depends on which underlying decimal floating point format is used. */
5733 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5735 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5737 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5738 nondec_name[0] = '_';
5739 nondec_name[1] = '_';
5740 if (targetm.libfunc_gnu_prefix)
5742 nondec_name[2] = 'g';
5743 nondec_name[3] = 'n';
5744 nondec_name[4] = 'u';
5745 nondec_name[5] = '_';
5748 memcpy (&nondec_name[prefix_len], opname, opname_len);
5749 nondec_suffix = nondec_name + opname_len + prefix_len;
5751 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5752 dec_name[0] = '_';
5753 dec_name[1] = '_';
5754 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5755 memcpy (&dec_name[2+dec_len], opname, opname_len);
5756 dec_suffix = dec_name + dec_len + opname_len + 2;
5758 fname = GET_MODE_NAME (fmode);
5759 tname = GET_MODE_NAME (tmode);
5761 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5763 libfunc_name = dec_name;
5764 suffix = dec_suffix;
5766 else
5768 libfunc_name = nondec_name;
5769 suffix = nondec_suffix;
5772 p = suffix;
5773 for (q = fname; *q; p++, q++)
5774 *p = TOLOWER (*q);
5775 for (q = tname; *q; p++, q++)
5776 *p = TOLOWER (*q);
5778 *p = '\0';
5780 set_conv_libfunc (tab, tmode, fmode,
5781 ggc_alloc_string (libfunc_name, p - libfunc_name));
5784 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5785 int->fp conversion. */
5787 void
5788 gen_int_to_fp_conv_libfunc (convert_optab tab,
5789 const char *opname,
5790 machine_mode tmode,
5791 machine_mode fmode)
5793 if (GET_MODE_CLASS (fmode) != MODE_INT)
5794 return;
5795 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5796 return;
5797 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5800 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5801 naming scheme. */
5803 void
5804 gen_ufloat_conv_libfunc (convert_optab tab,
5805 const char *opname ATTRIBUTE_UNUSED,
5806 machine_mode tmode,
5807 machine_mode fmode)
5809 if (DECIMAL_FLOAT_MODE_P (tmode))
5810 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5811 else
5812 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5815 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5816 fp->int conversion. */
5818 void
5819 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5820 const char *opname,
5821 machine_mode tmode,
5822 machine_mode fmode)
5824 if (GET_MODE_CLASS (fmode) != MODE_INT)
5825 return;
5826 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5827 return;
5828 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5831 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5832 fp->int conversion with no decimal floating point involved. */
5834 void
5835 gen_fp_to_int_conv_libfunc (convert_optab tab,
5836 const char *opname,
5837 machine_mode tmode,
5838 machine_mode fmode)
5840 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5841 return;
5842 if (GET_MODE_CLASS (tmode) != MODE_INT)
5843 return;
5844 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5847 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5848 The string formation rules are
5849 similar to the ones for init_libfunc, above. */
5851 void
5852 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5853 machine_mode tmode, machine_mode fmode)
5855 size_t opname_len = strlen (opname);
5856 size_t mname_len = 0;
5858 const char *fname, *tname;
5859 const char *q;
5860 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5861 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5862 char *libfunc_name, *suffix;
5863 char *p;
5865 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5866 depends on which underlying decimal floating point format is used. */
5867 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5869 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5871 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5872 nondec_name[0] = '_';
5873 nondec_name[1] = '_';
5874 if (targetm.libfunc_gnu_prefix)
5876 nondec_name[2] = 'g';
5877 nondec_name[3] = 'n';
5878 nondec_name[4] = 'u';
5879 nondec_name[5] = '_';
5881 memcpy (&nondec_name[prefix_len], opname, opname_len);
5882 nondec_suffix = nondec_name + opname_len + prefix_len;
5884 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5885 dec_name[0] = '_';
5886 dec_name[1] = '_';
5887 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5888 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5889 dec_suffix = dec_name + dec_len + opname_len + 2;
5891 fname = GET_MODE_NAME (fmode);
5892 tname = GET_MODE_NAME (tmode);
5894 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5896 libfunc_name = dec_name;
5897 suffix = dec_suffix;
5899 else
5901 libfunc_name = nondec_name;
5902 suffix = nondec_suffix;
5905 p = suffix;
5906 for (q = fname; *q; p++, q++)
5907 *p = TOLOWER (*q);
5908 for (q = tname; *q; p++, q++)
5909 *p = TOLOWER (*q);
5911 *p++ = '2';
5912 *p = '\0';
5914 set_conv_libfunc (tab, tmode, fmode,
5915 ggc_alloc_string (libfunc_name, p - libfunc_name));
5918 /* Pick proper libcall for trunc_optab. We need to chose if we do
5919 truncation or extension and interclass or intraclass. */
5921 void
5922 gen_trunc_conv_libfunc (convert_optab tab,
5923 const char *opname,
5924 machine_mode tmode,
5925 machine_mode fmode)
5927 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5928 return;
5929 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5930 return;
5931 if (tmode == fmode)
5932 return;
5934 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5935 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5936 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5938 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5939 return;
5941 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5942 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5943 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5944 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5947 /* Pick proper libcall for extend_optab. We need to chose if we do
5948 truncation or extension and interclass or intraclass. */
5950 void
5951 gen_extend_conv_libfunc (convert_optab tab,
5952 const char *opname ATTRIBUTE_UNUSED,
5953 machine_mode tmode,
5954 machine_mode fmode)
5956 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5957 return;
5958 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5959 return;
5960 if (tmode == fmode)
5961 return;
5963 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5964 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5965 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5967 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5968 return;
5970 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5971 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5972 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5973 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5976 /* Pick proper libcall for fract_optab. We need to chose if we do
5977 interclass or intraclass. */
5979 void
5980 gen_fract_conv_libfunc (convert_optab tab,
5981 const char *opname,
5982 machine_mode tmode,
5983 machine_mode fmode)
5985 if (tmode == fmode)
5986 return;
5987 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5988 return;
5990 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5991 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5992 else
5993 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5996 /* Pick proper libcall for fractuns_optab. */
5998 void
5999 gen_fractuns_conv_libfunc (convert_optab tab,
6000 const char *opname,
6001 machine_mode tmode,
6002 machine_mode fmode)
6004 if (tmode == fmode)
6005 return;
6006 /* One mode must be a fixed-point mode, and the other must be an integer
6007 mode. */
6008 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
6009 || (ALL_FIXED_POINT_MODE_P (fmode)
6010 && GET_MODE_CLASS (tmode) == MODE_INT)))
6011 return;
6013 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6016 /* Pick proper libcall for satfract_optab. We need to chose if we do
6017 interclass or intraclass. */
6019 void
6020 gen_satfract_conv_libfunc (convert_optab tab,
6021 const char *opname,
6022 machine_mode tmode,
6023 machine_mode fmode)
6025 if (tmode == fmode)
6026 return;
6027 /* TMODE must be a fixed-point mode. */
6028 if (!ALL_FIXED_POINT_MODE_P (tmode))
6029 return;
6031 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6032 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6033 else
6034 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6037 /* Pick proper libcall for satfractuns_optab. */
6039 void
6040 gen_satfractuns_conv_libfunc (convert_optab tab,
6041 const char *opname,
6042 machine_mode tmode,
6043 machine_mode fmode)
6045 if (tmode == fmode)
6046 return;
6047 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
6048 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
6049 return;
6051 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6054 /* Hashtable callbacks for libfunc_decls. */
6056 struct libfunc_decl_hasher : ggc_ptr_hash<tree_node>
6058 static hashval_t
6059 hash (tree entry)
6061 return IDENTIFIER_HASH_VALUE (DECL_NAME (entry));
6064 static bool
6065 equal (tree decl, tree name)
6067 return DECL_NAME (decl) == name;
6071 /* A table of previously-created libfuncs, hashed by name. */
6072 static GTY (()) hash_table<libfunc_decl_hasher> *libfunc_decls;
6074 /* Build a decl for a libfunc named NAME. */
6076 tree
6077 build_libfunc_function (const char *name)
6079 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6080 get_identifier (name),
6081 build_function_type (integer_type_node, NULL_TREE));
6082 /* ??? We don't have any type information except for this is
6083 a function. Pretend this is "int foo()". */
6084 DECL_ARTIFICIAL (decl) = 1;
6085 DECL_EXTERNAL (decl) = 1;
6086 TREE_PUBLIC (decl) = 1;
6087 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6089 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6090 are the flags assigned by targetm.encode_section_info. */
6091 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6093 return decl;
6097 init_one_libfunc (const char *name)
6099 tree id, decl;
6100 hashval_t hash;
6102 if (libfunc_decls == NULL)
6103 libfunc_decls = hash_table<libfunc_decl_hasher>::create_ggc (37);
6105 /* See if we have already created a libfunc decl for this function. */
6106 id = get_identifier (name);
6107 hash = IDENTIFIER_HASH_VALUE (id);
6108 tree *slot = libfunc_decls->find_slot_with_hash (id, hash, INSERT);
6109 decl = *slot;
6110 if (decl == NULL)
6112 /* Create a new decl, so that it can be passed to
6113 targetm.encode_section_info. */
6114 decl = build_libfunc_function (name);
6115 *slot = decl;
6117 return XEXP (DECL_RTL (decl), 0);
6120 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6123 set_user_assembler_libfunc (const char *name, const char *asmspec)
6125 tree id, decl;
6126 hashval_t hash;
6128 id = get_identifier (name);
6129 hash = IDENTIFIER_HASH_VALUE (id);
6130 tree *slot = libfunc_decls->find_slot_with_hash (id, hash, NO_INSERT);
6131 gcc_assert (slot);
6132 decl = (tree) *slot;
6133 set_user_assembler_name (decl, asmspec);
6134 return XEXP (DECL_RTL (decl), 0);
6137 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6138 MODE to NAME, which should be either 0 or a string constant. */
6139 void
6140 set_optab_libfunc (optab op, machine_mode mode, const char *name)
6142 rtx val;
6143 struct libfunc_entry e;
6144 struct libfunc_entry **slot;
6146 e.op = op;
6147 e.mode1 = mode;
6148 e.mode2 = VOIDmode;
6150 if (name)
6151 val = init_one_libfunc (name);
6152 else
6153 val = 0;
6154 slot = libfunc_hash->find_slot (&e, INSERT);
6155 if (*slot == NULL)
6156 *slot = ggc_alloc<libfunc_entry> ();
6157 (*slot)->op = op;
6158 (*slot)->mode1 = mode;
6159 (*slot)->mode2 = VOIDmode;
6160 (*slot)->libfunc = val;
6163 /* Call this to reset the function entry for one conversion optab
6164 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6165 either 0 or a string constant. */
6166 void
6167 set_conv_libfunc (convert_optab optab, machine_mode tmode,
6168 machine_mode fmode, const char *name)
6170 rtx val;
6171 struct libfunc_entry e;
6172 struct libfunc_entry **slot;
6174 e.op = optab;
6175 e.mode1 = tmode;
6176 e.mode2 = fmode;
6178 if (name)
6179 val = init_one_libfunc (name);
6180 else
6181 val = 0;
6182 slot = libfunc_hash->find_slot (&e, INSERT);
6183 if (*slot == NULL)
6184 *slot = ggc_alloc<libfunc_entry> ();
6185 (*slot)->op = optab;
6186 (*slot)->mode1 = tmode;
6187 (*slot)->mode2 = fmode;
6188 (*slot)->libfunc = val;
6191 /* Call this to initialize the contents of the optabs
6192 appropriately for the current target machine. */
6194 void
6195 init_optabs (void)
6197 if (libfunc_hash)
6198 libfunc_hash->empty ();
6199 else
6200 libfunc_hash = hash_table<libfunc_hasher>::create_ggc (10);
6202 /* Fill in the optabs with the insns we support. */
6203 init_all_optabs (this_fn_optabs);
6205 /* The ffs function operates on `int'. Fall back on it if we do not
6206 have a libgcc2 function for that width. */
6207 if (INT_TYPE_SIZE < BITS_PER_WORD)
6208 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6209 "ffs");
6211 /* Explicitly initialize the bswap libfuncs since we need them to be
6212 valid for things other than word_mode. */
6213 if (targetm.libfunc_gnu_prefix)
6215 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6216 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6218 else
6220 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6221 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6224 /* Use cabs for double complex abs, since systems generally have cabs.
6225 Don't define any libcall for float complex, so that cabs will be used. */
6226 if (complex_double_type_node)
6227 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node),
6228 "cabs");
6230 abort_libfunc = init_one_libfunc ("abort");
6231 memcpy_libfunc = init_one_libfunc ("memcpy");
6232 memmove_libfunc = init_one_libfunc ("memmove");
6233 memcmp_libfunc = init_one_libfunc ("memcmp");
6234 memset_libfunc = init_one_libfunc ("memset");
6235 setbits_libfunc = init_one_libfunc ("__setbits");
6237 #ifndef DONT_USE_BUILTIN_SETJMP
6238 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6239 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6240 #else
6241 setjmp_libfunc = init_one_libfunc ("setjmp");
6242 longjmp_libfunc = init_one_libfunc ("longjmp");
6243 #endif
6244 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6245 unwind_sjlj_unregister_libfunc
6246 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6248 /* For function entry/exit instrumentation. */
6249 profile_function_entry_libfunc
6250 = init_one_libfunc ("__cyg_profile_func_enter");
6251 profile_function_exit_libfunc
6252 = init_one_libfunc ("__cyg_profile_func_exit");
6254 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6256 /* Allow the target to add more libcalls or rename some, etc. */
6257 targetm.init_libfuncs ();
6260 /* Use the current target and options to initialize
6261 TREE_OPTIMIZATION_OPTABS (OPTNODE). */
6263 void
6264 init_tree_optimization_optabs (tree optnode)
6266 /* Quick exit if we have already computed optabs for this target. */
6267 if (TREE_OPTIMIZATION_BASE_OPTABS (optnode) == this_target_optabs)
6268 return;
6270 /* Forget any previous information and set up for the current target. */
6271 TREE_OPTIMIZATION_BASE_OPTABS (optnode) = this_target_optabs;
6272 struct target_optabs *tmp_optabs = (struct target_optabs *)
6273 TREE_OPTIMIZATION_OPTABS (optnode);
6274 if (tmp_optabs)
6275 memset (tmp_optabs, 0, sizeof (struct target_optabs));
6276 else
6277 tmp_optabs = ggc_alloc<target_optabs> ();
6279 /* Generate a new set of optabs into tmp_optabs. */
6280 init_all_optabs (tmp_optabs);
6282 /* If the optabs changed, record it. */
6283 if (memcmp (tmp_optabs, this_target_optabs, sizeof (struct target_optabs)))
6284 TREE_OPTIMIZATION_OPTABS (optnode) = tmp_optabs;
6285 else
6287 TREE_OPTIMIZATION_OPTABS (optnode) = NULL;
6288 ggc_free (tmp_optabs);
6292 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6293 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6295 static void
6296 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6298 machine_mode mode;
6299 char buf[64];
6300 size_t len = strlen (base);
6301 int i;
6303 gcc_assert (max <= 8);
6304 gcc_assert (len + 3 < sizeof (buf));
6306 memcpy (buf, base, len);
6307 buf[len] = '_';
6308 buf[len + 1] = '0';
6309 buf[len + 2] = '\0';
6311 mode = QImode;
6312 for (i = 1; i <= max; i *= 2)
6314 buf[len + 1] = '0' + i;
6315 set_optab_libfunc (tab, mode, buf);
6316 mode = GET_MODE_2XWIDER_MODE (mode);
6320 void
6321 init_sync_libfuncs (int max)
6323 if (!flag_sync_libcalls)
6324 return;
6326 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6327 "__sync_val_compare_and_swap", max);
6328 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6329 "__sync_lock_test_and_set", max);
6331 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6332 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6333 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6334 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6335 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6336 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6338 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6339 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6340 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6341 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6342 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6343 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6346 /* Print information about the current contents of the optabs on
6347 STDERR. */
6349 DEBUG_FUNCTION void
6350 debug_optab_libfuncs (void)
6352 int i, j, k;
6354 /* Dump the arithmetic optabs. */
6355 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
6356 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6358 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
6359 if (l)
6361 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6362 fprintf (stderr, "%s\t%s:\t%s\n",
6363 GET_RTX_NAME (optab_to_code ((optab) i)),
6364 GET_MODE_NAME (j),
6365 XSTR (l, 0));
6369 /* Dump the conversion optabs. */
6370 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
6371 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6372 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6374 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
6375 (machine_mode) k);
6376 if (l)
6378 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6379 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6380 GET_RTX_NAME (optab_to_code ((optab) i)),
6381 GET_MODE_NAME (j),
6382 GET_MODE_NAME (k),
6383 XSTR (l, 0));
6389 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6390 CODE. Return 0 on failure. */
6392 rtx_insn *
6393 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6395 machine_mode mode = GET_MODE (op1);
6396 enum insn_code icode;
6397 rtx_insn *insn;
6398 rtx trap_rtx;
6400 if (mode == VOIDmode)
6401 return 0;
6403 icode = optab_handler (ctrap_optab, mode);
6404 if (icode == CODE_FOR_nothing)
6405 return 0;
6407 /* Some targets only accept a zero trap code. */
6408 if (!insn_operand_matches (icode, 3, tcode))
6409 return 0;
6411 do_pending_stack_adjust ();
6412 start_sequence ();
6413 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6414 &trap_rtx, &mode);
6415 if (!trap_rtx)
6416 insn = NULL;
6417 else
6418 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6419 tcode);
6421 /* If that failed, then give up. */
6422 if (insn == 0)
6424 end_sequence ();
6425 return 0;
6428 emit_insn (insn);
6429 insn = get_insns ();
6430 end_sequence ();
6431 return insn;
6434 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6435 or unsigned operation code. */
6437 enum rtx_code
6438 get_rtx_code (enum tree_code tcode, bool unsignedp)
6440 enum rtx_code code;
6441 switch (tcode)
6443 case EQ_EXPR:
6444 code = EQ;
6445 break;
6446 case NE_EXPR:
6447 code = NE;
6448 break;
6449 case LT_EXPR:
6450 code = unsignedp ? LTU : LT;
6451 break;
6452 case LE_EXPR:
6453 code = unsignedp ? LEU : LE;
6454 break;
6455 case GT_EXPR:
6456 code = unsignedp ? GTU : GT;
6457 break;
6458 case GE_EXPR:
6459 code = unsignedp ? GEU : GE;
6460 break;
6462 case UNORDERED_EXPR:
6463 code = UNORDERED;
6464 break;
6465 case ORDERED_EXPR:
6466 code = ORDERED;
6467 break;
6468 case UNLT_EXPR:
6469 code = UNLT;
6470 break;
6471 case UNLE_EXPR:
6472 code = UNLE;
6473 break;
6474 case UNGT_EXPR:
6475 code = UNGT;
6476 break;
6477 case UNGE_EXPR:
6478 code = UNGE;
6479 break;
6480 case UNEQ_EXPR:
6481 code = UNEQ;
6482 break;
6483 case LTGT_EXPR:
6484 code = LTGT;
6485 break;
6487 case BIT_AND_EXPR:
6488 code = AND;
6489 break;
6491 case BIT_IOR_EXPR:
6492 code = IOR;
6493 break;
6495 default:
6496 gcc_unreachable ();
6498 return code;
6501 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6502 unsigned operators. Do not generate compare instruction. */
6504 static rtx
6505 vector_compare_rtx (enum tree_code tcode, tree t_op0, tree t_op1,
6506 bool unsignedp, enum insn_code icode)
6508 struct expand_operand ops[2];
6509 rtx rtx_op0, rtx_op1;
6510 machine_mode m0, m1;
6511 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
6513 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
6515 /* Expand operands. For vector types with scalar modes, e.g. where int64x1_t
6516 has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
6517 cases, use the original mode. */
6518 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6519 EXPAND_STACK_PARM);
6520 m0 = GET_MODE (rtx_op0);
6521 if (m0 == VOIDmode)
6522 m0 = TYPE_MODE (TREE_TYPE (t_op0));
6524 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6525 EXPAND_STACK_PARM);
6526 m1 = GET_MODE (rtx_op1);
6527 if (m1 == VOIDmode)
6528 m1 = TYPE_MODE (TREE_TYPE (t_op1));
6530 create_input_operand (&ops[0], rtx_op0, m0);
6531 create_input_operand (&ops[1], rtx_op1, m1);
6532 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6533 gcc_unreachable ();
6534 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6537 /* Return true if VEC_PERM_EXPR of arbitrary input vectors can be expanded using
6538 SIMD extensions of the CPU. SEL may be NULL, which stands for an unknown
6539 constant. Note that additional permutations representing whole-vector shifts
6540 may also be handled via the vec_shr optab, but only where the second input
6541 vector is entirely constant zeroes; this case is not dealt with here. */
6543 bool
6544 can_vec_perm_p (machine_mode mode, bool variable,
6545 const unsigned char *sel)
6547 machine_mode qimode;
6549 /* If the target doesn't implement a vector mode for the vector type,
6550 then no operations are supported. */
6551 if (!VECTOR_MODE_P (mode))
6552 return false;
6554 if (!variable)
6556 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6557 && (sel == NULL
6558 || targetm.vectorize.vec_perm_const_ok == NULL
6559 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6560 return true;
6563 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6564 return true;
6566 /* We allow fallback to a QI vector mode, and adjust the mask. */
6567 if (GET_MODE_INNER (mode) == QImode)
6568 return false;
6569 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6570 if (!VECTOR_MODE_P (qimode))
6571 return false;
6573 /* ??? For completeness, we ought to check the QImode version of
6574 vec_perm_const_optab. But all users of this implicit lowering
6575 feature implement the variable vec_perm_optab. */
6576 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6577 return false;
6579 /* In order to support the lowering of variable permutations,
6580 we need to support shifts and adds. */
6581 if (variable)
6583 if (GET_MODE_UNIT_SIZE (mode) > 2
6584 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6585 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6586 return false;
6587 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6588 return false;
6591 return true;
6594 /* Checks if vec_perm mask SEL is a constant equivalent to a shift of the first
6595 vec_perm operand, assuming the second operand is a constant vector of zeroes.
6596 Return the shift distance in bits if so, or NULL_RTX if the vec_perm is not a
6597 shift. */
6598 static rtx
6599 shift_amt_for_vec_perm_mask (rtx sel)
6601 unsigned int i, first, nelt = GET_MODE_NUNITS (GET_MODE (sel));
6602 unsigned int bitsize = GET_MODE_UNIT_BITSIZE (GET_MODE (sel));
6604 if (GET_CODE (sel) != CONST_VECTOR)
6605 return NULL_RTX;
6607 first = INTVAL (CONST_VECTOR_ELT (sel, 0));
6608 if (first >= 2*nelt)
6609 return NULL_RTX;
6610 for (i = 1; i < nelt; i++)
6612 int idx = INTVAL (CONST_VECTOR_ELT (sel, i));
6613 unsigned int expected = (i + first) & (2 * nelt - 1);
6614 /* Indices into the second vector are all equivalent. */
6615 if (idx < 0 || (MIN (nelt, (unsigned) idx) != MIN (nelt, expected)))
6616 return NULL_RTX;
6619 return GEN_INT (first * bitsize);
6622 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
6624 static rtx
6625 expand_vec_perm_1 (enum insn_code icode, rtx target,
6626 rtx v0, rtx v1, rtx sel)
6628 machine_mode tmode = GET_MODE (target);
6629 machine_mode smode = GET_MODE (sel);
6630 struct expand_operand ops[4];
6632 create_output_operand (&ops[0], target, tmode);
6633 create_input_operand (&ops[3], sel, smode);
6635 /* Make an effort to preserve v0 == v1. The target expander is able to
6636 rely on this to determine if we're permuting a single input operand. */
6637 if (rtx_equal_p (v0, v1))
6639 if (!insn_operand_matches (icode, 1, v0))
6640 v0 = force_reg (tmode, v0);
6641 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6642 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6644 create_fixed_operand (&ops[1], v0);
6645 create_fixed_operand (&ops[2], v0);
6647 else
6649 create_input_operand (&ops[1], v0, tmode);
6650 /* See if this can be handled with a vec_shr. We only do this if the
6651 second vector is all zeroes. */
6652 enum insn_code shift_code = optab_handler (vec_shr_optab, GET_MODE (v0));
6653 if (v1 == CONST0_RTX (GET_MODE (v1)) && shift_code)
6654 if (rtx shift_amt = shift_amt_for_vec_perm_mask (sel))
6656 create_convert_operand_from_type (&ops[2], shift_amt,
6657 sizetype_tab[(int) stk_sizetype]);
6658 if (maybe_expand_insn (shift_code, 3, ops))
6659 return ops[0].value;
6661 create_input_operand (&ops[2], v1, tmode);
6664 if (maybe_expand_insn (icode, 4, ops))
6665 return ops[0].value;
6666 return NULL_RTX;
6669 /* Generate instructions for vec_perm optab given its mode
6670 and three operands. */
6673 expand_vec_perm (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6675 enum insn_code icode;
6676 machine_mode qimode;
6677 unsigned int i, w, e, u;
6678 rtx tmp, sel_qi = NULL;
6679 rtvec vec;
6681 if (!target || GET_MODE (target) != mode)
6682 target = gen_reg_rtx (mode);
6684 w = GET_MODE_SIZE (mode);
6685 e = GET_MODE_NUNITS (mode);
6686 u = GET_MODE_UNIT_SIZE (mode);
6688 /* Set QIMODE to a different vector mode with byte elements.
6689 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6690 qimode = VOIDmode;
6691 if (GET_MODE_INNER (mode) != QImode)
6693 qimode = mode_for_vector (QImode, w);
6694 if (!VECTOR_MODE_P (qimode))
6695 qimode = VOIDmode;
6698 /* If the input is a constant, expand it specially. */
6699 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6700 if (GET_CODE (sel) == CONST_VECTOR)
6702 icode = direct_optab_handler (vec_perm_const_optab, mode);
6703 if (icode != CODE_FOR_nothing)
6705 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6706 if (tmp)
6707 return tmp;
6710 /* Fall back to a constant byte-based permutation. */
6711 if (qimode != VOIDmode)
6713 vec = rtvec_alloc (w);
6714 for (i = 0; i < e; ++i)
6716 unsigned int j, this_e;
6718 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6719 this_e &= 2 * e - 1;
6720 this_e *= u;
6722 for (j = 0; j < u; ++j)
6723 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
6725 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
6727 icode = direct_optab_handler (vec_perm_const_optab, qimode);
6728 if (icode != CODE_FOR_nothing)
6730 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6731 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6732 gen_lowpart (qimode, v1), sel_qi);
6733 if (tmp)
6734 return gen_lowpart (mode, tmp);
6739 /* Otherwise expand as a fully variable permuation. */
6740 icode = direct_optab_handler (vec_perm_optab, mode);
6741 if (icode != CODE_FOR_nothing)
6743 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6744 if (tmp)
6745 return tmp;
6748 /* As a special case to aid several targets, lower the element-based
6749 permutation to a byte-based permutation and try again. */
6750 if (qimode == VOIDmode)
6751 return NULL_RTX;
6752 icode = direct_optab_handler (vec_perm_optab, qimode);
6753 if (icode == CODE_FOR_nothing)
6754 return NULL_RTX;
6756 if (sel_qi == NULL)
6758 /* Multiply each element by its byte size. */
6759 machine_mode selmode = GET_MODE (sel);
6760 if (u == 2)
6761 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6762 NULL, 0, OPTAB_DIRECT);
6763 else
6764 sel = expand_simple_binop (selmode, ASHIFT, sel,
6765 GEN_INT (exact_log2 (u)),
6766 NULL, 0, OPTAB_DIRECT);
6767 gcc_assert (sel != NULL);
6769 /* Broadcast the low byte each element into each of its bytes. */
6770 vec = rtvec_alloc (w);
6771 for (i = 0; i < w; ++i)
6773 int this_e = i / u * u;
6774 if (BYTES_BIG_ENDIAN)
6775 this_e += u - 1;
6776 RTVEC_ELT (vec, i) = GEN_INT (this_e);
6778 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6779 sel = gen_lowpart (qimode, sel);
6780 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
6781 gcc_assert (sel != NULL);
6783 /* Add the byte offset to each byte element. */
6784 /* Note that the definition of the indicies here is memory ordering,
6785 so there should be no difference between big and little endian. */
6786 vec = rtvec_alloc (w);
6787 for (i = 0; i < w; ++i)
6788 RTVEC_ELT (vec, i) = GEN_INT (i % u);
6789 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6790 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6791 sel, 0, OPTAB_DIRECT);
6792 gcc_assert (sel_qi != NULL);
6795 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6796 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6797 gen_lowpart (qimode, v1), sel_qi);
6798 if (tmp)
6799 tmp = gen_lowpart (mode, tmp);
6800 return tmp;
6803 /* Return insn code for a conditional operator with a comparison in
6804 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
6806 static inline enum insn_code
6807 get_vcond_icode (machine_mode vmode, machine_mode cmode, bool uns)
6809 enum insn_code icode = CODE_FOR_nothing;
6810 if (uns)
6811 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
6812 else
6813 icode = convert_optab_handler (vcond_optab, vmode, cmode);
6814 return icode;
6817 /* Return TRUE iff, appropriate vector insns are available
6818 for vector cond expr with vector type VALUE_TYPE and a comparison
6819 with operand vector types in CMP_OP_TYPE. */
6821 bool
6822 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
6824 machine_mode value_mode = TYPE_MODE (value_type);
6825 machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
6826 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
6827 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
6828 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
6829 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
6830 return false;
6831 return true;
6834 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6835 three operands. */
6838 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6839 rtx target)
6841 struct expand_operand ops[6];
6842 enum insn_code icode;
6843 rtx comparison, rtx_op1, rtx_op2;
6844 machine_mode mode = TYPE_MODE (vec_cond_type);
6845 machine_mode cmp_op_mode;
6846 bool unsignedp;
6847 tree op0a, op0b;
6848 enum tree_code tcode;
6850 if (COMPARISON_CLASS_P (op0))
6852 op0a = TREE_OPERAND (op0, 0);
6853 op0b = TREE_OPERAND (op0, 1);
6854 tcode = TREE_CODE (op0);
6856 else
6858 /* Fake op0 < 0. */
6859 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (op0)));
6860 op0a = op0;
6861 op0b = build_zero_cst (TREE_TYPE (op0));
6862 tcode = LT_EXPR;
6864 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6865 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
6868 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
6869 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
6871 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
6872 if (icode == CODE_FOR_nothing)
6873 return 0;
6875 comparison = vector_compare_rtx (tcode, op0a, op0b, unsignedp, icode);
6876 rtx_op1 = expand_normal (op1);
6877 rtx_op2 = expand_normal (op2);
6879 create_output_operand (&ops[0], target, mode);
6880 create_input_operand (&ops[1], rtx_op1, mode);
6881 create_input_operand (&ops[2], rtx_op2, mode);
6882 create_fixed_operand (&ops[3], comparison);
6883 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6884 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6885 expand_insn (icode, 6, ops);
6886 return ops[0].value;
6889 /* Return non-zero if a highpart multiply is supported of can be synthisized.
6890 For the benefit of expand_mult_highpart, the return value is 1 for direct,
6891 2 for even/odd widening, and 3 for hi/lo widening. */
6894 can_mult_highpart_p (machine_mode mode, bool uns_p)
6896 optab op;
6897 unsigned char *sel;
6898 unsigned i, nunits;
6900 op = uns_p ? umul_highpart_optab : smul_highpart_optab;
6901 if (optab_handler (op, mode) != CODE_FOR_nothing)
6902 return 1;
6904 /* If the mode is an integral vector, synth from widening operations. */
6905 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
6906 return 0;
6908 nunits = GET_MODE_NUNITS (mode);
6909 sel = XALLOCAVEC (unsigned char, nunits);
6911 op = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6912 if (optab_handler (op, mode) != CODE_FOR_nothing)
6914 op = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6915 if (optab_handler (op, mode) != CODE_FOR_nothing)
6917 for (i = 0; i < nunits; ++i)
6918 sel[i] = !BYTES_BIG_ENDIAN + (i & ~1) + ((i & 1) ? nunits : 0);
6919 if (can_vec_perm_p (mode, false, sel))
6920 return 2;
6924 op = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6925 if (optab_handler (op, mode) != CODE_FOR_nothing)
6927 op = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6928 if (optab_handler (op, mode) != CODE_FOR_nothing)
6930 for (i = 0; i < nunits; ++i)
6931 sel[i] = 2 * i + (BYTES_BIG_ENDIAN ? 0 : 1);
6932 if (can_vec_perm_p (mode, false, sel))
6933 return 3;
6937 return 0;
6940 /* Expand a highpart multiply. */
6943 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
6944 rtx target, bool uns_p)
6946 struct expand_operand eops[3];
6947 enum insn_code icode;
6948 int method, i, nunits;
6949 machine_mode wmode;
6950 rtx m1, m2, perm;
6951 optab tab1, tab2;
6952 rtvec v;
6954 method = can_mult_highpart_p (mode, uns_p);
6955 switch (method)
6957 case 0:
6958 return NULL_RTX;
6959 case 1:
6960 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6961 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6962 OPTAB_LIB_WIDEN);
6963 case 2:
6964 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6965 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6966 break;
6967 case 3:
6968 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6969 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6970 if (BYTES_BIG_ENDIAN)
6971 std::swap (tab1, tab2);
6972 break;
6973 default:
6974 gcc_unreachable ();
6977 icode = optab_handler (tab1, mode);
6978 nunits = GET_MODE_NUNITS (mode);
6979 wmode = insn_data[icode].operand[0].mode;
6980 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
6981 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
6983 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6984 create_input_operand (&eops[1], op0, mode);
6985 create_input_operand (&eops[2], op1, mode);
6986 expand_insn (icode, 3, eops);
6987 m1 = gen_lowpart (mode, eops[0].value);
6989 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6990 create_input_operand (&eops[1], op0, mode);
6991 create_input_operand (&eops[2], op1, mode);
6992 expand_insn (optab_handler (tab2, mode), 3, eops);
6993 m2 = gen_lowpart (mode, eops[0].value);
6995 v = rtvec_alloc (nunits);
6996 if (method == 2)
6998 for (i = 0; i < nunits; ++i)
6999 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
7000 + ((i & 1) ? nunits : 0));
7002 else
7004 for (i = 0; i < nunits; ++i)
7005 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
7007 perm = gen_rtx_CONST_VECTOR (mode, v);
7009 return expand_vec_perm (mode, m1, m2, perm, target);
7012 /* Return true if target supports vector masked load/store for mode. */
7013 bool
7014 can_vec_mask_load_store_p (machine_mode mode, bool is_load)
7016 optab op = is_load ? maskload_optab : maskstore_optab;
7017 machine_mode vmode;
7018 unsigned int vector_sizes;
7020 /* If mode is vector mode, check it directly. */
7021 if (VECTOR_MODE_P (mode))
7022 return optab_handler (op, mode) != CODE_FOR_nothing;
7024 /* Otherwise, return true if there is some vector mode with
7025 the mask load/store supported. */
7027 /* See if there is any chance the mask load or store might be
7028 vectorized. If not, punt. */
7029 vmode = targetm.vectorize.preferred_simd_mode (mode);
7030 if (!VECTOR_MODE_P (vmode))
7031 return false;
7033 if (optab_handler (op, vmode) != CODE_FOR_nothing)
7034 return true;
7036 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
7037 while (vector_sizes != 0)
7039 unsigned int cur = 1 << floor_log2 (vector_sizes);
7040 vector_sizes &= ~cur;
7041 if (cur <= GET_MODE_SIZE (mode))
7042 continue;
7043 vmode = mode_for_vector (mode, cur / GET_MODE_SIZE (mode));
7044 if (VECTOR_MODE_P (vmode)
7045 && optab_handler (op, vmode) != CODE_FOR_nothing)
7046 return true;
7048 return false;
7051 /* Return true if there is a compare_and_swap pattern. */
7053 bool
7054 can_compare_and_swap_p (machine_mode mode, bool allow_libcall)
7056 enum insn_code icode;
7058 /* Check for __atomic_compare_and_swap. */
7059 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7060 if (icode != CODE_FOR_nothing)
7061 return true;
7063 /* Check for __sync_compare_and_swap. */
7064 icode = optab_handler (sync_compare_and_swap_optab, mode);
7065 if (icode != CODE_FOR_nothing)
7066 return true;
7067 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
7068 return true;
7070 /* No inline compare and swap. */
7071 return false;
7074 /* Return true if an atomic exchange can be performed. */
7076 bool
7077 can_atomic_exchange_p (machine_mode mode, bool allow_libcall)
7079 enum insn_code icode;
7081 /* Check for __atomic_exchange. */
7082 icode = direct_optab_handler (atomic_exchange_optab, mode);
7083 if (icode != CODE_FOR_nothing)
7084 return true;
7086 /* Don't check __sync_test_and_set, as on some platforms that
7087 has reduced functionality. Targets that really do support
7088 a proper exchange should simply be updated to the __atomics. */
7090 return can_compare_and_swap_p (mode, allow_libcall);
7094 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
7095 pattern. */
7097 static void
7098 find_cc_set (rtx x, const_rtx pat, void *data)
7100 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
7101 && GET_CODE (pat) == SET)
7103 rtx *p_cc_reg = (rtx *) data;
7104 gcc_assert (!*p_cc_reg);
7105 *p_cc_reg = x;
7109 /* This is a helper function for the other atomic operations. This function
7110 emits a loop that contains SEQ that iterates until a compare-and-swap
7111 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7112 a set of instructions that takes a value from OLD_REG as an input and
7113 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7114 set to the current contents of MEM. After SEQ, a compare-and-swap will
7115 attempt to update MEM with NEW_REG. The function returns true when the
7116 loop was generated successfully. */
7118 static bool
7119 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7121 machine_mode mode = GET_MODE (mem);
7122 rtx_code_label *label;
7123 rtx cmp_reg, success, oldval;
7125 /* The loop we want to generate looks like
7127 cmp_reg = mem;
7128 label:
7129 old_reg = cmp_reg;
7130 seq;
7131 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
7132 if (success)
7133 goto label;
7135 Note that we only do the plain load from memory once. Subsequent
7136 iterations use the value loaded by the compare-and-swap pattern. */
7138 label = gen_label_rtx ();
7139 cmp_reg = gen_reg_rtx (mode);
7141 emit_move_insn (cmp_reg, mem);
7142 emit_label (label);
7143 emit_move_insn (old_reg, cmp_reg);
7144 if (seq)
7145 emit_insn (seq);
7147 success = NULL_RTX;
7148 oldval = cmp_reg;
7149 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
7150 new_reg, false, MEMMODEL_SYNC_SEQ_CST,
7151 MEMMODEL_RELAXED))
7152 return false;
7154 if (oldval != cmp_reg)
7155 emit_move_insn (cmp_reg, oldval);
7157 /* Mark this jump predicted not taken. */
7158 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
7159 GET_MODE (success), 1, label, 0);
7160 return true;
7164 /* This function tries to emit an atomic_exchange intruction. VAL is written
7165 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
7166 using TARGET if possible. */
7168 static rtx
7169 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7171 machine_mode mode = GET_MODE (mem);
7172 enum insn_code icode;
7174 /* If the target supports the exchange directly, great. */
7175 icode = direct_optab_handler (atomic_exchange_optab, mode);
7176 if (icode != CODE_FOR_nothing)
7178 struct expand_operand ops[4];
7180 create_output_operand (&ops[0], target, mode);
7181 create_fixed_operand (&ops[1], mem);
7182 create_input_operand (&ops[2], val, mode);
7183 create_integer_operand (&ops[3], model);
7184 if (maybe_expand_insn (icode, 4, ops))
7185 return ops[0].value;
7188 return NULL_RTX;
7191 /* This function tries to implement an atomic exchange operation using
7192 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7193 The previous contents of *MEM are returned, using TARGET if possible.
7194 Since this instructionn is an acquire barrier only, stronger memory
7195 models may require additional barriers to be emitted. */
7197 static rtx
7198 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7199 enum memmodel model)
7201 machine_mode mode = GET_MODE (mem);
7202 enum insn_code icode;
7203 rtx_insn *last_insn = get_last_insn ();
7205 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7207 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7208 exists, and the memory model is stronger than acquire, add a release
7209 barrier before the instruction. */
7211 if (is_mm_seq_cst (model) || is_mm_release (model) || is_mm_acq_rel (model))
7212 expand_mem_thread_fence (model);
7214 if (icode != CODE_FOR_nothing)
7216 struct expand_operand ops[3];
7217 create_output_operand (&ops[0], target, mode);
7218 create_fixed_operand (&ops[1], mem);
7219 create_input_operand (&ops[2], val, mode);
7220 if (maybe_expand_insn (icode, 3, ops))
7221 return ops[0].value;
7224 /* If an external test-and-set libcall is provided, use that instead of
7225 any external compare-and-swap that we might get from the compare-and-
7226 swap-loop expansion later. */
7227 if (!can_compare_and_swap_p (mode, false))
7229 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7230 if (libfunc != NULL)
7232 rtx addr;
7234 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7235 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7236 mode, 2, addr, ptr_mode,
7237 val, mode);
7241 /* If the test_and_set can't be emitted, eliminate any barrier that might
7242 have been emitted. */
7243 delete_insns_since (last_insn);
7244 return NULL_RTX;
7247 /* This function tries to implement an atomic exchange operation using a
7248 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7249 *MEM are returned, using TARGET if possible. No memory model is required
7250 since a compare_and_swap loop is seq-cst. */
7252 static rtx
7253 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7255 machine_mode mode = GET_MODE (mem);
7257 if (can_compare_and_swap_p (mode, true))
7259 if (!target || !register_operand (target, mode))
7260 target = gen_reg_rtx (mode);
7261 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7262 return target;
7265 return NULL_RTX;
7268 /* This function tries to implement an atomic test-and-set operation
7269 using the atomic_test_and_set instruction pattern. A boolean value
7270 is returned from the operation, using TARGET if possible. */
7272 static rtx
7273 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7275 machine_mode pat_bool_mode;
7276 struct expand_operand ops[3];
7278 if (!targetm.have_atomic_test_and_set ())
7279 return NULL_RTX;
7281 /* While we always get QImode from __atomic_test_and_set, we get
7282 other memory modes from __sync_lock_test_and_set. Note that we
7283 use no endian adjustment here. This matches the 4.6 behavior
7284 in the Sparc backend. */
7285 enum insn_code icode = targetm.code_for_atomic_test_and_set;
7286 gcc_checking_assert (insn_data[icode].operand[1].mode == QImode);
7287 if (GET_MODE (mem) != QImode)
7288 mem = adjust_address_nv (mem, QImode, 0);
7290 pat_bool_mode = insn_data[icode].operand[0].mode;
7291 create_output_operand (&ops[0], target, pat_bool_mode);
7292 create_fixed_operand (&ops[1], mem);
7293 create_integer_operand (&ops[2], model);
7295 if (maybe_expand_insn (icode, 3, ops))
7296 return ops[0].value;
7297 return NULL_RTX;
7300 /* This function expands the legacy _sync_lock test_and_set operation which is
7301 generally an atomic exchange. Some limited targets only allow the
7302 constant 1 to be stored. This is an ACQUIRE operation.
7304 TARGET is an optional place to stick the return value.
7305 MEM is where VAL is stored. */
7308 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7310 rtx ret;
7312 /* Try an atomic_exchange first. */
7313 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_SYNC_ACQUIRE);
7314 if (ret)
7315 return ret;
7317 ret = maybe_emit_sync_lock_test_and_set (target, mem, val,
7318 MEMMODEL_SYNC_ACQUIRE);
7319 if (ret)
7320 return ret;
7322 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7323 if (ret)
7324 return ret;
7326 /* If there are no other options, try atomic_test_and_set if the value
7327 being stored is 1. */
7328 if (val == const1_rtx)
7329 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_SYNC_ACQUIRE);
7331 return ret;
7334 /* This function expands the atomic test_and_set operation:
7335 atomically store a boolean TRUE into MEM and return the previous value.
7337 MEMMODEL is the memory model variant to use.
7338 TARGET is an optional place to stick the return value. */
7341 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7343 machine_mode mode = GET_MODE (mem);
7344 rtx ret, trueval, subtarget;
7346 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7347 if (ret)
7348 return ret;
7350 /* Be binary compatible with non-default settings of trueval, and different
7351 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7352 another only has atomic-exchange. */
7353 if (targetm.atomic_test_and_set_trueval == 1)
7355 trueval = const1_rtx;
7356 subtarget = target ? target : gen_reg_rtx (mode);
7358 else
7360 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7361 subtarget = gen_reg_rtx (mode);
7364 /* Try the atomic-exchange optab... */
7365 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7367 /* ... then an atomic-compare-and-swap loop ... */
7368 if (!ret)
7369 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7371 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7372 if (!ret)
7373 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7375 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7376 things with the value 1. Thus we try again without trueval. */
7377 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7378 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7380 /* Failing all else, assume a single threaded environment and simply
7381 perform the operation. */
7382 if (!ret)
7384 /* If the result is ignored skip the move to target. */
7385 if (subtarget != const0_rtx)
7386 emit_move_insn (subtarget, mem);
7388 emit_move_insn (mem, trueval);
7389 ret = subtarget;
7392 /* Recall that have to return a boolean value; rectify if trueval
7393 is not exactly one. */
7394 if (targetm.atomic_test_and_set_trueval != 1)
7395 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7397 return ret;
7400 /* This function expands the atomic exchange operation:
7401 atomically store VAL in MEM and return the previous value in MEM.
7403 MEMMODEL is the memory model variant to use.
7404 TARGET is an optional place to stick the return value. */
7407 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7409 rtx ret;
7411 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7413 /* Next try a compare-and-swap loop for the exchange. */
7414 if (!ret)
7415 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7417 return ret;
7420 /* This function expands the atomic compare exchange operation:
7422 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7423 *PTARGET_OVAL is an optional place to store the old value from memory.
7424 Both target parameters may be NULL to indicate that we do not care about
7425 that return value. Both target parameters are updated on success to
7426 the actual location of the corresponding result.
7428 MEMMODEL is the memory model variant to use.
7430 The return value of the function is true for success. */
7432 bool
7433 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7434 rtx mem, rtx expected, rtx desired,
7435 bool is_weak, enum memmodel succ_model,
7436 enum memmodel fail_model)
7438 machine_mode mode = GET_MODE (mem);
7439 struct expand_operand ops[8];
7440 enum insn_code icode;
7441 rtx target_oval, target_bool = NULL_RTX;
7442 rtx libfunc;
7444 /* Load expected into a register for the compare and swap. */
7445 if (MEM_P (expected))
7446 expected = copy_to_reg (expected);
7448 /* Make sure we always have some place to put the return oldval.
7449 Further, make sure that place is distinct from the input expected,
7450 just in case we need that path down below. */
7451 if (ptarget_oval == NULL
7452 || (target_oval = *ptarget_oval) == NULL
7453 || reg_overlap_mentioned_p (expected, target_oval))
7454 target_oval = gen_reg_rtx (mode);
7456 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7457 if (icode != CODE_FOR_nothing)
7459 machine_mode bool_mode = insn_data[icode].operand[0].mode;
7461 /* Make sure we always have a place for the bool operand. */
7462 if (ptarget_bool == NULL
7463 || (target_bool = *ptarget_bool) == NULL
7464 || GET_MODE (target_bool) != bool_mode)
7465 target_bool = gen_reg_rtx (bool_mode);
7467 /* Emit the compare_and_swap. */
7468 create_output_operand (&ops[0], target_bool, bool_mode);
7469 create_output_operand (&ops[1], target_oval, mode);
7470 create_fixed_operand (&ops[2], mem);
7471 create_input_operand (&ops[3], expected, mode);
7472 create_input_operand (&ops[4], desired, mode);
7473 create_integer_operand (&ops[5], is_weak);
7474 create_integer_operand (&ops[6], succ_model);
7475 create_integer_operand (&ops[7], fail_model);
7476 if (maybe_expand_insn (icode, 8, ops))
7478 /* Return success/failure. */
7479 target_bool = ops[0].value;
7480 target_oval = ops[1].value;
7481 goto success;
7485 /* Otherwise fall back to the original __sync_val_compare_and_swap
7486 which is always seq-cst. */
7487 icode = optab_handler (sync_compare_and_swap_optab, mode);
7488 if (icode != CODE_FOR_nothing)
7490 rtx cc_reg;
7492 create_output_operand (&ops[0], target_oval, mode);
7493 create_fixed_operand (&ops[1], mem);
7494 create_input_operand (&ops[2], expected, mode);
7495 create_input_operand (&ops[3], desired, mode);
7496 if (!maybe_expand_insn (icode, 4, ops))
7497 return false;
7499 target_oval = ops[0].value;
7501 /* If the caller isn't interested in the boolean return value,
7502 skip the computation of it. */
7503 if (ptarget_bool == NULL)
7504 goto success;
7506 /* Otherwise, work out if the compare-and-swap succeeded. */
7507 cc_reg = NULL_RTX;
7508 if (have_insn_for (COMPARE, CCmode))
7509 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7510 if (cc_reg)
7512 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7513 const0_rtx, VOIDmode, 0, 1);
7514 goto success;
7516 goto success_bool_from_val;
7519 /* Also check for library support for __sync_val_compare_and_swap. */
7520 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7521 if (libfunc != NULL)
7523 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7524 rtx target = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7525 mode, 3, addr, ptr_mode,
7526 expected, mode, desired, mode);
7527 emit_move_insn (target_oval, target);
7529 /* Compute the boolean return value only if requested. */
7530 if (ptarget_bool)
7531 goto success_bool_from_val;
7532 else
7533 goto success;
7536 /* Failure. */
7537 return false;
7539 success_bool_from_val:
7540 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7541 expected, VOIDmode, 1, 1);
7542 success:
7543 /* Make sure that the oval output winds up where the caller asked. */
7544 if (ptarget_oval)
7545 *ptarget_oval = target_oval;
7546 if (ptarget_bool)
7547 *ptarget_bool = target_bool;
7548 return true;
7551 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7553 static void
7554 expand_asm_memory_barrier (void)
7556 rtx asm_op, clob;
7558 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7559 rtvec_alloc (0), rtvec_alloc (0),
7560 rtvec_alloc (0), UNKNOWN_LOCATION);
7561 MEM_VOLATILE_P (asm_op) = 1;
7563 clob = gen_rtx_SCRATCH (VOIDmode);
7564 clob = gen_rtx_MEM (BLKmode, clob);
7565 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7567 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7570 /* This routine will either emit the mem_thread_fence pattern or issue a
7571 sync_synchronize to generate a fence for memory model MEMMODEL. */
7573 void
7574 expand_mem_thread_fence (enum memmodel model)
7576 if (targetm.have_mem_thread_fence ())
7577 emit_insn (targetm.gen_mem_thread_fence (GEN_INT (model)));
7578 else if (!is_mm_relaxed (model))
7580 if (targetm.have_memory_barrier ())
7581 emit_insn (targetm.gen_memory_barrier ());
7582 else if (synchronize_libfunc != NULL_RTX)
7583 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7584 else
7585 expand_asm_memory_barrier ();
7589 /* This routine will either emit the mem_signal_fence pattern or issue a
7590 sync_synchronize to generate a fence for memory model MEMMODEL. */
7592 void
7593 expand_mem_signal_fence (enum memmodel model)
7595 if (targetm.have_mem_signal_fence ())
7596 emit_insn (targetm.gen_mem_signal_fence (GEN_INT (model)));
7597 else if (!is_mm_relaxed (model))
7599 /* By default targets are coherent between a thread and the signal
7600 handler running on the same thread. Thus this really becomes a
7601 compiler barrier, in that stores must not be sunk past
7602 (or raised above) a given point. */
7603 expand_asm_memory_barrier ();
7607 /* This function expands the atomic load operation:
7608 return the atomically loaded value in MEM.
7610 MEMMODEL is the memory model variant to use.
7611 TARGET is an option place to stick the return value. */
7614 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7616 machine_mode mode = GET_MODE (mem);
7617 enum insn_code icode;
7619 /* If the target supports the load directly, great. */
7620 icode = direct_optab_handler (atomic_load_optab, mode);
7621 if (icode != CODE_FOR_nothing)
7623 struct expand_operand ops[3];
7625 create_output_operand (&ops[0], target, mode);
7626 create_fixed_operand (&ops[1], mem);
7627 create_integer_operand (&ops[2], model);
7628 if (maybe_expand_insn (icode, 3, ops))
7629 return ops[0].value;
7632 /* If the size of the object is greater than word size on this target,
7633 then we assume that a load will not be atomic. */
7634 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7636 /* Issue val = compare_and_swap (mem, 0, 0).
7637 This may cause the occasional harmless store of 0 when the value is
7638 already 0, but it seems to be OK according to the standards guys. */
7639 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7640 const0_rtx, false, model, model))
7641 return target;
7642 else
7643 /* Otherwise there is no atomic load, leave the library call. */
7644 return NULL_RTX;
7647 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7648 if (!target || target == const0_rtx)
7649 target = gen_reg_rtx (mode);
7651 /* For SEQ_CST, emit a barrier before the load. */
7652 if (is_mm_seq_cst (model))
7653 expand_mem_thread_fence (model);
7655 emit_move_insn (target, mem);
7657 /* Emit the appropriate barrier after the load. */
7658 expand_mem_thread_fence (model);
7660 return target;
7663 /* This function expands the atomic store operation:
7664 Atomically store VAL in MEM.
7665 MEMMODEL is the memory model variant to use.
7666 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7667 function returns const0_rtx if a pattern was emitted. */
7670 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7672 machine_mode mode = GET_MODE (mem);
7673 enum insn_code icode;
7674 struct expand_operand ops[3];
7676 /* If the target supports the store directly, great. */
7677 icode = direct_optab_handler (atomic_store_optab, mode);
7678 if (icode != CODE_FOR_nothing)
7680 create_fixed_operand (&ops[0], mem);
7681 create_input_operand (&ops[1], val, mode);
7682 create_integer_operand (&ops[2], model);
7683 if (maybe_expand_insn (icode, 3, ops))
7684 return const0_rtx;
7687 /* If using __sync_lock_release is a viable alternative, try it. */
7688 if (use_release)
7690 icode = direct_optab_handler (sync_lock_release_optab, mode);
7691 if (icode != CODE_FOR_nothing)
7693 create_fixed_operand (&ops[0], mem);
7694 create_input_operand (&ops[1], const0_rtx, mode);
7695 if (maybe_expand_insn (icode, 2, ops))
7697 /* lock_release is only a release barrier. */
7698 if (is_mm_seq_cst (model))
7699 expand_mem_thread_fence (model);
7700 return const0_rtx;
7705 /* If the size of the object is greater than word size on this target,
7706 a default store will not be atomic, Try a mem_exchange and throw away
7707 the result. If that doesn't work, don't do anything. */
7708 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7710 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7711 if (!target)
7712 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7713 if (target)
7714 return const0_rtx;
7715 else
7716 return NULL_RTX;
7719 /* Otherwise assume stores are atomic, and emit the proper barriers. */
7720 expand_mem_thread_fence (model);
7722 emit_move_insn (mem, val);
7724 /* For SEQ_CST, also emit a barrier after the store. */
7725 if (is_mm_seq_cst (model))
7726 expand_mem_thread_fence (model);
7728 return const0_rtx;
7732 /* Structure containing the pointers and values required to process the
7733 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7735 struct atomic_op_functions
7737 direct_optab mem_fetch_before;
7738 direct_optab mem_fetch_after;
7739 direct_optab mem_no_result;
7740 optab fetch_before;
7741 optab fetch_after;
7742 direct_optab no_result;
7743 enum rtx_code reverse_code;
7747 /* Fill in structure pointed to by OP with the various optab entries for an
7748 operation of type CODE. */
7750 static void
7751 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7753 gcc_assert (op!= NULL);
7755 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7756 in the source code during compilation, and the optab entries are not
7757 computable until runtime. Fill in the values at runtime. */
7758 switch (code)
7760 case PLUS:
7761 op->mem_fetch_before = atomic_fetch_add_optab;
7762 op->mem_fetch_after = atomic_add_fetch_optab;
7763 op->mem_no_result = atomic_add_optab;
7764 op->fetch_before = sync_old_add_optab;
7765 op->fetch_after = sync_new_add_optab;
7766 op->no_result = sync_add_optab;
7767 op->reverse_code = MINUS;
7768 break;
7769 case MINUS:
7770 op->mem_fetch_before = atomic_fetch_sub_optab;
7771 op->mem_fetch_after = atomic_sub_fetch_optab;
7772 op->mem_no_result = atomic_sub_optab;
7773 op->fetch_before = sync_old_sub_optab;
7774 op->fetch_after = sync_new_sub_optab;
7775 op->no_result = sync_sub_optab;
7776 op->reverse_code = PLUS;
7777 break;
7778 case XOR:
7779 op->mem_fetch_before = atomic_fetch_xor_optab;
7780 op->mem_fetch_after = atomic_xor_fetch_optab;
7781 op->mem_no_result = atomic_xor_optab;
7782 op->fetch_before = sync_old_xor_optab;
7783 op->fetch_after = sync_new_xor_optab;
7784 op->no_result = sync_xor_optab;
7785 op->reverse_code = XOR;
7786 break;
7787 case AND:
7788 op->mem_fetch_before = atomic_fetch_and_optab;
7789 op->mem_fetch_after = atomic_and_fetch_optab;
7790 op->mem_no_result = atomic_and_optab;
7791 op->fetch_before = sync_old_and_optab;
7792 op->fetch_after = sync_new_and_optab;
7793 op->no_result = sync_and_optab;
7794 op->reverse_code = UNKNOWN;
7795 break;
7796 case IOR:
7797 op->mem_fetch_before = atomic_fetch_or_optab;
7798 op->mem_fetch_after = atomic_or_fetch_optab;
7799 op->mem_no_result = atomic_or_optab;
7800 op->fetch_before = sync_old_ior_optab;
7801 op->fetch_after = sync_new_ior_optab;
7802 op->no_result = sync_ior_optab;
7803 op->reverse_code = UNKNOWN;
7804 break;
7805 case NOT:
7806 op->mem_fetch_before = atomic_fetch_nand_optab;
7807 op->mem_fetch_after = atomic_nand_fetch_optab;
7808 op->mem_no_result = atomic_nand_optab;
7809 op->fetch_before = sync_old_nand_optab;
7810 op->fetch_after = sync_new_nand_optab;
7811 op->no_result = sync_nand_optab;
7812 op->reverse_code = UNKNOWN;
7813 break;
7814 default:
7815 gcc_unreachable ();
7819 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7820 using memory order MODEL. If AFTER is true the operation needs to return
7821 the value of *MEM after the operation, otherwise the previous value.
7822 TARGET is an optional place to place the result. The result is unused if
7823 it is const0_rtx.
7824 Return the result if there is a better sequence, otherwise NULL_RTX. */
7826 static rtx
7827 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7828 enum memmodel model, bool after)
7830 /* If the value is prefetched, or not used, it may be possible to replace
7831 the sequence with a native exchange operation. */
7832 if (!after || target == const0_rtx)
7834 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7835 if (code == AND && val == const0_rtx)
7837 if (target == const0_rtx)
7838 target = gen_reg_rtx (GET_MODE (mem));
7839 return maybe_emit_atomic_exchange (target, mem, val, model);
7842 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7843 if (code == IOR && val == constm1_rtx)
7845 if (target == const0_rtx)
7846 target = gen_reg_rtx (GET_MODE (mem));
7847 return maybe_emit_atomic_exchange (target, mem, val, model);
7851 return NULL_RTX;
7854 /* Try to emit an instruction for a specific operation varaition.
7855 OPTAB contains the OP functions.
7856 TARGET is an optional place to return the result. const0_rtx means unused.
7857 MEM is the memory location to operate on.
7858 VAL is the value to use in the operation.
7859 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7860 MODEL is the memory model, if used.
7861 AFTER is true if the returned result is the value after the operation. */
7863 static rtx
7864 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7865 rtx val, bool use_memmodel, enum memmodel model, bool after)
7867 machine_mode mode = GET_MODE (mem);
7868 struct expand_operand ops[4];
7869 enum insn_code icode;
7870 int op_counter = 0;
7871 int num_ops;
7873 /* Check to see if there is a result returned. */
7874 if (target == const0_rtx)
7876 if (use_memmodel)
7878 icode = direct_optab_handler (optab->mem_no_result, mode);
7879 create_integer_operand (&ops[2], model);
7880 num_ops = 3;
7882 else
7884 icode = direct_optab_handler (optab->no_result, mode);
7885 num_ops = 2;
7888 /* Otherwise, we need to generate a result. */
7889 else
7891 if (use_memmodel)
7893 icode = direct_optab_handler (after ? optab->mem_fetch_after
7894 : optab->mem_fetch_before, mode);
7895 create_integer_operand (&ops[3], model);
7896 num_ops = 4;
7898 else
7900 icode = optab_handler (after ? optab->fetch_after
7901 : optab->fetch_before, mode);
7902 num_ops = 3;
7904 create_output_operand (&ops[op_counter++], target, mode);
7906 if (icode == CODE_FOR_nothing)
7907 return NULL_RTX;
7909 create_fixed_operand (&ops[op_counter++], mem);
7910 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7911 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7913 if (maybe_expand_insn (icode, num_ops, ops))
7914 return (target == const0_rtx ? const0_rtx : ops[0].value);
7916 return NULL_RTX;
7920 /* This function expands an atomic fetch_OP or OP_fetch operation:
7921 TARGET is an option place to stick the return value. const0_rtx indicates
7922 the result is unused.
7923 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7924 CODE is the operation being performed (OP)
7925 MEMMODEL is the memory model variant to use.
7926 AFTER is true to return the result of the operation (OP_fetch).
7927 AFTER is false to return the value before the operation (fetch_OP).
7929 This function will *only* generate instructions if there is a direct
7930 optab. No compare and swap loops or libcalls will be generated. */
7932 static rtx
7933 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7934 enum rtx_code code, enum memmodel model,
7935 bool after)
7937 machine_mode mode = GET_MODE (mem);
7938 struct atomic_op_functions optab;
7939 rtx result;
7940 bool unused_result = (target == const0_rtx);
7942 get_atomic_op_for_code (&optab, code);
7944 /* Check to see if there are any better instructions. */
7945 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7946 if (result)
7947 return result;
7949 /* Check for the case where the result isn't used and try those patterns. */
7950 if (unused_result)
7952 /* Try the memory model variant first. */
7953 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7954 if (result)
7955 return result;
7957 /* Next try the old style withuot a memory model. */
7958 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7959 if (result)
7960 return result;
7962 /* There is no no-result pattern, so try patterns with a result. */
7963 target = NULL_RTX;
7966 /* Try the __atomic version. */
7967 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
7968 if (result)
7969 return result;
7971 /* Try the older __sync version. */
7972 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
7973 if (result)
7974 return result;
7976 /* If the fetch value can be calculated from the other variation of fetch,
7977 try that operation. */
7978 if (after || unused_result || optab.reverse_code != UNKNOWN)
7980 /* Try the __atomic version, then the older __sync version. */
7981 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
7982 if (!result)
7983 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
7985 if (result)
7987 /* If the result isn't used, no need to do compensation code. */
7988 if (unused_result)
7989 return result;
7991 /* Issue compensation code. Fetch_after == fetch_before OP val.
7992 Fetch_before == after REVERSE_OP val. */
7993 if (!after)
7994 code = optab.reverse_code;
7995 if (code == NOT)
7997 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
7998 true, OPTAB_LIB_WIDEN);
7999 result = expand_simple_unop (mode, NOT, result, target, true);
8001 else
8002 result = expand_simple_binop (mode, code, result, val, target,
8003 true, OPTAB_LIB_WIDEN);
8004 return result;
8008 /* No direct opcode can be generated. */
8009 return NULL_RTX;
8014 /* This function expands an atomic fetch_OP or OP_fetch operation:
8015 TARGET is an option place to stick the return value. const0_rtx indicates
8016 the result is unused.
8017 atomically fetch MEM, perform the operation with VAL and return it to MEM.
8018 CODE is the operation being performed (OP)
8019 MEMMODEL is the memory model variant to use.
8020 AFTER is true to return the result of the operation (OP_fetch).
8021 AFTER is false to return the value before the operation (fetch_OP). */
8023 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
8024 enum memmodel model, bool after)
8026 machine_mode mode = GET_MODE (mem);
8027 rtx result;
8028 bool unused_result = (target == const0_rtx);
8030 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
8031 after);
8033 if (result)
8034 return result;
8036 /* Add/sub can be implemented by doing the reverse operation with -(val). */
8037 if (code == PLUS || code == MINUS)
8039 rtx tmp;
8040 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
8042 start_sequence ();
8043 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
8044 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
8045 model, after);
8046 if (result)
8048 /* PLUS worked so emit the insns and return. */
8049 tmp = get_insns ();
8050 end_sequence ();
8051 emit_insn (tmp);
8052 return result;
8055 /* PLUS did not work, so throw away the negation code and continue. */
8056 end_sequence ();
8059 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
8060 if (!can_compare_and_swap_p (mode, false))
8062 rtx libfunc;
8063 bool fixup = false;
8064 enum rtx_code orig_code = code;
8065 struct atomic_op_functions optab;
8067 get_atomic_op_for_code (&optab, code);
8068 libfunc = optab_libfunc (after ? optab.fetch_after
8069 : optab.fetch_before, mode);
8070 if (libfunc == NULL
8071 && (after || unused_result || optab.reverse_code != UNKNOWN))
8073 fixup = true;
8074 if (!after)
8075 code = optab.reverse_code;
8076 libfunc = optab_libfunc (after ? optab.fetch_before
8077 : optab.fetch_after, mode);
8079 if (libfunc != NULL)
8081 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
8082 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
8083 2, addr, ptr_mode, val, mode);
8085 if (!unused_result && fixup)
8086 result = expand_simple_binop (mode, code, result, val, target,
8087 true, OPTAB_LIB_WIDEN);
8088 return result;
8091 /* We need the original code for any further attempts. */
8092 code = orig_code;
8095 /* If nothing else has succeeded, default to a compare and swap loop. */
8096 if (can_compare_and_swap_p (mode, true))
8098 rtx_insn *insn;
8099 rtx t0 = gen_reg_rtx (mode), t1;
8101 start_sequence ();
8103 /* If the result is used, get a register for it. */
8104 if (!unused_result)
8106 if (!target || !register_operand (target, mode))
8107 target = gen_reg_rtx (mode);
8108 /* If fetch_before, copy the value now. */
8109 if (!after)
8110 emit_move_insn (target, t0);
8112 else
8113 target = const0_rtx;
8115 t1 = t0;
8116 if (code == NOT)
8118 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
8119 true, OPTAB_LIB_WIDEN);
8120 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
8122 else
8123 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
8124 OPTAB_LIB_WIDEN);
8126 /* For after, copy the value now. */
8127 if (!unused_result && after)
8128 emit_move_insn (target, t1);
8129 insn = get_insns ();
8130 end_sequence ();
8132 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
8133 return target;
8136 return NULL_RTX;
8139 /* Return true if OPERAND is suitable for operand number OPNO of
8140 instruction ICODE. */
8142 bool
8143 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
8145 return (!insn_data[(int) icode].operand[opno].predicate
8146 || (insn_data[(int) icode].operand[opno].predicate
8147 (operand, insn_data[(int) icode].operand[opno].mode)));
8150 /* TARGET is a target of a multiword operation that we are going to
8151 implement as a series of word-mode operations. Return true if
8152 TARGET is suitable for this purpose. */
8154 bool
8155 valid_multiword_target_p (rtx target)
8157 machine_mode mode;
8158 int i;
8160 mode = GET_MODE (target);
8161 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
8162 if (!validate_subreg (word_mode, mode, target, i))
8163 return false;
8164 return true;
8167 /* Like maybe_legitimize_operand, but do not change the code of the
8168 current rtx value. */
8170 static bool
8171 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8172 struct expand_operand *op)
8174 /* See if the operand matches in its current form. */
8175 if (insn_operand_matches (icode, opno, op->value))
8176 return true;
8178 /* If the operand is a memory whose address has no side effects,
8179 try forcing the address into a non-virtual pseudo register.
8180 The check for side effects is important because copy_to_mode_reg
8181 cannot handle things like auto-modified addresses. */
8182 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8184 rtx addr, mem;
8186 mem = op->value;
8187 addr = XEXP (mem, 0);
8188 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8189 && !side_effects_p (addr))
8191 rtx_insn *last;
8192 machine_mode mode;
8194 last = get_last_insn ();
8195 mode = get_address_mode (mem);
8196 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8197 if (insn_operand_matches (icode, opno, mem))
8199 op->value = mem;
8200 return true;
8202 delete_insns_since (last);
8206 return false;
8209 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8210 on success, storing the new operand value back in OP. */
8212 static bool
8213 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8214 struct expand_operand *op)
8216 machine_mode mode, imode;
8217 bool old_volatile_ok, result;
8219 mode = op->mode;
8220 switch (op->type)
8222 case EXPAND_FIXED:
8223 old_volatile_ok = volatile_ok;
8224 volatile_ok = true;
8225 result = maybe_legitimize_operand_same_code (icode, opno, op);
8226 volatile_ok = old_volatile_ok;
8227 return result;
8229 case EXPAND_OUTPUT:
8230 gcc_assert (mode != VOIDmode);
8231 if (op->value
8232 && op->value != const0_rtx
8233 && GET_MODE (op->value) == mode
8234 && maybe_legitimize_operand_same_code (icode, opno, op))
8235 return true;
8237 op->value = gen_reg_rtx (mode);
8238 break;
8240 case EXPAND_INPUT:
8241 input:
8242 gcc_assert (mode != VOIDmode);
8243 gcc_assert (GET_MODE (op->value) == VOIDmode
8244 || GET_MODE (op->value) == mode);
8245 if (maybe_legitimize_operand_same_code (icode, opno, op))
8246 return true;
8248 op->value = copy_to_mode_reg (mode, op->value);
8249 break;
8251 case EXPAND_CONVERT_TO:
8252 gcc_assert (mode != VOIDmode);
8253 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8254 goto input;
8256 case EXPAND_CONVERT_FROM:
8257 if (GET_MODE (op->value) != VOIDmode)
8258 mode = GET_MODE (op->value);
8259 else
8260 /* The caller must tell us what mode this value has. */
8261 gcc_assert (mode != VOIDmode);
8263 imode = insn_data[(int) icode].operand[opno].mode;
8264 if (imode != VOIDmode && imode != mode)
8266 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8267 mode = imode;
8269 goto input;
8271 case EXPAND_ADDRESS:
8272 gcc_assert (mode != VOIDmode);
8273 op->value = convert_memory_address (mode, op->value);
8274 goto input;
8276 case EXPAND_INTEGER:
8277 mode = insn_data[(int) icode].operand[opno].mode;
8278 if (mode != VOIDmode && const_int_operand (op->value, mode))
8279 goto input;
8280 break;
8282 return insn_operand_matches (icode, opno, op->value);
8285 /* Make OP describe an input operand that should have the same value
8286 as VALUE, after any mode conversion that the target might request.
8287 TYPE is the type of VALUE. */
8289 void
8290 create_convert_operand_from_type (struct expand_operand *op,
8291 rtx value, tree type)
8293 create_convert_operand_from (op, value, TYPE_MODE (type),
8294 TYPE_UNSIGNED (type));
8297 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8298 of instruction ICODE. Return true on success, leaving the new operand
8299 values in the OPS themselves. Emit no code on failure. */
8301 bool
8302 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8303 unsigned int nops, struct expand_operand *ops)
8305 rtx_insn *last;
8306 unsigned int i;
8308 last = get_last_insn ();
8309 for (i = 0; i < nops; i++)
8310 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8312 delete_insns_since (last);
8313 return false;
8315 return true;
8318 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8319 as its operands. Return the instruction pattern on success,
8320 and emit any necessary set-up code. Return null and emit no
8321 code on failure. */
8323 rtx_insn *
8324 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8325 struct expand_operand *ops)
8327 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8328 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8329 return NULL;
8331 switch (nops)
8333 case 1:
8334 return GEN_FCN (icode) (ops[0].value);
8335 case 2:
8336 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8337 case 3:
8338 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8339 case 4:
8340 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8341 ops[3].value);
8342 case 5:
8343 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8344 ops[3].value, ops[4].value);
8345 case 6:
8346 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8347 ops[3].value, ops[4].value, ops[5].value);
8348 case 7:
8349 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8350 ops[3].value, ops[4].value, ops[5].value,
8351 ops[6].value);
8352 case 8:
8353 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8354 ops[3].value, ops[4].value, ops[5].value,
8355 ops[6].value, ops[7].value);
8356 case 9:
8357 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8358 ops[3].value, ops[4].value, ops[5].value,
8359 ops[6].value, ops[7].value, ops[8].value);
8361 gcc_unreachable ();
8364 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8365 as its operands. Return true on success and emit no code on failure. */
8367 bool
8368 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8369 struct expand_operand *ops)
8371 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
8372 if (pat)
8374 emit_insn (pat);
8375 return true;
8377 return false;
8380 /* Like maybe_expand_insn, but for jumps. */
8382 bool
8383 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8384 struct expand_operand *ops)
8386 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
8387 if (pat)
8389 emit_jump_insn (pat);
8390 return true;
8392 return false;
8395 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8396 as its operands. */
8398 void
8399 expand_insn (enum insn_code icode, unsigned int nops,
8400 struct expand_operand *ops)
8402 if (!maybe_expand_insn (icode, nops, ops))
8403 gcc_unreachable ();
8406 /* Like expand_insn, but for jumps. */
8408 void
8409 expand_jump_insn (enum insn_code icode, unsigned int nops,
8410 struct expand_operand *ops)
8412 if (!maybe_expand_jump_insn (icode, nops, ops))
8413 gcc_unreachable ();
8416 /* Reduce conditional compilation elsewhere. */
8418 /* Enumerates the possible types of structure operand to an
8419 extraction_insn. */
8420 enum extraction_type { ET_unaligned_mem, ET_reg };
8422 /* Check whether insv, extv or extzv pattern ICODE can be used for an
8423 insertion or extraction of type TYPE on a structure of mode MODE.
8424 Return true if so and fill in *INSN accordingly. STRUCT_OP is the
8425 operand number of the structure (the first sign_extract or zero_extract
8426 operand) and FIELD_OP is the operand number of the field (the other
8427 side of the set from the sign_extract or zero_extract). */
8429 static bool
8430 get_traditional_extraction_insn (extraction_insn *insn,
8431 enum extraction_type type,
8432 machine_mode mode,
8433 enum insn_code icode,
8434 int struct_op, int field_op)
8436 const struct insn_data_d *data = &insn_data[icode];
8438 machine_mode struct_mode = data->operand[struct_op].mode;
8439 if (struct_mode == VOIDmode)
8440 struct_mode = word_mode;
8441 if (mode != struct_mode)
8442 return false;
8444 machine_mode field_mode = data->operand[field_op].mode;
8445 if (field_mode == VOIDmode)
8446 field_mode = word_mode;
8448 machine_mode pos_mode = data->operand[struct_op + 2].mode;
8449 if (pos_mode == VOIDmode)
8450 pos_mode = word_mode;
8452 insn->icode = icode;
8453 insn->field_mode = field_mode;
8454 insn->struct_mode = (type == ET_unaligned_mem ? byte_mode : struct_mode);
8455 insn->pos_mode = pos_mode;
8456 return true;
8459 /* Return true if an optab exists to perform an insertion or extraction
8460 of type TYPE in mode MODE. Describe the instruction in *INSN if so.
8462 REG_OPTAB is the optab to use for register structures and
8463 MISALIGN_OPTAB is the optab to use for misaligned memory structures.
8464 POS_OP is the operand number of the bit position. */
8466 static bool
8467 get_optab_extraction_insn (struct extraction_insn *insn,
8468 enum extraction_type type,
8469 machine_mode mode, direct_optab reg_optab,
8470 direct_optab misalign_optab, int pos_op)
8472 direct_optab optab = (type == ET_unaligned_mem ? misalign_optab : reg_optab);
8473 enum insn_code icode = direct_optab_handler (optab, mode);
8474 if (icode == CODE_FOR_nothing)
8475 return false;
8477 const struct insn_data_d *data = &insn_data[icode];
8479 insn->icode = icode;
8480 insn->field_mode = mode;
8481 insn->struct_mode = (type == ET_unaligned_mem ? BLKmode : mode);
8482 insn->pos_mode = data->operand[pos_op].mode;
8483 if (insn->pos_mode == VOIDmode)
8484 insn->pos_mode = word_mode;
8485 return true;
8488 /* Return true if an instruction exists to perform an insertion or
8489 extraction (PATTERN says which) of type TYPE in mode MODE.
8490 Describe the instruction in *INSN if so. */
8492 static bool
8493 get_extraction_insn (extraction_insn *insn,
8494 enum extraction_pattern pattern,
8495 enum extraction_type type,
8496 machine_mode mode)
8498 switch (pattern)
8500 case EP_insv:
8501 if (targetm.have_insv ()
8502 && get_traditional_extraction_insn (insn, type, mode,
8503 targetm.code_for_insv, 0, 3))
8504 return true;
8505 return get_optab_extraction_insn (insn, type, mode, insv_optab,
8506 insvmisalign_optab, 2);
8508 case EP_extv:
8509 if (targetm.have_extv ()
8510 && get_traditional_extraction_insn (insn, type, mode,
8511 targetm.code_for_extv, 1, 0))
8512 return true;
8513 return get_optab_extraction_insn (insn, type, mode, extv_optab,
8514 extvmisalign_optab, 3);
8516 case EP_extzv:
8517 if (targetm.have_extzv ()
8518 && get_traditional_extraction_insn (insn, type, mode,
8519 targetm.code_for_extzv, 1, 0))
8520 return true;
8521 return get_optab_extraction_insn (insn, type, mode, extzv_optab,
8522 extzvmisalign_optab, 3);
8524 default:
8525 gcc_unreachable ();
8529 /* Return true if an instruction exists to access a field of mode
8530 FIELDMODE in a structure that has STRUCT_BITS significant bits.
8531 Describe the "best" such instruction in *INSN if so. PATTERN and
8532 TYPE describe the type of insertion or extraction we want to perform.
8534 For an insertion, the number of significant structure bits includes
8535 all bits of the target. For an extraction, it need only include the
8536 most significant bit of the field. Larger widths are acceptable
8537 in both cases. */
8539 static bool
8540 get_best_extraction_insn (extraction_insn *insn,
8541 enum extraction_pattern pattern,
8542 enum extraction_type type,
8543 unsigned HOST_WIDE_INT struct_bits,
8544 machine_mode field_mode)
8546 machine_mode mode = smallest_mode_for_size (struct_bits, MODE_INT);
8547 while (mode != VOIDmode)
8549 if (get_extraction_insn (insn, pattern, type, mode))
8551 while (mode != VOIDmode
8552 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (field_mode)
8553 && !TRULY_NOOP_TRUNCATION_MODES_P (insn->field_mode,
8554 field_mode))
8556 get_extraction_insn (insn, pattern, type, mode);
8557 mode = GET_MODE_WIDER_MODE (mode);
8559 return true;
8561 mode = GET_MODE_WIDER_MODE (mode);
8563 return false;
8566 /* Return true if an instruction exists to access a field of mode
8567 FIELDMODE in a register structure that has STRUCT_BITS significant bits.
8568 Describe the "best" such instruction in *INSN if so. PATTERN describes
8569 the type of insertion or extraction we want to perform.
8571 For an insertion, the number of significant structure bits includes
8572 all bits of the target. For an extraction, it need only include the
8573 most significant bit of the field. Larger widths are acceptable
8574 in both cases. */
8576 bool
8577 get_best_reg_extraction_insn (extraction_insn *insn,
8578 enum extraction_pattern pattern,
8579 unsigned HOST_WIDE_INT struct_bits,
8580 machine_mode field_mode)
8582 return get_best_extraction_insn (insn, pattern, ET_reg, struct_bits,
8583 field_mode);
8586 /* Return true if an instruction exists to access a field of BITSIZE
8587 bits starting BITNUM bits into a memory structure. Describe the
8588 "best" such instruction in *INSN if so. PATTERN describes the type
8589 of insertion or extraction we want to perform and FIELDMODE is the
8590 natural mode of the extracted field.
8592 The instructions considered here only access bytes that overlap
8593 the bitfield; they do not touch any surrounding bytes. */
8595 bool
8596 get_best_mem_extraction_insn (extraction_insn *insn,
8597 enum extraction_pattern pattern,
8598 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitnum,
8599 machine_mode field_mode)
8601 unsigned HOST_WIDE_INT struct_bits = (bitnum % BITS_PER_UNIT
8602 + bitsize
8603 + BITS_PER_UNIT - 1);
8604 struct_bits -= struct_bits % BITS_PER_UNIT;
8605 return get_best_extraction_insn (insn, pattern, ET_unaligned_mem,
8606 struct_bits, field_mode);
8609 /* Determine whether "1 << x" is relatively cheap in word_mode. */
8611 bool
8612 lshift_cheap_p (bool speed_p)
8614 /* FIXME: This should be made target dependent via this "this_target"
8615 mechanism, similar to e.g. can_copy_init_p in gcse.c. */
8616 static bool init[2] = { false, false };
8617 static bool cheap[2] = { true, true };
8619 /* If the targer has no lshift in word_mode, the operation will most
8620 probably not be cheap. ??? Does GCC even work for such targets? */
8621 if (optab_handler (ashl_optab, word_mode) == CODE_FOR_nothing)
8622 return false;
8624 if (!init[speed_p])
8626 rtx reg = gen_raw_REG (word_mode, 10000);
8627 int cost = set_src_cost (gen_rtx_ASHIFT (word_mode, const1_rtx, reg),
8628 word_mode, speed_p);
8629 cheap[speed_p] = cost < COSTS_N_INSNS (3);
8630 init[speed_p] = true;
8633 return cheap[speed_p];
8636 #include "gt-optabs.h"