* pt.c (lookup_template_class_1): Splice out abi_tag attribute if
[official-gcc.git] / gcc / optabs.c
blob605615d7458e794995dfd27d7fdf39e37baa910a
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "diagnostic-core.h"
27 /* Include insn-config.h before expr.h so that HAVE_conditional_move
28 is properly defined. */
29 #include "insn-config.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "stor-layout.h"
33 #include "stringpool.h"
34 #include "varasm.h"
35 #include "tm_p.h"
36 #include "flags.h"
37 #include "function.h"
38 #include "except.h"
39 #include "expr.h"
40 #include "optabs.h"
41 #include "libfuncs.h"
42 #include "recog.h"
43 #include "reload.h"
44 #include "ggc.h"
45 #include "basic-block.h"
46 #include "target.h"
48 struct target_optabs default_target_optabs;
49 struct target_libfuncs default_target_libfuncs;
50 struct target_optabs *this_fn_optabs = &default_target_optabs;
51 #if SWITCHABLE_TARGET
52 struct target_optabs *this_target_optabs = &default_target_optabs;
53 struct target_libfuncs *this_target_libfuncs = &default_target_libfuncs;
54 #endif
56 #define libfunc_hash \
57 (this_target_libfuncs->x_libfunc_hash)
59 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
60 enum machine_mode *);
61 static rtx expand_unop_direct (enum machine_mode, optab, rtx, rtx, int);
62 static void emit_libcall_block_1 (rtx_insn *, rtx, rtx, rtx, bool);
64 /* Debug facility for use in GDB. */
65 void debug_optab_libfuncs (void);
67 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
68 #if ENABLE_DECIMAL_BID_FORMAT
69 #define DECIMAL_PREFIX "bid_"
70 #else
71 #define DECIMAL_PREFIX "dpd_"
72 #endif
74 /* Used for libfunc_hash. */
76 static hashval_t
77 hash_libfunc (const void *p)
79 const struct libfunc_entry *const e = (const struct libfunc_entry *) p;
80 return ((e->mode1 + e->mode2 * NUM_MACHINE_MODES) ^ e->op);
83 /* Used for libfunc_hash. */
85 static int
86 eq_libfunc (const void *p, const void *q)
88 const struct libfunc_entry *const e1 = (const struct libfunc_entry *) p;
89 const struct libfunc_entry *const e2 = (const struct libfunc_entry *) q;
90 return e1->op == e2->op && e1->mode1 == e2->mode1 && e1->mode2 == e2->mode2;
93 /* Return libfunc corresponding operation defined by OPTAB converting
94 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
95 if no libfunc is available. */
96 rtx
97 convert_optab_libfunc (convert_optab optab, enum machine_mode mode1,
98 enum machine_mode mode2)
100 struct libfunc_entry e;
101 struct libfunc_entry **slot;
103 /* ??? This ought to be an assert, but not all of the places
104 that we expand optabs know about the optabs that got moved
105 to being direct. */
106 if (!(optab >= FIRST_CONV_OPTAB && optab <= LAST_CONVLIB_OPTAB))
107 return NULL_RTX;
109 e.op = optab;
110 e.mode1 = mode1;
111 e.mode2 = mode2;
112 slot = (struct libfunc_entry **)
113 htab_find_slot (libfunc_hash, &e, NO_INSERT);
114 if (!slot)
116 const struct convert_optab_libcall_d *d
117 = &convlib_def[optab - FIRST_CONV_OPTAB];
119 if (d->libcall_gen == NULL)
120 return NULL;
122 d->libcall_gen (optab, d->libcall_basename, mode1, mode2);
123 slot = (struct libfunc_entry **)
124 htab_find_slot (libfunc_hash, &e, NO_INSERT);
125 if (!slot)
126 return NULL;
128 return (*slot)->libfunc;
131 /* Return libfunc corresponding operation defined by OPTAB in MODE.
132 Trigger lazy initialization if needed, return NULL if no libfunc is
133 available. */
135 optab_libfunc (optab optab, enum machine_mode mode)
137 struct libfunc_entry e;
138 struct libfunc_entry **slot;
140 /* ??? This ought to be an assert, but not all of the places
141 that we expand optabs know about the optabs that got moved
142 to being direct. */
143 if (!(optab >= FIRST_NORM_OPTAB && optab <= LAST_NORMLIB_OPTAB))
144 return NULL_RTX;
146 e.op = optab;
147 e.mode1 = mode;
148 e.mode2 = VOIDmode;
149 slot = (struct libfunc_entry **)
150 htab_find_slot (libfunc_hash, &e, NO_INSERT);
151 if (!slot)
153 const struct optab_libcall_d *d
154 = &normlib_def[optab - FIRST_NORM_OPTAB];
156 if (d->libcall_gen == NULL)
157 return NULL;
159 d->libcall_gen (optab, d->libcall_basename, d->libcall_suffix, mode);
160 slot = (struct libfunc_entry **)
161 htab_find_slot (libfunc_hash, &e, NO_INSERT);
162 if (!slot)
163 return NULL;
165 return (*slot)->libfunc;
169 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
170 the result of operation CODE applied to OP0 (and OP1 if it is a binary
171 operation).
173 If the last insn does not set TARGET, don't do anything, but return 1.
175 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
176 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
177 try again, ensuring that TARGET is not one of the operands. */
179 static int
180 add_equal_note (rtx_insn *insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
182 rtx_insn *last_insn;
183 rtx set;
184 rtx note;
186 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
188 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
189 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
190 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
191 && GET_RTX_CLASS (code) != RTX_COMPARE
192 && GET_RTX_CLASS (code) != RTX_UNARY)
193 return 1;
195 if (GET_CODE (target) == ZERO_EXTRACT)
196 return 1;
198 for (last_insn = insns;
199 NEXT_INSN (last_insn) != NULL_RTX;
200 last_insn = NEXT_INSN (last_insn))
203 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
204 a value changing in the insn, so the note would be invalid for CSE. */
205 if (reg_overlap_mentioned_p (target, op0)
206 || (op1 && reg_overlap_mentioned_p (target, op1)))
208 if (MEM_P (target)
209 && (rtx_equal_p (target, op0)
210 || (op1 && rtx_equal_p (target, op1))))
212 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
213 over expanding it as temp = MEM op X, MEM = temp. If the target
214 supports MEM = MEM op X instructions, it is sometimes too hard
215 to reconstruct that form later, especially if X is also a memory,
216 and due to multiple occurrences of addresses the address might
217 be forced into register unnecessarily.
218 Note that not emitting the REG_EQUIV note might inhibit
219 CSE in some cases. */
220 set = single_set (last_insn);
221 if (set
222 && GET_CODE (SET_SRC (set)) == code
223 && MEM_P (SET_DEST (set))
224 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
225 || (op1 && rtx_equal_p (SET_DEST (set),
226 XEXP (SET_SRC (set), 1)))))
227 return 1;
229 return 0;
232 set = set_for_reg_notes (last_insn);
233 if (set == NULL_RTX)
234 return 1;
236 if (! rtx_equal_p (SET_DEST (set), target)
237 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
238 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
239 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
240 return 1;
242 if (GET_RTX_CLASS (code) == RTX_UNARY)
243 switch (code)
245 case FFS:
246 case CLZ:
247 case CTZ:
248 case CLRSB:
249 case POPCOUNT:
250 case PARITY:
251 case BSWAP:
252 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
254 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
255 if (GET_MODE_SIZE (GET_MODE (op0))
256 > GET_MODE_SIZE (GET_MODE (target)))
257 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
258 note, GET_MODE (op0));
259 else
260 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
261 note, GET_MODE (op0));
262 break;
264 /* FALLTHRU */
265 default:
266 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
267 break;
269 else
270 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
272 set_unique_reg_note (last_insn, REG_EQUAL, note);
274 return 1;
277 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
278 for a widening operation would be. In most cases this would be OP0, but if
279 that's a constant it'll be VOIDmode, which isn't useful. */
281 static enum machine_mode
282 widened_mode (enum machine_mode to_mode, rtx op0, rtx op1)
284 enum machine_mode m0 = GET_MODE (op0);
285 enum machine_mode m1 = GET_MODE (op1);
286 enum machine_mode result;
288 if (m0 == VOIDmode && m1 == VOIDmode)
289 return to_mode;
290 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
291 result = m1;
292 else
293 result = m0;
295 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
296 return to_mode;
298 return result;
301 /* Like optab_handler, but for widening_operations that have a
302 TO_MODE and a FROM_MODE. */
304 enum insn_code
305 widening_optab_handler (optab op, enum machine_mode to_mode,
306 enum machine_mode from_mode)
308 unsigned scode = (op << 16) | to_mode;
309 if (to_mode != from_mode && from_mode != VOIDmode)
311 /* ??? Why does find_widening_optab_handler_and_mode attempt to
312 widen things that can't be widened? E.g. add_optab... */
313 if (op > LAST_CONV_OPTAB)
314 return CODE_FOR_nothing;
315 scode |= from_mode << 8;
317 return raw_optab_handler (scode);
320 /* Find a widening optab even if it doesn't widen as much as we want.
321 E.g. if from_mode is HImode, and to_mode is DImode, and there is no
322 direct HI->SI insn, then return SI->DI, if that exists.
323 If PERMIT_NON_WIDENING is non-zero then this can be used with
324 non-widening optabs also. */
326 enum insn_code
327 find_widening_optab_handler_and_mode (optab op, enum machine_mode to_mode,
328 enum machine_mode from_mode,
329 int permit_non_widening,
330 enum machine_mode *found_mode)
332 for (; (permit_non_widening || from_mode != to_mode)
333 && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
334 && from_mode != VOIDmode;
335 from_mode = GET_MODE_WIDER_MODE (from_mode))
337 enum insn_code handler = widening_optab_handler (op, to_mode,
338 from_mode);
340 if (handler != CODE_FOR_nothing)
342 if (found_mode)
343 *found_mode = from_mode;
344 return handler;
348 return CODE_FOR_nothing;
351 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
352 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
353 not actually do a sign-extend or zero-extend, but can leave the
354 higher-order bits of the result rtx undefined, for example, in the case
355 of logical operations, but not right shifts. */
357 static rtx
358 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
359 int unsignedp, int no_extend)
361 rtx result;
363 /* If we don't have to extend and this is a constant, return it. */
364 if (no_extend && GET_MODE (op) == VOIDmode)
365 return op;
367 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
368 extend since it will be more efficient to do so unless the signedness of
369 a promoted object differs from our extension. */
370 if (! no_extend
371 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
372 && SUBREG_CHECK_PROMOTED_SIGN (op, unsignedp)))
373 return convert_modes (mode, oldmode, op, unsignedp);
375 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
376 SUBREG. */
377 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
378 return gen_lowpart (mode, force_reg (GET_MODE (op), op));
380 /* Otherwise, get an object of MODE, clobber it, and set the low-order
381 part to OP. */
383 result = gen_reg_rtx (mode);
384 emit_clobber (result);
385 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
386 return result;
389 /* Return the optab used for computing the operation given by the tree code,
390 CODE and the tree EXP. This function is not always usable (for example, it
391 cannot give complete results for multiplication or division) but probably
392 ought to be relied on more widely throughout the expander. */
393 optab
394 optab_for_tree_code (enum tree_code code, const_tree type,
395 enum optab_subtype subtype)
397 bool trapv;
398 switch (code)
400 case BIT_AND_EXPR:
401 return and_optab;
403 case BIT_IOR_EXPR:
404 return ior_optab;
406 case BIT_NOT_EXPR:
407 return one_cmpl_optab;
409 case BIT_XOR_EXPR:
410 return xor_optab;
412 case MULT_HIGHPART_EXPR:
413 return TYPE_UNSIGNED (type) ? umul_highpart_optab : smul_highpart_optab;
415 case TRUNC_MOD_EXPR:
416 case CEIL_MOD_EXPR:
417 case FLOOR_MOD_EXPR:
418 case ROUND_MOD_EXPR:
419 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
421 case RDIV_EXPR:
422 case TRUNC_DIV_EXPR:
423 case CEIL_DIV_EXPR:
424 case FLOOR_DIV_EXPR:
425 case ROUND_DIV_EXPR:
426 case EXACT_DIV_EXPR:
427 if (TYPE_SATURATING (type))
428 return TYPE_UNSIGNED (type) ? usdiv_optab : ssdiv_optab;
429 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
431 case LSHIFT_EXPR:
432 if (TREE_CODE (type) == VECTOR_TYPE)
434 if (subtype == optab_vector)
435 return TYPE_SATURATING (type) ? unknown_optab : vashl_optab;
437 gcc_assert (subtype == optab_scalar);
439 if (TYPE_SATURATING (type))
440 return TYPE_UNSIGNED (type) ? usashl_optab : ssashl_optab;
441 return ashl_optab;
443 case RSHIFT_EXPR:
444 if (TREE_CODE (type) == VECTOR_TYPE)
446 if (subtype == optab_vector)
447 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
449 gcc_assert (subtype == optab_scalar);
451 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
453 case LROTATE_EXPR:
454 if (TREE_CODE (type) == VECTOR_TYPE)
456 if (subtype == optab_vector)
457 return vrotl_optab;
459 gcc_assert (subtype == optab_scalar);
461 return rotl_optab;
463 case RROTATE_EXPR:
464 if (TREE_CODE (type) == VECTOR_TYPE)
466 if (subtype == optab_vector)
467 return vrotr_optab;
469 gcc_assert (subtype == optab_scalar);
471 return rotr_optab;
473 case MAX_EXPR:
474 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
476 case MIN_EXPR:
477 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
479 case REALIGN_LOAD_EXPR:
480 return vec_realign_load_optab;
482 case WIDEN_SUM_EXPR:
483 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
485 case DOT_PROD_EXPR:
486 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
488 case SAD_EXPR:
489 return TYPE_UNSIGNED (type) ? usad_optab : ssad_optab;
491 case WIDEN_MULT_PLUS_EXPR:
492 return (TYPE_UNSIGNED (type)
493 ? (TYPE_SATURATING (type)
494 ? usmadd_widen_optab : umadd_widen_optab)
495 : (TYPE_SATURATING (type)
496 ? ssmadd_widen_optab : smadd_widen_optab));
498 case WIDEN_MULT_MINUS_EXPR:
499 return (TYPE_UNSIGNED (type)
500 ? (TYPE_SATURATING (type)
501 ? usmsub_widen_optab : umsub_widen_optab)
502 : (TYPE_SATURATING (type)
503 ? ssmsub_widen_optab : smsub_widen_optab));
505 case FMA_EXPR:
506 return fma_optab;
508 case REDUC_MAX_EXPR:
509 return TYPE_UNSIGNED (type) ? reduc_umax_optab : reduc_smax_optab;
511 case REDUC_MIN_EXPR:
512 return TYPE_UNSIGNED (type) ? reduc_umin_optab : reduc_smin_optab;
514 case REDUC_PLUS_EXPR:
515 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
517 case VEC_LSHIFT_EXPR:
518 return vec_shl_optab;
520 case VEC_RSHIFT_EXPR:
521 return vec_shr_optab;
523 case VEC_WIDEN_MULT_HI_EXPR:
524 return TYPE_UNSIGNED (type) ?
525 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
527 case VEC_WIDEN_MULT_LO_EXPR:
528 return TYPE_UNSIGNED (type) ?
529 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
531 case VEC_WIDEN_MULT_EVEN_EXPR:
532 return TYPE_UNSIGNED (type) ?
533 vec_widen_umult_even_optab : vec_widen_smult_even_optab;
535 case VEC_WIDEN_MULT_ODD_EXPR:
536 return TYPE_UNSIGNED (type) ?
537 vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
539 case VEC_WIDEN_LSHIFT_HI_EXPR:
540 return TYPE_UNSIGNED (type) ?
541 vec_widen_ushiftl_hi_optab : vec_widen_sshiftl_hi_optab;
543 case VEC_WIDEN_LSHIFT_LO_EXPR:
544 return TYPE_UNSIGNED (type) ?
545 vec_widen_ushiftl_lo_optab : vec_widen_sshiftl_lo_optab;
547 case VEC_UNPACK_HI_EXPR:
548 return TYPE_UNSIGNED (type) ?
549 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
551 case VEC_UNPACK_LO_EXPR:
552 return TYPE_UNSIGNED (type) ?
553 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
555 case VEC_UNPACK_FLOAT_HI_EXPR:
556 /* The signedness is determined from input operand. */
557 return TYPE_UNSIGNED (type) ?
558 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
560 case VEC_UNPACK_FLOAT_LO_EXPR:
561 /* The signedness is determined from input operand. */
562 return TYPE_UNSIGNED (type) ?
563 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
565 case VEC_PACK_TRUNC_EXPR:
566 return vec_pack_trunc_optab;
568 case VEC_PACK_SAT_EXPR:
569 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
571 case VEC_PACK_FIX_TRUNC_EXPR:
572 /* The signedness is determined from output operand. */
573 return TYPE_UNSIGNED (type) ?
574 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
576 default:
577 break;
580 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
581 switch (code)
583 case POINTER_PLUS_EXPR:
584 case PLUS_EXPR:
585 if (TYPE_SATURATING (type))
586 return TYPE_UNSIGNED (type) ? usadd_optab : ssadd_optab;
587 return trapv ? addv_optab : add_optab;
589 case MINUS_EXPR:
590 if (TYPE_SATURATING (type))
591 return TYPE_UNSIGNED (type) ? ussub_optab : sssub_optab;
592 return trapv ? subv_optab : sub_optab;
594 case MULT_EXPR:
595 if (TYPE_SATURATING (type))
596 return TYPE_UNSIGNED (type) ? usmul_optab : ssmul_optab;
597 return trapv ? smulv_optab : smul_optab;
599 case NEGATE_EXPR:
600 if (TYPE_SATURATING (type))
601 return TYPE_UNSIGNED (type) ? usneg_optab : ssneg_optab;
602 return trapv ? negv_optab : neg_optab;
604 case ABS_EXPR:
605 return trapv ? absv_optab : abs_optab;
607 default:
608 return unknown_optab;
613 /* Expand vector widening operations.
615 There are two different classes of operations handled here:
616 1) Operations whose result is wider than all the arguments to the operation.
617 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
618 In this case OP0 and optionally OP1 would be initialized,
619 but WIDE_OP wouldn't (not relevant for this case).
620 2) Operations whose result is of the same size as the last argument to the
621 operation, but wider than all the other arguments to the operation.
622 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
623 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
625 E.g, when called to expand the following operations, this is how
626 the arguments will be initialized:
627 nops OP0 OP1 WIDE_OP
628 widening-sum 2 oprnd0 - oprnd1
629 widening-dot-product 3 oprnd0 oprnd1 oprnd2
630 widening-mult 2 oprnd0 oprnd1 -
631 type-promotion (vec-unpack) 1 oprnd0 - - */
634 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
635 rtx target, int unsignedp)
637 struct expand_operand eops[4];
638 tree oprnd0, oprnd1, oprnd2;
639 enum machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
640 optab widen_pattern_optab;
641 enum insn_code icode;
642 int nops = TREE_CODE_LENGTH (ops->code);
643 int op;
645 oprnd0 = ops->op0;
646 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
647 widen_pattern_optab =
648 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
649 if (ops->code == WIDEN_MULT_PLUS_EXPR
650 || ops->code == WIDEN_MULT_MINUS_EXPR)
651 icode = find_widening_optab_handler (widen_pattern_optab,
652 TYPE_MODE (TREE_TYPE (ops->op2)),
653 tmode0, 0);
654 else
655 icode = optab_handler (widen_pattern_optab, tmode0);
656 gcc_assert (icode != CODE_FOR_nothing);
658 if (nops >= 2)
660 oprnd1 = ops->op1;
661 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
664 /* The last operand is of a wider mode than the rest of the operands. */
665 if (nops == 2)
666 wmode = tmode1;
667 else if (nops == 3)
669 gcc_assert (tmode1 == tmode0);
670 gcc_assert (op1);
671 oprnd2 = ops->op2;
672 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
675 op = 0;
676 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
677 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
678 if (op1)
679 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
680 if (wide_op)
681 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
682 expand_insn (icode, op, eops);
683 return eops[0].value;
686 /* Generate code to perform an operation specified by TERNARY_OPTAB
687 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
689 UNSIGNEDP is for the case where we have to widen the operands
690 to perform the operation. It says to use zero-extension.
692 If TARGET is nonzero, the value
693 is generated there, if it is convenient to do so.
694 In all cases an rtx is returned for the locus of the value;
695 this may or may not be TARGET. */
698 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
699 rtx op1, rtx op2, rtx target, int unsignedp)
701 struct expand_operand ops[4];
702 enum insn_code icode = optab_handler (ternary_optab, mode);
704 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
706 create_output_operand (&ops[0], target, mode);
707 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
708 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
709 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
710 expand_insn (icode, 4, ops);
711 return ops[0].value;
715 /* Like expand_binop, but return a constant rtx if the result can be
716 calculated at compile time. The arguments and return value are
717 otherwise the same as for expand_binop. */
720 simplify_expand_binop (enum machine_mode mode, optab binoptab,
721 rtx op0, rtx op1, rtx target, int unsignedp,
722 enum optab_methods methods)
724 if (CONSTANT_P (op0) && CONSTANT_P (op1))
726 rtx x = simplify_binary_operation (optab_to_code (binoptab),
727 mode, op0, op1);
728 if (x)
729 return x;
732 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
735 /* Like simplify_expand_binop, but always put the result in TARGET.
736 Return true if the expansion succeeded. */
738 bool
739 force_expand_binop (enum machine_mode mode, optab binoptab,
740 rtx op0, rtx op1, rtx target, int unsignedp,
741 enum optab_methods methods)
743 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
744 target, unsignedp, methods);
745 if (x == 0)
746 return false;
747 if (x != target)
748 emit_move_insn (target, x);
749 return true;
752 /* Generate insns for VEC_LSHIFT_EXPR, VEC_RSHIFT_EXPR. */
755 expand_vec_shift_expr (sepops ops, rtx target)
757 struct expand_operand eops[3];
758 enum insn_code icode;
759 rtx rtx_op1, rtx_op2;
760 enum machine_mode mode = TYPE_MODE (ops->type);
761 tree vec_oprnd = ops->op0;
762 tree shift_oprnd = ops->op1;
763 optab shift_optab;
765 switch (ops->code)
767 case VEC_RSHIFT_EXPR:
768 shift_optab = vec_shr_optab;
769 break;
770 case VEC_LSHIFT_EXPR:
771 shift_optab = vec_shl_optab;
772 break;
773 default:
774 gcc_unreachable ();
777 icode = optab_handler (shift_optab, mode);
778 gcc_assert (icode != CODE_FOR_nothing);
780 rtx_op1 = expand_normal (vec_oprnd);
781 rtx_op2 = expand_normal (shift_oprnd);
783 create_output_operand (&eops[0], target, mode);
784 create_input_operand (&eops[1], rtx_op1, GET_MODE (rtx_op1));
785 create_convert_operand_from_type (&eops[2], rtx_op2, TREE_TYPE (shift_oprnd));
786 expand_insn (icode, 3, eops);
788 return eops[0].value;
791 /* Create a new vector value in VMODE with all elements set to OP. The
792 mode of OP must be the element mode of VMODE. If OP is a constant,
793 then the return value will be a constant. */
795 static rtx
796 expand_vector_broadcast (enum machine_mode vmode, rtx op)
798 enum insn_code icode;
799 rtvec vec;
800 rtx ret;
801 int i, n;
803 gcc_checking_assert (VECTOR_MODE_P (vmode));
805 n = GET_MODE_NUNITS (vmode);
806 vec = rtvec_alloc (n);
807 for (i = 0; i < n; ++i)
808 RTVEC_ELT (vec, i) = op;
810 if (CONSTANT_P (op))
811 return gen_rtx_CONST_VECTOR (vmode, vec);
813 /* ??? If the target doesn't have a vec_init, then we have no easy way
814 of performing this operation. Most of this sort of generic support
815 is hidden away in the vector lowering support in gimple. */
816 icode = optab_handler (vec_init_optab, vmode);
817 if (icode == CODE_FOR_nothing)
818 return NULL;
820 ret = gen_reg_rtx (vmode);
821 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
823 return ret;
826 /* This subroutine of expand_doubleword_shift handles the cases in which
827 the effective shift value is >= BITS_PER_WORD. The arguments and return
828 value are the same as for the parent routine, except that SUPERWORD_OP1
829 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
830 INTO_TARGET may be null if the caller has decided to calculate it. */
832 static bool
833 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
834 rtx outof_target, rtx into_target,
835 int unsignedp, enum optab_methods methods)
837 if (into_target != 0)
838 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
839 into_target, unsignedp, methods))
840 return false;
842 if (outof_target != 0)
844 /* For a signed right shift, we must fill OUTOF_TARGET with copies
845 of the sign bit, otherwise we must fill it with zeros. */
846 if (binoptab != ashr_optab)
847 emit_move_insn (outof_target, CONST0_RTX (word_mode));
848 else
849 if (!force_expand_binop (word_mode, binoptab,
850 outof_input, GEN_INT (BITS_PER_WORD - 1),
851 outof_target, unsignedp, methods))
852 return false;
854 return true;
857 /* This subroutine of expand_doubleword_shift handles the cases in which
858 the effective shift value is < BITS_PER_WORD. The arguments and return
859 value are the same as for the parent routine. */
861 static bool
862 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
863 rtx outof_input, rtx into_input, rtx op1,
864 rtx outof_target, rtx into_target,
865 int unsignedp, enum optab_methods methods,
866 unsigned HOST_WIDE_INT shift_mask)
868 optab reverse_unsigned_shift, unsigned_shift;
869 rtx tmp, carries;
871 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
872 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
874 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
875 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
876 the opposite direction to BINOPTAB. */
877 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
879 carries = outof_input;
880 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD,
881 op1_mode), op1_mode);
882 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
883 0, true, methods);
885 else
887 /* We must avoid shifting by BITS_PER_WORD bits since that is either
888 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
889 has unknown behavior. Do a single shift first, then shift by the
890 remainder. It's OK to use ~OP1 as the remainder if shift counts
891 are truncated to the mode size. */
892 carries = expand_binop (word_mode, reverse_unsigned_shift,
893 outof_input, const1_rtx, 0, unsignedp, methods);
894 if (shift_mask == BITS_PER_WORD - 1)
896 tmp = immed_wide_int_const
897 (wi::minus_one (GET_MODE_PRECISION (op1_mode)), op1_mode);
898 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
899 0, true, methods);
901 else
903 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD - 1,
904 op1_mode), op1_mode);
905 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
906 0, true, methods);
909 if (tmp == 0 || carries == 0)
910 return false;
911 carries = expand_binop (word_mode, reverse_unsigned_shift,
912 carries, tmp, 0, unsignedp, methods);
913 if (carries == 0)
914 return false;
916 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
917 so the result can go directly into INTO_TARGET if convenient. */
918 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
919 into_target, unsignedp, methods);
920 if (tmp == 0)
921 return false;
923 /* Now OR in the bits carried over from OUTOF_INPUT. */
924 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
925 into_target, unsignedp, methods))
926 return false;
928 /* Use a standard word_mode shift for the out-of half. */
929 if (outof_target != 0)
930 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
931 outof_target, unsignedp, methods))
932 return false;
934 return true;
938 #ifdef HAVE_conditional_move
939 /* Try implementing expand_doubleword_shift using conditional moves.
940 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
941 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
942 are the shift counts to use in the former and latter case. All other
943 arguments are the same as the parent routine. */
945 static bool
946 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
947 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
948 rtx outof_input, rtx into_input,
949 rtx subword_op1, rtx superword_op1,
950 rtx outof_target, rtx into_target,
951 int unsignedp, enum optab_methods methods,
952 unsigned HOST_WIDE_INT shift_mask)
954 rtx outof_superword, into_superword;
956 /* Put the superword version of the output into OUTOF_SUPERWORD and
957 INTO_SUPERWORD. */
958 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
959 if (outof_target != 0 && subword_op1 == superword_op1)
961 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
962 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
963 into_superword = outof_target;
964 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
965 outof_superword, 0, unsignedp, methods))
966 return false;
968 else
970 into_superword = gen_reg_rtx (word_mode);
971 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
972 outof_superword, into_superword,
973 unsignedp, methods))
974 return false;
977 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
978 if (!expand_subword_shift (op1_mode, binoptab,
979 outof_input, into_input, subword_op1,
980 outof_target, into_target,
981 unsignedp, methods, shift_mask))
982 return false;
984 /* Select between them. Do the INTO half first because INTO_SUPERWORD
985 might be the current value of OUTOF_TARGET. */
986 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
987 into_target, into_superword, word_mode, false))
988 return false;
990 if (outof_target != 0)
991 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
992 outof_target, outof_superword,
993 word_mode, false))
994 return false;
996 return true;
998 #endif
1000 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
1001 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
1002 input operand; the shift moves bits in the direction OUTOF_INPUT->
1003 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
1004 of the target. OP1 is the shift count and OP1_MODE is its mode.
1005 If OP1 is constant, it will have been truncated as appropriate
1006 and is known to be nonzero.
1008 If SHIFT_MASK is zero, the result of word shifts is undefined when the
1009 shift count is outside the range [0, BITS_PER_WORD). This routine must
1010 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
1012 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
1013 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
1014 fill with zeros or sign bits as appropriate.
1016 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
1017 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
1018 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
1019 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
1020 are undefined.
1022 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
1023 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
1024 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
1025 function wants to calculate it itself.
1027 Return true if the shift could be successfully synthesized. */
1029 static bool
1030 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
1031 rtx outof_input, rtx into_input, rtx op1,
1032 rtx outof_target, rtx into_target,
1033 int unsignedp, enum optab_methods methods,
1034 unsigned HOST_WIDE_INT shift_mask)
1036 rtx superword_op1, tmp, cmp1, cmp2;
1037 enum rtx_code cmp_code;
1039 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1040 fill the result with sign or zero bits as appropriate. If so, the value
1041 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1042 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1043 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1045 This isn't worthwhile for constant shifts since the optimizers will
1046 cope better with in-range shift counts. */
1047 if (shift_mask >= BITS_PER_WORD
1048 && outof_target != 0
1049 && !CONSTANT_P (op1))
1051 if (!expand_doubleword_shift (op1_mode, binoptab,
1052 outof_input, into_input, op1,
1053 0, into_target,
1054 unsignedp, methods, shift_mask))
1055 return false;
1056 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1057 outof_target, unsignedp, methods))
1058 return false;
1059 return true;
1062 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1063 is true when the effective shift value is less than BITS_PER_WORD.
1064 Set SUPERWORD_OP1 to the shift count that should be used to shift
1065 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1066 tmp = immed_wide_int_const (wi::shwi (BITS_PER_WORD, op1_mode), op1_mode);
1067 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1069 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1070 is a subword shift count. */
1071 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1072 0, true, methods);
1073 cmp2 = CONST0_RTX (op1_mode);
1074 cmp_code = EQ;
1075 superword_op1 = op1;
1077 else
1079 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1080 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1081 0, true, methods);
1082 cmp2 = CONST0_RTX (op1_mode);
1083 cmp_code = LT;
1084 superword_op1 = cmp1;
1086 if (cmp1 == 0)
1087 return false;
1089 /* If we can compute the condition at compile time, pick the
1090 appropriate subroutine. */
1091 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1092 if (tmp != 0 && CONST_INT_P (tmp))
1094 if (tmp == const0_rtx)
1095 return expand_superword_shift (binoptab, outof_input, superword_op1,
1096 outof_target, into_target,
1097 unsignedp, methods);
1098 else
1099 return expand_subword_shift (op1_mode, binoptab,
1100 outof_input, into_input, op1,
1101 outof_target, into_target,
1102 unsignedp, methods, shift_mask);
1105 #ifdef HAVE_conditional_move
1106 /* Try using conditional moves to generate straight-line code. */
1108 rtx_insn *start = get_last_insn ();
1109 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1110 cmp_code, cmp1, cmp2,
1111 outof_input, into_input,
1112 op1, superword_op1,
1113 outof_target, into_target,
1114 unsignedp, methods, shift_mask))
1115 return true;
1116 delete_insns_since (start);
1118 #endif
1120 /* As a last resort, use branches to select the correct alternative. */
1121 rtx_code_label *subword_label = gen_label_rtx ();
1122 rtx_code_label *done_label = gen_label_rtx ();
1124 NO_DEFER_POP;
1125 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1126 0, 0, subword_label, -1);
1127 OK_DEFER_POP;
1129 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1130 outof_target, into_target,
1131 unsignedp, methods))
1132 return false;
1134 emit_jump_insn (gen_jump (done_label));
1135 emit_barrier ();
1136 emit_label (subword_label);
1138 if (!expand_subword_shift (op1_mode, binoptab,
1139 outof_input, into_input, op1,
1140 outof_target, into_target,
1141 unsignedp, methods, shift_mask))
1142 return false;
1144 emit_label (done_label);
1145 return true;
1148 /* Subroutine of expand_binop. Perform a double word multiplication of
1149 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1150 as the target's word_mode. This function return NULL_RTX if anything
1151 goes wrong, in which case it may have already emitted instructions
1152 which need to be deleted.
1154 If we want to multiply two two-word values and have normal and widening
1155 multiplies of single-word values, we can do this with three smaller
1156 multiplications.
1158 The multiplication proceeds as follows:
1159 _______________________
1160 [__op0_high_|__op0_low__]
1161 _______________________
1162 * [__op1_high_|__op1_low__]
1163 _______________________________________________
1164 _______________________
1165 (1) [__op0_low__*__op1_low__]
1166 _______________________
1167 (2a) [__op0_low__*__op1_high_]
1168 _______________________
1169 (2b) [__op0_high_*__op1_low__]
1170 _______________________
1171 (3) [__op0_high_*__op1_high_]
1174 This gives a 4-word result. Since we are only interested in the
1175 lower 2 words, partial result (3) and the upper words of (2a) and
1176 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1177 calculated using non-widening multiplication.
1179 (1), however, needs to be calculated with an unsigned widening
1180 multiplication. If this operation is not directly supported we
1181 try using a signed widening multiplication and adjust the result.
1182 This adjustment works as follows:
1184 If both operands are positive then no adjustment is needed.
1186 If the operands have different signs, for example op0_low < 0 and
1187 op1_low >= 0, the instruction treats the most significant bit of
1188 op0_low as a sign bit instead of a bit with significance
1189 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1190 with 2**BITS_PER_WORD - op0_low, and two's complements the
1191 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1192 the result.
1194 Similarly, if both operands are negative, we need to add
1195 (op0_low + op1_low) * 2**BITS_PER_WORD.
1197 We use a trick to adjust quickly. We logically shift op0_low right
1198 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1199 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1200 logical shift exists, we do an arithmetic right shift and subtract
1201 the 0 or -1. */
1203 static rtx
1204 expand_doubleword_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
1205 bool umulp, enum optab_methods methods)
1207 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1208 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1209 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1210 rtx product, adjust, product_high, temp;
1212 rtx op0_high = operand_subword_force (op0, high, mode);
1213 rtx op0_low = operand_subword_force (op0, low, mode);
1214 rtx op1_high = operand_subword_force (op1, high, mode);
1215 rtx op1_low = operand_subword_force (op1, low, mode);
1217 /* If we're using an unsigned multiply to directly compute the product
1218 of the low-order words of the operands and perform any required
1219 adjustments of the operands, we begin by trying two more multiplications
1220 and then computing the appropriate sum.
1222 We have checked above that the required addition is provided.
1223 Full-word addition will normally always succeed, especially if
1224 it is provided at all, so we don't worry about its failure. The
1225 multiplication may well fail, however, so we do handle that. */
1227 if (!umulp)
1229 /* ??? This could be done with emit_store_flag where available. */
1230 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1231 NULL_RTX, 1, methods);
1232 if (temp)
1233 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1234 NULL_RTX, 0, OPTAB_DIRECT);
1235 else
1237 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1238 NULL_RTX, 0, methods);
1239 if (!temp)
1240 return NULL_RTX;
1241 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1242 NULL_RTX, 0, OPTAB_DIRECT);
1245 if (!op0_high)
1246 return NULL_RTX;
1249 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1250 NULL_RTX, 0, OPTAB_DIRECT);
1251 if (!adjust)
1252 return NULL_RTX;
1254 /* OP0_HIGH should now be dead. */
1256 if (!umulp)
1258 /* ??? This could be done with emit_store_flag where available. */
1259 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1260 NULL_RTX, 1, methods);
1261 if (temp)
1262 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1263 NULL_RTX, 0, OPTAB_DIRECT);
1264 else
1266 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1267 NULL_RTX, 0, methods);
1268 if (!temp)
1269 return NULL_RTX;
1270 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1271 NULL_RTX, 0, OPTAB_DIRECT);
1274 if (!op1_high)
1275 return NULL_RTX;
1278 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1279 NULL_RTX, 0, OPTAB_DIRECT);
1280 if (!temp)
1281 return NULL_RTX;
1283 /* OP1_HIGH should now be dead. */
1285 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1286 NULL_RTX, 0, OPTAB_DIRECT);
1288 if (target && !REG_P (target))
1289 target = NULL_RTX;
1291 if (umulp)
1292 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1293 target, 1, OPTAB_DIRECT);
1294 else
1295 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1296 target, 1, OPTAB_DIRECT);
1298 if (!product)
1299 return NULL_RTX;
1301 product_high = operand_subword (product, high, 1, mode);
1302 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1303 NULL_RTX, 0, OPTAB_DIRECT);
1304 emit_move_insn (product_high, adjust);
1305 return product;
1308 /* Wrapper around expand_binop which takes an rtx code to specify
1309 the operation to perform, not an optab pointer. All other
1310 arguments are the same. */
1312 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
1313 rtx op1, rtx target, int unsignedp,
1314 enum optab_methods methods)
1316 optab binop = code_to_optab (code);
1317 gcc_assert (binop);
1319 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1322 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1323 binop. Order them according to commutative_operand_precedence and, if
1324 possible, try to put TARGET or a pseudo first. */
1325 static bool
1326 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1328 int op0_prec = commutative_operand_precedence (op0);
1329 int op1_prec = commutative_operand_precedence (op1);
1331 if (op0_prec < op1_prec)
1332 return true;
1334 if (op0_prec > op1_prec)
1335 return false;
1337 /* With equal precedence, both orders are ok, but it is better if the
1338 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1339 if (target == 0 || REG_P (target))
1340 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1341 else
1342 return rtx_equal_p (op1, target);
1345 /* Return true if BINOPTAB implements a shift operation. */
1347 static bool
1348 shift_optab_p (optab binoptab)
1350 switch (optab_to_code (binoptab))
1352 case ASHIFT:
1353 case SS_ASHIFT:
1354 case US_ASHIFT:
1355 case ASHIFTRT:
1356 case LSHIFTRT:
1357 case ROTATE:
1358 case ROTATERT:
1359 return true;
1361 default:
1362 return false;
1366 /* Return true if BINOPTAB implements a commutative binary operation. */
1368 static bool
1369 commutative_optab_p (optab binoptab)
1371 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1372 || binoptab == smul_widen_optab
1373 || binoptab == umul_widen_optab
1374 || binoptab == smul_highpart_optab
1375 || binoptab == umul_highpart_optab);
1378 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1379 optimizing, and if the operand is a constant that costs more than
1380 1 instruction, force the constant into a register and return that
1381 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1383 static rtx
1384 avoid_expensive_constant (enum machine_mode mode, optab binoptab,
1385 int opn, rtx x, bool unsignedp)
1387 bool speed = optimize_insn_for_speed_p ();
1389 if (mode != VOIDmode
1390 && optimize
1391 && CONSTANT_P (x)
1392 && (rtx_cost (x, optab_to_code (binoptab), opn, speed)
1393 > set_src_cost (x, speed)))
1395 if (CONST_INT_P (x))
1397 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1398 if (intval != INTVAL (x))
1399 x = GEN_INT (intval);
1401 else
1402 x = convert_modes (mode, VOIDmode, x, unsignedp);
1403 x = force_reg (mode, x);
1405 return x;
1408 /* Helper function for expand_binop: handle the case where there
1409 is an insn that directly implements the indicated operation.
1410 Returns null if this is not possible. */
1411 static rtx
1412 expand_binop_directly (enum machine_mode mode, optab binoptab,
1413 rtx op0, rtx op1,
1414 rtx target, int unsignedp, enum optab_methods methods,
1415 rtx_insn *last)
1417 enum machine_mode from_mode = widened_mode (mode, op0, op1);
1418 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1419 from_mode, 1);
1420 enum machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1421 enum machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1422 enum machine_mode mode0, mode1, tmp_mode;
1423 struct expand_operand ops[3];
1424 bool commutative_p;
1425 rtx pat;
1426 rtx xop0 = op0, xop1 = op1;
1427 rtx swap;
1429 /* If it is a commutative operator and the modes would match
1430 if we would swap the operands, we can save the conversions. */
1431 commutative_p = commutative_optab_p (binoptab);
1432 if (commutative_p
1433 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1434 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1436 swap = xop0;
1437 xop0 = xop1;
1438 xop1 = swap;
1441 /* If we are optimizing, force expensive constants into a register. */
1442 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1443 if (!shift_optab_p (binoptab))
1444 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1446 /* In case the insn wants input operands in modes different from
1447 those of the actual operands, convert the operands. It would
1448 seem that we don't need to convert CONST_INTs, but we do, so
1449 that they're properly zero-extended, sign-extended or truncated
1450 for their mode. */
1452 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1453 if (xmode0 != VOIDmode && xmode0 != mode0)
1455 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1456 mode0 = xmode0;
1459 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1460 if (xmode1 != VOIDmode && xmode1 != mode1)
1462 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1463 mode1 = xmode1;
1466 /* If operation is commutative,
1467 try to make the first operand a register.
1468 Even better, try to make it the same as the target.
1469 Also try to make the last operand a constant. */
1470 if (commutative_p
1471 && swap_commutative_operands_with_target (target, xop0, xop1))
1473 swap = xop1;
1474 xop1 = xop0;
1475 xop0 = swap;
1478 /* Now, if insn's predicates don't allow our operands, put them into
1479 pseudo regs. */
1481 if (binoptab == vec_pack_trunc_optab
1482 || binoptab == vec_pack_usat_optab
1483 || binoptab == vec_pack_ssat_optab
1484 || binoptab == vec_pack_ufix_trunc_optab
1485 || binoptab == vec_pack_sfix_trunc_optab)
1487 /* The mode of the result is different then the mode of the
1488 arguments. */
1489 tmp_mode = insn_data[(int) icode].operand[0].mode;
1490 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1492 delete_insns_since (last);
1493 return NULL_RTX;
1496 else
1497 tmp_mode = mode;
1499 create_output_operand (&ops[0], target, tmp_mode);
1500 create_input_operand (&ops[1], xop0, mode0);
1501 create_input_operand (&ops[2], xop1, mode1);
1502 pat = maybe_gen_insn (icode, 3, ops);
1503 if (pat)
1505 /* If PAT is composed of more than one insn, try to add an appropriate
1506 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1507 operand, call expand_binop again, this time without a target. */
1508 if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
1509 && ! add_equal_note (as_a <rtx_insn *> (pat), ops[0].value,
1510 optab_to_code (binoptab),
1511 ops[1].value, ops[2].value))
1513 delete_insns_since (last);
1514 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1515 unsignedp, methods);
1518 emit_insn (pat);
1519 return ops[0].value;
1521 delete_insns_since (last);
1522 return NULL_RTX;
1525 /* Generate code to perform an operation specified by BINOPTAB
1526 on operands OP0 and OP1, with result having machine-mode MODE.
1528 UNSIGNEDP is for the case where we have to widen the operands
1529 to perform the operation. It says to use zero-extension.
1531 If TARGET is nonzero, the value
1532 is generated there, if it is convenient to do so.
1533 In all cases an rtx is returned for the locus of the value;
1534 this may or may not be TARGET. */
1537 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
1538 rtx target, int unsignedp, enum optab_methods methods)
1540 enum optab_methods next_methods
1541 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1542 ? OPTAB_WIDEN : methods);
1543 enum mode_class mclass;
1544 enum machine_mode wider_mode;
1545 rtx libfunc;
1546 rtx temp;
1547 rtx_insn *entry_last = get_last_insn ();
1548 rtx_insn *last;
1550 mclass = GET_MODE_CLASS (mode);
1552 /* If subtracting an integer constant, convert this into an addition of
1553 the negated constant. */
1555 if (binoptab == sub_optab && CONST_INT_P (op1))
1557 op1 = negate_rtx (mode, op1);
1558 binoptab = add_optab;
1561 /* Record where to delete back to if we backtrack. */
1562 last = get_last_insn ();
1564 /* If we can do it with a three-operand insn, do so. */
1566 if (methods != OPTAB_MUST_WIDEN
1567 && find_widening_optab_handler (binoptab, mode,
1568 widened_mode (mode, op0, op1), 1)
1569 != CODE_FOR_nothing)
1571 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1572 unsignedp, methods, last);
1573 if (temp)
1574 return temp;
1577 /* If we were trying to rotate, and that didn't work, try rotating
1578 the other direction before falling back to shifts and bitwise-or. */
1579 if (((binoptab == rotl_optab
1580 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1581 || (binoptab == rotr_optab
1582 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1583 && mclass == MODE_INT)
1585 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1586 rtx newop1;
1587 unsigned int bits = GET_MODE_PRECISION (mode);
1589 if (CONST_INT_P (op1))
1590 newop1 = GEN_INT (bits - INTVAL (op1));
1591 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1592 newop1 = negate_rtx (GET_MODE (op1), op1);
1593 else
1594 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1595 gen_int_mode (bits, GET_MODE (op1)), op1,
1596 NULL_RTX, unsignedp, OPTAB_DIRECT);
1598 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1599 target, unsignedp, methods, last);
1600 if (temp)
1601 return temp;
1604 /* If this is a multiply, see if we can do a widening operation that
1605 takes operands of this mode and makes a wider mode. */
1607 if (binoptab == smul_optab
1608 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1609 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1610 : smul_widen_optab),
1611 GET_MODE_2XWIDER_MODE (mode), mode)
1612 != CODE_FOR_nothing))
1614 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1615 unsignedp ? umul_widen_optab : smul_widen_optab,
1616 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1618 if (temp != 0)
1620 if (GET_MODE_CLASS (mode) == MODE_INT
1621 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1622 return gen_lowpart (mode, temp);
1623 else
1624 return convert_to_mode (mode, temp, unsignedp);
1628 /* If this is a vector shift by a scalar, see if we can do a vector
1629 shift by a vector. If so, broadcast the scalar into a vector. */
1630 if (mclass == MODE_VECTOR_INT)
1632 optab otheroptab = unknown_optab;
1634 if (binoptab == ashl_optab)
1635 otheroptab = vashl_optab;
1636 else if (binoptab == ashr_optab)
1637 otheroptab = vashr_optab;
1638 else if (binoptab == lshr_optab)
1639 otheroptab = vlshr_optab;
1640 else if (binoptab == rotl_optab)
1641 otheroptab = vrotl_optab;
1642 else if (binoptab == rotr_optab)
1643 otheroptab = vrotr_optab;
1645 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1647 rtx vop1 = expand_vector_broadcast (mode, op1);
1648 if (vop1)
1650 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1651 target, unsignedp, methods, last);
1652 if (temp)
1653 return temp;
1658 /* Look for a wider mode of the same class for which we think we
1659 can open-code the operation. Check for a widening multiply at the
1660 wider mode as well. */
1662 if (CLASS_HAS_WIDER_MODES_P (mclass)
1663 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1664 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1665 wider_mode != VOIDmode;
1666 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1668 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1669 || (binoptab == smul_optab
1670 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1671 && (find_widening_optab_handler ((unsignedp
1672 ? umul_widen_optab
1673 : smul_widen_optab),
1674 GET_MODE_WIDER_MODE (wider_mode),
1675 mode, 0)
1676 != CODE_FOR_nothing)))
1678 rtx xop0 = op0, xop1 = op1;
1679 int no_extend = 0;
1681 /* For certain integer operations, we need not actually extend
1682 the narrow operands, as long as we will truncate
1683 the results to the same narrowness. */
1685 if ((binoptab == ior_optab || binoptab == and_optab
1686 || binoptab == xor_optab
1687 || binoptab == add_optab || binoptab == sub_optab
1688 || binoptab == smul_optab || binoptab == ashl_optab)
1689 && mclass == MODE_INT)
1691 no_extend = 1;
1692 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1693 xop0, unsignedp);
1694 if (binoptab != ashl_optab)
1695 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1696 xop1, unsignedp);
1699 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1701 /* The second operand of a shift must always be extended. */
1702 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1703 no_extend && binoptab != ashl_optab);
1705 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1706 unsignedp, OPTAB_DIRECT);
1707 if (temp)
1709 if (mclass != MODE_INT
1710 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1712 if (target == 0)
1713 target = gen_reg_rtx (mode);
1714 convert_move (target, temp, 0);
1715 return target;
1717 else
1718 return gen_lowpart (mode, temp);
1720 else
1721 delete_insns_since (last);
1725 /* If operation is commutative,
1726 try to make the first operand a register.
1727 Even better, try to make it the same as the target.
1728 Also try to make the last operand a constant. */
1729 if (commutative_optab_p (binoptab)
1730 && swap_commutative_operands_with_target (target, op0, op1))
1732 temp = op1;
1733 op1 = op0;
1734 op0 = temp;
1737 /* These can be done a word at a time. */
1738 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1739 && mclass == MODE_INT
1740 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1741 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1743 int i;
1744 rtx_insn *insns;
1746 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1747 won't be accurate, so use a new target. */
1748 if (target == 0
1749 || target == op0
1750 || target == op1
1751 || !valid_multiword_target_p (target))
1752 target = gen_reg_rtx (mode);
1754 start_sequence ();
1756 /* Do the actual arithmetic. */
1757 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1759 rtx target_piece = operand_subword (target, i, 1, mode);
1760 rtx x = expand_binop (word_mode, binoptab,
1761 operand_subword_force (op0, i, mode),
1762 operand_subword_force (op1, i, mode),
1763 target_piece, unsignedp, next_methods);
1765 if (x == 0)
1766 break;
1768 if (target_piece != x)
1769 emit_move_insn (target_piece, x);
1772 insns = get_insns ();
1773 end_sequence ();
1775 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1777 emit_insn (insns);
1778 return target;
1782 /* Synthesize double word shifts from single word shifts. */
1783 if ((binoptab == lshr_optab || binoptab == ashl_optab
1784 || binoptab == ashr_optab)
1785 && mclass == MODE_INT
1786 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1787 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1788 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1789 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1790 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1791 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1793 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1794 enum machine_mode op1_mode;
1796 double_shift_mask = targetm.shift_truncation_mask (mode);
1797 shift_mask = targetm.shift_truncation_mask (word_mode);
1798 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1800 /* Apply the truncation to constant shifts. */
1801 if (double_shift_mask > 0 && CONST_INT_P (op1))
1802 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1804 if (op1 == CONST0_RTX (op1_mode))
1805 return op0;
1807 /* Make sure that this is a combination that expand_doubleword_shift
1808 can handle. See the comments there for details. */
1809 if (double_shift_mask == 0
1810 || (shift_mask == BITS_PER_WORD - 1
1811 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1813 rtx_insn *insns;
1814 rtx into_target, outof_target;
1815 rtx into_input, outof_input;
1816 int left_shift, outof_word;
1818 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1819 won't be accurate, so use a new target. */
1820 if (target == 0
1821 || target == op0
1822 || target == op1
1823 || !valid_multiword_target_p (target))
1824 target = gen_reg_rtx (mode);
1826 start_sequence ();
1828 /* OUTOF_* is the word we are shifting bits away from, and
1829 INTO_* is the word that we are shifting bits towards, thus
1830 they differ depending on the direction of the shift and
1831 WORDS_BIG_ENDIAN. */
1833 left_shift = binoptab == ashl_optab;
1834 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1836 outof_target = operand_subword (target, outof_word, 1, mode);
1837 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1839 outof_input = operand_subword_force (op0, outof_word, mode);
1840 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1842 if (expand_doubleword_shift (op1_mode, binoptab,
1843 outof_input, into_input, op1,
1844 outof_target, into_target,
1845 unsignedp, next_methods, shift_mask))
1847 insns = get_insns ();
1848 end_sequence ();
1850 emit_insn (insns);
1851 return target;
1853 end_sequence ();
1857 /* Synthesize double word rotates from single word shifts. */
1858 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1859 && mclass == MODE_INT
1860 && CONST_INT_P (op1)
1861 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1862 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1863 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1865 rtx_insn *insns;
1866 rtx into_target, outof_target;
1867 rtx into_input, outof_input;
1868 rtx inter;
1869 int shift_count, left_shift, outof_word;
1871 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1872 won't be accurate, so use a new target. Do this also if target is not
1873 a REG, first because having a register instead may open optimization
1874 opportunities, and second because if target and op0 happen to be MEMs
1875 designating the same location, we would risk clobbering it too early
1876 in the code sequence we generate below. */
1877 if (target == 0
1878 || target == op0
1879 || target == op1
1880 || !REG_P (target)
1881 || !valid_multiword_target_p (target))
1882 target = gen_reg_rtx (mode);
1884 start_sequence ();
1886 shift_count = INTVAL (op1);
1888 /* OUTOF_* is the word we are shifting bits away from, and
1889 INTO_* is the word that we are shifting bits towards, thus
1890 they differ depending on the direction of the shift and
1891 WORDS_BIG_ENDIAN. */
1893 left_shift = (binoptab == rotl_optab);
1894 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1896 outof_target = operand_subword (target, outof_word, 1, mode);
1897 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1899 outof_input = operand_subword_force (op0, outof_word, mode);
1900 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1902 if (shift_count == BITS_PER_WORD)
1904 /* This is just a word swap. */
1905 emit_move_insn (outof_target, into_input);
1906 emit_move_insn (into_target, outof_input);
1907 inter = const0_rtx;
1909 else
1911 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1912 rtx first_shift_count, second_shift_count;
1913 optab reverse_unsigned_shift, unsigned_shift;
1915 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1916 ? lshr_optab : ashl_optab);
1918 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1919 ? ashl_optab : lshr_optab);
1921 if (shift_count > BITS_PER_WORD)
1923 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1924 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1926 else
1928 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1929 second_shift_count = GEN_INT (shift_count);
1932 into_temp1 = expand_binop (word_mode, unsigned_shift,
1933 outof_input, first_shift_count,
1934 NULL_RTX, unsignedp, next_methods);
1935 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1936 into_input, second_shift_count,
1937 NULL_RTX, unsignedp, next_methods);
1939 if (into_temp1 != 0 && into_temp2 != 0)
1940 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1941 into_target, unsignedp, next_methods);
1942 else
1943 inter = 0;
1945 if (inter != 0 && inter != into_target)
1946 emit_move_insn (into_target, inter);
1948 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1949 into_input, first_shift_count,
1950 NULL_RTX, unsignedp, next_methods);
1951 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1952 outof_input, second_shift_count,
1953 NULL_RTX, unsignedp, next_methods);
1955 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1956 inter = expand_binop (word_mode, ior_optab,
1957 outof_temp1, outof_temp2,
1958 outof_target, unsignedp, next_methods);
1960 if (inter != 0 && inter != outof_target)
1961 emit_move_insn (outof_target, inter);
1964 insns = get_insns ();
1965 end_sequence ();
1967 if (inter != 0)
1969 emit_insn (insns);
1970 return target;
1974 /* These can be done a word at a time by propagating carries. */
1975 if ((binoptab == add_optab || binoptab == sub_optab)
1976 && mclass == MODE_INT
1977 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1978 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1980 unsigned int i;
1981 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1982 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1983 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1984 rtx xop0, xop1, xtarget;
1986 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1987 value is one of those, use it. Otherwise, use 1 since it is the
1988 one easiest to get. */
1989 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1990 int normalizep = STORE_FLAG_VALUE;
1991 #else
1992 int normalizep = 1;
1993 #endif
1995 /* Prepare the operands. */
1996 xop0 = force_reg (mode, op0);
1997 xop1 = force_reg (mode, op1);
1999 xtarget = gen_reg_rtx (mode);
2001 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
2002 target = xtarget;
2004 /* Indicate for flow that the entire target reg is being set. */
2005 if (REG_P (target))
2006 emit_clobber (xtarget);
2008 /* Do the actual arithmetic. */
2009 for (i = 0; i < nwords; i++)
2011 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
2012 rtx target_piece = operand_subword (xtarget, index, 1, mode);
2013 rtx op0_piece = operand_subword_force (xop0, index, mode);
2014 rtx op1_piece = operand_subword_force (xop1, index, mode);
2015 rtx x;
2017 /* Main add/subtract of the input operands. */
2018 x = expand_binop (word_mode, binoptab,
2019 op0_piece, op1_piece,
2020 target_piece, unsignedp, next_methods);
2021 if (x == 0)
2022 break;
2024 if (i + 1 < nwords)
2026 /* Store carry from main add/subtract. */
2027 carry_out = gen_reg_rtx (word_mode);
2028 carry_out = emit_store_flag_force (carry_out,
2029 (binoptab == add_optab
2030 ? LT : GT),
2031 x, op0_piece,
2032 word_mode, 1, normalizep);
2035 if (i > 0)
2037 rtx newx;
2039 /* Add/subtract previous carry to main result. */
2040 newx = expand_binop (word_mode,
2041 normalizep == 1 ? binoptab : otheroptab,
2042 x, carry_in,
2043 NULL_RTX, 1, next_methods);
2045 if (i + 1 < nwords)
2047 /* Get out carry from adding/subtracting carry in. */
2048 rtx carry_tmp = gen_reg_rtx (word_mode);
2049 carry_tmp = emit_store_flag_force (carry_tmp,
2050 (binoptab == add_optab
2051 ? LT : GT),
2052 newx, x,
2053 word_mode, 1, normalizep);
2055 /* Logical-ior the two poss. carry together. */
2056 carry_out = expand_binop (word_mode, ior_optab,
2057 carry_out, carry_tmp,
2058 carry_out, 0, next_methods);
2059 if (carry_out == 0)
2060 break;
2062 emit_move_insn (target_piece, newx);
2064 else
2066 if (x != target_piece)
2067 emit_move_insn (target_piece, x);
2070 carry_in = carry_out;
2073 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2075 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2076 || ! rtx_equal_p (target, xtarget))
2078 rtx temp = emit_move_insn (target, xtarget);
2080 set_dst_reg_note (temp, REG_EQUAL,
2081 gen_rtx_fmt_ee (optab_to_code (binoptab),
2082 mode, copy_rtx (xop0),
2083 copy_rtx (xop1)),
2084 target);
2086 else
2087 target = xtarget;
2089 return target;
2092 else
2093 delete_insns_since (last);
2096 /* Attempt to synthesize double word multiplies using a sequence of word
2097 mode multiplications. We first attempt to generate a sequence using a
2098 more efficient unsigned widening multiply, and if that fails we then
2099 try using a signed widening multiply. */
2101 if (binoptab == smul_optab
2102 && mclass == MODE_INT
2103 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2104 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2105 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2107 rtx product = NULL_RTX;
2108 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
2109 != CODE_FOR_nothing)
2111 product = expand_doubleword_mult (mode, op0, op1, target,
2112 true, methods);
2113 if (!product)
2114 delete_insns_since (last);
2117 if (product == NULL_RTX
2118 && widening_optab_handler (smul_widen_optab, mode, word_mode)
2119 != CODE_FOR_nothing)
2121 product = expand_doubleword_mult (mode, op0, op1, target,
2122 false, methods);
2123 if (!product)
2124 delete_insns_since (last);
2127 if (product != NULL_RTX)
2129 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2131 temp = emit_move_insn (target ? target : product, product);
2132 set_dst_reg_note (temp,
2133 REG_EQUAL,
2134 gen_rtx_fmt_ee (MULT, mode,
2135 copy_rtx (op0),
2136 copy_rtx (op1)),
2137 target ? target : product);
2139 return product;
2143 /* It can't be open-coded in this mode.
2144 Use a library call if one is available and caller says that's ok. */
2146 libfunc = optab_libfunc (binoptab, mode);
2147 if (libfunc
2148 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2150 rtx_insn *insns;
2151 rtx op1x = op1;
2152 enum machine_mode op1_mode = mode;
2153 rtx value;
2155 start_sequence ();
2157 if (shift_optab_p (binoptab))
2159 op1_mode = targetm.libgcc_shift_count_mode ();
2160 /* Specify unsigned here,
2161 since negative shift counts are meaningless. */
2162 op1x = convert_to_mode (op1_mode, op1, 1);
2165 if (GET_MODE (op0) != VOIDmode
2166 && GET_MODE (op0) != mode)
2167 op0 = convert_to_mode (mode, op0, unsignedp);
2169 /* Pass 1 for NO_QUEUE so we don't lose any increments
2170 if the libcall is cse'd or moved. */
2171 value = emit_library_call_value (libfunc,
2172 NULL_RTX, LCT_CONST, mode, 2,
2173 op0, mode, op1x, op1_mode);
2175 insns = get_insns ();
2176 end_sequence ();
2178 target = gen_reg_rtx (mode);
2179 emit_libcall_block_1 (insns, target, value,
2180 gen_rtx_fmt_ee (optab_to_code (binoptab),
2181 mode, op0, op1),
2182 trapv_binoptab_p (binoptab));
2184 return target;
2187 delete_insns_since (last);
2189 /* It can't be done in this mode. Can we do it in a wider mode? */
2191 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2192 || methods == OPTAB_MUST_WIDEN))
2194 /* Caller says, don't even try. */
2195 delete_insns_since (entry_last);
2196 return 0;
2199 /* Compute the value of METHODS to pass to recursive calls.
2200 Don't allow widening to be tried recursively. */
2202 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2204 /* Look for a wider mode of the same class for which it appears we can do
2205 the operation. */
2207 if (CLASS_HAS_WIDER_MODES_P (mclass))
2209 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2210 wider_mode != VOIDmode;
2211 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2213 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2214 != CODE_FOR_nothing
2215 || (methods == OPTAB_LIB
2216 && optab_libfunc (binoptab, wider_mode)))
2218 rtx xop0 = op0, xop1 = op1;
2219 int no_extend = 0;
2221 /* For certain integer operations, we need not actually extend
2222 the narrow operands, as long as we will truncate
2223 the results to the same narrowness. */
2225 if ((binoptab == ior_optab || binoptab == and_optab
2226 || binoptab == xor_optab
2227 || binoptab == add_optab || binoptab == sub_optab
2228 || binoptab == smul_optab || binoptab == ashl_optab)
2229 && mclass == MODE_INT)
2230 no_extend = 1;
2232 xop0 = widen_operand (xop0, wider_mode, mode,
2233 unsignedp, no_extend);
2235 /* The second operand of a shift must always be extended. */
2236 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2237 no_extend && binoptab != ashl_optab);
2239 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2240 unsignedp, methods);
2241 if (temp)
2243 if (mclass != MODE_INT
2244 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2246 if (target == 0)
2247 target = gen_reg_rtx (mode);
2248 convert_move (target, temp, 0);
2249 return target;
2251 else
2252 return gen_lowpart (mode, temp);
2254 else
2255 delete_insns_since (last);
2260 delete_insns_since (entry_last);
2261 return 0;
2264 /* Expand a binary operator which has both signed and unsigned forms.
2265 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2266 signed operations.
2268 If we widen unsigned operands, we may use a signed wider operation instead
2269 of an unsigned wider operation, since the result would be the same. */
2272 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
2273 rtx op0, rtx op1, rtx target, int unsignedp,
2274 enum optab_methods methods)
2276 rtx temp;
2277 optab direct_optab = unsignedp ? uoptab : soptab;
2278 bool save_enable;
2280 /* Do it without widening, if possible. */
2281 temp = expand_binop (mode, direct_optab, op0, op1, target,
2282 unsignedp, OPTAB_DIRECT);
2283 if (temp || methods == OPTAB_DIRECT)
2284 return temp;
2286 /* Try widening to a signed int. Disable any direct use of any
2287 signed insn in the current mode. */
2288 save_enable = swap_optab_enable (soptab, mode, false);
2290 temp = expand_binop (mode, soptab, op0, op1, target,
2291 unsignedp, OPTAB_WIDEN);
2293 /* For unsigned operands, try widening to an unsigned int. */
2294 if (!temp && unsignedp)
2295 temp = expand_binop (mode, uoptab, op0, op1, target,
2296 unsignedp, OPTAB_WIDEN);
2297 if (temp || methods == OPTAB_WIDEN)
2298 goto egress;
2300 /* Use the right width libcall if that exists. */
2301 temp = expand_binop (mode, direct_optab, op0, op1, target,
2302 unsignedp, OPTAB_LIB);
2303 if (temp || methods == OPTAB_LIB)
2304 goto egress;
2306 /* Must widen and use a libcall, use either signed or unsigned. */
2307 temp = expand_binop (mode, soptab, op0, op1, target,
2308 unsignedp, methods);
2309 if (!temp && unsignedp)
2310 temp = expand_binop (mode, uoptab, op0, op1, target,
2311 unsignedp, methods);
2313 egress:
2314 /* Undo the fiddling above. */
2315 if (save_enable)
2316 swap_optab_enable (soptab, mode, true);
2317 return temp;
2320 /* Generate code to perform an operation specified by UNOPPTAB
2321 on operand OP0, with two results to TARG0 and TARG1.
2322 We assume that the order of the operands for the instruction
2323 is TARG0, TARG1, OP0.
2325 Either TARG0 or TARG1 may be zero, but what that means is that
2326 the result is not actually wanted. We will generate it into
2327 a dummy pseudo-reg and discard it. They may not both be zero.
2329 Returns 1 if this operation can be performed; 0 if not. */
2332 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2333 int unsignedp)
2335 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2336 enum mode_class mclass;
2337 enum machine_mode wider_mode;
2338 rtx_insn *entry_last = get_last_insn ();
2339 rtx_insn *last;
2341 mclass = GET_MODE_CLASS (mode);
2343 if (!targ0)
2344 targ0 = gen_reg_rtx (mode);
2345 if (!targ1)
2346 targ1 = gen_reg_rtx (mode);
2348 /* Record where to go back to if we fail. */
2349 last = get_last_insn ();
2351 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2353 struct expand_operand ops[3];
2354 enum insn_code icode = optab_handler (unoptab, mode);
2356 create_fixed_operand (&ops[0], targ0);
2357 create_fixed_operand (&ops[1], targ1);
2358 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2359 if (maybe_expand_insn (icode, 3, ops))
2360 return 1;
2363 /* It can't be done in this mode. Can we do it in a wider mode? */
2365 if (CLASS_HAS_WIDER_MODES_P (mclass))
2367 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2368 wider_mode != VOIDmode;
2369 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2371 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2373 rtx t0 = gen_reg_rtx (wider_mode);
2374 rtx t1 = gen_reg_rtx (wider_mode);
2375 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2377 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2379 convert_move (targ0, t0, unsignedp);
2380 convert_move (targ1, t1, unsignedp);
2381 return 1;
2383 else
2384 delete_insns_since (last);
2389 delete_insns_since (entry_last);
2390 return 0;
2393 /* Generate code to perform an operation specified by BINOPTAB
2394 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2395 We assume that the order of the operands for the instruction
2396 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2397 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2399 Either TARG0 or TARG1 may be zero, but what that means is that
2400 the result is not actually wanted. We will generate it into
2401 a dummy pseudo-reg and discard it. They may not both be zero.
2403 Returns 1 if this operation can be performed; 0 if not. */
2406 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2407 int unsignedp)
2409 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2410 enum mode_class mclass;
2411 enum machine_mode wider_mode;
2412 rtx_insn *entry_last = get_last_insn ();
2413 rtx_insn *last;
2415 mclass = GET_MODE_CLASS (mode);
2417 if (!targ0)
2418 targ0 = gen_reg_rtx (mode);
2419 if (!targ1)
2420 targ1 = gen_reg_rtx (mode);
2422 /* Record where to go back to if we fail. */
2423 last = get_last_insn ();
2425 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2427 struct expand_operand ops[4];
2428 enum insn_code icode = optab_handler (binoptab, mode);
2429 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2430 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
2431 rtx xop0 = op0, xop1 = op1;
2433 /* If we are optimizing, force expensive constants into a register. */
2434 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2435 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2437 create_fixed_operand (&ops[0], targ0);
2438 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2439 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2440 create_fixed_operand (&ops[3], targ1);
2441 if (maybe_expand_insn (icode, 4, ops))
2442 return 1;
2443 delete_insns_since (last);
2446 /* It can't be done in this mode. Can we do it in a wider mode? */
2448 if (CLASS_HAS_WIDER_MODES_P (mclass))
2450 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2451 wider_mode != VOIDmode;
2452 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2454 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2456 rtx t0 = gen_reg_rtx (wider_mode);
2457 rtx t1 = gen_reg_rtx (wider_mode);
2458 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2459 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2461 if (expand_twoval_binop (binoptab, cop0, cop1,
2462 t0, t1, unsignedp))
2464 convert_move (targ0, t0, unsignedp);
2465 convert_move (targ1, t1, unsignedp);
2466 return 1;
2468 else
2469 delete_insns_since (last);
2474 delete_insns_since (entry_last);
2475 return 0;
2478 /* Expand the two-valued library call indicated by BINOPTAB, but
2479 preserve only one of the values. If TARG0 is non-NULL, the first
2480 value is placed into TARG0; otherwise the second value is placed
2481 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2482 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2483 This routine assumes that the value returned by the library call is
2484 as if the return value was of an integral mode twice as wide as the
2485 mode of OP0. Returns 1 if the call was successful. */
2487 bool
2488 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2489 rtx targ0, rtx targ1, enum rtx_code code)
2491 enum machine_mode mode;
2492 enum machine_mode libval_mode;
2493 rtx libval;
2494 rtx_insn *insns;
2495 rtx libfunc;
2497 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2498 gcc_assert (!targ0 != !targ1);
2500 mode = GET_MODE (op0);
2501 libfunc = optab_libfunc (binoptab, mode);
2502 if (!libfunc)
2503 return false;
2505 /* The value returned by the library function will have twice as
2506 many bits as the nominal MODE. */
2507 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2508 MODE_INT);
2509 start_sequence ();
2510 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2511 libval_mode, 2,
2512 op0, mode,
2513 op1, mode);
2514 /* Get the part of VAL containing the value that we want. */
2515 libval = simplify_gen_subreg (mode, libval, libval_mode,
2516 targ0 ? 0 : GET_MODE_SIZE (mode));
2517 insns = get_insns ();
2518 end_sequence ();
2519 /* Move the into the desired location. */
2520 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2521 gen_rtx_fmt_ee (code, mode, op0, op1));
2523 return true;
2527 /* Wrapper around expand_unop which takes an rtx code to specify
2528 the operation to perform, not an optab pointer. All other
2529 arguments are the same. */
2531 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2532 rtx target, int unsignedp)
2534 optab unop = code_to_optab (code);
2535 gcc_assert (unop);
2537 return expand_unop (mode, unop, op0, target, unsignedp);
2540 /* Try calculating
2541 (clz:narrow x)
2543 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2545 A similar operation can be used for clrsb. UNOPTAB says which operation
2546 we are trying to expand. */
2547 static rtx
2548 widen_leading (enum machine_mode mode, rtx op0, rtx target, optab unoptab)
2550 enum mode_class mclass = GET_MODE_CLASS (mode);
2551 if (CLASS_HAS_WIDER_MODES_P (mclass))
2553 enum machine_mode wider_mode;
2554 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2555 wider_mode != VOIDmode;
2556 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2558 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2560 rtx xop0, temp;
2561 rtx_insn *last;
2563 last = get_last_insn ();
2565 if (target == 0)
2566 target = gen_reg_rtx (mode);
2567 xop0 = widen_operand (op0, wider_mode, mode,
2568 unoptab != clrsb_optab, false);
2569 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2570 unoptab != clrsb_optab);
2571 if (temp != 0)
2572 temp = expand_binop
2573 (wider_mode, sub_optab, temp,
2574 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2575 - GET_MODE_PRECISION (mode),
2576 wider_mode),
2577 target, true, OPTAB_DIRECT);
2578 if (temp == 0)
2579 delete_insns_since (last);
2581 return temp;
2585 return 0;
2588 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2589 quantities, choosing which based on whether the high word is nonzero. */
2590 static rtx
2591 expand_doubleword_clz (enum machine_mode mode, rtx op0, rtx target)
2593 rtx xop0 = force_reg (mode, op0);
2594 rtx subhi = gen_highpart (word_mode, xop0);
2595 rtx sublo = gen_lowpart (word_mode, xop0);
2596 rtx_code_label *hi0_label = gen_label_rtx ();
2597 rtx_code_label *after_label = gen_label_rtx ();
2598 rtx_insn *seq;
2599 rtx temp, result;
2601 /* If we were not given a target, use a word_mode register, not a
2602 'mode' register. The result will fit, and nobody is expecting
2603 anything bigger (the return type of __builtin_clz* is int). */
2604 if (!target)
2605 target = gen_reg_rtx (word_mode);
2607 /* In any case, write to a word_mode scratch in both branches of the
2608 conditional, so we can ensure there is a single move insn setting
2609 'target' to tag a REG_EQUAL note on. */
2610 result = gen_reg_rtx (word_mode);
2612 start_sequence ();
2614 /* If the high word is not equal to zero,
2615 then clz of the full value is clz of the high word. */
2616 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2617 word_mode, true, hi0_label);
2619 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2620 if (!temp)
2621 goto fail;
2623 if (temp != result)
2624 convert_move (result, temp, true);
2626 emit_jump_insn (gen_jump (after_label));
2627 emit_barrier ();
2629 /* Else clz of the full value is clz of the low word plus the number
2630 of bits in the high word. */
2631 emit_label (hi0_label);
2633 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2634 if (!temp)
2635 goto fail;
2636 temp = expand_binop (word_mode, add_optab, temp,
2637 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2638 result, true, OPTAB_DIRECT);
2639 if (!temp)
2640 goto fail;
2641 if (temp != result)
2642 convert_move (result, temp, true);
2644 emit_label (after_label);
2645 convert_move (target, result, true);
2647 seq = get_insns ();
2648 end_sequence ();
2650 add_equal_note (seq, target, CLZ, xop0, 0);
2651 emit_insn (seq);
2652 return target;
2654 fail:
2655 end_sequence ();
2656 return 0;
2659 /* Try calculating
2660 (bswap:narrow x)
2662 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2663 static rtx
2664 widen_bswap (enum machine_mode mode, rtx op0, rtx target)
2666 enum mode_class mclass = GET_MODE_CLASS (mode);
2667 enum machine_mode wider_mode;
2668 rtx x;
2669 rtx_insn *last;
2671 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2672 return NULL_RTX;
2674 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2675 wider_mode != VOIDmode;
2676 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2677 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2678 goto found;
2679 return NULL_RTX;
2681 found:
2682 last = get_last_insn ();
2684 x = widen_operand (op0, wider_mode, mode, true, true);
2685 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2687 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2688 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2689 if (x != 0)
2690 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2691 GET_MODE_BITSIZE (wider_mode)
2692 - GET_MODE_BITSIZE (mode),
2693 NULL_RTX, true);
2695 if (x != 0)
2697 if (target == 0)
2698 target = gen_reg_rtx (mode);
2699 emit_move_insn (target, gen_lowpart (mode, x));
2701 else
2702 delete_insns_since (last);
2704 return target;
2707 /* Try calculating bswap as two bswaps of two word-sized operands. */
2709 static rtx
2710 expand_doubleword_bswap (enum machine_mode mode, rtx op, rtx target)
2712 rtx t0, t1;
2714 t1 = expand_unop (word_mode, bswap_optab,
2715 operand_subword_force (op, 0, mode), NULL_RTX, true);
2716 t0 = expand_unop (word_mode, bswap_optab,
2717 operand_subword_force (op, 1, mode), NULL_RTX, true);
2719 if (target == 0 || !valid_multiword_target_p (target))
2720 target = gen_reg_rtx (mode);
2721 if (REG_P (target))
2722 emit_clobber (target);
2723 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2724 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2726 return target;
2729 /* Try calculating (parity x) as (and (popcount x) 1), where
2730 popcount can also be done in a wider mode. */
2731 static rtx
2732 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2734 enum mode_class mclass = GET_MODE_CLASS (mode);
2735 if (CLASS_HAS_WIDER_MODES_P (mclass))
2737 enum machine_mode wider_mode;
2738 for (wider_mode = mode; wider_mode != VOIDmode;
2739 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2741 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2743 rtx xop0, temp;
2744 rtx_insn *last;
2746 last = get_last_insn ();
2748 if (target == 0)
2749 target = gen_reg_rtx (mode);
2750 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2751 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2752 true);
2753 if (temp != 0)
2754 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2755 target, true, OPTAB_DIRECT);
2756 if (temp == 0)
2757 delete_insns_since (last);
2759 return temp;
2763 return 0;
2766 /* Try calculating ctz(x) as K - clz(x & -x) ,
2767 where K is GET_MODE_PRECISION(mode) - 1.
2769 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2770 don't have to worry about what the hardware does in that case. (If
2771 the clz instruction produces the usual value at 0, which is K, the
2772 result of this code sequence will be -1; expand_ffs, below, relies
2773 on this. It might be nice to have it be K instead, for consistency
2774 with the (very few) processors that provide a ctz with a defined
2775 value, but that would take one more instruction, and it would be
2776 less convenient for expand_ffs anyway. */
2778 static rtx
2779 expand_ctz (enum machine_mode mode, rtx op0, rtx target)
2781 rtx_insn *seq;
2782 rtx temp;
2784 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2785 return 0;
2787 start_sequence ();
2789 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2790 if (temp)
2791 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2792 true, OPTAB_DIRECT);
2793 if (temp)
2794 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2795 if (temp)
2796 temp = expand_binop (mode, sub_optab,
2797 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2798 temp, target,
2799 true, OPTAB_DIRECT);
2800 if (temp == 0)
2802 end_sequence ();
2803 return 0;
2806 seq = get_insns ();
2807 end_sequence ();
2809 add_equal_note (seq, temp, CTZ, op0, 0);
2810 emit_insn (seq);
2811 return temp;
2815 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2816 else with the sequence used by expand_clz.
2818 The ffs builtin promises to return zero for a zero value and ctz/clz
2819 may have an undefined value in that case. If they do not give us a
2820 convenient value, we have to generate a test and branch. */
2821 static rtx
2822 expand_ffs (enum machine_mode mode, rtx op0, rtx target)
2824 HOST_WIDE_INT val = 0;
2825 bool defined_at_zero = false;
2826 rtx temp;
2827 rtx_insn *seq;
2829 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2831 start_sequence ();
2833 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2834 if (!temp)
2835 goto fail;
2837 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2839 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2841 start_sequence ();
2842 temp = expand_ctz (mode, op0, 0);
2843 if (!temp)
2844 goto fail;
2846 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2848 defined_at_zero = true;
2849 val = (GET_MODE_PRECISION (mode) - 1) - val;
2852 else
2853 return 0;
2855 if (defined_at_zero && val == -1)
2856 /* No correction needed at zero. */;
2857 else
2859 /* We don't try to do anything clever with the situation found
2860 on some processors (eg Alpha) where ctz(0:mode) ==
2861 bitsize(mode). If someone can think of a way to send N to -1
2862 and leave alone all values in the range 0..N-1 (where N is a
2863 power of two), cheaper than this test-and-branch, please add it.
2865 The test-and-branch is done after the operation itself, in case
2866 the operation sets condition codes that can be recycled for this.
2867 (This is true on i386, for instance.) */
2869 rtx_code_label *nonzero_label = gen_label_rtx ();
2870 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2871 mode, true, nonzero_label);
2873 convert_move (temp, GEN_INT (-1), false);
2874 emit_label (nonzero_label);
2877 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2878 to produce a value in the range 0..bitsize. */
2879 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2880 target, false, OPTAB_DIRECT);
2881 if (!temp)
2882 goto fail;
2884 seq = get_insns ();
2885 end_sequence ();
2887 add_equal_note (seq, temp, FFS, op0, 0);
2888 emit_insn (seq);
2889 return temp;
2891 fail:
2892 end_sequence ();
2893 return 0;
2896 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2897 conditions, VAL may already be a SUBREG against which we cannot generate
2898 a further SUBREG. In this case, we expect forcing the value into a
2899 register will work around the situation. */
2901 static rtx
2902 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2903 enum machine_mode imode)
2905 rtx ret;
2906 ret = lowpart_subreg (omode, val, imode);
2907 if (ret == NULL)
2909 val = force_reg (imode, val);
2910 ret = lowpart_subreg (omode, val, imode);
2911 gcc_assert (ret != NULL);
2913 return ret;
2916 /* Expand a floating point absolute value or negation operation via a
2917 logical operation on the sign bit. */
2919 static rtx
2920 expand_absneg_bit (enum rtx_code code, enum machine_mode mode,
2921 rtx op0, rtx target)
2923 const struct real_format *fmt;
2924 int bitpos, word, nwords, i;
2925 enum machine_mode imode;
2926 rtx temp;
2927 rtx_insn *insns;
2929 /* The format has to have a simple sign bit. */
2930 fmt = REAL_MODE_FORMAT (mode);
2931 if (fmt == NULL)
2932 return NULL_RTX;
2934 bitpos = fmt->signbit_rw;
2935 if (bitpos < 0)
2936 return NULL_RTX;
2938 /* Don't create negative zeros if the format doesn't support them. */
2939 if (code == NEG && !fmt->has_signed_zero)
2940 return NULL_RTX;
2942 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2944 imode = int_mode_for_mode (mode);
2945 if (imode == BLKmode)
2946 return NULL_RTX;
2947 word = 0;
2948 nwords = 1;
2950 else
2952 imode = word_mode;
2954 if (FLOAT_WORDS_BIG_ENDIAN)
2955 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2956 else
2957 word = bitpos / BITS_PER_WORD;
2958 bitpos = bitpos % BITS_PER_WORD;
2959 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2962 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
2963 if (code == ABS)
2964 mask = ~mask;
2966 if (target == 0
2967 || target == op0
2968 || (nwords > 1 && !valid_multiword_target_p (target)))
2969 target = gen_reg_rtx (mode);
2971 if (nwords > 1)
2973 start_sequence ();
2975 for (i = 0; i < nwords; ++i)
2977 rtx targ_piece = operand_subword (target, i, 1, mode);
2978 rtx op0_piece = operand_subword_force (op0, i, mode);
2980 if (i == word)
2982 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2983 op0_piece,
2984 immed_wide_int_const (mask, imode),
2985 targ_piece, 1, OPTAB_LIB_WIDEN);
2986 if (temp != targ_piece)
2987 emit_move_insn (targ_piece, temp);
2989 else
2990 emit_move_insn (targ_piece, op0_piece);
2993 insns = get_insns ();
2994 end_sequence ();
2996 emit_insn (insns);
2998 else
3000 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
3001 gen_lowpart (imode, op0),
3002 immed_wide_int_const (mask, imode),
3003 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3004 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3006 set_dst_reg_note (get_last_insn (), REG_EQUAL,
3007 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
3008 target);
3011 return target;
3014 /* As expand_unop, but will fail rather than attempt the operation in a
3015 different mode or with a libcall. */
3016 static rtx
3017 expand_unop_direct (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
3018 int unsignedp)
3020 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
3022 struct expand_operand ops[2];
3023 enum insn_code icode = optab_handler (unoptab, mode);
3024 rtx_insn *last = get_last_insn ();
3025 rtx pat;
3027 create_output_operand (&ops[0], target, mode);
3028 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
3029 pat = maybe_gen_insn (icode, 2, ops);
3030 if (pat)
3032 if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
3033 && ! add_equal_note (as_a <rtx_insn *> (pat), ops[0].value,
3034 optab_to_code (unoptab),
3035 ops[1].value, NULL_RTX))
3037 delete_insns_since (last);
3038 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3041 emit_insn (pat);
3043 return ops[0].value;
3046 return 0;
3049 /* Generate code to perform an operation specified by UNOPTAB
3050 on operand OP0, with result having machine-mode MODE.
3052 UNSIGNEDP is for the case where we have to widen the operands
3053 to perform the operation. It says to use zero-extension.
3055 If TARGET is nonzero, the value
3056 is generated there, if it is convenient to do so.
3057 In all cases an rtx is returned for the locus of the value;
3058 this may or may not be TARGET. */
3061 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
3062 int unsignedp)
3064 enum mode_class mclass = GET_MODE_CLASS (mode);
3065 enum machine_mode wider_mode;
3066 rtx temp;
3067 rtx libfunc;
3069 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3070 if (temp)
3071 return temp;
3073 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3075 /* Widening (or narrowing) clz needs special treatment. */
3076 if (unoptab == clz_optab)
3078 temp = widen_leading (mode, op0, target, unoptab);
3079 if (temp)
3080 return temp;
3082 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3083 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3085 temp = expand_doubleword_clz (mode, op0, target);
3086 if (temp)
3087 return temp;
3090 goto try_libcall;
3093 if (unoptab == clrsb_optab)
3095 temp = widen_leading (mode, op0, target, unoptab);
3096 if (temp)
3097 return temp;
3098 goto try_libcall;
3101 /* Widening (or narrowing) bswap needs special treatment. */
3102 if (unoptab == bswap_optab)
3104 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3105 or ROTATERT. First try these directly; if this fails, then try the
3106 obvious pair of shifts with allowed widening, as this will probably
3107 be always more efficient than the other fallback methods. */
3108 if (mode == HImode)
3110 rtx_insn *last;
3111 rtx temp1, temp2;
3113 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3115 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
3116 unsignedp, OPTAB_DIRECT);
3117 if (temp)
3118 return temp;
3121 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3123 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
3124 unsignedp, OPTAB_DIRECT);
3125 if (temp)
3126 return temp;
3129 last = get_last_insn ();
3131 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
3132 unsignedp, OPTAB_WIDEN);
3133 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
3134 unsignedp, OPTAB_WIDEN);
3135 if (temp1 && temp2)
3137 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3138 unsignedp, OPTAB_WIDEN);
3139 if (temp)
3140 return temp;
3143 delete_insns_since (last);
3146 temp = widen_bswap (mode, op0, target);
3147 if (temp)
3148 return temp;
3150 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3151 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3153 temp = expand_doubleword_bswap (mode, op0, target);
3154 if (temp)
3155 return temp;
3158 goto try_libcall;
3161 if (CLASS_HAS_WIDER_MODES_P (mclass))
3162 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3163 wider_mode != VOIDmode;
3164 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3166 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3168 rtx xop0 = op0;
3169 rtx_insn *last = get_last_insn ();
3171 /* For certain operations, we need not actually extend
3172 the narrow operand, as long as we will truncate the
3173 results to the same narrowness. */
3175 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3176 (unoptab == neg_optab
3177 || unoptab == one_cmpl_optab)
3178 && mclass == MODE_INT);
3180 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3181 unsignedp);
3183 if (temp)
3185 if (mclass != MODE_INT
3186 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3188 if (target == 0)
3189 target = gen_reg_rtx (mode);
3190 convert_move (target, temp, 0);
3191 return target;
3193 else
3194 return gen_lowpart (mode, temp);
3196 else
3197 delete_insns_since (last);
3201 /* These can be done a word at a time. */
3202 if (unoptab == one_cmpl_optab
3203 && mclass == MODE_INT
3204 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3205 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3207 int i;
3208 rtx_insn *insns;
3210 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3211 target = gen_reg_rtx (mode);
3213 start_sequence ();
3215 /* Do the actual arithmetic. */
3216 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3218 rtx target_piece = operand_subword (target, i, 1, mode);
3219 rtx x = expand_unop (word_mode, unoptab,
3220 operand_subword_force (op0, i, mode),
3221 target_piece, unsignedp);
3223 if (target_piece != x)
3224 emit_move_insn (target_piece, x);
3227 insns = get_insns ();
3228 end_sequence ();
3230 emit_insn (insns);
3231 return target;
3234 if (optab_to_code (unoptab) == NEG)
3236 /* Try negating floating point values by flipping the sign bit. */
3237 if (SCALAR_FLOAT_MODE_P (mode))
3239 temp = expand_absneg_bit (NEG, mode, op0, target);
3240 if (temp)
3241 return temp;
3244 /* If there is no negation pattern, and we have no negative zero,
3245 try subtracting from zero. */
3246 if (!HONOR_SIGNED_ZEROS (mode))
3248 temp = expand_binop (mode, (unoptab == negv_optab
3249 ? subv_optab : sub_optab),
3250 CONST0_RTX (mode), op0, target,
3251 unsignedp, OPTAB_DIRECT);
3252 if (temp)
3253 return temp;
3257 /* Try calculating parity (x) as popcount (x) % 2. */
3258 if (unoptab == parity_optab)
3260 temp = expand_parity (mode, op0, target);
3261 if (temp)
3262 return temp;
3265 /* Try implementing ffs (x) in terms of clz (x). */
3266 if (unoptab == ffs_optab)
3268 temp = expand_ffs (mode, op0, target);
3269 if (temp)
3270 return temp;
3273 /* Try implementing ctz (x) in terms of clz (x). */
3274 if (unoptab == ctz_optab)
3276 temp = expand_ctz (mode, op0, target);
3277 if (temp)
3278 return temp;
3281 try_libcall:
3282 /* Now try a library call in this mode. */
3283 libfunc = optab_libfunc (unoptab, mode);
3284 if (libfunc)
3286 rtx_insn *insns;
3287 rtx value;
3288 rtx eq_value;
3289 enum machine_mode outmode = mode;
3291 /* All of these functions return small values. Thus we choose to
3292 have them return something that isn't a double-word. */
3293 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3294 || unoptab == clrsb_optab || unoptab == popcount_optab
3295 || unoptab == parity_optab)
3296 outmode
3297 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3298 optab_libfunc (unoptab, mode)));
3300 start_sequence ();
3302 /* Pass 1 for NO_QUEUE so we don't lose any increments
3303 if the libcall is cse'd or moved. */
3304 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3305 1, op0, mode);
3306 insns = get_insns ();
3307 end_sequence ();
3309 target = gen_reg_rtx (outmode);
3310 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3311 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3312 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3313 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3314 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3315 emit_libcall_block_1 (insns, target, value, eq_value,
3316 trapv_unoptab_p (unoptab));
3318 return target;
3321 /* It can't be done in this mode. Can we do it in a wider mode? */
3323 if (CLASS_HAS_WIDER_MODES_P (mclass))
3325 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3326 wider_mode != VOIDmode;
3327 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3329 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3330 || optab_libfunc (unoptab, wider_mode))
3332 rtx xop0 = op0;
3333 rtx_insn *last = get_last_insn ();
3335 /* For certain operations, we need not actually extend
3336 the narrow operand, as long as we will truncate the
3337 results to the same narrowness. */
3338 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3339 (unoptab == neg_optab
3340 || unoptab == one_cmpl_optab
3341 || unoptab == bswap_optab)
3342 && mclass == MODE_INT);
3344 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3345 unsignedp);
3347 /* If we are generating clz using wider mode, adjust the
3348 result. Similarly for clrsb. */
3349 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3350 && temp != 0)
3351 temp = expand_binop
3352 (wider_mode, sub_optab, temp,
3353 gen_int_mode (GET_MODE_PRECISION (wider_mode)
3354 - GET_MODE_PRECISION (mode),
3355 wider_mode),
3356 target, true, OPTAB_DIRECT);
3358 /* Likewise for bswap. */
3359 if (unoptab == bswap_optab && temp != 0)
3361 gcc_assert (GET_MODE_PRECISION (wider_mode)
3362 == GET_MODE_BITSIZE (wider_mode)
3363 && GET_MODE_PRECISION (mode)
3364 == GET_MODE_BITSIZE (mode));
3366 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3367 GET_MODE_BITSIZE (wider_mode)
3368 - GET_MODE_BITSIZE (mode),
3369 NULL_RTX, true);
3372 if (temp)
3374 if (mclass != MODE_INT)
3376 if (target == 0)
3377 target = gen_reg_rtx (mode);
3378 convert_move (target, temp, 0);
3379 return target;
3381 else
3382 return gen_lowpart (mode, temp);
3384 else
3385 delete_insns_since (last);
3390 /* One final attempt at implementing negation via subtraction,
3391 this time allowing widening of the operand. */
3392 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3394 rtx temp;
3395 temp = expand_binop (mode,
3396 unoptab == negv_optab ? subv_optab : sub_optab,
3397 CONST0_RTX (mode), op0,
3398 target, unsignedp, OPTAB_LIB_WIDEN);
3399 if (temp)
3400 return temp;
3403 return 0;
3406 /* Emit code to compute the absolute value of OP0, with result to
3407 TARGET if convenient. (TARGET may be 0.) The return value says
3408 where the result actually is to be found.
3410 MODE is the mode of the operand; the mode of the result is
3411 different but can be deduced from MODE.
3416 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
3417 int result_unsignedp)
3419 rtx temp;
3421 if (GET_MODE_CLASS (mode) != MODE_INT
3422 || ! flag_trapv)
3423 result_unsignedp = 1;
3425 /* First try to do it with a special abs instruction. */
3426 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3427 op0, target, 0);
3428 if (temp != 0)
3429 return temp;
3431 /* For floating point modes, try clearing the sign bit. */
3432 if (SCALAR_FLOAT_MODE_P (mode))
3434 temp = expand_absneg_bit (ABS, mode, op0, target);
3435 if (temp)
3436 return temp;
3439 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3440 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3441 && !HONOR_SIGNED_ZEROS (mode))
3443 rtx_insn *last = get_last_insn ();
3445 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3446 op0, NULL_RTX, 0);
3447 if (temp != 0)
3448 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3449 OPTAB_WIDEN);
3451 if (temp != 0)
3452 return temp;
3454 delete_insns_since (last);
3457 /* If this machine has expensive jumps, we can do integer absolute
3458 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3459 where W is the width of MODE. */
3461 if (GET_MODE_CLASS (mode) == MODE_INT
3462 && BRANCH_COST (optimize_insn_for_speed_p (),
3463 false) >= 2)
3465 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3466 GET_MODE_PRECISION (mode) - 1,
3467 NULL_RTX, 0);
3469 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3470 OPTAB_LIB_WIDEN);
3471 if (temp != 0)
3472 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3473 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3475 if (temp != 0)
3476 return temp;
3479 return NULL_RTX;
3483 expand_abs (enum machine_mode mode, rtx op0, rtx target,
3484 int result_unsignedp, int safe)
3486 rtx temp;
3487 rtx_code_label *op1;
3489 if (GET_MODE_CLASS (mode) != MODE_INT
3490 || ! flag_trapv)
3491 result_unsignedp = 1;
3493 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3494 if (temp != 0)
3495 return temp;
3497 /* If that does not win, use conditional jump and negate. */
3499 /* It is safe to use the target if it is the same
3500 as the source if this is also a pseudo register */
3501 if (op0 == target && REG_P (op0)
3502 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3503 safe = 1;
3505 op1 = gen_label_rtx ();
3506 if (target == 0 || ! safe
3507 || GET_MODE (target) != mode
3508 || (MEM_P (target) && MEM_VOLATILE_P (target))
3509 || (REG_P (target)
3510 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3511 target = gen_reg_rtx (mode);
3513 emit_move_insn (target, op0);
3514 NO_DEFER_POP;
3516 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3517 NULL_RTX, NULL_RTX, op1, -1);
3519 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3520 target, target, 0);
3521 if (op0 != target)
3522 emit_move_insn (target, op0);
3523 emit_label (op1);
3524 OK_DEFER_POP;
3525 return target;
3528 /* Emit code to compute the one's complement absolute value of OP0
3529 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3530 (TARGET may be NULL_RTX.) The return value says where the result
3531 actually is to be found.
3533 MODE is the mode of the operand; the mode of the result is
3534 different but can be deduced from MODE. */
3537 expand_one_cmpl_abs_nojump (enum machine_mode mode, rtx op0, rtx target)
3539 rtx temp;
3541 /* Not applicable for floating point modes. */
3542 if (FLOAT_MODE_P (mode))
3543 return NULL_RTX;
3545 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3546 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3548 rtx_insn *last = get_last_insn ();
3550 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3551 if (temp != 0)
3552 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3553 OPTAB_WIDEN);
3555 if (temp != 0)
3556 return temp;
3558 delete_insns_since (last);
3561 /* If this machine has expensive jumps, we can do one's complement
3562 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3564 if (GET_MODE_CLASS (mode) == MODE_INT
3565 && BRANCH_COST (optimize_insn_for_speed_p (),
3566 false) >= 2)
3568 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3569 GET_MODE_PRECISION (mode) - 1,
3570 NULL_RTX, 0);
3572 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3573 OPTAB_LIB_WIDEN);
3575 if (temp != 0)
3576 return temp;
3579 return NULL_RTX;
3582 /* A subroutine of expand_copysign, perform the copysign operation using the
3583 abs and neg primitives advertised to exist on the target. The assumption
3584 is that we have a split register file, and leaving op0 in fp registers,
3585 and not playing with subregs so much, will help the register allocator. */
3587 static rtx
3588 expand_copysign_absneg (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3589 int bitpos, bool op0_is_abs)
3591 enum machine_mode imode;
3592 enum insn_code icode;
3593 rtx sign;
3594 rtx_code_label *label;
3596 if (target == op1)
3597 target = NULL_RTX;
3599 /* Check if the back end provides an insn that handles signbit for the
3600 argument's mode. */
3601 icode = optab_handler (signbit_optab, mode);
3602 if (icode != CODE_FOR_nothing)
3604 imode = insn_data[(int) icode].operand[0].mode;
3605 sign = gen_reg_rtx (imode);
3606 emit_unop_insn (icode, sign, op1, UNKNOWN);
3608 else
3610 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3612 imode = int_mode_for_mode (mode);
3613 if (imode == BLKmode)
3614 return NULL_RTX;
3615 op1 = gen_lowpart (imode, op1);
3617 else
3619 int word;
3621 imode = word_mode;
3622 if (FLOAT_WORDS_BIG_ENDIAN)
3623 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3624 else
3625 word = bitpos / BITS_PER_WORD;
3626 bitpos = bitpos % BITS_PER_WORD;
3627 op1 = operand_subword_force (op1, word, mode);
3630 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3631 sign = expand_binop (imode, and_optab, op1,
3632 immed_wide_int_const (mask, imode),
3633 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3636 if (!op0_is_abs)
3638 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3639 if (op0 == NULL)
3640 return NULL_RTX;
3641 target = op0;
3643 else
3645 if (target == NULL_RTX)
3646 target = copy_to_reg (op0);
3647 else
3648 emit_move_insn (target, op0);
3651 label = gen_label_rtx ();
3652 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3654 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3655 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3656 else
3657 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3658 if (op0 != target)
3659 emit_move_insn (target, op0);
3661 emit_label (label);
3663 return target;
3667 /* A subroutine of expand_copysign, perform the entire copysign operation
3668 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3669 is true if op0 is known to have its sign bit clear. */
3671 static rtx
3672 expand_copysign_bit (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3673 int bitpos, bool op0_is_abs)
3675 enum machine_mode imode;
3676 int word, nwords, i;
3677 rtx temp;
3678 rtx_insn *insns;
3680 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3682 imode = int_mode_for_mode (mode);
3683 if (imode == BLKmode)
3684 return NULL_RTX;
3685 word = 0;
3686 nwords = 1;
3688 else
3690 imode = word_mode;
3692 if (FLOAT_WORDS_BIG_ENDIAN)
3693 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3694 else
3695 word = bitpos / BITS_PER_WORD;
3696 bitpos = bitpos % BITS_PER_WORD;
3697 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3700 wide_int mask = wi::set_bit_in_zero (bitpos, GET_MODE_PRECISION (imode));
3702 if (target == 0
3703 || target == op0
3704 || target == op1
3705 || (nwords > 1 && !valid_multiword_target_p (target)))
3706 target = gen_reg_rtx (mode);
3708 if (nwords > 1)
3710 start_sequence ();
3712 for (i = 0; i < nwords; ++i)
3714 rtx targ_piece = operand_subword (target, i, 1, mode);
3715 rtx op0_piece = operand_subword_force (op0, i, mode);
3717 if (i == word)
3719 if (!op0_is_abs)
3720 op0_piece
3721 = expand_binop (imode, and_optab, op0_piece,
3722 immed_wide_int_const (~mask, imode),
3723 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3724 op1 = expand_binop (imode, and_optab,
3725 operand_subword_force (op1, i, mode),
3726 immed_wide_int_const (mask, imode),
3727 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3729 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3730 targ_piece, 1, OPTAB_LIB_WIDEN);
3731 if (temp != targ_piece)
3732 emit_move_insn (targ_piece, temp);
3734 else
3735 emit_move_insn (targ_piece, op0_piece);
3738 insns = get_insns ();
3739 end_sequence ();
3741 emit_insn (insns);
3743 else
3745 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3746 immed_wide_int_const (mask, imode),
3747 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3749 op0 = gen_lowpart (imode, op0);
3750 if (!op0_is_abs)
3751 op0 = expand_binop (imode, and_optab, op0,
3752 immed_wide_int_const (~mask, imode),
3753 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3755 temp = expand_binop (imode, ior_optab, op0, op1,
3756 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3757 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3760 return target;
3763 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3764 scalar floating point mode. Return NULL if we do not know how to
3765 expand the operation inline. */
3768 expand_copysign (rtx op0, rtx op1, rtx target)
3770 enum machine_mode mode = GET_MODE (op0);
3771 const struct real_format *fmt;
3772 bool op0_is_abs;
3773 rtx temp;
3775 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3776 gcc_assert (GET_MODE (op1) == mode);
3778 /* First try to do it with a special instruction. */
3779 temp = expand_binop (mode, copysign_optab, op0, op1,
3780 target, 0, OPTAB_DIRECT);
3781 if (temp)
3782 return temp;
3784 fmt = REAL_MODE_FORMAT (mode);
3785 if (fmt == NULL || !fmt->has_signed_zero)
3786 return NULL_RTX;
3788 op0_is_abs = false;
3789 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3791 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3792 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3793 op0_is_abs = true;
3796 if (fmt->signbit_ro >= 0
3797 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3798 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3799 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3801 temp = expand_copysign_absneg (mode, op0, op1, target,
3802 fmt->signbit_ro, op0_is_abs);
3803 if (temp)
3804 return temp;
3807 if (fmt->signbit_rw < 0)
3808 return NULL_RTX;
3809 return expand_copysign_bit (mode, op0, op1, target,
3810 fmt->signbit_rw, op0_is_abs);
3813 /* Generate an instruction whose insn-code is INSN_CODE,
3814 with two operands: an output TARGET and an input OP0.
3815 TARGET *must* be nonzero, and the output is always stored there.
3816 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3817 the value that is stored into TARGET.
3819 Return false if expansion failed. */
3821 bool
3822 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3823 enum rtx_code code)
3825 struct expand_operand ops[2];
3826 rtx pat;
3828 create_output_operand (&ops[0], target, GET_MODE (target));
3829 create_input_operand (&ops[1], op0, GET_MODE (op0));
3830 pat = maybe_gen_insn (icode, 2, ops);
3831 if (!pat)
3832 return false;
3834 if (INSN_P (pat) && NEXT_INSN (as_a <rtx_insn *> (pat)) != NULL_RTX
3835 && code != UNKNOWN)
3836 add_equal_note (as_a <rtx_insn *> (pat), ops[0].value, code, ops[1].value,
3837 NULL_RTX);
3839 emit_insn (pat);
3841 if (ops[0].value != target)
3842 emit_move_insn (target, ops[0].value);
3843 return true;
3845 /* Generate an instruction whose insn-code is INSN_CODE,
3846 with two operands: an output TARGET and an input OP0.
3847 TARGET *must* be nonzero, and the output is always stored there.
3848 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3849 the value that is stored into TARGET. */
3851 void
3852 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3854 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3855 gcc_assert (ok);
3858 struct no_conflict_data
3860 rtx target;
3861 rtx_insn *first, *insn;
3862 bool must_stay;
3865 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3866 the currently examined clobber / store has to stay in the list of
3867 insns that constitute the actual libcall block. */
3868 static void
3869 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3871 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3873 /* If this inns directly contributes to setting the target, it must stay. */
3874 if (reg_overlap_mentioned_p (p->target, dest))
3875 p->must_stay = true;
3876 /* If we haven't committed to keeping any other insns in the list yet,
3877 there is nothing more to check. */
3878 else if (p->insn == p->first)
3879 return;
3880 /* If this insn sets / clobbers a register that feeds one of the insns
3881 already in the list, this insn has to stay too. */
3882 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3883 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3884 || reg_used_between_p (dest, p->first, p->insn)
3885 /* Likewise if this insn depends on a register set by a previous
3886 insn in the list, or if it sets a result (presumably a hard
3887 register) that is set or clobbered by a previous insn.
3888 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3889 SET_DEST perform the former check on the address, and the latter
3890 check on the MEM. */
3891 || (GET_CODE (set) == SET
3892 && (modified_in_p (SET_SRC (set), p->first)
3893 || modified_in_p (SET_DEST (set), p->first)
3894 || modified_between_p (SET_SRC (set), p->first, p->insn)
3895 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3896 p->must_stay = true;
3900 /* Emit code to make a call to a constant function or a library call.
3902 INSNS is a list containing all insns emitted in the call.
3903 These insns leave the result in RESULT. Our block is to copy RESULT
3904 to TARGET, which is logically equivalent to EQUIV.
3906 We first emit any insns that set a pseudo on the assumption that these are
3907 loading constants into registers; doing so allows them to be safely cse'ed
3908 between blocks. Then we emit all the other insns in the block, followed by
3909 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3910 note with an operand of EQUIV. */
3912 static void
3913 emit_libcall_block_1 (rtx_insn *insns, rtx target, rtx result, rtx equiv,
3914 bool equiv_may_trap)
3916 rtx final_dest = target;
3917 rtx_insn *next, *last, *insn;
3919 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3920 into a MEM later. Protect the libcall block from this change. */
3921 if (! REG_P (target) || REG_USERVAR_P (target))
3922 target = gen_reg_rtx (GET_MODE (target));
3924 /* If we're using non-call exceptions, a libcall corresponding to an
3925 operation that may trap may also trap. */
3926 /* ??? See the comment in front of make_reg_eh_region_note. */
3927 if (cfun->can_throw_non_call_exceptions
3928 && (equiv_may_trap || may_trap_p (equiv)))
3930 for (insn = insns; insn; insn = NEXT_INSN (insn))
3931 if (CALL_P (insn))
3933 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3934 if (note)
3936 int lp_nr = INTVAL (XEXP (note, 0));
3937 if (lp_nr == 0 || lp_nr == INT_MIN)
3938 remove_note (insn, note);
3942 else
3944 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3945 reg note to indicate that this call cannot throw or execute a nonlocal
3946 goto (unless there is already a REG_EH_REGION note, in which case
3947 we update it). */
3948 for (insn = insns; insn; insn = NEXT_INSN (insn))
3949 if (CALL_P (insn))
3950 make_reg_eh_region_note_nothrow_nononlocal (insn);
3953 /* First emit all insns that set pseudos. Remove them from the list as
3954 we go. Avoid insns that set pseudos which were referenced in previous
3955 insns. These can be generated by move_by_pieces, for example,
3956 to update an address. Similarly, avoid insns that reference things
3957 set in previous insns. */
3959 for (insn = insns; insn; insn = next)
3961 rtx set = single_set (insn);
3963 next = NEXT_INSN (insn);
3965 if (set != 0 && REG_P (SET_DEST (set))
3966 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3968 struct no_conflict_data data;
3970 data.target = const0_rtx;
3971 data.first = insns;
3972 data.insn = insn;
3973 data.must_stay = 0;
3974 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3975 if (! data.must_stay)
3977 if (PREV_INSN (insn))
3978 SET_NEXT_INSN (PREV_INSN (insn)) = next;
3979 else
3980 insns = next;
3982 if (next)
3983 SET_PREV_INSN (next) = PREV_INSN (insn);
3985 add_insn (insn);
3989 /* Some ports use a loop to copy large arguments onto the stack.
3990 Don't move anything outside such a loop. */
3991 if (LABEL_P (insn))
3992 break;
3995 /* Write the remaining insns followed by the final copy. */
3996 for (insn = insns; insn; insn = next)
3998 next = NEXT_INSN (insn);
4000 add_insn (insn);
4003 last = emit_move_insn (target, result);
4004 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
4006 if (final_dest != target)
4007 emit_move_insn (final_dest, target);
4010 void
4011 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
4013 emit_libcall_block_1 (safe_as_a <rtx_insn *> (insns),
4014 target, result, equiv, false);
4017 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
4018 PURPOSE describes how this comparison will be used. CODE is the rtx
4019 comparison code we will be using.
4021 ??? Actually, CODE is slightly weaker than that. A target is still
4022 required to implement all of the normal bcc operations, but not
4023 required to implement all (or any) of the unordered bcc operations. */
4026 can_compare_p (enum rtx_code code, enum machine_mode mode,
4027 enum can_compare_purpose purpose)
4029 rtx test;
4030 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
4033 enum insn_code icode;
4035 if (purpose == ccp_jump
4036 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
4037 && insn_operand_matches (icode, 0, test))
4038 return 1;
4039 if (purpose == ccp_store_flag
4040 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
4041 && insn_operand_matches (icode, 1, test))
4042 return 1;
4043 if (purpose == ccp_cmov
4044 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
4045 return 1;
4047 mode = GET_MODE_WIDER_MODE (mode);
4048 PUT_MODE (test, mode);
4050 while (mode != VOIDmode);
4052 return 0;
4055 /* This function is called when we are going to emit a compare instruction that
4056 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
4058 *PMODE is the mode of the inputs (in case they are const_int).
4059 *PUNSIGNEDP nonzero says that the operands are unsigned;
4060 this matters if they need to be widened (as given by METHODS).
4062 If they have mode BLKmode, then SIZE specifies the size of both operands.
4064 This function performs all the setup necessary so that the caller only has
4065 to emit a single comparison insn. This setup can involve doing a BLKmode
4066 comparison or emitting a library call to perform the comparison if no insn
4067 is available to handle it.
4068 The values which are passed in through pointers can be modified; the caller
4069 should perform the comparison on the modified values. Constant
4070 comparisons must have already been folded. */
4072 static void
4073 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4074 int unsignedp, enum optab_methods methods,
4075 rtx *ptest, enum machine_mode *pmode)
4077 enum machine_mode mode = *pmode;
4078 rtx libfunc, test;
4079 enum machine_mode cmp_mode;
4080 enum mode_class mclass;
4082 /* The other methods are not needed. */
4083 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4084 || methods == OPTAB_LIB_WIDEN);
4086 /* If we are optimizing, force expensive constants into a register. */
4087 if (CONSTANT_P (x) && optimize
4088 && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
4089 > COSTS_N_INSNS (1)))
4090 x = force_reg (mode, x);
4092 if (CONSTANT_P (y) && optimize
4093 && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
4094 > COSTS_N_INSNS (1)))
4095 y = force_reg (mode, y);
4097 #ifdef HAVE_cc0
4098 /* Make sure if we have a canonical comparison. The RTL
4099 documentation states that canonical comparisons are required only
4100 for targets which have cc0. */
4101 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4102 #endif
4104 /* Don't let both operands fail to indicate the mode. */
4105 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4106 x = force_reg (mode, x);
4107 if (mode == VOIDmode)
4108 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4110 /* Handle all BLKmode compares. */
4112 if (mode == BLKmode)
4114 enum machine_mode result_mode;
4115 enum insn_code cmp_code;
4116 tree length_type;
4117 rtx libfunc;
4118 rtx result;
4119 rtx opalign
4120 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4122 gcc_assert (size);
4124 /* Try to use a memory block compare insn - either cmpstr
4125 or cmpmem will do. */
4126 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4127 cmp_mode != VOIDmode;
4128 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4130 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4131 if (cmp_code == CODE_FOR_nothing)
4132 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4133 if (cmp_code == CODE_FOR_nothing)
4134 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4135 if (cmp_code == CODE_FOR_nothing)
4136 continue;
4138 /* Must make sure the size fits the insn's mode. */
4139 if ((CONST_INT_P (size)
4140 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4141 || (GET_MODE_BITSIZE (GET_MODE (size))
4142 > GET_MODE_BITSIZE (cmp_mode)))
4143 continue;
4145 result_mode = insn_data[cmp_code].operand[0].mode;
4146 result = gen_reg_rtx (result_mode);
4147 size = convert_to_mode (cmp_mode, size, 1);
4148 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4150 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4151 *pmode = result_mode;
4152 return;
4155 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4156 goto fail;
4158 /* Otherwise call a library function, memcmp. */
4159 libfunc = memcmp_libfunc;
4160 length_type = sizetype;
4161 result_mode = TYPE_MODE (integer_type_node);
4162 cmp_mode = TYPE_MODE (length_type);
4163 size = convert_to_mode (TYPE_MODE (length_type), size,
4164 TYPE_UNSIGNED (length_type));
4166 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4167 result_mode, 3,
4168 XEXP (x, 0), Pmode,
4169 XEXP (y, 0), Pmode,
4170 size, cmp_mode);
4171 x = result;
4172 y = const0_rtx;
4173 mode = result_mode;
4174 methods = OPTAB_LIB_WIDEN;
4175 unsignedp = false;
4178 /* Don't allow operands to the compare to trap, as that can put the
4179 compare and branch in different basic blocks. */
4180 if (cfun->can_throw_non_call_exceptions)
4182 if (may_trap_p (x))
4183 x = force_reg (mode, x);
4184 if (may_trap_p (y))
4185 y = force_reg (mode, y);
4188 if (GET_MODE_CLASS (mode) == MODE_CC)
4190 gcc_assert (can_compare_p (comparison, CCmode, ccp_jump));
4191 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4192 return;
4195 mclass = GET_MODE_CLASS (mode);
4196 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4197 cmp_mode = mode;
4200 enum insn_code icode;
4201 icode = optab_handler (cbranch_optab, cmp_mode);
4202 if (icode != CODE_FOR_nothing
4203 && insn_operand_matches (icode, 0, test))
4205 rtx_insn *last = get_last_insn ();
4206 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4207 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4208 if (op0 && op1
4209 && insn_operand_matches (icode, 1, op0)
4210 && insn_operand_matches (icode, 2, op1))
4212 XEXP (test, 0) = op0;
4213 XEXP (test, 1) = op1;
4214 *ptest = test;
4215 *pmode = cmp_mode;
4216 return;
4218 delete_insns_since (last);
4221 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4222 break;
4223 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4225 while (cmp_mode != VOIDmode);
4227 if (methods != OPTAB_LIB_WIDEN)
4228 goto fail;
4230 if (!SCALAR_FLOAT_MODE_P (mode))
4232 rtx result;
4233 enum machine_mode ret_mode;
4235 /* Handle a libcall just for the mode we are using. */
4236 libfunc = optab_libfunc (cmp_optab, mode);
4237 gcc_assert (libfunc);
4239 /* If we want unsigned, and this mode has a distinct unsigned
4240 comparison routine, use that. */
4241 if (unsignedp)
4243 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4244 if (ulibfunc)
4245 libfunc = ulibfunc;
4248 ret_mode = targetm.libgcc_cmp_return_mode ();
4249 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4250 ret_mode, 2, x, mode, y, mode);
4252 /* There are two kinds of comparison routines. Biased routines
4253 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4254 of gcc expect that the comparison operation is equivalent
4255 to the modified comparison. For signed comparisons compare the
4256 result against 1 in the biased case, and zero in the unbiased
4257 case. For unsigned comparisons always compare against 1 after
4258 biasing the unbiased result by adding 1. This gives us a way to
4259 represent LTU.
4260 The comparisons in the fixed-point helper library are always
4261 biased. */
4262 x = result;
4263 y = const1_rtx;
4265 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4267 if (unsignedp)
4268 x = plus_constant (ret_mode, result, 1);
4269 else
4270 y = const0_rtx;
4273 *pmode = word_mode;
4274 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4275 ptest, pmode);
4277 else
4278 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4280 return;
4282 fail:
4283 *ptest = NULL_RTX;
4286 /* Before emitting an insn with code ICODE, make sure that X, which is going
4287 to be used for operand OPNUM of the insn, is converted from mode MODE to
4288 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4289 that it is accepted by the operand predicate. Return the new value. */
4292 prepare_operand (enum insn_code icode, rtx x, int opnum, enum machine_mode mode,
4293 enum machine_mode wider_mode, int unsignedp)
4295 if (mode != wider_mode)
4296 x = convert_modes (wider_mode, mode, x, unsignedp);
4298 if (!insn_operand_matches (icode, opnum, x))
4300 if (reload_completed)
4301 return NULL_RTX;
4302 x = copy_to_mode_reg (insn_data[(int) icode].operand[opnum].mode, x);
4305 return x;
4308 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4309 we can do the branch. */
4311 static void
4312 emit_cmp_and_jump_insn_1 (rtx test, enum machine_mode mode, rtx label, int prob)
4314 enum machine_mode optab_mode;
4315 enum mode_class mclass;
4316 enum insn_code icode;
4317 rtx_insn *insn;
4319 mclass = GET_MODE_CLASS (mode);
4320 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4321 icode = optab_handler (cbranch_optab, optab_mode);
4323 gcc_assert (icode != CODE_FOR_nothing);
4324 gcc_assert (insn_operand_matches (icode, 0, test));
4325 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4326 XEXP (test, 1), label));
4327 if (prob != -1
4328 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4329 && insn
4330 && JUMP_P (insn)
4331 && any_condjump_p (insn)
4332 && !find_reg_note (insn, REG_BR_PROB, 0))
4333 add_int_reg_note (insn, REG_BR_PROB, prob);
4336 /* Generate code to compare X with Y so that the condition codes are
4337 set and to jump to LABEL if the condition is true. If X is a
4338 constant and Y is not a constant, then the comparison is swapped to
4339 ensure that the comparison RTL has the canonical form.
4341 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4342 need to be widened. UNSIGNEDP is also used to select the proper
4343 branch condition code.
4345 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4347 MODE is the mode of the inputs (in case they are const_int).
4349 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4350 It will be potentially converted into an unsigned variant based on
4351 UNSIGNEDP to select a proper jump instruction.
4353 PROB is the probability of jumping to LABEL. */
4355 void
4356 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4357 enum machine_mode mode, int unsignedp, rtx label,
4358 int prob)
4360 rtx op0 = x, op1 = y;
4361 rtx test;
4363 /* Swap operands and condition to ensure canonical RTL. */
4364 if (swap_commutative_operands_p (x, y)
4365 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4367 op0 = y, op1 = x;
4368 comparison = swap_condition (comparison);
4371 /* If OP0 is still a constant, then both X and Y must be constants
4372 or the opposite comparison is not supported. Force X into a register
4373 to create canonical RTL. */
4374 if (CONSTANT_P (op0))
4375 op0 = force_reg (mode, op0);
4377 if (unsignedp)
4378 comparison = unsigned_condition (comparison);
4380 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4381 &test, &mode);
4382 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4386 /* Emit a library call comparison between floating point X and Y.
4387 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4389 static void
4390 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4391 rtx *ptest, enum machine_mode *pmode)
4393 enum rtx_code swapped = swap_condition (comparison);
4394 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4395 enum machine_mode orig_mode = GET_MODE (x);
4396 enum machine_mode mode, cmp_mode;
4397 rtx true_rtx, false_rtx;
4398 rtx value, target, equiv;
4399 rtx_insn *insns;
4400 rtx libfunc = 0;
4401 bool reversed_p = false;
4402 cmp_mode = targetm.libgcc_cmp_return_mode ();
4404 for (mode = orig_mode;
4405 mode != VOIDmode;
4406 mode = GET_MODE_WIDER_MODE (mode))
4408 if (code_to_optab (comparison)
4409 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4410 break;
4412 if (code_to_optab (swapped)
4413 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4415 rtx tmp;
4416 tmp = x; x = y; y = tmp;
4417 comparison = swapped;
4418 break;
4421 if (code_to_optab (reversed)
4422 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4424 comparison = reversed;
4425 reversed_p = true;
4426 break;
4430 gcc_assert (mode != VOIDmode);
4432 if (mode != orig_mode)
4434 x = convert_to_mode (mode, x, 0);
4435 y = convert_to_mode (mode, y, 0);
4438 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4439 the RTL. The allows the RTL optimizers to delete the libcall if the
4440 condition can be determined at compile-time. */
4441 if (comparison == UNORDERED
4442 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4444 true_rtx = const_true_rtx;
4445 false_rtx = const0_rtx;
4447 else
4449 switch (comparison)
4451 case EQ:
4452 true_rtx = const0_rtx;
4453 false_rtx = const_true_rtx;
4454 break;
4456 case NE:
4457 true_rtx = const_true_rtx;
4458 false_rtx = const0_rtx;
4459 break;
4461 case GT:
4462 true_rtx = const1_rtx;
4463 false_rtx = const0_rtx;
4464 break;
4466 case GE:
4467 true_rtx = const0_rtx;
4468 false_rtx = constm1_rtx;
4469 break;
4471 case LT:
4472 true_rtx = constm1_rtx;
4473 false_rtx = const0_rtx;
4474 break;
4476 case LE:
4477 true_rtx = const0_rtx;
4478 false_rtx = const1_rtx;
4479 break;
4481 default:
4482 gcc_unreachable ();
4486 if (comparison == UNORDERED)
4488 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4489 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4490 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4491 temp, const_true_rtx, equiv);
4493 else
4495 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4496 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4497 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4498 equiv, true_rtx, false_rtx);
4501 start_sequence ();
4502 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4503 cmp_mode, 2, x, mode, y, mode);
4504 insns = get_insns ();
4505 end_sequence ();
4507 target = gen_reg_rtx (cmp_mode);
4508 emit_libcall_block (insns, target, value, equiv);
4510 if (comparison == UNORDERED
4511 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4512 || reversed_p)
4513 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4514 else
4515 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4517 *pmode = cmp_mode;
4520 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4522 void
4523 emit_indirect_jump (rtx loc)
4525 struct expand_operand ops[1];
4527 create_address_operand (&ops[0], loc);
4528 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4529 emit_barrier ();
4532 #ifdef HAVE_conditional_move
4534 /* Emit a conditional move instruction if the machine supports one for that
4535 condition and machine mode.
4537 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4538 the mode to use should they be constants. If it is VOIDmode, they cannot
4539 both be constants.
4541 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4542 should be stored there. MODE is the mode to use should they be constants.
4543 If it is VOIDmode, they cannot both be constants.
4545 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4546 is not supported. */
4549 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4550 enum machine_mode cmode, rtx op2, rtx op3,
4551 enum machine_mode mode, int unsignedp)
4553 rtx tem, comparison;
4554 rtx_insn *last;
4555 enum insn_code icode;
4556 enum rtx_code reversed;
4558 /* If one operand is constant, make it the second one. Only do this
4559 if the other operand is not constant as well. */
4561 if (swap_commutative_operands_p (op0, op1))
4563 tem = op0;
4564 op0 = op1;
4565 op1 = tem;
4566 code = swap_condition (code);
4569 /* get_condition will prefer to generate LT and GT even if the old
4570 comparison was against zero, so undo that canonicalization here since
4571 comparisons against zero are cheaper. */
4572 if (code == LT && op1 == const1_rtx)
4573 code = LE, op1 = const0_rtx;
4574 else if (code == GT && op1 == constm1_rtx)
4575 code = GE, op1 = const0_rtx;
4577 if (cmode == VOIDmode)
4578 cmode = GET_MODE (op0);
4580 if (swap_commutative_operands_p (op2, op3)
4581 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4582 != UNKNOWN))
4584 tem = op2;
4585 op2 = op3;
4586 op3 = tem;
4587 code = reversed;
4590 if (mode == VOIDmode)
4591 mode = GET_MODE (op2);
4593 icode = direct_optab_handler (movcc_optab, mode);
4595 if (icode == CODE_FOR_nothing)
4596 return 0;
4598 if (!target)
4599 target = gen_reg_rtx (mode);
4601 code = unsignedp ? unsigned_condition (code) : code;
4602 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4604 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4605 return NULL and let the caller figure out how best to deal with this
4606 situation. */
4607 if (!COMPARISON_P (comparison))
4608 return NULL_RTX;
4610 saved_pending_stack_adjust save;
4611 save_pending_stack_adjust (&save);
4612 last = get_last_insn ();
4613 do_pending_stack_adjust ();
4614 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4615 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4616 &comparison, &cmode);
4617 if (comparison)
4619 struct expand_operand ops[4];
4621 create_output_operand (&ops[0], target, mode);
4622 create_fixed_operand (&ops[1], comparison);
4623 create_input_operand (&ops[2], op2, mode);
4624 create_input_operand (&ops[3], op3, mode);
4625 if (maybe_expand_insn (icode, 4, ops))
4627 if (ops[0].value != target)
4628 convert_move (target, ops[0].value, false);
4629 return target;
4632 delete_insns_since (last);
4633 restore_pending_stack_adjust (&save);
4634 return NULL_RTX;
4637 /* Return nonzero if a conditional move of mode MODE is supported.
4639 This function is for combine so it can tell whether an insn that looks
4640 like a conditional move is actually supported by the hardware. If we
4641 guess wrong we lose a bit on optimization, but that's it. */
4642 /* ??? sparc64 supports conditionally moving integers values based on fp
4643 comparisons, and vice versa. How do we handle them? */
4646 can_conditionally_move_p (enum machine_mode mode)
4648 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4649 return 1;
4651 return 0;
4654 #endif /* HAVE_conditional_move */
4656 /* Emit a conditional addition instruction if the machine supports one for that
4657 condition and machine mode.
4659 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4660 the mode to use should they be constants. If it is VOIDmode, they cannot
4661 both be constants.
4663 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4664 should be stored there. MODE is the mode to use should they be constants.
4665 If it is VOIDmode, they cannot both be constants.
4667 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4668 is not supported. */
4671 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4672 enum machine_mode cmode, rtx op2, rtx op3,
4673 enum machine_mode mode, int unsignedp)
4675 rtx tem, comparison;
4676 rtx_insn *last;
4677 enum insn_code icode;
4679 /* If one operand is constant, make it the second one. Only do this
4680 if the other operand is not constant as well. */
4682 if (swap_commutative_operands_p (op0, op1))
4684 tem = op0;
4685 op0 = op1;
4686 op1 = tem;
4687 code = swap_condition (code);
4690 /* get_condition will prefer to generate LT and GT even if the old
4691 comparison was against zero, so undo that canonicalization here since
4692 comparisons against zero are cheaper. */
4693 if (code == LT && op1 == const1_rtx)
4694 code = LE, op1 = const0_rtx;
4695 else if (code == GT && op1 == constm1_rtx)
4696 code = GE, op1 = const0_rtx;
4698 if (cmode == VOIDmode)
4699 cmode = GET_MODE (op0);
4701 if (mode == VOIDmode)
4702 mode = GET_MODE (op2);
4704 icode = optab_handler (addcc_optab, mode);
4706 if (icode == CODE_FOR_nothing)
4707 return 0;
4709 if (!target)
4710 target = gen_reg_rtx (mode);
4712 code = unsignedp ? unsigned_condition (code) : code;
4713 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4715 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4716 return NULL and let the caller figure out how best to deal with this
4717 situation. */
4718 if (!COMPARISON_P (comparison))
4719 return NULL_RTX;
4721 do_pending_stack_adjust ();
4722 last = get_last_insn ();
4723 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4724 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4725 &comparison, &cmode);
4726 if (comparison)
4728 struct expand_operand ops[4];
4730 create_output_operand (&ops[0], target, mode);
4731 create_fixed_operand (&ops[1], comparison);
4732 create_input_operand (&ops[2], op2, mode);
4733 create_input_operand (&ops[3], op3, mode);
4734 if (maybe_expand_insn (icode, 4, ops))
4736 if (ops[0].value != target)
4737 convert_move (target, ops[0].value, false);
4738 return target;
4741 delete_insns_since (last);
4742 return NULL_RTX;
4745 /* These functions attempt to generate an insn body, rather than
4746 emitting the insn, but if the gen function already emits them, we
4747 make no attempt to turn them back into naked patterns. */
4749 /* Generate and return an insn body to add Y to X. */
4752 gen_add2_insn (rtx x, rtx y)
4754 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4756 gcc_assert (insn_operand_matches (icode, 0, x));
4757 gcc_assert (insn_operand_matches (icode, 1, x));
4758 gcc_assert (insn_operand_matches (icode, 2, y));
4760 return GEN_FCN (icode) (x, x, y);
4763 /* Generate and return an insn body to add r1 and c,
4764 storing the result in r0. */
4767 gen_add3_insn (rtx r0, rtx r1, rtx c)
4769 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4771 if (icode == CODE_FOR_nothing
4772 || !insn_operand_matches (icode, 0, r0)
4773 || !insn_operand_matches (icode, 1, r1)
4774 || !insn_operand_matches (icode, 2, c))
4775 return NULL_RTX;
4777 return GEN_FCN (icode) (r0, r1, c);
4781 have_add2_insn (rtx x, rtx y)
4783 enum insn_code icode;
4785 gcc_assert (GET_MODE (x) != VOIDmode);
4787 icode = optab_handler (add_optab, GET_MODE (x));
4789 if (icode == CODE_FOR_nothing)
4790 return 0;
4792 if (!insn_operand_matches (icode, 0, x)
4793 || !insn_operand_matches (icode, 1, x)
4794 || !insn_operand_matches (icode, 2, y))
4795 return 0;
4797 return 1;
4800 /* Generate and return an insn body to add Y to X. */
4803 gen_addptr3_insn (rtx x, rtx y, rtx z)
4805 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4807 gcc_assert (insn_operand_matches (icode, 0, x));
4808 gcc_assert (insn_operand_matches (icode, 1, y));
4809 gcc_assert (insn_operand_matches (icode, 2, z));
4811 return GEN_FCN (icode) (x, y, z);
4814 /* Return true if the target implements an addptr pattern and X, Y,
4815 and Z are valid for the pattern predicates. */
4818 have_addptr3_insn (rtx x, rtx y, rtx z)
4820 enum insn_code icode;
4822 gcc_assert (GET_MODE (x) != VOIDmode);
4824 icode = optab_handler (addptr3_optab, GET_MODE (x));
4826 if (icode == CODE_FOR_nothing)
4827 return 0;
4829 if (!insn_operand_matches (icode, 0, x)
4830 || !insn_operand_matches (icode, 1, y)
4831 || !insn_operand_matches (icode, 2, z))
4832 return 0;
4834 return 1;
4837 /* Generate and return an insn body to subtract Y from X. */
4840 gen_sub2_insn (rtx x, rtx y)
4842 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4844 gcc_assert (insn_operand_matches (icode, 0, x));
4845 gcc_assert (insn_operand_matches (icode, 1, x));
4846 gcc_assert (insn_operand_matches (icode, 2, y));
4848 return GEN_FCN (icode) (x, x, y);
4851 /* Generate and return an insn body to subtract r1 and c,
4852 storing the result in r0. */
4855 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4857 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4859 if (icode == CODE_FOR_nothing
4860 || !insn_operand_matches (icode, 0, r0)
4861 || !insn_operand_matches (icode, 1, r1)
4862 || !insn_operand_matches (icode, 2, c))
4863 return NULL_RTX;
4865 return GEN_FCN (icode) (r0, r1, c);
4869 have_sub2_insn (rtx x, rtx y)
4871 enum insn_code icode;
4873 gcc_assert (GET_MODE (x) != VOIDmode);
4875 icode = optab_handler (sub_optab, GET_MODE (x));
4877 if (icode == CODE_FOR_nothing)
4878 return 0;
4880 if (!insn_operand_matches (icode, 0, x)
4881 || !insn_operand_matches (icode, 1, x)
4882 || !insn_operand_matches (icode, 2, y))
4883 return 0;
4885 return 1;
4888 /* Generate the body of an instruction to copy Y into X.
4889 It may be a list of insns, if one insn isn't enough. */
4892 gen_move_insn (rtx x, rtx y)
4894 rtx_insn *seq;
4896 start_sequence ();
4897 emit_move_insn_1 (x, y);
4898 seq = get_insns ();
4899 end_sequence ();
4900 return seq;
4903 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4904 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4905 no such operation exists, CODE_FOR_nothing will be returned. */
4907 enum insn_code
4908 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
4909 int unsignedp)
4911 convert_optab tab;
4912 #ifdef HAVE_ptr_extend
4913 if (unsignedp < 0)
4914 return CODE_FOR_ptr_extend;
4915 #endif
4917 tab = unsignedp ? zext_optab : sext_optab;
4918 return convert_optab_handler (tab, to_mode, from_mode);
4921 /* Generate the body of an insn to extend Y (with mode MFROM)
4922 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4925 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4926 enum machine_mode mfrom, int unsignedp)
4928 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4929 return GEN_FCN (icode) (x, y);
4932 /* can_fix_p and can_float_p say whether the target machine
4933 can directly convert a given fixed point type to
4934 a given floating point type, or vice versa.
4935 The returned value is the CODE_FOR_... value to use,
4936 or CODE_FOR_nothing if these modes cannot be directly converted.
4938 *TRUNCP_PTR is set to 1 if it is necessary to output
4939 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4941 static enum insn_code
4942 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4943 int unsignedp, int *truncp_ptr)
4945 convert_optab tab;
4946 enum insn_code icode;
4948 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4949 icode = convert_optab_handler (tab, fixmode, fltmode);
4950 if (icode != CODE_FOR_nothing)
4952 *truncp_ptr = 0;
4953 return icode;
4956 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4957 for this to work. We need to rework the fix* and ftrunc* patterns
4958 and documentation. */
4959 tab = unsignedp ? ufix_optab : sfix_optab;
4960 icode = convert_optab_handler (tab, fixmode, fltmode);
4961 if (icode != CODE_FOR_nothing
4962 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4964 *truncp_ptr = 1;
4965 return icode;
4968 *truncp_ptr = 0;
4969 return CODE_FOR_nothing;
4972 enum insn_code
4973 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4974 int unsignedp)
4976 convert_optab tab;
4978 tab = unsignedp ? ufloat_optab : sfloat_optab;
4979 return convert_optab_handler (tab, fltmode, fixmode);
4982 /* Function supportable_convert_operation
4984 Check whether an operation represented by the code CODE is a
4985 convert operation that is supported by the target platform in
4986 vector form (i.e., when operating on arguments of type VECTYPE_IN
4987 producing a result of type VECTYPE_OUT).
4989 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4990 This function checks if these operations are supported
4991 by the target platform either directly (via vector tree-codes), or via
4992 target builtins.
4994 Output:
4995 - CODE1 is code of vector operation to be used when
4996 vectorizing the operation, if available.
4997 - DECL is decl of target builtin functions to be used
4998 when vectorizing the operation, if available. In this case,
4999 CODE1 is CALL_EXPR. */
5001 bool
5002 supportable_convert_operation (enum tree_code code,
5003 tree vectype_out, tree vectype_in,
5004 tree *decl, enum tree_code *code1)
5006 enum machine_mode m1,m2;
5007 int truncp;
5009 m1 = TYPE_MODE (vectype_out);
5010 m2 = TYPE_MODE (vectype_in);
5012 /* First check if we can done conversion directly. */
5013 if ((code == FIX_TRUNC_EXPR
5014 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
5015 != CODE_FOR_nothing)
5016 || (code == FLOAT_EXPR
5017 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
5018 != CODE_FOR_nothing))
5020 *code1 = code;
5021 return true;
5024 /* Now check for builtin. */
5025 if (targetm.vectorize.builtin_conversion
5026 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
5028 *code1 = CALL_EXPR;
5029 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
5030 return true;
5032 return false;
5036 /* Generate code to convert FROM to floating point
5037 and store in TO. FROM must be fixed point and not VOIDmode.
5038 UNSIGNEDP nonzero means regard FROM as unsigned.
5039 Normally this is done by correcting the final value
5040 if it is negative. */
5042 void
5043 expand_float (rtx to, rtx from, int unsignedp)
5045 enum insn_code icode;
5046 rtx target = to;
5047 enum machine_mode fmode, imode;
5048 bool can_do_signed = false;
5050 /* Crash now, because we won't be able to decide which mode to use. */
5051 gcc_assert (GET_MODE (from) != VOIDmode);
5053 /* Look for an insn to do the conversion. Do it in the specified
5054 modes if possible; otherwise convert either input, output or both to
5055 wider mode. If the integer mode is wider than the mode of FROM,
5056 we can do the conversion signed even if the input is unsigned. */
5058 for (fmode = GET_MODE (to); fmode != VOIDmode;
5059 fmode = GET_MODE_WIDER_MODE (fmode))
5060 for (imode = GET_MODE (from); imode != VOIDmode;
5061 imode = GET_MODE_WIDER_MODE (imode))
5063 int doing_unsigned = unsignedp;
5065 if (fmode != GET_MODE (to)
5066 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
5067 continue;
5069 icode = can_float_p (fmode, imode, unsignedp);
5070 if (icode == CODE_FOR_nothing && unsignedp)
5072 enum insn_code scode = can_float_p (fmode, imode, 0);
5073 if (scode != CODE_FOR_nothing)
5074 can_do_signed = true;
5075 if (imode != GET_MODE (from))
5076 icode = scode, doing_unsigned = 0;
5079 if (icode != CODE_FOR_nothing)
5081 if (imode != GET_MODE (from))
5082 from = convert_to_mode (imode, from, unsignedp);
5084 if (fmode != GET_MODE (to))
5085 target = gen_reg_rtx (fmode);
5087 emit_unop_insn (icode, target, from,
5088 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
5090 if (target != to)
5091 convert_move (to, target, 0);
5092 return;
5096 /* Unsigned integer, and no way to convert directly. Convert as signed,
5097 then unconditionally adjust the result. */
5098 if (unsignedp && can_do_signed)
5100 rtx_code_label *label = gen_label_rtx ();
5101 rtx temp;
5102 REAL_VALUE_TYPE offset;
5104 /* Look for a usable floating mode FMODE wider than the source and at
5105 least as wide as the target. Using FMODE will avoid rounding woes
5106 with unsigned values greater than the signed maximum value. */
5108 for (fmode = GET_MODE (to); fmode != VOIDmode;
5109 fmode = GET_MODE_WIDER_MODE (fmode))
5110 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
5111 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
5112 break;
5114 if (fmode == VOIDmode)
5116 /* There is no such mode. Pretend the target is wide enough. */
5117 fmode = GET_MODE (to);
5119 /* Avoid double-rounding when TO is narrower than FROM. */
5120 if ((significand_size (fmode) + 1)
5121 < GET_MODE_PRECISION (GET_MODE (from)))
5123 rtx temp1;
5124 rtx_code_label *neglabel = gen_label_rtx ();
5126 /* Don't use TARGET if it isn't a register, is a hard register,
5127 or is the wrong mode. */
5128 if (!REG_P (target)
5129 || REGNO (target) < FIRST_PSEUDO_REGISTER
5130 || GET_MODE (target) != fmode)
5131 target = gen_reg_rtx (fmode);
5133 imode = GET_MODE (from);
5134 do_pending_stack_adjust ();
5136 /* Test whether the sign bit is set. */
5137 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5138 0, neglabel);
5140 /* The sign bit is not set. Convert as signed. */
5141 expand_float (target, from, 0);
5142 emit_jump_insn (gen_jump (label));
5143 emit_barrier ();
5145 /* The sign bit is set.
5146 Convert to a usable (positive signed) value by shifting right
5147 one bit, while remembering if a nonzero bit was shifted
5148 out; i.e., compute (from & 1) | (from >> 1). */
5150 emit_label (neglabel);
5151 temp = expand_binop (imode, and_optab, from, const1_rtx,
5152 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5153 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5154 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5155 OPTAB_LIB_WIDEN);
5156 expand_float (target, temp, 0);
5158 /* Multiply by 2 to undo the shift above. */
5159 temp = expand_binop (fmode, add_optab, target, target,
5160 target, 0, OPTAB_LIB_WIDEN);
5161 if (temp != target)
5162 emit_move_insn (target, temp);
5164 do_pending_stack_adjust ();
5165 emit_label (label);
5166 goto done;
5170 /* If we are about to do some arithmetic to correct for an
5171 unsigned operand, do it in a pseudo-register. */
5173 if (GET_MODE (to) != fmode
5174 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5175 target = gen_reg_rtx (fmode);
5177 /* Convert as signed integer to floating. */
5178 expand_float (target, from, 0);
5180 /* If FROM is negative (and therefore TO is negative),
5181 correct its value by 2**bitwidth. */
5183 do_pending_stack_adjust ();
5184 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5185 0, label);
5188 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5189 temp = expand_binop (fmode, add_optab, target,
5190 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5191 target, 0, OPTAB_LIB_WIDEN);
5192 if (temp != target)
5193 emit_move_insn (target, temp);
5195 do_pending_stack_adjust ();
5196 emit_label (label);
5197 goto done;
5200 /* No hardware instruction available; call a library routine. */
5202 rtx libfunc;
5203 rtx_insn *insns;
5204 rtx value;
5205 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5207 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_PRECISION (SImode))
5208 from = convert_to_mode (SImode, from, unsignedp);
5210 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5211 gcc_assert (libfunc);
5213 start_sequence ();
5215 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5216 GET_MODE (to), 1, from,
5217 GET_MODE (from));
5218 insns = get_insns ();
5219 end_sequence ();
5221 emit_libcall_block (insns, target, value,
5222 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5223 GET_MODE (to), from));
5226 done:
5228 /* Copy result to requested destination
5229 if we have been computing in a temp location. */
5231 if (target != to)
5233 if (GET_MODE (target) == GET_MODE (to))
5234 emit_move_insn (to, target);
5235 else
5236 convert_move (to, target, 0);
5240 /* Generate code to convert FROM to fixed point and store in TO. FROM
5241 must be floating point. */
5243 void
5244 expand_fix (rtx to, rtx from, int unsignedp)
5246 enum insn_code icode;
5247 rtx target = to;
5248 enum machine_mode fmode, imode;
5249 int must_trunc = 0;
5251 /* We first try to find a pair of modes, one real and one integer, at
5252 least as wide as FROM and TO, respectively, in which we can open-code
5253 this conversion. If the integer mode is wider than the mode of TO,
5254 we can do the conversion either signed or unsigned. */
5256 for (fmode = GET_MODE (from); fmode != VOIDmode;
5257 fmode = GET_MODE_WIDER_MODE (fmode))
5258 for (imode = GET_MODE (to); imode != VOIDmode;
5259 imode = GET_MODE_WIDER_MODE (imode))
5261 int doing_unsigned = unsignedp;
5263 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5264 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5265 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5267 if (icode != CODE_FOR_nothing)
5269 rtx_insn *last = get_last_insn ();
5270 if (fmode != GET_MODE (from))
5271 from = convert_to_mode (fmode, from, 0);
5273 if (must_trunc)
5275 rtx temp = gen_reg_rtx (GET_MODE (from));
5276 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5277 temp, 0);
5280 if (imode != GET_MODE (to))
5281 target = gen_reg_rtx (imode);
5283 if (maybe_emit_unop_insn (icode, target, from,
5284 doing_unsigned ? UNSIGNED_FIX : FIX))
5286 if (target != to)
5287 convert_move (to, target, unsignedp);
5288 return;
5290 delete_insns_since (last);
5294 /* For an unsigned conversion, there is one more way to do it.
5295 If we have a signed conversion, we generate code that compares
5296 the real value to the largest representable positive number. If if
5297 is smaller, the conversion is done normally. Otherwise, subtract
5298 one plus the highest signed number, convert, and add it back.
5300 We only need to check all real modes, since we know we didn't find
5301 anything with a wider integer mode.
5303 This code used to extend FP value into mode wider than the destination.
5304 This is needed for decimal float modes which cannot accurately
5305 represent one plus the highest signed number of the same size, but
5306 not for binary modes. Consider, for instance conversion from SFmode
5307 into DImode.
5309 The hot path through the code is dealing with inputs smaller than 2^63
5310 and doing just the conversion, so there is no bits to lose.
5312 In the other path we know the value is positive in the range 2^63..2^64-1
5313 inclusive. (as for other input overflow happens and result is undefined)
5314 So we know that the most important bit set in mantissa corresponds to
5315 2^63. The subtraction of 2^63 should not generate any rounding as it
5316 simply clears out that bit. The rest is trivial. */
5318 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5319 for (fmode = GET_MODE (from); fmode != VOIDmode;
5320 fmode = GET_MODE_WIDER_MODE (fmode))
5321 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5322 && (!DECIMAL_FLOAT_MODE_P (fmode)
5323 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5325 int bitsize;
5326 REAL_VALUE_TYPE offset;
5327 rtx limit;
5328 rtx_code_label *lab1, *lab2;
5329 rtx_insn *insn;
5331 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5332 real_2expN (&offset, bitsize - 1, fmode);
5333 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5334 lab1 = gen_label_rtx ();
5335 lab2 = gen_label_rtx ();
5337 if (fmode != GET_MODE (from))
5338 from = convert_to_mode (fmode, from, 0);
5340 /* See if we need to do the subtraction. */
5341 do_pending_stack_adjust ();
5342 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5343 0, lab1);
5345 /* If not, do the signed "fix" and branch around fixup code. */
5346 expand_fix (to, from, 0);
5347 emit_jump_insn (gen_jump (lab2));
5348 emit_barrier ();
5350 /* Otherwise, subtract 2**(N-1), convert to signed number,
5351 then add 2**(N-1). Do the addition using XOR since this
5352 will often generate better code. */
5353 emit_label (lab1);
5354 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5355 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5356 expand_fix (to, target, 0);
5357 target = expand_binop (GET_MODE (to), xor_optab, to,
5358 gen_int_mode
5359 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5360 GET_MODE (to)),
5361 to, 1, OPTAB_LIB_WIDEN);
5363 if (target != to)
5364 emit_move_insn (to, target);
5366 emit_label (lab2);
5368 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5370 /* Make a place for a REG_NOTE and add it. */
5371 insn = emit_move_insn (to, to);
5372 set_dst_reg_note (insn, REG_EQUAL,
5373 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5374 copy_rtx (from)),
5375 to);
5378 return;
5381 /* We can't do it with an insn, so use a library call. But first ensure
5382 that the mode of TO is at least as wide as SImode, since those are the
5383 only library calls we know about. */
5385 if (GET_MODE_PRECISION (GET_MODE (to)) < GET_MODE_PRECISION (SImode))
5387 target = gen_reg_rtx (SImode);
5389 expand_fix (target, from, unsignedp);
5391 else
5393 rtx_insn *insns;
5394 rtx value;
5395 rtx libfunc;
5397 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5398 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5399 gcc_assert (libfunc);
5401 start_sequence ();
5403 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5404 GET_MODE (to), 1, from,
5405 GET_MODE (from));
5406 insns = get_insns ();
5407 end_sequence ();
5409 emit_libcall_block (insns, target, value,
5410 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5411 GET_MODE (to), from));
5414 if (target != to)
5416 if (GET_MODE (to) == GET_MODE (target))
5417 emit_move_insn (to, target);
5418 else
5419 convert_move (to, target, 0);
5423 /* Generate code to convert FROM or TO a fixed-point.
5424 If UINTP is true, either TO or FROM is an unsigned integer.
5425 If SATP is true, we need to saturate the result. */
5427 void
5428 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5430 enum machine_mode to_mode = GET_MODE (to);
5431 enum machine_mode from_mode = GET_MODE (from);
5432 convert_optab tab;
5433 enum rtx_code this_code;
5434 enum insn_code code;
5435 rtx_insn *insns;
5436 rtx value;
5437 rtx libfunc;
5439 if (to_mode == from_mode)
5441 emit_move_insn (to, from);
5442 return;
5445 if (uintp)
5447 tab = satp ? satfractuns_optab : fractuns_optab;
5448 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5450 else
5452 tab = satp ? satfract_optab : fract_optab;
5453 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5455 code = convert_optab_handler (tab, to_mode, from_mode);
5456 if (code != CODE_FOR_nothing)
5458 emit_unop_insn (code, to, from, this_code);
5459 return;
5462 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5463 gcc_assert (libfunc);
5465 start_sequence ();
5466 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5467 1, from, from_mode);
5468 insns = get_insns ();
5469 end_sequence ();
5471 emit_libcall_block (insns, to, value,
5472 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5475 /* Generate code to convert FROM to fixed point and store in TO. FROM
5476 must be floating point, TO must be signed. Use the conversion optab
5477 TAB to do the conversion. */
5479 bool
5480 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5482 enum insn_code icode;
5483 rtx target = to;
5484 enum machine_mode fmode, imode;
5486 /* We first try to find a pair of modes, one real and one integer, at
5487 least as wide as FROM and TO, respectively, in which we can open-code
5488 this conversion. If the integer mode is wider than the mode of TO,
5489 we can do the conversion either signed or unsigned. */
5491 for (fmode = GET_MODE (from); fmode != VOIDmode;
5492 fmode = GET_MODE_WIDER_MODE (fmode))
5493 for (imode = GET_MODE (to); imode != VOIDmode;
5494 imode = GET_MODE_WIDER_MODE (imode))
5496 icode = convert_optab_handler (tab, imode, fmode);
5497 if (icode != CODE_FOR_nothing)
5499 rtx_insn *last = get_last_insn ();
5500 if (fmode != GET_MODE (from))
5501 from = convert_to_mode (fmode, from, 0);
5503 if (imode != GET_MODE (to))
5504 target = gen_reg_rtx (imode);
5506 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5508 delete_insns_since (last);
5509 continue;
5511 if (target != to)
5512 convert_move (to, target, 0);
5513 return true;
5517 return false;
5520 /* Report whether we have an instruction to perform the operation
5521 specified by CODE on operands of mode MODE. */
5523 have_insn_for (enum rtx_code code, enum machine_mode mode)
5525 return (code_to_optab (code)
5526 && (optab_handler (code_to_optab (code), mode)
5527 != CODE_FOR_nothing));
5530 /* Initialize the libfunc fields of an entire group of entries in some
5531 optab. Each entry is set equal to a string consisting of a leading
5532 pair of underscores followed by a generic operation name followed by
5533 a mode name (downshifted to lowercase) followed by a single character
5534 representing the number of operands for the given operation (which is
5535 usually one of the characters '2', '3', or '4').
5537 OPTABLE is the table in which libfunc fields are to be initialized.
5538 OPNAME is the generic (string) name of the operation.
5539 SUFFIX is the character which specifies the number of operands for
5540 the given generic operation.
5541 MODE is the mode to generate for.
5544 static void
5545 gen_libfunc (optab optable, const char *opname, int suffix,
5546 enum machine_mode mode)
5548 unsigned opname_len = strlen (opname);
5549 const char *mname = GET_MODE_NAME (mode);
5550 unsigned mname_len = strlen (mname);
5551 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5552 int len = prefix_len + opname_len + mname_len + 1 + 1;
5553 char *libfunc_name = XALLOCAVEC (char, len);
5554 char *p;
5555 const char *q;
5557 p = libfunc_name;
5558 *p++ = '_';
5559 *p++ = '_';
5560 if (targetm.libfunc_gnu_prefix)
5562 *p++ = 'g';
5563 *p++ = 'n';
5564 *p++ = 'u';
5565 *p++ = '_';
5567 for (q = opname; *q; )
5568 *p++ = *q++;
5569 for (q = mname; *q; q++)
5570 *p++ = TOLOWER (*q);
5571 *p++ = suffix;
5572 *p = '\0';
5574 set_optab_libfunc (optable, mode,
5575 ggc_alloc_string (libfunc_name, p - libfunc_name));
5578 /* Like gen_libfunc, but verify that integer operation is involved. */
5580 void
5581 gen_int_libfunc (optab optable, const char *opname, char suffix,
5582 enum machine_mode mode)
5584 int maxsize = 2 * BITS_PER_WORD;
5585 int minsize = BITS_PER_WORD;
5587 if (GET_MODE_CLASS (mode) != MODE_INT)
5588 return;
5589 if (maxsize < LONG_LONG_TYPE_SIZE)
5590 maxsize = LONG_LONG_TYPE_SIZE;
5591 if (minsize > INT_TYPE_SIZE
5592 && (trapv_binoptab_p (optable)
5593 || trapv_unoptab_p (optable)))
5594 minsize = INT_TYPE_SIZE;
5595 if (GET_MODE_BITSIZE (mode) < minsize
5596 || GET_MODE_BITSIZE (mode) > maxsize)
5597 return;
5598 gen_libfunc (optable, opname, suffix, mode);
5601 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5603 void
5604 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5605 enum machine_mode mode)
5607 char *dec_opname;
5609 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5610 gen_libfunc (optable, opname, suffix, mode);
5611 if (DECIMAL_FLOAT_MODE_P (mode))
5613 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5614 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5615 depending on the low level floating format used. */
5616 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5617 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5618 gen_libfunc (optable, dec_opname, suffix, mode);
5622 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5624 void
5625 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5626 enum machine_mode mode)
5628 if (!ALL_FIXED_POINT_MODE_P (mode))
5629 return;
5630 gen_libfunc (optable, opname, suffix, mode);
5633 /* Like gen_libfunc, but verify that signed fixed-point operation is
5634 involved. */
5636 void
5637 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5638 enum machine_mode mode)
5640 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5641 return;
5642 gen_libfunc (optable, opname, suffix, mode);
5645 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5646 involved. */
5648 void
5649 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5650 enum machine_mode mode)
5652 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5653 return;
5654 gen_libfunc (optable, opname, suffix, mode);
5657 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5659 void
5660 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5661 enum machine_mode mode)
5663 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5664 gen_fp_libfunc (optable, name, suffix, mode);
5665 if (INTEGRAL_MODE_P (mode))
5666 gen_int_libfunc (optable, name, suffix, mode);
5669 /* Like gen_libfunc, but verify that FP or INT operation is involved
5670 and add 'v' suffix for integer operation. */
5672 void
5673 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5674 enum machine_mode mode)
5676 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5677 gen_fp_libfunc (optable, name, suffix, mode);
5678 if (GET_MODE_CLASS (mode) == MODE_INT)
5680 int len = strlen (name);
5681 char *v_name = XALLOCAVEC (char, len + 2);
5682 strcpy (v_name, name);
5683 v_name[len] = 'v';
5684 v_name[len + 1] = 0;
5685 gen_int_libfunc (optable, v_name, suffix, mode);
5689 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5690 involved. */
5692 void
5693 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5694 enum machine_mode mode)
5696 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5697 gen_fp_libfunc (optable, name, suffix, mode);
5698 if (INTEGRAL_MODE_P (mode))
5699 gen_int_libfunc (optable, name, suffix, mode);
5700 if (ALL_FIXED_POINT_MODE_P (mode))
5701 gen_fixed_libfunc (optable, name, suffix, mode);
5704 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5705 involved. */
5707 void
5708 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5709 enum machine_mode mode)
5711 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5712 gen_fp_libfunc (optable, name, suffix, mode);
5713 if (INTEGRAL_MODE_P (mode))
5714 gen_int_libfunc (optable, name, suffix, mode);
5715 if (SIGNED_FIXED_POINT_MODE_P (mode))
5716 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5719 /* Like gen_libfunc, but verify that INT or FIXED operation is
5720 involved. */
5722 void
5723 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5724 enum machine_mode mode)
5726 if (INTEGRAL_MODE_P (mode))
5727 gen_int_libfunc (optable, name, suffix, mode);
5728 if (ALL_FIXED_POINT_MODE_P (mode))
5729 gen_fixed_libfunc (optable, name, suffix, mode);
5732 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5733 involved. */
5735 void
5736 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5737 enum machine_mode mode)
5739 if (INTEGRAL_MODE_P (mode))
5740 gen_int_libfunc (optable, name, suffix, mode);
5741 if (SIGNED_FIXED_POINT_MODE_P (mode))
5742 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5745 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5746 involved. */
5748 void
5749 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5750 enum machine_mode mode)
5752 if (INTEGRAL_MODE_P (mode))
5753 gen_int_libfunc (optable, name, suffix, mode);
5754 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5755 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5758 /* Initialize the libfunc fields of an entire group of entries of an
5759 inter-mode-class conversion optab. The string formation rules are
5760 similar to the ones for init_libfuncs, above, but instead of having
5761 a mode name and an operand count these functions have two mode names
5762 and no operand count. */
5764 void
5765 gen_interclass_conv_libfunc (convert_optab tab,
5766 const char *opname,
5767 enum machine_mode tmode,
5768 enum machine_mode fmode)
5770 size_t opname_len = strlen (opname);
5771 size_t mname_len = 0;
5773 const char *fname, *tname;
5774 const char *q;
5775 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5776 char *libfunc_name, *suffix;
5777 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5778 char *p;
5780 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5781 depends on which underlying decimal floating point format is used. */
5782 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5784 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5786 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5787 nondec_name[0] = '_';
5788 nondec_name[1] = '_';
5789 if (targetm.libfunc_gnu_prefix)
5791 nondec_name[2] = 'g';
5792 nondec_name[3] = 'n';
5793 nondec_name[4] = 'u';
5794 nondec_name[5] = '_';
5797 memcpy (&nondec_name[prefix_len], opname, opname_len);
5798 nondec_suffix = nondec_name + opname_len + prefix_len;
5800 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5801 dec_name[0] = '_';
5802 dec_name[1] = '_';
5803 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5804 memcpy (&dec_name[2+dec_len], opname, opname_len);
5805 dec_suffix = dec_name + dec_len + opname_len + 2;
5807 fname = GET_MODE_NAME (fmode);
5808 tname = GET_MODE_NAME (tmode);
5810 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5812 libfunc_name = dec_name;
5813 suffix = dec_suffix;
5815 else
5817 libfunc_name = nondec_name;
5818 suffix = nondec_suffix;
5821 p = suffix;
5822 for (q = fname; *q; p++, q++)
5823 *p = TOLOWER (*q);
5824 for (q = tname; *q; p++, q++)
5825 *p = TOLOWER (*q);
5827 *p = '\0';
5829 set_conv_libfunc (tab, tmode, fmode,
5830 ggc_alloc_string (libfunc_name, p - libfunc_name));
5833 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5834 int->fp conversion. */
5836 void
5837 gen_int_to_fp_conv_libfunc (convert_optab tab,
5838 const char *opname,
5839 enum machine_mode tmode,
5840 enum machine_mode fmode)
5842 if (GET_MODE_CLASS (fmode) != MODE_INT)
5843 return;
5844 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5845 return;
5846 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5849 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5850 naming scheme. */
5852 void
5853 gen_ufloat_conv_libfunc (convert_optab tab,
5854 const char *opname ATTRIBUTE_UNUSED,
5855 enum machine_mode tmode,
5856 enum machine_mode fmode)
5858 if (DECIMAL_FLOAT_MODE_P (tmode))
5859 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5860 else
5861 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5864 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5865 fp->int conversion. */
5867 void
5868 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5869 const char *opname,
5870 enum machine_mode tmode,
5871 enum machine_mode fmode)
5873 if (GET_MODE_CLASS (fmode) != MODE_INT)
5874 return;
5875 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5876 return;
5877 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5880 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5881 fp->int conversion with no decimal floating point involved. */
5883 void
5884 gen_fp_to_int_conv_libfunc (convert_optab tab,
5885 const char *opname,
5886 enum machine_mode tmode,
5887 enum machine_mode fmode)
5889 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5890 return;
5891 if (GET_MODE_CLASS (tmode) != MODE_INT)
5892 return;
5893 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5896 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5897 The string formation rules are
5898 similar to the ones for init_libfunc, above. */
5900 void
5901 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5902 enum machine_mode tmode, enum machine_mode fmode)
5904 size_t opname_len = strlen (opname);
5905 size_t mname_len = 0;
5907 const char *fname, *tname;
5908 const char *q;
5909 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5910 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5911 char *libfunc_name, *suffix;
5912 char *p;
5914 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5915 depends on which underlying decimal floating point format is used. */
5916 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5918 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5920 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5921 nondec_name[0] = '_';
5922 nondec_name[1] = '_';
5923 if (targetm.libfunc_gnu_prefix)
5925 nondec_name[2] = 'g';
5926 nondec_name[3] = 'n';
5927 nondec_name[4] = 'u';
5928 nondec_name[5] = '_';
5930 memcpy (&nondec_name[prefix_len], opname, opname_len);
5931 nondec_suffix = nondec_name + opname_len + prefix_len;
5933 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5934 dec_name[0] = '_';
5935 dec_name[1] = '_';
5936 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5937 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5938 dec_suffix = dec_name + dec_len + opname_len + 2;
5940 fname = GET_MODE_NAME (fmode);
5941 tname = GET_MODE_NAME (tmode);
5943 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5945 libfunc_name = dec_name;
5946 suffix = dec_suffix;
5948 else
5950 libfunc_name = nondec_name;
5951 suffix = nondec_suffix;
5954 p = suffix;
5955 for (q = fname; *q; p++, q++)
5956 *p = TOLOWER (*q);
5957 for (q = tname; *q; p++, q++)
5958 *p = TOLOWER (*q);
5960 *p++ = '2';
5961 *p = '\0';
5963 set_conv_libfunc (tab, tmode, fmode,
5964 ggc_alloc_string (libfunc_name, p - libfunc_name));
5967 /* Pick proper libcall for trunc_optab. We need to chose if we do
5968 truncation or extension and interclass or intraclass. */
5970 void
5971 gen_trunc_conv_libfunc (convert_optab tab,
5972 const char *opname,
5973 enum machine_mode tmode,
5974 enum machine_mode fmode)
5976 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5977 return;
5978 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5979 return;
5980 if (tmode == fmode)
5981 return;
5983 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5984 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5985 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5987 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5988 return;
5990 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5991 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5992 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5993 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5996 /* Pick proper libcall for extend_optab. We need to chose if we do
5997 truncation or extension and interclass or intraclass. */
5999 void
6000 gen_extend_conv_libfunc (convert_optab tab,
6001 const char *opname ATTRIBUTE_UNUSED,
6002 enum machine_mode tmode,
6003 enum machine_mode fmode)
6005 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
6006 return;
6007 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
6008 return;
6009 if (tmode == fmode)
6010 return;
6012 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
6013 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
6014 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6016 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
6017 return;
6019 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
6020 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
6021 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
6022 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6025 /* Pick proper libcall for fract_optab. We need to chose if we do
6026 interclass or intraclass. */
6028 void
6029 gen_fract_conv_libfunc (convert_optab tab,
6030 const char *opname,
6031 enum machine_mode tmode,
6032 enum machine_mode fmode)
6034 if (tmode == fmode)
6035 return;
6036 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
6037 return;
6039 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6040 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6041 else
6042 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6045 /* Pick proper libcall for fractuns_optab. */
6047 void
6048 gen_fractuns_conv_libfunc (convert_optab tab,
6049 const char *opname,
6050 enum machine_mode tmode,
6051 enum machine_mode fmode)
6053 if (tmode == fmode)
6054 return;
6055 /* One mode must be a fixed-point mode, and the other must be an integer
6056 mode. */
6057 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
6058 || (ALL_FIXED_POINT_MODE_P (fmode)
6059 && GET_MODE_CLASS (tmode) == MODE_INT)))
6060 return;
6062 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6065 /* Pick proper libcall for satfract_optab. We need to chose if we do
6066 interclass or intraclass. */
6068 void
6069 gen_satfract_conv_libfunc (convert_optab tab,
6070 const char *opname,
6071 enum machine_mode tmode,
6072 enum machine_mode fmode)
6074 if (tmode == fmode)
6075 return;
6076 /* TMODE must be a fixed-point mode. */
6077 if (!ALL_FIXED_POINT_MODE_P (tmode))
6078 return;
6080 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6081 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6082 else
6083 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6086 /* Pick proper libcall for satfractuns_optab. */
6088 void
6089 gen_satfractuns_conv_libfunc (convert_optab tab,
6090 const char *opname,
6091 enum machine_mode tmode,
6092 enum machine_mode fmode)
6094 if (tmode == fmode)
6095 return;
6096 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
6097 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
6098 return;
6100 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6103 /* A table of previously-created libfuncs, hashed by name. */
6104 static GTY ((param_is (union tree_node))) htab_t libfunc_decls;
6106 /* Hashtable callbacks for libfunc_decls. */
6108 static hashval_t
6109 libfunc_decl_hash (const void *entry)
6111 return IDENTIFIER_HASH_VALUE (DECL_NAME ((const_tree) entry));
6114 static int
6115 libfunc_decl_eq (const void *entry1, const void *entry2)
6117 return DECL_NAME ((const_tree) entry1) == (const_tree) entry2;
6120 /* Build a decl for a libfunc named NAME. */
6122 tree
6123 build_libfunc_function (const char *name)
6125 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6126 get_identifier (name),
6127 build_function_type (integer_type_node, NULL_TREE));
6128 /* ??? We don't have any type information except for this is
6129 a function. Pretend this is "int foo()". */
6130 DECL_ARTIFICIAL (decl) = 1;
6131 DECL_EXTERNAL (decl) = 1;
6132 TREE_PUBLIC (decl) = 1;
6133 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6135 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6136 are the flags assigned by targetm.encode_section_info. */
6137 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6139 return decl;
6143 init_one_libfunc (const char *name)
6145 tree id, decl;
6146 void **slot;
6147 hashval_t hash;
6149 if (libfunc_decls == NULL)
6150 libfunc_decls = htab_create_ggc (37, libfunc_decl_hash,
6151 libfunc_decl_eq, NULL);
6153 /* See if we have already created a libfunc decl for this function. */
6154 id = get_identifier (name);
6155 hash = IDENTIFIER_HASH_VALUE (id);
6156 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, INSERT);
6157 decl = (tree) *slot;
6158 if (decl == NULL)
6160 /* Create a new decl, so that it can be passed to
6161 targetm.encode_section_info. */
6162 decl = build_libfunc_function (name);
6163 *slot = decl;
6165 return XEXP (DECL_RTL (decl), 0);
6168 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6171 set_user_assembler_libfunc (const char *name, const char *asmspec)
6173 tree id, decl;
6174 void **slot;
6175 hashval_t hash;
6177 id = get_identifier (name);
6178 hash = IDENTIFIER_HASH_VALUE (id);
6179 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, NO_INSERT);
6180 gcc_assert (slot);
6181 decl = (tree) *slot;
6182 set_user_assembler_name (decl, asmspec);
6183 return XEXP (DECL_RTL (decl), 0);
6186 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6187 MODE to NAME, which should be either 0 or a string constant. */
6188 void
6189 set_optab_libfunc (optab op, enum machine_mode mode, const char *name)
6191 rtx val;
6192 struct libfunc_entry e;
6193 struct libfunc_entry **slot;
6195 e.op = op;
6196 e.mode1 = mode;
6197 e.mode2 = VOIDmode;
6199 if (name)
6200 val = init_one_libfunc (name);
6201 else
6202 val = 0;
6203 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6204 if (*slot == NULL)
6205 *slot = ggc_alloc<libfunc_entry> ();
6206 (*slot)->op = op;
6207 (*slot)->mode1 = mode;
6208 (*slot)->mode2 = VOIDmode;
6209 (*slot)->libfunc = val;
6212 /* Call this to reset the function entry for one conversion optab
6213 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6214 either 0 or a string constant. */
6215 void
6216 set_conv_libfunc (convert_optab optab, enum machine_mode tmode,
6217 enum machine_mode fmode, const char *name)
6219 rtx val;
6220 struct libfunc_entry e;
6221 struct libfunc_entry **slot;
6223 e.op = optab;
6224 e.mode1 = tmode;
6225 e.mode2 = fmode;
6227 if (name)
6228 val = init_one_libfunc (name);
6229 else
6230 val = 0;
6231 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6232 if (*slot == NULL)
6233 *slot = ggc_alloc<libfunc_entry> ();
6234 (*slot)->op = optab;
6235 (*slot)->mode1 = tmode;
6236 (*slot)->mode2 = fmode;
6237 (*slot)->libfunc = val;
6240 /* Call this to initialize the contents of the optabs
6241 appropriately for the current target machine. */
6243 void
6244 init_optabs (void)
6246 if (libfunc_hash)
6247 htab_empty (libfunc_hash);
6248 else
6249 libfunc_hash = htab_create_ggc (10, hash_libfunc, eq_libfunc, NULL);
6251 /* Fill in the optabs with the insns we support. */
6252 init_all_optabs (this_fn_optabs);
6254 /* The ffs function operates on `int'. Fall back on it if we do not
6255 have a libgcc2 function for that width. */
6256 if (INT_TYPE_SIZE < BITS_PER_WORD)
6257 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6258 "ffs");
6260 /* Explicitly initialize the bswap libfuncs since we need them to be
6261 valid for things other than word_mode. */
6262 if (targetm.libfunc_gnu_prefix)
6264 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6265 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6267 else
6269 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6270 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6273 /* Use cabs for double complex abs, since systems generally have cabs.
6274 Don't define any libcall for float complex, so that cabs will be used. */
6275 if (complex_double_type_node)
6276 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node),
6277 "cabs");
6279 abort_libfunc = init_one_libfunc ("abort");
6280 memcpy_libfunc = init_one_libfunc ("memcpy");
6281 memmove_libfunc = init_one_libfunc ("memmove");
6282 memcmp_libfunc = init_one_libfunc ("memcmp");
6283 memset_libfunc = init_one_libfunc ("memset");
6284 setbits_libfunc = init_one_libfunc ("__setbits");
6286 #ifndef DONT_USE_BUILTIN_SETJMP
6287 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6288 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6289 #else
6290 setjmp_libfunc = init_one_libfunc ("setjmp");
6291 longjmp_libfunc = init_one_libfunc ("longjmp");
6292 #endif
6293 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6294 unwind_sjlj_unregister_libfunc
6295 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6297 /* For function entry/exit instrumentation. */
6298 profile_function_entry_libfunc
6299 = init_one_libfunc ("__cyg_profile_func_enter");
6300 profile_function_exit_libfunc
6301 = init_one_libfunc ("__cyg_profile_func_exit");
6303 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6305 /* Allow the target to add more libcalls or rename some, etc. */
6306 targetm.init_libfuncs ();
6309 /* Use the current target and options to initialize
6310 TREE_OPTIMIZATION_OPTABS (OPTNODE). */
6312 void
6313 init_tree_optimization_optabs (tree optnode)
6315 /* Quick exit if we have already computed optabs for this target. */
6316 if (TREE_OPTIMIZATION_BASE_OPTABS (optnode) == this_target_optabs)
6317 return;
6319 /* Forget any previous information and set up for the current target. */
6320 TREE_OPTIMIZATION_BASE_OPTABS (optnode) = this_target_optabs;
6321 struct target_optabs *tmp_optabs = (struct target_optabs *)
6322 TREE_OPTIMIZATION_OPTABS (optnode);
6323 if (tmp_optabs)
6324 memset (tmp_optabs, 0, sizeof (struct target_optabs));
6325 else
6326 tmp_optabs = ggc_alloc<target_optabs> ();
6328 /* Generate a new set of optabs into tmp_optabs. */
6329 init_all_optabs (tmp_optabs);
6331 /* If the optabs changed, record it. */
6332 if (memcmp (tmp_optabs, this_target_optabs, sizeof (struct target_optabs)))
6333 TREE_OPTIMIZATION_OPTABS (optnode) = tmp_optabs;
6334 else
6336 TREE_OPTIMIZATION_OPTABS (optnode) = NULL;
6337 ggc_free (tmp_optabs);
6341 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6342 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6344 static void
6345 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6347 enum machine_mode mode;
6348 char buf[64];
6349 size_t len = strlen (base);
6350 int i;
6352 gcc_assert (max <= 8);
6353 gcc_assert (len + 3 < sizeof (buf));
6355 memcpy (buf, base, len);
6356 buf[len] = '_';
6357 buf[len + 1] = '0';
6358 buf[len + 2] = '\0';
6360 mode = QImode;
6361 for (i = 1; i <= max; i *= 2)
6363 buf[len + 1] = '0' + i;
6364 set_optab_libfunc (tab, mode, buf);
6365 mode = GET_MODE_2XWIDER_MODE (mode);
6369 void
6370 init_sync_libfuncs (int max)
6372 if (!flag_sync_libcalls)
6373 return;
6375 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6376 "__sync_val_compare_and_swap", max);
6377 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6378 "__sync_lock_test_and_set", max);
6380 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6381 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6382 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6383 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6384 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6385 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6387 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6388 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6389 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6390 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6391 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6392 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6395 /* Print information about the current contents of the optabs on
6396 STDERR. */
6398 DEBUG_FUNCTION void
6399 debug_optab_libfuncs (void)
6401 int i, j, k;
6403 /* Dump the arithmetic optabs. */
6404 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
6405 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6407 rtx l = optab_libfunc ((optab) i, (enum machine_mode) j);
6408 if (l)
6410 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6411 fprintf (stderr, "%s\t%s:\t%s\n",
6412 GET_RTX_NAME (optab_to_code ((optab) i)),
6413 GET_MODE_NAME (j),
6414 XSTR (l, 0));
6418 /* Dump the conversion optabs. */
6419 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
6420 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6421 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6423 rtx l = convert_optab_libfunc ((optab) i, (enum machine_mode) j,
6424 (enum machine_mode) k);
6425 if (l)
6427 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6428 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6429 GET_RTX_NAME (optab_to_code ((optab) i)),
6430 GET_MODE_NAME (j),
6431 GET_MODE_NAME (k),
6432 XSTR (l, 0));
6438 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6439 CODE. Return 0 on failure. */
6442 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6444 enum machine_mode mode = GET_MODE (op1);
6445 enum insn_code icode;
6446 rtx insn;
6447 rtx trap_rtx;
6449 if (mode == VOIDmode)
6450 return 0;
6452 icode = optab_handler (ctrap_optab, mode);
6453 if (icode == CODE_FOR_nothing)
6454 return 0;
6456 /* Some targets only accept a zero trap code. */
6457 if (!insn_operand_matches (icode, 3, tcode))
6458 return 0;
6460 do_pending_stack_adjust ();
6461 start_sequence ();
6462 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6463 &trap_rtx, &mode);
6464 if (!trap_rtx)
6465 insn = NULL_RTX;
6466 else
6467 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6468 tcode);
6470 /* If that failed, then give up. */
6471 if (insn == 0)
6473 end_sequence ();
6474 return 0;
6477 emit_insn (insn);
6478 insn = get_insns ();
6479 end_sequence ();
6480 return insn;
6483 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6484 or unsigned operation code. */
6486 static enum rtx_code
6487 get_rtx_code (enum tree_code tcode, bool unsignedp)
6489 enum rtx_code code;
6490 switch (tcode)
6492 case EQ_EXPR:
6493 code = EQ;
6494 break;
6495 case NE_EXPR:
6496 code = NE;
6497 break;
6498 case LT_EXPR:
6499 code = unsignedp ? LTU : LT;
6500 break;
6501 case LE_EXPR:
6502 code = unsignedp ? LEU : LE;
6503 break;
6504 case GT_EXPR:
6505 code = unsignedp ? GTU : GT;
6506 break;
6507 case GE_EXPR:
6508 code = unsignedp ? GEU : GE;
6509 break;
6511 case UNORDERED_EXPR:
6512 code = UNORDERED;
6513 break;
6514 case ORDERED_EXPR:
6515 code = ORDERED;
6516 break;
6517 case UNLT_EXPR:
6518 code = UNLT;
6519 break;
6520 case UNLE_EXPR:
6521 code = UNLE;
6522 break;
6523 case UNGT_EXPR:
6524 code = UNGT;
6525 break;
6526 case UNGE_EXPR:
6527 code = UNGE;
6528 break;
6529 case UNEQ_EXPR:
6530 code = UNEQ;
6531 break;
6532 case LTGT_EXPR:
6533 code = LTGT;
6534 break;
6536 default:
6537 gcc_unreachable ();
6539 return code;
6542 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6543 unsigned operators. Do not generate compare instruction. */
6545 static rtx
6546 vector_compare_rtx (enum tree_code tcode, tree t_op0, tree t_op1,
6547 bool unsignedp, enum insn_code icode)
6549 struct expand_operand ops[2];
6550 rtx rtx_op0, rtx_op1;
6551 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
6553 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
6555 /* Expand operands. */
6556 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6557 EXPAND_STACK_PARM);
6558 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6559 EXPAND_STACK_PARM);
6561 create_input_operand (&ops[0], rtx_op0, GET_MODE (rtx_op0));
6562 create_input_operand (&ops[1], rtx_op1, GET_MODE (rtx_op1));
6563 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6564 gcc_unreachable ();
6565 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6568 /* Return true if VEC_PERM_EXPR can be expanded using SIMD extensions
6569 of the CPU. SEL may be NULL, which stands for an unknown constant. */
6571 bool
6572 can_vec_perm_p (enum machine_mode mode, bool variable,
6573 const unsigned char *sel)
6575 enum machine_mode qimode;
6577 /* If the target doesn't implement a vector mode for the vector type,
6578 then no operations are supported. */
6579 if (!VECTOR_MODE_P (mode))
6580 return false;
6582 if (!variable)
6584 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6585 && (sel == NULL
6586 || targetm.vectorize.vec_perm_const_ok == NULL
6587 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6588 return true;
6591 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6592 return true;
6594 /* We allow fallback to a QI vector mode, and adjust the mask. */
6595 if (GET_MODE_INNER (mode) == QImode)
6596 return false;
6597 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6598 if (!VECTOR_MODE_P (qimode))
6599 return false;
6601 /* ??? For completeness, we ought to check the QImode version of
6602 vec_perm_const_optab. But all users of this implicit lowering
6603 feature implement the variable vec_perm_optab. */
6604 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6605 return false;
6607 /* In order to support the lowering of variable permutations,
6608 we need to support shifts and adds. */
6609 if (variable)
6611 if (GET_MODE_UNIT_SIZE (mode) > 2
6612 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6613 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6614 return false;
6615 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6616 return false;
6619 return true;
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 enum machine_mode tmode = GET_MODE (target);
6629 enum 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 create_input_operand (&ops[2], v1, tmode);
6653 if (maybe_expand_insn (icode, 4, ops))
6654 return ops[0].value;
6655 return NULL_RTX;
6658 /* Generate instructions for vec_perm optab given its mode
6659 and three operands. */
6662 expand_vec_perm (enum machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6664 enum insn_code icode;
6665 enum machine_mode qimode;
6666 unsigned int i, w, e, u;
6667 rtx tmp, sel_qi = NULL;
6668 rtvec vec;
6670 if (!target || GET_MODE (target) != mode)
6671 target = gen_reg_rtx (mode);
6673 w = GET_MODE_SIZE (mode);
6674 e = GET_MODE_NUNITS (mode);
6675 u = GET_MODE_UNIT_SIZE (mode);
6677 /* Set QIMODE to a different vector mode with byte elements.
6678 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6679 qimode = VOIDmode;
6680 if (GET_MODE_INNER (mode) != QImode)
6682 qimode = mode_for_vector (QImode, w);
6683 if (!VECTOR_MODE_P (qimode))
6684 qimode = VOIDmode;
6687 /* If the input is a constant, expand it specially. */
6688 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6689 if (GET_CODE (sel) == CONST_VECTOR)
6691 icode = direct_optab_handler (vec_perm_const_optab, mode);
6692 if (icode != CODE_FOR_nothing)
6694 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6695 if (tmp)
6696 return tmp;
6699 /* Fall back to a constant byte-based permutation. */
6700 if (qimode != VOIDmode)
6702 vec = rtvec_alloc (w);
6703 for (i = 0; i < e; ++i)
6705 unsigned int j, this_e;
6707 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6708 this_e &= 2 * e - 1;
6709 this_e *= u;
6711 for (j = 0; j < u; ++j)
6712 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
6714 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
6716 icode = direct_optab_handler (vec_perm_const_optab, qimode);
6717 if (icode != CODE_FOR_nothing)
6719 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6720 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6721 gen_lowpart (qimode, v1), sel_qi);
6722 if (tmp)
6723 return gen_lowpart (mode, tmp);
6728 /* Otherwise expand as a fully variable permuation. */
6729 icode = direct_optab_handler (vec_perm_optab, mode);
6730 if (icode != CODE_FOR_nothing)
6732 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6733 if (tmp)
6734 return tmp;
6737 /* As a special case to aid several targets, lower the element-based
6738 permutation to a byte-based permutation and try again. */
6739 if (qimode == VOIDmode)
6740 return NULL_RTX;
6741 icode = direct_optab_handler (vec_perm_optab, qimode);
6742 if (icode == CODE_FOR_nothing)
6743 return NULL_RTX;
6745 if (sel_qi == NULL)
6747 /* Multiply each element by its byte size. */
6748 enum machine_mode selmode = GET_MODE (sel);
6749 if (u == 2)
6750 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6751 sel, 0, OPTAB_DIRECT);
6752 else
6753 sel = expand_simple_binop (selmode, ASHIFT, sel,
6754 GEN_INT (exact_log2 (u)),
6755 sel, 0, OPTAB_DIRECT);
6756 gcc_assert (sel != NULL);
6758 /* Broadcast the low byte each element into each of its bytes. */
6759 vec = rtvec_alloc (w);
6760 for (i = 0; i < w; ++i)
6762 int this_e = i / u * u;
6763 if (BYTES_BIG_ENDIAN)
6764 this_e += u - 1;
6765 RTVEC_ELT (vec, i) = GEN_INT (this_e);
6767 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6768 sel = gen_lowpart (qimode, sel);
6769 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
6770 gcc_assert (sel != NULL);
6772 /* Add the byte offset to each byte element. */
6773 /* Note that the definition of the indicies here is memory ordering,
6774 so there should be no difference between big and little endian. */
6775 vec = rtvec_alloc (w);
6776 for (i = 0; i < w; ++i)
6777 RTVEC_ELT (vec, i) = GEN_INT (i % u);
6778 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6779 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6780 sel, 0, OPTAB_DIRECT);
6781 gcc_assert (sel_qi != NULL);
6784 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6785 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6786 gen_lowpart (qimode, v1), sel_qi);
6787 if (tmp)
6788 tmp = gen_lowpart (mode, tmp);
6789 return tmp;
6792 /* Return insn code for a conditional operator with a comparison in
6793 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
6795 static inline enum insn_code
6796 get_vcond_icode (enum machine_mode vmode, enum machine_mode cmode, bool uns)
6798 enum insn_code icode = CODE_FOR_nothing;
6799 if (uns)
6800 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
6801 else
6802 icode = convert_optab_handler (vcond_optab, vmode, cmode);
6803 return icode;
6806 /* Return TRUE iff, appropriate vector insns are available
6807 for vector cond expr with vector type VALUE_TYPE and a comparison
6808 with operand vector types in CMP_OP_TYPE. */
6810 bool
6811 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
6813 enum machine_mode value_mode = TYPE_MODE (value_type);
6814 enum machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
6815 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
6816 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
6817 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
6818 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
6819 return false;
6820 return true;
6823 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6824 three operands. */
6827 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6828 rtx target)
6830 struct expand_operand ops[6];
6831 enum insn_code icode;
6832 rtx comparison, rtx_op1, rtx_op2;
6833 enum machine_mode mode = TYPE_MODE (vec_cond_type);
6834 enum machine_mode cmp_op_mode;
6835 bool unsignedp;
6836 tree op0a, op0b;
6837 enum tree_code tcode;
6839 if (COMPARISON_CLASS_P (op0))
6841 op0a = TREE_OPERAND (op0, 0);
6842 op0b = TREE_OPERAND (op0, 1);
6843 tcode = TREE_CODE (op0);
6845 else
6847 /* Fake op0 < 0. */
6848 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (op0)));
6849 op0a = op0;
6850 op0b = build_zero_cst (TREE_TYPE (op0));
6851 tcode = LT_EXPR;
6853 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6854 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
6857 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
6858 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
6860 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
6861 if (icode == CODE_FOR_nothing)
6862 return 0;
6864 comparison = vector_compare_rtx (tcode, op0a, op0b, unsignedp, icode);
6865 rtx_op1 = expand_normal (op1);
6866 rtx_op2 = expand_normal (op2);
6868 create_output_operand (&ops[0], target, mode);
6869 create_input_operand (&ops[1], rtx_op1, mode);
6870 create_input_operand (&ops[2], rtx_op2, mode);
6871 create_fixed_operand (&ops[3], comparison);
6872 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6873 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6874 expand_insn (icode, 6, ops);
6875 return ops[0].value;
6878 /* Return non-zero if a highpart multiply is supported of can be synthisized.
6879 For the benefit of expand_mult_highpart, the return value is 1 for direct,
6880 2 for even/odd widening, and 3 for hi/lo widening. */
6883 can_mult_highpart_p (enum machine_mode mode, bool uns_p)
6885 optab op;
6886 unsigned char *sel;
6887 unsigned i, nunits;
6889 op = uns_p ? umul_highpart_optab : smul_highpart_optab;
6890 if (optab_handler (op, mode) != CODE_FOR_nothing)
6891 return 1;
6893 /* If the mode is an integral vector, synth from widening operations. */
6894 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
6895 return 0;
6897 nunits = GET_MODE_NUNITS (mode);
6898 sel = XALLOCAVEC (unsigned char, nunits);
6900 op = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6901 if (optab_handler (op, mode) != CODE_FOR_nothing)
6903 op = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6904 if (optab_handler (op, mode) != CODE_FOR_nothing)
6906 for (i = 0; i < nunits; ++i)
6907 sel[i] = !BYTES_BIG_ENDIAN + (i & ~1) + ((i & 1) ? nunits : 0);
6908 if (can_vec_perm_p (mode, false, sel))
6909 return 2;
6913 op = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6914 if (optab_handler (op, mode) != CODE_FOR_nothing)
6916 op = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6917 if (optab_handler (op, mode) != CODE_FOR_nothing)
6919 for (i = 0; i < nunits; ++i)
6920 sel[i] = 2 * i + (BYTES_BIG_ENDIAN ? 0 : 1);
6921 if (can_vec_perm_p (mode, false, sel))
6922 return 3;
6926 return 0;
6929 /* Expand a highpart multiply. */
6932 expand_mult_highpart (enum machine_mode mode, rtx op0, rtx op1,
6933 rtx target, bool uns_p)
6935 struct expand_operand eops[3];
6936 enum insn_code icode;
6937 int method, i, nunits;
6938 enum machine_mode wmode;
6939 rtx m1, m2, perm;
6940 optab tab1, tab2;
6941 rtvec v;
6943 method = can_mult_highpart_p (mode, uns_p);
6944 switch (method)
6946 case 0:
6947 return NULL_RTX;
6948 case 1:
6949 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6950 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6951 OPTAB_LIB_WIDEN);
6952 case 2:
6953 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6954 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6955 break;
6956 case 3:
6957 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6958 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6959 if (BYTES_BIG_ENDIAN)
6961 optab t = tab1;
6962 tab1 = tab2;
6963 tab2 = t;
6965 break;
6966 default:
6967 gcc_unreachable ();
6970 icode = optab_handler (tab1, mode);
6971 nunits = GET_MODE_NUNITS (mode);
6972 wmode = insn_data[icode].operand[0].mode;
6973 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
6974 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
6976 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6977 create_input_operand (&eops[1], op0, mode);
6978 create_input_operand (&eops[2], op1, mode);
6979 expand_insn (icode, 3, eops);
6980 m1 = gen_lowpart (mode, eops[0].value);
6982 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6983 create_input_operand (&eops[1], op0, mode);
6984 create_input_operand (&eops[2], op1, mode);
6985 expand_insn (optab_handler (tab2, mode), 3, eops);
6986 m2 = gen_lowpart (mode, eops[0].value);
6988 v = rtvec_alloc (nunits);
6989 if (method == 2)
6991 for (i = 0; i < nunits; ++i)
6992 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
6993 + ((i & 1) ? nunits : 0));
6995 else
6997 for (i = 0; i < nunits; ++i)
6998 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
7000 perm = gen_rtx_CONST_VECTOR (mode, v);
7002 return expand_vec_perm (mode, m1, m2, perm, target);
7005 /* Return true if target supports vector masked load/store for mode. */
7006 bool
7007 can_vec_mask_load_store_p (enum machine_mode mode, bool is_load)
7009 optab op = is_load ? maskload_optab : maskstore_optab;
7010 enum machine_mode vmode;
7011 unsigned int vector_sizes;
7013 /* If mode is vector mode, check it directly. */
7014 if (VECTOR_MODE_P (mode))
7015 return optab_handler (op, mode) != CODE_FOR_nothing;
7017 /* Otherwise, return true if there is some vector mode with
7018 the mask load/store supported. */
7020 /* See if there is any chance the mask load or store might be
7021 vectorized. If not, punt. */
7022 vmode = targetm.vectorize.preferred_simd_mode (mode);
7023 if (!VECTOR_MODE_P (vmode))
7024 return false;
7026 if (optab_handler (op, vmode) != CODE_FOR_nothing)
7027 return true;
7029 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
7030 while (vector_sizes != 0)
7032 unsigned int cur = 1 << floor_log2 (vector_sizes);
7033 vector_sizes &= ~cur;
7034 if (cur <= GET_MODE_SIZE (mode))
7035 continue;
7036 vmode = mode_for_vector (mode, cur / GET_MODE_SIZE (mode));
7037 if (VECTOR_MODE_P (vmode)
7038 && optab_handler (op, vmode) != CODE_FOR_nothing)
7039 return true;
7041 return false;
7044 /* Return true if there is a compare_and_swap pattern. */
7046 bool
7047 can_compare_and_swap_p (enum machine_mode mode, bool allow_libcall)
7049 enum insn_code icode;
7051 /* Check for __atomic_compare_and_swap. */
7052 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7053 if (icode != CODE_FOR_nothing)
7054 return true;
7056 /* Check for __sync_compare_and_swap. */
7057 icode = optab_handler (sync_compare_and_swap_optab, mode);
7058 if (icode != CODE_FOR_nothing)
7059 return true;
7060 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
7061 return true;
7063 /* No inline compare and swap. */
7064 return false;
7067 /* Return true if an atomic exchange can be performed. */
7069 bool
7070 can_atomic_exchange_p (enum machine_mode mode, bool allow_libcall)
7072 enum insn_code icode;
7074 /* Check for __atomic_exchange. */
7075 icode = direct_optab_handler (atomic_exchange_optab, mode);
7076 if (icode != CODE_FOR_nothing)
7077 return true;
7079 /* Don't check __sync_test_and_set, as on some platforms that
7080 has reduced functionality. Targets that really do support
7081 a proper exchange should simply be updated to the __atomics. */
7083 return can_compare_and_swap_p (mode, allow_libcall);
7087 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
7088 pattern. */
7090 static void
7091 find_cc_set (rtx x, const_rtx pat, void *data)
7093 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
7094 && GET_CODE (pat) == SET)
7096 rtx *p_cc_reg = (rtx *) data;
7097 gcc_assert (!*p_cc_reg);
7098 *p_cc_reg = x;
7102 /* This is a helper function for the other atomic operations. This function
7103 emits a loop that contains SEQ that iterates until a compare-and-swap
7104 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7105 a set of instructions that takes a value from OLD_REG as an input and
7106 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7107 set to the current contents of MEM. After SEQ, a compare-and-swap will
7108 attempt to update MEM with NEW_REG. The function returns true when the
7109 loop was generated successfully. */
7111 static bool
7112 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7114 enum machine_mode mode = GET_MODE (mem);
7115 rtx_code_label *label;
7116 rtx cmp_reg, success, oldval;
7118 /* The loop we want to generate looks like
7120 cmp_reg = mem;
7121 label:
7122 old_reg = cmp_reg;
7123 seq;
7124 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
7125 if (success)
7126 goto label;
7128 Note that we only do the plain load from memory once. Subsequent
7129 iterations use the value loaded by the compare-and-swap pattern. */
7131 label = gen_label_rtx ();
7132 cmp_reg = gen_reg_rtx (mode);
7134 emit_move_insn (cmp_reg, mem);
7135 emit_label (label);
7136 emit_move_insn (old_reg, cmp_reg);
7137 if (seq)
7138 emit_insn (seq);
7140 success = NULL_RTX;
7141 oldval = cmp_reg;
7142 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
7143 new_reg, false, MEMMODEL_SEQ_CST,
7144 MEMMODEL_RELAXED))
7145 return false;
7147 if (oldval != cmp_reg)
7148 emit_move_insn (cmp_reg, oldval);
7150 /* Mark this jump predicted not taken. */
7151 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
7152 GET_MODE (success), 1, label, 0);
7153 return true;
7157 /* This function tries to emit an atomic_exchange intruction. VAL is written
7158 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
7159 using TARGET if possible. */
7161 static rtx
7162 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7164 enum machine_mode mode = GET_MODE (mem);
7165 enum insn_code icode;
7167 /* If the target supports the exchange directly, great. */
7168 icode = direct_optab_handler (atomic_exchange_optab, mode);
7169 if (icode != CODE_FOR_nothing)
7171 struct expand_operand ops[4];
7173 create_output_operand (&ops[0], target, mode);
7174 create_fixed_operand (&ops[1], mem);
7175 create_input_operand (&ops[2], val, mode);
7176 create_integer_operand (&ops[3], model);
7177 if (maybe_expand_insn (icode, 4, ops))
7178 return ops[0].value;
7181 return NULL_RTX;
7184 /* This function tries to implement an atomic exchange operation using
7185 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7186 The previous contents of *MEM are returned, using TARGET if possible.
7187 Since this instructionn is an acquire barrier only, stronger memory
7188 models may require additional barriers to be emitted. */
7190 static rtx
7191 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7192 enum memmodel model)
7194 enum machine_mode mode = GET_MODE (mem);
7195 enum insn_code icode;
7196 rtx_insn *last_insn = get_last_insn ();
7198 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7200 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7201 exists, and the memory model is stronger than acquire, add a release
7202 barrier before the instruction. */
7204 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST
7205 || (model & MEMMODEL_MASK) == MEMMODEL_RELEASE
7206 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
7207 expand_mem_thread_fence (model);
7209 if (icode != CODE_FOR_nothing)
7211 struct expand_operand ops[3];
7212 create_output_operand (&ops[0], target, mode);
7213 create_fixed_operand (&ops[1], mem);
7214 create_input_operand (&ops[2], val, mode);
7215 if (maybe_expand_insn (icode, 3, ops))
7216 return ops[0].value;
7219 /* If an external test-and-set libcall is provided, use that instead of
7220 any external compare-and-swap that we might get from the compare-and-
7221 swap-loop expansion later. */
7222 if (!can_compare_and_swap_p (mode, false))
7224 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7225 if (libfunc != NULL)
7227 rtx addr;
7229 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7230 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7231 mode, 2, addr, ptr_mode,
7232 val, mode);
7236 /* If the test_and_set can't be emitted, eliminate any barrier that might
7237 have been emitted. */
7238 delete_insns_since (last_insn);
7239 return NULL_RTX;
7242 /* This function tries to implement an atomic exchange operation using a
7243 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7244 *MEM are returned, using TARGET if possible. No memory model is required
7245 since a compare_and_swap loop is seq-cst. */
7247 static rtx
7248 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7250 enum machine_mode mode = GET_MODE (mem);
7252 if (can_compare_and_swap_p (mode, true))
7254 if (!target || !register_operand (target, mode))
7255 target = gen_reg_rtx (mode);
7256 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7257 return target;
7260 return NULL_RTX;
7263 /* This function tries to implement an atomic test-and-set operation
7264 using the atomic_test_and_set instruction pattern. A boolean value
7265 is returned from the operation, using TARGET if possible. */
7267 #ifndef HAVE_atomic_test_and_set
7268 #define HAVE_atomic_test_and_set 0
7269 #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
7270 #endif
7272 static rtx
7273 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7275 enum machine_mode pat_bool_mode;
7276 struct expand_operand ops[3];
7278 if (!HAVE_atomic_test_and_set)
7279 return NULL_RTX;
7281 /* While we always get QImode from __atomic_test_and_set, we get
7282 other memory modes from __sync_lock_test_and_set. Note that we
7283 use no endian adjustment here. This matches the 4.6 behavior
7284 in the Sparc backend. */
7285 gcc_checking_assert
7286 (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
7287 if (GET_MODE (mem) != QImode)
7288 mem = adjust_address_nv (mem, QImode, 0);
7290 pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
7291 create_output_operand (&ops[0], target, pat_bool_mode);
7292 create_fixed_operand (&ops[1], mem);
7293 create_integer_operand (&ops[2], model);
7295 if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
7296 return ops[0].value;
7297 return NULL_RTX;
7300 /* This function expands the legacy _sync_lock test_and_set operation which is
7301 generally an atomic exchange. Some limited targets only allow the
7302 constant 1 to be stored. This is an ACQUIRE operation.
7304 TARGET is an optional place to stick the return value.
7305 MEM is where VAL is stored. */
7308 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7310 rtx ret;
7312 /* Try an atomic_exchange first. */
7313 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_ACQUIRE);
7314 if (ret)
7315 return ret;
7317 ret = maybe_emit_sync_lock_test_and_set (target, mem, val, MEMMODEL_ACQUIRE);
7318 if (ret)
7319 return ret;
7321 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7322 if (ret)
7323 return ret;
7325 /* If there are no other options, try atomic_test_and_set if the value
7326 being stored is 1. */
7327 if (val == const1_rtx)
7328 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_ACQUIRE);
7330 return ret;
7333 /* This function expands the atomic test_and_set operation:
7334 atomically store a boolean TRUE into MEM and return the previous value.
7336 MEMMODEL is the memory model variant to use.
7337 TARGET is an optional place to stick the return value. */
7340 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7342 enum machine_mode mode = GET_MODE (mem);
7343 rtx ret, trueval, subtarget;
7345 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7346 if (ret)
7347 return ret;
7349 /* Be binary compatible with non-default settings of trueval, and different
7350 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7351 another only has atomic-exchange. */
7352 if (targetm.atomic_test_and_set_trueval == 1)
7354 trueval = const1_rtx;
7355 subtarget = target ? target : gen_reg_rtx (mode);
7357 else
7359 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7360 subtarget = gen_reg_rtx (mode);
7363 /* Try the atomic-exchange optab... */
7364 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7366 /* ... then an atomic-compare-and-swap loop ... */
7367 if (!ret)
7368 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7370 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7371 if (!ret)
7372 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7374 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7375 things with the value 1. Thus we try again without trueval. */
7376 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7377 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7379 /* Failing all else, assume a single threaded environment and simply
7380 perform the operation. */
7381 if (!ret)
7383 /* If the result is ignored skip the move to target. */
7384 if (subtarget != const0_rtx)
7385 emit_move_insn (subtarget, mem);
7387 emit_move_insn (mem, trueval);
7388 ret = subtarget;
7391 /* Recall that have to return a boolean value; rectify if trueval
7392 is not exactly one. */
7393 if (targetm.atomic_test_and_set_trueval != 1)
7394 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7396 return ret;
7399 /* This function expands the atomic exchange operation:
7400 atomically store VAL in MEM and return the previous value in MEM.
7402 MEMMODEL is the memory model variant to use.
7403 TARGET is an optional place to stick the return value. */
7406 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7408 rtx ret;
7410 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7412 /* Next try a compare-and-swap loop for the exchange. */
7413 if (!ret)
7414 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7416 return ret;
7419 /* This function expands the atomic compare exchange operation:
7421 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7422 *PTARGET_OVAL is an optional place to store the old value from memory.
7423 Both target parameters may be NULL to indicate that we do not care about
7424 that return value. Both target parameters are updated on success to
7425 the actual location of the corresponding result.
7427 MEMMODEL is the memory model variant to use.
7429 The return value of the function is true for success. */
7431 bool
7432 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7433 rtx mem, rtx expected, rtx desired,
7434 bool is_weak, enum memmodel succ_model,
7435 enum memmodel fail_model)
7437 enum machine_mode mode = GET_MODE (mem);
7438 struct expand_operand ops[8];
7439 enum insn_code icode;
7440 rtx target_oval, target_bool = NULL_RTX;
7441 rtx libfunc;
7443 /* Load expected into a register for the compare and swap. */
7444 if (MEM_P (expected))
7445 expected = copy_to_reg (expected);
7447 /* Make sure we always have some place to put the return oldval.
7448 Further, make sure that place is distinct from the input expected,
7449 just in case we need that path down below. */
7450 if (ptarget_oval == NULL
7451 || (target_oval = *ptarget_oval) == NULL
7452 || reg_overlap_mentioned_p (expected, target_oval))
7453 target_oval = gen_reg_rtx (mode);
7455 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7456 if (icode != CODE_FOR_nothing)
7458 enum machine_mode bool_mode = insn_data[icode].operand[0].mode;
7460 /* Make sure we always have a place for the bool operand. */
7461 if (ptarget_bool == NULL
7462 || (target_bool = *ptarget_bool) == NULL
7463 || GET_MODE (target_bool) != bool_mode)
7464 target_bool = gen_reg_rtx (bool_mode);
7466 /* Emit the compare_and_swap. */
7467 create_output_operand (&ops[0], target_bool, bool_mode);
7468 create_output_operand (&ops[1], target_oval, mode);
7469 create_fixed_operand (&ops[2], mem);
7470 create_input_operand (&ops[3], expected, mode);
7471 create_input_operand (&ops[4], desired, mode);
7472 create_integer_operand (&ops[5], is_weak);
7473 create_integer_operand (&ops[6], succ_model);
7474 create_integer_operand (&ops[7], fail_model);
7475 if (maybe_expand_insn (icode, 8, ops))
7477 /* Return success/failure. */
7478 target_bool = ops[0].value;
7479 target_oval = ops[1].value;
7480 goto success;
7484 /* Otherwise fall back to the original __sync_val_compare_and_swap
7485 which is always seq-cst. */
7486 icode = optab_handler (sync_compare_and_swap_optab, mode);
7487 if (icode != CODE_FOR_nothing)
7489 rtx cc_reg;
7491 create_output_operand (&ops[0], target_oval, mode);
7492 create_fixed_operand (&ops[1], mem);
7493 create_input_operand (&ops[2], expected, mode);
7494 create_input_operand (&ops[3], desired, mode);
7495 if (!maybe_expand_insn (icode, 4, ops))
7496 return false;
7498 target_oval = ops[0].value;
7500 /* If the caller isn't interested in the boolean return value,
7501 skip the computation of it. */
7502 if (ptarget_bool == NULL)
7503 goto success;
7505 /* Otherwise, work out if the compare-and-swap succeeded. */
7506 cc_reg = NULL_RTX;
7507 if (have_insn_for (COMPARE, CCmode))
7508 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7509 if (cc_reg)
7511 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7512 const0_rtx, VOIDmode, 0, 1);
7513 goto success;
7515 goto success_bool_from_val;
7518 /* Also check for library support for __sync_val_compare_and_swap. */
7519 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7520 if (libfunc != NULL)
7522 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7523 target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7524 mode, 3, addr, ptr_mode,
7525 expected, mode, desired, mode);
7527 /* Compute the boolean return value only if requested. */
7528 if (ptarget_bool)
7529 goto success_bool_from_val;
7530 else
7531 goto success;
7534 /* Failure. */
7535 return false;
7537 success_bool_from_val:
7538 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7539 expected, VOIDmode, 1, 1);
7540 success:
7541 /* Make sure that the oval output winds up where the caller asked. */
7542 if (ptarget_oval)
7543 *ptarget_oval = target_oval;
7544 if (ptarget_bool)
7545 *ptarget_bool = target_bool;
7546 return true;
7549 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7551 static void
7552 expand_asm_memory_barrier (void)
7554 rtx asm_op, clob;
7556 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7557 rtvec_alloc (0), rtvec_alloc (0),
7558 rtvec_alloc (0), UNKNOWN_LOCATION);
7559 MEM_VOLATILE_P (asm_op) = 1;
7561 clob = gen_rtx_SCRATCH (VOIDmode);
7562 clob = gen_rtx_MEM (BLKmode, clob);
7563 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7565 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7568 /* This routine will either emit the mem_thread_fence pattern or issue a
7569 sync_synchronize to generate a fence for memory model MEMMODEL. */
7571 #ifndef HAVE_mem_thread_fence
7572 # define HAVE_mem_thread_fence 0
7573 # define gen_mem_thread_fence(x) (gcc_unreachable (), NULL_RTX)
7574 #endif
7575 #ifndef HAVE_memory_barrier
7576 # define HAVE_memory_barrier 0
7577 # define gen_memory_barrier() (gcc_unreachable (), NULL_RTX)
7578 #endif
7580 void
7581 expand_mem_thread_fence (enum memmodel model)
7583 if (HAVE_mem_thread_fence)
7584 emit_insn (gen_mem_thread_fence (GEN_INT (model)));
7585 else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
7587 if (HAVE_memory_barrier)
7588 emit_insn (gen_memory_barrier ());
7589 else if (synchronize_libfunc != NULL_RTX)
7590 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7591 else
7592 expand_asm_memory_barrier ();
7596 /* This routine will either emit the mem_signal_fence pattern or issue a
7597 sync_synchronize to generate a fence for memory model MEMMODEL. */
7599 #ifndef HAVE_mem_signal_fence
7600 # define HAVE_mem_signal_fence 0
7601 # define gen_mem_signal_fence(x) (gcc_unreachable (), NULL_RTX)
7602 #endif
7604 void
7605 expand_mem_signal_fence (enum memmodel model)
7607 if (HAVE_mem_signal_fence)
7608 emit_insn (gen_mem_signal_fence (GEN_INT (model)));
7609 else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
7611 /* By default targets are coherent between a thread and the signal
7612 handler running on the same thread. Thus this really becomes a
7613 compiler barrier, in that stores must not be sunk past
7614 (or raised above) a given point. */
7615 expand_asm_memory_barrier ();
7619 /* This function expands the atomic load operation:
7620 return the atomically loaded value in MEM.
7622 MEMMODEL is the memory model variant to use.
7623 TARGET is an option place to stick the return value. */
7626 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7628 enum machine_mode mode = GET_MODE (mem);
7629 enum insn_code icode;
7631 /* If the target supports the load directly, great. */
7632 icode = direct_optab_handler (atomic_load_optab, mode);
7633 if (icode != CODE_FOR_nothing)
7635 struct expand_operand ops[3];
7637 create_output_operand (&ops[0], target, mode);
7638 create_fixed_operand (&ops[1], mem);
7639 create_integer_operand (&ops[2], model);
7640 if (maybe_expand_insn (icode, 3, ops))
7641 return ops[0].value;
7644 /* If the size of the object is greater than word size on this target,
7645 then we assume that a load will not be atomic. */
7646 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7648 /* Issue val = compare_and_swap (mem, 0, 0).
7649 This may cause the occasional harmless store of 0 when the value is
7650 already 0, but it seems to be OK according to the standards guys. */
7651 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7652 const0_rtx, false, model, model))
7653 return target;
7654 else
7655 /* Otherwise there is no atomic load, leave the library call. */
7656 return NULL_RTX;
7659 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7660 if (!target || target == const0_rtx)
7661 target = gen_reg_rtx (mode);
7663 /* For SEQ_CST, emit a barrier before the load. */
7664 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7665 expand_mem_thread_fence (model);
7667 emit_move_insn (target, mem);
7669 /* Emit the appropriate barrier after the load. */
7670 expand_mem_thread_fence (model);
7672 return target;
7675 /* This function expands the atomic store operation:
7676 Atomically store VAL in MEM.
7677 MEMMODEL is the memory model variant to use.
7678 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7679 function returns const0_rtx if a pattern was emitted. */
7682 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7684 enum machine_mode mode = GET_MODE (mem);
7685 enum insn_code icode;
7686 struct expand_operand ops[3];
7688 /* If the target supports the store directly, great. */
7689 icode = direct_optab_handler (atomic_store_optab, mode);
7690 if (icode != CODE_FOR_nothing)
7692 create_fixed_operand (&ops[0], mem);
7693 create_input_operand (&ops[1], val, mode);
7694 create_integer_operand (&ops[2], model);
7695 if (maybe_expand_insn (icode, 3, ops))
7696 return const0_rtx;
7699 /* If using __sync_lock_release is a viable alternative, try it. */
7700 if (use_release)
7702 icode = direct_optab_handler (sync_lock_release_optab, mode);
7703 if (icode != CODE_FOR_nothing)
7705 create_fixed_operand (&ops[0], mem);
7706 create_input_operand (&ops[1], const0_rtx, mode);
7707 if (maybe_expand_insn (icode, 2, ops))
7709 /* lock_release is only a release barrier. */
7710 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7711 expand_mem_thread_fence (model);
7712 return const0_rtx;
7717 /* If the size of the object is greater than word size on this target,
7718 a default store will not be atomic, Try a mem_exchange and throw away
7719 the result. If that doesn't work, don't do anything. */
7720 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7722 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7723 if (!target)
7724 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7725 if (target)
7726 return const0_rtx;
7727 else
7728 return NULL_RTX;
7731 /* Otherwise assume stores are atomic, and emit the proper barriers. */
7732 expand_mem_thread_fence (model);
7734 emit_move_insn (mem, val);
7736 /* For SEQ_CST, also emit a barrier after the store. */
7737 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7738 expand_mem_thread_fence (model);
7740 return const0_rtx;
7744 /* Structure containing the pointers and values required to process the
7745 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7747 struct atomic_op_functions
7749 direct_optab mem_fetch_before;
7750 direct_optab mem_fetch_after;
7751 direct_optab mem_no_result;
7752 optab fetch_before;
7753 optab fetch_after;
7754 direct_optab no_result;
7755 enum rtx_code reverse_code;
7759 /* Fill in structure pointed to by OP with the various optab entries for an
7760 operation of type CODE. */
7762 static void
7763 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7765 gcc_assert (op!= NULL);
7767 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7768 in the source code during compilation, and the optab entries are not
7769 computable until runtime. Fill in the values at runtime. */
7770 switch (code)
7772 case PLUS:
7773 op->mem_fetch_before = atomic_fetch_add_optab;
7774 op->mem_fetch_after = atomic_add_fetch_optab;
7775 op->mem_no_result = atomic_add_optab;
7776 op->fetch_before = sync_old_add_optab;
7777 op->fetch_after = sync_new_add_optab;
7778 op->no_result = sync_add_optab;
7779 op->reverse_code = MINUS;
7780 break;
7781 case MINUS:
7782 op->mem_fetch_before = atomic_fetch_sub_optab;
7783 op->mem_fetch_after = atomic_sub_fetch_optab;
7784 op->mem_no_result = atomic_sub_optab;
7785 op->fetch_before = sync_old_sub_optab;
7786 op->fetch_after = sync_new_sub_optab;
7787 op->no_result = sync_sub_optab;
7788 op->reverse_code = PLUS;
7789 break;
7790 case XOR:
7791 op->mem_fetch_before = atomic_fetch_xor_optab;
7792 op->mem_fetch_after = atomic_xor_fetch_optab;
7793 op->mem_no_result = atomic_xor_optab;
7794 op->fetch_before = sync_old_xor_optab;
7795 op->fetch_after = sync_new_xor_optab;
7796 op->no_result = sync_xor_optab;
7797 op->reverse_code = XOR;
7798 break;
7799 case AND:
7800 op->mem_fetch_before = atomic_fetch_and_optab;
7801 op->mem_fetch_after = atomic_and_fetch_optab;
7802 op->mem_no_result = atomic_and_optab;
7803 op->fetch_before = sync_old_and_optab;
7804 op->fetch_after = sync_new_and_optab;
7805 op->no_result = sync_and_optab;
7806 op->reverse_code = UNKNOWN;
7807 break;
7808 case IOR:
7809 op->mem_fetch_before = atomic_fetch_or_optab;
7810 op->mem_fetch_after = atomic_or_fetch_optab;
7811 op->mem_no_result = atomic_or_optab;
7812 op->fetch_before = sync_old_ior_optab;
7813 op->fetch_after = sync_new_ior_optab;
7814 op->no_result = sync_ior_optab;
7815 op->reverse_code = UNKNOWN;
7816 break;
7817 case NOT:
7818 op->mem_fetch_before = atomic_fetch_nand_optab;
7819 op->mem_fetch_after = atomic_nand_fetch_optab;
7820 op->mem_no_result = atomic_nand_optab;
7821 op->fetch_before = sync_old_nand_optab;
7822 op->fetch_after = sync_new_nand_optab;
7823 op->no_result = sync_nand_optab;
7824 op->reverse_code = UNKNOWN;
7825 break;
7826 default:
7827 gcc_unreachable ();
7831 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7832 using memory order MODEL. If AFTER is true the operation needs to return
7833 the value of *MEM after the operation, otherwise the previous value.
7834 TARGET is an optional place to place the result. The result is unused if
7835 it is const0_rtx.
7836 Return the result if there is a better sequence, otherwise NULL_RTX. */
7838 static rtx
7839 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7840 enum memmodel model, bool after)
7842 /* If the value is prefetched, or not used, it may be possible to replace
7843 the sequence with a native exchange operation. */
7844 if (!after || target == const0_rtx)
7846 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7847 if (code == AND && val == const0_rtx)
7849 if (target == const0_rtx)
7850 target = gen_reg_rtx (GET_MODE (mem));
7851 return maybe_emit_atomic_exchange (target, mem, val, model);
7854 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7855 if (code == IOR && val == constm1_rtx)
7857 if (target == const0_rtx)
7858 target = gen_reg_rtx (GET_MODE (mem));
7859 return maybe_emit_atomic_exchange (target, mem, val, model);
7863 return NULL_RTX;
7866 /* Try to emit an instruction for a specific operation varaition.
7867 OPTAB contains the OP functions.
7868 TARGET is an optional place to return the result. const0_rtx means unused.
7869 MEM is the memory location to operate on.
7870 VAL is the value to use in the operation.
7871 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7872 MODEL is the memory model, if used.
7873 AFTER is true if the returned result is the value after the operation. */
7875 static rtx
7876 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7877 rtx val, bool use_memmodel, enum memmodel model, bool after)
7879 enum machine_mode mode = GET_MODE (mem);
7880 struct expand_operand ops[4];
7881 enum insn_code icode;
7882 int op_counter = 0;
7883 int num_ops;
7885 /* Check to see if there is a result returned. */
7886 if (target == const0_rtx)
7888 if (use_memmodel)
7890 icode = direct_optab_handler (optab->mem_no_result, mode);
7891 create_integer_operand (&ops[2], model);
7892 num_ops = 3;
7894 else
7896 icode = direct_optab_handler (optab->no_result, mode);
7897 num_ops = 2;
7900 /* Otherwise, we need to generate a result. */
7901 else
7903 if (use_memmodel)
7905 icode = direct_optab_handler (after ? optab->mem_fetch_after
7906 : optab->mem_fetch_before, mode);
7907 create_integer_operand (&ops[3], model);
7908 num_ops = 4;
7910 else
7912 icode = optab_handler (after ? optab->fetch_after
7913 : optab->fetch_before, mode);
7914 num_ops = 3;
7916 create_output_operand (&ops[op_counter++], target, mode);
7918 if (icode == CODE_FOR_nothing)
7919 return NULL_RTX;
7921 create_fixed_operand (&ops[op_counter++], mem);
7922 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7923 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7925 if (maybe_expand_insn (icode, num_ops, ops))
7926 return (target == const0_rtx ? const0_rtx : ops[0].value);
7928 return NULL_RTX;
7932 /* This function expands an atomic fetch_OP or OP_fetch operation:
7933 TARGET is an option place to stick the return value. const0_rtx indicates
7934 the result is unused.
7935 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7936 CODE is the operation being performed (OP)
7937 MEMMODEL is the memory model variant to use.
7938 AFTER is true to return the result of the operation (OP_fetch).
7939 AFTER is false to return the value before the operation (fetch_OP).
7941 This function will *only* generate instructions if there is a direct
7942 optab. No compare and swap loops or libcalls will be generated. */
7944 static rtx
7945 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7946 enum rtx_code code, enum memmodel model,
7947 bool after)
7949 enum machine_mode mode = GET_MODE (mem);
7950 struct atomic_op_functions optab;
7951 rtx result;
7952 bool unused_result = (target == const0_rtx);
7954 get_atomic_op_for_code (&optab, code);
7956 /* Check to see if there are any better instructions. */
7957 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7958 if (result)
7959 return result;
7961 /* Check for the case where the result isn't used and try those patterns. */
7962 if (unused_result)
7964 /* Try the memory model variant first. */
7965 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7966 if (result)
7967 return result;
7969 /* Next try the old style withuot a memory model. */
7970 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7971 if (result)
7972 return result;
7974 /* There is no no-result pattern, so try patterns with a result. */
7975 target = NULL_RTX;
7978 /* Try the __atomic version. */
7979 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
7980 if (result)
7981 return result;
7983 /* Try the older __sync version. */
7984 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
7985 if (result)
7986 return result;
7988 /* If the fetch value can be calculated from the other variation of fetch,
7989 try that operation. */
7990 if (after || unused_result || optab.reverse_code != UNKNOWN)
7992 /* Try the __atomic version, then the older __sync version. */
7993 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
7994 if (!result)
7995 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
7997 if (result)
7999 /* If the result isn't used, no need to do compensation code. */
8000 if (unused_result)
8001 return result;
8003 /* Issue compensation code. Fetch_after == fetch_before OP val.
8004 Fetch_before == after REVERSE_OP val. */
8005 if (!after)
8006 code = optab.reverse_code;
8007 if (code == NOT)
8009 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
8010 true, OPTAB_LIB_WIDEN);
8011 result = expand_simple_unop (mode, NOT, result, target, true);
8013 else
8014 result = expand_simple_binop (mode, code, result, val, target,
8015 true, OPTAB_LIB_WIDEN);
8016 return result;
8020 /* No direct opcode can be generated. */
8021 return NULL_RTX;
8026 /* This function expands an atomic fetch_OP or OP_fetch operation:
8027 TARGET is an option place to stick the return value. const0_rtx indicates
8028 the result is unused.
8029 atomically fetch MEM, perform the operation with VAL and return it to MEM.
8030 CODE is the operation being performed (OP)
8031 MEMMODEL is the memory model variant to use.
8032 AFTER is true to return the result of the operation (OP_fetch).
8033 AFTER is false to return the value before the operation (fetch_OP). */
8035 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
8036 enum memmodel model, bool after)
8038 enum machine_mode mode = GET_MODE (mem);
8039 rtx result;
8040 bool unused_result = (target == const0_rtx);
8042 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
8043 after);
8045 if (result)
8046 return result;
8048 /* Add/sub can be implemented by doing the reverse operation with -(val). */
8049 if (code == PLUS || code == MINUS)
8051 rtx tmp;
8052 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
8054 start_sequence ();
8055 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
8056 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
8057 model, after);
8058 if (result)
8060 /* PLUS worked so emit the insns and return. */
8061 tmp = get_insns ();
8062 end_sequence ();
8063 emit_insn (tmp);
8064 return result;
8067 /* PLUS did not work, so throw away the negation code and continue. */
8068 end_sequence ();
8071 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
8072 if (!can_compare_and_swap_p (mode, false))
8074 rtx libfunc;
8075 bool fixup = false;
8076 enum rtx_code orig_code = code;
8077 struct atomic_op_functions optab;
8079 get_atomic_op_for_code (&optab, code);
8080 libfunc = optab_libfunc (after ? optab.fetch_after
8081 : optab.fetch_before, mode);
8082 if (libfunc == NULL
8083 && (after || unused_result || optab.reverse_code != UNKNOWN))
8085 fixup = true;
8086 if (!after)
8087 code = optab.reverse_code;
8088 libfunc = optab_libfunc (after ? optab.fetch_before
8089 : optab.fetch_after, mode);
8091 if (libfunc != NULL)
8093 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
8094 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
8095 2, addr, ptr_mode, val, mode);
8097 if (!unused_result && fixup)
8098 result = expand_simple_binop (mode, code, result, val, target,
8099 true, OPTAB_LIB_WIDEN);
8100 return result;
8103 /* We need the original code for any further attempts. */
8104 code = orig_code;
8107 /* If nothing else has succeeded, default to a compare and swap loop. */
8108 if (can_compare_and_swap_p (mode, true))
8110 rtx_insn *insn;
8111 rtx t0 = gen_reg_rtx (mode), t1;
8113 start_sequence ();
8115 /* If the result is used, get a register for it. */
8116 if (!unused_result)
8118 if (!target || !register_operand (target, mode))
8119 target = gen_reg_rtx (mode);
8120 /* If fetch_before, copy the value now. */
8121 if (!after)
8122 emit_move_insn (target, t0);
8124 else
8125 target = const0_rtx;
8127 t1 = t0;
8128 if (code == NOT)
8130 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
8131 true, OPTAB_LIB_WIDEN);
8132 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
8134 else
8135 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
8136 OPTAB_LIB_WIDEN);
8138 /* For after, copy the value now. */
8139 if (!unused_result && after)
8140 emit_move_insn (target, t1);
8141 insn = get_insns ();
8142 end_sequence ();
8144 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
8145 return target;
8148 return NULL_RTX;
8151 /* Return true if OPERAND is suitable for operand number OPNO of
8152 instruction ICODE. */
8154 bool
8155 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
8157 return (!insn_data[(int) icode].operand[opno].predicate
8158 || (insn_data[(int) icode].operand[opno].predicate
8159 (operand, insn_data[(int) icode].operand[opno].mode)));
8162 /* TARGET is a target of a multiword operation that we are going to
8163 implement as a series of word-mode operations. Return true if
8164 TARGET is suitable for this purpose. */
8166 bool
8167 valid_multiword_target_p (rtx target)
8169 enum machine_mode mode;
8170 int i;
8172 mode = GET_MODE (target);
8173 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
8174 if (!validate_subreg (word_mode, mode, target, i))
8175 return false;
8176 return true;
8179 /* Like maybe_legitimize_operand, but do not change the code of the
8180 current rtx value. */
8182 static bool
8183 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8184 struct expand_operand *op)
8186 /* See if the operand matches in its current form. */
8187 if (insn_operand_matches (icode, opno, op->value))
8188 return true;
8190 /* If the operand is a memory whose address has no side effects,
8191 try forcing the address into a non-virtual pseudo register.
8192 The check for side effects is important because copy_to_mode_reg
8193 cannot handle things like auto-modified addresses. */
8194 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8196 rtx addr, mem;
8198 mem = op->value;
8199 addr = XEXP (mem, 0);
8200 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8201 && !side_effects_p (addr))
8203 rtx_insn *last;
8204 enum machine_mode mode;
8206 last = get_last_insn ();
8207 mode = get_address_mode (mem);
8208 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8209 if (insn_operand_matches (icode, opno, mem))
8211 op->value = mem;
8212 return true;
8214 delete_insns_since (last);
8218 return false;
8221 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8222 on success, storing the new operand value back in OP. */
8224 static bool
8225 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8226 struct expand_operand *op)
8228 enum machine_mode mode, imode;
8229 bool old_volatile_ok, result;
8231 mode = op->mode;
8232 switch (op->type)
8234 case EXPAND_FIXED:
8235 old_volatile_ok = volatile_ok;
8236 volatile_ok = true;
8237 result = maybe_legitimize_operand_same_code (icode, opno, op);
8238 volatile_ok = old_volatile_ok;
8239 return result;
8241 case EXPAND_OUTPUT:
8242 gcc_assert (mode != VOIDmode);
8243 if (op->value
8244 && op->value != const0_rtx
8245 && GET_MODE (op->value) == mode
8246 && maybe_legitimize_operand_same_code (icode, opno, op))
8247 return true;
8249 op->value = gen_reg_rtx (mode);
8250 break;
8252 case EXPAND_INPUT:
8253 input:
8254 gcc_assert (mode != VOIDmode);
8255 gcc_assert (GET_MODE (op->value) == VOIDmode
8256 || GET_MODE (op->value) == mode);
8257 if (maybe_legitimize_operand_same_code (icode, opno, op))
8258 return true;
8260 op->value = copy_to_mode_reg (mode, op->value);
8261 break;
8263 case EXPAND_CONVERT_TO:
8264 gcc_assert (mode != VOIDmode);
8265 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8266 goto input;
8268 case EXPAND_CONVERT_FROM:
8269 if (GET_MODE (op->value) != VOIDmode)
8270 mode = GET_MODE (op->value);
8271 else
8272 /* The caller must tell us what mode this value has. */
8273 gcc_assert (mode != VOIDmode);
8275 imode = insn_data[(int) icode].operand[opno].mode;
8276 if (imode != VOIDmode && imode != mode)
8278 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8279 mode = imode;
8281 goto input;
8283 case EXPAND_ADDRESS:
8284 gcc_assert (mode != VOIDmode);
8285 op->value = convert_memory_address (mode, op->value);
8286 goto input;
8288 case EXPAND_INTEGER:
8289 mode = insn_data[(int) icode].operand[opno].mode;
8290 if (mode != VOIDmode && const_int_operand (op->value, mode))
8291 goto input;
8292 break;
8294 return insn_operand_matches (icode, opno, op->value);
8297 /* Make OP describe an input operand that should have the same value
8298 as VALUE, after any mode conversion that the target might request.
8299 TYPE is the type of VALUE. */
8301 void
8302 create_convert_operand_from_type (struct expand_operand *op,
8303 rtx value, tree type)
8305 create_convert_operand_from (op, value, TYPE_MODE (type),
8306 TYPE_UNSIGNED (type));
8309 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8310 of instruction ICODE. Return true on success, leaving the new operand
8311 values in the OPS themselves. Emit no code on failure. */
8313 bool
8314 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8315 unsigned int nops, struct expand_operand *ops)
8317 rtx_insn *last;
8318 unsigned int i;
8320 last = get_last_insn ();
8321 for (i = 0; i < nops; i++)
8322 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8324 delete_insns_since (last);
8325 return false;
8327 return true;
8330 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8331 as its operands. Return the instruction pattern on success,
8332 and emit any necessary set-up code. Return null and emit no
8333 code on failure. */
8336 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8337 struct expand_operand *ops)
8339 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8340 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8341 return NULL_RTX;
8343 switch (nops)
8345 case 1:
8346 return GEN_FCN (icode) (ops[0].value);
8347 case 2:
8348 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8349 case 3:
8350 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8351 case 4:
8352 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8353 ops[3].value);
8354 case 5:
8355 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8356 ops[3].value, ops[4].value);
8357 case 6:
8358 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8359 ops[3].value, ops[4].value, ops[5].value);
8360 case 7:
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);
8364 case 8:
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);
8368 case 9:
8369 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8370 ops[3].value, ops[4].value, ops[5].value,
8371 ops[6].value, ops[7].value, ops[8].value);
8373 gcc_unreachable ();
8376 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8377 as its operands. Return true on success and emit no code on failure. */
8379 bool
8380 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8381 struct expand_operand *ops)
8383 rtx pat = maybe_gen_insn (icode, nops, ops);
8384 if (pat)
8386 emit_insn (pat);
8387 return true;
8389 return false;
8392 /* Like maybe_expand_insn, but for jumps. */
8394 bool
8395 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8396 struct expand_operand *ops)
8398 rtx pat = maybe_gen_insn (icode, nops, ops);
8399 if (pat)
8401 emit_jump_insn (pat);
8402 return true;
8404 return false;
8407 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8408 as its operands. */
8410 void
8411 expand_insn (enum insn_code icode, unsigned int nops,
8412 struct expand_operand *ops)
8414 if (!maybe_expand_insn (icode, nops, ops))
8415 gcc_unreachable ();
8418 /* Like expand_insn, but for jumps. */
8420 void
8421 expand_jump_insn (enum insn_code icode, unsigned int nops,
8422 struct expand_operand *ops)
8424 if (!maybe_expand_jump_insn (icode, nops, ops))
8425 gcc_unreachable ();
8428 /* Reduce conditional compilation elsewhere. */
8429 #ifndef HAVE_insv
8430 #define HAVE_insv 0
8431 #define CODE_FOR_insv CODE_FOR_nothing
8432 #endif
8433 #ifndef HAVE_extv
8434 #define HAVE_extv 0
8435 #define CODE_FOR_extv CODE_FOR_nothing
8436 #endif
8437 #ifndef HAVE_extzv
8438 #define HAVE_extzv 0
8439 #define CODE_FOR_extzv CODE_FOR_nothing
8440 #endif
8442 /* Enumerates the possible types of structure operand to an
8443 extraction_insn. */
8444 enum extraction_type { ET_unaligned_mem, ET_reg };
8446 /* Check whether insv, extv or extzv pattern ICODE can be used for an
8447 insertion or extraction of type TYPE on a structure of mode MODE.
8448 Return true if so and fill in *INSN accordingly. STRUCT_OP is the
8449 operand number of the structure (the first sign_extract or zero_extract
8450 operand) and FIELD_OP is the operand number of the field (the other
8451 side of the set from the sign_extract or zero_extract). */
8453 static bool
8454 get_traditional_extraction_insn (extraction_insn *insn,
8455 enum extraction_type type,
8456 enum machine_mode mode,
8457 enum insn_code icode,
8458 int struct_op, int field_op)
8460 const struct insn_data_d *data = &insn_data[icode];
8462 enum machine_mode struct_mode = data->operand[struct_op].mode;
8463 if (struct_mode == VOIDmode)
8464 struct_mode = word_mode;
8465 if (mode != struct_mode)
8466 return false;
8468 enum machine_mode field_mode = data->operand[field_op].mode;
8469 if (field_mode == VOIDmode)
8470 field_mode = word_mode;
8472 enum machine_mode pos_mode = data->operand[struct_op + 2].mode;
8473 if (pos_mode == VOIDmode)
8474 pos_mode = word_mode;
8476 insn->icode = icode;
8477 insn->field_mode = field_mode;
8478 insn->struct_mode = (type == ET_unaligned_mem ? byte_mode : struct_mode);
8479 insn->pos_mode = pos_mode;
8480 return true;
8483 /* Return true if an optab exists to perform an insertion or extraction
8484 of type TYPE in mode MODE. Describe the instruction in *INSN if so.
8486 REG_OPTAB is the optab to use for register structures and
8487 MISALIGN_OPTAB is the optab to use for misaligned memory structures.
8488 POS_OP is the operand number of the bit position. */
8490 static bool
8491 get_optab_extraction_insn (struct extraction_insn *insn,
8492 enum extraction_type type,
8493 enum machine_mode mode, direct_optab reg_optab,
8494 direct_optab misalign_optab, int pos_op)
8496 direct_optab optab = (type == ET_unaligned_mem ? misalign_optab : reg_optab);
8497 enum insn_code icode = direct_optab_handler (optab, mode);
8498 if (icode == CODE_FOR_nothing)
8499 return false;
8501 const struct insn_data_d *data = &insn_data[icode];
8503 insn->icode = icode;
8504 insn->field_mode = mode;
8505 insn->struct_mode = (type == ET_unaligned_mem ? BLKmode : mode);
8506 insn->pos_mode = data->operand[pos_op].mode;
8507 if (insn->pos_mode == VOIDmode)
8508 insn->pos_mode = word_mode;
8509 return true;
8512 /* Return true if an instruction exists to perform an insertion or
8513 extraction (PATTERN says which) of type TYPE in mode MODE.
8514 Describe the instruction in *INSN if so. */
8516 static bool
8517 get_extraction_insn (extraction_insn *insn,
8518 enum extraction_pattern pattern,
8519 enum extraction_type type,
8520 enum machine_mode mode)
8522 switch (pattern)
8524 case EP_insv:
8525 if (HAVE_insv
8526 && get_traditional_extraction_insn (insn, type, mode,
8527 CODE_FOR_insv, 0, 3))
8528 return true;
8529 return get_optab_extraction_insn (insn, type, mode, insv_optab,
8530 insvmisalign_optab, 2);
8532 case EP_extv:
8533 if (HAVE_extv
8534 && get_traditional_extraction_insn (insn, type, mode,
8535 CODE_FOR_extv, 1, 0))
8536 return true;
8537 return get_optab_extraction_insn (insn, type, mode, extv_optab,
8538 extvmisalign_optab, 3);
8540 case EP_extzv:
8541 if (HAVE_extzv
8542 && get_traditional_extraction_insn (insn, type, mode,
8543 CODE_FOR_extzv, 1, 0))
8544 return true;
8545 return get_optab_extraction_insn (insn, type, mode, extzv_optab,
8546 extzvmisalign_optab, 3);
8548 default:
8549 gcc_unreachable ();
8553 /* Return true if an instruction exists to access a field of mode
8554 FIELDMODE in a structure that has STRUCT_BITS significant bits.
8555 Describe the "best" such instruction in *INSN if so. PATTERN and
8556 TYPE describe the type of insertion or extraction we want to perform.
8558 For an insertion, the number of significant structure bits includes
8559 all bits of the target. For an extraction, it need only include the
8560 most significant bit of the field. Larger widths are acceptable
8561 in both cases. */
8563 static bool
8564 get_best_extraction_insn (extraction_insn *insn,
8565 enum extraction_pattern pattern,
8566 enum extraction_type type,
8567 unsigned HOST_WIDE_INT struct_bits,
8568 enum machine_mode field_mode)
8570 enum machine_mode mode = smallest_mode_for_size (struct_bits, MODE_INT);
8571 while (mode != VOIDmode)
8573 if (get_extraction_insn (insn, pattern, type, mode))
8575 while (mode != VOIDmode
8576 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (field_mode)
8577 && !TRULY_NOOP_TRUNCATION_MODES_P (insn->field_mode,
8578 field_mode))
8580 get_extraction_insn (insn, pattern, type, mode);
8581 mode = GET_MODE_WIDER_MODE (mode);
8583 return true;
8585 mode = GET_MODE_WIDER_MODE (mode);
8587 return false;
8590 /* Return true if an instruction exists to access a field of mode
8591 FIELDMODE in a register structure that has STRUCT_BITS significant bits.
8592 Describe the "best" such instruction in *INSN if so. PATTERN describes
8593 the type of insertion or extraction we want to perform.
8595 For an insertion, the number of significant structure bits includes
8596 all bits of the target. For an extraction, it need only include the
8597 most significant bit of the field. Larger widths are acceptable
8598 in both cases. */
8600 bool
8601 get_best_reg_extraction_insn (extraction_insn *insn,
8602 enum extraction_pattern pattern,
8603 unsigned HOST_WIDE_INT struct_bits,
8604 enum machine_mode field_mode)
8606 return get_best_extraction_insn (insn, pattern, ET_reg, struct_bits,
8607 field_mode);
8610 /* Return true if an instruction exists to access a field of BITSIZE
8611 bits starting BITNUM bits into a memory structure. Describe the
8612 "best" such instruction in *INSN if so. PATTERN describes the type
8613 of insertion or extraction we want to perform and FIELDMODE is the
8614 natural mode of the extracted field.
8616 The instructions considered here only access bytes that overlap
8617 the bitfield; they do not touch any surrounding bytes. */
8619 bool
8620 get_best_mem_extraction_insn (extraction_insn *insn,
8621 enum extraction_pattern pattern,
8622 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitnum,
8623 enum machine_mode field_mode)
8625 unsigned HOST_WIDE_INT struct_bits = (bitnum % BITS_PER_UNIT
8626 + bitsize
8627 + BITS_PER_UNIT - 1);
8628 struct_bits -= struct_bits % BITS_PER_UNIT;
8629 return get_best_extraction_insn (insn, pattern, ET_unaligned_mem,
8630 struct_bits, field_mode);
8633 #include "gt-optabs.h"