* [SH] Miscellaneous changes for LRA.
[official-gcc.git] / gcc / optabs.c
blobf8d584eeeb11a2c19d8c8d887a0ff18aed5f56b4
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "diagnostic-core.h"
27 /* Include insn-config.h before expr.h so that HAVE_conditional_move
28 is properly defined. */
29 #include "insn-config.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "tree-hasher.h"
33 #include "stor-layout.h"
34 #include "stringpool.h"
35 #include "varasm.h"
36 #include "tm_p.h"
37 #include "flags.h"
38 #include "hashtab.h"
39 #include "hash-set.h"
40 #include "vec.h"
41 #include "machmode.h"
42 #include "hard-reg-set.h"
43 #include "input.h"
44 #include "function.h"
45 #include "except.h"
46 #include "expr.h"
47 #include "insn-codes.h"
48 #include "optabs.h"
49 #include "libfuncs.h"
50 #include "recog.h"
51 #include "reload.h"
52 #include "ggc.h"
53 #include "predict.h"
54 #include "dominance.h"
55 #include "cfg.h"
56 #include "basic-block.h"
57 #include "target.h"
59 struct target_optabs default_target_optabs;
60 struct target_libfuncs default_target_libfuncs;
61 struct target_optabs *this_fn_optabs = &default_target_optabs;
62 #if SWITCHABLE_TARGET
63 struct target_optabs *this_target_optabs = &default_target_optabs;
64 struct target_libfuncs *this_target_libfuncs = &default_target_libfuncs;
65 #endif
67 #define libfunc_hash \
68 (this_target_libfuncs->x_libfunc_hash)
70 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
71 machine_mode *);
72 static rtx expand_unop_direct (machine_mode, optab, rtx, rtx, int);
73 static void emit_libcall_block_1 (rtx_insn *, rtx, rtx, rtx, bool);
75 /* Debug facility for use in GDB. */
76 void debug_optab_libfuncs (void);
78 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
79 #if ENABLE_DECIMAL_BID_FORMAT
80 #define DECIMAL_PREFIX "bid_"
81 #else
82 #define DECIMAL_PREFIX "dpd_"
83 #endif
85 /* Used for libfunc_hash. */
87 hashval_t
88 libfunc_hasher::hash (libfunc_entry *e)
90 return ((e->mode1 + e->mode2 * NUM_MACHINE_MODES) ^ e->op);
93 /* Used for libfunc_hash. */
95 bool
96 libfunc_hasher::equal (libfunc_entry *e1, libfunc_entry *e2)
98 return e1->op == e2->op && e1->mode1 == e2->mode1 && e1->mode2 == e2->mode2;
101 /* Return libfunc corresponding operation defined by OPTAB converting
102 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
103 if no libfunc is available. */
105 convert_optab_libfunc (convert_optab optab, machine_mode mode1,
106 machine_mode mode2)
108 struct libfunc_entry e;
109 struct libfunc_entry **slot;
111 /* ??? This ought to be an assert, but not all of the places
112 that we expand optabs know about the optabs that got moved
113 to being direct. */
114 if (!(optab >= FIRST_CONV_OPTAB && optab <= LAST_CONVLIB_OPTAB))
115 return NULL_RTX;
117 e.op = optab;
118 e.mode1 = mode1;
119 e.mode2 = mode2;
120 slot = libfunc_hash->find_slot (&e, NO_INSERT);
121 if (!slot)
123 const struct convert_optab_libcall_d *d
124 = &convlib_def[optab - FIRST_CONV_OPTAB];
126 if (d->libcall_gen == NULL)
127 return NULL;
129 d->libcall_gen (optab, d->libcall_basename, mode1, mode2);
130 slot = libfunc_hash->find_slot (&e, NO_INSERT);
131 if (!slot)
132 return NULL;
134 return (*slot)->libfunc;
137 /* Return libfunc corresponding operation defined by OPTAB in MODE.
138 Trigger lazy initialization if needed, return NULL if no libfunc is
139 available. */
141 optab_libfunc (optab optab, machine_mode mode)
143 struct libfunc_entry e;
144 struct libfunc_entry **slot;
146 /* ??? This ought to be an assert, but not all of the places
147 that we expand optabs know about the optabs that got moved
148 to being direct. */
149 if (!(optab >= FIRST_NORM_OPTAB && optab <= LAST_NORMLIB_OPTAB))
150 return NULL_RTX;
152 e.op = optab;
153 e.mode1 = mode;
154 e.mode2 = VOIDmode;
155 slot = libfunc_hash->find_slot (&e, NO_INSERT);
156 if (!slot)
158 const struct optab_libcall_d *d
159 = &normlib_def[optab - FIRST_NORM_OPTAB];
161 if (d->libcall_gen == NULL)
162 return NULL;
164 d->libcall_gen (optab, d->libcall_basename, d->libcall_suffix, mode);
165 slot = libfunc_hash->find_slot (&e, NO_INSERT);
166 if (!slot)
167 return NULL;
169 return (*slot)->libfunc;
173 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
174 the result of operation CODE applied to OP0 (and OP1 if it is a binary
175 operation).
177 If the last insn does not set TARGET, don't do anything, but return 1.
179 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
180 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
181 try again, ensuring that TARGET is not one of the operands. */
183 static int
184 add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
186 rtx_insn *last_insn;
187 rtx set;
188 rtx note;
190 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
192 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
193 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
194 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
195 && GET_RTX_CLASS (code) != RTX_COMPARE
196 && GET_RTX_CLASS (code) != RTX_UNARY)
197 return 1;
199 if (GET_CODE (target) == ZERO_EXTRACT)
200 return 1;
202 for (last_insn = insns;
203 NEXT_INSN (last_insn) != NULL_RTX;
204 last_insn = NEXT_INSN (last_insn))
207 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
208 a value changing in the insn, so the note would be invalid for CSE. */
209 if (reg_overlap_mentioned_p (target, op0)
210 || (op1 && reg_overlap_mentioned_p (target, op1)))
212 if (MEM_P (target)
213 && (rtx_equal_p (target, op0)
214 || (op1 && rtx_equal_p (target, op1))))
216 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
217 over expanding it as temp = MEM op X, MEM = temp. If the target
218 supports MEM = MEM op X instructions, it is sometimes too hard
219 to reconstruct that form later, especially if X is also a memory,
220 and due to multiple occurrences of addresses the address might
221 be forced into register unnecessarily.
222 Note that not emitting the REG_EQUIV note might inhibit
223 CSE in some cases. */
224 set = single_set (last_insn);
225 if (set
226 && GET_CODE (SET_SRC (set)) == code
227 && MEM_P (SET_DEST (set))
228 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
229 || (op1 && rtx_equal_p (SET_DEST (set),
230 XEXP (SET_SRC (set), 1)))))
231 return 1;
233 return 0;
236 set = set_for_reg_notes (last_insn);
237 if (set == NULL_RTX)
238 return 1;
240 if (! rtx_equal_p (SET_DEST (set), target)
241 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
242 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
243 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
244 return 1;
246 if (GET_RTX_CLASS (code) == RTX_UNARY)
247 switch (code)
249 case FFS:
250 case CLZ:
251 case CTZ:
252 case CLRSB:
253 case POPCOUNT:
254 case PARITY:
255 case BSWAP:
256 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
258 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
259 if (GET_MODE_SIZE (GET_MODE (op0))
260 > GET_MODE_SIZE (GET_MODE (target)))
261 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
262 note, GET_MODE (op0));
263 else
264 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
265 note, GET_MODE (op0));
266 break;
268 /* FALLTHRU */
269 default:
270 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
271 break;
273 else
274 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
276 set_unique_reg_note (last_insn, REG_EQUAL, note);
278 return 1;
281 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
282 for a widening operation would be. In most cases this would be OP0, but if
283 that's a constant it'll be VOIDmode, which isn't useful. */
285 static machine_mode
286 widened_mode (machine_mode to_mode, rtx op0, rtx op1)
288 machine_mode m0 = GET_MODE (op0);
289 machine_mode m1 = GET_MODE (op1);
290 machine_mode result;
292 if (m0 == VOIDmode && m1 == VOIDmode)
293 return to_mode;
294 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
295 result = m1;
296 else
297 result = m0;
299 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
300 return to_mode;
302 return result;
305 /* Like optab_handler, but for widening_operations that have a
306 TO_MODE and a FROM_MODE. */
308 enum insn_code
309 widening_optab_handler (optab op, machine_mode to_mode,
310 machine_mode from_mode)
312 unsigned scode = (op << 16) | to_mode;
313 if (to_mode != from_mode && from_mode != VOIDmode)
315 /* ??? Why does find_widening_optab_handler_and_mode attempt to
316 widen things that can't be widened? E.g. add_optab... */
317 if (op > LAST_CONV_OPTAB)
318 return CODE_FOR_nothing;
319 scode |= from_mode << 8;
321 return raw_optab_handler (scode);
324 /* Find a widening optab even if it doesn't widen as much as we want.
325 E.g. if from_mode is HImode, and to_mode is DImode, and there is no
326 direct HI->SI insn, then return SI->DI, if that exists.
327 If PERMIT_NON_WIDENING is non-zero then this can be used with
328 non-widening optabs also. */
330 enum insn_code
331 find_widening_optab_handler_and_mode (optab op, machine_mode to_mode,
332 machine_mode from_mode,
333 int permit_non_widening,
334 machine_mode *found_mode)
336 for (; (permit_non_widening || from_mode != to_mode)
337 && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
338 && from_mode != VOIDmode;
339 from_mode = GET_MODE_WIDER_MODE (from_mode))
341 enum insn_code handler = widening_optab_handler (op, to_mode,
342 from_mode);
344 if (handler != CODE_FOR_nothing)
346 if (found_mode)
347 *found_mode = from_mode;
348 return handler;
352 return CODE_FOR_nothing;
355 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
356 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
357 not actually do a sign-extend or zero-extend, but can leave the
358 higher-order bits of the result rtx undefined, for example, in the case
359 of logical operations, but not right shifts. */
361 static rtx
362 widen_operand (rtx op, machine_mode mode, machine_mode oldmode,
363 int unsignedp, int no_extend)
365 rtx result;
367 /* If we don't have to extend and this is a constant, return it. */
368 if (no_extend && GET_MODE (op) == VOIDmode)
369 return op;
371 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
372 extend since it will be more efficient to do so unless the signedness of
373 a promoted object differs from our extension. */
374 if (! no_extend
375 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
376 && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
377 return convert_modes (mode, oldmode, op, unsignedp);
379 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
380 SUBREG. */
381 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
382 return gen_lowpart (mode, force_reg (GET_MODE (op), op));
384 /* Otherwise, get an object of MODE, clobber it, and set the low-order
385 part to OP. */
387 result = gen_reg_rtx (mode);
388 emit_clobber (result);
389 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
390 return result;
393 /* Return the optab used for computing the operation given by the tree code,
394 CODE and the tree EXP. This function is not always usable (for example, it
395 cannot give complete results for multiplication or division) but probably
396 ought to be relied on more widely throughout the expander. */
397 optab
398 optab_for_tree_code (enum tree_code code, const_tree type,
399 enum optab_subtype subtype)
401 bool trapv;
402 switch (code)
404 case BIT_AND_EXPR:
405 return and_optab;
407 case BIT_IOR_EXPR:
408 return ior_optab;
410 case BIT_NOT_EXPR:
411 return one_cmpl_optab;
413 case BIT_XOR_EXPR:
414 return xor_optab;
416 case MULT_HIGHPART_EXPR:
417 return TYPE_UNSIGNED (type) ? umul_highpart_optab : smul_highpart_optab;
419 case TRUNC_MOD_EXPR:
420 case CEIL_MOD_EXPR:
421 case FLOOR_MOD_EXPR:
422 case ROUND_MOD_EXPR:
423 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
425 case RDIV_EXPR:
426 case TRUNC_DIV_EXPR:
427 case CEIL_DIV_EXPR:
428 case FLOOR_DIV_EXPR:
429 case ROUND_DIV_EXPR:
430 case EXACT_DIV_EXPR:
431 if (TYPE_SATURATING (type))
432 return TYPE_UNSIGNED (type) ? usdiv_optab : ssdiv_optab;
433 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
435 case LSHIFT_EXPR:
436 if (TREE_CODE (type) == VECTOR_TYPE)
438 if (subtype == optab_vector)
439 return TYPE_SATURATING (type) ? unknown_optab : vashl_optab;
441 gcc_assert (subtype == optab_scalar);
443 if (TYPE_SATURATING (type))
444 return TYPE_UNSIGNED (type) ? usashl_optab : ssashl_optab;
445 return ashl_optab;
447 case RSHIFT_EXPR:
448 if (TREE_CODE (type) == VECTOR_TYPE)
450 if (subtype == optab_vector)
451 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
453 gcc_assert (subtype == optab_scalar);
455 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
457 case LROTATE_EXPR:
458 if (TREE_CODE (type) == VECTOR_TYPE)
460 if (subtype == optab_vector)
461 return vrotl_optab;
463 gcc_assert (subtype == optab_scalar);
465 return rotl_optab;
467 case RROTATE_EXPR:
468 if (TREE_CODE (type) == VECTOR_TYPE)
470 if (subtype == optab_vector)
471 return vrotr_optab;
473 gcc_assert (subtype == optab_scalar);
475 return rotr_optab;
477 case MAX_EXPR:
478 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
480 case MIN_EXPR:
481 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
483 case REALIGN_LOAD_EXPR:
484 return vec_realign_load_optab;
486 case WIDEN_SUM_EXPR:
487 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
489 case DOT_PROD_EXPR:
490 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
492 case SAD_EXPR:
493 return TYPE_UNSIGNED (type) ? usad_optab : ssad_optab;
495 case WIDEN_MULT_PLUS_EXPR:
496 return (TYPE_UNSIGNED (type)
497 ? (TYPE_SATURATING (type)
498 ? usmadd_widen_optab : umadd_widen_optab)
499 : (TYPE_SATURATING (type)
500 ? ssmadd_widen_optab : smadd_widen_optab));
502 case WIDEN_MULT_MINUS_EXPR:
503 return (TYPE_UNSIGNED (type)
504 ? (TYPE_SATURATING (type)
505 ? usmsub_widen_optab : umsub_widen_optab)
506 : (TYPE_SATURATING (type)
507 ? ssmsub_widen_optab : smsub_widen_optab));
509 case FMA_EXPR:
510 return fma_optab;
512 case REDUC_MAX_EXPR:
513 return TYPE_UNSIGNED (type)
514 ? reduc_umax_scal_optab : reduc_smax_scal_optab;
516 case REDUC_MIN_EXPR:
517 return TYPE_UNSIGNED (type)
518 ? reduc_umin_scal_optab : reduc_smin_scal_optab;
520 case REDUC_PLUS_EXPR:
521 return reduc_plus_scal_optab;
523 case VEC_WIDEN_MULT_HI_EXPR:
524 return TYPE_UNSIGNED (type) ?
525 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
527 case VEC_WIDEN_MULT_LO_EXPR:
528 return TYPE_UNSIGNED (type) ?
529 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
531 case VEC_WIDEN_MULT_EVEN_EXPR:
532 return TYPE_UNSIGNED (type) ?
533 vec_widen_umult_even_optab : vec_widen_smult_even_optab;
535 case VEC_WIDEN_MULT_ODD_EXPR:
536 return TYPE_UNSIGNED (type) ?
537 vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
539 case VEC_WIDEN_LSHIFT_HI_EXPR:
540 return TYPE_UNSIGNED (type) ?
541 vec_widen_ushiftl_hi_optab : vec_widen_sshiftl_hi_optab;
543 case VEC_WIDEN_LSHIFT_LO_EXPR:
544 return TYPE_UNSIGNED (type) ?
545 vec_widen_ushiftl_lo_optab : vec_widen_sshiftl_lo_optab;
547 case VEC_UNPACK_HI_EXPR:
548 return TYPE_UNSIGNED (type) ?
549 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
551 case VEC_UNPACK_LO_EXPR:
552 return TYPE_UNSIGNED (type) ?
553 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
555 case VEC_UNPACK_FLOAT_HI_EXPR:
556 /* The signedness is determined from input operand. */
557 return TYPE_UNSIGNED (type) ?
558 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
560 case VEC_UNPACK_FLOAT_LO_EXPR:
561 /* The signedness is determined from input operand. */
562 return TYPE_UNSIGNED (type) ?
563 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
565 case VEC_PACK_TRUNC_EXPR:
566 return vec_pack_trunc_optab;
568 case VEC_PACK_SAT_EXPR:
569 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
571 case VEC_PACK_FIX_TRUNC_EXPR:
572 /* The signedness is determined from output operand. */
573 return TYPE_UNSIGNED (type) ?
574 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
576 default:
577 break;
580 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
581 switch (code)
583 case POINTER_PLUS_EXPR:
584 case PLUS_EXPR:
585 if (TYPE_SATURATING (type))
586 return TYPE_UNSIGNED (type) ? usadd_optab : ssadd_optab;
587 return trapv ? addv_optab : add_optab;
589 case MINUS_EXPR:
590 if (TYPE_SATURATING (type))
591 return TYPE_UNSIGNED (type) ? ussub_optab : sssub_optab;
592 return trapv ? subv_optab : sub_optab;
594 case MULT_EXPR:
595 if (TYPE_SATURATING (type))
596 return TYPE_UNSIGNED (type) ? usmul_optab : ssmul_optab;
597 return trapv ? smulv_optab : smul_optab;
599 case NEGATE_EXPR:
600 if (TYPE_SATURATING (type))
601 return TYPE_UNSIGNED (type) ? usneg_optab : ssneg_optab;
602 return trapv ? negv_optab : neg_optab;
604 case ABS_EXPR:
605 return trapv ? absv_optab : abs_optab;
607 default:
608 return unknown_optab;
612 /* Given optab UNOPTAB that reduces a vector to a scalar, find instead the old
613 optab that produces a vector with the reduction result in one element,
614 for a tree with type TYPE. */
616 optab
617 scalar_reduc_to_vector (optab unoptab, const_tree type)
619 switch (unoptab)
621 case reduc_plus_scal_optab:
622 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
624 case reduc_smin_scal_optab: return reduc_smin_optab;
625 case reduc_umin_scal_optab: return reduc_umin_optab;
626 case reduc_smax_scal_optab: return reduc_smax_optab;
627 case reduc_umax_scal_optab: return reduc_umax_optab;
628 default: return unknown_optab;
632 /* Expand vector widening operations.
634 There are two different classes of operations handled here:
635 1) Operations whose result is wider than all the arguments to the operation.
636 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
637 In this case OP0 and optionally OP1 would be initialized,
638 but WIDE_OP wouldn't (not relevant for this case).
639 2) Operations whose result is of the same size as the last argument to the
640 operation, but wider than all the other arguments to the operation.
641 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
642 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
644 E.g, when called to expand the following operations, this is how
645 the arguments will be initialized:
646 nops OP0 OP1 WIDE_OP
647 widening-sum 2 oprnd0 - oprnd1
648 widening-dot-product 3 oprnd0 oprnd1 oprnd2
649 widening-mult 2 oprnd0 oprnd1 -
650 type-promotion (vec-unpack) 1 oprnd0 - - */
653 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
654 rtx target, int unsignedp)
656 struct expand_operand eops[4];
657 tree oprnd0, oprnd1, oprnd2;
658 machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
659 optab widen_pattern_optab;
660 enum insn_code icode;
661 int nops = TREE_CODE_LENGTH (ops->code);
662 int op;
664 oprnd0 = ops->op0;
665 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
666 widen_pattern_optab =
667 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
668 if (ops->code == WIDEN_MULT_PLUS_EXPR
669 || ops->code == WIDEN_MULT_MINUS_EXPR)
670 icode = find_widening_optab_handler (widen_pattern_optab,
671 TYPE_MODE (TREE_TYPE (ops->op2)),
672 tmode0, 0);
673 else
674 icode = optab_handler (widen_pattern_optab, tmode0);
675 gcc_assert (icode != CODE_FOR_nothing);
677 if (nops >= 2)
679 oprnd1 = ops->op1;
680 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
683 /* The last operand is of a wider mode than the rest of the operands. */
684 if (nops == 2)
685 wmode = tmode1;
686 else if (nops == 3)
688 gcc_assert (tmode1 == tmode0);
689 gcc_assert (op1);
690 oprnd2 = ops->op2;
691 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
694 op = 0;
695 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
696 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
697 if (op1)
698 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
699 if (wide_op)
700 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
701 expand_insn (icode, op, eops);
702 return eops[0].value;
705 /* Generate code to perform an operation specified by TERNARY_OPTAB
706 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
708 UNSIGNEDP is for the case where we have to widen the operands
709 to perform the operation. It says to use zero-extension.
711 If TARGET is nonzero, the value
712 is generated there, if it is convenient to do so.
713 In all cases an rtx is returned for the locus of the value;
714 this may or may not be TARGET. */
717 expand_ternary_op (machine_mode mode, optab ternary_optab, rtx op0,
718 rtx op1, rtx op2, rtx target, int unsignedp)
720 struct expand_operand ops[4];
721 enum insn_code icode = optab_handler (ternary_optab, mode);
723 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
725 create_output_operand (&ops[0], target, mode);
726 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
727 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
728 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
729 expand_insn (icode, 4, ops);
730 return ops[0].value;
734 /* Like expand_binop, but return a constant rtx if the result can be
735 calculated at compile time. The arguments and return value are
736 otherwise the same as for expand_binop. */
739 simplify_expand_binop (machine_mode mode, optab binoptab,
740 rtx op0, rtx op1, rtx target, int unsignedp,
741 enum optab_methods methods)
743 if (CONSTANT_P (op0) && CONSTANT_P (op1))
745 rtx x = simplify_binary_operation (optab_to_code (binoptab),
746 mode, op0, op1);
747 if (x)
748 return x;
751 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
754 /* Like simplify_expand_binop, but always put the result in TARGET.
755 Return true if the expansion succeeded. */
757 bool
758 force_expand_binop (machine_mode mode, optab binoptab,
759 rtx op0, rtx op1, rtx target, int unsignedp,
760 enum optab_methods methods)
762 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
763 target, unsignedp, methods);
764 if (x == 0)
765 return false;
766 if (x != target)
767 emit_move_insn (target, x);
768 return true;
771 /* Create a new vector value in VMODE with all elements set to OP. The
772 mode of OP must be the element mode of VMODE. If OP is a constant,
773 then the return value will be a constant. */
775 static rtx
776 expand_vector_broadcast (machine_mode vmode, rtx op)
778 enum insn_code icode;
779 rtvec vec;
780 rtx ret;
781 int i, n;
783 gcc_checking_assert (VECTOR_MODE_P (vmode));
785 n = GET_MODE_NUNITS (vmode);
786 vec = rtvec_alloc (n);
787 for (i = 0; i < n; ++i)
788 RTVEC_ELT (vec, i) = op;
790 if (CONSTANT_P (op))
791 return gen_rtx_CONST_VECTOR (vmode, vec);
793 /* ??? If the target doesn't have a vec_init, then we have no easy way
794 of performing this operation. Most of this sort of generic support
795 is hidden away in the vector lowering support in gimple. */
796 icode = optab_handler (vec_init_optab, vmode);
797 if (icode == CODE_FOR_nothing)
798 return NULL;
800 ret = gen_reg_rtx (vmode);
801 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
803 return ret;
806 /* This subroutine of expand_doubleword_shift handles the cases in which
807 the effective shift value is >= BITS_PER_WORD. The arguments and return
808 value are the same as for the parent routine, except that SUPERWORD_OP1
809 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
810 INTO_TARGET may be null if the caller has decided to calculate it. */
812 static bool
813 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
814 rtx outof_target, rtx into_target,
815 int unsignedp, enum optab_methods methods)
817 if (into_target != 0)
818 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
819 into_target, unsignedp, methods))
820 return false;
822 if (outof_target != 0)
824 /* For a signed right shift, we must fill OUTOF_TARGET with copies
825 of the sign bit, otherwise we must fill it with zeros. */
826 if (binoptab != ashr_optab)
827 emit_move_insn (outof_target, CONST0_RTX (word_mode));
828 else
829 if (!force_expand_binop (word_mode, binoptab,
830 outof_input, GEN_INT (BITS_PER_WORD - 1),
831 outof_target, unsignedp, methods))
832 return false;
834 return true;
837 /* This subroutine of expand_doubleword_shift handles the cases in which
838 the effective shift value is < BITS_PER_WORD. The arguments and return
839 value are the same as for the parent routine. */
841 static bool
842 expand_subword_shift (machine_mode op1_mode, optab binoptab,
843 rtx outof_input, rtx into_input, rtx op1,
844 rtx outof_target, rtx into_target,
845 int unsignedp, enum optab_methods methods,
846 unsigned HOST_WIDE_INT shift_mask)
848 optab reverse_unsigned_shift, unsigned_shift;
849 rtx tmp, carries;
851 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
852 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
854 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
855 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
856 the opposite direction to BINOPTAB. */
857 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
859 carries = outof_input;
860 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
861 op1_mode), op1_mode);
862 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
863 0, true, methods);
865 else
867 /* We must avoid shifting by BITS_PER_WORD bits since that is either
868 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
869 has unknown behavior. Do a single shift first, then shift by the
870 remainder. It's OK to use ~OP1 as the remainder if shift counts
871 are truncated to the mode size. */
872 carries = expand_binop (word_mode, reverse_unsigned_shift,
873 outof_input, const1_rtx, 0, unsignedp, methods);
874 if (shift_mask == BITS_PER_WORD - 1)
876 tmp = immed_wide_int_const
877 (wi::minus_one (GET_MODE_PRECISION (op1_mode)), op1_mode);
878 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
879 0, true, methods);
881 else
883 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
884 op1_mode), op1_mode);
885 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
886 0, true, methods);
889 if (tmp == 0 || carries == 0)
890 return false;
891 carries = expand_binop (word_mode, reverse_unsigned_shift,
892 carries, tmp, 0, unsignedp, methods);
893 if (carries == 0)
894 return false;
896 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
897 so the result can go directly into INTO_TARGET if convenient. */
898 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
899 into_target, unsignedp, methods);
900 if (tmp == 0)
901 return false;
903 /* Now OR in the bits carried over from OUTOF_INPUT. */
904 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
905 into_target, unsignedp, methods))
906 return false;
908 /* Use a standard word_mode shift for the out-of half. */
909 if (outof_target != 0)
910 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
911 outof_target, unsignedp, methods))
912 return false;
914 return true;
918 #ifdef HAVE_conditional_move
919 /* Try implementing expand_doubleword_shift using conditional moves.
920 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
921 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
922 are the shift counts to use in the former and latter case. All other
923 arguments are the same as the parent routine. */
925 static bool
926 expand_doubleword_shift_condmove (machine_mode op1_mode, optab binoptab,
927 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
928 rtx outof_input, rtx into_input,
929 rtx subword_op1, rtx superword_op1,
930 rtx outof_target, rtx into_target,
931 int unsignedp, enum optab_methods methods,
932 unsigned HOST_WIDE_INT shift_mask)
934 rtx outof_superword, into_superword;
936 /* Put the superword version of the output into OUTOF_SUPERWORD and
937 INTO_SUPERWORD. */
938 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
939 if (outof_target != 0 && subword_op1 == superword_op1)
941 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
942 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
943 into_superword = outof_target;
944 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
945 outof_superword, 0, unsignedp, methods))
946 return false;
948 else
950 into_superword = gen_reg_rtx (word_mode);
951 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
952 outof_superword, into_superword,
953 unsignedp, methods))
954 return false;
957 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
958 if (!expand_subword_shift (op1_mode, binoptab,
959 outof_input, into_input, subword_op1,
960 outof_target, into_target,
961 unsignedp, methods, shift_mask))
962 return false;
964 /* Select between them. Do the INTO half first because INTO_SUPERWORD
965 might be the current value of OUTOF_TARGET. */
966 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
967 into_target, into_superword, word_mode, false))
968 return false;
970 if (outof_target != 0)
971 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
972 outof_target, outof_superword,
973 word_mode, false))
974 return false;
976 return true;
978 #endif
980 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
981 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
982 input operand; the shift moves bits in the direction OUTOF_INPUT->
983 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
984 of the target. OP1 is the shift count and OP1_MODE is its mode.
985 If OP1 is constant, it will have been truncated as appropriate
986 and is known to be nonzero.
988 If SHIFT_MASK is zero, the result of word shifts is undefined when the
989 shift count is outside the range [0, BITS_PER_WORD). This routine must
990 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
992 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
993 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
994 fill with zeros or sign bits as appropriate.
996 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
997 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
998 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
999 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
1000 are undefined.
1002 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
1003 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
1004 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
1005 function wants to calculate it itself.
1007 Return true if the shift could be successfully synthesized. */
1009 static bool
1010 expand_doubleword_shift (machine_mode op1_mode, optab binoptab,
1011 rtx outof_input, rtx into_input, rtx op1,
1012 rtx outof_target, rtx into_target,
1013 int unsignedp, enum optab_methods methods,
1014 unsigned HOST_WIDE_INT shift_mask)
1016 rtx superword_op1, tmp, cmp1, cmp2;
1017 enum rtx_code cmp_code;
1019 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1020 fill the result with sign or zero bits as appropriate. If so, the value
1021 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1022 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1023 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1025 This isn't worthwhile for constant shifts since the optimizers will
1026 cope better with in-range shift counts. */
1027 if (shift_mask >= BITS_PER_WORD
1028 && outof_target != 0
1029 && !CONSTANT_P (op1))
1031 if (!expand_doubleword_shift (op1_mode, binoptab,
1032 outof_input, into_input, op1,
1033 0, into_target,
1034 unsignedp, methods, shift_mask))
1035 return false;
1036 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1037 outof_target, unsignedp, methods))
1038 return false;
1039 return true;
1042 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1043 is true when the effective shift value is less than BITS_PER_WORD.
1044 Set SUPERWORD_OP1 to the shift count that should be used to shift
1045 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1046 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
1047 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1049 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1050 is a subword shift count. */
1051 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1052 0, true, methods);
1053 cmp2 = CONST0_RTX (op1_mode);
1054 cmp_code = EQ;
1055 superword_op1 = op1;
1057 else
1059 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1060 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1061 0, true, methods);
1062 cmp2 = CONST0_RTX (op1_mode);
1063 cmp_code = LT;
1064 superword_op1 = cmp1;
1066 if (cmp1 == 0)
1067 return false;
1069 /* If we can compute the condition at compile time, pick the
1070 appropriate subroutine. */
1071 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1072 if (tmp != 0 && CONST_INT_P (tmp))
1074 if (tmp == const0_rtx)
1075 return expand_superword_shift (binoptab, outof_input, superword_op1,
1076 outof_target, into_target,
1077 unsignedp, methods);
1078 else
1079 return expand_subword_shift (op1_mode, binoptab,
1080 outof_input, into_input, op1,
1081 outof_target, into_target,
1082 unsignedp, methods, shift_mask);
1085 #ifdef HAVE_conditional_move
1086 /* Try using conditional moves to generate straight-line code. */
1088 rtx_insn *start = get_last_insn ();
1089 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1090 cmp_code, cmp1, cmp2,
1091 outof_input, into_input,
1092 op1, superword_op1,
1093 outof_target, into_target,
1094 unsignedp, methods, shift_mask))
1095 return true;
1096 delete_insns_since (start);
1098 #endif
1100 /* As a last resort, use branches to select the correct alternative. */
1101 rtx_code_label *subword_label = gen_label_rtx ();
1102 rtx_code_label *done_label = gen_label_rtx ();
1104 NO_DEFER_POP;
1105 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1106 0, 0, subword_label, -1);
1107 OK_DEFER_POP;
1109 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1110 outof_target, into_target,
1111 unsignedp, methods))
1112 return false;
1114 emit_jump_insn (gen_jump (done_label));
1115 emit_barrier ();
1116 emit_label (subword_label);
1118 if (!expand_subword_shift (op1_mode, binoptab,
1119 outof_input, into_input, op1,
1120 outof_target, into_target,
1121 unsignedp, methods, shift_mask))
1122 return false;
1124 emit_label (done_label);
1125 return true;
1128 /* Subroutine of expand_binop. Perform a double word multiplication of
1129 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1130 as the target's word_mode. This function return NULL_RTX if anything
1131 goes wrong, in which case it may have already emitted instructions
1132 which need to be deleted.
1134 If we want to multiply two two-word values and have normal and widening
1135 multiplies of single-word values, we can do this with three smaller
1136 multiplications.
1138 The multiplication proceeds as follows:
1139 _______________________
1140 [__op0_high_|__op0_low__]
1141 _______________________
1142 * [__op1_high_|__op1_low__]
1143 _______________________________________________
1144 _______________________
1145 (1) [__op0_low__*__op1_low__]
1146 _______________________
1147 (2a) [__op0_low__*__op1_high_]
1148 _______________________
1149 (2b) [__op0_high_*__op1_low__]
1150 _______________________
1151 (3) [__op0_high_*__op1_high_]
1154 This gives a 4-word result. Since we are only interested in the
1155 lower 2 words, partial result (3) and the upper words of (2a) and
1156 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1157 calculated using non-widening multiplication.
1159 (1), however, needs to be calculated with an unsigned widening
1160 multiplication. If this operation is not directly supported we
1161 try using a signed widening multiplication and adjust the result.
1162 This adjustment works as follows:
1164 If both operands are positive then no adjustment is needed.
1166 If the operands have different signs, for example op0_low < 0 and
1167 op1_low >= 0, the instruction treats the most significant bit of
1168 op0_low as a sign bit instead of a bit with significance
1169 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1170 with 2**BITS_PER_WORD - op0_low, and two's complements the
1171 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1172 the result.
1174 Similarly, if both operands are negative, we need to add
1175 (op0_low + op1_low) * 2**BITS_PER_WORD.
1177 We use a trick to adjust quickly. We logically shift op0_low right
1178 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1179 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1180 logical shift exists, we do an arithmetic right shift and subtract
1181 the 0 or -1. */
1183 static rtx
1184 expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
1185 bool umulp, enum optab_methods methods)
1187 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1188 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1189 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1190 rtx product, adjust, product_high, temp;
1192 rtx op0_high = operand_subword_force (op0, high, mode);
1193 rtx op0_low = operand_subword_force (op0, low, mode);
1194 rtx op1_high = operand_subword_force (op1, high, mode);
1195 rtx op1_low = operand_subword_force (op1, low, mode);
1197 /* If we're using an unsigned multiply to directly compute the product
1198 of the low-order words of the operands and perform any required
1199 adjustments of the operands, we begin by trying two more multiplications
1200 and then computing the appropriate sum.
1202 We have checked above that the required addition is provided.
1203 Full-word addition will normally always succeed, especially if
1204 it is provided at all, so we don't worry about its failure. The
1205 multiplication may well fail, however, so we do handle that. */
1207 if (!umulp)
1209 /* ??? This could be done with emit_store_flag where available. */
1210 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1211 NULL_RTX, 1, methods);
1212 if (temp)
1213 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1214 NULL_RTX, 0, OPTAB_DIRECT);
1215 else
1217 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1218 NULL_RTX, 0, methods);
1219 if (!temp)
1220 return NULL_RTX;
1221 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1222 NULL_RTX, 0, OPTAB_DIRECT);
1225 if (!op0_high)
1226 return NULL_RTX;
1229 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1230 NULL_RTX, 0, OPTAB_DIRECT);
1231 if (!adjust)
1232 return NULL_RTX;
1234 /* OP0_HIGH should now be dead. */
1236 if (!umulp)
1238 /* ??? This could be done with emit_store_flag where available. */
1239 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1240 NULL_RTX, 1, methods);
1241 if (temp)
1242 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1243 NULL_RTX, 0, OPTAB_DIRECT);
1244 else
1246 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1247 NULL_RTX, 0, methods);
1248 if (!temp)
1249 return NULL_RTX;
1250 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1251 NULL_RTX, 0, OPTAB_DIRECT);
1254 if (!op1_high)
1255 return NULL_RTX;
1258 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1259 NULL_RTX, 0, OPTAB_DIRECT);
1260 if (!temp)
1261 return NULL_RTX;
1263 /* OP1_HIGH should now be dead. */
1265 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1266 NULL_RTX, 0, OPTAB_DIRECT);
1268 if (target && !REG_P (target))
1269 target = NULL_RTX;
1271 if (umulp)
1272 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1273 target, 1, OPTAB_DIRECT);
1274 else
1275 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1276 target, 1, OPTAB_DIRECT);
1278 if (!product)
1279 return NULL_RTX;
1281 product_high = operand_subword (product, high, 1, mode);
1282 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1283 NULL_RTX, 0, OPTAB_DIRECT);
1284 emit_move_insn (product_high, adjust);
1285 return product;
1288 /* Wrapper around expand_binop which takes an rtx code to specify
1289 the operation to perform, not an optab pointer. All other
1290 arguments are the same. */
1292 expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
1293 rtx op1, rtx target, int unsignedp,
1294 enum optab_methods methods)
1296 optab binop = code_to_optab (code);
1297 gcc_assert (binop);
1299 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1302 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1303 binop. Order them according to commutative_operand_precedence and, if
1304 possible, try to put TARGET or a pseudo first. */
1305 static bool
1306 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1308 int op0_prec = commutative_operand_precedence (op0);
1309 int op1_prec = commutative_operand_precedence (op1);
1311 if (op0_prec < op1_prec)
1312 return true;
1314 if (op0_prec > op1_prec)
1315 return false;
1317 /* With equal precedence, both orders are ok, but it is better if the
1318 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1319 if (target == 0 || REG_P (target))
1320 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1321 else
1322 return rtx_equal_p (op1, target);
1325 /* Return true if BINOPTAB implements a shift operation. */
1327 static bool
1328 shift_optab_p (optab binoptab)
1330 switch (optab_to_code (binoptab))
1332 case ASHIFT:
1333 case SS_ASHIFT:
1334 case US_ASHIFT:
1335 case ASHIFTRT:
1336 case LSHIFTRT:
1337 case ROTATE:
1338 case ROTATERT:
1339 return true;
1341 default:
1342 return false;
1346 /* Return true if BINOPTAB implements a commutative binary operation. */
1348 static bool
1349 commutative_optab_p (optab binoptab)
1351 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1352 || binoptab == smul_widen_optab
1353 || binoptab == umul_widen_optab
1354 || binoptab == smul_highpart_optab
1355 || binoptab == umul_highpart_optab);
1358 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1359 optimizing, and if the operand is a constant that costs more than
1360 1 instruction, force the constant into a register and return that
1361 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1363 static rtx
1364 avoid_expensive_constant (machine_mode mode, optab binoptab,
1365 int opn, rtx x, bool unsignedp)
1367 bool speed = optimize_insn_for_speed_p ();
1369 if (mode != VOIDmode
1370 && optimize
1371 && CONSTANT_P (x)
1372 && (rtx_cost (x, optab_to_code (binoptab), opn, speed)
1373 > set_src_cost (x, speed)))
1375 if (CONST_INT_P (x))
1377 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1378 if (intval != INTVAL (x))
1379 x = GEN_INT (intval);
1381 else
1382 x = convert_modes (mode, VOIDmode, x, unsignedp);
1383 x = force_reg (mode, x);
1385 return x;
1388 /* Helper function for expand_binop: handle the case where there
1389 is an insn that directly implements the indicated operation.
1390 Returns null if this is not possible. */
1391 static rtx
1392 expand_binop_directly (machine_mode mode, optab binoptab,
1393 rtx op0, rtx op1,
1394 rtx target, int unsignedp, enum optab_methods methods,
1395 rtx_insn *last)
1397 machine_mode from_mode = widened_mode (mode, op0, op1);
1398 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1399 from_mode, 1);
1400 machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1401 machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1402 machine_mode mode0, mode1, tmp_mode;
1403 struct expand_operand ops[3];
1404 bool commutative_p;
1405 rtx pat;
1406 rtx xop0 = op0, xop1 = op1;
1407 rtx swap;
1409 /* If it is a commutative operator and the modes would match
1410 if we would swap the operands, we can save the conversions. */
1411 commutative_p = commutative_optab_p (binoptab);
1412 if (commutative_p
1413 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1414 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1416 swap = xop0;
1417 xop0 = xop1;
1418 xop1 = swap;
1421 /* If we are optimizing, force expensive constants into a register. */
1422 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1423 if (!shift_optab_p (binoptab))
1424 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1426 /* In case the insn wants input operands in modes different from
1427 those of the actual operands, convert the operands. It would
1428 seem that we don't need to convert CONST_INTs, but we do, so
1429 that they're properly zero-extended, sign-extended or truncated
1430 for their mode. */
1432 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1433 if (xmode0 != VOIDmode && xmode0 != mode0)
1435 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1436 mode0 = xmode0;
1439 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1440 if (xmode1 != VOIDmode && xmode1 != mode1)
1442 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1443 mode1 = xmode1;
1446 /* If operation is commutative,
1447 try to make the first operand a register.
1448 Even better, try to make it the same as the target.
1449 Also try to make the last operand a constant. */
1450 if (commutative_p
1451 && swap_commutative_operands_with_target (target, xop0, xop1))
1453 swap = xop1;
1454 xop1 = xop0;
1455 xop0 = swap;
1458 /* Now, if insn's predicates don't allow our operands, put them into
1459 pseudo regs. */
1461 if (binoptab == vec_pack_trunc_optab
1462 || binoptab == vec_pack_usat_optab
1463 || binoptab == vec_pack_ssat_optab
1464 || binoptab == vec_pack_ufix_trunc_optab
1465 || binoptab == vec_pack_sfix_trunc_optab)
1467 /* The mode of the result is different then the mode of the
1468 arguments. */
1469 tmp_mode = insn_data[(int) icode].operand[0].mode;
1470 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1472 delete_insns_since (last);
1473 return NULL_RTX;
1476 else
1477 tmp_mode = mode;
1479 create_output_operand (&ops[0], target, tmp_mode);
1480 create_input_operand (&ops[1], xop0, mode0);
1481 create_input_operand (&ops[2], xop1, mode1);
1482 pat = maybe_gen_insn (icode, 3, ops);
1483 if (pat)
1485 /* If PAT is composed of more than one insn, try to add an appropriate
1486 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1487 operand, call expand_binop again, this time without a target. */
1488 if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
1489 && ! add_equal_note (as_a <rtx_insn *> (pat), ops[0].value,
1490 optab_to_code (binoptab),
1491 ops[1].value, ops[2].value))
1493 delete_insns_since (last);
1494 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1495 unsignedp, methods);
1498 emit_insn (pat);
1499 return ops[0].value;
1501 delete_insns_since (last);
1502 return NULL_RTX;
1505 /* Generate code to perform an operation specified by BINOPTAB
1506 on operands OP0 and OP1, with result having machine-mode MODE.
1508 UNSIGNEDP is for the case where we have to widen the operands
1509 to perform the operation. It says to use zero-extension.
1511 If TARGET is nonzero, the value
1512 is generated there, if it is convenient to do so.
1513 In all cases an rtx is returned for the locus of the value;
1514 this may or may not be TARGET. */
1517 expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
1518 rtx target, int unsignedp, enum optab_methods methods)
1520 enum optab_methods next_methods
1521 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1522 ? OPTAB_WIDEN : methods);
1523 enum mode_class mclass;
1524 machine_mode wider_mode;
1525 rtx libfunc;
1526 rtx temp;
1527 rtx_insn *entry_last = get_last_insn ();
1528 rtx_insn *last;
1530 mclass = GET_MODE_CLASS (mode);
1532 /* If subtracting an integer constant, convert this into an addition of
1533 the negated constant. */
1535 if (binoptab == sub_optab && CONST_INT_P (op1))
1537 op1 = negate_rtx (mode, op1);
1538 binoptab = add_optab;
1541 /* Record where to delete back to if we backtrack. */
1542 last = get_last_insn ();
1544 /* If we can do it with a three-operand insn, do so. */
1546 if (methods != OPTAB_MUST_WIDEN
1547 && find_widening_optab_handler (binoptab, mode,
1548 widened_mode (mode, op0, op1), 1)
1549 != CODE_FOR_nothing)
1551 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1552 unsignedp, methods, last);
1553 if (temp)
1554 return temp;
1557 /* If we were trying to rotate, and that didn't work, try rotating
1558 the other direction before falling back to shifts and bitwise-or. */
1559 if (((binoptab == rotl_optab
1560 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1561 || (binoptab == rotr_optab
1562 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1563 && mclass == MODE_INT)
1565 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1566 rtx newop1;
1567 unsigned int bits = GET_MODE_PRECISION (mode);
1569 if (CONST_INT_P (op1))
1570 newop1 = GEN_INT (bits - INTVAL (op1));
1571 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1572 newop1 = negate_rtx (GET_MODE (op1), op1);
1573 else
1574 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1575 gen_int_mode (bits, GET_MODE (op1)), op1,
1576 NULL_RTX, unsignedp, OPTAB_DIRECT);
1578 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1579 target, unsignedp, methods, last);
1580 if (temp)
1581 return temp;
1584 /* If this is a multiply, see if we can do a widening operation that
1585 takes operands of this mode and makes a wider mode. */
1587 if (binoptab == smul_optab
1588 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1589 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1590 : smul_widen_optab),
1591 GET_MODE_2XWIDER_MODE (mode), mode)
1592 != CODE_FOR_nothing))
1594 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1595 unsignedp ? umul_widen_optab : smul_widen_optab,
1596 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1598 if (temp != 0)
1600 if (GET_MODE_CLASS (mode) == MODE_INT
1601 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1602 return gen_lowpart (mode, temp);
1603 else
1604 return convert_to_mode (mode, temp, unsignedp);
1608 /* If this is a vector shift by a scalar, see if we can do a vector
1609 shift by a vector. If so, broadcast the scalar into a vector. */
1610 if (mclass == MODE_VECTOR_INT)
1612 optab otheroptab = unknown_optab;
1614 if (binoptab == ashl_optab)
1615 otheroptab = vashl_optab;
1616 else if (binoptab == ashr_optab)
1617 otheroptab = vashr_optab;
1618 else if (binoptab == lshr_optab)
1619 otheroptab = vlshr_optab;
1620 else if (binoptab == rotl_optab)
1621 otheroptab = vrotl_optab;
1622 else if (binoptab == rotr_optab)
1623 otheroptab = vrotr_optab;
1625 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1627 rtx vop1 = expand_vector_broadcast (mode, op1);
1628 if (vop1)
1630 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1631 target, unsignedp, methods, last);
1632 if (temp)
1633 return temp;
1638 /* Look for a wider mode of the same class for which we think we
1639 can open-code the operation. Check for a widening multiply at the
1640 wider mode as well. */
1642 if (CLASS_HAS_WIDER_MODES_P (mclass)
1643 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1644 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1645 wider_mode != VOIDmode;
1646 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1648 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1649 || (binoptab == smul_optab
1650 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1651 && (find_widening_optab_handler ((unsignedp
1652 ? umul_widen_optab
1653 : smul_widen_optab),
1654 GET_MODE_WIDER_MODE (wider_mode),
1655 mode, 0)
1656 != CODE_FOR_nothing)))
1658 rtx xop0 = op0, xop1 = op1;
1659 int no_extend = 0;
1661 /* For certain integer operations, we need not actually extend
1662 the narrow operands, as long as we will truncate
1663 the results to the same narrowness. */
1665 if ((binoptab == ior_optab || binoptab == and_optab
1666 || binoptab == xor_optab
1667 || binoptab == add_optab || binoptab == sub_optab
1668 || binoptab == smul_optab || binoptab == ashl_optab)
1669 && mclass == MODE_INT)
1671 no_extend = 1;
1672 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1673 xop0, unsignedp);
1674 if (binoptab != ashl_optab)
1675 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1676 xop1, unsignedp);
1679 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1681 /* The second operand of a shift must always be extended. */
1682 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1683 no_extend && binoptab != ashl_optab);
1685 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1686 unsignedp, OPTAB_DIRECT);
1687 if (temp)
1689 if (mclass != MODE_INT
1690 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1692 if (target == 0)
1693 target = gen_reg_rtx (mode);
1694 convert_move (target, temp, 0);
1695 return target;
1697 else
1698 return gen_lowpart (mode, temp);
1700 else
1701 delete_insns_since (last);
1705 /* If operation is commutative,
1706 try to make the first operand a register.
1707 Even better, try to make it the same as the target.
1708 Also try to make the last operand a constant. */
1709 if (commutative_optab_p (binoptab)
1710 && swap_commutative_operands_with_target (target, op0, op1))
1712 temp = op1;
1713 op1 = op0;
1714 op0 = temp;
1717 /* These can be done a word at a time. */
1718 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1719 && mclass == MODE_INT
1720 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1721 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1723 int i;
1724 rtx_insn *insns;
1726 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1727 won't be accurate, so use a new target. */
1728 if (target == 0
1729 || target == op0
1730 || target == op1
1731 || !valid_multiword_target_p (target))
1732 target = gen_reg_rtx (mode);
1734 start_sequence ();
1736 /* Do the actual arithmetic. */
1737 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1739 rtx target_piece = operand_subword (target, i, 1, mode);
1740 rtx x = expand_binop (word_mode, binoptab,
1741 operand_subword_force (op0, i, mode),
1742 operand_subword_force (op1, i, mode),
1743 target_piece, unsignedp, next_methods);
1745 if (x == 0)
1746 break;
1748 if (target_piece != x)
1749 emit_move_insn (target_piece, x);
1752 insns = get_insns ();
1753 end_sequence ();
1755 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1757 emit_insn (insns);
1758 return target;
1762 /* Synthesize double word shifts from single word shifts. */
1763 if ((binoptab == lshr_optab || binoptab == ashl_optab
1764 || binoptab == ashr_optab)
1765 && mclass == MODE_INT
1766 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1767 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1768 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1769 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1770 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1771 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1773 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1774 machine_mode op1_mode;
1776 double_shift_mask = targetm.shift_truncation_mask (mode);
1777 shift_mask = targetm.shift_truncation_mask (word_mode);
1778 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1780 /* Apply the truncation to constant shifts. */
1781 if (double_shift_mask > 0 && CONST_INT_P (op1))
1782 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1784 if (op1 == CONST0_RTX (op1_mode))
1785 return op0;
1787 /* Make sure that this is a combination that expand_doubleword_shift
1788 can handle. See the comments there for details. */
1789 if (double_shift_mask == 0
1790 || (shift_mask == BITS_PER_WORD - 1
1791 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1793 rtx_insn *insns;
1794 rtx into_target, outof_target;
1795 rtx into_input, outof_input;
1796 int left_shift, outof_word;
1798 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1799 won't be accurate, so use a new target. */
1800 if (target == 0
1801 || target == op0
1802 || target == op1
1803 || !valid_multiword_target_p (target))
1804 target = gen_reg_rtx (mode);
1806 start_sequence ();
1808 /* OUTOF_* is the word we are shifting bits away from, and
1809 INTO_* is the word that we are shifting bits towards, thus
1810 they differ depending on the direction of the shift and
1811 WORDS_BIG_ENDIAN. */
1813 left_shift = binoptab == ashl_optab;
1814 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1816 outof_target = operand_subword (target, outof_word, 1, mode);
1817 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1819 outof_input = operand_subword_force (op0, outof_word, mode);
1820 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1822 if (expand_doubleword_shift (op1_mode, binoptab,
1823 outof_input, into_input, op1,
1824 outof_target, into_target,
1825 unsignedp, next_methods, shift_mask))
1827 insns = get_insns ();
1828 end_sequence ();
1830 emit_insn (insns);
1831 return target;
1833 end_sequence ();
1837 /* Synthesize double word rotates from single word shifts. */
1838 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1839 && mclass == MODE_INT
1840 && CONST_INT_P (op1)
1841 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1842 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1843 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1845 rtx_insn *insns;
1846 rtx into_target, outof_target;
1847 rtx into_input, outof_input;
1848 rtx inter;
1849 int shift_count, left_shift, outof_word;
1851 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1852 won't be accurate, so use a new target. Do this also if target is not
1853 a REG, first because having a register instead may open optimization
1854 opportunities, and second because if target and op0 happen to be MEMs
1855 designating the same location, we would risk clobbering it too early
1856 in the code sequence we generate below. */
1857 if (target == 0
1858 || target == op0
1859 || target == op1
1860 || !REG_P (target)
1861 || !valid_multiword_target_p (target))
1862 target = gen_reg_rtx (mode);
1864 start_sequence ();
1866 shift_count = INTVAL (op1);
1868 /* OUTOF_* is the word we are shifting bits away from, and
1869 INTO_* is the word that we are shifting bits towards, thus
1870 they differ depending on the direction of the shift and
1871 WORDS_BIG_ENDIAN. */
1873 left_shift = (binoptab == rotl_optab);
1874 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1876 outof_target = operand_subword (target, outof_word, 1, mode);
1877 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1879 outof_input = operand_subword_force (op0, outof_word, mode);
1880 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1882 if (shift_count == BITS_PER_WORD)
1884 /* This is just a word swap. */
1885 emit_move_insn (outof_target, into_input);
1886 emit_move_insn (into_target, outof_input);
1887 inter = const0_rtx;
1889 else
1891 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1892 rtx first_shift_count, second_shift_count;
1893 optab reverse_unsigned_shift, unsigned_shift;
1895 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1896 ? lshr_optab : ashl_optab);
1898 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1899 ? ashl_optab : lshr_optab);
1901 if (shift_count > BITS_PER_WORD)
1903 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1904 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1906 else
1908 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1909 second_shift_count = GEN_INT (shift_count);
1912 into_temp1 = expand_binop (word_mode, unsigned_shift,
1913 outof_input, first_shift_count,
1914 NULL_RTX, unsignedp, next_methods);
1915 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1916 into_input, second_shift_count,
1917 NULL_RTX, unsignedp, next_methods);
1919 if (into_temp1 != 0 && into_temp2 != 0)
1920 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1921 into_target, unsignedp, next_methods);
1922 else
1923 inter = 0;
1925 if (inter != 0 && inter != into_target)
1926 emit_move_insn (into_target, inter);
1928 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1929 into_input, first_shift_count,
1930 NULL_RTX, unsignedp, next_methods);
1931 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1932 outof_input, second_shift_count,
1933 NULL_RTX, unsignedp, next_methods);
1935 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1936 inter = expand_binop (word_mode, ior_optab,
1937 outof_temp1, outof_temp2,
1938 outof_target, unsignedp, next_methods);
1940 if (inter != 0 && inter != outof_target)
1941 emit_move_insn (outof_target, inter);
1944 insns = get_insns ();
1945 end_sequence ();
1947 if (inter != 0)
1949 emit_insn (insns);
1950 return target;
1954 /* These can be done a word at a time by propagating carries. */
1955 if ((binoptab == add_optab || binoptab == sub_optab)
1956 && mclass == MODE_INT
1957 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1958 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1960 unsigned int i;
1961 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1962 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1963 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1964 rtx xop0, xop1, xtarget;
1966 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1967 value is one of those, use it. Otherwise, use 1 since it is the
1968 one easiest to get. */
1969 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1970 int normalizep = STORE_FLAG_VALUE;
1971 #else
1972 int normalizep = 1;
1973 #endif
1975 /* Prepare the operands. */
1976 xop0 = force_reg (mode, op0);
1977 xop1 = force_reg (mode, op1);
1979 xtarget = gen_reg_rtx (mode);
1981 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1982 target = xtarget;
1984 /* Indicate for flow that the entire target reg is being set. */
1985 if (REG_P (target))
1986 emit_clobber (xtarget);
1988 /* Do the actual arithmetic. */
1989 for (i = 0; i < nwords; i++)
1991 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1992 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1993 rtx op0_piece = operand_subword_force (xop0, index, mode);
1994 rtx op1_piece = operand_subword_force (xop1, index, mode);
1995 rtx x;
1997 /* Main add/subtract of the input operands. */
1998 x = expand_binop (word_mode, binoptab,
1999 op0_piece, op1_piece,
2000 target_piece, unsignedp, next_methods);
2001 if (x == 0)
2002 break;
2004 if (i + 1 < nwords)
2006 /* Store carry from main add/subtract. */
2007 carry_out = gen_reg_rtx (word_mode);
2008 carry_out = emit_store_flag_force (carry_out,
2009 (binoptab == add_optab
2010 ? LT : GT),
2011 x, op0_piece,
2012 word_mode, 1, normalizep);
2015 if (i > 0)
2017 rtx newx;
2019 /* Add/subtract previous carry to main result. */
2020 newx = expand_binop (word_mode,
2021 normalizep == 1 ? binoptab : otheroptab,
2022 x, carry_in,
2023 NULL_RTX, 1, next_methods);
2025 if (i + 1 < nwords)
2027 /* Get out carry from adding/subtracting carry in. */
2028 rtx carry_tmp = gen_reg_rtx (word_mode);
2029 carry_tmp = emit_store_flag_force (carry_tmp,
2030 (binoptab == add_optab
2031 ? LT : GT),
2032 newx, x,
2033 word_mode, 1, normalizep);
2035 /* Logical-ior the two poss. carry together. */
2036 carry_out = expand_binop (word_mode, ior_optab,
2037 carry_out, carry_tmp,
2038 carry_out, 0, next_methods);
2039 if (carry_out == 0)
2040 break;
2042 emit_move_insn (target_piece, newx);
2044 else
2046 if (x != target_piece)
2047 emit_move_insn (target_piece, x);
2050 carry_in = carry_out;
2053 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2055 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2056 || ! rtx_equal_p (target, xtarget))
2058 rtx temp = emit_move_insn (target, xtarget);
2060 set_dst_reg_note (temp, REG_EQUAL,
2061 gen_rtx_fmt_ee (optab_to_code (binoptab),
2062 mode, copy_rtx (xop0),
2063 copy_rtx (xop1)),
2064 target);
2066 else
2067 target = xtarget;
2069 return target;
2072 else
2073 delete_insns_since (last);
2076 /* Attempt to synthesize double word multiplies using a sequence of word
2077 mode multiplications. We first attempt to generate a sequence using a
2078 more efficient unsigned widening multiply, and if that fails we then
2079 try using a signed widening multiply. */
2081 if (binoptab == smul_optab
2082 && mclass == MODE_INT
2083 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2084 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2085 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2087 rtx product = NULL_RTX;
2088 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
2089 != CODE_FOR_nothing)
2091 product = expand_doubleword_mult (mode, op0, op1, target,
2092 true, methods);
2093 if (!product)
2094 delete_insns_since (last);
2097 if (product == NULL_RTX
2098 && widening_optab_handler (smul_widen_optab, mode, word_mode)
2099 != CODE_FOR_nothing)
2101 product = expand_doubleword_mult (mode, op0, op1, target,
2102 false, methods);
2103 if (!product)
2104 delete_insns_since (last);
2107 if (product != NULL_RTX)
2109 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2111 temp = emit_move_insn (target ? target : product, product);
2112 set_dst_reg_note (temp,
2113 REG_EQUAL,
2114 gen_rtx_fmt_ee (MULT, mode,
2115 copy_rtx (op0),
2116 copy_rtx (op1)),
2117 target ? target : product);
2119 return product;
2123 /* It can't be open-coded in this mode.
2124 Use a library call if one is available and caller says that's ok. */
2126 libfunc = optab_libfunc (binoptab, mode);
2127 if (libfunc
2128 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2130 rtx_insn *insns;
2131 rtx op1x = op1;
2132 machine_mode op1_mode = mode;
2133 rtx value;
2135 start_sequence ();
2137 if (shift_optab_p (binoptab))
2139 op1_mode = targetm.libgcc_shift_count_mode ();
2140 /* Specify unsigned here,
2141 since negative shift counts are meaningless. */
2142 op1x = convert_to_mode (op1_mode, op1, 1);
2145 if (GET_MODE (op0) != VOIDmode
2146 && GET_MODE (op0) != mode)
2147 op0 = convert_to_mode (mode, op0, unsignedp);
2149 /* Pass 1 for NO_QUEUE so we don't lose any increments
2150 if the libcall is cse'd or moved. */
2151 value = emit_library_call_value (libfunc,
2152 NULL_RTX, LCT_CONST, mode, 2,
2153 op0, mode, op1x, op1_mode);
2155 insns = get_insns ();
2156 end_sequence ();
2158 target = gen_reg_rtx (mode);
2159 emit_libcall_block_1 (insns, target, value,
2160 gen_rtx_fmt_ee (optab_to_code (binoptab),
2161 mode, op0, op1),
2162 trapv_binoptab_p (binoptab));
2164 return target;
2167 delete_insns_since (last);
2169 /* It can't be done in this mode. Can we do it in a wider mode? */
2171 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2172 || methods == OPTAB_MUST_WIDEN))
2174 /* Caller says, don't even try. */
2175 delete_insns_since (entry_last);
2176 return 0;
2179 /* Compute the value of METHODS to pass to recursive calls.
2180 Don't allow widening to be tried recursively. */
2182 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2184 /* Look for a wider mode of the same class for which it appears we can do
2185 the operation. */
2187 if (CLASS_HAS_WIDER_MODES_P (mclass))
2189 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2190 wider_mode != VOIDmode;
2191 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2193 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2194 != CODE_FOR_nothing
2195 || (methods == OPTAB_LIB
2196 && optab_libfunc (binoptab, wider_mode)))
2198 rtx xop0 = op0, xop1 = op1;
2199 int no_extend = 0;
2201 /* For certain integer operations, we need not actually extend
2202 the narrow operands, as long as we will truncate
2203 the results to the same narrowness. */
2205 if ((binoptab == ior_optab || binoptab == and_optab
2206 || binoptab == xor_optab
2207 || binoptab == add_optab || binoptab == sub_optab
2208 || binoptab == smul_optab || binoptab == ashl_optab)
2209 && mclass == MODE_INT)
2210 no_extend = 1;
2212 xop0 = widen_operand (xop0, wider_mode, mode,
2213 unsignedp, no_extend);
2215 /* The second operand of a shift must always be extended. */
2216 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2217 no_extend && binoptab != ashl_optab);
2219 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2220 unsignedp, methods);
2221 if (temp)
2223 if (mclass != MODE_INT
2224 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2226 if (target == 0)
2227 target = gen_reg_rtx (mode);
2228 convert_move (target, temp, 0);
2229 return target;
2231 else
2232 return gen_lowpart (mode, temp);
2234 else
2235 delete_insns_since (last);
2240 delete_insns_since (entry_last);
2241 return 0;
2244 /* Expand a binary operator which has both signed and unsigned forms.
2245 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2246 signed operations.
2248 If we widen unsigned operands, we may use a signed wider operation instead
2249 of an unsigned wider operation, since the result would be the same. */
2252 sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
2253 rtx op0, rtx op1, rtx target, int unsignedp,
2254 enum optab_methods methods)
2256 rtx temp;
2257 optab direct_optab = unsignedp ? uoptab : soptab;
2258 bool save_enable;
2260 /* Do it without widening, if possible. */
2261 temp = expand_binop (mode, direct_optab, op0, op1, target,
2262 unsignedp, OPTAB_DIRECT);
2263 if (temp || methods == OPTAB_DIRECT)
2264 return temp;
2266 /* Try widening to a signed int. Disable any direct use of any
2267 signed insn in the current mode. */
2268 save_enable = swap_optab_enable (soptab, mode, false);
2270 temp = expand_binop (mode, soptab, op0, op1, target,
2271 unsignedp, OPTAB_WIDEN);
2273 /* For unsigned operands, try widening to an unsigned int. */
2274 if (!temp && unsignedp)
2275 temp = expand_binop (mode, uoptab, op0, op1, target,
2276 unsignedp, OPTAB_WIDEN);
2277 if (temp || methods == OPTAB_WIDEN)
2278 goto egress;
2280 /* Use the right width libcall if that exists. */
2281 temp = expand_binop (mode, direct_optab, op0, op1, target,
2282 unsignedp, OPTAB_LIB);
2283 if (temp || methods == OPTAB_LIB)
2284 goto egress;
2286 /* Must widen and use a libcall, use either signed or unsigned. */
2287 temp = expand_binop (mode, soptab, op0, op1, target,
2288 unsignedp, methods);
2289 if (!temp && unsignedp)
2290 temp = expand_binop (mode, uoptab, op0, op1, target,
2291 unsignedp, methods);
2293 egress:
2294 /* Undo the fiddling above. */
2295 if (save_enable)
2296 swap_optab_enable (soptab, mode, true);
2297 return temp;
2300 /* Generate code to perform an operation specified by UNOPPTAB
2301 on operand OP0, with two results to TARG0 and TARG1.
2302 We assume that the order of the operands for the instruction
2303 is TARG0, TARG1, OP0.
2305 Either TARG0 or TARG1 may be zero, but what that means is that
2306 the result is not actually wanted. We will generate it into
2307 a dummy pseudo-reg and discard it. They may not both be zero.
2309 Returns 1 if this operation can be performed; 0 if not. */
2312 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2313 int unsignedp)
2315 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2316 enum mode_class mclass;
2317 machine_mode wider_mode;
2318 rtx_insn *entry_last = get_last_insn ();
2319 rtx_insn *last;
2321 mclass = GET_MODE_CLASS (mode);
2323 if (!targ0)
2324 targ0 = gen_reg_rtx (mode);
2325 if (!targ1)
2326 targ1 = gen_reg_rtx (mode);
2328 /* Record where to go back to if we fail. */
2329 last = get_last_insn ();
2331 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2333 struct expand_operand ops[3];
2334 enum insn_code icode = optab_handler (unoptab, mode);
2336 create_fixed_operand (&ops[0], targ0);
2337 create_fixed_operand (&ops[1], targ1);
2338 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2339 if (maybe_expand_insn (icode, 3, ops))
2340 return 1;
2343 /* It can't be done in this mode. Can we do it in a wider mode? */
2345 if (CLASS_HAS_WIDER_MODES_P (mclass))
2347 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2348 wider_mode != VOIDmode;
2349 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2351 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2353 rtx t0 = gen_reg_rtx (wider_mode);
2354 rtx t1 = gen_reg_rtx (wider_mode);
2355 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2357 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2359 convert_move (targ0, t0, unsignedp);
2360 convert_move (targ1, t1, unsignedp);
2361 return 1;
2363 else
2364 delete_insns_since (last);
2369 delete_insns_since (entry_last);
2370 return 0;
2373 /* Generate code to perform an operation specified by BINOPTAB
2374 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2375 We assume that the order of the operands for the instruction
2376 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2377 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2379 Either TARG0 or TARG1 may be zero, but what that means is that
2380 the result is not actually wanted. We will generate it into
2381 a dummy pseudo-reg and discard it. They may not both be zero.
2383 Returns 1 if this operation can be performed; 0 if not. */
2386 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2387 int unsignedp)
2389 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2390 enum mode_class mclass;
2391 machine_mode wider_mode;
2392 rtx_insn *entry_last = get_last_insn ();
2393 rtx_insn *last;
2395 mclass = GET_MODE_CLASS (mode);
2397 if (!targ0)
2398 targ0 = gen_reg_rtx (mode);
2399 if (!targ1)
2400 targ1 = gen_reg_rtx (mode);
2402 /* Record where to go back to if we fail. */
2403 last = get_last_insn ();
2405 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2407 struct expand_operand ops[4];
2408 enum insn_code icode = optab_handler (binoptab, mode);
2409 machine_mode mode0 = insn_data[icode].operand[1].mode;
2410 machine_mode mode1 = insn_data[icode].operand[2].mode;
2411 rtx xop0 = op0, xop1 = op1;
2413 /* If we are optimizing, force expensive constants into a register. */
2414 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2415 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2417 create_fixed_operand (&ops[0], targ0);
2418 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2419 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2420 create_fixed_operand (&ops[3], targ1);
2421 if (maybe_expand_insn (icode, 4, ops))
2422 return 1;
2423 delete_insns_since (last);
2426 /* It can't be done in this mode. Can we do it in a wider mode? */
2428 if (CLASS_HAS_WIDER_MODES_P (mclass))
2430 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2431 wider_mode != VOIDmode;
2432 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2434 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2436 rtx t0 = gen_reg_rtx (wider_mode);
2437 rtx t1 = gen_reg_rtx (wider_mode);
2438 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2439 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2441 if (expand_twoval_binop (binoptab, cop0, cop1,
2442 t0, t1, unsignedp))
2444 convert_move (targ0, t0, unsignedp);
2445 convert_move (targ1, t1, unsignedp);
2446 return 1;
2448 else
2449 delete_insns_since (last);
2454 delete_insns_since (entry_last);
2455 return 0;
2458 /* Expand the two-valued library call indicated by BINOPTAB, but
2459 preserve only one of the values. If TARG0 is non-NULL, the first
2460 value is placed into TARG0; otherwise the second value is placed
2461 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2462 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2463 This routine assumes that the value returned by the library call is
2464 as if the return value was of an integral mode twice as wide as the
2465 mode of OP0. Returns 1 if the call was successful. */
2467 bool
2468 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2469 rtx targ0, rtx targ1, enum rtx_code code)
2471 machine_mode mode;
2472 machine_mode libval_mode;
2473 rtx libval;
2474 rtx_insn *insns;
2475 rtx libfunc;
2477 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2478 gcc_assert (!targ0 != !targ1);
2480 mode = GET_MODE (op0);
2481 libfunc = optab_libfunc (binoptab, mode);
2482 if (!libfunc)
2483 return false;
2485 /* The value returned by the library function will have twice as
2486 many bits as the nominal MODE. */
2487 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2488 MODE_INT);
2489 start_sequence ();
2490 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2491 libval_mode, 2,
2492 op0, mode,
2493 op1, mode);
2494 /* Get the part of VAL containing the value that we want. */
2495 libval = simplify_gen_subreg (mode, libval, libval_mode,
2496 targ0 ? 0 : GET_MODE_SIZE (mode));
2497 insns = get_insns ();
2498 end_sequence ();
2499 /* Move the into the desired location. */
2500 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2501 gen_rtx_fmt_ee (code, mode, op0, op1));
2503 return true;
2507 /* Wrapper around expand_unop which takes an rtx code to specify
2508 the operation to perform, not an optab pointer. All other
2509 arguments are the same. */
2511 expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
2512 rtx target, int unsignedp)
2514 optab unop = code_to_optab (code);
2515 gcc_assert (unop);
2517 return expand_unop (mode, unop, op0, target, unsignedp);
2520 /* Try calculating
2521 (clz:narrow x)
2523 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2525 A similar operation can be used for clrsb. UNOPTAB says which operation
2526 we are trying to expand. */
2527 static rtx
2528 widen_leading (machine_mode mode, rtx op0, rtx target, optab unoptab)
2530 enum mode_class mclass = GET_MODE_CLASS (mode);
2531 if (CLASS_HAS_WIDER_MODES_P (mclass))
2533 machine_mode wider_mode;
2534 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2535 wider_mode != VOIDmode;
2536 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2538 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2540 rtx xop0, temp;
2541 rtx_insn *last;
2543 last = get_last_insn ();
2545 if (target == 0)
2546 target = gen_reg_rtx (mode);
2547 xop0 = widen_operand (op0, wider_mode, mode,
2548 unoptab != clrsb_optab, false);
2549 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2550 unoptab != clrsb_optab);
2551 if (temp != 0)
2552 temp = expand_binop
2553 (wider_mode, sub_optab, temp,
2554 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2555 - GET_MODE_PRECISION (mode),
2556 wider_mode),
2557 target, true, OPTAB_DIRECT);
2558 if (temp == 0)
2559 delete_insns_since (last);
2561 return temp;
2565 return 0;
2568 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2569 quantities, choosing which based on whether the high word is nonzero. */
2570 static rtx
2571 expand_doubleword_clz (machine_mode mode, rtx op0, rtx target)
2573 rtx xop0 = force_reg (mode, op0);
2574 rtx subhi = gen_highpart (word_mode, xop0);
2575 rtx sublo = gen_lowpart (word_mode, xop0);
2576 rtx_code_label *hi0_label = gen_label_rtx ();
2577 rtx_code_label *after_label = gen_label_rtx ();
2578 rtx_insn *seq;
2579 rtx temp, result;
2581 /* If we were not given a target, use a word_mode register, not a
2582 'mode' register. The result will fit, and nobody is expecting
2583 anything bigger (the return type of __builtin_clz* is int). */
2584 if (!target)
2585 target = gen_reg_rtx (word_mode);
2587 /* In any case, write to a word_mode scratch in both branches of the
2588 conditional, so we can ensure there is a single move insn setting
2589 'target' to tag a REG_EQUAL note on. */
2590 result = gen_reg_rtx (word_mode);
2592 start_sequence ();
2594 /* If the high word is not equal to zero,
2595 then clz of the full value is clz of the high word. */
2596 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2597 word_mode, true, hi0_label);
2599 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2600 if (!temp)
2601 goto fail;
2603 if (temp != result)
2604 convert_move (result, temp, true);
2606 emit_jump_insn (gen_jump (after_label));
2607 emit_barrier ();
2609 /* Else clz of the full value is clz of the low word plus the number
2610 of bits in the high word. */
2611 emit_label (hi0_label);
2613 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2614 if (!temp)
2615 goto fail;
2616 temp = expand_binop (word_mode, add_optab, temp,
2617 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2618 result, true, OPTAB_DIRECT);
2619 if (!temp)
2620 goto fail;
2621 if (temp != result)
2622 convert_move (result, temp, true);
2624 emit_label (after_label);
2625 convert_move (target, result, true);
2627 seq = get_insns ();
2628 end_sequence ();
2630 add_equal_note (seq, target, CLZ, xop0, 0);
2631 emit_insn (seq);
2632 return target;
2634 fail:
2635 end_sequence ();
2636 return 0;
2639 /* Try calculating
2640 (bswap:narrow x)
2642 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2643 static rtx
2644 widen_bswap (machine_mode mode, rtx op0, rtx target)
2646 enum mode_class mclass = GET_MODE_CLASS (mode);
2647 machine_mode wider_mode;
2648 rtx x;
2649 rtx_insn *last;
2651 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2652 return NULL_RTX;
2654 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2655 wider_mode != VOIDmode;
2656 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2657 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2658 goto found;
2659 return NULL_RTX;
2661 found:
2662 last = get_last_insn ();
2664 x = widen_operand (op0, wider_mode, mode, true, true);
2665 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2667 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2668 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2669 if (x != 0)
2670 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2671 GET_MODE_BITSIZE (wider_mode)
2672 - GET_MODE_BITSIZE (mode),
2673 NULL_RTX, true);
2675 if (x != 0)
2677 if (target == 0)
2678 target = gen_reg_rtx (mode);
2679 emit_move_insn (target, gen_lowpart (mode, x));
2681 else
2682 delete_insns_since (last);
2684 return target;
2687 /* Try calculating bswap as two bswaps of two word-sized operands. */
2689 static rtx
2690 expand_doubleword_bswap (machine_mode mode, rtx op, rtx target)
2692 rtx t0, t1;
2694 t1 = expand_unop (word_mode, bswap_optab,
2695 operand_subword_force (op, 0, mode), NULL_RTX, true);
2696 t0 = expand_unop (word_mode, bswap_optab,
2697 operand_subword_force (op, 1, mode), NULL_RTX, true);
2699 if (target == 0 || !valid_multiword_target_p (target))
2700 target = gen_reg_rtx (mode);
2701 if (REG_P (target))
2702 emit_clobber (target);
2703 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2704 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2706 return target;
2709 /* Try calculating (parity x) as (and (popcount x) 1), where
2710 popcount can also be done in a wider mode. */
2711 static rtx
2712 expand_parity (machine_mode mode, rtx op0, rtx target)
2714 enum mode_class mclass = GET_MODE_CLASS (mode);
2715 if (CLASS_HAS_WIDER_MODES_P (mclass))
2717 machine_mode wider_mode;
2718 for (wider_mode = mode; wider_mode != VOIDmode;
2719 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2721 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2723 rtx xop0, temp;
2724 rtx_insn *last;
2726 last = get_last_insn ();
2728 if (target == 0)
2729 target = gen_reg_rtx (mode);
2730 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2731 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2732 true);
2733 if (temp != 0)
2734 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2735 target, true, OPTAB_DIRECT);
2736 if (temp == 0)
2737 delete_insns_since (last);
2739 return temp;
2743 return 0;
2746 /* Try calculating ctz(x) as K - clz(x & -x) ,
2747 where K is GET_MODE_PRECISION(mode) - 1.
2749 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2750 don't have to worry about what the hardware does in that case. (If
2751 the clz instruction produces the usual value at 0, which is K, the
2752 result of this code sequence will be -1; expand_ffs, below, relies
2753 on this. It might be nice to have it be K instead, for consistency
2754 with the (very few) processors that provide a ctz with a defined
2755 value, but that would take one more instruction, and it would be
2756 less convenient for expand_ffs anyway. */
2758 static rtx
2759 expand_ctz (machine_mode mode, rtx op0, rtx target)
2761 rtx_insn *seq;
2762 rtx temp;
2764 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2765 return 0;
2767 start_sequence ();
2769 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2770 if (temp)
2771 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2772 true, OPTAB_DIRECT);
2773 if (temp)
2774 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2775 if (temp)
2776 temp = expand_binop (mode, sub_optab,
2777 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2778 temp, target,
2779 true, OPTAB_DIRECT);
2780 if (temp == 0)
2782 end_sequence ();
2783 return 0;
2786 seq = get_insns ();
2787 end_sequence ();
2789 add_equal_note (seq, temp, CTZ, op0, 0);
2790 emit_insn (seq);
2791 return temp;
2795 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2796 else with the sequence used by expand_clz.
2798 The ffs builtin promises to return zero for a zero value and ctz/clz
2799 may have an undefined value in that case. If they do not give us a
2800 convenient value, we have to generate a test and branch. */
2801 static rtx
2802 expand_ffs (machine_mode mode, rtx op0, rtx target)
2804 HOST_WIDE_INT val = 0;
2805 bool defined_at_zero = false;
2806 rtx temp;
2807 rtx_insn *seq;
2809 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2811 start_sequence ();
2813 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2814 if (!temp)
2815 goto fail;
2817 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2819 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2821 start_sequence ();
2822 temp = expand_ctz (mode, op0, 0);
2823 if (!temp)
2824 goto fail;
2826 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2828 defined_at_zero = true;
2829 val = (GET_MODE_PRECISION (mode) - 1) - val;
2832 else
2833 return 0;
2835 if (defined_at_zero && val == -1)
2836 /* No correction needed at zero. */;
2837 else
2839 /* We don't try to do anything clever with the situation found
2840 on some processors (eg Alpha) where ctz(0:mode) ==
2841 bitsize(mode). If someone can think of a way to send N to -1
2842 and leave alone all values in the range 0..N-1 (where N is a
2843 power of two), cheaper than this test-and-branch, please add it.
2845 The test-and-branch is done after the operation itself, in case
2846 the operation sets condition codes that can be recycled for this.
2847 (This is true on i386, for instance.) */
2849 rtx_code_label *nonzero_label = gen_label_rtx ();
2850 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2851 mode, true, nonzero_label);
2853 convert_move (temp, GEN_INT (-1), false);
2854 emit_label (nonzero_label);
2857 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2858 to produce a value in the range 0..bitsize. */
2859 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2860 target, false, OPTAB_DIRECT);
2861 if (!temp)
2862 goto fail;
2864 seq = get_insns ();
2865 end_sequence ();
2867 add_equal_note (seq, temp, FFS, op0, 0);
2868 emit_insn (seq);
2869 return temp;
2871 fail:
2872 end_sequence ();
2873 return 0;
2876 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2877 conditions, VAL may already be a SUBREG against which we cannot generate
2878 a further SUBREG. In this case, we expect forcing the value into a
2879 register will work around the situation. */
2881 static rtx
2882 lowpart_subreg_maybe_copy (machine_mode omode, rtx val,
2883 machine_mode imode)
2885 rtx ret;
2886 ret = lowpart_subreg (omode, val, imode);
2887 if (ret == NULL)
2889 val = force_reg (imode, val);
2890 ret = lowpart_subreg (omode, val, imode);
2891 gcc_assert (ret != NULL);
2893 return ret;
2896 /* Expand a floating point absolute value or negation operation via a
2897 logical operation on the sign bit. */
2899 static rtx
2900 expand_absneg_bit (enum rtx_code code, machine_mode mode,
2901 rtx op0, rtx target)
2903 const struct real_format *fmt;
2904 int bitpos, word, nwords, i;
2905 machine_mode imode;
2906 rtx temp;
2907 rtx_insn *insns;
2909 /* The format has to have a simple sign bit. */
2910 fmt = REAL_MODE_FORMAT (mode);
2911 if (fmt == NULL)
2912 return NULL_RTX;
2914 bitpos = fmt->signbit_rw;
2915 if (bitpos < 0)
2916 return NULL_RTX;
2918 /* Don't create negative zeros if the format doesn't support them. */
2919 if (code == NEG && !fmt->has_signed_zero)
2920 return NULL_RTX;
2922 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2924 imode = int_mode_for_mode (mode);
2925 if (imode == BLKmode)
2926 return NULL_RTX;
2927 word = 0;
2928 nwords = 1;
2930 else
2932 imode = word_mode;
2934 if (FLOAT_WORDS_BIG_ENDIAN)
2935 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2936 else
2937 word = bitpos / BITS_PER_WORD;
2938 bitpos = bitpos % BITS_PER_WORD;
2939 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2942 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
2943 if (code == ABS)
2944 mask = ~mask;
2946 if (target == 0
2947 || target == op0
2948 || (nwords > 1 && !valid_multiword_target_p (target)))
2949 target = gen_reg_rtx (mode);
2951 if (nwords > 1)
2953 start_sequence ();
2955 for (i = 0; i < nwords; ++i)
2957 rtx targ_piece = operand_subword (target, i, 1, mode);
2958 rtx op0_piece = operand_subword_force (op0, i, mode);
2960 if (i == word)
2962 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2963 op0_piece,
2964 immed_wide_int_const (mask, imode),
2965 targ_piece, 1, OPTAB_LIB_WIDEN);
2966 if (temp != targ_piece)
2967 emit_move_insn (targ_piece, temp);
2969 else
2970 emit_move_insn (targ_piece, op0_piece);
2973 insns = get_insns ();
2974 end_sequence ();
2976 emit_insn (insns);
2978 else
2980 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2981 gen_lowpart (imode, op0),
2982 immed_wide_int_const (mask, imode),
2983 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2984 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2986 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2987 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2988 target);
2991 return target;
2994 /* As expand_unop, but will fail rather than attempt the operation in a
2995 different mode or with a libcall. */
2996 static rtx
2997 expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
2998 int unsignedp)
3000 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
3002 struct expand_operand ops[2];
3003 enum insn_code icode = optab_handler (unoptab, mode);
3004 rtx_insn *last = get_last_insn ();
3005 rtx pat;
3007 create_output_operand (&ops[0], target, mode);
3008 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
3009 pat = maybe_gen_insn (icode, 2, ops);
3010 if (pat)
3012 if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
3013 && ! add_equal_note (as_a <rtx_insn *> (pat), ops[0].value,
3014 optab_to_code (unoptab),
3015 ops[1].value, NULL_RTX))
3017 delete_insns_since (last);
3018 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3021 emit_insn (pat);
3023 return ops[0].value;
3026 return 0;
3029 /* Generate code to perform an operation specified by UNOPTAB
3030 on operand OP0, with result having machine-mode MODE.
3032 UNSIGNEDP is for the case where we have to widen the operands
3033 to perform the operation. It says to use zero-extension.
3035 If TARGET is nonzero, the value
3036 is generated there, if it is convenient to do so.
3037 In all cases an rtx is returned for the locus of the value;
3038 this may or may not be TARGET. */
3041 expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
3042 int unsignedp)
3044 enum mode_class mclass = GET_MODE_CLASS (mode);
3045 machine_mode wider_mode;
3046 rtx temp;
3047 rtx libfunc;
3049 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3050 if (temp)
3051 return temp;
3053 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3055 /* Widening (or narrowing) clz needs special treatment. */
3056 if (unoptab == clz_optab)
3058 temp = widen_leading (mode, op0, target, unoptab);
3059 if (temp)
3060 return temp;
3062 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3063 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3065 temp = expand_doubleword_clz (mode, op0, target);
3066 if (temp)
3067 return temp;
3070 goto try_libcall;
3073 if (unoptab == clrsb_optab)
3075 temp = widen_leading (mode, op0, target, unoptab);
3076 if (temp)
3077 return temp;
3078 goto try_libcall;
3081 /* Widening (or narrowing) bswap needs special treatment. */
3082 if (unoptab == bswap_optab)
3084 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3085 or ROTATERT. First try these directly; if this fails, then try the
3086 obvious pair of shifts with allowed widening, as this will probably
3087 be always more efficient than the other fallback methods. */
3088 if (mode == HImode)
3090 rtx_insn *last;
3091 rtx temp1, temp2;
3093 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3095 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
3096 unsignedp, OPTAB_DIRECT);
3097 if (temp)
3098 return temp;
3101 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3103 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
3104 unsignedp, OPTAB_DIRECT);
3105 if (temp)
3106 return temp;
3109 last = get_last_insn ();
3111 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
3112 unsignedp, OPTAB_WIDEN);
3113 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
3114 unsignedp, OPTAB_WIDEN);
3115 if (temp1 && temp2)
3117 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3118 unsignedp, OPTAB_WIDEN);
3119 if (temp)
3120 return temp;
3123 delete_insns_since (last);
3126 temp = widen_bswap (mode, op0, target);
3127 if (temp)
3128 return temp;
3130 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3131 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3133 temp = expand_doubleword_bswap (mode, op0, target);
3134 if (temp)
3135 return temp;
3138 goto try_libcall;
3141 if (CLASS_HAS_WIDER_MODES_P (mclass))
3142 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3143 wider_mode != VOIDmode;
3144 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3146 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3148 rtx xop0 = op0;
3149 rtx_insn *last = get_last_insn ();
3151 /* For certain operations, we need not actually extend
3152 the narrow operand, as long as we will truncate the
3153 results to the same narrowness. */
3155 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3156 (unoptab == neg_optab
3157 || unoptab == one_cmpl_optab)
3158 && mclass == MODE_INT);
3160 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3161 unsignedp);
3163 if (temp)
3165 if (mclass != MODE_INT
3166 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3168 if (target == 0)
3169 target = gen_reg_rtx (mode);
3170 convert_move (target, temp, 0);
3171 return target;
3173 else
3174 return gen_lowpart (mode, temp);
3176 else
3177 delete_insns_since (last);
3181 /* These can be done a word at a time. */
3182 if (unoptab == one_cmpl_optab
3183 && mclass == MODE_INT
3184 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3185 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3187 int i;
3188 rtx_insn *insns;
3190 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3191 target = gen_reg_rtx (mode);
3193 start_sequence ();
3195 /* Do the actual arithmetic. */
3196 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3198 rtx target_piece = operand_subword (target, i, 1, mode);
3199 rtx x = expand_unop (word_mode, unoptab,
3200 operand_subword_force (op0, i, mode),
3201 target_piece, unsignedp);
3203 if (target_piece != x)
3204 emit_move_insn (target_piece, x);
3207 insns = get_insns ();
3208 end_sequence ();
3210 emit_insn (insns);
3211 return target;
3214 if (optab_to_code (unoptab) == NEG)
3216 /* Try negating floating point values by flipping the sign bit. */
3217 if (SCALAR_FLOAT_MODE_P (mode))
3219 temp = expand_absneg_bit (NEG, mode, op0, target);
3220 if (temp)
3221 return temp;
3224 /* If there is no negation pattern, and we have no negative zero,
3225 try subtracting from zero. */
3226 if (!HONOR_SIGNED_ZEROS (mode))
3228 temp = expand_binop (mode, (unoptab == negv_optab
3229 ? subv_optab : sub_optab),
3230 CONST0_RTX (mode), op0, target,
3231 unsignedp, OPTAB_DIRECT);
3232 if (temp)
3233 return temp;
3237 /* Try calculating parity (x) as popcount (x) % 2. */
3238 if (unoptab == parity_optab)
3240 temp = expand_parity (mode, op0, target);
3241 if (temp)
3242 return temp;
3245 /* Try implementing ffs (x) in terms of clz (x). */
3246 if (unoptab == ffs_optab)
3248 temp = expand_ffs (mode, op0, target);
3249 if (temp)
3250 return temp;
3253 /* Try implementing ctz (x) in terms of clz (x). */
3254 if (unoptab == ctz_optab)
3256 temp = expand_ctz (mode, op0, target);
3257 if (temp)
3258 return temp;
3261 try_libcall:
3262 /* Now try a library call in this mode. */
3263 libfunc = optab_libfunc (unoptab, mode);
3264 if (libfunc)
3266 rtx_insn *insns;
3267 rtx value;
3268 rtx eq_value;
3269 machine_mode outmode = mode;
3271 /* All of these functions return small values. Thus we choose to
3272 have them return something that isn't a double-word. */
3273 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3274 || unoptab == clrsb_optab || unoptab == popcount_optab
3275 || unoptab == parity_optab)
3276 outmode
3277 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3278 optab_libfunc (unoptab, mode)));
3280 start_sequence ();
3282 /* Pass 1 for NO_QUEUE so we don't lose any increments
3283 if the libcall is cse'd or moved. */
3284 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3285 1, op0, mode);
3286 insns = get_insns ();
3287 end_sequence ();
3289 target = gen_reg_rtx (outmode);
3290 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3291 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3292 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3293 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3294 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3295 emit_libcall_block_1 (insns, target, value, eq_value,
3296 trapv_unoptab_p (unoptab));
3298 return target;
3301 /* It can't be done in this mode. Can we do it in a wider mode? */
3303 if (CLASS_HAS_WIDER_MODES_P (mclass))
3305 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3306 wider_mode != VOIDmode;
3307 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3309 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3310 || optab_libfunc (unoptab, wider_mode))
3312 rtx xop0 = op0;
3313 rtx_insn *last = get_last_insn ();
3315 /* For certain operations, we need not actually extend
3316 the narrow operand, as long as we will truncate the
3317 results to the same narrowness. */
3318 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3319 (unoptab == neg_optab
3320 || unoptab == one_cmpl_optab
3321 || unoptab == bswap_optab)
3322 && mclass == MODE_INT);
3324 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3325 unsignedp);
3327 /* If we are generating clz using wider mode, adjust the
3328 result. Similarly for clrsb. */
3329 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3330 && temp != 0)
3331 temp = expand_binop
3332 (wider_mode, sub_optab, temp,
3333 gen_int_mode (GET_MODE_PRECISION (wider_mode)
3334 - GET_MODE_PRECISION (mode),
3335 wider_mode),
3336 target, true, OPTAB_DIRECT);
3338 /* Likewise for bswap. */
3339 if (unoptab == bswap_optab && temp != 0)
3341 gcc_assert (GET_MODE_PRECISION (wider_mode)
3342 == GET_MODE_BITSIZE (wider_mode)
3343 && GET_MODE_PRECISION (mode)
3344 == GET_MODE_BITSIZE (mode));
3346 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3347 GET_MODE_BITSIZE (wider_mode)
3348 - GET_MODE_BITSIZE (mode),
3349 NULL_RTX, true);
3352 if (temp)
3354 if (mclass != MODE_INT)
3356 if (target == 0)
3357 target = gen_reg_rtx (mode);
3358 convert_move (target, temp, 0);
3359 return target;
3361 else
3362 return gen_lowpart (mode, temp);
3364 else
3365 delete_insns_since (last);
3370 /* One final attempt at implementing negation via subtraction,
3371 this time allowing widening of the operand. */
3372 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3374 rtx temp;
3375 temp = expand_binop (mode,
3376 unoptab == negv_optab ? subv_optab : sub_optab,
3377 CONST0_RTX (mode), op0,
3378 target, unsignedp, OPTAB_LIB_WIDEN);
3379 if (temp)
3380 return temp;
3383 return 0;
3386 /* Emit code to compute the absolute value of OP0, with result to
3387 TARGET if convenient. (TARGET may be 0.) The return value says
3388 where the result actually is to be found.
3390 MODE is the mode of the operand; the mode of the result is
3391 different but can be deduced from MODE.
3396 expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
3397 int result_unsignedp)
3399 rtx temp;
3401 if (GET_MODE_CLASS (mode) != MODE_INT
3402 || ! flag_trapv)
3403 result_unsignedp = 1;
3405 /* First try to do it with a special abs instruction. */
3406 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3407 op0, target, 0);
3408 if (temp != 0)
3409 return temp;
3411 /* For floating point modes, try clearing the sign bit. */
3412 if (SCALAR_FLOAT_MODE_P (mode))
3414 temp = expand_absneg_bit (ABS, mode, op0, target);
3415 if (temp)
3416 return temp;
3419 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3420 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3421 && !HONOR_SIGNED_ZEROS (mode))
3423 rtx_insn *last = get_last_insn ();
3425 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3426 op0, NULL_RTX, 0);
3427 if (temp != 0)
3428 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3429 OPTAB_WIDEN);
3431 if (temp != 0)
3432 return temp;
3434 delete_insns_since (last);
3437 /* If this machine has expensive jumps, we can do integer absolute
3438 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3439 where W is the width of MODE. */
3441 if (GET_MODE_CLASS (mode) == MODE_INT
3442 && BRANCH_COST (optimize_insn_for_speed_p (),
3443 false) >= 2)
3445 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3446 GET_MODE_PRECISION (mode) - 1,
3447 NULL_RTX, 0);
3449 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3450 OPTAB_LIB_WIDEN);
3451 if (temp != 0)
3452 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3453 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3455 if (temp != 0)
3456 return temp;
3459 return NULL_RTX;
3463 expand_abs (machine_mode mode, rtx op0, rtx target,
3464 int result_unsignedp, int safe)
3466 rtx temp;
3467 rtx_code_label *op1;
3469 if (GET_MODE_CLASS (mode) != MODE_INT
3470 || ! flag_trapv)
3471 result_unsignedp = 1;
3473 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3474 if (temp != 0)
3475 return temp;
3477 /* If that does not win, use conditional jump and negate. */
3479 /* It is safe to use the target if it is the same
3480 as the source if this is also a pseudo register */
3481 if (op0 == target && REG_P (op0)
3482 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3483 safe = 1;
3485 op1 = gen_label_rtx ();
3486 if (target == 0 || ! safe
3487 || GET_MODE (target) != mode
3488 || (MEM_P (target) && MEM_VOLATILE_P (target))
3489 || (REG_P (target)
3490 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3491 target = gen_reg_rtx (mode);
3493 emit_move_insn (target, op0);
3494 NO_DEFER_POP;
3496 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3497 NULL_RTX, NULL_RTX, op1, -1);
3499 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3500 target, target, 0);
3501 if (op0 != target)
3502 emit_move_insn (target, op0);
3503 emit_label (op1);
3504 OK_DEFER_POP;
3505 return target;
3508 /* Emit code to compute the one's complement absolute value of OP0
3509 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3510 (TARGET may be NULL_RTX.) The return value says where the result
3511 actually is to be found.
3513 MODE is the mode of the operand; the mode of the result is
3514 different but can be deduced from MODE. */
3517 expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
3519 rtx temp;
3521 /* Not applicable for floating point modes. */
3522 if (FLOAT_MODE_P (mode))
3523 return NULL_RTX;
3525 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3526 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3528 rtx_insn *last = get_last_insn ();
3530 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3531 if (temp != 0)
3532 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3533 OPTAB_WIDEN);
3535 if (temp != 0)
3536 return temp;
3538 delete_insns_since (last);
3541 /* If this machine has expensive jumps, we can do one's complement
3542 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3544 if (GET_MODE_CLASS (mode) == MODE_INT
3545 && BRANCH_COST (optimize_insn_for_speed_p (),
3546 false) >= 2)
3548 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3549 GET_MODE_PRECISION (mode) - 1,
3550 NULL_RTX, 0);
3552 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3553 OPTAB_LIB_WIDEN);
3555 if (temp != 0)
3556 return temp;
3559 return NULL_RTX;
3562 /* A subroutine of expand_copysign, perform the copysign operation using the
3563 abs and neg primitives advertised to exist on the target. The assumption
3564 is that we have a split register file, and leaving op0 in fp registers,
3565 and not playing with subregs so much, will help the register allocator. */
3567 static rtx
3568 expand_copysign_absneg (machine_mode mode, rtx op0, rtx op1, rtx target,
3569 int bitpos, bool op0_is_abs)
3571 machine_mode imode;
3572 enum insn_code icode;
3573 rtx sign;
3574 rtx_code_label *label;
3576 if (target == op1)
3577 target = NULL_RTX;
3579 /* Check if the back end provides an insn that handles signbit for the
3580 argument's mode. */
3581 icode = optab_handler (signbit_optab, mode);
3582 if (icode != CODE_FOR_nothing)
3584 imode = insn_data[(int) icode].operand[0].mode;
3585 sign = gen_reg_rtx (imode);
3586 emit_unop_insn (icode, sign, op1, UNKNOWN);
3588 else
3590 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3592 imode = int_mode_for_mode (mode);
3593 if (imode == BLKmode)
3594 return NULL_RTX;
3595 op1 = gen_lowpart (imode, op1);
3597 else
3599 int word;
3601 imode = word_mode;
3602 if (FLOAT_WORDS_BIG_ENDIAN)
3603 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3604 else
3605 word = bitpos / BITS_PER_WORD;
3606 bitpos = bitpos % BITS_PER_WORD;
3607 op1 = operand_subword_force (op1, word, mode);
3610 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3611 sign = expand_binop (imode, and_optab, op1,
3612 immed_wide_int_const (mask, imode),
3613 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3616 if (!op0_is_abs)
3618 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3619 if (op0 == NULL)
3620 return NULL_RTX;
3621 target = op0;
3623 else
3625 if (target == NULL_RTX)
3626 target = copy_to_reg (op0);
3627 else
3628 emit_move_insn (target, op0);
3631 label = gen_label_rtx ();
3632 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3634 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3635 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3636 else
3637 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3638 if (op0 != target)
3639 emit_move_insn (target, op0);
3641 emit_label (label);
3643 return target;
3647 /* A subroutine of expand_copysign, perform the entire copysign operation
3648 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3649 is true if op0 is known to have its sign bit clear. */
3651 static rtx
3652 expand_copysign_bit (machine_mode mode, rtx op0, rtx op1, rtx target,
3653 int bitpos, bool op0_is_abs)
3655 machine_mode imode;
3656 int word, nwords, i;
3657 rtx temp;
3658 rtx_insn *insns;
3660 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3662 imode = int_mode_for_mode (mode);
3663 if (imode == BLKmode)
3664 return NULL_RTX;
3665 word = 0;
3666 nwords = 1;
3668 else
3670 imode = word_mode;
3672 if (FLOAT_WORDS_BIG_ENDIAN)
3673 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3674 else
3675 word = bitpos / BITS_PER_WORD;
3676 bitpos = bitpos % BITS_PER_WORD;
3677 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3680 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3682 if (target == 0
3683 || target == op0
3684 || target == op1
3685 || (nwords > 1 && !valid_multiword_target_p (target)))
3686 target = gen_reg_rtx (mode);
3688 if (nwords > 1)
3690 start_sequence ();
3692 for (i = 0; i < nwords; ++i)
3694 rtx targ_piece = operand_subword (target, i, 1, mode);
3695 rtx op0_piece = operand_subword_force (op0, i, mode);
3697 if (i == word)
3699 if (!op0_is_abs)
3700 op0_piece
3701 = expand_binop (imode, and_optab, op0_piece,
3702 immed_wide_int_const (~mask, imode),
3703 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3704 op1 = expand_binop (imode, and_optab,
3705 operand_subword_force (op1, i, mode),
3706 immed_wide_int_const (mask, imode),
3707 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3709 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3710 targ_piece, 1, OPTAB_LIB_WIDEN);
3711 if (temp != targ_piece)
3712 emit_move_insn (targ_piece, temp);
3714 else
3715 emit_move_insn (targ_piece, op0_piece);
3718 insns = get_insns ();
3719 end_sequence ();
3721 emit_insn (insns);
3723 else
3725 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3726 immed_wide_int_const (mask, imode),
3727 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3729 op0 = gen_lowpart (imode, op0);
3730 if (!op0_is_abs)
3731 op0 = expand_binop (imode, and_optab, op0,
3732 immed_wide_int_const (~mask, imode),
3733 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3735 temp = expand_binop (imode, ior_optab, op0, op1,
3736 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3737 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3740 return target;
3743 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3744 scalar floating point mode. Return NULL if we do not know how to
3745 expand the operation inline. */
3748 expand_copysign (rtx op0, rtx op1, rtx target)
3750 machine_mode mode = GET_MODE (op0);
3751 const struct real_format *fmt;
3752 bool op0_is_abs;
3753 rtx temp;
3755 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3756 gcc_assert (GET_MODE (op1) == mode);
3758 /* First try to do it with a special instruction. */
3759 temp = expand_binop (mode, copysign_optab, op0, op1,
3760 target, 0, OPTAB_DIRECT);
3761 if (temp)
3762 return temp;
3764 fmt = REAL_MODE_FORMAT (mode);
3765 if (fmt == NULL || !fmt->has_signed_zero)
3766 return NULL_RTX;
3768 op0_is_abs = false;
3769 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3771 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3772 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3773 op0_is_abs = true;
3776 if (fmt->signbit_ro >= 0
3777 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3778 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3779 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3781 temp = expand_copysign_absneg (mode, op0, op1, target,
3782 fmt->signbit_ro, op0_is_abs);
3783 if (temp)
3784 return temp;
3787 if (fmt->signbit_rw < 0)
3788 return NULL_RTX;
3789 return expand_copysign_bit (mode, op0, op1, target,
3790 fmt->signbit_rw, op0_is_abs);
3793 /* Generate an instruction whose insn-code is INSN_CODE,
3794 with two operands: an output TARGET and an input OP0.
3795 TARGET *must* be nonzero, and the output is always stored there.
3796 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3797 the value that is stored into TARGET.
3799 Return false if expansion failed. */
3801 bool
3802 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3803 enum rtx_code code)
3805 struct expand_operand ops[2];
3806 rtx pat;
3808 create_output_operand (&ops[0], target, GET_MODE (target));
3809 create_input_operand (&ops[1], op0, GET_MODE (op0));
3810 pat = maybe_gen_insn (icode, 2, ops);
3811 if (!pat)
3812 return false;
3814 if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
3815 && code != UNKNOWN)
3816 add_equal_note (as_a <rtx_insn *> (pat), ops[0].value, code, ops[1].value,
3817 NULL_RTX);
3819 emit_insn (pat);
3821 if (ops[0].value != target)
3822 emit_move_insn (target, ops[0].value);
3823 return true;
3825 /* Generate an instruction whose insn-code is INSN_CODE,
3826 with two operands: an output TARGET and an input OP0.
3827 TARGET *must* be nonzero, and the output is always stored there.
3828 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3829 the value that is stored into TARGET. */
3831 void
3832 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3834 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3835 gcc_assert (ok);
3838 struct no_conflict_data
3840 rtx target;
3841 rtx_insn *first, *insn;
3842 bool must_stay;
3845 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3846 the currently examined clobber / store has to stay in the list of
3847 insns that constitute the actual libcall block. */
3848 static void
3849 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3851 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3853 /* If this inns directly contributes to setting the target, it must stay. */
3854 if (reg_overlap_mentioned_p (p->target, dest))
3855 p->must_stay = true;
3856 /* If we haven't committed to keeping any other insns in the list yet,
3857 there is nothing more to check. */
3858 else if (p->insn == p->first)
3859 return;
3860 /* If this insn sets / clobbers a register that feeds one of the insns
3861 already in the list, this insn has to stay too. */
3862 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3863 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3864 || reg_used_between_p (dest, p->first, p->insn)
3865 /* Likewise if this insn depends on a register set by a previous
3866 insn in the list, or if it sets a result (presumably a hard
3867 register) that is set or clobbered by a previous insn.
3868 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3869 SET_DEST perform the former check on the address, and the latter
3870 check on the MEM. */
3871 || (GET_CODE (set) == SET
3872 && (modified_in_p (SET_SRC (set), p->first)
3873 || modified_in_p (SET_DEST (set), p->first)
3874 || modified_between_p (SET_SRC (set), p->first, p->insn)
3875 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3876 p->must_stay = true;
3880 /* Emit code to make a call to a constant function or a library call.
3882 INSNS is a list containing all insns emitted in the call.
3883 These insns leave the result in RESULT. Our block is to copy RESULT
3884 to TARGET, which is logically equivalent to EQUIV.
3886 We first emit any insns that set a pseudo on the assumption that these are
3887 loading constants into registers; doing so allows them to be safely cse'ed
3888 between blocks. Then we emit all the other insns in the block, followed by
3889 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3890 note with an operand of EQUIV. */
3892 static void
3893 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
3894 bool equiv_may_trap)
3896 rtx final_dest = target;
3897 rtx_insn *next, *last, *insn;
3899 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3900 into a MEM later. Protect the libcall block from this change. */
3901 if (! REG_P (target) || REG_USERVAR_P (target))
3902 target = gen_reg_rtx (GET_MODE (target));
3904 /* If we're using non-call exceptions, a libcall corresponding to an
3905 operation that may trap may also trap. */
3906 /* ??? See the comment in front of make_reg_eh_region_note. */
3907 if (cfun->can_throw_non_call_exceptions
3908 && (equiv_may_trap || may_trap_p (equiv)))
3910 for (insn = insns; insn; insn = NEXT_INSN (insn))
3911 if (CALL_P (insn))
3913 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3914 if (note)
3916 int lp_nr = INTVAL (XEXP (note, 0));
3917 if (lp_nr == 0 || lp_nr == INT_MIN)
3918 remove_note (insn, note);
3922 else
3924 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3925 reg note to indicate that this call cannot throw or execute a nonlocal
3926 goto (unless there is already a REG_EH_REGION note, in which case
3927 we update it). */
3928 for (insn = insns; insn; insn = NEXT_INSN (insn))
3929 if (CALL_P (insn))
3930 make_reg_eh_region_note_nothrow_nononlocal (insn);
3933 /* First emit all insns that set pseudos. Remove them from the list as
3934 we go. Avoid insns that set pseudos which were referenced in previous
3935 insns. These can be generated by move_by_pieces, for example,
3936 to update an address. Similarly, avoid insns that reference things
3937 set in previous insns. */
3939 for (insn = insns; insn; insn = next)
3941 rtx set = single_set (insn);
3943 next = NEXT_INSN (insn);
3945 if (set != 0 && REG_P (SET_DEST (set))
3946 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3948 struct no_conflict_data data;
3950 data.target = const0_rtx;
3951 data.first = insns;
3952 data.insn = insn;
3953 data.must_stay = 0;
3954 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3955 if (! data.must_stay)
3957 if (PREV_INSN (insn))
3958 SET_NEXT_INSN (PREV_INSN (insn)) = next;
3959 else
3960 insns = next;
3962 if (next)
3963 SET_PREV_INSN (next) = PREV_INSN (insn);
3965 add_insn (insn);
3969 /* Some ports use a loop to copy large arguments onto the stack.
3970 Don't move anything outside such a loop. */
3971 if (LABEL_P (insn))
3972 break;
3975 /* Write the remaining insns followed by the final copy. */
3976 for (insn = insns; insn; insn = next)
3978 next = NEXT_INSN (insn);
3980 add_insn (insn);
3983 last = emit_move_insn (target, result);
3984 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3986 if (final_dest != target)
3987 emit_move_insn (final_dest, target);
3990 void
3991 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3993 emit_libcall_block_1 (safe_as_a <rtx_insn *> (insns),
3994 target, result, equiv, false);
3997 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3998 PURPOSE describes how this comparison will be used. CODE is the rtx
3999 comparison code we will be using.
4001 ??? Actually, CODE is slightly weaker than that. A target is still
4002 required to implement all of the normal bcc operations, but not
4003 required to implement all (or any) of the unordered bcc operations. */
4006 can_compare_p (enum rtx_code code, machine_mode mode,
4007 enum can_compare_purpose purpose)
4009 rtx test;
4010 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
4013 enum insn_code icode;
4015 if (purpose == ccp_jump
4016 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
4017 && insn_operand_matches (icode, 0, test))
4018 return 1;
4019 if (purpose == ccp_store_flag
4020 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
4021 && insn_operand_matches (icode, 1, test))
4022 return 1;
4023 if (purpose == ccp_cmov
4024 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
4025 return 1;
4027 mode = GET_MODE_WIDER_MODE (mode);
4028 PUT_MODE (test, mode);
4030 while (mode != VOIDmode);
4032 return 0;
4035 /* This function is called when we are going to emit a compare instruction that
4036 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
4038 *PMODE is the mode of the inputs (in case they are const_int).
4039 *PUNSIGNEDP nonzero says that the operands are unsigned;
4040 this matters if they need to be widened (as given by METHODS).
4042 If they have mode BLKmode, then SIZE specifies the size of both operands.
4044 This function performs all the setup necessary so that the caller only has
4045 to emit a single comparison insn. This setup can involve doing a BLKmode
4046 comparison or emitting a library call to perform the comparison if no insn
4047 is available to handle it.
4048 The values which are passed in through pointers can be modified; the caller
4049 should perform the comparison on the modified values. Constant
4050 comparisons must have already been folded. */
4052 static void
4053 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4054 int unsignedp, enum optab_methods methods,
4055 rtx *ptest, machine_mode *pmode)
4057 machine_mode mode = *pmode;
4058 rtx libfunc, test;
4059 machine_mode cmp_mode;
4060 enum mode_class mclass;
4062 /* The other methods are not needed. */
4063 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4064 || methods == OPTAB_LIB_WIDEN);
4066 /* If we are optimizing, force expensive constants into a register. */
4067 if (CONSTANT_P (x) && optimize
4068 && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
4069 > COSTS_N_INSNS (1)))
4070 x = force_reg (mode, x);
4072 if (CONSTANT_P (y) && optimize
4073 && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
4074 > COSTS_N_INSNS (1)))
4075 y = force_reg (mode, y);
4077 #ifdef HAVE_cc0
4078 /* Make sure if we have a canonical comparison. The RTL
4079 documentation states that canonical comparisons are required only
4080 for targets which have cc0. */
4081 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4082 #endif
4084 /* Don't let both operands fail to indicate the mode. */
4085 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4086 x = force_reg (mode, x);
4087 if (mode == VOIDmode)
4088 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4090 /* Handle all BLKmode compares. */
4092 if (mode == BLKmode)
4094 machine_mode result_mode;
4095 enum insn_code cmp_code;
4096 tree length_type;
4097 rtx libfunc;
4098 rtx result;
4099 rtx opalign
4100 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4102 gcc_assert (size);
4104 /* Try to use a memory block compare insn - either cmpstr
4105 or cmpmem will do. */
4106 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4107 cmp_mode != VOIDmode;
4108 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4110 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4111 if (cmp_code == CODE_FOR_nothing)
4112 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4113 if (cmp_code == CODE_FOR_nothing)
4114 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4115 if (cmp_code == CODE_FOR_nothing)
4116 continue;
4118 /* Must make sure the size fits the insn's mode. */
4119 if ((CONST_INT_P (size)
4120 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4121 || (GET_MODE_BITSIZE (GET_MODE (size))
4122 > GET_MODE_BITSIZE (cmp_mode)))
4123 continue;
4125 result_mode = insn_data[cmp_code].operand[0].mode;
4126 result = gen_reg_rtx (result_mode);
4127 size = convert_to_mode (cmp_mode, size, 1);
4128 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4130 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4131 *pmode = result_mode;
4132 return;
4135 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4136 goto fail;
4138 /* Otherwise call a library function, memcmp. */
4139 libfunc = memcmp_libfunc;
4140 length_type = sizetype;
4141 result_mode = TYPE_MODE (integer_type_node);
4142 cmp_mode = TYPE_MODE (length_type);
4143 size = convert_to_mode (TYPE_MODE (length_type), size,
4144 TYPE_UNSIGNED (length_type));
4146 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4147 result_mode, 3,
4148 XEXP (x, 0), Pmode,
4149 XEXP (y, 0), Pmode,
4150 size, cmp_mode);
4151 x = result;
4152 y = const0_rtx;
4153 mode = result_mode;
4154 methods = OPTAB_LIB_WIDEN;
4155 unsignedp = false;
4158 /* Don't allow operands to the compare to trap, as that can put the
4159 compare and branch in different basic blocks. */
4160 if (cfun->can_throw_non_call_exceptions)
4162 if (may_trap_p (x))
4163 x = force_reg (mode, x);
4164 if (may_trap_p (y))
4165 y = force_reg (mode, y);
4168 if (GET_MODE_CLASS (mode) == MODE_CC)
4170 enum insn_code icode = optab_handler (cbranch_optab, CCmode);
4171 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4172 gcc_assert (icode != CODE_FOR_nothing
4173 && insn_operand_matches (icode, 0, test));
4174 *ptest = test;
4175 return;
4178 mclass = GET_MODE_CLASS (mode);
4179 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4180 cmp_mode = mode;
4183 enum insn_code icode;
4184 icode = optab_handler (cbranch_optab, cmp_mode);
4185 if (icode != CODE_FOR_nothing
4186 && insn_operand_matches (icode, 0, test))
4188 rtx_insn *last = get_last_insn ();
4189 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4190 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4191 if (op0 && op1
4192 && insn_operand_matches (icode, 1, op0)
4193 && insn_operand_matches (icode, 2, op1))
4195 XEXP (test, 0) = op0;
4196 XEXP (test, 1) = op1;
4197 *ptest = test;
4198 *pmode = cmp_mode;
4199 return;
4201 delete_insns_since (last);
4204 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4205 break;
4206 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4208 while (cmp_mode != VOIDmode);
4210 if (methods != OPTAB_LIB_WIDEN)
4211 goto fail;
4213 if (!SCALAR_FLOAT_MODE_P (mode))
4215 rtx result;
4216 machine_mode ret_mode;
4218 /* Handle a libcall just for the mode we are using. */
4219 libfunc = optab_libfunc (cmp_optab, mode);
4220 gcc_assert (libfunc);
4222 /* If we want unsigned, and this mode has a distinct unsigned
4223 comparison routine, use that. */
4224 if (unsignedp)
4226 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4227 if (ulibfunc)
4228 libfunc = ulibfunc;
4231 ret_mode = targetm.libgcc_cmp_return_mode ();
4232 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4233 ret_mode, 2, x, mode, y, mode);
4235 /* There are two kinds of comparison routines. Biased routines
4236 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4237 of gcc expect that the comparison operation is equivalent
4238 to the modified comparison. For signed comparisons compare the
4239 result against 1 in the biased case, and zero in the unbiased
4240 case. For unsigned comparisons always compare against 1 after
4241 biasing the unbiased result by adding 1. This gives us a way to
4242 represent LTU.
4243 The comparisons in the fixed-point helper library are always
4244 biased. */
4245 x = result;
4246 y = const1_rtx;
4248 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4250 if (unsignedp)
4251 x = plus_constant (ret_mode, result, 1);
4252 else
4253 y = const0_rtx;
4256 *pmode = ret_mode;
4257 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4258 ptest, pmode);
4260 else
4261 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4263 return;
4265 fail:
4266 *ptest = NULL_RTX;
4269 /* Before emitting an insn with code ICODE, make sure that X, which is going
4270 to be used for operand OPNUM of the insn, is converted from mode MODE to
4271 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4272 that it is accepted by the operand predicate. Return the new value. */
4275 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
4276 machine_mode wider_mode, int unsignedp)
4278 if (mode != wider_mode)
4279 x = convert_modes (wider_mode, mode, x, unsignedp);
4281 if (!insn_operand_matches (icode, opnum, x))
4283 machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
4284 if (reload_completed)
4285 return NULL_RTX;
4286 if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
4287 return NULL_RTX;
4288 x = copy_to_mode_reg (op_mode, x);
4291 return x;
4294 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4295 we can do the branch. */
4297 static void
4298 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label, int prob)
4300 machine_mode optab_mode;
4301 enum mode_class mclass;
4302 enum insn_code icode;
4303 rtx_insn *insn;
4305 mclass = GET_MODE_CLASS (mode);
4306 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4307 icode = optab_handler (cbranch_optab, optab_mode);
4309 gcc_assert (icode != CODE_FOR_nothing);
4310 gcc_assert (insn_operand_matches (icode, 0, test));
4311 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4312 XEXP (test, 1), label));
4313 if (prob != -1
4314 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4315 && insn
4316 && JUMP_P (insn)
4317 && any_condjump_p (insn)
4318 && !find_reg_note (insn, REG_BR_PROB, 0))
4319 add_int_reg_note (insn, REG_BR_PROB, prob);
4322 /* Generate code to compare X with Y so that the condition codes are
4323 set and to jump to LABEL if the condition is true. If X is a
4324 constant and Y is not a constant, then the comparison is swapped to
4325 ensure that the comparison RTL has the canonical form.
4327 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4328 need to be widened. UNSIGNEDP is also used to select the proper
4329 branch condition code.
4331 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4333 MODE is the mode of the inputs (in case they are const_int).
4335 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4336 It will be potentially converted into an unsigned variant based on
4337 UNSIGNEDP to select a proper jump instruction.
4339 PROB is the probability of jumping to LABEL. */
4341 void
4342 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4343 machine_mode mode, int unsignedp, rtx label,
4344 int prob)
4346 rtx op0 = x, op1 = y;
4347 rtx test;
4349 /* Swap operands and condition to ensure canonical RTL. */
4350 if (swap_commutative_operands_p (x, y)
4351 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4353 op0 = y, op1 = x;
4354 comparison = swap_condition (comparison);
4357 /* If OP0 is still a constant, then both X and Y must be constants
4358 or the opposite comparison is not supported. Force X into a register
4359 to create canonical RTL. */
4360 if (CONSTANT_P (op0))
4361 op0 = force_reg (mode, op0);
4363 if (unsignedp)
4364 comparison = unsigned_condition (comparison);
4366 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4367 &test, &mode);
4368 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4372 /* Emit a library call comparison between floating point X and Y.
4373 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4375 static void
4376 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4377 rtx *ptest, machine_mode *pmode)
4379 enum rtx_code swapped = swap_condition (comparison);
4380 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4381 machine_mode orig_mode = GET_MODE (x);
4382 machine_mode mode, cmp_mode;
4383 rtx true_rtx, false_rtx;
4384 rtx value, target, equiv;
4385 rtx_insn *insns;
4386 rtx libfunc = 0;
4387 bool reversed_p = false;
4388 cmp_mode = targetm.libgcc_cmp_return_mode ();
4390 for (mode = orig_mode;
4391 mode != VOIDmode;
4392 mode = GET_MODE_WIDER_MODE (mode))
4394 if (code_to_optab (comparison)
4395 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4396 break;
4398 if (code_to_optab (swapped)
4399 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4401 rtx tmp;
4402 tmp = x; x = y; y = tmp;
4403 comparison = swapped;
4404 break;
4407 if (code_to_optab (reversed)
4408 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4410 comparison = reversed;
4411 reversed_p = true;
4412 break;
4416 gcc_assert (mode != VOIDmode);
4418 if (mode != orig_mode)
4420 x = convert_to_mode (mode, x, 0);
4421 y = convert_to_mode (mode, y, 0);
4424 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4425 the RTL. The allows the RTL optimizers to delete the libcall if the
4426 condition can be determined at compile-time. */
4427 if (comparison == UNORDERED
4428 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4430 true_rtx = const_true_rtx;
4431 false_rtx = const0_rtx;
4433 else
4435 switch (comparison)
4437 case EQ:
4438 true_rtx = const0_rtx;
4439 false_rtx = const_true_rtx;
4440 break;
4442 case NE:
4443 true_rtx = const_true_rtx;
4444 false_rtx = const0_rtx;
4445 break;
4447 case GT:
4448 true_rtx = const1_rtx;
4449 false_rtx = const0_rtx;
4450 break;
4452 case GE:
4453 true_rtx = const0_rtx;
4454 false_rtx = constm1_rtx;
4455 break;
4457 case LT:
4458 true_rtx = constm1_rtx;
4459 false_rtx = const0_rtx;
4460 break;
4462 case LE:
4463 true_rtx = const0_rtx;
4464 false_rtx = const1_rtx;
4465 break;
4467 default:
4468 gcc_unreachable ();
4472 if (comparison == UNORDERED)
4474 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4475 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4476 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4477 temp, const_true_rtx, equiv);
4479 else
4481 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4482 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4483 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4484 equiv, true_rtx, false_rtx);
4487 start_sequence ();
4488 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4489 cmp_mode, 2, x, mode, y, mode);
4490 insns = get_insns ();
4491 end_sequence ();
4493 target = gen_reg_rtx (cmp_mode);
4494 emit_libcall_block (insns, target, value, equiv);
4496 if (comparison == UNORDERED
4497 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4498 || reversed_p)
4499 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4500 else
4501 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4503 *pmode = cmp_mode;
4506 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4508 void
4509 emit_indirect_jump (rtx loc ATTRIBUTE_UNUSED)
4511 #ifndef HAVE_indirect_jump
4512 sorry ("indirect jumps are not available on this target");
4513 #else
4514 struct expand_operand ops[1];
4515 create_address_operand (&ops[0], loc);
4516 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4517 emit_barrier ();
4518 #endif
4521 #ifdef HAVE_conditional_move
4523 /* Emit a conditional move instruction if the machine supports one for that
4524 condition and machine mode.
4526 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4527 the mode to use should they be constants. If it is VOIDmode, they cannot
4528 both be constants.
4530 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4531 should be stored there. MODE is the mode to use should they be constants.
4532 If it is VOIDmode, they cannot both be constants.
4534 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4535 is not supported. */
4538 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4539 machine_mode cmode, rtx op2, rtx op3,
4540 machine_mode mode, int unsignedp)
4542 rtx tem, comparison;
4543 rtx_insn *last;
4544 enum insn_code icode;
4545 enum rtx_code reversed;
4547 /* If one operand is constant, make it the second one. Only do this
4548 if the other operand is not constant as well. */
4550 if (swap_commutative_operands_p (op0, op1))
4552 tem = op0;
4553 op0 = op1;
4554 op1 = tem;
4555 code = swap_condition (code);
4558 /* get_condition will prefer to generate LT and GT even if the old
4559 comparison was against zero, so undo that canonicalization here since
4560 comparisons against zero are cheaper. */
4561 if (code == LT && op1 == const1_rtx)
4562 code = LE, op1 = const0_rtx;
4563 else if (code == GT && op1 == constm1_rtx)
4564 code = GE, op1 = const0_rtx;
4566 if (cmode == VOIDmode)
4567 cmode = GET_MODE (op0);
4569 if (swap_commutative_operands_p (op2, op3)
4570 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4571 != UNKNOWN))
4573 tem = op2;
4574 op2 = op3;
4575 op3 = tem;
4576 code = reversed;
4579 if (mode == VOIDmode)
4580 mode = GET_MODE (op2);
4582 icode = direct_optab_handler (movcc_optab, mode);
4584 if (icode == CODE_FOR_nothing)
4585 return 0;
4587 if (!target)
4588 target = gen_reg_rtx (mode);
4590 code = unsignedp ? unsigned_condition (code) : code;
4591 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4593 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4594 return NULL and let the caller figure out how best to deal with this
4595 situation. */
4596 if (!COMPARISON_P (comparison))
4597 return NULL_RTX;
4599 saved_pending_stack_adjust save;
4600 save_pending_stack_adjust (&save);
4601 last = get_last_insn ();
4602 do_pending_stack_adjust ();
4603 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4604 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4605 &comparison, &cmode);
4606 if (comparison)
4608 struct expand_operand ops[4];
4610 create_output_operand (&ops[0], target, mode);
4611 create_fixed_operand (&ops[1], comparison);
4612 create_input_operand (&ops[2], op2, mode);
4613 create_input_operand (&ops[3], op3, mode);
4614 if (maybe_expand_insn (icode, 4, ops))
4616 if (ops[0].value != target)
4617 convert_move (target, ops[0].value, false);
4618 return target;
4621 delete_insns_since (last);
4622 restore_pending_stack_adjust (&save);
4623 return NULL_RTX;
4626 /* Return nonzero if a conditional move of mode MODE is supported.
4628 This function is for combine so it can tell whether an insn that looks
4629 like a conditional move is actually supported by the hardware. If we
4630 guess wrong we lose a bit on optimization, but that's it. */
4631 /* ??? sparc64 supports conditionally moving integers values based on fp
4632 comparisons, and vice versa. How do we handle them? */
4635 can_conditionally_move_p (machine_mode mode)
4637 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4638 return 1;
4640 return 0;
4643 #endif /* HAVE_conditional_move */
4645 /* Emit a conditional addition instruction if the machine supports one for that
4646 condition and machine mode.
4648 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4649 the mode to use should they be constants. If it is VOIDmode, they cannot
4650 both be constants.
4652 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4653 should be stored there. MODE is the mode to use should they be constants.
4654 If it is VOIDmode, they cannot both be constants.
4656 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4657 is not supported. */
4660 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4661 machine_mode cmode, rtx op2, rtx op3,
4662 machine_mode mode, int unsignedp)
4664 rtx tem, comparison;
4665 rtx_insn *last;
4666 enum insn_code icode;
4668 /* If one operand is constant, make it the second one. Only do this
4669 if the other operand is not constant as well. */
4671 if (swap_commutative_operands_p (op0, op1))
4673 tem = op0;
4674 op0 = op1;
4675 op1 = tem;
4676 code = swap_condition (code);
4679 /* get_condition will prefer to generate LT and GT even if the old
4680 comparison was against zero, so undo that canonicalization here since
4681 comparisons against zero are cheaper. */
4682 if (code == LT && op1 == const1_rtx)
4683 code = LE, op1 = const0_rtx;
4684 else if (code == GT && op1 == constm1_rtx)
4685 code = GE, op1 = const0_rtx;
4687 if (cmode == VOIDmode)
4688 cmode = GET_MODE (op0);
4690 if (mode == VOIDmode)
4691 mode = GET_MODE (op2);
4693 icode = optab_handler (addcc_optab, mode);
4695 if (icode == CODE_FOR_nothing)
4696 return 0;
4698 if (!target)
4699 target = gen_reg_rtx (mode);
4701 code = unsignedp ? unsigned_condition (code) : code;
4702 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4704 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4705 return NULL and let the caller figure out how best to deal with this
4706 situation. */
4707 if (!COMPARISON_P (comparison))
4708 return NULL_RTX;
4710 do_pending_stack_adjust ();
4711 last = get_last_insn ();
4712 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4713 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4714 &comparison, &cmode);
4715 if (comparison)
4717 struct expand_operand ops[4];
4719 create_output_operand (&ops[0], target, mode);
4720 create_fixed_operand (&ops[1], comparison);
4721 create_input_operand (&ops[2], op2, mode);
4722 create_input_operand (&ops[3], op3, mode);
4723 if (maybe_expand_insn (icode, 4, ops))
4725 if (ops[0].value != target)
4726 convert_move (target, ops[0].value, false);
4727 return target;
4730 delete_insns_since (last);
4731 return NULL_RTX;
4734 /* These functions attempt to generate an insn body, rather than
4735 emitting the insn, but if the gen function already emits them, we
4736 make no attempt to turn them back into naked patterns. */
4738 /* Generate and return an insn body to add Y to X. */
4741 gen_add2_insn (rtx x, rtx y)
4743 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4745 gcc_assert (insn_operand_matches (icode, 0, x));
4746 gcc_assert (insn_operand_matches (icode, 1, x));
4747 gcc_assert (insn_operand_matches (icode, 2, y));
4749 return GEN_FCN (icode) (x, x, y);
4752 /* Generate and return an insn body to add r1 and c,
4753 storing the result in r0. */
4756 gen_add3_insn (rtx r0, rtx r1, rtx c)
4758 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4760 if (icode == CODE_FOR_nothing
4761 || !insn_operand_matches (icode, 0, r0)
4762 || !insn_operand_matches (icode, 1, r1)
4763 || !insn_operand_matches (icode, 2, c))
4764 return NULL_RTX;
4766 return GEN_FCN (icode) (r0, r1, c);
4770 have_add2_insn (rtx x, rtx y)
4772 enum insn_code icode;
4774 gcc_assert (GET_MODE (x) != VOIDmode);
4776 icode = optab_handler (add_optab, GET_MODE (x));
4778 if (icode == CODE_FOR_nothing)
4779 return 0;
4781 if (!insn_operand_matches (icode, 0, x)
4782 || !insn_operand_matches (icode, 1, x)
4783 || !insn_operand_matches (icode, 2, y))
4784 return 0;
4786 return 1;
4789 /* Generate and return an insn body to add Y to X. */
4792 gen_addptr3_insn (rtx x, rtx y, rtx z)
4794 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4796 gcc_assert (insn_operand_matches (icode, 0, x));
4797 gcc_assert (insn_operand_matches (icode, 1, y));
4798 gcc_assert (insn_operand_matches (icode, 2, z));
4800 return GEN_FCN (icode) (x, y, z);
4803 /* Return true if the target implements an addptr pattern and X, Y,
4804 and Z are valid for the pattern predicates. */
4807 have_addptr3_insn (rtx x, rtx y, rtx z)
4809 enum insn_code icode;
4811 gcc_assert (GET_MODE (x) != VOIDmode);
4813 icode = optab_handler (addptr3_optab, GET_MODE (x));
4815 if (icode == CODE_FOR_nothing)
4816 return 0;
4818 if (!insn_operand_matches (icode, 0, x)
4819 || !insn_operand_matches (icode, 1, y)
4820 || !insn_operand_matches (icode, 2, z))
4821 return 0;
4823 return 1;
4826 /* Generate and return an insn body to subtract Y from X. */
4829 gen_sub2_insn (rtx x, rtx y)
4831 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4833 gcc_assert (insn_operand_matches (icode, 0, x));
4834 gcc_assert (insn_operand_matches (icode, 1, x));
4835 gcc_assert (insn_operand_matches (icode, 2, y));
4837 return GEN_FCN (icode) (x, x, y);
4840 /* Generate and return an insn body to subtract r1 and c,
4841 storing the result in r0. */
4844 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4846 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4848 if (icode == CODE_FOR_nothing
4849 || !insn_operand_matches (icode, 0, r0)
4850 || !insn_operand_matches (icode, 1, r1)
4851 || !insn_operand_matches (icode, 2, c))
4852 return NULL_RTX;
4854 return GEN_FCN (icode) (r0, r1, c);
4858 have_sub2_insn (rtx x, rtx y)
4860 enum insn_code icode;
4862 gcc_assert (GET_MODE (x) != VOIDmode);
4864 icode = optab_handler (sub_optab, GET_MODE (x));
4866 if (icode == CODE_FOR_nothing)
4867 return 0;
4869 if (!insn_operand_matches (icode, 0, x)
4870 || !insn_operand_matches (icode, 1, x)
4871 || !insn_operand_matches (icode, 2, y))
4872 return 0;
4874 return 1;
4877 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4878 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4879 no such operation exists, CODE_FOR_nothing will be returned. */
4881 enum insn_code
4882 can_extend_p (machine_mode to_mode, machine_mode from_mode,
4883 int unsignedp)
4885 convert_optab tab;
4886 #ifdef HAVE_ptr_extend
4887 if (unsignedp < 0)
4888 return CODE_FOR_ptr_extend;
4889 #endif
4891 tab = unsignedp ? zext_optab : sext_optab;
4892 return convert_optab_handler (tab, to_mode, from_mode);
4895 /* Generate the body of an insn to extend Y (with mode MFROM)
4896 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4899 gen_extend_insn (rtx x, rtx y, machine_mode mto,
4900 machine_mode mfrom, int unsignedp)
4902 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4903 return GEN_FCN (icode) (x, y);
4906 /* can_fix_p and can_float_p say whether the target machine
4907 can directly convert a given fixed point type to
4908 a given floating point type, or vice versa.
4909 The returned value is the CODE_FOR_... value to use,
4910 or CODE_FOR_nothing if these modes cannot be directly converted.
4912 *TRUNCP_PTR is set to 1 if it is necessary to output
4913 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4915 static enum insn_code
4916 can_fix_p (machine_mode fixmode, machine_mode fltmode,
4917 int unsignedp, int *truncp_ptr)
4919 convert_optab tab;
4920 enum insn_code icode;
4922 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4923 icode = convert_optab_handler (tab, fixmode, fltmode);
4924 if (icode != CODE_FOR_nothing)
4926 *truncp_ptr = 0;
4927 return icode;
4930 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4931 for this to work. We need to rework the fix* and ftrunc* patterns
4932 and documentation. */
4933 tab = unsignedp ? ufix_optab : sfix_optab;
4934 icode = convert_optab_handler (tab, fixmode, fltmode);
4935 if (icode != CODE_FOR_nothing
4936 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4938 *truncp_ptr = 1;
4939 return icode;
4942 *truncp_ptr = 0;
4943 return CODE_FOR_nothing;
4946 enum insn_code
4947 can_float_p (machine_mode fltmode, machine_mode fixmode,
4948 int unsignedp)
4950 convert_optab tab;
4952 tab = unsignedp ? ufloat_optab : sfloat_optab;
4953 return convert_optab_handler (tab, fltmode, fixmode);
4956 /* Function supportable_convert_operation
4958 Check whether an operation represented by the code CODE is a
4959 convert operation that is supported by the target platform in
4960 vector form (i.e., when operating on arguments of type VECTYPE_IN
4961 producing a result of type VECTYPE_OUT).
4963 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4964 This function checks if these operations are supported
4965 by the target platform either directly (via vector tree-codes), or via
4966 target builtins.
4968 Output:
4969 - CODE1 is code of vector operation to be used when
4970 vectorizing the operation, if available.
4971 - DECL is decl of target builtin functions to be used
4972 when vectorizing the operation, if available. In this case,
4973 CODE1 is CALL_EXPR. */
4975 bool
4976 supportable_convert_operation (enum tree_code code,
4977 tree vectype_out, tree vectype_in,
4978 tree *decl, enum tree_code *code1)
4980 machine_mode m1,m2;
4981 int truncp;
4983 m1 = TYPE_MODE (vectype_out);
4984 m2 = TYPE_MODE (vectype_in);
4986 /* First check if we can done conversion directly. */
4987 if ((code == FIX_TRUNC_EXPR
4988 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
4989 != CODE_FOR_nothing)
4990 || (code == FLOAT_EXPR
4991 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
4992 != CODE_FOR_nothing))
4994 *code1 = code;
4995 return true;
4998 /* Now check for builtin. */
4999 if (targetm.vectorize.builtin_conversion
5000 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
5002 *code1 = CALL_EXPR;
5003 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
5004 return true;
5006 return false;
5010 /* Generate code to convert FROM to floating point
5011 and store in TO. FROM must be fixed point and not VOIDmode.
5012 UNSIGNEDP nonzero means regard FROM as unsigned.
5013 Normally this is done by correcting the final value
5014 if it is negative. */
5016 void
5017 expand_float (rtx to, rtx from, int unsignedp)
5019 enum insn_code icode;
5020 rtx target = to;
5021 machine_mode fmode, imode;
5022 bool can_do_signed = false;
5024 /* Crash now, because we won't be able to decide which mode to use. */
5025 gcc_assert (GET_MODE (from) != VOIDmode);
5027 /* Look for an insn to do the conversion. Do it in the specified
5028 modes if possible; otherwise convert either input, output or both to
5029 wider mode. If the integer mode is wider than the mode of FROM,
5030 we can do the conversion signed even if the input is unsigned. */
5032 for (fmode = GET_MODE (to); fmode != VOIDmode;
5033 fmode = GET_MODE_WIDER_MODE (fmode))
5034 for (imode = GET_MODE (from); imode != VOIDmode;
5035 imode = GET_MODE_WIDER_MODE (imode))
5037 int doing_unsigned = unsignedp;
5039 if (fmode != GET_MODE (to)
5040 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
5041 continue;
5043 icode = can_float_p (fmode, imode, unsignedp);
5044 if (icode == CODE_FOR_nothing && unsignedp)
5046 enum insn_code scode = can_float_p (fmode, imode, 0);
5047 if (scode != CODE_FOR_nothing)
5048 can_do_signed = true;
5049 if (imode != GET_MODE (from))
5050 icode = scode, doing_unsigned = 0;
5053 if (icode != CODE_FOR_nothing)
5055 if (imode != GET_MODE (from))
5056 from = convert_to_mode (imode, from, unsignedp);
5058 if (fmode != GET_MODE (to))
5059 target = gen_reg_rtx (fmode);
5061 emit_unop_insn (icode, target, from,
5062 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
5064 if (target != to)
5065 convert_move (to, target, 0);
5066 return;
5070 /* Unsigned integer, and no way to convert directly. Convert as signed,
5071 then unconditionally adjust the result. */
5072 if (unsignedp && can_do_signed)
5074 rtx_code_label *label = gen_label_rtx ();
5075 rtx temp;
5076 REAL_VALUE_TYPE offset;
5078 /* Look for a usable floating mode FMODE wider than the source and at
5079 least as wide as the target. Using FMODE will avoid rounding woes
5080 with unsigned values greater than the signed maximum value. */
5082 for (fmode = GET_MODE (to); fmode != VOIDmode;
5083 fmode = GET_MODE_WIDER_MODE (fmode))
5084 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
5085 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
5086 break;
5088 if (fmode == VOIDmode)
5090 /* There is no such mode. Pretend the target is wide enough. */
5091 fmode = GET_MODE (to);
5093 /* Avoid double-rounding when TO is narrower than FROM. */
5094 if ((significand_size (fmode) + 1)
5095 < GET_MODE_PRECISION (GET_MODE (from)))
5097 rtx temp1;
5098 rtx_code_label *neglabel = gen_label_rtx ();
5100 /* Don't use TARGET if it isn't a register, is a hard register,
5101 or is the wrong mode. */
5102 if (!REG_P (target)
5103 || REGNO (target) < FIRST_PSEUDO_REGISTER
5104 || GET_MODE (target) != fmode)
5105 target = gen_reg_rtx (fmode);
5107 imode = GET_MODE (from);
5108 do_pending_stack_adjust ();
5110 /* Test whether the sign bit is set. */
5111 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5112 0, neglabel);
5114 /* The sign bit is not set. Convert as signed. */
5115 expand_float (target, from, 0);
5116 emit_jump_insn (gen_jump (label));
5117 emit_barrier ();
5119 /* The sign bit is set.
5120 Convert to a usable (positive signed) value by shifting right
5121 one bit, while remembering if a nonzero bit was shifted
5122 out; i.e., compute (from & 1) | (from >> 1). */
5124 emit_label (neglabel);
5125 temp = expand_binop (imode, and_optab, from, const1_rtx,
5126 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5127 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5128 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5129 OPTAB_LIB_WIDEN);
5130 expand_float (target, temp, 0);
5132 /* Multiply by 2 to undo the shift above. */
5133 temp = expand_binop (fmode, add_optab, target, target,
5134 target, 0, OPTAB_LIB_WIDEN);
5135 if (temp != target)
5136 emit_move_insn (target, temp);
5138 do_pending_stack_adjust ();
5139 emit_label (label);
5140 goto done;
5144 /* If we are about to do some arithmetic to correct for an
5145 unsigned operand, do it in a pseudo-register. */
5147 if (GET_MODE (to) != fmode
5148 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5149 target = gen_reg_rtx (fmode);
5151 /* Convert as signed integer to floating. */
5152 expand_float (target, from, 0);
5154 /* If FROM is negative (and therefore TO is negative),
5155 correct its value by 2**bitwidth. */
5157 do_pending_stack_adjust ();
5158 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5159 0, label);
5162 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5163 temp = expand_binop (fmode, add_optab, target,
5164 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5165 target, 0, OPTAB_LIB_WIDEN);
5166 if (temp != target)
5167 emit_move_insn (target, temp);
5169 do_pending_stack_adjust ();
5170 emit_label (label);
5171 goto done;
5174 /* No hardware instruction available; call a library routine. */
5176 rtx libfunc;
5177 rtx_insn *insns;
5178 rtx value;
5179 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5181 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_PRECISION (SImode))
5182 from = convert_to_mode (SImode, from, unsignedp);
5184 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5185 gcc_assert (libfunc);
5187 start_sequence ();
5189 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5190 GET_MODE (to), 1, from,
5191 GET_MODE (from));
5192 insns = get_insns ();
5193 end_sequence ();
5195 emit_libcall_block (insns, target, value,
5196 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5197 GET_MODE (to), from));
5200 done:
5202 /* Copy result to requested destination
5203 if we have been computing in a temp location. */
5205 if (target != to)
5207 if (GET_MODE (target) == GET_MODE (to))
5208 emit_move_insn (to, target);
5209 else
5210 convert_move (to, target, 0);
5214 /* Generate code to convert FROM to fixed point and store in TO. FROM
5215 must be floating point. */
5217 void
5218 expand_fix (rtx to, rtx from, int unsignedp)
5220 enum insn_code icode;
5221 rtx target = to;
5222 machine_mode fmode, imode;
5223 int must_trunc = 0;
5225 /* We first try to find a pair of modes, one real and one integer, at
5226 least as wide as FROM and TO, respectively, in which we can open-code
5227 this conversion. If the integer mode is wider than the mode of TO,
5228 we can do the conversion either signed or unsigned. */
5230 for (fmode = GET_MODE (from); fmode != VOIDmode;
5231 fmode = GET_MODE_WIDER_MODE (fmode))
5232 for (imode = GET_MODE (to); imode != VOIDmode;
5233 imode = GET_MODE_WIDER_MODE (imode))
5235 int doing_unsigned = unsignedp;
5237 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5238 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5239 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5241 if (icode != CODE_FOR_nothing)
5243 rtx_insn *last = get_last_insn ();
5244 if (fmode != GET_MODE (from))
5245 from = convert_to_mode (fmode, from, 0);
5247 if (must_trunc)
5249 rtx temp = gen_reg_rtx (GET_MODE (from));
5250 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5251 temp, 0);
5254 if (imode != GET_MODE (to))
5255 target = gen_reg_rtx (imode);
5257 if (maybe_emit_unop_insn (icode, target, from,
5258 doing_unsigned ? UNSIGNED_FIX : FIX))
5260 if (target != to)
5261 convert_move (to, target, unsignedp);
5262 return;
5264 delete_insns_since (last);
5268 /* For an unsigned conversion, there is one more way to do it.
5269 If we have a signed conversion, we generate code that compares
5270 the real value to the largest representable positive number. If if
5271 is smaller, the conversion is done normally. Otherwise, subtract
5272 one plus the highest signed number, convert, and add it back.
5274 We only need to check all real modes, since we know we didn't find
5275 anything with a wider integer mode.
5277 This code used to extend FP value into mode wider than the destination.
5278 This is needed for decimal float modes which cannot accurately
5279 represent one plus the highest signed number of the same size, but
5280 not for binary modes. Consider, for instance conversion from SFmode
5281 into DImode.
5283 The hot path through the code is dealing with inputs smaller than 2^63
5284 and doing just the conversion, so there is no bits to lose.
5286 In the other path we know the value is positive in the range 2^63..2^64-1
5287 inclusive. (as for other input overflow happens and result is undefined)
5288 So we know that the most important bit set in mantissa corresponds to
5289 2^63. The subtraction of 2^63 should not generate any rounding as it
5290 simply clears out that bit. The rest is trivial. */
5292 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5293 for (fmode = GET_MODE (from); fmode != VOIDmode;
5294 fmode = GET_MODE_WIDER_MODE (fmode))
5295 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5296 && (!DECIMAL_FLOAT_MODE_P (fmode)
5297 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5299 int bitsize;
5300 REAL_VALUE_TYPE offset;
5301 rtx limit;
5302 rtx_code_label *lab1, *lab2;
5303 rtx_insn *insn;
5305 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5306 real_2expN (&offset, bitsize - 1, fmode);
5307 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5308 lab1 = gen_label_rtx ();
5309 lab2 = gen_label_rtx ();
5311 if (fmode != GET_MODE (from))
5312 from = convert_to_mode (fmode, from, 0);
5314 /* See if we need to do the subtraction. */
5315 do_pending_stack_adjust ();
5316 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5317 0, lab1);
5319 /* If not, do the signed "fix" and branch around fixup code. */
5320 expand_fix (to, from, 0);
5321 emit_jump_insn (gen_jump (lab2));
5322 emit_barrier ();
5324 /* Otherwise, subtract 2**(N-1), convert to signed number,
5325 then add 2**(N-1). Do the addition using XOR since this
5326 will often generate better code. */
5327 emit_label (lab1);
5328 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5329 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5330 expand_fix (to, target, 0);
5331 target = expand_binop (GET_MODE (to), xor_optab, to,
5332 gen_int_mode
5333 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5334 GET_MODE (to)),
5335 to, 1, OPTAB_LIB_WIDEN);
5337 if (target != to)
5338 emit_move_insn (to, target);
5340 emit_label (lab2);
5342 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5344 /* Make a place for a REG_NOTE and add it. */
5345 insn = emit_move_insn (to, to);
5346 set_dst_reg_note (insn, REG_EQUAL,
5347 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5348 copy_rtx (from)),
5349 to);
5352 return;
5355 /* We can't do it with an insn, so use a library call. But first ensure
5356 that the mode of TO is at least as wide as SImode, since those are the
5357 only library calls we know about. */
5359 if (GET_MODE_PRECISION (GET_MODE (to)) < GET_MODE_PRECISION (SImode))
5361 target = gen_reg_rtx (SImode);
5363 expand_fix (target, from, unsignedp);
5365 else
5367 rtx_insn *insns;
5368 rtx value;
5369 rtx libfunc;
5371 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5372 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5373 gcc_assert (libfunc);
5375 start_sequence ();
5377 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5378 GET_MODE (to), 1, from,
5379 GET_MODE (from));
5380 insns = get_insns ();
5381 end_sequence ();
5383 emit_libcall_block (insns, target, value,
5384 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5385 GET_MODE (to), from));
5388 if (target != to)
5390 if (GET_MODE (to) == GET_MODE (target))
5391 emit_move_insn (to, target);
5392 else
5393 convert_move (to, target, 0);
5397 /* Generate code to convert FROM or TO a fixed-point.
5398 If UINTP is true, either TO or FROM is an unsigned integer.
5399 If SATP is true, we need to saturate the result. */
5401 void
5402 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5404 machine_mode to_mode = GET_MODE (to);
5405 machine_mode from_mode = GET_MODE (from);
5406 convert_optab tab;
5407 enum rtx_code this_code;
5408 enum insn_code code;
5409 rtx_insn *insns;
5410 rtx value;
5411 rtx libfunc;
5413 if (to_mode == from_mode)
5415 emit_move_insn (to, from);
5416 return;
5419 if (uintp)
5421 tab = satp ? satfractuns_optab : fractuns_optab;
5422 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5424 else
5426 tab = satp ? satfract_optab : fract_optab;
5427 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5429 code = convert_optab_handler (tab, to_mode, from_mode);
5430 if (code != CODE_FOR_nothing)
5432 emit_unop_insn (code, to, from, this_code);
5433 return;
5436 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5437 gcc_assert (libfunc);
5439 start_sequence ();
5440 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5441 1, from, from_mode);
5442 insns = get_insns ();
5443 end_sequence ();
5445 emit_libcall_block (insns, to, value,
5446 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5449 /* Generate code to convert FROM to fixed point and store in TO. FROM
5450 must be floating point, TO must be signed. Use the conversion optab
5451 TAB to do the conversion. */
5453 bool
5454 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5456 enum insn_code icode;
5457 rtx target = to;
5458 machine_mode fmode, imode;
5460 /* We first try to find a pair of modes, one real and one integer, at
5461 least as wide as FROM and TO, respectively, in which we can open-code
5462 this conversion. If the integer mode is wider than the mode of TO,
5463 we can do the conversion either signed or unsigned. */
5465 for (fmode = GET_MODE (from); fmode != VOIDmode;
5466 fmode = GET_MODE_WIDER_MODE (fmode))
5467 for (imode = GET_MODE (to); imode != VOIDmode;
5468 imode = GET_MODE_WIDER_MODE (imode))
5470 icode = convert_optab_handler (tab, imode, fmode);
5471 if (icode != CODE_FOR_nothing)
5473 rtx_insn *last = get_last_insn ();
5474 if (fmode != GET_MODE (from))
5475 from = convert_to_mode (fmode, from, 0);
5477 if (imode != GET_MODE (to))
5478 target = gen_reg_rtx (imode);
5480 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5482 delete_insns_since (last);
5483 continue;
5485 if (target != to)
5486 convert_move (to, target, 0);
5487 return true;
5491 return false;
5494 /* Report whether we have an instruction to perform the operation
5495 specified by CODE on operands of mode MODE. */
5497 have_insn_for (enum rtx_code code, machine_mode mode)
5499 return (code_to_optab (code)
5500 && (optab_handler (code_to_optab (code), mode)
5501 != CODE_FOR_nothing));
5504 /* Initialize the libfunc fields of an entire group of entries in some
5505 optab. Each entry is set equal to a string consisting of a leading
5506 pair of underscores followed by a generic operation name followed by
5507 a mode name (downshifted to lowercase) followed by a single character
5508 representing the number of operands for the given operation (which is
5509 usually one of the characters '2', '3', or '4').
5511 OPTABLE is the table in which libfunc fields are to be initialized.
5512 OPNAME is the generic (string) name of the operation.
5513 SUFFIX is the character which specifies the number of operands for
5514 the given generic operation.
5515 MODE is the mode to generate for.
5518 static void
5519 gen_libfunc (optab optable, const char *opname, int suffix,
5520 machine_mode mode)
5522 unsigned opname_len = strlen (opname);
5523 const char *mname = GET_MODE_NAME (mode);
5524 unsigned mname_len = strlen (mname);
5525 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5526 int len = prefix_len + opname_len + mname_len + 1 + 1;
5527 char *libfunc_name = XALLOCAVEC (char, len);
5528 char *p;
5529 const char *q;
5531 p = libfunc_name;
5532 *p++ = '_';
5533 *p++ = '_';
5534 if (targetm.libfunc_gnu_prefix)
5536 *p++ = 'g';
5537 *p++ = 'n';
5538 *p++ = 'u';
5539 *p++ = '_';
5541 for (q = opname; *q; )
5542 *p++ = *q++;
5543 for (q = mname; *q; q++)
5544 *p++ = TOLOWER (*q);
5545 *p++ = suffix;
5546 *p = '\0';
5548 set_optab_libfunc (optable, mode,
5549 ggc_alloc_string (libfunc_name, p - libfunc_name));
5552 /* Like gen_libfunc, but verify that integer operation is involved. */
5554 void
5555 gen_int_libfunc (optab optable, const char *opname, char suffix,
5556 machine_mode mode)
5558 int maxsize = 2 * BITS_PER_WORD;
5559 int minsize = BITS_PER_WORD;
5561 if (GET_MODE_CLASS (mode) != MODE_INT)
5562 return;
5563 if (maxsize < LONG_LONG_TYPE_SIZE)
5564 maxsize = LONG_LONG_TYPE_SIZE;
5565 if (minsize > INT_TYPE_SIZE
5566 && (trapv_binoptab_p (optable)
5567 || trapv_unoptab_p (optable)))
5568 minsize = INT_TYPE_SIZE;
5569 if (GET_MODE_BITSIZE (mode) < minsize
5570 || GET_MODE_BITSIZE (mode) > maxsize)
5571 return;
5572 gen_libfunc (optable, opname, suffix, mode);
5575 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5577 void
5578 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5579 machine_mode mode)
5581 char *dec_opname;
5583 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5584 gen_libfunc (optable, opname, suffix, mode);
5585 if (DECIMAL_FLOAT_MODE_P (mode))
5587 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5588 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5589 depending on the low level floating format used. */
5590 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5591 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5592 gen_libfunc (optable, dec_opname, suffix, mode);
5596 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5598 void
5599 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5600 machine_mode mode)
5602 if (!ALL_FIXED_POINT_MODE_P (mode))
5603 return;
5604 gen_libfunc (optable, opname, suffix, mode);
5607 /* Like gen_libfunc, but verify that signed fixed-point operation is
5608 involved. */
5610 void
5611 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5612 machine_mode mode)
5614 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5615 return;
5616 gen_libfunc (optable, opname, suffix, mode);
5619 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5620 involved. */
5622 void
5623 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5624 machine_mode mode)
5626 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5627 return;
5628 gen_libfunc (optable, opname, suffix, mode);
5631 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5633 void
5634 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5635 machine_mode mode)
5637 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5638 gen_fp_libfunc (optable, name, suffix, mode);
5639 if (INTEGRAL_MODE_P (mode))
5640 gen_int_libfunc (optable, name, suffix, mode);
5643 /* Like gen_libfunc, but verify that FP or INT operation is involved
5644 and add 'v' suffix for integer operation. */
5646 void
5647 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5648 machine_mode mode)
5650 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5651 gen_fp_libfunc (optable, name, suffix, mode);
5652 if (GET_MODE_CLASS (mode) == MODE_INT)
5654 int len = strlen (name);
5655 char *v_name = XALLOCAVEC (char, len + 2);
5656 strcpy (v_name, name);
5657 v_name[len] = 'v';
5658 v_name[len + 1] = 0;
5659 gen_int_libfunc (optable, v_name, suffix, mode);
5663 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5664 involved. */
5666 void
5667 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5668 machine_mode mode)
5670 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5671 gen_fp_libfunc (optable, name, suffix, mode);
5672 if (INTEGRAL_MODE_P (mode))
5673 gen_int_libfunc (optable, name, suffix, mode);
5674 if (ALL_FIXED_POINT_MODE_P (mode))
5675 gen_fixed_libfunc (optable, name, suffix, mode);
5678 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5679 involved. */
5681 void
5682 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5683 machine_mode mode)
5685 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5686 gen_fp_libfunc (optable, name, suffix, mode);
5687 if (INTEGRAL_MODE_P (mode))
5688 gen_int_libfunc (optable, name, suffix, mode);
5689 if (SIGNED_FIXED_POINT_MODE_P (mode))
5690 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5693 /* Like gen_libfunc, but verify that INT or FIXED operation is
5694 involved. */
5696 void
5697 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5698 machine_mode mode)
5700 if (INTEGRAL_MODE_P (mode))
5701 gen_int_libfunc (optable, name, suffix, mode);
5702 if (ALL_FIXED_POINT_MODE_P (mode))
5703 gen_fixed_libfunc (optable, name, suffix, mode);
5706 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5707 involved. */
5709 void
5710 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5711 machine_mode mode)
5713 if (INTEGRAL_MODE_P (mode))
5714 gen_int_libfunc (optable, name, suffix, mode);
5715 if (SIGNED_FIXED_POINT_MODE_P (mode))
5716 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5719 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5720 involved. */
5722 void
5723 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5724 machine_mode mode)
5726 if (INTEGRAL_MODE_P (mode))
5727 gen_int_libfunc (optable, name, suffix, mode);
5728 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5729 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5732 /* Initialize the libfunc fields of an entire group of entries of an
5733 inter-mode-class conversion optab. The string formation rules are
5734 similar to the ones for init_libfuncs, above, but instead of having
5735 a mode name and an operand count these functions have two mode names
5736 and no operand count. */
5738 void
5739 gen_interclass_conv_libfunc (convert_optab tab,
5740 const char *opname,
5741 machine_mode tmode,
5742 machine_mode fmode)
5744 size_t opname_len = strlen (opname);
5745 size_t mname_len = 0;
5747 const char *fname, *tname;
5748 const char *q;
5749 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5750 char *libfunc_name, *suffix;
5751 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5752 char *p;
5754 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5755 depends on which underlying decimal floating point format is used. */
5756 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5758 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5760 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5761 nondec_name[0] = '_';
5762 nondec_name[1] = '_';
5763 if (targetm.libfunc_gnu_prefix)
5765 nondec_name[2] = 'g';
5766 nondec_name[3] = 'n';
5767 nondec_name[4] = 'u';
5768 nondec_name[5] = '_';
5771 memcpy (&nondec_name[prefix_len], opname, opname_len);
5772 nondec_suffix = nondec_name + opname_len + prefix_len;
5774 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5775 dec_name[0] = '_';
5776 dec_name[1] = '_';
5777 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5778 memcpy (&dec_name[2+dec_len], opname, opname_len);
5779 dec_suffix = dec_name + dec_len + opname_len + 2;
5781 fname = GET_MODE_NAME (fmode);
5782 tname = GET_MODE_NAME (tmode);
5784 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5786 libfunc_name = dec_name;
5787 suffix = dec_suffix;
5789 else
5791 libfunc_name = nondec_name;
5792 suffix = nondec_suffix;
5795 p = suffix;
5796 for (q = fname; *q; p++, q++)
5797 *p = TOLOWER (*q);
5798 for (q = tname; *q; p++, q++)
5799 *p = TOLOWER (*q);
5801 *p = '\0';
5803 set_conv_libfunc (tab, tmode, fmode,
5804 ggc_alloc_string (libfunc_name, p - libfunc_name));
5807 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5808 int->fp conversion. */
5810 void
5811 gen_int_to_fp_conv_libfunc (convert_optab tab,
5812 const char *opname,
5813 machine_mode tmode,
5814 machine_mode fmode)
5816 if (GET_MODE_CLASS (fmode) != MODE_INT)
5817 return;
5818 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5819 return;
5820 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5823 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5824 naming scheme. */
5826 void
5827 gen_ufloat_conv_libfunc (convert_optab tab,
5828 const char *opname ATTRIBUTE_UNUSED,
5829 machine_mode tmode,
5830 machine_mode fmode)
5832 if (DECIMAL_FLOAT_MODE_P (tmode))
5833 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5834 else
5835 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5838 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5839 fp->int conversion. */
5841 void
5842 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5843 const char *opname,
5844 machine_mode tmode,
5845 machine_mode fmode)
5847 if (GET_MODE_CLASS (fmode) != MODE_INT)
5848 return;
5849 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5850 return;
5851 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5854 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5855 fp->int conversion with no decimal floating point involved. */
5857 void
5858 gen_fp_to_int_conv_libfunc (convert_optab tab,
5859 const char *opname,
5860 machine_mode tmode,
5861 machine_mode fmode)
5863 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5864 return;
5865 if (GET_MODE_CLASS (tmode) != MODE_INT)
5866 return;
5867 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5870 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5871 The string formation rules are
5872 similar to the ones for init_libfunc, above. */
5874 void
5875 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5876 machine_mode tmode, machine_mode fmode)
5878 size_t opname_len = strlen (opname);
5879 size_t mname_len = 0;
5881 const char *fname, *tname;
5882 const char *q;
5883 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5884 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5885 char *libfunc_name, *suffix;
5886 char *p;
5888 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5889 depends on which underlying decimal floating point format is used. */
5890 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5892 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5894 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5895 nondec_name[0] = '_';
5896 nondec_name[1] = '_';
5897 if (targetm.libfunc_gnu_prefix)
5899 nondec_name[2] = 'g';
5900 nondec_name[3] = 'n';
5901 nondec_name[4] = 'u';
5902 nondec_name[5] = '_';
5904 memcpy (&nondec_name[prefix_len], opname, opname_len);
5905 nondec_suffix = nondec_name + opname_len + prefix_len;
5907 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5908 dec_name[0] = '_';
5909 dec_name[1] = '_';
5910 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5911 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5912 dec_suffix = dec_name + dec_len + opname_len + 2;
5914 fname = GET_MODE_NAME (fmode);
5915 tname = GET_MODE_NAME (tmode);
5917 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5919 libfunc_name = dec_name;
5920 suffix = dec_suffix;
5922 else
5924 libfunc_name = nondec_name;
5925 suffix = nondec_suffix;
5928 p = suffix;
5929 for (q = fname; *q; p++, q++)
5930 *p = TOLOWER (*q);
5931 for (q = tname; *q; p++, q++)
5932 *p = TOLOWER (*q);
5934 *p++ = '2';
5935 *p = '\0';
5937 set_conv_libfunc (tab, tmode, fmode,
5938 ggc_alloc_string (libfunc_name, p - libfunc_name));
5941 /* Pick proper libcall for trunc_optab. We need to chose if we do
5942 truncation or extension and interclass or intraclass. */
5944 void
5945 gen_trunc_conv_libfunc (convert_optab tab,
5946 const char *opname,
5947 machine_mode tmode,
5948 machine_mode fmode)
5950 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5951 return;
5952 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5953 return;
5954 if (tmode == fmode)
5955 return;
5957 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5958 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5959 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5961 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5962 return;
5964 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5965 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5966 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5967 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5970 /* Pick proper libcall for extend_optab. We need to chose if we do
5971 truncation or extension and interclass or intraclass. */
5973 void
5974 gen_extend_conv_libfunc (convert_optab tab,
5975 const char *opname ATTRIBUTE_UNUSED,
5976 machine_mode tmode,
5977 machine_mode fmode)
5979 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5980 return;
5981 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5982 return;
5983 if (tmode == fmode)
5984 return;
5986 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5987 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5988 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5990 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5991 return;
5993 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5994 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5995 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5996 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5999 /* Pick proper libcall for fract_optab. We need to chose if we do
6000 interclass or intraclass. */
6002 void
6003 gen_fract_conv_libfunc (convert_optab tab,
6004 const char *opname,
6005 machine_mode tmode,
6006 machine_mode fmode)
6008 if (tmode == fmode)
6009 return;
6010 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
6011 return;
6013 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6014 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6015 else
6016 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6019 /* Pick proper libcall for fractuns_optab. */
6021 void
6022 gen_fractuns_conv_libfunc (convert_optab tab,
6023 const char *opname,
6024 machine_mode tmode,
6025 machine_mode fmode)
6027 if (tmode == fmode)
6028 return;
6029 /* One mode must be a fixed-point mode, and the other must be an integer
6030 mode. */
6031 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
6032 || (ALL_FIXED_POINT_MODE_P (fmode)
6033 && GET_MODE_CLASS (tmode) == MODE_INT)))
6034 return;
6036 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6039 /* Pick proper libcall for satfract_optab. We need to chose if we do
6040 interclass or intraclass. */
6042 void
6043 gen_satfract_conv_libfunc (convert_optab tab,
6044 const char *opname,
6045 machine_mode tmode,
6046 machine_mode fmode)
6048 if (tmode == fmode)
6049 return;
6050 /* TMODE must be a fixed-point mode. */
6051 if (!ALL_FIXED_POINT_MODE_P (tmode))
6052 return;
6054 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6055 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6056 else
6057 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6060 /* Pick proper libcall for satfractuns_optab. */
6062 void
6063 gen_satfractuns_conv_libfunc (convert_optab tab,
6064 const char *opname,
6065 machine_mode tmode,
6066 machine_mode fmode)
6068 if (tmode == fmode)
6069 return;
6070 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
6071 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
6072 return;
6074 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6077 /* Hashtable callbacks for libfunc_decls. */
6079 struct libfunc_decl_hasher : ggc_hasher<tree>
6081 static hashval_t
6082 hash (tree entry)
6084 return IDENTIFIER_HASH_VALUE (DECL_NAME (entry));
6087 static bool
6088 equal (tree decl, tree name)
6090 return DECL_NAME (decl) == name;
6094 /* A table of previously-created libfuncs, hashed by name. */
6095 static GTY (()) hash_table<libfunc_decl_hasher> *libfunc_decls;
6097 /* Build a decl for a libfunc named NAME. */
6099 tree
6100 build_libfunc_function (const char *name)
6102 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6103 get_identifier (name),
6104 build_function_type (integer_type_node, NULL_TREE));
6105 /* ??? We don't have any type information except for this is
6106 a function. Pretend this is "int foo()". */
6107 DECL_ARTIFICIAL (decl) = 1;
6108 DECL_EXTERNAL (decl) = 1;
6109 TREE_PUBLIC (decl) = 1;
6110 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6112 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6113 are the flags assigned by targetm.encode_section_info. */
6114 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6116 return decl;
6120 init_one_libfunc (const char *name)
6122 tree id, decl;
6123 hashval_t hash;
6125 if (libfunc_decls == NULL)
6126 libfunc_decls = hash_table<libfunc_decl_hasher>::create_ggc (37);
6128 /* See if we have already created a libfunc decl for this function. */
6129 id = get_identifier (name);
6130 hash = IDENTIFIER_HASH_VALUE (id);
6131 tree *slot = libfunc_decls->find_slot_with_hash (id, hash, INSERT);
6132 decl = *slot;
6133 if (decl == NULL)
6135 /* Create a new decl, so that it can be passed to
6136 targetm.encode_section_info. */
6137 decl = build_libfunc_function (name);
6138 *slot = decl;
6140 return XEXP (DECL_RTL (decl), 0);
6143 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6146 set_user_assembler_libfunc (const char *name, const char *asmspec)
6148 tree id, decl;
6149 hashval_t hash;
6151 id = get_identifier (name);
6152 hash = IDENTIFIER_HASH_VALUE (id);
6153 tree *slot = libfunc_decls->find_slot_with_hash (id, hash, NO_INSERT);
6154 gcc_assert (slot);
6155 decl = (tree) *slot;
6156 set_user_assembler_name (decl, asmspec);
6157 return XEXP (DECL_RTL (decl), 0);
6160 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6161 MODE to NAME, which should be either 0 or a string constant. */
6162 void
6163 set_optab_libfunc (optab op, machine_mode mode, const char *name)
6165 rtx val;
6166 struct libfunc_entry e;
6167 struct libfunc_entry **slot;
6169 e.op = op;
6170 e.mode1 = mode;
6171 e.mode2 = VOIDmode;
6173 if (name)
6174 val = init_one_libfunc (name);
6175 else
6176 val = 0;
6177 slot = libfunc_hash->find_slot (&e, INSERT);
6178 if (*slot == NULL)
6179 *slot = ggc_alloc<libfunc_entry> ();
6180 (*slot)->op = op;
6181 (*slot)->mode1 = mode;
6182 (*slot)->mode2 = VOIDmode;
6183 (*slot)->libfunc = val;
6186 /* Call this to reset the function entry for one conversion optab
6187 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6188 either 0 or a string constant. */
6189 void
6190 set_conv_libfunc (convert_optab optab, machine_mode tmode,
6191 machine_mode fmode, const char *name)
6193 rtx val;
6194 struct libfunc_entry e;
6195 struct libfunc_entry **slot;
6197 e.op = optab;
6198 e.mode1 = tmode;
6199 e.mode2 = fmode;
6201 if (name)
6202 val = init_one_libfunc (name);
6203 else
6204 val = 0;
6205 slot = libfunc_hash->find_slot (&e, INSERT);
6206 if (*slot == NULL)
6207 *slot = ggc_alloc<libfunc_entry> ();
6208 (*slot)->op = optab;
6209 (*slot)->mode1 = tmode;
6210 (*slot)->mode2 = fmode;
6211 (*slot)->libfunc = val;
6214 /* Call this to initialize the contents of the optabs
6215 appropriately for the current target machine. */
6217 void
6218 init_optabs (void)
6220 if (libfunc_hash)
6221 libfunc_hash->empty ();
6222 else
6223 libfunc_hash = hash_table<libfunc_hasher>::create_ggc (10);
6225 /* Fill in the optabs with the insns we support. */
6226 init_all_optabs (this_fn_optabs);
6228 /* The ffs function operates on `int'. Fall back on it if we do not
6229 have a libgcc2 function for that width. */
6230 if (INT_TYPE_SIZE < BITS_PER_WORD)
6231 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6232 "ffs");
6234 /* Explicitly initialize the bswap libfuncs since we need them to be
6235 valid for things other than word_mode. */
6236 if (targetm.libfunc_gnu_prefix)
6238 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6239 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6241 else
6243 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6244 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6247 /* Use cabs for double complex abs, since systems generally have cabs.
6248 Don't define any libcall for float complex, so that cabs will be used. */
6249 if (complex_double_type_node)
6250 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node),
6251 "cabs");
6253 abort_libfunc = init_one_libfunc ("abort");
6254 memcpy_libfunc = init_one_libfunc ("memcpy");
6255 memmove_libfunc = init_one_libfunc ("memmove");
6256 memcmp_libfunc = init_one_libfunc ("memcmp");
6257 memset_libfunc = init_one_libfunc ("memset");
6258 setbits_libfunc = init_one_libfunc ("__setbits");
6260 #ifndef DONT_USE_BUILTIN_SETJMP
6261 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6262 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6263 #else
6264 setjmp_libfunc = init_one_libfunc ("setjmp");
6265 longjmp_libfunc = init_one_libfunc ("longjmp");
6266 #endif
6267 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6268 unwind_sjlj_unregister_libfunc
6269 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6271 /* For function entry/exit instrumentation. */
6272 profile_function_entry_libfunc
6273 = init_one_libfunc ("__cyg_profile_func_enter");
6274 profile_function_exit_libfunc
6275 = init_one_libfunc ("__cyg_profile_func_exit");
6277 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6279 /* Allow the target to add more libcalls or rename some, etc. */
6280 targetm.init_libfuncs ();
6283 /* Use the current target and options to initialize
6284 TREE_OPTIMIZATION_OPTABS (OPTNODE). */
6286 void
6287 init_tree_optimization_optabs (tree optnode)
6289 /* Quick exit if we have already computed optabs for this target. */
6290 if (TREE_OPTIMIZATION_BASE_OPTABS (optnode) == this_target_optabs)
6291 return;
6293 /* Forget any previous information and set up for the current target. */
6294 TREE_OPTIMIZATION_BASE_OPTABS (optnode) = this_target_optabs;
6295 struct target_optabs *tmp_optabs = (struct target_optabs *)
6296 TREE_OPTIMIZATION_OPTABS (optnode);
6297 if (tmp_optabs)
6298 memset (tmp_optabs, 0, sizeof (struct target_optabs));
6299 else
6300 tmp_optabs = ggc_alloc<target_optabs> ();
6302 /* Generate a new set of optabs into tmp_optabs. */
6303 init_all_optabs (tmp_optabs);
6305 /* If the optabs changed, record it. */
6306 if (memcmp (tmp_optabs, this_target_optabs, sizeof (struct target_optabs)))
6307 TREE_OPTIMIZATION_OPTABS (optnode) = tmp_optabs;
6308 else
6310 TREE_OPTIMIZATION_OPTABS (optnode) = NULL;
6311 ggc_free (tmp_optabs);
6315 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6316 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6318 static void
6319 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6321 machine_mode mode;
6322 char buf[64];
6323 size_t len = strlen (base);
6324 int i;
6326 gcc_assert (max <= 8);
6327 gcc_assert (len + 3 < sizeof (buf));
6329 memcpy (buf, base, len);
6330 buf[len] = '_';
6331 buf[len + 1] = '0';
6332 buf[len + 2] = '\0';
6334 mode = QImode;
6335 for (i = 1; i <= max; i *= 2)
6337 buf[len + 1] = '0' + i;
6338 set_optab_libfunc (tab, mode, buf);
6339 mode = GET_MODE_2XWIDER_MODE (mode);
6343 void
6344 init_sync_libfuncs (int max)
6346 if (!flag_sync_libcalls)
6347 return;
6349 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6350 "__sync_val_compare_and_swap", max);
6351 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6352 "__sync_lock_test_and_set", max);
6354 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6355 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6356 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6357 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6358 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6359 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6361 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6362 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6363 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6364 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6365 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6366 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6369 /* Print information about the current contents of the optabs on
6370 STDERR. */
6372 DEBUG_FUNCTION void
6373 debug_optab_libfuncs (void)
6375 int i, j, k;
6377 /* Dump the arithmetic optabs. */
6378 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
6379 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6381 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
6382 if (l)
6384 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6385 fprintf (stderr, "%s\t%s:\t%s\n",
6386 GET_RTX_NAME (optab_to_code ((optab) i)),
6387 GET_MODE_NAME (j),
6388 XSTR (l, 0));
6392 /* Dump the conversion optabs. */
6393 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
6394 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6395 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6397 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
6398 (machine_mode) k);
6399 if (l)
6401 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6402 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6403 GET_RTX_NAME (optab_to_code ((optab) i)),
6404 GET_MODE_NAME (j),
6405 GET_MODE_NAME (k),
6406 XSTR (l, 0));
6412 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6413 CODE. Return 0 on failure. */
6416 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6418 machine_mode mode = GET_MODE (op1);
6419 enum insn_code icode;
6420 rtx insn;
6421 rtx trap_rtx;
6423 if (mode == VOIDmode)
6424 return 0;
6426 icode = optab_handler (ctrap_optab, mode);
6427 if (icode == CODE_FOR_nothing)
6428 return 0;
6430 /* Some targets only accept a zero trap code. */
6431 if (!insn_operand_matches (icode, 3, tcode))
6432 return 0;
6434 do_pending_stack_adjust ();
6435 start_sequence ();
6436 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6437 &trap_rtx, &mode);
6438 if (!trap_rtx)
6439 insn = NULL_RTX;
6440 else
6441 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6442 tcode);
6444 /* If that failed, then give up. */
6445 if (insn == 0)
6447 end_sequence ();
6448 return 0;
6451 emit_insn (insn);
6452 insn = get_insns ();
6453 end_sequence ();
6454 return insn;
6457 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6458 or unsigned operation code. */
6460 enum rtx_code
6461 get_rtx_code (enum tree_code tcode, bool unsignedp)
6463 enum rtx_code code;
6464 switch (tcode)
6466 case EQ_EXPR:
6467 code = EQ;
6468 break;
6469 case NE_EXPR:
6470 code = NE;
6471 break;
6472 case LT_EXPR:
6473 code = unsignedp ? LTU : LT;
6474 break;
6475 case LE_EXPR:
6476 code = unsignedp ? LEU : LE;
6477 break;
6478 case GT_EXPR:
6479 code = unsignedp ? GTU : GT;
6480 break;
6481 case GE_EXPR:
6482 code = unsignedp ? GEU : GE;
6483 break;
6485 case UNORDERED_EXPR:
6486 code = UNORDERED;
6487 break;
6488 case ORDERED_EXPR:
6489 code = ORDERED;
6490 break;
6491 case UNLT_EXPR:
6492 code = UNLT;
6493 break;
6494 case UNLE_EXPR:
6495 code = UNLE;
6496 break;
6497 case UNGT_EXPR:
6498 code = UNGT;
6499 break;
6500 case UNGE_EXPR:
6501 code = UNGE;
6502 break;
6503 case UNEQ_EXPR:
6504 code = UNEQ;
6505 break;
6506 case LTGT_EXPR:
6507 code = LTGT;
6508 break;
6510 case BIT_AND_EXPR:
6511 code = AND;
6512 break;
6514 case BIT_IOR_EXPR:
6515 code = IOR;
6516 break;
6518 default:
6519 gcc_unreachable ();
6521 return code;
6524 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6525 unsigned operators. Do not generate compare instruction. */
6527 static rtx
6528 vector_compare_rtx (enum tree_code tcode, tree t_op0, tree t_op1,
6529 bool unsignedp, enum insn_code icode)
6531 struct expand_operand ops[2];
6532 rtx rtx_op0, rtx_op1;
6533 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
6535 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
6537 /* Expand operands. */
6538 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6539 EXPAND_STACK_PARM);
6540 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6541 EXPAND_STACK_PARM);
6543 create_input_operand (&ops[0], rtx_op0, GET_MODE (rtx_op0));
6544 create_input_operand (&ops[1], rtx_op1, GET_MODE (rtx_op1));
6545 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6546 gcc_unreachable ();
6547 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6550 /* Return true if VEC_PERM_EXPR of arbitrary input vectors can be expanded using
6551 SIMD extensions of the CPU. SEL may be NULL, which stands for an unknown
6552 constant. Note that additional permutations representing whole-vector shifts
6553 may also be handled via the vec_shr optab, but only where the second input
6554 vector is entirely constant zeroes; this case is not dealt with here. */
6556 bool
6557 can_vec_perm_p (machine_mode mode, bool variable,
6558 const unsigned char *sel)
6560 machine_mode qimode;
6562 /* If the target doesn't implement a vector mode for the vector type,
6563 then no operations are supported. */
6564 if (!VECTOR_MODE_P (mode))
6565 return false;
6567 if (!variable)
6569 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6570 && (sel == NULL
6571 || targetm.vectorize.vec_perm_const_ok == NULL
6572 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6573 return true;
6576 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6577 return true;
6579 /* We allow fallback to a QI vector mode, and adjust the mask. */
6580 if (GET_MODE_INNER (mode) == QImode)
6581 return false;
6582 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6583 if (!VECTOR_MODE_P (qimode))
6584 return false;
6586 /* ??? For completeness, we ought to check the QImode version of
6587 vec_perm_const_optab. But all users of this implicit lowering
6588 feature implement the variable vec_perm_optab. */
6589 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6590 return false;
6592 /* In order to support the lowering of variable permutations,
6593 we need to support shifts and adds. */
6594 if (variable)
6596 if (GET_MODE_UNIT_SIZE (mode) > 2
6597 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6598 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6599 return false;
6600 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6601 return false;
6604 return true;
6607 /* Checks if vec_perm mask SEL is a constant equivalent to a shift of the first
6608 vec_perm operand, assuming the second operand is a constant vector of zeroes.
6609 Return the shift distance in bits if so, or NULL_RTX if the vec_perm is not a
6610 shift. */
6611 static rtx
6612 shift_amt_for_vec_perm_mask (rtx sel)
6614 unsigned int i, first, nelt = GET_MODE_NUNITS (GET_MODE (sel));
6615 unsigned int bitsize = GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (sel)));
6617 if (GET_CODE (sel) != CONST_VECTOR)
6618 return NULL_RTX;
6620 first = INTVAL (CONST_VECTOR_ELT (sel, 0));
6621 if (first >= 2*nelt)
6622 return NULL_RTX;
6623 for (i = 1; i < nelt; i++)
6625 int idx = INTVAL (CONST_VECTOR_ELT (sel, i));
6626 unsigned int expected = (i + first) & (2 * nelt - 1);
6627 /* Indices into the second vector are all equivalent. */
6628 if (idx < 0 || (MIN (nelt, (unsigned) idx) != MIN (nelt, expected)))
6629 return NULL_RTX;
6632 return GEN_INT (first * bitsize);
6635 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
6637 static rtx
6638 expand_vec_perm_1 (enum insn_code icode, rtx target,
6639 rtx v0, rtx v1, rtx sel)
6641 machine_mode tmode = GET_MODE (target);
6642 machine_mode smode = GET_MODE (sel);
6643 struct expand_operand ops[4];
6645 create_output_operand (&ops[0], target, tmode);
6646 create_input_operand (&ops[3], sel, smode);
6648 /* Make an effort to preserve v0 == v1. The target expander is able to
6649 rely on this to determine if we're permuting a single input operand. */
6650 if (rtx_equal_p (v0, v1))
6652 if (!insn_operand_matches (icode, 1, v0))
6653 v0 = force_reg (tmode, v0);
6654 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6655 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6657 create_fixed_operand (&ops[1], v0);
6658 create_fixed_operand (&ops[2], v0);
6660 else
6662 create_input_operand (&ops[1], v0, tmode);
6663 /* See if this can be handled with a vec_shr. We only do this if the
6664 second vector is all zeroes. */
6665 enum insn_code shift_code = optab_handler (vec_shr_optab, GET_MODE (v0));
6666 if (v1 == CONST0_RTX (GET_MODE (v1)) && shift_code)
6667 if (rtx shift_amt = shift_amt_for_vec_perm_mask (sel))
6669 create_convert_operand_from_type (&ops[2], shift_amt,
6670 sizetype_tab[(int) stk_sizetype]);
6671 if (maybe_expand_insn (shift_code, 3, ops))
6672 return ops[0].value;
6674 create_input_operand (&ops[2], v1, tmode);
6677 if (maybe_expand_insn (icode, 4, ops))
6678 return ops[0].value;
6679 return NULL_RTX;
6682 /* Generate instructions for vec_perm optab given its mode
6683 and three operands. */
6686 expand_vec_perm (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6688 enum insn_code icode;
6689 machine_mode qimode;
6690 unsigned int i, w, e, u;
6691 rtx tmp, sel_qi = NULL;
6692 rtvec vec;
6694 if (!target || GET_MODE (target) != mode)
6695 target = gen_reg_rtx (mode);
6697 w = GET_MODE_SIZE (mode);
6698 e = GET_MODE_NUNITS (mode);
6699 u = GET_MODE_UNIT_SIZE (mode);
6701 /* Set QIMODE to a different vector mode with byte elements.
6702 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6703 qimode = VOIDmode;
6704 if (GET_MODE_INNER (mode) != QImode)
6706 qimode = mode_for_vector (QImode, w);
6707 if (!VECTOR_MODE_P (qimode))
6708 qimode = VOIDmode;
6711 /* If the input is a constant, expand it specially. */
6712 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6713 if (GET_CODE (sel) == CONST_VECTOR)
6715 icode = direct_optab_handler (vec_perm_const_optab, mode);
6716 if (icode != CODE_FOR_nothing)
6718 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6719 if (tmp)
6720 return tmp;
6723 /* Fall back to a constant byte-based permutation. */
6724 if (qimode != VOIDmode)
6726 vec = rtvec_alloc (w);
6727 for (i = 0; i < e; ++i)
6729 unsigned int j, this_e;
6731 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6732 this_e &= 2 * e - 1;
6733 this_e *= u;
6735 for (j = 0; j < u; ++j)
6736 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
6738 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
6740 icode = direct_optab_handler (vec_perm_const_optab, qimode);
6741 if (icode != CODE_FOR_nothing)
6743 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6744 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6745 gen_lowpart (qimode, v1), sel_qi);
6746 if (tmp)
6747 return gen_lowpart (mode, tmp);
6752 /* Otherwise expand as a fully variable permuation. */
6753 icode = direct_optab_handler (vec_perm_optab, mode);
6754 if (icode != CODE_FOR_nothing)
6756 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6757 if (tmp)
6758 return tmp;
6761 /* As a special case to aid several targets, lower the element-based
6762 permutation to a byte-based permutation and try again. */
6763 if (qimode == VOIDmode)
6764 return NULL_RTX;
6765 icode = direct_optab_handler (vec_perm_optab, qimode);
6766 if (icode == CODE_FOR_nothing)
6767 return NULL_RTX;
6769 if (sel_qi == NULL)
6771 /* Multiply each element by its byte size. */
6772 machine_mode selmode = GET_MODE (sel);
6773 if (u == 2)
6774 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6775 sel, 0, OPTAB_DIRECT);
6776 else
6777 sel = expand_simple_binop (selmode, ASHIFT, sel,
6778 GEN_INT (exact_log2 (u)),
6779 sel, 0, OPTAB_DIRECT);
6780 gcc_assert (sel != NULL);
6782 /* Broadcast the low byte each element into each of its bytes. */
6783 vec = rtvec_alloc (w);
6784 for (i = 0; i < w; ++i)
6786 int this_e = i / u * u;
6787 if (BYTES_BIG_ENDIAN)
6788 this_e += u - 1;
6789 RTVEC_ELT (vec, i) = GEN_INT (this_e);
6791 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6792 sel = gen_lowpart (qimode, sel);
6793 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
6794 gcc_assert (sel != NULL);
6796 /* Add the byte offset to each byte element. */
6797 /* Note that the definition of the indicies here is memory ordering,
6798 so there should be no difference between big and little endian. */
6799 vec = rtvec_alloc (w);
6800 for (i = 0; i < w; ++i)
6801 RTVEC_ELT (vec, i) = GEN_INT (i % u);
6802 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6803 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6804 sel, 0, OPTAB_DIRECT);
6805 gcc_assert (sel_qi != NULL);
6808 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6809 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6810 gen_lowpart (qimode, v1), sel_qi);
6811 if (tmp)
6812 tmp = gen_lowpart (mode, tmp);
6813 return tmp;
6816 /* Return insn code for a conditional operator with a comparison in
6817 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
6819 static inline enum insn_code
6820 get_vcond_icode (machine_mode vmode, machine_mode cmode, bool uns)
6822 enum insn_code icode = CODE_FOR_nothing;
6823 if (uns)
6824 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
6825 else
6826 icode = convert_optab_handler (vcond_optab, vmode, cmode);
6827 return icode;
6830 /* Return TRUE iff, appropriate vector insns are available
6831 for vector cond expr with vector type VALUE_TYPE and a comparison
6832 with operand vector types in CMP_OP_TYPE. */
6834 bool
6835 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
6837 machine_mode value_mode = TYPE_MODE (value_type);
6838 machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
6839 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
6840 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
6841 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
6842 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
6843 return false;
6844 return true;
6847 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6848 three operands. */
6851 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6852 rtx target)
6854 struct expand_operand ops[6];
6855 enum insn_code icode;
6856 rtx comparison, rtx_op1, rtx_op2;
6857 machine_mode mode = TYPE_MODE (vec_cond_type);
6858 machine_mode cmp_op_mode;
6859 bool unsignedp;
6860 tree op0a, op0b;
6861 enum tree_code tcode;
6863 if (COMPARISON_CLASS_P (op0))
6865 op0a = TREE_OPERAND (op0, 0);
6866 op0b = TREE_OPERAND (op0, 1);
6867 tcode = TREE_CODE (op0);
6869 else
6871 /* Fake op0 < 0. */
6872 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (op0)));
6873 op0a = op0;
6874 op0b = build_zero_cst (TREE_TYPE (op0));
6875 tcode = LT_EXPR;
6877 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6878 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
6881 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
6882 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
6884 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
6885 if (icode == CODE_FOR_nothing)
6886 return 0;
6888 comparison = vector_compare_rtx (tcode, op0a, op0b, unsignedp, icode);
6889 rtx_op1 = expand_normal (op1);
6890 rtx_op2 = expand_normal (op2);
6892 create_output_operand (&ops[0], target, mode);
6893 create_input_operand (&ops[1], rtx_op1, mode);
6894 create_input_operand (&ops[2], rtx_op2, mode);
6895 create_fixed_operand (&ops[3], comparison);
6896 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6897 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6898 expand_insn (icode, 6, ops);
6899 return ops[0].value;
6902 /* Return non-zero if a highpart multiply is supported of can be synthisized.
6903 For the benefit of expand_mult_highpart, the return value is 1 for direct,
6904 2 for even/odd widening, and 3 for hi/lo widening. */
6907 can_mult_highpart_p (machine_mode mode, bool uns_p)
6909 optab op;
6910 unsigned char *sel;
6911 unsigned i, nunits;
6913 op = uns_p ? umul_highpart_optab : smul_highpart_optab;
6914 if (optab_handler (op, mode) != CODE_FOR_nothing)
6915 return 1;
6917 /* If the mode is an integral vector, synth from widening operations. */
6918 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
6919 return 0;
6921 nunits = GET_MODE_NUNITS (mode);
6922 sel = XALLOCAVEC (unsigned char, nunits);
6924 op = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6925 if (optab_handler (op, mode) != CODE_FOR_nothing)
6927 op = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6928 if (optab_handler (op, mode) != CODE_FOR_nothing)
6930 for (i = 0; i < nunits; ++i)
6931 sel[i] = !BYTES_BIG_ENDIAN + (i & ~1) + ((i & 1) ? nunits : 0);
6932 if (can_vec_perm_p (mode, false, sel))
6933 return 2;
6937 op = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6938 if (optab_handler (op, mode) != CODE_FOR_nothing)
6940 op = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6941 if (optab_handler (op, mode) != CODE_FOR_nothing)
6943 for (i = 0; i < nunits; ++i)
6944 sel[i] = 2 * i + (BYTES_BIG_ENDIAN ? 0 : 1);
6945 if (can_vec_perm_p (mode, false, sel))
6946 return 3;
6950 return 0;
6953 /* Expand a highpart multiply. */
6956 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
6957 rtx target, bool uns_p)
6959 struct expand_operand eops[3];
6960 enum insn_code icode;
6961 int method, i, nunits;
6962 machine_mode wmode;
6963 rtx m1, m2, perm;
6964 optab tab1, tab2;
6965 rtvec v;
6967 method = can_mult_highpart_p (mode, uns_p);
6968 switch (method)
6970 case 0:
6971 return NULL_RTX;
6972 case 1:
6973 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6974 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6975 OPTAB_LIB_WIDEN);
6976 case 2:
6977 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6978 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6979 break;
6980 case 3:
6981 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6982 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6983 if (BYTES_BIG_ENDIAN)
6985 optab t = tab1;
6986 tab1 = tab2;
6987 tab2 = t;
6989 break;
6990 default:
6991 gcc_unreachable ();
6994 icode = optab_handler (tab1, mode);
6995 nunits = GET_MODE_NUNITS (mode);
6996 wmode = insn_data[icode].operand[0].mode;
6997 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
6998 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
7000 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
7001 create_input_operand (&eops[1], op0, mode);
7002 create_input_operand (&eops[2], op1, mode);
7003 expand_insn (icode, 3, eops);
7004 m1 = gen_lowpart (mode, eops[0].value);
7006 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
7007 create_input_operand (&eops[1], op0, mode);
7008 create_input_operand (&eops[2], op1, mode);
7009 expand_insn (optab_handler (tab2, mode), 3, eops);
7010 m2 = gen_lowpart (mode, eops[0].value);
7012 v = rtvec_alloc (nunits);
7013 if (method == 2)
7015 for (i = 0; i < nunits; ++i)
7016 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
7017 + ((i & 1) ? nunits : 0));
7019 else
7021 for (i = 0; i < nunits; ++i)
7022 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
7024 perm = gen_rtx_CONST_VECTOR (mode, v);
7026 return expand_vec_perm (mode, m1, m2, perm, target);
7029 /* Return true if target supports vector masked load/store for mode. */
7030 bool
7031 can_vec_mask_load_store_p (machine_mode mode, bool is_load)
7033 optab op = is_load ? maskload_optab : maskstore_optab;
7034 machine_mode vmode;
7035 unsigned int vector_sizes;
7037 /* If mode is vector mode, check it directly. */
7038 if (VECTOR_MODE_P (mode))
7039 return optab_handler (op, mode) != CODE_FOR_nothing;
7041 /* Otherwise, return true if there is some vector mode with
7042 the mask load/store supported. */
7044 /* See if there is any chance the mask load or store might be
7045 vectorized. If not, punt. */
7046 vmode = targetm.vectorize.preferred_simd_mode (mode);
7047 if (!VECTOR_MODE_P (vmode))
7048 return false;
7050 if (optab_handler (op, vmode) != CODE_FOR_nothing)
7051 return true;
7053 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
7054 while (vector_sizes != 0)
7056 unsigned int cur = 1 << floor_log2 (vector_sizes);
7057 vector_sizes &= ~cur;
7058 if (cur <= GET_MODE_SIZE (mode))
7059 continue;
7060 vmode = mode_for_vector (mode, cur / GET_MODE_SIZE (mode));
7061 if (VECTOR_MODE_P (vmode)
7062 && optab_handler (op, vmode) != CODE_FOR_nothing)
7063 return true;
7065 return false;
7068 /* Return true if there is a compare_and_swap pattern. */
7070 bool
7071 can_compare_and_swap_p (machine_mode mode, bool allow_libcall)
7073 enum insn_code icode;
7075 /* Check for __atomic_compare_and_swap. */
7076 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7077 if (icode != CODE_FOR_nothing)
7078 return true;
7080 /* Check for __sync_compare_and_swap. */
7081 icode = optab_handler (sync_compare_and_swap_optab, mode);
7082 if (icode != CODE_FOR_nothing)
7083 return true;
7084 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
7085 return true;
7087 /* No inline compare and swap. */
7088 return false;
7091 /* Return true if an atomic exchange can be performed. */
7093 bool
7094 can_atomic_exchange_p (machine_mode mode, bool allow_libcall)
7096 enum insn_code icode;
7098 /* Check for __atomic_exchange. */
7099 icode = direct_optab_handler (atomic_exchange_optab, mode);
7100 if (icode != CODE_FOR_nothing)
7101 return true;
7103 /* Don't check __sync_test_and_set, as on some platforms that
7104 has reduced functionality. Targets that really do support
7105 a proper exchange should simply be updated to the __atomics. */
7107 return can_compare_and_swap_p (mode, allow_libcall);
7111 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
7112 pattern. */
7114 static void
7115 find_cc_set (rtx x, const_rtx pat, void *data)
7117 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
7118 && GET_CODE (pat) == SET)
7120 rtx *p_cc_reg = (rtx *) data;
7121 gcc_assert (!*p_cc_reg);
7122 *p_cc_reg = x;
7126 /* This is a helper function for the other atomic operations. This function
7127 emits a loop that contains SEQ that iterates until a compare-and-swap
7128 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7129 a set of instructions that takes a value from OLD_REG as an input and
7130 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7131 set to the current contents of MEM. After SEQ, a compare-and-swap will
7132 attempt to update MEM with NEW_REG. The function returns true when the
7133 loop was generated successfully. */
7135 static bool
7136 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7138 machine_mode mode = GET_MODE (mem);
7139 rtx_code_label *label;
7140 rtx cmp_reg, success, oldval;
7142 /* The loop we want to generate looks like
7144 cmp_reg = mem;
7145 label:
7146 old_reg = cmp_reg;
7147 seq;
7148 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
7149 if (success)
7150 goto label;
7152 Note that we only do the plain load from memory once. Subsequent
7153 iterations use the value loaded by the compare-and-swap pattern. */
7155 label = gen_label_rtx ();
7156 cmp_reg = gen_reg_rtx (mode);
7158 emit_move_insn (cmp_reg, mem);
7159 emit_label (label);
7160 emit_move_insn (old_reg, cmp_reg);
7161 if (seq)
7162 emit_insn (seq);
7164 success = NULL_RTX;
7165 oldval = cmp_reg;
7166 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
7167 new_reg, false, MEMMODEL_SEQ_CST,
7168 MEMMODEL_RELAXED))
7169 return false;
7171 if (oldval != cmp_reg)
7172 emit_move_insn (cmp_reg, oldval);
7174 /* Mark this jump predicted not taken. */
7175 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
7176 GET_MODE (success), 1, label, 0);
7177 return true;
7181 /* This function tries to emit an atomic_exchange intruction. VAL is written
7182 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
7183 using TARGET if possible. */
7185 static rtx
7186 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7188 machine_mode mode = GET_MODE (mem);
7189 enum insn_code icode;
7191 /* If the target supports the exchange directly, great. */
7192 icode = direct_optab_handler (atomic_exchange_optab, mode);
7193 if (icode != CODE_FOR_nothing)
7195 struct expand_operand ops[4];
7197 create_output_operand (&ops[0], target, mode);
7198 create_fixed_operand (&ops[1], mem);
7199 create_input_operand (&ops[2], val, mode);
7200 create_integer_operand (&ops[3], model);
7201 if (maybe_expand_insn (icode, 4, ops))
7202 return ops[0].value;
7205 return NULL_RTX;
7208 /* This function tries to implement an atomic exchange operation using
7209 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7210 The previous contents of *MEM are returned, using TARGET if possible.
7211 Since this instructionn is an acquire barrier only, stronger memory
7212 models may require additional barriers to be emitted. */
7214 static rtx
7215 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7216 enum memmodel model)
7218 machine_mode mode = GET_MODE (mem);
7219 enum insn_code icode;
7220 rtx_insn *last_insn = get_last_insn ();
7222 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7224 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7225 exists, and the memory model is stronger than acquire, add a release
7226 barrier before the instruction. */
7228 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST
7229 || (model & MEMMODEL_MASK) == MEMMODEL_RELEASE
7230 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
7231 expand_mem_thread_fence (model);
7233 if (icode != CODE_FOR_nothing)
7235 struct expand_operand ops[3];
7236 create_output_operand (&ops[0], target, mode);
7237 create_fixed_operand (&ops[1], mem);
7238 create_input_operand (&ops[2], val, mode);
7239 if (maybe_expand_insn (icode, 3, ops))
7240 return ops[0].value;
7243 /* If an external test-and-set libcall is provided, use that instead of
7244 any external compare-and-swap that we might get from the compare-and-
7245 swap-loop expansion later. */
7246 if (!can_compare_and_swap_p (mode, false))
7248 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7249 if (libfunc != NULL)
7251 rtx addr;
7253 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7254 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7255 mode, 2, addr, ptr_mode,
7256 val, mode);
7260 /* If the test_and_set can't be emitted, eliminate any barrier that might
7261 have been emitted. */
7262 delete_insns_since (last_insn);
7263 return NULL_RTX;
7266 /* This function tries to implement an atomic exchange operation using a
7267 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7268 *MEM are returned, using TARGET if possible. No memory model is required
7269 since a compare_and_swap loop is seq-cst. */
7271 static rtx
7272 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7274 machine_mode mode = GET_MODE (mem);
7276 if (can_compare_and_swap_p (mode, true))
7278 if (!target || !register_operand (target, mode))
7279 target = gen_reg_rtx (mode);
7280 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7281 return target;
7284 return NULL_RTX;
7287 /* This function tries to implement an atomic test-and-set operation
7288 using the atomic_test_and_set instruction pattern. A boolean value
7289 is returned from the operation, using TARGET if possible. */
7291 #ifndef HAVE_atomic_test_and_set
7292 #define HAVE_atomic_test_and_set 0
7293 #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
7294 #endif
7296 static rtx
7297 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7299 machine_mode pat_bool_mode;
7300 struct expand_operand ops[3];
7302 if (!HAVE_atomic_test_and_set)
7303 return NULL_RTX;
7305 /* While we always get QImode from __atomic_test_and_set, we get
7306 other memory modes from __sync_lock_test_and_set. Note that we
7307 use no endian adjustment here. This matches the 4.6 behavior
7308 in the Sparc backend. */
7309 gcc_checking_assert
7310 (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
7311 if (GET_MODE (mem) != QImode)
7312 mem = adjust_address_nv (mem, QImode, 0);
7314 pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
7315 create_output_operand (&ops[0], target, pat_bool_mode);
7316 create_fixed_operand (&ops[1], mem);
7317 create_integer_operand (&ops[2], model);
7319 if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
7320 return ops[0].value;
7321 return NULL_RTX;
7324 /* This function expands the legacy _sync_lock test_and_set operation which is
7325 generally an atomic exchange. Some limited targets only allow the
7326 constant 1 to be stored. This is an ACQUIRE operation.
7328 TARGET is an optional place to stick the return value.
7329 MEM is where VAL is stored. */
7332 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7334 rtx ret;
7336 /* Try an atomic_exchange first. */
7337 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_ACQUIRE);
7338 if (ret)
7339 return ret;
7341 ret = maybe_emit_sync_lock_test_and_set (target, mem, val, MEMMODEL_ACQUIRE);
7342 if (ret)
7343 return ret;
7345 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7346 if (ret)
7347 return ret;
7349 /* If there are no other options, try atomic_test_and_set if the value
7350 being stored is 1. */
7351 if (val == const1_rtx)
7352 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_ACQUIRE);
7354 return ret;
7357 /* This function expands the atomic test_and_set operation:
7358 atomically store a boolean TRUE into MEM and return the previous value.
7360 MEMMODEL is the memory model variant to use.
7361 TARGET is an optional place to stick the return value. */
7364 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7366 machine_mode mode = GET_MODE (mem);
7367 rtx ret, trueval, subtarget;
7369 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7370 if (ret)
7371 return ret;
7373 /* Be binary compatible with non-default settings of trueval, and different
7374 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7375 another only has atomic-exchange. */
7376 if (targetm.atomic_test_and_set_trueval == 1)
7378 trueval = const1_rtx;
7379 subtarget = target ? target : gen_reg_rtx (mode);
7381 else
7383 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7384 subtarget = gen_reg_rtx (mode);
7387 /* Try the atomic-exchange optab... */
7388 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7390 /* ... then an atomic-compare-and-swap loop ... */
7391 if (!ret)
7392 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7394 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7395 if (!ret)
7396 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7398 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7399 things with the value 1. Thus we try again without trueval. */
7400 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7401 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7403 /* Failing all else, assume a single threaded environment and simply
7404 perform the operation. */
7405 if (!ret)
7407 /* If the result is ignored skip the move to target. */
7408 if (subtarget != const0_rtx)
7409 emit_move_insn (subtarget, mem);
7411 emit_move_insn (mem, trueval);
7412 ret = subtarget;
7415 /* Recall that have to return a boolean value; rectify if trueval
7416 is not exactly one. */
7417 if (targetm.atomic_test_and_set_trueval != 1)
7418 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7420 return ret;
7423 /* This function expands the atomic exchange operation:
7424 atomically store VAL in MEM and return the previous value in MEM.
7426 MEMMODEL is the memory model variant to use.
7427 TARGET is an optional place to stick the return value. */
7430 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7432 rtx ret;
7434 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7436 /* Next try a compare-and-swap loop for the exchange. */
7437 if (!ret)
7438 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7440 return ret;
7443 /* This function expands the atomic compare exchange operation:
7445 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7446 *PTARGET_OVAL is an optional place to store the old value from memory.
7447 Both target parameters may be NULL to indicate that we do not care about
7448 that return value. Both target parameters are updated on success to
7449 the actual location of the corresponding result.
7451 MEMMODEL is the memory model variant to use.
7453 The return value of the function is true for success. */
7455 bool
7456 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7457 rtx mem, rtx expected, rtx desired,
7458 bool is_weak, enum memmodel succ_model,
7459 enum memmodel fail_model)
7461 machine_mode mode = GET_MODE (mem);
7462 struct expand_operand ops[8];
7463 enum insn_code icode;
7464 rtx target_oval, target_bool = NULL_RTX;
7465 rtx libfunc;
7467 /* Load expected into a register for the compare and swap. */
7468 if (MEM_P (expected))
7469 expected = copy_to_reg (expected);
7471 /* Make sure we always have some place to put the return oldval.
7472 Further, make sure that place is distinct from the input expected,
7473 just in case we need that path down below. */
7474 if (ptarget_oval == NULL
7475 || (target_oval = *ptarget_oval) == NULL
7476 || reg_overlap_mentioned_p (expected, target_oval))
7477 target_oval = gen_reg_rtx (mode);
7479 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7480 if (icode != CODE_FOR_nothing)
7482 machine_mode bool_mode = insn_data[icode].operand[0].mode;
7484 /* Make sure we always have a place for the bool operand. */
7485 if (ptarget_bool == NULL
7486 || (target_bool = *ptarget_bool) == NULL
7487 || GET_MODE (target_bool) != bool_mode)
7488 target_bool = gen_reg_rtx (bool_mode);
7490 /* Emit the compare_and_swap. */
7491 create_output_operand (&ops[0], target_bool, bool_mode);
7492 create_output_operand (&ops[1], target_oval, mode);
7493 create_fixed_operand (&ops[2], mem);
7494 create_input_operand (&ops[3], expected, mode);
7495 create_input_operand (&ops[4], desired, mode);
7496 create_integer_operand (&ops[5], is_weak);
7497 create_integer_operand (&ops[6], succ_model);
7498 create_integer_operand (&ops[7], fail_model);
7499 if (maybe_expand_insn (icode, 8, ops))
7501 /* Return success/failure. */
7502 target_bool = ops[0].value;
7503 target_oval = ops[1].value;
7504 goto success;
7508 /* Otherwise fall back to the original __sync_val_compare_and_swap
7509 which is always seq-cst. */
7510 icode = optab_handler (sync_compare_and_swap_optab, mode);
7511 if (icode != CODE_FOR_nothing)
7513 rtx cc_reg;
7515 create_output_operand (&ops[0], target_oval, mode);
7516 create_fixed_operand (&ops[1], mem);
7517 create_input_operand (&ops[2], expected, mode);
7518 create_input_operand (&ops[3], desired, mode);
7519 if (!maybe_expand_insn (icode, 4, ops))
7520 return false;
7522 target_oval = ops[0].value;
7524 /* If the caller isn't interested in the boolean return value,
7525 skip the computation of it. */
7526 if (ptarget_bool == NULL)
7527 goto success;
7529 /* Otherwise, work out if the compare-and-swap succeeded. */
7530 cc_reg = NULL_RTX;
7531 if (have_insn_for (COMPARE, CCmode))
7532 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7533 if (cc_reg)
7535 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7536 const0_rtx, VOIDmode, 0, 1);
7537 goto success;
7539 goto success_bool_from_val;
7542 /* Also check for library support for __sync_val_compare_and_swap. */
7543 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7544 if (libfunc != NULL)
7546 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7547 target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7548 mode, 3, addr, ptr_mode,
7549 expected, mode, desired, mode);
7551 /* Compute the boolean return value only if requested. */
7552 if (ptarget_bool)
7553 goto success_bool_from_val;
7554 else
7555 goto success;
7558 /* Failure. */
7559 return false;
7561 success_bool_from_val:
7562 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7563 expected, VOIDmode, 1, 1);
7564 success:
7565 /* Make sure that the oval output winds up where the caller asked. */
7566 if (ptarget_oval)
7567 *ptarget_oval = target_oval;
7568 if (ptarget_bool)
7569 *ptarget_bool = target_bool;
7570 return true;
7573 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7575 static void
7576 expand_asm_memory_barrier (void)
7578 rtx asm_op, clob;
7580 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7581 rtvec_alloc (0), rtvec_alloc (0),
7582 rtvec_alloc (0), UNKNOWN_LOCATION);
7583 MEM_VOLATILE_P (asm_op) = 1;
7585 clob = gen_rtx_SCRATCH (VOIDmode);
7586 clob = gen_rtx_MEM (BLKmode, clob);
7587 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7589 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7592 /* This routine will either emit the mem_thread_fence pattern or issue a
7593 sync_synchronize to generate a fence for memory model MEMMODEL. */
7595 #ifndef HAVE_mem_thread_fence
7596 # define HAVE_mem_thread_fence 0
7597 # define gen_mem_thread_fence(x) (gcc_unreachable (), NULL_RTX)
7598 #endif
7599 #ifndef HAVE_memory_barrier
7600 # define HAVE_memory_barrier 0
7601 # define gen_memory_barrier() (gcc_unreachable (), NULL_RTX)
7602 #endif
7604 void
7605 expand_mem_thread_fence (enum memmodel model)
7607 if (HAVE_mem_thread_fence)
7608 emit_insn (gen_mem_thread_fence (GEN_INT (model)));
7609 else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
7611 if (HAVE_memory_barrier)
7612 emit_insn (gen_memory_barrier ());
7613 else if (synchronize_libfunc != NULL_RTX)
7614 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7615 else
7616 expand_asm_memory_barrier ();
7620 /* This routine will either emit the mem_signal_fence pattern or issue a
7621 sync_synchronize to generate a fence for memory model MEMMODEL. */
7623 #ifndef HAVE_mem_signal_fence
7624 # define HAVE_mem_signal_fence 0
7625 # define gen_mem_signal_fence(x) (gcc_unreachable (), NULL_RTX)
7626 #endif
7628 void
7629 expand_mem_signal_fence (enum memmodel model)
7631 if (HAVE_mem_signal_fence)
7632 emit_insn (gen_mem_signal_fence (GEN_INT (model)));
7633 else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
7635 /* By default targets are coherent between a thread and the signal
7636 handler running on the same thread. Thus this really becomes a
7637 compiler barrier, in that stores must not be sunk past
7638 (or raised above) a given point. */
7639 expand_asm_memory_barrier ();
7643 /* This function expands the atomic load operation:
7644 return the atomically loaded value in MEM.
7646 MEMMODEL is the memory model variant to use.
7647 TARGET is an option place to stick the return value. */
7650 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7652 machine_mode mode = GET_MODE (mem);
7653 enum insn_code icode;
7655 /* If the target supports the load directly, great. */
7656 icode = direct_optab_handler (atomic_load_optab, mode);
7657 if (icode != CODE_FOR_nothing)
7659 struct expand_operand ops[3];
7661 create_output_operand (&ops[0], target, mode);
7662 create_fixed_operand (&ops[1], mem);
7663 create_integer_operand (&ops[2], model);
7664 if (maybe_expand_insn (icode, 3, ops))
7665 return ops[0].value;
7668 /* If the size of the object is greater than word size on this target,
7669 then we assume that a load will not be atomic. */
7670 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7672 /* Issue val = compare_and_swap (mem, 0, 0).
7673 This may cause the occasional harmless store of 0 when the value is
7674 already 0, but it seems to be OK according to the standards guys. */
7675 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7676 const0_rtx, false, model, model))
7677 return target;
7678 else
7679 /* Otherwise there is no atomic load, leave the library call. */
7680 return NULL_RTX;
7683 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7684 if (!target || target == const0_rtx)
7685 target = gen_reg_rtx (mode);
7687 /* For SEQ_CST, emit a barrier before the load. */
7688 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7689 expand_mem_thread_fence (model);
7691 emit_move_insn (target, mem);
7693 /* Emit the appropriate barrier after the load. */
7694 expand_mem_thread_fence (model);
7696 return target;
7699 /* This function expands the atomic store operation:
7700 Atomically store VAL in MEM.
7701 MEMMODEL is the memory model variant to use.
7702 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7703 function returns const0_rtx if a pattern was emitted. */
7706 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7708 machine_mode mode = GET_MODE (mem);
7709 enum insn_code icode;
7710 struct expand_operand ops[3];
7712 /* If the target supports the store directly, great. */
7713 icode = direct_optab_handler (atomic_store_optab, mode);
7714 if (icode != CODE_FOR_nothing)
7716 create_fixed_operand (&ops[0], mem);
7717 create_input_operand (&ops[1], val, mode);
7718 create_integer_operand (&ops[2], model);
7719 if (maybe_expand_insn (icode, 3, ops))
7720 return const0_rtx;
7723 /* If using __sync_lock_release is a viable alternative, try it. */
7724 if (use_release)
7726 icode = direct_optab_handler (sync_lock_release_optab, mode);
7727 if (icode != CODE_FOR_nothing)
7729 create_fixed_operand (&ops[0], mem);
7730 create_input_operand (&ops[1], const0_rtx, mode);
7731 if (maybe_expand_insn (icode, 2, ops))
7733 /* lock_release is only a release barrier. */
7734 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7735 expand_mem_thread_fence (model);
7736 return const0_rtx;
7741 /* If the size of the object is greater than word size on this target,
7742 a default store will not be atomic, Try a mem_exchange and throw away
7743 the result. If that doesn't work, don't do anything. */
7744 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7746 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7747 if (!target)
7748 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7749 if (target)
7750 return const0_rtx;
7751 else
7752 return NULL_RTX;
7755 /* Otherwise assume stores are atomic, and emit the proper barriers. */
7756 expand_mem_thread_fence (model);
7758 emit_move_insn (mem, val);
7760 /* For SEQ_CST, also emit a barrier after the store. */
7761 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7762 expand_mem_thread_fence (model);
7764 return const0_rtx;
7768 /* Structure containing the pointers and values required to process the
7769 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7771 struct atomic_op_functions
7773 direct_optab mem_fetch_before;
7774 direct_optab mem_fetch_after;
7775 direct_optab mem_no_result;
7776 optab fetch_before;
7777 optab fetch_after;
7778 direct_optab no_result;
7779 enum rtx_code reverse_code;
7783 /* Fill in structure pointed to by OP with the various optab entries for an
7784 operation of type CODE. */
7786 static void
7787 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7789 gcc_assert (op!= NULL);
7791 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7792 in the source code during compilation, and the optab entries are not
7793 computable until runtime. Fill in the values at runtime. */
7794 switch (code)
7796 case PLUS:
7797 op->mem_fetch_before = atomic_fetch_add_optab;
7798 op->mem_fetch_after = atomic_add_fetch_optab;
7799 op->mem_no_result = atomic_add_optab;
7800 op->fetch_before = sync_old_add_optab;
7801 op->fetch_after = sync_new_add_optab;
7802 op->no_result = sync_add_optab;
7803 op->reverse_code = MINUS;
7804 break;
7805 case MINUS:
7806 op->mem_fetch_before = atomic_fetch_sub_optab;
7807 op->mem_fetch_after = atomic_sub_fetch_optab;
7808 op->mem_no_result = atomic_sub_optab;
7809 op->fetch_before = sync_old_sub_optab;
7810 op->fetch_after = sync_new_sub_optab;
7811 op->no_result = sync_sub_optab;
7812 op->reverse_code = PLUS;
7813 break;
7814 case XOR:
7815 op->mem_fetch_before = atomic_fetch_xor_optab;
7816 op->mem_fetch_after = atomic_xor_fetch_optab;
7817 op->mem_no_result = atomic_xor_optab;
7818 op->fetch_before = sync_old_xor_optab;
7819 op->fetch_after = sync_new_xor_optab;
7820 op->no_result = sync_xor_optab;
7821 op->reverse_code = XOR;
7822 break;
7823 case AND:
7824 op->mem_fetch_before = atomic_fetch_and_optab;
7825 op->mem_fetch_after = atomic_and_fetch_optab;
7826 op->mem_no_result = atomic_and_optab;
7827 op->fetch_before = sync_old_and_optab;
7828 op->fetch_after = sync_new_and_optab;
7829 op->no_result = sync_and_optab;
7830 op->reverse_code = UNKNOWN;
7831 break;
7832 case IOR:
7833 op->mem_fetch_before = atomic_fetch_or_optab;
7834 op->mem_fetch_after = atomic_or_fetch_optab;
7835 op->mem_no_result = atomic_or_optab;
7836 op->fetch_before = sync_old_ior_optab;
7837 op->fetch_after = sync_new_ior_optab;
7838 op->no_result = sync_ior_optab;
7839 op->reverse_code = UNKNOWN;
7840 break;
7841 case NOT:
7842 op->mem_fetch_before = atomic_fetch_nand_optab;
7843 op->mem_fetch_after = atomic_nand_fetch_optab;
7844 op->mem_no_result = atomic_nand_optab;
7845 op->fetch_before = sync_old_nand_optab;
7846 op->fetch_after = sync_new_nand_optab;
7847 op->no_result = sync_nand_optab;
7848 op->reverse_code = UNKNOWN;
7849 break;
7850 default:
7851 gcc_unreachable ();
7855 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7856 using memory order MODEL. If AFTER is true the operation needs to return
7857 the value of *MEM after the operation, otherwise the previous value.
7858 TARGET is an optional place to place the result. The result is unused if
7859 it is const0_rtx.
7860 Return the result if there is a better sequence, otherwise NULL_RTX. */
7862 static rtx
7863 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7864 enum memmodel model, bool after)
7866 /* If the value is prefetched, or not used, it may be possible to replace
7867 the sequence with a native exchange operation. */
7868 if (!after || target == const0_rtx)
7870 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7871 if (code == AND && val == const0_rtx)
7873 if (target == const0_rtx)
7874 target = gen_reg_rtx (GET_MODE (mem));
7875 return maybe_emit_atomic_exchange (target, mem, val, model);
7878 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7879 if (code == IOR && val == constm1_rtx)
7881 if (target == const0_rtx)
7882 target = gen_reg_rtx (GET_MODE (mem));
7883 return maybe_emit_atomic_exchange (target, mem, val, model);
7887 return NULL_RTX;
7890 /* Try to emit an instruction for a specific operation varaition.
7891 OPTAB contains the OP functions.
7892 TARGET is an optional place to return the result. const0_rtx means unused.
7893 MEM is the memory location to operate on.
7894 VAL is the value to use in the operation.
7895 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7896 MODEL is the memory model, if used.
7897 AFTER is true if the returned result is the value after the operation. */
7899 static rtx
7900 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7901 rtx val, bool use_memmodel, enum memmodel model, bool after)
7903 machine_mode mode = GET_MODE (mem);
7904 struct expand_operand ops[4];
7905 enum insn_code icode;
7906 int op_counter = 0;
7907 int num_ops;
7909 /* Check to see if there is a result returned. */
7910 if (target == const0_rtx)
7912 if (use_memmodel)
7914 icode = direct_optab_handler (optab->mem_no_result, mode);
7915 create_integer_operand (&ops[2], model);
7916 num_ops = 3;
7918 else
7920 icode = direct_optab_handler (optab->no_result, mode);
7921 num_ops = 2;
7924 /* Otherwise, we need to generate a result. */
7925 else
7927 if (use_memmodel)
7929 icode = direct_optab_handler (after ? optab->mem_fetch_after
7930 : optab->mem_fetch_before, mode);
7931 create_integer_operand (&ops[3], model);
7932 num_ops = 4;
7934 else
7936 icode = optab_handler (after ? optab->fetch_after
7937 : optab->fetch_before, mode);
7938 num_ops = 3;
7940 create_output_operand (&ops[op_counter++], target, mode);
7942 if (icode == CODE_FOR_nothing)
7943 return NULL_RTX;
7945 create_fixed_operand (&ops[op_counter++], mem);
7946 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7947 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7949 if (maybe_expand_insn (icode, num_ops, ops))
7950 return (target == const0_rtx ? const0_rtx : ops[0].value);
7952 return NULL_RTX;
7956 /* This function expands an atomic fetch_OP or OP_fetch operation:
7957 TARGET is an option place to stick the return value. const0_rtx indicates
7958 the result is unused.
7959 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7960 CODE is the operation being performed (OP)
7961 MEMMODEL is the memory model variant to use.
7962 AFTER is true to return the result of the operation (OP_fetch).
7963 AFTER is false to return the value before the operation (fetch_OP).
7965 This function will *only* generate instructions if there is a direct
7966 optab. No compare and swap loops or libcalls will be generated. */
7968 static rtx
7969 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7970 enum rtx_code code, enum memmodel model,
7971 bool after)
7973 machine_mode mode = GET_MODE (mem);
7974 struct atomic_op_functions optab;
7975 rtx result;
7976 bool unused_result = (target == const0_rtx);
7978 get_atomic_op_for_code (&optab, code);
7980 /* Check to see if there are any better instructions. */
7981 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7982 if (result)
7983 return result;
7985 /* Check for the case where the result isn't used and try those patterns. */
7986 if (unused_result)
7988 /* Try the memory model variant first. */
7989 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7990 if (result)
7991 return result;
7993 /* Next try the old style withuot a memory model. */
7994 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7995 if (result)
7996 return result;
7998 /* There is no no-result pattern, so try patterns with a result. */
7999 target = NULL_RTX;
8002 /* Try the __atomic version. */
8003 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
8004 if (result)
8005 return result;
8007 /* Try the older __sync version. */
8008 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
8009 if (result)
8010 return result;
8012 /* If the fetch value can be calculated from the other variation of fetch,
8013 try that operation. */
8014 if (after || unused_result || optab.reverse_code != UNKNOWN)
8016 /* Try the __atomic version, then the older __sync version. */
8017 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
8018 if (!result)
8019 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
8021 if (result)
8023 /* If the result isn't used, no need to do compensation code. */
8024 if (unused_result)
8025 return result;
8027 /* Issue compensation code. Fetch_after == fetch_before OP val.
8028 Fetch_before == after REVERSE_OP val. */
8029 if (!after)
8030 code = optab.reverse_code;
8031 if (code == NOT)
8033 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
8034 true, OPTAB_LIB_WIDEN);
8035 result = expand_simple_unop (mode, NOT, result, target, true);
8037 else
8038 result = expand_simple_binop (mode, code, result, val, target,
8039 true, OPTAB_LIB_WIDEN);
8040 return result;
8044 /* No direct opcode can be generated. */
8045 return NULL_RTX;
8050 /* This function expands an atomic fetch_OP or OP_fetch operation:
8051 TARGET is an option place to stick the return value. const0_rtx indicates
8052 the result is unused.
8053 atomically fetch MEM, perform the operation with VAL and return it to MEM.
8054 CODE is the operation being performed (OP)
8055 MEMMODEL is the memory model variant to use.
8056 AFTER is true to return the result of the operation (OP_fetch).
8057 AFTER is false to return the value before the operation (fetch_OP). */
8059 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
8060 enum memmodel model, bool after)
8062 machine_mode mode = GET_MODE (mem);
8063 rtx result;
8064 bool unused_result = (target == const0_rtx);
8066 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
8067 after);
8069 if (result)
8070 return result;
8072 /* Add/sub can be implemented by doing the reverse operation with -(val). */
8073 if (code == PLUS || code == MINUS)
8075 rtx tmp;
8076 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
8078 start_sequence ();
8079 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
8080 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
8081 model, after);
8082 if (result)
8084 /* PLUS worked so emit the insns and return. */
8085 tmp = get_insns ();
8086 end_sequence ();
8087 emit_insn (tmp);
8088 return result;
8091 /* PLUS did not work, so throw away the negation code and continue. */
8092 end_sequence ();
8095 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
8096 if (!can_compare_and_swap_p (mode, false))
8098 rtx libfunc;
8099 bool fixup = false;
8100 enum rtx_code orig_code = code;
8101 struct atomic_op_functions optab;
8103 get_atomic_op_for_code (&optab, code);
8104 libfunc = optab_libfunc (after ? optab.fetch_after
8105 : optab.fetch_before, mode);
8106 if (libfunc == NULL
8107 && (after || unused_result || optab.reverse_code != UNKNOWN))
8109 fixup = true;
8110 if (!after)
8111 code = optab.reverse_code;
8112 libfunc = optab_libfunc (after ? optab.fetch_before
8113 : optab.fetch_after, mode);
8115 if (libfunc != NULL)
8117 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
8118 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
8119 2, addr, ptr_mode, val, mode);
8121 if (!unused_result && fixup)
8122 result = expand_simple_binop (mode, code, result, val, target,
8123 true, OPTAB_LIB_WIDEN);
8124 return result;
8127 /* We need the original code for any further attempts. */
8128 code = orig_code;
8131 /* If nothing else has succeeded, default to a compare and swap loop. */
8132 if (can_compare_and_swap_p (mode, true))
8134 rtx_insn *insn;
8135 rtx t0 = gen_reg_rtx (mode), t1;
8137 start_sequence ();
8139 /* If the result is used, get a register for it. */
8140 if (!unused_result)
8142 if (!target || !register_operand (target, mode))
8143 target = gen_reg_rtx (mode);
8144 /* If fetch_before, copy the value now. */
8145 if (!after)
8146 emit_move_insn (target, t0);
8148 else
8149 target = const0_rtx;
8151 t1 = t0;
8152 if (code == NOT)
8154 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
8155 true, OPTAB_LIB_WIDEN);
8156 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
8158 else
8159 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
8160 OPTAB_LIB_WIDEN);
8162 /* For after, copy the value now. */
8163 if (!unused_result && after)
8164 emit_move_insn (target, t1);
8165 insn = get_insns ();
8166 end_sequence ();
8168 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
8169 return target;
8172 return NULL_RTX;
8175 /* Return true if OPERAND is suitable for operand number OPNO of
8176 instruction ICODE. */
8178 bool
8179 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
8181 return (!insn_data[(int) icode].operand[opno].predicate
8182 || (insn_data[(int) icode].operand[opno].predicate
8183 (operand, insn_data[(int) icode].operand[opno].mode)));
8186 /* TARGET is a target of a multiword operation that we are going to
8187 implement as a series of word-mode operations. Return true if
8188 TARGET is suitable for this purpose. */
8190 bool
8191 valid_multiword_target_p (rtx target)
8193 machine_mode mode;
8194 int i;
8196 mode = GET_MODE (target);
8197 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
8198 if (!validate_subreg (word_mode, mode, target, i))
8199 return false;
8200 return true;
8203 /* Like maybe_legitimize_operand, but do not change the code of the
8204 current rtx value. */
8206 static bool
8207 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8208 struct expand_operand *op)
8210 /* See if the operand matches in its current form. */
8211 if (insn_operand_matches (icode, opno, op->value))
8212 return true;
8214 /* If the operand is a memory whose address has no side effects,
8215 try forcing the address into a non-virtual pseudo register.
8216 The check for side effects is important because copy_to_mode_reg
8217 cannot handle things like auto-modified addresses. */
8218 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8220 rtx addr, mem;
8222 mem = op->value;
8223 addr = XEXP (mem, 0);
8224 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8225 && !side_effects_p (addr))
8227 rtx_insn *last;
8228 machine_mode mode;
8230 last = get_last_insn ();
8231 mode = get_address_mode (mem);
8232 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8233 if (insn_operand_matches (icode, opno, mem))
8235 op->value = mem;
8236 return true;
8238 delete_insns_since (last);
8242 return false;
8245 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8246 on success, storing the new operand value back in OP. */
8248 static bool
8249 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8250 struct expand_operand *op)
8252 machine_mode mode, imode;
8253 bool old_volatile_ok, result;
8255 mode = op->mode;
8256 switch (op->type)
8258 case EXPAND_FIXED:
8259 old_volatile_ok = volatile_ok;
8260 volatile_ok = true;
8261 result = maybe_legitimize_operand_same_code (icode, opno, op);
8262 volatile_ok = old_volatile_ok;
8263 return result;
8265 case EXPAND_OUTPUT:
8266 gcc_assert (mode != VOIDmode);
8267 if (op->value
8268 && op->value != const0_rtx
8269 && GET_MODE (op->value) == mode
8270 && maybe_legitimize_operand_same_code (icode, opno, op))
8271 return true;
8273 op->value = gen_reg_rtx (mode);
8274 break;
8276 case EXPAND_INPUT:
8277 input:
8278 gcc_assert (mode != VOIDmode);
8279 gcc_assert (GET_MODE (op->value) == VOIDmode
8280 || GET_MODE (op->value) == mode);
8281 if (maybe_legitimize_operand_same_code (icode, opno, op))
8282 return true;
8284 op->value = copy_to_mode_reg (mode, op->value);
8285 break;
8287 case EXPAND_CONVERT_TO:
8288 gcc_assert (mode != VOIDmode);
8289 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8290 goto input;
8292 case EXPAND_CONVERT_FROM:
8293 if (GET_MODE (op->value) != VOIDmode)
8294 mode = GET_MODE (op->value);
8295 else
8296 /* The caller must tell us what mode this value has. */
8297 gcc_assert (mode != VOIDmode);
8299 imode = insn_data[(int) icode].operand[opno].mode;
8300 if (imode != VOIDmode && imode != mode)
8302 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8303 mode = imode;
8305 goto input;
8307 case EXPAND_ADDRESS:
8308 gcc_assert (mode != VOIDmode);
8309 op->value = convert_memory_address (mode, op->value);
8310 goto input;
8312 case EXPAND_INTEGER:
8313 mode = insn_data[(int) icode].operand[opno].mode;
8314 if (mode != VOIDmode && const_int_operand (op->value, mode))
8315 goto input;
8316 break;
8318 return insn_operand_matches (icode, opno, op->value);
8321 /* Make OP describe an input operand that should have the same value
8322 as VALUE, after any mode conversion that the target might request.
8323 TYPE is the type of VALUE. */
8325 void
8326 create_convert_operand_from_type (struct expand_operand *op,
8327 rtx value, tree type)
8329 create_convert_operand_from (op, value, TYPE_MODE (type),
8330 TYPE_UNSIGNED (type));
8333 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8334 of instruction ICODE. Return true on success, leaving the new operand
8335 values in the OPS themselves. Emit no code on failure. */
8337 bool
8338 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8339 unsigned int nops, struct expand_operand *ops)
8341 rtx_insn *last;
8342 unsigned int i;
8344 last = get_last_insn ();
8345 for (i = 0; i < nops; i++)
8346 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8348 delete_insns_since (last);
8349 return false;
8351 return true;
8354 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8355 as its operands. Return the instruction pattern on success,
8356 and emit any necessary set-up code. Return null and emit no
8357 code on failure. */
8360 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8361 struct expand_operand *ops)
8363 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8364 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8365 return NULL_RTX;
8367 switch (nops)
8369 case 1:
8370 return GEN_FCN (icode) (ops[0].value);
8371 case 2:
8372 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8373 case 3:
8374 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8375 case 4:
8376 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8377 ops[3].value);
8378 case 5:
8379 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8380 ops[3].value, ops[4].value);
8381 case 6:
8382 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8383 ops[3].value, ops[4].value, ops[5].value);
8384 case 7:
8385 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8386 ops[3].value, ops[4].value, ops[5].value,
8387 ops[6].value);
8388 case 8:
8389 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8390 ops[3].value, ops[4].value, ops[5].value,
8391 ops[6].value, ops[7].value);
8392 case 9:
8393 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8394 ops[3].value, ops[4].value, ops[5].value,
8395 ops[6].value, ops[7].value, ops[8].value);
8397 gcc_unreachable ();
8400 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8401 as its operands. Return true on success and emit no code on failure. */
8403 bool
8404 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8405 struct expand_operand *ops)
8407 rtx pat = maybe_gen_insn (icode, nops, ops);
8408 if (pat)
8410 emit_insn (pat);
8411 return true;
8413 return false;
8416 /* Like maybe_expand_insn, but for jumps. */
8418 bool
8419 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8420 struct expand_operand *ops)
8422 rtx pat = maybe_gen_insn (icode, nops, ops);
8423 if (pat)
8425 emit_jump_insn (pat);
8426 return true;
8428 return false;
8431 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8432 as its operands. */
8434 void
8435 expand_insn (enum insn_code icode, unsigned int nops,
8436 struct expand_operand *ops)
8438 if (!maybe_expand_insn (icode, nops, ops))
8439 gcc_unreachable ();
8442 /* Like expand_insn, but for jumps. */
8444 void
8445 expand_jump_insn (enum insn_code icode, unsigned int nops,
8446 struct expand_operand *ops)
8448 if (!maybe_expand_jump_insn (icode, nops, ops))
8449 gcc_unreachable ();
8452 /* Reduce conditional compilation elsewhere. */
8453 #ifndef HAVE_insv
8454 #define HAVE_insv 0
8455 #define CODE_FOR_insv CODE_FOR_nothing
8456 #endif
8457 #ifndef HAVE_extv
8458 #define HAVE_extv 0
8459 #define CODE_FOR_extv CODE_FOR_nothing
8460 #endif
8461 #ifndef HAVE_extzv
8462 #define HAVE_extzv 0
8463 #define CODE_FOR_extzv CODE_FOR_nothing
8464 #endif
8466 /* Enumerates the possible types of structure operand to an
8467 extraction_insn. */
8468 enum extraction_type { ET_unaligned_mem, ET_reg };
8470 /* Check whether insv, extv or extzv pattern ICODE can be used for an
8471 insertion or extraction of type TYPE on a structure of mode MODE.
8472 Return true if so and fill in *INSN accordingly. STRUCT_OP is the
8473 operand number of the structure (the first sign_extract or zero_extract
8474 operand) and FIELD_OP is the operand number of the field (the other
8475 side of the set from the sign_extract or zero_extract). */
8477 static bool
8478 get_traditional_extraction_insn (extraction_insn *insn,
8479 enum extraction_type type,
8480 machine_mode mode,
8481 enum insn_code icode,
8482 int struct_op, int field_op)
8484 const struct insn_data_d *data = &insn_data[icode];
8486 machine_mode struct_mode = data->operand[struct_op].mode;
8487 if (struct_mode == VOIDmode)
8488 struct_mode = word_mode;
8489 if (mode != struct_mode)
8490 return false;
8492 machine_mode field_mode = data->operand[field_op].mode;
8493 if (field_mode == VOIDmode)
8494 field_mode = word_mode;
8496 machine_mode pos_mode = data->operand[struct_op + 2].mode;
8497 if (pos_mode == VOIDmode)
8498 pos_mode = word_mode;
8500 insn->icode = icode;
8501 insn->field_mode = field_mode;
8502 insn->struct_mode = (type == ET_unaligned_mem ? byte_mode : struct_mode);
8503 insn->pos_mode = pos_mode;
8504 return true;
8507 /* Return true if an optab exists to perform an insertion or extraction
8508 of type TYPE in mode MODE. Describe the instruction in *INSN if so.
8510 REG_OPTAB is the optab to use for register structures and
8511 MISALIGN_OPTAB is the optab to use for misaligned memory structures.
8512 POS_OP is the operand number of the bit position. */
8514 static bool
8515 get_optab_extraction_insn (struct extraction_insn *insn,
8516 enum extraction_type type,
8517 machine_mode mode, direct_optab reg_optab,
8518 direct_optab misalign_optab, int pos_op)
8520 direct_optab optab = (type == ET_unaligned_mem ? misalign_optab : reg_optab);
8521 enum insn_code icode = direct_optab_handler (optab, mode);
8522 if (icode == CODE_FOR_nothing)
8523 return false;
8525 const struct insn_data_d *data = &insn_data[icode];
8527 insn->icode = icode;
8528 insn->field_mode = mode;
8529 insn->struct_mode = (type == ET_unaligned_mem ? BLKmode : mode);
8530 insn->pos_mode = data->operand[pos_op].mode;
8531 if (insn->pos_mode == VOIDmode)
8532 insn->pos_mode = word_mode;
8533 return true;
8536 /* Return true if an instruction exists to perform an insertion or
8537 extraction (PATTERN says which) of type TYPE in mode MODE.
8538 Describe the instruction in *INSN if so. */
8540 static bool
8541 get_extraction_insn (extraction_insn *insn,
8542 enum extraction_pattern pattern,
8543 enum extraction_type type,
8544 machine_mode mode)
8546 switch (pattern)
8548 case EP_insv:
8549 if (HAVE_insv
8550 && get_traditional_extraction_insn (insn, type, mode,
8551 CODE_FOR_insv, 0, 3))
8552 return true;
8553 return get_optab_extraction_insn (insn, type, mode, insv_optab,
8554 insvmisalign_optab, 2);
8556 case EP_extv:
8557 if (HAVE_extv
8558 && get_traditional_extraction_insn (insn, type, mode,
8559 CODE_FOR_extv, 1, 0))
8560 return true;
8561 return get_optab_extraction_insn (insn, type, mode, extv_optab,
8562 extvmisalign_optab, 3);
8564 case EP_extzv:
8565 if (HAVE_extzv
8566 && get_traditional_extraction_insn (insn, type, mode,
8567 CODE_FOR_extzv, 1, 0))
8568 return true;
8569 return get_optab_extraction_insn (insn, type, mode, extzv_optab,
8570 extzvmisalign_optab, 3);
8572 default:
8573 gcc_unreachable ();
8577 /* Return true if an instruction exists to access a field of mode
8578 FIELDMODE in a structure that has STRUCT_BITS significant bits.
8579 Describe the "best" such instruction in *INSN if so. PATTERN and
8580 TYPE describe the type of insertion or extraction we want to perform.
8582 For an insertion, the number of significant structure bits includes
8583 all bits of the target. For an extraction, it need only include the
8584 most significant bit of the field. Larger widths are acceptable
8585 in both cases. */
8587 static bool
8588 get_best_extraction_insn (extraction_insn *insn,
8589 enum extraction_pattern pattern,
8590 enum extraction_type type,
8591 unsigned HOST_WIDE_INT struct_bits,
8592 machine_mode field_mode)
8594 machine_mode mode = smallest_mode_for_size (struct_bits, MODE_INT);
8595 while (mode != VOIDmode)
8597 if (get_extraction_insn (insn, pattern, type, mode))
8599 while (mode != VOIDmode
8600 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (field_mode)
8601 && !TRULY_NOOP_TRUNCATION_MODES_P (insn->field_mode,
8602 field_mode))
8604 get_extraction_insn (insn, pattern, type, mode);
8605 mode = GET_MODE_WIDER_MODE (mode);
8607 return true;
8609 mode = GET_MODE_WIDER_MODE (mode);
8611 return false;
8614 /* Return true if an instruction exists to access a field of mode
8615 FIELDMODE in a register structure that has STRUCT_BITS significant bits.
8616 Describe the "best" such instruction in *INSN if so. PATTERN describes
8617 the type of insertion or extraction we want to perform.
8619 For an insertion, the number of significant structure bits includes
8620 all bits of the target. For an extraction, it need only include the
8621 most significant bit of the field. Larger widths are acceptable
8622 in both cases. */
8624 bool
8625 get_best_reg_extraction_insn (extraction_insn *insn,
8626 enum extraction_pattern pattern,
8627 unsigned HOST_WIDE_INT struct_bits,
8628 machine_mode field_mode)
8630 return get_best_extraction_insn (insn, pattern, ET_reg, struct_bits,
8631 field_mode);
8634 /* Return true if an instruction exists to access a field of BITSIZE
8635 bits starting BITNUM bits into a memory structure. Describe the
8636 "best" such instruction in *INSN if so. PATTERN describes the type
8637 of insertion or extraction we want to perform and FIELDMODE is the
8638 natural mode of the extracted field.
8640 The instructions considered here only access bytes that overlap
8641 the bitfield; they do not touch any surrounding bytes. */
8643 bool
8644 get_best_mem_extraction_insn (extraction_insn *insn,
8645 enum extraction_pattern pattern,
8646 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitnum,
8647 machine_mode field_mode)
8649 unsigned HOST_WIDE_INT struct_bits = (bitnum % BITS_PER_UNIT
8650 + bitsize
8651 + BITS_PER_UNIT - 1);
8652 struct_bits -= struct_bits % BITS_PER_UNIT;
8653 return get_best_extraction_insn (insn, pattern, ET_unaligned_mem,
8654 struct_bits, field_mode);
8657 /* Determine whether "1 << x" is relatively cheap in word_mode. */
8659 bool
8660 lshift_cheap_p (bool speed_p)
8662 /* FIXME: This should be made target dependent via this "this_target"
8663 mechanism, similar to e.g. can_copy_init_p in gcse.c. */
8664 static bool init[2] = { false, false };
8665 static bool cheap[2] = { true, true };
8667 /* If the targer has no lshift in word_mode, the operation will most
8668 probably not be cheap. ??? Does GCC even work for such targets? */
8669 if (optab_handler (ashl_optab, word_mode) == CODE_FOR_nothing)
8670 return false;
8672 if (!init[speed_p])
8674 rtx reg = gen_raw_REG (word_mode, 10000);
8675 int cost = set_src_cost (gen_rtx_ASHIFT (word_mode, const1_rtx, reg),
8676 speed_p);
8677 cheap[speed_p] = cost < COSTS_N_INSNS (3);
8678 init[speed_p] = true;
8681 return cheap[speed_p];
8684 #include "gt-optabs.h"