* tree.h (TYPE_OVERFLOW_SANITIZED): Define.
[official-gcc.git] / gcc / optabs.c
blob6278d7daad1457503cdfe6b0404543258a4d7f13
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_RSHIFT_EXPR:
524 return vec_shr_optab;
526 case VEC_WIDEN_MULT_HI_EXPR:
527 return TYPE_UNSIGNED (type) ?
528 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
530 case VEC_WIDEN_MULT_LO_EXPR:
531 return TYPE_UNSIGNED (type) ?
532 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
534 case VEC_WIDEN_MULT_EVEN_EXPR:
535 return TYPE_UNSIGNED (type) ?
536 vec_widen_umult_even_optab : vec_widen_smult_even_optab;
538 case VEC_WIDEN_MULT_ODD_EXPR:
539 return TYPE_UNSIGNED (type) ?
540 vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
542 case VEC_WIDEN_LSHIFT_HI_EXPR:
543 return TYPE_UNSIGNED (type) ?
544 vec_widen_ushiftl_hi_optab : vec_widen_sshiftl_hi_optab;
546 case VEC_WIDEN_LSHIFT_LO_EXPR:
547 return TYPE_UNSIGNED (type) ?
548 vec_widen_ushiftl_lo_optab : vec_widen_sshiftl_lo_optab;
550 case VEC_UNPACK_HI_EXPR:
551 return TYPE_UNSIGNED (type) ?
552 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
554 case VEC_UNPACK_LO_EXPR:
555 return TYPE_UNSIGNED (type) ?
556 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
558 case VEC_UNPACK_FLOAT_HI_EXPR:
559 /* The signedness is determined from input operand. */
560 return TYPE_UNSIGNED (type) ?
561 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
563 case VEC_UNPACK_FLOAT_LO_EXPR:
564 /* The signedness is determined from input operand. */
565 return TYPE_UNSIGNED (type) ?
566 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
568 case VEC_PACK_TRUNC_EXPR:
569 return vec_pack_trunc_optab;
571 case VEC_PACK_SAT_EXPR:
572 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
574 case VEC_PACK_FIX_TRUNC_EXPR:
575 /* The signedness is determined from output operand. */
576 return TYPE_UNSIGNED (type) ?
577 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
579 default:
580 break;
583 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
584 switch (code)
586 case POINTER_PLUS_EXPR:
587 case PLUS_EXPR:
588 if (TYPE_SATURATING (type))
589 return TYPE_UNSIGNED (type) ? usadd_optab : ssadd_optab;
590 return trapv ? addv_optab : add_optab;
592 case MINUS_EXPR:
593 if (TYPE_SATURATING (type))
594 return TYPE_UNSIGNED (type) ? ussub_optab : sssub_optab;
595 return trapv ? subv_optab : sub_optab;
597 case MULT_EXPR:
598 if (TYPE_SATURATING (type))
599 return TYPE_UNSIGNED (type) ? usmul_optab : ssmul_optab;
600 return trapv ? smulv_optab : smul_optab;
602 case NEGATE_EXPR:
603 if (TYPE_SATURATING (type))
604 return TYPE_UNSIGNED (type) ? usneg_optab : ssneg_optab;
605 return trapv ? negv_optab : neg_optab;
607 case ABS_EXPR:
608 return trapv ? absv_optab : abs_optab;
610 default:
611 return unknown_optab;
615 /* Given optab UNOPTAB that reduces a vector to a scalar, find instead the old
616 optab that produces a vector with the reduction result in one element,
617 for a tree with type TYPE. */
619 optab
620 scalar_reduc_to_vector (optab unoptab, const_tree type)
622 switch (unoptab)
624 case reduc_plus_scal_optab:
625 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
627 case reduc_smin_scal_optab: return reduc_smin_optab;
628 case reduc_umin_scal_optab: return reduc_umin_optab;
629 case reduc_smax_scal_optab: return reduc_smax_optab;
630 case reduc_umax_scal_optab: return reduc_umax_optab;
631 default: return unknown_optab;
635 /* Expand vector widening operations.
637 There are two different classes of operations handled here:
638 1) Operations whose result is wider than all the arguments to the operation.
639 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
640 In this case OP0 and optionally OP1 would be initialized,
641 but WIDE_OP wouldn't (not relevant for this case).
642 2) Operations whose result is of the same size as the last argument to the
643 operation, but wider than all the other arguments to the operation.
644 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
645 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
647 E.g, when called to expand the following operations, this is how
648 the arguments will be initialized:
649 nops OP0 OP1 WIDE_OP
650 widening-sum 2 oprnd0 - oprnd1
651 widening-dot-product 3 oprnd0 oprnd1 oprnd2
652 widening-mult 2 oprnd0 oprnd1 -
653 type-promotion (vec-unpack) 1 oprnd0 - - */
656 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
657 rtx target, int unsignedp)
659 struct expand_operand eops[4];
660 tree oprnd0, oprnd1, oprnd2;
661 machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
662 optab widen_pattern_optab;
663 enum insn_code icode;
664 int nops = TREE_CODE_LENGTH (ops->code);
665 int op;
667 oprnd0 = ops->op0;
668 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
669 widen_pattern_optab =
670 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
671 if (ops->code == WIDEN_MULT_PLUS_EXPR
672 || ops->code == WIDEN_MULT_MINUS_EXPR)
673 icode = find_widening_optab_handler (widen_pattern_optab,
674 TYPE_MODE (TREE_TYPE (ops->op2)),
675 tmode0, 0);
676 else
677 icode = optab_handler (widen_pattern_optab, tmode0);
678 gcc_assert (icode != CODE_FOR_nothing);
680 if (nops >= 2)
682 oprnd1 = ops->op1;
683 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
686 /* The last operand is of a wider mode than the rest of the operands. */
687 if (nops == 2)
688 wmode = tmode1;
689 else if (nops == 3)
691 gcc_assert (tmode1 == tmode0);
692 gcc_assert (op1);
693 oprnd2 = ops->op2;
694 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
697 op = 0;
698 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
699 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
700 if (op1)
701 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
702 if (wide_op)
703 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
704 expand_insn (icode, op, eops);
705 return eops[0].value;
708 /* Generate code to perform an operation specified by TERNARY_OPTAB
709 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
711 UNSIGNEDP is for the case where we have to widen the operands
712 to perform the operation. It says to use zero-extension.
714 If TARGET is nonzero, the value
715 is generated there, if it is convenient to do so.
716 In all cases an rtx is returned for the locus of the value;
717 this may or may not be TARGET. */
720 expand_ternary_op (machine_mode mode, optab ternary_optab, rtx op0,
721 rtx op1, rtx op2, rtx target, int unsignedp)
723 struct expand_operand ops[4];
724 enum insn_code icode = optab_handler (ternary_optab, mode);
726 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
728 create_output_operand (&ops[0], target, mode);
729 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
730 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
731 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
732 expand_insn (icode, 4, ops);
733 return ops[0].value;
737 /* Like expand_binop, but return a constant rtx if the result can be
738 calculated at compile time. The arguments and return value are
739 otherwise the same as for expand_binop. */
742 simplify_expand_binop (machine_mode mode, optab binoptab,
743 rtx op0, rtx op1, rtx target, int unsignedp,
744 enum optab_methods methods)
746 if (CONSTANT_P (op0) && CONSTANT_P (op1))
748 rtx x = simplify_binary_operation (optab_to_code (binoptab),
749 mode, op0, op1);
750 if (x)
751 return x;
754 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
757 /* Like simplify_expand_binop, but always put the result in TARGET.
758 Return true if the expansion succeeded. */
760 bool
761 force_expand_binop (machine_mode mode, optab binoptab,
762 rtx op0, rtx op1, rtx target, int unsignedp,
763 enum optab_methods methods)
765 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
766 target, unsignedp, methods);
767 if (x == 0)
768 return false;
769 if (x != target)
770 emit_move_insn (target, x);
771 return true;
774 /* Generate insns for VEC_RSHIFT_EXPR. */
777 expand_vec_shift_expr (sepops ops, rtx target)
779 struct expand_operand eops[3];
780 enum insn_code icode;
781 rtx rtx_op1, rtx_op2;
782 machine_mode mode = TYPE_MODE (ops->type);
783 tree vec_oprnd = ops->op0;
784 tree shift_oprnd = ops->op1;
786 gcc_assert (ops->code == VEC_RSHIFT_EXPR);
788 icode = optab_handler (vec_shr_optab, mode);
789 gcc_assert (icode != CODE_FOR_nothing);
791 rtx_op1 = expand_normal (vec_oprnd);
792 rtx_op2 = expand_normal (shift_oprnd);
794 create_output_operand (&eops[0], target, mode);
795 create_input_operand (&eops[1], rtx_op1, GET_MODE (rtx_op1));
796 create_convert_operand_from_type (&eops[2], rtx_op2, TREE_TYPE (shift_oprnd));
797 expand_insn (icode, 3, eops);
799 return eops[0].value;
802 /* Create a new vector value in VMODE with all elements set to OP. The
803 mode of OP must be the element mode of VMODE. If OP is a constant,
804 then the return value will be a constant. */
806 static rtx
807 expand_vector_broadcast (machine_mode vmode, rtx op)
809 enum insn_code icode;
810 rtvec vec;
811 rtx ret;
812 int i, n;
814 gcc_checking_assert (VECTOR_MODE_P (vmode));
816 n = GET_MODE_NUNITS (vmode);
817 vec = rtvec_alloc (n);
818 for (i = 0; i < n; ++i)
819 RTVEC_ELT (vec, i) = op;
821 if (CONSTANT_P (op))
822 return gen_rtx_CONST_VECTOR (vmode, vec);
824 /* ??? If the target doesn't have a vec_init, then we have no easy way
825 of performing this operation. Most of this sort of generic support
826 is hidden away in the vector lowering support in gimple. */
827 icode = optab_handler (vec_init_optab, vmode);
828 if (icode == CODE_FOR_nothing)
829 return NULL;
831 ret = gen_reg_rtx (vmode);
832 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
834 return ret;
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, except that SUPERWORD_OP1
840 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
841 INTO_TARGET may be null if the caller has decided to calculate it. */
843 static bool
844 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
845 rtx outof_target, rtx into_target,
846 int unsignedp, enum optab_methods methods)
848 if (into_target != 0)
849 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
850 into_target, unsignedp, methods))
851 return false;
853 if (outof_target != 0)
855 /* For a signed right shift, we must fill OUTOF_TARGET with copies
856 of the sign bit, otherwise we must fill it with zeros. */
857 if (binoptab != ashr_optab)
858 emit_move_insn (outof_target, CONST0_RTX (word_mode));
859 else
860 if (!force_expand_binop (word_mode, binoptab,
861 outof_input, GEN_INT (BITS_PER_WORD - 1),
862 outof_target, unsignedp, methods))
863 return false;
865 return true;
868 /* This subroutine of expand_doubleword_shift handles the cases in which
869 the effective shift value is < BITS_PER_WORD. The arguments and return
870 value are the same as for the parent routine. */
872 static bool
873 expand_subword_shift (machine_mode op1_mode, optab binoptab,
874 rtx outof_input, rtx into_input, rtx op1,
875 rtx outof_target, rtx into_target,
876 int unsignedp, enum optab_methods methods,
877 unsigned HOST_WIDE_INT shift_mask)
879 optab reverse_unsigned_shift, unsigned_shift;
880 rtx tmp, carries;
882 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
883 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
885 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
886 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
887 the opposite direction to BINOPTAB. */
888 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
890 carries = outof_input;
891 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
892 op1_mode), op1_mode);
893 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
894 0, true, methods);
896 else
898 /* We must avoid shifting by BITS_PER_WORD bits since that is either
899 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
900 has unknown behavior. Do a single shift first, then shift by the
901 remainder. It's OK to use ~OP1 as the remainder if shift counts
902 are truncated to the mode size. */
903 carries = expand_binop (word_mode, reverse_unsigned_shift,
904 outof_input, const1_rtx, 0, unsignedp, methods);
905 if (shift_mask == BITS_PER_WORD - 1)
907 tmp = immed_wide_int_const
908 (wi::minus_one (GET_MODE_PRECISION (op1_mode)), op1_mode);
909 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
910 0, true, methods);
912 else
914 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
915 op1_mode), op1_mode);
916 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
917 0, true, methods);
920 if (tmp == 0 || carries == 0)
921 return false;
922 carries = expand_binop (word_mode, reverse_unsigned_shift,
923 carries, tmp, 0, unsignedp, methods);
924 if (carries == 0)
925 return false;
927 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
928 so the result can go directly into INTO_TARGET if convenient. */
929 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
930 into_target, unsignedp, methods);
931 if (tmp == 0)
932 return false;
934 /* Now OR in the bits carried over from OUTOF_INPUT. */
935 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
936 into_target, unsignedp, methods))
937 return false;
939 /* Use a standard word_mode shift for the out-of half. */
940 if (outof_target != 0)
941 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
942 outof_target, unsignedp, methods))
943 return false;
945 return true;
949 #ifdef HAVE_conditional_move
950 /* Try implementing expand_doubleword_shift using conditional moves.
951 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
952 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
953 are the shift counts to use in the former and latter case. All other
954 arguments are the same as the parent routine. */
956 static bool
957 expand_doubleword_shift_condmove (machine_mode op1_mode, optab binoptab,
958 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
959 rtx outof_input, rtx into_input,
960 rtx subword_op1, rtx superword_op1,
961 rtx outof_target, rtx into_target,
962 int unsignedp, enum optab_methods methods,
963 unsigned HOST_WIDE_INT shift_mask)
965 rtx outof_superword, into_superword;
967 /* Put the superword version of the output into OUTOF_SUPERWORD and
968 INTO_SUPERWORD. */
969 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
970 if (outof_target != 0 && subword_op1 == superword_op1)
972 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
973 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
974 into_superword = outof_target;
975 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
976 outof_superword, 0, unsignedp, methods))
977 return false;
979 else
981 into_superword = gen_reg_rtx (word_mode);
982 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
983 outof_superword, into_superword,
984 unsignedp, methods))
985 return false;
988 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
989 if (!expand_subword_shift (op1_mode, binoptab,
990 outof_input, into_input, subword_op1,
991 outof_target, into_target,
992 unsignedp, methods, shift_mask))
993 return false;
995 /* Select between them. Do the INTO half first because INTO_SUPERWORD
996 might be the current value of OUTOF_TARGET. */
997 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
998 into_target, into_superword, word_mode, false))
999 return false;
1001 if (outof_target != 0)
1002 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
1003 outof_target, outof_superword,
1004 word_mode, false))
1005 return false;
1007 return true;
1009 #endif
1011 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
1012 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
1013 input operand; the shift moves bits in the direction OUTOF_INPUT->
1014 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
1015 of the target. OP1 is the shift count and OP1_MODE is its mode.
1016 If OP1 is constant, it will have been truncated as appropriate
1017 and is known to be nonzero.
1019 If SHIFT_MASK is zero, the result of word shifts is undefined when the
1020 shift count is outside the range [0, BITS_PER_WORD). This routine must
1021 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
1023 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
1024 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
1025 fill with zeros or sign bits as appropriate.
1027 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
1028 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
1029 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
1030 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
1031 are undefined.
1033 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
1034 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
1035 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
1036 function wants to calculate it itself.
1038 Return true if the shift could be successfully synthesized. */
1040 static bool
1041 expand_doubleword_shift (machine_mode op1_mode, optab binoptab,
1042 rtx outof_input, rtx into_input, rtx op1,
1043 rtx outof_target, rtx into_target,
1044 int unsignedp, enum optab_methods methods,
1045 unsigned HOST_WIDE_INT shift_mask)
1047 rtx superword_op1, tmp, cmp1, cmp2;
1048 enum rtx_code cmp_code;
1050 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1051 fill the result with sign or zero bits as appropriate. If so, the value
1052 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1053 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1054 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1056 This isn't worthwhile for constant shifts since the optimizers will
1057 cope better with in-range shift counts. */
1058 if (shift_mask >= BITS_PER_WORD
1059 && outof_target != 0
1060 && !CONSTANT_P (op1))
1062 if (!expand_doubleword_shift (op1_mode, binoptab,
1063 outof_input, into_input, op1,
1064 0, into_target,
1065 unsignedp, methods, shift_mask))
1066 return false;
1067 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1068 outof_target, unsignedp, methods))
1069 return false;
1070 return true;
1073 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1074 is true when the effective shift value is less than BITS_PER_WORD.
1075 Set SUPERWORD_OP1 to the shift count that should be used to shift
1076 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1077 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
1078 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1080 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1081 is a subword shift count. */
1082 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1083 0, true, methods);
1084 cmp2 = CONST0_RTX (op1_mode);
1085 cmp_code = EQ;
1086 superword_op1 = op1;
1088 else
1090 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1091 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1092 0, true, methods);
1093 cmp2 = CONST0_RTX (op1_mode);
1094 cmp_code = LT;
1095 superword_op1 = cmp1;
1097 if (cmp1 == 0)
1098 return false;
1100 /* If we can compute the condition at compile time, pick the
1101 appropriate subroutine. */
1102 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1103 if (tmp != 0 && CONST_INT_P (tmp))
1105 if (tmp == const0_rtx)
1106 return expand_superword_shift (binoptab, outof_input, superword_op1,
1107 outof_target, into_target,
1108 unsignedp, methods);
1109 else
1110 return expand_subword_shift (op1_mode, binoptab,
1111 outof_input, into_input, op1,
1112 outof_target, into_target,
1113 unsignedp, methods, shift_mask);
1116 #ifdef HAVE_conditional_move
1117 /* Try using conditional moves to generate straight-line code. */
1119 rtx_insn *start = get_last_insn ();
1120 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1121 cmp_code, cmp1, cmp2,
1122 outof_input, into_input,
1123 op1, superword_op1,
1124 outof_target, into_target,
1125 unsignedp, methods, shift_mask))
1126 return true;
1127 delete_insns_since (start);
1129 #endif
1131 /* As a last resort, use branches to select the correct alternative. */
1132 rtx_code_label *subword_label = gen_label_rtx ();
1133 rtx_code_label *done_label = gen_label_rtx ();
1135 NO_DEFER_POP;
1136 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1137 0, 0, subword_label, -1);
1138 OK_DEFER_POP;
1140 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1141 outof_target, into_target,
1142 unsignedp, methods))
1143 return false;
1145 emit_jump_insn (gen_jump (done_label));
1146 emit_barrier ();
1147 emit_label (subword_label);
1149 if (!expand_subword_shift (op1_mode, binoptab,
1150 outof_input, into_input, op1,
1151 outof_target, into_target,
1152 unsignedp, methods, shift_mask))
1153 return false;
1155 emit_label (done_label);
1156 return true;
1159 /* Subroutine of expand_binop. Perform a double word multiplication of
1160 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1161 as the target's word_mode. This function return NULL_RTX if anything
1162 goes wrong, in which case it may have already emitted instructions
1163 which need to be deleted.
1165 If we want to multiply two two-word values and have normal and widening
1166 multiplies of single-word values, we can do this with three smaller
1167 multiplications.
1169 The multiplication proceeds as follows:
1170 _______________________
1171 [__op0_high_|__op0_low__]
1172 _______________________
1173 * [__op1_high_|__op1_low__]
1174 _______________________________________________
1175 _______________________
1176 (1) [__op0_low__*__op1_low__]
1177 _______________________
1178 (2a) [__op0_low__*__op1_high_]
1179 _______________________
1180 (2b) [__op0_high_*__op1_low__]
1181 _______________________
1182 (3) [__op0_high_*__op1_high_]
1185 This gives a 4-word result. Since we are only interested in the
1186 lower 2 words, partial result (3) and the upper words of (2a) and
1187 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1188 calculated using non-widening multiplication.
1190 (1), however, needs to be calculated with an unsigned widening
1191 multiplication. If this operation is not directly supported we
1192 try using a signed widening multiplication and adjust the result.
1193 This adjustment works as follows:
1195 If both operands are positive then no adjustment is needed.
1197 If the operands have different signs, for example op0_low < 0 and
1198 op1_low >= 0, the instruction treats the most significant bit of
1199 op0_low as a sign bit instead of a bit with significance
1200 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1201 with 2**BITS_PER_WORD - op0_low, and two's complements the
1202 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1203 the result.
1205 Similarly, if both operands are negative, we need to add
1206 (op0_low + op1_low) * 2**BITS_PER_WORD.
1208 We use a trick to adjust quickly. We logically shift op0_low right
1209 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1210 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1211 logical shift exists, we do an arithmetic right shift and subtract
1212 the 0 or -1. */
1214 static rtx
1215 expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
1216 bool umulp, enum optab_methods methods)
1218 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1219 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1220 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1221 rtx product, adjust, product_high, temp;
1223 rtx op0_high = operand_subword_force (op0, high, mode);
1224 rtx op0_low = operand_subword_force (op0, low, mode);
1225 rtx op1_high = operand_subword_force (op1, high, mode);
1226 rtx op1_low = operand_subword_force (op1, low, mode);
1228 /* If we're using an unsigned multiply to directly compute the product
1229 of the low-order words of the operands and perform any required
1230 adjustments of the operands, we begin by trying two more multiplications
1231 and then computing the appropriate sum.
1233 We have checked above that the required addition is provided.
1234 Full-word addition will normally always succeed, especially if
1235 it is provided at all, so we don't worry about its failure. The
1236 multiplication may well fail, however, so we do handle that. */
1238 if (!umulp)
1240 /* ??? This could be done with emit_store_flag where available. */
1241 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1242 NULL_RTX, 1, methods);
1243 if (temp)
1244 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1245 NULL_RTX, 0, OPTAB_DIRECT);
1246 else
1248 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1249 NULL_RTX, 0, methods);
1250 if (!temp)
1251 return NULL_RTX;
1252 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1253 NULL_RTX, 0, OPTAB_DIRECT);
1256 if (!op0_high)
1257 return NULL_RTX;
1260 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1261 NULL_RTX, 0, OPTAB_DIRECT);
1262 if (!adjust)
1263 return NULL_RTX;
1265 /* OP0_HIGH should now be dead. */
1267 if (!umulp)
1269 /* ??? This could be done with emit_store_flag where available. */
1270 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1271 NULL_RTX, 1, methods);
1272 if (temp)
1273 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1274 NULL_RTX, 0, OPTAB_DIRECT);
1275 else
1277 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1278 NULL_RTX, 0, methods);
1279 if (!temp)
1280 return NULL_RTX;
1281 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1282 NULL_RTX, 0, OPTAB_DIRECT);
1285 if (!op1_high)
1286 return NULL_RTX;
1289 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1290 NULL_RTX, 0, OPTAB_DIRECT);
1291 if (!temp)
1292 return NULL_RTX;
1294 /* OP1_HIGH should now be dead. */
1296 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1297 NULL_RTX, 0, OPTAB_DIRECT);
1299 if (target && !REG_P (target))
1300 target = NULL_RTX;
1302 if (umulp)
1303 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1304 target, 1, OPTAB_DIRECT);
1305 else
1306 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1307 target, 1, OPTAB_DIRECT);
1309 if (!product)
1310 return NULL_RTX;
1312 product_high = operand_subword (product, high, 1, mode);
1313 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1314 NULL_RTX, 0, OPTAB_DIRECT);
1315 emit_move_insn (product_high, adjust);
1316 return product;
1319 /* Wrapper around expand_binop which takes an rtx code to specify
1320 the operation to perform, not an optab pointer. All other
1321 arguments are the same. */
1323 expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
1324 rtx op1, rtx target, int unsignedp,
1325 enum optab_methods methods)
1327 optab binop = code_to_optab (code);
1328 gcc_assert (binop);
1330 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1333 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1334 binop. Order them according to commutative_operand_precedence and, if
1335 possible, try to put TARGET or a pseudo first. */
1336 static bool
1337 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1339 int op0_prec = commutative_operand_precedence (op0);
1340 int op1_prec = commutative_operand_precedence (op1);
1342 if (op0_prec < op1_prec)
1343 return true;
1345 if (op0_prec > op1_prec)
1346 return false;
1348 /* With equal precedence, both orders are ok, but it is better if the
1349 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1350 if (target == 0 || REG_P (target))
1351 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1352 else
1353 return rtx_equal_p (op1, target);
1356 /* Return true if BINOPTAB implements a shift operation. */
1358 static bool
1359 shift_optab_p (optab binoptab)
1361 switch (optab_to_code (binoptab))
1363 case ASHIFT:
1364 case SS_ASHIFT:
1365 case US_ASHIFT:
1366 case ASHIFTRT:
1367 case LSHIFTRT:
1368 case ROTATE:
1369 case ROTATERT:
1370 return true;
1372 default:
1373 return false;
1377 /* Return true if BINOPTAB implements a commutative binary operation. */
1379 static bool
1380 commutative_optab_p (optab binoptab)
1382 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1383 || binoptab == smul_widen_optab
1384 || binoptab == umul_widen_optab
1385 || binoptab == smul_highpart_optab
1386 || binoptab == umul_highpart_optab);
1389 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1390 optimizing, and if the operand is a constant that costs more than
1391 1 instruction, force the constant into a register and return that
1392 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1394 static rtx
1395 avoid_expensive_constant (machine_mode mode, optab binoptab,
1396 int opn, rtx x, bool unsignedp)
1398 bool speed = optimize_insn_for_speed_p ();
1400 if (mode != VOIDmode
1401 && optimize
1402 && CONSTANT_P (x)
1403 && (rtx_cost (x, optab_to_code (binoptab), opn, speed)
1404 > set_src_cost (x, speed)))
1406 if (CONST_INT_P (x))
1408 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1409 if (intval != INTVAL (x))
1410 x = GEN_INT (intval);
1412 else
1413 x = convert_modes (mode, VOIDmode, x, unsignedp);
1414 x = force_reg (mode, x);
1416 return x;
1419 /* Helper function for expand_binop: handle the case where there
1420 is an insn that directly implements the indicated operation.
1421 Returns null if this is not possible. */
1422 static rtx
1423 expand_binop_directly (machine_mode mode, optab binoptab,
1424 rtx op0, rtx op1,
1425 rtx target, int unsignedp, enum optab_methods methods,
1426 rtx_insn *last)
1428 machine_mode from_mode = widened_mode (mode, op0, op1);
1429 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1430 from_mode, 1);
1431 machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1432 machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1433 machine_mode mode0, mode1, tmp_mode;
1434 struct expand_operand ops[3];
1435 bool commutative_p;
1436 rtx pat;
1437 rtx xop0 = op0, xop1 = op1;
1438 rtx swap;
1440 /* If it is a commutative operator and the modes would match
1441 if we would swap the operands, we can save the conversions. */
1442 commutative_p = commutative_optab_p (binoptab);
1443 if (commutative_p
1444 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1445 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1447 swap = xop0;
1448 xop0 = xop1;
1449 xop1 = swap;
1452 /* If we are optimizing, force expensive constants into a register. */
1453 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1454 if (!shift_optab_p (binoptab))
1455 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1457 /* In case the insn wants input operands in modes different from
1458 those of the actual operands, convert the operands. It would
1459 seem that we don't need to convert CONST_INTs, but we do, so
1460 that they're properly zero-extended, sign-extended or truncated
1461 for their mode. */
1463 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1464 if (xmode0 != VOIDmode && xmode0 != mode0)
1466 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1467 mode0 = xmode0;
1470 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1471 if (xmode1 != VOIDmode && xmode1 != mode1)
1473 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1474 mode1 = xmode1;
1477 /* If operation is commutative,
1478 try to make the first operand a register.
1479 Even better, try to make it the same as the target.
1480 Also try to make the last operand a constant. */
1481 if (commutative_p
1482 && swap_commutative_operands_with_target (target, xop0, xop1))
1484 swap = xop1;
1485 xop1 = xop0;
1486 xop0 = swap;
1489 /* Now, if insn's predicates don't allow our operands, put them into
1490 pseudo regs. */
1492 if (binoptab == vec_pack_trunc_optab
1493 || binoptab == vec_pack_usat_optab
1494 || binoptab == vec_pack_ssat_optab
1495 || binoptab == vec_pack_ufix_trunc_optab
1496 || binoptab == vec_pack_sfix_trunc_optab)
1498 /* The mode of the result is different then the mode of the
1499 arguments. */
1500 tmp_mode = insn_data[(int) icode].operand[0].mode;
1501 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1503 delete_insns_since (last);
1504 return NULL_RTX;
1507 else
1508 tmp_mode = mode;
1510 create_output_operand (&ops[0], target, tmp_mode);
1511 create_input_operand (&ops[1], xop0, mode0);
1512 create_input_operand (&ops[2], xop1, mode1);
1513 pat = maybe_gen_insn (icode, 3, ops);
1514 if (pat)
1516 /* If PAT is composed of more than one insn, try to add an appropriate
1517 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1518 operand, call expand_binop again, this time without a target. */
1519 if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
1520 && ! add_equal_note (as_a <rtx_insn *> (pat), ops[0].value,
1521 optab_to_code (binoptab),
1522 ops[1].value, ops[2].value))
1524 delete_insns_since (last);
1525 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1526 unsignedp, methods);
1529 emit_insn (pat);
1530 return ops[0].value;
1532 delete_insns_since (last);
1533 return NULL_RTX;
1536 /* Generate code to perform an operation specified by BINOPTAB
1537 on operands OP0 and OP1, with result having machine-mode MODE.
1539 UNSIGNEDP is for the case where we have to widen the operands
1540 to perform the operation. It says to use zero-extension.
1542 If TARGET is nonzero, the value
1543 is generated there, if it is convenient to do so.
1544 In all cases an rtx is returned for the locus of the value;
1545 this may or may not be TARGET. */
1548 expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
1549 rtx target, int unsignedp, enum optab_methods methods)
1551 enum optab_methods next_methods
1552 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1553 ? OPTAB_WIDEN : methods);
1554 enum mode_class mclass;
1555 machine_mode wider_mode;
1556 rtx libfunc;
1557 rtx temp;
1558 rtx_insn *entry_last = get_last_insn ();
1559 rtx_insn *last;
1561 mclass = GET_MODE_CLASS (mode);
1563 /* If subtracting an integer constant, convert this into an addition of
1564 the negated constant. */
1566 if (binoptab == sub_optab && CONST_INT_P (op1))
1568 op1 = negate_rtx (mode, op1);
1569 binoptab = add_optab;
1572 /* Record where to delete back to if we backtrack. */
1573 last = get_last_insn ();
1575 /* If we can do it with a three-operand insn, do so. */
1577 if (methods != OPTAB_MUST_WIDEN
1578 && find_widening_optab_handler (binoptab, mode,
1579 widened_mode (mode, op0, op1), 1)
1580 != CODE_FOR_nothing)
1582 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1583 unsignedp, methods, last);
1584 if (temp)
1585 return temp;
1588 /* If we were trying to rotate, and that didn't work, try rotating
1589 the other direction before falling back to shifts and bitwise-or. */
1590 if (((binoptab == rotl_optab
1591 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1592 || (binoptab == rotr_optab
1593 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1594 && mclass == MODE_INT)
1596 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1597 rtx newop1;
1598 unsigned int bits = GET_MODE_PRECISION (mode);
1600 if (CONST_INT_P (op1))
1601 newop1 = GEN_INT (bits - INTVAL (op1));
1602 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1603 newop1 = negate_rtx (GET_MODE (op1), op1);
1604 else
1605 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1606 gen_int_mode (bits, GET_MODE (op1)), op1,
1607 NULL_RTX, unsignedp, OPTAB_DIRECT);
1609 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1610 target, unsignedp, methods, last);
1611 if (temp)
1612 return temp;
1615 /* If this is a multiply, see if we can do a widening operation that
1616 takes operands of this mode and makes a wider mode. */
1618 if (binoptab == smul_optab
1619 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1620 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1621 : smul_widen_optab),
1622 GET_MODE_2XWIDER_MODE (mode), mode)
1623 != CODE_FOR_nothing))
1625 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1626 unsignedp ? umul_widen_optab : smul_widen_optab,
1627 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1629 if (temp != 0)
1631 if (GET_MODE_CLASS (mode) == MODE_INT
1632 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1633 return gen_lowpart (mode, temp);
1634 else
1635 return convert_to_mode (mode, temp, unsignedp);
1639 /* If this is a vector shift by a scalar, see if we can do a vector
1640 shift by a vector. If so, broadcast the scalar into a vector. */
1641 if (mclass == MODE_VECTOR_INT)
1643 optab otheroptab = unknown_optab;
1645 if (binoptab == ashl_optab)
1646 otheroptab = vashl_optab;
1647 else if (binoptab == ashr_optab)
1648 otheroptab = vashr_optab;
1649 else if (binoptab == lshr_optab)
1650 otheroptab = vlshr_optab;
1651 else if (binoptab == rotl_optab)
1652 otheroptab = vrotl_optab;
1653 else if (binoptab == rotr_optab)
1654 otheroptab = vrotr_optab;
1656 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1658 rtx vop1 = expand_vector_broadcast (mode, op1);
1659 if (vop1)
1661 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1662 target, unsignedp, methods, last);
1663 if (temp)
1664 return temp;
1669 /* Look for a wider mode of the same class for which we think we
1670 can open-code the operation. Check for a widening multiply at the
1671 wider mode as well. */
1673 if (CLASS_HAS_WIDER_MODES_P (mclass)
1674 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1675 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1676 wider_mode != VOIDmode;
1677 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1679 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1680 || (binoptab == smul_optab
1681 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1682 && (find_widening_optab_handler ((unsignedp
1683 ? umul_widen_optab
1684 : smul_widen_optab),
1685 GET_MODE_WIDER_MODE (wider_mode),
1686 mode, 0)
1687 != CODE_FOR_nothing)))
1689 rtx xop0 = op0, xop1 = op1;
1690 int no_extend = 0;
1692 /* For certain integer operations, we need not actually extend
1693 the narrow operands, as long as we will truncate
1694 the results to the same narrowness. */
1696 if ((binoptab == ior_optab || binoptab == and_optab
1697 || binoptab == xor_optab
1698 || binoptab == add_optab || binoptab == sub_optab
1699 || binoptab == smul_optab || binoptab == ashl_optab)
1700 && mclass == MODE_INT)
1702 no_extend = 1;
1703 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1704 xop0, unsignedp);
1705 if (binoptab != ashl_optab)
1706 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1707 xop1, unsignedp);
1710 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1712 /* The second operand of a shift must always be extended. */
1713 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1714 no_extend && binoptab != ashl_optab);
1716 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1717 unsignedp, OPTAB_DIRECT);
1718 if (temp)
1720 if (mclass != MODE_INT
1721 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1723 if (target == 0)
1724 target = gen_reg_rtx (mode);
1725 convert_move (target, temp, 0);
1726 return target;
1728 else
1729 return gen_lowpart (mode, temp);
1731 else
1732 delete_insns_since (last);
1736 /* If operation is commutative,
1737 try to make the first operand a register.
1738 Even better, try to make it the same as the target.
1739 Also try to make the last operand a constant. */
1740 if (commutative_optab_p (binoptab)
1741 && swap_commutative_operands_with_target (target, op0, op1))
1743 temp = op1;
1744 op1 = op0;
1745 op0 = temp;
1748 /* These can be done a word at a time. */
1749 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1750 && mclass == MODE_INT
1751 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1752 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1754 int i;
1755 rtx_insn *insns;
1757 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1758 won't be accurate, so use a new target. */
1759 if (target == 0
1760 || target == op0
1761 || target == op1
1762 || !valid_multiword_target_p (target))
1763 target = gen_reg_rtx (mode);
1765 start_sequence ();
1767 /* Do the actual arithmetic. */
1768 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1770 rtx target_piece = operand_subword (target, i, 1, mode);
1771 rtx x = expand_binop (word_mode, binoptab,
1772 operand_subword_force (op0, i, mode),
1773 operand_subword_force (op1, i, mode),
1774 target_piece, unsignedp, next_methods);
1776 if (x == 0)
1777 break;
1779 if (target_piece != x)
1780 emit_move_insn (target_piece, x);
1783 insns = get_insns ();
1784 end_sequence ();
1786 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1788 emit_insn (insns);
1789 return target;
1793 /* Synthesize double word shifts from single word shifts. */
1794 if ((binoptab == lshr_optab || binoptab == ashl_optab
1795 || binoptab == ashr_optab)
1796 && mclass == MODE_INT
1797 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1798 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1799 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1800 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1801 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1802 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1804 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1805 machine_mode op1_mode;
1807 double_shift_mask = targetm.shift_truncation_mask (mode);
1808 shift_mask = targetm.shift_truncation_mask (word_mode);
1809 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1811 /* Apply the truncation to constant shifts. */
1812 if (double_shift_mask > 0 && CONST_INT_P (op1))
1813 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1815 if (op1 == CONST0_RTX (op1_mode))
1816 return op0;
1818 /* Make sure that this is a combination that expand_doubleword_shift
1819 can handle. See the comments there for details. */
1820 if (double_shift_mask == 0
1821 || (shift_mask == BITS_PER_WORD - 1
1822 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1824 rtx_insn *insns;
1825 rtx into_target, outof_target;
1826 rtx into_input, outof_input;
1827 int left_shift, outof_word;
1829 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1830 won't be accurate, so use a new target. */
1831 if (target == 0
1832 || target == op0
1833 || target == op1
1834 || !valid_multiword_target_p (target))
1835 target = gen_reg_rtx (mode);
1837 start_sequence ();
1839 /* OUTOF_* is the word we are shifting bits away from, and
1840 INTO_* is the word that we are shifting bits towards, thus
1841 they differ depending on the direction of the shift and
1842 WORDS_BIG_ENDIAN. */
1844 left_shift = binoptab == ashl_optab;
1845 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1847 outof_target = operand_subword (target, outof_word, 1, mode);
1848 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1850 outof_input = operand_subword_force (op0, outof_word, mode);
1851 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1853 if (expand_doubleword_shift (op1_mode, binoptab,
1854 outof_input, into_input, op1,
1855 outof_target, into_target,
1856 unsignedp, next_methods, shift_mask))
1858 insns = get_insns ();
1859 end_sequence ();
1861 emit_insn (insns);
1862 return target;
1864 end_sequence ();
1868 /* Synthesize double word rotates from single word shifts. */
1869 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1870 && mclass == MODE_INT
1871 && CONST_INT_P (op1)
1872 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1873 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1874 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1876 rtx_insn *insns;
1877 rtx into_target, outof_target;
1878 rtx into_input, outof_input;
1879 rtx inter;
1880 int shift_count, left_shift, outof_word;
1882 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1883 won't be accurate, so use a new target. Do this also if target is not
1884 a REG, first because having a register instead may open optimization
1885 opportunities, and second because if target and op0 happen to be MEMs
1886 designating the same location, we would risk clobbering it too early
1887 in the code sequence we generate below. */
1888 if (target == 0
1889 || target == op0
1890 || target == op1
1891 || !REG_P (target)
1892 || !valid_multiword_target_p (target))
1893 target = gen_reg_rtx (mode);
1895 start_sequence ();
1897 shift_count = INTVAL (op1);
1899 /* OUTOF_* is the word we are shifting bits away from, and
1900 INTO_* is the word that we are shifting bits towards, thus
1901 they differ depending on the direction of the shift and
1902 WORDS_BIG_ENDIAN. */
1904 left_shift = (binoptab == rotl_optab);
1905 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1907 outof_target = operand_subword (target, outof_word, 1, mode);
1908 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1910 outof_input = operand_subword_force (op0, outof_word, mode);
1911 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1913 if (shift_count == BITS_PER_WORD)
1915 /* This is just a word swap. */
1916 emit_move_insn (outof_target, into_input);
1917 emit_move_insn (into_target, outof_input);
1918 inter = const0_rtx;
1920 else
1922 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1923 rtx first_shift_count, second_shift_count;
1924 optab reverse_unsigned_shift, unsigned_shift;
1926 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1927 ? lshr_optab : ashl_optab);
1929 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1930 ? ashl_optab : lshr_optab);
1932 if (shift_count > BITS_PER_WORD)
1934 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1935 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1937 else
1939 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1940 second_shift_count = GEN_INT (shift_count);
1943 into_temp1 = expand_binop (word_mode, unsigned_shift,
1944 outof_input, first_shift_count,
1945 NULL_RTX, unsignedp, next_methods);
1946 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1947 into_input, second_shift_count,
1948 NULL_RTX, unsignedp, next_methods);
1950 if (into_temp1 != 0 && into_temp2 != 0)
1951 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1952 into_target, unsignedp, next_methods);
1953 else
1954 inter = 0;
1956 if (inter != 0 && inter != into_target)
1957 emit_move_insn (into_target, inter);
1959 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1960 into_input, first_shift_count,
1961 NULL_RTX, unsignedp, next_methods);
1962 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1963 outof_input, second_shift_count,
1964 NULL_RTX, unsignedp, next_methods);
1966 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1967 inter = expand_binop (word_mode, ior_optab,
1968 outof_temp1, outof_temp2,
1969 outof_target, unsignedp, next_methods);
1971 if (inter != 0 && inter != outof_target)
1972 emit_move_insn (outof_target, inter);
1975 insns = get_insns ();
1976 end_sequence ();
1978 if (inter != 0)
1980 emit_insn (insns);
1981 return target;
1985 /* These can be done a word at a time by propagating carries. */
1986 if ((binoptab == add_optab || binoptab == sub_optab)
1987 && mclass == MODE_INT
1988 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1989 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1991 unsigned int i;
1992 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1993 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1994 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1995 rtx xop0, xop1, xtarget;
1997 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1998 value is one of those, use it. Otherwise, use 1 since it is the
1999 one easiest to get. */
2000 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
2001 int normalizep = STORE_FLAG_VALUE;
2002 #else
2003 int normalizep = 1;
2004 #endif
2006 /* Prepare the operands. */
2007 xop0 = force_reg (mode, op0);
2008 xop1 = force_reg (mode, op1);
2010 xtarget = gen_reg_rtx (mode);
2012 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
2013 target = xtarget;
2015 /* Indicate for flow that the entire target reg is being set. */
2016 if (REG_P (target))
2017 emit_clobber (xtarget);
2019 /* Do the actual arithmetic. */
2020 for (i = 0; i < nwords; i++)
2022 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
2023 rtx target_piece = operand_subword (xtarget, index, 1, mode);
2024 rtx op0_piece = operand_subword_force (xop0, index, mode);
2025 rtx op1_piece = operand_subword_force (xop1, index, mode);
2026 rtx x;
2028 /* Main add/subtract of the input operands. */
2029 x = expand_binop (word_mode, binoptab,
2030 op0_piece, op1_piece,
2031 target_piece, unsignedp, next_methods);
2032 if (x == 0)
2033 break;
2035 if (i + 1 < nwords)
2037 /* Store carry from main add/subtract. */
2038 carry_out = gen_reg_rtx (word_mode);
2039 carry_out = emit_store_flag_force (carry_out,
2040 (binoptab == add_optab
2041 ? LT : GT),
2042 x, op0_piece,
2043 word_mode, 1, normalizep);
2046 if (i > 0)
2048 rtx newx;
2050 /* Add/subtract previous carry to main result. */
2051 newx = expand_binop (word_mode,
2052 normalizep == 1 ? binoptab : otheroptab,
2053 x, carry_in,
2054 NULL_RTX, 1, next_methods);
2056 if (i + 1 < nwords)
2058 /* Get out carry from adding/subtracting carry in. */
2059 rtx carry_tmp = gen_reg_rtx (word_mode);
2060 carry_tmp = emit_store_flag_force (carry_tmp,
2061 (binoptab == add_optab
2062 ? LT : GT),
2063 newx, x,
2064 word_mode, 1, normalizep);
2066 /* Logical-ior the two poss. carry together. */
2067 carry_out = expand_binop (word_mode, ior_optab,
2068 carry_out, carry_tmp,
2069 carry_out, 0, next_methods);
2070 if (carry_out == 0)
2071 break;
2073 emit_move_insn (target_piece, newx);
2075 else
2077 if (x != target_piece)
2078 emit_move_insn (target_piece, x);
2081 carry_in = carry_out;
2084 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2086 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2087 || ! rtx_equal_p (target, xtarget))
2089 rtx temp = emit_move_insn (target, xtarget);
2091 set_dst_reg_note (temp, REG_EQUAL,
2092 gen_rtx_fmt_ee (optab_to_code (binoptab),
2093 mode, copy_rtx (xop0),
2094 copy_rtx (xop1)),
2095 target);
2097 else
2098 target = xtarget;
2100 return target;
2103 else
2104 delete_insns_since (last);
2107 /* Attempt to synthesize double word multiplies using a sequence of word
2108 mode multiplications. We first attempt to generate a sequence using a
2109 more efficient unsigned widening multiply, and if that fails we then
2110 try using a signed widening multiply. */
2112 if (binoptab == smul_optab
2113 && mclass == MODE_INT
2114 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2115 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2116 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2118 rtx product = NULL_RTX;
2119 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
2120 != CODE_FOR_nothing)
2122 product = expand_doubleword_mult (mode, op0, op1, target,
2123 true, methods);
2124 if (!product)
2125 delete_insns_since (last);
2128 if (product == NULL_RTX
2129 && widening_optab_handler (smul_widen_optab, mode, word_mode)
2130 != CODE_FOR_nothing)
2132 product = expand_doubleword_mult (mode, op0, op1, target,
2133 false, methods);
2134 if (!product)
2135 delete_insns_since (last);
2138 if (product != NULL_RTX)
2140 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2142 temp = emit_move_insn (target ? target : product, product);
2143 set_dst_reg_note (temp,
2144 REG_EQUAL,
2145 gen_rtx_fmt_ee (MULT, mode,
2146 copy_rtx (op0),
2147 copy_rtx (op1)),
2148 target ? target : product);
2150 return product;
2154 /* It can't be open-coded in this mode.
2155 Use a library call if one is available and caller says that's ok. */
2157 libfunc = optab_libfunc (binoptab, mode);
2158 if (libfunc
2159 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2161 rtx_insn *insns;
2162 rtx op1x = op1;
2163 machine_mode op1_mode = mode;
2164 rtx value;
2166 start_sequence ();
2168 if (shift_optab_p (binoptab))
2170 op1_mode = targetm.libgcc_shift_count_mode ();
2171 /* Specify unsigned here,
2172 since negative shift counts are meaningless. */
2173 op1x = convert_to_mode (op1_mode, op1, 1);
2176 if (GET_MODE (op0) != VOIDmode
2177 && GET_MODE (op0) != mode)
2178 op0 = convert_to_mode (mode, op0, unsignedp);
2180 /* Pass 1 for NO_QUEUE so we don't lose any increments
2181 if the libcall is cse'd or moved. */
2182 value = emit_library_call_value (libfunc,
2183 NULL_RTX, LCT_CONST, mode, 2,
2184 op0, mode, op1x, op1_mode);
2186 insns = get_insns ();
2187 end_sequence ();
2189 target = gen_reg_rtx (mode);
2190 emit_libcall_block_1 (insns, target, value,
2191 gen_rtx_fmt_ee (optab_to_code (binoptab),
2192 mode, op0, op1),
2193 trapv_binoptab_p (binoptab));
2195 return target;
2198 delete_insns_since (last);
2200 /* It can't be done in this mode. Can we do it in a wider mode? */
2202 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2203 || methods == OPTAB_MUST_WIDEN))
2205 /* Caller says, don't even try. */
2206 delete_insns_since (entry_last);
2207 return 0;
2210 /* Compute the value of METHODS to pass to recursive calls.
2211 Don't allow widening to be tried recursively. */
2213 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2215 /* Look for a wider mode of the same class for which it appears we can do
2216 the operation. */
2218 if (CLASS_HAS_WIDER_MODES_P (mclass))
2220 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2221 wider_mode != VOIDmode;
2222 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2224 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2225 != CODE_FOR_nothing
2226 || (methods == OPTAB_LIB
2227 && optab_libfunc (binoptab, wider_mode)))
2229 rtx xop0 = op0, xop1 = op1;
2230 int no_extend = 0;
2232 /* For certain integer operations, we need not actually extend
2233 the narrow operands, as long as we will truncate
2234 the results to the same narrowness. */
2236 if ((binoptab == ior_optab || binoptab == and_optab
2237 || binoptab == xor_optab
2238 || binoptab == add_optab || binoptab == sub_optab
2239 || binoptab == smul_optab || binoptab == ashl_optab)
2240 && mclass == MODE_INT)
2241 no_extend = 1;
2243 xop0 = widen_operand (xop0, wider_mode, mode,
2244 unsignedp, no_extend);
2246 /* The second operand of a shift must always be extended. */
2247 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2248 no_extend && binoptab != ashl_optab);
2250 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2251 unsignedp, methods);
2252 if (temp)
2254 if (mclass != MODE_INT
2255 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2257 if (target == 0)
2258 target = gen_reg_rtx (mode);
2259 convert_move (target, temp, 0);
2260 return target;
2262 else
2263 return gen_lowpart (mode, temp);
2265 else
2266 delete_insns_since (last);
2271 delete_insns_since (entry_last);
2272 return 0;
2275 /* Expand a binary operator which has both signed and unsigned forms.
2276 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2277 signed operations.
2279 If we widen unsigned operands, we may use a signed wider operation instead
2280 of an unsigned wider operation, since the result would be the same. */
2283 sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
2284 rtx op0, rtx op1, rtx target, int unsignedp,
2285 enum optab_methods methods)
2287 rtx temp;
2288 optab direct_optab = unsignedp ? uoptab : soptab;
2289 bool save_enable;
2291 /* Do it without widening, if possible. */
2292 temp = expand_binop (mode, direct_optab, op0, op1, target,
2293 unsignedp, OPTAB_DIRECT);
2294 if (temp || methods == OPTAB_DIRECT)
2295 return temp;
2297 /* Try widening to a signed int. Disable any direct use of any
2298 signed insn in the current mode. */
2299 save_enable = swap_optab_enable (soptab, mode, false);
2301 temp = expand_binop (mode, soptab, op0, op1, target,
2302 unsignedp, OPTAB_WIDEN);
2304 /* For unsigned operands, try widening to an unsigned int. */
2305 if (!temp && unsignedp)
2306 temp = expand_binop (mode, uoptab, op0, op1, target,
2307 unsignedp, OPTAB_WIDEN);
2308 if (temp || methods == OPTAB_WIDEN)
2309 goto egress;
2311 /* Use the right width libcall if that exists. */
2312 temp = expand_binop (mode, direct_optab, op0, op1, target,
2313 unsignedp, OPTAB_LIB);
2314 if (temp || methods == OPTAB_LIB)
2315 goto egress;
2317 /* Must widen and use a libcall, use either signed or unsigned. */
2318 temp = expand_binop (mode, soptab, op0, op1, target,
2319 unsignedp, methods);
2320 if (!temp && unsignedp)
2321 temp = expand_binop (mode, uoptab, op0, op1, target,
2322 unsignedp, methods);
2324 egress:
2325 /* Undo the fiddling above. */
2326 if (save_enable)
2327 swap_optab_enable (soptab, mode, true);
2328 return temp;
2331 /* Generate code to perform an operation specified by UNOPPTAB
2332 on operand OP0, with two results to TARG0 and TARG1.
2333 We assume that the order of the operands for the instruction
2334 is TARG0, TARG1, OP0.
2336 Either TARG0 or TARG1 may be zero, but what that means is that
2337 the result is not actually wanted. We will generate it into
2338 a dummy pseudo-reg and discard it. They may not both be zero.
2340 Returns 1 if this operation can be performed; 0 if not. */
2343 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2344 int unsignedp)
2346 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2347 enum mode_class mclass;
2348 machine_mode wider_mode;
2349 rtx_insn *entry_last = get_last_insn ();
2350 rtx_insn *last;
2352 mclass = GET_MODE_CLASS (mode);
2354 if (!targ0)
2355 targ0 = gen_reg_rtx (mode);
2356 if (!targ1)
2357 targ1 = gen_reg_rtx (mode);
2359 /* Record where to go back to if we fail. */
2360 last = get_last_insn ();
2362 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2364 struct expand_operand ops[3];
2365 enum insn_code icode = optab_handler (unoptab, mode);
2367 create_fixed_operand (&ops[0], targ0);
2368 create_fixed_operand (&ops[1], targ1);
2369 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2370 if (maybe_expand_insn (icode, 3, ops))
2371 return 1;
2374 /* It can't be done in this mode. Can we do it in a wider mode? */
2376 if (CLASS_HAS_WIDER_MODES_P (mclass))
2378 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2379 wider_mode != VOIDmode;
2380 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2382 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2384 rtx t0 = gen_reg_rtx (wider_mode);
2385 rtx t1 = gen_reg_rtx (wider_mode);
2386 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2388 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2390 convert_move (targ0, t0, unsignedp);
2391 convert_move (targ1, t1, unsignedp);
2392 return 1;
2394 else
2395 delete_insns_since (last);
2400 delete_insns_since (entry_last);
2401 return 0;
2404 /* Generate code to perform an operation specified by BINOPTAB
2405 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2406 We assume that the order of the operands for the instruction
2407 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2408 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2410 Either TARG0 or TARG1 may be zero, but what that means is that
2411 the result is not actually wanted. We will generate it into
2412 a dummy pseudo-reg and discard it. They may not both be zero.
2414 Returns 1 if this operation can be performed; 0 if not. */
2417 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2418 int unsignedp)
2420 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2421 enum mode_class mclass;
2422 machine_mode wider_mode;
2423 rtx_insn *entry_last = get_last_insn ();
2424 rtx_insn *last;
2426 mclass = GET_MODE_CLASS (mode);
2428 if (!targ0)
2429 targ0 = gen_reg_rtx (mode);
2430 if (!targ1)
2431 targ1 = gen_reg_rtx (mode);
2433 /* Record where to go back to if we fail. */
2434 last = get_last_insn ();
2436 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2438 struct expand_operand ops[4];
2439 enum insn_code icode = optab_handler (binoptab, mode);
2440 machine_mode mode0 = insn_data[icode].operand[1].mode;
2441 machine_mode mode1 = insn_data[icode].operand[2].mode;
2442 rtx xop0 = op0, xop1 = op1;
2444 /* If we are optimizing, force expensive constants into a register. */
2445 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2446 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2448 create_fixed_operand (&ops[0], targ0);
2449 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2450 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2451 create_fixed_operand (&ops[3], targ1);
2452 if (maybe_expand_insn (icode, 4, ops))
2453 return 1;
2454 delete_insns_since (last);
2457 /* It can't be done in this mode. Can we do it in a wider mode? */
2459 if (CLASS_HAS_WIDER_MODES_P (mclass))
2461 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2462 wider_mode != VOIDmode;
2463 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2465 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2467 rtx t0 = gen_reg_rtx (wider_mode);
2468 rtx t1 = gen_reg_rtx (wider_mode);
2469 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2470 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2472 if (expand_twoval_binop (binoptab, cop0, cop1,
2473 t0, t1, unsignedp))
2475 convert_move (targ0, t0, unsignedp);
2476 convert_move (targ1, t1, unsignedp);
2477 return 1;
2479 else
2480 delete_insns_since (last);
2485 delete_insns_since (entry_last);
2486 return 0;
2489 /* Expand the two-valued library call indicated by BINOPTAB, but
2490 preserve only one of the values. If TARG0 is non-NULL, the first
2491 value is placed into TARG0; otherwise the second value is placed
2492 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2493 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2494 This routine assumes that the value returned by the library call is
2495 as if the return value was of an integral mode twice as wide as the
2496 mode of OP0. Returns 1 if the call was successful. */
2498 bool
2499 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2500 rtx targ0, rtx targ1, enum rtx_code code)
2502 machine_mode mode;
2503 machine_mode libval_mode;
2504 rtx libval;
2505 rtx_insn *insns;
2506 rtx libfunc;
2508 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2509 gcc_assert (!targ0 != !targ1);
2511 mode = GET_MODE (op0);
2512 libfunc = optab_libfunc (binoptab, mode);
2513 if (!libfunc)
2514 return false;
2516 /* The value returned by the library function will have twice as
2517 many bits as the nominal MODE. */
2518 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2519 MODE_INT);
2520 start_sequence ();
2521 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2522 libval_mode, 2,
2523 op0, mode,
2524 op1, mode);
2525 /* Get the part of VAL containing the value that we want. */
2526 libval = simplify_gen_subreg (mode, libval, libval_mode,
2527 targ0 ? 0 : GET_MODE_SIZE (mode));
2528 insns = get_insns ();
2529 end_sequence ();
2530 /* Move the into the desired location. */
2531 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2532 gen_rtx_fmt_ee (code, mode, op0, op1));
2534 return true;
2538 /* Wrapper around expand_unop which takes an rtx code to specify
2539 the operation to perform, not an optab pointer. All other
2540 arguments are the same. */
2542 expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
2543 rtx target, int unsignedp)
2545 optab unop = code_to_optab (code);
2546 gcc_assert (unop);
2548 return expand_unop (mode, unop, op0, target, unsignedp);
2551 /* Try calculating
2552 (clz:narrow x)
2554 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2556 A similar operation can be used for clrsb. UNOPTAB says which operation
2557 we are trying to expand. */
2558 static rtx
2559 widen_leading (machine_mode mode, rtx op0, rtx target, optab unoptab)
2561 enum mode_class mclass = GET_MODE_CLASS (mode);
2562 if (CLASS_HAS_WIDER_MODES_P (mclass))
2564 machine_mode wider_mode;
2565 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2566 wider_mode != VOIDmode;
2567 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2569 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2571 rtx xop0, temp;
2572 rtx_insn *last;
2574 last = get_last_insn ();
2576 if (target == 0)
2577 target = gen_reg_rtx (mode);
2578 xop0 = widen_operand (op0, wider_mode, mode,
2579 unoptab != clrsb_optab, false);
2580 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2581 unoptab != clrsb_optab);
2582 if (temp != 0)
2583 temp = expand_binop
2584 (wider_mode, sub_optab, temp,
2585 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2586 - GET_MODE_PRECISION (mode),
2587 wider_mode),
2588 target, true, OPTAB_DIRECT);
2589 if (temp == 0)
2590 delete_insns_since (last);
2592 return temp;
2596 return 0;
2599 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2600 quantities, choosing which based on whether the high word is nonzero. */
2601 static rtx
2602 expand_doubleword_clz (machine_mode mode, rtx op0, rtx target)
2604 rtx xop0 = force_reg (mode, op0);
2605 rtx subhi = gen_highpart (word_mode, xop0);
2606 rtx sublo = gen_lowpart (word_mode, xop0);
2607 rtx_code_label *hi0_label = gen_label_rtx ();
2608 rtx_code_label *after_label = gen_label_rtx ();
2609 rtx_insn *seq;
2610 rtx temp, result;
2612 /* If we were not given a target, use a word_mode register, not a
2613 'mode' register. The result will fit, and nobody is expecting
2614 anything bigger (the return type of __builtin_clz* is int). */
2615 if (!target)
2616 target = gen_reg_rtx (word_mode);
2618 /* In any case, write to a word_mode scratch in both branches of the
2619 conditional, so we can ensure there is a single move insn setting
2620 'target' to tag a REG_EQUAL note on. */
2621 result = gen_reg_rtx (word_mode);
2623 start_sequence ();
2625 /* If the high word is not equal to zero,
2626 then clz of the full value is clz of the high word. */
2627 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2628 word_mode, true, hi0_label);
2630 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2631 if (!temp)
2632 goto fail;
2634 if (temp != result)
2635 convert_move (result, temp, true);
2637 emit_jump_insn (gen_jump (after_label));
2638 emit_barrier ();
2640 /* Else clz of the full value is clz of the low word plus the number
2641 of bits in the high word. */
2642 emit_label (hi0_label);
2644 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2645 if (!temp)
2646 goto fail;
2647 temp = expand_binop (word_mode, add_optab, temp,
2648 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2649 result, true, OPTAB_DIRECT);
2650 if (!temp)
2651 goto fail;
2652 if (temp != result)
2653 convert_move (result, temp, true);
2655 emit_label (after_label);
2656 convert_move (target, result, true);
2658 seq = get_insns ();
2659 end_sequence ();
2661 add_equal_note (seq, target, CLZ, xop0, 0);
2662 emit_insn (seq);
2663 return target;
2665 fail:
2666 end_sequence ();
2667 return 0;
2670 /* Try calculating
2671 (bswap:narrow x)
2673 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2674 static rtx
2675 widen_bswap (machine_mode mode, rtx op0, rtx target)
2677 enum mode_class mclass = GET_MODE_CLASS (mode);
2678 machine_mode wider_mode;
2679 rtx x;
2680 rtx_insn *last;
2682 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2683 return NULL_RTX;
2685 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2686 wider_mode != VOIDmode;
2687 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2688 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2689 goto found;
2690 return NULL_RTX;
2692 found:
2693 last = get_last_insn ();
2695 x = widen_operand (op0, wider_mode, mode, true, true);
2696 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2698 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2699 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2700 if (x != 0)
2701 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2702 GET_MODE_BITSIZE (wider_mode)
2703 - GET_MODE_BITSIZE (mode),
2704 NULL_RTX, true);
2706 if (x != 0)
2708 if (target == 0)
2709 target = gen_reg_rtx (mode);
2710 emit_move_insn (target, gen_lowpart (mode, x));
2712 else
2713 delete_insns_since (last);
2715 return target;
2718 /* Try calculating bswap as two bswaps of two word-sized operands. */
2720 static rtx
2721 expand_doubleword_bswap (machine_mode mode, rtx op, rtx target)
2723 rtx t0, t1;
2725 t1 = expand_unop (word_mode, bswap_optab,
2726 operand_subword_force (op, 0, mode), NULL_RTX, true);
2727 t0 = expand_unop (word_mode, bswap_optab,
2728 operand_subword_force (op, 1, mode), NULL_RTX, true);
2730 if (target == 0 || !valid_multiword_target_p (target))
2731 target = gen_reg_rtx (mode);
2732 if (REG_P (target))
2733 emit_clobber (target);
2734 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2735 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2737 return target;
2740 /* Try calculating (parity x) as (and (popcount x) 1), where
2741 popcount can also be done in a wider mode. */
2742 static rtx
2743 expand_parity (machine_mode mode, rtx op0, rtx target)
2745 enum mode_class mclass = GET_MODE_CLASS (mode);
2746 if (CLASS_HAS_WIDER_MODES_P (mclass))
2748 machine_mode wider_mode;
2749 for (wider_mode = mode; wider_mode != VOIDmode;
2750 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2752 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2754 rtx xop0, temp;
2755 rtx_insn *last;
2757 last = get_last_insn ();
2759 if (target == 0)
2760 target = gen_reg_rtx (mode);
2761 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2762 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2763 true);
2764 if (temp != 0)
2765 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2766 target, true, OPTAB_DIRECT);
2767 if (temp == 0)
2768 delete_insns_since (last);
2770 return temp;
2774 return 0;
2777 /* Try calculating ctz(x) as K - clz(x & -x) ,
2778 where K is GET_MODE_PRECISION(mode) - 1.
2780 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2781 don't have to worry about what the hardware does in that case. (If
2782 the clz instruction produces the usual value at 0, which is K, the
2783 result of this code sequence will be -1; expand_ffs, below, relies
2784 on this. It might be nice to have it be K instead, for consistency
2785 with the (very few) processors that provide a ctz with a defined
2786 value, but that would take one more instruction, and it would be
2787 less convenient for expand_ffs anyway. */
2789 static rtx
2790 expand_ctz (machine_mode mode, rtx op0, rtx target)
2792 rtx_insn *seq;
2793 rtx temp;
2795 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2796 return 0;
2798 start_sequence ();
2800 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2801 if (temp)
2802 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2803 true, OPTAB_DIRECT);
2804 if (temp)
2805 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2806 if (temp)
2807 temp = expand_binop (mode, sub_optab,
2808 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2809 temp, target,
2810 true, OPTAB_DIRECT);
2811 if (temp == 0)
2813 end_sequence ();
2814 return 0;
2817 seq = get_insns ();
2818 end_sequence ();
2820 add_equal_note (seq, temp, CTZ, op0, 0);
2821 emit_insn (seq);
2822 return temp;
2826 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2827 else with the sequence used by expand_clz.
2829 The ffs builtin promises to return zero for a zero value and ctz/clz
2830 may have an undefined value in that case. If they do not give us a
2831 convenient value, we have to generate a test and branch. */
2832 static rtx
2833 expand_ffs (machine_mode mode, rtx op0, rtx target)
2835 HOST_WIDE_INT val = 0;
2836 bool defined_at_zero = false;
2837 rtx temp;
2838 rtx_insn *seq;
2840 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2842 start_sequence ();
2844 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2845 if (!temp)
2846 goto fail;
2848 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2850 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2852 start_sequence ();
2853 temp = expand_ctz (mode, op0, 0);
2854 if (!temp)
2855 goto fail;
2857 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2859 defined_at_zero = true;
2860 val = (GET_MODE_PRECISION (mode) - 1) - val;
2863 else
2864 return 0;
2866 if (defined_at_zero && val == -1)
2867 /* No correction needed at zero. */;
2868 else
2870 /* We don't try to do anything clever with the situation found
2871 on some processors (eg Alpha) where ctz(0:mode) ==
2872 bitsize(mode). If someone can think of a way to send N to -1
2873 and leave alone all values in the range 0..N-1 (where N is a
2874 power of two), cheaper than this test-and-branch, please add it.
2876 The test-and-branch is done after the operation itself, in case
2877 the operation sets condition codes that can be recycled for this.
2878 (This is true on i386, for instance.) */
2880 rtx_code_label *nonzero_label = gen_label_rtx ();
2881 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2882 mode, true, nonzero_label);
2884 convert_move (temp, GEN_INT (-1), false);
2885 emit_label (nonzero_label);
2888 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2889 to produce a value in the range 0..bitsize. */
2890 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2891 target, false, OPTAB_DIRECT);
2892 if (!temp)
2893 goto fail;
2895 seq = get_insns ();
2896 end_sequence ();
2898 add_equal_note (seq, temp, FFS, op0, 0);
2899 emit_insn (seq);
2900 return temp;
2902 fail:
2903 end_sequence ();
2904 return 0;
2907 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2908 conditions, VAL may already be a SUBREG against which we cannot generate
2909 a further SUBREG. In this case, we expect forcing the value into a
2910 register will work around the situation. */
2912 static rtx
2913 lowpart_subreg_maybe_copy (machine_mode omode, rtx val,
2914 machine_mode imode)
2916 rtx ret;
2917 ret = lowpart_subreg (omode, val, imode);
2918 if (ret == NULL)
2920 val = force_reg (imode, val);
2921 ret = lowpart_subreg (omode, val, imode);
2922 gcc_assert (ret != NULL);
2924 return ret;
2927 /* Expand a floating point absolute value or negation operation via a
2928 logical operation on the sign bit. */
2930 static rtx
2931 expand_absneg_bit (enum rtx_code code, machine_mode mode,
2932 rtx op0, rtx target)
2934 const struct real_format *fmt;
2935 int bitpos, word, nwords, i;
2936 machine_mode imode;
2937 rtx temp;
2938 rtx_insn *insns;
2940 /* The format has to have a simple sign bit. */
2941 fmt = REAL_MODE_FORMAT (mode);
2942 if (fmt == NULL)
2943 return NULL_RTX;
2945 bitpos = fmt->signbit_rw;
2946 if (bitpos < 0)
2947 return NULL_RTX;
2949 /* Don't create negative zeros if the format doesn't support them. */
2950 if (code == NEG && !fmt->has_signed_zero)
2951 return NULL_RTX;
2953 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2955 imode = int_mode_for_mode (mode);
2956 if (imode == BLKmode)
2957 return NULL_RTX;
2958 word = 0;
2959 nwords = 1;
2961 else
2963 imode = word_mode;
2965 if (FLOAT_WORDS_BIG_ENDIAN)
2966 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2967 else
2968 word = bitpos / BITS_PER_WORD;
2969 bitpos = bitpos % BITS_PER_WORD;
2970 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2973 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
2974 if (code == ABS)
2975 mask = ~mask;
2977 if (target == 0
2978 || target == op0
2979 || (nwords > 1 && !valid_multiword_target_p (target)))
2980 target = gen_reg_rtx (mode);
2982 if (nwords > 1)
2984 start_sequence ();
2986 for (i = 0; i < nwords; ++i)
2988 rtx targ_piece = operand_subword (target, i, 1, mode);
2989 rtx op0_piece = operand_subword_force (op0, i, mode);
2991 if (i == word)
2993 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2994 op0_piece,
2995 immed_wide_int_const (mask, imode),
2996 targ_piece, 1, OPTAB_LIB_WIDEN);
2997 if (temp != targ_piece)
2998 emit_move_insn (targ_piece, temp);
3000 else
3001 emit_move_insn (targ_piece, op0_piece);
3004 insns = get_insns ();
3005 end_sequence ();
3007 emit_insn (insns);
3009 else
3011 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
3012 gen_lowpart (imode, op0),
3013 immed_wide_int_const (mask, imode),
3014 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3015 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3017 set_dst_reg_note (get_last_insn (), REG_EQUAL,
3018 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
3019 target);
3022 return target;
3025 /* As expand_unop, but will fail rather than attempt the operation in a
3026 different mode or with a libcall. */
3027 static rtx
3028 expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
3029 int unsignedp)
3031 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
3033 struct expand_operand ops[2];
3034 enum insn_code icode = optab_handler (unoptab, mode);
3035 rtx_insn *last = get_last_insn ();
3036 rtx pat;
3038 create_output_operand (&ops[0], target, mode);
3039 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
3040 pat = maybe_gen_insn (icode, 2, ops);
3041 if (pat)
3043 if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
3044 && ! add_equal_note (as_a <rtx_insn *> (pat), ops[0].value,
3045 optab_to_code (unoptab),
3046 ops[1].value, NULL_RTX))
3048 delete_insns_since (last);
3049 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3052 emit_insn (pat);
3054 return ops[0].value;
3057 return 0;
3060 /* Generate code to perform an operation specified by UNOPTAB
3061 on operand OP0, with result having machine-mode MODE.
3063 UNSIGNEDP is for the case where we have to widen the operands
3064 to perform the operation. It says to use zero-extension.
3066 If TARGET is nonzero, the value
3067 is generated there, if it is convenient to do so.
3068 In all cases an rtx is returned for the locus of the value;
3069 this may or may not be TARGET. */
3072 expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
3073 int unsignedp)
3075 enum mode_class mclass = GET_MODE_CLASS (mode);
3076 machine_mode wider_mode;
3077 rtx temp;
3078 rtx libfunc;
3080 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3081 if (temp)
3082 return temp;
3084 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3086 /* Widening (or narrowing) clz needs special treatment. */
3087 if (unoptab == clz_optab)
3089 temp = widen_leading (mode, op0, target, unoptab);
3090 if (temp)
3091 return temp;
3093 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3094 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3096 temp = expand_doubleword_clz (mode, op0, target);
3097 if (temp)
3098 return temp;
3101 goto try_libcall;
3104 if (unoptab == clrsb_optab)
3106 temp = widen_leading (mode, op0, target, unoptab);
3107 if (temp)
3108 return temp;
3109 goto try_libcall;
3112 /* Widening (or narrowing) bswap needs special treatment. */
3113 if (unoptab == bswap_optab)
3115 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3116 or ROTATERT. First try these directly; if this fails, then try the
3117 obvious pair of shifts with allowed widening, as this will probably
3118 be always more efficient than the other fallback methods. */
3119 if (mode == HImode)
3121 rtx_insn *last;
3122 rtx temp1, temp2;
3124 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3126 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
3127 unsignedp, OPTAB_DIRECT);
3128 if (temp)
3129 return temp;
3132 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3134 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
3135 unsignedp, OPTAB_DIRECT);
3136 if (temp)
3137 return temp;
3140 last = get_last_insn ();
3142 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
3143 unsignedp, OPTAB_WIDEN);
3144 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
3145 unsignedp, OPTAB_WIDEN);
3146 if (temp1 && temp2)
3148 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3149 unsignedp, OPTAB_WIDEN);
3150 if (temp)
3151 return temp;
3154 delete_insns_since (last);
3157 temp = widen_bswap (mode, op0, target);
3158 if (temp)
3159 return temp;
3161 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3162 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3164 temp = expand_doubleword_bswap (mode, op0, target);
3165 if (temp)
3166 return temp;
3169 goto try_libcall;
3172 if (CLASS_HAS_WIDER_MODES_P (mclass))
3173 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3174 wider_mode != VOIDmode;
3175 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3177 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3179 rtx xop0 = op0;
3180 rtx_insn *last = get_last_insn ();
3182 /* For certain operations, we need not actually extend
3183 the narrow operand, as long as we will truncate the
3184 results to the same narrowness. */
3186 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3187 (unoptab == neg_optab
3188 || unoptab == one_cmpl_optab)
3189 && mclass == MODE_INT);
3191 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3192 unsignedp);
3194 if (temp)
3196 if (mclass != MODE_INT
3197 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3199 if (target == 0)
3200 target = gen_reg_rtx (mode);
3201 convert_move (target, temp, 0);
3202 return target;
3204 else
3205 return gen_lowpart (mode, temp);
3207 else
3208 delete_insns_since (last);
3212 /* These can be done a word at a time. */
3213 if (unoptab == one_cmpl_optab
3214 && mclass == MODE_INT
3215 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3216 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3218 int i;
3219 rtx_insn *insns;
3221 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3222 target = gen_reg_rtx (mode);
3224 start_sequence ();
3226 /* Do the actual arithmetic. */
3227 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3229 rtx target_piece = operand_subword (target, i, 1, mode);
3230 rtx x = expand_unop (word_mode, unoptab,
3231 operand_subword_force (op0, i, mode),
3232 target_piece, unsignedp);
3234 if (target_piece != x)
3235 emit_move_insn (target_piece, x);
3238 insns = get_insns ();
3239 end_sequence ();
3241 emit_insn (insns);
3242 return target;
3245 if (optab_to_code (unoptab) == NEG)
3247 /* Try negating floating point values by flipping the sign bit. */
3248 if (SCALAR_FLOAT_MODE_P (mode))
3250 temp = expand_absneg_bit (NEG, mode, op0, target);
3251 if (temp)
3252 return temp;
3255 /* If there is no negation pattern, and we have no negative zero,
3256 try subtracting from zero. */
3257 if (!HONOR_SIGNED_ZEROS (mode))
3259 temp = expand_binop (mode, (unoptab == negv_optab
3260 ? subv_optab : sub_optab),
3261 CONST0_RTX (mode), op0, target,
3262 unsignedp, OPTAB_DIRECT);
3263 if (temp)
3264 return temp;
3268 /* Try calculating parity (x) as popcount (x) % 2. */
3269 if (unoptab == parity_optab)
3271 temp = expand_parity (mode, op0, target);
3272 if (temp)
3273 return temp;
3276 /* Try implementing ffs (x) in terms of clz (x). */
3277 if (unoptab == ffs_optab)
3279 temp = expand_ffs (mode, op0, target);
3280 if (temp)
3281 return temp;
3284 /* Try implementing ctz (x) in terms of clz (x). */
3285 if (unoptab == ctz_optab)
3287 temp = expand_ctz (mode, op0, target);
3288 if (temp)
3289 return temp;
3292 try_libcall:
3293 /* Now try a library call in this mode. */
3294 libfunc = optab_libfunc (unoptab, mode);
3295 if (libfunc)
3297 rtx_insn *insns;
3298 rtx value;
3299 rtx eq_value;
3300 machine_mode outmode = mode;
3302 /* All of these functions return small values. Thus we choose to
3303 have them return something that isn't a double-word. */
3304 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3305 || unoptab == clrsb_optab || unoptab == popcount_optab
3306 || unoptab == parity_optab)
3307 outmode
3308 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3309 optab_libfunc (unoptab, mode)));
3311 start_sequence ();
3313 /* Pass 1 for NO_QUEUE so we don't lose any increments
3314 if the libcall is cse'd or moved. */
3315 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3316 1, op0, mode);
3317 insns = get_insns ();
3318 end_sequence ();
3320 target = gen_reg_rtx (outmode);
3321 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3322 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3323 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3324 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3325 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3326 emit_libcall_block_1 (insns, target, value, eq_value,
3327 trapv_unoptab_p (unoptab));
3329 return target;
3332 /* It can't be done in this mode. Can we do it in a wider mode? */
3334 if (CLASS_HAS_WIDER_MODES_P (mclass))
3336 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3337 wider_mode != VOIDmode;
3338 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3340 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3341 || optab_libfunc (unoptab, wider_mode))
3343 rtx xop0 = op0;
3344 rtx_insn *last = get_last_insn ();
3346 /* For certain operations, we need not actually extend
3347 the narrow operand, as long as we will truncate the
3348 results to the same narrowness. */
3349 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3350 (unoptab == neg_optab
3351 || unoptab == one_cmpl_optab
3352 || unoptab == bswap_optab)
3353 && mclass == MODE_INT);
3355 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3356 unsignedp);
3358 /* If we are generating clz using wider mode, adjust the
3359 result. Similarly for clrsb. */
3360 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3361 && temp != 0)
3362 temp = expand_binop
3363 (wider_mode, sub_optab, temp,
3364 gen_int_mode (GET_MODE_PRECISION (wider_mode)
3365 - GET_MODE_PRECISION (mode),
3366 wider_mode),
3367 target, true, OPTAB_DIRECT);
3369 /* Likewise for bswap. */
3370 if (unoptab == bswap_optab && temp != 0)
3372 gcc_assert (GET_MODE_PRECISION (wider_mode)
3373 == GET_MODE_BITSIZE (wider_mode)
3374 && GET_MODE_PRECISION (mode)
3375 == GET_MODE_BITSIZE (mode));
3377 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3378 GET_MODE_BITSIZE (wider_mode)
3379 - GET_MODE_BITSIZE (mode),
3380 NULL_RTX, true);
3383 if (temp)
3385 if (mclass != MODE_INT)
3387 if (target == 0)
3388 target = gen_reg_rtx (mode);
3389 convert_move (target, temp, 0);
3390 return target;
3392 else
3393 return gen_lowpart (mode, temp);
3395 else
3396 delete_insns_since (last);
3401 /* One final attempt at implementing negation via subtraction,
3402 this time allowing widening of the operand. */
3403 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3405 rtx temp;
3406 temp = expand_binop (mode,
3407 unoptab == negv_optab ? subv_optab : sub_optab,
3408 CONST0_RTX (mode), op0,
3409 target, unsignedp, OPTAB_LIB_WIDEN);
3410 if (temp)
3411 return temp;
3414 return 0;
3417 /* Emit code to compute the absolute value of OP0, with result to
3418 TARGET if convenient. (TARGET may be 0.) The return value says
3419 where the result actually is to be found.
3421 MODE is the mode of the operand; the mode of the result is
3422 different but can be deduced from MODE.
3427 expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
3428 int result_unsignedp)
3430 rtx temp;
3432 if (GET_MODE_CLASS (mode) != MODE_INT
3433 || ! flag_trapv)
3434 result_unsignedp = 1;
3436 /* First try to do it with a special abs instruction. */
3437 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3438 op0, target, 0);
3439 if (temp != 0)
3440 return temp;
3442 /* For floating point modes, try clearing the sign bit. */
3443 if (SCALAR_FLOAT_MODE_P (mode))
3445 temp = expand_absneg_bit (ABS, mode, op0, target);
3446 if (temp)
3447 return temp;
3450 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3451 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3452 && !HONOR_SIGNED_ZEROS (mode))
3454 rtx_insn *last = get_last_insn ();
3456 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3457 op0, NULL_RTX, 0);
3458 if (temp != 0)
3459 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3460 OPTAB_WIDEN);
3462 if (temp != 0)
3463 return temp;
3465 delete_insns_since (last);
3468 /* If this machine has expensive jumps, we can do integer absolute
3469 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3470 where W is the width of MODE. */
3472 if (GET_MODE_CLASS (mode) == MODE_INT
3473 && BRANCH_COST (optimize_insn_for_speed_p (),
3474 false) >= 2)
3476 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3477 GET_MODE_PRECISION (mode) - 1,
3478 NULL_RTX, 0);
3480 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3481 OPTAB_LIB_WIDEN);
3482 if (temp != 0)
3483 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3484 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3486 if (temp != 0)
3487 return temp;
3490 return NULL_RTX;
3494 expand_abs (machine_mode mode, rtx op0, rtx target,
3495 int result_unsignedp, int safe)
3497 rtx temp;
3498 rtx_code_label *op1;
3500 if (GET_MODE_CLASS (mode) != MODE_INT
3501 || ! flag_trapv)
3502 result_unsignedp = 1;
3504 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3505 if (temp != 0)
3506 return temp;
3508 /* If that does not win, use conditional jump and negate. */
3510 /* It is safe to use the target if it is the same
3511 as the source if this is also a pseudo register */
3512 if (op0 == target && REG_P (op0)
3513 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3514 safe = 1;
3516 op1 = gen_label_rtx ();
3517 if (target == 0 || ! safe
3518 || GET_MODE (target) != mode
3519 || (MEM_P (target) && MEM_VOLATILE_P (target))
3520 || (REG_P (target)
3521 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3522 target = gen_reg_rtx (mode);
3524 emit_move_insn (target, op0);
3525 NO_DEFER_POP;
3527 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3528 NULL_RTX, NULL_RTX, op1, -1);
3530 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3531 target, target, 0);
3532 if (op0 != target)
3533 emit_move_insn (target, op0);
3534 emit_label (op1);
3535 OK_DEFER_POP;
3536 return target;
3539 /* Emit code to compute the one's complement absolute value of OP0
3540 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3541 (TARGET may be NULL_RTX.) The return value says where the result
3542 actually is to be found.
3544 MODE is the mode of the operand; the mode of the result is
3545 different but can be deduced from MODE. */
3548 expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
3550 rtx temp;
3552 /* Not applicable for floating point modes. */
3553 if (FLOAT_MODE_P (mode))
3554 return NULL_RTX;
3556 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3557 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3559 rtx_insn *last = get_last_insn ();
3561 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3562 if (temp != 0)
3563 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3564 OPTAB_WIDEN);
3566 if (temp != 0)
3567 return temp;
3569 delete_insns_since (last);
3572 /* If this machine has expensive jumps, we can do one's complement
3573 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3575 if (GET_MODE_CLASS (mode) == MODE_INT
3576 && BRANCH_COST (optimize_insn_for_speed_p (),
3577 false) >= 2)
3579 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3580 GET_MODE_PRECISION (mode) - 1,
3581 NULL_RTX, 0);
3583 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3584 OPTAB_LIB_WIDEN);
3586 if (temp != 0)
3587 return temp;
3590 return NULL_RTX;
3593 /* A subroutine of expand_copysign, perform the copysign operation using the
3594 abs and neg primitives advertised to exist on the target. The assumption
3595 is that we have a split register file, and leaving op0 in fp registers,
3596 and not playing with subregs so much, will help the register allocator. */
3598 static rtx
3599 expand_copysign_absneg (machine_mode mode, rtx op0, rtx op1, rtx target,
3600 int bitpos, bool op0_is_abs)
3602 machine_mode imode;
3603 enum insn_code icode;
3604 rtx sign;
3605 rtx_code_label *label;
3607 if (target == op1)
3608 target = NULL_RTX;
3610 /* Check if the back end provides an insn that handles signbit for the
3611 argument's mode. */
3612 icode = optab_handler (signbit_optab, mode);
3613 if (icode != CODE_FOR_nothing)
3615 imode = insn_data[(int) icode].operand[0].mode;
3616 sign = gen_reg_rtx (imode);
3617 emit_unop_insn (icode, sign, op1, UNKNOWN);
3619 else
3621 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3623 imode = int_mode_for_mode (mode);
3624 if (imode == BLKmode)
3625 return NULL_RTX;
3626 op1 = gen_lowpart (imode, op1);
3628 else
3630 int word;
3632 imode = word_mode;
3633 if (FLOAT_WORDS_BIG_ENDIAN)
3634 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3635 else
3636 word = bitpos / BITS_PER_WORD;
3637 bitpos = bitpos % BITS_PER_WORD;
3638 op1 = operand_subword_force (op1, word, mode);
3641 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3642 sign = expand_binop (imode, and_optab, op1,
3643 immed_wide_int_const (mask, imode),
3644 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3647 if (!op0_is_abs)
3649 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3650 if (op0 == NULL)
3651 return NULL_RTX;
3652 target = op0;
3654 else
3656 if (target == NULL_RTX)
3657 target = copy_to_reg (op0);
3658 else
3659 emit_move_insn (target, op0);
3662 label = gen_label_rtx ();
3663 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3665 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3666 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3667 else
3668 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3669 if (op0 != target)
3670 emit_move_insn (target, op0);
3672 emit_label (label);
3674 return target;
3678 /* A subroutine of expand_copysign, perform the entire copysign operation
3679 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3680 is true if op0 is known to have its sign bit clear. */
3682 static rtx
3683 expand_copysign_bit (machine_mode mode, rtx op0, rtx op1, rtx target,
3684 int bitpos, bool op0_is_abs)
3686 machine_mode imode;
3687 int word, nwords, i;
3688 rtx temp;
3689 rtx_insn *insns;
3691 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3693 imode = int_mode_for_mode (mode);
3694 if (imode == BLKmode)
3695 return NULL_RTX;
3696 word = 0;
3697 nwords = 1;
3699 else
3701 imode = word_mode;
3703 if (FLOAT_WORDS_BIG_ENDIAN)
3704 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3705 else
3706 word = bitpos / BITS_PER_WORD;
3707 bitpos = bitpos % BITS_PER_WORD;
3708 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3711 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3713 if (target == 0
3714 || target == op0
3715 || target == op1
3716 || (nwords > 1 && !valid_multiword_target_p (target)))
3717 target = gen_reg_rtx (mode);
3719 if (nwords > 1)
3721 start_sequence ();
3723 for (i = 0; i < nwords; ++i)
3725 rtx targ_piece = operand_subword (target, i, 1, mode);
3726 rtx op0_piece = operand_subword_force (op0, i, mode);
3728 if (i == word)
3730 if (!op0_is_abs)
3731 op0_piece
3732 = expand_binop (imode, and_optab, op0_piece,
3733 immed_wide_int_const (~mask, imode),
3734 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3735 op1 = expand_binop (imode, and_optab,
3736 operand_subword_force (op1, i, mode),
3737 immed_wide_int_const (mask, imode),
3738 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3740 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3741 targ_piece, 1, OPTAB_LIB_WIDEN);
3742 if (temp != targ_piece)
3743 emit_move_insn (targ_piece, temp);
3745 else
3746 emit_move_insn (targ_piece, op0_piece);
3749 insns = get_insns ();
3750 end_sequence ();
3752 emit_insn (insns);
3754 else
3756 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3757 immed_wide_int_const (mask, imode),
3758 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3760 op0 = gen_lowpart (imode, op0);
3761 if (!op0_is_abs)
3762 op0 = expand_binop (imode, and_optab, op0,
3763 immed_wide_int_const (~mask, imode),
3764 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3766 temp = expand_binop (imode, ior_optab, op0, op1,
3767 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3768 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3771 return target;
3774 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3775 scalar floating point mode. Return NULL if we do not know how to
3776 expand the operation inline. */
3779 expand_copysign (rtx op0, rtx op1, rtx target)
3781 machine_mode mode = GET_MODE (op0);
3782 const struct real_format *fmt;
3783 bool op0_is_abs;
3784 rtx temp;
3786 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3787 gcc_assert (GET_MODE (op1) == mode);
3789 /* First try to do it with a special instruction. */
3790 temp = expand_binop (mode, copysign_optab, op0, op1,
3791 target, 0, OPTAB_DIRECT);
3792 if (temp)
3793 return temp;
3795 fmt = REAL_MODE_FORMAT (mode);
3796 if (fmt == NULL || !fmt->has_signed_zero)
3797 return NULL_RTX;
3799 op0_is_abs = false;
3800 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3802 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3803 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3804 op0_is_abs = true;
3807 if (fmt->signbit_ro >= 0
3808 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3809 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3810 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3812 temp = expand_copysign_absneg (mode, op0, op1, target,
3813 fmt->signbit_ro, op0_is_abs);
3814 if (temp)
3815 return temp;
3818 if (fmt->signbit_rw < 0)
3819 return NULL_RTX;
3820 return expand_copysign_bit (mode, op0, op1, target,
3821 fmt->signbit_rw, op0_is_abs);
3824 /* Generate an instruction whose insn-code is INSN_CODE,
3825 with two operands: an output TARGET and an input OP0.
3826 TARGET *must* be nonzero, and the output is always stored there.
3827 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3828 the value that is stored into TARGET.
3830 Return false if expansion failed. */
3832 bool
3833 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3834 enum rtx_code code)
3836 struct expand_operand ops[2];
3837 rtx pat;
3839 create_output_operand (&ops[0], target, GET_MODE (target));
3840 create_input_operand (&ops[1], op0, GET_MODE (op0));
3841 pat = maybe_gen_insn (icode, 2, ops);
3842 if (!pat)
3843 return false;
3845 if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
3846 && code != UNKNOWN)
3847 add_equal_note (as_a <rtx_insn *> (pat), ops[0].value, code, ops[1].value,
3848 NULL_RTX);
3850 emit_insn (pat);
3852 if (ops[0].value != target)
3853 emit_move_insn (target, ops[0].value);
3854 return true;
3856 /* Generate an instruction whose insn-code is INSN_CODE,
3857 with two operands: an output TARGET and an input OP0.
3858 TARGET *must* be nonzero, and the output is always stored there.
3859 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3860 the value that is stored into TARGET. */
3862 void
3863 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3865 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3866 gcc_assert (ok);
3869 struct no_conflict_data
3871 rtx target;
3872 rtx_insn *first, *insn;
3873 bool must_stay;
3876 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3877 the currently examined clobber / store has to stay in the list of
3878 insns that constitute the actual libcall block. */
3879 static void
3880 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3882 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3884 /* If this inns directly contributes to setting the target, it must stay. */
3885 if (reg_overlap_mentioned_p (p->target, dest))
3886 p->must_stay = true;
3887 /* If we haven't committed to keeping any other insns in the list yet,
3888 there is nothing more to check. */
3889 else if (p->insn == p->first)
3890 return;
3891 /* If this insn sets / clobbers a register that feeds one of the insns
3892 already in the list, this insn has to stay too. */
3893 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3894 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3895 || reg_used_between_p (dest, p->first, p->insn)
3896 /* Likewise if this insn depends on a register set by a previous
3897 insn in the list, or if it sets a result (presumably a hard
3898 register) that is set or clobbered by a previous insn.
3899 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3900 SET_DEST perform the former check on the address, and the latter
3901 check on the MEM. */
3902 || (GET_CODE (set) == SET
3903 && (modified_in_p (SET_SRC (set), p->first)
3904 || modified_in_p (SET_DEST (set), p->first)
3905 || modified_between_p (SET_SRC (set), p->first, p->insn)
3906 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3907 p->must_stay = true;
3911 /* Emit code to make a call to a constant function or a library call.
3913 INSNS is a list containing all insns emitted in the call.
3914 These insns leave the result in RESULT. Our block is to copy RESULT
3915 to TARGET, which is logically equivalent to EQUIV.
3917 We first emit any insns that set a pseudo on the assumption that these are
3918 loading constants into registers; doing so allows them to be safely cse'ed
3919 between blocks. Then we emit all the other insns in the block, followed by
3920 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3921 note with an operand of EQUIV. */
3923 static void
3924 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
3925 bool equiv_may_trap)
3927 rtx final_dest = target;
3928 rtx_insn *next, *last, *insn;
3930 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3931 into a MEM later. Protect the libcall block from this change. */
3932 if (! REG_P (target) || REG_USERVAR_P (target))
3933 target = gen_reg_rtx (GET_MODE (target));
3935 /* If we're using non-call exceptions, a libcall corresponding to an
3936 operation that may trap may also trap. */
3937 /* ??? See the comment in front of make_reg_eh_region_note. */
3938 if (cfun->can_throw_non_call_exceptions
3939 && (equiv_may_trap || may_trap_p (equiv)))
3941 for (insn = insns; insn; insn = NEXT_INSN (insn))
3942 if (CALL_P (insn))
3944 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3945 if (note)
3947 int lp_nr = INTVAL (XEXP (note, 0));
3948 if (lp_nr == 0 || lp_nr == INT_MIN)
3949 remove_note (insn, note);
3953 else
3955 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3956 reg note to indicate that this call cannot throw or execute a nonlocal
3957 goto (unless there is already a REG_EH_REGION note, in which case
3958 we update it). */
3959 for (insn = insns; insn; insn = NEXT_INSN (insn))
3960 if (CALL_P (insn))
3961 make_reg_eh_region_note_nothrow_nononlocal (insn);
3964 /* First emit all insns that set pseudos. Remove them from the list as
3965 we go. Avoid insns that set pseudos which were referenced in previous
3966 insns. These can be generated by move_by_pieces, for example,
3967 to update an address. Similarly, avoid insns that reference things
3968 set in previous insns. */
3970 for (insn = insns; insn; insn = next)
3972 rtx set = single_set (insn);
3974 next = NEXT_INSN (insn);
3976 if (set != 0 && REG_P (SET_DEST (set))
3977 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3979 struct no_conflict_data data;
3981 data.target = const0_rtx;
3982 data.first = insns;
3983 data.insn = insn;
3984 data.must_stay = 0;
3985 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3986 if (! data.must_stay)
3988 if (PREV_INSN (insn))
3989 SET_NEXT_INSN (PREV_INSN (insn)) = next;
3990 else
3991 insns = next;
3993 if (next)
3994 SET_PREV_INSN (next) = PREV_INSN (insn);
3996 add_insn (insn);
4000 /* Some ports use a loop to copy large arguments onto the stack.
4001 Don't move anything outside such a loop. */
4002 if (LABEL_P (insn))
4003 break;
4006 /* Write the remaining insns followed by the final copy. */
4007 for (insn = insns; insn; insn = next)
4009 next = NEXT_INSN (insn);
4011 add_insn (insn);
4014 last = emit_move_insn (target, result);
4015 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
4017 if (final_dest != target)
4018 emit_move_insn (final_dest, target);
4021 void
4022 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
4024 emit_libcall_block_1 (safe_as_a <rtx_insn *> (insns),
4025 target, result, equiv, false);
4028 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
4029 PURPOSE describes how this comparison will be used. CODE is the rtx
4030 comparison code we will be using.
4032 ??? Actually, CODE is slightly weaker than that. A target is still
4033 required to implement all of the normal bcc operations, but not
4034 required to implement all (or any) of the unordered bcc operations. */
4037 can_compare_p (enum rtx_code code, machine_mode mode,
4038 enum can_compare_purpose purpose)
4040 rtx test;
4041 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
4044 enum insn_code icode;
4046 if (purpose == ccp_jump
4047 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
4048 && insn_operand_matches (icode, 0, test))
4049 return 1;
4050 if (purpose == ccp_store_flag
4051 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
4052 && insn_operand_matches (icode, 1, test))
4053 return 1;
4054 if (purpose == ccp_cmov
4055 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
4056 return 1;
4058 mode = GET_MODE_WIDER_MODE (mode);
4059 PUT_MODE (test, mode);
4061 while (mode != VOIDmode);
4063 return 0;
4066 /* This function is called when we are going to emit a compare instruction that
4067 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
4069 *PMODE is the mode of the inputs (in case they are const_int).
4070 *PUNSIGNEDP nonzero says that the operands are unsigned;
4071 this matters if they need to be widened (as given by METHODS).
4073 If they have mode BLKmode, then SIZE specifies the size of both operands.
4075 This function performs all the setup necessary so that the caller only has
4076 to emit a single comparison insn. This setup can involve doing a BLKmode
4077 comparison or emitting a library call to perform the comparison if no insn
4078 is available to handle it.
4079 The values which are passed in through pointers can be modified; the caller
4080 should perform the comparison on the modified values. Constant
4081 comparisons must have already been folded. */
4083 static void
4084 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4085 int unsignedp, enum optab_methods methods,
4086 rtx *ptest, machine_mode *pmode)
4088 machine_mode mode = *pmode;
4089 rtx libfunc, test;
4090 machine_mode cmp_mode;
4091 enum mode_class mclass;
4093 /* The other methods are not needed. */
4094 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4095 || methods == OPTAB_LIB_WIDEN);
4097 /* If we are optimizing, force expensive constants into a register. */
4098 if (CONSTANT_P (x) && optimize
4099 && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
4100 > COSTS_N_INSNS (1)))
4101 x = force_reg (mode, x);
4103 if (CONSTANT_P (y) && optimize
4104 && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
4105 > COSTS_N_INSNS (1)))
4106 y = force_reg (mode, y);
4108 #ifdef HAVE_cc0
4109 /* Make sure if we have a canonical comparison. The RTL
4110 documentation states that canonical comparisons are required only
4111 for targets which have cc0. */
4112 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4113 #endif
4115 /* Don't let both operands fail to indicate the mode. */
4116 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4117 x = force_reg (mode, x);
4118 if (mode == VOIDmode)
4119 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4121 /* Handle all BLKmode compares. */
4123 if (mode == BLKmode)
4125 machine_mode result_mode;
4126 enum insn_code cmp_code;
4127 tree length_type;
4128 rtx libfunc;
4129 rtx result;
4130 rtx opalign
4131 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4133 gcc_assert (size);
4135 /* Try to use a memory block compare insn - either cmpstr
4136 or cmpmem will do. */
4137 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4138 cmp_mode != VOIDmode;
4139 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4141 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4142 if (cmp_code == CODE_FOR_nothing)
4143 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4144 if (cmp_code == CODE_FOR_nothing)
4145 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4146 if (cmp_code == CODE_FOR_nothing)
4147 continue;
4149 /* Must make sure the size fits the insn's mode. */
4150 if ((CONST_INT_P (size)
4151 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4152 || (GET_MODE_BITSIZE (GET_MODE (size))
4153 > GET_MODE_BITSIZE (cmp_mode)))
4154 continue;
4156 result_mode = insn_data[cmp_code].operand[0].mode;
4157 result = gen_reg_rtx (result_mode);
4158 size = convert_to_mode (cmp_mode, size, 1);
4159 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4161 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4162 *pmode = result_mode;
4163 return;
4166 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4167 goto fail;
4169 /* Otherwise call a library function, memcmp. */
4170 libfunc = memcmp_libfunc;
4171 length_type = sizetype;
4172 result_mode = TYPE_MODE (integer_type_node);
4173 cmp_mode = TYPE_MODE (length_type);
4174 size = convert_to_mode (TYPE_MODE (length_type), size,
4175 TYPE_UNSIGNED (length_type));
4177 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4178 result_mode, 3,
4179 XEXP (x, 0), Pmode,
4180 XEXP (y, 0), Pmode,
4181 size, cmp_mode);
4182 x = result;
4183 y = const0_rtx;
4184 mode = result_mode;
4185 methods = OPTAB_LIB_WIDEN;
4186 unsignedp = false;
4189 /* Don't allow operands to the compare to trap, as that can put the
4190 compare and branch in different basic blocks. */
4191 if (cfun->can_throw_non_call_exceptions)
4193 if (may_trap_p (x))
4194 x = force_reg (mode, x);
4195 if (may_trap_p (y))
4196 y = force_reg (mode, y);
4199 if (GET_MODE_CLASS (mode) == MODE_CC)
4201 gcc_assert (can_compare_p (comparison, CCmode, ccp_jump));
4202 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4203 return;
4206 mclass = GET_MODE_CLASS (mode);
4207 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4208 cmp_mode = mode;
4211 enum insn_code icode;
4212 icode = optab_handler (cbranch_optab, cmp_mode);
4213 if (icode != CODE_FOR_nothing
4214 && insn_operand_matches (icode, 0, test))
4216 rtx_insn *last = get_last_insn ();
4217 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4218 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4219 if (op0 && op1
4220 && insn_operand_matches (icode, 1, op0)
4221 && insn_operand_matches (icode, 2, op1))
4223 XEXP (test, 0) = op0;
4224 XEXP (test, 1) = op1;
4225 *ptest = test;
4226 *pmode = cmp_mode;
4227 return;
4229 delete_insns_since (last);
4232 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4233 break;
4234 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4236 while (cmp_mode != VOIDmode);
4238 if (methods != OPTAB_LIB_WIDEN)
4239 goto fail;
4241 if (!SCALAR_FLOAT_MODE_P (mode))
4243 rtx result;
4244 machine_mode ret_mode;
4246 /* Handle a libcall just for the mode we are using. */
4247 libfunc = optab_libfunc (cmp_optab, mode);
4248 gcc_assert (libfunc);
4250 /* If we want unsigned, and this mode has a distinct unsigned
4251 comparison routine, use that. */
4252 if (unsignedp)
4254 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4255 if (ulibfunc)
4256 libfunc = ulibfunc;
4259 ret_mode = targetm.libgcc_cmp_return_mode ();
4260 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4261 ret_mode, 2, x, mode, y, mode);
4263 /* There are two kinds of comparison routines. Biased routines
4264 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4265 of gcc expect that the comparison operation is equivalent
4266 to the modified comparison. For signed comparisons compare the
4267 result against 1 in the biased case, and zero in the unbiased
4268 case. For unsigned comparisons always compare against 1 after
4269 biasing the unbiased result by adding 1. This gives us a way to
4270 represent LTU.
4271 The comparisons in the fixed-point helper library are always
4272 biased. */
4273 x = result;
4274 y = const1_rtx;
4276 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4278 if (unsignedp)
4279 x = plus_constant (ret_mode, result, 1);
4280 else
4281 y = const0_rtx;
4284 *pmode = ret_mode;
4285 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4286 ptest, pmode);
4288 else
4289 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4291 return;
4293 fail:
4294 *ptest = NULL_RTX;
4297 /* Before emitting an insn with code ICODE, make sure that X, which is going
4298 to be used for operand OPNUM of the insn, is converted from mode MODE to
4299 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4300 that it is accepted by the operand predicate. Return the new value. */
4303 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
4304 machine_mode wider_mode, int unsignedp)
4306 if (mode != wider_mode)
4307 x = convert_modes (wider_mode, mode, x, unsignedp);
4309 if (!insn_operand_matches (icode, opnum, x))
4311 if (reload_completed)
4312 return NULL_RTX;
4313 x = copy_to_mode_reg (insn_data[(int) icode].operand[opnum].mode, x);
4316 return x;
4319 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4320 we can do the branch. */
4322 static void
4323 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label, int prob)
4325 machine_mode optab_mode;
4326 enum mode_class mclass;
4327 enum insn_code icode;
4328 rtx_insn *insn;
4330 mclass = GET_MODE_CLASS (mode);
4331 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4332 icode = optab_handler (cbranch_optab, optab_mode);
4334 gcc_assert (icode != CODE_FOR_nothing);
4335 gcc_assert (insn_operand_matches (icode, 0, test));
4336 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4337 XEXP (test, 1), label));
4338 if (prob != -1
4339 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4340 && insn
4341 && JUMP_P (insn)
4342 && any_condjump_p (insn)
4343 && !find_reg_note (insn, REG_BR_PROB, 0))
4344 add_int_reg_note (insn, REG_BR_PROB, prob);
4347 /* Generate code to compare X with Y so that the condition codes are
4348 set and to jump to LABEL if the condition is true. If X is a
4349 constant and Y is not a constant, then the comparison is swapped to
4350 ensure that the comparison RTL has the canonical form.
4352 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4353 need to be widened. UNSIGNEDP is also used to select the proper
4354 branch condition code.
4356 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4358 MODE is the mode of the inputs (in case they are const_int).
4360 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4361 It will be potentially converted into an unsigned variant based on
4362 UNSIGNEDP to select a proper jump instruction.
4364 PROB is the probability of jumping to LABEL. */
4366 void
4367 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4368 machine_mode mode, int unsignedp, rtx label,
4369 int prob)
4371 rtx op0 = x, op1 = y;
4372 rtx test;
4374 /* Swap operands and condition to ensure canonical RTL. */
4375 if (swap_commutative_operands_p (x, y)
4376 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4378 op0 = y, op1 = x;
4379 comparison = swap_condition (comparison);
4382 /* If OP0 is still a constant, then both X and Y must be constants
4383 or the opposite comparison is not supported. Force X into a register
4384 to create canonical RTL. */
4385 if (CONSTANT_P (op0))
4386 op0 = force_reg (mode, op0);
4388 if (unsignedp)
4389 comparison = unsigned_condition (comparison);
4391 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4392 &test, &mode);
4393 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4397 /* Emit a library call comparison between floating point X and Y.
4398 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4400 static void
4401 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4402 rtx *ptest, machine_mode *pmode)
4404 enum rtx_code swapped = swap_condition (comparison);
4405 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4406 machine_mode orig_mode = GET_MODE (x);
4407 machine_mode mode, cmp_mode;
4408 rtx true_rtx, false_rtx;
4409 rtx value, target, equiv;
4410 rtx_insn *insns;
4411 rtx libfunc = 0;
4412 bool reversed_p = false;
4413 cmp_mode = targetm.libgcc_cmp_return_mode ();
4415 for (mode = orig_mode;
4416 mode != VOIDmode;
4417 mode = GET_MODE_WIDER_MODE (mode))
4419 if (code_to_optab (comparison)
4420 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4421 break;
4423 if (code_to_optab (swapped)
4424 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4426 rtx tmp;
4427 tmp = x; x = y; y = tmp;
4428 comparison = swapped;
4429 break;
4432 if (code_to_optab (reversed)
4433 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4435 comparison = reversed;
4436 reversed_p = true;
4437 break;
4441 gcc_assert (mode != VOIDmode);
4443 if (mode != orig_mode)
4445 x = convert_to_mode (mode, x, 0);
4446 y = convert_to_mode (mode, y, 0);
4449 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4450 the RTL. The allows the RTL optimizers to delete the libcall if the
4451 condition can be determined at compile-time. */
4452 if (comparison == UNORDERED
4453 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4455 true_rtx = const_true_rtx;
4456 false_rtx = const0_rtx;
4458 else
4460 switch (comparison)
4462 case EQ:
4463 true_rtx = const0_rtx;
4464 false_rtx = const_true_rtx;
4465 break;
4467 case NE:
4468 true_rtx = const_true_rtx;
4469 false_rtx = const0_rtx;
4470 break;
4472 case GT:
4473 true_rtx = const1_rtx;
4474 false_rtx = const0_rtx;
4475 break;
4477 case GE:
4478 true_rtx = const0_rtx;
4479 false_rtx = constm1_rtx;
4480 break;
4482 case LT:
4483 true_rtx = constm1_rtx;
4484 false_rtx = const0_rtx;
4485 break;
4487 case LE:
4488 true_rtx = const0_rtx;
4489 false_rtx = const1_rtx;
4490 break;
4492 default:
4493 gcc_unreachable ();
4497 if (comparison == UNORDERED)
4499 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4500 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4501 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4502 temp, const_true_rtx, equiv);
4504 else
4506 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4507 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4508 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4509 equiv, true_rtx, false_rtx);
4512 start_sequence ();
4513 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4514 cmp_mode, 2, x, mode, y, mode);
4515 insns = get_insns ();
4516 end_sequence ();
4518 target = gen_reg_rtx (cmp_mode);
4519 emit_libcall_block (insns, target, value, equiv);
4521 if (comparison == UNORDERED
4522 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4523 || reversed_p)
4524 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4525 else
4526 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4528 *pmode = cmp_mode;
4531 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4533 void
4534 emit_indirect_jump (rtx loc ATTRIBUTE_UNUSED)
4536 #ifndef HAVE_indirect_jump
4537 sorry ("indirect jumps are not available on this target");
4538 #else
4539 struct expand_operand ops[1];
4540 create_address_operand (&ops[0], loc);
4541 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4542 emit_barrier ();
4543 #endif
4546 #ifdef HAVE_conditional_move
4548 /* Emit a conditional move instruction if the machine supports one for that
4549 condition and machine mode.
4551 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4552 the mode to use should they be constants. If it is VOIDmode, they cannot
4553 both be constants.
4555 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4556 should be stored there. MODE is the mode to use should they be constants.
4557 If it is VOIDmode, they cannot both be constants.
4559 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4560 is not supported. */
4563 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4564 machine_mode cmode, rtx op2, rtx op3,
4565 machine_mode mode, int unsignedp)
4567 rtx tem, comparison;
4568 rtx_insn *last;
4569 enum insn_code icode;
4570 enum rtx_code reversed;
4572 /* If one operand is constant, make it the second one. Only do this
4573 if the other operand is not constant as well. */
4575 if (swap_commutative_operands_p (op0, op1))
4577 tem = op0;
4578 op0 = op1;
4579 op1 = tem;
4580 code = swap_condition (code);
4583 /* get_condition will prefer to generate LT and GT even if the old
4584 comparison was against zero, so undo that canonicalization here since
4585 comparisons against zero are cheaper. */
4586 if (code == LT && op1 == const1_rtx)
4587 code = LE, op1 = const0_rtx;
4588 else if (code == GT && op1 == constm1_rtx)
4589 code = GE, op1 = const0_rtx;
4591 if (cmode == VOIDmode)
4592 cmode = GET_MODE (op0);
4594 if (swap_commutative_operands_p (op2, op3)
4595 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4596 != UNKNOWN))
4598 tem = op2;
4599 op2 = op3;
4600 op3 = tem;
4601 code = reversed;
4604 if (mode == VOIDmode)
4605 mode = GET_MODE (op2);
4607 icode = direct_optab_handler (movcc_optab, mode);
4609 if (icode == CODE_FOR_nothing)
4610 return 0;
4612 if (!target)
4613 target = gen_reg_rtx (mode);
4615 code = unsignedp ? unsigned_condition (code) : code;
4616 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4618 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4619 return NULL and let the caller figure out how best to deal with this
4620 situation. */
4621 if (!COMPARISON_P (comparison))
4622 return NULL_RTX;
4624 saved_pending_stack_adjust save;
4625 save_pending_stack_adjust (&save);
4626 last = get_last_insn ();
4627 do_pending_stack_adjust ();
4628 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4629 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4630 &comparison, &cmode);
4631 if (comparison)
4633 struct expand_operand ops[4];
4635 create_output_operand (&ops[0], target, mode);
4636 create_fixed_operand (&ops[1], comparison);
4637 create_input_operand (&ops[2], op2, mode);
4638 create_input_operand (&ops[3], op3, mode);
4639 if (maybe_expand_insn (icode, 4, ops))
4641 if (ops[0].value != target)
4642 convert_move (target, ops[0].value, false);
4643 return target;
4646 delete_insns_since (last);
4647 restore_pending_stack_adjust (&save);
4648 return NULL_RTX;
4651 /* Return nonzero if a conditional move of mode MODE is supported.
4653 This function is for combine so it can tell whether an insn that looks
4654 like a conditional move is actually supported by the hardware. If we
4655 guess wrong we lose a bit on optimization, but that's it. */
4656 /* ??? sparc64 supports conditionally moving integers values based on fp
4657 comparisons, and vice versa. How do we handle them? */
4660 can_conditionally_move_p (machine_mode mode)
4662 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4663 return 1;
4665 return 0;
4668 #endif /* HAVE_conditional_move */
4670 /* Emit a conditional addition instruction if the machine supports one for that
4671 condition and machine mode.
4673 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4674 the mode to use should they be constants. If it is VOIDmode, they cannot
4675 both be constants.
4677 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4678 should be stored there. MODE is the mode to use should they be constants.
4679 If it is VOIDmode, they cannot both be constants.
4681 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4682 is not supported. */
4685 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4686 machine_mode cmode, rtx op2, rtx op3,
4687 machine_mode mode, int unsignedp)
4689 rtx tem, comparison;
4690 rtx_insn *last;
4691 enum insn_code icode;
4693 /* If one operand is constant, make it the second one. Only do this
4694 if the other operand is not constant as well. */
4696 if (swap_commutative_operands_p (op0, op1))
4698 tem = op0;
4699 op0 = op1;
4700 op1 = tem;
4701 code = swap_condition (code);
4704 /* get_condition will prefer to generate LT and GT even if the old
4705 comparison was against zero, so undo that canonicalization here since
4706 comparisons against zero are cheaper. */
4707 if (code == LT && op1 == const1_rtx)
4708 code = LE, op1 = const0_rtx;
4709 else if (code == GT && op1 == constm1_rtx)
4710 code = GE, op1 = const0_rtx;
4712 if (cmode == VOIDmode)
4713 cmode = GET_MODE (op0);
4715 if (mode == VOIDmode)
4716 mode = GET_MODE (op2);
4718 icode = optab_handler (addcc_optab, mode);
4720 if (icode == CODE_FOR_nothing)
4721 return 0;
4723 if (!target)
4724 target = gen_reg_rtx (mode);
4726 code = unsignedp ? unsigned_condition (code) : code;
4727 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4729 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4730 return NULL and let the caller figure out how best to deal with this
4731 situation. */
4732 if (!COMPARISON_P (comparison))
4733 return NULL_RTX;
4735 do_pending_stack_adjust ();
4736 last = get_last_insn ();
4737 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4738 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4739 &comparison, &cmode);
4740 if (comparison)
4742 struct expand_operand ops[4];
4744 create_output_operand (&ops[0], target, mode);
4745 create_fixed_operand (&ops[1], comparison);
4746 create_input_operand (&ops[2], op2, mode);
4747 create_input_operand (&ops[3], op3, mode);
4748 if (maybe_expand_insn (icode, 4, ops))
4750 if (ops[0].value != target)
4751 convert_move (target, ops[0].value, false);
4752 return target;
4755 delete_insns_since (last);
4756 return NULL_RTX;
4759 /* These functions attempt to generate an insn body, rather than
4760 emitting the insn, but if the gen function already emits them, we
4761 make no attempt to turn them back into naked patterns. */
4763 /* Generate and return an insn body to add Y to X. */
4766 gen_add2_insn (rtx x, rtx y)
4768 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4770 gcc_assert (insn_operand_matches (icode, 0, x));
4771 gcc_assert (insn_operand_matches (icode, 1, x));
4772 gcc_assert (insn_operand_matches (icode, 2, y));
4774 return GEN_FCN (icode) (x, x, y);
4777 /* Generate and return an insn body to add r1 and c,
4778 storing the result in r0. */
4781 gen_add3_insn (rtx r0, rtx r1, rtx c)
4783 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4785 if (icode == CODE_FOR_nothing
4786 || !insn_operand_matches (icode, 0, r0)
4787 || !insn_operand_matches (icode, 1, r1)
4788 || !insn_operand_matches (icode, 2, c))
4789 return NULL_RTX;
4791 return GEN_FCN (icode) (r0, r1, c);
4795 have_add2_insn (rtx x, rtx y)
4797 enum insn_code icode;
4799 gcc_assert (GET_MODE (x) != VOIDmode);
4801 icode = optab_handler (add_optab, GET_MODE (x));
4803 if (icode == CODE_FOR_nothing)
4804 return 0;
4806 if (!insn_operand_matches (icode, 0, x)
4807 || !insn_operand_matches (icode, 1, x)
4808 || !insn_operand_matches (icode, 2, y))
4809 return 0;
4811 return 1;
4814 /* Generate and return an insn body to add Y to X. */
4817 gen_addptr3_insn (rtx x, rtx y, rtx z)
4819 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4821 gcc_assert (insn_operand_matches (icode, 0, x));
4822 gcc_assert (insn_operand_matches (icode, 1, y));
4823 gcc_assert (insn_operand_matches (icode, 2, z));
4825 return GEN_FCN (icode) (x, y, z);
4828 /* Return true if the target implements an addptr pattern and X, Y,
4829 and Z are valid for the pattern predicates. */
4832 have_addptr3_insn (rtx x, rtx y, rtx z)
4834 enum insn_code icode;
4836 gcc_assert (GET_MODE (x) != VOIDmode);
4838 icode = optab_handler (addptr3_optab, GET_MODE (x));
4840 if (icode == CODE_FOR_nothing)
4841 return 0;
4843 if (!insn_operand_matches (icode, 0, x)
4844 || !insn_operand_matches (icode, 1, y)
4845 || !insn_operand_matches (icode, 2, z))
4846 return 0;
4848 return 1;
4851 /* Generate and return an insn body to subtract Y from X. */
4854 gen_sub2_insn (rtx x, rtx y)
4856 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4858 gcc_assert (insn_operand_matches (icode, 0, x));
4859 gcc_assert (insn_operand_matches (icode, 1, x));
4860 gcc_assert (insn_operand_matches (icode, 2, y));
4862 return GEN_FCN (icode) (x, x, y);
4865 /* Generate and return an insn body to subtract r1 and c,
4866 storing the result in r0. */
4869 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4871 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4873 if (icode == CODE_FOR_nothing
4874 || !insn_operand_matches (icode, 0, r0)
4875 || !insn_operand_matches (icode, 1, r1)
4876 || !insn_operand_matches (icode, 2, c))
4877 return NULL_RTX;
4879 return GEN_FCN (icode) (r0, r1, c);
4883 have_sub2_insn (rtx x, rtx y)
4885 enum insn_code icode;
4887 gcc_assert (GET_MODE (x) != VOIDmode);
4889 icode = optab_handler (sub_optab, GET_MODE (x));
4891 if (icode == CODE_FOR_nothing)
4892 return 0;
4894 if (!insn_operand_matches (icode, 0, x)
4895 || !insn_operand_matches (icode, 1, x)
4896 || !insn_operand_matches (icode, 2, y))
4897 return 0;
4899 return 1;
4902 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4903 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4904 no such operation exists, CODE_FOR_nothing will be returned. */
4906 enum insn_code
4907 can_extend_p (machine_mode to_mode, machine_mode from_mode,
4908 int unsignedp)
4910 convert_optab tab;
4911 #ifdef HAVE_ptr_extend
4912 if (unsignedp < 0)
4913 return CODE_FOR_ptr_extend;
4914 #endif
4916 tab = unsignedp ? zext_optab : sext_optab;
4917 return convert_optab_handler (tab, to_mode, from_mode);
4920 /* Generate the body of an insn to extend Y (with mode MFROM)
4921 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4924 gen_extend_insn (rtx x, rtx y, machine_mode mto,
4925 machine_mode mfrom, int unsignedp)
4927 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4928 return GEN_FCN (icode) (x, y);
4931 /* can_fix_p and can_float_p say whether the target machine
4932 can directly convert a given fixed point type to
4933 a given floating point type, or vice versa.
4934 The returned value is the CODE_FOR_... value to use,
4935 or CODE_FOR_nothing if these modes cannot be directly converted.
4937 *TRUNCP_PTR is set to 1 if it is necessary to output
4938 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4940 static enum insn_code
4941 can_fix_p (machine_mode fixmode, machine_mode fltmode,
4942 int unsignedp, int *truncp_ptr)
4944 convert_optab tab;
4945 enum insn_code icode;
4947 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4948 icode = convert_optab_handler (tab, fixmode, fltmode);
4949 if (icode != CODE_FOR_nothing)
4951 *truncp_ptr = 0;
4952 return icode;
4955 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4956 for this to work. We need to rework the fix* and ftrunc* patterns
4957 and documentation. */
4958 tab = unsignedp ? ufix_optab : sfix_optab;
4959 icode = convert_optab_handler (tab, fixmode, fltmode);
4960 if (icode != CODE_FOR_nothing
4961 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4963 *truncp_ptr = 1;
4964 return icode;
4967 *truncp_ptr = 0;
4968 return CODE_FOR_nothing;
4971 enum insn_code
4972 can_float_p (machine_mode fltmode, machine_mode fixmode,
4973 int unsignedp)
4975 convert_optab tab;
4977 tab = unsignedp ? ufloat_optab : sfloat_optab;
4978 return convert_optab_handler (tab, fltmode, fixmode);
4981 /* Function supportable_convert_operation
4983 Check whether an operation represented by the code CODE is a
4984 convert operation that is supported by the target platform in
4985 vector form (i.e., when operating on arguments of type VECTYPE_IN
4986 producing a result of type VECTYPE_OUT).
4988 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4989 This function checks if these operations are supported
4990 by the target platform either directly (via vector tree-codes), or via
4991 target builtins.
4993 Output:
4994 - CODE1 is code of vector operation to be used when
4995 vectorizing the operation, if available.
4996 - DECL is decl of target builtin functions to be used
4997 when vectorizing the operation, if available. In this case,
4998 CODE1 is CALL_EXPR. */
5000 bool
5001 supportable_convert_operation (enum tree_code code,
5002 tree vectype_out, tree vectype_in,
5003 tree *decl, enum tree_code *code1)
5005 machine_mode m1,m2;
5006 int truncp;
5008 m1 = TYPE_MODE (vectype_out);
5009 m2 = TYPE_MODE (vectype_in);
5011 /* First check if we can done conversion directly. */
5012 if ((code == FIX_TRUNC_EXPR
5013 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
5014 != CODE_FOR_nothing)
5015 || (code == FLOAT_EXPR
5016 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
5017 != CODE_FOR_nothing))
5019 *code1 = code;
5020 return true;
5023 /* Now check for builtin. */
5024 if (targetm.vectorize.builtin_conversion
5025 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
5027 *code1 = CALL_EXPR;
5028 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
5029 return true;
5031 return false;
5035 /* Generate code to convert FROM to floating point
5036 and store in TO. FROM must be fixed point and not VOIDmode.
5037 UNSIGNEDP nonzero means regard FROM as unsigned.
5038 Normally this is done by correcting the final value
5039 if it is negative. */
5041 void
5042 expand_float (rtx to, rtx from, int unsignedp)
5044 enum insn_code icode;
5045 rtx target = to;
5046 machine_mode fmode, imode;
5047 bool can_do_signed = false;
5049 /* Crash now, because we won't be able to decide which mode to use. */
5050 gcc_assert (GET_MODE (from) != VOIDmode);
5052 /* Look for an insn to do the conversion. Do it in the specified
5053 modes if possible; otherwise convert either input, output or both to
5054 wider mode. If the integer mode is wider than the mode of FROM,
5055 we can do the conversion signed even if the input is unsigned. */
5057 for (fmode = GET_MODE (to); fmode != VOIDmode;
5058 fmode = GET_MODE_WIDER_MODE (fmode))
5059 for (imode = GET_MODE (from); imode != VOIDmode;
5060 imode = GET_MODE_WIDER_MODE (imode))
5062 int doing_unsigned = unsignedp;
5064 if (fmode != GET_MODE (to)
5065 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
5066 continue;
5068 icode = can_float_p (fmode, imode, unsignedp);
5069 if (icode == CODE_FOR_nothing && unsignedp)
5071 enum insn_code scode = can_float_p (fmode, imode, 0);
5072 if (scode != CODE_FOR_nothing)
5073 can_do_signed = true;
5074 if (imode != GET_MODE (from))
5075 icode = scode, doing_unsigned = 0;
5078 if (icode != CODE_FOR_nothing)
5080 if (imode != GET_MODE (from))
5081 from = convert_to_mode (imode, from, unsignedp);
5083 if (fmode != GET_MODE (to))
5084 target = gen_reg_rtx (fmode);
5086 emit_unop_insn (icode, target, from,
5087 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
5089 if (target != to)
5090 convert_move (to, target, 0);
5091 return;
5095 /* Unsigned integer, and no way to convert directly. Convert as signed,
5096 then unconditionally adjust the result. */
5097 if (unsignedp && can_do_signed)
5099 rtx_code_label *label = gen_label_rtx ();
5100 rtx temp;
5101 REAL_VALUE_TYPE offset;
5103 /* Look for a usable floating mode FMODE wider than the source and at
5104 least as wide as the target. Using FMODE will avoid rounding woes
5105 with unsigned values greater than the signed maximum value. */
5107 for (fmode = GET_MODE (to); fmode != VOIDmode;
5108 fmode = GET_MODE_WIDER_MODE (fmode))
5109 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
5110 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
5111 break;
5113 if (fmode == VOIDmode)
5115 /* There is no such mode. Pretend the target is wide enough. */
5116 fmode = GET_MODE (to);
5118 /* Avoid double-rounding when TO is narrower than FROM. */
5119 if ((significand_size (fmode) + 1)
5120 < GET_MODE_PRECISION (GET_MODE (from)))
5122 rtx temp1;
5123 rtx_code_label *neglabel = gen_label_rtx ();
5125 /* Don't use TARGET if it isn't a register, is a hard register,
5126 or is the wrong mode. */
5127 if (!REG_P (target)
5128 || REGNO (target) < FIRST_PSEUDO_REGISTER
5129 || GET_MODE (target) != fmode)
5130 target = gen_reg_rtx (fmode);
5132 imode = GET_MODE (from);
5133 do_pending_stack_adjust ();
5135 /* Test whether the sign bit is set. */
5136 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5137 0, neglabel);
5139 /* The sign bit is not set. Convert as signed. */
5140 expand_float (target, from, 0);
5141 emit_jump_insn (gen_jump (label));
5142 emit_barrier ();
5144 /* The sign bit is set.
5145 Convert to a usable (positive signed) value by shifting right
5146 one bit, while remembering if a nonzero bit was shifted
5147 out; i.e., compute (from & 1) | (from >> 1). */
5149 emit_label (neglabel);
5150 temp = expand_binop (imode, and_optab, from, const1_rtx,
5151 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5152 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5153 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5154 OPTAB_LIB_WIDEN);
5155 expand_float (target, temp, 0);
5157 /* Multiply by 2 to undo the shift above. */
5158 temp = expand_binop (fmode, add_optab, target, target,
5159 target, 0, OPTAB_LIB_WIDEN);
5160 if (temp != target)
5161 emit_move_insn (target, temp);
5163 do_pending_stack_adjust ();
5164 emit_label (label);
5165 goto done;
5169 /* If we are about to do some arithmetic to correct for an
5170 unsigned operand, do it in a pseudo-register. */
5172 if (GET_MODE (to) != fmode
5173 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5174 target = gen_reg_rtx (fmode);
5176 /* Convert as signed integer to floating. */
5177 expand_float (target, from, 0);
5179 /* If FROM is negative (and therefore TO is negative),
5180 correct its value by 2**bitwidth. */
5182 do_pending_stack_adjust ();
5183 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5184 0, label);
5187 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5188 temp = expand_binop (fmode, add_optab, target,
5189 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5190 target, 0, OPTAB_LIB_WIDEN);
5191 if (temp != target)
5192 emit_move_insn (target, temp);
5194 do_pending_stack_adjust ();
5195 emit_label (label);
5196 goto done;
5199 /* No hardware instruction available; call a library routine. */
5201 rtx libfunc;
5202 rtx_insn *insns;
5203 rtx value;
5204 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5206 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_PRECISION (SImode))
5207 from = convert_to_mode (SImode, from, unsignedp);
5209 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5210 gcc_assert (libfunc);
5212 start_sequence ();
5214 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5215 GET_MODE (to), 1, from,
5216 GET_MODE (from));
5217 insns = get_insns ();
5218 end_sequence ();
5220 emit_libcall_block (insns, target, value,
5221 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5222 GET_MODE (to), from));
5225 done:
5227 /* Copy result to requested destination
5228 if we have been computing in a temp location. */
5230 if (target != to)
5232 if (GET_MODE (target) == GET_MODE (to))
5233 emit_move_insn (to, target);
5234 else
5235 convert_move (to, target, 0);
5239 /* Generate code to convert FROM to fixed point and store in TO. FROM
5240 must be floating point. */
5242 void
5243 expand_fix (rtx to, rtx from, int unsignedp)
5245 enum insn_code icode;
5246 rtx target = to;
5247 machine_mode fmode, imode;
5248 int must_trunc = 0;
5250 /* We first try to find a pair of modes, one real and one integer, at
5251 least as wide as FROM and TO, respectively, in which we can open-code
5252 this conversion. If the integer mode is wider than the mode of TO,
5253 we can do the conversion either signed or unsigned. */
5255 for (fmode = GET_MODE (from); fmode != VOIDmode;
5256 fmode = GET_MODE_WIDER_MODE (fmode))
5257 for (imode = GET_MODE (to); imode != VOIDmode;
5258 imode = GET_MODE_WIDER_MODE (imode))
5260 int doing_unsigned = unsignedp;
5262 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5263 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5264 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5266 if (icode != CODE_FOR_nothing)
5268 rtx_insn *last = get_last_insn ();
5269 if (fmode != GET_MODE (from))
5270 from = convert_to_mode (fmode, from, 0);
5272 if (must_trunc)
5274 rtx temp = gen_reg_rtx (GET_MODE (from));
5275 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5276 temp, 0);
5279 if (imode != GET_MODE (to))
5280 target = gen_reg_rtx (imode);
5282 if (maybe_emit_unop_insn (icode, target, from,
5283 doing_unsigned ? UNSIGNED_FIX : FIX))
5285 if (target != to)
5286 convert_move (to, target, unsignedp);
5287 return;
5289 delete_insns_since (last);
5293 /* For an unsigned conversion, there is one more way to do it.
5294 If we have a signed conversion, we generate code that compares
5295 the real value to the largest representable positive number. If if
5296 is smaller, the conversion is done normally. Otherwise, subtract
5297 one plus the highest signed number, convert, and add it back.
5299 We only need to check all real modes, since we know we didn't find
5300 anything with a wider integer mode.
5302 This code used to extend FP value into mode wider than the destination.
5303 This is needed for decimal float modes which cannot accurately
5304 represent one plus the highest signed number of the same size, but
5305 not for binary modes. Consider, for instance conversion from SFmode
5306 into DImode.
5308 The hot path through the code is dealing with inputs smaller than 2^63
5309 and doing just the conversion, so there is no bits to lose.
5311 In the other path we know the value is positive in the range 2^63..2^64-1
5312 inclusive. (as for other input overflow happens and result is undefined)
5313 So we know that the most important bit set in mantissa corresponds to
5314 2^63. The subtraction of 2^63 should not generate any rounding as it
5315 simply clears out that bit. The rest is trivial. */
5317 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5318 for (fmode = GET_MODE (from); fmode != VOIDmode;
5319 fmode = GET_MODE_WIDER_MODE (fmode))
5320 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5321 && (!DECIMAL_FLOAT_MODE_P (fmode)
5322 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5324 int bitsize;
5325 REAL_VALUE_TYPE offset;
5326 rtx limit;
5327 rtx_code_label *lab1, *lab2;
5328 rtx_insn *insn;
5330 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5331 real_2expN (&offset, bitsize - 1, fmode);
5332 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5333 lab1 = gen_label_rtx ();
5334 lab2 = gen_label_rtx ();
5336 if (fmode != GET_MODE (from))
5337 from = convert_to_mode (fmode, from, 0);
5339 /* See if we need to do the subtraction. */
5340 do_pending_stack_adjust ();
5341 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5342 0, lab1);
5344 /* If not, do the signed "fix" and branch around fixup code. */
5345 expand_fix (to, from, 0);
5346 emit_jump_insn (gen_jump (lab2));
5347 emit_barrier ();
5349 /* Otherwise, subtract 2**(N-1), convert to signed number,
5350 then add 2**(N-1). Do the addition using XOR since this
5351 will often generate better code. */
5352 emit_label (lab1);
5353 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5354 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5355 expand_fix (to, target, 0);
5356 target = expand_binop (GET_MODE (to), xor_optab, to,
5357 gen_int_mode
5358 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5359 GET_MODE (to)),
5360 to, 1, OPTAB_LIB_WIDEN);
5362 if (target != to)
5363 emit_move_insn (to, target);
5365 emit_label (lab2);
5367 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5369 /* Make a place for a REG_NOTE and add it. */
5370 insn = emit_move_insn (to, to);
5371 set_dst_reg_note (insn, REG_EQUAL,
5372 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5373 copy_rtx (from)),
5374 to);
5377 return;
5380 /* We can't do it with an insn, so use a library call. But first ensure
5381 that the mode of TO is at least as wide as SImode, since those are the
5382 only library calls we know about. */
5384 if (GET_MODE_PRECISION (GET_MODE (to)) < GET_MODE_PRECISION (SImode))
5386 target = gen_reg_rtx (SImode);
5388 expand_fix (target, from, unsignedp);
5390 else
5392 rtx_insn *insns;
5393 rtx value;
5394 rtx libfunc;
5396 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5397 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5398 gcc_assert (libfunc);
5400 start_sequence ();
5402 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5403 GET_MODE (to), 1, from,
5404 GET_MODE (from));
5405 insns = get_insns ();
5406 end_sequence ();
5408 emit_libcall_block (insns, target, value,
5409 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5410 GET_MODE (to), from));
5413 if (target != to)
5415 if (GET_MODE (to) == GET_MODE (target))
5416 emit_move_insn (to, target);
5417 else
5418 convert_move (to, target, 0);
5422 /* Generate code to convert FROM or TO a fixed-point.
5423 If UINTP is true, either TO or FROM is an unsigned integer.
5424 If SATP is true, we need to saturate the result. */
5426 void
5427 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5429 machine_mode to_mode = GET_MODE (to);
5430 machine_mode from_mode = GET_MODE (from);
5431 convert_optab tab;
5432 enum rtx_code this_code;
5433 enum insn_code code;
5434 rtx_insn *insns;
5435 rtx value;
5436 rtx libfunc;
5438 if (to_mode == from_mode)
5440 emit_move_insn (to, from);
5441 return;
5444 if (uintp)
5446 tab = satp ? satfractuns_optab : fractuns_optab;
5447 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5449 else
5451 tab = satp ? satfract_optab : fract_optab;
5452 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5454 code = convert_optab_handler (tab, to_mode, from_mode);
5455 if (code != CODE_FOR_nothing)
5457 emit_unop_insn (code, to, from, this_code);
5458 return;
5461 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5462 gcc_assert (libfunc);
5464 start_sequence ();
5465 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5466 1, from, from_mode);
5467 insns = get_insns ();
5468 end_sequence ();
5470 emit_libcall_block (insns, to, value,
5471 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5474 /* Generate code to convert FROM to fixed point and store in TO. FROM
5475 must be floating point, TO must be signed. Use the conversion optab
5476 TAB to do the conversion. */
5478 bool
5479 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5481 enum insn_code icode;
5482 rtx target = to;
5483 machine_mode fmode, imode;
5485 /* We first try to find a pair of modes, one real and one integer, at
5486 least as wide as FROM and TO, respectively, in which we can open-code
5487 this conversion. If the integer mode is wider than the mode of TO,
5488 we can do the conversion either signed or unsigned. */
5490 for (fmode = GET_MODE (from); fmode != VOIDmode;
5491 fmode = GET_MODE_WIDER_MODE (fmode))
5492 for (imode = GET_MODE (to); imode != VOIDmode;
5493 imode = GET_MODE_WIDER_MODE (imode))
5495 icode = convert_optab_handler (tab, imode, fmode);
5496 if (icode != CODE_FOR_nothing)
5498 rtx_insn *last = get_last_insn ();
5499 if (fmode != GET_MODE (from))
5500 from = convert_to_mode (fmode, from, 0);
5502 if (imode != GET_MODE (to))
5503 target = gen_reg_rtx (imode);
5505 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5507 delete_insns_since (last);
5508 continue;
5510 if (target != to)
5511 convert_move (to, target, 0);
5512 return true;
5516 return false;
5519 /* Report whether we have an instruction to perform the operation
5520 specified by CODE on operands of mode MODE. */
5522 have_insn_for (enum rtx_code code, machine_mode mode)
5524 return (code_to_optab (code)
5525 && (optab_handler (code_to_optab (code), mode)
5526 != CODE_FOR_nothing));
5529 /* Initialize the libfunc fields of an entire group of entries in some
5530 optab. Each entry is set equal to a string consisting of a leading
5531 pair of underscores followed by a generic operation name followed by
5532 a mode name (downshifted to lowercase) followed by a single character
5533 representing the number of operands for the given operation (which is
5534 usually one of the characters '2', '3', or '4').
5536 OPTABLE is the table in which libfunc fields are to be initialized.
5537 OPNAME is the generic (string) name of the operation.
5538 SUFFIX is the character which specifies the number of operands for
5539 the given generic operation.
5540 MODE is the mode to generate for.
5543 static void
5544 gen_libfunc (optab optable, const char *opname, int suffix,
5545 machine_mode mode)
5547 unsigned opname_len = strlen (opname);
5548 const char *mname = GET_MODE_NAME (mode);
5549 unsigned mname_len = strlen (mname);
5550 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5551 int len = prefix_len + opname_len + mname_len + 1 + 1;
5552 char *libfunc_name = XALLOCAVEC (char, len);
5553 char *p;
5554 const char *q;
5556 p = libfunc_name;
5557 *p++ = '_';
5558 *p++ = '_';
5559 if (targetm.libfunc_gnu_prefix)
5561 *p++ = 'g';
5562 *p++ = 'n';
5563 *p++ = 'u';
5564 *p++ = '_';
5566 for (q = opname; *q; )
5567 *p++ = *q++;
5568 for (q = mname; *q; q++)
5569 *p++ = TOLOWER (*q);
5570 *p++ = suffix;
5571 *p = '\0';
5573 set_optab_libfunc (optable, mode,
5574 ggc_alloc_string (libfunc_name, p - libfunc_name));
5577 /* Like gen_libfunc, but verify that integer operation is involved. */
5579 void
5580 gen_int_libfunc (optab optable, const char *opname, char suffix,
5581 machine_mode mode)
5583 int maxsize = 2 * BITS_PER_WORD;
5584 int minsize = BITS_PER_WORD;
5586 if (GET_MODE_CLASS (mode) != MODE_INT)
5587 return;
5588 if (maxsize < LONG_LONG_TYPE_SIZE)
5589 maxsize = LONG_LONG_TYPE_SIZE;
5590 if (minsize > INT_TYPE_SIZE
5591 && (trapv_binoptab_p (optable)
5592 || trapv_unoptab_p (optable)))
5593 minsize = INT_TYPE_SIZE;
5594 if (GET_MODE_BITSIZE (mode) < minsize
5595 || GET_MODE_BITSIZE (mode) > maxsize)
5596 return;
5597 gen_libfunc (optable, opname, suffix, mode);
5600 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5602 void
5603 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5604 machine_mode mode)
5606 char *dec_opname;
5608 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5609 gen_libfunc (optable, opname, suffix, mode);
5610 if (DECIMAL_FLOAT_MODE_P (mode))
5612 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5613 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5614 depending on the low level floating format used. */
5615 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5616 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5617 gen_libfunc (optable, dec_opname, suffix, mode);
5621 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5623 void
5624 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5625 machine_mode mode)
5627 if (!ALL_FIXED_POINT_MODE_P (mode))
5628 return;
5629 gen_libfunc (optable, opname, suffix, mode);
5632 /* Like gen_libfunc, but verify that signed fixed-point operation is
5633 involved. */
5635 void
5636 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5637 machine_mode mode)
5639 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5640 return;
5641 gen_libfunc (optable, opname, suffix, mode);
5644 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5645 involved. */
5647 void
5648 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5649 machine_mode mode)
5651 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5652 return;
5653 gen_libfunc (optable, opname, suffix, mode);
5656 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5658 void
5659 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5660 machine_mode mode)
5662 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5663 gen_fp_libfunc (optable, name, suffix, mode);
5664 if (INTEGRAL_MODE_P (mode))
5665 gen_int_libfunc (optable, name, suffix, mode);
5668 /* Like gen_libfunc, but verify that FP or INT operation is involved
5669 and add 'v' suffix for integer operation. */
5671 void
5672 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5673 machine_mode mode)
5675 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5676 gen_fp_libfunc (optable, name, suffix, mode);
5677 if (GET_MODE_CLASS (mode) == MODE_INT)
5679 int len = strlen (name);
5680 char *v_name = XALLOCAVEC (char, len + 2);
5681 strcpy (v_name, name);
5682 v_name[len] = 'v';
5683 v_name[len + 1] = 0;
5684 gen_int_libfunc (optable, v_name, suffix, mode);
5688 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5689 involved. */
5691 void
5692 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5693 machine_mode mode)
5695 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5696 gen_fp_libfunc (optable, name, suffix, mode);
5697 if (INTEGRAL_MODE_P (mode))
5698 gen_int_libfunc (optable, name, suffix, mode);
5699 if (ALL_FIXED_POINT_MODE_P (mode))
5700 gen_fixed_libfunc (optable, name, suffix, mode);
5703 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5704 involved. */
5706 void
5707 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5708 machine_mode mode)
5710 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5711 gen_fp_libfunc (optable, name, suffix, mode);
5712 if (INTEGRAL_MODE_P (mode))
5713 gen_int_libfunc (optable, name, suffix, mode);
5714 if (SIGNED_FIXED_POINT_MODE_P (mode))
5715 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5718 /* Like gen_libfunc, but verify that INT or FIXED operation is
5719 involved. */
5721 void
5722 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5723 machine_mode mode)
5725 if (INTEGRAL_MODE_P (mode))
5726 gen_int_libfunc (optable, name, suffix, mode);
5727 if (ALL_FIXED_POINT_MODE_P (mode))
5728 gen_fixed_libfunc (optable, name, suffix, mode);
5731 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5732 involved. */
5734 void
5735 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5736 machine_mode mode)
5738 if (INTEGRAL_MODE_P (mode))
5739 gen_int_libfunc (optable, name, suffix, mode);
5740 if (SIGNED_FIXED_POINT_MODE_P (mode))
5741 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5744 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5745 involved. */
5747 void
5748 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5749 machine_mode mode)
5751 if (INTEGRAL_MODE_P (mode))
5752 gen_int_libfunc (optable, name, suffix, mode);
5753 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5754 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5757 /* Initialize the libfunc fields of an entire group of entries of an
5758 inter-mode-class conversion optab. The string formation rules are
5759 similar to the ones for init_libfuncs, above, but instead of having
5760 a mode name and an operand count these functions have two mode names
5761 and no operand count. */
5763 void
5764 gen_interclass_conv_libfunc (convert_optab tab,
5765 const char *opname,
5766 machine_mode tmode,
5767 machine_mode fmode)
5769 size_t opname_len = strlen (opname);
5770 size_t mname_len = 0;
5772 const char *fname, *tname;
5773 const char *q;
5774 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5775 char *libfunc_name, *suffix;
5776 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5777 char *p;
5779 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5780 depends on which underlying decimal floating point format is used. */
5781 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5783 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5785 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5786 nondec_name[0] = '_';
5787 nondec_name[1] = '_';
5788 if (targetm.libfunc_gnu_prefix)
5790 nondec_name[2] = 'g';
5791 nondec_name[3] = 'n';
5792 nondec_name[4] = 'u';
5793 nondec_name[5] = '_';
5796 memcpy (&nondec_name[prefix_len], opname, opname_len);
5797 nondec_suffix = nondec_name + opname_len + prefix_len;
5799 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5800 dec_name[0] = '_';
5801 dec_name[1] = '_';
5802 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5803 memcpy (&dec_name[2+dec_len], opname, opname_len);
5804 dec_suffix = dec_name + dec_len + opname_len + 2;
5806 fname = GET_MODE_NAME (fmode);
5807 tname = GET_MODE_NAME (tmode);
5809 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5811 libfunc_name = dec_name;
5812 suffix = dec_suffix;
5814 else
5816 libfunc_name = nondec_name;
5817 suffix = nondec_suffix;
5820 p = suffix;
5821 for (q = fname; *q; p++, q++)
5822 *p = TOLOWER (*q);
5823 for (q = tname; *q; p++, q++)
5824 *p = TOLOWER (*q);
5826 *p = '\0';
5828 set_conv_libfunc (tab, tmode, fmode,
5829 ggc_alloc_string (libfunc_name, p - libfunc_name));
5832 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5833 int->fp conversion. */
5835 void
5836 gen_int_to_fp_conv_libfunc (convert_optab tab,
5837 const char *opname,
5838 machine_mode tmode,
5839 machine_mode fmode)
5841 if (GET_MODE_CLASS (fmode) != MODE_INT)
5842 return;
5843 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5844 return;
5845 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5848 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5849 naming scheme. */
5851 void
5852 gen_ufloat_conv_libfunc (convert_optab tab,
5853 const char *opname ATTRIBUTE_UNUSED,
5854 machine_mode tmode,
5855 machine_mode fmode)
5857 if (DECIMAL_FLOAT_MODE_P (tmode))
5858 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5859 else
5860 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5863 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5864 fp->int conversion. */
5866 void
5867 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5868 const char *opname,
5869 machine_mode tmode,
5870 machine_mode fmode)
5872 if (GET_MODE_CLASS (fmode) != MODE_INT)
5873 return;
5874 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5875 return;
5876 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5879 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5880 fp->int conversion with no decimal floating point involved. */
5882 void
5883 gen_fp_to_int_conv_libfunc (convert_optab tab,
5884 const char *opname,
5885 machine_mode tmode,
5886 machine_mode fmode)
5888 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5889 return;
5890 if (GET_MODE_CLASS (tmode) != MODE_INT)
5891 return;
5892 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5895 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5896 The string formation rules are
5897 similar to the ones for init_libfunc, above. */
5899 void
5900 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5901 machine_mode tmode, machine_mode fmode)
5903 size_t opname_len = strlen (opname);
5904 size_t mname_len = 0;
5906 const char *fname, *tname;
5907 const char *q;
5908 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5909 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5910 char *libfunc_name, *suffix;
5911 char *p;
5913 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5914 depends on which underlying decimal floating point format is used. */
5915 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5917 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5919 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5920 nondec_name[0] = '_';
5921 nondec_name[1] = '_';
5922 if (targetm.libfunc_gnu_prefix)
5924 nondec_name[2] = 'g';
5925 nondec_name[3] = 'n';
5926 nondec_name[4] = 'u';
5927 nondec_name[5] = '_';
5929 memcpy (&nondec_name[prefix_len], opname, opname_len);
5930 nondec_suffix = nondec_name + opname_len + prefix_len;
5932 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5933 dec_name[0] = '_';
5934 dec_name[1] = '_';
5935 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5936 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5937 dec_suffix = dec_name + dec_len + opname_len + 2;
5939 fname = GET_MODE_NAME (fmode);
5940 tname = GET_MODE_NAME (tmode);
5942 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5944 libfunc_name = dec_name;
5945 suffix = dec_suffix;
5947 else
5949 libfunc_name = nondec_name;
5950 suffix = nondec_suffix;
5953 p = suffix;
5954 for (q = fname; *q; p++, q++)
5955 *p = TOLOWER (*q);
5956 for (q = tname; *q; p++, q++)
5957 *p = TOLOWER (*q);
5959 *p++ = '2';
5960 *p = '\0';
5962 set_conv_libfunc (tab, tmode, fmode,
5963 ggc_alloc_string (libfunc_name, p - libfunc_name));
5966 /* Pick proper libcall for trunc_optab. We need to chose if we do
5967 truncation or extension and interclass or intraclass. */
5969 void
5970 gen_trunc_conv_libfunc (convert_optab tab,
5971 const char *opname,
5972 machine_mode tmode,
5973 machine_mode fmode)
5975 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5976 return;
5977 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5978 return;
5979 if (tmode == fmode)
5980 return;
5982 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5983 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5984 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5986 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5987 return;
5989 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5990 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5991 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5992 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5995 /* Pick proper libcall for extend_optab. We need to chose if we do
5996 truncation or extension and interclass or intraclass. */
5998 void
5999 gen_extend_conv_libfunc (convert_optab tab,
6000 const char *opname ATTRIBUTE_UNUSED,
6001 machine_mode tmode,
6002 machine_mode fmode)
6004 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
6005 return;
6006 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
6007 return;
6008 if (tmode == fmode)
6009 return;
6011 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
6012 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
6013 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6015 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
6016 return;
6018 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
6019 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
6020 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
6021 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6024 /* Pick proper libcall for fract_optab. We need to chose if we do
6025 interclass or intraclass. */
6027 void
6028 gen_fract_conv_libfunc (convert_optab tab,
6029 const char *opname,
6030 machine_mode tmode,
6031 machine_mode fmode)
6033 if (tmode == fmode)
6034 return;
6035 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
6036 return;
6038 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6039 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6040 else
6041 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6044 /* Pick proper libcall for fractuns_optab. */
6046 void
6047 gen_fractuns_conv_libfunc (convert_optab tab,
6048 const char *opname,
6049 machine_mode tmode,
6050 machine_mode fmode)
6052 if (tmode == fmode)
6053 return;
6054 /* One mode must be a fixed-point mode, and the other must be an integer
6055 mode. */
6056 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
6057 || (ALL_FIXED_POINT_MODE_P (fmode)
6058 && GET_MODE_CLASS (tmode) == MODE_INT)))
6059 return;
6061 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6064 /* Pick proper libcall for satfract_optab. We need to chose if we do
6065 interclass or intraclass. */
6067 void
6068 gen_satfract_conv_libfunc (convert_optab tab,
6069 const char *opname,
6070 machine_mode tmode,
6071 machine_mode fmode)
6073 if (tmode == fmode)
6074 return;
6075 /* TMODE must be a fixed-point mode. */
6076 if (!ALL_FIXED_POINT_MODE_P (tmode))
6077 return;
6079 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6080 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6081 else
6082 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6085 /* Pick proper libcall for satfractuns_optab. */
6087 void
6088 gen_satfractuns_conv_libfunc (convert_optab tab,
6089 const char *opname,
6090 machine_mode tmode,
6091 machine_mode fmode)
6093 if (tmode == fmode)
6094 return;
6095 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
6096 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
6097 return;
6099 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6102 /* Hashtable callbacks for libfunc_decls. */
6104 struct libfunc_decl_hasher : ggc_hasher<tree>
6106 static hashval_t
6107 hash (tree entry)
6109 return IDENTIFIER_HASH_VALUE (DECL_NAME (entry));
6112 static bool
6113 equal (tree decl, tree name)
6115 return DECL_NAME (decl) == name;
6119 /* A table of previously-created libfuncs, hashed by name. */
6120 static GTY (()) hash_table<libfunc_decl_hasher> *libfunc_decls;
6122 /* Build a decl for a libfunc named NAME. */
6124 tree
6125 build_libfunc_function (const char *name)
6127 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6128 get_identifier (name),
6129 build_function_type (integer_type_node, NULL_TREE));
6130 /* ??? We don't have any type information except for this is
6131 a function. Pretend this is "int foo()". */
6132 DECL_ARTIFICIAL (decl) = 1;
6133 DECL_EXTERNAL (decl) = 1;
6134 TREE_PUBLIC (decl) = 1;
6135 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6137 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6138 are the flags assigned by targetm.encode_section_info. */
6139 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6141 return decl;
6145 init_one_libfunc (const char *name)
6147 tree id, decl;
6148 hashval_t hash;
6150 if (libfunc_decls == NULL)
6151 libfunc_decls = hash_table<libfunc_decl_hasher>::create_ggc (37);
6153 /* See if we have already created a libfunc decl for this function. */
6154 id = get_identifier (name);
6155 hash = IDENTIFIER_HASH_VALUE (id);
6156 tree *slot = libfunc_decls->find_slot_with_hash (id, hash, INSERT);
6157 decl = *slot;
6158 if (decl == NULL)
6160 /* Create a new decl, so that it can be passed to
6161 targetm.encode_section_info. */
6162 decl = build_libfunc_function (name);
6163 *slot = decl;
6165 return XEXP (DECL_RTL (decl), 0);
6168 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6171 set_user_assembler_libfunc (const char *name, const char *asmspec)
6173 tree id, decl;
6174 hashval_t hash;
6176 id = get_identifier (name);
6177 hash = IDENTIFIER_HASH_VALUE (id);
6178 tree *slot = libfunc_decls->find_slot_with_hash (id, hash, NO_INSERT);
6179 gcc_assert (slot);
6180 decl = (tree) *slot;
6181 set_user_assembler_name (decl, asmspec);
6182 return XEXP (DECL_RTL (decl), 0);
6185 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6186 MODE to NAME, which should be either 0 or a string constant. */
6187 void
6188 set_optab_libfunc (optab op, machine_mode mode, const char *name)
6190 rtx val;
6191 struct libfunc_entry e;
6192 struct libfunc_entry **slot;
6194 e.op = op;
6195 e.mode1 = mode;
6196 e.mode2 = VOIDmode;
6198 if (name)
6199 val = init_one_libfunc (name);
6200 else
6201 val = 0;
6202 slot = libfunc_hash->find_slot (&e, INSERT);
6203 if (*slot == NULL)
6204 *slot = ggc_alloc<libfunc_entry> ();
6205 (*slot)->op = op;
6206 (*slot)->mode1 = mode;
6207 (*slot)->mode2 = VOIDmode;
6208 (*slot)->libfunc = val;
6211 /* Call this to reset the function entry for one conversion optab
6212 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6213 either 0 or a string constant. */
6214 void
6215 set_conv_libfunc (convert_optab optab, machine_mode tmode,
6216 machine_mode fmode, const char *name)
6218 rtx val;
6219 struct libfunc_entry e;
6220 struct libfunc_entry **slot;
6222 e.op = optab;
6223 e.mode1 = tmode;
6224 e.mode2 = fmode;
6226 if (name)
6227 val = init_one_libfunc (name);
6228 else
6229 val = 0;
6230 slot = libfunc_hash->find_slot (&e, INSERT);
6231 if (*slot == NULL)
6232 *slot = ggc_alloc<libfunc_entry> ();
6233 (*slot)->op = optab;
6234 (*slot)->mode1 = tmode;
6235 (*slot)->mode2 = fmode;
6236 (*slot)->libfunc = val;
6239 /* Call this to initialize the contents of the optabs
6240 appropriately for the current target machine. */
6242 void
6243 init_optabs (void)
6245 if (libfunc_hash)
6246 libfunc_hash->empty ();
6247 else
6248 libfunc_hash = hash_table<libfunc_hasher>::create_ggc (10);
6250 /* Fill in the optabs with the insns we support. */
6251 init_all_optabs (this_fn_optabs);
6253 /* The ffs function operates on `int'. Fall back on it if we do not
6254 have a libgcc2 function for that width. */
6255 if (INT_TYPE_SIZE < BITS_PER_WORD)
6256 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6257 "ffs");
6259 /* Explicitly initialize the bswap libfuncs since we need them to be
6260 valid for things other than word_mode. */
6261 if (targetm.libfunc_gnu_prefix)
6263 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6264 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6266 else
6268 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6269 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6272 /* Use cabs for double complex abs, since systems generally have cabs.
6273 Don't define any libcall for float complex, so that cabs will be used. */
6274 if (complex_double_type_node)
6275 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node),
6276 "cabs");
6278 abort_libfunc = init_one_libfunc ("abort");
6279 memcpy_libfunc = init_one_libfunc ("memcpy");
6280 memmove_libfunc = init_one_libfunc ("memmove");
6281 memcmp_libfunc = init_one_libfunc ("memcmp");
6282 memset_libfunc = init_one_libfunc ("memset");
6283 setbits_libfunc = init_one_libfunc ("__setbits");
6285 #ifndef DONT_USE_BUILTIN_SETJMP
6286 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6287 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6288 #else
6289 setjmp_libfunc = init_one_libfunc ("setjmp");
6290 longjmp_libfunc = init_one_libfunc ("longjmp");
6291 #endif
6292 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6293 unwind_sjlj_unregister_libfunc
6294 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6296 /* For function entry/exit instrumentation. */
6297 profile_function_entry_libfunc
6298 = init_one_libfunc ("__cyg_profile_func_enter");
6299 profile_function_exit_libfunc
6300 = init_one_libfunc ("__cyg_profile_func_exit");
6302 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6304 /* Allow the target to add more libcalls or rename some, etc. */
6305 targetm.init_libfuncs ();
6308 /* Use the current target and options to initialize
6309 TREE_OPTIMIZATION_OPTABS (OPTNODE). */
6311 void
6312 init_tree_optimization_optabs (tree optnode)
6314 /* Quick exit if we have already computed optabs for this target. */
6315 if (TREE_OPTIMIZATION_BASE_OPTABS (optnode) == this_target_optabs)
6316 return;
6318 /* Forget any previous information and set up for the current target. */
6319 TREE_OPTIMIZATION_BASE_OPTABS (optnode) = this_target_optabs;
6320 struct target_optabs *tmp_optabs = (struct target_optabs *)
6321 TREE_OPTIMIZATION_OPTABS (optnode);
6322 if (tmp_optabs)
6323 memset (tmp_optabs, 0, sizeof (struct target_optabs));
6324 else
6325 tmp_optabs = ggc_alloc<target_optabs> ();
6327 /* Generate a new set of optabs into tmp_optabs. */
6328 init_all_optabs (tmp_optabs);
6330 /* If the optabs changed, record it. */
6331 if (memcmp (tmp_optabs, this_target_optabs, sizeof (struct target_optabs)))
6332 TREE_OPTIMIZATION_OPTABS (optnode) = tmp_optabs;
6333 else
6335 TREE_OPTIMIZATION_OPTABS (optnode) = NULL;
6336 ggc_free (tmp_optabs);
6340 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6341 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6343 static void
6344 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6346 machine_mode mode;
6347 char buf[64];
6348 size_t len = strlen (base);
6349 int i;
6351 gcc_assert (max <= 8);
6352 gcc_assert (len + 3 < sizeof (buf));
6354 memcpy (buf, base, len);
6355 buf[len] = '_';
6356 buf[len + 1] = '0';
6357 buf[len + 2] = '\0';
6359 mode = QImode;
6360 for (i = 1; i <= max; i *= 2)
6362 buf[len + 1] = '0' + i;
6363 set_optab_libfunc (tab, mode, buf);
6364 mode = GET_MODE_2XWIDER_MODE (mode);
6368 void
6369 init_sync_libfuncs (int max)
6371 if (!flag_sync_libcalls)
6372 return;
6374 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6375 "__sync_val_compare_and_swap", max);
6376 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6377 "__sync_lock_test_and_set", max);
6379 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6380 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6381 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6382 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6383 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6384 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6386 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6387 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6388 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6389 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6390 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6391 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6394 /* Print information about the current contents of the optabs on
6395 STDERR. */
6397 DEBUG_FUNCTION void
6398 debug_optab_libfuncs (void)
6400 int i, j, k;
6402 /* Dump the arithmetic optabs. */
6403 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
6404 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6406 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
6407 if (l)
6409 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6410 fprintf (stderr, "%s\t%s:\t%s\n",
6411 GET_RTX_NAME (optab_to_code ((optab) i)),
6412 GET_MODE_NAME (j),
6413 XSTR (l, 0));
6417 /* Dump the conversion optabs. */
6418 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
6419 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6420 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6422 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
6423 (machine_mode) k);
6424 if (l)
6426 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6427 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6428 GET_RTX_NAME (optab_to_code ((optab) i)),
6429 GET_MODE_NAME (j),
6430 GET_MODE_NAME (k),
6431 XSTR (l, 0));
6437 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6438 CODE. Return 0 on failure. */
6441 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6443 machine_mode mode = GET_MODE (op1);
6444 enum insn_code icode;
6445 rtx insn;
6446 rtx trap_rtx;
6448 if (mode == VOIDmode)
6449 return 0;
6451 icode = optab_handler (ctrap_optab, mode);
6452 if (icode == CODE_FOR_nothing)
6453 return 0;
6455 /* Some targets only accept a zero trap code. */
6456 if (!insn_operand_matches (icode, 3, tcode))
6457 return 0;
6459 do_pending_stack_adjust ();
6460 start_sequence ();
6461 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6462 &trap_rtx, &mode);
6463 if (!trap_rtx)
6464 insn = NULL_RTX;
6465 else
6466 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6467 tcode);
6469 /* If that failed, then give up. */
6470 if (insn == 0)
6472 end_sequence ();
6473 return 0;
6476 emit_insn (insn);
6477 insn = get_insns ();
6478 end_sequence ();
6479 return insn;
6482 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6483 or unsigned operation code. */
6485 static enum rtx_code
6486 get_rtx_code (enum tree_code tcode, bool unsignedp)
6488 enum rtx_code code;
6489 switch (tcode)
6491 case EQ_EXPR:
6492 code = EQ;
6493 break;
6494 case NE_EXPR:
6495 code = NE;
6496 break;
6497 case LT_EXPR:
6498 code = unsignedp ? LTU : LT;
6499 break;
6500 case LE_EXPR:
6501 code = unsignedp ? LEU : LE;
6502 break;
6503 case GT_EXPR:
6504 code = unsignedp ? GTU : GT;
6505 break;
6506 case GE_EXPR:
6507 code = unsignedp ? GEU : GE;
6508 break;
6510 case UNORDERED_EXPR:
6511 code = UNORDERED;
6512 break;
6513 case ORDERED_EXPR:
6514 code = ORDERED;
6515 break;
6516 case UNLT_EXPR:
6517 code = UNLT;
6518 break;
6519 case UNLE_EXPR:
6520 code = UNLE;
6521 break;
6522 case UNGT_EXPR:
6523 code = UNGT;
6524 break;
6525 case UNGE_EXPR:
6526 code = UNGE;
6527 break;
6528 case UNEQ_EXPR:
6529 code = UNEQ;
6530 break;
6531 case LTGT_EXPR:
6532 code = LTGT;
6533 break;
6535 default:
6536 gcc_unreachable ();
6538 return code;
6541 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6542 unsigned operators. Do not generate compare instruction. */
6544 static rtx
6545 vector_compare_rtx (enum tree_code tcode, tree t_op0, tree t_op1,
6546 bool unsignedp, enum insn_code icode)
6548 struct expand_operand ops[2];
6549 rtx rtx_op0, rtx_op1;
6550 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
6552 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
6554 /* Expand operands. */
6555 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6556 EXPAND_STACK_PARM);
6557 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6558 EXPAND_STACK_PARM);
6560 create_input_operand (&ops[0], rtx_op0, GET_MODE (rtx_op0));
6561 create_input_operand (&ops[1], rtx_op1, GET_MODE (rtx_op1));
6562 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6563 gcc_unreachable ();
6564 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6567 /* Return true if VEC_PERM_EXPR can be expanded using SIMD extensions
6568 of the CPU. SEL may be NULL, which stands for an unknown constant. */
6570 bool
6571 can_vec_perm_p (machine_mode mode, bool variable,
6572 const unsigned char *sel)
6574 machine_mode qimode;
6576 /* If the target doesn't implement a vector mode for the vector type,
6577 then no operations are supported. */
6578 if (!VECTOR_MODE_P (mode))
6579 return false;
6581 if (!variable)
6583 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6584 && (sel == NULL
6585 || targetm.vectorize.vec_perm_const_ok == NULL
6586 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6587 return true;
6590 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6591 return true;
6593 /* We allow fallback to a QI vector mode, and adjust the mask. */
6594 if (GET_MODE_INNER (mode) == QImode)
6595 return false;
6596 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6597 if (!VECTOR_MODE_P (qimode))
6598 return false;
6600 /* ??? For completeness, we ought to check the QImode version of
6601 vec_perm_const_optab. But all users of this implicit lowering
6602 feature implement the variable vec_perm_optab. */
6603 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6604 return false;
6606 /* In order to support the lowering of variable permutations,
6607 we need to support shifts and adds. */
6608 if (variable)
6610 if (GET_MODE_UNIT_SIZE (mode) > 2
6611 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6612 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6613 return false;
6614 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6615 return false;
6618 return true;
6621 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
6623 static rtx
6624 expand_vec_perm_1 (enum insn_code icode, rtx target,
6625 rtx v0, rtx v1, rtx sel)
6627 machine_mode tmode = GET_MODE (target);
6628 machine_mode smode = GET_MODE (sel);
6629 struct expand_operand ops[4];
6631 create_output_operand (&ops[0], target, tmode);
6632 create_input_operand (&ops[3], sel, smode);
6634 /* Make an effort to preserve v0 == v1. The target expander is able to
6635 rely on this to determine if we're permuting a single input operand. */
6636 if (rtx_equal_p (v0, v1))
6638 if (!insn_operand_matches (icode, 1, v0))
6639 v0 = force_reg (tmode, v0);
6640 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6641 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6643 create_fixed_operand (&ops[1], v0);
6644 create_fixed_operand (&ops[2], v0);
6646 else
6648 create_input_operand (&ops[1], v0, tmode);
6649 create_input_operand (&ops[2], v1, tmode);
6652 if (maybe_expand_insn (icode, 4, ops))
6653 return ops[0].value;
6654 return NULL_RTX;
6657 /* Generate instructions for vec_perm optab given its mode
6658 and three operands. */
6661 expand_vec_perm (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6663 enum insn_code icode;
6664 machine_mode qimode;
6665 unsigned int i, w, e, u;
6666 rtx tmp, sel_qi = NULL;
6667 rtvec vec;
6669 if (!target || GET_MODE (target) != mode)
6670 target = gen_reg_rtx (mode);
6672 w = GET_MODE_SIZE (mode);
6673 e = GET_MODE_NUNITS (mode);
6674 u = GET_MODE_UNIT_SIZE (mode);
6676 /* Set QIMODE to a different vector mode with byte elements.
6677 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6678 qimode = VOIDmode;
6679 if (GET_MODE_INNER (mode) != QImode)
6681 qimode = mode_for_vector (QImode, w);
6682 if (!VECTOR_MODE_P (qimode))
6683 qimode = VOIDmode;
6686 /* If the input is a constant, expand it specially. */
6687 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6688 if (GET_CODE (sel) == CONST_VECTOR)
6690 icode = direct_optab_handler (vec_perm_const_optab, mode);
6691 if (icode != CODE_FOR_nothing)
6693 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6694 if (tmp)
6695 return tmp;
6698 /* Fall back to a constant byte-based permutation. */
6699 if (qimode != VOIDmode)
6701 vec = rtvec_alloc (w);
6702 for (i = 0; i < e; ++i)
6704 unsigned int j, this_e;
6706 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6707 this_e &= 2 * e - 1;
6708 this_e *= u;
6710 for (j = 0; j < u; ++j)
6711 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
6713 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
6715 icode = direct_optab_handler (vec_perm_const_optab, qimode);
6716 if (icode != CODE_FOR_nothing)
6718 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6719 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6720 gen_lowpart (qimode, v1), sel_qi);
6721 if (tmp)
6722 return gen_lowpart (mode, tmp);
6727 /* Otherwise expand as a fully variable permuation. */
6728 icode = direct_optab_handler (vec_perm_optab, mode);
6729 if (icode != CODE_FOR_nothing)
6731 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6732 if (tmp)
6733 return tmp;
6736 /* As a special case to aid several targets, lower the element-based
6737 permutation to a byte-based permutation and try again. */
6738 if (qimode == VOIDmode)
6739 return NULL_RTX;
6740 icode = direct_optab_handler (vec_perm_optab, qimode);
6741 if (icode == CODE_FOR_nothing)
6742 return NULL_RTX;
6744 if (sel_qi == NULL)
6746 /* Multiply each element by its byte size. */
6747 machine_mode selmode = GET_MODE (sel);
6748 if (u == 2)
6749 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6750 sel, 0, OPTAB_DIRECT);
6751 else
6752 sel = expand_simple_binop (selmode, ASHIFT, sel,
6753 GEN_INT (exact_log2 (u)),
6754 sel, 0, OPTAB_DIRECT);
6755 gcc_assert (sel != NULL);
6757 /* Broadcast the low byte each element into each of its bytes. */
6758 vec = rtvec_alloc (w);
6759 for (i = 0; i < w; ++i)
6761 int this_e = i / u * u;
6762 if (BYTES_BIG_ENDIAN)
6763 this_e += u - 1;
6764 RTVEC_ELT (vec, i) = GEN_INT (this_e);
6766 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6767 sel = gen_lowpart (qimode, sel);
6768 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
6769 gcc_assert (sel != NULL);
6771 /* Add the byte offset to each byte element. */
6772 /* Note that the definition of the indicies here is memory ordering,
6773 so there should be no difference between big and little endian. */
6774 vec = rtvec_alloc (w);
6775 for (i = 0; i < w; ++i)
6776 RTVEC_ELT (vec, i) = GEN_INT (i % u);
6777 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6778 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6779 sel, 0, OPTAB_DIRECT);
6780 gcc_assert (sel_qi != NULL);
6783 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6784 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6785 gen_lowpart (qimode, v1), sel_qi);
6786 if (tmp)
6787 tmp = gen_lowpart (mode, tmp);
6788 return tmp;
6791 /* Return insn code for a conditional operator with a comparison in
6792 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
6794 static inline enum insn_code
6795 get_vcond_icode (machine_mode vmode, machine_mode cmode, bool uns)
6797 enum insn_code icode = CODE_FOR_nothing;
6798 if (uns)
6799 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
6800 else
6801 icode = convert_optab_handler (vcond_optab, vmode, cmode);
6802 return icode;
6805 /* Return TRUE iff, appropriate vector insns are available
6806 for vector cond expr with vector type VALUE_TYPE and a comparison
6807 with operand vector types in CMP_OP_TYPE. */
6809 bool
6810 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
6812 machine_mode value_mode = TYPE_MODE (value_type);
6813 machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
6814 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
6815 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
6816 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
6817 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
6818 return false;
6819 return true;
6822 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6823 three operands. */
6826 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6827 rtx target)
6829 struct expand_operand ops[6];
6830 enum insn_code icode;
6831 rtx comparison, rtx_op1, rtx_op2;
6832 machine_mode mode = TYPE_MODE (vec_cond_type);
6833 machine_mode cmp_op_mode;
6834 bool unsignedp;
6835 tree op0a, op0b;
6836 enum tree_code tcode;
6838 if (COMPARISON_CLASS_P (op0))
6840 op0a = TREE_OPERAND (op0, 0);
6841 op0b = TREE_OPERAND (op0, 1);
6842 tcode = TREE_CODE (op0);
6844 else
6846 /* Fake op0 < 0. */
6847 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (op0)));
6848 op0a = op0;
6849 op0b = build_zero_cst (TREE_TYPE (op0));
6850 tcode = LT_EXPR;
6852 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6853 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
6856 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
6857 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
6859 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
6860 if (icode == CODE_FOR_nothing)
6861 return 0;
6863 comparison = vector_compare_rtx (tcode, op0a, op0b, unsignedp, icode);
6864 rtx_op1 = expand_normal (op1);
6865 rtx_op2 = expand_normal (op2);
6867 create_output_operand (&ops[0], target, mode);
6868 create_input_operand (&ops[1], rtx_op1, mode);
6869 create_input_operand (&ops[2], rtx_op2, mode);
6870 create_fixed_operand (&ops[3], comparison);
6871 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6872 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6873 expand_insn (icode, 6, ops);
6874 return ops[0].value;
6877 /* Return non-zero if a highpart multiply is supported of can be synthisized.
6878 For the benefit of expand_mult_highpart, the return value is 1 for direct,
6879 2 for even/odd widening, and 3 for hi/lo widening. */
6882 can_mult_highpart_p (machine_mode mode, bool uns_p)
6884 optab op;
6885 unsigned char *sel;
6886 unsigned i, nunits;
6888 op = uns_p ? umul_highpart_optab : smul_highpart_optab;
6889 if (optab_handler (op, mode) != CODE_FOR_nothing)
6890 return 1;
6892 /* If the mode is an integral vector, synth from widening operations. */
6893 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
6894 return 0;
6896 nunits = GET_MODE_NUNITS (mode);
6897 sel = XALLOCAVEC (unsigned char, nunits);
6899 op = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6900 if (optab_handler (op, mode) != CODE_FOR_nothing)
6902 op = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6903 if (optab_handler (op, mode) != CODE_FOR_nothing)
6905 for (i = 0; i < nunits; ++i)
6906 sel[i] = !BYTES_BIG_ENDIAN + (i & ~1) + ((i & 1) ? nunits : 0);
6907 if (can_vec_perm_p (mode, false, sel))
6908 return 2;
6912 op = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6913 if (optab_handler (op, mode) != CODE_FOR_nothing)
6915 op = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6916 if (optab_handler (op, mode) != CODE_FOR_nothing)
6918 for (i = 0; i < nunits; ++i)
6919 sel[i] = 2 * i + (BYTES_BIG_ENDIAN ? 0 : 1);
6920 if (can_vec_perm_p (mode, false, sel))
6921 return 3;
6925 return 0;
6928 /* Expand a highpart multiply. */
6931 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
6932 rtx target, bool uns_p)
6934 struct expand_operand eops[3];
6935 enum insn_code icode;
6936 int method, i, nunits;
6937 machine_mode wmode;
6938 rtx m1, m2, perm;
6939 optab tab1, tab2;
6940 rtvec v;
6942 method = can_mult_highpart_p (mode, uns_p);
6943 switch (method)
6945 case 0:
6946 return NULL_RTX;
6947 case 1:
6948 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6949 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6950 OPTAB_LIB_WIDEN);
6951 case 2:
6952 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6953 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6954 break;
6955 case 3:
6956 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6957 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6958 if (BYTES_BIG_ENDIAN)
6960 optab t = tab1;
6961 tab1 = tab2;
6962 tab2 = t;
6964 break;
6965 default:
6966 gcc_unreachable ();
6969 icode = optab_handler (tab1, mode);
6970 nunits = GET_MODE_NUNITS (mode);
6971 wmode = insn_data[icode].operand[0].mode;
6972 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
6973 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
6975 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6976 create_input_operand (&eops[1], op0, mode);
6977 create_input_operand (&eops[2], op1, mode);
6978 expand_insn (icode, 3, eops);
6979 m1 = gen_lowpart (mode, eops[0].value);
6981 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6982 create_input_operand (&eops[1], op0, mode);
6983 create_input_operand (&eops[2], op1, mode);
6984 expand_insn (optab_handler (tab2, mode), 3, eops);
6985 m2 = gen_lowpart (mode, eops[0].value);
6987 v = rtvec_alloc (nunits);
6988 if (method == 2)
6990 for (i = 0; i < nunits; ++i)
6991 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
6992 + ((i & 1) ? nunits : 0));
6994 else
6996 for (i = 0; i < nunits; ++i)
6997 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
6999 perm = gen_rtx_CONST_VECTOR (mode, v);
7001 return expand_vec_perm (mode, m1, m2, perm, target);
7004 /* Return true if target supports vector masked load/store for mode. */
7005 bool
7006 can_vec_mask_load_store_p (machine_mode mode, bool is_load)
7008 optab op = is_load ? maskload_optab : maskstore_optab;
7009 machine_mode vmode;
7010 unsigned int vector_sizes;
7012 /* If mode is vector mode, check it directly. */
7013 if (VECTOR_MODE_P (mode))
7014 return optab_handler (op, mode) != CODE_FOR_nothing;
7016 /* Otherwise, return true if there is some vector mode with
7017 the mask load/store supported. */
7019 /* See if there is any chance the mask load or store might be
7020 vectorized. If not, punt. */
7021 vmode = targetm.vectorize.preferred_simd_mode (mode);
7022 if (!VECTOR_MODE_P (vmode))
7023 return false;
7025 if (optab_handler (op, vmode) != CODE_FOR_nothing)
7026 return true;
7028 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
7029 while (vector_sizes != 0)
7031 unsigned int cur = 1 << floor_log2 (vector_sizes);
7032 vector_sizes &= ~cur;
7033 if (cur <= GET_MODE_SIZE (mode))
7034 continue;
7035 vmode = mode_for_vector (mode, cur / GET_MODE_SIZE (mode));
7036 if (VECTOR_MODE_P (vmode)
7037 && optab_handler (op, vmode) != CODE_FOR_nothing)
7038 return true;
7040 return false;
7043 /* Return true if there is a compare_and_swap pattern. */
7045 bool
7046 can_compare_and_swap_p (machine_mode mode, bool allow_libcall)
7048 enum insn_code icode;
7050 /* Check for __atomic_compare_and_swap. */
7051 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7052 if (icode != CODE_FOR_nothing)
7053 return true;
7055 /* Check for __sync_compare_and_swap. */
7056 icode = optab_handler (sync_compare_and_swap_optab, mode);
7057 if (icode != CODE_FOR_nothing)
7058 return true;
7059 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
7060 return true;
7062 /* No inline compare and swap. */
7063 return false;
7066 /* Return true if an atomic exchange can be performed. */
7068 bool
7069 can_atomic_exchange_p (machine_mode mode, bool allow_libcall)
7071 enum insn_code icode;
7073 /* Check for __atomic_exchange. */
7074 icode = direct_optab_handler (atomic_exchange_optab, mode);
7075 if (icode != CODE_FOR_nothing)
7076 return true;
7078 /* Don't check __sync_test_and_set, as on some platforms that
7079 has reduced functionality. Targets that really do support
7080 a proper exchange should simply be updated to the __atomics. */
7082 return can_compare_and_swap_p (mode, allow_libcall);
7086 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
7087 pattern. */
7089 static void
7090 find_cc_set (rtx x, const_rtx pat, void *data)
7092 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
7093 && GET_CODE (pat) == SET)
7095 rtx *p_cc_reg = (rtx *) data;
7096 gcc_assert (!*p_cc_reg);
7097 *p_cc_reg = x;
7101 /* This is a helper function for the other atomic operations. This function
7102 emits a loop that contains SEQ that iterates until a compare-and-swap
7103 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7104 a set of instructions that takes a value from OLD_REG as an input and
7105 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7106 set to the current contents of MEM. After SEQ, a compare-and-swap will
7107 attempt to update MEM with NEW_REG. The function returns true when the
7108 loop was generated successfully. */
7110 static bool
7111 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7113 machine_mode mode = GET_MODE (mem);
7114 rtx_code_label *label;
7115 rtx cmp_reg, success, oldval;
7117 /* The loop we want to generate looks like
7119 cmp_reg = mem;
7120 label:
7121 old_reg = cmp_reg;
7122 seq;
7123 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
7124 if (success)
7125 goto label;
7127 Note that we only do the plain load from memory once. Subsequent
7128 iterations use the value loaded by the compare-and-swap pattern. */
7130 label = gen_label_rtx ();
7131 cmp_reg = gen_reg_rtx (mode);
7133 emit_move_insn (cmp_reg, mem);
7134 emit_label (label);
7135 emit_move_insn (old_reg, cmp_reg);
7136 if (seq)
7137 emit_insn (seq);
7139 success = NULL_RTX;
7140 oldval = cmp_reg;
7141 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
7142 new_reg, false, MEMMODEL_SEQ_CST,
7143 MEMMODEL_RELAXED))
7144 return false;
7146 if (oldval != cmp_reg)
7147 emit_move_insn (cmp_reg, oldval);
7149 /* Mark this jump predicted not taken. */
7150 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
7151 GET_MODE (success), 1, label, 0);
7152 return true;
7156 /* This function tries to emit an atomic_exchange intruction. VAL is written
7157 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
7158 using TARGET if possible. */
7160 static rtx
7161 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7163 machine_mode mode = GET_MODE (mem);
7164 enum insn_code icode;
7166 /* If the target supports the exchange directly, great. */
7167 icode = direct_optab_handler (atomic_exchange_optab, mode);
7168 if (icode != CODE_FOR_nothing)
7170 struct expand_operand ops[4];
7172 create_output_operand (&ops[0], target, mode);
7173 create_fixed_operand (&ops[1], mem);
7174 create_input_operand (&ops[2], val, mode);
7175 create_integer_operand (&ops[3], model);
7176 if (maybe_expand_insn (icode, 4, ops))
7177 return ops[0].value;
7180 return NULL_RTX;
7183 /* This function tries to implement an atomic exchange operation using
7184 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7185 The previous contents of *MEM are returned, using TARGET if possible.
7186 Since this instructionn is an acquire barrier only, stronger memory
7187 models may require additional barriers to be emitted. */
7189 static rtx
7190 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7191 enum memmodel model)
7193 machine_mode mode = GET_MODE (mem);
7194 enum insn_code icode;
7195 rtx_insn *last_insn = get_last_insn ();
7197 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7199 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7200 exists, and the memory model is stronger than acquire, add a release
7201 barrier before the instruction. */
7203 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST
7204 || (model & MEMMODEL_MASK) == MEMMODEL_RELEASE
7205 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
7206 expand_mem_thread_fence (model);
7208 if (icode != CODE_FOR_nothing)
7210 struct expand_operand ops[3];
7211 create_output_operand (&ops[0], target, mode);
7212 create_fixed_operand (&ops[1], mem);
7213 create_input_operand (&ops[2], val, mode);
7214 if (maybe_expand_insn (icode, 3, ops))
7215 return ops[0].value;
7218 /* If an external test-and-set libcall is provided, use that instead of
7219 any external compare-and-swap that we might get from the compare-and-
7220 swap-loop expansion later. */
7221 if (!can_compare_and_swap_p (mode, false))
7223 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7224 if (libfunc != NULL)
7226 rtx addr;
7228 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7229 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7230 mode, 2, addr, ptr_mode,
7231 val, mode);
7235 /* If the test_and_set can't be emitted, eliminate any barrier that might
7236 have been emitted. */
7237 delete_insns_since (last_insn);
7238 return NULL_RTX;
7241 /* This function tries to implement an atomic exchange operation using a
7242 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7243 *MEM are returned, using TARGET if possible. No memory model is required
7244 since a compare_and_swap loop is seq-cst. */
7246 static rtx
7247 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7249 machine_mode mode = GET_MODE (mem);
7251 if (can_compare_and_swap_p (mode, true))
7253 if (!target || !register_operand (target, mode))
7254 target = gen_reg_rtx (mode);
7255 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7256 return target;
7259 return NULL_RTX;
7262 /* This function tries to implement an atomic test-and-set operation
7263 using the atomic_test_and_set instruction pattern. A boolean value
7264 is returned from the operation, using TARGET if possible. */
7266 #ifndef HAVE_atomic_test_and_set
7267 #define HAVE_atomic_test_and_set 0
7268 #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
7269 #endif
7271 static rtx
7272 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7274 machine_mode pat_bool_mode;
7275 struct expand_operand ops[3];
7277 if (!HAVE_atomic_test_and_set)
7278 return NULL_RTX;
7280 /* While we always get QImode from __atomic_test_and_set, we get
7281 other memory modes from __sync_lock_test_and_set. Note that we
7282 use no endian adjustment here. This matches the 4.6 behavior
7283 in the Sparc backend. */
7284 gcc_checking_assert
7285 (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
7286 if (GET_MODE (mem) != QImode)
7287 mem = adjust_address_nv (mem, QImode, 0);
7289 pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
7290 create_output_operand (&ops[0], target, pat_bool_mode);
7291 create_fixed_operand (&ops[1], mem);
7292 create_integer_operand (&ops[2], model);
7294 if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
7295 return ops[0].value;
7296 return NULL_RTX;
7299 /* This function expands the legacy _sync_lock test_and_set operation which is
7300 generally an atomic exchange. Some limited targets only allow the
7301 constant 1 to be stored. This is an ACQUIRE operation.
7303 TARGET is an optional place to stick the return value.
7304 MEM is where VAL is stored. */
7307 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7309 rtx ret;
7311 /* Try an atomic_exchange first. */
7312 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_ACQUIRE);
7313 if (ret)
7314 return ret;
7316 ret = maybe_emit_sync_lock_test_and_set (target, mem, val, MEMMODEL_ACQUIRE);
7317 if (ret)
7318 return ret;
7320 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7321 if (ret)
7322 return ret;
7324 /* If there are no other options, try atomic_test_and_set if the value
7325 being stored is 1. */
7326 if (val == const1_rtx)
7327 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_ACQUIRE);
7329 return ret;
7332 /* This function expands the atomic test_and_set operation:
7333 atomically store a boolean TRUE into MEM and return the previous value.
7335 MEMMODEL is the memory model variant to use.
7336 TARGET is an optional place to stick the return value. */
7339 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7341 machine_mode mode = GET_MODE (mem);
7342 rtx ret, trueval, subtarget;
7344 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7345 if (ret)
7346 return ret;
7348 /* Be binary compatible with non-default settings of trueval, and different
7349 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7350 another only has atomic-exchange. */
7351 if (targetm.atomic_test_and_set_trueval == 1)
7353 trueval = const1_rtx;
7354 subtarget = target ? target : gen_reg_rtx (mode);
7356 else
7358 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7359 subtarget = gen_reg_rtx (mode);
7362 /* Try the atomic-exchange optab... */
7363 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7365 /* ... then an atomic-compare-and-swap loop ... */
7366 if (!ret)
7367 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7369 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7370 if (!ret)
7371 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7373 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7374 things with the value 1. Thus we try again without trueval. */
7375 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7376 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7378 /* Failing all else, assume a single threaded environment and simply
7379 perform the operation. */
7380 if (!ret)
7382 /* If the result is ignored skip the move to target. */
7383 if (subtarget != const0_rtx)
7384 emit_move_insn (subtarget, mem);
7386 emit_move_insn (mem, trueval);
7387 ret = subtarget;
7390 /* Recall that have to return a boolean value; rectify if trueval
7391 is not exactly one. */
7392 if (targetm.atomic_test_and_set_trueval != 1)
7393 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7395 return ret;
7398 /* This function expands the atomic exchange operation:
7399 atomically store VAL in MEM and return the previous value in MEM.
7401 MEMMODEL is the memory model variant to use.
7402 TARGET is an optional place to stick the return value. */
7405 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7407 rtx ret;
7409 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7411 /* Next try a compare-and-swap loop for the exchange. */
7412 if (!ret)
7413 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7415 return ret;
7418 /* This function expands the atomic compare exchange operation:
7420 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7421 *PTARGET_OVAL is an optional place to store the old value from memory.
7422 Both target parameters may be NULL to indicate that we do not care about
7423 that return value. Both target parameters are updated on success to
7424 the actual location of the corresponding result.
7426 MEMMODEL is the memory model variant to use.
7428 The return value of the function is true for success. */
7430 bool
7431 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7432 rtx mem, rtx expected, rtx desired,
7433 bool is_weak, enum memmodel succ_model,
7434 enum memmodel fail_model)
7436 machine_mode mode = GET_MODE (mem);
7437 struct expand_operand ops[8];
7438 enum insn_code icode;
7439 rtx target_oval, target_bool = NULL_RTX;
7440 rtx libfunc;
7442 /* Load expected into a register for the compare and swap. */
7443 if (MEM_P (expected))
7444 expected = copy_to_reg (expected);
7446 /* Make sure we always have some place to put the return oldval.
7447 Further, make sure that place is distinct from the input expected,
7448 just in case we need that path down below. */
7449 if (ptarget_oval == NULL
7450 || (target_oval = *ptarget_oval) == NULL
7451 || reg_overlap_mentioned_p (expected, target_oval))
7452 target_oval = gen_reg_rtx (mode);
7454 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7455 if (icode != CODE_FOR_nothing)
7457 machine_mode bool_mode = insn_data[icode].operand[0].mode;
7459 /* Make sure we always have a place for the bool operand. */
7460 if (ptarget_bool == NULL
7461 || (target_bool = *ptarget_bool) == NULL
7462 || GET_MODE (target_bool) != bool_mode)
7463 target_bool = gen_reg_rtx (bool_mode);
7465 /* Emit the compare_and_swap. */
7466 create_output_operand (&ops[0], target_bool, bool_mode);
7467 create_output_operand (&ops[1], target_oval, mode);
7468 create_fixed_operand (&ops[2], mem);
7469 create_input_operand (&ops[3], expected, mode);
7470 create_input_operand (&ops[4], desired, mode);
7471 create_integer_operand (&ops[5], is_weak);
7472 create_integer_operand (&ops[6], succ_model);
7473 create_integer_operand (&ops[7], fail_model);
7474 if (maybe_expand_insn (icode, 8, ops))
7476 /* Return success/failure. */
7477 target_bool = ops[0].value;
7478 target_oval = ops[1].value;
7479 goto success;
7483 /* Otherwise fall back to the original __sync_val_compare_and_swap
7484 which is always seq-cst. */
7485 icode = optab_handler (sync_compare_and_swap_optab, mode);
7486 if (icode != CODE_FOR_nothing)
7488 rtx cc_reg;
7490 create_output_operand (&ops[0], target_oval, mode);
7491 create_fixed_operand (&ops[1], mem);
7492 create_input_operand (&ops[2], expected, mode);
7493 create_input_operand (&ops[3], desired, mode);
7494 if (!maybe_expand_insn (icode, 4, ops))
7495 return false;
7497 target_oval = ops[0].value;
7499 /* If the caller isn't interested in the boolean return value,
7500 skip the computation of it. */
7501 if (ptarget_bool == NULL)
7502 goto success;
7504 /* Otherwise, work out if the compare-and-swap succeeded. */
7505 cc_reg = NULL_RTX;
7506 if (have_insn_for (COMPARE, CCmode))
7507 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7508 if (cc_reg)
7510 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7511 const0_rtx, VOIDmode, 0, 1);
7512 goto success;
7514 goto success_bool_from_val;
7517 /* Also check for library support for __sync_val_compare_and_swap. */
7518 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7519 if (libfunc != NULL)
7521 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7522 target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7523 mode, 3, addr, ptr_mode,
7524 expected, mode, desired, mode);
7526 /* Compute the boolean return value only if requested. */
7527 if (ptarget_bool)
7528 goto success_bool_from_val;
7529 else
7530 goto success;
7533 /* Failure. */
7534 return false;
7536 success_bool_from_val:
7537 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7538 expected, VOIDmode, 1, 1);
7539 success:
7540 /* Make sure that the oval output winds up where the caller asked. */
7541 if (ptarget_oval)
7542 *ptarget_oval = target_oval;
7543 if (ptarget_bool)
7544 *ptarget_bool = target_bool;
7545 return true;
7548 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7550 static void
7551 expand_asm_memory_barrier (void)
7553 rtx asm_op, clob;
7555 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7556 rtvec_alloc (0), rtvec_alloc (0),
7557 rtvec_alloc (0), UNKNOWN_LOCATION);
7558 MEM_VOLATILE_P (asm_op) = 1;
7560 clob = gen_rtx_SCRATCH (VOIDmode);
7561 clob = gen_rtx_MEM (BLKmode, clob);
7562 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7564 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7567 /* This routine will either emit the mem_thread_fence pattern or issue a
7568 sync_synchronize to generate a fence for memory model MEMMODEL. */
7570 #ifndef HAVE_mem_thread_fence
7571 # define HAVE_mem_thread_fence 0
7572 # define gen_mem_thread_fence(x) (gcc_unreachable (), NULL_RTX)
7573 #endif
7574 #ifndef HAVE_memory_barrier
7575 # define HAVE_memory_barrier 0
7576 # define gen_memory_barrier() (gcc_unreachable (), NULL_RTX)
7577 #endif
7579 void
7580 expand_mem_thread_fence (enum memmodel model)
7582 if (HAVE_mem_thread_fence)
7583 emit_insn (gen_mem_thread_fence (GEN_INT (model)));
7584 else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
7586 if (HAVE_memory_barrier)
7587 emit_insn (gen_memory_barrier ());
7588 else if (synchronize_libfunc != NULL_RTX)
7589 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7590 else
7591 expand_asm_memory_barrier ();
7595 /* This routine will either emit the mem_signal_fence pattern or issue a
7596 sync_synchronize to generate a fence for memory model MEMMODEL. */
7598 #ifndef HAVE_mem_signal_fence
7599 # define HAVE_mem_signal_fence 0
7600 # define gen_mem_signal_fence(x) (gcc_unreachable (), NULL_RTX)
7601 #endif
7603 void
7604 expand_mem_signal_fence (enum memmodel model)
7606 if (HAVE_mem_signal_fence)
7607 emit_insn (gen_mem_signal_fence (GEN_INT (model)));
7608 else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
7610 /* By default targets are coherent between a thread and the signal
7611 handler running on the same thread. Thus this really becomes a
7612 compiler barrier, in that stores must not be sunk past
7613 (or raised above) a given point. */
7614 expand_asm_memory_barrier ();
7618 /* This function expands the atomic load operation:
7619 return the atomically loaded value in MEM.
7621 MEMMODEL is the memory model variant to use.
7622 TARGET is an option place to stick the return value. */
7625 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7627 machine_mode mode = GET_MODE (mem);
7628 enum insn_code icode;
7630 /* If the target supports the load directly, great. */
7631 icode = direct_optab_handler (atomic_load_optab, mode);
7632 if (icode != CODE_FOR_nothing)
7634 struct expand_operand ops[3];
7636 create_output_operand (&ops[0], target, mode);
7637 create_fixed_operand (&ops[1], mem);
7638 create_integer_operand (&ops[2], model);
7639 if (maybe_expand_insn (icode, 3, ops))
7640 return ops[0].value;
7643 /* If the size of the object is greater than word size on this target,
7644 then we assume that a load will not be atomic. */
7645 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7647 /* Issue val = compare_and_swap (mem, 0, 0).
7648 This may cause the occasional harmless store of 0 when the value is
7649 already 0, but it seems to be OK according to the standards guys. */
7650 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7651 const0_rtx, false, model, model))
7652 return target;
7653 else
7654 /* Otherwise there is no atomic load, leave the library call. */
7655 return NULL_RTX;
7658 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7659 if (!target || target == const0_rtx)
7660 target = gen_reg_rtx (mode);
7662 /* For SEQ_CST, emit a barrier before the load. */
7663 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7664 expand_mem_thread_fence (model);
7666 emit_move_insn (target, mem);
7668 /* Emit the appropriate barrier after the load. */
7669 expand_mem_thread_fence (model);
7671 return target;
7674 /* This function expands the atomic store operation:
7675 Atomically store VAL in MEM.
7676 MEMMODEL is the memory model variant to use.
7677 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7678 function returns const0_rtx if a pattern was emitted. */
7681 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7683 machine_mode mode = GET_MODE (mem);
7684 enum insn_code icode;
7685 struct expand_operand ops[3];
7687 /* If the target supports the store directly, great. */
7688 icode = direct_optab_handler (atomic_store_optab, mode);
7689 if (icode != CODE_FOR_nothing)
7691 create_fixed_operand (&ops[0], mem);
7692 create_input_operand (&ops[1], val, mode);
7693 create_integer_operand (&ops[2], model);
7694 if (maybe_expand_insn (icode, 3, ops))
7695 return const0_rtx;
7698 /* If using __sync_lock_release is a viable alternative, try it. */
7699 if (use_release)
7701 icode = direct_optab_handler (sync_lock_release_optab, mode);
7702 if (icode != CODE_FOR_nothing)
7704 create_fixed_operand (&ops[0], mem);
7705 create_input_operand (&ops[1], const0_rtx, mode);
7706 if (maybe_expand_insn (icode, 2, ops))
7708 /* lock_release is only a release barrier. */
7709 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7710 expand_mem_thread_fence (model);
7711 return const0_rtx;
7716 /* If the size of the object is greater than word size on this target,
7717 a default store will not be atomic, Try a mem_exchange and throw away
7718 the result. If that doesn't work, don't do anything. */
7719 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7721 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7722 if (!target)
7723 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7724 if (target)
7725 return const0_rtx;
7726 else
7727 return NULL_RTX;
7730 /* Otherwise assume stores are atomic, and emit the proper barriers. */
7731 expand_mem_thread_fence (model);
7733 emit_move_insn (mem, val);
7735 /* For SEQ_CST, also emit a barrier after the store. */
7736 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7737 expand_mem_thread_fence (model);
7739 return const0_rtx;
7743 /* Structure containing the pointers and values required to process the
7744 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7746 struct atomic_op_functions
7748 direct_optab mem_fetch_before;
7749 direct_optab mem_fetch_after;
7750 direct_optab mem_no_result;
7751 optab fetch_before;
7752 optab fetch_after;
7753 direct_optab no_result;
7754 enum rtx_code reverse_code;
7758 /* Fill in structure pointed to by OP with the various optab entries for an
7759 operation of type CODE. */
7761 static void
7762 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7764 gcc_assert (op!= NULL);
7766 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7767 in the source code during compilation, and the optab entries are not
7768 computable until runtime. Fill in the values at runtime. */
7769 switch (code)
7771 case PLUS:
7772 op->mem_fetch_before = atomic_fetch_add_optab;
7773 op->mem_fetch_after = atomic_add_fetch_optab;
7774 op->mem_no_result = atomic_add_optab;
7775 op->fetch_before = sync_old_add_optab;
7776 op->fetch_after = sync_new_add_optab;
7777 op->no_result = sync_add_optab;
7778 op->reverse_code = MINUS;
7779 break;
7780 case MINUS:
7781 op->mem_fetch_before = atomic_fetch_sub_optab;
7782 op->mem_fetch_after = atomic_sub_fetch_optab;
7783 op->mem_no_result = atomic_sub_optab;
7784 op->fetch_before = sync_old_sub_optab;
7785 op->fetch_after = sync_new_sub_optab;
7786 op->no_result = sync_sub_optab;
7787 op->reverse_code = PLUS;
7788 break;
7789 case XOR:
7790 op->mem_fetch_before = atomic_fetch_xor_optab;
7791 op->mem_fetch_after = atomic_xor_fetch_optab;
7792 op->mem_no_result = atomic_xor_optab;
7793 op->fetch_before = sync_old_xor_optab;
7794 op->fetch_after = sync_new_xor_optab;
7795 op->no_result = sync_xor_optab;
7796 op->reverse_code = XOR;
7797 break;
7798 case AND:
7799 op->mem_fetch_before = atomic_fetch_and_optab;
7800 op->mem_fetch_after = atomic_and_fetch_optab;
7801 op->mem_no_result = atomic_and_optab;
7802 op->fetch_before = sync_old_and_optab;
7803 op->fetch_after = sync_new_and_optab;
7804 op->no_result = sync_and_optab;
7805 op->reverse_code = UNKNOWN;
7806 break;
7807 case IOR:
7808 op->mem_fetch_before = atomic_fetch_or_optab;
7809 op->mem_fetch_after = atomic_or_fetch_optab;
7810 op->mem_no_result = atomic_or_optab;
7811 op->fetch_before = sync_old_ior_optab;
7812 op->fetch_after = sync_new_ior_optab;
7813 op->no_result = sync_ior_optab;
7814 op->reverse_code = UNKNOWN;
7815 break;
7816 case NOT:
7817 op->mem_fetch_before = atomic_fetch_nand_optab;
7818 op->mem_fetch_after = atomic_nand_fetch_optab;
7819 op->mem_no_result = atomic_nand_optab;
7820 op->fetch_before = sync_old_nand_optab;
7821 op->fetch_after = sync_new_nand_optab;
7822 op->no_result = sync_nand_optab;
7823 op->reverse_code = UNKNOWN;
7824 break;
7825 default:
7826 gcc_unreachable ();
7830 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7831 using memory order MODEL. If AFTER is true the operation needs to return
7832 the value of *MEM after the operation, otherwise the previous value.
7833 TARGET is an optional place to place the result. The result is unused if
7834 it is const0_rtx.
7835 Return the result if there is a better sequence, otherwise NULL_RTX. */
7837 static rtx
7838 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7839 enum memmodel model, bool after)
7841 /* If the value is prefetched, or not used, it may be possible to replace
7842 the sequence with a native exchange operation. */
7843 if (!after || target == const0_rtx)
7845 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7846 if (code == AND && val == const0_rtx)
7848 if (target == const0_rtx)
7849 target = gen_reg_rtx (GET_MODE (mem));
7850 return maybe_emit_atomic_exchange (target, mem, val, model);
7853 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7854 if (code == IOR && val == constm1_rtx)
7856 if (target == const0_rtx)
7857 target = gen_reg_rtx (GET_MODE (mem));
7858 return maybe_emit_atomic_exchange (target, mem, val, model);
7862 return NULL_RTX;
7865 /* Try to emit an instruction for a specific operation varaition.
7866 OPTAB contains the OP functions.
7867 TARGET is an optional place to return the result. const0_rtx means unused.
7868 MEM is the memory location to operate on.
7869 VAL is the value to use in the operation.
7870 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7871 MODEL is the memory model, if used.
7872 AFTER is true if the returned result is the value after the operation. */
7874 static rtx
7875 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7876 rtx val, bool use_memmodel, enum memmodel model, bool after)
7878 machine_mode mode = GET_MODE (mem);
7879 struct expand_operand ops[4];
7880 enum insn_code icode;
7881 int op_counter = 0;
7882 int num_ops;
7884 /* Check to see if there is a result returned. */
7885 if (target == const0_rtx)
7887 if (use_memmodel)
7889 icode = direct_optab_handler (optab->mem_no_result, mode);
7890 create_integer_operand (&ops[2], model);
7891 num_ops = 3;
7893 else
7895 icode = direct_optab_handler (optab->no_result, mode);
7896 num_ops = 2;
7899 /* Otherwise, we need to generate a result. */
7900 else
7902 if (use_memmodel)
7904 icode = direct_optab_handler (after ? optab->mem_fetch_after
7905 : optab->mem_fetch_before, mode);
7906 create_integer_operand (&ops[3], model);
7907 num_ops = 4;
7909 else
7911 icode = optab_handler (after ? optab->fetch_after
7912 : optab->fetch_before, mode);
7913 num_ops = 3;
7915 create_output_operand (&ops[op_counter++], target, mode);
7917 if (icode == CODE_FOR_nothing)
7918 return NULL_RTX;
7920 create_fixed_operand (&ops[op_counter++], mem);
7921 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7922 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7924 if (maybe_expand_insn (icode, num_ops, ops))
7925 return (target == const0_rtx ? const0_rtx : ops[0].value);
7927 return NULL_RTX;
7931 /* This function expands an atomic fetch_OP or OP_fetch operation:
7932 TARGET is an option place to stick the return value. const0_rtx indicates
7933 the result is unused.
7934 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7935 CODE is the operation being performed (OP)
7936 MEMMODEL is the memory model variant to use.
7937 AFTER is true to return the result of the operation (OP_fetch).
7938 AFTER is false to return the value before the operation (fetch_OP).
7940 This function will *only* generate instructions if there is a direct
7941 optab. No compare and swap loops or libcalls will be generated. */
7943 static rtx
7944 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7945 enum rtx_code code, enum memmodel model,
7946 bool after)
7948 machine_mode mode = GET_MODE (mem);
7949 struct atomic_op_functions optab;
7950 rtx result;
7951 bool unused_result = (target == const0_rtx);
7953 get_atomic_op_for_code (&optab, code);
7955 /* Check to see if there are any better instructions. */
7956 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7957 if (result)
7958 return result;
7960 /* Check for the case where the result isn't used and try those patterns. */
7961 if (unused_result)
7963 /* Try the memory model variant first. */
7964 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7965 if (result)
7966 return result;
7968 /* Next try the old style withuot a memory model. */
7969 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7970 if (result)
7971 return result;
7973 /* There is no no-result pattern, so try patterns with a result. */
7974 target = NULL_RTX;
7977 /* Try the __atomic version. */
7978 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
7979 if (result)
7980 return result;
7982 /* Try the older __sync version. */
7983 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
7984 if (result)
7985 return result;
7987 /* If the fetch value can be calculated from the other variation of fetch,
7988 try that operation. */
7989 if (after || unused_result || optab.reverse_code != UNKNOWN)
7991 /* Try the __atomic version, then the older __sync version. */
7992 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
7993 if (!result)
7994 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
7996 if (result)
7998 /* If the result isn't used, no need to do compensation code. */
7999 if (unused_result)
8000 return result;
8002 /* Issue compensation code. Fetch_after == fetch_before OP val.
8003 Fetch_before == after REVERSE_OP val. */
8004 if (!after)
8005 code = optab.reverse_code;
8006 if (code == NOT)
8008 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
8009 true, OPTAB_LIB_WIDEN);
8010 result = expand_simple_unop (mode, NOT, result, target, true);
8012 else
8013 result = expand_simple_binop (mode, code, result, val, target,
8014 true, OPTAB_LIB_WIDEN);
8015 return result;
8019 /* No direct opcode can be generated. */
8020 return NULL_RTX;
8025 /* This function expands an atomic fetch_OP or OP_fetch operation:
8026 TARGET is an option place to stick the return value. const0_rtx indicates
8027 the result is unused.
8028 atomically fetch MEM, perform the operation with VAL and return it to MEM.
8029 CODE is the operation being performed (OP)
8030 MEMMODEL is the memory model variant to use.
8031 AFTER is true to return the result of the operation (OP_fetch).
8032 AFTER is false to return the value before the operation (fetch_OP). */
8034 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
8035 enum memmodel model, bool after)
8037 machine_mode mode = GET_MODE (mem);
8038 rtx result;
8039 bool unused_result = (target == const0_rtx);
8041 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
8042 after);
8044 if (result)
8045 return result;
8047 /* Add/sub can be implemented by doing the reverse operation with -(val). */
8048 if (code == PLUS || code == MINUS)
8050 rtx tmp;
8051 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
8053 start_sequence ();
8054 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
8055 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
8056 model, after);
8057 if (result)
8059 /* PLUS worked so emit the insns and return. */
8060 tmp = get_insns ();
8061 end_sequence ();
8062 emit_insn (tmp);
8063 return result;
8066 /* PLUS did not work, so throw away the negation code and continue. */
8067 end_sequence ();
8070 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
8071 if (!can_compare_and_swap_p (mode, false))
8073 rtx libfunc;
8074 bool fixup = false;
8075 enum rtx_code orig_code = code;
8076 struct atomic_op_functions optab;
8078 get_atomic_op_for_code (&optab, code);
8079 libfunc = optab_libfunc (after ? optab.fetch_after
8080 : optab.fetch_before, mode);
8081 if (libfunc == NULL
8082 && (after || unused_result || optab.reverse_code != UNKNOWN))
8084 fixup = true;
8085 if (!after)
8086 code = optab.reverse_code;
8087 libfunc = optab_libfunc (after ? optab.fetch_before
8088 : optab.fetch_after, mode);
8090 if (libfunc != NULL)
8092 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
8093 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
8094 2, addr, ptr_mode, val, mode);
8096 if (!unused_result && fixup)
8097 result = expand_simple_binop (mode, code, result, val, target,
8098 true, OPTAB_LIB_WIDEN);
8099 return result;
8102 /* We need the original code for any further attempts. */
8103 code = orig_code;
8106 /* If nothing else has succeeded, default to a compare and swap loop. */
8107 if (can_compare_and_swap_p (mode, true))
8109 rtx_insn *insn;
8110 rtx t0 = gen_reg_rtx (mode), t1;
8112 start_sequence ();
8114 /* If the result is used, get a register for it. */
8115 if (!unused_result)
8117 if (!target || !register_operand (target, mode))
8118 target = gen_reg_rtx (mode);
8119 /* If fetch_before, copy the value now. */
8120 if (!after)
8121 emit_move_insn (target, t0);
8123 else
8124 target = const0_rtx;
8126 t1 = t0;
8127 if (code == NOT)
8129 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
8130 true, OPTAB_LIB_WIDEN);
8131 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
8133 else
8134 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
8135 OPTAB_LIB_WIDEN);
8137 /* For after, copy the value now. */
8138 if (!unused_result && after)
8139 emit_move_insn (target, t1);
8140 insn = get_insns ();
8141 end_sequence ();
8143 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
8144 return target;
8147 return NULL_RTX;
8150 /* Return true if OPERAND is suitable for operand number OPNO of
8151 instruction ICODE. */
8153 bool
8154 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
8156 return (!insn_data[(int) icode].operand[opno].predicate
8157 || (insn_data[(int) icode].operand[opno].predicate
8158 (operand, insn_data[(int) icode].operand[opno].mode)));
8161 /* TARGET is a target of a multiword operation that we are going to
8162 implement as a series of word-mode operations. Return true if
8163 TARGET is suitable for this purpose. */
8165 bool
8166 valid_multiword_target_p (rtx target)
8168 machine_mode mode;
8169 int i;
8171 mode = GET_MODE (target);
8172 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
8173 if (!validate_subreg (word_mode, mode, target, i))
8174 return false;
8175 return true;
8178 /* Like maybe_legitimize_operand, but do not change the code of the
8179 current rtx value. */
8181 static bool
8182 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8183 struct expand_operand *op)
8185 /* See if the operand matches in its current form. */
8186 if (insn_operand_matches (icode, opno, op->value))
8187 return true;
8189 /* If the operand is a memory whose address has no side effects,
8190 try forcing the address into a non-virtual pseudo register.
8191 The check for side effects is important because copy_to_mode_reg
8192 cannot handle things like auto-modified addresses. */
8193 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8195 rtx addr, mem;
8197 mem = op->value;
8198 addr = XEXP (mem, 0);
8199 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8200 && !side_effects_p (addr))
8202 rtx_insn *last;
8203 machine_mode mode;
8205 last = get_last_insn ();
8206 mode = get_address_mode (mem);
8207 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8208 if (insn_operand_matches (icode, opno, mem))
8210 op->value = mem;
8211 return true;
8213 delete_insns_since (last);
8217 return false;
8220 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8221 on success, storing the new operand value back in OP. */
8223 static bool
8224 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8225 struct expand_operand *op)
8227 machine_mode mode, imode;
8228 bool old_volatile_ok, result;
8230 mode = op->mode;
8231 switch (op->type)
8233 case EXPAND_FIXED:
8234 old_volatile_ok = volatile_ok;
8235 volatile_ok = true;
8236 result = maybe_legitimize_operand_same_code (icode, opno, op);
8237 volatile_ok = old_volatile_ok;
8238 return result;
8240 case EXPAND_OUTPUT:
8241 gcc_assert (mode != VOIDmode);
8242 if (op->value
8243 && op->value != const0_rtx
8244 && GET_MODE (op->value) == mode
8245 && maybe_legitimize_operand_same_code (icode, opno, op))
8246 return true;
8248 op->value = gen_reg_rtx (mode);
8249 break;
8251 case EXPAND_INPUT:
8252 input:
8253 gcc_assert (mode != VOIDmode);
8254 gcc_assert (GET_MODE (op->value) == VOIDmode
8255 || GET_MODE (op->value) == mode);
8256 if (maybe_legitimize_operand_same_code (icode, opno, op))
8257 return true;
8259 op->value = copy_to_mode_reg (mode, op->value);
8260 break;
8262 case EXPAND_CONVERT_TO:
8263 gcc_assert (mode != VOIDmode);
8264 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8265 goto input;
8267 case EXPAND_CONVERT_FROM:
8268 if (GET_MODE (op->value) != VOIDmode)
8269 mode = GET_MODE (op->value);
8270 else
8271 /* The caller must tell us what mode this value has. */
8272 gcc_assert (mode != VOIDmode);
8274 imode = insn_data[(int) icode].operand[opno].mode;
8275 if (imode != VOIDmode && imode != mode)
8277 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8278 mode = imode;
8280 goto input;
8282 case EXPAND_ADDRESS:
8283 gcc_assert (mode != VOIDmode);
8284 op->value = convert_memory_address (mode, op->value);
8285 goto input;
8287 case EXPAND_INTEGER:
8288 mode = insn_data[(int) icode].operand[opno].mode;
8289 if (mode != VOIDmode && const_int_operand (op->value, mode))
8290 goto input;
8291 break;
8293 return insn_operand_matches (icode, opno, op->value);
8296 /* Make OP describe an input operand that should have the same value
8297 as VALUE, after any mode conversion that the target might request.
8298 TYPE is the type of VALUE. */
8300 void
8301 create_convert_operand_from_type (struct expand_operand *op,
8302 rtx value, tree type)
8304 create_convert_operand_from (op, value, TYPE_MODE (type),
8305 TYPE_UNSIGNED (type));
8308 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8309 of instruction ICODE. Return true on success, leaving the new operand
8310 values in the OPS themselves. Emit no code on failure. */
8312 bool
8313 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8314 unsigned int nops, struct expand_operand *ops)
8316 rtx_insn *last;
8317 unsigned int i;
8319 last = get_last_insn ();
8320 for (i = 0; i < nops; i++)
8321 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8323 delete_insns_since (last);
8324 return false;
8326 return true;
8329 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8330 as its operands. Return the instruction pattern on success,
8331 and emit any necessary set-up code. Return null and emit no
8332 code on failure. */
8335 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8336 struct expand_operand *ops)
8338 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8339 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8340 return NULL_RTX;
8342 switch (nops)
8344 case 1:
8345 return GEN_FCN (icode) (ops[0].value);
8346 case 2:
8347 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8348 case 3:
8349 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8350 case 4:
8351 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8352 ops[3].value);
8353 case 5:
8354 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8355 ops[3].value, ops[4].value);
8356 case 6:
8357 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8358 ops[3].value, ops[4].value, ops[5].value);
8359 case 7:
8360 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8361 ops[3].value, ops[4].value, ops[5].value,
8362 ops[6].value);
8363 case 8:
8364 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8365 ops[3].value, ops[4].value, ops[5].value,
8366 ops[6].value, ops[7].value);
8367 case 9:
8368 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8369 ops[3].value, ops[4].value, ops[5].value,
8370 ops[6].value, ops[7].value, ops[8].value);
8372 gcc_unreachable ();
8375 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8376 as its operands. Return true on success and emit no code on failure. */
8378 bool
8379 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8380 struct expand_operand *ops)
8382 rtx pat = maybe_gen_insn (icode, nops, ops);
8383 if (pat)
8385 emit_insn (pat);
8386 return true;
8388 return false;
8391 /* Like maybe_expand_insn, but for jumps. */
8393 bool
8394 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8395 struct expand_operand *ops)
8397 rtx pat = maybe_gen_insn (icode, nops, ops);
8398 if (pat)
8400 emit_jump_insn (pat);
8401 return true;
8403 return false;
8406 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8407 as its operands. */
8409 void
8410 expand_insn (enum insn_code icode, unsigned int nops,
8411 struct expand_operand *ops)
8413 if (!maybe_expand_insn (icode, nops, ops))
8414 gcc_unreachable ();
8417 /* Like expand_insn, but for jumps. */
8419 void
8420 expand_jump_insn (enum insn_code icode, unsigned int nops,
8421 struct expand_operand *ops)
8423 if (!maybe_expand_jump_insn (icode, nops, ops))
8424 gcc_unreachable ();
8427 /* Reduce conditional compilation elsewhere. */
8428 #ifndef HAVE_insv
8429 #define HAVE_insv 0
8430 #define CODE_FOR_insv CODE_FOR_nothing
8431 #endif
8432 #ifndef HAVE_extv
8433 #define HAVE_extv 0
8434 #define CODE_FOR_extv CODE_FOR_nothing
8435 #endif
8436 #ifndef HAVE_extzv
8437 #define HAVE_extzv 0
8438 #define CODE_FOR_extzv CODE_FOR_nothing
8439 #endif
8441 /* Enumerates the possible types of structure operand to an
8442 extraction_insn. */
8443 enum extraction_type { ET_unaligned_mem, ET_reg };
8445 /* Check whether insv, extv or extzv pattern ICODE can be used for an
8446 insertion or extraction of type TYPE on a structure of mode MODE.
8447 Return true if so and fill in *INSN accordingly. STRUCT_OP is the
8448 operand number of the structure (the first sign_extract or zero_extract
8449 operand) and FIELD_OP is the operand number of the field (the other
8450 side of the set from the sign_extract or zero_extract). */
8452 static bool
8453 get_traditional_extraction_insn (extraction_insn *insn,
8454 enum extraction_type type,
8455 machine_mode mode,
8456 enum insn_code icode,
8457 int struct_op, int field_op)
8459 const struct insn_data_d *data = &insn_data[icode];
8461 machine_mode struct_mode = data->operand[struct_op].mode;
8462 if (struct_mode == VOIDmode)
8463 struct_mode = word_mode;
8464 if (mode != struct_mode)
8465 return false;
8467 machine_mode field_mode = data->operand[field_op].mode;
8468 if (field_mode == VOIDmode)
8469 field_mode = word_mode;
8471 machine_mode pos_mode = data->operand[struct_op + 2].mode;
8472 if (pos_mode == VOIDmode)
8473 pos_mode = word_mode;
8475 insn->icode = icode;
8476 insn->field_mode = field_mode;
8477 insn->struct_mode = (type == ET_unaligned_mem ? byte_mode : struct_mode);
8478 insn->pos_mode = pos_mode;
8479 return true;
8482 /* Return true if an optab exists to perform an insertion or extraction
8483 of type TYPE in mode MODE. Describe the instruction in *INSN if so.
8485 REG_OPTAB is the optab to use for register structures and
8486 MISALIGN_OPTAB is the optab to use for misaligned memory structures.
8487 POS_OP is the operand number of the bit position. */
8489 static bool
8490 get_optab_extraction_insn (struct extraction_insn *insn,
8491 enum extraction_type type,
8492 machine_mode mode, direct_optab reg_optab,
8493 direct_optab misalign_optab, int pos_op)
8495 direct_optab optab = (type == ET_unaligned_mem ? misalign_optab : reg_optab);
8496 enum insn_code icode = direct_optab_handler (optab, mode);
8497 if (icode == CODE_FOR_nothing)
8498 return false;
8500 const struct insn_data_d *data = &insn_data[icode];
8502 insn->icode = icode;
8503 insn->field_mode = mode;
8504 insn->struct_mode = (type == ET_unaligned_mem ? BLKmode : mode);
8505 insn->pos_mode = data->operand[pos_op].mode;
8506 if (insn->pos_mode == VOIDmode)
8507 insn->pos_mode = word_mode;
8508 return true;
8511 /* Return true if an instruction exists to perform an insertion or
8512 extraction (PATTERN says which) of type TYPE in mode MODE.
8513 Describe the instruction in *INSN if so. */
8515 static bool
8516 get_extraction_insn (extraction_insn *insn,
8517 enum extraction_pattern pattern,
8518 enum extraction_type type,
8519 machine_mode mode)
8521 switch (pattern)
8523 case EP_insv:
8524 if (HAVE_insv
8525 && get_traditional_extraction_insn (insn, type, mode,
8526 CODE_FOR_insv, 0, 3))
8527 return true;
8528 return get_optab_extraction_insn (insn, type, mode, insv_optab,
8529 insvmisalign_optab, 2);
8531 case EP_extv:
8532 if (HAVE_extv
8533 && get_traditional_extraction_insn (insn, type, mode,
8534 CODE_FOR_extv, 1, 0))
8535 return true;
8536 return get_optab_extraction_insn (insn, type, mode, extv_optab,
8537 extvmisalign_optab, 3);
8539 case EP_extzv:
8540 if (HAVE_extzv
8541 && get_traditional_extraction_insn (insn, type, mode,
8542 CODE_FOR_extzv, 1, 0))
8543 return true;
8544 return get_optab_extraction_insn (insn, type, mode, extzv_optab,
8545 extzvmisalign_optab, 3);
8547 default:
8548 gcc_unreachable ();
8552 /* Return true if an instruction exists to access a field of mode
8553 FIELDMODE in a structure that has STRUCT_BITS significant bits.
8554 Describe the "best" such instruction in *INSN if so. PATTERN and
8555 TYPE describe the type of insertion or extraction we want to perform.
8557 For an insertion, the number of significant structure bits includes
8558 all bits of the target. For an extraction, it need only include the
8559 most significant bit of the field. Larger widths are acceptable
8560 in both cases. */
8562 static bool
8563 get_best_extraction_insn (extraction_insn *insn,
8564 enum extraction_pattern pattern,
8565 enum extraction_type type,
8566 unsigned HOST_WIDE_INT struct_bits,
8567 machine_mode field_mode)
8569 machine_mode mode = smallest_mode_for_size (struct_bits, MODE_INT);
8570 while (mode != VOIDmode)
8572 if (get_extraction_insn (insn, pattern, type, mode))
8574 while (mode != VOIDmode
8575 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (field_mode)
8576 && !TRULY_NOOP_TRUNCATION_MODES_P (insn->field_mode,
8577 field_mode))
8579 get_extraction_insn (insn, pattern, type, mode);
8580 mode = GET_MODE_WIDER_MODE (mode);
8582 return true;
8584 mode = GET_MODE_WIDER_MODE (mode);
8586 return false;
8589 /* Return true if an instruction exists to access a field of mode
8590 FIELDMODE in a register structure that has STRUCT_BITS significant bits.
8591 Describe the "best" such instruction in *INSN if so. PATTERN describes
8592 the type of insertion or extraction we want to perform.
8594 For an insertion, the number of significant structure bits includes
8595 all bits of the target. For an extraction, it need only include the
8596 most significant bit of the field. Larger widths are acceptable
8597 in both cases. */
8599 bool
8600 get_best_reg_extraction_insn (extraction_insn *insn,
8601 enum extraction_pattern pattern,
8602 unsigned HOST_WIDE_INT struct_bits,
8603 machine_mode field_mode)
8605 return get_best_extraction_insn (insn, pattern, ET_reg, struct_bits,
8606 field_mode);
8609 /* Return true if an instruction exists to access a field of BITSIZE
8610 bits starting BITNUM bits into a memory structure. Describe the
8611 "best" such instruction in *INSN if so. PATTERN describes the type
8612 of insertion or extraction we want to perform and FIELDMODE is the
8613 natural mode of the extracted field.
8615 The instructions considered here only access bytes that overlap
8616 the bitfield; they do not touch any surrounding bytes. */
8618 bool
8619 get_best_mem_extraction_insn (extraction_insn *insn,
8620 enum extraction_pattern pattern,
8621 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitnum,
8622 machine_mode field_mode)
8624 unsigned HOST_WIDE_INT struct_bits = (bitnum % BITS_PER_UNIT
8625 + bitsize
8626 + BITS_PER_UNIT - 1);
8627 struct_bits -= struct_bits % BITS_PER_UNIT;
8628 return get_best_extraction_insn (insn, pattern, ET_unaligned_mem,
8629 struct_bits, field_mode);
8632 /* Determine whether "1 << x" is relatively cheap in word_mode. */
8634 bool
8635 lshift_cheap_p (bool speed_p)
8637 /* FIXME: This should be made target dependent via this "this_target"
8638 mechanism, similar to e.g. can_copy_init_p in gcse.c. */
8639 static bool init[2] = { false, false };
8640 static bool cheap[2] = { true, true };
8642 /* If the targer has no lshift in word_mode, the operation will most
8643 probably not be cheap. ??? Does GCC even work for such targets? */
8644 if (optab_handler (ashl_optab, word_mode) == CODE_FOR_nothing)
8645 return false;
8647 if (!init[speed_p])
8649 rtx reg = gen_raw_REG (word_mode, 10000);
8650 int cost = set_src_cost (gen_rtx_ASHIFT (word_mode, const1_rtx, reg),
8651 speed_p);
8652 cheap[speed_p] = cost < COSTS_N_INSNS (3);
8653 init[speed_p] = true;
8656 return cheap[speed_p];
8659 #include "gt-optabs.h"