2014-04-14 Martin Jambor <mjambor@suse.cz>
[official-gcc.git] / gcc / optabs.c
blob54f07ab02b9a26a9ac7cdd1e80f279aa77cbd427
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, 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 insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
182 rtx last_insn, set;
183 rtx note;
185 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
187 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
188 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
189 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
190 && GET_RTX_CLASS (code) != RTX_COMPARE
191 && GET_RTX_CLASS (code) != RTX_UNARY)
192 return 1;
194 if (GET_CODE (target) == ZERO_EXTRACT)
195 return 1;
197 for (last_insn = insns;
198 NEXT_INSN (last_insn) != NULL_RTX;
199 last_insn = NEXT_INSN (last_insn))
202 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
203 a value changing in the insn, so the note would be invalid for CSE. */
204 if (reg_overlap_mentioned_p (target, op0)
205 || (op1 && reg_overlap_mentioned_p (target, op1)))
207 if (MEM_P (target)
208 && (rtx_equal_p (target, op0)
209 || (op1 && rtx_equal_p (target, op1))))
211 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
212 over expanding it as temp = MEM op X, MEM = temp. If the target
213 supports MEM = MEM op X instructions, it is sometimes too hard
214 to reconstruct that form later, especially if X is also a memory,
215 and due to multiple occurrences of addresses the address might
216 be forced into register unnecessarily.
217 Note that not emitting the REG_EQUIV note might inhibit
218 CSE in some cases. */
219 set = single_set (last_insn);
220 if (set
221 && GET_CODE (SET_SRC (set)) == code
222 && MEM_P (SET_DEST (set))
223 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
224 || (op1 && rtx_equal_p (SET_DEST (set),
225 XEXP (SET_SRC (set), 1)))))
226 return 1;
228 return 0;
231 set = single_set (last_insn);
232 if (set == NULL_RTX)
233 return 1;
235 if (! rtx_equal_p (SET_DEST (set), target)
236 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
237 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
238 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
239 return 1;
241 if (GET_RTX_CLASS (code) == RTX_UNARY)
242 switch (code)
244 case FFS:
245 case CLZ:
246 case CTZ:
247 case CLRSB:
248 case POPCOUNT:
249 case PARITY:
250 case BSWAP:
251 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
253 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
254 if (GET_MODE_SIZE (GET_MODE (op0))
255 > GET_MODE_SIZE (GET_MODE (target)))
256 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
257 note, GET_MODE (op0));
258 else
259 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
260 note, GET_MODE (op0));
261 break;
263 /* FALLTHRU */
264 default:
265 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
266 break;
268 else
269 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
271 set_unique_reg_note (last_insn, REG_EQUAL, note);
273 return 1;
276 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
277 for a widening operation would be. In most cases this would be OP0, but if
278 that's a constant it'll be VOIDmode, which isn't useful. */
280 static enum machine_mode
281 widened_mode (enum machine_mode to_mode, rtx op0, rtx op1)
283 enum machine_mode m0 = GET_MODE (op0);
284 enum machine_mode m1 = GET_MODE (op1);
285 enum machine_mode result;
287 if (m0 == VOIDmode && m1 == VOIDmode)
288 return to_mode;
289 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
290 result = m1;
291 else
292 result = m0;
294 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
295 return to_mode;
297 return result;
300 /* Find a widening optab even if it doesn't widen as much as we want.
301 E.g. if from_mode is HImode, and to_mode is DImode, and there is no
302 direct HI->SI insn, then return SI->DI, if that exists.
303 If PERMIT_NON_WIDENING is non-zero then this can be used with
304 non-widening optabs also. */
306 enum insn_code
307 find_widening_optab_handler_and_mode (optab op, enum machine_mode to_mode,
308 enum machine_mode from_mode,
309 int permit_non_widening,
310 enum machine_mode *found_mode)
312 for (; (permit_non_widening || from_mode != to_mode)
313 && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
314 && from_mode != VOIDmode;
315 from_mode = GET_MODE_WIDER_MODE (from_mode))
317 enum insn_code handler = widening_optab_handler (op, to_mode,
318 from_mode);
320 if (handler != CODE_FOR_nothing)
322 if (found_mode)
323 *found_mode = from_mode;
324 return handler;
328 return CODE_FOR_nothing;
331 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
332 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
333 not actually do a sign-extend or zero-extend, but can leave the
334 higher-order bits of the result rtx undefined, for example, in the case
335 of logical operations, but not right shifts. */
337 static rtx
338 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
339 int unsignedp, int no_extend)
341 rtx result;
343 /* If we don't have to extend and this is a constant, return it. */
344 if (no_extend && GET_MODE (op) == VOIDmode)
345 return op;
347 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
348 extend since it will be more efficient to do so unless the signedness of
349 a promoted object differs from our extension. */
350 if (! no_extend
351 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
352 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
353 return convert_modes (mode, oldmode, op, unsignedp);
355 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
356 SUBREG. */
357 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
358 return gen_lowpart (mode, force_reg (GET_MODE (op), op));
360 /* Otherwise, get an object of MODE, clobber it, and set the low-order
361 part to OP. */
363 result = gen_reg_rtx (mode);
364 emit_clobber (result);
365 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
366 return result;
369 /* Return the optab used for computing the operation given by the tree code,
370 CODE and the tree EXP. This function is not always usable (for example, it
371 cannot give complete results for multiplication or division) but probably
372 ought to be relied on more widely throughout the expander. */
373 optab
374 optab_for_tree_code (enum tree_code code, const_tree type,
375 enum optab_subtype subtype)
377 bool trapv;
378 switch (code)
380 case BIT_AND_EXPR:
381 return and_optab;
383 case BIT_IOR_EXPR:
384 return ior_optab;
386 case BIT_NOT_EXPR:
387 return one_cmpl_optab;
389 case BIT_XOR_EXPR:
390 return xor_optab;
392 case MULT_HIGHPART_EXPR:
393 return TYPE_UNSIGNED (type) ? umul_highpart_optab : smul_highpart_optab;
395 case TRUNC_MOD_EXPR:
396 case CEIL_MOD_EXPR:
397 case FLOOR_MOD_EXPR:
398 case ROUND_MOD_EXPR:
399 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
401 case RDIV_EXPR:
402 case TRUNC_DIV_EXPR:
403 case CEIL_DIV_EXPR:
404 case FLOOR_DIV_EXPR:
405 case ROUND_DIV_EXPR:
406 case EXACT_DIV_EXPR:
407 if (TYPE_SATURATING (type))
408 return TYPE_UNSIGNED (type) ? usdiv_optab : ssdiv_optab;
409 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
411 case LSHIFT_EXPR:
412 if (TREE_CODE (type) == VECTOR_TYPE)
414 if (subtype == optab_vector)
415 return TYPE_SATURATING (type) ? unknown_optab : vashl_optab;
417 gcc_assert (subtype == optab_scalar);
419 if (TYPE_SATURATING (type))
420 return TYPE_UNSIGNED (type) ? usashl_optab : ssashl_optab;
421 return ashl_optab;
423 case RSHIFT_EXPR:
424 if (TREE_CODE (type) == VECTOR_TYPE)
426 if (subtype == optab_vector)
427 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
429 gcc_assert (subtype == optab_scalar);
431 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
433 case LROTATE_EXPR:
434 if (TREE_CODE (type) == VECTOR_TYPE)
436 if (subtype == optab_vector)
437 return vrotl_optab;
439 gcc_assert (subtype == optab_scalar);
441 return rotl_optab;
443 case RROTATE_EXPR:
444 if (TREE_CODE (type) == VECTOR_TYPE)
446 if (subtype == optab_vector)
447 return vrotr_optab;
449 gcc_assert (subtype == optab_scalar);
451 return rotr_optab;
453 case MAX_EXPR:
454 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
456 case MIN_EXPR:
457 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
459 case REALIGN_LOAD_EXPR:
460 return vec_realign_load_optab;
462 case WIDEN_SUM_EXPR:
463 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
465 case DOT_PROD_EXPR:
466 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
468 case WIDEN_MULT_PLUS_EXPR:
469 return (TYPE_UNSIGNED (type)
470 ? (TYPE_SATURATING (type)
471 ? usmadd_widen_optab : umadd_widen_optab)
472 : (TYPE_SATURATING (type)
473 ? ssmadd_widen_optab : smadd_widen_optab));
475 case WIDEN_MULT_MINUS_EXPR:
476 return (TYPE_UNSIGNED (type)
477 ? (TYPE_SATURATING (type)
478 ? usmsub_widen_optab : umsub_widen_optab)
479 : (TYPE_SATURATING (type)
480 ? ssmsub_widen_optab : smsub_widen_optab));
482 case FMA_EXPR:
483 return fma_optab;
485 case REDUC_MAX_EXPR:
486 return TYPE_UNSIGNED (type) ? reduc_umax_optab : reduc_smax_optab;
488 case REDUC_MIN_EXPR:
489 return TYPE_UNSIGNED (type) ? reduc_umin_optab : reduc_smin_optab;
491 case REDUC_PLUS_EXPR:
492 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
494 case VEC_LSHIFT_EXPR:
495 return vec_shl_optab;
497 case VEC_RSHIFT_EXPR:
498 return vec_shr_optab;
500 case VEC_WIDEN_MULT_HI_EXPR:
501 return TYPE_UNSIGNED (type) ?
502 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
504 case VEC_WIDEN_MULT_LO_EXPR:
505 return TYPE_UNSIGNED (type) ?
506 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
508 case VEC_WIDEN_MULT_EVEN_EXPR:
509 return TYPE_UNSIGNED (type) ?
510 vec_widen_umult_even_optab : vec_widen_smult_even_optab;
512 case VEC_WIDEN_MULT_ODD_EXPR:
513 return TYPE_UNSIGNED (type) ?
514 vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
516 case VEC_WIDEN_LSHIFT_HI_EXPR:
517 return TYPE_UNSIGNED (type) ?
518 vec_widen_ushiftl_hi_optab : vec_widen_sshiftl_hi_optab;
520 case VEC_WIDEN_LSHIFT_LO_EXPR:
521 return TYPE_UNSIGNED (type) ?
522 vec_widen_ushiftl_lo_optab : vec_widen_sshiftl_lo_optab;
524 case VEC_UNPACK_HI_EXPR:
525 return TYPE_UNSIGNED (type) ?
526 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
528 case VEC_UNPACK_LO_EXPR:
529 return TYPE_UNSIGNED (type) ?
530 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
532 case VEC_UNPACK_FLOAT_HI_EXPR:
533 /* The signedness is determined from input operand. */
534 return TYPE_UNSIGNED (type) ?
535 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
537 case VEC_UNPACK_FLOAT_LO_EXPR:
538 /* The signedness is determined from input operand. */
539 return TYPE_UNSIGNED (type) ?
540 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
542 case VEC_PACK_TRUNC_EXPR:
543 return vec_pack_trunc_optab;
545 case VEC_PACK_SAT_EXPR:
546 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
548 case VEC_PACK_FIX_TRUNC_EXPR:
549 /* The signedness is determined from output operand. */
550 return TYPE_UNSIGNED (type) ?
551 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
553 default:
554 break;
557 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
558 switch (code)
560 case POINTER_PLUS_EXPR:
561 case PLUS_EXPR:
562 if (TYPE_SATURATING (type))
563 return TYPE_UNSIGNED (type) ? usadd_optab : ssadd_optab;
564 return trapv ? addv_optab : add_optab;
566 case MINUS_EXPR:
567 if (TYPE_SATURATING (type))
568 return TYPE_UNSIGNED (type) ? ussub_optab : sssub_optab;
569 return trapv ? subv_optab : sub_optab;
571 case MULT_EXPR:
572 if (TYPE_SATURATING (type))
573 return TYPE_UNSIGNED (type) ? usmul_optab : ssmul_optab;
574 return trapv ? smulv_optab : smul_optab;
576 case NEGATE_EXPR:
577 if (TYPE_SATURATING (type))
578 return TYPE_UNSIGNED (type) ? usneg_optab : ssneg_optab;
579 return trapv ? negv_optab : neg_optab;
581 case ABS_EXPR:
582 return trapv ? absv_optab : abs_optab;
584 default:
585 return unknown_optab;
590 /* Expand vector widening operations.
592 There are two different classes of operations handled here:
593 1) Operations whose result is wider than all the arguments to the operation.
594 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
595 In this case OP0 and optionally OP1 would be initialized,
596 but WIDE_OP wouldn't (not relevant for this case).
597 2) Operations whose result is of the same size as the last argument to the
598 operation, but wider than all the other arguments to the operation.
599 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
600 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
602 E.g, when called to expand the following operations, this is how
603 the arguments will be initialized:
604 nops OP0 OP1 WIDE_OP
605 widening-sum 2 oprnd0 - oprnd1
606 widening-dot-product 3 oprnd0 oprnd1 oprnd2
607 widening-mult 2 oprnd0 oprnd1 -
608 type-promotion (vec-unpack) 1 oprnd0 - - */
611 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
612 rtx target, int unsignedp)
614 struct expand_operand eops[4];
615 tree oprnd0, oprnd1, oprnd2;
616 enum machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
617 optab widen_pattern_optab;
618 enum insn_code icode;
619 int nops = TREE_CODE_LENGTH (ops->code);
620 int op;
622 oprnd0 = ops->op0;
623 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
624 widen_pattern_optab =
625 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
626 if (ops->code == WIDEN_MULT_PLUS_EXPR
627 || ops->code == WIDEN_MULT_MINUS_EXPR)
628 icode = find_widening_optab_handler (widen_pattern_optab,
629 TYPE_MODE (TREE_TYPE (ops->op2)),
630 tmode0, 0);
631 else
632 icode = optab_handler (widen_pattern_optab, tmode0);
633 gcc_assert (icode != CODE_FOR_nothing);
635 if (nops >= 2)
637 oprnd1 = ops->op1;
638 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
641 /* The last operand is of a wider mode than the rest of the operands. */
642 if (nops == 2)
643 wmode = tmode1;
644 else if (nops == 3)
646 gcc_assert (tmode1 == tmode0);
647 gcc_assert (op1);
648 oprnd2 = ops->op2;
649 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
652 op = 0;
653 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
654 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
655 if (op1)
656 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
657 if (wide_op)
658 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
659 expand_insn (icode, op, eops);
660 return eops[0].value;
663 /* Generate code to perform an operation specified by TERNARY_OPTAB
664 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
666 UNSIGNEDP is for the case where we have to widen the operands
667 to perform the operation. It says to use zero-extension.
669 If TARGET is nonzero, the value
670 is generated there, if it is convenient to do so.
671 In all cases an rtx is returned for the locus of the value;
672 this may or may not be TARGET. */
675 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
676 rtx op1, rtx op2, rtx target, int unsignedp)
678 struct expand_operand ops[4];
679 enum insn_code icode = optab_handler (ternary_optab, mode);
681 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
683 create_output_operand (&ops[0], target, mode);
684 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
685 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
686 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
687 expand_insn (icode, 4, ops);
688 return ops[0].value;
692 /* Like expand_binop, but return a constant rtx if the result can be
693 calculated at compile time. The arguments and return value are
694 otherwise the same as for expand_binop. */
697 simplify_expand_binop (enum machine_mode mode, optab binoptab,
698 rtx op0, rtx op1, rtx target, int unsignedp,
699 enum optab_methods methods)
701 if (CONSTANT_P (op0) && CONSTANT_P (op1))
703 rtx x = simplify_binary_operation (optab_to_code (binoptab),
704 mode, op0, op1);
705 if (x)
706 return x;
709 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
712 /* Like simplify_expand_binop, but always put the result in TARGET.
713 Return true if the expansion succeeded. */
715 bool
716 force_expand_binop (enum machine_mode mode, optab binoptab,
717 rtx op0, rtx op1, rtx target, int unsignedp,
718 enum optab_methods methods)
720 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
721 target, unsignedp, methods);
722 if (x == 0)
723 return false;
724 if (x != target)
725 emit_move_insn (target, x);
726 return true;
729 /* Generate insns for VEC_LSHIFT_EXPR, VEC_RSHIFT_EXPR. */
732 expand_vec_shift_expr (sepops ops, rtx target)
734 struct expand_operand eops[3];
735 enum insn_code icode;
736 rtx rtx_op1, rtx_op2;
737 enum machine_mode mode = TYPE_MODE (ops->type);
738 tree vec_oprnd = ops->op0;
739 tree shift_oprnd = ops->op1;
740 optab shift_optab;
742 switch (ops->code)
744 case VEC_RSHIFT_EXPR:
745 shift_optab = vec_shr_optab;
746 break;
747 case VEC_LSHIFT_EXPR:
748 shift_optab = vec_shl_optab;
749 break;
750 default:
751 gcc_unreachable ();
754 icode = optab_handler (shift_optab, mode);
755 gcc_assert (icode != CODE_FOR_nothing);
757 rtx_op1 = expand_normal (vec_oprnd);
758 rtx_op2 = expand_normal (shift_oprnd);
760 create_output_operand (&eops[0], target, mode);
761 create_input_operand (&eops[1], rtx_op1, GET_MODE (rtx_op1));
762 create_convert_operand_from_type (&eops[2], rtx_op2, TREE_TYPE (shift_oprnd));
763 expand_insn (icode, 3, eops);
765 return eops[0].value;
768 /* Create a new vector value in VMODE with all elements set to OP. The
769 mode of OP must be the element mode of VMODE. If OP is a constant,
770 then the return value will be a constant. */
772 static rtx
773 expand_vector_broadcast (enum machine_mode vmode, rtx op)
775 enum insn_code icode;
776 rtvec vec;
777 rtx ret;
778 int i, n;
780 gcc_checking_assert (VECTOR_MODE_P (vmode));
782 n = GET_MODE_NUNITS (vmode);
783 vec = rtvec_alloc (n);
784 for (i = 0; i < n; ++i)
785 RTVEC_ELT (vec, i) = op;
787 if (CONSTANT_P (op))
788 return gen_rtx_CONST_VECTOR (vmode, vec);
790 /* ??? If the target doesn't have a vec_init, then we have no easy way
791 of performing this operation. Most of this sort of generic support
792 is hidden away in the vector lowering support in gimple. */
793 icode = optab_handler (vec_init_optab, vmode);
794 if (icode == CODE_FOR_nothing)
795 return NULL;
797 ret = gen_reg_rtx (vmode);
798 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
800 return ret;
803 /* This subroutine of expand_doubleword_shift handles the cases in which
804 the effective shift value is >= BITS_PER_WORD. The arguments and return
805 value are the same as for the parent routine, except that SUPERWORD_OP1
806 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
807 INTO_TARGET may be null if the caller has decided to calculate it. */
809 static bool
810 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
811 rtx outof_target, rtx into_target,
812 int unsignedp, enum optab_methods methods)
814 if (into_target != 0)
815 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
816 into_target, unsignedp, methods))
817 return false;
819 if (outof_target != 0)
821 /* For a signed right shift, we must fill OUTOF_TARGET with copies
822 of the sign bit, otherwise we must fill it with zeros. */
823 if (binoptab != ashr_optab)
824 emit_move_insn (outof_target, CONST0_RTX (word_mode));
825 else
826 if (!force_expand_binop (word_mode, binoptab,
827 outof_input, GEN_INT (BITS_PER_WORD - 1),
828 outof_target, unsignedp, methods))
829 return false;
831 return true;
834 /* This subroutine of expand_doubleword_shift handles the cases in which
835 the effective shift value is < BITS_PER_WORD. The arguments and return
836 value are the same as for the parent routine. */
838 static bool
839 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
840 rtx outof_input, rtx into_input, rtx op1,
841 rtx outof_target, rtx into_target,
842 int unsignedp, enum optab_methods methods,
843 unsigned HOST_WIDE_INT shift_mask)
845 optab reverse_unsigned_shift, unsigned_shift;
846 rtx tmp, carries;
848 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
849 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
851 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
852 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
853 the opposite direction to BINOPTAB. */
854 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
856 carries = outof_input;
857 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
858 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
859 0, true, methods);
861 else
863 /* We must avoid shifting by BITS_PER_WORD bits since that is either
864 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
865 has unknown behavior. Do a single shift first, then shift by the
866 remainder. It's OK to use ~OP1 as the remainder if shift counts
867 are truncated to the mode size. */
868 carries = expand_binop (word_mode, reverse_unsigned_shift,
869 outof_input, const1_rtx, 0, unsignedp, methods);
870 if (shift_mask == BITS_PER_WORD - 1)
872 tmp = immed_double_const (-1, -1, op1_mode);
873 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
874 0, true, methods);
876 else
878 tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
879 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
880 0, true, methods);
883 if (tmp == 0 || carries == 0)
884 return false;
885 carries = expand_binop (word_mode, reverse_unsigned_shift,
886 carries, tmp, 0, unsignedp, methods);
887 if (carries == 0)
888 return false;
890 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
891 so the result can go directly into INTO_TARGET if convenient. */
892 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
893 into_target, unsignedp, methods);
894 if (tmp == 0)
895 return false;
897 /* Now OR in the bits carried over from OUTOF_INPUT. */
898 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
899 into_target, unsignedp, methods))
900 return false;
902 /* Use a standard word_mode shift for the out-of half. */
903 if (outof_target != 0)
904 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
905 outof_target, unsignedp, methods))
906 return false;
908 return true;
912 #ifdef HAVE_conditional_move
913 /* Try implementing expand_doubleword_shift using conditional moves.
914 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
915 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
916 are the shift counts to use in the former and latter case. All other
917 arguments are the same as the parent routine. */
919 static bool
920 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
921 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
922 rtx outof_input, rtx into_input,
923 rtx subword_op1, rtx superword_op1,
924 rtx outof_target, rtx into_target,
925 int unsignedp, enum optab_methods methods,
926 unsigned HOST_WIDE_INT shift_mask)
928 rtx outof_superword, into_superword;
930 /* Put the superword version of the output into OUTOF_SUPERWORD and
931 INTO_SUPERWORD. */
932 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
933 if (outof_target != 0 && subword_op1 == superword_op1)
935 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
936 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
937 into_superword = outof_target;
938 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
939 outof_superword, 0, unsignedp, methods))
940 return false;
942 else
944 into_superword = gen_reg_rtx (word_mode);
945 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
946 outof_superword, into_superword,
947 unsignedp, methods))
948 return false;
951 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
952 if (!expand_subword_shift (op1_mode, binoptab,
953 outof_input, into_input, subword_op1,
954 outof_target, into_target,
955 unsignedp, methods, shift_mask))
956 return false;
958 /* Select between them. Do the INTO half first because INTO_SUPERWORD
959 might be the current value of OUTOF_TARGET. */
960 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
961 into_target, into_superword, word_mode, false))
962 return false;
964 if (outof_target != 0)
965 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
966 outof_target, outof_superword,
967 word_mode, false))
968 return false;
970 return true;
972 #endif
974 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
975 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
976 input operand; the shift moves bits in the direction OUTOF_INPUT->
977 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
978 of the target. OP1 is the shift count and OP1_MODE is its mode.
979 If OP1 is constant, it will have been truncated as appropriate
980 and is known to be nonzero.
982 If SHIFT_MASK is zero, the result of word shifts is undefined when the
983 shift count is outside the range [0, BITS_PER_WORD). This routine must
984 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
986 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
987 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
988 fill with zeros or sign bits as appropriate.
990 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
991 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
992 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
993 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
994 are undefined.
996 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
997 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
998 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
999 function wants to calculate it itself.
1001 Return true if the shift could be successfully synthesized. */
1003 static bool
1004 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
1005 rtx outof_input, rtx into_input, rtx op1,
1006 rtx outof_target, rtx into_target,
1007 int unsignedp, enum optab_methods methods,
1008 unsigned HOST_WIDE_INT shift_mask)
1010 rtx superword_op1, tmp, cmp1, cmp2;
1011 rtx subword_label, done_label;
1012 enum rtx_code cmp_code;
1014 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1015 fill the result with sign or zero bits as appropriate. If so, the value
1016 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1017 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1018 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1020 This isn't worthwhile for constant shifts since the optimizers will
1021 cope better with in-range shift counts. */
1022 if (shift_mask >= BITS_PER_WORD
1023 && outof_target != 0
1024 && !CONSTANT_P (op1))
1026 if (!expand_doubleword_shift (op1_mode, binoptab,
1027 outof_input, into_input, op1,
1028 0, into_target,
1029 unsignedp, methods, shift_mask))
1030 return false;
1031 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1032 outof_target, unsignedp, methods))
1033 return false;
1034 return true;
1037 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1038 is true when the effective shift value is less than BITS_PER_WORD.
1039 Set SUPERWORD_OP1 to the shift count that should be used to shift
1040 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1041 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
1042 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1044 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1045 is a subword shift count. */
1046 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1047 0, true, methods);
1048 cmp2 = CONST0_RTX (op1_mode);
1049 cmp_code = EQ;
1050 superword_op1 = op1;
1052 else
1054 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1055 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1056 0, true, methods);
1057 cmp2 = CONST0_RTX (op1_mode);
1058 cmp_code = LT;
1059 superword_op1 = cmp1;
1061 if (cmp1 == 0)
1062 return false;
1064 /* If we can compute the condition at compile time, pick the
1065 appropriate subroutine. */
1066 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1067 if (tmp != 0 && CONST_INT_P (tmp))
1069 if (tmp == const0_rtx)
1070 return expand_superword_shift (binoptab, outof_input, superword_op1,
1071 outof_target, into_target,
1072 unsignedp, methods);
1073 else
1074 return expand_subword_shift (op1_mode, binoptab,
1075 outof_input, into_input, op1,
1076 outof_target, into_target,
1077 unsignedp, methods, shift_mask);
1080 #ifdef HAVE_conditional_move
1081 /* Try using conditional moves to generate straight-line code. */
1083 rtx start = get_last_insn ();
1084 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1085 cmp_code, cmp1, cmp2,
1086 outof_input, into_input,
1087 op1, superword_op1,
1088 outof_target, into_target,
1089 unsignedp, methods, shift_mask))
1090 return true;
1091 delete_insns_since (start);
1093 #endif
1095 /* As a last resort, use branches to select the correct alternative. */
1096 subword_label = gen_label_rtx ();
1097 done_label = gen_label_rtx ();
1099 NO_DEFER_POP;
1100 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1101 0, 0, subword_label, -1);
1102 OK_DEFER_POP;
1104 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1105 outof_target, into_target,
1106 unsignedp, methods))
1107 return false;
1109 emit_jump_insn (gen_jump (done_label));
1110 emit_barrier ();
1111 emit_label (subword_label);
1113 if (!expand_subword_shift (op1_mode, binoptab,
1114 outof_input, into_input, op1,
1115 outof_target, into_target,
1116 unsignedp, methods, shift_mask))
1117 return false;
1119 emit_label (done_label);
1120 return true;
1123 /* Subroutine of expand_binop. Perform a double word multiplication of
1124 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1125 as the target's word_mode. This function return NULL_RTX if anything
1126 goes wrong, in which case it may have already emitted instructions
1127 which need to be deleted.
1129 If we want to multiply two two-word values and have normal and widening
1130 multiplies of single-word values, we can do this with three smaller
1131 multiplications.
1133 The multiplication proceeds as follows:
1134 _______________________
1135 [__op0_high_|__op0_low__]
1136 _______________________
1137 * [__op1_high_|__op1_low__]
1138 _______________________________________________
1139 _______________________
1140 (1) [__op0_low__*__op1_low__]
1141 _______________________
1142 (2a) [__op0_low__*__op1_high_]
1143 _______________________
1144 (2b) [__op0_high_*__op1_low__]
1145 _______________________
1146 (3) [__op0_high_*__op1_high_]
1149 This gives a 4-word result. Since we are only interested in the
1150 lower 2 words, partial result (3) and the upper words of (2a) and
1151 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1152 calculated using non-widening multiplication.
1154 (1), however, needs to be calculated with an unsigned widening
1155 multiplication. If this operation is not directly supported we
1156 try using a signed widening multiplication and adjust the result.
1157 This adjustment works as follows:
1159 If both operands are positive then no adjustment is needed.
1161 If the operands have different signs, for example op0_low < 0 and
1162 op1_low >= 0, the instruction treats the most significant bit of
1163 op0_low as a sign bit instead of a bit with significance
1164 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1165 with 2**BITS_PER_WORD - op0_low, and two's complements the
1166 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1167 the result.
1169 Similarly, if both operands are negative, we need to add
1170 (op0_low + op1_low) * 2**BITS_PER_WORD.
1172 We use a trick to adjust quickly. We logically shift op0_low right
1173 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1174 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1175 logical shift exists, we do an arithmetic right shift and subtract
1176 the 0 or -1. */
1178 static rtx
1179 expand_doubleword_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
1180 bool umulp, enum optab_methods methods)
1182 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1183 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1184 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1185 rtx product, adjust, product_high, temp;
1187 rtx op0_high = operand_subword_force (op0, high, mode);
1188 rtx op0_low = operand_subword_force (op0, low, mode);
1189 rtx op1_high = operand_subword_force (op1, high, mode);
1190 rtx op1_low = operand_subword_force (op1, low, mode);
1192 /* If we're using an unsigned multiply to directly compute the product
1193 of the low-order words of the operands and perform any required
1194 adjustments of the operands, we begin by trying two more multiplications
1195 and then computing the appropriate sum.
1197 We have checked above that the required addition is provided.
1198 Full-word addition will normally always succeed, especially if
1199 it is provided at all, so we don't worry about its failure. The
1200 multiplication may well fail, however, so we do handle that. */
1202 if (!umulp)
1204 /* ??? This could be done with emit_store_flag where available. */
1205 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1206 NULL_RTX, 1, methods);
1207 if (temp)
1208 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1209 NULL_RTX, 0, OPTAB_DIRECT);
1210 else
1212 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1213 NULL_RTX, 0, methods);
1214 if (!temp)
1215 return NULL_RTX;
1216 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1217 NULL_RTX, 0, OPTAB_DIRECT);
1220 if (!op0_high)
1221 return NULL_RTX;
1224 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1225 NULL_RTX, 0, OPTAB_DIRECT);
1226 if (!adjust)
1227 return NULL_RTX;
1229 /* OP0_HIGH should now be dead. */
1231 if (!umulp)
1233 /* ??? This could be done with emit_store_flag where available. */
1234 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1235 NULL_RTX, 1, methods);
1236 if (temp)
1237 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1238 NULL_RTX, 0, OPTAB_DIRECT);
1239 else
1241 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1242 NULL_RTX, 0, methods);
1243 if (!temp)
1244 return NULL_RTX;
1245 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1246 NULL_RTX, 0, OPTAB_DIRECT);
1249 if (!op1_high)
1250 return NULL_RTX;
1253 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1254 NULL_RTX, 0, OPTAB_DIRECT);
1255 if (!temp)
1256 return NULL_RTX;
1258 /* OP1_HIGH should now be dead. */
1260 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1261 NULL_RTX, 0, OPTAB_DIRECT);
1263 if (target && !REG_P (target))
1264 target = NULL_RTX;
1266 if (umulp)
1267 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1268 target, 1, OPTAB_DIRECT);
1269 else
1270 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1271 target, 1, OPTAB_DIRECT);
1273 if (!product)
1274 return NULL_RTX;
1276 product_high = operand_subword (product, high, 1, mode);
1277 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1278 NULL_RTX, 0, OPTAB_DIRECT);
1279 emit_move_insn (product_high, adjust);
1280 return product;
1283 /* Wrapper around expand_binop which takes an rtx code to specify
1284 the operation to perform, not an optab pointer. All other
1285 arguments are the same. */
1287 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
1288 rtx op1, rtx target, int unsignedp,
1289 enum optab_methods methods)
1291 optab binop = code_to_optab (code);
1292 gcc_assert (binop);
1294 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1297 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1298 binop. Order them according to commutative_operand_precedence and, if
1299 possible, try to put TARGET or a pseudo first. */
1300 static bool
1301 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1303 int op0_prec = commutative_operand_precedence (op0);
1304 int op1_prec = commutative_operand_precedence (op1);
1306 if (op0_prec < op1_prec)
1307 return true;
1309 if (op0_prec > op1_prec)
1310 return false;
1312 /* With equal precedence, both orders are ok, but it is better if the
1313 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1314 if (target == 0 || REG_P (target))
1315 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1316 else
1317 return rtx_equal_p (op1, target);
1320 /* Return true if BINOPTAB implements a shift operation. */
1322 static bool
1323 shift_optab_p (optab binoptab)
1325 switch (optab_to_code (binoptab))
1327 case ASHIFT:
1328 case SS_ASHIFT:
1329 case US_ASHIFT:
1330 case ASHIFTRT:
1331 case LSHIFTRT:
1332 case ROTATE:
1333 case ROTATERT:
1334 return true;
1336 default:
1337 return false;
1341 /* Return true if BINOPTAB implements a commutative binary operation. */
1343 static bool
1344 commutative_optab_p (optab binoptab)
1346 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1347 || binoptab == smul_widen_optab
1348 || binoptab == umul_widen_optab
1349 || binoptab == smul_highpart_optab
1350 || binoptab == umul_highpart_optab);
1353 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1354 optimizing, and if the operand is a constant that costs more than
1355 1 instruction, force the constant into a register and return that
1356 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1358 static rtx
1359 avoid_expensive_constant (enum machine_mode mode, optab binoptab,
1360 int opn, rtx x, bool unsignedp)
1362 bool speed = optimize_insn_for_speed_p ();
1364 if (mode != VOIDmode
1365 && optimize
1366 && CONSTANT_P (x)
1367 && (rtx_cost (x, optab_to_code (binoptab), opn, speed)
1368 > set_src_cost (x, speed)))
1370 if (CONST_INT_P (x))
1372 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1373 if (intval != INTVAL (x))
1374 x = GEN_INT (intval);
1376 else
1377 x = convert_modes (mode, VOIDmode, x, unsignedp);
1378 x = force_reg (mode, x);
1380 return x;
1383 /* Helper function for expand_binop: handle the case where there
1384 is an insn that directly implements the indicated operation.
1385 Returns null if this is not possible. */
1386 static rtx
1387 expand_binop_directly (enum machine_mode mode, optab binoptab,
1388 rtx op0, rtx op1,
1389 rtx target, int unsignedp, enum optab_methods methods,
1390 rtx last)
1392 enum machine_mode from_mode = widened_mode (mode, op0, op1);
1393 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1394 from_mode, 1);
1395 enum machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1396 enum machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1397 enum machine_mode mode0, mode1, tmp_mode;
1398 struct expand_operand ops[3];
1399 bool commutative_p;
1400 rtx pat;
1401 rtx xop0 = op0, xop1 = op1;
1402 rtx swap;
1404 /* If it is a commutative operator and the modes would match
1405 if we would swap the operands, we can save the conversions. */
1406 commutative_p = commutative_optab_p (binoptab);
1407 if (commutative_p
1408 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1409 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1411 swap = xop0;
1412 xop0 = xop1;
1413 xop1 = swap;
1416 /* If we are optimizing, force expensive constants into a register. */
1417 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1418 if (!shift_optab_p (binoptab))
1419 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1421 /* In case the insn wants input operands in modes different from
1422 those of the actual operands, convert the operands. It would
1423 seem that we don't need to convert CONST_INTs, but we do, so
1424 that they're properly zero-extended, sign-extended or truncated
1425 for their mode. */
1427 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1428 if (xmode0 != VOIDmode && xmode0 != mode0)
1430 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1431 mode0 = xmode0;
1434 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1435 if (xmode1 != VOIDmode && xmode1 != mode1)
1437 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1438 mode1 = xmode1;
1441 /* If operation is commutative,
1442 try to make the first operand a register.
1443 Even better, try to make it the same as the target.
1444 Also try to make the last operand a constant. */
1445 if (commutative_p
1446 && swap_commutative_operands_with_target (target, xop0, xop1))
1448 swap = xop1;
1449 xop1 = xop0;
1450 xop0 = swap;
1453 /* Now, if insn's predicates don't allow our operands, put them into
1454 pseudo regs. */
1456 if (binoptab == vec_pack_trunc_optab
1457 || binoptab == vec_pack_usat_optab
1458 || binoptab == vec_pack_ssat_optab
1459 || binoptab == vec_pack_ufix_trunc_optab
1460 || binoptab == vec_pack_sfix_trunc_optab)
1462 /* The mode of the result is different then the mode of the
1463 arguments. */
1464 tmp_mode = insn_data[(int) icode].operand[0].mode;
1465 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1467 delete_insns_since (last);
1468 return NULL_RTX;
1471 else
1472 tmp_mode = mode;
1474 create_output_operand (&ops[0], target, tmp_mode);
1475 create_input_operand (&ops[1], xop0, mode0);
1476 create_input_operand (&ops[2], xop1, mode1);
1477 pat = maybe_gen_insn (icode, 3, ops);
1478 if (pat)
1480 /* If PAT is composed of more than one insn, try to add an appropriate
1481 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1482 operand, call expand_binop again, this time without a target. */
1483 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1484 && ! add_equal_note (pat, ops[0].value, optab_to_code (binoptab),
1485 ops[1].value, ops[2].value))
1487 delete_insns_since (last);
1488 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1489 unsignedp, methods);
1492 emit_insn (pat);
1493 return ops[0].value;
1495 delete_insns_since (last);
1496 return NULL_RTX;
1499 /* Generate code to perform an operation specified by BINOPTAB
1500 on operands OP0 and OP1, with result having machine-mode MODE.
1502 UNSIGNEDP is for the case where we have to widen the operands
1503 to perform the operation. It says to use zero-extension.
1505 If TARGET is nonzero, the value
1506 is generated there, if it is convenient to do so.
1507 In all cases an rtx is returned for the locus of the value;
1508 this may or may not be TARGET. */
1511 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
1512 rtx target, int unsignedp, enum optab_methods methods)
1514 enum optab_methods next_methods
1515 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1516 ? OPTAB_WIDEN : methods);
1517 enum mode_class mclass;
1518 enum machine_mode wider_mode;
1519 rtx libfunc;
1520 rtx temp;
1521 rtx entry_last = get_last_insn ();
1522 rtx last;
1524 mclass = GET_MODE_CLASS (mode);
1526 /* If subtracting an integer constant, convert this into an addition of
1527 the negated constant. */
1529 if (binoptab == sub_optab && CONST_INT_P (op1))
1531 op1 = negate_rtx (mode, op1);
1532 binoptab = add_optab;
1535 /* Record where to delete back to if we backtrack. */
1536 last = get_last_insn ();
1538 /* If we can do it with a three-operand insn, do so. */
1540 if (methods != OPTAB_MUST_WIDEN
1541 && find_widening_optab_handler (binoptab, mode,
1542 widened_mode (mode, op0, op1), 1)
1543 != CODE_FOR_nothing)
1545 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1546 unsignedp, methods, last);
1547 if (temp)
1548 return temp;
1551 /* If we were trying to rotate, and that didn't work, try rotating
1552 the other direction before falling back to shifts and bitwise-or. */
1553 if (((binoptab == rotl_optab
1554 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1555 || (binoptab == rotr_optab
1556 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1557 && mclass == MODE_INT)
1559 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1560 rtx newop1;
1561 unsigned int bits = GET_MODE_PRECISION (mode);
1563 if (CONST_INT_P (op1))
1564 newop1 = GEN_INT (bits - INTVAL (op1));
1565 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1566 newop1 = negate_rtx (GET_MODE (op1), op1);
1567 else
1568 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1569 gen_int_mode (bits, GET_MODE (op1)), op1,
1570 NULL_RTX, unsignedp, OPTAB_DIRECT);
1572 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1573 target, unsignedp, methods, last);
1574 if (temp)
1575 return temp;
1578 /* If this is a multiply, see if we can do a widening operation that
1579 takes operands of this mode and makes a wider mode. */
1581 if (binoptab == smul_optab
1582 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1583 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1584 : smul_widen_optab),
1585 GET_MODE_2XWIDER_MODE (mode), mode)
1586 != CODE_FOR_nothing))
1588 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1589 unsignedp ? umul_widen_optab : smul_widen_optab,
1590 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1592 if (temp != 0)
1594 if (GET_MODE_CLASS (mode) == MODE_INT
1595 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1596 return gen_lowpart (mode, temp);
1597 else
1598 return convert_to_mode (mode, temp, unsignedp);
1602 /* If this is a vector shift by a scalar, see if we can do a vector
1603 shift by a vector. If so, broadcast the scalar into a vector. */
1604 if (mclass == MODE_VECTOR_INT)
1606 optab otheroptab = unknown_optab;
1608 if (binoptab == ashl_optab)
1609 otheroptab = vashl_optab;
1610 else if (binoptab == ashr_optab)
1611 otheroptab = vashr_optab;
1612 else if (binoptab == lshr_optab)
1613 otheroptab = vlshr_optab;
1614 else if (binoptab == rotl_optab)
1615 otheroptab = vrotl_optab;
1616 else if (binoptab == rotr_optab)
1617 otheroptab = vrotr_optab;
1619 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1621 rtx vop1 = expand_vector_broadcast (mode, op1);
1622 if (vop1)
1624 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1625 target, unsignedp, methods, last);
1626 if (temp)
1627 return temp;
1632 /* Look for a wider mode of the same class for which we think we
1633 can open-code the operation. Check for a widening multiply at the
1634 wider mode as well. */
1636 if (CLASS_HAS_WIDER_MODES_P (mclass)
1637 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1638 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1639 wider_mode != VOIDmode;
1640 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1642 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1643 || (binoptab == smul_optab
1644 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1645 && (find_widening_optab_handler ((unsignedp
1646 ? umul_widen_optab
1647 : smul_widen_optab),
1648 GET_MODE_WIDER_MODE (wider_mode),
1649 mode, 0)
1650 != CODE_FOR_nothing)))
1652 rtx xop0 = op0, xop1 = op1;
1653 int no_extend = 0;
1655 /* For certain integer operations, we need not actually extend
1656 the narrow operands, as long as we will truncate
1657 the results to the same narrowness. */
1659 if ((binoptab == ior_optab || binoptab == and_optab
1660 || binoptab == xor_optab
1661 || binoptab == add_optab || binoptab == sub_optab
1662 || binoptab == smul_optab || binoptab == ashl_optab)
1663 && mclass == MODE_INT)
1665 no_extend = 1;
1666 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1667 xop0, unsignedp);
1668 if (binoptab != ashl_optab)
1669 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1670 xop1, unsignedp);
1673 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1675 /* The second operand of a shift must always be extended. */
1676 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1677 no_extend && binoptab != ashl_optab);
1679 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1680 unsignedp, OPTAB_DIRECT);
1681 if (temp)
1683 if (mclass != MODE_INT
1684 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1686 if (target == 0)
1687 target = gen_reg_rtx (mode);
1688 convert_move (target, temp, 0);
1689 return target;
1691 else
1692 return gen_lowpart (mode, temp);
1694 else
1695 delete_insns_since (last);
1699 /* If operation is commutative,
1700 try to make the first operand a register.
1701 Even better, try to make it the same as the target.
1702 Also try to make the last operand a constant. */
1703 if (commutative_optab_p (binoptab)
1704 && swap_commutative_operands_with_target (target, op0, op1))
1706 temp = op1;
1707 op1 = op0;
1708 op0 = temp;
1711 /* These can be done a word at a time. */
1712 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1713 && mclass == MODE_INT
1714 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1715 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1717 int i;
1718 rtx insns;
1720 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1721 won't be accurate, so use a new target. */
1722 if (target == 0
1723 || target == op0
1724 || target == op1
1725 || !valid_multiword_target_p (target))
1726 target = gen_reg_rtx (mode);
1728 start_sequence ();
1730 /* Do the actual arithmetic. */
1731 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1733 rtx target_piece = operand_subword (target, i, 1, mode);
1734 rtx x = expand_binop (word_mode, binoptab,
1735 operand_subword_force (op0, i, mode),
1736 operand_subword_force (op1, i, mode),
1737 target_piece, unsignedp, next_methods);
1739 if (x == 0)
1740 break;
1742 if (target_piece != x)
1743 emit_move_insn (target_piece, x);
1746 insns = get_insns ();
1747 end_sequence ();
1749 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1751 emit_insn (insns);
1752 return target;
1756 /* Synthesize double word shifts from single word shifts. */
1757 if ((binoptab == lshr_optab || binoptab == ashl_optab
1758 || binoptab == ashr_optab)
1759 && mclass == MODE_INT
1760 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1761 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1762 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1763 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1764 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1765 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1767 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1768 enum machine_mode op1_mode;
1770 double_shift_mask = targetm.shift_truncation_mask (mode);
1771 shift_mask = targetm.shift_truncation_mask (word_mode);
1772 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1774 /* Apply the truncation to constant shifts. */
1775 if (double_shift_mask > 0 && CONST_INT_P (op1))
1776 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1778 if (op1 == CONST0_RTX (op1_mode))
1779 return op0;
1781 /* Make sure that this is a combination that expand_doubleword_shift
1782 can handle. See the comments there for details. */
1783 if (double_shift_mask == 0
1784 || (shift_mask == BITS_PER_WORD - 1
1785 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1787 rtx insns;
1788 rtx into_target, outof_target;
1789 rtx into_input, outof_input;
1790 int left_shift, outof_word;
1792 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1793 won't be accurate, so use a new target. */
1794 if (target == 0
1795 || target == op0
1796 || target == op1
1797 || !valid_multiword_target_p (target))
1798 target = gen_reg_rtx (mode);
1800 start_sequence ();
1802 /* OUTOF_* is the word we are shifting bits away from, and
1803 INTO_* is the word that we are shifting bits towards, thus
1804 they differ depending on the direction of the shift and
1805 WORDS_BIG_ENDIAN. */
1807 left_shift = binoptab == ashl_optab;
1808 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1810 outof_target = operand_subword (target, outof_word, 1, mode);
1811 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1813 outof_input = operand_subword_force (op0, outof_word, mode);
1814 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1816 if (expand_doubleword_shift (op1_mode, binoptab,
1817 outof_input, into_input, op1,
1818 outof_target, into_target,
1819 unsignedp, next_methods, shift_mask))
1821 insns = get_insns ();
1822 end_sequence ();
1824 emit_insn (insns);
1825 return target;
1827 end_sequence ();
1831 /* Synthesize double word rotates from single word shifts. */
1832 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1833 && mclass == MODE_INT
1834 && CONST_INT_P (op1)
1835 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1836 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1837 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1839 rtx insns;
1840 rtx into_target, outof_target;
1841 rtx into_input, outof_input;
1842 rtx inter;
1843 int shift_count, left_shift, outof_word;
1845 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1846 won't be accurate, so use a new target. Do this also if target is not
1847 a REG, first because having a register instead may open optimization
1848 opportunities, and second because if target and op0 happen to be MEMs
1849 designating the same location, we would risk clobbering it too early
1850 in the code sequence we generate below. */
1851 if (target == 0
1852 || target == op0
1853 || target == op1
1854 || !REG_P (target)
1855 || !valid_multiword_target_p (target))
1856 target = gen_reg_rtx (mode);
1858 start_sequence ();
1860 shift_count = INTVAL (op1);
1862 /* OUTOF_* is the word we are shifting bits away from, and
1863 INTO_* is the word that we are shifting bits towards, thus
1864 they differ depending on the direction of the shift and
1865 WORDS_BIG_ENDIAN. */
1867 left_shift = (binoptab == rotl_optab);
1868 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1870 outof_target = operand_subword (target, outof_word, 1, mode);
1871 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1873 outof_input = operand_subword_force (op0, outof_word, mode);
1874 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1876 if (shift_count == BITS_PER_WORD)
1878 /* This is just a word swap. */
1879 emit_move_insn (outof_target, into_input);
1880 emit_move_insn (into_target, outof_input);
1881 inter = const0_rtx;
1883 else
1885 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1886 rtx first_shift_count, second_shift_count;
1887 optab reverse_unsigned_shift, unsigned_shift;
1889 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1890 ? lshr_optab : ashl_optab);
1892 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1893 ? ashl_optab : lshr_optab);
1895 if (shift_count > BITS_PER_WORD)
1897 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1898 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1900 else
1902 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1903 second_shift_count = GEN_INT (shift_count);
1906 into_temp1 = expand_binop (word_mode, unsigned_shift,
1907 outof_input, first_shift_count,
1908 NULL_RTX, unsignedp, next_methods);
1909 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1910 into_input, second_shift_count,
1911 NULL_RTX, unsignedp, next_methods);
1913 if (into_temp1 != 0 && into_temp2 != 0)
1914 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1915 into_target, unsignedp, next_methods);
1916 else
1917 inter = 0;
1919 if (inter != 0 && inter != into_target)
1920 emit_move_insn (into_target, inter);
1922 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1923 into_input, first_shift_count,
1924 NULL_RTX, unsignedp, next_methods);
1925 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1926 outof_input, second_shift_count,
1927 NULL_RTX, unsignedp, next_methods);
1929 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1930 inter = expand_binop (word_mode, ior_optab,
1931 outof_temp1, outof_temp2,
1932 outof_target, unsignedp, next_methods);
1934 if (inter != 0 && inter != outof_target)
1935 emit_move_insn (outof_target, inter);
1938 insns = get_insns ();
1939 end_sequence ();
1941 if (inter != 0)
1943 emit_insn (insns);
1944 return target;
1948 /* These can be done a word at a time by propagating carries. */
1949 if ((binoptab == add_optab || binoptab == sub_optab)
1950 && mclass == MODE_INT
1951 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1952 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1954 unsigned int i;
1955 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1956 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1957 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1958 rtx xop0, xop1, xtarget;
1960 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1961 value is one of those, use it. Otherwise, use 1 since it is the
1962 one easiest to get. */
1963 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1964 int normalizep = STORE_FLAG_VALUE;
1965 #else
1966 int normalizep = 1;
1967 #endif
1969 /* Prepare the operands. */
1970 xop0 = force_reg (mode, op0);
1971 xop1 = force_reg (mode, op1);
1973 xtarget = gen_reg_rtx (mode);
1975 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1976 target = xtarget;
1978 /* Indicate for flow that the entire target reg is being set. */
1979 if (REG_P (target))
1980 emit_clobber (xtarget);
1982 /* Do the actual arithmetic. */
1983 for (i = 0; i < nwords; i++)
1985 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1986 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1987 rtx op0_piece = operand_subword_force (xop0, index, mode);
1988 rtx op1_piece = operand_subword_force (xop1, index, mode);
1989 rtx x;
1991 /* Main add/subtract of the input operands. */
1992 x = expand_binop (word_mode, binoptab,
1993 op0_piece, op1_piece,
1994 target_piece, unsignedp, next_methods);
1995 if (x == 0)
1996 break;
1998 if (i + 1 < nwords)
2000 /* Store carry from main add/subtract. */
2001 carry_out = gen_reg_rtx (word_mode);
2002 carry_out = emit_store_flag_force (carry_out,
2003 (binoptab == add_optab
2004 ? LT : GT),
2005 x, op0_piece,
2006 word_mode, 1, normalizep);
2009 if (i > 0)
2011 rtx newx;
2013 /* Add/subtract previous carry to main result. */
2014 newx = expand_binop (word_mode,
2015 normalizep == 1 ? binoptab : otheroptab,
2016 x, carry_in,
2017 NULL_RTX, 1, next_methods);
2019 if (i + 1 < nwords)
2021 /* Get out carry from adding/subtracting carry in. */
2022 rtx carry_tmp = gen_reg_rtx (word_mode);
2023 carry_tmp = emit_store_flag_force (carry_tmp,
2024 (binoptab == add_optab
2025 ? LT : GT),
2026 newx, x,
2027 word_mode, 1, normalizep);
2029 /* Logical-ior the two poss. carry together. */
2030 carry_out = expand_binop (word_mode, ior_optab,
2031 carry_out, carry_tmp,
2032 carry_out, 0, next_methods);
2033 if (carry_out == 0)
2034 break;
2036 emit_move_insn (target_piece, newx);
2038 else
2040 if (x != target_piece)
2041 emit_move_insn (target_piece, x);
2044 carry_in = carry_out;
2047 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2049 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2050 || ! rtx_equal_p (target, xtarget))
2052 rtx temp = emit_move_insn (target, xtarget);
2054 set_dst_reg_note (temp, REG_EQUAL,
2055 gen_rtx_fmt_ee (optab_to_code (binoptab),
2056 mode, copy_rtx (xop0),
2057 copy_rtx (xop1)),
2058 target);
2060 else
2061 target = xtarget;
2063 return target;
2066 else
2067 delete_insns_since (last);
2070 /* Attempt to synthesize double word multiplies using a sequence of word
2071 mode multiplications. We first attempt to generate a sequence using a
2072 more efficient unsigned widening multiply, and if that fails we then
2073 try using a signed widening multiply. */
2075 if (binoptab == smul_optab
2076 && mclass == MODE_INT
2077 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2078 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2079 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2081 rtx product = NULL_RTX;
2082 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
2083 != CODE_FOR_nothing)
2085 product = expand_doubleword_mult (mode, op0, op1, target,
2086 true, methods);
2087 if (!product)
2088 delete_insns_since (last);
2091 if (product == NULL_RTX
2092 && widening_optab_handler (smul_widen_optab, mode, word_mode)
2093 != CODE_FOR_nothing)
2095 product = expand_doubleword_mult (mode, op0, op1, target,
2096 false, methods);
2097 if (!product)
2098 delete_insns_since (last);
2101 if (product != NULL_RTX)
2103 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2105 temp = emit_move_insn (target ? target : product, product);
2106 set_dst_reg_note (temp,
2107 REG_EQUAL,
2108 gen_rtx_fmt_ee (MULT, mode,
2109 copy_rtx (op0),
2110 copy_rtx (op1)),
2111 target ? target : product);
2113 return product;
2117 /* It can't be open-coded in this mode.
2118 Use a library call if one is available and caller says that's ok. */
2120 libfunc = optab_libfunc (binoptab, mode);
2121 if (libfunc
2122 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2124 rtx insns;
2125 rtx op1x = op1;
2126 enum machine_mode op1_mode = mode;
2127 rtx value;
2129 start_sequence ();
2131 if (shift_optab_p (binoptab))
2133 op1_mode = targetm.libgcc_shift_count_mode ();
2134 /* Specify unsigned here,
2135 since negative shift counts are meaningless. */
2136 op1x = convert_to_mode (op1_mode, op1, 1);
2139 if (GET_MODE (op0) != VOIDmode
2140 && GET_MODE (op0) != mode)
2141 op0 = convert_to_mode (mode, op0, unsignedp);
2143 /* Pass 1 for NO_QUEUE so we don't lose any increments
2144 if the libcall is cse'd or moved. */
2145 value = emit_library_call_value (libfunc,
2146 NULL_RTX, LCT_CONST, mode, 2,
2147 op0, mode, op1x, op1_mode);
2149 insns = get_insns ();
2150 end_sequence ();
2152 target = gen_reg_rtx (mode);
2153 emit_libcall_block_1 (insns, target, value,
2154 gen_rtx_fmt_ee (optab_to_code (binoptab),
2155 mode, op0, op1),
2156 trapv_binoptab_p (binoptab));
2158 return target;
2161 delete_insns_since (last);
2163 /* It can't be done in this mode. Can we do it in a wider mode? */
2165 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2166 || methods == OPTAB_MUST_WIDEN))
2168 /* Caller says, don't even try. */
2169 delete_insns_since (entry_last);
2170 return 0;
2173 /* Compute the value of METHODS to pass to recursive calls.
2174 Don't allow widening to be tried recursively. */
2176 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2178 /* Look for a wider mode of the same class for which it appears we can do
2179 the operation. */
2181 if (CLASS_HAS_WIDER_MODES_P (mclass))
2183 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2184 wider_mode != VOIDmode;
2185 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2187 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2188 != CODE_FOR_nothing
2189 || (methods == OPTAB_LIB
2190 && optab_libfunc (binoptab, wider_mode)))
2192 rtx xop0 = op0, xop1 = op1;
2193 int no_extend = 0;
2195 /* For certain integer operations, we need not actually extend
2196 the narrow operands, as long as we will truncate
2197 the results to the same narrowness. */
2199 if ((binoptab == ior_optab || binoptab == and_optab
2200 || binoptab == xor_optab
2201 || binoptab == add_optab || binoptab == sub_optab
2202 || binoptab == smul_optab || binoptab == ashl_optab)
2203 && mclass == MODE_INT)
2204 no_extend = 1;
2206 xop0 = widen_operand (xop0, wider_mode, mode,
2207 unsignedp, no_extend);
2209 /* The second operand of a shift must always be extended. */
2210 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2211 no_extend && binoptab != ashl_optab);
2213 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2214 unsignedp, methods);
2215 if (temp)
2217 if (mclass != MODE_INT
2218 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2220 if (target == 0)
2221 target = gen_reg_rtx (mode);
2222 convert_move (target, temp, 0);
2223 return target;
2225 else
2226 return gen_lowpart (mode, temp);
2228 else
2229 delete_insns_since (last);
2234 delete_insns_since (entry_last);
2235 return 0;
2238 /* Expand a binary operator which has both signed and unsigned forms.
2239 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2240 signed operations.
2242 If we widen unsigned operands, we may use a signed wider operation instead
2243 of an unsigned wider operation, since the result would be the same. */
2246 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
2247 rtx op0, rtx op1, rtx target, int unsignedp,
2248 enum optab_methods methods)
2250 rtx temp;
2251 optab direct_optab = unsignedp ? uoptab : soptab;
2252 bool save_enable;
2254 /* Do it without widening, if possible. */
2255 temp = expand_binop (mode, direct_optab, op0, op1, target,
2256 unsignedp, OPTAB_DIRECT);
2257 if (temp || methods == OPTAB_DIRECT)
2258 return temp;
2260 /* Try widening to a signed int. Disable any direct use of any
2261 signed insn in the current mode. */
2262 save_enable = swap_optab_enable (soptab, mode, false);
2264 temp = expand_binop (mode, soptab, op0, op1, target,
2265 unsignedp, OPTAB_WIDEN);
2267 /* For unsigned operands, try widening to an unsigned int. */
2268 if (!temp && unsignedp)
2269 temp = expand_binop (mode, uoptab, op0, op1, target,
2270 unsignedp, OPTAB_WIDEN);
2271 if (temp || methods == OPTAB_WIDEN)
2272 goto egress;
2274 /* Use the right width libcall if that exists. */
2275 temp = expand_binop (mode, direct_optab, op0, op1, target,
2276 unsignedp, OPTAB_LIB);
2277 if (temp || methods == OPTAB_LIB)
2278 goto egress;
2280 /* Must widen and use a libcall, use either signed or unsigned. */
2281 temp = expand_binop (mode, soptab, op0, op1, target,
2282 unsignedp, methods);
2283 if (!temp && unsignedp)
2284 temp = expand_binop (mode, uoptab, op0, op1, target,
2285 unsignedp, methods);
2287 egress:
2288 /* Undo the fiddling above. */
2289 if (save_enable)
2290 swap_optab_enable (soptab, mode, true);
2291 return temp;
2294 /* Generate code to perform an operation specified by UNOPPTAB
2295 on operand OP0, with two results to TARG0 and TARG1.
2296 We assume that the order of the operands for the instruction
2297 is TARG0, TARG1, OP0.
2299 Either TARG0 or TARG1 may be zero, but what that means is that
2300 the result is not actually wanted. We will generate it into
2301 a dummy pseudo-reg and discard it. They may not both be zero.
2303 Returns 1 if this operation can be performed; 0 if not. */
2306 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2307 int unsignedp)
2309 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2310 enum mode_class mclass;
2311 enum machine_mode wider_mode;
2312 rtx entry_last = get_last_insn ();
2313 rtx last;
2315 mclass = GET_MODE_CLASS (mode);
2317 if (!targ0)
2318 targ0 = gen_reg_rtx (mode);
2319 if (!targ1)
2320 targ1 = gen_reg_rtx (mode);
2322 /* Record where to go back to if we fail. */
2323 last = get_last_insn ();
2325 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2327 struct expand_operand ops[3];
2328 enum insn_code icode = optab_handler (unoptab, mode);
2330 create_fixed_operand (&ops[0], targ0);
2331 create_fixed_operand (&ops[1], targ1);
2332 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2333 if (maybe_expand_insn (icode, 3, ops))
2334 return 1;
2337 /* It can't be done in this mode. Can we do it in a wider mode? */
2339 if (CLASS_HAS_WIDER_MODES_P (mclass))
2341 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2342 wider_mode != VOIDmode;
2343 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2345 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2347 rtx t0 = gen_reg_rtx (wider_mode);
2348 rtx t1 = gen_reg_rtx (wider_mode);
2349 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2351 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2353 convert_move (targ0, t0, unsignedp);
2354 convert_move (targ1, t1, unsignedp);
2355 return 1;
2357 else
2358 delete_insns_since (last);
2363 delete_insns_since (entry_last);
2364 return 0;
2367 /* Generate code to perform an operation specified by BINOPTAB
2368 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2369 We assume that the order of the operands for the instruction
2370 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2371 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2373 Either TARG0 or TARG1 may be zero, but what that means is that
2374 the result is not actually wanted. We will generate it into
2375 a dummy pseudo-reg and discard it. They may not both be zero.
2377 Returns 1 if this operation can be performed; 0 if not. */
2380 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2381 int unsignedp)
2383 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2384 enum mode_class mclass;
2385 enum machine_mode wider_mode;
2386 rtx entry_last = get_last_insn ();
2387 rtx last;
2389 mclass = GET_MODE_CLASS (mode);
2391 if (!targ0)
2392 targ0 = gen_reg_rtx (mode);
2393 if (!targ1)
2394 targ1 = gen_reg_rtx (mode);
2396 /* Record where to go back to if we fail. */
2397 last = get_last_insn ();
2399 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2401 struct expand_operand ops[4];
2402 enum insn_code icode = optab_handler (binoptab, mode);
2403 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2404 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
2405 rtx xop0 = op0, xop1 = op1;
2407 /* If we are optimizing, force expensive constants into a register. */
2408 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2409 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2411 create_fixed_operand (&ops[0], targ0);
2412 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2413 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2414 create_fixed_operand (&ops[3], targ1);
2415 if (maybe_expand_insn (icode, 4, ops))
2416 return 1;
2417 delete_insns_since (last);
2420 /* It can't be done in this mode. Can we do it in a wider mode? */
2422 if (CLASS_HAS_WIDER_MODES_P (mclass))
2424 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2425 wider_mode != VOIDmode;
2426 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2428 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2430 rtx t0 = gen_reg_rtx (wider_mode);
2431 rtx t1 = gen_reg_rtx (wider_mode);
2432 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2433 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2435 if (expand_twoval_binop (binoptab, cop0, cop1,
2436 t0, t1, unsignedp))
2438 convert_move (targ0, t0, unsignedp);
2439 convert_move (targ1, t1, unsignedp);
2440 return 1;
2442 else
2443 delete_insns_since (last);
2448 delete_insns_since (entry_last);
2449 return 0;
2452 /* Expand the two-valued library call indicated by BINOPTAB, but
2453 preserve only one of the values. If TARG0 is non-NULL, the first
2454 value is placed into TARG0; otherwise the second value is placed
2455 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2456 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2457 This routine assumes that the value returned by the library call is
2458 as if the return value was of an integral mode twice as wide as the
2459 mode of OP0. Returns 1 if the call was successful. */
2461 bool
2462 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2463 rtx targ0, rtx targ1, enum rtx_code code)
2465 enum machine_mode mode;
2466 enum machine_mode libval_mode;
2467 rtx libval;
2468 rtx insns;
2469 rtx libfunc;
2471 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2472 gcc_assert (!targ0 != !targ1);
2474 mode = GET_MODE (op0);
2475 libfunc = optab_libfunc (binoptab, mode);
2476 if (!libfunc)
2477 return false;
2479 /* The value returned by the library function will have twice as
2480 many bits as the nominal MODE. */
2481 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2482 MODE_INT);
2483 start_sequence ();
2484 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2485 libval_mode, 2,
2486 op0, mode,
2487 op1, mode);
2488 /* Get the part of VAL containing the value that we want. */
2489 libval = simplify_gen_subreg (mode, libval, libval_mode,
2490 targ0 ? 0 : GET_MODE_SIZE (mode));
2491 insns = get_insns ();
2492 end_sequence ();
2493 /* Move the into the desired location. */
2494 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2495 gen_rtx_fmt_ee (code, mode, op0, op1));
2497 return true;
2501 /* Wrapper around expand_unop which takes an rtx code to specify
2502 the operation to perform, not an optab pointer. All other
2503 arguments are the same. */
2505 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2506 rtx target, int unsignedp)
2508 optab unop = code_to_optab (code);
2509 gcc_assert (unop);
2511 return expand_unop (mode, unop, op0, target, unsignedp);
2514 /* Try calculating
2515 (clz:narrow x)
2517 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2519 A similar operation can be used for clrsb. UNOPTAB says which operation
2520 we are trying to expand. */
2521 static rtx
2522 widen_leading (enum machine_mode mode, rtx op0, rtx target, optab unoptab)
2524 enum mode_class mclass = GET_MODE_CLASS (mode);
2525 if (CLASS_HAS_WIDER_MODES_P (mclass))
2527 enum machine_mode wider_mode;
2528 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2529 wider_mode != VOIDmode;
2530 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2532 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2534 rtx xop0, temp, last;
2536 last = get_last_insn ();
2538 if (target == 0)
2539 target = gen_reg_rtx (mode);
2540 xop0 = widen_operand (op0, wider_mode, mode,
2541 unoptab != clrsb_optab, false);
2542 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2543 unoptab != clrsb_optab);
2544 if (temp != 0)
2545 temp = expand_binop
2546 (wider_mode, sub_optab, temp,
2547 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2548 - GET_MODE_PRECISION (mode),
2549 wider_mode),
2550 target, true, OPTAB_DIRECT);
2551 if (temp == 0)
2552 delete_insns_since (last);
2554 return temp;
2558 return 0;
2561 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2562 quantities, choosing which based on whether the high word is nonzero. */
2563 static rtx
2564 expand_doubleword_clz (enum machine_mode mode, rtx op0, rtx target)
2566 rtx xop0 = force_reg (mode, op0);
2567 rtx subhi = gen_highpart (word_mode, xop0);
2568 rtx sublo = gen_lowpart (word_mode, xop0);
2569 rtx hi0_label = gen_label_rtx ();
2570 rtx after_label = gen_label_rtx ();
2571 rtx seq, temp, result;
2573 /* If we were not given a target, use a word_mode register, not a
2574 'mode' register. The result will fit, and nobody is expecting
2575 anything bigger (the return type of __builtin_clz* is int). */
2576 if (!target)
2577 target = gen_reg_rtx (word_mode);
2579 /* In any case, write to a word_mode scratch in both branches of the
2580 conditional, so we can ensure there is a single move insn setting
2581 'target' to tag a REG_EQUAL note on. */
2582 result = gen_reg_rtx (word_mode);
2584 start_sequence ();
2586 /* If the high word is not equal to zero,
2587 then clz of the full value is clz of the high word. */
2588 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2589 word_mode, true, hi0_label);
2591 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2592 if (!temp)
2593 goto fail;
2595 if (temp != result)
2596 convert_move (result, temp, true);
2598 emit_jump_insn (gen_jump (after_label));
2599 emit_barrier ();
2601 /* Else clz of the full value is clz of the low word plus the number
2602 of bits in the high word. */
2603 emit_label (hi0_label);
2605 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2606 if (!temp)
2607 goto fail;
2608 temp = expand_binop (word_mode, add_optab, temp,
2609 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2610 result, true, OPTAB_DIRECT);
2611 if (!temp)
2612 goto fail;
2613 if (temp != result)
2614 convert_move (result, temp, true);
2616 emit_label (after_label);
2617 convert_move (target, result, true);
2619 seq = get_insns ();
2620 end_sequence ();
2622 add_equal_note (seq, target, CLZ, xop0, 0);
2623 emit_insn (seq);
2624 return target;
2626 fail:
2627 end_sequence ();
2628 return 0;
2631 /* Try calculating
2632 (bswap:narrow x)
2634 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2635 static rtx
2636 widen_bswap (enum machine_mode mode, rtx op0, rtx target)
2638 enum mode_class mclass = GET_MODE_CLASS (mode);
2639 enum machine_mode wider_mode;
2640 rtx x, last;
2642 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2643 return NULL_RTX;
2645 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2646 wider_mode != VOIDmode;
2647 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2648 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2649 goto found;
2650 return NULL_RTX;
2652 found:
2653 last = get_last_insn ();
2655 x = widen_operand (op0, wider_mode, mode, true, true);
2656 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2658 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2659 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2660 if (x != 0)
2661 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2662 GET_MODE_BITSIZE (wider_mode)
2663 - GET_MODE_BITSIZE (mode),
2664 NULL_RTX, true);
2666 if (x != 0)
2668 if (target == 0)
2669 target = gen_reg_rtx (mode);
2670 emit_move_insn (target, gen_lowpart (mode, x));
2672 else
2673 delete_insns_since (last);
2675 return target;
2678 /* Try calculating bswap as two bswaps of two word-sized operands. */
2680 static rtx
2681 expand_doubleword_bswap (enum machine_mode mode, rtx op, rtx target)
2683 rtx t0, t1;
2685 t1 = expand_unop (word_mode, bswap_optab,
2686 operand_subword_force (op, 0, mode), NULL_RTX, true);
2687 t0 = expand_unop (word_mode, bswap_optab,
2688 operand_subword_force (op, 1, mode), NULL_RTX, true);
2690 if (target == 0 || !valid_multiword_target_p (target))
2691 target = gen_reg_rtx (mode);
2692 if (REG_P (target))
2693 emit_clobber (target);
2694 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2695 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2697 return target;
2700 /* Try calculating (parity x) as (and (popcount x) 1), where
2701 popcount can also be done in a wider mode. */
2702 static rtx
2703 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2705 enum mode_class mclass = GET_MODE_CLASS (mode);
2706 if (CLASS_HAS_WIDER_MODES_P (mclass))
2708 enum machine_mode wider_mode;
2709 for (wider_mode = mode; wider_mode != VOIDmode;
2710 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2712 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2714 rtx xop0, temp, last;
2716 last = get_last_insn ();
2718 if (target == 0)
2719 target = gen_reg_rtx (mode);
2720 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2721 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2722 true);
2723 if (temp != 0)
2724 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2725 target, true, OPTAB_DIRECT);
2726 if (temp == 0)
2727 delete_insns_since (last);
2729 return temp;
2733 return 0;
2736 /* Try calculating ctz(x) as K - clz(x & -x) ,
2737 where K is GET_MODE_PRECISION(mode) - 1.
2739 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2740 don't have to worry about what the hardware does in that case. (If
2741 the clz instruction produces the usual value at 0, which is K, the
2742 result of this code sequence will be -1; expand_ffs, below, relies
2743 on this. It might be nice to have it be K instead, for consistency
2744 with the (very few) processors that provide a ctz with a defined
2745 value, but that would take one more instruction, and it would be
2746 less convenient for expand_ffs anyway. */
2748 static rtx
2749 expand_ctz (enum machine_mode mode, rtx op0, rtx target)
2751 rtx seq, temp;
2753 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2754 return 0;
2756 start_sequence ();
2758 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2759 if (temp)
2760 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2761 true, OPTAB_DIRECT);
2762 if (temp)
2763 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2764 if (temp)
2765 temp = expand_binop (mode, sub_optab,
2766 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2767 temp, target,
2768 true, OPTAB_DIRECT);
2769 if (temp == 0)
2771 end_sequence ();
2772 return 0;
2775 seq = get_insns ();
2776 end_sequence ();
2778 add_equal_note (seq, temp, CTZ, op0, 0);
2779 emit_insn (seq);
2780 return temp;
2784 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2785 else with the sequence used by expand_clz.
2787 The ffs builtin promises to return zero for a zero value and ctz/clz
2788 may have an undefined value in that case. If they do not give us a
2789 convenient value, we have to generate a test and branch. */
2790 static rtx
2791 expand_ffs (enum machine_mode mode, rtx op0, rtx target)
2793 HOST_WIDE_INT val = 0;
2794 bool defined_at_zero = false;
2795 rtx temp, seq;
2797 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2799 start_sequence ();
2801 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2802 if (!temp)
2803 goto fail;
2805 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2807 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2809 start_sequence ();
2810 temp = expand_ctz (mode, op0, 0);
2811 if (!temp)
2812 goto fail;
2814 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2816 defined_at_zero = true;
2817 val = (GET_MODE_PRECISION (mode) - 1) - val;
2820 else
2821 return 0;
2823 if (defined_at_zero && val == -1)
2824 /* No correction needed at zero. */;
2825 else
2827 /* We don't try to do anything clever with the situation found
2828 on some processors (eg Alpha) where ctz(0:mode) ==
2829 bitsize(mode). If someone can think of a way to send N to -1
2830 and leave alone all values in the range 0..N-1 (where N is a
2831 power of two), cheaper than this test-and-branch, please add it.
2833 The test-and-branch is done after the operation itself, in case
2834 the operation sets condition codes that can be recycled for this.
2835 (This is true on i386, for instance.) */
2837 rtx nonzero_label = gen_label_rtx ();
2838 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2839 mode, true, nonzero_label);
2841 convert_move (temp, GEN_INT (-1), false);
2842 emit_label (nonzero_label);
2845 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2846 to produce a value in the range 0..bitsize. */
2847 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2848 target, false, OPTAB_DIRECT);
2849 if (!temp)
2850 goto fail;
2852 seq = get_insns ();
2853 end_sequence ();
2855 add_equal_note (seq, temp, FFS, op0, 0);
2856 emit_insn (seq);
2857 return temp;
2859 fail:
2860 end_sequence ();
2861 return 0;
2864 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2865 conditions, VAL may already be a SUBREG against which we cannot generate
2866 a further SUBREG. In this case, we expect forcing the value into a
2867 register will work around the situation. */
2869 static rtx
2870 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2871 enum machine_mode imode)
2873 rtx ret;
2874 ret = lowpart_subreg (omode, val, imode);
2875 if (ret == NULL)
2877 val = force_reg (imode, val);
2878 ret = lowpart_subreg (omode, val, imode);
2879 gcc_assert (ret != NULL);
2881 return ret;
2884 /* Expand a floating point absolute value or negation operation via a
2885 logical operation on the sign bit. */
2887 static rtx
2888 expand_absneg_bit (enum rtx_code code, enum machine_mode mode,
2889 rtx op0, rtx target)
2891 const struct real_format *fmt;
2892 int bitpos, word, nwords, i;
2893 enum machine_mode imode;
2894 double_int mask;
2895 rtx temp, insns;
2897 /* The format has to have a simple sign bit. */
2898 fmt = REAL_MODE_FORMAT (mode);
2899 if (fmt == NULL)
2900 return NULL_RTX;
2902 bitpos = fmt->signbit_rw;
2903 if (bitpos < 0)
2904 return NULL_RTX;
2906 /* Don't create negative zeros if the format doesn't support them. */
2907 if (code == NEG && !fmt->has_signed_zero)
2908 return NULL_RTX;
2910 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2912 imode = int_mode_for_mode (mode);
2913 if (imode == BLKmode)
2914 return NULL_RTX;
2915 word = 0;
2916 nwords = 1;
2918 else
2920 imode = word_mode;
2922 if (FLOAT_WORDS_BIG_ENDIAN)
2923 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2924 else
2925 word = bitpos / BITS_PER_WORD;
2926 bitpos = bitpos % BITS_PER_WORD;
2927 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2930 mask = double_int_zero.set_bit (bitpos);
2931 if (code == ABS)
2932 mask = ~mask;
2934 if (target == 0
2935 || target == op0
2936 || (nwords > 1 && !valid_multiword_target_p (target)))
2937 target = gen_reg_rtx (mode);
2939 if (nwords > 1)
2941 start_sequence ();
2943 for (i = 0; i < nwords; ++i)
2945 rtx targ_piece = operand_subword (target, i, 1, mode);
2946 rtx op0_piece = operand_subword_force (op0, i, mode);
2948 if (i == word)
2950 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2951 op0_piece,
2952 immed_double_int_const (mask, imode),
2953 targ_piece, 1, OPTAB_LIB_WIDEN);
2954 if (temp != targ_piece)
2955 emit_move_insn (targ_piece, temp);
2957 else
2958 emit_move_insn (targ_piece, op0_piece);
2961 insns = get_insns ();
2962 end_sequence ();
2964 emit_insn (insns);
2966 else
2968 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2969 gen_lowpart (imode, op0),
2970 immed_double_int_const (mask, imode),
2971 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2972 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2974 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2975 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2976 target);
2979 return target;
2982 /* As expand_unop, but will fail rather than attempt the operation in a
2983 different mode or with a libcall. */
2984 static rtx
2985 expand_unop_direct (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2986 int unsignedp)
2988 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2990 struct expand_operand ops[2];
2991 enum insn_code icode = optab_handler (unoptab, mode);
2992 rtx last = get_last_insn ();
2993 rtx pat;
2995 create_output_operand (&ops[0], target, mode);
2996 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2997 pat = maybe_gen_insn (icode, 2, ops);
2998 if (pat)
3000 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3001 && ! add_equal_note (pat, ops[0].value, optab_to_code (unoptab),
3002 ops[1].value, NULL_RTX))
3004 delete_insns_since (last);
3005 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3008 emit_insn (pat);
3010 return ops[0].value;
3013 return 0;
3016 /* Generate code to perform an operation specified by UNOPTAB
3017 on operand OP0, with result having machine-mode MODE.
3019 UNSIGNEDP is for the case where we have to widen the operands
3020 to perform the operation. It says to use zero-extension.
3022 If TARGET is nonzero, the value
3023 is generated there, if it is convenient to do so.
3024 In all cases an rtx is returned for the locus of the value;
3025 this may or may not be TARGET. */
3028 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
3029 int unsignedp)
3031 enum mode_class mclass = GET_MODE_CLASS (mode);
3032 enum machine_mode wider_mode;
3033 rtx temp;
3034 rtx libfunc;
3036 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3037 if (temp)
3038 return temp;
3040 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3042 /* Widening (or narrowing) clz needs special treatment. */
3043 if (unoptab == clz_optab)
3045 temp = widen_leading (mode, op0, target, unoptab);
3046 if (temp)
3047 return temp;
3049 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3050 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3052 temp = expand_doubleword_clz (mode, op0, target);
3053 if (temp)
3054 return temp;
3057 goto try_libcall;
3060 if (unoptab == clrsb_optab)
3062 temp = widen_leading (mode, op0, target, unoptab);
3063 if (temp)
3064 return temp;
3065 goto try_libcall;
3068 /* Widening (or narrowing) bswap needs special treatment. */
3069 if (unoptab == bswap_optab)
3071 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3072 or ROTATERT. First try these directly; if this fails, then try the
3073 obvious pair of shifts with allowed widening, as this will probably
3074 be always more efficient than the other fallback methods. */
3075 if (mode == HImode)
3077 rtx last, temp1, temp2;
3079 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3081 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
3082 unsignedp, OPTAB_DIRECT);
3083 if (temp)
3084 return temp;
3087 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3089 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
3090 unsignedp, OPTAB_DIRECT);
3091 if (temp)
3092 return temp;
3095 last = get_last_insn ();
3097 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
3098 unsignedp, OPTAB_WIDEN);
3099 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
3100 unsignedp, OPTAB_WIDEN);
3101 if (temp1 && temp2)
3103 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3104 unsignedp, OPTAB_WIDEN);
3105 if (temp)
3106 return temp;
3109 delete_insns_since (last);
3112 temp = widen_bswap (mode, op0, target);
3113 if (temp)
3114 return temp;
3116 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3117 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3119 temp = expand_doubleword_bswap (mode, op0, target);
3120 if (temp)
3121 return temp;
3124 goto try_libcall;
3127 if (CLASS_HAS_WIDER_MODES_P (mclass))
3128 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3129 wider_mode != VOIDmode;
3130 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3132 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3134 rtx xop0 = op0;
3135 rtx last = get_last_insn ();
3137 /* For certain operations, we need not actually extend
3138 the narrow operand, as long as we will truncate the
3139 results to the same narrowness. */
3141 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3142 (unoptab == neg_optab
3143 || unoptab == one_cmpl_optab)
3144 && mclass == MODE_INT);
3146 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3147 unsignedp);
3149 if (temp)
3151 if (mclass != MODE_INT
3152 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3154 if (target == 0)
3155 target = gen_reg_rtx (mode);
3156 convert_move (target, temp, 0);
3157 return target;
3159 else
3160 return gen_lowpart (mode, temp);
3162 else
3163 delete_insns_since (last);
3167 /* These can be done a word at a time. */
3168 if (unoptab == one_cmpl_optab
3169 && mclass == MODE_INT
3170 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3171 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3173 int i;
3174 rtx insns;
3176 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3177 target = gen_reg_rtx (mode);
3179 start_sequence ();
3181 /* Do the actual arithmetic. */
3182 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3184 rtx target_piece = operand_subword (target, i, 1, mode);
3185 rtx x = expand_unop (word_mode, unoptab,
3186 operand_subword_force (op0, i, mode),
3187 target_piece, unsignedp);
3189 if (target_piece != x)
3190 emit_move_insn (target_piece, x);
3193 insns = get_insns ();
3194 end_sequence ();
3196 emit_insn (insns);
3197 return target;
3200 if (optab_to_code (unoptab) == NEG)
3202 /* Try negating floating point values by flipping the sign bit. */
3203 if (SCALAR_FLOAT_MODE_P (mode))
3205 temp = expand_absneg_bit (NEG, mode, op0, target);
3206 if (temp)
3207 return temp;
3210 /* If there is no negation pattern, and we have no negative zero,
3211 try subtracting from zero. */
3212 if (!HONOR_SIGNED_ZEROS (mode))
3214 temp = expand_binop (mode, (unoptab == negv_optab
3215 ? subv_optab : sub_optab),
3216 CONST0_RTX (mode), op0, target,
3217 unsignedp, OPTAB_DIRECT);
3218 if (temp)
3219 return temp;
3223 /* Try calculating parity (x) as popcount (x) % 2. */
3224 if (unoptab == parity_optab)
3226 temp = expand_parity (mode, op0, target);
3227 if (temp)
3228 return temp;
3231 /* Try implementing ffs (x) in terms of clz (x). */
3232 if (unoptab == ffs_optab)
3234 temp = expand_ffs (mode, op0, target);
3235 if (temp)
3236 return temp;
3239 /* Try implementing ctz (x) in terms of clz (x). */
3240 if (unoptab == ctz_optab)
3242 temp = expand_ctz (mode, op0, target);
3243 if (temp)
3244 return temp;
3247 try_libcall:
3248 /* Now try a library call in this mode. */
3249 libfunc = optab_libfunc (unoptab, mode);
3250 if (libfunc)
3252 rtx insns;
3253 rtx value;
3254 rtx eq_value;
3255 enum machine_mode outmode = mode;
3257 /* All of these functions return small values. Thus we choose to
3258 have them return something that isn't a double-word. */
3259 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3260 || unoptab == clrsb_optab || unoptab == popcount_optab
3261 || unoptab == parity_optab)
3262 outmode
3263 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3264 optab_libfunc (unoptab, mode)));
3266 start_sequence ();
3268 /* Pass 1 for NO_QUEUE so we don't lose any increments
3269 if the libcall is cse'd or moved. */
3270 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3271 1, op0, mode);
3272 insns = get_insns ();
3273 end_sequence ();
3275 target = gen_reg_rtx (outmode);
3276 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3277 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3278 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3279 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3280 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3281 emit_libcall_block_1 (insns, target, value, eq_value,
3282 trapv_unoptab_p (unoptab));
3284 return target;
3287 /* It can't be done in this mode. Can we do it in a wider mode? */
3289 if (CLASS_HAS_WIDER_MODES_P (mclass))
3291 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3292 wider_mode != VOIDmode;
3293 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3295 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3296 || optab_libfunc (unoptab, wider_mode))
3298 rtx xop0 = op0;
3299 rtx last = get_last_insn ();
3301 /* For certain operations, we need not actually extend
3302 the narrow operand, as long as we will truncate the
3303 results to the same narrowness. */
3304 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3305 (unoptab == neg_optab
3306 || unoptab == one_cmpl_optab
3307 || unoptab == bswap_optab)
3308 && mclass == MODE_INT);
3310 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3311 unsignedp);
3313 /* If we are generating clz using wider mode, adjust the
3314 result. Similarly for clrsb. */
3315 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3316 && temp != 0)
3317 temp = expand_binop
3318 (wider_mode, sub_optab, temp,
3319 gen_int_mode (GET_MODE_PRECISION (wider_mode)
3320 - GET_MODE_PRECISION (mode),
3321 wider_mode),
3322 target, true, OPTAB_DIRECT);
3324 /* Likewise for bswap. */
3325 if (unoptab == bswap_optab && temp != 0)
3327 gcc_assert (GET_MODE_PRECISION (wider_mode)
3328 == GET_MODE_BITSIZE (wider_mode)
3329 && GET_MODE_PRECISION (mode)
3330 == GET_MODE_BITSIZE (mode));
3332 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3333 GET_MODE_BITSIZE (wider_mode)
3334 - GET_MODE_BITSIZE (mode),
3335 NULL_RTX, true);
3338 if (temp)
3340 if (mclass != MODE_INT)
3342 if (target == 0)
3343 target = gen_reg_rtx (mode);
3344 convert_move (target, temp, 0);
3345 return target;
3347 else
3348 return gen_lowpart (mode, temp);
3350 else
3351 delete_insns_since (last);
3356 /* One final attempt at implementing negation via subtraction,
3357 this time allowing widening of the operand. */
3358 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3360 rtx temp;
3361 temp = expand_binop (mode,
3362 unoptab == negv_optab ? subv_optab : sub_optab,
3363 CONST0_RTX (mode), op0,
3364 target, unsignedp, OPTAB_LIB_WIDEN);
3365 if (temp)
3366 return temp;
3369 return 0;
3372 /* Emit code to compute the absolute value of OP0, with result to
3373 TARGET if convenient. (TARGET may be 0.) The return value says
3374 where the result actually is to be found.
3376 MODE is the mode of the operand; the mode of the result is
3377 different but can be deduced from MODE.
3382 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
3383 int result_unsignedp)
3385 rtx temp;
3387 if (GET_MODE_CLASS (mode) != MODE_INT
3388 || ! flag_trapv)
3389 result_unsignedp = 1;
3391 /* First try to do it with a special abs instruction. */
3392 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3393 op0, target, 0);
3394 if (temp != 0)
3395 return temp;
3397 /* For floating point modes, try clearing the sign bit. */
3398 if (SCALAR_FLOAT_MODE_P (mode))
3400 temp = expand_absneg_bit (ABS, mode, op0, target);
3401 if (temp)
3402 return temp;
3405 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3406 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3407 && !HONOR_SIGNED_ZEROS (mode))
3409 rtx last = get_last_insn ();
3411 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3412 op0, NULL_RTX, 0);
3413 if (temp != 0)
3414 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3415 OPTAB_WIDEN);
3417 if (temp != 0)
3418 return temp;
3420 delete_insns_since (last);
3423 /* If this machine has expensive jumps, we can do integer absolute
3424 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3425 where W is the width of MODE. */
3427 if (GET_MODE_CLASS (mode) == MODE_INT
3428 && BRANCH_COST (optimize_insn_for_speed_p (),
3429 false) >= 2)
3431 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3432 GET_MODE_PRECISION (mode) - 1,
3433 NULL_RTX, 0);
3435 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3436 OPTAB_LIB_WIDEN);
3437 if (temp != 0)
3438 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3439 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3441 if (temp != 0)
3442 return temp;
3445 return NULL_RTX;
3449 expand_abs (enum machine_mode mode, rtx op0, rtx target,
3450 int result_unsignedp, int safe)
3452 rtx temp, op1;
3454 if (GET_MODE_CLASS (mode) != MODE_INT
3455 || ! flag_trapv)
3456 result_unsignedp = 1;
3458 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3459 if (temp != 0)
3460 return temp;
3462 /* If that does not win, use conditional jump and negate. */
3464 /* It is safe to use the target if it is the same
3465 as the source if this is also a pseudo register */
3466 if (op0 == target && REG_P (op0)
3467 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3468 safe = 1;
3470 op1 = gen_label_rtx ();
3471 if (target == 0 || ! safe
3472 || GET_MODE (target) != mode
3473 || (MEM_P (target) && MEM_VOLATILE_P (target))
3474 || (REG_P (target)
3475 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3476 target = gen_reg_rtx (mode);
3478 emit_move_insn (target, op0);
3479 NO_DEFER_POP;
3481 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3482 NULL_RTX, NULL_RTX, op1, -1);
3484 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3485 target, target, 0);
3486 if (op0 != target)
3487 emit_move_insn (target, op0);
3488 emit_label (op1);
3489 OK_DEFER_POP;
3490 return target;
3493 /* Emit code to compute the one's complement absolute value of OP0
3494 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3495 (TARGET may be NULL_RTX.) The return value says where the result
3496 actually is to be found.
3498 MODE is the mode of the operand; the mode of the result is
3499 different but can be deduced from MODE. */
3502 expand_one_cmpl_abs_nojump (enum machine_mode mode, rtx op0, rtx target)
3504 rtx temp;
3506 /* Not applicable for floating point modes. */
3507 if (FLOAT_MODE_P (mode))
3508 return NULL_RTX;
3510 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3511 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3513 rtx last = get_last_insn ();
3515 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3516 if (temp != 0)
3517 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3518 OPTAB_WIDEN);
3520 if (temp != 0)
3521 return temp;
3523 delete_insns_since (last);
3526 /* If this machine has expensive jumps, we can do one's complement
3527 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3529 if (GET_MODE_CLASS (mode) == MODE_INT
3530 && BRANCH_COST (optimize_insn_for_speed_p (),
3531 false) >= 2)
3533 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3534 GET_MODE_PRECISION (mode) - 1,
3535 NULL_RTX, 0);
3537 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3538 OPTAB_LIB_WIDEN);
3540 if (temp != 0)
3541 return temp;
3544 return NULL_RTX;
3547 /* A subroutine of expand_copysign, perform the copysign operation using the
3548 abs and neg primitives advertised to exist on the target. The assumption
3549 is that we have a split register file, and leaving op0 in fp registers,
3550 and not playing with subregs so much, will help the register allocator. */
3552 static rtx
3553 expand_copysign_absneg (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3554 int bitpos, bool op0_is_abs)
3556 enum machine_mode imode;
3557 enum insn_code icode;
3558 rtx sign, label;
3560 if (target == op1)
3561 target = NULL_RTX;
3563 /* Check if the back end provides an insn that handles signbit for the
3564 argument's mode. */
3565 icode = optab_handler (signbit_optab, mode);
3566 if (icode != CODE_FOR_nothing)
3568 imode = insn_data[(int) icode].operand[0].mode;
3569 sign = gen_reg_rtx (imode);
3570 emit_unop_insn (icode, sign, op1, UNKNOWN);
3572 else
3574 double_int mask;
3576 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3578 imode = int_mode_for_mode (mode);
3579 if (imode == BLKmode)
3580 return NULL_RTX;
3581 op1 = gen_lowpart (imode, op1);
3583 else
3585 int word;
3587 imode = word_mode;
3588 if (FLOAT_WORDS_BIG_ENDIAN)
3589 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3590 else
3591 word = bitpos / BITS_PER_WORD;
3592 bitpos = bitpos % BITS_PER_WORD;
3593 op1 = operand_subword_force (op1, word, mode);
3596 mask = double_int_zero.set_bit (bitpos);
3598 sign = expand_binop (imode, and_optab, op1,
3599 immed_double_int_const (mask, imode),
3600 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3603 if (!op0_is_abs)
3605 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3606 if (op0 == NULL)
3607 return NULL_RTX;
3608 target = op0;
3610 else
3612 if (target == NULL_RTX)
3613 target = copy_to_reg (op0);
3614 else
3615 emit_move_insn (target, op0);
3618 label = gen_label_rtx ();
3619 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3621 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3622 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3623 else
3624 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3625 if (op0 != target)
3626 emit_move_insn (target, op0);
3628 emit_label (label);
3630 return target;
3634 /* A subroutine of expand_copysign, perform the entire copysign operation
3635 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3636 is true if op0 is known to have its sign bit clear. */
3638 static rtx
3639 expand_copysign_bit (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3640 int bitpos, bool op0_is_abs)
3642 enum machine_mode imode;
3643 double_int mask;
3644 int word, nwords, i;
3645 rtx temp, insns;
3647 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3649 imode = int_mode_for_mode (mode);
3650 if (imode == BLKmode)
3651 return NULL_RTX;
3652 word = 0;
3653 nwords = 1;
3655 else
3657 imode = word_mode;
3659 if (FLOAT_WORDS_BIG_ENDIAN)
3660 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3661 else
3662 word = bitpos / BITS_PER_WORD;
3663 bitpos = bitpos % BITS_PER_WORD;
3664 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3667 mask = double_int_zero.set_bit (bitpos);
3669 if (target == 0
3670 || target == op0
3671 || target == op1
3672 || (nwords > 1 && !valid_multiword_target_p (target)))
3673 target = gen_reg_rtx (mode);
3675 if (nwords > 1)
3677 start_sequence ();
3679 for (i = 0; i < nwords; ++i)
3681 rtx targ_piece = operand_subword (target, i, 1, mode);
3682 rtx op0_piece = operand_subword_force (op0, i, mode);
3684 if (i == word)
3686 if (!op0_is_abs)
3687 op0_piece
3688 = expand_binop (imode, and_optab, op0_piece,
3689 immed_double_int_const (~mask, imode),
3690 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3692 op1 = expand_binop (imode, and_optab,
3693 operand_subword_force (op1, i, mode),
3694 immed_double_int_const (mask, imode),
3695 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3697 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3698 targ_piece, 1, OPTAB_LIB_WIDEN);
3699 if (temp != targ_piece)
3700 emit_move_insn (targ_piece, temp);
3702 else
3703 emit_move_insn (targ_piece, op0_piece);
3706 insns = get_insns ();
3707 end_sequence ();
3709 emit_insn (insns);
3711 else
3713 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3714 immed_double_int_const (mask, imode),
3715 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3717 op0 = gen_lowpart (imode, op0);
3718 if (!op0_is_abs)
3719 op0 = expand_binop (imode, and_optab, op0,
3720 immed_double_int_const (~mask, imode),
3721 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3723 temp = expand_binop (imode, ior_optab, op0, op1,
3724 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3725 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3728 return target;
3731 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3732 scalar floating point mode. Return NULL if we do not know how to
3733 expand the operation inline. */
3736 expand_copysign (rtx op0, rtx op1, rtx target)
3738 enum machine_mode mode = GET_MODE (op0);
3739 const struct real_format *fmt;
3740 bool op0_is_abs;
3741 rtx temp;
3743 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3744 gcc_assert (GET_MODE (op1) == mode);
3746 /* First try to do it with a special instruction. */
3747 temp = expand_binop (mode, copysign_optab, op0, op1,
3748 target, 0, OPTAB_DIRECT);
3749 if (temp)
3750 return temp;
3752 fmt = REAL_MODE_FORMAT (mode);
3753 if (fmt == NULL || !fmt->has_signed_zero)
3754 return NULL_RTX;
3756 op0_is_abs = false;
3757 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3759 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3760 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3761 op0_is_abs = true;
3764 if (fmt->signbit_ro >= 0
3765 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3766 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3767 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3769 temp = expand_copysign_absneg (mode, op0, op1, target,
3770 fmt->signbit_ro, op0_is_abs);
3771 if (temp)
3772 return temp;
3775 if (fmt->signbit_rw < 0)
3776 return NULL_RTX;
3777 return expand_copysign_bit (mode, op0, op1, target,
3778 fmt->signbit_rw, op0_is_abs);
3781 /* Generate an instruction whose insn-code is INSN_CODE,
3782 with two operands: an output TARGET and an input OP0.
3783 TARGET *must* be nonzero, and the output is always stored there.
3784 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3785 the value that is stored into TARGET.
3787 Return false if expansion failed. */
3789 bool
3790 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3791 enum rtx_code code)
3793 struct expand_operand ops[2];
3794 rtx pat;
3796 create_output_operand (&ops[0], target, GET_MODE (target));
3797 create_input_operand (&ops[1], op0, GET_MODE (op0));
3798 pat = maybe_gen_insn (icode, 2, ops);
3799 if (!pat)
3800 return false;
3802 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
3803 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3805 emit_insn (pat);
3807 if (ops[0].value != target)
3808 emit_move_insn (target, ops[0].value);
3809 return true;
3811 /* Generate an instruction whose insn-code is INSN_CODE,
3812 with two operands: an output TARGET and an input OP0.
3813 TARGET *must* be nonzero, and the output is always stored there.
3814 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3815 the value that is stored into TARGET. */
3817 void
3818 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3820 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3821 gcc_assert (ok);
3824 struct no_conflict_data
3826 rtx target, first, insn;
3827 bool must_stay;
3830 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3831 the currently examined clobber / store has to stay in the list of
3832 insns that constitute the actual libcall block. */
3833 static void
3834 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3836 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3838 /* If this inns directly contributes to setting the target, it must stay. */
3839 if (reg_overlap_mentioned_p (p->target, dest))
3840 p->must_stay = true;
3841 /* If we haven't committed to keeping any other insns in the list yet,
3842 there is nothing more to check. */
3843 else if (p->insn == p->first)
3844 return;
3845 /* If this insn sets / clobbers a register that feeds one of the insns
3846 already in the list, this insn has to stay too. */
3847 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3848 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3849 || reg_used_between_p (dest, p->first, p->insn)
3850 /* Likewise if this insn depends on a register set by a previous
3851 insn in the list, or if it sets a result (presumably a hard
3852 register) that is set or clobbered by a previous insn.
3853 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3854 SET_DEST perform the former check on the address, and the latter
3855 check on the MEM. */
3856 || (GET_CODE (set) == SET
3857 && (modified_in_p (SET_SRC (set), p->first)
3858 || modified_in_p (SET_DEST (set), p->first)
3859 || modified_between_p (SET_SRC (set), p->first, p->insn)
3860 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3861 p->must_stay = true;
3865 /* Emit code to make a call to a constant function or a library call.
3867 INSNS is a list containing all insns emitted in the call.
3868 These insns leave the result in RESULT. Our block is to copy RESULT
3869 to TARGET, which is logically equivalent to EQUIV.
3871 We first emit any insns that set a pseudo on the assumption that these are
3872 loading constants into registers; doing so allows them to be safely cse'ed
3873 between blocks. Then we emit all the other insns in the block, followed by
3874 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3875 note with an operand of EQUIV. */
3877 static void
3878 emit_libcall_block_1 (rtx insns, rtx target, rtx result, rtx equiv,
3879 bool equiv_may_trap)
3881 rtx final_dest = target;
3882 rtx next, last, insn;
3884 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3885 into a MEM later. Protect the libcall block from this change. */
3886 if (! REG_P (target) || REG_USERVAR_P (target))
3887 target = gen_reg_rtx (GET_MODE (target));
3889 /* If we're using non-call exceptions, a libcall corresponding to an
3890 operation that may trap may also trap. */
3891 /* ??? See the comment in front of make_reg_eh_region_note. */
3892 if (cfun->can_throw_non_call_exceptions
3893 && (equiv_may_trap || may_trap_p (equiv)))
3895 for (insn = insns; insn; insn = NEXT_INSN (insn))
3896 if (CALL_P (insn))
3898 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3899 if (note)
3901 int lp_nr = INTVAL (XEXP (note, 0));
3902 if (lp_nr == 0 || lp_nr == INT_MIN)
3903 remove_note (insn, note);
3907 else
3909 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3910 reg note to indicate that this call cannot throw or execute a nonlocal
3911 goto (unless there is already a REG_EH_REGION note, in which case
3912 we update it). */
3913 for (insn = insns; insn; insn = NEXT_INSN (insn))
3914 if (CALL_P (insn))
3915 make_reg_eh_region_note_nothrow_nononlocal (insn);
3918 /* First emit all insns that set pseudos. Remove them from the list as
3919 we go. Avoid insns that set pseudos which were referenced in previous
3920 insns. These can be generated by move_by_pieces, for example,
3921 to update an address. Similarly, avoid insns that reference things
3922 set in previous insns. */
3924 for (insn = insns; insn; insn = next)
3926 rtx set = single_set (insn);
3928 next = NEXT_INSN (insn);
3930 if (set != 0 && REG_P (SET_DEST (set))
3931 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3933 struct no_conflict_data data;
3935 data.target = const0_rtx;
3936 data.first = insns;
3937 data.insn = insn;
3938 data.must_stay = 0;
3939 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3940 if (! data.must_stay)
3942 if (PREV_INSN (insn))
3943 NEXT_INSN (PREV_INSN (insn)) = next;
3944 else
3945 insns = next;
3947 if (next)
3948 PREV_INSN (next) = PREV_INSN (insn);
3950 add_insn (insn);
3954 /* Some ports use a loop to copy large arguments onto the stack.
3955 Don't move anything outside such a loop. */
3956 if (LABEL_P (insn))
3957 break;
3960 /* Write the remaining insns followed by the final copy. */
3961 for (insn = insns; insn; insn = next)
3963 next = NEXT_INSN (insn);
3965 add_insn (insn);
3968 last = emit_move_insn (target, result);
3969 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3971 if (final_dest != target)
3972 emit_move_insn (final_dest, target);
3975 void
3976 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3978 emit_libcall_block_1 (insns, target, result, equiv, false);
3981 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3982 PURPOSE describes how this comparison will be used. CODE is the rtx
3983 comparison code we will be using.
3985 ??? Actually, CODE is slightly weaker than that. A target is still
3986 required to implement all of the normal bcc operations, but not
3987 required to implement all (or any) of the unordered bcc operations. */
3990 can_compare_p (enum rtx_code code, enum machine_mode mode,
3991 enum can_compare_purpose purpose)
3993 rtx test;
3994 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3997 enum insn_code icode;
3999 if (purpose == ccp_jump
4000 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
4001 && insn_operand_matches (icode, 0, test))
4002 return 1;
4003 if (purpose == ccp_store_flag
4004 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
4005 && insn_operand_matches (icode, 1, test))
4006 return 1;
4007 if (purpose == ccp_cmov
4008 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
4009 return 1;
4011 mode = GET_MODE_WIDER_MODE (mode);
4012 PUT_MODE (test, mode);
4014 while (mode != VOIDmode);
4016 return 0;
4019 /* This function is called when we are going to emit a compare instruction that
4020 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
4022 *PMODE is the mode of the inputs (in case they are const_int).
4023 *PUNSIGNEDP nonzero says that the operands are unsigned;
4024 this matters if they need to be widened (as given by METHODS).
4026 If they have mode BLKmode, then SIZE specifies the size of both operands.
4028 This function performs all the setup necessary so that the caller only has
4029 to emit a single comparison insn. This setup can involve doing a BLKmode
4030 comparison or emitting a library call to perform the comparison if no insn
4031 is available to handle it.
4032 The values which are passed in through pointers can be modified; the caller
4033 should perform the comparison on the modified values. Constant
4034 comparisons must have already been folded. */
4036 static void
4037 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4038 int unsignedp, enum optab_methods methods,
4039 rtx *ptest, enum machine_mode *pmode)
4041 enum machine_mode mode = *pmode;
4042 rtx libfunc, test;
4043 enum machine_mode cmp_mode;
4044 enum mode_class mclass;
4046 /* The other methods are not needed. */
4047 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4048 || methods == OPTAB_LIB_WIDEN);
4050 /* If we are optimizing, force expensive constants into a register. */
4051 if (CONSTANT_P (x) && optimize
4052 && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
4053 > COSTS_N_INSNS (1)))
4054 x = force_reg (mode, x);
4056 if (CONSTANT_P (y) && optimize
4057 && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
4058 > COSTS_N_INSNS (1)))
4059 y = force_reg (mode, y);
4061 #ifdef HAVE_cc0
4062 /* Make sure if we have a canonical comparison. The RTL
4063 documentation states that canonical comparisons are required only
4064 for targets which have cc0. */
4065 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4066 #endif
4068 /* Don't let both operands fail to indicate the mode. */
4069 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4070 x = force_reg (mode, x);
4071 if (mode == VOIDmode)
4072 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4074 /* Handle all BLKmode compares. */
4076 if (mode == BLKmode)
4078 enum machine_mode result_mode;
4079 enum insn_code cmp_code;
4080 tree length_type;
4081 rtx libfunc;
4082 rtx result;
4083 rtx opalign
4084 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4086 gcc_assert (size);
4088 /* Try to use a memory block compare insn - either cmpstr
4089 or cmpmem will do. */
4090 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4091 cmp_mode != VOIDmode;
4092 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4094 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4095 if (cmp_code == CODE_FOR_nothing)
4096 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4097 if (cmp_code == CODE_FOR_nothing)
4098 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4099 if (cmp_code == CODE_FOR_nothing)
4100 continue;
4102 /* Must make sure the size fits the insn's mode. */
4103 if ((CONST_INT_P (size)
4104 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4105 || (GET_MODE_BITSIZE (GET_MODE (size))
4106 > GET_MODE_BITSIZE (cmp_mode)))
4107 continue;
4109 result_mode = insn_data[cmp_code].operand[0].mode;
4110 result = gen_reg_rtx (result_mode);
4111 size = convert_to_mode (cmp_mode, size, 1);
4112 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4114 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4115 *pmode = result_mode;
4116 return;
4119 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4120 goto fail;
4122 /* Otherwise call a library function, memcmp. */
4123 libfunc = memcmp_libfunc;
4124 length_type = sizetype;
4125 result_mode = TYPE_MODE (integer_type_node);
4126 cmp_mode = TYPE_MODE (length_type);
4127 size = convert_to_mode (TYPE_MODE (length_type), size,
4128 TYPE_UNSIGNED (length_type));
4130 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4131 result_mode, 3,
4132 XEXP (x, 0), Pmode,
4133 XEXP (y, 0), Pmode,
4134 size, cmp_mode);
4135 x = result;
4136 y = const0_rtx;
4137 mode = result_mode;
4138 methods = OPTAB_LIB_WIDEN;
4139 unsignedp = false;
4142 /* Don't allow operands to the compare to trap, as that can put the
4143 compare and branch in different basic blocks. */
4144 if (cfun->can_throw_non_call_exceptions)
4146 if (may_trap_p (x))
4147 x = force_reg (mode, x);
4148 if (may_trap_p (y))
4149 y = force_reg (mode, y);
4152 if (GET_MODE_CLASS (mode) == MODE_CC)
4154 gcc_assert (can_compare_p (comparison, CCmode, ccp_jump));
4155 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4156 return;
4159 mclass = GET_MODE_CLASS (mode);
4160 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4161 cmp_mode = mode;
4164 enum insn_code icode;
4165 icode = optab_handler (cbranch_optab, cmp_mode);
4166 if (icode != CODE_FOR_nothing
4167 && insn_operand_matches (icode, 0, test))
4169 rtx last = get_last_insn ();
4170 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4171 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4172 if (op0 && op1
4173 && insn_operand_matches (icode, 1, op0)
4174 && insn_operand_matches (icode, 2, op1))
4176 XEXP (test, 0) = op0;
4177 XEXP (test, 1) = op1;
4178 *ptest = test;
4179 *pmode = cmp_mode;
4180 return;
4182 delete_insns_since (last);
4185 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4186 break;
4187 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4189 while (cmp_mode != VOIDmode);
4191 if (methods != OPTAB_LIB_WIDEN)
4192 goto fail;
4194 if (!SCALAR_FLOAT_MODE_P (mode))
4196 rtx result;
4197 enum machine_mode ret_mode;
4199 /* Handle a libcall just for the mode we are using. */
4200 libfunc = optab_libfunc (cmp_optab, mode);
4201 gcc_assert (libfunc);
4203 /* If we want unsigned, and this mode has a distinct unsigned
4204 comparison routine, use that. */
4205 if (unsignedp)
4207 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4208 if (ulibfunc)
4209 libfunc = ulibfunc;
4212 ret_mode = targetm.libgcc_cmp_return_mode ();
4213 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4214 ret_mode, 2, x, mode, y, mode);
4216 /* There are two kinds of comparison routines. Biased routines
4217 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4218 of gcc expect that the comparison operation is equivalent
4219 to the modified comparison. For signed comparisons compare the
4220 result against 1 in the biased case, and zero in the unbiased
4221 case. For unsigned comparisons always compare against 1 after
4222 biasing the unbiased result by adding 1. This gives us a way to
4223 represent LTU.
4224 The comparisons in the fixed-point helper library are always
4225 biased. */
4226 x = result;
4227 y = const1_rtx;
4229 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4231 if (unsignedp)
4232 x = plus_constant (ret_mode, result, 1);
4233 else
4234 y = const0_rtx;
4237 *pmode = word_mode;
4238 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4239 ptest, pmode);
4241 else
4242 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4244 return;
4246 fail:
4247 *ptest = NULL_RTX;
4250 /* Before emitting an insn with code ICODE, make sure that X, which is going
4251 to be used for operand OPNUM of the insn, is converted from mode MODE to
4252 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4253 that it is accepted by the operand predicate. Return the new value. */
4256 prepare_operand (enum insn_code icode, rtx x, int opnum, enum machine_mode mode,
4257 enum machine_mode wider_mode, int unsignedp)
4259 if (mode != wider_mode)
4260 x = convert_modes (wider_mode, mode, x, unsignedp);
4262 if (!insn_operand_matches (icode, opnum, x))
4264 if (reload_completed)
4265 return NULL_RTX;
4266 x = copy_to_mode_reg (insn_data[(int) icode].operand[opnum].mode, x);
4269 return x;
4272 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4273 we can do the branch. */
4275 static void
4276 emit_cmp_and_jump_insn_1 (rtx test, enum machine_mode mode, rtx label, int prob)
4278 enum machine_mode optab_mode;
4279 enum mode_class mclass;
4280 enum insn_code icode;
4281 rtx insn;
4283 mclass = GET_MODE_CLASS (mode);
4284 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4285 icode = optab_handler (cbranch_optab, optab_mode);
4287 gcc_assert (icode != CODE_FOR_nothing);
4288 gcc_assert (insn_operand_matches (icode, 0, test));
4289 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4290 XEXP (test, 1), label));
4291 if (prob != -1
4292 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4293 && insn
4294 && JUMP_P (insn)
4295 && any_condjump_p (insn)
4296 && !find_reg_note (insn, REG_BR_PROB, 0))
4297 add_int_reg_note (insn, REG_BR_PROB, prob);
4300 /* Generate code to compare X with Y so that the condition codes are
4301 set and to jump to LABEL if the condition is true. If X is a
4302 constant and Y is not a constant, then the comparison is swapped to
4303 ensure that the comparison RTL has the canonical form.
4305 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4306 need to be widened. UNSIGNEDP is also used to select the proper
4307 branch condition code.
4309 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4311 MODE is the mode of the inputs (in case they are const_int).
4313 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4314 It will be potentially converted into an unsigned variant based on
4315 UNSIGNEDP to select a proper jump instruction.
4317 PROB is the probability of jumping to LABEL. */
4319 void
4320 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4321 enum machine_mode mode, int unsignedp, rtx label,
4322 int prob)
4324 rtx op0 = x, op1 = y;
4325 rtx test;
4327 /* Swap operands and condition to ensure canonical RTL. */
4328 if (swap_commutative_operands_p (x, y)
4329 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4331 op0 = y, op1 = x;
4332 comparison = swap_condition (comparison);
4335 /* If OP0 is still a constant, then both X and Y must be constants
4336 or the opposite comparison is not supported. Force X into a register
4337 to create canonical RTL. */
4338 if (CONSTANT_P (op0))
4339 op0 = force_reg (mode, op0);
4341 if (unsignedp)
4342 comparison = unsigned_condition (comparison);
4344 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4345 &test, &mode);
4346 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4350 /* Emit a library call comparison between floating point X and Y.
4351 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4353 static void
4354 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4355 rtx *ptest, enum machine_mode *pmode)
4357 enum rtx_code swapped = swap_condition (comparison);
4358 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4359 enum machine_mode orig_mode = GET_MODE (x);
4360 enum machine_mode mode, cmp_mode;
4361 rtx true_rtx, false_rtx;
4362 rtx value, target, insns, equiv;
4363 rtx libfunc = 0;
4364 bool reversed_p = false;
4365 cmp_mode = targetm.libgcc_cmp_return_mode ();
4367 for (mode = orig_mode;
4368 mode != VOIDmode;
4369 mode = GET_MODE_WIDER_MODE (mode))
4371 if (code_to_optab (comparison)
4372 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4373 break;
4375 if (code_to_optab (swapped)
4376 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4378 rtx tmp;
4379 tmp = x; x = y; y = tmp;
4380 comparison = swapped;
4381 break;
4384 if (code_to_optab (reversed)
4385 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4387 comparison = reversed;
4388 reversed_p = true;
4389 break;
4393 gcc_assert (mode != VOIDmode);
4395 if (mode != orig_mode)
4397 x = convert_to_mode (mode, x, 0);
4398 y = convert_to_mode (mode, y, 0);
4401 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4402 the RTL. The allows the RTL optimizers to delete the libcall if the
4403 condition can be determined at compile-time. */
4404 if (comparison == UNORDERED
4405 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4407 true_rtx = const_true_rtx;
4408 false_rtx = const0_rtx;
4410 else
4412 switch (comparison)
4414 case EQ:
4415 true_rtx = const0_rtx;
4416 false_rtx = const_true_rtx;
4417 break;
4419 case NE:
4420 true_rtx = const_true_rtx;
4421 false_rtx = const0_rtx;
4422 break;
4424 case GT:
4425 true_rtx = const1_rtx;
4426 false_rtx = const0_rtx;
4427 break;
4429 case GE:
4430 true_rtx = const0_rtx;
4431 false_rtx = constm1_rtx;
4432 break;
4434 case LT:
4435 true_rtx = constm1_rtx;
4436 false_rtx = const0_rtx;
4437 break;
4439 case LE:
4440 true_rtx = const0_rtx;
4441 false_rtx = const1_rtx;
4442 break;
4444 default:
4445 gcc_unreachable ();
4449 if (comparison == UNORDERED)
4451 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4452 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4453 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4454 temp, const_true_rtx, equiv);
4456 else
4458 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4459 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4460 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4461 equiv, true_rtx, false_rtx);
4464 start_sequence ();
4465 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4466 cmp_mode, 2, x, mode, y, mode);
4467 insns = get_insns ();
4468 end_sequence ();
4470 target = gen_reg_rtx (cmp_mode);
4471 emit_libcall_block (insns, target, value, equiv);
4473 if (comparison == UNORDERED
4474 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4475 || reversed_p)
4476 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4477 else
4478 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4480 *pmode = cmp_mode;
4483 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4485 void
4486 emit_indirect_jump (rtx loc)
4488 struct expand_operand ops[1];
4490 create_address_operand (&ops[0], loc);
4491 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4492 emit_barrier ();
4495 #ifdef HAVE_conditional_move
4497 /* Emit a conditional move instruction if the machine supports one for that
4498 condition and machine mode.
4500 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4501 the mode to use should they be constants. If it is VOIDmode, they cannot
4502 both be constants.
4504 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4505 should be stored there. MODE is the mode to use should they be constants.
4506 If it is VOIDmode, they cannot both be constants.
4508 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4509 is not supported. */
4512 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4513 enum machine_mode cmode, rtx op2, rtx op3,
4514 enum machine_mode mode, int unsignedp)
4516 rtx tem, comparison, last;
4517 enum insn_code icode;
4518 enum rtx_code reversed;
4520 /* If one operand is constant, make it the second one. Only do this
4521 if the other operand is not constant as well. */
4523 if (swap_commutative_operands_p (op0, op1))
4525 tem = op0;
4526 op0 = op1;
4527 op1 = tem;
4528 code = swap_condition (code);
4531 /* get_condition will prefer to generate LT and GT even if the old
4532 comparison was against zero, so undo that canonicalization here since
4533 comparisons against zero are cheaper. */
4534 if (code == LT && op1 == const1_rtx)
4535 code = LE, op1 = const0_rtx;
4536 else if (code == GT && op1 == constm1_rtx)
4537 code = GE, op1 = const0_rtx;
4539 if (cmode == VOIDmode)
4540 cmode = GET_MODE (op0);
4542 if (swap_commutative_operands_p (op2, op3)
4543 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4544 != UNKNOWN))
4546 tem = op2;
4547 op2 = op3;
4548 op3 = tem;
4549 code = reversed;
4552 if (mode == VOIDmode)
4553 mode = GET_MODE (op2);
4555 icode = direct_optab_handler (movcc_optab, mode);
4557 if (icode == CODE_FOR_nothing)
4558 return 0;
4560 if (!target)
4561 target = gen_reg_rtx (mode);
4563 code = unsignedp ? unsigned_condition (code) : code;
4564 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4566 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4567 return NULL and let the caller figure out how best to deal with this
4568 situation. */
4569 if (!COMPARISON_P (comparison))
4570 return NULL_RTX;
4572 saved_pending_stack_adjust save;
4573 save_pending_stack_adjust (&save);
4574 last = get_last_insn ();
4575 do_pending_stack_adjust ();
4576 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4577 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4578 &comparison, &cmode);
4579 if (comparison)
4581 struct expand_operand ops[4];
4583 create_output_operand (&ops[0], target, mode);
4584 create_fixed_operand (&ops[1], comparison);
4585 create_input_operand (&ops[2], op2, mode);
4586 create_input_operand (&ops[3], op3, mode);
4587 if (maybe_expand_insn (icode, 4, ops))
4589 if (ops[0].value != target)
4590 convert_move (target, ops[0].value, false);
4591 return target;
4594 delete_insns_since (last);
4595 restore_pending_stack_adjust (&save);
4596 return NULL_RTX;
4599 /* Return nonzero if a conditional move of mode MODE is supported.
4601 This function is for combine so it can tell whether an insn that looks
4602 like a conditional move is actually supported by the hardware. If we
4603 guess wrong we lose a bit on optimization, but that's it. */
4604 /* ??? sparc64 supports conditionally moving integers values based on fp
4605 comparisons, and vice versa. How do we handle them? */
4608 can_conditionally_move_p (enum machine_mode mode)
4610 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4611 return 1;
4613 return 0;
4616 #endif /* HAVE_conditional_move */
4618 /* Emit a conditional addition instruction if the machine supports one for that
4619 condition and machine mode.
4621 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4622 the mode to use should they be constants. If it is VOIDmode, they cannot
4623 both be constants.
4625 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4626 should be stored there. MODE is the mode to use should they be constants.
4627 If it is VOIDmode, they cannot both be constants.
4629 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4630 is not supported. */
4633 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4634 enum machine_mode cmode, rtx op2, rtx op3,
4635 enum machine_mode mode, int unsignedp)
4637 rtx tem, comparison, last;
4638 enum insn_code icode;
4640 /* If one operand is constant, make it the second one. Only do this
4641 if the other operand is not constant as well. */
4643 if (swap_commutative_operands_p (op0, op1))
4645 tem = op0;
4646 op0 = op1;
4647 op1 = tem;
4648 code = swap_condition (code);
4651 /* get_condition will prefer to generate LT and GT even if the old
4652 comparison was against zero, so undo that canonicalization here since
4653 comparisons against zero are cheaper. */
4654 if (code == LT && op1 == const1_rtx)
4655 code = LE, op1 = const0_rtx;
4656 else if (code == GT && op1 == constm1_rtx)
4657 code = GE, op1 = const0_rtx;
4659 if (cmode == VOIDmode)
4660 cmode = GET_MODE (op0);
4662 if (mode == VOIDmode)
4663 mode = GET_MODE (op2);
4665 icode = optab_handler (addcc_optab, mode);
4667 if (icode == CODE_FOR_nothing)
4668 return 0;
4670 if (!target)
4671 target = gen_reg_rtx (mode);
4673 code = unsignedp ? unsigned_condition (code) : code;
4674 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4676 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4677 return NULL and let the caller figure out how best to deal with this
4678 situation. */
4679 if (!COMPARISON_P (comparison))
4680 return NULL_RTX;
4682 do_pending_stack_adjust ();
4683 last = get_last_insn ();
4684 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4685 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4686 &comparison, &cmode);
4687 if (comparison)
4689 struct expand_operand ops[4];
4691 create_output_operand (&ops[0], target, mode);
4692 create_fixed_operand (&ops[1], comparison);
4693 create_input_operand (&ops[2], op2, mode);
4694 create_input_operand (&ops[3], op3, mode);
4695 if (maybe_expand_insn (icode, 4, ops))
4697 if (ops[0].value != target)
4698 convert_move (target, ops[0].value, false);
4699 return target;
4702 delete_insns_since (last);
4703 return NULL_RTX;
4706 /* These functions attempt to generate an insn body, rather than
4707 emitting the insn, but if the gen function already emits them, we
4708 make no attempt to turn them back into naked patterns. */
4710 /* Generate and return an insn body to add Y to X. */
4713 gen_add2_insn (rtx x, rtx y)
4715 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4717 gcc_assert (insn_operand_matches (icode, 0, x));
4718 gcc_assert (insn_operand_matches (icode, 1, x));
4719 gcc_assert (insn_operand_matches (icode, 2, y));
4721 return GEN_FCN (icode) (x, x, y);
4724 /* Generate and return an insn body to add r1 and c,
4725 storing the result in r0. */
4728 gen_add3_insn (rtx r0, rtx r1, rtx c)
4730 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4732 if (icode == CODE_FOR_nothing
4733 || !insn_operand_matches (icode, 0, r0)
4734 || !insn_operand_matches (icode, 1, r1)
4735 || !insn_operand_matches (icode, 2, c))
4736 return NULL_RTX;
4738 return GEN_FCN (icode) (r0, r1, c);
4742 have_add2_insn (rtx x, rtx y)
4744 enum insn_code icode;
4746 gcc_assert (GET_MODE (x) != VOIDmode);
4748 icode = optab_handler (add_optab, GET_MODE (x));
4750 if (icode == CODE_FOR_nothing)
4751 return 0;
4753 if (!insn_operand_matches (icode, 0, x)
4754 || !insn_operand_matches (icode, 1, x)
4755 || !insn_operand_matches (icode, 2, y))
4756 return 0;
4758 return 1;
4761 /* Generate and return an insn body to add Y to X. */
4764 gen_addptr3_insn (rtx x, rtx y, rtx z)
4766 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4768 gcc_assert (insn_operand_matches (icode, 0, x));
4769 gcc_assert (insn_operand_matches (icode, 1, y));
4770 gcc_assert (insn_operand_matches (icode, 2, z));
4772 return GEN_FCN (icode) (x, y, z);
4775 /* Return true if the target implements an addptr pattern and X, Y,
4776 and Z are valid for the pattern predicates. */
4779 have_addptr3_insn (rtx x, rtx y, rtx z)
4781 enum insn_code icode;
4783 gcc_assert (GET_MODE (x) != VOIDmode);
4785 icode = optab_handler (addptr3_optab, GET_MODE (x));
4787 if (icode == CODE_FOR_nothing)
4788 return 0;
4790 if (!insn_operand_matches (icode, 0, x)
4791 || !insn_operand_matches (icode, 1, y)
4792 || !insn_operand_matches (icode, 2, z))
4793 return 0;
4795 return 1;
4798 /* Generate and return an insn body to subtract Y from X. */
4801 gen_sub2_insn (rtx x, rtx y)
4803 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4805 gcc_assert (insn_operand_matches (icode, 0, x));
4806 gcc_assert (insn_operand_matches (icode, 1, x));
4807 gcc_assert (insn_operand_matches (icode, 2, y));
4809 return GEN_FCN (icode) (x, x, y);
4812 /* Generate and return an insn body to subtract r1 and c,
4813 storing the result in r0. */
4816 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4818 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4820 if (icode == CODE_FOR_nothing
4821 || !insn_operand_matches (icode, 0, r0)
4822 || !insn_operand_matches (icode, 1, r1)
4823 || !insn_operand_matches (icode, 2, c))
4824 return NULL_RTX;
4826 return GEN_FCN (icode) (r0, r1, c);
4830 have_sub2_insn (rtx x, rtx y)
4832 enum insn_code icode;
4834 gcc_assert (GET_MODE (x) != VOIDmode);
4836 icode = optab_handler (sub_optab, GET_MODE (x));
4838 if (icode == CODE_FOR_nothing)
4839 return 0;
4841 if (!insn_operand_matches (icode, 0, x)
4842 || !insn_operand_matches (icode, 1, x)
4843 || !insn_operand_matches (icode, 2, y))
4844 return 0;
4846 return 1;
4849 /* Generate the body of an instruction to copy Y into X.
4850 It may be a list of insns, if one insn isn't enough. */
4853 gen_move_insn (rtx x, rtx y)
4855 rtx seq;
4857 start_sequence ();
4858 emit_move_insn_1 (x, y);
4859 seq = get_insns ();
4860 end_sequence ();
4861 return seq;
4864 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4865 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4866 no such operation exists, CODE_FOR_nothing will be returned. */
4868 enum insn_code
4869 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
4870 int unsignedp)
4872 convert_optab tab;
4873 #ifdef HAVE_ptr_extend
4874 if (unsignedp < 0)
4875 return CODE_FOR_ptr_extend;
4876 #endif
4878 tab = unsignedp ? zext_optab : sext_optab;
4879 return convert_optab_handler (tab, to_mode, from_mode);
4882 /* Generate the body of an insn to extend Y (with mode MFROM)
4883 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4886 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4887 enum machine_mode mfrom, int unsignedp)
4889 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4890 return GEN_FCN (icode) (x, y);
4893 /* can_fix_p and can_float_p say whether the target machine
4894 can directly convert a given fixed point type to
4895 a given floating point type, or vice versa.
4896 The returned value is the CODE_FOR_... value to use,
4897 or CODE_FOR_nothing if these modes cannot be directly converted.
4899 *TRUNCP_PTR is set to 1 if it is necessary to output
4900 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4902 static enum insn_code
4903 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4904 int unsignedp, int *truncp_ptr)
4906 convert_optab tab;
4907 enum insn_code icode;
4909 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4910 icode = convert_optab_handler (tab, fixmode, fltmode);
4911 if (icode != CODE_FOR_nothing)
4913 *truncp_ptr = 0;
4914 return icode;
4917 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4918 for this to work. We need to rework the fix* and ftrunc* patterns
4919 and documentation. */
4920 tab = unsignedp ? ufix_optab : sfix_optab;
4921 icode = convert_optab_handler (tab, fixmode, fltmode);
4922 if (icode != CODE_FOR_nothing
4923 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4925 *truncp_ptr = 1;
4926 return icode;
4929 *truncp_ptr = 0;
4930 return CODE_FOR_nothing;
4933 enum insn_code
4934 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4935 int unsignedp)
4937 convert_optab tab;
4939 tab = unsignedp ? ufloat_optab : sfloat_optab;
4940 return convert_optab_handler (tab, fltmode, fixmode);
4943 /* Function supportable_convert_operation
4945 Check whether an operation represented by the code CODE is a
4946 convert operation that is supported by the target platform in
4947 vector form (i.e., when operating on arguments of type VECTYPE_IN
4948 producing a result of type VECTYPE_OUT).
4950 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4951 This function checks if these operations are supported
4952 by the target platform either directly (via vector tree-codes), or via
4953 target builtins.
4955 Output:
4956 - CODE1 is code of vector operation to be used when
4957 vectorizing the operation, if available.
4958 - DECL is decl of target builtin functions to be used
4959 when vectorizing the operation, if available. In this case,
4960 CODE1 is CALL_EXPR. */
4962 bool
4963 supportable_convert_operation (enum tree_code code,
4964 tree vectype_out, tree vectype_in,
4965 tree *decl, enum tree_code *code1)
4967 enum machine_mode m1,m2;
4968 int truncp;
4970 m1 = TYPE_MODE (vectype_out);
4971 m2 = TYPE_MODE (vectype_in);
4973 /* First check if we can done conversion directly. */
4974 if ((code == FIX_TRUNC_EXPR
4975 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
4976 != CODE_FOR_nothing)
4977 || (code == FLOAT_EXPR
4978 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
4979 != CODE_FOR_nothing))
4981 *code1 = code;
4982 return true;
4985 /* Now check for builtin. */
4986 if (targetm.vectorize.builtin_conversion
4987 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
4989 *code1 = CALL_EXPR;
4990 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
4991 return true;
4993 return false;
4997 /* Generate code to convert FROM to floating point
4998 and store in TO. FROM must be fixed point and not VOIDmode.
4999 UNSIGNEDP nonzero means regard FROM as unsigned.
5000 Normally this is done by correcting the final value
5001 if it is negative. */
5003 void
5004 expand_float (rtx to, rtx from, int unsignedp)
5006 enum insn_code icode;
5007 rtx target = to;
5008 enum machine_mode fmode, imode;
5009 bool can_do_signed = false;
5011 /* Crash now, because we won't be able to decide which mode to use. */
5012 gcc_assert (GET_MODE (from) != VOIDmode);
5014 /* Look for an insn to do the conversion. Do it in the specified
5015 modes if possible; otherwise convert either input, output or both to
5016 wider mode. If the integer mode is wider than the mode of FROM,
5017 we can do the conversion signed even if the input is unsigned. */
5019 for (fmode = GET_MODE (to); fmode != VOIDmode;
5020 fmode = GET_MODE_WIDER_MODE (fmode))
5021 for (imode = GET_MODE (from); imode != VOIDmode;
5022 imode = GET_MODE_WIDER_MODE (imode))
5024 int doing_unsigned = unsignedp;
5026 if (fmode != GET_MODE (to)
5027 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
5028 continue;
5030 icode = can_float_p (fmode, imode, unsignedp);
5031 if (icode == CODE_FOR_nothing && unsignedp)
5033 enum insn_code scode = can_float_p (fmode, imode, 0);
5034 if (scode != CODE_FOR_nothing)
5035 can_do_signed = true;
5036 if (imode != GET_MODE (from))
5037 icode = scode, doing_unsigned = 0;
5040 if (icode != CODE_FOR_nothing)
5042 if (imode != GET_MODE (from))
5043 from = convert_to_mode (imode, from, unsignedp);
5045 if (fmode != GET_MODE (to))
5046 target = gen_reg_rtx (fmode);
5048 emit_unop_insn (icode, target, from,
5049 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
5051 if (target != to)
5052 convert_move (to, target, 0);
5053 return;
5057 /* Unsigned integer, and no way to convert directly. Convert as signed,
5058 then unconditionally adjust the result. */
5059 if (unsignedp && can_do_signed)
5061 rtx label = gen_label_rtx ();
5062 rtx temp;
5063 REAL_VALUE_TYPE offset;
5065 /* Look for a usable floating mode FMODE wider than the source and at
5066 least as wide as the target. Using FMODE will avoid rounding woes
5067 with unsigned values greater than the signed maximum value. */
5069 for (fmode = GET_MODE (to); fmode != VOIDmode;
5070 fmode = GET_MODE_WIDER_MODE (fmode))
5071 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
5072 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
5073 break;
5075 if (fmode == VOIDmode)
5077 /* There is no such mode. Pretend the target is wide enough. */
5078 fmode = GET_MODE (to);
5080 /* Avoid double-rounding when TO is narrower than FROM. */
5081 if ((significand_size (fmode) + 1)
5082 < GET_MODE_PRECISION (GET_MODE (from)))
5084 rtx temp1;
5085 rtx neglabel = gen_label_rtx ();
5087 /* Don't use TARGET if it isn't a register, is a hard register,
5088 or is the wrong mode. */
5089 if (!REG_P (target)
5090 || REGNO (target) < FIRST_PSEUDO_REGISTER
5091 || GET_MODE (target) != fmode)
5092 target = gen_reg_rtx (fmode);
5094 imode = GET_MODE (from);
5095 do_pending_stack_adjust ();
5097 /* Test whether the sign bit is set. */
5098 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5099 0, neglabel);
5101 /* The sign bit is not set. Convert as signed. */
5102 expand_float (target, from, 0);
5103 emit_jump_insn (gen_jump (label));
5104 emit_barrier ();
5106 /* The sign bit is set.
5107 Convert to a usable (positive signed) value by shifting right
5108 one bit, while remembering if a nonzero bit was shifted
5109 out; i.e., compute (from & 1) | (from >> 1). */
5111 emit_label (neglabel);
5112 temp = expand_binop (imode, and_optab, from, const1_rtx,
5113 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5114 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5115 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5116 OPTAB_LIB_WIDEN);
5117 expand_float (target, temp, 0);
5119 /* Multiply by 2 to undo the shift above. */
5120 temp = expand_binop (fmode, add_optab, target, target,
5121 target, 0, OPTAB_LIB_WIDEN);
5122 if (temp != target)
5123 emit_move_insn (target, temp);
5125 do_pending_stack_adjust ();
5126 emit_label (label);
5127 goto done;
5131 /* If we are about to do some arithmetic to correct for an
5132 unsigned operand, do it in a pseudo-register. */
5134 if (GET_MODE (to) != fmode
5135 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5136 target = gen_reg_rtx (fmode);
5138 /* Convert as signed integer to floating. */
5139 expand_float (target, from, 0);
5141 /* If FROM is negative (and therefore TO is negative),
5142 correct its value by 2**bitwidth. */
5144 do_pending_stack_adjust ();
5145 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5146 0, label);
5149 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5150 temp = expand_binop (fmode, add_optab, target,
5151 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5152 target, 0, OPTAB_LIB_WIDEN);
5153 if (temp != target)
5154 emit_move_insn (target, temp);
5156 do_pending_stack_adjust ();
5157 emit_label (label);
5158 goto done;
5161 /* No hardware instruction available; call a library routine. */
5163 rtx libfunc;
5164 rtx insns;
5165 rtx value;
5166 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5168 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
5169 from = convert_to_mode (SImode, from, unsignedp);
5171 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5172 gcc_assert (libfunc);
5174 start_sequence ();
5176 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5177 GET_MODE (to), 1, from,
5178 GET_MODE (from));
5179 insns = get_insns ();
5180 end_sequence ();
5182 emit_libcall_block (insns, target, value,
5183 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5184 GET_MODE (to), from));
5187 done:
5189 /* Copy result to requested destination
5190 if we have been computing in a temp location. */
5192 if (target != to)
5194 if (GET_MODE (target) == GET_MODE (to))
5195 emit_move_insn (to, target);
5196 else
5197 convert_move (to, target, 0);
5201 /* Generate code to convert FROM to fixed point and store in TO. FROM
5202 must be floating point. */
5204 void
5205 expand_fix (rtx to, rtx from, int unsignedp)
5207 enum insn_code icode;
5208 rtx target = to;
5209 enum machine_mode fmode, imode;
5210 int must_trunc = 0;
5212 /* We first try to find a pair of modes, one real and one integer, at
5213 least as wide as FROM and TO, respectively, in which we can open-code
5214 this conversion. If the integer mode is wider than the mode of TO,
5215 we can do the conversion either signed or unsigned. */
5217 for (fmode = GET_MODE (from); fmode != VOIDmode;
5218 fmode = GET_MODE_WIDER_MODE (fmode))
5219 for (imode = GET_MODE (to); imode != VOIDmode;
5220 imode = GET_MODE_WIDER_MODE (imode))
5222 int doing_unsigned = unsignedp;
5224 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5225 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5226 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5228 if (icode != CODE_FOR_nothing)
5230 rtx last = get_last_insn ();
5231 if (fmode != GET_MODE (from))
5232 from = convert_to_mode (fmode, from, 0);
5234 if (must_trunc)
5236 rtx temp = gen_reg_rtx (GET_MODE (from));
5237 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5238 temp, 0);
5241 if (imode != GET_MODE (to))
5242 target = gen_reg_rtx (imode);
5244 if (maybe_emit_unop_insn (icode, target, from,
5245 doing_unsigned ? UNSIGNED_FIX : FIX))
5247 if (target != to)
5248 convert_move (to, target, unsignedp);
5249 return;
5251 delete_insns_since (last);
5255 /* For an unsigned conversion, there is one more way to do it.
5256 If we have a signed conversion, we generate code that compares
5257 the real value to the largest representable positive number. If if
5258 is smaller, the conversion is done normally. Otherwise, subtract
5259 one plus the highest signed number, convert, and add it back.
5261 We only need to check all real modes, since we know we didn't find
5262 anything with a wider integer mode.
5264 This code used to extend FP value into mode wider than the destination.
5265 This is needed for decimal float modes which cannot accurately
5266 represent one plus the highest signed number of the same size, but
5267 not for binary modes. Consider, for instance conversion from SFmode
5268 into DImode.
5270 The hot path through the code is dealing with inputs smaller than 2^63
5271 and doing just the conversion, so there is no bits to lose.
5273 In the other path we know the value is positive in the range 2^63..2^64-1
5274 inclusive. (as for other input overflow happens and result is undefined)
5275 So we know that the most important bit set in mantissa corresponds to
5276 2^63. The subtraction of 2^63 should not generate any rounding as it
5277 simply clears out that bit. The rest is trivial. */
5279 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5280 for (fmode = GET_MODE (from); fmode != VOIDmode;
5281 fmode = GET_MODE_WIDER_MODE (fmode))
5282 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5283 && (!DECIMAL_FLOAT_MODE_P (fmode)
5284 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5286 int bitsize;
5287 REAL_VALUE_TYPE offset;
5288 rtx limit, lab1, lab2, insn;
5290 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5291 real_2expN (&offset, bitsize - 1, fmode);
5292 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5293 lab1 = gen_label_rtx ();
5294 lab2 = gen_label_rtx ();
5296 if (fmode != GET_MODE (from))
5297 from = convert_to_mode (fmode, from, 0);
5299 /* See if we need to do the subtraction. */
5300 do_pending_stack_adjust ();
5301 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5302 0, lab1);
5304 /* If not, do the signed "fix" and branch around fixup code. */
5305 expand_fix (to, from, 0);
5306 emit_jump_insn (gen_jump (lab2));
5307 emit_barrier ();
5309 /* Otherwise, subtract 2**(N-1), convert to signed number,
5310 then add 2**(N-1). Do the addition using XOR since this
5311 will often generate better code. */
5312 emit_label (lab1);
5313 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5314 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5315 expand_fix (to, target, 0);
5316 target = expand_binop (GET_MODE (to), xor_optab, to,
5317 gen_int_mode
5318 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5319 GET_MODE (to)),
5320 to, 1, OPTAB_LIB_WIDEN);
5322 if (target != to)
5323 emit_move_insn (to, target);
5325 emit_label (lab2);
5327 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5329 /* Make a place for a REG_NOTE and add it. */
5330 insn = emit_move_insn (to, to);
5331 set_dst_reg_note (insn, REG_EQUAL,
5332 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5333 copy_rtx (from)),
5334 to);
5337 return;
5340 /* We can't do it with an insn, so use a library call. But first ensure
5341 that the mode of TO is at least as wide as SImode, since those are the
5342 only library calls we know about. */
5344 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
5346 target = gen_reg_rtx (SImode);
5348 expand_fix (target, from, unsignedp);
5350 else
5352 rtx insns;
5353 rtx value;
5354 rtx libfunc;
5356 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5357 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5358 gcc_assert (libfunc);
5360 start_sequence ();
5362 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5363 GET_MODE (to), 1, from,
5364 GET_MODE (from));
5365 insns = get_insns ();
5366 end_sequence ();
5368 emit_libcall_block (insns, target, value,
5369 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5370 GET_MODE (to), from));
5373 if (target != to)
5375 if (GET_MODE (to) == GET_MODE (target))
5376 emit_move_insn (to, target);
5377 else
5378 convert_move (to, target, 0);
5382 /* Generate code to convert FROM or TO a fixed-point.
5383 If UINTP is true, either TO or FROM is an unsigned integer.
5384 If SATP is true, we need to saturate the result. */
5386 void
5387 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5389 enum machine_mode to_mode = GET_MODE (to);
5390 enum machine_mode from_mode = GET_MODE (from);
5391 convert_optab tab;
5392 enum rtx_code this_code;
5393 enum insn_code code;
5394 rtx insns, value;
5395 rtx libfunc;
5397 if (to_mode == from_mode)
5399 emit_move_insn (to, from);
5400 return;
5403 if (uintp)
5405 tab = satp ? satfractuns_optab : fractuns_optab;
5406 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5408 else
5410 tab = satp ? satfract_optab : fract_optab;
5411 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5413 code = convert_optab_handler (tab, to_mode, from_mode);
5414 if (code != CODE_FOR_nothing)
5416 emit_unop_insn (code, to, from, this_code);
5417 return;
5420 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5421 gcc_assert (libfunc);
5423 start_sequence ();
5424 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5425 1, from, from_mode);
5426 insns = get_insns ();
5427 end_sequence ();
5429 emit_libcall_block (insns, to, value,
5430 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5433 /* Generate code to convert FROM to fixed point and store in TO. FROM
5434 must be floating point, TO must be signed. Use the conversion optab
5435 TAB to do the conversion. */
5437 bool
5438 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5440 enum insn_code icode;
5441 rtx target = to;
5442 enum machine_mode fmode, imode;
5444 /* We first try to find a pair of modes, one real and one integer, at
5445 least as wide as FROM and TO, respectively, in which we can open-code
5446 this conversion. If the integer mode is wider than the mode of TO,
5447 we can do the conversion either signed or unsigned. */
5449 for (fmode = GET_MODE (from); fmode != VOIDmode;
5450 fmode = GET_MODE_WIDER_MODE (fmode))
5451 for (imode = GET_MODE (to); imode != VOIDmode;
5452 imode = GET_MODE_WIDER_MODE (imode))
5454 icode = convert_optab_handler (tab, imode, fmode);
5455 if (icode != CODE_FOR_nothing)
5457 rtx last = get_last_insn ();
5458 if (fmode != GET_MODE (from))
5459 from = convert_to_mode (fmode, from, 0);
5461 if (imode != GET_MODE (to))
5462 target = gen_reg_rtx (imode);
5464 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5466 delete_insns_since (last);
5467 continue;
5469 if (target != to)
5470 convert_move (to, target, 0);
5471 return true;
5475 return false;
5478 /* Report whether we have an instruction to perform the operation
5479 specified by CODE on operands of mode MODE. */
5481 have_insn_for (enum rtx_code code, enum machine_mode mode)
5483 return (code_to_optab (code)
5484 && (optab_handler (code_to_optab (code), mode)
5485 != CODE_FOR_nothing));
5488 /* Initialize the libfunc fields of an entire group of entries in some
5489 optab. Each entry is set equal to a string consisting of a leading
5490 pair of underscores followed by a generic operation name followed by
5491 a mode name (downshifted to lowercase) followed by a single character
5492 representing the number of operands for the given operation (which is
5493 usually one of the characters '2', '3', or '4').
5495 OPTABLE is the table in which libfunc fields are to be initialized.
5496 OPNAME is the generic (string) name of the operation.
5497 SUFFIX is the character which specifies the number of operands for
5498 the given generic operation.
5499 MODE is the mode to generate for.
5502 static void
5503 gen_libfunc (optab optable, const char *opname, int suffix,
5504 enum machine_mode mode)
5506 unsigned opname_len = strlen (opname);
5507 const char *mname = GET_MODE_NAME (mode);
5508 unsigned mname_len = strlen (mname);
5509 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5510 int len = prefix_len + opname_len + mname_len + 1 + 1;
5511 char *libfunc_name = XALLOCAVEC (char, len);
5512 char *p;
5513 const char *q;
5515 p = libfunc_name;
5516 *p++ = '_';
5517 *p++ = '_';
5518 if (targetm.libfunc_gnu_prefix)
5520 *p++ = 'g';
5521 *p++ = 'n';
5522 *p++ = 'u';
5523 *p++ = '_';
5525 for (q = opname; *q; )
5526 *p++ = *q++;
5527 for (q = mname; *q; q++)
5528 *p++ = TOLOWER (*q);
5529 *p++ = suffix;
5530 *p = '\0';
5532 set_optab_libfunc (optable, mode,
5533 ggc_alloc_string (libfunc_name, p - libfunc_name));
5536 /* Like gen_libfunc, but verify that integer operation is involved. */
5538 void
5539 gen_int_libfunc (optab optable, const char *opname, char suffix,
5540 enum machine_mode mode)
5542 int maxsize = 2 * BITS_PER_WORD;
5544 if (GET_MODE_CLASS (mode) != MODE_INT)
5545 return;
5546 if (maxsize < LONG_LONG_TYPE_SIZE)
5547 maxsize = LONG_LONG_TYPE_SIZE;
5548 if (GET_MODE_CLASS (mode) != MODE_INT
5549 || GET_MODE_BITSIZE (mode) < BITS_PER_WORD
5550 || GET_MODE_BITSIZE (mode) > maxsize)
5551 return;
5552 gen_libfunc (optable, opname, suffix, mode);
5555 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5557 void
5558 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5559 enum machine_mode mode)
5561 char *dec_opname;
5563 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5564 gen_libfunc (optable, opname, suffix, mode);
5565 if (DECIMAL_FLOAT_MODE_P (mode))
5567 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5568 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5569 depending on the low level floating format used. */
5570 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5571 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5572 gen_libfunc (optable, dec_opname, suffix, mode);
5576 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5578 void
5579 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5580 enum machine_mode mode)
5582 if (!ALL_FIXED_POINT_MODE_P (mode))
5583 return;
5584 gen_libfunc (optable, opname, suffix, mode);
5587 /* Like gen_libfunc, but verify that signed fixed-point operation is
5588 involved. */
5590 void
5591 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5592 enum machine_mode mode)
5594 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5595 return;
5596 gen_libfunc (optable, opname, suffix, mode);
5599 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5600 involved. */
5602 void
5603 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5604 enum machine_mode mode)
5606 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5607 return;
5608 gen_libfunc (optable, opname, suffix, mode);
5611 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5613 void
5614 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5615 enum machine_mode mode)
5617 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5618 gen_fp_libfunc (optable, name, suffix, mode);
5619 if (INTEGRAL_MODE_P (mode))
5620 gen_int_libfunc (optable, name, suffix, mode);
5623 /* Like gen_libfunc, but verify that FP or INT operation is involved
5624 and add 'v' suffix for integer operation. */
5626 void
5627 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5628 enum machine_mode mode)
5630 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5631 gen_fp_libfunc (optable, name, suffix, mode);
5632 if (GET_MODE_CLASS (mode) == MODE_INT)
5634 int len = strlen (name);
5635 char *v_name = XALLOCAVEC (char, len + 2);
5636 strcpy (v_name, name);
5637 v_name[len] = 'v';
5638 v_name[len + 1] = 0;
5639 gen_int_libfunc (optable, v_name, suffix, mode);
5643 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5644 involved. */
5646 void
5647 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5648 enum machine_mode mode)
5650 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5651 gen_fp_libfunc (optable, name, suffix, mode);
5652 if (INTEGRAL_MODE_P (mode))
5653 gen_int_libfunc (optable, name, suffix, mode);
5654 if (ALL_FIXED_POINT_MODE_P (mode))
5655 gen_fixed_libfunc (optable, name, suffix, mode);
5658 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5659 involved. */
5661 void
5662 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5663 enum machine_mode mode)
5665 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5666 gen_fp_libfunc (optable, name, suffix, mode);
5667 if (INTEGRAL_MODE_P (mode))
5668 gen_int_libfunc (optable, name, suffix, mode);
5669 if (SIGNED_FIXED_POINT_MODE_P (mode))
5670 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5673 /* Like gen_libfunc, but verify that INT or FIXED operation is
5674 involved. */
5676 void
5677 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5678 enum machine_mode mode)
5680 if (INTEGRAL_MODE_P (mode))
5681 gen_int_libfunc (optable, name, suffix, mode);
5682 if (ALL_FIXED_POINT_MODE_P (mode))
5683 gen_fixed_libfunc (optable, name, suffix, mode);
5686 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5687 involved. */
5689 void
5690 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5691 enum machine_mode mode)
5693 if (INTEGRAL_MODE_P (mode))
5694 gen_int_libfunc (optable, name, suffix, mode);
5695 if (SIGNED_FIXED_POINT_MODE_P (mode))
5696 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5699 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5700 involved. */
5702 void
5703 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5704 enum machine_mode mode)
5706 if (INTEGRAL_MODE_P (mode))
5707 gen_int_libfunc (optable, name, suffix, mode);
5708 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5709 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5712 /* Initialize the libfunc fields of an entire group of entries of an
5713 inter-mode-class conversion optab. The string formation rules are
5714 similar to the ones for init_libfuncs, above, but instead of having
5715 a mode name and an operand count these functions have two mode names
5716 and no operand count. */
5718 void
5719 gen_interclass_conv_libfunc (convert_optab tab,
5720 const char *opname,
5721 enum machine_mode tmode,
5722 enum machine_mode fmode)
5724 size_t opname_len = strlen (opname);
5725 size_t mname_len = 0;
5727 const char *fname, *tname;
5728 const char *q;
5729 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5730 char *libfunc_name, *suffix;
5731 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5732 char *p;
5734 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5735 depends on which underlying decimal floating point format is used. */
5736 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5738 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5740 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5741 nondec_name[0] = '_';
5742 nondec_name[1] = '_';
5743 if (targetm.libfunc_gnu_prefix)
5745 nondec_name[2] = 'g';
5746 nondec_name[3] = 'n';
5747 nondec_name[4] = 'u';
5748 nondec_name[5] = '_';
5751 memcpy (&nondec_name[prefix_len], opname, opname_len);
5752 nondec_suffix = nondec_name + opname_len + prefix_len;
5754 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5755 dec_name[0] = '_';
5756 dec_name[1] = '_';
5757 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5758 memcpy (&dec_name[2+dec_len], opname, opname_len);
5759 dec_suffix = dec_name + dec_len + opname_len + 2;
5761 fname = GET_MODE_NAME (fmode);
5762 tname = GET_MODE_NAME (tmode);
5764 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5766 libfunc_name = dec_name;
5767 suffix = dec_suffix;
5769 else
5771 libfunc_name = nondec_name;
5772 suffix = nondec_suffix;
5775 p = suffix;
5776 for (q = fname; *q; p++, q++)
5777 *p = TOLOWER (*q);
5778 for (q = tname; *q; p++, q++)
5779 *p = TOLOWER (*q);
5781 *p = '\0';
5783 set_conv_libfunc (tab, tmode, fmode,
5784 ggc_alloc_string (libfunc_name, p - libfunc_name));
5787 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5788 int->fp conversion. */
5790 void
5791 gen_int_to_fp_conv_libfunc (convert_optab tab,
5792 const char *opname,
5793 enum machine_mode tmode,
5794 enum machine_mode fmode)
5796 if (GET_MODE_CLASS (fmode) != MODE_INT)
5797 return;
5798 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5799 return;
5800 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5803 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5804 naming scheme. */
5806 void
5807 gen_ufloat_conv_libfunc (convert_optab tab,
5808 const char *opname ATTRIBUTE_UNUSED,
5809 enum machine_mode tmode,
5810 enum machine_mode fmode)
5812 if (DECIMAL_FLOAT_MODE_P (tmode))
5813 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5814 else
5815 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5818 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5819 fp->int conversion. */
5821 void
5822 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5823 const char *opname,
5824 enum machine_mode tmode,
5825 enum machine_mode fmode)
5827 if (GET_MODE_CLASS (fmode) != MODE_INT)
5828 return;
5829 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5830 return;
5831 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5834 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5835 fp->int conversion with no decimal floating point involved. */
5837 void
5838 gen_fp_to_int_conv_libfunc (convert_optab tab,
5839 const char *opname,
5840 enum machine_mode tmode,
5841 enum machine_mode fmode)
5843 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5844 return;
5845 if (GET_MODE_CLASS (tmode) != MODE_INT)
5846 return;
5847 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5850 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5851 The string formation rules are
5852 similar to the ones for init_libfunc, above. */
5854 void
5855 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5856 enum machine_mode tmode, enum machine_mode fmode)
5858 size_t opname_len = strlen (opname);
5859 size_t mname_len = 0;
5861 const char *fname, *tname;
5862 const char *q;
5863 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5864 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5865 char *libfunc_name, *suffix;
5866 char *p;
5868 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5869 depends on which underlying decimal floating point format is used. */
5870 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5872 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5874 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5875 nondec_name[0] = '_';
5876 nondec_name[1] = '_';
5877 if (targetm.libfunc_gnu_prefix)
5879 nondec_name[2] = 'g';
5880 nondec_name[3] = 'n';
5881 nondec_name[4] = 'u';
5882 nondec_name[5] = '_';
5884 memcpy (&nondec_name[prefix_len], opname, opname_len);
5885 nondec_suffix = nondec_name + opname_len + prefix_len;
5887 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5888 dec_name[0] = '_';
5889 dec_name[1] = '_';
5890 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5891 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5892 dec_suffix = dec_name + dec_len + opname_len + 2;
5894 fname = GET_MODE_NAME (fmode);
5895 tname = GET_MODE_NAME (tmode);
5897 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5899 libfunc_name = dec_name;
5900 suffix = dec_suffix;
5902 else
5904 libfunc_name = nondec_name;
5905 suffix = nondec_suffix;
5908 p = suffix;
5909 for (q = fname; *q; p++, q++)
5910 *p = TOLOWER (*q);
5911 for (q = tname; *q; p++, q++)
5912 *p = TOLOWER (*q);
5914 *p++ = '2';
5915 *p = '\0';
5917 set_conv_libfunc (tab, tmode, fmode,
5918 ggc_alloc_string (libfunc_name, p - libfunc_name));
5921 /* Pick proper libcall for trunc_optab. We need to chose if we do
5922 truncation or extension and interclass or intraclass. */
5924 void
5925 gen_trunc_conv_libfunc (convert_optab tab,
5926 const char *opname,
5927 enum machine_mode tmode,
5928 enum machine_mode fmode)
5930 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5931 return;
5932 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5933 return;
5934 if (tmode == fmode)
5935 return;
5937 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5938 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5939 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5941 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5942 return;
5944 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5945 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5946 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5947 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5950 /* Pick proper libcall for extend_optab. We need to chose if we do
5951 truncation or extension and interclass or intraclass. */
5953 void
5954 gen_extend_conv_libfunc (convert_optab tab,
5955 const char *opname ATTRIBUTE_UNUSED,
5956 enum machine_mode tmode,
5957 enum machine_mode fmode)
5959 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5960 return;
5961 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5962 return;
5963 if (tmode == fmode)
5964 return;
5966 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5967 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5968 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5970 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5971 return;
5973 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5974 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5975 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5976 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5979 /* Pick proper libcall for fract_optab. We need to chose if we do
5980 interclass or intraclass. */
5982 void
5983 gen_fract_conv_libfunc (convert_optab tab,
5984 const char *opname,
5985 enum machine_mode tmode,
5986 enum machine_mode fmode)
5988 if (tmode == fmode)
5989 return;
5990 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5991 return;
5993 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5994 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5995 else
5996 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5999 /* Pick proper libcall for fractuns_optab. */
6001 void
6002 gen_fractuns_conv_libfunc (convert_optab tab,
6003 const char *opname,
6004 enum machine_mode tmode,
6005 enum machine_mode fmode)
6007 if (tmode == fmode)
6008 return;
6009 /* One mode must be a fixed-point mode, and the other must be an integer
6010 mode. */
6011 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
6012 || (ALL_FIXED_POINT_MODE_P (fmode)
6013 && GET_MODE_CLASS (tmode) == MODE_INT)))
6014 return;
6016 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6019 /* Pick proper libcall for satfract_optab. We need to chose if we do
6020 interclass or intraclass. */
6022 void
6023 gen_satfract_conv_libfunc (convert_optab tab,
6024 const char *opname,
6025 enum machine_mode tmode,
6026 enum machine_mode fmode)
6028 if (tmode == fmode)
6029 return;
6030 /* TMODE must be a fixed-point mode. */
6031 if (!ALL_FIXED_POINT_MODE_P (tmode))
6032 return;
6034 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6035 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6036 else
6037 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6040 /* Pick proper libcall for satfractuns_optab. */
6042 void
6043 gen_satfractuns_conv_libfunc (convert_optab tab,
6044 const char *opname,
6045 enum machine_mode tmode,
6046 enum machine_mode fmode)
6048 if (tmode == fmode)
6049 return;
6050 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
6051 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
6052 return;
6054 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6057 /* A table of previously-created libfuncs, hashed by name. */
6058 static GTY ((param_is (union tree_node))) htab_t libfunc_decls;
6060 /* Hashtable callbacks for libfunc_decls. */
6062 static hashval_t
6063 libfunc_decl_hash (const void *entry)
6065 return IDENTIFIER_HASH_VALUE (DECL_NAME ((const_tree) entry));
6068 static int
6069 libfunc_decl_eq (const void *entry1, const void *entry2)
6071 return DECL_NAME ((const_tree) entry1) == (const_tree) entry2;
6074 /* Build a decl for a libfunc named NAME. */
6076 tree
6077 build_libfunc_function (const char *name)
6079 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6080 get_identifier (name),
6081 build_function_type (integer_type_node, NULL_TREE));
6082 /* ??? We don't have any type information except for this is
6083 a function. Pretend this is "int foo()". */
6084 DECL_ARTIFICIAL (decl) = 1;
6085 DECL_EXTERNAL (decl) = 1;
6086 TREE_PUBLIC (decl) = 1;
6087 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6089 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6090 are the flags assigned by targetm.encode_section_info. */
6091 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6093 return decl;
6097 init_one_libfunc (const char *name)
6099 tree id, decl;
6100 void **slot;
6101 hashval_t hash;
6103 if (libfunc_decls == NULL)
6104 libfunc_decls = htab_create_ggc (37, libfunc_decl_hash,
6105 libfunc_decl_eq, NULL);
6107 /* See if we have already created a libfunc decl for this function. */
6108 id = get_identifier (name);
6109 hash = IDENTIFIER_HASH_VALUE (id);
6110 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, INSERT);
6111 decl = (tree) *slot;
6112 if (decl == NULL)
6114 /* Create a new decl, so that it can be passed to
6115 targetm.encode_section_info. */
6116 decl = build_libfunc_function (name);
6117 *slot = decl;
6119 return XEXP (DECL_RTL (decl), 0);
6122 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6125 set_user_assembler_libfunc (const char *name, const char *asmspec)
6127 tree id, decl;
6128 void **slot;
6129 hashval_t hash;
6131 id = get_identifier (name);
6132 hash = IDENTIFIER_HASH_VALUE (id);
6133 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, NO_INSERT);
6134 gcc_assert (slot);
6135 decl = (tree) *slot;
6136 set_user_assembler_name (decl, asmspec);
6137 return XEXP (DECL_RTL (decl), 0);
6140 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6141 MODE to NAME, which should be either 0 or a string constant. */
6142 void
6143 set_optab_libfunc (optab op, enum machine_mode mode, const char *name)
6145 rtx val;
6146 struct libfunc_entry e;
6147 struct libfunc_entry **slot;
6149 e.op = op;
6150 e.mode1 = mode;
6151 e.mode2 = VOIDmode;
6153 if (name)
6154 val = init_one_libfunc (name);
6155 else
6156 val = 0;
6157 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6158 if (*slot == NULL)
6159 *slot = ggc_alloc_libfunc_entry ();
6160 (*slot)->op = op;
6161 (*slot)->mode1 = mode;
6162 (*slot)->mode2 = VOIDmode;
6163 (*slot)->libfunc = val;
6166 /* Call this to reset the function entry for one conversion optab
6167 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6168 either 0 or a string constant. */
6169 void
6170 set_conv_libfunc (convert_optab optab, enum machine_mode tmode,
6171 enum machine_mode fmode, const char *name)
6173 rtx val;
6174 struct libfunc_entry e;
6175 struct libfunc_entry **slot;
6177 e.op = optab;
6178 e.mode1 = tmode;
6179 e.mode2 = fmode;
6181 if (name)
6182 val = init_one_libfunc (name);
6183 else
6184 val = 0;
6185 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6186 if (*slot == NULL)
6187 *slot = ggc_alloc_libfunc_entry ();
6188 (*slot)->op = optab;
6189 (*slot)->mode1 = tmode;
6190 (*slot)->mode2 = fmode;
6191 (*slot)->libfunc = val;
6194 /* Call this to initialize the contents of the optabs
6195 appropriately for the current target machine. */
6197 void
6198 init_optabs (void)
6200 if (libfunc_hash)
6201 htab_empty (libfunc_hash);
6202 else
6203 libfunc_hash = htab_create_ggc (10, hash_libfunc, eq_libfunc, NULL);
6205 /* Fill in the optabs with the insns we support. */
6206 init_all_optabs (this_fn_optabs);
6208 /* The ffs function operates on `int'. Fall back on it if we do not
6209 have a libgcc2 function for that width. */
6210 if (INT_TYPE_SIZE < BITS_PER_WORD)
6211 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6212 "ffs");
6214 /* Explicitly initialize the bswap libfuncs since we need them to be
6215 valid for things other than word_mode. */
6216 if (targetm.libfunc_gnu_prefix)
6218 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6219 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6221 else
6223 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6224 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6227 /* Use cabs for double complex abs, since systems generally have cabs.
6228 Don't define any libcall for float complex, so that cabs will be used. */
6229 if (complex_double_type_node)
6230 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node),
6231 "cabs");
6233 abort_libfunc = init_one_libfunc ("abort");
6234 memcpy_libfunc = init_one_libfunc ("memcpy");
6235 memmove_libfunc = init_one_libfunc ("memmove");
6236 memcmp_libfunc = init_one_libfunc ("memcmp");
6237 memset_libfunc = init_one_libfunc ("memset");
6238 setbits_libfunc = init_one_libfunc ("__setbits");
6240 #ifndef DONT_USE_BUILTIN_SETJMP
6241 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6242 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6243 #else
6244 setjmp_libfunc = init_one_libfunc ("setjmp");
6245 longjmp_libfunc = init_one_libfunc ("longjmp");
6246 #endif
6247 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6248 unwind_sjlj_unregister_libfunc
6249 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6251 /* For function entry/exit instrumentation. */
6252 profile_function_entry_libfunc
6253 = init_one_libfunc ("__cyg_profile_func_enter");
6254 profile_function_exit_libfunc
6255 = init_one_libfunc ("__cyg_profile_func_exit");
6257 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6259 /* Allow the target to add more libcalls or rename some, etc. */
6260 targetm.init_libfuncs ();
6263 /* Use the current target and options to initialize
6264 TREE_OPTIMIZATION_OPTABS (OPTNODE). */
6266 void
6267 init_tree_optimization_optabs (tree optnode)
6269 /* Quick exit if we have already computed optabs for this target. */
6270 if (TREE_OPTIMIZATION_BASE_OPTABS (optnode) == this_target_optabs)
6271 return;
6273 /* Forget any previous information and set up for the current target. */
6274 TREE_OPTIMIZATION_BASE_OPTABS (optnode) = this_target_optabs;
6275 struct target_optabs *tmp_optabs = (struct target_optabs *)
6276 TREE_OPTIMIZATION_OPTABS (optnode);
6277 if (tmp_optabs)
6278 memset (tmp_optabs, 0, sizeof (struct target_optabs));
6279 else
6280 tmp_optabs = (struct target_optabs *)
6281 ggc_alloc_atomic (sizeof (struct target_optabs));
6283 /* Generate a new set of optabs into tmp_optabs. */
6284 init_all_optabs (tmp_optabs);
6286 /* If the optabs changed, record it. */
6287 if (memcmp (tmp_optabs, this_target_optabs, sizeof (struct target_optabs)))
6288 TREE_OPTIMIZATION_OPTABS (optnode) = tmp_optabs;
6289 else
6291 TREE_OPTIMIZATION_OPTABS (optnode) = NULL;
6292 ggc_free (tmp_optabs);
6296 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6297 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6299 static void
6300 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6302 enum machine_mode mode;
6303 char buf[64];
6304 size_t len = strlen (base);
6305 int i;
6307 gcc_assert (max <= 8);
6308 gcc_assert (len + 3 < sizeof (buf));
6310 memcpy (buf, base, len);
6311 buf[len] = '_';
6312 buf[len + 1] = '0';
6313 buf[len + 2] = '\0';
6315 mode = QImode;
6316 for (i = 1; i <= max; i *= 2)
6318 buf[len + 1] = '0' + i;
6319 set_optab_libfunc (tab, mode, buf);
6320 mode = GET_MODE_2XWIDER_MODE (mode);
6324 void
6325 init_sync_libfuncs (int max)
6327 if (!flag_sync_libcalls)
6328 return;
6330 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6331 "__sync_val_compare_and_swap", max);
6332 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6333 "__sync_lock_test_and_set", max);
6335 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6336 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6337 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6338 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6339 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6340 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6342 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6343 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6344 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6345 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6346 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6347 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6350 /* Print information about the current contents of the optabs on
6351 STDERR. */
6353 DEBUG_FUNCTION void
6354 debug_optab_libfuncs (void)
6356 int i, j, k;
6358 /* Dump the arithmetic optabs. */
6359 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
6360 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6362 rtx l = optab_libfunc ((optab) i, (enum machine_mode) j);
6363 if (l)
6365 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6366 fprintf (stderr, "%s\t%s:\t%s\n",
6367 GET_RTX_NAME (optab_to_code ((optab) i)),
6368 GET_MODE_NAME (j),
6369 XSTR (l, 0));
6373 /* Dump the conversion optabs. */
6374 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
6375 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6376 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6378 rtx l = convert_optab_libfunc ((optab) i, (enum machine_mode) j,
6379 (enum machine_mode) k);
6380 if (l)
6382 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6383 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6384 GET_RTX_NAME (optab_to_code ((optab) i)),
6385 GET_MODE_NAME (j),
6386 GET_MODE_NAME (k),
6387 XSTR (l, 0));
6393 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6394 CODE. Return 0 on failure. */
6397 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6399 enum machine_mode mode = GET_MODE (op1);
6400 enum insn_code icode;
6401 rtx insn;
6402 rtx trap_rtx;
6404 if (mode == VOIDmode)
6405 return 0;
6407 icode = optab_handler (ctrap_optab, mode);
6408 if (icode == CODE_FOR_nothing)
6409 return 0;
6411 /* Some targets only accept a zero trap code. */
6412 if (!insn_operand_matches (icode, 3, tcode))
6413 return 0;
6415 do_pending_stack_adjust ();
6416 start_sequence ();
6417 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6418 &trap_rtx, &mode);
6419 if (!trap_rtx)
6420 insn = NULL_RTX;
6421 else
6422 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6423 tcode);
6425 /* If that failed, then give up. */
6426 if (insn == 0)
6428 end_sequence ();
6429 return 0;
6432 emit_insn (insn);
6433 insn = get_insns ();
6434 end_sequence ();
6435 return insn;
6438 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6439 or unsigned operation code. */
6441 static enum rtx_code
6442 get_rtx_code (enum tree_code tcode, bool unsignedp)
6444 enum rtx_code code;
6445 switch (tcode)
6447 case EQ_EXPR:
6448 code = EQ;
6449 break;
6450 case NE_EXPR:
6451 code = NE;
6452 break;
6453 case LT_EXPR:
6454 code = unsignedp ? LTU : LT;
6455 break;
6456 case LE_EXPR:
6457 code = unsignedp ? LEU : LE;
6458 break;
6459 case GT_EXPR:
6460 code = unsignedp ? GTU : GT;
6461 break;
6462 case GE_EXPR:
6463 code = unsignedp ? GEU : GE;
6464 break;
6466 case UNORDERED_EXPR:
6467 code = UNORDERED;
6468 break;
6469 case ORDERED_EXPR:
6470 code = ORDERED;
6471 break;
6472 case UNLT_EXPR:
6473 code = UNLT;
6474 break;
6475 case UNLE_EXPR:
6476 code = UNLE;
6477 break;
6478 case UNGT_EXPR:
6479 code = UNGT;
6480 break;
6481 case UNGE_EXPR:
6482 code = UNGE;
6483 break;
6484 case UNEQ_EXPR:
6485 code = UNEQ;
6486 break;
6487 case LTGT_EXPR:
6488 code = LTGT;
6489 break;
6491 default:
6492 gcc_unreachable ();
6494 return code;
6497 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6498 unsigned operators. Do not generate compare instruction. */
6500 static rtx
6501 vector_compare_rtx (enum tree_code tcode, tree t_op0, tree t_op1,
6502 bool unsignedp, enum insn_code icode)
6504 struct expand_operand ops[2];
6505 rtx rtx_op0, rtx_op1;
6506 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
6508 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
6510 /* Expand operands. */
6511 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6512 EXPAND_STACK_PARM);
6513 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6514 EXPAND_STACK_PARM);
6516 create_input_operand (&ops[0], rtx_op0, GET_MODE (rtx_op0));
6517 create_input_operand (&ops[1], rtx_op1, GET_MODE (rtx_op1));
6518 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6519 gcc_unreachable ();
6520 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6523 /* Return true if VEC_PERM_EXPR can be expanded using SIMD extensions
6524 of the CPU. SEL may be NULL, which stands for an unknown constant. */
6526 bool
6527 can_vec_perm_p (enum machine_mode mode, bool variable,
6528 const unsigned char *sel)
6530 enum machine_mode qimode;
6532 /* If the target doesn't implement a vector mode for the vector type,
6533 then no operations are supported. */
6534 if (!VECTOR_MODE_P (mode))
6535 return false;
6537 if (!variable)
6539 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6540 && (sel == NULL
6541 || targetm.vectorize.vec_perm_const_ok == NULL
6542 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6543 return true;
6546 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6547 return true;
6549 /* We allow fallback to a QI vector mode, and adjust the mask. */
6550 if (GET_MODE_INNER (mode) == QImode)
6551 return false;
6552 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6553 if (!VECTOR_MODE_P (qimode))
6554 return false;
6556 /* ??? For completeness, we ought to check the QImode version of
6557 vec_perm_const_optab. But all users of this implicit lowering
6558 feature implement the variable vec_perm_optab. */
6559 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6560 return false;
6562 /* In order to support the lowering of variable permutations,
6563 we need to support shifts and adds. */
6564 if (variable)
6566 if (GET_MODE_UNIT_SIZE (mode) > 2
6567 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6568 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6569 return false;
6570 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6571 return false;
6574 return true;
6577 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
6579 static rtx
6580 expand_vec_perm_1 (enum insn_code icode, rtx target,
6581 rtx v0, rtx v1, rtx sel)
6583 enum machine_mode tmode = GET_MODE (target);
6584 enum machine_mode smode = GET_MODE (sel);
6585 struct expand_operand ops[4];
6587 create_output_operand (&ops[0], target, tmode);
6588 create_input_operand (&ops[3], sel, smode);
6590 /* Make an effort to preserve v0 == v1. The target expander is able to
6591 rely on this to determine if we're permuting a single input operand. */
6592 if (rtx_equal_p (v0, v1))
6594 if (!insn_operand_matches (icode, 1, v0))
6595 v0 = force_reg (tmode, v0);
6596 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6597 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6599 create_fixed_operand (&ops[1], v0);
6600 create_fixed_operand (&ops[2], v0);
6602 else
6604 create_input_operand (&ops[1], v0, tmode);
6605 create_input_operand (&ops[2], v1, tmode);
6608 if (maybe_expand_insn (icode, 4, ops))
6609 return ops[0].value;
6610 return NULL_RTX;
6613 /* Generate instructions for vec_perm optab given its mode
6614 and three operands. */
6617 expand_vec_perm (enum machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6619 enum insn_code icode;
6620 enum machine_mode qimode;
6621 unsigned int i, w, e, u;
6622 rtx tmp, sel_qi = NULL;
6623 rtvec vec;
6625 if (!target || GET_MODE (target) != mode)
6626 target = gen_reg_rtx (mode);
6628 w = GET_MODE_SIZE (mode);
6629 e = GET_MODE_NUNITS (mode);
6630 u = GET_MODE_UNIT_SIZE (mode);
6632 /* Set QIMODE to a different vector mode with byte elements.
6633 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6634 qimode = VOIDmode;
6635 if (GET_MODE_INNER (mode) != QImode)
6637 qimode = mode_for_vector (QImode, w);
6638 if (!VECTOR_MODE_P (qimode))
6639 qimode = VOIDmode;
6642 /* If the input is a constant, expand it specially. */
6643 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6644 if (GET_CODE (sel) == CONST_VECTOR)
6646 icode = direct_optab_handler (vec_perm_const_optab, mode);
6647 if (icode != CODE_FOR_nothing)
6649 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6650 if (tmp)
6651 return tmp;
6654 /* Fall back to a constant byte-based permutation. */
6655 if (qimode != VOIDmode)
6657 vec = rtvec_alloc (w);
6658 for (i = 0; i < e; ++i)
6660 unsigned int j, this_e;
6662 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6663 this_e &= 2 * e - 1;
6664 this_e *= u;
6666 for (j = 0; j < u; ++j)
6667 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
6669 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
6671 icode = direct_optab_handler (vec_perm_const_optab, qimode);
6672 if (icode != CODE_FOR_nothing)
6674 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6675 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6676 gen_lowpart (qimode, v1), sel_qi);
6677 if (tmp)
6678 return gen_lowpart (mode, tmp);
6683 /* Otherwise expand as a fully variable permuation. */
6684 icode = direct_optab_handler (vec_perm_optab, mode);
6685 if (icode != CODE_FOR_nothing)
6687 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6688 if (tmp)
6689 return tmp;
6692 /* As a special case to aid several targets, lower the element-based
6693 permutation to a byte-based permutation and try again. */
6694 if (qimode == VOIDmode)
6695 return NULL_RTX;
6696 icode = direct_optab_handler (vec_perm_optab, qimode);
6697 if (icode == CODE_FOR_nothing)
6698 return NULL_RTX;
6700 if (sel_qi == NULL)
6702 /* Multiply each element by its byte size. */
6703 enum machine_mode selmode = GET_MODE (sel);
6704 if (u == 2)
6705 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6706 sel, 0, OPTAB_DIRECT);
6707 else
6708 sel = expand_simple_binop (selmode, ASHIFT, sel,
6709 GEN_INT (exact_log2 (u)),
6710 sel, 0, OPTAB_DIRECT);
6711 gcc_assert (sel != NULL);
6713 /* Broadcast the low byte each element into each of its bytes. */
6714 vec = rtvec_alloc (w);
6715 for (i = 0; i < w; ++i)
6717 int this_e = i / u * u;
6718 if (BYTES_BIG_ENDIAN)
6719 this_e += u - 1;
6720 RTVEC_ELT (vec, i) = GEN_INT (this_e);
6722 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6723 sel = gen_lowpart (qimode, sel);
6724 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
6725 gcc_assert (sel != NULL);
6727 /* Add the byte offset to each byte element. */
6728 /* Note that the definition of the indicies here is memory ordering,
6729 so there should be no difference between big and little endian. */
6730 vec = rtvec_alloc (w);
6731 for (i = 0; i < w; ++i)
6732 RTVEC_ELT (vec, i) = GEN_INT (i % u);
6733 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6734 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6735 sel, 0, OPTAB_DIRECT);
6736 gcc_assert (sel_qi != NULL);
6739 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6740 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6741 gen_lowpart (qimode, v1), sel_qi);
6742 if (tmp)
6743 tmp = gen_lowpart (mode, tmp);
6744 return tmp;
6747 /* Return insn code for a conditional operator with a comparison in
6748 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
6750 static inline enum insn_code
6751 get_vcond_icode (enum machine_mode vmode, enum machine_mode cmode, bool uns)
6753 enum insn_code icode = CODE_FOR_nothing;
6754 if (uns)
6755 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
6756 else
6757 icode = convert_optab_handler (vcond_optab, vmode, cmode);
6758 return icode;
6761 /* Return TRUE iff, appropriate vector insns are available
6762 for vector cond expr with vector type VALUE_TYPE and a comparison
6763 with operand vector types in CMP_OP_TYPE. */
6765 bool
6766 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
6768 enum machine_mode value_mode = TYPE_MODE (value_type);
6769 enum machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
6770 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
6771 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
6772 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
6773 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
6774 return false;
6775 return true;
6778 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6779 three operands. */
6782 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6783 rtx target)
6785 struct expand_operand ops[6];
6786 enum insn_code icode;
6787 rtx comparison, rtx_op1, rtx_op2;
6788 enum machine_mode mode = TYPE_MODE (vec_cond_type);
6789 enum machine_mode cmp_op_mode;
6790 bool unsignedp;
6791 tree op0a, op0b;
6792 enum tree_code tcode;
6794 if (COMPARISON_CLASS_P (op0))
6796 op0a = TREE_OPERAND (op0, 0);
6797 op0b = TREE_OPERAND (op0, 1);
6798 tcode = TREE_CODE (op0);
6800 else
6802 /* Fake op0 < 0. */
6803 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (op0)));
6804 op0a = op0;
6805 op0b = build_zero_cst (TREE_TYPE (op0));
6806 tcode = LT_EXPR;
6808 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6809 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
6812 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
6813 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
6815 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
6816 if (icode == CODE_FOR_nothing)
6817 return 0;
6819 comparison = vector_compare_rtx (tcode, op0a, op0b, unsignedp, icode);
6820 rtx_op1 = expand_normal (op1);
6821 rtx_op2 = expand_normal (op2);
6823 create_output_operand (&ops[0], target, mode);
6824 create_input_operand (&ops[1], rtx_op1, mode);
6825 create_input_operand (&ops[2], rtx_op2, mode);
6826 create_fixed_operand (&ops[3], comparison);
6827 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6828 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6829 expand_insn (icode, 6, ops);
6830 return ops[0].value;
6833 /* Return non-zero if a highpart multiply is supported of can be synthisized.
6834 For the benefit of expand_mult_highpart, the return value is 1 for direct,
6835 2 for even/odd widening, and 3 for hi/lo widening. */
6838 can_mult_highpart_p (enum machine_mode mode, bool uns_p)
6840 optab op;
6841 unsigned char *sel;
6842 unsigned i, nunits;
6844 op = uns_p ? umul_highpart_optab : smul_highpart_optab;
6845 if (optab_handler (op, mode) != CODE_FOR_nothing)
6846 return 1;
6848 /* If the mode is an integral vector, synth from widening operations. */
6849 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
6850 return 0;
6852 nunits = GET_MODE_NUNITS (mode);
6853 sel = XALLOCAVEC (unsigned char, nunits);
6855 op = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6856 if (optab_handler (op, mode) != CODE_FOR_nothing)
6858 op = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6859 if (optab_handler (op, mode) != CODE_FOR_nothing)
6861 for (i = 0; i < nunits; ++i)
6862 sel[i] = !BYTES_BIG_ENDIAN + (i & ~1) + ((i & 1) ? nunits : 0);
6863 if (can_vec_perm_p (mode, false, sel))
6864 return 2;
6868 op = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6869 if (optab_handler (op, mode) != CODE_FOR_nothing)
6871 op = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6872 if (optab_handler (op, mode) != CODE_FOR_nothing)
6874 for (i = 0; i < nunits; ++i)
6875 sel[i] = 2 * i + (BYTES_BIG_ENDIAN ? 0 : 1);
6876 if (can_vec_perm_p (mode, false, sel))
6877 return 3;
6881 return 0;
6884 /* Expand a highpart multiply. */
6887 expand_mult_highpart (enum machine_mode mode, rtx op0, rtx op1,
6888 rtx target, bool uns_p)
6890 struct expand_operand eops[3];
6891 enum insn_code icode;
6892 int method, i, nunits;
6893 enum machine_mode wmode;
6894 rtx m1, m2, perm;
6895 optab tab1, tab2;
6896 rtvec v;
6898 method = can_mult_highpart_p (mode, uns_p);
6899 switch (method)
6901 case 0:
6902 return NULL_RTX;
6903 case 1:
6904 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6905 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6906 OPTAB_LIB_WIDEN);
6907 case 2:
6908 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6909 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6910 break;
6911 case 3:
6912 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6913 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6914 if (BYTES_BIG_ENDIAN)
6916 optab t = tab1;
6917 tab1 = tab2;
6918 tab2 = t;
6920 break;
6921 default:
6922 gcc_unreachable ();
6925 icode = optab_handler (tab1, mode);
6926 nunits = GET_MODE_NUNITS (mode);
6927 wmode = insn_data[icode].operand[0].mode;
6928 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
6929 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
6931 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6932 create_input_operand (&eops[1], op0, mode);
6933 create_input_operand (&eops[2], op1, mode);
6934 expand_insn (icode, 3, eops);
6935 m1 = gen_lowpart (mode, eops[0].value);
6937 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6938 create_input_operand (&eops[1], op0, mode);
6939 create_input_operand (&eops[2], op1, mode);
6940 expand_insn (optab_handler (tab2, mode), 3, eops);
6941 m2 = gen_lowpart (mode, eops[0].value);
6943 v = rtvec_alloc (nunits);
6944 if (method == 2)
6946 for (i = 0; i < nunits; ++i)
6947 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
6948 + ((i & 1) ? nunits : 0));
6950 else
6952 for (i = 0; i < nunits; ++i)
6953 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
6955 perm = gen_rtx_CONST_VECTOR (mode, v);
6957 return expand_vec_perm (mode, m1, m2, perm, target);
6960 /* Return true if target supports vector masked load/store for mode. */
6961 bool
6962 can_vec_mask_load_store_p (enum machine_mode mode, bool is_load)
6964 optab op = is_load ? maskload_optab : maskstore_optab;
6965 enum machine_mode vmode;
6966 unsigned int vector_sizes;
6968 /* If mode is vector mode, check it directly. */
6969 if (VECTOR_MODE_P (mode))
6970 return optab_handler (op, mode) != CODE_FOR_nothing;
6972 /* Otherwise, return true if there is some vector mode with
6973 the mask load/store supported. */
6975 /* See if there is any chance the mask load or store might be
6976 vectorized. If not, punt. */
6977 vmode = targetm.vectorize.preferred_simd_mode (mode);
6978 if (!VECTOR_MODE_P (vmode))
6979 return false;
6981 if (optab_handler (op, vmode) != CODE_FOR_nothing)
6982 return true;
6984 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
6985 while (vector_sizes != 0)
6987 unsigned int cur = 1 << floor_log2 (vector_sizes);
6988 vector_sizes &= ~cur;
6989 if (cur <= GET_MODE_SIZE (mode))
6990 continue;
6991 vmode = mode_for_vector (mode, cur / GET_MODE_SIZE (mode));
6992 if (VECTOR_MODE_P (vmode)
6993 && optab_handler (op, vmode) != CODE_FOR_nothing)
6994 return true;
6996 return false;
6999 /* Return true if there is a compare_and_swap pattern. */
7001 bool
7002 can_compare_and_swap_p (enum machine_mode mode, bool allow_libcall)
7004 enum insn_code icode;
7006 /* Check for __atomic_compare_and_swap. */
7007 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7008 if (icode != CODE_FOR_nothing)
7009 return true;
7011 /* Check for __sync_compare_and_swap. */
7012 icode = optab_handler (sync_compare_and_swap_optab, mode);
7013 if (icode != CODE_FOR_nothing)
7014 return true;
7015 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
7016 return true;
7018 /* No inline compare and swap. */
7019 return false;
7022 /* Return true if an atomic exchange can be performed. */
7024 bool
7025 can_atomic_exchange_p (enum machine_mode mode, bool allow_libcall)
7027 enum insn_code icode;
7029 /* Check for __atomic_exchange. */
7030 icode = direct_optab_handler (atomic_exchange_optab, mode);
7031 if (icode != CODE_FOR_nothing)
7032 return true;
7034 /* Don't check __sync_test_and_set, as on some platforms that
7035 has reduced functionality. Targets that really do support
7036 a proper exchange should simply be updated to the __atomics. */
7038 return can_compare_and_swap_p (mode, allow_libcall);
7042 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
7043 pattern. */
7045 static void
7046 find_cc_set (rtx x, const_rtx pat, void *data)
7048 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
7049 && GET_CODE (pat) == SET)
7051 rtx *p_cc_reg = (rtx *) data;
7052 gcc_assert (!*p_cc_reg);
7053 *p_cc_reg = x;
7057 /* This is a helper function for the other atomic operations. This function
7058 emits a loop that contains SEQ that iterates until a compare-and-swap
7059 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7060 a set of instructions that takes a value from OLD_REG as an input and
7061 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7062 set to the current contents of MEM. After SEQ, a compare-and-swap will
7063 attempt to update MEM with NEW_REG. The function returns true when the
7064 loop was generated successfully. */
7066 static bool
7067 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7069 enum machine_mode mode = GET_MODE (mem);
7070 rtx label, cmp_reg, success, oldval;
7072 /* The loop we want to generate looks like
7074 cmp_reg = mem;
7075 label:
7076 old_reg = cmp_reg;
7077 seq;
7078 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
7079 if (success)
7080 goto label;
7082 Note that we only do the plain load from memory once. Subsequent
7083 iterations use the value loaded by the compare-and-swap pattern. */
7085 label = gen_label_rtx ();
7086 cmp_reg = gen_reg_rtx (mode);
7088 emit_move_insn (cmp_reg, mem);
7089 emit_label (label);
7090 emit_move_insn (old_reg, cmp_reg);
7091 if (seq)
7092 emit_insn (seq);
7094 success = NULL_RTX;
7095 oldval = cmp_reg;
7096 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
7097 new_reg, false, MEMMODEL_SEQ_CST,
7098 MEMMODEL_RELAXED))
7099 return false;
7101 if (oldval != cmp_reg)
7102 emit_move_insn (cmp_reg, oldval);
7104 /* Mark this jump predicted not taken. */
7105 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
7106 GET_MODE (success), 1, label, 0);
7107 return true;
7111 /* This function tries to emit an atomic_exchange intruction. VAL is written
7112 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
7113 using TARGET if possible. */
7115 static rtx
7116 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7118 enum machine_mode mode = GET_MODE (mem);
7119 enum insn_code icode;
7121 /* If the target supports the exchange directly, great. */
7122 icode = direct_optab_handler (atomic_exchange_optab, mode);
7123 if (icode != CODE_FOR_nothing)
7125 struct expand_operand ops[4];
7127 create_output_operand (&ops[0], target, mode);
7128 create_fixed_operand (&ops[1], mem);
7129 create_input_operand (&ops[2], val, mode);
7130 create_integer_operand (&ops[3], model);
7131 if (maybe_expand_insn (icode, 4, ops))
7132 return ops[0].value;
7135 return NULL_RTX;
7138 /* This function tries to implement an atomic exchange operation using
7139 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7140 The previous contents of *MEM are returned, using TARGET if possible.
7141 Since this instructionn is an acquire barrier only, stronger memory
7142 models may require additional barriers to be emitted. */
7144 static rtx
7145 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7146 enum memmodel model)
7148 enum machine_mode mode = GET_MODE (mem);
7149 enum insn_code icode;
7150 rtx last_insn = get_last_insn ();
7152 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7154 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7155 exists, and the memory model is stronger than acquire, add a release
7156 barrier before the instruction. */
7158 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST
7159 || (model & MEMMODEL_MASK) == MEMMODEL_RELEASE
7160 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
7161 expand_mem_thread_fence (model);
7163 if (icode != CODE_FOR_nothing)
7165 struct expand_operand ops[3];
7166 create_output_operand (&ops[0], target, mode);
7167 create_fixed_operand (&ops[1], mem);
7168 create_input_operand (&ops[2], val, mode);
7169 if (maybe_expand_insn (icode, 3, ops))
7170 return ops[0].value;
7173 /* If an external test-and-set libcall is provided, use that instead of
7174 any external compare-and-swap that we might get from the compare-and-
7175 swap-loop expansion later. */
7176 if (!can_compare_and_swap_p (mode, false))
7178 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7179 if (libfunc != NULL)
7181 rtx addr;
7183 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7184 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7185 mode, 2, addr, ptr_mode,
7186 val, mode);
7190 /* If the test_and_set can't be emitted, eliminate any barrier that might
7191 have been emitted. */
7192 delete_insns_since (last_insn);
7193 return NULL_RTX;
7196 /* This function tries to implement an atomic exchange operation using a
7197 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7198 *MEM are returned, using TARGET if possible. No memory model is required
7199 since a compare_and_swap loop is seq-cst. */
7201 static rtx
7202 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7204 enum machine_mode mode = GET_MODE (mem);
7206 if (can_compare_and_swap_p (mode, true))
7208 if (!target || !register_operand (target, mode))
7209 target = gen_reg_rtx (mode);
7210 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7211 return target;
7214 return NULL_RTX;
7217 /* This function tries to implement an atomic test-and-set operation
7218 using the atomic_test_and_set instruction pattern. A boolean value
7219 is returned from the operation, using TARGET if possible. */
7221 #ifndef HAVE_atomic_test_and_set
7222 #define HAVE_atomic_test_and_set 0
7223 #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
7224 #endif
7226 static rtx
7227 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7229 enum machine_mode pat_bool_mode;
7230 struct expand_operand ops[3];
7232 if (!HAVE_atomic_test_and_set)
7233 return NULL_RTX;
7235 /* While we always get QImode from __atomic_test_and_set, we get
7236 other memory modes from __sync_lock_test_and_set. Note that we
7237 use no endian adjustment here. This matches the 4.6 behavior
7238 in the Sparc backend. */
7239 gcc_checking_assert
7240 (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
7241 if (GET_MODE (mem) != QImode)
7242 mem = adjust_address_nv (mem, QImode, 0);
7244 pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
7245 create_output_operand (&ops[0], target, pat_bool_mode);
7246 create_fixed_operand (&ops[1], mem);
7247 create_integer_operand (&ops[2], model);
7249 if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
7250 return ops[0].value;
7251 return NULL_RTX;
7254 /* This function expands the legacy _sync_lock test_and_set operation which is
7255 generally an atomic exchange. Some limited targets only allow the
7256 constant 1 to be stored. This is an ACQUIRE operation.
7258 TARGET is an optional place to stick the return value.
7259 MEM is where VAL is stored. */
7262 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7264 rtx ret;
7266 /* Try an atomic_exchange first. */
7267 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_ACQUIRE);
7268 if (ret)
7269 return ret;
7271 ret = maybe_emit_sync_lock_test_and_set (target, mem, val, MEMMODEL_ACQUIRE);
7272 if (ret)
7273 return ret;
7275 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7276 if (ret)
7277 return ret;
7279 /* If there are no other options, try atomic_test_and_set if the value
7280 being stored is 1. */
7281 if (val == const1_rtx)
7282 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_ACQUIRE);
7284 return ret;
7287 /* This function expands the atomic test_and_set operation:
7288 atomically store a boolean TRUE into MEM and return the previous value.
7290 MEMMODEL is the memory model variant to use.
7291 TARGET is an optional place to stick the return value. */
7294 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7296 enum machine_mode mode = GET_MODE (mem);
7297 rtx ret, trueval, subtarget;
7299 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7300 if (ret)
7301 return ret;
7303 /* Be binary compatible with non-default settings of trueval, and different
7304 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7305 another only has atomic-exchange. */
7306 if (targetm.atomic_test_and_set_trueval == 1)
7308 trueval = const1_rtx;
7309 subtarget = target ? target : gen_reg_rtx (mode);
7311 else
7313 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7314 subtarget = gen_reg_rtx (mode);
7317 /* Try the atomic-exchange optab... */
7318 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7320 /* ... then an atomic-compare-and-swap loop ... */
7321 if (!ret)
7322 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7324 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7325 if (!ret)
7326 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7328 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7329 things with the value 1. Thus we try again without trueval. */
7330 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7331 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7333 /* Failing all else, assume a single threaded environment and simply
7334 perform the operation. */
7335 if (!ret)
7337 emit_move_insn (subtarget, mem);
7338 emit_move_insn (mem, trueval);
7339 ret = subtarget;
7342 /* Recall that have to return a boolean value; rectify if trueval
7343 is not exactly one. */
7344 if (targetm.atomic_test_and_set_trueval != 1)
7345 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7347 return ret;
7350 /* This function expands the atomic exchange operation:
7351 atomically store VAL in MEM and return the previous value in MEM.
7353 MEMMODEL is the memory model variant to use.
7354 TARGET is an optional place to stick the return value. */
7357 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7359 rtx ret;
7361 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7363 /* Next try a compare-and-swap loop for the exchange. */
7364 if (!ret)
7365 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7367 return ret;
7370 /* This function expands the atomic compare exchange operation:
7372 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7373 *PTARGET_OVAL is an optional place to store the old value from memory.
7374 Both target parameters may be NULL to indicate that we do not care about
7375 that return value. Both target parameters are updated on success to
7376 the actual location of the corresponding result.
7378 MEMMODEL is the memory model variant to use.
7380 The return value of the function is true for success. */
7382 bool
7383 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7384 rtx mem, rtx expected, rtx desired,
7385 bool is_weak, enum memmodel succ_model,
7386 enum memmodel fail_model)
7388 enum machine_mode mode = GET_MODE (mem);
7389 struct expand_operand ops[8];
7390 enum insn_code icode;
7391 rtx target_oval, target_bool = NULL_RTX;
7392 rtx libfunc;
7394 /* Load expected into a register for the compare and swap. */
7395 if (MEM_P (expected))
7396 expected = copy_to_reg (expected);
7398 /* Make sure we always have some place to put the return oldval.
7399 Further, make sure that place is distinct from the input expected,
7400 just in case we need that path down below. */
7401 if (ptarget_oval == NULL
7402 || (target_oval = *ptarget_oval) == NULL
7403 || reg_overlap_mentioned_p (expected, target_oval))
7404 target_oval = gen_reg_rtx (mode);
7406 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7407 if (icode != CODE_FOR_nothing)
7409 enum machine_mode bool_mode = insn_data[icode].operand[0].mode;
7411 /* Make sure we always have a place for the bool operand. */
7412 if (ptarget_bool == NULL
7413 || (target_bool = *ptarget_bool) == NULL
7414 || GET_MODE (target_bool) != bool_mode)
7415 target_bool = gen_reg_rtx (bool_mode);
7417 /* Emit the compare_and_swap. */
7418 create_output_operand (&ops[0], target_bool, bool_mode);
7419 create_output_operand (&ops[1], target_oval, mode);
7420 create_fixed_operand (&ops[2], mem);
7421 create_input_operand (&ops[3], expected, mode);
7422 create_input_operand (&ops[4], desired, mode);
7423 create_integer_operand (&ops[5], is_weak);
7424 create_integer_operand (&ops[6], succ_model);
7425 create_integer_operand (&ops[7], fail_model);
7426 if (maybe_expand_insn (icode, 8, ops))
7428 /* Return success/failure. */
7429 target_bool = ops[0].value;
7430 target_oval = ops[1].value;
7431 goto success;
7435 /* Otherwise fall back to the original __sync_val_compare_and_swap
7436 which is always seq-cst. */
7437 icode = optab_handler (sync_compare_and_swap_optab, mode);
7438 if (icode != CODE_FOR_nothing)
7440 rtx cc_reg;
7442 create_output_operand (&ops[0], target_oval, mode);
7443 create_fixed_operand (&ops[1], mem);
7444 create_input_operand (&ops[2], expected, mode);
7445 create_input_operand (&ops[3], desired, mode);
7446 if (!maybe_expand_insn (icode, 4, ops))
7447 return false;
7449 target_oval = ops[0].value;
7451 /* If the caller isn't interested in the boolean return value,
7452 skip the computation of it. */
7453 if (ptarget_bool == NULL)
7454 goto success;
7456 /* Otherwise, work out if the compare-and-swap succeeded. */
7457 cc_reg = NULL_RTX;
7458 if (have_insn_for (COMPARE, CCmode))
7459 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7460 if (cc_reg)
7462 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7463 const0_rtx, VOIDmode, 0, 1);
7464 goto success;
7466 goto success_bool_from_val;
7469 /* Also check for library support for __sync_val_compare_and_swap. */
7470 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7471 if (libfunc != NULL)
7473 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7474 target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7475 mode, 3, addr, ptr_mode,
7476 expected, mode, desired, mode);
7478 /* Compute the boolean return value only if requested. */
7479 if (ptarget_bool)
7480 goto success_bool_from_val;
7481 else
7482 goto success;
7485 /* Failure. */
7486 return false;
7488 success_bool_from_val:
7489 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7490 expected, VOIDmode, 1, 1);
7491 success:
7492 /* Make sure that the oval output winds up where the caller asked. */
7493 if (ptarget_oval)
7494 *ptarget_oval = target_oval;
7495 if (ptarget_bool)
7496 *ptarget_bool = target_bool;
7497 return true;
7500 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7502 static void
7503 expand_asm_memory_barrier (void)
7505 rtx asm_op, clob;
7507 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7508 rtvec_alloc (0), rtvec_alloc (0),
7509 rtvec_alloc (0), UNKNOWN_LOCATION);
7510 MEM_VOLATILE_P (asm_op) = 1;
7512 clob = gen_rtx_SCRATCH (VOIDmode);
7513 clob = gen_rtx_MEM (BLKmode, clob);
7514 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7516 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7519 /* This routine will either emit the mem_thread_fence pattern or issue a
7520 sync_synchronize to generate a fence for memory model MEMMODEL. */
7522 #ifndef HAVE_mem_thread_fence
7523 # define HAVE_mem_thread_fence 0
7524 # define gen_mem_thread_fence(x) (gcc_unreachable (), NULL_RTX)
7525 #endif
7526 #ifndef HAVE_memory_barrier
7527 # define HAVE_memory_barrier 0
7528 # define gen_memory_barrier() (gcc_unreachable (), NULL_RTX)
7529 #endif
7531 void
7532 expand_mem_thread_fence (enum memmodel model)
7534 if (HAVE_mem_thread_fence)
7535 emit_insn (gen_mem_thread_fence (GEN_INT (model)));
7536 else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
7538 if (HAVE_memory_barrier)
7539 emit_insn (gen_memory_barrier ());
7540 else if (synchronize_libfunc != NULL_RTX)
7541 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7542 else
7543 expand_asm_memory_barrier ();
7547 /* This routine will either emit the mem_signal_fence pattern or issue a
7548 sync_synchronize to generate a fence for memory model MEMMODEL. */
7550 #ifndef HAVE_mem_signal_fence
7551 # define HAVE_mem_signal_fence 0
7552 # define gen_mem_signal_fence(x) (gcc_unreachable (), NULL_RTX)
7553 #endif
7555 void
7556 expand_mem_signal_fence (enum memmodel model)
7558 if (HAVE_mem_signal_fence)
7559 emit_insn (gen_mem_signal_fence (GEN_INT (model)));
7560 else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
7562 /* By default targets are coherent between a thread and the signal
7563 handler running on the same thread. Thus this really becomes a
7564 compiler barrier, in that stores must not be sunk past
7565 (or raised above) a given point. */
7566 expand_asm_memory_barrier ();
7570 /* This function expands the atomic load operation:
7571 return the atomically loaded value in MEM.
7573 MEMMODEL is the memory model variant to use.
7574 TARGET is an option place to stick the return value. */
7577 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7579 enum machine_mode mode = GET_MODE (mem);
7580 enum insn_code icode;
7582 /* If the target supports the load directly, great. */
7583 icode = direct_optab_handler (atomic_load_optab, mode);
7584 if (icode != CODE_FOR_nothing)
7586 struct expand_operand ops[3];
7588 create_output_operand (&ops[0], target, mode);
7589 create_fixed_operand (&ops[1], mem);
7590 create_integer_operand (&ops[2], model);
7591 if (maybe_expand_insn (icode, 3, ops))
7592 return ops[0].value;
7595 /* If the size of the object is greater than word size on this target,
7596 then we assume that a load will not be atomic. */
7597 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7599 /* Issue val = compare_and_swap (mem, 0, 0).
7600 This may cause the occasional harmless store of 0 when the value is
7601 already 0, but it seems to be OK according to the standards guys. */
7602 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7603 const0_rtx, false, model, model))
7604 return target;
7605 else
7606 /* Otherwise there is no atomic load, leave the library call. */
7607 return NULL_RTX;
7610 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7611 if (!target || target == const0_rtx)
7612 target = gen_reg_rtx (mode);
7614 /* For SEQ_CST, emit a barrier before the load. */
7615 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7616 expand_mem_thread_fence (model);
7618 emit_move_insn (target, mem);
7620 /* Emit the appropriate barrier after the load. */
7621 expand_mem_thread_fence (model);
7623 return target;
7626 /* This function expands the atomic store operation:
7627 Atomically store VAL in MEM.
7628 MEMMODEL is the memory model variant to use.
7629 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7630 function returns const0_rtx if a pattern was emitted. */
7633 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7635 enum machine_mode mode = GET_MODE (mem);
7636 enum insn_code icode;
7637 struct expand_operand ops[3];
7639 /* If the target supports the store directly, great. */
7640 icode = direct_optab_handler (atomic_store_optab, mode);
7641 if (icode != CODE_FOR_nothing)
7643 create_fixed_operand (&ops[0], mem);
7644 create_input_operand (&ops[1], val, mode);
7645 create_integer_operand (&ops[2], model);
7646 if (maybe_expand_insn (icode, 3, ops))
7647 return const0_rtx;
7650 /* If using __sync_lock_release is a viable alternative, try it. */
7651 if (use_release)
7653 icode = direct_optab_handler (sync_lock_release_optab, mode);
7654 if (icode != CODE_FOR_nothing)
7656 create_fixed_operand (&ops[0], mem);
7657 create_input_operand (&ops[1], const0_rtx, mode);
7658 if (maybe_expand_insn (icode, 2, ops))
7660 /* lock_release is only a release barrier. */
7661 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7662 expand_mem_thread_fence (model);
7663 return const0_rtx;
7668 /* If the size of the object is greater than word size on this target,
7669 a default store will not be atomic, Try a mem_exchange and throw away
7670 the result. If that doesn't work, don't do anything. */
7671 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7673 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7674 if (!target)
7675 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7676 if (target)
7677 return const0_rtx;
7678 else
7679 return NULL_RTX;
7682 /* Otherwise assume stores are atomic, and emit the proper barriers. */
7683 expand_mem_thread_fence (model);
7685 emit_move_insn (mem, val);
7687 /* For SEQ_CST, also emit a barrier after the store. */
7688 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7689 expand_mem_thread_fence (model);
7691 return const0_rtx;
7695 /* Structure containing the pointers and values required to process the
7696 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7698 struct atomic_op_functions
7700 direct_optab mem_fetch_before;
7701 direct_optab mem_fetch_after;
7702 direct_optab mem_no_result;
7703 optab fetch_before;
7704 optab fetch_after;
7705 direct_optab no_result;
7706 enum rtx_code reverse_code;
7710 /* Fill in structure pointed to by OP with the various optab entries for an
7711 operation of type CODE. */
7713 static void
7714 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7716 gcc_assert (op!= NULL);
7718 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7719 in the source code during compilation, and the optab entries are not
7720 computable until runtime. Fill in the values at runtime. */
7721 switch (code)
7723 case PLUS:
7724 op->mem_fetch_before = atomic_fetch_add_optab;
7725 op->mem_fetch_after = atomic_add_fetch_optab;
7726 op->mem_no_result = atomic_add_optab;
7727 op->fetch_before = sync_old_add_optab;
7728 op->fetch_after = sync_new_add_optab;
7729 op->no_result = sync_add_optab;
7730 op->reverse_code = MINUS;
7731 break;
7732 case MINUS:
7733 op->mem_fetch_before = atomic_fetch_sub_optab;
7734 op->mem_fetch_after = atomic_sub_fetch_optab;
7735 op->mem_no_result = atomic_sub_optab;
7736 op->fetch_before = sync_old_sub_optab;
7737 op->fetch_after = sync_new_sub_optab;
7738 op->no_result = sync_sub_optab;
7739 op->reverse_code = PLUS;
7740 break;
7741 case XOR:
7742 op->mem_fetch_before = atomic_fetch_xor_optab;
7743 op->mem_fetch_after = atomic_xor_fetch_optab;
7744 op->mem_no_result = atomic_xor_optab;
7745 op->fetch_before = sync_old_xor_optab;
7746 op->fetch_after = sync_new_xor_optab;
7747 op->no_result = sync_xor_optab;
7748 op->reverse_code = XOR;
7749 break;
7750 case AND:
7751 op->mem_fetch_before = atomic_fetch_and_optab;
7752 op->mem_fetch_after = atomic_and_fetch_optab;
7753 op->mem_no_result = atomic_and_optab;
7754 op->fetch_before = sync_old_and_optab;
7755 op->fetch_after = sync_new_and_optab;
7756 op->no_result = sync_and_optab;
7757 op->reverse_code = UNKNOWN;
7758 break;
7759 case IOR:
7760 op->mem_fetch_before = atomic_fetch_or_optab;
7761 op->mem_fetch_after = atomic_or_fetch_optab;
7762 op->mem_no_result = atomic_or_optab;
7763 op->fetch_before = sync_old_ior_optab;
7764 op->fetch_after = sync_new_ior_optab;
7765 op->no_result = sync_ior_optab;
7766 op->reverse_code = UNKNOWN;
7767 break;
7768 case NOT:
7769 op->mem_fetch_before = atomic_fetch_nand_optab;
7770 op->mem_fetch_after = atomic_nand_fetch_optab;
7771 op->mem_no_result = atomic_nand_optab;
7772 op->fetch_before = sync_old_nand_optab;
7773 op->fetch_after = sync_new_nand_optab;
7774 op->no_result = sync_nand_optab;
7775 op->reverse_code = UNKNOWN;
7776 break;
7777 default:
7778 gcc_unreachable ();
7782 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7783 using memory order MODEL. If AFTER is true the operation needs to return
7784 the value of *MEM after the operation, otherwise the previous value.
7785 TARGET is an optional place to place the result. The result is unused if
7786 it is const0_rtx.
7787 Return the result if there is a better sequence, otherwise NULL_RTX. */
7789 static rtx
7790 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7791 enum memmodel model, bool after)
7793 /* If the value is prefetched, or not used, it may be possible to replace
7794 the sequence with a native exchange operation. */
7795 if (!after || target == const0_rtx)
7797 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7798 if (code == AND && val == const0_rtx)
7800 if (target == const0_rtx)
7801 target = gen_reg_rtx (GET_MODE (mem));
7802 return maybe_emit_atomic_exchange (target, mem, val, model);
7805 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7806 if (code == IOR && val == constm1_rtx)
7808 if (target == const0_rtx)
7809 target = gen_reg_rtx (GET_MODE (mem));
7810 return maybe_emit_atomic_exchange (target, mem, val, model);
7814 return NULL_RTX;
7817 /* Try to emit an instruction for a specific operation varaition.
7818 OPTAB contains the OP functions.
7819 TARGET is an optional place to return the result. const0_rtx means unused.
7820 MEM is the memory location to operate on.
7821 VAL is the value to use in the operation.
7822 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7823 MODEL is the memory model, if used.
7824 AFTER is true if the returned result is the value after the operation. */
7826 static rtx
7827 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7828 rtx val, bool use_memmodel, enum memmodel model, bool after)
7830 enum machine_mode mode = GET_MODE (mem);
7831 struct expand_operand ops[4];
7832 enum insn_code icode;
7833 int op_counter = 0;
7834 int num_ops;
7836 /* Check to see if there is a result returned. */
7837 if (target == const0_rtx)
7839 if (use_memmodel)
7841 icode = direct_optab_handler (optab->mem_no_result, mode);
7842 create_integer_operand (&ops[2], model);
7843 num_ops = 3;
7845 else
7847 icode = direct_optab_handler (optab->no_result, mode);
7848 num_ops = 2;
7851 /* Otherwise, we need to generate a result. */
7852 else
7854 if (use_memmodel)
7856 icode = direct_optab_handler (after ? optab->mem_fetch_after
7857 : optab->mem_fetch_before, mode);
7858 create_integer_operand (&ops[3], model);
7859 num_ops = 4;
7861 else
7863 icode = optab_handler (after ? optab->fetch_after
7864 : optab->fetch_before, mode);
7865 num_ops = 3;
7867 create_output_operand (&ops[op_counter++], target, mode);
7869 if (icode == CODE_FOR_nothing)
7870 return NULL_RTX;
7872 create_fixed_operand (&ops[op_counter++], mem);
7873 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7874 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7876 if (maybe_expand_insn (icode, num_ops, ops))
7877 return (target == const0_rtx ? const0_rtx : ops[0].value);
7879 return NULL_RTX;
7883 /* This function expands an atomic fetch_OP or OP_fetch operation:
7884 TARGET is an option place to stick the return value. const0_rtx indicates
7885 the result is unused.
7886 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7887 CODE is the operation being performed (OP)
7888 MEMMODEL is the memory model variant to use.
7889 AFTER is true to return the result of the operation (OP_fetch).
7890 AFTER is false to return the value before the operation (fetch_OP).
7892 This function will *only* generate instructions if there is a direct
7893 optab. No compare and swap loops or libcalls will be generated. */
7895 static rtx
7896 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7897 enum rtx_code code, enum memmodel model,
7898 bool after)
7900 enum machine_mode mode = GET_MODE (mem);
7901 struct atomic_op_functions optab;
7902 rtx result;
7903 bool unused_result = (target == const0_rtx);
7905 get_atomic_op_for_code (&optab, code);
7907 /* Check to see if there are any better instructions. */
7908 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7909 if (result)
7910 return result;
7912 /* Check for the case where the result isn't used and try those patterns. */
7913 if (unused_result)
7915 /* Try the memory model variant first. */
7916 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7917 if (result)
7918 return result;
7920 /* Next try the old style withuot a memory model. */
7921 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7922 if (result)
7923 return result;
7925 /* There is no no-result pattern, so try patterns with a result. */
7926 target = NULL_RTX;
7929 /* Try the __atomic version. */
7930 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
7931 if (result)
7932 return result;
7934 /* Try the older __sync version. */
7935 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
7936 if (result)
7937 return result;
7939 /* If the fetch value can be calculated from the other variation of fetch,
7940 try that operation. */
7941 if (after || unused_result || optab.reverse_code != UNKNOWN)
7943 /* Try the __atomic version, then the older __sync version. */
7944 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
7945 if (!result)
7946 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
7948 if (result)
7950 /* If the result isn't used, no need to do compensation code. */
7951 if (unused_result)
7952 return result;
7954 /* Issue compensation code. Fetch_after == fetch_before OP val.
7955 Fetch_before == after REVERSE_OP val. */
7956 if (!after)
7957 code = optab.reverse_code;
7958 if (code == NOT)
7960 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
7961 true, OPTAB_LIB_WIDEN);
7962 result = expand_simple_unop (mode, NOT, result, target, true);
7964 else
7965 result = expand_simple_binop (mode, code, result, val, target,
7966 true, OPTAB_LIB_WIDEN);
7967 return result;
7971 /* No direct opcode can be generated. */
7972 return NULL_RTX;
7977 /* This function expands an atomic fetch_OP or OP_fetch operation:
7978 TARGET is an option place to stick the return value. const0_rtx indicates
7979 the result is unused.
7980 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7981 CODE is the operation being performed (OP)
7982 MEMMODEL is the memory model variant to use.
7983 AFTER is true to return the result of the operation (OP_fetch).
7984 AFTER is false to return the value before the operation (fetch_OP). */
7986 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7987 enum memmodel model, bool after)
7989 enum machine_mode mode = GET_MODE (mem);
7990 rtx result;
7991 bool unused_result = (target == const0_rtx);
7993 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
7994 after);
7996 if (result)
7997 return result;
7999 /* Add/sub can be implemented by doing the reverse operation with -(val). */
8000 if (code == PLUS || code == MINUS)
8002 rtx tmp;
8003 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
8005 start_sequence ();
8006 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
8007 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
8008 model, after);
8009 if (result)
8011 /* PLUS worked so emit the insns and return. */
8012 tmp = get_insns ();
8013 end_sequence ();
8014 emit_insn (tmp);
8015 return result;
8018 /* PLUS did not work, so throw away the negation code and continue. */
8019 end_sequence ();
8022 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
8023 if (!can_compare_and_swap_p (mode, false))
8025 rtx libfunc;
8026 bool fixup = false;
8027 enum rtx_code orig_code = code;
8028 struct atomic_op_functions optab;
8030 get_atomic_op_for_code (&optab, code);
8031 libfunc = optab_libfunc (after ? optab.fetch_after
8032 : optab.fetch_before, mode);
8033 if (libfunc == NULL
8034 && (after || unused_result || optab.reverse_code != UNKNOWN))
8036 fixup = true;
8037 if (!after)
8038 code = optab.reverse_code;
8039 libfunc = optab_libfunc (after ? optab.fetch_before
8040 : optab.fetch_after, mode);
8042 if (libfunc != NULL)
8044 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
8045 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
8046 2, addr, ptr_mode, val, mode);
8048 if (!unused_result && fixup)
8049 result = expand_simple_binop (mode, code, result, val, target,
8050 true, OPTAB_LIB_WIDEN);
8051 return result;
8054 /* We need the original code for any further attempts. */
8055 code = orig_code;
8058 /* If nothing else has succeeded, default to a compare and swap loop. */
8059 if (can_compare_and_swap_p (mode, true))
8061 rtx insn;
8062 rtx t0 = gen_reg_rtx (mode), t1;
8064 start_sequence ();
8066 /* If the result is used, get a register for it. */
8067 if (!unused_result)
8069 if (!target || !register_operand (target, mode))
8070 target = gen_reg_rtx (mode);
8071 /* If fetch_before, copy the value now. */
8072 if (!after)
8073 emit_move_insn (target, t0);
8075 else
8076 target = const0_rtx;
8078 t1 = t0;
8079 if (code == NOT)
8081 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
8082 true, OPTAB_LIB_WIDEN);
8083 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
8085 else
8086 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
8087 OPTAB_LIB_WIDEN);
8089 /* For after, copy the value now. */
8090 if (!unused_result && after)
8091 emit_move_insn (target, t1);
8092 insn = get_insns ();
8093 end_sequence ();
8095 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
8096 return target;
8099 return NULL_RTX;
8102 /* Return true if OPERAND is suitable for operand number OPNO of
8103 instruction ICODE. */
8105 bool
8106 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
8108 return (!insn_data[(int) icode].operand[opno].predicate
8109 || (insn_data[(int) icode].operand[opno].predicate
8110 (operand, insn_data[(int) icode].operand[opno].mode)));
8113 /* TARGET is a target of a multiword operation that we are going to
8114 implement as a series of word-mode operations. Return true if
8115 TARGET is suitable for this purpose. */
8117 bool
8118 valid_multiword_target_p (rtx target)
8120 enum machine_mode mode;
8121 int i;
8123 mode = GET_MODE (target);
8124 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
8125 if (!validate_subreg (word_mode, mode, target, i))
8126 return false;
8127 return true;
8130 /* Like maybe_legitimize_operand, but do not change the code of the
8131 current rtx value. */
8133 static bool
8134 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8135 struct expand_operand *op)
8137 /* See if the operand matches in its current form. */
8138 if (insn_operand_matches (icode, opno, op->value))
8139 return true;
8141 /* If the operand is a memory whose address has no side effects,
8142 try forcing the address into a non-virtual pseudo register.
8143 The check for side effects is important because copy_to_mode_reg
8144 cannot handle things like auto-modified addresses. */
8145 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8147 rtx addr, mem;
8149 mem = op->value;
8150 addr = XEXP (mem, 0);
8151 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8152 && !side_effects_p (addr))
8154 rtx last;
8155 enum machine_mode mode;
8157 last = get_last_insn ();
8158 mode = get_address_mode (mem);
8159 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8160 if (insn_operand_matches (icode, opno, mem))
8162 op->value = mem;
8163 return true;
8165 delete_insns_since (last);
8169 return false;
8172 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8173 on success, storing the new operand value back in OP. */
8175 static bool
8176 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8177 struct expand_operand *op)
8179 enum machine_mode mode, imode;
8180 bool old_volatile_ok, result;
8182 mode = op->mode;
8183 switch (op->type)
8185 case EXPAND_FIXED:
8186 old_volatile_ok = volatile_ok;
8187 volatile_ok = true;
8188 result = maybe_legitimize_operand_same_code (icode, opno, op);
8189 volatile_ok = old_volatile_ok;
8190 return result;
8192 case EXPAND_OUTPUT:
8193 gcc_assert (mode != VOIDmode);
8194 if (op->value
8195 && op->value != const0_rtx
8196 && GET_MODE (op->value) == mode
8197 && maybe_legitimize_operand_same_code (icode, opno, op))
8198 return true;
8200 op->value = gen_reg_rtx (mode);
8201 break;
8203 case EXPAND_INPUT:
8204 input:
8205 gcc_assert (mode != VOIDmode);
8206 gcc_assert (GET_MODE (op->value) == VOIDmode
8207 || GET_MODE (op->value) == mode);
8208 if (maybe_legitimize_operand_same_code (icode, opno, op))
8209 return true;
8211 op->value = copy_to_mode_reg (mode, op->value);
8212 break;
8214 case EXPAND_CONVERT_TO:
8215 gcc_assert (mode != VOIDmode);
8216 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8217 goto input;
8219 case EXPAND_CONVERT_FROM:
8220 if (GET_MODE (op->value) != VOIDmode)
8221 mode = GET_MODE (op->value);
8222 else
8223 /* The caller must tell us what mode this value has. */
8224 gcc_assert (mode != VOIDmode);
8226 imode = insn_data[(int) icode].operand[opno].mode;
8227 if (imode != VOIDmode && imode != mode)
8229 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8230 mode = imode;
8232 goto input;
8234 case EXPAND_ADDRESS:
8235 gcc_assert (mode != VOIDmode);
8236 op->value = convert_memory_address (mode, op->value);
8237 goto input;
8239 case EXPAND_INTEGER:
8240 mode = insn_data[(int) icode].operand[opno].mode;
8241 if (mode != VOIDmode && const_int_operand (op->value, mode))
8242 goto input;
8243 break;
8245 return insn_operand_matches (icode, opno, op->value);
8248 /* Make OP describe an input operand that should have the same value
8249 as VALUE, after any mode conversion that the target might request.
8250 TYPE is the type of VALUE. */
8252 void
8253 create_convert_operand_from_type (struct expand_operand *op,
8254 rtx value, tree type)
8256 create_convert_operand_from (op, value, TYPE_MODE (type),
8257 TYPE_UNSIGNED (type));
8260 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8261 of instruction ICODE. Return true on success, leaving the new operand
8262 values in the OPS themselves. Emit no code on failure. */
8264 bool
8265 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8266 unsigned int nops, struct expand_operand *ops)
8268 rtx last;
8269 unsigned int i;
8271 last = get_last_insn ();
8272 for (i = 0; i < nops; i++)
8273 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8275 delete_insns_since (last);
8276 return false;
8278 return true;
8281 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8282 as its operands. Return the instruction pattern on success,
8283 and emit any necessary set-up code. Return null and emit no
8284 code on failure. */
8287 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8288 struct expand_operand *ops)
8290 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8291 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8292 return NULL_RTX;
8294 switch (nops)
8296 case 1:
8297 return GEN_FCN (icode) (ops[0].value);
8298 case 2:
8299 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8300 case 3:
8301 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8302 case 4:
8303 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8304 ops[3].value);
8305 case 5:
8306 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8307 ops[3].value, ops[4].value);
8308 case 6:
8309 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8310 ops[3].value, ops[4].value, ops[5].value);
8311 case 7:
8312 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8313 ops[3].value, ops[4].value, ops[5].value,
8314 ops[6].value);
8315 case 8:
8316 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8317 ops[3].value, ops[4].value, ops[5].value,
8318 ops[6].value, ops[7].value);
8319 case 9:
8320 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8321 ops[3].value, ops[4].value, ops[5].value,
8322 ops[6].value, ops[7].value, ops[8].value);
8324 gcc_unreachable ();
8327 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8328 as its operands. Return true on success and emit no code on failure. */
8330 bool
8331 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8332 struct expand_operand *ops)
8334 rtx pat = maybe_gen_insn (icode, nops, ops);
8335 if (pat)
8337 emit_insn (pat);
8338 return true;
8340 return false;
8343 /* Like maybe_expand_insn, but for jumps. */
8345 bool
8346 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8347 struct expand_operand *ops)
8349 rtx pat = maybe_gen_insn (icode, nops, ops);
8350 if (pat)
8352 emit_jump_insn (pat);
8353 return true;
8355 return false;
8358 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8359 as its operands. */
8361 void
8362 expand_insn (enum insn_code icode, unsigned int nops,
8363 struct expand_operand *ops)
8365 if (!maybe_expand_insn (icode, nops, ops))
8366 gcc_unreachable ();
8369 /* Like expand_insn, but for jumps. */
8371 void
8372 expand_jump_insn (enum insn_code icode, unsigned int nops,
8373 struct expand_operand *ops)
8375 if (!maybe_expand_jump_insn (icode, nops, ops))
8376 gcc_unreachable ();
8379 /* Reduce conditional compilation elsewhere. */
8380 #ifndef HAVE_insv
8381 #define HAVE_insv 0
8382 #define CODE_FOR_insv CODE_FOR_nothing
8383 #endif
8384 #ifndef HAVE_extv
8385 #define HAVE_extv 0
8386 #define CODE_FOR_extv CODE_FOR_nothing
8387 #endif
8388 #ifndef HAVE_extzv
8389 #define HAVE_extzv 0
8390 #define CODE_FOR_extzv CODE_FOR_nothing
8391 #endif
8393 /* Enumerates the possible types of structure operand to an
8394 extraction_insn. */
8395 enum extraction_type { ET_unaligned_mem, ET_reg };
8397 /* Check whether insv, extv or extzv pattern ICODE can be used for an
8398 insertion or extraction of type TYPE on a structure of mode MODE.
8399 Return true if so and fill in *INSN accordingly. STRUCT_OP is the
8400 operand number of the structure (the first sign_extract or zero_extract
8401 operand) and FIELD_OP is the operand number of the field (the other
8402 side of the set from the sign_extract or zero_extract). */
8404 static bool
8405 get_traditional_extraction_insn (extraction_insn *insn,
8406 enum extraction_type type,
8407 enum machine_mode mode,
8408 enum insn_code icode,
8409 int struct_op, int field_op)
8411 const struct insn_data_d *data = &insn_data[icode];
8413 enum machine_mode struct_mode = data->operand[struct_op].mode;
8414 if (struct_mode == VOIDmode)
8415 struct_mode = word_mode;
8416 if (mode != struct_mode)
8417 return false;
8419 enum machine_mode field_mode = data->operand[field_op].mode;
8420 if (field_mode == VOIDmode)
8421 field_mode = word_mode;
8423 enum machine_mode pos_mode = data->operand[struct_op + 2].mode;
8424 if (pos_mode == VOIDmode)
8425 pos_mode = word_mode;
8427 insn->icode = icode;
8428 insn->field_mode = field_mode;
8429 insn->struct_mode = (type == ET_unaligned_mem ? byte_mode : struct_mode);
8430 insn->pos_mode = pos_mode;
8431 return true;
8434 /* Return true if an optab exists to perform an insertion or extraction
8435 of type TYPE in mode MODE. Describe the instruction in *INSN if so.
8437 REG_OPTAB is the optab to use for register structures and
8438 MISALIGN_OPTAB is the optab to use for misaligned memory structures.
8439 POS_OP is the operand number of the bit position. */
8441 static bool
8442 get_optab_extraction_insn (struct extraction_insn *insn,
8443 enum extraction_type type,
8444 enum machine_mode mode, direct_optab reg_optab,
8445 direct_optab misalign_optab, int pos_op)
8447 direct_optab optab = (type == ET_unaligned_mem ? misalign_optab : reg_optab);
8448 enum insn_code icode = direct_optab_handler (optab, mode);
8449 if (icode == CODE_FOR_nothing)
8450 return false;
8452 const struct insn_data_d *data = &insn_data[icode];
8454 insn->icode = icode;
8455 insn->field_mode = mode;
8456 insn->struct_mode = (type == ET_unaligned_mem ? BLKmode : mode);
8457 insn->pos_mode = data->operand[pos_op].mode;
8458 if (insn->pos_mode == VOIDmode)
8459 insn->pos_mode = word_mode;
8460 return true;
8463 /* Return true if an instruction exists to perform an insertion or
8464 extraction (PATTERN says which) of type TYPE in mode MODE.
8465 Describe the instruction in *INSN if so. */
8467 static bool
8468 get_extraction_insn (extraction_insn *insn,
8469 enum extraction_pattern pattern,
8470 enum extraction_type type,
8471 enum machine_mode mode)
8473 switch (pattern)
8475 case EP_insv:
8476 if (HAVE_insv
8477 && get_traditional_extraction_insn (insn, type, mode,
8478 CODE_FOR_insv, 0, 3))
8479 return true;
8480 return get_optab_extraction_insn (insn, type, mode, insv_optab,
8481 insvmisalign_optab, 2);
8483 case EP_extv:
8484 if (HAVE_extv
8485 && get_traditional_extraction_insn (insn, type, mode,
8486 CODE_FOR_extv, 1, 0))
8487 return true;
8488 return get_optab_extraction_insn (insn, type, mode, extv_optab,
8489 extvmisalign_optab, 3);
8491 case EP_extzv:
8492 if (HAVE_extzv
8493 && get_traditional_extraction_insn (insn, type, mode,
8494 CODE_FOR_extzv, 1, 0))
8495 return true;
8496 return get_optab_extraction_insn (insn, type, mode, extzv_optab,
8497 extzvmisalign_optab, 3);
8499 default:
8500 gcc_unreachable ();
8504 /* Return true if an instruction exists to access a field of mode
8505 FIELDMODE in a structure that has STRUCT_BITS significant bits.
8506 Describe the "best" such instruction in *INSN if so. PATTERN and
8507 TYPE describe the type of insertion or extraction we want to perform.
8509 For an insertion, the number of significant structure bits includes
8510 all bits of the target. For an extraction, it need only include the
8511 most significant bit of the field. Larger widths are acceptable
8512 in both cases. */
8514 static bool
8515 get_best_extraction_insn (extraction_insn *insn,
8516 enum extraction_pattern pattern,
8517 enum extraction_type type,
8518 unsigned HOST_WIDE_INT struct_bits,
8519 enum machine_mode field_mode)
8521 enum machine_mode mode = smallest_mode_for_size (struct_bits, MODE_INT);
8522 while (mode != VOIDmode)
8524 if (get_extraction_insn (insn, pattern, type, mode))
8526 while (mode != VOIDmode
8527 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (field_mode)
8528 && !TRULY_NOOP_TRUNCATION_MODES_P (insn->field_mode,
8529 field_mode))
8531 get_extraction_insn (insn, pattern, type, mode);
8532 mode = GET_MODE_WIDER_MODE (mode);
8534 return true;
8536 mode = GET_MODE_WIDER_MODE (mode);
8538 return false;
8541 /* Return true if an instruction exists to access a field of mode
8542 FIELDMODE in a register structure that has STRUCT_BITS significant bits.
8543 Describe the "best" such instruction in *INSN if so. PATTERN describes
8544 the type of insertion or extraction we want to perform.
8546 For an insertion, the number of significant structure bits includes
8547 all bits of the target. For an extraction, it need only include the
8548 most significant bit of the field. Larger widths are acceptable
8549 in both cases. */
8551 bool
8552 get_best_reg_extraction_insn (extraction_insn *insn,
8553 enum extraction_pattern pattern,
8554 unsigned HOST_WIDE_INT struct_bits,
8555 enum machine_mode field_mode)
8557 return get_best_extraction_insn (insn, pattern, ET_reg, struct_bits,
8558 field_mode);
8561 /* Return true if an instruction exists to access a field of BITSIZE
8562 bits starting BITNUM bits into a memory structure. Describe the
8563 "best" such instruction in *INSN if so. PATTERN describes the type
8564 of insertion or extraction we want to perform and FIELDMODE is the
8565 natural mode of the extracted field.
8567 The instructions considered here only access bytes that overlap
8568 the bitfield; they do not touch any surrounding bytes. */
8570 bool
8571 get_best_mem_extraction_insn (extraction_insn *insn,
8572 enum extraction_pattern pattern,
8573 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitnum,
8574 enum machine_mode field_mode)
8576 unsigned HOST_WIDE_INT struct_bits = (bitnum % BITS_PER_UNIT
8577 + bitsize
8578 + BITS_PER_UNIT - 1);
8579 struct_bits -= struct_bits % BITS_PER_UNIT;
8580 return get_best_extraction_insn (insn, pattern, ET_unaligned_mem,
8581 struct_bits, field_mode);
8584 #include "gt-optabs.h"