* altivec.md: Delete UNSPEC_VMLADDUHM.
[official-gcc.git] / gcc / optabs.c
blobda5dfa48a5fddb6fc5cb575dc8c8eacfbdaeb916
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "diagnostic-core.h"
27 /* Include insn-config.h before expr.h so that HAVE_conditional_move
28 is properly defined. */
29 #include "insn-config.h"
30 #include "rtl.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "tree.h"
35 #include "tree-hasher.h"
36 #include "stor-layout.h"
37 #include "stringpool.h"
38 #include "varasm.h"
39 #include "tm_p.h"
40 #include "flags.h"
41 #include "hard-reg-set.h"
42 #include "function.h"
43 #include "except.h"
44 #include "expmed.h"
45 #include "dojump.h"
46 #include "explow.h"
47 #include "calls.h"
48 #include "emit-rtl.h"
49 #include "stmt.h"
50 #include "expr.h"
51 #include "insn-codes.h"
52 #include "optabs.h"
53 #include "libfuncs.h"
54 #include "recog.h"
55 #include "reload.h"
56 #include "predict.h"
57 #include "dominance.h"
58 #include "cfg.h"
59 #include "basic-block.h"
60 #include "target.h"
62 struct target_optabs default_target_optabs;
63 struct target_libfuncs default_target_libfuncs;
64 struct target_optabs *this_fn_optabs = &default_target_optabs;
65 #if SWITCHABLE_TARGET
66 struct target_optabs *this_target_optabs = &default_target_optabs;
67 struct target_libfuncs *this_target_libfuncs = &default_target_libfuncs;
68 #endif
70 #define libfunc_hash \
71 (this_target_libfuncs->x_libfunc_hash)
73 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
74 machine_mode *);
75 static rtx expand_unop_direct (machine_mode, optab, rtx, rtx, int);
76 static void emit_libcall_block_1 (rtx_insn *, rtx, rtx, rtx, bool);
78 /* Debug facility for use in GDB. */
79 void debug_optab_libfuncs (void);
81 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
82 #if ENABLE_DECIMAL_BID_FORMAT
83 #define DECIMAL_PREFIX "bid_"
84 #else
85 #define DECIMAL_PREFIX "dpd_"
86 #endif
88 /* Used for libfunc_hash. */
90 hashval_t
91 libfunc_hasher::hash (libfunc_entry *e)
93 return ((e->mode1 + e->mode2 * NUM_MACHINE_MODES) ^ e->op);
96 /* Used for libfunc_hash. */
98 bool
99 libfunc_hasher::equal (libfunc_entry *e1, libfunc_entry *e2)
101 return e1->op == e2->op && e1->mode1 == e2->mode1 && e1->mode2 == e2->mode2;
104 /* Return libfunc corresponding operation defined by OPTAB converting
105 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
106 if no libfunc is available. */
108 convert_optab_libfunc (convert_optab optab, machine_mode mode1,
109 machine_mode mode2)
111 struct libfunc_entry e;
112 struct libfunc_entry **slot;
114 /* ??? This ought to be an assert, but not all of the places
115 that we expand optabs know about the optabs that got moved
116 to being direct. */
117 if (!(optab >= FIRST_CONV_OPTAB && optab <= LAST_CONVLIB_OPTAB))
118 return NULL_RTX;
120 e.op = optab;
121 e.mode1 = mode1;
122 e.mode2 = mode2;
123 slot = libfunc_hash->find_slot (&e, NO_INSERT);
124 if (!slot)
126 const struct convert_optab_libcall_d *d
127 = &convlib_def[optab - FIRST_CONV_OPTAB];
129 if (d->libcall_gen == NULL)
130 return NULL;
132 d->libcall_gen (optab, d->libcall_basename, mode1, mode2);
133 slot = libfunc_hash->find_slot (&e, NO_INSERT);
134 if (!slot)
135 return NULL;
137 return (*slot)->libfunc;
140 /* Return libfunc corresponding operation defined by OPTAB in MODE.
141 Trigger lazy initialization if needed, return NULL if no libfunc is
142 available. */
144 optab_libfunc (optab optab, machine_mode mode)
146 struct libfunc_entry e;
147 struct libfunc_entry **slot;
149 /* ??? This ought to be an assert, but not all of the places
150 that we expand optabs know about the optabs that got moved
151 to being direct. */
152 if (!(optab >= FIRST_NORM_OPTAB && optab <= LAST_NORMLIB_OPTAB))
153 return NULL_RTX;
155 e.op = optab;
156 e.mode1 = mode;
157 e.mode2 = VOIDmode;
158 slot = libfunc_hash->find_slot (&e, NO_INSERT);
159 if (!slot)
161 const struct optab_libcall_d *d
162 = &normlib_def[optab - FIRST_NORM_OPTAB];
164 if (d->libcall_gen == NULL)
165 return NULL;
167 d->libcall_gen (optab, d->libcall_basename, d->libcall_suffix, mode);
168 slot = libfunc_hash->find_slot (&e, NO_INSERT);
169 if (!slot)
170 return NULL;
172 return (*slot)->libfunc;
176 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
177 the result of operation CODE applied to OP0 (and OP1 if it is a binary
178 operation).
180 If the last insn does not set TARGET, don't do anything, but return 1.
182 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
183 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
184 try again, ensuring that TARGET is not one of the operands. */
186 static int
187 add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
189 rtx_insn *last_insn;
190 rtx set;
191 rtx note;
193 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
195 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
196 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
197 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
198 && GET_RTX_CLASS (code) != RTX_COMPARE
199 && GET_RTX_CLASS (code) != RTX_UNARY)
200 return 1;
202 if (GET_CODE (target) == ZERO_EXTRACT)
203 return 1;
205 for (last_insn = insns;
206 NEXT_INSN (last_insn) != NULL_RTX;
207 last_insn = NEXT_INSN (last_insn))
210 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
211 a value changing in the insn, so the note would be invalid for CSE. */
212 if (reg_overlap_mentioned_p (target, op0)
213 || (op1 && reg_overlap_mentioned_p (target, op1)))
215 if (MEM_P (target)
216 && (rtx_equal_p (target, op0)
217 || (op1 && rtx_equal_p (target, op1))))
219 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
220 over expanding it as temp = MEM op X, MEM = temp. If the target
221 supports MEM = MEM op X instructions, it is sometimes too hard
222 to reconstruct that form later, especially if X is also a memory,
223 and due to multiple occurrences of addresses the address might
224 be forced into register unnecessarily.
225 Note that not emitting the REG_EQUIV note might inhibit
226 CSE in some cases. */
227 set = single_set (last_insn);
228 if (set
229 && GET_CODE (SET_SRC (set)) == code
230 && MEM_P (SET_DEST (set))
231 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
232 || (op1 && rtx_equal_p (SET_DEST (set),
233 XEXP (SET_SRC (set), 1)))))
234 return 1;
236 return 0;
239 set = set_for_reg_notes (last_insn);
240 if (set == NULL_RTX)
241 return 1;
243 if (! rtx_equal_p (SET_DEST (set), target)
244 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
245 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
246 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
247 return 1;
249 if (GET_RTX_CLASS (code) == RTX_UNARY)
250 switch (code)
252 case FFS:
253 case CLZ:
254 case CTZ:
255 case CLRSB:
256 case POPCOUNT:
257 case PARITY:
258 case BSWAP:
259 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
261 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
262 if (GET_MODE_SIZE (GET_MODE (op0))
263 > GET_MODE_SIZE (GET_MODE (target)))
264 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
265 note, GET_MODE (op0));
266 else
267 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
268 note, GET_MODE (op0));
269 break;
271 /* FALLTHRU */
272 default:
273 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
274 break;
276 else
277 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
279 set_unique_reg_note (last_insn, REG_EQUAL, note);
281 return 1;
284 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
285 for a widening operation would be. In most cases this would be OP0, but if
286 that's a constant it'll be VOIDmode, which isn't useful. */
288 static machine_mode
289 widened_mode (machine_mode to_mode, rtx op0, rtx op1)
291 machine_mode m0 = GET_MODE (op0);
292 machine_mode m1 = GET_MODE (op1);
293 machine_mode result;
295 if (m0 == VOIDmode && m1 == VOIDmode)
296 return to_mode;
297 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
298 result = m1;
299 else
300 result = m0;
302 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
303 return to_mode;
305 return result;
308 /* Like optab_handler, but for widening_operations that have a
309 TO_MODE and a FROM_MODE. */
311 enum insn_code
312 widening_optab_handler (optab op, machine_mode to_mode,
313 machine_mode from_mode)
315 unsigned scode = (op << 16) | to_mode;
316 if (to_mode != from_mode && from_mode != VOIDmode)
318 /* ??? Why does find_widening_optab_handler_and_mode attempt to
319 widen things that can't be widened? E.g. add_optab... */
320 if (op > LAST_CONV_OPTAB)
321 return CODE_FOR_nothing;
322 scode |= from_mode << 8;
324 return raw_optab_handler (scode);
327 /* Find a widening optab even if it doesn't widen as much as we want.
328 E.g. if from_mode is HImode, and to_mode is DImode, and there is no
329 direct HI->SI insn, then return SI->DI, if that exists.
330 If PERMIT_NON_WIDENING is non-zero then this can be used with
331 non-widening optabs also. */
333 enum insn_code
334 find_widening_optab_handler_and_mode (optab op, machine_mode to_mode,
335 machine_mode from_mode,
336 int permit_non_widening,
337 machine_mode *found_mode)
339 for (; (permit_non_widening || from_mode != to_mode)
340 && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
341 && from_mode != VOIDmode;
342 from_mode = GET_MODE_WIDER_MODE (from_mode))
344 enum insn_code handler = widening_optab_handler (op, to_mode,
345 from_mode);
347 if (handler != CODE_FOR_nothing)
349 if (found_mode)
350 *found_mode = from_mode;
351 return handler;
355 return CODE_FOR_nothing;
358 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
359 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
360 not actually do a sign-extend or zero-extend, but can leave the
361 higher-order bits of the result rtx undefined, for example, in the case
362 of logical operations, but not right shifts. */
364 static rtx
365 widen_operand (rtx op, machine_mode mode, machine_mode oldmode,
366 int unsignedp, int no_extend)
368 rtx result;
370 /* If we don't have to extend and this is a constant, return it. */
371 if (no_extend && GET_MODE (op) == VOIDmode)
372 return op;
374 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
375 extend since it will be more efficient to do so unless the signedness of
376 a promoted object differs from our extension. */
377 if (! no_extend
378 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
379 && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
380 return convert_modes (mode, oldmode, op, unsignedp);
382 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
383 SUBREG. */
384 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
385 return gen_lowpart (mode, force_reg (GET_MODE (op), op));
387 /* Otherwise, get an object of MODE, clobber it, and set the low-order
388 part to OP. */
390 result = gen_reg_rtx (mode);
391 emit_clobber (result);
392 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
393 return result;
396 /* Return the optab used for computing the operation given by the tree code,
397 CODE and the tree EXP. This function is not always usable (for example, it
398 cannot give complete results for multiplication or division) but probably
399 ought to be relied on more widely throughout the expander. */
400 optab
401 optab_for_tree_code (enum tree_code code, const_tree type,
402 enum optab_subtype subtype)
404 bool trapv;
405 switch (code)
407 case BIT_AND_EXPR:
408 return and_optab;
410 case BIT_IOR_EXPR:
411 return ior_optab;
413 case BIT_NOT_EXPR:
414 return one_cmpl_optab;
416 case BIT_XOR_EXPR:
417 return xor_optab;
419 case MULT_HIGHPART_EXPR:
420 return TYPE_UNSIGNED (type) ? umul_highpart_optab : smul_highpart_optab;
422 case TRUNC_MOD_EXPR:
423 case CEIL_MOD_EXPR:
424 case FLOOR_MOD_EXPR:
425 case ROUND_MOD_EXPR:
426 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
428 case RDIV_EXPR:
429 case TRUNC_DIV_EXPR:
430 case CEIL_DIV_EXPR:
431 case FLOOR_DIV_EXPR:
432 case ROUND_DIV_EXPR:
433 case EXACT_DIV_EXPR:
434 if (TYPE_SATURATING (type))
435 return TYPE_UNSIGNED (type) ? usdiv_optab : ssdiv_optab;
436 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
438 case LSHIFT_EXPR:
439 if (TREE_CODE (type) == VECTOR_TYPE)
441 if (subtype == optab_vector)
442 return TYPE_SATURATING (type) ? unknown_optab : vashl_optab;
444 gcc_assert (subtype == optab_scalar);
446 if (TYPE_SATURATING (type))
447 return TYPE_UNSIGNED (type) ? usashl_optab : ssashl_optab;
448 return ashl_optab;
450 case RSHIFT_EXPR:
451 if (TREE_CODE (type) == VECTOR_TYPE)
453 if (subtype == optab_vector)
454 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
456 gcc_assert (subtype == optab_scalar);
458 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
460 case LROTATE_EXPR:
461 if (TREE_CODE (type) == VECTOR_TYPE)
463 if (subtype == optab_vector)
464 return vrotl_optab;
466 gcc_assert (subtype == optab_scalar);
468 return rotl_optab;
470 case RROTATE_EXPR:
471 if (TREE_CODE (type) == VECTOR_TYPE)
473 if (subtype == optab_vector)
474 return vrotr_optab;
476 gcc_assert (subtype == optab_scalar);
478 return rotr_optab;
480 case MAX_EXPR:
481 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
483 case MIN_EXPR:
484 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
486 case REALIGN_LOAD_EXPR:
487 return vec_realign_load_optab;
489 case WIDEN_SUM_EXPR:
490 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
492 case DOT_PROD_EXPR:
493 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
495 case SAD_EXPR:
496 return TYPE_UNSIGNED (type) ? usad_optab : ssad_optab;
498 case WIDEN_MULT_PLUS_EXPR:
499 return (TYPE_UNSIGNED (type)
500 ? (TYPE_SATURATING (type)
501 ? usmadd_widen_optab : umadd_widen_optab)
502 : (TYPE_SATURATING (type)
503 ? ssmadd_widen_optab : smadd_widen_optab));
505 case WIDEN_MULT_MINUS_EXPR:
506 return (TYPE_UNSIGNED (type)
507 ? (TYPE_SATURATING (type)
508 ? usmsub_widen_optab : umsub_widen_optab)
509 : (TYPE_SATURATING (type)
510 ? ssmsub_widen_optab : smsub_widen_optab));
512 case FMA_EXPR:
513 return fma_optab;
515 case REDUC_MAX_EXPR:
516 return TYPE_UNSIGNED (type)
517 ? reduc_umax_scal_optab : reduc_smax_scal_optab;
519 case REDUC_MIN_EXPR:
520 return TYPE_UNSIGNED (type)
521 ? reduc_umin_scal_optab : reduc_smin_scal_optab;
523 case REDUC_PLUS_EXPR:
524 return reduc_plus_scal_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 /* Create a new vector value in VMODE with all elements set to OP. The
775 mode of OP must be the element mode of VMODE. If OP is a constant,
776 then the return value will be a constant. */
778 static rtx
779 expand_vector_broadcast (machine_mode vmode, rtx op)
781 enum insn_code icode;
782 rtvec vec;
783 rtx ret;
784 int i, n;
786 gcc_checking_assert (VECTOR_MODE_P (vmode));
788 n = GET_MODE_NUNITS (vmode);
789 vec = rtvec_alloc (n);
790 for (i = 0; i < n; ++i)
791 RTVEC_ELT (vec, i) = op;
793 if (CONSTANT_P (op))
794 return gen_rtx_CONST_VECTOR (vmode, vec);
796 /* ??? If the target doesn't have a vec_init, then we have no easy way
797 of performing this operation. Most of this sort of generic support
798 is hidden away in the vector lowering support in gimple. */
799 icode = optab_handler (vec_init_optab, vmode);
800 if (icode == CODE_FOR_nothing)
801 return NULL;
803 ret = gen_reg_rtx (vmode);
804 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
806 return ret;
809 /* This subroutine of expand_doubleword_shift handles the cases in which
810 the effective shift value is >= BITS_PER_WORD. The arguments and return
811 value are the same as for the parent routine, except that SUPERWORD_OP1
812 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
813 INTO_TARGET may be null if the caller has decided to calculate it. */
815 static bool
816 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
817 rtx outof_target, rtx into_target,
818 int unsignedp, enum optab_methods methods)
820 if (into_target != 0)
821 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
822 into_target, unsignedp, methods))
823 return false;
825 if (outof_target != 0)
827 /* For a signed right shift, we must fill OUTOF_TARGET with copies
828 of the sign bit, otherwise we must fill it with zeros. */
829 if (binoptab != ashr_optab)
830 emit_move_insn (outof_target, CONST0_RTX (word_mode));
831 else
832 if (!force_expand_binop (word_mode, binoptab,
833 outof_input, GEN_INT (BITS_PER_WORD - 1),
834 outof_target, unsignedp, methods))
835 return false;
837 return true;
840 /* This subroutine of expand_doubleword_shift handles the cases in which
841 the effective shift value is < BITS_PER_WORD. The arguments and return
842 value are the same as for the parent routine. */
844 static bool
845 expand_subword_shift (machine_mode op1_mode, optab binoptab,
846 rtx outof_input, rtx into_input, rtx op1,
847 rtx outof_target, rtx into_target,
848 int unsignedp, enum optab_methods methods,
849 unsigned HOST_WIDE_INT shift_mask)
851 optab reverse_unsigned_shift, unsigned_shift;
852 rtx tmp, carries;
854 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
855 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
857 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
858 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
859 the opposite direction to BINOPTAB. */
860 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
862 carries = outof_input;
863 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
864 op1_mode), op1_mode);
865 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
866 0, true, methods);
868 else
870 /* We must avoid shifting by BITS_PER_WORD bits since that is either
871 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
872 has unknown behavior. Do a single shift first, then shift by the
873 remainder. It's OK to use ~OP1 as the remainder if shift counts
874 are truncated to the mode size. */
875 carries = expand_binop (word_mode, reverse_unsigned_shift,
876 outof_input, const1_rtx, 0, unsignedp, methods);
877 if (shift_mask == BITS_PER_WORD - 1)
879 tmp = immed_wide_int_const
880 (wi::minus_one (GET_MODE_PRECISION (op1_mode)), op1_mode);
881 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
882 0, true, methods);
884 else
886 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
887 op1_mode), op1_mode);
888 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
889 0, true, methods);
892 if (tmp == 0 || carries == 0)
893 return false;
894 carries = expand_binop (word_mode, reverse_unsigned_shift,
895 carries, tmp, 0, unsignedp, methods);
896 if (carries == 0)
897 return false;
899 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
900 so the result can go directly into INTO_TARGET if convenient. */
901 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
902 into_target, unsignedp, methods);
903 if (tmp == 0)
904 return false;
906 /* Now OR in the bits carried over from OUTOF_INPUT. */
907 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
908 into_target, unsignedp, methods))
909 return false;
911 /* Use a standard word_mode shift for the out-of half. */
912 if (outof_target != 0)
913 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
914 outof_target, unsignedp, methods))
915 return false;
917 return true;
921 /* Try implementing expand_doubleword_shift using conditional moves.
922 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
923 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
924 are the shift counts to use in the former and latter case. All other
925 arguments are the same as the parent routine. */
927 static bool
928 expand_doubleword_shift_condmove (machine_mode op1_mode, optab binoptab,
929 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
930 rtx outof_input, rtx into_input,
931 rtx subword_op1, rtx superword_op1,
932 rtx outof_target, rtx into_target,
933 int unsignedp, enum optab_methods methods,
934 unsigned HOST_WIDE_INT shift_mask)
936 rtx outof_superword, into_superword;
938 /* Put the superword version of the output into OUTOF_SUPERWORD and
939 INTO_SUPERWORD. */
940 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
941 if (outof_target != 0 && subword_op1 == superword_op1)
943 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
944 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
945 into_superword = outof_target;
946 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
947 outof_superword, 0, unsignedp, methods))
948 return false;
950 else
952 into_superword = gen_reg_rtx (word_mode);
953 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
954 outof_superword, into_superword,
955 unsignedp, methods))
956 return false;
959 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
960 if (!expand_subword_shift (op1_mode, binoptab,
961 outof_input, into_input, subword_op1,
962 outof_target, into_target,
963 unsignedp, methods, shift_mask))
964 return false;
966 /* Select between them. Do the INTO half first because INTO_SUPERWORD
967 might be the current value of OUTOF_TARGET. */
968 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
969 into_target, into_superword, word_mode, false))
970 return false;
972 if (outof_target != 0)
973 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
974 outof_target, outof_superword,
975 word_mode, false))
976 return false;
978 return true;
981 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
982 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
983 input operand; the shift moves bits in the direction OUTOF_INPUT->
984 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
985 of the target. OP1 is the shift count and OP1_MODE is its mode.
986 If OP1 is constant, it will have been truncated as appropriate
987 and is known to be nonzero.
989 If SHIFT_MASK is zero, the result of word shifts is undefined when the
990 shift count is outside the range [0, BITS_PER_WORD). This routine must
991 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
993 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
994 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
995 fill with zeros or sign bits as appropriate.
997 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
998 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
999 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
1000 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
1001 are undefined.
1003 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
1004 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
1005 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
1006 function wants to calculate it itself.
1008 Return true if the shift could be successfully synthesized. */
1010 static bool
1011 expand_doubleword_shift (machine_mode op1_mode, optab binoptab,
1012 rtx outof_input, rtx into_input, rtx op1,
1013 rtx outof_target, rtx into_target,
1014 int unsignedp, enum optab_methods methods,
1015 unsigned HOST_WIDE_INT shift_mask)
1017 rtx superword_op1, tmp, cmp1, cmp2;
1018 enum rtx_code cmp_code;
1020 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1021 fill the result with sign or zero bits as appropriate. If so, the value
1022 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1023 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1024 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1026 This isn't worthwhile for constant shifts since the optimizers will
1027 cope better with in-range shift counts. */
1028 if (shift_mask >= BITS_PER_WORD
1029 && outof_target != 0
1030 && !CONSTANT_P (op1))
1032 if (!expand_doubleword_shift (op1_mode, binoptab,
1033 outof_input, into_input, op1,
1034 0, into_target,
1035 unsignedp, methods, shift_mask))
1036 return false;
1037 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1038 outof_target, unsignedp, methods))
1039 return false;
1040 return true;
1043 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1044 is true when the effective shift value is less than BITS_PER_WORD.
1045 Set SUPERWORD_OP1 to the shift count that should be used to shift
1046 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1047 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
1048 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1050 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1051 is a subword shift count. */
1052 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1053 0, true, methods);
1054 cmp2 = CONST0_RTX (op1_mode);
1055 cmp_code = EQ;
1056 superword_op1 = op1;
1058 else
1060 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1061 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1062 0, true, methods);
1063 cmp2 = CONST0_RTX (op1_mode);
1064 cmp_code = LT;
1065 superword_op1 = cmp1;
1067 if (cmp1 == 0)
1068 return false;
1070 /* If we can compute the condition at compile time, pick the
1071 appropriate subroutine. */
1072 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1073 if (tmp != 0 && CONST_INT_P (tmp))
1075 if (tmp == const0_rtx)
1076 return expand_superword_shift (binoptab, outof_input, superword_op1,
1077 outof_target, into_target,
1078 unsignedp, methods);
1079 else
1080 return expand_subword_shift (op1_mode, binoptab,
1081 outof_input, into_input, op1,
1082 outof_target, into_target,
1083 unsignedp, methods, shift_mask);
1086 /* Try using conditional moves to generate straight-line code. */
1087 if (HAVE_conditional_move)
1089 rtx_insn *start = get_last_insn ();
1090 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1091 cmp_code, cmp1, cmp2,
1092 outof_input, into_input,
1093 op1, superword_op1,
1094 outof_target, into_target,
1095 unsignedp, methods, shift_mask))
1096 return true;
1097 delete_insns_since (start);
1100 /* As a last resort, use branches to select the correct alternative. */
1101 rtx_code_label *subword_label = gen_label_rtx ();
1102 rtx_code_label *done_label = gen_label_rtx ();
1104 NO_DEFER_POP;
1105 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1106 0, 0, subword_label, -1);
1107 OK_DEFER_POP;
1109 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1110 outof_target, into_target,
1111 unsignedp, methods))
1112 return false;
1114 emit_jump_insn (gen_jump (done_label));
1115 emit_barrier ();
1116 emit_label (subword_label);
1118 if (!expand_subword_shift (op1_mode, binoptab,
1119 outof_input, into_input, op1,
1120 outof_target, into_target,
1121 unsignedp, methods, shift_mask))
1122 return false;
1124 emit_label (done_label);
1125 return true;
1128 /* Subroutine of expand_binop. Perform a double word multiplication of
1129 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1130 as the target's word_mode. This function return NULL_RTX if anything
1131 goes wrong, in which case it may have already emitted instructions
1132 which need to be deleted.
1134 If we want to multiply two two-word values and have normal and widening
1135 multiplies of single-word values, we can do this with three smaller
1136 multiplications.
1138 The multiplication proceeds as follows:
1139 _______________________
1140 [__op0_high_|__op0_low__]
1141 _______________________
1142 * [__op1_high_|__op1_low__]
1143 _______________________________________________
1144 _______________________
1145 (1) [__op0_low__*__op1_low__]
1146 _______________________
1147 (2a) [__op0_low__*__op1_high_]
1148 _______________________
1149 (2b) [__op0_high_*__op1_low__]
1150 _______________________
1151 (3) [__op0_high_*__op1_high_]
1154 This gives a 4-word result. Since we are only interested in the
1155 lower 2 words, partial result (3) and the upper words of (2a) and
1156 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1157 calculated using non-widening multiplication.
1159 (1), however, needs to be calculated with an unsigned widening
1160 multiplication. If this operation is not directly supported we
1161 try using a signed widening multiplication and adjust the result.
1162 This adjustment works as follows:
1164 If both operands are positive then no adjustment is needed.
1166 If the operands have different signs, for example op0_low < 0 and
1167 op1_low >= 0, the instruction treats the most significant bit of
1168 op0_low as a sign bit instead of a bit with significance
1169 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1170 with 2**BITS_PER_WORD - op0_low, and two's complements the
1171 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1172 the result.
1174 Similarly, if both operands are negative, we need to add
1175 (op0_low + op1_low) * 2**BITS_PER_WORD.
1177 We use a trick to adjust quickly. We logically shift op0_low right
1178 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1179 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1180 logical shift exists, we do an arithmetic right shift and subtract
1181 the 0 or -1. */
1183 static rtx
1184 expand_doubleword_mult (machine_mode mode, rtx op0, rtx op1, rtx target,
1185 bool umulp, enum optab_methods methods)
1187 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1188 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1189 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1190 rtx product, adjust, product_high, temp;
1192 rtx op0_high = operand_subword_force (op0, high, mode);
1193 rtx op0_low = operand_subword_force (op0, low, mode);
1194 rtx op1_high = operand_subword_force (op1, high, mode);
1195 rtx op1_low = operand_subword_force (op1, low, mode);
1197 /* If we're using an unsigned multiply to directly compute the product
1198 of the low-order words of the operands and perform any required
1199 adjustments of the operands, we begin by trying two more multiplications
1200 and then computing the appropriate sum.
1202 We have checked above that the required addition is provided.
1203 Full-word addition will normally always succeed, especially if
1204 it is provided at all, so we don't worry about its failure. The
1205 multiplication may well fail, however, so we do handle that. */
1207 if (!umulp)
1209 /* ??? This could be done with emit_store_flag where available. */
1210 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1211 NULL_RTX, 1, methods);
1212 if (temp)
1213 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1214 NULL_RTX, 0, OPTAB_DIRECT);
1215 else
1217 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1218 NULL_RTX, 0, methods);
1219 if (!temp)
1220 return NULL_RTX;
1221 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1222 NULL_RTX, 0, OPTAB_DIRECT);
1225 if (!op0_high)
1226 return NULL_RTX;
1229 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1230 NULL_RTX, 0, OPTAB_DIRECT);
1231 if (!adjust)
1232 return NULL_RTX;
1234 /* OP0_HIGH should now be dead. */
1236 if (!umulp)
1238 /* ??? This could be done with emit_store_flag where available. */
1239 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1240 NULL_RTX, 1, methods);
1241 if (temp)
1242 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1243 NULL_RTX, 0, OPTAB_DIRECT);
1244 else
1246 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1247 NULL_RTX, 0, methods);
1248 if (!temp)
1249 return NULL_RTX;
1250 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1251 NULL_RTX, 0, OPTAB_DIRECT);
1254 if (!op1_high)
1255 return NULL_RTX;
1258 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1259 NULL_RTX, 0, OPTAB_DIRECT);
1260 if (!temp)
1261 return NULL_RTX;
1263 /* OP1_HIGH should now be dead. */
1265 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1266 NULL_RTX, 0, OPTAB_DIRECT);
1268 if (target && !REG_P (target))
1269 target = NULL_RTX;
1271 if (umulp)
1272 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1273 target, 1, OPTAB_DIRECT);
1274 else
1275 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1276 target, 1, OPTAB_DIRECT);
1278 if (!product)
1279 return NULL_RTX;
1281 product_high = operand_subword (product, high, 1, mode);
1282 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1283 NULL_RTX, 0, OPTAB_DIRECT);
1284 emit_move_insn (product_high, adjust);
1285 return product;
1288 /* Wrapper around expand_binop which takes an rtx code to specify
1289 the operation to perform, not an optab pointer. All other
1290 arguments are the same. */
1292 expand_simple_binop (machine_mode mode, enum rtx_code code, rtx op0,
1293 rtx op1, rtx target, int unsignedp,
1294 enum optab_methods methods)
1296 optab binop = code_to_optab (code);
1297 gcc_assert (binop);
1299 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1302 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1303 binop. Order them according to commutative_operand_precedence and, if
1304 possible, try to put TARGET or a pseudo first. */
1305 static bool
1306 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1308 int op0_prec = commutative_operand_precedence (op0);
1309 int op1_prec = commutative_operand_precedence (op1);
1311 if (op0_prec < op1_prec)
1312 return true;
1314 if (op0_prec > op1_prec)
1315 return false;
1317 /* With equal precedence, both orders are ok, but it is better if the
1318 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1319 if (target == 0 || REG_P (target))
1320 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1321 else
1322 return rtx_equal_p (op1, target);
1325 /* Return true if BINOPTAB implements a shift operation. */
1327 static bool
1328 shift_optab_p (optab binoptab)
1330 switch (optab_to_code (binoptab))
1332 case ASHIFT:
1333 case SS_ASHIFT:
1334 case US_ASHIFT:
1335 case ASHIFTRT:
1336 case LSHIFTRT:
1337 case ROTATE:
1338 case ROTATERT:
1339 return true;
1341 default:
1342 return false;
1346 /* Return true if BINOPTAB implements a commutative binary operation. */
1348 static bool
1349 commutative_optab_p (optab binoptab)
1351 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1352 || binoptab == smul_widen_optab
1353 || binoptab == umul_widen_optab
1354 || binoptab == smul_highpart_optab
1355 || binoptab == umul_highpart_optab);
1358 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1359 optimizing, and if the operand is a constant that costs more than
1360 1 instruction, force the constant into a register and return that
1361 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1363 static rtx
1364 avoid_expensive_constant (machine_mode mode, optab binoptab,
1365 int opn, rtx x, bool unsignedp)
1367 bool speed = optimize_insn_for_speed_p ();
1369 if (mode != VOIDmode
1370 && optimize
1371 && CONSTANT_P (x)
1372 && (rtx_cost (x, optab_to_code (binoptab), opn, speed)
1373 > set_src_cost (x, speed)))
1375 if (CONST_INT_P (x))
1377 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1378 if (intval != INTVAL (x))
1379 x = GEN_INT (intval);
1381 else
1382 x = convert_modes (mode, VOIDmode, x, unsignedp);
1383 x = force_reg (mode, x);
1385 return x;
1388 /* Helper function for expand_binop: handle the case where there
1389 is an insn that directly implements the indicated operation.
1390 Returns null if this is not possible. */
1391 static rtx
1392 expand_binop_directly (machine_mode mode, optab binoptab,
1393 rtx op0, rtx op1,
1394 rtx target, int unsignedp, enum optab_methods methods,
1395 rtx_insn *last)
1397 machine_mode from_mode = widened_mode (mode, op0, op1);
1398 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1399 from_mode, 1);
1400 machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1401 machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1402 machine_mode mode0, mode1, tmp_mode;
1403 struct expand_operand ops[3];
1404 bool commutative_p;
1405 rtx_insn *pat;
1406 rtx xop0 = op0, xop1 = op1;
1408 /* If it is a commutative operator and the modes would match
1409 if we would swap the operands, we can save the conversions. */
1410 commutative_p = commutative_optab_p (binoptab);
1411 if (commutative_p
1412 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1413 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1414 std::swap (xop0, xop1);
1416 /* If we are optimizing, force expensive constants into a register. */
1417 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1418 if (!shift_optab_p (binoptab))
1419 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1421 /* In case the insn wants input operands in modes different from
1422 those of the actual operands, convert the operands. It would
1423 seem that we don't need to convert CONST_INTs, but we do, so
1424 that they're properly zero-extended, sign-extended or truncated
1425 for their mode. */
1427 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1428 if (xmode0 != VOIDmode && xmode0 != mode0)
1430 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1431 mode0 = xmode0;
1434 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1435 if (xmode1 != VOIDmode && xmode1 != mode1)
1437 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1438 mode1 = xmode1;
1441 /* If operation is commutative,
1442 try to make the first operand a register.
1443 Even better, try to make it the same as the target.
1444 Also try to make the last operand a constant. */
1445 if (commutative_p
1446 && swap_commutative_operands_with_target (target, xop0, xop1))
1447 std::swap (xop0, xop1);
1449 /* Now, if insn's predicates don't allow our operands, put them into
1450 pseudo regs. */
1452 if (binoptab == vec_pack_trunc_optab
1453 || binoptab == vec_pack_usat_optab
1454 || binoptab == vec_pack_ssat_optab
1455 || binoptab == vec_pack_ufix_trunc_optab
1456 || binoptab == vec_pack_sfix_trunc_optab)
1458 /* The mode of the result is different then the mode of the
1459 arguments. */
1460 tmp_mode = insn_data[(int) icode].operand[0].mode;
1461 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1463 delete_insns_since (last);
1464 return NULL_RTX;
1467 else
1468 tmp_mode = mode;
1470 create_output_operand (&ops[0], target, tmp_mode);
1471 create_input_operand (&ops[1], xop0, mode0);
1472 create_input_operand (&ops[2], xop1, mode1);
1473 pat = maybe_gen_insn (icode, 3, ops);
1474 if (pat)
1476 /* If PAT is composed of more than one insn, try to add an appropriate
1477 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1478 operand, call expand_binop again, this time without a target. */
1479 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1480 && ! add_equal_note (pat, ops[0].value,
1481 optab_to_code (binoptab),
1482 ops[1].value, ops[2].value))
1484 delete_insns_since (last);
1485 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1486 unsignedp, methods);
1489 emit_insn (pat);
1490 return ops[0].value;
1492 delete_insns_since (last);
1493 return NULL_RTX;
1496 /* Generate code to perform an operation specified by BINOPTAB
1497 on operands OP0 and OP1, with result having machine-mode MODE.
1499 UNSIGNEDP is for the case where we have to widen the operands
1500 to perform the operation. It says to use zero-extension.
1502 If TARGET is nonzero, the value
1503 is generated there, if it is convenient to do so.
1504 In all cases an rtx is returned for the locus of the value;
1505 this may or may not be TARGET. */
1508 expand_binop (machine_mode mode, optab binoptab, rtx op0, rtx op1,
1509 rtx target, int unsignedp, enum optab_methods methods)
1511 enum optab_methods next_methods
1512 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1513 ? OPTAB_WIDEN : methods);
1514 enum mode_class mclass;
1515 machine_mode wider_mode;
1516 rtx libfunc;
1517 rtx temp;
1518 rtx_insn *entry_last = get_last_insn ();
1519 rtx_insn *last;
1521 mclass = GET_MODE_CLASS (mode);
1523 /* If subtracting an integer constant, convert this into an addition of
1524 the negated constant. */
1526 if (binoptab == sub_optab && CONST_INT_P (op1))
1528 op1 = negate_rtx (mode, op1);
1529 binoptab = add_optab;
1532 /* Record where to delete back to if we backtrack. */
1533 last = get_last_insn ();
1535 /* If we can do it with a three-operand insn, do so. */
1537 if (methods != OPTAB_MUST_WIDEN
1538 && find_widening_optab_handler (binoptab, mode,
1539 widened_mode (mode, op0, op1), 1)
1540 != CODE_FOR_nothing)
1542 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1543 unsignedp, methods, last);
1544 if (temp)
1545 return temp;
1548 /* If we were trying to rotate, and that didn't work, try rotating
1549 the other direction before falling back to shifts and bitwise-or. */
1550 if (((binoptab == rotl_optab
1551 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1552 || (binoptab == rotr_optab
1553 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1554 && mclass == MODE_INT)
1556 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1557 rtx newop1;
1558 unsigned int bits = GET_MODE_PRECISION (mode);
1560 if (CONST_INT_P (op1))
1561 newop1 = GEN_INT (bits - INTVAL (op1));
1562 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1563 newop1 = negate_rtx (GET_MODE (op1), op1);
1564 else
1565 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1566 gen_int_mode (bits, GET_MODE (op1)), op1,
1567 NULL_RTX, unsignedp, OPTAB_DIRECT);
1569 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1570 target, unsignedp, methods, last);
1571 if (temp)
1572 return temp;
1575 /* If this is a multiply, see if we can do a widening operation that
1576 takes operands of this mode and makes a wider mode. */
1578 if (binoptab == smul_optab
1579 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1580 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1581 : smul_widen_optab),
1582 GET_MODE_2XWIDER_MODE (mode), mode)
1583 != CODE_FOR_nothing))
1585 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1586 unsignedp ? umul_widen_optab : smul_widen_optab,
1587 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1589 if (temp != 0)
1591 if (GET_MODE_CLASS (mode) == MODE_INT
1592 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1593 return gen_lowpart (mode, temp);
1594 else
1595 return convert_to_mode (mode, temp, unsignedp);
1599 /* If this is a vector shift by a scalar, see if we can do a vector
1600 shift by a vector. If so, broadcast the scalar into a vector. */
1601 if (mclass == MODE_VECTOR_INT)
1603 optab otheroptab = unknown_optab;
1605 if (binoptab == ashl_optab)
1606 otheroptab = vashl_optab;
1607 else if (binoptab == ashr_optab)
1608 otheroptab = vashr_optab;
1609 else if (binoptab == lshr_optab)
1610 otheroptab = vlshr_optab;
1611 else if (binoptab == rotl_optab)
1612 otheroptab = vrotl_optab;
1613 else if (binoptab == rotr_optab)
1614 otheroptab = vrotr_optab;
1616 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1618 rtx vop1 = expand_vector_broadcast (mode, op1);
1619 if (vop1)
1621 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1622 target, unsignedp, methods, last);
1623 if (temp)
1624 return temp;
1629 /* Look for a wider mode of the same class for which we think we
1630 can open-code the operation. Check for a widening multiply at the
1631 wider mode as well. */
1633 if (CLASS_HAS_WIDER_MODES_P (mclass)
1634 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1635 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1636 wider_mode != VOIDmode;
1637 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1639 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1640 || (binoptab == smul_optab
1641 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1642 && (find_widening_optab_handler ((unsignedp
1643 ? umul_widen_optab
1644 : smul_widen_optab),
1645 GET_MODE_WIDER_MODE (wider_mode),
1646 mode, 0)
1647 != CODE_FOR_nothing)))
1649 rtx xop0 = op0, xop1 = op1;
1650 int no_extend = 0;
1652 /* For certain integer operations, we need not actually extend
1653 the narrow operands, as long as we will truncate
1654 the results to the same narrowness. */
1656 if ((binoptab == ior_optab || binoptab == and_optab
1657 || binoptab == xor_optab
1658 || binoptab == add_optab || binoptab == sub_optab
1659 || binoptab == smul_optab || binoptab == ashl_optab)
1660 && mclass == MODE_INT)
1662 no_extend = 1;
1663 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1664 xop0, unsignedp);
1665 if (binoptab != ashl_optab)
1666 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1667 xop1, unsignedp);
1670 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1672 /* The second operand of a shift must always be extended. */
1673 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1674 no_extend && binoptab != ashl_optab);
1676 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1677 unsignedp, OPTAB_DIRECT);
1678 if (temp)
1680 if (mclass != MODE_INT
1681 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1683 if (target == 0)
1684 target = gen_reg_rtx (mode);
1685 convert_move (target, temp, 0);
1686 return target;
1688 else
1689 return gen_lowpart (mode, temp);
1691 else
1692 delete_insns_since (last);
1696 /* If operation is commutative,
1697 try to make the first operand a register.
1698 Even better, try to make it the same as the target.
1699 Also try to make the last operand a constant. */
1700 if (commutative_optab_p (binoptab)
1701 && swap_commutative_operands_with_target (target, op0, op1))
1702 std::swap (op0, op1);
1704 /* These can be done a word at a time. */
1705 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1706 && mclass == MODE_INT
1707 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1708 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1710 int i;
1711 rtx_insn *insns;
1713 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1714 won't be accurate, so use a new target. */
1715 if (target == 0
1716 || target == op0
1717 || target == op1
1718 || !valid_multiword_target_p (target))
1719 target = gen_reg_rtx (mode);
1721 start_sequence ();
1723 /* Do the actual arithmetic. */
1724 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1726 rtx target_piece = operand_subword (target, i, 1, mode);
1727 rtx x = expand_binop (word_mode, binoptab,
1728 operand_subword_force (op0, i, mode),
1729 operand_subword_force (op1, i, mode),
1730 target_piece, unsignedp, next_methods);
1732 if (x == 0)
1733 break;
1735 if (target_piece != x)
1736 emit_move_insn (target_piece, x);
1739 insns = get_insns ();
1740 end_sequence ();
1742 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1744 emit_insn (insns);
1745 return target;
1749 /* Synthesize double word shifts from single word shifts. */
1750 if ((binoptab == lshr_optab || binoptab == ashl_optab
1751 || binoptab == ashr_optab)
1752 && mclass == MODE_INT
1753 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1754 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1755 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1756 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1757 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1758 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1760 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1761 machine_mode op1_mode;
1763 double_shift_mask = targetm.shift_truncation_mask (mode);
1764 shift_mask = targetm.shift_truncation_mask (word_mode);
1765 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1767 /* Apply the truncation to constant shifts. */
1768 if (double_shift_mask > 0 && CONST_INT_P (op1))
1769 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1771 if (op1 == CONST0_RTX (op1_mode))
1772 return op0;
1774 /* Make sure that this is a combination that expand_doubleword_shift
1775 can handle. See the comments there for details. */
1776 if (double_shift_mask == 0
1777 || (shift_mask == BITS_PER_WORD - 1
1778 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1780 rtx_insn *insns;
1781 rtx into_target, outof_target;
1782 rtx into_input, outof_input;
1783 int left_shift, outof_word;
1785 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1786 won't be accurate, so use a new target. */
1787 if (target == 0
1788 || target == op0
1789 || target == op1
1790 || !valid_multiword_target_p (target))
1791 target = gen_reg_rtx (mode);
1793 start_sequence ();
1795 /* OUTOF_* is the word we are shifting bits away from, and
1796 INTO_* is the word that we are shifting bits towards, thus
1797 they differ depending on the direction of the shift and
1798 WORDS_BIG_ENDIAN. */
1800 left_shift = binoptab == ashl_optab;
1801 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1803 outof_target = operand_subword (target, outof_word, 1, mode);
1804 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1806 outof_input = operand_subword_force (op0, outof_word, mode);
1807 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1809 if (expand_doubleword_shift (op1_mode, binoptab,
1810 outof_input, into_input, op1,
1811 outof_target, into_target,
1812 unsignedp, next_methods, shift_mask))
1814 insns = get_insns ();
1815 end_sequence ();
1817 emit_insn (insns);
1818 return target;
1820 end_sequence ();
1824 /* Synthesize double word rotates from single word shifts. */
1825 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1826 && mclass == MODE_INT
1827 && CONST_INT_P (op1)
1828 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1829 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1830 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1832 rtx_insn *insns;
1833 rtx into_target, outof_target;
1834 rtx into_input, outof_input;
1835 rtx inter;
1836 int shift_count, left_shift, outof_word;
1838 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1839 won't be accurate, so use a new target. Do this also if target is not
1840 a REG, first because having a register instead may open optimization
1841 opportunities, and second because if target and op0 happen to be MEMs
1842 designating the same location, we would risk clobbering it too early
1843 in the code sequence we generate below. */
1844 if (target == 0
1845 || target == op0
1846 || target == op1
1847 || !REG_P (target)
1848 || !valid_multiword_target_p (target))
1849 target = gen_reg_rtx (mode);
1851 start_sequence ();
1853 shift_count = INTVAL (op1);
1855 /* OUTOF_* is the word we are shifting bits away from, and
1856 INTO_* is the word that we are shifting bits towards, thus
1857 they differ depending on the direction of the shift and
1858 WORDS_BIG_ENDIAN. */
1860 left_shift = (binoptab == rotl_optab);
1861 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1863 outof_target = operand_subword (target, outof_word, 1, mode);
1864 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1866 outof_input = operand_subword_force (op0, outof_word, mode);
1867 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1869 if (shift_count == BITS_PER_WORD)
1871 /* This is just a word swap. */
1872 emit_move_insn (outof_target, into_input);
1873 emit_move_insn (into_target, outof_input);
1874 inter = const0_rtx;
1876 else
1878 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1879 rtx first_shift_count, second_shift_count;
1880 optab reverse_unsigned_shift, unsigned_shift;
1882 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1883 ? lshr_optab : ashl_optab);
1885 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1886 ? ashl_optab : lshr_optab);
1888 if (shift_count > BITS_PER_WORD)
1890 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1891 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1893 else
1895 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1896 second_shift_count = GEN_INT (shift_count);
1899 into_temp1 = expand_binop (word_mode, unsigned_shift,
1900 outof_input, first_shift_count,
1901 NULL_RTX, unsignedp, next_methods);
1902 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1903 into_input, second_shift_count,
1904 NULL_RTX, unsignedp, next_methods);
1906 if (into_temp1 != 0 && into_temp2 != 0)
1907 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1908 into_target, unsignedp, next_methods);
1909 else
1910 inter = 0;
1912 if (inter != 0 && inter != into_target)
1913 emit_move_insn (into_target, inter);
1915 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1916 into_input, first_shift_count,
1917 NULL_RTX, unsignedp, next_methods);
1918 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1919 outof_input, second_shift_count,
1920 NULL_RTX, unsignedp, next_methods);
1922 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1923 inter = expand_binop (word_mode, ior_optab,
1924 outof_temp1, outof_temp2,
1925 outof_target, unsignedp, next_methods);
1927 if (inter != 0 && inter != outof_target)
1928 emit_move_insn (outof_target, inter);
1931 insns = get_insns ();
1932 end_sequence ();
1934 if (inter != 0)
1936 emit_insn (insns);
1937 return target;
1941 /* These can be done a word at a time by propagating carries. */
1942 if ((binoptab == add_optab || binoptab == sub_optab)
1943 && mclass == MODE_INT
1944 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1945 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1947 unsigned int i;
1948 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1949 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1950 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1951 rtx xop0, xop1, xtarget;
1953 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1954 value is one of those, use it. Otherwise, use 1 since it is the
1955 one easiest to get. */
1956 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1957 int normalizep = STORE_FLAG_VALUE;
1958 #else
1959 int normalizep = 1;
1960 #endif
1962 /* Prepare the operands. */
1963 xop0 = force_reg (mode, op0);
1964 xop1 = force_reg (mode, op1);
1966 xtarget = gen_reg_rtx (mode);
1968 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1969 target = xtarget;
1971 /* Indicate for flow that the entire target reg is being set. */
1972 if (REG_P (target))
1973 emit_clobber (xtarget);
1975 /* Do the actual arithmetic. */
1976 for (i = 0; i < nwords; i++)
1978 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1979 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1980 rtx op0_piece = operand_subword_force (xop0, index, mode);
1981 rtx op1_piece = operand_subword_force (xop1, index, mode);
1982 rtx x;
1984 /* Main add/subtract of the input operands. */
1985 x = expand_binop (word_mode, binoptab,
1986 op0_piece, op1_piece,
1987 target_piece, unsignedp, next_methods);
1988 if (x == 0)
1989 break;
1991 if (i + 1 < nwords)
1993 /* Store carry from main add/subtract. */
1994 carry_out = gen_reg_rtx (word_mode);
1995 carry_out = emit_store_flag_force (carry_out,
1996 (binoptab == add_optab
1997 ? LT : GT),
1998 x, op0_piece,
1999 word_mode, 1, normalizep);
2002 if (i > 0)
2004 rtx newx;
2006 /* Add/subtract previous carry to main result. */
2007 newx = expand_binop (word_mode,
2008 normalizep == 1 ? binoptab : otheroptab,
2009 x, carry_in,
2010 NULL_RTX, 1, next_methods);
2012 if (i + 1 < nwords)
2014 /* Get out carry from adding/subtracting carry in. */
2015 rtx carry_tmp = gen_reg_rtx (word_mode);
2016 carry_tmp = emit_store_flag_force (carry_tmp,
2017 (binoptab == add_optab
2018 ? LT : GT),
2019 newx, x,
2020 word_mode, 1, normalizep);
2022 /* Logical-ior the two poss. carry together. */
2023 carry_out = expand_binop (word_mode, ior_optab,
2024 carry_out, carry_tmp,
2025 carry_out, 0, next_methods);
2026 if (carry_out == 0)
2027 break;
2029 emit_move_insn (target_piece, newx);
2031 else
2033 if (x != target_piece)
2034 emit_move_insn (target_piece, x);
2037 carry_in = carry_out;
2040 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2042 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2043 || ! rtx_equal_p (target, xtarget))
2045 rtx_insn *temp = emit_move_insn (target, xtarget);
2047 set_dst_reg_note (temp, REG_EQUAL,
2048 gen_rtx_fmt_ee (optab_to_code (binoptab),
2049 mode, copy_rtx (xop0),
2050 copy_rtx (xop1)),
2051 target);
2053 else
2054 target = xtarget;
2056 return target;
2059 else
2060 delete_insns_since (last);
2063 /* Attempt to synthesize double word multiplies using a sequence of word
2064 mode multiplications. We first attempt to generate a sequence using a
2065 more efficient unsigned widening multiply, and if that fails we then
2066 try using a signed widening multiply. */
2068 if (binoptab == smul_optab
2069 && mclass == MODE_INT
2070 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2071 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2072 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2074 rtx product = NULL_RTX;
2075 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
2076 != CODE_FOR_nothing)
2078 product = expand_doubleword_mult (mode, op0, op1, target,
2079 true, methods);
2080 if (!product)
2081 delete_insns_since (last);
2084 if (product == NULL_RTX
2085 && widening_optab_handler (smul_widen_optab, mode, word_mode)
2086 != CODE_FOR_nothing)
2088 product = expand_doubleword_mult (mode, op0, op1, target,
2089 false, methods);
2090 if (!product)
2091 delete_insns_since (last);
2094 if (product != NULL_RTX)
2096 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2098 temp = emit_move_insn (target ? target : product, product);
2099 set_dst_reg_note (temp,
2100 REG_EQUAL,
2101 gen_rtx_fmt_ee (MULT, mode,
2102 copy_rtx (op0),
2103 copy_rtx (op1)),
2104 target ? target : product);
2106 return product;
2110 /* It can't be open-coded in this mode.
2111 Use a library call if one is available and caller says that's ok. */
2113 libfunc = optab_libfunc (binoptab, mode);
2114 if (libfunc
2115 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2117 rtx_insn *insns;
2118 rtx op1x = op1;
2119 machine_mode op1_mode = mode;
2120 rtx value;
2122 start_sequence ();
2124 if (shift_optab_p (binoptab))
2126 op1_mode = targetm.libgcc_shift_count_mode ();
2127 /* Specify unsigned here,
2128 since negative shift counts are meaningless. */
2129 op1x = convert_to_mode (op1_mode, op1, 1);
2132 if (GET_MODE (op0) != VOIDmode
2133 && GET_MODE (op0) != mode)
2134 op0 = convert_to_mode (mode, op0, unsignedp);
2136 /* Pass 1 for NO_QUEUE so we don't lose any increments
2137 if the libcall is cse'd or moved. */
2138 value = emit_library_call_value (libfunc,
2139 NULL_RTX, LCT_CONST, mode, 2,
2140 op0, mode, op1x, op1_mode);
2142 insns = get_insns ();
2143 end_sequence ();
2145 target = gen_reg_rtx (mode);
2146 emit_libcall_block_1 (insns, target, value,
2147 gen_rtx_fmt_ee (optab_to_code (binoptab),
2148 mode, op0, op1),
2149 trapv_binoptab_p (binoptab));
2151 return target;
2154 delete_insns_since (last);
2156 /* It can't be done in this mode. Can we do it in a wider mode? */
2158 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2159 || methods == OPTAB_MUST_WIDEN))
2161 /* Caller says, don't even try. */
2162 delete_insns_since (entry_last);
2163 return 0;
2166 /* Compute the value of METHODS to pass to recursive calls.
2167 Don't allow widening to be tried recursively. */
2169 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2171 /* Look for a wider mode of the same class for which it appears we can do
2172 the operation. */
2174 if (CLASS_HAS_WIDER_MODES_P (mclass))
2176 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2177 wider_mode != VOIDmode;
2178 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2180 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2181 != CODE_FOR_nothing
2182 || (methods == OPTAB_LIB
2183 && optab_libfunc (binoptab, wider_mode)))
2185 rtx xop0 = op0, xop1 = op1;
2186 int no_extend = 0;
2188 /* For certain integer operations, we need not actually extend
2189 the narrow operands, as long as we will truncate
2190 the results to the same narrowness. */
2192 if ((binoptab == ior_optab || binoptab == and_optab
2193 || binoptab == xor_optab
2194 || binoptab == add_optab || binoptab == sub_optab
2195 || binoptab == smul_optab || binoptab == ashl_optab)
2196 && mclass == MODE_INT)
2197 no_extend = 1;
2199 xop0 = widen_operand (xop0, wider_mode, mode,
2200 unsignedp, no_extend);
2202 /* The second operand of a shift must always be extended. */
2203 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2204 no_extend && binoptab != ashl_optab);
2206 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2207 unsignedp, methods);
2208 if (temp)
2210 if (mclass != MODE_INT
2211 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2213 if (target == 0)
2214 target = gen_reg_rtx (mode);
2215 convert_move (target, temp, 0);
2216 return target;
2218 else
2219 return gen_lowpart (mode, temp);
2221 else
2222 delete_insns_since (last);
2227 delete_insns_since (entry_last);
2228 return 0;
2231 /* Expand a binary operator which has both signed and unsigned forms.
2232 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2233 signed operations.
2235 If we widen unsigned operands, we may use a signed wider operation instead
2236 of an unsigned wider operation, since the result would be the same. */
2239 sign_expand_binop (machine_mode mode, optab uoptab, optab soptab,
2240 rtx op0, rtx op1, rtx target, int unsignedp,
2241 enum optab_methods methods)
2243 rtx temp;
2244 optab direct_optab = unsignedp ? uoptab : soptab;
2245 bool save_enable;
2247 /* Do it without widening, if possible. */
2248 temp = expand_binop (mode, direct_optab, op0, op1, target,
2249 unsignedp, OPTAB_DIRECT);
2250 if (temp || methods == OPTAB_DIRECT)
2251 return temp;
2253 /* Try widening to a signed int. Disable any direct use of any
2254 signed insn in the current mode. */
2255 save_enable = swap_optab_enable (soptab, mode, false);
2257 temp = expand_binop (mode, soptab, op0, op1, target,
2258 unsignedp, OPTAB_WIDEN);
2260 /* For unsigned operands, try widening to an unsigned int. */
2261 if (!temp && unsignedp)
2262 temp = expand_binop (mode, uoptab, op0, op1, target,
2263 unsignedp, OPTAB_WIDEN);
2264 if (temp || methods == OPTAB_WIDEN)
2265 goto egress;
2267 /* Use the right width libcall if that exists. */
2268 temp = expand_binop (mode, direct_optab, op0, op1, target,
2269 unsignedp, OPTAB_LIB);
2270 if (temp || methods == OPTAB_LIB)
2271 goto egress;
2273 /* Must widen and use a libcall, use either signed or unsigned. */
2274 temp = expand_binop (mode, soptab, op0, op1, target,
2275 unsignedp, methods);
2276 if (!temp && unsignedp)
2277 temp = expand_binop (mode, uoptab, op0, op1, target,
2278 unsignedp, methods);
2280 egress:
2281 /* Undo the fiddling above. */
2282 if (save_enable)
2283 swap_optab_enable (soptab, mode, true);
2284 return temp;
2287 /* Generate code to perform an operation specified by UNOPPTAB
2288 on operand OP0, with two results to TARG0 and TARG1.
2289 We assume that the order of the operands for the instruction
2290 is TARG0, TARG1, OP0.
2292 Either TARG0 or TARG1 may be zero, but what that means is that
2293 the result is not actually wanted. We will generate it into
2294 a dummy pseudo-reg and discard it. They may not both be zero.
2296 Returns 1 if this operation can be performed; 0 if not. */
2299 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2300 int unsignedp)
2302 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2303 enum mode_class mclass;
2304 machine_mode wider_mode;
2305 rtx_insn *entry_last = get_last_insn ();
2306 rtx_insn *last;
2308 mclass = GET_MODE_CLASS (mode);
2310 if (!targ0)
2311 targ0 = gen_reg_rtx (mode);
2312 if (!targ1)
2313 targ1 = gen_reg_rtx (mode);
2315 /* Record where to go back to if we fail. */
2316 last = get_last_insn ();
2318 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2320 struct expand_operand ops[3];
2321 enum insn_code icode = optab_handler (unoptab, mode);
2323 create_fixed_operand (&ops[0], targ0);
2324 create_fixed_operand (&ops[1], targ1);
2325 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2326 if (maybe_expand_insn (icode, 3, ops))
2327 return 1;
2330 /* It can't be done in this mode. Can we do it in a wider mode? */
2332 if (CLASS_HAS_WIDER_MODES_P (mclass))
2334 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2335 wider_mode != VOIDmode;
2336 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2338 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2340 rtx t0 = gen_reg_rtx (wider_mode);
2341 rtx t1 = gen_reg_rtx (wider_mode);
2342 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2344 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2346 convert_move (targ0, t0, unsignedp);
2347 convert_move (targ1, t1, unsignedp);
2348 return 1;
2350 else
2351 delete_insns_since (last);
2356 delete_insns_since (entry_last);
2357 return 0;
2360 /* Generate code to perform an operation specified by BINOPTAB
2361 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2362 We assume that the order of the operands for the instruction
2363 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2364 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2366 Either TARG0 or TARG1 may be zero, but what that means is that
2367 the result is not actually wanted. We will generate it into
2368 a dummy pseudo-reg and discard it. They may not both be zero.
2370 Returns 1 if this operation can be performed; 0 if not. */
2373 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2374 int unsignedp)
2376 machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2377 enum mode_class mclass;
2378 machine_mode wider_mode;
2379 rtx_insn *entry_last = get_last_insn ();
2380 rtx_insn *last;
2382 mclass = GET_MODE_CLASS (mode);
2384 if (!targ0)
2385 targ0 = gen_reg_rtx (mode);
2386 if (!targ1)
2387 targ1 = gen_reg_rtx (mode);
2389 /* Record where to go back to if we fail. */
2390 last = get_last_insn ();
2392 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2394 struct expand_operand ops[4];
2395 enum insn_code icode = optab_handler (binoptab, mode);
2396 machine_mode mode0 = insn_data[icode].operand[1].mode;
2397 machine_mode mode1 = insn_data[icode].operand[2].mode;
2398 rtx xop0 = op0, xop1 = op1;
2400 /* If we are optimizing, force expensive constants into a register. */
2401 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2402 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2404 create_fixed_operand (&ops[0], targ0);
2405 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2406 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2407 create_fixed_operand (&ops[3], targ1);
2408 if (maybe_expand_insn (icode, 4, ops))
2409 return 1;
2410 delete_insns_since (last);
2413 /* It can't be done in this mode. Can we do it in a wider mode? */
2415 if (CLASS_HAS_WIDER_MODES_P (mclass))
2417 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2418 wider_mode != VOIDmode;
2419 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2421 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2423 rtx t0 = gen_reg_rtx (wider_mode);
2424 rtx t1 = gen_reg_rtx (wider_mode);
2425 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2426 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2428 if (expand_twoval_binop (binoptab, cop0, cop1,
2429 t0, t1, unsignedp))
2431 convert_move (targ0, t0, unsignedp);
2432 convert_move (targ1, t1, unsignedp);
2433 return 1;
2435 else
2436 delete_insns_since (last);
2441 delete_insns_since (entry_last);
2442 return 0;
2445 /* Expand the two-valued library call indicated by BINOPTAB, but
2446 preserve only one of the values. If TARG0 is non-NULL, the first
2447 value is placed into TARG0; otherwise the second value is placed
2448 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2449 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2450 This routine assumes that the value returned by the library call is
2451 as if the return value was of an integral mode twice as wide as the
2452 mode of OP0. Returns 1 if the call was successful. */
2454 bool
2455 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2456 rtx targ0, rtx targ1, enum rtx_code code)
2458 machine_mode mode;
2459 machine_mode libval_mode;
2460 rtx libval;
2461 rtx_insn *insns;
2462 rtx libfunc;
2464 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2465 gcc_assert (!targ0 != !targ1);
2467 mode = GET_MODE (op0);
2468 libfunc = optab_libfunc (binoptab, mode);
2469 if (!libfunc)
2470 return false;
2472 /* The value returned by the library function will have twice as
2473 many bits as the nominal MODE. */
2474 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2475 MODE_INT);
2476 start_sequence ();
2477 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2478 libval_mode, 2,
2479 op0, mode,
2480 op1, mode);
2481 /* Get the part of VAL containing the value that we want. */
2482 libval = simplify_gen_subreg (mode, libval, libval_mode,
2483 targ0 ? 0 : GET_MODE_SIZE (mode));
2484 insns = get_insns ();
2485 end_sequence ();
2486 /* Move the into the desired location. */
2487 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2488 gen_rtx_fmt_ee (code, mode, op0, op1));
2490 return true;
2494 /* Wrapper around expand_unop which takes an rtx code to specify
2495 the operation to perform, not an optab pointer. All other
2496 arguments are the same. */
2498 expand_simple_unop (machine_mode mode, enum rtx_code code, rtx op0,
2499 rtx target, int unsignedp)
2501 optab unop = code_to_optab (code);
2502 gcc_assert (unop);
2504 return expand_unop (mode, unop, op0, target, unsignedp);
2507 /* Try calculating
2508 (clz:narrow x)
2510 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2512 A similar operation can be used for clrsb. UNOPTAB says which operation
2513 we are trying to expand. */
2514 static rtx
2515 widen_leading (machine_mode mode, rtx op0, rtx target, optab unoptab)
2517 enum mode_class mclass = GET_MODE_CLASS (mode);
2518 if (CLASS_HAS_WIDER_MODES_P (mclass))
2520 machine_mode wider_mode;
2521 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2522 wider_mode != VOIDmode;
2523 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2525 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2527 rtx xop0, temp;
2528 rtx_insn *last;
2530 last = get_last_insn ();
2532 if (target == 0)
2533 target = gen_reg_rtx (mode);
2534 xop0 = widen_operand (op0, wider_mode, mode,
2535 unoptab != clrsb_optab, false);
2536 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2537 unoptab != clrsb_optab);
2538 if (temp != 0)
2539 temp = expand_binop
2540 (wider_mode, sub_optab, temp,
2541 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2542 - GET_MODE_PRECISION (mode),
2543 wider_mode),
2544 target, true, OPTAB_DIRECT);
2545 if (temp == 0)
2546 delete_insns_since (last);
2548 return temp;
2552 return 0;
2555 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2556 quantities, choosing which based on whether the high word is nonzero. */
2557 static rtx
2558 expand_doubleword_clz (machine_mode mode, rtx op0, rtx target)
2560 rtx xop0 = force_reg (mode, op0);
2561 rtx subhi = gen_highpart (word_mode, xop0);
2562 rtx sublo = gen_lowpart (word_mode, xop0);
2563 rtx_code_label *hi0_label = gen_label_rtx ();
2564 rtx_code_label *after_label = gen_label_rtx ();
2565 rtx_insn *seq;
2566 rtx temp, result;
2568 /* If we were not given a target, use a word_mode register, not a
2569 'mode' register. The result will fit, and nobody is expecting
2570 anything bigger (the return type of __builtin_clz* is int). */
2571 if (!target)
2572 target = gen_reg_rtx (word_mode);
2574 /* In any case, write to a word_mode scratch in both branches of the
2575 conditional, so we can ensure there is a single move insn setting
2576 'target' to tag a REG_EQUAL note on. */
2577 result = gen_reg_rtx (word_mode);
2579 start_sequence ();
2581 /* If the high word is not equal to zero,
2582 then clz of the full value is clz of the high word. */
2583 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2584 word_mode, true, hi0_label);
2586 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2587 if (!temp)
2588 goto fail;
2590 if (temp != result)
2591 convert_move (result, temp, true);
2593 emit_jump_insn (gen_jump (after_label));
2594 emit_barrier ();
2596 /* Else clz of the full value is clz of the low word plus the number
2597 of bits in the high word. */
2598 emit_label (hi0_label);
2600 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2601 if (!temp)
2602 goto fail;
2603 temp = expand_binop (word_mode, add_optab, temp,
2604 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2605 result, true, OPTAB_DIRECT);
2606 if (!temp)
2607 goto fail;
2608 if (temp != result)
2609 convert_move (result, temp, true);
2611 emit_label (after_label);
2612 convert_move (target, result, true);
2614 seq = get_insns ();
2615 end_sequence ();
2617 add_equal_note (seq, target, CLZ, xop0, 0);
2618 emit_insn (seq);
2619 return target;
2621 fail:
2622 end_sequence ();
2623 return 0;
2626 /* Try calculating
2627 (bswap:narrow x)
2629 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2630 static rtx
2631 widen_bswap (machine_mode mode, rtx op0, rtx target)
2633 enum mode_class mclass = GET_MODE_CLASS (mode);
2634 machine_mode wider_mode;
2635 rtx x;
2636 rtx_insn *last;
2638 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2639 return NULL_RTX;
2641 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2642 wider_mode != VOIDmode;
2643 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2644 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2645 goto found;
2646 return NULL_RTX;
2648 found:
2649 last = get_last_insn ();
2651 x = widen_operand (op0, wider_mode, mode, true, true);
2652 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2654 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2655 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2656 if (x != 0)
2657 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2658 GET_MODE_BITSIZE (wider_mode)
2659 - GET_MODE_BITSIZE (mode),
2660 NULL_RTX, true);
2662 if (x != 0)
2664 if (target == 0)
2665 target = gen_reg_rtx (mode);
2666 emit_move_insn (target, gen_lowpart (mode, x));
2668 else
2669 delete_insns_since (last);
2671 return target;
2674 /* Try calculating bswap as two bswaps of two word-sized operands. */
2676 static rtx
2677 expand_doubleword_bswap (machine_mode mode, rtx op, rtx target)
2679 rtx t0, t1;
2681 t1 = expand_unop (word_mode, bswap_optab,
2682 operand_subword_force (op, 0, mode), NULL_RTX, true);
2683 t0 = expand_unop (word_mode, bswap_optab,
2684 operand_subword_force (op, 1, mode), NULL_RTX, true);
2686 if (target == 0 || !valid_multiword_target_p (target))
2687 target = gen_reg_rtx (mode);
2688 if (REG_P (target))
2689 emit_clobber (target);
2690 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2691 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2693 return target;
2696 /* Try calculating (parity x) as (and (popcount x) 1), where
2697 popcount can also be done in a wider mode. */
2698 static rtx
2699 expand_parity (machine_mode mode, rtx op0, rtx target)
2701 enum mode_class mclass = GET_MODE_CLASS (mode);
2702 if (CLASS_HAS_WIDER_MODES_P (mclass))
2704 machine_mode wider_mode;
2705 for (wider_mode = mode; wider_mode != VOIDmode;
2706 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2708 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2710 rtx xop0, temp;
2711 rtx_insn *last;
2713 last = get_last_insn ();
2715 if (target == 0)
2716 target = gen_reg_rtx (mode);
2717 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2718 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2719 true);
2720 if (temp != 0)
2721 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2722 target, true, OPTAB_DIRECT);
2723 if (temp == 0)
2724 delete_insns_since (last);
2726 return temp;
2730 return 0;
2733 /* Try calculating ctz(x) as K - clz(x & -x) ,
2734 where K is GET_MODE_PRECISION(mode) - 1.
2736 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2737 don't have to worry about what the hardware does in that case. (If
2738 the clz instruction produces the usual value at 0, which is K, the
2739 result of this code sequence will be -1; expand_ffs, below, relies
2740 on this. It might be nice to have it be K instead, for consistency
2741 with the (very few) processors that provide a ctz with a defined
2742 value, but that would take one more instruction, and it would be
2743 less convenient for expand_ffs anyway. */
2745 static rtx
2746 expand_ctz (machine_mode mode, rtx op0, rtx target)
2748 rtx_insn *seq;
2749 rtx temp;
2751 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2752 return 0;
2754 start_sequence ();
2756 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2757 if (temp)
2758 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2759 true, OPTAB_DIRECT);
2760 if (temp)
2761 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2762 if (temp)
2763 temp = expand_binop (mode, sub_optab,
2764 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2765 temp, target,
2766 true, OPTAB_DIRECT);
2767 if (temp == 0)
2769 end_sequence ();
2770 return 0;
2773 seq = get_insns ();
2774 end_sequence ();
2776 add_equal_note (seq, temp, CTZ, op0, 0);
2777 emit_insn (seq);
2778 return temp;
2782 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2783 else with the sequence used by expand_clz.
2785 The ffs builtin promises to return zero for a zero value and ctz/clz
2786 may have an undefined value in that case. If they do not give us a
2787 convenient value, we have to generate a test and branch. */
2788 static rtx
2789 expand_ffs (machine_mode mode, rtx op0, rtx target)
2791 HOST_WIDE_INT val = 0;
2792 bool defined_at_zero = false;
2793 rtx temp;
2794 rtx_insn *seq;
2796 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2798 start_sequence ();
2800 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2801 if (!temp)
2802 goto fail;
2804 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2806 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2808 start_sequence ();
2809 temp = expand_ctz (mode, op0, 0);
2810 if (!temp)
2811 goto fail;
2813 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2815 defined_at_zero = true;
2816 val = (GET_MODE_PRECISION (mode) - 1) - val;
2819 else
2820 return 0;
2822 if (defined_at_zero && val == -1)
2823 /* No correction needed at zero. */;
2824 else
2826 /* We don't try to do anything clever with the situation found
2827 on some processors (eg Alpha) where ctz(0:mode) ==
2828 bitsize(mode). If someone can think of a way to send N to -1
2829 and leave alone all values in the range 0..N-1 (where N is a
2830 power of two), cheaper than this test-and-branch, please add it.
2832 The test-and-branch is done after the operation itself, in case
2833 the operation sets condition codes that can be recycled for this.
2834 (This is true on i386, for instance.) */
2836 rtx_code_label *nonzero_label = gen_label_rtx ();
2837 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2838 mode, true, nonzero_label);
2840 convert_move (temp, GEN_INT (-1), false);
2841 emit_label (nonzero_label);
2844 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2845 to produce a value in the range 0..bitsize. */
2846 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2847 target, false, OPTAB_DIRECT);
2848 if (!temp)
2849 goto fail;
2851 seq = get_insns ();
2852 end_sequence ();
2854 add_equal_note (seq, temp, FFS, op0, 0);
2855 emit_insn (seq);
2856 return temp;
2858 fail:
2859 end_sequence ();
2860 return 0;
2863 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2864 conditions, VAL may already be a SUBREG against which we cannot generate
2865 a further SUBREG. In this case, we expect forcing the value into a
2866 register will work around the situation. */
2868 static rtx
2869 lowpart_subreg_maybe_copy (machine_mode omode, rtx val,
2870 machine_mode imode)
2872 rtx ret;
2873 ret = lowpart_subreg (omode, val, imode);
2874 if (ret == NULL)
2876 val = force_reg (imode, val);
2877 ret = lowpart_subreg (omode, val, imode);
2878 gcc_assert (ret != NULL);
2880 return ret;
2883 /* Expand a floating point absolute value or negation operation via a
2884 logical operation on the sign bit. */
2886 static rtx
2887 expand_absneg_bit (enum rtx_code code, machine_mode mode,
2888 rtx op0, rtx target)
2890 const struct real_format *fmt;
2891 int bitpos, word, nwords, i;
2892 machine_mode imode;
2893 rtx temp;
2894 rtx_insn *insns;
2896 /* The format has to have a simple sign bit. */
2897 fmt = REAL_MODE_FORMAT (mode);
2898 if (fmt == NULL)
2899 return NULL_RTX;
2901 bitpos = fmt->signbit_rw;
2902 if (bitpos < 0)
2903 return NULL_RTX;
2905 /* Don't create negative zeros if the format doesn't support them. */
2906 if (code == NEG && !fmt->has_signed_zero)
2907 return NULL_RTX;
2909 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2911 imode = int_mode_for_mode (mode);
2912 if (imode == BLKmode)
2913 return NULL_RTX;
2914 word = 0;
2915 nwords = 1;
2917 else
2919 imode = word_mode;
2921 if (FLOAT_WORDS_BIG_ENDIAN)
2922 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2923 else
2924 word = bitpos / BITS_PER_WORD;
2925 bitpos = bitpos % BITS_PER_WORD;
2926 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2929 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
2930 if (code == ABS)
2931 mask = ~mask;
2933 if (target == 0
2934 || target == op0
2935 || (nwords > 1 && !valid_multiword_target_p (target)))
2936 target = gen_reg_rtx (mode);
2938 if (nwords > 1)
2940 start_sequence ();
2942 for (i = 0; i < nwords; ++i)
2944 rtx targ_piece = operand_subword (target, i, 1, mode);
2945 rtx op0_piece = operand_subword_force (op0, i, mode);
2947 if (i == word)
2949 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2950 op0_piece,
2951 immed_wide_int_const (mask, imode),
2952 targ_piece, 1, OPTAB_LIB_WIDEN);
2953 if (temp != targ_piece)
2954 emit_move_insn (targ_piece, temp);
2956 else
2957 emit_move_insn (targ_piece, op0_piece);
2960 insns = get_insns ();
2961 end_sequence ();
2963 emit_insn (insns);
2965 else
2967 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2968 gen_lowpart (imode, op0),
2969 immed_wide_int_const (mask, imode),
2970 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2971 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2973 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2974 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2975 target);
2978 return target;
2981 /* As expand_unop, but will fail rather than attempt the operation in a
2982 different mode or with a libcall. */
2983 static rtx
2984 expand_unop_direct (machine_mode mode, optab unoptab, rtx op0, rtx target,
2985 int unsignedp)
2987 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2989 struct expand_operand ops[2];
2990 enum insn_code icode = optab_handler (unoptab, mode);
2991 rtx_insn *last = get_last_insn ();
2992 rtx_insn *pat;
2994 create_output_operand (&ops[0], target, mode);
2995 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2996 pat = maybe_gen_insn (icode, 2, ops);
2997 if (pat)
2999 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3000 && ! add_equal_note (pat, ops[0].value,
3001 optab_to_code (unoptab),
3002 ops[1].value, NULL_RTX))
3004 delete_insns_since (last);
3005 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3008 emit_insn (pat);
3010 return ops[0].value;
3013 return 0;
3016 /* Generate code to perform an operation specified by UNOPTAB
3017 on operand OP0, with result having machine-mode MODE.
3019 UNSIGNEDP is for the case where we have to widen the operands
3020 to perform the operation. It says to use zero-extension.
3022 If TARGET is nonzero, the value
3023 is generated there, if it is convenient to do so.
3024 In all cases an rtx is returned for the locus of the value;
3025 this may or may not be TARGET. */
3028 expand_unop (machine_mode mode, optab unoptab, rtx op0, rtx target,
3029 int unsignedp)
3031 enum mode_class mclass = GET_MODE_CLASS (mode);
3032 machine_mode wider_mode;
3033 rtx temp;
3034 rtx libfunc;
3036 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3037 if (temp)
3038 return temp;
3040 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3042 /* Widening (or narrowing) clz needs special treatment. */
3043 if (unoptab == clz_optab)
3045 temp = widen_leading (mode, op0, target, unoptab);
3046 if (temp)
3047 return temp;
3049 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3050 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3052 temp = expand_doubleword_clz (mode, op0, target);
3053 if (temp)
3054 return temp;
3057 goto try_libcall;
3060 if (unoptab == clrsb_optab)
3062 temp = widen_leading (mode, op0, target, unoptab);
3063 if (temp)
3064 return temp;
3065 goto try_libcall;
3068 /* Widening (or narrowing) bswap needs special treatment. */
3069 if (unoptab == bswap_optab)
3071 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3072 or ROTATERT. First try these directly; if this fails, then try the
3073 obvious pair of shifts with allowed widening, as this will probably
3074 be always more efficient than the other fallback methods. */
3075 if (mode == HImode)
3077 rtx_insn *last;
3078 rtx temp1, temp2;
3080 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3082 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
3083 unsignedp, OPTAB_DIRECT);
3084 if (temp)
3085 return temp;
3088 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3090 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
3091 unsignedp, OPTAB_DIRECT);
3092 if (temp)
3093 return temp;
3096 last = get_last_insn ();
3098 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
3099 unsignedp, OPTAB_WIDEN);
3100 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
3101 unsignedp, OPTAB_WIDEN);
3102 if (temp1 && temp2)
3104 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3105 unsignedp, OPTAB_WIDEN);
3106 if (temp)
3107 return temp;
3110 delete_insns_since (last);
3113 temp = widen_bswap (mode, op0, target);
3114 if (temp)
3115 return temp;
3117 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3118 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3120 temp = expand_doubleword_bswap (mode, op0, target);
3121 if (temp)
3122 return temp;
3125 goto try_libcall;
3128 if (CLASS_HAS_WIDER_MODES_P (mclass))
3129 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3130 wider_mode != VOIDmode;
3131 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3133 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3135 rtx xop0 = op0;
3136 rtx_insn *last = get_last_insn ();
3138 /* For certain operations, we need not actually extend
3139 the narrow operand, as long as we will truncate the
3140 results to the same narrowness. */
3142 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3143 (unoptab == neg_optab
3144 || unoptab == one_cmpl_optab)
3145 && mclass == MODE_INT);
3147 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3148 unsignedp);
3150 if (temp)
3152 if (mclass != MODE_INT
3153 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3155 if (target == 0)
3156 target = gen_reg_rtx (mode);
3157 convert_move (target, temp, 0);
3158 return target;
3160 else
3161 return gen_lowpart (mode, temp);
3163 else
3164 delete_insns_since (last);
3168 /* These can be done a word at a time. */
3169 if (unoptab == one_cmpl_optab
3170 && mclass == MODE_INT
3171 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3172 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3174 int i;
3175 rtx_insn *insns;
3177 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3178 target = gen_reg_rtx (mode);
3180 start_sequence ();
3182 /* Do the actual arithmetic. */
3183 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3185 rtx target_piece = operand_subword (target, i, 1, mode);
3186 rtx x = expand_unop (word_mode, unoptab,
3187 operand_subword_force (op0, i, mode),
3188 target_piece, unsignedp);
3190 if (target_piece != x)
3191 emit_move_insn (target_piece, x);
3194 insns = get_insns ();
3195 end_sequence ();
3197 emit_insn (insns);
3198 return target;
3201 if (optab_to_code (unoptab) == NEG)
3203 /* Try negating floating point values by flipping the sign bit. */
3204 if (SCALAR_FLOAT_MODE_P (mode))
3206 temp = expand_absneg_bit (NEG, mode, op0, target);
3207 if (temp)
3208 return temp;
3211 /* If there is no negation pattern, and we have no negative zero,
3212 try subtracting from zero. */
3213 if (!HONOR_SIGNED_ZEROS (mode))
3215 temp = expand_binop (mode, (unoptab == negv_optab
3216 ? subv_optab : sub_optab),
3217 CONST0_RTX (mode), op0, target,
3218 unsignedp, OPTAB_DIRECT);
3219 if (temp)
3220 return temp;
3224 /* Try calculating parity (x) as popcount (x) % 2. */
3225 if (unoptab == parity_optab)
3227 temp = expand_parity (mode, op0, target);
3228 if (temp)
3229 return temp;
3232 /* Try implementing ffs (x) in terms of clz (x). */
3233 if (unoptab == ffs_optab)
3235 temp = expand_ffs (mode, op0, target);
3236 if (temp)
3237 return temp;
3240 /* Try implementing ctz (x) in terms of clz (x). */
3241 if (unoptab == ctz_optab)
3243 temp = expand_ctz (mode, op0, target);
3244 if (temp)
3245 return temp;
3248 try_libcall:
3249 /* Now try a library call in this mode. */
3250 libfunc = optab_libfunc (unoptab, mode);
3251 if (libfunc)
3253 rtx_insn *insns;
3254 rtx value;
3255 rtx eq_value;
3256 machine_mode outmode = mode;
3258 /* All of these functions return small values. Thus we choose to
3259 have them return something that isn't a double-word. */
3260 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3261 || unoptab == clrsb_optab || unoptab == popcount_optab
3262 || unoptab == parity_optab)
3263 outmode
3264 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3265 optab_libfunc (unoptab, mode)));
3267 start_sequence ();
3269 /* Pass 1 for NO_QUEUE so we don't lose any increments
3270 if the libcall is cse'd or moved. */
3271 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3272 1, op0, mode);
3273 insns = get_insns ();
3274 end_sequence ();
3276 target = gen_reg_rtx (outmode);
3277 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3278 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3279 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3280 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3281 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3282 emit_libcall_block_1 (insns, target, value, eq_value,
3283 trapv_unoptab_p (unoptab));
3285 return target;
3288 /* It can't be done in this mode. Can we do it in a wider mode? */
3290 if (CLASS_HAS_WIDER_MODES_P (mclass))
3292 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3293 wider_mode != VOIDmode;
3294 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3296 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3297 || optab_libfunc (unoptab, wider_mode))
3299 rtx xop0 = op0;
3300 rtx_insn *last = get_last_insn ();
3302 /* For certain operations, we need not actually extend
3303 the narrow operand, as long as we will truncate the
3304 results to the same narrowness. */
3305 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3306 (unoptab == neg_optab
3307 || unoptab == one_cmpl_optab
3308 || unoptab == bswap_optab)
3309 && mclass == MODE_INT);
3311 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3312 unsignedp);
3314 /* If we are generating clz using wider mode, adjust the
3315 result. Similarly for clrsb. */
3316 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3317 && temp != 0)
3318 temp = expand_binop
3319 (wider_mode, sub_optab, temp,
3320 gen_int_mode (GET_MODE_PRECISION (wider_mode)
3321 - GET_MODE_PRECISION (mode),
3322 wider_mode),
3323 target, true, OPTAB_DIRECT);
3325 /* Likewise for bswap. */
3326 if (unoptab == bswap_optab && temp != 0)
3328 gcc_assert (GET_MODE_PRECISION (wider_mode)
3329 == GET_MODE_BITSIZE (wider_mode)
3330 && GET_MODE_PRECISION (mode)
3331 == GET_MODE_BITSIZE (mode));
3333 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3334 GET_MODE_BITSIZE (wider_mode)
3335 - GET_MODE_BITSIZE (mode),
3336 NULL_RTX, true);
3339 if (temp)
3341 if (mclass != MODE_INT)
3343 if (target == 0)
3344 target = gen_reg_rtx (mode);
3345 convert_move (target, temp, 0);
3346 return target;
3348 else
3349 return gen_lowpart (mode, temp);
3351 else
3352 delete_insns_since (last);
3357 /* One final attempt at implementing negation via subtraction,
3358 this time allowing widening of the operand. */
3359 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3361 rtx temp;
3362 temp = expand_binop (mode,
3363 unoptab == negv_optab ? subv_optab : sub_optab,
3364 CONST0_RTX (mode), op0,
3365 target, unsignedp, OPTAB_LIB_WIDEN);
3366 if (temp)
3367 return temp;
3370 return 0;
3373 /* Emit code to compute the absolute value of OP0, with result to
3374 TARGET if convenient. (TARGET may be 0.) The return value says
3375 where the result actually is to be found.
3377 MODE is the mode of the operand; the mode of the result is
3378 different but can be deduced from MODE.
3383 expand_abs_nojump (machine_mode mode, rtx op0, rtx target,
3384 int result_unsignedp)
3386 rtx temp;
3388 if (GET_MODE_CLASS (mode) != MODE_INT
3389 || ! flag_trapv)
3390 result_unsignedp = 1;
3392 /* First try to do it with a special abs instruction. */
3393 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3394 op0, target, 0);
3395 if (temp != 0)
3396 return temp;
3398 /* For floating point modes, try clearing the sign bit. */
3399 if (SCALAR_FLOAT_MODE_P (mode))
3401 temp = expand_absneg_bit (ABS, mode, op0, target);
3402 if (temp)
3403 return temp;
3406 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3407 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3408 && !HONOR_SIGNED_ZEROS (mode))
3410 rtx_insn *last = get_last_insn ();
3412 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3413 op0, NULL_RTX, 0);
3414 if (temp != 0)
3415 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3416 OPTAB_WIDEN);
3418 if (temp != 0)
3419 return temp;
3421 delete_insns_since (last);
3424 /* If this machine has expensive jumps, we can do integer absolute
3425 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3426 where W is the width of MODE. */
3428 if (GET_MODE_CLASS (mode) == MODE_INT
3429 && BRANCH_COST (optimize_insn_for_speed_p (),
3430 false) >= 2)
3432 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3433 GET_MODE_PRECISION (mode) - 1,
3434 NULL_RTX, 0);
3436 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3437 OPTAB_LIB_WIDEN);
3438 if (temp != 0)
3439 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3440 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3442 if (temp != 0)
3443 return temp;
3446 return NULL_RTX;
3450 expand_abs (machine_mode mode, rtx op0, rtx target,
3451 int result_unsignedp, int safe)
3453 rtx temp;
3454 rtx_code_label *op1;
3456 if (GET_MODE_CLASS (mode) != MODE_INT
3457 || ! flag_trapv)
3458 result_unsignedp = 1;
3460 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3461 if (temp != 0)
3462 return temp;
3464 /* If that does not win, use conditional jump and negate. */
3466 /* It is safe to use the target if it is the same
3467 as the source if this is also a pseudo register */
3468 if (op0 == target && REG_P (op0)
3469 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3470 safe = 1;
3472 op1 = gen_label_rtx ();
3473 if (target == 0 || ! safe
3474 || GET_MODE (target) != mode
3475 || (MEM_P (target) && MEM_VOLATILE_P (target))
3476 || (REG_P (target)
3477 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3478 target = gen_reg_rtx (mode);
3480 emit_move_insn (target, op0);
3481 NO_DEFER_POP;
3483 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3484 NULL_RTX, NULL, op1, -1);
3486 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3487 target, target, 0);
3488 if (op0 != target)
3489 emit_move_insn (target, op0);
3490 emit_label (op1);
3491 OK_DEFER_POP;
3492 return target;
3495 /* Emit code to compute the one's complement absolute value of OP0
3496 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3497 (TARGET may be NULL_RTX.) The return value says where the result
3498 actually is to be found.
3500 MODE is the mode of the operand; the mode of the result is
3501 different but can be deduced from MODE. */
3504 expand_one_cmpl_abs_nojump (machine_mode mode, rtx op0, rtx target)
3506 rtx temp;
3508 /* Not applicable for floating point modes. */
3509 if (FLOAT_MODE_P (mode))
3510 return NULL_RTX;
3512 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3513 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3515 rtx_insn *last = get_last_insn ();
3517 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3518 if (temp != 0)
3519 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3520 OPTAB_WIDEN);
3522 if (temp != 0)
3523 return temp;
3525 delete_insns_since (last);
3528 /* If this machine has expensive jumps, we can do one's complement
3529 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3531 if (GET_MODE_CLASS (mode) == MODE_INT
3532 && BRANCH_COST (optimize_insn_for_speed_p (),
3533 false) >= 2)
3535 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3536 GET_MODE_PRECISION (mode) - 1,
3537 NULL_RTX, 0);
3539 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3540 OPTAB_LIB_WIDEN);
3542 if (temp != 0)
3543 return temp;
3546 return NULL_RTX;
3549 /* A subroutine of expand_copysign, perform the copysign operation using the
3550 abs and neg primitives advertised to exist on the target. The assumption
3551 is that we have a split register file, and leaving op0 in fp registers,
3552 and not playing with subregs so much, will help the register allocator. */
3554 static rtx
3555 expand_copysign_absneg (machine_mode mode, rtx op0, rtx op1, rtx target,
3556 int bitpos, bool op0_is_abs)
3558 machine_mode imode;
3559 enum insn_code icode;
3560 rtx sign;
3561 rtx_code_label *label;
3563 if (target == op1)
3564 target = NULL_RTX;
3566 /* Check if the back end provides an insn that handles signbit for the
3567 argument's mode. */
3568 icode = optab_handler (signbit_optab, mode);
3569 if (icode != CODE_FOR_nothing)
3571 imode = insn_data[(int) icode].operand[0].mode;
3572 sign = gen_reg_rtx (imode);
3573 emit_unop_insn (icode, sign, op1, UNKNOWN);
3575 else
3577 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3579 imode = int_mode_for_mode (mode);
3580 if (imode == BLKmode)
3581 return NULL_RTX;
3582 op1 = gen_lowpart (imode, op1);
3584 else
3586 int word;
3588 imode = word_mode;
3589 if (FLOAT_WORDS_BIG_ENDIAN)
3590 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3591 else
3592 word = bitpos / BITS_PER_WORD;
3593 bitpos = bitpos % BITS_PER_WORD;
3594 op1 = operand_subword_force (op1, word, mode);
3597 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3598 sign = expand_binop (imode, and_optab, op1,
3599 immed_wide_int_const (mask, imode),
3600 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3603 if (!op0_is_abs)
3605 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3606 if (op0 == NULL)
3607 return NULL_RTX;
3608 target = op0;
3610 else
3612 if (target == NULL_RTX)
3613 target = copy_to_reg (op0);
3614 else
3615 emit_move_insn (target, op0);
3618 label = gen_label_rtx ();
3619 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3621 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3622 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3623 else
3624 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3625 if (op0 != target)
3626 emit_move_insn (target, op0);
3628 emit_label (label);
3630 return target;
3634 /* A subroutine of expand_copysign, perform the entire copysign operation
3635 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3636 is true if op0 is known to have its sign bit clear. */
3638 static rtx
3639 expand_copysign_bit (machine_mode mode, rtx op0, rtx op1, rtx target,
3640 int bitpos, bool op0_is_abs)
3642 machine_mode imode;
3643 int word, nwords, i;
3644 rtx temp;
3645 rtx_insn *insns;
3647 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3649 imode = int_mode_for_mode (mode);
3650 if (imode == BLKmode)
3651 return NULL_RTX;
3652 word = 0;
3653 nwords = 1;
3655 else
3657 imode = word_mode;
3659 if (FLOAT_WORDS_BIG_ENDIAN)
3660 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3661 else
3662 word = bitpos / BITS_PER_WORD;
3663 bitpos = bitpos % BITS_PER_WORD;
3664 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3667 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3669 if (target == 0
3670 || target == op0
3671 || target == op1
3672 || (nwords > 1 && !valid_multiword_target_p (target)))
3673 target = gen_reg_rtx (mode);
3675 if (nwords > 1)
3677 start_sequence ();
3679 for (i = 0; i < nwords; ++i)
3681 rtx targ_piece = operand_subword (target, i, 1, mode);
3682 rtx op0_piece = operand_subword_force (op0, i, mode);
3684 if (i == word)
3686 if (!op0_is_abs)
3687 op0_piece
3688 = expand_binop (imode, and_optab, op0_piece,
3689 immed_wide_int_const (~mask, imode),
3690 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3691 op1 = expand_binop (imode, and_optab,
3692 operand_subword_force (op1, i, mode),
3693 immed_wide_int_const (mask, imode),
3694 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3696 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3697 targ_piece, 1, OPTAB_LIB_WIDEN);
3698 if (temp != targ_piece)
3699 emit_move_insn (targ_piece, temp);
3701 else
3702 emit_move_insn (targ_piece, op0_piece);
3705 insns = get_insns ();
3706 end_sequence ();
3708 emit_insn (insns);
3710 else
3712 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3713 immed_wide_int_const (mask, imode),
3714 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3716 op0 = gen_lowpart (imode, op0);
3717 if (!op0_is_abs)
3718 op0 = expand_binop (imode, and_optab, op0,
3719 immed_wide_int_const (~mask, imode),
3720 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3722 temp = expand_binop (imode, ior_optab, op0, op1,
3723 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3724 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3727 return target;
3730 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3731 scalar floating point mode. Return NULL if we do not know how to
3732 expand the operation inline. */
3735 expand_copysign (rtx op0, rtx op1, rtx target)
3737 machine_mode mode = GET_MODE (op0);
3738 const struct real_format *fmt;
3739 bool op0_is_abs;
3740 rtx temp;
3742 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3743 gcc_assert (GET_MODE (op1) == mode);
3745 /* First try to do it with a special instruction. */
3746 temp = expand_binop (mode, copysign_optab, op0, op1,
3747 target, 0, OPTAB_DIRECT);
3748 if (temp)
3749 return temp;
3751 fmt = REAL_MODE_FORMAT (mode);
3752 if (fmt == NULL || !fmt->has_signed_zero)
3753 return NULL_RTX;
3755 op0_is_abs = false;
3756 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3758 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3759 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3760 op0_is_abs = true;
3763 if (fmt->signbit_ro >= 0
3764 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3765 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3766 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3768 temp = expand_copysign_absneg (mode, op0, op1, target,
3769 fmt->signbit_ro, op0_is_abs);
3770 if (temp)
3771 return temp;
3774 if (fmt->signbit_rw < 0)
3775 return NULL_RTX;
3776 return expand_copysign_bit (mode, op0, op1, target,
3777 fmt->signbit_rw, op0_is_abs);
3780 /* Generate an instruction whose insn-code is INSN_CODE,
3781 with two operands: an output TARGET and an input OP0.
3782 TARGET *must* be nonzero, and the output is always stored there.
3783 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3784 the value that is stored into TARGET.
3786 Return false if expansion failed. */
3788 bool
3789 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3790 enum rtx_code code)
3792 struct expand_operand ops[2];
3793 rtx_insn *pat;
3795 create_output_operand (&ops[0], target, GET_MODE (target));
3796 create_input_operand (&ops[1], op0, GET_MODE (op0));
3797 pat = maybe_gen_insn (icode, 2, ops);
3798 if (!pat)
3799 return false;
3801 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3802 && code != UNKNOWN)
3803 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3805 emit_insn (pat);
3807 if (ops[0].value != target)
3808 emit_move_insn (target, ops[0].value);
3809 return true;
3811 /* Generate an instruction whose insn-code is INSN_CODE,
3812 with two operands: an output TARGET and an input OP0.
3813 TARGET *must* be nonzero, and the output is always stored there.
3814 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3815 the value that is stored into TARGET. */
3817 void
3818 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3820 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3821 gcc_assert (ok);
3824 struct no_conflict_data
3826 rtx target;
3827 rtx_insn *first, *insn;
3828 bool must_stay;
3831 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3832 the currently examined clobber / store has to stay in the list of
3833 insns that constitute the actual libcall block. */
3834 static void
3835 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3837 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3839 /* If this inns directly contributes to setting the target, it must stay. */
3840 if (reg_overlap_mentioned_p (p->target, dest))
3841 p->must_stay = true;
3842 /* If we haven't committed to keeping any other insns in the list yet,
3843 there is nothing more to check. */
3844 else if (p->insn == p->first)
3845 return;
3846 /* If this insn sets / clobbers a register that feeds one of the insns
3847 already in the list, this insn has to stay too. */
3848 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3849 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3850 || reg_used_between_p (dest, p->first, p->insn)
3851 /* Likewise if this insn depends on a register set by a previous
3852 insn in the list, or if it sets a result (presumably a hard
3853 register) that is set or clobbered by a previous insn.
3854 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3855 SET_DEST perform the former check on the address, and the latter
3856 check on the MEM. */
3857 || (GET_CODE (set) == SET
3858 && (modified_in_p (SET_SRC (set), p->first)
3859 || modified_in_p (SET_DEST (set), p->first)
3860 || modified_between_p (SET_SRC (set), p->first, p->insn)
3861 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3862 p->must_stay = true;
3866 /* Emit code to make a call to a constant function or a library call.
3868 INSNS is a list containing all insns emitted in the call.
3869 These insns leave the result in RESULT. Our block is to copy RESULT
3870 to TARGET, which is logically equivalent to EQUIV.
3872 We first emit any insns that set a pseudo on the assumption that these are
3873 loading constants into registers; doing so allows them to be safely cse'ed
3874 between blocks. Then we emit all the other insns in the block, followed by
3875 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3876 note with an operand of EQUIV. */
3878 static void
3879 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
3880 bool equiv_may_trap)
3882 rtx final_dest = target;
3883 rtx_insn *next, *last, *insn;
3885 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3886 into a MEM later. Protect the libcall block from this change. */
3887 if (! REG_P (target) || REG_USERVAR_P (target))
3888 target = gen_reg_rtx (GET_MODE (target));
3890 /* If we're using non-call exceptions, a libcall corresponding to an
3891 operation that may trap may also trap. */
3892 /* ??? See the comment in front of make_reg_eh_region_note. */
3893 if (cfun->can_throw_non_call_exceptions
3894 && (equiv_may_trap || may_trap_p (equiv)))
3896 for (insn = insns; insn; insn = NEXT_INSN (insn))
3897 if (CALL_P (insn))
3899 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3900 if (note)
3902 int lp_nr = INTVAL (XEXP (note, 0));
3903 if (lp_nr == 0 || lp_nr == INT_MIN)
3904 remove_note (insn, note);
3908 else
3910 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3911 reg note to indicate that this call cannot throw or execute a nonlocal
3912 goto (unless there is already a REG_EH_REGION note, in which case
3913 we update it). */
3914 for (insn = insns; insn; insn = NEXT_INSN (insn))
3915 if (CALL_P (insn))
3916 make_reg_eh_region_note_nothrow_nononlocal (insn);
3919 /* First emit all insns that set pseudos. Remove them from the list as
3920 we go. Avoid insns that set pseudos which were referenced in previous
3921 insns. These can be generated by move_by_pieces, for example,
3922 to update an address. Similarly, avoid insns that reference things
3923 set in previous insns. */
3925 for (insn = insns; insn; insn = next)
3927 rtx set = single_set (insn);
3929 next = NEXT_INSN (insn);
3931 if (set != 0 && REG_P (SET_DEST (set))
3932 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3934 struct no_conflict_data data;
3936 data.target = const0_rtx;
3937 data.first = insns;
3938 data.insn = insn;
3939 data.must_stay = 0;
3940 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3941 if (! data.must_stay)
3943 if (PREV_INSN (insn))
3944 SET_NEXT_INSN (PREV_INSN (insn)) = next;
3945 else
3946 insns = next;
3948 if (next)
3949 SET_PREV_INSN (next) = PREV_INSN (insn);
3951 add_insn (insn);
3955 /* Some ports use a loop to copy large arguments onto the stack.
3956 Don't move anything outside such a loop. */
3957 if (LABEL_P (insn))
3958 break;
3961 /* Write the remaining insns followed by the final copy. */
3962 for (insn = insns; insn; insn = next)
3964 next = NEXT_INSN (insn);
3966 add_insn (insn);
3969 last = emit_move_insn (target, result);
3970 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3972 if (final_dest != target)
3973 emit_move_insn (final_dest, target);
3976 void
3977 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3979 emit_libcall_block_1 (safe_as_a <rtx_insn *> (insns),
3980 target, result, equiv, false);
3983 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3984 PURPOSE describes how this comparison will be used. CODE is the rtx
3985 comparison code we will be using.
3987 ??? Actually, CODE is slightly weaker than that. A target is still
3988 required to implement all of the normal bcc operations, but not
3989 required to implement all (or any) of the unordered bcc operations. */
3992 can_compare_p (enum rtx_code code, machine_mode mode,
3993 enum can_compare_purpose purpose)
3995 rtx test;
3996 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3999 enum insn_code icode;
4001 if (purpose == ccp_jump
4002 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
4003 && insn_operand_matches (icode, 0, test))
4004 return 1;
4005 if (purpose == ccp_store_flag
4006 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
4007 && insn_operand_matches (icode, 1, test))
4008 return 1;
4009 if (purpose == ccp_cmov
4010 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
4011 return 1;
4013 mode = GET_MODE_WIDER_MODE (mode);
4014 PUT_MODE (test, mode);
4016 while (mode != VOIDmode);
4018 return 0;
4021 /* This function is called when we are going to emit a compare instruction that
4022 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
4024 *PMODE is the mode of the inputs (in case they are const_int).
4025 *PUNSIGNEDP nonzero says that the operands are unsigned;
4026 this matters if they need to be widened (as given by METHODS).
4028 If they have mode BLKmode, then SIZE specifies the size of both operands.
4030 This function performs all the setup necessary so that the caller only has
4031 to emit a single comparison insn. This setup can involve doing a BLKmode
4032 comparison or emitting a library call to perform the comparison if no insn
4033 is available to handle it.
4034 The values which are passed in through pointers can be modified; the caller
4035 should perform the comparison on the modified values. Constant
4036 comparisons must have already been folded. */
4038 static void
4039 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4040 int unsignedp, enum optab_methods methods,
4041 rtx *ptest, machine_mode *pmode)
4043 machine_mode mode = *pmode;
4044 rtx libfunc, test;
4045 machine_mode cmp_mode;
4046 enum mode_class mclass;
4048 /* The other methods are not needed. */
4049 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4050 || methods == OPTAB_LIB_WIDEN);
4052 /* If we are optimizing, force expensive constants into a register. */
4053 if (CONSTANT_P (x) && optimize
4054 && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
4055 > COSTS_N_INSNS (1)))
4056 x = force_reg (mode, x);
4058 if (CONSTANT_P (y) && optimize
4059 && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
4060 > COSTS_N_INSNS (1)))
4061 y = force_reg (mode, y);
4063 #if HAVE_cc0
4064 /* Make sure if we have a canonical comparison. The RTL
4065 documentation states that canonical comparisons are required only
4066 for targets which have cc0. */
4067 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4068 #endif
4070 /* Don't let both operands fail to indicate the mode. */
4071 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4072 x = force_reg (mode, x);
4073 if (mode == VOIDmode)
4074 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4076 /* Handle all BLKmode compares. */
4078 if (mode == BLKmode)
4080 machine_mode result_mode;
4081 enum insn_code cmp_code;
4082 tree length_type;
4083 rtx libfunc;
4084 rtx result;
4085 rtx opalign
4086 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4088 gcc_assert (size);
4090 /* Try to use a memory block compare insn - either cmpstr
4091 or cmpmem will do. */
4092 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4093 cmp_mode != VOIDmode;
4094 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4096 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4097 if (cmp_code == CODE_FOR_nothing)
4098 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4099 if (cmp_code == CODE_FOR_nothing)
4100 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4101 if (cmp_code == CODE_FOR_nothing)
4102 continue;
4104 /* Must make sure the size fits the insn's mode. */
4105 if ((CONST_INT_P (size)
4106 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4107 || (GET_MODE_BITSIZE (GET_MODE (size))
4108 > GET_MODE_BITSIZE (cmp_mode)))
4109 continue;
4111 result_mode = insn_data[cmp_code].operand[0].mode;
4112 result = gen_reg_rtx (result_mode);
4113 size = convert_to_mode (cmp_mode, size, 1);
4114 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4116 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4117 *pmode = result_mode;
4118 return;
4121 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4122 goto fail;
4124 /* Otherwise call a library function, memcmp. */
4125 libfunc = memcmp_libfunc;
4126 length_type = sizetype;
4127 result_mode = TYPE_MODE (integer_type_node);
4128 cmp_mode = TYPE_MODE (length_type);
4129 size = convert_to_mode (TYPE_MODE (length_type), size,
4130 TYPE_UNSIGNED (length_type));
4132 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4133 result_mode, 3,
4134 XEXP (x, 0), Pmode,
4135 XEXP (y, 0), Pmode,
4136 size, cmp_mode);
4137 x = result;
4138 y = const0_rtx;
4139 mode = result_mode;
4140 methods = OPTAB_LIB_WIDEN;
4141 unsignedp = false;
4144 /* Don't allow operands to the compare to trap, as that can put the
4145 compare and branch in different basic blocks. */
4146 if (cfun->can_throw_non_call_exceptions)
4148 if (may_trap_p (x))
4149 x = force_reg (mode, x);
4150 if (may_trap_p (y))
4151 y = force_reg (mode, y);
4154 if (GET_MODE_CLASS (mode) == MODE_CC)
4156 enum insn_code icode = optab_handler (cbranch_optab, CCmode);
4157 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4158 gcc_assert (icode != CODE_FOR_nothing
4159 && insn_operand_matches (icode, 0, test));
4160 *ptest = test;
4161 return;
4164 mclass = GET_MODE_CLASS (mode);
4165 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4166 cmp_mode = mode;
4169 enum insn_code icode;
4170 icode = optab_handler (cbranch_optab, cmp_mode);
4171 if (icode != CODE_FOR_nothing
4172 && insn_operand_matches (icode, 0, test))
4174 rtx_insn *last = get_last_insn ();
4175 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4176 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4177 if (op0 && op1
4178 && insn_operand_matches (icode, 1, op0)
4179 && insn_operand_matches (icode, 2, op1))
4181 XEXP (test, 0) = op0;
4182 XEXP (test, 1) = op1;
4183 *ptest = test;
4184 *pmode = cmp_mode;
4185 return;
4187 delete_insns_since (last);
4190 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4191 break;
4192 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4194 while (cmp_mode != VOIDmode);
4196 if (methods != OPTAB_LIB_WIDEN)
4197 goto fail;
4199 if (!SCALAR_FLOAT_MODE_P (mode))
4201 rtx result;
4202 machine_mode ret_mode;
4204 /* Handle a libcall just for the mode we are using. */
4205 libfunc = optab_libfunc (cmp_optab, mode);
4206 gcc_assert (libfunc);
4208 /* If we want unsigned, and this mode has a distinct unsigned
4209 comparison routine, use that. */
4210 if (unsignedp)
4212 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4213 if (ulibfunc)
4214 libfunc = ulibfunc;
4217 ret_mode = targetm.libgcc_cmp_return_mode ();
4218 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4219 ret_mode, 2, x, mode, y, mode);
4221 /* There are two kinds of comparison routines. Biased routines
4222 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4223 of gcc expect that the comparison operation is equivalent
4224 to the modified comparison. For signed comparisons compare the
4225 result against 1 in the biased case, and zero in the unbiased
4226 case. For unsigned comparisons always compare against 1 after
4227 biasing the unbiased result by adding 1. This gives us a way to
4228 represent LTU.
4229 The comparisons in the fixed-point helper library are always
4230 biased. */
4231 x = result;
4232 y = const1_rtx;
4234 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4236 if (unsignedp)
4237 x = plus_constant (ret_mode, result, 1);
4238 else
4239 y = const0_rtx;
4242 *pmode = ret_mode;
4243 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4244 ptest, pmode);
4246 else
4247 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4249 return;
4251 fail:
4252 *ptest = NULL_RTX;
4255 /* Before emitting an insn with code ICODE, make sure that X, which is going
4256 to be used for operand OPNUM of the insn, is converted from mode MODE to
4257 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4258 that it is accepted by the operand predicate. Return the new value. */
4261 prepare_operand (enum insn_code icode, rtx x, int opnum, machine_mode mode,
4262 machine_mode wider_mode, int unsignedp)
4264 if (mode != wider_mode)
4265 x = convert_modes (wider_mode, mode, x, unsignedp);
4267 if (!insn_operand_matches (icode, opnum, x))
4269 machine_mode op_mode = insn_data[(int) icode].operand[opnum].mode;
4270 if (reload_completed)
4271 return NULL_RTX;
4272 if (GET_MODE (x) != op_mode && GET_MODE (x) != VOIDmode)
4273 return NULL_RTX;
4274 x = copy_to_mode_reg (op_mode, x);
4277 return x;
4280 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4281 we can do the branch. */
4283 static void
4284 emit_cmp_and_jump_insn_1 (rtx test, machine_mode mode, rtx label, int prob)
4286 machine_mode optab_mode;
4287 enum mode_class mclass;
4288 enum insn_code icode;
4289 rtx_insn *insn;
4291 mclass = GET_MODE_CLASS (mode);
4292 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4293 icode = optab_handler (cbranch_optab, optab_mode);
4295 gcc_assert (icode != CODE_FOR_nothing);
4296 gcc_assert (insn_operand_matches (icode, 0, test));
4297 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4298 XEXP (test, 1), label));
4299 if (prob != -1
4300 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4301 && insn
4302 && JUMP_P (insn)
4303 && any_condjump_p (insn)
4304 && !find_reg_note (insn, REG_BR_PROB, 0))
4305 add_int_reg_note (insn, REG_BR_PROB, prob);
4308 /* Generate code to compare X with Y so that the condition codes are
4309 set and to jump to LABEL if the condition is true. If X is a
4310 constant and Y is not a constant, then the comparison is swapped to
4311 ensure that the comparison RTL has the canonical form.
4313 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4314 need to be widened. UNSIGNEDP is also used to select the proper
4315 branch condition code.
4317 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4319 MODE is the mode of the inputs (in case they are const_int).
4321 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4322 It will be potentially converted into an unsigned variant based on
4323 UNSIGNEDP to select a proper jump instruction.
4325 PROB is the probability of jumping to LABEL. */
4327 void
4328 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4329 machine_mode mode, int unsignedp, rtx label,
4330 int prob)
4332 rtx op0 = x, op1 = y;
4333 rtx test;
4335 /* Swap operands and condition to ensure canonical RTL. */
4336 if (swap_commutative_operands_p (x, y)
4337 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4339 op0 = y, op1 = x;
4340 comparison = swap_condition (comparison);
4343 /* If OP0 is still a constant, then both X and Y must be constants
4344 or the opposite comparison is not supported. Force X into a register
4345 to create canonical RTL. */
4346 if (CONSTANT_P (op0))
4347 op0 = force_reg (mode, op0);
4349 if (unsignedp)
4350 comparison = unsigned_condition (comparison);
4352 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4353 &test, &mode);
4354 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4358 /* Emit a library call comparison between floating point X and Y.
4359 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4361 static void
4362 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4363 rtx *ptest, machine_mode *pmode)
4365 enum rtx_code swapped = swap_condition (comparison);
4366 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4367 machine_mode orig_mode = GET_MODE (x);
4368 machine_mode mode, cmp_mode;
4369 rtx true_rtx, false_rtx;
4370 rtx value, target, equiv;
4371 rtx_insn *insns;
4372 rtx libfunc = 0;
4373 bool reversed_p = false;
4374 cmp_mode = targetm.libgcc_cmp_return_mode ();
4376 for (mode = orig_mode;
4377 mode != VOIDmode;
4378 mode = GET_MODE_WIDER_MODE (mode))
4380 if (code_to_optab (comparison)
4381 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4382 break;
4384 if (code_to_optab (swapped)
4385 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4387 rtx tmp;
4388 tmp = x; x = y; y = tmp;
4389 comparison = swapped;
4390 break;
4393 if (code_to_optab (reversed)
4394 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4396 comparison = reversed;
4397 reversed_p = true;
4398 break;
4402 gcc_assert (mode != VOIDmode);
4404 if (mode != orig_mode)
4406 x = convert_to_mode (mode, x, 0);
4407 y = convert_to_mode (mode, y, 0);
4410 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4411 the RTL. The allows the RTL optimizers to delete the libcall if the
4412 condition can be determined at compile-time. */
4413 if (comparison == UNORDERED
4414 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4416 true_rtx = const_true_rtx;
4417 false_rtx = const0_rtx;
4419 else
4421 switch (comparison)
4423 case EQ:
4424 true_rtx = const0_rtx;
4425 false_rtx = const_true_rtx;
4426 break;
4428 case NE:
4429 true_rtx = const_true_rtx;
4430 false_rtx = const0_rtx;
4431 break;
4433 case GT:
4434 true_rtx = const1_rtx;
4435 false_rtx = const0_rtx;
4436 break;
4438 case GE:
4439 true_rtx = const0_rtx;
4440 false_rtx = constm1_rtx;
4441 break;
4443 case LT:
4444 true_rtx = constm1_rtx;
4445 false_rtx = const0_rtx;
4446 break;
4448 case LE:
4449 true_rtx = const0_rtx;
4450 false_rtx = const1_rtx;
4451 break;
4453 default:
4454 gcc_unreachable ();
4458 if (comparison == UNORDERED)
4460 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4461 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4462 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4463 temp, const_true_rtx, equiv);
4465 else
4467 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4468 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4469 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4470 equiv, true_rtx, false_rtx);
4473 start_sequence ();
4474 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4475 cmp_mode, 2, x, mode, y, mode);
4476 insns = get_insns ();
4477 end_sequence ();
4479 target = gen_reg_rtx (cmp_mode);
4480 emit_libcall_block (insns, target, value, equiv);
4482 if (comparison == UNORDERED
4483 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4484 || reversed_p)
4485 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4486 else
4487 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4489 *pmode = cmp_mode;
4492 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4494 void
4495 emit_indirect_jump (rtx loc ATTRIBUTE_UNUSED)
4497 #ifndef HAVE_indirect_jump
4498 sorry ("indirect jumps are not available on this target");
4499 #else
4500 struct expand_operand ops[1];
4501 create_address_operand (&ops[0], loc);
4502 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4503 emit_barrier ();
4504 #endif
4508 /* Emit a conditional move instruction if the machine supports one for that
4509 condition and machine mode.
4511 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4512 the mode to use should they be constants. If it is VOIDmode, they cannot
4513 both be constants.
4515 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4516 should be stored there. MODE is the mode to use should they be constants.
4517 If it is VOIDmode, they cannot both be constants.
4519 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4520 is not supported. */
4523 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4524 machine_mode cmode, rtx op2, rtx op3,
4525 machine_mode mode, int unsignedp)
4527 rtx comparison;
4528 rtx_insn *last;
4529 enum insn_code icode;
4530 enum rtx_code reversed;
4532 /* If one operand is constant, make it the second one. Only do this
4533 if the other operand is not constant as well. */
4535 if (swap_commutative_operands_p (op0, op1))
4537 std::swap (op0, op1);
4538 code = swap_condition (code);
4541 /* get_condition will prefer to generate LT and GT even if the old
4542 comparison was against zero, so undo that canonicalization here since
4543 comparisons against zero are cheaper. */
4544 if (code == LT && op1 == const1_rtx)
4545 code = LE, op1 = const0_rtx;
4546 else if (code == GT && op1 == constm1_rtx)
4547 code = GE, op1 = const0_rtx;
4549 if (cmode == VOIDmode)
4550 cmode = GET_MODE (op0);
4552 if (swap_commutative_operands_p (op2, op3)
4553 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4554 != UNKNOWN))
4556 std::swap (op2, op3);
4557 code = reversed;
4560 if (mode == VOIDmode)
4561 mode = GET_MODE (op2);
4563 icode = direct_optab_handler (movcc_optab, mode);
4565 if (icode == CODE_FOR_nothing)
4566 return 0;
4568 if (!target)
4569 target = gen_reg_rtx (mode);
4571 code = unsignedp ? unsigned_condition (code) : code;
4572 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4574 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4575 return NULL and let the caller figure out how best to deal with this
4576 situation. */
4577 if (!COMPARISON_P (comparison))
4578 return NULL_RTX;
4580 saved_pending_stack_adjust save;
4581 save_pending_stack_adjust (&save);
4582 last = get_last_insn ();
4583 do_pending_stack_adjust ();
4584 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4585 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4586 &comparison, &cmode);
4587 if (comparison)
4589 struct expand_operand ops[4];
4591 create_output_operand (&ops[0], target, mode);
4592 create_fixed_operand (&ops[1], comparison);
4593 create_input_operand (&ops[2], op2, mode);
4594 create_input_operand (&ops[3], op3, mode);
4595 if (maybe_expand_insn (icode, 4, ops))
4597 if (ops[0].value != target)
4598 convert_move (target, ops[0].value, false);
4599 return target;
4602 delete_insns_since (last);
4603 restore_pending_stack_adjust (&save);
4604 return NULL_RTX;
4607 /* Return nonzero if a conditional move of mode MODE is supported.
4609 This function is for combine so it can tell whether an insn that looks
4610 like a conditional move is actually supported by the hardware. If we
4611 guess wrong we lose a bit on optimization, but that's it. */
4612 /* ??? sparc64 supports conditionally moving integers values based on fp
4613 comparisons, and vice versa. How do we handle them? */
4616 can_conditionally_move_p (machine_mode mode)
4618 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4619 return 1;
4621 return 0;
4624 /* Emit a conditional addition instruction if the machine supports one for that
4625 condition and machine mode.
4627 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4628 the mode to use should they be constants. If it is VOIDmode, they cannot
4629 both be constants.
4631 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4632 should be stored there. MODE is the mode to use should they be constants.
4633 If it is VOIDmode, they cannot both be constants.
4635 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4636 is not supported. */
4639 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4640 machine_mode cmode, rtx op2, rtx op3,
4641 machine_mode mode, int unsignedp)
4643 rtx comparison;
4644 rtx_insn *last;
4645 enum insn_code icode;
4647 /* If one operand is constant, make it the second one. Only do this
4648 if the other operand is not constant as well. */
4650 if (swap_commutative_operands_p (op0, op1))
4652 std::swap (op0, op1);
4653 code = swap_condition (code);
4656 /* get_condition will prefer to generate LT and GT even if the old
4657 comparison was against zero, so undo that canonicalization here since
4658 comparisons against zero are cheaper. */
4659 if (code == LT && op1 == const1_rtx)
4660 code = LE, op1 = const0_rtx;
4661 else if (code == GT && op1 == constm1_rtx)
4662 code = GE, op1 = const0_rtx;
4664 if (cmode == VOIDmode)
4665 cmode = GET_MODE (op0);
4667 if (mode == VOIDmode)
4668 mode = GET_MODE (op2);
4670 icode = optab_handler (addcc_optab, mode);
4672 if (icode == CODE_FOR_nothing)
4673 return 0;
4675 if (!target)
4676 target = gen_reg_rtx (mode);
4678 code = unsignedp ? unsigned_condition (code) : code;
4679 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4681 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4682 return NULL and let the caller figure out how best to deal with this
4683 situation. */
4684 if (!COMPARISON_P (comparison))
4685 return NULL_RTX;
4687 do_pending_stack_adjust ();
4688 last = get_last_insn ();
4689 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4690 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4691 &comparison, &cmode);
4692 if (comparison)
4694 struct expand_operand ops[4];
4696 create_output_operand (&ops[0], target, mode);
4697 create_fixed_operand (&ops[1], comparison);
4698 create_input_operand (&ops[2], op2, mode);
4699 create_input_operand (&ops[3], op3, mode);
4700 if (maybe_expand_insn (icode, 4, ops))
4702 if (ops[0].value != target)
4703 convert_move (target, ops[0].value, false);
4704 return target;
4707 delete_insns_since (last);
4708 return NULL_RTX;
4711 /* These functions attempt to generate an insn body, rather than
4712 emitting the insn, but if the gen function already emits them, we
4713 make no attempt to turn them back into naked patterns. */
4715 /* Generate and return an insn body to add Y to X. */
4717 rtx_insn *
4718 gen_add2_insn (rtx x, rtx y)
4720 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4722 gcc_assert (insn_operand_matches (icode, 0, x));
4723 gcc_assert (insn_operand_matches (icode, 1, x));
4724 gcc_assert (insn_operand_matches (icode, 2, y));
4726 return GEN_FCN (icode) (x, x, y);
4729 /* Generate and return an insn body to add r1 and c,
4730 storing the result in r0. */
4732 rtx_insn *
4733 gen_add3_insn (rtx r0, rtx r1, rtx c)
4735 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4737 if (icode == CODE_FOR_nothing
4738 || !insn_operand_matches (icode, 0, r0)
4739 || !insn_operand_matches (icode, 1, r1)
4740 || !insn_operand_matches (icode, 2, c))
4741 return NULL;
4743 return GEN_FCN (icode) (r0, r1, c);
4747 have_add2_insn (rtx x, rtx y)
4749 enum insn_code icode;
4751 gcc_assert (GET_MODE (x) != VOIDmode);
4753 icode = optab_handler (add_optab, GET_MODE (x));
4755 if (icode == CODE_FOR_nothing)
4756 return 0;
4758 if (!insn_operand_matches (icode, 0, x)
4759 || !insn_operand_matches (icode, 1, x)
4760 || !insn_operand_matches (icode, 2, y))
4761 return 0;
4763 return 1;
4766 /* Generate and return an insn body to add Y to X. */
4768 rtx_insn *
4769 gen_addptr3_insn (rtx x, rtx y, rtx z)
4771 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4773 gcc_assert (insn_operand_matches (icode, 0, x));
4774 gcc_assert (insn_operand_matches (icode, 1, y));
4775 gcc_assert (insn_operand_matches (icode, 2, z));
4777 return GEN_FCN (icode) (x, y, z);
4780 /* Return true if the target implements an addptr pattern and X, Y,
4781 and Z are valid for the pattern predicates. */
4784 have_addptr3_insn (rtx x, rtx y, rtx z)
4786 enum insn_code icode;
4788 gcc_assert (GET_MODE (x) != VOIDmode);
4790 icode = optab_handler (addptr3_optab, GET_MODE (x));
4792 if (icode == CODE_FOR_nothing)
4793 return 0;
4795 if (!insn_operand_matches (icode, 0, x)
4796 || !insn_operand_matches (icode, 1, y)
4797 || !insn_operand_matches (icode, 2, z))
4798 return 0;
4800 return 1;
4803 /* Generate and return an insn body to subtract Y from X. */
4805 rtx_insn *
4806 gen_sub2_insn (rtx x, rtx y)
4808 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4810 gcc_assert (insn_operand_matches (icode, 0, x));
4811 gcc_assert (insn_operand_matches (icode, 1, x));
4812 gcc_assert (insn_operand_matches (icode, 2, y));
4814 return GEN_FCN (icode) (x, x, y);
4817 /* Generate and return an insn body to subtract r1 and c,
4818 storing the result in r0. */
4820 rtx_insn *
4821 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4823 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4825 if (icode == CODE_FOR_nothing
4826 || !insn_operand_matches (icode, 0, r0)
4827 || !insn_operand_matches (icode, 1, r1)
4828 || !insn_operand_matches (icode, 2, c))
4829 return NULL;
4831 return GEN_FCN (icode) (r0, r1, c);
4835 have_sub2_insn (rtx x, rtx y)
4837 enum insn_code icode;
4839 gcc_assert (GET_MODE (x) != VOIDmode);
4841 icode = optab_handler (sub_optab, GET_MODE (x));
4843 if (icode == CODE_FOR_nothing)
4844 return 0;
4846 if (!insn_operand_matches (icode, 0, x)
4847 || !insn_operand_matches (icode, 1, x)
4848 || !insn_operand_matches (icode, 2, y))
4849 return 0;
4851 return 1;
4854 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4855 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4856 no such operation exists, CODE_FOR_nothing will be returned. */
4858 enum insn_code
4859 can_extend_p (machine_mode to_mode, machine_mode from_mode,
4860 int unsignedp)
4862 convert_optab tab;
4863 #ifdef HAVE_ptr_extend
4864 if (unsignedp < 0)
4865 return CODE_FOR_ptr_extend;
4866 #endif
4868 tab = unsignedp ? zext_optab : sext_optab;
4869 return convert_optab_handler (tab, to_mode, from_mode);
4872 /* Generate the body of an insn to extend Y (with mode MFROM)
4873 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4875 rtx_insn *
4876 gen_extend_insn (rtx x, rtx y, machine_mode mto,
4877 machine_mode mfrom, int unsignedp)
4879 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4880 return GEN_FCN (icode) (x, y);
4883 /* can_fix_p and can_float_p say whether the target machine
4884 can directly convert a given fixed point type to
4885 a given floating point type, or vice versa.
4886 The returned value is the CODE_FOR_... value to use,
4887 or CODE_FOR_nothing if these modes cannot be directly converted.
4889 *TRUNCP_PTR is set to 1 if it is necessary to output
4890 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4892 static enum insn_code
4893 can_fix_p (machine_mode fixmode, machine_mode fltmode,
4894 int unsignedp, int *truncp_ptr)
4896 convert_optab tab;
4897 enum insn_code icode;
4899 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4900 icode = convert_optab_handler (tab, fixmode, fltmode);
4901 if (icode != CODE_FOR_nothing)
4903 *truncp_ptr = 0;
4904 return icode;
4907 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4908 for this to work. We need to rework the fix* and ftrunc* patterns
4909 and documentation. */
4910 tab = unsignedp ? ufix_optab : sfix_optab;
4911 icode = convert_optab_handler (tab, fixmode, fltmode);
4912 if (icode != CODE_FOR_nothing
4913 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4915 *truncp_ptr = 1;
4916 return icode;
4919 *truncp_ptr = 0;
4920 return CODE_FOR_nothing;
4923 enum insn_code
4924 can_float_p (machine_mode fltmode, machine_mode fixmode,
4925 int unsignedp)
4927 convert_optab tab;
4929 tab = unsignedp ? ufloat_optab : sfloat_optab;
4930 return convert_optab_handler (tab, fltmode, fixmode);
4933 /* Function supportable_convert_operation
4935 Check whether an operation represented by the code CODE is a
4936 convert operation that is supported by the target platform in
4937 vector form (i.e., when operating on arguments of type VECTYPE_IN
4938 producing a result of type VECTYPE_OUT).
4940 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4941 This function checks if these operations are supported
4942 by the target platform either directly (via vector tree-codes), or via
4943 target builtins.
4945 Output:
4946 - CODE1 is code of vector operation to be used when
4947 vectorizing the operation, if available.
4948 - DECL is decl of target builtin functions to be used
4949 when vectorizing the operation, if available. In this case,
4950 CODE1 is CALL_EXPR. */
4952 bool
4953 supportable_convert_operation (enum tree_code code,
4954 tree vectype_out, tree vectype_in,
4955 tree *decl, enum tree_code *code1)
4957 machine_mode m1,m2;
4958 int truncp;
4960 m1 = TYPE_MODE (vectype_out);
4961 m2 = TYPE_MODE (vectype_in);
4963 /* First check if we can done conversion directly. */
4964 if ((code == FIX_TRUNC_EXPR
4965 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
4966 != CODE_FOR_nothing)
4967 || (code == FLOAT_EXPR
4968 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
4969 != CODE_FOR_nothing))
4971 *code1 = code;
4972 return true;
4975 /* Now check for builtin. */
4976 if (targetm.vectorize.builtin_conversion
4977 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
4979 *code1 = CALL_EXPR;
4980 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
4981 return true;
4983 return false;
4987 /* Generate code to convert FROM to floating point
4988 and store in TO. FROM must be fixed point and not VOIDmode.
4989 UNSIGNEDP nonzero means regard FROM as unsigned.
4990 Normally this is done by correcting the final value
4991 if it is negative. */
4993 void
4994 expand_float (rtx to, rtx from, int unsignedp)
4996 enum insn_code icode;
4997 rtx target = to;
4998 machine_mode fmode, imode;
4999 bool can_do_signed = false;
5001 /* Crash now, because we won't be able to decide which mode to use. */
5002 gcc_assert (GET_MODE (from) != VOIDmode);
5004 /* Look for an insn to do the conversion. Do it in the specified
5005 modes if possible; otherwise convert either input, output or both to
5006 wider mode. If the integer mode is wider than the mode of FROM,
5007 we can do the conversion signed even if the input is unsigned. */
5009 for (fmode = GET_MODE (to); fmode != VOIDmode;
5010 fmode = GET_MODE_WIDER_MODE (fmode))
5011 for (imode = GET_MODE (from); imode != VOIDmode;
5012 imode = GET_MODE_WIDER_MODE (imode))
5014 int doing_unsigned = unsignedp;
5016 if (fmode != GET_MODE (to)
5017 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
5018 continue;
5020 icode = can_float_p (fmode, imode, unsignedp);
5021 if (icode == CODE_FOR_nothing && unsignedp)
5023 enum insn_code scode = can_float_p (fmode, imode, 0);
5024 if (scode != CODE_FOR_nothing)
5025 can_do_signed = true;
5026 if (imode != GET_MODE (from))
5027 icode = scode, doing_unsigned = 0;
5030 if (icode != CODE_FOR_nothing)
5032 if (imode != GET_MODE (from))
5033 from = convert_to_mode (imode, from, unsignedp);
5035 if (fmode != GET_MODE (to))
5036 target = gen_reg_rtx (fmode);
5038 emit_unop_insn (icode, target, from,
5039 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
5041 if (target != to)
5042 convert_move (to, target, 0);
5043 return;
5047 /* Unsigned integer, and no way to convert directly. Convert as signed,
5048 then unconditionally adjust the result. */
5049 if (unsignedp && can_do_signed)
5051 rtx_code_label *label = gen_label_rtx ();
5052 rtx temp;
5053 REAL_VALUE_TYPE offset;
5055 /* Look for a usable floating mode FMODE wider than the source and at
5056 least as wide as the target. Using FMODE will avoid rounding woes
5057 with unsigned values greater than the signed maximum value. */
5059 for (fmode = GET_MODE (to); fmode != VOIDmode;
5060 fmode = GET_MODE_WIDER_MODE (fmode))
5061 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
5062 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
5063 break;
5065 if (fmode == VOIDmode)
5067 /* There is no such mode. Pretend the target is wide enough. */
5068 fmode = GET_MODE (to);
5070 /* Avoid double-rounding when TO is narrower than FROM. */
5071 if ((significand_size (fmode) + 1)
5072 < GET_MODE_PRECISION (GET_MODE (from)))
5074 rtx temp1;
5075 rtx_code_label *neglabel = gen_label_rtx ();
5077 /* Don't use TARGET if it isn't a register, is a hard register,
5078 or is the wrong mode. */
5079 if (!REG_P (target)
5080 || REGNO (target) < FIRST_PSEUDO_REGISTER
5081 || GET_MODE (target) != fmode)
5082 target = gen_reg_rtx (fmode);
5084 imode = GET_MODE (from);
5085 do_pending_stack_adjust ();
5087 /* Test whether the sign bit is set. */
5088 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5089 0, neglabel);
5091 /* The sign bit is not set. Convert as signed. */
5092 expand_float (target, from, 0);
5093 emit_jump_insn (gen_jump (label));
5094 emit_barrier ();
5096 /* The sign bit is set.
5097 Convert to a usable (positive signed) value by shifting right
5098 one bit, while remembering if a nonzero bit was shifted
5099 out; i.e., compute (from & 1) | (from >> 1). */
5101 emit_label (neglabel);
5102 temp = expand_binop (imode, and_optab, from, const1_rtx,
5103 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5104 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5105 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5106 OPTAB_LIB_WIDEN);
5107 expand_float (target, temp, 0);
5109 /* Multiply by 2 to undo the shift above. */
5110 temp = expand_binop (fmode, add_optab, target, target,
5111 target, 0, OPTAB_LIB_WIDEN);
5112 if (temp != target)
5113 emit_move_insn (target, temp);
5115 do_pending_stack_adjust ();
5116 emit_label (label);
5117 goto done;
5121 /* If we are about to do some arithmetic to correct for an
5122 unsigned operand, do it in a pseudo-register. */
5124 if (GET_MODE (to) != fmode
5125 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5126 target = gen_reg_rtx (fmode);
5128 /* Convert as signed integer to floating. */
5129 expand_float (target, from, 0);
5131 /* If FROM is negative (and therefore TO is negative),
5132 correct its value by 2**bitwidth. */
5134 do_pending_stack_adjust ();
5135 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5136 0, label);
5139 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5140 temp = expand_binop (fmode, add_optab, target,
5141 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5142 target, 0, OPTAB_LIB_WIDEN);
5143 if (temp != target)
5144 emit_move_insn (target, temp);
5146 do_pending_stack_adjust ();
5147 emit_label (label);
5148 goto done;
5151 /* No hardware instruction available; call a library routine. */
5153 rtx libfunc;
5154 rtx_insn *insns;
5155 rtx value;
5156 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5158 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_PRECISION (SImode))
5159 from = convert_to_mode (SImode, from, unsignedp);
5161 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5162 gcc_assert (libfunc);
5164 start_sequence ();
5166 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5167 GET_MODE (to), 1, from,
5168 GET_MODE (from));
5169 insns = get_insns ();
5170 end_sequence ();
5172 emit_libcall_block (insns, target, value,
5173 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5174 GET_MODE (to), from));
5177 done:
5179 /* Copy result to requested destination
5180 if we have been computing in a temp location. */
5182 if (target != to)
5184 if (GET_MODE (target) == GET_MODE (to))
5185 emit_move_insn (to, target);
5186 else
5187 convert_move (to, target, 0);
5191 /* Generate code to convert FROM to fixed point and store in TO. FROM
5192 must be floating point. */
5194 void
5195 expand_fix (rtx to, rtx from, int unsignedp)
5197 enum insn_code icode;
5198 rtx target = to;
5199 machine_mode fmode, imode;
5200 int must_trunc = 0;
5202 /* We first try to find a pair of modes, one real and one integer, at
5203 least as wide as FROM and TO, respectively, in which we can open-code
5204 this conversion. If the integer mode is wider than the mode of TO,
5205 we can do the conversion either signed or unsigned. */
5207 for (fmode = GET_MODE (from); fmode != VOIDmode;
5208 fmode = GET_MODE_WIDER_MODE (fmode))
5209 for (imode = GET_MODE (to); imode != VOIDmode;
5210 imode = GET_MODE_WIDER_MODE (imode))
5212 int doing_unsigned = unsignedp;
5214 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5215 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5216 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5218 if (icode != CODE_FOR_nothing)
5220 rtx_insn *last = get_last_insn ();
5221 if (fmode != GET_MODE (from))
5222 from = convert_to_mode (fmode, from, 0);
5224 if (must_trunc)
5226 rtx temp = gen_reg_rtx (GET_MODE (from));
5227 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5228 temp, 0);
5231 if (imode != GET_MODE (to))
5232 target = gen_reg_rtx (imode);
5234 if (maybe_emit_unop_insn (icode, target, from,
5235 doing_unsigned ? UNSIGNED_FIX : FIX))
5237 if (target != to)
5238 convert_move (to, target, unsignedp);
5239 return;
5241 delete_insns_since (last);
5245 /* For an unsigned conversion, there is one more way to do it.
5246 If we have a signed conversion, we generate code that compares
5247 the real value to the largest representable positive number. If if
5248 is smaller, the conversion is done normally. Otherwise, subtract
5249 one plus the highest signed number, convert, and add it back.
5251 We only need to check all real modes, since we know we didn't find
5252 anything with a wider integer mode.
5254 This code used to extend FP value into mode wider than the destination.
5255 This is needed for decimal float modes which cannot accurately
5256 represent one plus the highest signed number of the same size, but
5257 not for binary modes. Consider, for instance conversion from SFmode
5258 into DImode.
5260 The hot path through the code is dealing with inputs smaller than 2^63
5261 and doing just the conversion, so there is no bits to lose.
5263 In the other path we know the value is positive in the range 2^63..2^64-1
5264 inclusive. (as for other input overflow happens and result is undefined)
5265 So we know that the most important bit set in mantissa corresponds to
5266 2^63. The subtraction of 2^63 should not generate any rounding as it
5267 simply clears out that bit. The rest is trivial. */
5269 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5270 for (fmode = GET_MODE (from); fmode != VOIDmode;
5271 fmode = GET_MODE_WIDER_MODE (fmode))
5272 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5273 && (!DECIMAL_FLOAT_MODE_P (fmode)
5274 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5276 int bitsize;
5277 REAL_VALUE_TYPE offset;
5278 rtx limit;
5279 rtx_code_label *lab1, *lab2;
5280 rtx_insn *insn;
5282 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5283 real_2expN (&offset, bitsize - 1, fmode);
5284 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5285 lab1 = gen_label_rtx ();
5286 lab2 = gen_label_rtx ();
5288 if (fmode != GET_MODE (from))
5289 from = convert_to_mode (fmode, from, 0);
5291 /* See if we need to do the subtraction. */
5292 do_pending_stack_adjust ();
5293 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5294 0, lab1);
5296 /* If not, do the signed "fix" and branch around fixup code. */
5297 expand_fix (to, from, 0);
5298 emit_jump_insn (gen_jump (lab2));
5299 emit_barrier ();
5301 /* Otherwise, subtract 2**(N-1), convert to signed number,
5302 then add 2**(N-1). Do the addition using XOR since this
5303 will often generate better code. */
5304 emit_label (lab1);
5305 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5306 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5307 expand_fix (to, target, 0);
5308 target = expand_binop (GET_MODE (to), xor_optab, to,
5309 gen_int_mode
5310 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5311 GET_MODE (to)),
5312 to, 1, OPTAB_LIB_WIDEN);
5314 if (target != to)
5315 emit_move_insn (to, target);
5317 emit_label (lab2);
5319 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5321 /* Make a place for a REG_NOTE and add it. */
5322 insn = emit_move_insn (to, to);
5323 set_dst_reg_note (insn, REG_EQUAL,
5324 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5325 copy_rtx (from)),
5326 to);
5329 return;
5332 /* We can't do it with an insn, so use a library call. But first ensure
5333 that the mode of TO is at least as wide as SImode, since those are the
5334 only library calls we know about. */
5336 if (GET_MODE_PRECISION (GET_MODE (to)) < GET_MODE_PRECISION (SImode))
5338 target = gen_reg_rtx (SImode);
5340 expand_fix (target, from, unsignedp);
5342 else
5344 rtx_insn *insns;
5345 rtx value;
5346 rtx libfunc;
5348 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5349 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5350 gcc_assert (libfunc);
5352 start_sequence ();
5354 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5355 GET_MODE (to), 1, from,
5356 GET_MODE (from));
5357 insns = get_insns ();
5358 end_sequence ();
5360 emit_libcall_block (insns, target, value,
5361 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5362 GET_MODE (to), from));
5365 if (target != to)
5367 if (GET_MODE (to) == GET_MODE (target))
5368 emit_move_insn (to, target);
5369 else
5370 convert_move (to, target, 0);
5374 /* Generate code to convert FROM or TO a fixed-point.
5375 If UINTP is true, either TO or FROM is an unsigned integer.
5376 If SATP is true, we need to saturate the result. */
5378 void
5379 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5381 machine_mode to_mode = GET_MODE (to);
5382 machine_mode from_mode = GET_MODE (from);
5383 convert_optab tab;
5384 enum rtx_code this_code;
5385 enum insn_code code;
5386 rtx_insn *insns;
5387 rtx value;
5388 rtx libfunc;
5390 if (to_mode == from_mode)
5392 emit_move_insn (to, from);
5393 return;
5396 if (uintp)
5398 tab = satp ? satfractuns_optab : fractuns_optab;
5399 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5401 else
5403 tab = satp ? satfract_optab : fract_optab;
5404 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5406 code = convert_optab_handler (tab, to_mode, from_mode);
5407 if (code != CODE_FOR_nothing)
5409 emit_unop_insn (code, to, from, this_code);
5410 return;
5413 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5414 gcc_assert (libfunc);
5416 start_sequence ();
5417 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5418 1, from, from_mode);
5419 insns = get_insns ();
5420 end_sequence ();
5422 emit_libcall_block (insns, to, value,
5423 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5426 /* Generate code to convert FROM to fixed point and store in TO. FROM
5427 must be floating point, TO must be signed. Use the conversion optab
5428 TAB to do the conversion. */
5430 bool
5431 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5433 enum insn_code icode;
5434 rtx target = to;
5435 machine_mode fmode, imode;
5437 /* We first try to find a pair of modes, one real and one integer, at
5438 least as wide as FROM and TO, respectively, in which we can open-code
5439 this conversion. If the integer mode is wider than the mode of TO,
5440 we can do the conversion either signed or unsigned. */
5442 for (fmode = GET_MODE (from); fmode != VOIDmode;
5443 fmode = GET_MODE_WIDER_MODE (fmode))
5444 for (imode = GET_MODE (to); imode != VOIDmode;
5445 imode = GET_MODE_WIDER_MODE (imode))
5447 icode = convert_optab_handler (tab, imode, fmode);
5448 if (icode != CODE_FOR_nothing)
5450 rtx_insn *last = get_last_insn ();
5451 if (fmode != GET_MODE (from))
5452 from = convert_to_mode (fmode, from, 0);
5454 if (imode != GET_MODE (to))
5455 target = gen_reg_rtx (imode);
5457 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5459 delete_insns_since (last);
5460 continue;
5462 if (target != to)
5463 convert_move (to, target, 0);
5464 return true;
5468 return false;
5471 /* Report whether we have an instruction to perform the operation
5472 specified by CODE on operands of mode MODE. */
5474 have_insn_for (enum rtx_code code, machine_mode mode)
5476 return (code_to_optab (code)
5477 && (optab_handler (code_to_optab (code), mode)
5478 != CODE_FOR_nothing));
5481 /* Initialize the libfunc fields of an entire group of entries in some
5482 optab. Each entry is set equal to a string consisting of a leading
5483 pair of underscores followed by a generic operation name followed by
5484 a mode name (downshifted to lowercase) followed by a single character
5485 representing the number of operands for the given operation (which is
5486 usually one of the characters '2', '3', or '4').
5488 OPTABLE is the table in which libfunc fields are to be initialized.
5489 OPNAME is the generic (string) name of the operation.
5490 SUFFIX is the character which specifies the number of operands for
5491 the given generic operation.
5492 MODE is the mode to generate for.
5495 static void
5496 gen_libfunc (optab optable, const char *opname, int suffix,
5497 machine_mode mode)
5499 unsigned opname_len = strlen (opname);
5500 const char *mname = GET_MODE_NAME (mode);
5501 unsigned mname_len = strlen (mname);
5502 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5503 int len = prefix_len + opname_len + mname_len + 1 + 1;
5504 char *libfunc_name = XALLOCAVEC (char, len);
5505 char *p;
5506 const char *q;
5508 p = libfunc_name;
5509 *p++ = '_';
5510 *p++ = '_';
5511 if (targetm.libfunc_gnu_prefix)
5513 *p++ = 'g';
5514 *p++ = 'n';
5515 *p++ = 'u';
5516 *p++ = '_';
5518 for (q = opname; *q; )
5519 *p++ = *q++;
5520 for (q = mname; *q; q++)
5521 *p++ = TOLOWER (*q);
5522 *p++ = suffix;
5523 *p = '\0';
5525 set_optab_libfunc (optable, mode,
5526 ggc_alloc_string (libfunc_name, p - libfunc_name));
5529 /* Like gen_libfunc, but verify that integer operation is involved. */
5531 void
5532 gen_int_libfunc (optab optable, const char *opname, char suffix,
5533 machine_mode mode)
5535 int maxsize = 2 * BITS_PER_WORD;
5536 int minsize = BITS_PER_WORD;
5538 if (GET_MODE_CLASS (mode) != MODE_INT)
5539 return;
5540 if (maxsize < LONG_LONG_TYPE_SIZE)
5541 maxsize = LONG_LONG_TYPE_SIZE;
5542 if (minsize > INT_TYPE_SIZE
5543 && (trapv_binoptab_p (optable)
5544 || trapv_unoptab_p (optable)))
5545 minsize = INT_TYPE_SIZE;
5546 if (GET_MODE_BITSIZE (mode) < minsize
5547 || GET_MODE_BITSIZE (mode) > maxsize)
5548 return;
5549 gen_libfunc (optable, opname, suffix, mode);
5552 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5554 void
5555 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5556 machine_mode mode)
5558 char *dec_opname;
5560 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5561 gen_libfunc (optable, opname, suffix, mode);
5562 if (DECIMAL_FLOAT_MODE_P (mode))
5564 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5565 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5566 depending on the low level floating format used. */
5567 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5568 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5569 gen_libfunc (optable, dec_opname, suffix, mode);
5573 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5575 void
5576 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5577 machine_mode mode)
5579 if (!ALL_FIXED_POINT_MODE_P (mode))
5580 return;
5581 gen_libfunc (optable, opname, suffix, mode);
5584 /* Like gen_libfunc, but verify that signed fixed-point operation is
5585 involved. */
5587 void
5588 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5589 machine_mode mode)
5591 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5592 return;
5593 gen_libfunc (optable, opname, suffix, mode);
5596 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5597 involved. */
5599 void
5600 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5601 machine_mode mode)
5603 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5604 return;
5605 gen_libfunc (optable, opname, suffix, mode);
5608 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5610 void
5611 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5612 machine_mode mode)
5614 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5615 gen_fp_libfunc (optable, name, suffix, mode);
5616 if (INTEGRAL_MODE_P (mode))
5617 gen_int_libfunc (optable, name, suffix, mode);
5620 /* Like gen_libfunc, but verify that FP or INT operation is involved
5621 and add 'v' suffix for integer operation. */
5623 void
5624 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5625 machine_mode mode)
5627 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5628 gen_fp_libfunc (optable, name, suffix, mode);
5629 if (GET_MODE_CLASS (mode) == MODE_INT)
5631 int len = strlen (name);
5632 char *v_name = XALLOCAVEC (char, len + 2);
5633 strcpy (v_name, name);
5634 v_name[len] = 'v';
5635 v_name[len + 1] = 0;
5636 gen_int_libfunc (optable, v_name, suffix, mode);
5640 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5641 involved. */
5643 void
5644 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5645 machine_mode mode)
5647 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5648 gen_fp_libfunc (optable, name, suffix, mode);
5649 if (INTEGRAL_MODE_P (mode))
5650 gen_int_libfunc (optable, name, suffix, mode);
5651 if (ALL_FIXED_POINT_MODE_P (mode))
5652 gen_fixed_libfunc (optable, name, suffix, mode);
5655 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5656 involved. */
5658 void
5659 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5660 machine_mode mode)
5662 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5663 gen_fp_libfunc (optable, name, suffix, mode);
5664 if (INTEGRAL_MODE_P (mode))
5665 gen_int_libfunc (optable, name, suffix, mode);
5666 if (SIGNED_FIXED_POINT_MODE_P (mode))
5667 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5670 /* Like gen_libfunc, but verify that INT or FIXED operation is
5671 involved. */
5673 void
5674 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5675 machine_mode mode)
5677 if (INTEGRAL_MODE_P (mode))
5678 gen_int_libfunc (optable, name, suffix, mode);
5679 if (ALL_FIXED_POINT_MODE_P (mode))
5680 gen_fixed_libfunc (optable, name, suffix, mode);
5683 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5684 involved. */
5686 void
5687 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5688 machine_mode mode)
5690 if (INTEGRAL_MODE_P (mode))
5691 gen_int_libfunc (optable, name, suffix, mode);
5692 if (SIGNED_FIXED_POINT_MODE_P (mode))
5693 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5696 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5697 involved. */
5699 void
5700 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5701 machine_mode mode)
5703 if (INTEGRAL_MODE_P (mode))
5704 gen_int_libfunc (optable, name, suffix, mode);
5705 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5706 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5709 /* Initialize the libfunc fields of an entire group of entries of an
5710 inter-mode-class conversion optab. The string formation rules are
5711 similar to the ones for init_libfuncs, above, but instead of having
5712 a mode name and an operand count these functions have two mode names
5713 and no operand count. */
5715 void
5716 gen_interclass_conv_libfunc (convert_optab tab,
5717 const char *opname,
5718 machine_mode tmode,
5719 machine_mode fmode)
5721 size_t opname_len = strlen (opname);
5722 size_t mname_len = 0;
5724 const char *fname, *tname;
5725 const char *q;
5726 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5727 char *libfunc_name, *suffix;
5728 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5729 char *p;
5731 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5732 depends on which underlying decimal floating point format is used. */
5733 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5735 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5737 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5738 nondec_name[0] = '_';
5739 nondec_name[1] = '_';
5740 if (targetm.libfunc_gnu_prefix)
5742 nondec_name[2] = 'g';
5743 nondec_name[3] = 'n';
5744 nondec_name[4] = 'u';
5745 nondec_name[5] = '_';
5748 memcpy (&nondec_name[prefix_len], opname, opname_len);
5749 nondec_suffix = nondec_name + opname_len + prefix_len;
5751 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5752 dec_name[0] = '_';
5753 dec_name[1] = '_';
5754 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5755 memcpy (&dec_name[2+dec_len], opname, opname_len);
5756 dec_suffix = dec_name + dec_len + opname_len + 2;
5758 fname = GET_MODE_NAME (fmode);
5759 tname = GET_MODE_NAME (tmode);
5761 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5763 libfunc_name = dec_name;
5764 suffix = dec_suffix;
5766 else
5768 libfunc_name = nondec_name;
5769 suffix = nondec_suffix;
5772 p = suffix;
5773 for (q = fname; *q; p++, q++)
5774 *p = TOLOWER (*q);
5775 for (q = tname; *q; p++, q++)
5776 *p = TOLOWER (*q);
5778 *p = '\0';
5780 set_conv_libfunc (tab, tmode, fmode,
5781 ggc_alloc_string (libfunc_name, p - libfunc_name));
5784 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5785 int->fp conversion. */
5787 void
5788 gen_int_to_fp_conv_libfunc (convert_optab tab,
5789 const char *opname,
5790 machine_mode tmode,
5791 machine_mode fmode)
5793 if (GET_MODE_CLASS (fmode) != MODE_INT)
5794 return;
5795 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5796 return;
5797 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5800 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5801 naming scheme. */
5803 void
5804 gen_ufloat_conv_libfunc (convert_optab tab,
5805 const char *opname ATTRIBUTE_UNUSED,
5806 machine_mode tmode,
5807 machine_mode fmode)
5809 if (DECIMAL_FLOAT_MODE_P (tmode))
5810 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5811 else
5812 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5815 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5816 fp->int conversion. */
5818 void
5819 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5820 const char *opname,
5821 machine_mode tmode,
5822 machine_mode fmode)
5824 if (GET_MODE_CLASS (fmode) != MODE_INT)
5825 return;
5826 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5827 return;
5828 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5831 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5832 fp->int conversion with no decimal floating point involved. */
5834 void
5835 gen_fp_to_int_conv_libfunc (convert_optab tab,
5836 const char *opname,
5837 machine_mode tmode,
5838 machine_mode fmode)
5840 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5841 return;
5842 if (GET_MODE_CLASS (tmode) != MODE_INT)
5843 return;
5844 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5847 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5848 The string formation rules are
5849 similar to the ones for init_libfunc, above. */
5851 void
5852 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5853 machine_mode tmode, machine_mode fmode)
5855 size_t opname_len = strlen (opname);
5856 size_t mname_len = 0;
5858 const char *fname, *tname;
5859 const char *q;
5860 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5861 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5862 char *libfunc_name, *suffix;
5863 char *p;
5865 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5866 depends on which underlying decimal floating point format is used. */
5867 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5869 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5871 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5872 nondec_name[0] = '_';
5873 nondec_name[1] = '_';
5874 if (targetm.libfunc_gnu_prefix)
5876 nondec_name[2] = 'g';
5877 nondec_name[3] = 'n';
5878 nondec_name[4] = 'u';
5879 nondec_name[5] = '_';
5881 memcpy (&nondec_name[prefix_len], opname, opname_len);
5882 nondec_suffix = nondec_name + opname_len + prefix_len;
5884 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5885 dec_name[0] = '_';
5886 dec_name[1] = '_';
5887 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5888 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5889 dec_suffix = dec_name + dec_len + opname_len + 2;
5891 fname = GET_MODE_NAME (fmode);
5892 tname = GET_MODE_NAME (tmode);
5894 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5896 libfunc_name = dec_name;
5897 suffix = dec_suffix;
5899 else
5901 libfunc_name = nondec_name;
5902 suffix = nondec_suffix;
5905 p = suffix;
5906 for (q = fname; *q; p++, q++)
5907 *p = TOLOWER (*q);
5908 for (q = tname; *q; p++, q++)
5909 *p = TOLOWER (*q);
5911 *p++ = '2';
5912 *p = '\0';
5914 set_conv_libfunc (tab, tmode, fmode,
5915 ggc_alloc_string (libfunc_name, p - libfunc_name));
5918 /* Pick proper libcall for trunc_optab. We need to chose if we do
5919 truncation or extension and interclass or intraclass. */
5921 void
5922 gen_trunc_conv_libfunc (convert_optab tab,
5923 const char *opname,
5924 machine_mode tmode,
5925 machine_mode fmode)
5927 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5928 return;
5929 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5930 return;
5931 if (tmode == fmode)
5932 return;
5934 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5935 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5936 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5938 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5939 return;
5941 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5942 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5943 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5944 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5947 /* Pick proper libcall for extend_optab. We need to chose if we do
5948 truncation or extension and interclass or intraclass. */
5950 void
5951 gen_extend_conv_libfunc (convert_optab tab,
5952 const char *opname ATTRIBUTE_UNUSED,
5953 machine_mode tmode,
5954 machine_mode fmode)
5956 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5957 return;
5958 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5959 return;
5960 if (tmode == fmode)
5961 return;
5963 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5964 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5965 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5967 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5968 return;
5970 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5971 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5972 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5973 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5976 /* Pick proper libcall for fract_optab. We need to chose if we do
5977 interclass or intraclass. */
5979 void
5980 gen_fract_conv_libfunc (convert_optab tab,
5981 const char *opname,
5982 machine_mode tmode,
5983 machine_mode fmode)
5985 if (tmode == fmode)
5986 return;
5987 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5988 return;
5990 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5991 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5992 else
5993 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5996 /* Pick proper libcall for fractuns_optab. */
5998 void
5999 gen_fractuns_conv_libfunc (convert_optab tab,
6000 const char *opname,
6001 machine_mode tmode,
6002 machine_mode fmode)
6004 if (tmode == fmode)
6005 return;
6006 /* One mode must be a fixed-point mode, and the other must be an integer
6007 mode. */
6008 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
6009 || (ALL_FIXED_POINT_MODE_P (fmode)
6010 && GET_MODE_CLASS (tmode) == MODE_INT)))
6011 return;
6013 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6016 /* Pick proper libcall for satfract_optab. We need to chose if we do
6017 interclass or intraclass. */
6019 void
6020 gen_satfract_conv_libfunc (convert_optab tab,
6021 const char *opname,
6022 machine_mode tmode,
6023 machine_mode fmode)
6025 if (tmode == fmode)
6026 return;
6027 /* TMODE must be a fixed-point mode. */
6028 if (!ALL_FIXED_POINT_MODE_P (tmode))
6029 return;
6031 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6032 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6033 else
6034 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6037 /* Pick proper libcall for satfractuns_optab. */
6039 void
6040 gen_satfractuns_conv_libfunc (convert_optab tab,
6041 const char *opname,
6042 machine_mode tmode,
6043 machine_mode fmode)
6045 if (tmode == fmode)
6046 return;
6047 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
6048 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
6049 return;
6051 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6054 /* Hashtable callbacks for libfunc_decls. */
6056 struct libfunc_decl_hasher : ggc_hasher<tree>
6058 static hashval_t
6059 hash (tree entry)
6061 return IDENTIFIER_HASH_VALUE (DECL_NAME (entry));
6064 static bool
6065 equal (tree decl, tree name)
6067 return DECL_NAME (decl) == name;
6071 /* A table of previously-created libfuncs, hashed by name. */
6072 static GTY (()) hash_table<libfunc_decl_hasher> *libfunc_decls;
6074 /* Build a decl for a libfunc named NAME. */
6076 tree
6077 build_libfunc_function (const char *name)
6079 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6080 get_identifier (name),
6081 build_function_type (integer_type_node, NULL_TREE));
6082 /* ??? We don't have any type information except for this is
6083 a function. Pretend this is "int foo()". */
6084 DECL_ARTIFICIAL (decl) = 1;
6085 DECL_EXTERNAL (decl) = 1;
6086 TREE_PUBLIC (decl) = 1;
6087 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6089 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6090 are the flags assigned by targetm.encode_section_info. */
6091 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6093 return decl;
6097 init_one_libfunc (const char *name)
6099 tree id, decl;
6100 hashval_t hash;
6102 if (libfunc_decls == NULL)
6103 libfunc_decls = hash_table<libfunc_decl_hasher>::create_ggc (37);
6105 /* See if we have already created a libfunc decl for this function. */
6106 id = get_identifier (name);
6107 hash = IDENTIFIER_HASH_VALUE (id);
6108 tree *slot = libfunc_decls->find_slot_with_hash (id, hash, INSERT);
6109 decl = *slot;
6110 if (decl == NULL)
6112 /* Create a new decl, so that it can be passed to
6113 targetm.encode_section_info. */
6114 decl = build_libfunc_function (name);
6115 *slot = decl;
6117 return XEXP (DECL_RTL (decl), 0);
6120 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6123 set_user_assembler_libfunc (const char *name, const char *asmspec)
6125 tree id, decl;
6126 hashval_t hash;
6128 id = get_identifier (name);
6129 hash = IDENTIFIER_HASH_VALUE (id);
6130 tree *slot = libfunc_decls->find_slot_with_hash (id, hash, NO_INSERT);
6131 gcc_assert (slot);
6132 decl = (tree) *slot;
6133 set_user_assembler_name (decl, asmspec);
6134 return XEXP (DECL_RTL (decl), 0);
6137 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6138 MODE to NAME, which should be either 0 or a string constant. */
6139 void
6140 set_optab_libfunc (optab op, machine_mode mode, const char *name)
6142 rtx val;
6143 struct libfunc_entry e;
6144 struct libfunc_entry **slot;
6146 e.op = op;
6147 e.mode1 = mode;
6148 e.mode2 = VOIDmode;
6150 if (name)
6151 val = init_one_libfunc (name);
6152 else
6153 val = 0;
6154 slot = libfunc_hash->find_slot (&e, INSERT);
6155 if (*slot == NULL)
6156 *slot = ggc_alloc<libfunc_entry> ();
6157 (*slot)->op = op;
6158 (*slot)->mode1 = mode;
6159 (*slot)->mode2 = VOIDmode;
6160 (*slot)->libfunc = val;
6163 /* Call this to reset the function entry for one conversion optab
6164 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6165 either 0 or a string constant. */
6166 void
6167 set_conv_libfunc (convert_optab optab, machine_mode tmode,
6168 machine_mode fmode, const char *name)
6170 rtx val;
6171 struct libfunc_entry e;
6172 struct libfunc_entry **slot;
6174 e.op = optab;
6175 e.mode1 = tmode;
6176 e.mode2 = fmode;
6178 if (name)
6179 val = init_one_libfunc (name);
6180 else
6181 val = 0;
6182 slot = libfunc_hash->find_slot (&e, INSERT);
6183 if (*slot == NULL)
6184 *slot = ggc_alloc<libfunc_entry> ();
6185 (*slot)->op = optab;
6186 (*slot)->mode1 = tmode;
6187 (*slot)->mode2 = fmode;
6188 (*slot)->libfunc = val;
6191 /* Call this to initialize the contents of the optabs
6192 appropriately for the current target machine. */
6194 void
6195 init_optabs (void)
6197 if (libfunc_hash)
6198 libfunc_hash->empty ();
6199 else
6200 libfunc_hash = hash_table<libfunc_hasher>::create_ggc (10);
6202 /* Fill in the optabs with the insns we support. */
6203 init_all_optabs (this_fn_optabs);
6205 /* The ffs function operates on `int'. Fall back on it if we do not
6206 have a libgcc2 function for that width. */
6207 if (INT_TYPE_SIZE < BITS_PER_WORD)
6208 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6209 "ffs");
6211 /* Explicitly initialize the bswap libfuncs since we need them to be
6212 valid for things other than word_mode. */
6213 if (targetm.libfunc_gnu_prefix)
6215 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6216 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6218 else
6220 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6221 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6224 /* Use cabs for double complex abs, since systems generally have cabs.
6225 Don't define any libcall for float complex, so that cabs will be used. */
6226 if (complex_double_type_node)
6227 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node),
6228 "cabs");
6230 abort_libfunc = init_one_libfunc ("abort");
6231 memcpy_libfunc = init_one_libfunc ("memcpy");
6232 memmove_libfunc = init_one_libfunc ("memmove");
6233 memcmp_libfunc = init_one_libfunc ("memcmp");
6234 memset_libfunc = init_one_libfunc ("memset");
6235 setbits_libfunc = init_one_libfunc ("__setbits");
6237 #ifndef DONT_USE_BUILTIN_SETJMP
6238 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6239 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6240 #else
6241 setjmp_libfunc = init_one_libfunc ("setjmp");
6242 longjmp_libfunc = init_one_libfunc ("longjmp");
6243 #endif
6244 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6245 unwind_sjlj_unregister_libfunc
6246 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6248 /* For function entry/exit instrumentation. */
6249 profile_function_entry_libfunc
6250 = init_one_libfunc ("__cyg_profile_func_enter");
6251 profile_function_exit_libfunc
6252 = init_one_libfunc ("__cyg_profile_func_exit");
6254 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6256 /* Allow the target to add more libcalls or rename some, etc. */
6257 targetm.init_libfuncs ();
6260 /* Use the current target and options to initialize
6261 TREE_OPTIMIZATION_OPTABS (OPTNODE). */
6263 void
6264 init_tree_optimization_optabs (tree optnode)
6266 /* Quick exit if we have already computed optabs for this target. */
6267 if (TREE_OPTIMIZATION_BASE_OPTABS (optnode) == this_target_optabs)
6268 return;
6270 /* Forget any previous information and set up for the current target. */
6271 TREE_OPTIMIZATION_BASE_OPTABS (optnode) = this_target_optabs;
6272 struct target_optabs *tmp_optabs = (struct target_optabs *)
6273 TREE_OPTIMIZATION_OPTABS (optnode);
6274 if (tmp_optabs)
6275 memset (tmp_optabs, 0, sizeof (struct target_optabs));
6276 else
6277 tmp_optabs = ggc_alloc<target_optabs> ();
6279 /* Generate a new set of optabs into tmp_optabs. */
6280 init_all_optabs (tmp_optabs);
6282 /* If the optabs changed, record it. */
6283 if (memcmp (tmp_optabs, this_target_optabs, sizeof (struct target_optabs)))
6284 TREE_OPTIMIZATION_OPTABS (optnode) = tmp_optabs;
6285 else
6287 TREE_OPTIMIZATION_OPTABS (optnode) = NULL;
6288 ggc_free (tmp_optabs);
6292 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6293 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6295 static void
6296 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6298 machine_mode mode;
6299 char buf[64];
6300 size_t len = strlen (base);
6301 int i;
6303 gcc_assert (max <= 8);
6304 gcc_assert (len + 3 < sizeof (buf));
6306 memcpy (buf, base, len);
6307 buf[len] = '_';
6308 buf[len + 1] = '0';
6309 buf[len + 2] = '\0';
6311 mode = QImode;
6312 for (i = 1; i <= max; i *= 2)
6314 buf[len + 1] = '0' + i;
6315 set_optab_libfunc (tab, mode, buf);
6316 mode = GET_MODE_2XWIDER_MODE (mode);
6320 void
6321 init_sync_libfuncs (int max)
6323 if (!flag_sync_libcalls)
6324 return;
6326 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6327 "__sync_val_compare_and_swap", max);
6328 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6329 "__sync_lock_test_and_set", max);
6331 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6332 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6333 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6334 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6335 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6336 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6338 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6339 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6340 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6341 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6342 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6343 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6346 /* Print information about the current contents of the optabs on
6347 STDERR. */
6349 DEBUG_FUNCTION void
6350 debug_optab_libfuncs (void)
6352 int i, j, k;
6354 /* Dump the arithmetic optabs. */
6355 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
6356 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6358 rtx l = optab_libfunc ((optab) i, (machine_mode) j);
6359 if (l)
6361 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6362 fprintf (stderr, "%s\t%s:\t%s\n",
6363 GET_RTX_NAME (optab_to_code ((optab) i)),
6364 GET_MODE_NAME (j),
6365 XSTR (l, 0));
6369 /* Dump the conversion optabs. */
6370 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
6371 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6372 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6374 rtx l = convert_optab_libfunc ((optab) i, (machine_mode) j,
6375 (machine_mode) k);
6376 if (l)
6378 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6379 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6380 GET_RTX_NAME (optab_to_code ((optab) i)),
6381 GET_MODE_NAME (j),
6382 GET_MODE_NAME (k),
6383 XSTR (l, 0));
6389 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6390 CODE. Return 0 on failure. */
6392 rtx_insn *
6393 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6395 machine_mode mode = GET_MODE (op1);
6396 enum insn_code icode;
6397 rtx_insn *insn;
6398 rtx trap_rtx;
6400 if (mode == VOIDmode)
6401 return 0;
6403 icode = optab_handler (ctrap_optab, mode);
6404 if (icode == CODE_FOR_nothing)
6405 return 0;
6407 /* Some targets only accept a zero trap code. */
6408 if (!insn_operand_matches (icode, 3, tcode))
6409 return 0;
6411 do_pending_stack_adjust ();
6412 start_sequence ();
6413 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6414 &trap_rtx, &mode);
6415 if (!trap_rtx)
6416 insn = NULL;
6417 else
6418 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6419 tcode);
6421 /* If that failed, then give up. */
6422 if (insn == 0)
6424 end_sequence ();
6425 return 0;
6428 emit_insn (insn);
6429 insn = get_insns ();
6430 end_sequence ();
6431 return insn;
6434 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6435 or unsigned operation code. */
6437 enum rtx_code
6438 get_rtx_code (enum tree_code tcode, bool unsignedp)
6440 enum rtx_code code;
6441 switch (tcode)
6443 case EQ_EXPR:
6444 code = EQ;
6445 break;
6446 case NE_EXPR:
6447 code = NE;
6448 break;
6449 case LT_EXPR:
6450 code = unsignedp ? LTU : LT;
6451 break;
6452 case LE_EXPR:
6453 code = unsignedp ? LEU : LE;
6454 break;
6455 case GT_EXPR:
6456 code = unsignedp ? GTU : GT;
6457 break;
6458 case GE_EXPR:
6459 code = unsignedp ? GEU : GE;
6460 break;
6462 case UNORDERED_EXPR:
6463 code = UNORDERED;
6464 break;
6465 case ORDERED_EXPR:
6466 code = ORDERED;
6467 break;
6468 case UNLT_EXPR:
6469 code = UNLT;
6470 break;
6471 case UNLE_EXPR:
6472 code = UNLE;
6473 break;
6474 case UNGT_EXPR:
6475 code = UNGT;
6476 break;
6477 case UNGE_EXPR:
6478 code = UNGE;
6479 break;
6480 case UNEQ_EXPR:
6481 code = UNEQ;
6482 break;
6483 case LTGT_EXPR:
6484 code = LTGT;
6485 break;
6487 case BIT_AND_EXPR:
6488 code = AND;
6489 break;
6491 case BIT_IOR_EXPR:
6492 code = IOR;
6493 break;
6495 default:
6496 gcc_unreachable ();
6498 return code;
6501 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6502 unsigned operators. Do not generate compare instruction. */
6504 static rtx
6505 vector_compare_rtx (enum tree_code tcode, tree t_op0, tree t_op1,
6506 bool unsignedp, enum insn_code icode)
6508 struct expand_operand ops[2];
6509 rtx rtx_op0, rtx_op1;
6510 machine_mode m0, m1;
6511 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
6513 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
6515 /* Expand operands. For vector types with scalar modes, e.g. where int64x1_t
6516 has mode DImode, this can produce a constant RTX of mode VOIDmode; in such
6517 cases, use the original mode. */
6518 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6519 EXPAND_STACK_PARM);
6520 m0 = GET_MODE (rtx_op0);
6521 if (m0 == VOIDmode)
6522 m0 = TYPE_MODE (TREE_TYPE (t_op0));
6524 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6525 EXPAND_STACK_PARM);
6526 m1 = GET_MODE (rtx_op1);
6527 if (m1 == VOIDmode)
6528 m1 = TYPE_MODE (TREE_TYPE (t_op1));
6530 create_input_operand (&ops[0], rtx_op0, m0);
6531 create_input_operand (&ops[1], rtx_op1, m1);
6532 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6533 gcc_unreachable ();
6534 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6537 /* Return true if VEC_PERM_EXPR of arbitrary input vectors can be expanded using
6538 SIMD extensions of the CPU. SEL may be NULL, which stands for an unknown
6539 constant. Note that additional permutations representing whole-vector shifts
6540 may also be handled via the vec_shr optab, but only where the second input
6541 vector is entirely constant zeroes; this case is not dealt with here. */
6543 bool
6544 can_vec_perm_p (machine_mode mode, bool variable,
6545 const unsigned char *sel)
6547 machine_mode qimode;
6549 /* If the target doesn't implement a vector mode for the vector type,
6550 then no operations are supported. */
6551 if (!VECTOR_MODE_P (mode))
6552 return false;
6554 if (!variable)
6556 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6557 && (sel == NULL
6558 || targetm.vectorize.vec_perm_const_ok == NULL
6559 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6560 return true;
6563 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6564 return true;
6566 /* We allow fallback to a QI vector mode, and adjust the mask. */
6567 if (GET_MODE_INNER (mode) == QImode)
6568 return false;
6569 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6570 if (!VECTOR_MODE_P (qimode))
6571 return false;
6573 /* ??? For completeness, we ought to check the QImode version of
6574 vec_perm_const_optab. But all users of this implicit lowering
6575 feature implement the variable vec_perm_optab. */
6576 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6577 return false;
6579 /* In order to support the lowering of variable permutations,
6580 we need to support shifts and adds. */
6581 if (variable)
6583 if (GET_MODE_UNIT_SIZE (mode) > 2
6584 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6585 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6586 return false;
6587 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6588 return false;
6591 return true;
6594 /* Checks if vec_perm mask SEL is a constant equivalent to a shift of the first
6595 vec_perm operand, assuming the second operand is a constant vector of zeroes.
6596 Return the shift distance in bits if so, or NULL_RTX if the vec_perm is not a
6597 shift. */
6598 static rtx
6599 shift_amt_for_vec_perm_mask (rtx sel)
6601 unsigned int i, first, nelt = GET_MODE_NUNITS (GET_MODE (sel));
6602 unsigned int bitsize = GET_MODE_BITSIZE (GET_MODE_INNER (GET_MODE (sel)));
6604 if (GET_CODE (sel) != CONST_VECTOR)
6605 return NULL_RTX;
6607 first = INTVAL (CONST_VECTOR_ELT (sel, 0));
6608 if (first >= 2*nelt)
6609 return NULL_RTX;
6610 for (i = 1; i < nelt; i++)
6612 int idx = INTVAL (CONST_VECTOR_ELT (sel, i));
6613 unsigned int expected = (i + first) & (2 * nelt - 1);
6614 /* Indices into the second vector are all equivalent. */
6615 if (idx < 0 || (MIN (nelt, (unsigned) idx) != MIN (nelt, expected)))
6616 return NULL_RTX;
6619 return GEN_INT (first * bitsize);
6622 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
6624 static rtx
6625 expand_vec_perm_1 (enum insn_code icode, rtx target,
6626 rtx v0, rtx v1, rtx sel)
6628 machine_mode tmode = GET_MODE (target);
6629 machine_mode smode = GET_MODE (sel);
6630 struct expand_operand ops[4];
6632 create_output_operand (&ops[0], target, tmode);
6633 create_input_operand (&ops[3], sel, smode);
6635 /* Make an effort to preserve v0 == v1. The target expander is able to
6636 rely on this to determine if we're permuting a single input operand. */
6637 if (rtx_equal_p (v0, v1))
6639 if (!insn_operand_matches (icode, 1, v0))
6640 v0 = force_reg (tmode, v0);
6641 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6642 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6644 create_fixed_operand (&ops[1], v0);
6645 create_fixed_operand (&ops[2], v0);
6647 else
6649 create_input_operand (&ops[1], v0, tmode);
6650 /* See if this can be handled with a vec_shr. We only do this if the
6651 second vector is all zeroes. */
6652 enum insn_code shift_code = optab_handler (vec_shr_optab, GET_MODE (v0));
6653 if (v1 == CONST0_RTX (GET_MODE (v1)) && shift_code)
6654 if (rtx shift_amt = shift_amt_for_vec_perm_mask (sel))
6656 create_convert_operand_from_type (&ops[2], shift_amt,
6657 sizetype_tab[(int) stk_sizetype]);
6658 if (maybe_expand_insn (shift_code, 3, ops))
6659 return ops[0].value;
6661 create_input_operand (&ops[2], v1, tmode);
6664 if (maybe_expand_insn (icode, 4, ops))
6665 return ops[0].value;
6666 return NULL_RTX;
6669 /* Generate instructions for vec_perm optab given its mode
6670 and three operands. */
6673 expand_vec_perm (machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6675 enum insn_code icode;
6676 machine_mode qimode;
6677 unsigned int i, w, e, u;
6678 rtx tmp, sel_qi = NULL;
6679 rtvec vec;
6681 if (!target || GET_MODE (target) != mode)
6682 target = gen_reg_rtx (mode);
6684 w = GET_MODE_SIZE (mode);
6685 e = GET_MODE_NUNITS (mode);
6686 u = GET_MODE_UNIT_SIZE (mode);
6688 /* Set QIMODE to a different vector mode with byte elements.
6689 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6690 qimode = VOIDmode;
6691 if (GET_MODE_INNER (mode) != QImode)
6693 qimode = mode_for_vector (QImode, w);
6694 if (!VECTOR_MODE_P (qimode))
6695 qimode = VOIDmode;
6698 /* If the input is a constant, expand it specially. */
6699 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6700 if (GET_CODE (sel) == CONST_VECTOR)
6702 icode = direct_optab_handler (vec_perm_const_optab, mode);
6703 if (icode != CODE_FOR_nothing)
6705 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6706 if (tmp)
6707 return tmp;
6710 /* Fall back to a constant byte-based permutation. */
6711 if (qimode != VOIDmode)
6713 vec = rtvec_alloc (w);
6714 for (i = 0; i < e; ++i)
6716 unsigned int j, this_e;
6718 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6719 this_e &= 2 * e - 1;
6720 this_e *= u;
6722 for (j = 0; j < u; ++j)
6723 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
6725 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
6727 icode = direct_optab_handler (vec_perm_const_optab, qimode);
6728 if (icode != CODE_FOR_nothing)
6730 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6731 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6732 gen_lowpart (qimode, v1), sel_qi);
6733 if (tmp)
6734 return gen_lowpart (mode, tmp);
6739 /* Otherwise expand as a fully variable permuation. */
6740 icode = direct_optab_handler (vec_perm_optab, mode);
6741 if (icode != CODE_FOR_nothing)
6743 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6744 if (tmp)
6745 return tmp;
6748 /* As a special case to aid several targets, lower the element-based
6749 permutation to a byte-based permutation and try again. */
6750 if (qimode == VOIDmode)
6751 return NULL_RTX;
6752 icode = direct_optab_handler (vec_perm_optab, qimode);
6753 if (icode == CODE_FOR_nothing)
6754 return NULL_RTX;
6756 if (sel_qi == NULL)
6758 /* Multiply each element by its byte size. */
6759 machine_mode selmode = GET_MODE (sel);
6760 if (u == 2)
6761 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6762 NULL, 0, OPTAB_DIRECT);
6763 else
6764 sel = expand_simple_binop (selmode, ASHIFT, sel,
6765 GEN_INT (exact_log2 (u)),
6766 NULL, 0, OPTAB_DIRECT);
6767 gcc_assert (sel != NULL);
6769 /* Broadcast the low byte each element into each of its bytes. */
6770 vec = rtvec_alloc (w);
6771 for (i = 0; i < w; ++i)
6773 int this_e = i / u * u;
6774 if (BYTES_BIG_ENDIAN)
6775 this_e += u - 1;
6776 RTVEC_ELT (vec, i) = GEN_INT (this_e);
6778 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6779 sel = gen_lowpart (qimode, sel);
6780 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
6781 gcc_assert (sel != NULL);
6783 /* Add the byte offset to each byte element. */
6784 /* Note that the definition of the indicies here is memory ordering,
6785 so there should be no difference between big and little endian. */
6786 vec = rtvec_alloc (w);
6787 for (i = 0; i < w; ++i)
6788 RTVEC_ELT (vec, i) = GEN_INT (i % u);
6789 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6790 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6791 sel, 0, OPTAB_DIRECT);
6792 gcc_assert (sel_qi != NULL);
6795 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6796 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6797 gen_lowpart (qimode, v1), sel_qi);
6798 if (tmp)
6799 tmp = gen_lowpart (mode, tmp);
6800 return tmp;
6803 /* Return insn code for a conditional operator with a comparison in
6804 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
6806 static inline enum insn_code
6807 get_vcond_icode (machine_mode vmode, machine_mode cmode, bool uns)
6809 enum insn_code icode = CODE_FOR_nothing;
6810 if (uns)
6811 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
6812 else
6813 icode = convert_optab_handler (vcond_optab, vmode, cmode);
6814 return icode;
6817 /* Return TRUE iff, appropriate vector insns are available
6818 for vector cond expr with vector type VALUE_TYPE and a comparison
6819 with operand vector types in CMP_OP_TYPE. */
6821 bool
6822 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
6824 machine_mode value_mode = TYPE_MODE (value_type);
6825 machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
6826 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
6827 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
6828 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
6829 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
6830 return false;
6831 return true;
6834 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6835 three operands. */
6838 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6839 rtx target)
6841 struct expand_operand ops[6];
6842 enum insn_code icode;
6843 rtx comparison, rtx_op1, rtx_op2;
6844 machine_mode mode = TYPE_MODE (vec_cond_type);
6845 machine_mode cmp_op_mode;
6846 bool unsignedp;
6847 tree op0a, op0b;
6848 enum tree_code tcode;
6850 if (COMPARISON_CLASS_P (op0))
6852 op0a = TREE_OPERAND (op0, 0);
6853 op0b = TREE_OPERAND (op0, 1);
6854 tcode = TREE_CODE (op0);
6856 else
6858 /* Fake op0 < 0. */
6859 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (op0)));
6860 op0a = op0;
6861 op0b = build_zero_cst (TREE_TYPE (op0));
6862 tcode = LT_EXPR;
6864 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6865 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
6868 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
6869 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
6871 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
6872 if (icode == CODE_FOR_nothing)
6873 return 0;
6875 comparison = vector_compare_rtx (tcode, op0a, op0b, unsignedp, icode);
6876 rtx_op1 = expand_normal (op1);
6877 rtx_op2 = expand_normal (op2);
6879 create_output_operand (&ops[0], target, mode);
6880 create_input_operand (&ops[1], rtx_op1, mode);
6881 create_input_operand (&ops[2], rtx_op2, mode);
6882 create_fixed_operand (&ops[3], comparison);
6883 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6884 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6885 expand_insn (icode, 6, ops);
6886 return ops[0].value;
6889 /* Return non-zero if a highpart multiply is supported of can be synthisized.
6890 For the benefit of expand_mult_highpart, the return value is 1 for direct,
6891 2 for even/odd widening, and 3 for hi/lo widening. */
6894 can_mult_highpart_p (machine_mode mode, bool uns_p)
6896 optab op;
6897 unsigned char *sel;
6898 unsigned i, nunits;
6900 op = uns_p ? umul_highpart_optab : smul_highpart_optab;
6901 if (optab_handler (op, mode) != CODE_FOR_nothing)
6902 return 1;
6904 /* If the mode is an integral vector, synth from widening operations. */
6905 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
6906 return 0;
6908 nunits = GET_MODE_NUNITS (mode);
6909 sel = XALLOCAVEC (unsigned char, nunits);
6911 op = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6912 if (optab_handler (op, mode) != CODE_FOR_nothing)
6914 op = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6915 if (optab_handler (op, mode) != CODE_FOR_nothing)
6917 for (i = 0; i < nunits; ++i)
6918 sel[i] = !BYTES_BIG_ENDIAN + (i & ~1) + ((i & 1) ? nunits : 0);
6919 if (can_vec_perm_p (mode, false, sel))
6920 return 2;
6924 op = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6925 if (optab_handler (op, mode) != CODE_FOR_nothing)
6927 op = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6928 if (optab_handler (op, mode) != CODE_FOR_nothing)
6930 for (i = 0; i < nunits; ++i)
6931 sel[i] = 2 * i + (BYTES_BIG_ENDIAN ? 0 : 1);
6932 if (can_vec_perm_p (mode, false, sel))
6933 return 3;
6937 return 0;
6940 /* Expand a highpart multiply. */
6943 expand_mult_highpart (machine_mode mode, rtx op0, rtx op1,
6944 rtx target, bool uns_p)
6946 struct expand_operand eops[3];
6947 enum insn_code icode;
6948 int method, i, nunits;
6949 machine_mode wmode;
6950 rtx m1, m2, perm;
6951 optab tab1, tab2;
6952 rtvec v;
6954 method = can_mult_highpart_p (mode, uns_p);
6955 switch (method)
6957 case 0:
6958 return NULL_RTX;
6959 case 1:
6960 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6961 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6962 OPTAB_LIB_WIDEN);
6963 case 2:
6964 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6965 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6966 break;
6967 case 3:
6968 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6969 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6970 if (BYTES_BIG_ENDIAN)
6972 optab t = tab1;
6973 tab1 = tab2;
6974 tab2 = t;
6976 break;
6977 default:
6978 gcc_unreachable ();
6981 icode = optab_handler (tab1, mode);
6982 nunits = GET_MODE_NUNITS (mode);
6983 wmode = insn_data[icode].operand[0].mode;
6984 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
6985 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
6987 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6988 create_input_operand (&eops[1], op0, mode);
6989 create_input_operand (&eops[2], op1, mode);
6990 expand_insn (icode, 3, eops);
6991 m1 = gen_lowpart (mode, eops[0].value);
6993 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6994 create_input_operand (&eops[1], op0, mode);
6995 create_input_operand (&eops[2], op1, mode);
6996 expand_insn (optab_handler (tab2, mode), 3, eops);
6997 m2 = gen_lowpart (mode, eops[0].value);
6999 v = rtvec_alloc (nunits);
7000 if (method == 2)
7002 for (i = 0; i < nunits; ++i)
7003 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
7004 + ((i & 1) ? nunits : 0));
7006 else
7008 for (i = 0; i < nunits; ++i)
7009 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
7011 perm = gen_rtx_CONST_VECTOR (mode, v);
7013 return expand_vec_perm (mode, m1, m2, perm, target);
7016 /* Return true if target supports vector masked load/store for mode. */
7017 bool
7018 can_vec_mask_load_store_p (machine_mode mode, bool is_load)
7020 optab op = is_load ? maskload_optab : maskstore_optab;
7021 machine_mode vmode;
7022 unsigned int vector_sizes;
7024 /* If mode is vector mode, check it directly. */
7025 if (VECTOR_MODE_P (mode))
7026 return optab_handler (op, mode) != CODE_FOR_nothing;
7028 /* Otherwise, return true if there is some vector mode with
7029 the mask load/store supported. */
7031 /* See if there is any chance the mask load or store might be
7032 vectorized. If not, punt. */
7033 vmode = targetm.vectorize.preferred_simd_mode (mode);
7034 if (!VECTOR_MODE_P (vmode))
7035 return false;
7037 if (optab_handler (op, vmode) != CODE_FOR_nothing)
7038 return true;
7040 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
7041 while (vector_sizes != 0)
7043 unsigned int cur = 1 << floor_log2 (vector_sizes);
7044 vector_sizes &= ~cur;
7045 if (cur <= GET_MODE_SIZE (mode))
7046 continue;
7047 vmode = mode_for_vector (mode, cur / GET_MODE_SIZE (mode));
7048 if (VECTOR_MODE_P (vmode)
7049 && optab_handler (op, vmode) != CODE_FOR_nothing)
7050 return true;
7052 return false;
7055 /* Return true if there is a compare_and_swap pattern. */
7057 bool
7058 can_compare_and_swap_p (machine_mode mode, bool allow_libcall)
7060 enum insn_code icode;
7062 /* Check for __atomic_compare_and_swap. */
7063 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7064 if (icode != CODE_FOR_nothing)
7065 return true;
7067 /* Check for __sync_compare_and_swap. */
7068 icode = optab_handler (sync_compare_and_swap_optab, mode);
7069 if (icode != CODE_FOR_nothing)
7070 return true;
7071 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
7072 return true;
7074 /* No inline compare and swap. */
7075 return false;
7078 /* Return true if an atomic exchange can be performed. */
7080 bool
7081 can_atomic_exchange_p (machine_mode mode, bool allow_libcall)
7083 enum insn_code icode;
7085 /* Check for __atomic_exchange. */
7086 icode = direct_optab_handler (atomic_exchange_optab, mode);
7087 if (icode != CODE_FOR_nothing)
7088 return true;
7090 /* Don't check __sync_test_and_set, as on some platforms that
7091 has reduced functionality. Targets that really do support
7092 a proper exchange should simply be updated to the __atomics. */
7094 return can_compare_and_swap_p (mode, allow_libcall);
7098 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
7099 pattern. */
7101 static void
7102 find_cc_set (rtx x, const_rtx pat, void *data)
7104 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
7105 && GET_CODE (pat) == SET)
7107 rtx *p_cc_reg = (rtx *) data;
7108 gcc_assert (!*p_cc_reg);
7109 *p_cc_reg = x;
7113 /* This is a helper function for the other atomic operations. This function
7114 emits a loop that contains SEQ that iterates until a compare-and-swap
7115 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7116 a set of instructions that takes a value from OLD_REG as an input and
7117 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7118 set to the current contents of MEM. After SEQ, a compare-and-swap will
7119 attempt to update MEM with NEW_REG. The function returns true when the
7120 loop was generated successfully. */
7122 static bool
7123 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7125 machine_mode mode = GET_MODE (mem);
7126 rtx_code_label *label;
7127 rtx cmp_reg, success, oldval;
7129 /* The loop we want to generate looks like
7131 cmp_reg = mem;
7132 label:
7133 old_reg = cmp_reg;
7134 seq;
7135 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
7136 if (success)
7137 goto label;
7139 Note that we only do the plain load from memory once. Subsequent
7140 iterations use the value loaded by the compare-and-swap pattern. */
7142 label = gen_label_rtx ();
7143 cmp_reg = gen_reg_rtx (mode);
7145 emit_move_insn (cmp_reg, mem);
7146 emit_label (label);
7147 emit_move_insn (old_reg, cmp_reg);
7148 if (seq)
7149 emit_insn (seq);
7151 success = NULL_RTX;
7152 oldval = cmp_reg;
7153 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
7154 new_reg, false, MEMMODEL_SYNC_SEQ_CST,
7155 MEMMODEL_RELAXED))
7156 return false;
7158 if (oldval != cmp_reg)
7159 emit_move_insn (cmp_reg, oldval);
7161 /* Mark this jump predicted not taken. */
7162 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
7163 GET_MODE (success), 1, label, 0);
7164 return true;
7168 /* This function tries to emit an atomic_exchange intruction. VAL is written
7169 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
7170 using TARGET if possible. */
7172 static rtx
7173 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7175 machine_mode mode = GET_MODE (mem);
7176 enum insn_code icode;
7178 /* If the target supports the exchange directly, great. */
7179 icode = direct_optab_handler (atomic_exchange_optab, mode);
7180 if (icode != CODE_FOR_nothing)
7182 struct expand_operand ops[4];
7184 create_output_operand (&ops[0], target, mode);
7185 create_fixed_operand (&ops[1], mem);
7186 create_input_operand (&ops[2], val, mode);
7187 create_integer_operand (&ops[3], model);
7188 if (maybe_expand_insn (icode, 4, ops))
7189 return ops[0].value;
7192 return NULL_RTX;
7195 /* This function tries to implement an atomic exchange operation using
7196 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7197 The previous contents of *MEM are returned, using TARGET if possible.
7198 Since this instructionn is an acquire barrier only, stronger memory
7199 models may require additional barriers to be emitted. */
7201 static rtx
7202 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7203 enum memmodel model)
7205 machine_mode mode = GET_MODE (mem);
7206 enum insn_code icode;
7207 rtx_insn *last_insn = get_last_insn ();
7209 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7211 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7212 exists, and the memory model is stronger than acquire, add a release
7213 barrier before the instruction. */
7215 if (is_mm_seq_cst (model) || is_mm_release (model) || is_mm_acq_rel (model))
7216 expand_mem_thread_fence (model);
7218 if (icode != CODE_FOR_nothing)
7220 struct expand_operand ops[3];
7221 create_output_operand (&ops[0], target, mode);
7222 create_fixed_operand (&ops[1], mem);
7223 create_input_operand (&ops[2], val, mode);
7224 if (maybe_expand_insn (icode, 3, ops))
7225 return ops[0].value;
7228 /* If an external test-and-set libcall is provided, use that instead of
7229 any external compare-and-swap that we might get from the compare-and-
7230 swap-loop expansion later. */
7231 if (!can_compare_and_swap_p (mode, false))
7233 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7234 if (libfunc != NULL)
7236 rtx addr;
7238 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7239 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7240 mode, 2, addr, ptr_mode,
7241 val, mode);
7245 /* If the test_and_set can't be emitted, eliminate any barrier that might
7246 have been emitted. */
7247 delete_insns_since (last_insn);
7248 return NULL_RTX;
7251 /* This function tries to implement an atomic exchange operation using a
7252 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7253 *MEM are returned, using TARGET if possible. No memory model is required
7254 since a compare_and_swap loop is seq-cst. */
7256 static rtx
7257 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7259 machine_mode mode = GET_MODE (mem);
7261 if (can_compare_and_swap_p (mode, true))
7263 if (!target || !register_operand (target, mode))
7264 target = gen_reg_rtx (mode);
7265 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7266 return target;
7269 return NULL_RTX;
7272 /* This function tries to implement an atomic test-and-set operation
7273 using the atomic_test_and_set instruction pattern. A boolean value
7274 is returned from the operation, using TARGET if possible. */
7276 #ifndef HAVE_atomic_test_and_set
7277 #define HAVE_atomic_test_and_set 0
7278 #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
7279 #endif
7281 static rtx
7282 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7284 machine_mode pat_bool_mode;
7285 struct expand_operand ops[3];
7287 if (!HAVE_atomic_test_and_set)
7288 return NULL_RTX;
7290 /* While we always get QImode from __atomic_test_and_set, we get
7291 other memory modes from __sync_lock_test_and_set. Note that we
7292 use no endian adjustment here. This matches the 4.6 behavior
7293 in the Sparc backend. */
7294 gcc_checking_assert
7295 (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
7296 if (GET_MODE (mem) != QImode)
7297 mem = adjust_address_nv (mem, QImode, 0);
7299 pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
7300 create_output_operand (&ops[0], target, pat_bool_mode);
7301 create_fixed_operand (&ops[1], mem);
7302 create_integer_operand (&ops[2], model);
7304 if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
7305 return ops[0].value;
7306 return NULL_RTX;
7309 /* This function expands the legacy _sync_lock test_and_set operation which is
7310 generally an atomic exchange. Some limited targets only allow the
7311 constant 1 to be stored. This is an ACQUIRE operation.
7313 TARGET is an optional place to stick the return value.
7314 MEM is where VAL is stored. */
7317 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7319 rtx ret;
7321 /* Try an atomic_exchange first. */
7322 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_SYNC_ACQUIRE);
7323 if (ret)
7324 return ret;
7326 ret = maybe_emit_sync_lock_test_and_set (target, mem, val,
7327 MEMMODEL_SYNC_ACQUIRE);
7328 if (ret)
7329 return ret;
7331 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7332 if (ret)
7333 return ret;
7335 /* If there are no other options, try atomic_test_and_set if the value
7336 being stored is 1. */
7337 if (val == const1_rtx)
7338 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_SYNC_ACQUIRE);
7340 return ret;
7343 /* This function expands the atomic test_and_set operation:
7344 atomically store a boolean TRUE into MEM and return the previous value.
7346 MEMMODEL is the memory model variant to use.
7347 TARGET is an optional place to stick the return value. */
7350 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7352 machine_mode mode = GET_MODE (mem);
7353 rtx ret, trueval, subtarget;
7355 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7356 if (ret)
7357 return ret;
7359 /* Be binary compatible with non-default settings of trueval, and different
7360 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7361 another only has atomic-exchange. */
7362 if (targetm.atomic_test_and_set_trueval == 1)
7364 trueval = const1_rtx;
7365 subtarget = target ? target : gen_reg_rtx (mode);
7367 else
7369 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7370 subtarget = gen_reg_rtx (mode);
7373 /* Try the atomic-exchange optab... */
7374 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7376 /* ... then an atomic-compare-and-swap loop ... */
7377 if (!ret)
7378 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7380 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7381 if (!ret)
7382 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7384 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7385 things with the value 1. Thus we try again without trueval. */
7386 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7387 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7389 /* Failing all else, assume a single threaded environment and simply
7390 perform the operation. */
7391 if (!ret)
7393 /* If the result is ignored skip the move to target. */
7394 if (subtarget != const0_rtx)
7395 emit_move_insn (subtarget, mem);
7397 emit_move_insn (mem, trueval);
7398 ret = subtarget;
7401 /* Recall that have to return a boolean value; rectify if trueval
7402 is not exactly one. */
7403 if (targetm.atomic_test_and_set_trueval != 1)
7404 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7406 return ret;
7409 /* This function expands the atomic exchange operation:
7410 atomically store VAL in MEM and return the previous value in MEM.
7412 MEMMODEL is the memory model variant to use.
7413 TARGET is an optional place to stick the return value. */
7416 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7418 rtx ret;
7420 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7422 /* Next try a compare-and-swap loop for the exchange. */
7423 if (!ret)
7424 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7426 return ret;
7429 /* This function expands the atomic compare exchange operation:
7431 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7432 *PTARGET_OVAL is an optional place to store the old value from memory.
7433 Both target parameters may be NULL to indicate that we do not care about
7434 that return value. Both target parameters are updated on success to
7435 the actual location of the corresponding result.
7437 MEMMODEL is the memory model variant to use.
7439 The return value of the function is true for success. */
7441 bool
7442 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7443 rtx mem, rtx expected, rtx desired,
7444 bool is_weak, enum memmodel succ_model,
7445 enum memmodel fail_model)
7447 machine_mode mode = GET_MODE (mem);
7448 struct expand_operand ops[8];
7449 enum insn_code icode;
7450 rtx target_oval, target_bool = NULL_RTX;
7451 rtx libfunc;
7453 /* Load expected into a register for the compare and swap. */
7454 if (MEM_P (expected))
7455 expected = copy_to_reg (expected);
7457 /* Make sure we always have some place to put the return oldval.
7458 Further, make sure that place is distinct from the input expected,
7459 just in case we need that path down below. */
7460 if (ptarget_oval == NULL
7461 || (target_oval = *ptarget_oval) == NULL
7462 || reg_overlap_mentioned_p (expected, target_oval))
7463 target_oval = gen_reg_rtx (mode);
7465 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7466 if (icode != CODE_FOR_nothing)
7468 machine_mode bool_mode = insn_data[icode].operand[0].mode;
7470 /* Make sure we always have a place for the bool operand. */
7471 if (ptarget_bool == NULL
7472 || (target_bool = *ptarget_bool) == NULL
7473 || GET_MODE (target_bool) != bool_mode)
7474 target_bool = gen_reg_rtx (bool_mode);
7476 /* Emit the compare_and_swap. */
7477 create_output_operand (&ops[0], target_bool, bool_mode);
7478 create_output_operand (&ops[1], target_oval, mode);
7479 create_fixed_operand (&ops[2], mem);
7480 create_input_operand (&ops[3], expected, mode);
7481 create_input_operand (&ops[4], desired, mode);
7482 create_integer_operand (&ops[5], is_weak);
7483 create_integer_operand (&ops[6], succ_model);
7484 create_integer_operand (&ops[7], fail_model);
7485 if (maybe_expand_insn (icode, 8, ops))
7487 /* Return success/failure. */
7488 target_bool = ops[0].value;
7489 target_oval = ops[1].value;
7490 goto success;
7494 /* Otherwise fall back to the original __sync_val_compare_and_swap
7495 which is always seq-cst. */
7496 icode = optab_handler (sync_compare_and_swap_optab, mode);
7497 if (icode != CODE_FOR_nothing)
7499 rtx cc_reg;
7501 create_output_operand (&ops[0], target_oval, mode);
7502 create_fixed_operand (&ops[1], mem);
7503 create_input_operand (&ops[2], expected, mode);
7504 create_input_operand (&ops[3], desired, mode);
7505 if (!maybe_expand_insn (icode, 4, ops))
7506 return false;
7508 target_oval = ops[0].value;
7510 /* If the caller isn't interested in the boolean return value,
7511 skip the computation of it. */
7512 if (ptarget_bool == NULL)
7513 goto success;
7515 /* Otherwise, work out if the compare-and-swap succeeded. */
7516 cc_reg = NULL_RTX;
7517 if (have_insn_for (COMPARE, CCmode))
7518 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7519 if (cc_reg)
7521 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7522 const0_rtx, VOIDmode, 0, 1);
7523 goto success;
7525 goto success_bool_from_val;
7528 /* Also check for library support for __sync_val_compare_and_swap. */
7529 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7530 if (libfunc != NULL)
7532 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7533 target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7534 mode, 3, addr, ptr_mode,
7535 expected, mode, desired, mode);
7537 /* Compute the boolean return value only if requested. */
7538 if (ptarget_bool)
7539 goto success_bool_from_val;
7540 else
7541 goto success;
7544 /* Failure. */
7545 return false;
7547 success_bool_from_val:
7548 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7549 expected, VOIDmode, 1, 1);
7550 success:
7551 /* Make sure that the oval output winds up where the caller asked. */
7552 if (ptarget_oval)
7553 *ptarget_oval = target_oval;
7554 if (ptarget_bool)
7555 *ptarget_bool = target_bool;
7556 return true;
7559 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7561 static void
7562 expand_asm_memory_barrier (void)
7564 rtx asm_op, clob;
7566 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7567 rtvec_alloc (0), rtvec_alloc (0),
7568 rtvec_alloc (0), UNKNOWN_LOCATION);
7569 MEM_VOLATILE_P (asm_op) = 1;
7571 clob = gen_rtx_SCRATCH (VOIDmode);
7572 clob = gen_rtx_MEM (BLKmode, clob);
7573 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7575 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7578 /* This routine will either emit the mem_thread_fence pattern or issue a
7579 sync_synchronize to generate a fence for memory model MEMMODEL. */
7581 void
7582 expand_mem_thread_fence (enum memmodel model)
7584 if (HAVE_mem_thread_fence)
7585 emit_insn (gen_mem_thread_fence (GEN_INT (model)));
7586 else if (!is_mm_relaxed (model))
7588 if (HAVE_memory_barrier)
7589 emit_insn (gen_memory_barrier ());
7590 else if (synchronize_libfunc != NULL_RTX)
7591 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7592 else
7593 expand_asm_memory_barrier ();
7597 /* This routine will either emit the mem_signal_fence pattern or issue a
7598 sync_synchronize to generate a fence for memory model MEMMODEL. */
7600 void
7601 expand_mem_signal_fence (enum memmodel model)
7603 if (HAVE_mem_signal_fence)
7604 emit_insn (gen_mem_signal_fence (GEN_INT (model)));
7605 else if (!is_mm_relaxed (model))
7607 /* By default targets are coherent between a thread and the signal
7608 handler running on the same thread. Thus this really becomes a
7609 compiler barrier, in that stores must not be sunk past
7610 (or raised above) a given point. */
7611 expand_asm_memory_barrier ();
7615 /* This function expands the atomic load operation:
7616 return the atomically loaded value in MEM.
7618 MEMMODEL is the memory model variant to use.
7619 TARGET is an option place to stick the return value. */
7622 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7624 machine_mode mode = GET_MODE (mem);
7625 enum insn_code icode;
7627 /* If the target supports the load directly, great. */
7628 icode = direct_optab_handler (atomic_load_optab, mode);
7629 if (icode != CODE_FOR_nothing)
7631 struct expand_operand ops[3];
7633 create_output_operand (&ops[0], target, mode);
7634 create_fixed_operand (&ops[1], mem);
7635 create_integer_operand (&ops[2], model);
7636 if (maybe_expand_insn (icode, 3, ops))
7637 return ops[0].value;
7640 /* If the size of the object is greater than word size on this target,
7641 then we assume that a load will not be atomic. */
7642 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7644 /* Issue val = compare_and_swap (mem, 0, 0).
7645 This may cause the occasional harmless store of 0 when the value is
7646 already 0, but it seems to be OK according to the standards guys. */
7647 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7648 const0_rtx, false, model, model))
7649 return target;
7650 else
7651 /* Otherwise there is no atomic load, leave the library call. */
7652 return NULL_RTX;
7655 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7656 if (!target || target == const0_rtx)
7657 target = gen_reg_rtx (mode);
7659 /* For SEQ_CST, emit a barrier before the load. */
7660 if (is_mm_seq_cst (model))
7661 expand_mem_thread_fence (model);
7663 emit_move_insn (target, mem);
7665 /* Emit the appropriate barrier after the load. */
7666 expand_mem_thread_fence (model);
7668 return target;
7671 /* This function expands the atomic store operation:
7672 Atomically store VAL in MEM.
7673 MEMMODEL is the memory model variant to use.
7674 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7675 function returns const0_rtx if a pattern was emitted. */
7678 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7680 machine_mode mode = GET_MODE (mem);
7681 enum insn_code icode;
7682 struct expand_operand ops[3];
7684 /* If the target supports the store directly, great. */
7685 icode = direct_optab_handler (atomic_store_optab, mode);
7686 if (icode != CODE_FOR_nothing)
7688 create_fixed_operand (&ops[0], mem);
7689 create_input_operand (&ops[1], val, mode);
7690 create_integer_operand (&ops[2], model);
7691 if (maybe_expand_insn (icode, 3, ops))
7692 return const0_rtx;
7695 /* If using __sync_lock_release is a viable alternative, try it. */
7696 if (use_release)
7698 icode = direct_optab_handler (sync_lock_release_optab, mode);
7699 if (icode != CODE_FOR_nothing)
7701 create_fixed_operand (&ops[0], mem);
7702 create_input_operand (&ops[1], const0_rtx, mode);
7703 if (maybe_expand_insn (icode, 2, ops))
7705 /* lock_release is only a release barrier. */
7706 if (is_mm_seq_cst (model))
7707 expand_mem_thread_fence (model);
7708 return const0_rtx;
7713 /* If the size of the object is greater than word size on this target,
7714 a default store will not be atomic, Try a mem_exchange and throw away
7715 the result. If that doesn't work, don't do anything. */
7716 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7718 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7719 if (!target)
7720 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7721 if (target)
7722 return const0_rtx;
7723 else
7724 return NULL_RTX;
7727 /* Otherwise assume stores are atomic, and emit the proper barriers. */
7728 expand_mem_thread_fence (model);
7730 emit_move_insn (mem, val);
7732 /* For SEQ_CST, also emit a barrier after the store. */
7733 if (is_mm_seq_cst (model))
7734 expand_mem_thread_fence (model);
7736 return const0_rtx;
7740 /* Structure containing the pointers and values required to process the
7741 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7743 struct atomic_op_functions
7745 direct_optab mem_fetch_before;
7746 direct_optab mem_fetch_after;
7747 direct_optab mem_no_result;
7748 optab fetch_before;
7749 optab fetch_after;
7750 direct_optab no_result;
7751 enum rtx_code reverse_code;
7755 /* Fill in structure pointed to by OP with the various optab entries for an
7756 operation of type CODE. */
7758 static void
7759 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7761 gcc_assert (op!= NULL);
7763 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7764 in the source code during compilation, and the optab entries are not
7765 computable until runtime. Fill in the values at runtime. */
7766 switch (code)
7768 case PLUS:
7769 op->mem_fetch_before = atomic_fetch_add_optab;
7770 op->mem_fetch_after = atomic_add_fetch_optab;
7771 op->mem_no_result = atomic_add_optab;
7772 op->fetch_before = sync_old_add_optab;
7773 op->fetch_after = sync_new_add_optab;
7774 op->no_result = sync_add_optab;
7775 op->reverse_code = MINUS;
7776 break;
7777 case MINUS:
7778 op->mem_fetch_before = atomic_fetch_sub_optab;
7779 op->mem_fetch_after = atomic_sub_fetch_optab;
7780 op->mem_no_result = atomic_sub_optab;
7781 op->fetch_before = sync_old_sub_optab;
7782 op->fetch_after = sync_new_sub_optab;
7783 op->no_result = sync_sub_optab;
7784 op->reverse_code = PLUS;
7785 break;
7786 case XOR:
7787 op->mem_fetch_before = atomic_fetch_xor_optab;
7788 op->mem_fetch_after = atomic_xor_fetch_optab;
7789 op->mem_no_result = atomic_xor_optab;
7790 op->fetch_before = sync_old_xor_optab;
7791 op->fetch_after = sync_new_xor_optab;
7792 op->no_result = sync_xor_optab;
7793 op->reverse_code = XOR;
7794 break;
7795 case AND:
7796 op->mem_fetch_before = atomic_fetch_and_optab;
7797 op->mem_fetch_after = atomic_and_fetch_optab;
7798 op->mem_no_result = atomic_and_optab;
7799 op->fetch_before = sync_old_and_optab;
7800 op->fetch_after = sync_new_and_optab;
7801 op->no_result = sync_and_optab;
7802 op->reverse_code = UNKNOWN;
7803 break;
7804 case IOR:
7805 op->mem_fetch_before = atomic_fetch_or_optab;
7806 op->mem_fetch_after = atomic_or_fetch_optab;
7807 op->mem_no_result = atomic_or_optab;
7808 op->fetch_before = sync_old_ior_optab;
7809 op->fetch_after = sync_new_ior_optab;
7810 op->no_result = sync_ior_optab;
7811 op->reverse_code = UNKNOWN;
7812 break;
7813 case NOT:
7814 op->mem_fetch_before = atomic_fetch_nand_optab;
7815 op->mem_fetch_after = atomic_nand_fetch_optab;
7816 op->mem_no_result = atomic_nand_optab;
7817 op->fetch_before = sync_old_nand_optab;
7818 op->fetch_after = sync_new_nand_optab;
7819 op->no_result = sync_nand_optab;
7820 op->reverse_code = UNKNOWN;
7821 break;
7822 default:
7823 gcc_unreachable ();
7827 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7828 using memory order MODEL. If AFTER is true the operation needs to return
7829 the value of *MEM after the operation, otherwise the previous value.
7830 TARGET is an optional place to place the result. The result is unused if
7831 it is const0_rtx.
7832 Return the result if there is a better sequence, otherwise NULL_RTX. */
7834 static rtx
7835 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7836 enum memmodel model, bool after)
7838 /* If the value is prefetched, or not used, it may be possible to replace
7839 the sequence with a native exchange operation. */
7840 if (!after || target == const0_rtx)
7842 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7843 if (code == AND && val == const0_rtx)
7845 if (target == const0_rtx)
7846 target = gen_reg_rtx (GET_MODE (mem));
7847 return maybe_emit_atomic_exchange (target, mem, val, model);
7850 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7851 if (code == IOR && val == constm1_rtx)
7853 if (target == const0_rtx)
7854 target = gen_reg_rtx (GET_MODE (mem));
7855 return maybe_emit_atomic_exchange (target, mem, val, model);
7859 return NULL_RTX;
7862 /* Try to emit an instruction for a specific operation varaition.
7863 OPTAB contains the OP functions.
7864 TARGET is an optional place to return the result. const0_rtx means unused.
7865 MEM is the memory location to operate on.
7866 VAL is the value to use in the operation.
7867 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7868 MODEL is the memory model, if used.
7869 AFTER is true if the returned result is the value after the operation. */
7871 static rtx
7872 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7873 rtx val, bool use_memmodel, enum memmodel model, bool after)
7875 machine_mode mode = GET_MODE (mem);
7876 struct expand_operand ops[4];
7877 enum insn_code icode;
7878 int op_counter = 0;
7879 int num_ops;
7881 /* Check to see if there is a result returned. */
7882 if (target == const0_rtx)
7884 if (use_memmodel)
7886 icode = direct_optab_handler (optab->mem_no_result, mode);
7887 create_integer_operand (&ops[2], model);
7888 num_ops = 3;
7890 else
7892 icode = direct_optab_handler (optab->no_result, mode);
7893 num_ops = 2;
7896 /* Otherwise, we need to generate a result. */
7897 else
7899 if (use_memmodel)
7901 icode = direct_optab_handler (after ? optab->mem_fetch_after
7902 : optab->mem_fetch_before, mode);
7903 create_integer_operand (&ops[3], model);
7904 num_ops = 4;
7906 else
7908 icode = optab_handler (after ? optab->fetch_after
7909 : optab->fetch_before, mode);
7910 num_ops = 3;
7912 create_output_operand (&ops[op_counter++], target, mode);
7914 if (icode == CODE_FOR_nothing)
7915 return NULL_RTX;
7917 create_fixed_operand (&ops[op_counter++], mem);
7918 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7919 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7921 if (maybe_expand_insn (icode, num_ops, ops))
7922 return (target == const0_rtx ? const0_rtx : ops[0].value);
7924 return NULL_RTX;
7928 /* This function expands an atomic fetch_OP or OP_fetch operation:
7929 TARGET is an option place to stick the return value. const0_rtx indicates
7930 the result is unused.
7931 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7932 CODE is the operation being performed (OP)
7933 MEMMODEL is the memory model variant to use.
7934 AFTER is true to return the result of the operation (OP_fetch).
7935 AFTER is false to return the value before the operation (fetch_OP).
7937 This function will *only* generate instructions if there is a direct
7938 optab. No compare and swap loops or libcalls will be generated. */
7940 static rtx
7941 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7942 enum rtx_code code, enum memmodel model,
7943 bool after)
7945 machine_mode mode = GET_MODE (mem);
7946 struct atomic_op_functions optab;
7947 rtx result;
7948 bool unused_result = (target == const0_rtx);
7950 get_atomic_op_for_code (&optab, code);
7952 /* Check to see if there are any better instructions. */
7953 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7954 if (result)
7955 return result;
7957 /* Check for the case where the result isn't used and try those patterns. */
7958 if (unused_result)
7960 /* Try the memory model variant first. */
7961 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7962 if (result)
7963 return result;
7965 /* Next try the old style withuot a memory model. */
7966 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7967 if (result)
7968 return result;
7970 /* There is no no-result pattern, so try patterns with a result. */
7971 target = NULL_RTX;
7974 /* Try the __atomic version. */
7975 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
7976 if (result)
7977 return result;
7979 /* Try the older __sync version. */
7980 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
7981 if (result)
7982 return result;
7984 /* If the fetch value can be calculated from the other variation of fetch,
7985 try that operation. */
7986 if (after || unused_result || optab.reverse_code != UNKNOWN)
7988 /* Try the __atomic version, then the older __sync version. */
7989 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
7990 if (!result)
7991 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
7993 if (result)
7995 /* If the result isn't used, no need to do compensation code. */
7996 if (unused_result)
7997 return result;
7999 /* Issue compensation code. Fetch_after == fetch_before OP val.
8000 Fetch_before == after REVERSE_OP val. */
8001 if (!after)
8002 code = optab.reverse_code;
8003 if (code == NOT)
8005 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
8006 true, OPTAB_LIB_WIDEN);
8007 result = expand_simple_unop (mode, NOT, result, target, true);
8009 else
8010 result = expand_simple_binop (mode, code, result, val, target,
8011 true, OPTAB_LIB_WIDEN);
8012 return result;
8016 /* No direct opcode can be generated. */
8017 return NULL_RTX;
8022 /* This function expands an atomic fetch_OP or OP_fetch operation:
8023 TARGET is an option place to stick the return value. const0_rtx indicates
8024 the result is unused.
8025 atomically fetch MEM, perform the operation with VAL and return it to MEM.
8026 CODE is the operation being performed (OP)
8027 MEMMODEL is the memory model variant to use.
8028 AFTER is true to return the result of the operation (OP_fetch).
8029 AFTER is false to return the value before the operation (fetch_OP). */
8031 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
8032 enum memmodel model, bool after)
8034 machine_mode mode = GET_MODE (mem);
8035 rtx result;
8036 bool unused_result = (target == const0_rtx);
8038 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
8039 after);
8041 if (result)
8042 return result;
8044 /* Add/sub can be implemented by doing the reverse operation with -(val). */
8045 if (code == PLUS || code == MINUS)
8047 rtx tmp;
8048 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
8050 start_sequence ();
8051 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
8052 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
8053 model, after);
8054 if (result)
8056 /* PLUS worked so emit the insns and return. */
8057 tmp = get_insns ();
8058 end_sequence ();
8059 emit_insn (tmp);
8060 return result;
8063 /* PLUS did not work, so throw away the negation code and continue. */
8064 end_sequence ();
8067 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
8068 if (!can_compare_and_swap_p (mode, false))
8070 rtx libfunc;
8071 bool fixup = false;
8072 enum rtx_code orig_code = code;
8073 struct atomic_op_functions optab;
8075 get_atomic_op_for_code (&optab, code);
8076 libfunc = optab_libfunc (after ? optab.fetch_after
8077 : optab.fetch_before, mode);
8078 if (libfunc == NULL
8079 && (after || unused_result || optab.reverse_code != UNKNOWN))
8081 fixup = true;
8082 if (!after)
8083 code = optab.reverse_code;
8084 libfunc = optab_libfunc (after ? optab.fetch_before
8085 : optab.fetch_after, mode);
8087 if (libfunc != NULL)
8089 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
8090 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
8091 2, addr, ptr_mode, val, mode);
8093 if (!unused_result && fixup)
8094 result = expand_simple_binop (mode, code, result, val, target,
8095 true, OPTAB_LIB_WIDEN);
8096 return result;
8099 /* We need the original code for any further attempts. */
8100 code = orig_code;
8103 /* If nothing else has succeeded, default to a compare and swap loop. */
8104 if (can_compare_and_swap_p (mode, true))
8106 rtx_insn *insn;
8107 rtx t0 = gen_reg_rtx (mode), t1;
8109 start_sequence ();
8111 /* If the result is used, get a register for it. */
8112 if (!unused_result)
8114 if (!target || !register_operand (target, mode))
8115 target = gen_reg_rtx (mode);
8116 /* If fetch_before, copy the value now. */
8117 if (!after)
8118 emit_move_insn (target, t0);
8120 else
8121 target = const0_rtx;
8123 t1 = t0;
8124 if (code == NOT)
8126 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
8127 true, OPTAB_LIB_WIDEN);
8128 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
8130 else
8131 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
8132 OPTAB_LIB_WIDEN);
8134 /* For after, copy the value now. */
8135 if (!unused_result && after)
8136 emit_move_insn (target, t1);
8137 insn = get_insns ();
8138 end_sequence ();
8140 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
8141 return target;
8144 return NULL_RTX;
8147 /* Return true if OPERAND is suitable for operand number OPNO of
8148 instruction ICODE. */
8150 bool
8151 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
8153 return (!insn_data[(int) icode].operand[opno].predicate
8154 || (insn_data[(int) icode].operand[opno].predicate
8155 (operand, insn_data[(int) icode].operand[opno].mode)));
8158 /* TARGET is a target of a multiword operation that we are going to
8159 implement as a series of word-mode operations. Return true if
8160 TARGET is suitable for this purpose. */
8162 bool
8163 valid_multiword_target_p (rtx target)
8165 machine_mode mode;
8166 int i;
8168 mode = GET_MODE (target);
8169 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
8170 if (!validate_subreg (word_mode, mode, target, i))
8171 return false;
8172 return true;
8175 /* Like maybe_legitimize_operand, but do not change the code of the
8176 current rtx value. */
8178 static bool
8179 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8180 struct expand_operand *op)
8182 /* See if the operand matches in its current form. */
8183 if (insn_operand_matches (icode, opno, op->value))
8184 return true;
8186 /* If the operand is a memory whose address has no side effects,
8187 try forcing the address into a non-virtual pseudo register.
8188 The check for side effects is important because copy_to_mode_reg
8189 cannot handle things like auto-modified addresses. */
8190 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8192 rtx addr, mem;
8194 mem = op->value;
8195 addr = XEXP (mem, 0);
8196 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8197 && !side_effects_p (addr))
8199 rtx_insn *last;
8200 machine_mode mode;
8202 last = get_last_insn ();
8203 mode = get_address_mode (mem);
8204 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8205 if (insn_operand_matches (icode, opno, mem))
8207 op->value = mem;
8208 return true;
8210 delete_insns_since (last);
8214 return false;
8217 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8218 on success, storing the new operand value back in OP. */
8220 static bool
8221 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8222 struct expand_operand *op)
8224 machine_mode mode, imode;
8225 bool old_volatile_ok, result;
8227 mode = op->mode;
8228 switch (op->type)
8230 case EXPAND_FIXED:
8231 old_volatile_ok = volatile_ok;
8232 volatile_ok = true;
8233 result = maybe_legitimize_operand_same_code (icode, opno, op);
8234 volatile_ok = old_volatile_ok;
8235 return result;
8237 case EXPAND_OUTPUT:
8238 gcc_assert (mode != VOIDmode);
8239 if (op->value
8240 && op->value != const0_rtx
8241 && GET_MODE (op->value) == mode
8242 && maybe_legitimize_operand_same_code (icode, opno, op))
8243 return true;
8245 op->value = gen_reg_rtx (mode);
8246 break;
8248 case EXPAND_INPUT:
8249 input:
8250 gcc_assert (mode != VOIDmode);
8251 gcc_assert (GET_MODE (op->value) == VOIDmode
8252 || GET_MODE (op->value) == mode);
8253 if (maybe_legitimize_operand_same_code (icode, opno, op))
8254 return true;
8256 op->value = copy_to_mode_reg (mode, op->value);
8257 break;
8259 case EXPAND_CONVERT_TO:
8260 gcc_assert (mode != VOIDmode);
8261 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8262 goto input;
8264 case EXPAND_CONVERT_FROM:
8265 if (GET_MODE (op->value) != VOIDmode)
8266 mode = GET_MODE (op->value);
8267 else
8268 /* The caller must tell us what mode this value has. */
8269 gcc_assert (mode != VOIDmode);
8271 imode = insn_data[(int) icode].operand[opno].mode;
8272 if (imode != VOIDmode && imode != mode)
8274 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8275 mode = imode;
8277 goto input;
8279 case EXPAND_ADDRESS:
8280 gcc_assert (mode != VOIDmode);
8281 op->value = convert_memory_address (mode, op->value);
8282 goto input;
8284 case EXPAND_INTEGER:
8285 mode = insn_data[(int) icode].operand[opno].mode;
8286 if (mode != VOIDmode && const_int_operand (op->value, mode))
8287 goto input;
8288 break;
8290 return insn_operand_matches (icode, opno, op->value);
8293 /* Make OP describe an input operand that should have the same value
8294 as VALUE, after any mode conversion that the target might request.
8295 TYPE is the type of VALUE. */
8297 void
8298 create_convert_operand_from_type (struct expand_operand *op,
8299 rtx value, tree type)
8301 create_convert_operand_from (op, value, TYPE_MODE (type),
8302 TYPE_UNSIGNED (type));
8305 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8306 of instruction ICODE. Return true on success, leaving the new operand
8307 values in the OPS themselves. Emit no code on failure. */
8309 bool
8310 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8311 unsigned int nops, struct expand_operand *ops)
8313 rtx_insn *last;
8314 unsigned int i;
8316 last = get_last_insn ();
8317 for (i = 0; i < nops; i++)
8318 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8320 delete_insns_since (last);
8321 return false;
8323 return true;
8326 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8327 as its operands. Return the instruction pattern on success,
8328 and emit any necessary set-up code. Return null and emit no
8329 code on failure. */
8331 rtx_insn *
8332 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8333 struct expand_operand *ops)
8335 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8336 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8337 return NULL;
8339 switch (nops)
8341 case 1:
8342 return GEN_FCN (icode) (ops[0].value);
8343 case 2:
8344 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8345 case 3:
8346 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8347 case 4:
8348 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8349 ops[3].value);
8350 case 5:
8351 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8352 ops[3].value, ops[4].value);
8353 case 6:
8354 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8355 ops[3].value, ops[4].value, ops[5].value);
8356 case 7:
8357 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8358 ops[3].value, ops[4].value, ops[5].value,
8359 ops[6].value);
8360 case 8:
8361 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8362 ops[3].value, ops[4].value, ops[5].value,
8363 ops[6].value, ops[7].value);
8364 case 9:
8365 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8366 ops[3].value, ops[4].value, ops[5].value,
8367 ops[6].value, ops[7].value, ops[8].value);
8369 gcc_unreachable ();
8372 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8373 as its operands. Return true on success and emit no code on failure. */
8375 bool
8376 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8377 struct expand_operand *ops)
8379 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
8380 if (pat)
8382 emit_insn (pat);
8383 return true;
8385 return false;
8388 /* Like maybe_expand_insn, but for jumps. */
8390 bool
8391 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8392 struct expand_operand *ops)
8394 rtx_insn *pat = maybe_gen_insn (icode, nops, ops);
8395 if (pat)
8397 emit_jump_insn (pat);
8398 return true;
8400 return false;
8403 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8404 as its operands. */
8406 void
8407 expand_insn (enum insn_code icode, unsigned int nops,
8408 struct expand_operand *ops)
8410 if (!maybe_expand_insn (icode, nops, ops))
8411 gcc_unreachable ();
8414 /* Like expand_insn, but for jumps. */
8416 void
8417 expand_jump_insn (enum insn_code icode, unsigned int nops,
8418 struct expand_operand *ops)
8420 if (!maybe_expand_jump_insn (icode, nops, ops))
8421 gcc_unreachable ();
8424 /* Reduce conditional compilation elsewhere. */
8425 #ifndef HAVE_insv
8426 #define HAVE_insv 0
8427 #define CODE_FOR_insv CODE_FOR_nothing
8428 #endif
8429 #ifndef HAVE_extv
8430 #define HAVE_extv 0
8431 #define CODE_FOR_extv CODE_FOR_nothing
8432 #endif
8433 #ifndef HAVE_extzv
8434 #define HAVE_extzv 0
8435 #define CODE_FOR_extzv CODE_FOR_nothing
8436 #endif
8438 /* Enumerates the possible types of structure operand to an
8439 extraction_insn. */
8440 enum extraction_type { ET_unaligned_mem, ET_reg };
8442 /* Check whether insv, extv or extzv pattern ICODE can be used for an
8443 insertion or extraction of type TYPE on a structure of mode MODE.
8444 Return true if so and fill in *INSN accordingly. STRUCT_OP is the
8445 operand number of the structure (the first sign_extract or zero_extract
8446 operand) and FIELD_OP is the operand number of the field (the other
8447 side of the set from the sign_extract or zero_extract). */
8449 static bool
8450 get_traditional_extraction_insn (extraction_insn *insn,
8451 enum extraction_type type,
8452 machine_mode mode,
8453 enum insn_code icode,
8454 int struct_op, int field_op)
8456 const struct insn_data_d *data = &insn_data[icode];
8458 machine_mode struct_mode = data->operand[struct_op].mode;
8459 if (struct_mode == VOIDmode)
8460 struct_mode = word_mode;
8461 if (mode != struct_mode)
8462 return false;
8464 machine_mode field_mode = data->operand[field_op].mode;
8465 if (field_mode == VOIDmode)
8466 field_mode = word_mode;
8468 machine_mode pos_mode = data->operand[struct_op + 2].mode;
8469 if (pos_mode == VOIDmode)
8470 pos_mode = word_mode;
8472 insn->icode = icode;
8473 insn->field_mode = field_mode;
8474 insn->struct_mode = (type == ET_unaligned_mem ? byte_mode : struct_mode);
8475 insn->pos_mode = pos_mode;
8476 return true;
8479 /* Return true if an optab exists to perform an insertion or extraction
8480 of type TYPE in mode MODE. Describe the instruction in *INSN if so.
8482 REG_OPTAB is the optab to use for register structures and
8483 MISALIGN_OPTAB is the optab to use for misaligned memory structures.
8484 POS_OP is the operand number of the bit position. */
8486 static bool
8487 get_optab_extraction_insn (struct extraction_insn *insn,
8488 enum extraction_type type,
8489 machine_mode mode, direct_optab reg_optab,
8490 direct_optab misalign_optab, int pos_op)
8492 direct_optab optab = (type == ET_unaligned_mem ? misalign_optab : reg_optab);
8493 enum insn_code icode = direct_optab_handler (optab, mode);
8494 if (icode == CODE_FOR_nothing)
8495 return false;
8497 const struct insn_data_d *data = &insn_data[icode];
8499 insn->icode = icode;
8500 insn->field_mode = mode;
8501 insn->struct_mode = (type == ET_unaligned_mem ? BLKmode : mode);
8502 insn->pos_mode = data->operand[pos_op].mode;
8503 if (insn->pos_mode == VOIDmode)
8504 insn->pos_mode = word_mode;
8505 return true;
8508 /* Return true if an instruction exists to perform an insertion or
8509 extraction (PATTERN says which) of type TYPE in mode MODE.
8510 Describe the instruction in *INSN if so. */
8512 static bool
8513 get_extraction_insn (extraction_insn *insn,
8514 enum extraction_pattern pattern,
8515 enum extraction_type type,
8516 machine_mode mode)
8518 switch (pattern)
8520 case EP_insv:
8521 if (HAVE_insv
8522 && get_traditional_extraction_insn (insn, type, mode,
8523 CODE_FOR_insv, 0, 3))
8524 return true;
8525 return get_optab_extraction_insn (insn, type, mode, insv_optab,
8526 insvmisalign_optab, 2);
8528 case EP_extv:
8529 if (HAVE_extv
8530 && get_traditional_extraction_insn (insn, type, mode,
8531 CODE_FOR_extv, 1, 0))
8532 return true;
8533 return get_optab_extraction_insn (insn, type, mode, extv_optab,
8534 extvmisalign_optab, 3);
8536 case EP_extzv:
8537 if (HAVE_extzv
8538 && get_traditional_extraction_insn (insn, type, mode,
8539 CODE_FOR_extzv, 1, 0))
8540 return true;
8541 return get_optab_extraction_insn (insn, type, mode, extzv_optab,
8542 extzvmisalign_optab, 3);
8544 default:
8545 gcc_unreachable ();
8549 /* Return true if an instruction exists to access a field of mode
8550 FIELDMODE in a structure that has STRUCT_BITS significant bits.
8551 Describe the "best" such instruction in *INSN if so. PATTERN and
8552 TYPE describe the type of insertion or extraction we want to perform.
8554 For an insertion, the number of significant structure bits includes
8555 all bits of the target. For an extraction, it need only include the
8556 most significant bit of the field. Larger widths are acceptable
8557 in both cases. */
8559 static bool
8560 get_best_extraction_insn (extraction_insn *insn,
8561 enum extraction_pattern pattern,
8562 enum extraction_type type,
8563 unsigned HOST_WIDE_INT struct_bits,
8564 machine_mode field_mode)
8566 machine_mode mode = smallest_mode_for_size (struct_bits, MODE_INT);
8567 while (mode != VOIDmode)
8569 if (get_extraction_insn (insn, pattern, type, mode))
8571 while (mode != VOIDmode
8572 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (field_mode)
8573 && !TRULY_NOOP_TRUNCATION_MODES_P (insn->field_mode,
8574 field_mode))
8576 get_extraction_insn (insn, pattern, type, mode);
8577 mode = GET_MODE_WIDER_MODE (mode);
8579 return true;
8581 mode = GET_MODE_WIDER_MODE (mode);
8583 return false;
8586 /* Return true if an instruction exists to access a field of mode
8587 FIELDMODE in a register structure that has STRUCT_BITS significant bits.
8588 Describe the "best" such instruction in *INSN if so. PATTERN describes
8589 the type of insertion or extraction we want to perform.
8591 For an insertion, the number of significant structure bits includes
8592 all bits of the target. For an extraction, it need only include the
8593 most significant bit of the field. Larger widths are acceptable
8594 in both cases. */
8596 bool
8597 get_best_reg_extraction_insn (extraction_insn *insn,
8598 enum extraction_pattern pattern,
8599 unsigned HOST_WIDE_INT struct_bits,
8600 machine_mode field_mode)
8602 return get_best_extraction_insn (insn, pattern, ET_reg, struct_bits,
8603 field_mode);
8606 /* Return true if an instruction exists to access a field of BITSIZE
8607 bits starting BITNUM bits into a memory structure. Describe the
8608 "best" such instruction in *INSN if so. PATTERN describes the type
8609 of insertion or extraction we want to perform and FIELDMODE is the
8610 natural mode of the extracted field.
8612 The instructions considered here only access bytes that overlap
8613 the bitfield; they do not touch any surrounding bytes. */
8615 bool
8616 get_best_mem_extraction_insn (extraction_insn *insn,
8617 enum extraction_pattern pattern,
8618 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitnum,
8619 machine_mode field_mode)
8621 unsigned HOST_WIDE_INT struct_bits = (bitnum % BITS_PER_UNIT
8622 + bitsize
8623 + BITS_PER_UNIT - 1);
8624 struct_bits -= struct_bits % BITS_PER_UNIT;
8625 return get_best_extraction_insn (insn, pattern, ET_unaligned_mem,
8626 struct_bits, field_mode);
8629 /* Determine whether "1 << x" is relatively cheap in word_mode. */
8631 bool
8632 lshift_cheap_p (bool speed_p)
8634 /* FIXME: This should be made target dependent via this "this_target"
8635 mechanism, similar to e.g. can_copy_init_p in gcse.c. */
8636 static bool init[2] = { false, false };
8637 static bool cheap[2] = { true, true };
8639 /* If the targer has no lshift in word_mode, the operation will most
8640 probably not be cheap. ??? Does GCC even work for such targets? */
8641 if (optab_handler (ashl_optab, word_mode) == CODE_FOR_nothing)
8642 return false;
8644 if (!init[speed_p])
8646 rtx reg = gen_raw_REG (word_mode, 10000);
8647 int cost = set_src_cost (gen_rtx_ASHIFT (word_mode, const1_rtx, reg),
8648 speed_p);
8649 cheap[speed_p] = cost < COSTS_N_INSNS (3);
8650 init[speed_p] = true;
8653 return cheap[speed_p];
8656 #include "gt-optabs.h"