2013-09-25 Yvan Roux <yvan.roux@linaro.org>
[official-gcc.git] / gcc / optabs.c
blob3238885c1879d297bd02773b2929943e11fa6716
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2013 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 "tm_p.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "except.h"
36 #include "expr.h"
37 #include "optabs.h"
38 #include "libfuncs.h"
39 #include "recog.h"
40 #include "reload.h"
41 #include "ggc.h"
42 #include "basic-block.h"
43 #include "target.h"
45 struct target_optabs default_target_optabs;
46 struct target_libfuncs default_target_libfuncs;
47 struct target_optabs *this_fn_optabs = &default_target_optabs;
48 #if SWITCHABLE_TARGET
49 struct target_optabs *this_target_optabs = &default_target_optabs;
50 struct target_libfuncs *this_target_libfuncs = &default_target_libfuncs;
51 #endif
53 #define libfunc_hash \
54 (this_target_libfuncs->x_libfunc_hash)
56 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
57 enum machine_mode *);
58 static rtx expand_unop_direct (enum machine_mode, optab, rtx, rtx, int);
59 static void emit_libcall_block_1 (rtx, rtx, rtx, rtx, bool);
61 /* Debug facility for use in GDB. */
62 void debug_optab_libfuncs (void);
64 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
65 #if ENABLE_DECIMAL_BID_FORMAT
66 #define DECIMAL_PREFIX "bid_"
67 #else
68 #define DECIMAL_PREFIX "dpd_"
69 #endif
71 /* Used for libfunc_hash. */
73 static hashval_t
74 hash_libfunc (const void *p)
76 const struct libfunc_entry *const e = (const struct libfunc_entry *) p;
77 return ((e->mode1 + e->mode2 * NUM_MACHINE_MODES) ^ e->op);
80 /* Used for libfunc_hash. */
82 static int
83 eq_libfunc (const void *p, const void *q)
85 const struct libfunc_entry *const e1 = (const struct libfunc_entry *) p;
86 const struct libfunc_entry *const e2 = (const struct libfunc_entry *) q;
87 return e1->op == e2->op && e1->mode1 == e2->mode1 && e1->mode2 == e2->mode2;
90 /* Return libfunc corresponding operation defined by OPTAB converting
91 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
92 if no libfunc is available. */
93 rtx
94 convert_optab_libfunc (convert_optab optab, enum machine_mode mode1,
95 enum machine_mode mode2)
97 struct libfunc_entry e;
98 struct libfunc_entry **slot;
100 /* ??? This ought to be an assert, but not all of the places
101 that we expand optabs know about the optabs that got moved
102 to being direct. */
103 if (!(optab >= FIRST_CONV_OPTAB && optab <= LAST_CONVLIB_OPTAB))
104 return NULL_RTX;
106 e.op = optab;
107 e.mode1 = mode1;
108 e.mode2 = mode2;
109 slot = (struct libfunc_entry **)
110 htab_find_slot (libfunc_hash, &e, NO_INSERT);
111 if (!slot)
113 const struct convert_optab_libcall_d *d
114 = &convlib_def[optab - FIRST_CONV_OPTAB];
116 if (d->libcall_gen == NULL)
117 return NULL;
119 d->libcall_gen (optab, d->libcall_basename, mode1, mode2);
120 slot = (struct libfunc_entry **)
121 htab_find_slot (libfunc_hash, &e, NO_INSERT);
122 if (!slot)
123 return NULL;
125 return (*slot)->libfunc;
128 /* Return libfunc corresponding operation defined by OPTAB in MODE.
129 Trigger lazy initialization if needed, return NULL if no libfunc is
130 available. */
132 optab_libfunc (optab optab, enum machine_mode mode)
134 struct libfunc_entry e;
135 struct libfunc_entry **slot;
137 /* ??? This ought to be an assert, but not all of the places
138 that we expand optabs know about the optabs that got moved
139 to being direct. */
140 if (!(optab >= FIRST_NORM_OPTAB && optab <= LAST_NORMLIB_OPTAB))
141 return NULL_RTX;
143 e.op = optab;
144 e.mode1 = mode;
145 e.mode2 = VOIDmode;
146 slot = (struct libfunc_entry **)
147 htab_find_slot (libfunc_hash, &e, NO_INSERT);
148 if (!slot)
150 const struct optab_libcall_d *d
151 = &normlib_def[optab - FIRST_NORM_OPTAB];
153 if (d->libcall_gen == NULL)
154 return NULL;
156 d->libcall_gen (optab, d->libcall_basename, d->libcall_suffix, mode);
157 slot = (struct libfunc_entry **)
158 htab_find_slot (libfunc_hash, &e, NO_INSERT);
159 if (!slot)
160 return NULL;
162 return (*slot)->libfunc;
166 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
167 the result of operation CODE applied to OP0 (and OP1 if it is a binary
168 operation).
170 If the last insn does not set TARGET, don't do anything, but return 1.
172 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
173 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
174 try again, ensuring that TARGET is not one of the operands. */
176 static int
177 add_equal_note (rtx insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
179 rtx last_insn, set;
180 rtx note;
182 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
184 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
185 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
186 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
187 && GET_RTX_CLASS (code) != RTX_COMPARE
188 && GET_RTX_CLASS (code) != RTX_UNARY)
189 return 1;
191 if (GET_CODE (target) == ZERO_EXTRACT)
192 return 1;
194 for (last_insn = insns;
195 NEXT_INSN (last_insn) != NULL_RTX;
196 last_insn = NEXT_INSN (last_insn))
199 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
200 a value changing in the insn, so the note would be invalid for CSE. */
201 if (reg_overlap_mentioned_p (target, op0)
202 || (op1 && reg_overlap_mentioned_p (target, op1)))
204 if (MEM_P (target)
205 && (rtx_equal_p (target, op0)
206 || (op1 && rtx_equal_p (target, op1))))
208 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
209 over expanding it as temp = MEM op X, MEM = temp. If the target
210 supports MEM = MEM op X instructions, it is sometimes too hard
211 to reconstruct that form later, especially if X is also a memory,
212 and due to multiple occurrences of addresses the address might
213 be forced into register unnecessarily.
214 Note that not emitting the REG_EQUIV note might inhibit
215 CSE in some cases. */
216 set = single_set (last_insn);
217 if (set
218 && GET_CODE (SET_SRC (set)) == code
219 && MEM_P (SET_DEST (set))
220 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
221 || (op1 && rtx_equal_p (SET_DEST (set),
222 XEXP (SET_SRC (set), 1)))))
223 return 1;
225 return 0;
228 set = single_set (last_insn);
229 if (set == NULL_RTX)
230 return 1;
232 if (! rtx_equal_p (SET_DEST (set), target)
233 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
234 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
235 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
236 return 1;
238 if (GET_RTX_CLASS (code) == RTX_UNARY)
239 switch (code)
241 case FFS:
242 case CLZ:
243 case CTZ:
244 case CLRSB:
245 case POPCOUNT:
246 case PARITY:
247 case BSWAP:
248 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
250 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
251 if (GET_MODE_SIZE (GET_MODE (op0))
252 > GET_MODE_SIZE (GET_MODE (target)))
253 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
254 note, GET_MODE (op0));
255 else
256 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
257 note, GET_MODE (op0));
258 break;
260 /* FALLTHRU */
261 default:
262 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
263 break;
265 else
266 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
268 set_unique_reg_note (last_insn, REG_EQUAL, note);
270 return 1;
273 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
274 for a widening operation would be. In most cases this would be OP0, but if
275 that's a constant it'll be VOIDmode, which isn't useful. */
277 static enum machine_mode
278 widened_mode (enum machine_mode to_mode, rtx op0, rtx op1)
280 enum machine_mode m0 = GET_MODE (op0);
281 enum machine_mode m1 = GET_MODE (op1);
282 enum machine_mode result;
284 if (m0 == VOIDmode && m1 == VOIDmode)
285 return to_mode;
286 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
287 result = m1;
288 else
289 result = m0;
291 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
292 return to_mode;
294 return result;
297 /* Find a widening optab even if it doesn't widen as much as we want.
298 E.g. if from_mode is HImode, and to_mode is DImode, and there is no
299 direct HI->SI insn, then return SI->DI, if that exists.
300 If PERMIT_NON_WIDENING is non-zero then this can be used with
301 non-widening optabs also. */
303 enum insn_code
304 find_widening_optab_handler_and_mode (optab op, enum machine_mode to_mode,
305 enum machine_mode from_mode,
306 int permit_non_widening,
307 enum machine_mode *found_mode)
309 for (; (permit_non_widening || from_mode != to_mode)
310 && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
311 && from_mode != VOIDmode;
312 from_mode = GET_MODE_WIDER_MODE (from_mode))
314 enum insn_code handler = widening_optab_handler (op, to_mode,
315 from_mode);
317 if (handler != CODE_FOR_nothing)
319 if (found_mode)
320 *found_mode = from_mode;
321 return handler;
325 return CODE_FOR_nothing;
328 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
329 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
330 not actually do a sign-extend or zero-extend, but can leave the
331 higher-order bits of the result rtx undefined, for example, in the case
332 of logical operations, but not right shifts. */
334 static rtx
335 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
336 int unsignedp, int no_extend)
338 rtx result;
340 /* If we don't have to extend and this is a constant, return it. */
341 if (no_extend && GET_MODE (op) == VOIDmode)
342 return op;
344 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
345 extend since it will be more efficient to do so unless the signedness of
346 a promoted object differs from our extension. */
347 if (! no_extend
348 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
349 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
350 return convert_modes (mode, oldmode, op, unsignedp);
352 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
353 SUBREG. */
354 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
355 return gen_lowpart (mode, force_reg (GET_MODE (op), op));
357 /* Otherwise, get an object of MODE, clobber it, and set the low-order
358 part to OP. */
360 result = gen_reg_rtx (mode);
361 emit_clobber (result);
362 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
363 return result;
366 /* Return the optab used for computing the operation given by the tree code,
367 CODE and the tree EXP. This function is not always usable (for example, it
368 cannot give complete results for multiplication or division) but probably
369 ought to be relied on more widely throughout the expander. */
370 optab
371 optab_for_tree_code (enum tree_code code, const_tree type,
372 enum optab_subtype subtype)
374 bool trapv;
375 switch (code)
377 case BIT_AND_EXPR:
378 return and_optab;
380 case BIT_IOR_EXPR:
381 return ior_optab;
383 case BIT_NOT_EXPR:
384 return one_cmpl_optab;
386 case BIT_XOR_EXPR:
387 return xor_optab;
389 case MULT_HIGHPART_EXPR:
390 return TYPE_UNSIGNED (type) ? umul_highpart_optab : smul_highpart_optab;
392 case TRUNC_MOD_EXPR:
393 case CEIL_MOD_EXPR:
394 case FLOOR_MOD_EXPR:
395 case ROUND_MOD_EXPR:
396 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
398 case RDIV_EXPR:
399 case TRUNC_DIV_EXPR:
400 case CEIL_DIV_EXPR:
401 case FLOOR_DIV_EXPR:
402 case ROUND_DIV_EXPR:
403 case EXACT_DIV_EXPR:
404 if (TYPE_SATURATING(type))
405 return TYPE_UNSIGNED(type) ? usdiv_optab : ssdiv_optab;
406 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
408 case LSHIFT_EXPR:
409 if (TREE_CODE (type) == VECTOR_TYPE)
411 if (subtype == optab_vector)
412 return TYPE_SATURATING (type) ? unknown_optab : vashl_optab;
414 gcc_assert (subtype == optab_scalar);
416 if (TYPE_SATURATING(type))
417 return TYPE_UNSIGNED(type) ? usashl_optab : ssashl_optab;
418 return ashl_optab;
420 case RSHIFT_EXPR:
421 if (TREE_CODE (type) == VECTOR_TYPE)
423 if (subtype == optab_vector)
424 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
426 gcc_assert (subtype == optab_scalar);
428 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
430 case LROTATE_EXPR:
431 if (TREE_CODE (type) == VECTOR_TYPE)
433 if (subtype == optab_vector)
434 return vrotl_optab;
436 gcc_assert (subtype == optab_scalar);
438 return rotl_optab;
440 case RROTATE_EXPR:
441 if (TREE_CODE (type) == VECTOR_TYPE)
443 if (subtype == optab_vector)
444 return vrotr_optab;
446 gcc_assert (subtype == optab_scalar);
448 return rotr_optab;
450 case MAX_EXPR:
451 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
453 case MIN_EXPR:
454 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
456 case REALIGN_LOAD_EXPR:
457 return vec_realign_load_optab;
459 case WIDEN_SUM_EXPR:
460 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
462 case DOT_PROD_EXPR:
463 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
465 case WIDEN_MULT_PLUS_EXPR:
466 return (TYPE_UNSIGNED (type)
467 ? (TYPE_SATURATING (type)
468 ? usmadd_widen_optab : umadd_widen_optab)
469 : (TYPE_SATURATING (type)
470 ? ssmadd_widen_optab : smadd_widen_optab));
472 case WIDEN_MULT_MINUS_EXPR:
473 return (TYPE_UNSIGNED (type)
474 ? (TYPE_SATURATING (type)
475 ? usmsub_widen_optab : umsub_widen_optab)
476 : (TYPE_SATURATING (type)
477 ? ssmsub_widen_optab : smsub_widen_optab));
479 case FMA_EXPR:
480 return fma_optab;
482 case REDUC_MAX_EXPR:
483 return TYPE_UNSIGNED (type) ? reduc_umax_optab : reduc_smax_optab;
485 case REDUC_MIN_EXPR:
486 return TYPE_UNSIGNED (type) ? reduc_umin_optab : reduc_smin_optab;
488 case REDUC_PLUS_EXPR:
489 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
491 case VEC_LSHIFT_EXPR:
492 return vec_shl_optab;
494 case VEC_RSHIFT_EXPR:
495 return vec_shr_optab;
497 case VEC_WIDEN_MULT_HI_EXPR:
498 return TYPE_UNSIGNED (type) ?
499 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
501 case VEC_WIDEN_MULT_LO_EXPR:
502 return TYPE_UNSIGNED (type) ?
503 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
505 case VEC_WIDEN_MULT_EVEN_EXPR:
506 return TYPE_UNSIGNED (type) ?
507 vec_widen_umult_even_optab : vec_widen_smult_even_optab;
509 case VEC_WIDEN_MULT_ODD_EXPR:
510 return TYPE_UNSIGNED (type) ?
511 vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
513 case VEC_WIDEN_LSHIFT_HI_EXPR:
514 return TYPE_UNSIGNED (type) ?
515 vec_widen_ushiftl_hi_optab : vec_widen_sshiftl_hi_optab;
517 case VEC_WIDEN_LSHIFT_LO_EXPR:
518 return TYPE_UNSIGNED (type) ?
519 vec_widen_ushiftl_lo_optab : vec_widen_sshiftl_lo_optab;
521 case VEC_UNPACK_HI_EXPR:
522 return TYPE_UNSIGNED (type) ?
523 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
525 case VEC_UNPACK_LO_EXPR:
526 return TYPE_UNSIGNED (type) ?
527 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
529 case VEC_UNPACK_FLOAT_HI_EXPR:
530 /* The signedness is determined from input operand. */
531 return TYPE_UNSIGNED (type) ?
532 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
534 case VEC_UNPACK_FLOAT_LO_EXPR:
535 /* The signedness is determined from input operand. */
536 return TYPE_UNSIGNED (type) ?
537 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
539 case VEC_PACK_TRUNC_EXPR:
540 return vec_pack_trunc_optab;
542 case VEC_PACK_SAT_EXPR:
543 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
545 case VEC_PACK_FIX_TRUNC_EXPR:
546 /* The signedness is determined from output operand. */
547 return TYPE_UNSIGNED (type) ?
548 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
550 default:
551 break;
554 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
555 switch (code)
557 case POINTER_PLUS_EXPR:
558 case PLUS_EXPR:
559 if (TYPE_SATURATING(type))
560 return TYPE_UNSIGNED(type) ? usadd_optab : ssadd_optab;
561 return trapv ? addv_optab : add_optab;
563 case MINUS_EXPR:
564 if (TYPE_SATURATING(type))
565 return TYPE_UNSIGNED(type) ? ussub_optab : sssub_optab;
566 return trapv ? subv_optab : sub_optab;
568 case MULT_EXPR:
569 if (TYPE_SATURATING(type))
570 return TYPE_UNSIGNED(type) ? usmul_optab : ssmul_optab;
571 return trapv ? smulv_optab : smul_optab;
573 case NEGATE_EXPR:
574 if (TYPE_SATURATING(type))
575 return TYPE_UNSIGNED(type) ? usneg_optab : ssneg_optab;
576 return trapv ? negv_optab : neg_optab;
578 case ABS_EXPR:
579 return trapv ? absv_optab : abs_optab;
581 default:
582 return unknown_optab;
587 /* Expand vector widening operations.
589 There are two different classes of operations handled here:
590 1) Operations whose result is wider than all the arguments to the operation.
591 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
592 In this case OP0 and optionally OP1 would be initialized,
593 but WIDE_OP wouldn't (not relevant for this case).
594 2) Operations whose result is of the same size as the last argument to the
595 operation, but wider than all the other arguments to the operation.
596 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
597 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
599 E.g, when called to expand the following operations, this is how
600 the arguments will be initialized:
601 nops OP0 OP1 WIDE_OP
602 widening-sum 2 oprnd0 - oprnd1
603 widening-dot-product 3 oprnd0 oprnd1 oprnd2
604 widening-mult 2 oprnd0 oprnd1 -
605 type-promotion (vec-unpack) 1 oprnd0 - - */
608 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
609 rtx target, int unsignedp)
611 struct expand_operand eops[4];
612 tree oprnd0, oprnd1, oprnd2;
613 enum machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
614 optab widen_pattern_optab;
615 enum insn_code icode;
616 int nops = TREE_CODE_LENGTH (ops->code);
617 int op;
619 oprnd0 = ops->op0;
620 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
621 widen_pattern_optab =
622 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
623 if (ops->code == WIDEN_MULT_PLUS_EXPR
624 || ops->code == WIDEN_MULT_MINUS_EXPR)
625 icode = find_widening_optab_handler (widen_pattern_optab,
626 TYPE_MODE (TREE_TYPE (ops->op2)),
627 tmode0, 0);
628 else
629 icode = optab_handler (widen_pattern_optab, tmode0);
630 gcc_assert (icode != CODE_FOR_nothing);
632 if (nops >= 2)
634 oprnd1 = ops->op1;
635 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
638 /* The last operand is of a wider mode than the rest of the operands. */
639 if (nops == 2)
640 wmode = tmode1;
641 else if (nops == 3)
643 gcc_assert (tmode1 == tmode0);
644 gcc_assert (op1);
645 oprnd2 = ops->op2;
646 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
649 op = 0;
650 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
651 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
652 if (op1)
653 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
654 if (wide_op)
655 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
656 expand_insn (icode, op, eops);
657 return eops[0].value;
660 /* Generate code to perform an operation specified by TERNARY_OPTAB
661 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
663 UNSIGNEDP is for the case where we have to widen the operands
664 to perform the operation. It says to use zero-extension.
666 If TARGET is nonzero, the value
667 is generated there, if it is convenient to do so.
668 In all cases an rtx is returned for the locus of the value;
669 this may or may not be TARGET. */
672 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
673 rtx op1, rtx op2, rtx target, int unsignedp)
675 struct expand_operand ops[4];
676 enum insn_code icode = optab_handler (ternary_optab, mode);
678 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
680 create_output_operand (&ops[0], target, mode);
681 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
682 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
683 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
684 expand_insn (icode, 4, ops);
685 return ops[0].value;
689 /* Like expand_binop, but return a constant rtx if the result can be
690 calculated at compile time. The arguments and return value are
691 otherwise the same as for expand_binop. */
694 simplify_expand_binop (enum machine_mode mode, optab binoptab,
695 rtx op0, rtx op1, rtx target, int unsignedp,
696 enum optab_methods methods)
698 if (CONSTANT_P (op0) && CONSTANT_P (op1))
700 rtx x = simplify_binary_operation (optab_to_code (binoptab),
701 mode, op0, op1);
702 if (x)
703 return x;
706 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
709 /* Like simplify_expand_binop, but always put the result in TARGET.
710 Return true if the expansion succeeded. */
712 bool
713 force_expand_binop (enum machine_mode mode, optab binoptab,
714 rtx op0, rtx op1, rtx target, int unsignedp,
715 enum optab_methods methods)
717 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
718 target, unsignedp, methods);
719 if (x == 0)
720 return false;
721 if (x != target)
722 emit_move_insn (target, x);
723 return true;
726 /* Generate insns for VEC_LSHIFT_EXPR, VEC_RSHIFT_EXPR. */
729 expand_vec_shift_expr (sepops ops, rtx target)
731 struct expand_operand eops[3];
732 enum insn_code icode;
733 rtx rtx_op1, rtx_op2;
734 enum machine_mode mode = TYPE_MODE (ops->type);
735 tree vec_oprnd = ops->op0;
736 tree shift_oprnd = ops->op1;
737 optab shift_optab;
739 switch (ops->code)
741 case VEC_RSHIFT_EXPR:
742 shift_optab = vec_shr_optab;
743 break;
744 case VEC_LSHIFT_EXPR:
745 shift_optab = vec_shl_optab;
746 break;
747 default:
748 gcc_unreachable ();
751 icode = optab_handler (shift_optab, mode);
752 gcc_assert (icode != CODE_FOR_nothing);
754 rtx_op1 = expand_normal (vec_oprnd);
755 rtx_op2 = expand_normal (shift_oprnd);
757 create_output_operand (&eops[0], target, mode);
758 create_input_operand (&eops[1], rtx_op1, GET_MODE (rtx_op1));
759 create_convert_operand_from_type (&eops[2], rtx_op2, TREE_TYPE (shift_oprnd));
760 expand_insn (icode, 3, eops);
762 return eops[0].value;
765 /* Create a new vector value in VMODE with all elements set to OP. The
766 mode of OP must be the element mode of VMODE. If OP is a constant,
767 then the return value will be a constant. */
769 static rtx
770 expand_vector_broadcast (enum machine_mode vmode, rtx op)
772 enum insn_code icode;
773 rtvec vec;
774 rtx ret;
775 int i, n;
777 gcc_checking_assert (VECTOR_MODE_P (vmode));
779 n = GET_MODE_NUNITS (vmode);
780 vec = rtvec_alloc (n);
781 for (i = 0; i < n; ++i)
782 RTVEC_ELT (vec, i) = op;
784 if (CONSTANT_P (op))
785 return gen_rtx_CONST_VECTOR (vmode, vec);
787 /* ??? If the target doesn't have a vec_init, then we have no easy way
788 of performing this operation. Most of this sort of generic support
789 is hidden away in the vector lowering support in gimple. */
790 icode = optab_handler (vec_init_optab, vmode);
791 if (icode == CODE_FOR_nothing)
792 return NULL;
794 ret = gen_reg_rtx (vmode);
795 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
797 return ret;
800 /* This subroutine of expand_doubleword_shift handles the cases in which
801 the effective shift value is >= BITS_PER_WORD. The arguments and return
802 value are the same as for the parent routine, except that SUPERWORD_OP1
803 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
804 INTO_TARGET may be null if the caller has decided to calculate it. */
806 static bool
807 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
808 rtx outof_target, rtx into_target,
809 int unsignedp, enum optab_methods methods)
811 if (into_target != 0)
812 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
813 into_target, unsignedp, methods))
814 return false;
816 if (outof_target != 0)
818 /* For a signed right shift, we must fill OUTOF_TARGET with copies
819 of the sign bit, otherwise we must fill it with zeros. */
820 if (binoptab != ashr_optab)
821 emit_move_insn (outof_target, CONST0_RTX (word_mode));
822 else
823 if (!force_expand_binop (word_mode, binoptab,
824 outof_input, GEN_INT (BITS_PER_WORD - 1),
825 outof_target, unsignedp, methods))
826 return false;
828 return true;
831 /* This subroutine of expand_doubleword_shift handles the cases in which
832 the effective shift value is < BITS_PER_WORD. The arguments and return
833 value are the same as for the parent routine. */
835 static bool
836 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
837 rtx outof_input, rtx into_input, rtx op1,
838 rtx outof_target, rtx into_target,
839 int unsignedp, enum optab_methods methods,
840 unsigned HOST_WIDE_INT shift_mask)
842 optab reverse_unsigned_shift, unsigned_shift;
843 rtx tmp, carries;
845 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
846 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
848 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
849 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
850 the opposite direction to BINOPTAB. */
851 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
853 carries = outof_input;
854 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
855 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
856 0, true, methods);
858 else
860 /* We must avoid shifting by BITS_PER_WORD bits since that is either
861 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
862 has unknown behavior. Do a single shift first, then shift by the
863 remainder. It's OK to use ~OP1 as the remainder if shift counts
864 are truncated to the mode size. */
865 carries = expand_binop (word_mode, reverse_unsigned_shift,
866 outof_input, const1_rtx, 0, unsignedp, methods);
867 if (shift_mask == BITS_PER_WORD - 1)
869 tmp = immed_double_const (-1, -1, op1_mode);
870 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
871 0, true, methods);
873 else
875 tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
876 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
877 0, true, methods);
880 if (tmp == 0 || carries == 0)
881 return false;
882 carries = expand_binop (word_mode, reverse_unsigned_shift,
883 carries, tmp, 0, unsignedp, methods);
884 if (carries == 0)
885 return false;
887 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
888 so the result can go directly into INTO_TARGET if convenient. */
889 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
890 into_target, unsignedp, methods);
891 if (tmp == 0)
892 return false;
894 /* Now OR in the bits carried over from OUTOF_INPUT. */
895 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
896 into_target, unsignedp, methods))
897 return false;
899 /* Use a standard word_mode shift for the out-of half. */
900 if (outof_target != 0)
901 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
902 outof_target, unsignedp, methods))
903 return false;
905 return true;
909 #ifdef HAVE_conditional_move
910 /* Try implementing expand_doubleword_shift using conditional moves.
911 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
912 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
913 are the shift counts to use in the former and latter case. All other
914 arguments are the same as the parent routine. */
916 static bool
917 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
918 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
919 rtx outof_input, rtx into_input,
920 rtx subword_op1, rtx superword_op1,
921 rtx outof_target, rtx into_target,
922 int unsignedp, enum optab_methods methods,
923 unsigned HOST_WIDE_INT shift_mask)
925 rtx outof_superword, into_superword;
927 /* Put the superword version of the output into OUTOF_SUPERWORD and
928 INTO_SUPERWORD. */
929 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
930 if (outof_target != 0 && subword_op1 == superword_op1)
932 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
933 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
934 into_superword = outof_target;
935 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
936 outof_superword, 0, unsignedp, methods))
937 return false;
939 else
941 into_superword = gen_reg_rtx (word_mode);
942 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
943 outof_superword, into_superword,
944 unsignedp, methods))
945 return false;
948 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
949 if (!expand_subword_shift (op1_mode, binoptab,
950 outof_input, into_input, subword_op1,
951 outof_target, into_target,
952 unsignedp, methods, shift_mask))
953 return false;
955 /* Select between them. Do the INTO half first because INTO_SUPERWORD
956 might be the current value of OUTOF_TARGET. */
957 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
958 into_target, into_superword, word_mode, false))
959 return false;
961 if (outof_target != 0)
962 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
963 outof_target, outof_superword,
964 word_mode, false))
965 return false;
967 return true;
969 #endif
971 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
972 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
973 input operand; the shift moves bits in the direction OUTOF_INPUT->
974 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
975 of the target. OP1 is the shift count and OP1_MODE is its mode.
976 If OP1 is constant, it will have been truncated as appropriate
977 and is known to be nonzero.
979 If SHIFT_MASK is zero, the result of word shifts is undefined when the
980 shift count is outside the range [0, BITS_PER_WORD). This routine must
981 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
983 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
984 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
985 fill with zeros or sign bits as appropriate.
987 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
988 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
989 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
990 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
991 are undefined.
993 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
994 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
995 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
996 function wants to calculate it itself.
998 Return true if the shift could be successfully synthesized. */
1000 static bool
1001 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
1002 rtx outof_input, rtx into_input, rtx op1,
1003 rtx outof_target, rtx into_target,
1004 int unsignedp, enum optab_methods methods,
1005 unsigned HOST_WIDE_INT shift_mask)
1007 rtx superword_op1, tmp, cmp1, cmp2;
1008 rtx subword_label, done_label;
1009 enum rtx_code cmp_code;
1011 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1012 fill the result with sign or zero bits as appropriate. If so, the value
1013 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1014 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1015 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1017 This isn't worthwhile for constant shifts since the optimizers will
1018 cope better with in-range shift counts. */
1019 if (shift_mask >= BITS_PER_WORD
1020 && outof_target != 0
1021 && !CONSTANT_P (op1))
1023 if (!expand_doubleword_shift (op1_mode, binoptab,
1024 outof_input, into_input, op1,
1025 0, into_target,
1026 unsignedp, methods, shift_mask))
1027 return false;
1028 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1029 outof_target, unsignedp, methods))
1030 return false;
1031 return true;
1034 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1035 is true when the effective shift value is less than BITS_PER_WORD.
1036 Set SUPERWORD_OP1 to the shift count that should be used to shift
1037 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1038 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
1039 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1041 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1042 is a subword shift count. */
1043 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1044 0, true, methods);
1045 cmp2 = CONST0_RTX (op1_mode);
1046 cmp_code = EQ;
1047 superword_op1 = op1;
1049 else
1051 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1052 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1053 0, true, methods);
1054 cmp2 = CONST0_RTX (op1_mode);
1055 cmp_code = LT;
1056 superword_op1 = cmp1;
1058 if (cmp1 == 0)
1059 return false;
1061 /* If we can compute the condition at compile time, pick the
1062 appropriate subroutine. */
1063 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1064 if (tmp != 0 && CONST_INT_P (tmp))
1066 if (tmp == const0_rtx)
1067 return expand_superword_shift (binoptab, outof_input, superword_op1,
1068 outof_target, into_target,
1069 unsignedp, methods);
1070 else
1071 return expand_subword_shift (op1_mode, binoptab,
1072 outof_input, into_input, op1,
1073 outof_target, into_target,
1074 unsignedp, methods, shift_mask);
1077 #ifdef HAVE_conditional_move
1078 /* Try using conditional moves to generate straight-line code. */
1080 rtx start = get_last_insn ();
1081 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1082 cmp_code, cmp1, cmp2,
1083 outof_input, into_input,
1084 op1, superword_op1,
1085 outof_target, into_target,
1086 unsignedp, methods, shift_mask))
1087 return true;
1088 delete_insns_since (start);
1090 #endif
1092 /* As a last resort, use branches to select the correct alternative. */
1093 subword_label = gen_label_rtx ();
1094 done_label = gen_label_rtx ();
1096 NO_DEFER_POP;
1097 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1098 0, 0, subword_label, -1);
1099 OK_DEFER_POP;
1101 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1102 outof_target, into_target,
1103 unsignedp, methods))
1104 return false;
1106 emit_jump_insn (gen_jump (done_label));
1107 emit_barrier ();
1108 emit_label (subword_label);
1110 if (!expand_subword_shift (op1_mode, binoptab,
1111 outof_input, into_input, op1,
1112 outof_target, into_target,
1113 unsignedp, methods, shift_mask))
1114 return false;
1116 emit_label (done_label);
1117 return true;
1120 /* Subroutine of expand_binop. Perform a double word multiplication of
1121 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1122 as the target's word_mode. This function return NULL_RTX if anything
1123 goes wrong, in which case it may have already emitted instructions
1124 which need to be deleted.
1126 If we want to multiply two two-word values and have normal and widening
1127 multiplies of single-word values, we can do this with three smaller
1128 multiplications.
1130 The multiplication proceeds as follows:
1131 _______________________
1132 [__op0_high_|__op0_low__]
1133 _______________________
1134 * [__op1_high_|__op1_low__]
1135 _______________________________________________
1136 _______________________
1137 (1) [__op0_low__*__op1_low__]
1138 _______________________
1139 (2a) [__op0_low__*__op1_high_]
1140 _______________________
1141 (2b) [__op0_high_*__op1_low__]
1142 _______________________
1143 (3) [__op0_high_*__op1_high_]
1146 This gives a 4-word result. Since we are only interested in the
1147 lower 2 words, partial result (3) and the upper words of (2a) and
1148 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1149 calculated using non-widening multiplication.
1151 (1), however, needs to be calculated with an unsigned widening
1152 multiplication. If this operation is not directly supported we
1153 try using a signed widening multiplication and adjust the result.
1154 This adjustment works as follows:
1156 If both operands are positive then no adjustment is needed.
1158 If the operands have different signs, for example op0_low < 0 and
1159 op1_low >= 0, the instruction treats the most significant bit of
1160 op0_low as a sign bit instead of a bit with significance
1161 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1162 with 2**BITS_PER_WORD - op0_low, and two's complements the
1163 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1164 the result.
1166 Similarly, if both operands are negative, we need to add
1167 (op0_low + op1_low) * 2**BITS_PER_WORD.
1169 We use a trick to adjust quickly. We logically shift op0_low right
1170 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1171 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1172 logical shift exists, we do an arithmetic right shift and subtract
1173 the 0 or -1. */
1175 static rtx
1176 expand_doubleword_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
1177 bool umulp, enum optab_methods methods)
1179 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1180 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1181 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1182 rtx product, adjust, product_high, temp;
1184 rtx op0_high = operand_subword_force (op0, high, mode);
1185 rtx op0_low = operand_subword_force (op0, low, mode);
1186 rtx op1_high = operand_subword_force (op1, high, mode);
1187 rtx op1_low = operand_subword_force (op1, low, mode);
1189 /* If we're using an unsigned multiply to directly compute the product
1190 of the low-order words of the operands and perform any required
1191 adjustments of the operands, we begin by trying two more multiplications
1192 and then computing the appropriate sum.
1194 We have checked above that the required addition is provided.
1195 Full-word addition will normally always succeed, especially if
1196 it is provided at all, so we don't worry about its failure. The
1197 multiplication may well fail, however, so we do handle that. */
1199 if (!umulp)
1201 /* ??? This could be done with emit_store_flag where available. */
1202 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1203 NULL_RTX, 1, methods);
1204 if (temp)
1205 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1206 NULL_RTX, 0, OPTAB_DIRECT);
1207 else
1209 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1210 NULL_RTX, 0, methods);
1211 if (!temp)
1212 return NULL_RTX;
1213 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1214 NULL_RTX, 0, OPTAB_DIRECT);
1217 if (!op0_high)
1218 return NULL_RTX;
1221 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1222 NULL_RTX, 0, OPTAB_DIRECT);
1223 if (!adjust)
1224 return NULL_RTX;
1226 /* OP0_HIGH should now be dead. */
1228 if (!umulp)
1230 /* ??? This could be done with emit_store_flag where available. */
1231 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1232 NULL_RTX, 1, methods);
1233 if (temp)
1234 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1235 NULL_RTX, 0, OPTAB_DIRECT);
1236 else
1238 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1239 NULL_RTX, 0, methods);
1240 if (!temp)
1241 return NULL_RTX;
1242 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1243 NULL_RTX, 0, OPTAB_DIRECT);
1246 if (!op1_high)
1247 return NULL_RTX;
1250 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1251 NULL_RTX, 0, OPTAB_DIRECT);
1252 if (!temp)
1253 return NULL_RTX;
1255 /* OP1_HIGH should now be dead. */
1257 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1258 NULL_RTX, 0, OPTAB_DIRECT);
1260 if (target && !REG_P (target))
1261 target = NULL_RTX;
1263 if (umulp)
1264 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1265 target, 1, OPTAB_DIRECT);
1266 else
1267 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1268 target, 1, OPTAB_DIRECT);
1270 if (!product)
1271 return NULL_RTX;
1273 product_high = operand_subword (product, high, 1, mode);
1274 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1275 NULL_RTX, 0, OPTAB_DIRECT);
1276 emit_move_insn (product_high, adjust);
1277 return product;
1280 /* Wrapper around expand_binop which takes an rtx code to specify
1281 the operation to perform, not an optab pointer. All other
1282 arguments are the same. */
1284 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
1285 rtx op1, rtx target, int unsignedp,
1286 enum optab_methods methods)
1288 optab binop = code_to_optab (code);
1289 gcc_assert (binop);
1291 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1294 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1295 binop. Order them according to commutative_operand_precedence and, if
1296 possible, try to put TARGET or a pseudo first. */
1297 static bool
1298 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1300 int op0_prec = commutative_operand_precedence (op0);
1301 int op1_prec = commutative_operand_precedence (op1);
1303 if (op0_prec < op1_prec)
1304 return true;
1306 if (op0_prec > op1_prec)
1307 return false;
1309 /* With equal precedence, both orders are ok, but it is better if the
1310 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1311 if (target == 0 || REG_P (target))
1312 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1313 else
1314 return rtx_equal_p (op1, target);
1317 /* Return true if BINOPTAB implements a shift operation. */
1319 static bool
1320 shift_optab_p (optab binoptab)
1322 switch (optab_to_code (binoptab))
1324 case ASHIFT:
1325 case SS_ASHIFT:
1326 case US_ASHIFT:
1327 case ASHIFTRT:
1328 case LSHIFTRT:
1329 case ROTATE:
1330 case ROTATERT:
1331 return true;
1333 default:
1334 return false;
1338 /* Return true if BINOPTAB implements a commutative binary operation. */
1340 static bool
1341 commutative_optab_p (optab binoptab)
1343 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1344 || binoptab == smul_widen_optab
1345 || binoptab == umul_widen_optab
1346 || binoptab == smul_highpart_optab
1347 || binoptab == umul_highpart_optab);
1350 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1351 optimizing, and if the operand is a constant that costs more than
1352 1 instruction, force the constant into a register and return that
1353 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1355 static rtx
1356 avoid_expensive_constant (enum machine_mode mode, optab binoptab,
1357 int opn, rtx x, bool unsignedp)
1359 bool speed = optimize_insn_for_speed_p ();
1361 if (mode != VOIDmode
1362 && optimize
1363 && CONSTANT_P (x)
1364 && (rtx_cost (x, optab_to_code (binoptab), opn, speed)
1365 > set_src_cost (x, speed)))
1367 if (CONST_INT_P (x))
1369 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1370 if (intval != INTVAL (x))
1371 x = GEN_INT (intval);
1373 else
1374 x = convert_modes (mode, VOIDmode, x, unsignedp);
1375 x = force_reg (mode, x);
1377 return x;
1380 /* Helper function for expand_binop: handle the case where there
1381 is an insn that directly implements the indicated operation.
1382 Returns null if this is not possible. */
1383 static rtx
1384 expand_binop_directly (enum machine_mode mode, optab binoptab,
1385 rtx op0, rtx op1,
1386 rtx target, int unsignedp, enum optab_methods methods,
1387 rtx last)
1389 enum machine_mode from_mode = widened_mode (mode, op0, op1);
1390 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1391 from_mode, 1);
1392 enum machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1393 enum machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1394 enum machine_mode mode0, mode1, tmp_mode;
1395 struct expand_operand ops[3];
1396 bool commutative_p;
1397 rtx pat;
1398 rtx xop0 = op0, xop1 = op1;
1399 rtx swap;
1401 /* If it is a commutative operator and the modes would match
1402 if we would swap the operands, we can save the conversions. */
1403 commutative_p = commutative_optab_p (binoptab);
1404 if (commutative_p
1405 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1406 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1408 swap = xop0;
1409 xop0 = xop1;
1410 xop1 = swap;
1413 /* If we are optimizing, force expensive constants into a register. */
1414 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1415 if (!shift_optab_p (binoptab))
1416 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1418 /* In case the insn wants input operands in modes different from
1419 those of the actual operands, convert the operands. It would
1420 seem that we don't need to convert CONST_INTs, but we do, so
1421 that they're properly zero-extended, sign-extended or truncated
1422 for their mode. */
1424 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1425 if (xmode0 != VOIDmode && xmode0 != mode0)
1427 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1428 mode0 = xmode0;
1431 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1432 if (xmode1 != VOIDmode && xmode1 != mode1)
1434 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1435 mode1 = xmode1;
1438 /* If operation is commutative,
1439 try to make the first operand a register.
1440 Even better, try to make it the same as the target.
1441 Also try to make the last operand a constant. */
1442 if (commutative_p
1443 && swap_commutative_operands_with_target (target, xop0, xop1))
1445 swap = xop1;
1446 xop1 = xop0;
1447 xop0 = swap;
1450 /* Now, if insn's predicates don't allow our operands, put them into
1451 pseudo regs. */
1453 if (binoptab == vec_pack_trunc_optab
1454 || binoptab == vec_pack_usat_optab
1455 || binoptab == vec_pack_ssat_optab
1456 || binoptab == vec_pack_ufix_trunc_optab
1457 || binoptab == vec_pack_sfix_trunc_optab)
1459 /* The mode of the result is different then the mode of the
1460 arguments. */
1461 tmp_mode = insn_data[(int) icode].operand[0].mode;
1462 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1464 delete_insns_since (last);
1465 return NULL_RTX;
1468 else
1469 tmp_mode = mode;
1471 create_output_operand (&ops[0], target, tmp_mode);
1472 create_input_operand (&ops[1], xop0, mode0);
1473 create_input_operand (&ops[2], xop1, mode1);
1474 pat = maybe_gen_insn (icode, 3, ops);
1475 if (pat)
1477 /* If PAT is composed of more than one insn, try to add an appropriate
1478 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1479 operand, call expand_binop again, this time without a target. */
1480 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1481 && ! add_equal_note (pat, ops[0].value, optab_to_code (binoptab),
1482 ops[1].value, ops[2].value))
1484 delete_insns_since (last);
1485 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1486 unsignedp, methods);
1489 emit_insn (pat);
1490 return ops[0].value;
1492 delete_insns_since (last);
1493 return NULL_RTX;
1496 /* Generate code to perform an operation specified by BINOPTAB
1497 on operands OP0 and OP1, with result having machine-mode MODE.
1499 UNSIGNEDP is for the case where we have to widen the operands
1500 to perform the operation. It says to use zero-extension.
1502 If TARGET is nonzero, the value
1503 is generated there, if it is convenient to do so.
1504 In all cases an rtx is returned for the locus of the value;
1505 this may or may not be TARGET. */
1508 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
1509 rtx target, int unsignedp, enum optab_methods methods)
1511 enum optab_methods next_methods
1512 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1513 ? OPTAB_WIDEN : methods);
1514 enum mode_class mclass;
1515 enum machine_mode wider_mode;
1516 rtx libfunc;
1517 rtx temp;
1518 rtx entry_last = get_last_insn ();
1519 rtx last;
1521 mclass = GET_MODE_CLASS (mode);
1523 /* If subtracting an integer constant, convert this into an addition of
1524 the negated constant. */
1526 if (binoptab == sub_optab && CONST_INT_P (op1))
1528 op1 = negate_rtx (mode, op1);
1529 binoptab = add_optab;
1532 /* Record where to delete back to if we backtrack. */
1533 last = get_last_insn ();
1535 /* If we can do it with a three-operand insn, do so. */
1537 if (methods != OPTAB_MUST_WIDEN
1538 && find_widening_optab_handler (binoptab, mode,
1539 widened_mode (mode, op0, op1), 1)
1540 != CODE_FOR_nothing)
1542 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1543 unsignedp, methods, last);
1544 if (temp)
1545 return temp;
1548 /* If we were trying to rotate, and that didn't work, try rotating
1549 the other direction before falling back to shifts and bitwise-or. */
1550 if (((binoptab == rotl_optab
1551 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1552 || (binoptab == rotr_optab
1553 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1554 && mclass == MODE_INT)
1556 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1557 rtx newop1;
1558 unsigned int bits = GET_MODE_PRECISION (mode);
1560 if (CONST_INT_P (op1))
1561 newop1 = GEN_INT (bits - INTVAL (op1));
1562 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1563 newop1 = negate_rtx (GET_MODE (op1), op1);
1564 else
1565 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1566 gen_int_mode (bits, GET_MODE (op1)), op1,
1567 NULL_RTX, unsignedp, OPTAB_DIRECT);
1569 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1570 target, unsignedp, methods, last);
1571 if (temp)
1572 return temp;
1575 /* If this is a multiply, see if we can do a widening operation that
1576 takes operands of this mode and makes a wider mode. */
1578 if (binoptab == smul_optab
1579 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1580 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1581 : smul_widen_optab),
1582 GET_MODE_2XWIDER_MODE (mode), mode)
1583 != CODE_FOR_nothing))
1585 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1586 unsignedp ? umul_widen_optab : smul_widen_optab,
1587 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1589 if (temp != 0)
1591 if (GET_MODE_CLASS (mode) == MODE_INT
1592 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1593 return gen_lowpart (mode, temp);
1594 else
1595 return convert_to_mode (mode, temp, unsignedp);
1599 /* If this is a vector shift by a scalar, see if we can do a vector
1600 shift by a vector. If so, broadcast the scalar into a vector. */
1601 if (mclass == MODE_VECTOR_INT)
1603 optab otheroptab = unknown_optab;
1605 if (binoptab == ashl_optab)
1606 otheroptab = vashl_optab;
1607 else if (binoptab == ashr_optab)
1608 otheroptab = vashr_optab;
1609 else if (binoptab == lshr_optab)
1610 otheroptab = vlshr_optab;
1611 else if (binoptab == rotl_optab)
1612 otheroptab = vrotl_optab;
1613 else if (binoptab == rotr_optab)
1614 otheroptab = vrotr_optab;
1616 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1618 rtx vop1 = expand_vector_broadcast (mode, op1);
1619 if (vop1)
1621 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1622 target, unsignedp, methods, last);
1623 if (temp)
1624 return temp;
1629 /* Look for a wider mode of the same class for which we think we
1630 can open-code the operation. Check for a widening multiply at the
1631 wider mode as well. */
1633 if (CLASS_HAS_WIDER_MODES_P (mclass)
1634 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1635 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1636 wider_mode != VOIDmode;
1637 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1639 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1640 || (binoptab == smul_optab
1641 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1642 && (find_widening_optab_handler ((unsignedp
1643 ? umul_widen_optab
1644 : smul_widen_optab),
1645 GET_MODE_WIDER_MODE (wider_mode),
1646 mode, 0)
1647 != CODE_FOR_nothing)))
1649 rtx xop0 = op0, xop1 = op1;
1650 int no_extend = 0;
1652 /* For certain integer operations, we need not actually extend
1653 the narrow operands, as long as we will truncate
1654 the results to the same narrowness. */
1656 if ((binoptab == ior_optab || binoptab == and_optab
1657 || binoptab == xor_optab
1658 || binoptab == add_optab || binoptab == sub_optab
1659 || binoptab == smul_optab || binoptab == ashl_optab)
1660 && mclass == MODE_INT)
1662 no_extend = 1;
1663 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1664 xop0, unsignedp);
1665 if (binoptab != ashl_optab)
1666 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1667 xop1, unsignedp);
1670 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1672 /* The second operand of a shift must always be extended. */
1673 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1674 no_extend && binoptab != ashl_optab);
1676 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1677 unsignedp, OPTAB_DIRECT);
1678 if (temp)
1680 if (mclass != MODE_INT
1681 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1683 if (target == 0)
1684 target = gen_reg_rtx (mode);
1685 convert_move (target, temp, 0);
1686 return target;
1688 else
1689 return gen_lowpart (mode, temp);
1691 else
1692 delete_insns_since (last);
1696 /* If operation is commutative,
1697 try to make the first operand a register.
1698 Even better, try to make it the same as the target.
1699 Also try to make the last operand a constant. */
1700 if (commutative_optab_p (binoptab)
1701 && swap_commutative_operands_with_target (target, op0, op1))
1703 temp = op1;
1704 op1 = op0;
1705 op0 = temp;
1708 /* These can be done a word at a time. */
1709 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1710 && mclass == MODE_INT
1711 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1712 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1714 int i;
1715 rtx insns;
1717 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1718 won't be accurate, so use a new target. */
1719 if (target == 0
1720 || target == op0
1721 || target == op1
1722 || !valid_multiword_target_p (target))
1723 target = gen_reg_rtx (mode);
1725 start_sequence ();
1727 /* Do the actual arithmetic. */
1728 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1730 rtx target_piece = operand_subword (target, i, 1, mode);
1731 rtx x = expand_binop (word_mode, binoptab,
1732 operand_subword_force (op0, i, mode),
1733 operand_subword_force (op1, i, mode),
1734 target_piece, unsignedp, next_methods);
1736 if (x == 0)
1737 break;
1739 if (target_piece != x)
1740 emit_move_insn (target_piece, x);
1743 insns = get_insns ();
1744 end_sequence ();
1746 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1748 emit_insn (insns);
1749 return target;
1753 /* Synthesize double word shifts from single word shifts. */
1754 if ((binoptab == lshr_optab || binoptab == ashl_optab
1755 || binoptab == ashr_optab)
1756 && mclass == MODE_INT
1757 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1758 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1759 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1760 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1761 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1762 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1764 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1765 enum machine_mode op1_mode;
1767 double_shift_mask = targetm.shift_truncation_mask (mode);
1768 shift_mask = targetm.shift_truncation_mask (word_mode);
1769 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1771 /* Apply the truncation to constant shifts. */
1772 if (double_shift_mask > 0 && CONST_INT_P (op1))
1773 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1775 if (op1 == CONST0_RTX (op1_mode))
1776 return op0;
1778 /* Make sure that this is a combination that expand_doubleword_shift
1779 can handle. See the comments there for details. */
1780 if (double_shift_mask == 0
1781 || (shift_mask == BITS_PER_WORD - 1
1782 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1784 rtx insns;
1785 rtx into_target, outof_target;
1786 rtx into_input, outof_input;
1787 int left_shift, outof_word;
1789 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1790 won't be accurate, so use a new target. */
1791 if (target == 0
1792 || target == op0
1793 || target == op1
1794 || !valid_multiword_target_p (target))
1795 target = gen_reg_rtx (mode);
1797 start_sequence ();
1799 /* OUTOF_* is the word we are shifting bits away from, and
1800 INTO_* is the word that we are shifting bits towards, thus
1801 they differ depending on the direction of the shift and
1802 WORDS_BIG_ENDIAN. */
1804 left_shift = binoptab == ashl_optab;
1805 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1807 outof_target = operand_subword (target, outof_word, 1, mode);
1808 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1810 outof_input = operand_subword_force (op0, outof_word, mode);
1811 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1813 if (expand_doubleword_shift (op1_mode, binoptab,
1814 outof_input, into_input, op1,
1815 outof_target, into_target,
1816 unsignedp, next_methods, shift_mask))
1818 insns = get_insns ();
1819 end_sequence ();
1821 emit_insn (insns);
1822 return target;
1824 end_sequence ();
1828 /* Synthesize double word rotates from single word shifts. */
1829 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1830 && mclass == MODE_INT
1831 && CONST_INT_P (op1)
1832 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1833 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1834 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1836 rtx insns;
1837 rtx into_target, outof_target;
1838 rtx into_input, outof_input;
1839 rtx inter;
1840 int shift_count, left_shift, outof_word;
1842 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1843 won't be accurate, so use a new target. Do this also if target is not
1844 a REG, first because having a register instead may open optimization
1845 opportunities, and second because if target and op0 happen to be MEMs
1846 designating the same location, we would risk clobbering it too early
1847 in the code sequence we generate below. */
1848 if (target == 0
1849 || target == op0
1850 || target == op1
1851 || !REG_P (target)
1852 || !valid_multiword_target_p (target))
1853 target = gen_reg_rtx (mode);
1855 start_sequence ();
1857 shift_count = INTVAL (op1);
1859 /* OUTOF_* is the word we are shifting bits away from, and
1860 INTO_* is the word that we are shifting bits towards, thus
1861 they differ depending on the direction of the shift and
1862 WORDS_BIG_ENDIAN. */
1864 left_shift = (binoptab == rotl_optab);
1865 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1867 outof_target = operand_subword (target, outof_word, 1, mode);
1868 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1870 outof_input = operand_subword_force (op0, outof_word, mode);
1871 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1873 if (shift_count == BITS_PER_WORD)
1875 /* This is just a word swap. */
1876 emit_move_insn (outof_target, into_input);
1877 emit_move_insn (into_target, outof_input);
1878 inter = const0_rtx;
1880 else
1882 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1883 rtx first_shift_count, second_shift_count;
1884 optab reverse_unsigned_shift, unsigned_shift;
1886 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1887 ? lshr_optab : ashl_optab);
1889 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1890 ? ashl_optab : lshr_optab);
1892 if (shift_count > BITS_PER_WORD)
1894 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1895 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1897 else
1899 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1900 second_shift_count = GEN_INT (shift_count);
1903 into_temp1 = expand_binop (word_mode, unsigned_shift,
1904 outof_input, first_shift_count,
1905 NULL_RTX, unsignedp, next_methods);
1906 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1907 into_input, second_shift_count,
1908 NULL_RTX, unsignedp, next_methods);
1910 if (into_temp1 != 0 && into_temp2 != 0)
1911 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1912 into_target, unsignedp, next_methods);
1913 else
1914 inter = 0;
1916 if (inter != 0 && inter != into_target)
1917 emit_move_insn (into_target, inter);
1919 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1920 into_input, first_shift_count,
1921 NULL_RTX, unsignedp, next_methods);
1922 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1923 outof_input, second_shift_count,
1924 NULL_RTX, unsignedp, next_methods);
1926 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1927 inter = expand_binop (word_mode, ior_optab,
1928 outof_temp1, outof_temp2,
1929 outof_target, unsignedp, next_methods);
1931 if (inter != 0 && inter != outof_target)
1932 emit_move_insn (outof_target, inter);
1935 insns = get_insns ();
1936 end_sequence ();
1938 if (inter != 0)
1940 emit_insn (insns);
1941 return target;
1945 /* These can be done a word at a time by propagating carries. */
1946 if ((binoptab == add_optab || binoptab == sub_optab)
1947 && mclass == MODE_INT
1948 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1949 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1951 unsigned int i;
1952 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1953 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1954 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1955 rtx xop0, xop1, xtarget;
1957 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1958 value is one of those, use it. Otherwise, use 1 since it is the
1959 one easiest to get. */
1960 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1961 int normalizep = STORE_FLAG_VALUE;
1962 #else
1963 int normalizep = 1;
1964 #endif
1966 /* Prepare the operands. */
1967 xop0 = force_reg (mode, op0);
1968 xop1 = force_reg (mode, op1);
1970 xtarget = gen_reg_rtx (mode);
1972 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1973 target = xtarget;
1975 /* Indicate for flow that the entire target reg is being set. */
1976 if (REG_P (target))
1977 emit_clobber (xtarget);
1979 /* Do the actual arithmetic. */
1980 for (i = 0; i < nwords; i++)
1982 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1983 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1984 rtx op0_piece = operand_subword_force (xop0, index, mode);
1985 rtx op1_piece = operand_subword_force (xop1, index, mode);
1986 rtx x;
1988 /* Main add/subtract of the input operands. */
1989 x = expand_binop (word_mode, binoptab,
1990 op0_piece, op1_piece,
1991 target_piece, unsignedp, next_methods);
1992 if (x == 0)
1993 break;
1995 if (i + 1 < nwords)
1997 /* Store carry from main add/subtract. */
1998 carry_out = gen_reg_rtx (word_mode);
1999 carry_out = emit_store_flag_force (carry_out,
2000 (binoptab == add_optab
2001 ? LT : GT),
2002 x, op0_piece,
2003 word_mode, 1, normalizep);
2006 if (i > 0)
2008 rtx newx;
2010 /* Add/subtract previous carry to main result. */
2011 newx = expand_binop (word_mode,
2012 normalizep == 1 ? binoptab : otheroptab,
2013 x, carry_in,
2014 NULL_RTX, 1, next_methods);
2016 if (i + 1 < nwords)
2018 /* Get out carry from adding/subtracting carry in. */
2019 rtx carry_tmp = gen_reg_rtx (word_mode);
2020 carry_tmp = emit_store_flag_force (carry_tmp,
2021 (binoptab == add_optab
2022 ? LT : GT),
2023 newx, x,
2024 word_mode, 1, normalizep);
2026 /* Logical-ior the two poss. carry together. */
2027 carry_out = expand_binop (word_mode, ior_optab,
2028 carry_out, carry_tmp,
2029 carry_out, 0, next_methods);
2030 if (carry_out == 0)
2031 break;
2033 emit_move_insn (target_piece, newx);
2035 else
2037 if (x != target_piece)
2038 emit_move_insn (target_piece, x);
2041 carry_in = carry_out;
2044 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2046 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2047 || ! rtx_equal_p (target, xtarget))
2049 rtx temp = emit_move_insn (target, xtarget);
2051 set_dst_reg_note (temp, REG_EQUAL,
2052 gen_rtx_fmt_ee (optab_to_code (binoptab),
2053 mode, copy_rtx (xop0),
2054 copy_rtx (xop1)),
2055 target);
2057 else
2058 target = xtarget;
2060 return target;
2063 else
2064 delete_insns_since (last);
2067 /* Attempt to synthesize double word multiplies using a sequence of word
2068 mode multiplications. We first attempt to generate a sequence using a
2069 more efficient unsigned widening multiply, and if that fails we then
2070 try using a signed widening multiply. */
2072 if (binoptab == smul_optab
2073 && mclass == MODE_INT
2074 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2075 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2076 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2078 rtx product = NULL_RTX;
2079 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
2080 != CODE_FOR_nothing)
2082 product = expand_doubleword_mult (mode, op0, op1, target,
2083 true, methods);
2084 if (!product)
2085 delete_insns_since (last);
2088 if (product == NULL_RTX
2089 && widening_optab_handler (smul_widen_optab, mode, word_mode)
2090 != CODE_FOR_nothing)
2092 product = expand_doubleword_mult (mode, op0, op1, target,
2093 false, methods);
2094 if (!product)
2095 delete_insns_since (last);
2098 if (product != NULL_RTX)
2100 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2102 temp = emit_move_insn (target ? target : product, product);
2103 set_dst_reg_note (temp,
2104 REG_EQUAL,
2105 gen_rtx_fmt_ee (MULT, mode,
2106 copy_rtx (op0),
2107 copy_rtx (op1)),
2108 target ? target : product);
2110 return product;
2114 /* It can't be open-coded in this mode.
2115 Use a library call if one is available and caller says that's ok. */
2117 libfunc = optab_libfunc (binoptab, mode);
2118 if (libfunc
2119 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2121 rtx insns;
2122 rtx op1x = op1;
2123 enum machine_mode op1_mode = mode;
2124 rtx value;
2126 start_sequence ();
2128 if (shift_optab_p (binoptab))
2130 op1_mode = targetm.libgcc_shift_count_mode ();
2131 /* Specify unsigned here,
2132 since negative shift counts are meaningless. */
2133 op1x = convert_to_mode (op1_mode, op1, 1);
2136 if (GET_MODE (op0) != VOIDmode
2137 && GET_MODE (op0) != mode)
2138 op0 = convert_to_mode (mode, op0, unsignedp);
2140 /* Pass 1 for NO_QUEUE so we don't lose any increments
2141 if the libcall is cse'd or moved. */
2142 value = emit_library_call_value (libfunc,
2143 NULL_RTX, LCT_CONST, mode, 2,
2144 op0, mode, op1x, op1_mode);
2146 insns = get_insns ();
2147 end_sequence ();
2149 target = gen_reg_rtx (mode);
2150 emit_libcall_block_1 (insns, target, value,
2151 gen_rtx_fmt_ee (optab_to_code (binoptab),
2152 mode, op0, op1),
2153 trapv_binoptab_p (binoptab));
2155 return target;
2158 delete_insns_since (last);
2160 /* It can't be done in this mode. Can we do it in a wider mode? */
2162 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2163 || methods == OPTAB_MUST_WIDEN))
2165 /* Caller says, don't even try. */
2166 delete_insns_since (entry_last);
2167 return 0;
2170 /* Compute the value of METHODS to pass to recursive calls.
2171 Don't allow widening to be tried recursively. */
2173 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2175 /* Look for a wider mode of the same class for which it appears we can do
2176 the operation. */
2178 if (CLASS_HAS_WIDER_MODES_P (mclass))
2180 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2181 wider_mode != VOIDmode;
2182 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2184 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2185 != CODE_FOR_nothing
2186 || (methods == OPTAB_LIB
2187 && optab_libfunc (binoptab, wider_mode)))
2189 rtx xop0 = op0, xop1 = op1;
2190 int no_extend = 0;
2192 /* For certain integer operations, we need not actually extend
2193 the narrow operands, as long as we will truncate
2194 the results to the same narrowness. */
2196 if ((binoptab == ior_optab || binoptab == and_optab
2197 || binoptab == xor_optab
2198 || binoptab == add_optab || binoptab == sub_optab
2199 || binoptab == smul_optab || binoptab == ashl_optab)
2200 && mclass == MODE_INT)
2201 no_extend = 1;
2203 xop0 = widen_operand (xop0, wider_mode, mode,
2204 unsignedp, no_extend);
2206 /* The second operand of a shift must always be extended. */
2207 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2208 no_extend && binoptab != ashl_optab);
2210 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2211 unsignedp, methods);
2212 if (temp)
2214 if (mclass != MODE_INT
2215 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2217 if (target == 0)
2218 target = gen_reg_rtx (mode);
2219 convert_move (target, temp, 0);
2220 return target;
2222 else
2223 return gen_lowpart (mode, temp);
2225 else
2226 delete_insns_since (last);
2231 delete_insns_since (entry_last);
2232 return 0;
2235 /* Expand a binary operator which has both signed and unsigned forms.
2236 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2237 signed operations.
2239 If we widen unsigned operands, we may use a signed wider operation instead
2240 of an unsigned wider operation, since the result would be the same. */
2243 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
2244 rtx op0, rtx op1, rtx target, int unsignedp,
2245 enum optab_methods methods)
2247 rtx temp;
2248 optab direct_optab = unsignedp ? uoptab : soptab;
2249 bool save_enable;
2251 /* Do it without widening, if possible. */
2252 temp = expand_binop (mode, direct_optab, op0, op1, target,
2253 unsignedp, OPTAB_DIRECT);
2254 if (temp || methods == OPTAB_DIRECT)
2255 return temp;
2257 /* Try widening to a signed int. Disable any direct use of any
2258 signed insn in the current mode. */
2259 save_enable = swap_optab_enable (soptab, mode, false);
2261 temp = expand_binop (mode, soptab, op0, op1, target,
2262 unsignedp, OPTAB_WIDEN);
2264 /* For unsigned operands, try widening to an unsigned int. */
2265 if (!temp && unsignedp)
2266 temp = expand_binop (mode, uoptab, op0, op1, target,
2267 unsignedp, OPTAB_WIDEN);
2268 if (temp || methods == OPTAB_WIDEN)
2269 goto egress;
2271 /* Use the right width libcall if that exists. */
2272 temp = expand_binop (mode, direct_optab, op0, op1, target,
2273 unsignedp, OPTAB_LIB);
2274 if (temp || methods == OPTAB_LIB)
2275 goto egress;
2277 /* Must widen and use a libcall, use either signed or unsigned. */
2278 temp = expand_binop (mode, soptab, op0, op1, target,
2279 unsignedp, methods);
2280 if (!temp && unsignedp)
2281 temp = expand_binop (mode, uoptab, op0, op1, target,
2282 unsignedp, methods);
2284 egress:
2285 /* Undo the fiddling above. */
2286 if (save_enable)
2287 swap_optab_enable (soptab, mode, true);
2288 return temp;
2291 /* Generate code to perform an operation specified by UNOPPTAB
2292 on operand OP0, with two results to TARG0 and TARG1.
2293 We assume that the order of the operands for the instruction
2294 is TARG0, TARG1, OP0.
2296 Either TARG0 or TARG1 may be zero, but what that means is that
2297 the result is not actually wanted. We will generate it into
2298 a dummy pseudo-reg and discard it. They may not both be zero.
2300 Returns 1 if this operation can be performed; 0 if not. */
2303 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2304 int unsignedp)
2306 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2307 enum mode_class mclass;
2308 enum machine_mode wider_mode;
2309 rtx entry_last = get_last_insn ();
2310 rtx last;
2312 mclass = GET_MODE_CLASS (mode);
2314 if (!targ0)
2315 targ0 = gen_reg_rtx (mode);
2316 if (!targ1)
2317 targ1 = gen_reg_rtx (mode);
2319 /* Record where to go back to if we fail. */
2320 last = get_last_insn ();
2322 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2324 struct expand_operand ops[3];
2325 enum insn_code icode = optab_handler (unoptab, mode);
2327 create_fixed_operand (&ops[0], targ0);
2328 create_fixed_operand (&ops[1], targ1);
2329 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2330 if (maybe_expand_insn (icode, 3, ops))
2331 return 1;
2334 /* It can't be done in this mode. Can we do it in a wider mode? */
2336 if (CLASS_HAS_WIDER_MODES_P (mclass))
2338 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2339 wider_mode != VOIDmode;
2340 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2342 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2344 rtx t0 = gen_reg_rtx (wider_mode);
2345 rtx t1 = gen_reg_rtx (wider_mode);
2346 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2348 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2350 convert_move (targ0, t0, unsignedp);
2351 convert_move (targ1, t1, unsignedp);
2352 return 1;
2354 else
2355 delete_insns_since (last);
2360 delete_insns_since (entry_last);
2361 return 0;
2364 /* Generate code to perform an operation specified by BINOPTAB
2365 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2366 We assume that the order of the operands for the instruction
2367 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2368 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2370 Either TARG0 or TARG1 may be zero, but what that means is that
2371 the result is not actually wanted. We will generate it into
2372 a dummy pseudo-reg and discard it. They may not both be zero.
2374 Returns 1 if this operation can be performed; 0 if not. */
2377 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2378 int unsignedp)
2380 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2381 enum mode_class mclass;
2382 enum machine_mode wider_mode;
2383 rtx entry_last = get_last_insn ();
2384 rtx last;
2386 mclass = GET_MODE_CLASS (mode);
2388 if (!targ0)
2389 targ0 = gen_reg_rtx (mode);
2390 if (!targ1)
2391 targ1 = gen_reg_rtx (mode);
2393 /* Record where to go back to if we fail. */
2394 last = get_last_insn ();
2396 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2398 struct expand_operand ops[4];
2399 enum insn_code icode = optab_handler (binoptab, mode);
2400 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2401 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
2402 rtx xop0 = op0, xop1 = op1;
2404 /* If we are optimizing, force expensive constants into a register. */
2405 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2406 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2408 create_fixed_operand (&ops[0], targ0);
2409 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2410 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2411 create_fixed_operand (&ops[3], targ1);
2412 if (maybe_expand_insn (icode, 4, ops))
2413 return 1;
2414 delete_insns_since (last);
2417 /* It can't be done in this mode. Can we do it in a wider mode? */
2419 if (CLASS_HAS_WIDER_MODES_P (mclass))
2421 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2422 wider_mode != VOIDmode;
2423 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2425 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2427 rtx t0 = gen_reg_rtx (wider_mode);
2428 rtx t1 = gen_reg_rtx (wider_mode);
2429 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2430 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2432 if (expand_twoval_binop (binoptab, cop0, cop1,
2433 t0, t1, unsignedp))
2435 convert_move (targ0, t0, unsignedp);
2436 convert_move (targ1, t1, unsignedp);
2437 return 1;
2439 else
2440 delete_insns_since (last);
2445 delete_insns_since (entry_last);
2446 return 0;
2449 /* Expand the two-valued library call indicated by BINOPTAB, but
2450 preserve only one of the values. If TARG0 is non-NULL, the first
2451 value is placed into TARG0; otherwise the second value is placed
2452 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2453 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2454 This routine assumes that the value returned by the library call is
2455 as if the return value was of an integral mode twice as wide as the
2456 mode of OP0. Returns 1 if the call was successful. */
2458 bool
2459 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2460 rtx targ0, rtx targ1, enum rtx_code code)
2462 enum machine_mode mode;
2463 enum machine_mode libval_mode;
2464 rtx libval;
2465 rtx insns;
2466 rtx libfunc;
2468 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2469 gcc_assert (!targ0 != !targ1);
2471 mode = GET_MODE (op0);
2472 libfunc = optab_libfunc (binoptab, mode);
2473 if (!libfunc)
2474 return false;
2476 /* The value returned by the library function will have twice as
2477 many bits as the nominal MODE. */
2478 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2479 MODE_INT);
2480 start_sequence ();
2481 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2482 libval_mode, 2,
2483 op0, mode,
2484 op1, mode);
2485 /* Get the part of VAL containing the value that we want. */
2486 libval = simplify_gen_subreg (mode, libval, libval_mode,
2487 targ0 ? 0 : GET_MODE_SIZE (mode));
2488 insns = get_insns ();
2489 end_sequence ();
2490 /* Move the into the desired location. */
2491 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2492 gen_rtx_fmt_ee (code, mode, op0, op1));
2494 return true;
2498 /* Wrapper around expand_unop which takes an rtx code to specify
2499 the operation to perform, not an optab pointer. All other
2500 arguments are the same. */
2502 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2503 rtx target, int unsignedp)
2505 optab unop = code_to_optab (code);
2506 gcc_assert (unop);
2508 return expand_unop (mode, unop, op0, target, unsignedp);
2511 /* Try calculating
2512 (clz:narrow x)
2514 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2516 A similar operation can be used for clrsb. UNOPTAB says which operation
2517 we are trying to expand. */
2518 static rtx
2519 widen_leading (enum machine_mode mode, rtx op0, rtx target, optab unoptab)
2521 enum mode_class mclass = GET_MODE_CLASS (mode);
2522 if (CLASS_HAS_WIDER_MODES_P (mclass))
2524 enum machine_mode wider_mode;
2525 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2526 wider_mode != VOIDmode;
2527 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2529 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2531 rtx xop0, temp, last;
2533 last = get_last_insn ();
2535 if (target == 0)
2536 target = gen_reg_rtx (mode);
2537 xop0 = widen_operand (op0, wider_mode, mode,
2538 unoptab != clrsb_optab, false);
2539 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2540 unoptab != clrsb_optab);
2541 if (temp != 0)
2542 temp = expand_binop
2543 (wider_mode, sub_optab, temp,
2544 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2545 - GET_MODE_PRECISION (mode),
2546 wider_mode),
2547 target, true, OPTAB_DIRECT);
2548 if (temp == 0)
2549 delete_insns_since (last);
2551 return temp;
2555 return 0;
2558 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2559 quantities, choosing which based on whether the high word is nonzero. */
2560 static rtx
2561 expand_doubleword_clz (enum machine_mode mode, rtx op0, rtx target)
2563 rtx xop0 = force_reg (mode, op0);
2564 rtx subhi = gen_highpart (word_mode, xop0);
2565 rtx sublo = gen_lowpart (word_mode, xop0);
2566 rtx hi0_label = gen_label_rtx ();
2567 rtx after_label = gen_label_rtx ();
2568 rtx seq, temp, result;
2570 /* If we were not given a target, use a word_mode register, not a
2571 'mode' register. The result will fit, and nobody is expecting
2572 anything bigger (the return type of __builtin_clz* is int). */
2573 if (!target)
2574 target = gen_reg_rtx (word_mode);
2576 /* In any case, write to a word_mode scratch in both branches of the
2577 conditional, so we can ensure there is a single move insn setting
2578 'target' to tag a REG_EQUAL note on. */
2579 result = gen_reg_rtx (word_mode);
2581 start_sequence ();
2583 /* If the high word is not equal to zero,
2584 then clz of the full value is clz of the high word. */
2585 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2586 word_mode, true, hi0_label);
2588 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2589 if (!temp)
2590 goto fail;
2592 if (temp != result)
2593 convert_move (result, temp, true);
2595 emit_jump_insn (gen_jump (after_label));
2596 emit_barrier ();
2598 /* Else clz of the full value is clz of the low word plus the number
2599 of bits in the high word. */
2600 emit_label (hi0_label);
2602 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2603 if (!temp)
2604 goto fail;
2605 temp = expand_binop (word_mode, add_optab, temp,
2606 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2607 result, true, OPTAB_DIRECT);
2608 if (!temp)
2609 goto fail;
2610 if (temp != result)
2611 convert_move (result, temp, true);
2613 emit_label (after_label);
2614 convert_move (target, result, true);
2616 seq = get_insns ();
2617 end_sequence ();
2619 add_equal_note (seq, target, CLZ, xop0, 0);
2620 emit_insn (seq);
2621 return target;
2623 fail:
2624 end_sequence ();
2625 return 0;
2628 /* Try calculating
2629 (bswap:narrow x)
2631 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2632 static rtx
2633 widen_bswap (enum machine_mode mode, rtx op0, rtx target)
2635 enum mode_class mclass = GET_MODE_CLASS (mode);
2636 enum machine_mode wider_mode;
2637 rtx x, last;
2639 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2640 return NULL_RTX;
2642 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2643 wider_mode != VOIDmode;
2644 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2645 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2646 goto found;
2647 return NULL_RTX;
2649 found:
2650 last = get_last_insn ();
2652 x = widen_operand (op0, wider_mode, mode, true, true);
2653 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2655 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2656 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2657 if (x != 0)
2658 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2659 GET_MODE_BITSIZE (wider_mode)
2660 - GET_MODE_BITSIZE (mode),
2661 NULL_RTX, true);
2663 if (x != 0)
2665 if (target == 0)
2666 target = gen_reg_rtx (mode);
2667 emit_move_insn (target, gen_lowpart (mode, x));
2669 else
2670 delete_insns_since (last);
2672 return target;
2675 /* Try calculating bswap as two bswaps of two word-sized operands. */
2677 static rtx
2678 expand_doubleword_bswap (enum machine_mode mode, rtx op, rtx target)
2680 rtx t0, t1;
2682 t1 = expand_unop (word_mode, bswap_optab,
2683 operand_subword_force (op, 0, mode), NULL_RTX, true);
2684 t0 = expand_unop (word_mode, bswap_optab,
2685 operand_subword_force (op, 1, mode), NULL_RTX, true);
2687 if (target == 0 || !valid_multiword_target_p (target))
2688 target = gen_reg_rtx (mode);
2689 if (REG_P (target))
2690 emit_clobber (target);
2691 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2692 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2694 return target;
2697 /* Try calculating (parity x) as (and (popcount x) 1), where
2698 popcount can also be done in a wider mode. */
2699 static rtx
2700 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2702 enum mode_class mclass = GET_MODE_CLASS (mode);
2703 if (CLASS_HAS_WIDER_MODES_P (mclass))
2705 enum machine_mode wider_mode;
2706 for (wider_mode = mode; wider_mode != VOIDmode;
2707 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2709 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2711 rtx xop0, temp, last;
2713 last = get_last_insn ();
2715 if (target == 0)
2716 target = gen_reg_rtx (mode);
2717 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2718 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2719 true);
2720 if (temp != 0)
2721 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2722 target, true, OPTAB_DIRECT);
2723 if (temp == 0)
2724 delete_insns_since (last);
2726 return temp;
2730 return 0;
2733 /* Try calculating ctz(x) as K - clz(x & -x) ,
2734 where K is GET_MODE_PRECISION(mode) - 1.
2736 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2737 don't have to worry about what the hardware does in that case. (If
2738 the clz instruction produces the usual value at 0, which is K, the
2739 result of this code sequence will be -1; expand_ffs, below, relies
2740 on this. It might be nice to have it be K instead, for consistency
2741 with the (very few) processors that provide a ctz with a defined
2742 value, but that would take one more instruction, and it would be
2743 less convenient for expand_ffs anyway. */
2745 static rtx
2746 expand_ctz (enum machine_mode mode, rtx op0, rtx target)
2748 rtx seq, temp;
2750 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2751 return 0;
2753 start_sequence ();
2755 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2756 if (temp)
2757 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2758 true, OPTAB_DIRECT);
2759 if (temp)
2760 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2761 if (temp)
2762 temp = expand_binop (mode, sub_optab,
2763 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2764 temp, target,
2765 true, OPTAB_DIRECT);
2766 if (temp == 0)
2768 end_sequence ();
2769 return 0;
2772 seq = get_insns ();
2773 end_sequence ();
2775 add_equal_note (seq, temp, CTZ, op0, 0);
2776 emit_insn (seq);
2777 return temp;
2781 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2782 else with the sequence used by expand_clz.
2784 The ffs builtin promises to return zero for a zero value and ctz/clz
2785 may have an undefined value in that case. If they do not give us a
2786 convenient value, we have to generate a test and branch. */
2787 static rtx
2788 expand_ffs (enum machine_mode mode, rtx op0, rtx target)
2790 HOST_WIDE_INT val = 0;
2791 bool defined_at_zero = false;
2792 rtx temp, seq;
2794 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2796 start_sequence ();
2798 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2799 if (!temp)
2800 goto fail;
2802 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2804 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2806 start_sequence ();
2807 temp = expand_ctz (mode, op0, 0);
2808 if (!temp)
2809 goto fail;
2811 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2813 defined_at_zero = true;
2814 val = (GET_MODE_PRECISION (mode) - 1) - val;
2817 else
2818 return 0;
2820 if (defined_at_zero && val == -1)
2821 /* No correction needed at zero. */;
2822 else
2824 /* We don't try to do anything clever with the situation found
2825 on some processors (eg Alpha) where ctz(0:mode) ==
2826 bitsize(mode). If someone can think of a way to send N to -1
2827 and leave alone all values in the range 0..N-1 (where N is a
2828 power of two), cheaper than this test-and-branch, please add it.
2830 The test-and-branch is done after the operation itself, in case
2831 the operation sets condition codes that can be recycled for this.
2832 (This is true on i386, for instance.) */
2834 rtx nonzero_label = gen_label_rtx ();
2835 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2836 mode, true, nonzero_label);
2838 convert_move (temp, GEN_INT (-1), false);
2839 emit_label (nonzero_label);
2842 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2843 to produce a value in the range 0..bitsize. */
2844 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2845 target, false, OPTAB_DIRECT);
2846 if (!temp)
2847 goto fail;
2849 seq = get_insns ();
2850 end_sequence ();
2852 add_equal_note (seq, temp, FFS, op0, 0);
2853 emit_insn (seq);
2854 return temp;
2856 fail:
2857 end_sequence ();
2858 return 0;
2861 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2862 conditions, VAL may already be a SUBREG against which we cannot generate
2863 a further SUBREG. In this case, we expect forcing the value into a
2864 register will work around the situation. */
2866 static rtx
2867 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2868 enum machine_mode imode)
2870 rtx ret;
2871 ret = lowpart_subreg (omode, val, imode);
2872 if (ret == NULL)
2874 val = force_reg (imode, val);
2875 ret = lowpart_subreg (omode, val, imode);
2876 gcc_assert (ret != NULL);
2878 return ret;
2881 /* Expand a floating point absolute value or negation operation via a
2882 logical operation on the sign bit. */
2884 static rtx
2885 expand_absneg_bit (enum rtx_code code, enum machine_mode mode,
2886 rtx op0, rtx target)
2888 const struct real_format *fmt;
2889 int bitpos, word, nwords, i;
2890 enum machine_mode imode;
2891 double_int mask;
2892 rtx temp, insns;
2894 /* The format has to have a simple sign bit. */
2895 fmt = REAL_MODE_FORMAT (mode);
2896 if (fmt == NULL)
2897 return NULL_RTX;
2899 bitpos = fmt->signbit_rw;
2900 if (bitpos < 0)
2901 return NULL_RTX;
2903 /* Don't create negative zeros if the format doesn't support them. */
2904 if (code == NEG && !fmt->has_signed_zero)
2905 return NULL_RTX;
2907 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2909 imode = int_mode_for_mode (mode);
2910 if (imode == BLKmode)
2911 return NULL_RTX;
2912 word = 0;
2913 nwords = 1;
2915 else
2917 imode = word_mode;
2919 if (FLOAT_WORDS_BIG_ENDIAN)
2920 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2921 else
2922 word = bitpos / BITS_PER_WORD;
2923 bitpos = bitpos % BITS_PER_WORD;
2924 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2927 mask = double_int_zero.set_bit (bitpos);
2928 if (code == ABS)
2929 mask = ~mask;
2931 if (target == 0
2932 || target == op0
2933 || (nwords > 1 && !valid_multiword_target_p (target)))
2934 target = gen_reg_rtx (mode);
2936 if (nwords > 1)
2938 start_sequence ();
2940 for (i = 0; i < nwords; ++i)
2942 rtx targ_piece = operand_subword (target, i, 1, mode);
2943 rtx op0_piece = operand_subword_force (op0, i, mode);
2945 if (i == word)
2947 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2948 op0_piece,
2949 immed_double_int_const (mask, imode),
2950 targ_piece, 1, OPTAB_LIB_WIDEN);
2951 if (temp != targ_piece)
2952 emit_move_insn (targ_piece, temp);
2954 else
2955 emit_move_insn (targ_piece, op0_piece);
2958 insns = get_insns ();
2959 end_sequence ();
2961 emit_insn (insns);
2963 else
2965 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2966 gen_lowpart (imode, op0),
2967 immed_double_int_const (mask, imode),
2968 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2969 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2971 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2972 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2973 target);
2976 return target;
2979 /* As expand_unop, but will fail rather than attempt the operation in a
2980 different mode or with a libcall. */
2981 static rtx
2982 expand_unop_direct (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2983 int unsignedp)
2985 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2987 struct expand_operand ops[2];
2988 enum insn_code icode = optab_handler (unoptab, mode);
2989 rtx last = get_last_insn ();
2990 rtx pat;
2992 create_output_operand (&ops[0], target, mode);
2993 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2994 pat = maybe_gen_insn (icode, 2, ops);
2995 if (pat)
2997 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2998 && ! add_equal_note (pat, ops[0].value, optab_to_code (unoptab),
2999 ops[1].value, NULL_RTX))
3001 delete_insns_since (last);
3002 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3005 emit_insn (pat);
3007 return ops[0].value;
3010 return 0;
3013 /* Generate code to perform an operation specified by UNOPTAB
3014 on operand OP0, with result having machine-mode MODE.
3016 UNSIGNEDP is for the case where we have to widen the operands
3017 to perform the operation. It says to use zero-extension.
3019 If TARGET is nonzero, the value
3020 is generated there, if it is convenient to do so.
3021 In all cases an rtx is returned for the locus of the value;
3022 this may or may not be TARGET. */
3025 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
3026 int unsignedp)
3028 enum mode_class mclass = GET_MODE_CLASS (mode);
3029 enum machine_mode wider_mode;
3030 rtx temp;
3031 rtx libfunc;
3033 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3034 if (temp)
3035 return temp;
3037 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3039 /* Widening (or narrowing) clz needs special treatment. */
3040 if (unoptab == clz_optab)
3042 temp = widen_leading (mode, op0, target, unoptab);
3043 if (temp)
3044 return temp;
3046 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3047 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3049 temp = expand_doubleword_clz (mode, op0, target);
3050 if (temp)
3051 return temp;
3054 goto try_libcall;
3057 if (unoptab == clrsb_optab)
3059 temp = widen_leading (mode, op0, target, unoptab);
3060 if (temp)
3061 return temp;
3062 goto try_libcall;
3065 /* Widening (or narrowing) bswap needs special treatment. */
3066 if (unoptab == bswap_optab)
3068 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3069 or ROTATERT. First try these directly; if this fails, then try the
3070 obvious pair of shifts with allowed widening, as this will probably
3071 be always more efficient than the other fallback methods. */
3072 if (mode == HImode)
3074 rtx last, temp1, temp2;
3076 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3078 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
3079 unsignedp, OPTAB_DIRECT);
3080 if (temp)
3081 return temp;
3084 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3086 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
3087 unsignedp, OPTAB_DIRECT);
3088 if (temp)
3089 return temp;
3092 last = get_last_insn ();
3094 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
3095 unsignedp, OPTAB_WIDEN);
3096 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
3097 unsignedp, OPTAB_WIDEN);
3098 if (temp1 && temp2)
3100 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3101 unsignedp, OPTAB_WIDEN);
3102 if (temp)
3103 return temp;
3106 delete_insns_since (last);
3109 temp = widen_bswap (mode, op0, target);
3110 if (temp)
3111 return temp;
3113 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3114 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3116 temp = expand_doubleword_bswap (mode, op0, target);
3117 if (temp)
3118 return temp;
3121 goto try_libcall;
3124 if (CLASS_HAS_WIDER_MODES_P (mclass))
3125 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3126 wider_mode != VOIDmode;
3127 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3129 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3131 rtx xop0 = op0;
3132 rtx last = get_last_insn ();
3134 /* For certain operations, we need not actually extend
3135 the narrow operand, as long as we will truncate the
3136 results to the same narrowness. */
3138 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3139 (unoptab == neg_optab
3140 || unoptab == one_cmpl_optab)
3141 && mclass == MODE_INT);
3143 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3144 unsignedp);
3146 if (temp)
3148 if (mclass != MODE_INT
3149 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3151 if (target == 0)
3152 target = gen_reg_rtx (mode);
3153 convert_move (target, temp, 0);
3154 return target;
3156 else
3157 return gen_lowpart (mode, temp);
3159 else
3160 delete_insns_since (last);
3164 /* These can be done a word at a time. */
3165 if (unoptab == one_cmpl_optab
3166 && mclass == MODE_INT
3167 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3168 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3170 int i;
3171 rtx insns;
3173 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3174 target = gen_reg_rtx (mode);
3176 start_sequence ();
3178 /* Do the actual arithmetic. */
3179 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3181 rtx target_piece = operand_subword (target, i, 1, mode);
3182 rtx x = expand_unop (word_mode, unoptab,
3183 operand_subword_force (op0, i, mode),
3184 target_piece, unsignedp);
3186 if (target_piece != x)
3187 emit_move_insn (target_piece, x);
3190 insns = get_insns ();
3191 end_sequence ();
3193 emit_insn (insns);
3194 return target;
3197 if (optab_to_code (unoptab) == NEG)
3199 /* Try negating floating point values by flipping the sign bit. */
3200 if (SCALAR_FLOAT_MODE_P (mode))
3202 temp = expand_absneg_bit (NEG, mode, op0, target);
3203 if (temp)
3204 return temp;
3207 /* If there is no negation pattern, and we have no negative zero,
3208 try subtracting from zero. */
3209 if (!HONOR_SIGNED_ZEROS (mode))
3211 temp = expand_binop (mode, (unoptab == negv_optab
3212 ? subv_optab : sub_optab),
3213 CONST0_RTX (mode), op0, target,
3214 unsignedp, OPTAB_DIRECT);
3215 if (temp)
3216 return temp;
3220 /* Try calculating parity (x) as popcount (x) % 2. */
3221 if (unoptab == parity_optab)
3223 temp = expand_parity (mode, op0, target);
3224 if (temp)
3225 return temp;
3228 /* Try implementing ffs (x) in terms of clz (x). */
3229 if (unoptab == ffs_optab)
3231 temp = expand_ffs (mode, op0, target);
3232 if (temp)
3233 return temp;
3236 /* Try implementing ctz (x) in terms of clz (x). */
3237 if (unoptab == ctz_optab)
3239 temp = expand_ctz (mode, op0, target);
3240 if (temp)
3241 return temp;
3244 try_libcall:
3245 /* Now try a library call in this mode. */
3246 libfunc = optab_libfunc (unoptab, mode);
3247 if (libfunc)
3249 rtx insns;
3250 rtx value;
3251 rtx eq_value;
3252 enum machine_mode outmode = mode;
3254 /* All of these functions return small values. Thus we choose to
3255 have them return something that isn't a double-word. */
3256 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3257 || unoptab == clrsb_optab || unoptab == popcount_optab
3258 || unoptab == parity_optab)
3259 outmode
3260 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3261 optab_libfunc (unoptab, mode)));
3263 start_sequence ();
3265 /* Pass 1 for NO_QUEUE so we don't lose any increments
3266 if the libcall is cse'd or moved. */
3267 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3268 1, op0, mode);
3269 insns = get_insns ();
3270 end_sequence ();
3272 target = gen_reg_rtx (outmode);
3273 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3274 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3275 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3276 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3277 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3278 emit_libcall_block_1 (insns, target, value, eq_value,
3279 trapv_unoptab_p (unoptab));
3281 return target;
3284 /* It can't be done in this mode. Can we do it in a wider mode? */
3286 if (CLASS_HAS_WIDER_MODES_P (mclass))
3288 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3289 wider_mode != VOIDmode;
3290 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3292 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3293 || optab_libfunc (unoptab, wider_mode))
3295 rtx xop0 = op0;
3296 rtx last = get_last_insn ();
3298 /* For certain operations, we need not actually extend
3299 the narrow operand, as long as we will truncate the
3300 results to the same narrowness. */
3301 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3302 (unoptab == neg_optab
3303 || unoptab == one_cmpl_optab
3304 || unoptab == bswap_optab)
3305 && mclass == MODE_INT);
3307 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3308 unsignedp);
3310 /* If we are generating clz using wider mode, adjust the
3311 result. Similarly for clrsb. */
3312 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3313 && temp != 0)
3314 temp = expand_binop
3315 (wider_mode, sub_optab, temp,
3316 gen_int_mode (GET_MODE_PRECISION (wider_mode)
3317 - GET_MODE_PRECISION (mode),
3318 wider_mode),
3319 target, true, OPTAB_DIRECT);
3321 /* Likewise for bswap. */
3322 if (unoptab == bswap_optab && temp != 0)
3324 gcc_assert (GET_MODE_PRECISION (wider_mode)
3325 == GET_MODE_BITSIZE (wider_mode)
3326 && GET_MODE_PRECISION (mode)
3327 == GET_MODE_BITSIZE (mode));
3329 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3330 GET_MODE_BITSIZE (wider_mode)
3331 - GET_MODE_BITSIZE (mode),
3332 NULL_RTX, true);
3335 if (temp)
3337 if (mclass != MODE_INT)
3339 if (target == 0)
3340 target = gen_reg_rtx (mode);
3341 convert_move (target, temp, 0);
3342 return target;
3344 else
3345 return gen_lowpart (mode, temp);
3347 else
3348 delete_insns_since (last);
3353 /* One final attempt at implementing negation via subtraction,
3354 this time allowing widening of the operand. */
3355 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3357 rtx temp;
3358 temp = expand_binop (mode,
3359 unoptab == negv_optab ? subv_optab : sub_optab,
3360 CONST0_RTX (mode), op0,
3361 target, unsignedp, OPTAB_LIB_WIDEN);
3362 if (temp)
3363 return temp;
3366 return 0;
3369 /* Emit code to compute the absolute value of OP0, with result to
3370 TARGET if convenient. (TARGET may be 0.) The return value says
3371 where the result actually is to be found.
3373 MODE is the mode of the operand; the mode of the result is
3374 different but can be deduced from MODE.
3379 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
3380 int result_unsignedp)
3382 rtx temp;
3384 if (! flag_trapv)
3385 result_unsignedp = 1;
3387 /* First try to do it with a special abs instruction. */
3388 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3389 op0, target, 0);
3390 if (temp != 0)
3391 return temp;
3393 /* For floating point modes, try clearing the sign bit. */
3394 if (SCALAR_FLOAT_MODE_P (mode))
3396 temp = expand_absneg_bit (ABS, mode, op0, target);
3397 if (temp)
3398 return temp;
3401 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3402 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3403 && !HONOR_SIGNED_ZEROS (mode))
3405 rtx last = get_last_insn ();
3407 temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
3408 if (temp != 0)
3409 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3410 OPTAB_WIDEN);
3412 if (temp != 0)
3413 return temp;
3415 delete_insns_since (last);
3418 /* If this machine has expensive jumps, we can do integer absolute
3419 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3420 where W is the width of MODE. */
3422 if (GET_MODE_CLASS (mode) == MODE_INT
3423 && BRANCH_COST (optimize_insn_for_speed_p (),
3424 false) >= 2)
3426 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3427 GET_MODE_PRECISION (mode) - 1,
3428 NULL_RTX, 0);
3430 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3431 OPTAB_LIB_WIDEN);
3432 if (temp != 0)
3433 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3434 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3436 if (temp != 0)
3437 return temp;
3440 return NULL_RTX;
3444 expand_abs (enum machine_mode mode, rtx op0, rtx target,
3445 int result_unsignedp, int safe)
3447 rtx temp, op1;
3449 if (! flag_trapv)
3450 result_unsignedp = 1;
3452 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3453 if (temp != 0)
3454 return temp;
3456 /* If that does not win, use conditional jump and negate. */
3458 /* It is safe to use the target if it is the same
3459 as the source if this is also a pseudo register */
3460 if (op0 == target && REG_P (op0)
3461 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3462 safe = 1;
3464 op1 = gen_label_rtx ();
3465 if (target == 0 || ! safe
3466 || GET_MODE (target) != mode
3467 || (MEM_P (target) && MEM_VOLATILE_P (target))
3468 || (REG_P (target)
3469 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3470 target = gen_reg_rtx (mode);
3472 emit_move_insn (target, op0);
3473 NO_DEFER_POP;
3475 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3476 NULL_RTX, NULL_RTX, op1, -1);
3478 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3479 target, target, 0);
3480 if (op0 != target)
3481 emit_move_insn (target, op0);
3482 emit_label (op1);
3483 OK_DEFER_POP;
3484 return target;
3487 /* Emit code to compute the one's complement absolute value of OP0
3488 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3489 (TARGET may be NULL_RTX.) The return value says where the result
3490 actually is to be found.
3492 MODE is the mode of the operand; the mode of the result is
3493 different but can be deduced from MODE. */
3496 expand_one_cmpl_abs_nojump (enum machine_mode mode, rtx op0, rtx target)
3498 rtx temp;
3500 /* Not applicable for floating point modes. */
3501 if (FLOAT_MODE_P (mode))
3502 return NULL_RTX;
3504 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3505 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3507 rtx last = get_last_insn ();
3509 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3510 if (temp != 0)
3511 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3512 OPTAB_WIDEN);
3514 if (temp != 0)
3515 return temp;
3517 delete_insns_since (last);
3520 /* If this machine has expensive jumps, we can do one's complement
3521 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3523 if (GET_MODE_CLASS (mode) == MODE_INT
3524 && BRANCH_COST (optimize_insn_for_speed_p (),
3525 false) >= 2)
3527 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3528 GET_MODE_PRECISION (mode) - 1,
3529 NULL_RTX, 0);
3531 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3532 OPTAB_LIB_WIDEN);
3534 if (temp != 0)
3535 return temp;
3538 return NULL_RTX;
3541 /* A subroutine of expand_copysign, perform the copysign operation using the
3542 abs and neg primitives advertised to exist on the target. The assumption
3543 is that we have a split register file, and leaving op0 in fp registers,
3544 and not playing with subregs so much, will help the register allocator. */
3546 static rtx
3547 expand_copysign_absneg (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3548 int bitpos, bool op0_is_abs)
3550 enum machine_mode imode;
3551 enum insn_code icode;
3552 rtx sign, label;
3554 if (target == op1)
3555 target = NULL_RTX;
3557 /* Check if the back end provides an insn that handles signbit for the
3558 argument's mode. */
3559 icode = optab_handler (signbit_optab, mode);
3560 if (icode != CODE_FOR_nothing)
3562 imode = insn_data[(int) icode].operand[0].mode;
3563 sign = gen_reg_rtx (imode);
3564 emit_unop_insn (icode, sign, op1, UNKNOWN);
3566 else
3568 double_int mask;
3570 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3572 imode = int_mode_for_mode (mode);
3573 if (imode == BLKmode)
3574 return NULL_RTX;
3575 op1 = gen_lowpart (imode, op1);
3577 else
3579 int word;
3581 imode = word_mode;
3582 if (FLOAT_WORDS_BIG_ENDIAN)
3583 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3584 else
3585 word = bitpos / BITS_PER_WORD;
3586 bitpos = bitpos % BITS_PER_WORD;
3587 op1 = operand_subword_force (op1, word, mode);
3590 mask = double_int_zero.set_bit (bitpos);
3592 sign = expand_binop (imode, and_optab, op1,
3593 immed_double_int_const (mask, imode),
3594 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3597 if (!op0_is_abs)
3599 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3600 if (op0 == NULL)
3601 return NULL_RTX;
3602 target = op0;
3604 else
3606 if (target == NULL_RTX)
3607 target = copy_to_reg (op0);
3608 else
3609 emit_move_insn (target, op0);
3612 label = gen_label_rtx ();
3613 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3615 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3616 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3617 else
3618 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3619 if (op0 != target)
3620 emit_move_insn (target, op0);
3622 emit_label (label);
3624 return target;
3628 /* A subroutine of expand_copysign, perform the entire copysign operation
3629 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3630 is true if op0 is known to have its sign bit clear. */
3632 static rtx
3633 expand_copysign_bit (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3634 int bitpos, bool op0_is_abs)
3636 enum machine_mode imode;
3637 double_int mask;
3638 int word, nwords, i;
3639 rtx temp, insns;
3641 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3643 imode = int_mode_for_mode (mode);
3644 if (imode == BLKmode)
3645 return NULL_RTX;
3646 word = 0;
3647 nwords = 1;
3649 else
3651 imode = word_mode;
3653 if (FLOAT_WORDS_BIG_ENDIAN)
3654 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3655 else
3656 word = bitpos / BITS_PER_WORD;
3657 bitpos = bitpos % BITS_PER_WORD;
3658 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3661 mask = double_int_zero.set_bit (bitpos);
3663 if (target == 0
3664 || target == op0
3665 || target == op1
3666 || (nwords > 1 && !valid_multiword_target_p (target)))
3667 target = gen_reg_rtx (mode);
3669 if (nwords > 1)
3671 start_sequence ();
3673 for (i = 0; i < nwords; ++i)
3675 rtx targ_piece = operand_subword (target, i, 1, mode);
3676 rtx op0_piece = operand_subword_force (op0, i, mode);
3678 if (i == word)
3680 if (!op0_is_abs)
3681 op0_piece
3682 = expand_binop (imode, and_optab, op0_piece,
3683 immed_double_int_const (~mask, imode),
3684 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3686 op1 = expand_binop (imode, and_optab,
3687 operand_subword_force (op1, i, mode),
3688 immed_double_int_const (mask, imode),
3689 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3691 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3692 targ_piece, 1, OPTAB_LIB_WIDEN);
3693 if (temp != targ_piece)
3694 emit_move_insn (targ_piece, temp);
3696 else
3697 emit_move_insn (targ_piece, op0_piece);
3700 insns = get_insns ();
3701 end_sequence ();
3703 emit_insn (insns);
3705 else
3707 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3708 immed_double_int_const (mask, imode),
3709 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3711 op0 = gen_lowpart (imode, op0);
3712 if (!op0_is_abs)
3713 op0 = expand_binop (imode, and_optab, op0,
3714 immed_double_int_const (~mask, imode),
3715 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3717 temp = expand_binop (imode, ior_optab, op0, op1,
3718 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3719 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3722 return target;
3725 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3726 scalar floating point mode. Return NULL if we do not know how to
3727 expand the operation inline. */
3730 expand_copysign (rtx op0, rtx op1, rtx target)
3732 enum machine_mode mode = GET_MODE (op0);
3733 const struct real_format *fmt;
3734 bool op0_is_abs;
3735 rtx temp;
3737 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3738 gcc_assert (GET_MODE (op1) == mode);
3740 /* First try to do it with a special instruction. */
3741 temp = expand_binop (mode, copysign_optab, op0, op1,
3742 target, 0, OPTAB_DIRECT);
3743 if (temp)
3744 return temp;
3746 fmt = REAL_MODE_FORMAT (mode);
3747 if (fmt == NULL || !fmt->has_signed_zero)
3748 return NULL_RTX;
3750 op0_is_abs = false;
3751 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3753 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3754 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3755 op0_is_abs = true;
3758 if (fmt->signbit_ro >= 0
3759 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3760 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3761 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3763 temp = expand_copysign_absneg (mode, op0, op1, target,
3764 fmt->signbit_ro, op0_is_abs);
3765 if (temp)
3766 return temp;
3769 if (fmt->signbit_rw < 0)
3770 return NULL_RTX;
3771 return expand_copysign_bit (mode, op0, op1, target,
3772 fmt->signbit_rw, op0_is_abs);
3775 /* Generate an instruction whose insn-code is INSN_CODE,
3776 with two operands: an output TARGET and an input OP0.
3777 TARGET *must* be nonzero, and the output is always stored there.
3778 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3779 the value that is stored into TARGET.
3781 Return false if expansion failed. */
3783 bool
3784 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3785 enum rtx_code code)
3787 struct expand_operand ops[2];
3788 rtx pat;
3790 create_output_operand (&ops[0], target, GET_MODE (target));
3791 create_input_operand (&ops[1], op0, GET_MODE (op0));
3792 pat = maybe_gen_insn (icode, 2, ops);
3793 if (!pat)
3794 return false;
3796 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
3797 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3799 emit_insn (pat);
3801 if (ops[0].value != target)
3802 emit_move_insn (target, ops[0].value);
3803 return true;
3805 /* Generate an instruction whose insn-code is INSN_CODE,
3806 with two operands: an output TARGET and an input OP0.
3807 TARGET *must* be nonzero, and the output is always stored there.
3808 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3809 the value that is stored into TARGET. */
3811 void
3812 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3814 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3815 gcc_assert (ok);
3818 struct no_conflict_data
3820 rtx target, first, insn;
3821 bool must_stay;
3824 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3825 the currently examined clobber / store has to stay in the list of
3826 insns that constitute the actual libcall block. */
3827 static void
3828 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3830 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3832 /* If this inns directly contributes to setting the target, it must stay. */
3833 if (reg_overlap_mentioned_p (p->target, dest))
3834 p->must_stay = true;
3835 /* If we haven't committed to keeping any other insns in the list yet,
3836 there is nothing more to check. */
3837 else if (p->insn == p->first)
3838 return;
3839 /* If this insn sets / clobbers a register that feeds one of the insns
3840 already in the list, this insn has to stay too. */
3841 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3842 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3843 || reg_used_between_p (dest, p->first, p->insn)
3844 /* Likewise if this insn depends on a register set by a previous
3845 insn in the list, or if it sets a result (presumably a hard
3846 register) that is set or clobbered by a previous insn.
3847 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3848 SET_DEST perform the former check on the address, and the latter
3849 check on the MEM. */
3850 || (GET_CODE (set) == SET
3851 && (modified_in_p (SET_SRC (set), p->first)
3852 || modified_in_p (SET_DEST (set), p->first)
3853 || modified_between_p (SET_SRC (set), p->first, p->insn)
3854 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3855 p->must_stay = true;
3859 /* Emit code to make a call to a constant function or a library call.
3861 INSNS is a list containing all insns emitted in the call.
3862 These insns leave the result in RESULT. Our block is to copy RESULT
3863 to TARGET, which is logically equivalent to EQUIV.
3865 We first emit any insns that set a pseudo on the assumption that these are
3866 loading constants into registers; doing so allows them to be safely cse'ed
3867 between blocks. Then we emit all the other insns in the block, followed by
3868 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3869 note with an operand of EQUIV. */
3871 static void
3872 emit_libcall_block_1 (rtx insns, rtx target, rtx result, rtx equiv,
3873 bool equiv_may_trap)
3875 rtx final_dest = target;
3876 rtx next, last, insn;
3878 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3879 into a MEM later. Protect the libcall block from this change. */
3880 if (! REG_P (target) || REG_USERVAR_P (target))
3881 target = gen_reg_rtx (GET_MODE (target));
3883 /* If we're using non-call exceptions, a libcall corresponding to an
3884 operation that may trap may also trap. */
3885 /* ??? See the comment in front of make_reg_eh_region_note. */
3886 if (cfun->can_throw_non_call_exceptions
3887 && (equiv_may_trap || may_trap_p (equiv)))
3889 for (insn = insns; insn; insn = NEXT_INSN (insn))
3890 if (CALL_P (insn))
3892 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3893 if (note)
3895 int lp_nr = INTVAL (XEXP (note, 0));
3896 if (lp_nr == 0 || lp_nr == INT_MIN)
3897 remove_note (insn, note);
3901 else
3903 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3904 reg note to indicate that this call cannot throw or execute a nonlocal
3905 goto (unless there is already a REG_EH_REGION note, in which case
3906 we update it). */
3907 for (insn = insns; insn; insn = NEXT_INSN (insn))
3908 if (CALL_P (insn))
3909 make_reg_eh_region_note_nothrow_nononlocal (insn);
3912 /* First emit all insns that set pseudos. Remove them from the list as
3913 we go. Avoid insns that set pseudos which were referenced in previous
3914 insns. These can be generated by move_by_pieces, for example,
3915 to update an address. Similarly, avoid insns that reference things
3916 set in previous insns. */
3918 for (insn = insns; insn; insn = next)
3920 rtx set = single_set (insn);
3922 next = NEXT_INSN (insn);
3924 if (set != 0 && REG_P (SET_DEST (set))
3925 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3927 struct no_conflict_data data;
3929 data.target = const0_rtx;
3930 data.first = insns;
3931 data.insn = insn;
3932 data.must_stay = 0;
3933 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3934 if (! data.must_stay)
3936 if (PREV_INSN (insn))
3937 NEXT_INSN (PREV_INSN (insn)) = next;
3938 else
3939 insns = next;
3941 if (next)
3942 PREV_INSN (next) = PREV_INSN (insn);
3944 add_insn (insn);
3948 /* Some ports use a loop to copy large arguments onto the stack.
3949 Don't move anything outside such a loop. */
3950 if (LABEL_P (insn))
3951 break;
3954 /* Write the remaining insns followed by the final copy. */
3955 for (insn = insns; insn; insn = next)
3957 next = NEXT_INSN (insn);
3959 add_insn (insn);
3962 last = emit_move_insn (target, result);
3963 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3965 if (final_dest != target)
3966 emit_move_insn (final_dest, target);
3969 void
3970 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3972 emit_libcall_block_1 (insns, target, result, equiv, false);
3975 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3976 PURPOSE describes how this comparison will be used. CODE is the rtx
3977 comparison code we will be using.
3979 ??? Actually, CODE is slightly weaker than that. A target is still
3980 required to implement all of the normal bcc operations, but not
3981 required to implement all (or any) of the unordered bcc operations. */
3984 can_compare_p (enum rtx_code code, enum machine_mode mode,
3985 enum can_compare_purpose purpose)
3987 rtx test;
3988 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3991 enum insn_code icode;
3993 if (purpose == ccp_jump
3994 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
3995 && insn_operand_matches (icode, 0, test))
3996 return 1;
3997 if (purpose == ccp_store_flag
3998 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
3999 && insn_operand_matches (icode, 1, test))
4000 return 1;
4001 if (purpose == ccp_cmov
4002 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
4003 return 1;
4005 mode = GET_MODE_WIDER_MODE (mode);
4006 PUT_MODE (test, mode);
4008 while (mode != VOIDmode);
4010 return 0;
4013 /* This function is called when we are going to emit a compare instruction that
4014 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
4016 *PMODE is the mode of the inputs (in case they are const_int).
4017 *PUNSIGNEDP nonzero says that the operands are unsigned;
4018 this matters if they need to be widened (as given by METHODS).
4020 If they have mode BLKmode, then SIZE specifies the size of both operands.
4022 This function performs all the setup necessary so that the caller only has
4023 to emit a single comparison insn. This setup can involve doing a BLKmode
4024 comparison or emitting a library call to perform the comparison if no insn
4025 is available to handle it.
4026 The values which are passed in through pointers can be modified; the caller
4027 should perform the comparison on the modified values. Constant
4028 comparisons must have already been folded. */
4030 static void
4031 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4032 int unsignedp, enum optab_methods methods,
4033 rtx *ptest, enum machine_mode *pmode)
4035 enum machine_mode mode = *pmode;
4036 rtx libfunc, test;
4037 enum machine_mode cmp_mode;
4038 enum mode_class mclass;
4040 /* The other methods are not needed. */
4041 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4042 || methods == OPTAB_LIB_WIDEN);
4044 /* If we are optimizing, force expensive constants into a register. */
4045 if (CONSTANT_P (x) && optimize
4046 && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
4047 > COSTS_N_INSNS (1)))
4048 x = force_reg (mode, x);
4050 if (CONSTANT_P (y) && optimize
4051 && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
4052 > COSTS_N_INSNS (1)))
4053 y = force_reg (mode, y);
4055 #ifdef HAVE_cc0
4056 /* Make sure if we have a canonical comparison. The RTL
4057 documentation states that canonical comparisons are required only
4058 for targets which have cc0. */
4059 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4060 #endif
4062 /* Don't let both operands fail to indicate the mode. */
4063 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4064 x = force_reg (mode, x);
4065 if (mode == VOIDmode)
4066 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4068 /* Handle all BLKmode compares. */
4070 if (mode == BLKmode)
4072 enum machine_mode result_mode;
4073 enum insn_code cmp_code;
4074 tree length_type;
4075 rtx libfunc;
4076 rtx result;
4077 rtx opalign
4078 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4080 gcc_assert (size);
4082 /* Try to use a memory block compare insn - either cmpstr
4083 or cmpmem will do. */
4084 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4085 cmp_mode != VOIDmode;
4086 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4088 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4089 if (cmp_code == CODE_FOR_nothing)
4090 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4091 if (cmp_code == CODE_FOR_nothing)
4092 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4093 if (cmp_code == CODE_FOR_nothing)
4094 continue;
4096 /* Must make sure the size fits the insn's mode. */
4097 if ((CONST_INT_P (size)
4098 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4099 || (GET_MODE_BITSIZE (GET_MODE (size))
4100 > GET_MODE_BITSIZE (cmp_mode)))
4101 continue;
4103 result_mode = insn_data[cmp_code].operand[0].mode;
4104 result = gen_reg_rtx (result_mode);
4105 size = convert_to_mode (cmp_mode, size, 1);
4106 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4108 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4109 *pmode = result_mode;
4110 return;
4113 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4114 goto fail;
4116 /* Otherwise call a library function, memcmp. */
4117 libfunc = memcmp_libfunc;
4118 length_type = sizetype;
4119 result_mode = TYPE_MODE (integer_type_node);
4120 cmp_mode = TYPE_MODE (length_type);
4121 size = convert_to_mode (TYPE_MODE (length_type), size,
4122 TYPE_UNSIGNED (length_type));
4124 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4125 result_mode, 3,
4126 XEXP (x, 0), Pmode,
4127 XEXP (y, 0), Pmode,
4128 size, cmp_mode);
4129 x = result;
4130 y = const0_rtx;
4131 mode = result_mode;
4132 methods = OPTAB_LIB_WIDEN;
4133 unsignedp = false;
4136 /* Don't allow operands to the compare to trap, as that can put the
4137 compare and branch in different basic blocks. */
4138 if (cfun->can_throw_non_call_exceptions)
4140 if (may_trap_p (x))
4141 x = force_reg (mode, x);
4142 if (may_trap_p (y))
4143 y = force_reg (mode, y);
4146 if (GET_MODE_CLASS (mode) == MODE_CC)
4148 gcc_assert (can_compare_p (comparison, CCmode, ccp_jump));
4149 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4150 return;
4153 mclass = GET_MODE_CLASS (mode);
4154 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4155 cmp_mode = mode;
4158 enum insn_code icode;
4159 icode = optab_handler (cbranch_optab, cmp_mode);
4160 if (icode != CODE_FOR_nothing
4161 && insn_operand_matches (icode, 0, test))
4163 rtx last = get_last_insn ();
4164 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4165 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4166 if (op0 && op1
4167 && insn_operand_matches (icode, 1, op0)
4168 && insn_operand_matches (icode, 2, op1))
4170 XEXP (test, 0) = op0;
4171 XEXP (test, 1) = op1;
4172 *ptest = test;
4173 *pmode = cmp_mode;
4174 return;
4176 delete_insns_since (last);
4179 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4180 break;
4181 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4183 while (cmp_mode != VOIDmode);
4185 if (methods != OPTAB_LIB_WIDEN)
4186 goto fail;
4188 if (!SCALAR_FLOAT_MODE_P (mode))
4190 rtx result;
4191 enum machine_mode ret_mode;
4193 /* Handle a libcall just for the mode we are using. */
4194 libfunc = optab_libfunc (cmp_optab, mode);
4195 gcc_assert (libfunc);
4197 /* If we want unsigned, and this mode has a distinct unsigned
4198 comparison routine, use that. */
4199 if (unsignedp)
4201 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4202 if (ulibfunc)
4203 libfunc = ulibfunc;
4206 ret_mode = targetm.libgcc_cmp_return_mode ();
4207 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4208 ret_mode, 2, x, mode, y, mode);
4210 /* There are two kinds of comparison routines. Biased routines
4211 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4212 of gcc expect that the comparison operation is equivalent
4213 to the modified comparison. For signed comparisons compare the
4214 result against 1 in the biased case, and zero in the unbiased
4215 case. For unsigned comparisons always compare against 1 after
4216 biasing the unbiased result by adding 1. This gives us a way to
4217 represent LTU.
4218 The comparisons in the fixed-point helper library are always
4219 biased. */
4220 x = result;
4221 y = const1_rtx;
4223 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4225 if (unsignedp)
4226 x = plus_constant (ret_mode, result, 1);
4227 else
4228 y = const0_rtx;
4231 *pmode = word_mode;
4232 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4233 ptest, pmode);
4235 else
4236 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4238 return;
4240 fail:
4241 *ptest = NULL_RTX;
4244 /* Before emitting an insn with code ICODE, make sure that X, which is going
4245 to be used for operand OPNUM of the insn, is converted from mode MODE to
4246 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4247 that it is accepted by the operand predicate. Return the new value. */
4250 prepare_operand (enum insn_code icode, rtx x, int opnum, enum machine_mode mode,
4251 enum machine_mode wider_mode, int unsignedp)
4253 if (mode != wider_mode)
4254 x = convert_modes (wider_mode, mode, x, unsignedp);
4256 if (!insn_operand_matches (icode, opnum, x))
4258 if (reload_completed)
4259 return NULL_RTX;
4260 x = copy_to_mode_reg (insn_data[(int) icode].operand[opnum].mode, x);
4263 return x;
4266 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4267 we can do the branch. */
4269 static void
4270 emit_cmp_and_jump_insn_1 (rtx test, enum machine_mode mode, rtx label, int prob)
4272 enum machine_mode optab_mode;
4273 enum mode_class mclass;
4274 enum insn_code icode;
4275 rtx insn;
4277 mclass = GET_MODE_CLASS (mode);
4278 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4279 icode = optab_handler (cbranch_optab, optab_mode);
4281 gcc_assert (icode != CODE_FOR_nothing);
4282 gcc_assert (insn_operand_matches (icode, 0, test));
4283 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4284 XEXP (test, 1), label));
4285 if (prob != -1
4286 && profile_status != PROFILE_ABSENT
4287 && insn
4288 && JUMP_P (insn)
4289 && any_condjump_p (insn)
4290 && !find_reg_note (insn, REG_BR_PROB, 0))
4291 add_int_reg_note (insn, REG_BR_PROB, prob);
4294 /* Generate code to compare X with Y so that the condition codes are
4295 set and to jump to LABEL if the condition is true. If X is a
4296 constant and Y is not a constant, then the comparison is swapped to
4297 ensure that the comparison RTL has the canonical form.
4299 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4300 need to be widened. UNSIGNEDP is also used to select the proper
4301 branch condition code.
4303 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4305 MODE is the mode of the inputs (in case they are const_int).
4307 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4308 It will be potentially converted into an unsigned variant based on
4309 UNSIGNEDP to select a proper jump instruction.
4311 PROB is the probability of jumping to LABEL. */
4313 void
4314 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4315 enum machine_mode mode, int unsignedp, rtx label,
4316 int prob)
4318 rtx op0 = x, op1 = y;
4319 rtx test;
4321 /* Swap operands and condition to ensure canonical RTL. */
4322 if (swap_commutative_operands_p (x, y)
4323 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4325 op0 = y, op1 = x;
4326 comparison = swap_condition (comparison);
4329 /* If OP0 is still a constant, then both X and Y must be constants
4330 or the opposite comparison is not supported. Force X into a register
4331 to create canonical RTL. */
4332 if (CONSTANT_P (op0))
4333 op0 = force_reg (mode, op0);
4335 if (unsignedp)
4336 comparison = unsigned_condition (comparison);
4338 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4339 &test, &mode);
4340 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4344 /* Emit a library call comparison between floating point X and Y.
4345 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4347 static void
4348 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4349 rtx *ptest, enum machine_mode *pmode)
4351 enum rtx_code swapped = swap_condition (comparison);
4352 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4353 enum machine_mode orig_mode = GET_MODE (x);
4354 enum machine_mode mode, cmp_mode;
4355 rtx true_rtx, false_rtx;
4356 rtx value, target, insns, equiv;
4357 rtx libfunc = 0;
4358 bool reversed_p = false;
4359 cmp_mode = targetm.libgcc_cmp_return_mode ();
4361 for (mode = orig_mode;
4362 mode != VOIDmode;
4363 mode = GET_MODE_WIDER_MODE (mode))
4365 if (code_to_optab (comparison)
4366 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4367 break;
4369 if (code_to_optab (swapped)
4370 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4372 rtx tmp;
4373 tmp = x; x = y; y = tmp;
4374 comparison = swapped;
4375 break;
4378 if (code_to_optab (reversed)
4379 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4381 comparison = reversed;
4382 reversed_p = true;
4383 break;
4387 gcc_assert (mode != VOIDmode);
4389 if (mode != orig_mode)
4391 x = convert_to_mode (mode, x, 0);
4392 y = convert_to_mode (mode, y, 0);
4395 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4396 the RTL. The allows the RTL optimizers to delete the libcall if the
4397 condition can be determined at compile-time. */
4398 if (comparison == UNORDERED
4399 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4401 true_rtx = const_true_rtx;
4402 false_rtx = const0_rtx;
4404 else
4406 switch (comparison)
4408 case EQ:
4409 true_rtx = const0_rtx;
4410 false_rtx = const_true_rtx;
4411 break;
4413 case NE:
4414 true_rtx = const_true_rtx;
4415 false_rtx = const0_rtx;
4416 break;
4418 case GT:
4419 true_rtx = const1_rtx;
4420 false_rtx = const0_rtx;
4421 break;
4423 case GE:
4424 true_rtx = const0_rtx;
4425 false_rtx = constm1_rtx;
4426 break;
4428 case LT:
4429 true_rtx = constm1_rtx;
4430 false_rtx = const0_rtx;
4431 break;
4433 case LE:
4434 true_rtx = const0_rtx;
4435 false_rtx = const1_rtx;
4436 break;
4438 default:
4439 gcc_unreachable ();
4443 if (comparison == UNORDERED)
4445 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4446 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4447 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4448 temp, const_true_rtx, equiv);
4450 else
4452 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4453 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4454 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4455 equiv, true_rtx, false_rtx);
4458 start_sequence ();
4459 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4460 cmp_mode, 2, x, mode, y, mode);
4461 insns = get_insns ();
4462 end_sequence ();
4464 target = gen_reg_rtx (cmp_mode);
4465 emit_libcall_block (insns, target, value, equiv);
4467 if (comparison == UNORDERED
4468 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4469 || reversed_p)
4470 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4471 else
4472 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4474 *pmode = cmp_mode;
4477 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4479 void
4480 emit_indirect_jump (rtx loc)
4482 struct expand_operand ops[1];
4484 create_address_operand (&ops[0], loc);
4485 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4486 emit_barrier ();
4489 #ifdef HAVE_conditional_move
4491 /* Emit a conditional move instruction if the machine supports one for that
4492 condition and machine mode.
4494 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4495 the mode to use should they be constants. If it is VOIDmode, they cannot
4496 both be constants.
4498 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4499 should be stored there. MODE is the mode to use should they be constants.
4500 If it is VOIDmode, they cannot both be constants.
4502 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4503 is not supported. */
4506 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4507 enum machine_mode cmode, rtx op2, rtx op3,
4508 enum machine_mode mode, int unsignedp)
4510 rtx tem, comparison, last;
4511 enum insn_code icode;
4512 enum rtx_code reversed;
4514 /* If one operand is constant, make it the second one. Only do this
4515 if the other operand is not constant as well. */
4517 if (swap_commutative_operands_p (op0, op1))
4519 tem = op0;
4520 op0 = op1;
4521 op1 = tem;
4522 code = swap_condition (code);
4525 /* get_condition will prefer to generate LT and GT even if the old
4526 comparison was against zero, so undo that canonicalization here since
4527 comparisons against zero are cheaper. */
4528 if (code == LT && op1 == const1_rtx)
4529 code = LE, op1 = const0_rtx;
4530 else if (code == GT && op1 == constm1_rtx)
4531 code = GE, op1 = const0_rtx;
4533 if (cmode == VOIDmode)
4534 cmode = GET_MODE (op0);
4536 if (swap_commutative_operands_p (op2, op3)
4537 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4538 != UNKNOWN))
4540 tem = op2;
4541 op2 = op3;
4542 op3 = tem;
4543 code = reversed;
4546 if (mode == VOIDmode)
4547 mode = GET_MODE (op2);
4549 icode = direct_optab_handler (movcc_optab, mode);
4551 if (icode == CODE_FOR_nothing)
4552 return 0;
4554 if (!target)
4555 target = gen_reg_rtx (mode);
4557 code = unsignedp ? unsigned_condition (code) : code;
4558 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4560 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4561 return NULL and let the caller figure out how best to deal with this
4562 situation. */
4563 if (!COMPARISON_P (comparison))
4564 return NULL_RTX;
4566 do_pending_stack_adjust ();
4567 last = get_last_insn ();
4568 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4569 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4570 &comparison, &cmode);
4571 if (comparison)
4573 struct expand_operand ops[4];
4575 create_output_operand (&ops[0], target, mode);
4576 create_fixed_operand (&ops[1], comparison);
4577 create_input_operand (&ops[2], op2, mode);
4578 create_input_operand (&ops[3], op3, mode);
4579 if (maybe_expand_insn (icode, 4, ops))
4581 if (ops[0].value != target)
4582 convert_move (target, ops[0].value, false);
4583 return target;
4586 delete_insns_since (last);
4587 return NULL_RTX;
4590 /* Return nonzero if a conditional move of mode MODE is supported.
4592 This function is for combine so it can tell whether an insn that looks
4593 like a conditional move is actually supported by the hardware. If we
4594 guess wrong we lose a bit on optimization, but that's it. */
4595 /* ??? sparc64 supports conditionally moving integers values based on fp
4596 comparisons, and vice versa. How do we handle them? */
4599 can_conditionally_move_p (enum machine_mode mode)
4601 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4602 return 1;
4604 return 0;
4607 #endif /* HAVE_conditional_move */
4609 /* Emit a conditional addition instruction if the machine supports one for that
4610 condition and machine mode.
4612 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4613 the mode to use should they be constants. If it is VOIDmode, they cannot
4614 both be constants.
4616 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4617 should be stored there. MODE is the mode to use should they be constants.
4618 If it is VOIDmode, they cannot both be constants.
4620 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4621 is not supported. */
4624 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4625 enum machine_mode cmode, rtx op2, rtx op3,
4626 enum machine_mode mode, int unsignedp)
4628 rtx tem, comparison, last;
4629 enum insn_code icode;
4631 /* If one operand is constant, make it the second one. Only do this
4632 if the other operand is not constant as well. */
4634 if (swap_commutative_operands_p (op0, op1))
4636 tem = op0;
4637 op0 = op1;
4638 op1 = tem;
4639 code = swap_condition (code);
4642 /* get_condition will prefer to generate LT and GT even if the old
4643 comparison was against zero, so undo that canonicalization here since
4644 comparisons against zero are cheaper. */
4645 if (code == LT && op1 == const1_rtx)
4646 code = LE, op1 = const0_rtx;
4647 else if (code == GT && op1 == constm1_rtx)
4648 code = GE, op1 = const0_rtx;
4650 if (cmode == VOIDmode)
4651 cmode = GET_MODE (op0);
4653 if (mode == VOIDmode)
4654 mode = GET_MODE (op2);
4656 icode = optab_handler (addcc_optab, mode);
4658 if (icode == CODE_FOR_nothing)
4659 return 0;
4661 if (!target)
4662 target = gen_reg_rtx (mode);
4664 code = unsignedp ? unsigned_condition (code) : code;
4665 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4667 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4668 return NULL and let the caller figure out how best to deal with this
4669 situation. */
4670 if (!COMPARISON_P (comparison))
4671 return NULL_RTX;
4673 do_pending_stack_adjust ();
4674 last = get_last_insn ();
4675 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4676 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4677 &comparison, &cmode);
4678 if (comparison)
4680 struct expand_operand ops[4];
4682 create_output_operand (&ops[0], target, mode);
4683 create_fixed_operand (&ops[1], comparison);
4684 create_input_operand (&ops[2], op2, mode);
4685 create_input_operand (&ops[3], op3, mode);
4686 if (maybe_expand_insn (icode, 4, ops))
4688 if (ops[0].value != target)
4689 convert_move (target, ops[0].value, false);
4690 return target;
4693 delete_insns_since (last);
4694 return NULL_RTX;
4697 /* These functions attempt to generate an insn body, rather than
4698 emitting the insn, but if the gen function already emits them, we
4699 make no attempt to turn them back into naked patterns. */
4701 /* Generate and return an insn body to add Y to X. */
4704 gen_add2_insn (rtx x, rtx y)
4706 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4708 gcc_assert (insn_operand_matches (icode, 0, x));
4709 gcc_assert (insn_operand_matches (icode, 1, x));
4710 gcc_assert (insn_operand_matches (icode, 2, y));
4712 return GEN_FCN (icode) (x, x, y);
4715 /* Generate and return an insn body to add r1 and c,
4716 storing the result in r0. */
4719 gen_add3_insn (rtx r0, rtx r1, rtx c)
4721 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4723 if (icode == CODE_FOR_nothing
4724 || !insn_operand_matches (icode, 0, r0)
4725 || !insn_operand_matches (icode, 1, r1)
4726 || !insn_operand_matches (icode, 2, c))
4727 return NULL_RTX;
4729 return GEN_FCN (icode) (r0, r1, c);
4733 have_add2_insn (rtx x, rtx y)
4735 enum insn_code icode;
4737 gcc_assert (GET_MODE (x) != VOIDmode);
4739 icode = optab_handler (add_optab, GET_MODE (x));
4741 if (icode == CODE_FOR_nothing)
4742 return 0;
4744 if (!insn_operand_matches (icode, 0, x)
4745 || !insn_operand_matches (icode, 1, x)
4746 || !insn_operand_matches (icode, 2, y))
4747 return 0;
4749 return 1;
4752 /* Generate and return an insn body to subtract Y from X. */
4755 gen_sub2_insn (rtx x, rtx y)
4757 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4759 gcc_assert (insn_operand_matches (icode, 0, x));
4760 gcc_assert (insn_operand_matches (icode, 1, x));
4761 gcc_assert (insn_operand_matches (icode, 2, y));
4763 return GEN_FCN (icode) (x, x, y);
4766 /* Generate and return an insn body to subtract r1 and c,
4767 storing the result in r0. */
4770 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4772 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4774 if (icode == CODE_FOR_nothing
4775 || !insn_operand_matches (icode, 0, r0)
4776 || !insn_operand_matches (icode, 1, r1)
4777 || !insn_operand_matches (icode, 2, c))
4778 return NULL_RTX;
4780 return GEN_FCN (icode) (r0, r1, c);
4784 have_sub2_insn (rtx x, rtx y)
4786 enum insn_code icode;
4788 gcc_assert (GET_MODE (x) != VOIDmode);
4790 icode = optab_handler (sub_optab, GET_MODE (x));
4792 if (icode == CODE_FOR_nothing)
4793 return 0;
4795 if (!insn_operand_matches (icode, 0, x)
4796 || !insn_operand_matches (icode, 1, x)
4797 || !insn_operand_matches (icode, 2, y))
4798 return 0;
4800 return 1;
4803 /* Generate the body of an instruction to copy Y into X.
4804 It may be a list of insns, if one insn isn't enough. */
4807 gen_move_insn (rtx x, rtx y)
4809 rtx seq;
4811 start_sequence ();
4812 emit_move_insn_1 (x, y);
4813 seq = get_insns ();
4814 end_sequence ();
4815 return seq;
4818 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4819 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4820 no such operation exists, CODE_FOR_nothing will be returned. */
4822 enum insn_code
4823 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
4824 int unsignedp)
4826 convert_optab tab;
4827 #ifdef HAVE_ptr_extend
4828 if (unsignedp < 0)
4829 return CODE_FOR_ptr_extend;
4830 #endif
4832 tab = unsignedp ? zext_optab : sext_optab;
4833 return convert_optab_handler (tab, to_mode, from_mode);
4836 /* Generate the body of an insn to extend Y (with mode MFROM)
4837 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4840 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4841 enum machine_mode mfrom, int unsignedp)
4843 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4844 return GEN_FCN (icode) (x, y);
4847 /* can_fix_p and can_float_p say whether the target machine
4848 can directly convert a given fixed point type to
4849 a given floating point type, or vice versa.
4850 The returned value is the CODE_FOR_... value to use,
4851 or CODE_FOR_nothing if these modes cannot be directly converted.
4853 *TRUNCP_PTR is set to 1 if it is necessary to output
4854 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4856 static enum insn_code
4857 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4858 int unsignedp, int *truncp_ptr)
4860 convert_optab tab;
4861 enum insn_code icode;
4863 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4864 icode = convert_optab_handler (tab, fixmode, fltmode);
4865 if (icode != CODE_FOR_nothing)
4867 *truncp_ptr = 0;
4868 return icode;
4871 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4872 for this to work. We need to rework the fix* and ftrunc* patterns
4873 and documentation. */
4874 tab = unsignedp ? ufix_optab : sfix_optab;
4875 icode = convert_optab_handler (tab, fixmode, fltmode);
4876 if (icode != CODE_FOR_nothing
4877 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4879 *truncp_ptr = 1;
4880 return icode;
4883 *truncp_ptr = 0;
4884 return CODE_FOR_nothing;
4887 enum insn_code
4888 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4889 int unsignedp)
4891 convert_optab tab;
4893 tab = unsignedp ? ufloat_optab : sfloat_optab;
4894 return convert_optab_handler (tab, fltmode, fixmode);
4897 /* Function supportable_convert_operation
4899 Check whether an operation represented by the code CODE is a
4900 convert operation that is supported by the target platform in
4901 vector form (i.e., when operating on arguments of type VECTYPE_IN
4902 producing a result of type VECTYPE_OUT).
4904 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4905 This function checks if these operations are supported
4906 by the target platform either directly (via vector tree-codes), or via
4907 target builtins.
4909 Output:
4910 - CODE1 is code of vector operation to be used when
4911 vectorizing the operation, if available.
4912 - DECL is decl of target builtin functions to be used
4913 when vectorizing the operation, if available. In this case,
4914 CODE1 is CALL_EXPR. */
4916 bool
4917 supportable_convert_operation (enum tree_code code,
4918 tree vectype_out, tree vectype_in,
4919 tree *decl, enum tree_code *code1)
4921 enum machine_mode m1,m2;
4922 int truncp;
4924 m1 = TYPE_MODE (vectype_out);
4925 m2 = TYPE_MODE (vectype_in);
4927 /* First check if we can done conversion directly. */
4928 if ((code == FIX_TRUNC_EXPR
4929 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
4930 != CODE_FOR_nothing)
4931 || (code == FLOAT_EXPR
4932 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
4933 != CODE_FOR_nothing))
4935 *code1 = code;
4936 return true;
4939 /* Now check for builtin. */
4940 if (targetm.vectorize.builtin_conversion
4941 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
4943 *code1 = CALL_EXPR;
4944 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
4945 return true;
4947 return false;
4951 /* Generate code to convert FROM to floating point
4952 and store in TO. FROM must be fixed point and not VOIDmode.
4953 UNSIGNEDP nonzero means regard FROM as unsigned.
4954 Normally this is done by correcting the final value
4955 if it is negative. */
4957 void
4958 expand_float (rtx to, rtx from, int unsignedp)
4960 enum insn_code icode;
4961 rtx target = to;
4962 enum machine_mode fmode, imode;
4963 bool can_do_signed = false;
4965 /* Crash now, because we won't be able to decide which mode to use. */
4966 gcc_assert (GET_MODE (from) != VOIDmode);
4968 /* Look for an insn to do the conversion. Do it in the specified
4969 modes if possible; otherwise convert either input, output or both to
4970 wider mode. If the integer mode is wider than the mode of FROM,
4971 we can do the conversion signed even if the input is unsigned. */
4973 for (fmode = GET_MODE (to); fmode != VOIDmode;
4974 fmode = GET_MODE_WIDER_MODE (fmode))
4975 for (imode = GET_MODE (from); imode != VOIDmode;
4976 imode = GET_MODE_WIDER_MODE (imode))
4978 int doing_unsigned = unsignedp;
4980 if (fmode != GET_MODE (to)
4981 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
4982 continue;
4984 icode = can_float_p (fmode, imode, unsignedp);
4985 if (icode == CODE_FOR_nothing && unsignedp)
4987 enum insn_code scode = can_float_p (fmode, imode, 0);
4988 if (scode != CODE_FOR_nothing)
4989 can_do_signed = true;
4990 if (imode != GET_MODE (from))
4991 icode = scode, doing_unsigned = 0;
4994 if (icode != CODE_FOR_nothing)
4996 if (imode != GET_MODE (from))
4997 from = convert_to_mode (imode, from, unsignedp);
4999 if (fmode != GET_MODE (to))
5000 target = gen_reg_rtx (fmode);
5002 emit_unop_insn (icode, target, from,
5003 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
5005 if (target != to)
5006 convert_move (to, target, 0);
5007 return;
5011 /* Unsigned integer, and no way to convert directly. Convert as signed,
5012 then unconditionally adjust the result. */
5013 if (unsignedp && can_do_signed)
5015 rtx label = gen_label_rtx ();
5016 rtx temp;
5017 REAL_VALUE_TYPE offset;
5019 /* Look for a usable floating mode FMODE wider than the source and at
5020 least as wide as the target. Using FMODE will avoid rounding woes
5021 with unsigned values greater than the signed maximum value. */
5023 for (fmode = GET_MODE (to); fmode != VOIDmode;
5024 fmode = GET_MODE_WIDER_MODE (fmode))
5025 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
5026 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
5027 break;
5029 if (fmode == VOIDmode)
5031 /* There is no such mode. Pretend the target is wide enough. */
5032 fmode = GET_MODE (to);
5034 /* Avoid double-rounding when TO is narrower than FROM. */
5035 if ((significand_size (fmode) + 1)
5036 < GET_MODE_PRECISION (GET_MODE (from)))
5038 rtx temp1;
5039 rtx neglabel = gen_label_rtx ();
5041 /* Don't use TARGET if it isn't a register, is a hard register,
5042 or is the wrong mode. */
5043 if (!REG_P (target)
5044 || REGNO (target) < FIRST_PSEUDO_REGISTER
5045 || GET_MODE (target) != fmode)
5046 target = gen_reg_rtx (fmode);
5048 imode = GET_MODE (from);
5049 do_pending_stack_adjust ();
5051 /* Test whether the sign bit is set. */
5052 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5053 0, neglabel);
5055 /* The sign bit is not set. Convert as signed. */
5056 expand_float (target, from, 0);
5057 emit_jump_insn (gen_jump (label));
5058 emit_barrier ();
5060 /* The sign bit is set.
5061 Convert to a usable (positive signed) value by shifting right
5062 one bit, while remembering if a nonzero bit was shifted
5063 out; i.e., compute (from & 1) | (from >> 1). */
5065 emit_label (neglabel);
5066 temp = expand_binop (imode, and_optab, from, const1_rtx,
5067 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5068 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5069 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5070 OPTAB_LIB_WIDEN);
5071 expand_float (target, temp, 0);
5073 /* Multiply by 2 to undo the shift above. */
5074 temp = expand_binop (fmode, add_optab, target, target,
5075 target, 0, OPTAB_LIB_WIDEN);
5076 if (temp != target)
5077 emit_move_insn (target, temp);
5079 do_pending_stack_adjust ();
5080 emit_label (label);
5081 goto done;
5085 /* If we are about to do some arithmetic to correct for an
5086 unsigned operand, do it in a pseudo-register. */
5088 if (GET_MODE (to) != fmode
5089 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5090 target = gen_reg_rtx (fmode);
5092 /* Convert as signed integer to floating. */
5093 expand_float (target, from, 0);
5095 /* If FROM is negative (and therefore TO is negative),
5096 correct its value by 2**bitwidth. */
5098 do_pending_stack_adjust ();
5099 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5100 0, label);
5103 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5104 temp = expand_binop (fmode, add_optab, target,
5105 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5106 target, 0, OPTAB_LIB_WIDEN);
5107 if (temp != target)
5108 emit_move_insn (target, temp);
5110 do_pending_stack_adjust ();
5111 emit_label (label);
5112 goto done;
5115 /* No hardware instruction available; call a library routine. */
5117 rtx libfunc;
5118 rtx insns;
5119 rtx value;
5120 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5122 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
5123 from = convert_to_mode (SImode, from, unsignedp);
5125 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5126 gcc_assert (libfunc);
5128 start_sequence ();
5130 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5131 GET_MODE (to), 1, from,
5132 GET_MODE (from));
5133 insns = get_insns ();
5134 end_sequence ();
5136 emit_libcall_block (insns, target, value,
5137 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5138 GET_MODE (to), from));
5141 done:
5143 /* Copy result to requested destination
5144 if we have been computing in a temp location. */
5146 if (target != to)
5148 if (GET_MODE (target) == GET_MODE (to))
5149 emit_move_insn (to, target);
5150 else
5151 convert_move (to, target, 0);
5155 /* Generate code to convert FROM to fixed point and store in TO. FROM
5156 must be floating point. */
5158 void
5159 expand_fix (rtx to, rtx from, int unsignedp)
5161 enum insn_code icode;
5162 rtx target = to;
5163 enum machine_mode fmode, imode;
5164 int must_trunc = 0;
5166 /* We first try to find a pair of modes, one real and one integer, at
5167 least as wide as FROM and TO, respectively, in which we can open-code
5168 this conversion. If the integer mode is wider than the mode of TO,
5169 we can do the conversion either signed or unsigned. */
5171 for (fmode = GET_MODE (from); fmode != VOIDmode;
5172 fmode = GET_MODE_WIDER_MODE (fmode))
5173 for (imode = GET_MODE (to); imode != VOIDmode;
5174 imode = GET_MODE_WIDER_MODE (imode))
5176 int doing_unsigned = unsignedp;
5178 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5179 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5180 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5182 if (icode != CODE_FOR_nothing)
5184 rtx last = get_last_insn ();
5185 if (fmode != GET_MODE (from))
5186 from = convert_to_mode (fmode, from, 0);
5188 if (must_trunc)
5190 rtx temp = gen_reg_rtx (GET_MODE (from));
5191 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5192 temp, 0);
5195 if (imode != GET_MODE (to))
5196 target = gen_reg_rtx (imode);
5198 if (maybe_emit_unop_insn (icode, target, from,
5199 doing_unsigned ? UNSIGNED_FIX : FIX))
5201 if (target != to)
5202 convert_move (to, target, unsignedp);
5203 return;
5205 delete_insns_since (last);
5209 /* For an unsigned conversion, there is one more way to do it.
5210 If we have a signed conversion, we generate code that compares
5211 the real value to the largest representable positive number. If if
5212 is smaller, the conversion is done normally. Otherwise, subtract
5213 one plus the highest signed number, convert, and add it back.
5215 We only need to check all real modes, since we know we didn't find
5216 anything with a wider integer mode.
5218 This code used to extend FP value into mode wider than the destination.
5219 This is needed for decimal float modes which cannot accurately
5220 represent one plus the highest signed number of the same size, but
5221 not for binary modes. Consider, for instance conversion from SFmode
5222 into DImode.
5224 The hot path through the code is dealing with inputs smaller than 2^63
5225 and doing just the conversion, so there is no bits to lose.
5227 In the other path we know the value is positive in the range 2^63..2^64-1
5228 inclusive. (as for other input overflow happens and result is undefined)
5229 So we know that the most important bit set in mantissa corresponds to
5230 2^63. The subtraction of 2^63 should not generate any rounding as it
5231 simply clears out that bit. The rest is trivial. */
5233 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5234 for (fmode = GET_MODE (from); fmode != VOIDmode;
5235 fmode = GET_MODE_WIDER_MODE (fmode))
5236 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5237 && (!DECIMAL_FLOAT_MODE_P (fmode)
5238 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5240 int bitsize;
5241 REAL_VALUE_TYPE offset;
5242 rtx limit, lab1, lab2, insn;
5244 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5245 real_2expN (&offset, bitsize - 1, fmode);
5246 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5247 lab1 = gen_label_rtx ();
5248 lab2 = gen_label_rtx ();
5250 if (fmode != GET_MODE (from))
5251 from = convert_to_mode (fmode, from, 0);
5253 /* See if we need to do the subtraction. */
5254 do_pending_stack_adjust ();
5255 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5256 0, lab1);
5258 /* If not, do the signed "fix" and branch around fixup code. */
5259 expand_fix (to, from, 0);
5260 emit_jump_insn (gen_jump (lab2));
5261 emit_barrier ();
5263 /* Otherwise, subtract 2**(N-1), convert to signed number,
5264 then add 2**(N-1). Do the addition using XOR since this
5265 will often generate better code. */
5266 emit_label (lab1);
5267 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5268 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5269 expand_fix (to, target, 0);
5270 target = expand_binop (GET_MODE (to), xor_optab, to,
5271 gen_int_mode
5272 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5273 GET_MODE (to)),
5274 to, 1, OPTAB_LIB_WIDEN);
5276 if (target != to)
5277 emit_move_insn (to, target);
5279 emit_label (lab2);
5281 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5283 /* Make a place for a REG_NOTE and add it. */
5284 insn = emit_move_insn (to, to);
5285 set_dst_reg_note (insn, REG_EQUAL,
5286 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5287 copy_rtx (from)),
5288 to);
5291 return;
5294 /* We can't do it with an insn, so use a library call. But first ensure
5295 that the mode of TO is at least as wide as SImode, since those are the
5296 only library calls we know about. */
5298 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
5300 target = gen_reg_rtx (SImode);
5302 expand_fix (target, from, unsignedp);
5304 else
5306 rtx insns;
5307 rtx value;
5308 rtx libfunc;
5310 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5311 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5312 gcc_assert (libfunc);
5314 start_sequence ();
5316 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5317 GET_MODE (to), 1, from,
5318 GET_MODE (from));
5319 insns = get_insns ();
5320 end_sequence ();
5322 emit_libcall_block (insns, target, value,
5323 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5324 GET_MODE (to), from));
5327 if (target != to)
5329 if (GET_MODE (to) == GET_MODE (target))
5330 emit_move_insn (to, target);
5331 else
5332 convert_move (to, target, 0);
5336 /* Generate code to convert FROM or TO a fixed-point.
5337 If UINTP is true, either TO or FROM is an unsigned integer.
5338 If SATP is true, we need to saturate the result. */
5340 void
5341 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5343 enum machine_mode to_mode = GET_MODE (to);
5344 enum machine_mode from_mode = GET_MODE (from);
5345 convert_optab tab;
5346 enum rtx_code this_code;
5347 enum insn_code code;
5348 rtx insns, value;
5349 rtx libfunc;
5351 if (to_mode == from_mode)
5353 emit_move_insn (to, from);
5354 return;
5357 if (uintp)
5359 tab = satp ? satfractuns_optab : fractuns_optab;
5360 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5362 else
5364 tab = satp ? satfract_optab : fract_optab;
5365 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5367 code = convert_optab_handler (tab, to_mode, from_mode);
5368 if (code != CODE_FOR_nothing)
5370 emit_unop_insn (code, to, from, this_code);
5371 return;
5374 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5375 gcc_assert (libfunc);
5377 start_sequence ();
5378 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5379 1, from, from_mode);
5380 insns = get_insns ();
5381 end_sequence ();
5383 emit_libcall_block (insns, to, value,
5384 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5387 /* Generate code to convert FROM to fixed point and store in TO. FROM
5388 must be floating point, TO must be signed. Use the conversion optab
5389 TAB to do the conversion. */
5391 bool
5392 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5394 enum insn_code icode;
5395 rtx target = to;
5396 enum machine_mode fmode, imode;
5398 /* We first try to find a pair of modes, one real and one integer, at
5399 least as wide as FROM and TO, respectively, in which we can open-code
5400 this conversion. If the integer mode is wider than the mode of TO,
5401 we can do the conversion either signed or unsigned. */
5403 for (fmode = GET_MODE (from); fmode != VOIDmode;
5404 fmode = GET_MODE_WIDER_MODE (fmode))
5405 for (imode = GET_MODE (to); imode != VOIDmode;
5406 imode = GET_MODE_WIDER_MODE (imode))
5408 icode = convert_optab_handler (tab, imode, fmode);
5409 if (icode != CODE_FOR_nothing)
5411 rtx last = get_last_insn ();
5412 if (fmode != GET_MODE (from))
5413 from = convert_to_mode (fmode, from, 0);
5415 if (imode != GET_MODE (to))
5416 target = gen_reg_rtx (imode);
5418 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5420 delete_insns_since (last);
5421 continue;
5423 if (target != to)
5424 convert_move (to, target, 0);
5425 return true;
5429 return false;
5432 /* Report whether we have an instruction to perform the operation
5433 specified by CODE on operands of mode MODE. */
5435 have_insn_for (enum rtx_code code, enum machine_mode mode)
5437 return (code_to_optab (code)
5438 && (optab_handler (code_to_optab (code), mode)
5439 != CODE_FOR_nothing));
5442 /* Initialize the libfunc fields of an entire group of entries in some
5443 optab. Each entry is set equal to a string consisting of a leading
5444 pair of underscores followed by a generic operation name followed by
5445 a mode name (downshifted to lowercase) followed by a single character
5446 representing the number of operands for the given operation (which is
5447 usually one of the characters '2', '3', or '4').
5449 OPTABLE is the table in which libfunc fields are to be initialized.
5450 OPNAME is the generic (string) name of the operation.
5451 SUFFIX is the character which specifies the number of operands for
5452 the given generic operation.
5453 MODE is the mode to generate for.
5456 static void
5457 gen_libfunc (optab optable, const char *opname, int suffix,
5458 enum machine_mode mode)
5460 unsigned opname_len = strlen (opname);
5461 const char *mname = GET_MODE_NAME (mode);
5462 unsigned mname_len = strlen (mname);
5463 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5464 int len = prefix_len + opname_len + mname_len + 1 + 1;
5465 char *libfunc_name = XALLOCAVEC (char, len);
5466 char *p;
5467 const char *q;
5469 p = libfunc_name;
5470 *p++ = '_';
5471 *p++ = '_';
5472 if (targetm.libfunc_gnu_prefix)
5474 *p++ = 'g';
5475 *p++ = 'n';
5476 *p++ = 'u';
5477 *p++ = '_';
5479 for (q = opname; *q; )
5480 *p++ = *q++;
5481 for (q = mname; *q; q++)
5482 *p++ = TOLOWER (*q);
5483 *p++ = suffix;
5484 *p = '\0';
5486 set_optab_libfunc (optable, mode,
5487 ggc_alloc_string (libfunc_name, p - libfunc_name));
5490 /* Like gen_libfunc, but verify that integer operation is involved. */
5492 void
5493 gen_int_libfunc (optab optable, const char *opname, char suffix,
5494 enum machine_mode mode)
5496 int maxsize = 2 * BITS_PER_WORD;
5498 if (GET_MODE_CLASS (mode) != MODE_INT)
5499 return;
5500 if (maxsize < LONG_LONG_TYPE_SIZE)
5501 maxsize = LONG_LONG_TYPE_SIZE;
5502 if (GET_MODE_CLASS (mode) != MODE_INT
5503 || mode < word_mode || GET_MODE_BITSIZE (mode) > maxsize)
5504 return;
5505 gen_libfunc (optable, opname, suffix, mode);
5508 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5510 void
5511 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5512 enum machine_mode mode)
5514 char *dec_opname;
5516 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5517 gen_libfunc (optable, opname, suffix, mode);
5518 if (DECIMAL_FLOAT_MODE_P (mode))
5520 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5521 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5522 depending on the low level floating format used. */
5523 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5524 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5525 gen_libfunc (optable, dec_opname, suffix, mode);
5529 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5531 void
5532 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5533 enum machine_mode mode)
5535 if (!ALL_FIXED_POINT_MODE_P (mode))
5536 return;
5537 gen_libfunc (optable, opname, suffix, mode);
5540 /* Like gen_libfunc, but verify that signed fixed-point operation is
5541 involved. */
5543 void
5544 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5545 enum machine_mode mode)
5547 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5548 return;
5549 gen_libfunc (optable, opname, suffix, mode);
5552 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5553 involved. */
5555 void
5556 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5557 enum machine_mode mode)
5559 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5560 return;
5561 gen_libfunc (optable, opname, suffix, mode);
5564 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5566 void
5567 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5568 enum machine_mode mode)
5570 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5571 gen_fp_libfunc (optable, name, suffix, mode);
5572 if (INTEGRAL_MODE_P (mode))
5573 gen_int_libfunc (optable, name, suffix, mode);
5576 /* Like gen_libfunc, but verify that FP or INT operation is involved
5577 and add 'v' suffix for integer operation. */
5579 void
5580 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5581 enum machine_mode mode)
5583 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5584 gen_fp_libfunc (optable, name, suffix, mode);
5585 if (GET_MODE_CLASS (mode) == MODE_INT)
5587 int len = strlen (name);
5588 char *v_name = XALLOCAVEC (char, len + 2);
5589 strcpy (v_name, name);
5590 v_name[len] = 'v';
5591 v_name[len + 1] = 0;
5592 gen_int_libfunc (optable, v_name, suffix, mode);
5596 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5597 involved. */
5599 void
5600 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5601 enum machine_mode mode)
5603 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5604 gen_fp_libfunc (optable, name, suffix, mode);
5605 if (INTEGRAL_MODE_P (mode))
5606 gen_int_libfunc (optable, name, suffix, mode);
5607 if (ALL_FIXED_POINT_MODE_P (mode))
5608 gen_fixed_libfunc (optable, name, suffix, mode);
5611 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5612 involved. */
5614 void
5615 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5616 enum machine_mode mode)
5618 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5619 gen_fp_libfunc (optable, name, suffix, mode);
5620 if (INTEGRAL_MODE_P (mode))
5621 gen_int_libfunc (optable, name, suffix, mode);
5622 if (SIGNED_FIXED_POINT_MODE_P (mode))
5623 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5626 /* Like gen_libfunc, but verify that INT or FIXED operation is
5627 involved. */
5629 void
5630 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5631 enum machine_mode mode)
5633 if (INTEGRAL_MODE_P (mode))
5634 gen_int_libfunc (optable, name, suffix, mode);
5635 if (ALL_FIXED_POINT_MODE_P (mode))
5636 gen_fixed_libfunc (optable, name, suffix, mode);
5639 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5640 involved. */
5642 void
5643 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5644 enum machine_mode mode)
5646 if (INTEGRAL_MODE_P (mode))
5647 gen_int_libfunc (optable, name, suffix, mode);
5648 if (SIGNED_FIXED_POINT_MODE_P (mode))
5649 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5652 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5653 involved. */
5655 void
5656 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5657 enum machine_mode mode)
5659 if (INTEGRAL_MODE_P (mode))
5660 gen_int_libfunc (optable, name, suffix, mode);
5661 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5662 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5665 /* Initialize the libfunc fields of an entire group of entries of an
5666 inter-mode-class conversion optab. The string formation rules are
5667 similar to the ones for init_libfuncs, above, but instead of having
5668 a mode name and an operand count these functions have two mode names
5669 and no operand count. */
5671 void
5672 gen_interclass_conv_libfunc (convert_optab tab,
5673 const char *opname,
5674 enum machine_mode tmode,
5675 enum machine_mode fmode)
5677 size_t opname_len = strlen (opname);
5678 size_t mname_len = 0;
5680 const char *fname, *tname;
5681 const char *q;
5682 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5683 char *libfunc_name, *suffix;
5684 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5685 char *p;
5687 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5688 depends on which underlying decimal floating point format is used. */
5689 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5691 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5693 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5694 nondec_name[0] = '_';
5695 nondec_name[1] = '_';
5696 if (targetm.libfunc_gnu_prefix)
5698 nondec_name[2] = 'g';
5699 nondec_name[3] = 'n';
5700 nondec_name[4] = 'u';
5701 nondec_name[5] = '_';
5704 memcpy (&nondec_name[prefix_len], opname, opname_len);
5705 nondec_suffix = nondec_name + opname_len + prefix_len;
5707 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5708 dec_name[0] = '_';
5709 dec_name[1] = '_';
5710 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5711 memcpy (&dec_name[2+dec_len], opname, opname_len);
5712 dec_suffix = dec_name + dec_len + opname_len + 2;
5714 fname = GET_MODE_NAME (fmode);
5715 tname = GET_MODE_NAME (tmode);
5717 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5719 libfunc_name = dec_name;
5720 suffix = dec_suffix;
5722 else
5724 libfunc_name = nondec_name;
5725 suffix = nondec_suffix;
5728 p = suffix;
5729 for (q = fname; *q; p++, q++)
5730 *p = TOLOWER (*q);
5731 for (q = tname; *q; p++, q++)
5732 *p = TOLOWER (*q);
5734 *p = '\0';
5736 set_conv_libfunc (tab, tmode, fmode,
5737 ggc_alloc_string (libfunc_name, p - libfunc_name));
5740 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5741 int->fp conversion. */
5743 void
5744 gen_int_to_fp_conv_libfunc (convert_optab tab,
5745 const char *opname,
5746 enum machine_mode tmode,
5747 enum machine_mode fmode)
5749 if (GET_MODE_CLASS (fmode) != MODE_INT)
5750 return;
5751 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5752 return;
5753 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5756 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5757 naming scheme. */
5759 void
5760 gen_ufloat_conv_libfunc (convert_optab tab,
5761 const char *opname ATTRIBUTE_UNUSED,
5762 enum machine_mode tmode,
5763 enum machine_mode fmode)
5765 if (DECIMAL_FLOAT_MODE_P (tmode))
5766 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5767 else
5768 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5771 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5772 fp->int conversion. */
5774 void
5775 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5776 const char *opname,
5777 enum machine_mode tmode,
5778 enum machine_mode fmode)
5780 if (GET_MODE_CLASS (fmode) != MODE_INT)
5781 return;
5782 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5783 return;
5784 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5787 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5788 fp->int conversion with no decimal floating point involved. */
5790 void
5791 gen_fp_to_int_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_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5797 return;
5798 if (GET_MODE_CLASS (tmode) != MODE_INT)
5799 return;
5800 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5803 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5804 The string formation rules are
5805 similar to the ones for init_libfunc, above. */
5807 void
5808 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5809 enum machine_mode tmode, enum machine_mode fmode)
5811 size_t opname_len = strlen (opname);
5812 size_t mname_len = 0;
5814 const char *fname, *tname;
5815 const char *q;
5816 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5817 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5818 char *libfunc_name, *suffix;
5819 char *p;
5821 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5822 depends on which underlying decimal floating point format is used. */
5823 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5825 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5827 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5828 nondec_name[0] = '_';
5829 nondec_name[1] = '_';
5830 if (targetm.libfunc_gnu_prefix)
5832 nondec_name[2] = 'g';
5833 nondec_name[3] = 'n';
5834 nondec_name[4] = 'u';
5835 nondec_name[5] = '_';
5837 memcpy (&nondec_name[prefix_len], opname, opname_len);
5838 nondec_suffix = nondec_name + opname_len + prefix_len;
5840 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5841 dec_name[0] = '_';
5842 dec_name[1] = '_';
5843 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5844 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5845 dec_suffix = dec_name + dec_len + opname_len + 2;
5847 fname = GET_MODE_NAME (fmode);
5848 tname = GET_MODE_NAME (tmode);
5850 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5852 libfunc_name = dec_name;
5853 suffix = dec_suffix;
5855 else
5857 libfunc_name = nondec_name;
5858 suffix = nondec_suffix;
5861 p = suffix;
5862 for (q = fname; *q; p++, q++)
5863 *p = TOLOWER (*q);
5864 for (q = tname; *q; p++, q++)
5865 *p = TOLOWER (*q);
5867 *p++ = '2';
5868 *p = '\0';
5870 set_conv_libfunc (tab, tmode, fmode,
5871 ggc_alloc_string (libfunc_name, p - libfunc_name));
5874 /* Pick proper libcall for trunc_optab. We need to chose if we do
5875 truncation or extension and interclass or intraclass. */
5877 void
5878 gen_trunc_conv_libfunc (convert_optab tab,
5879 const char *opname,
5880 enum machine_mode tmode,
5881 enum machine_mode fmode)
5883 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5884 return;
5885 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5886 return;
5887 if (tmode == fmode)
5888 return;
5890 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5891 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5892 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5894 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5895 return;
5897 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5898 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5899 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5900 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5903 /* Pick proper libcall for extend_optab. We need to chose if we do
5904 truncation or extension and interclass or intraclass. */
5906 void
5907 gen_extend_conv_libfunc (convert_optab tab,
5908 const char *opname ATTRIBUTE_UNUSED,
5909 enum machine_mode tmode,
5910 enum machine_mode fmode)
5912 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5913 return;
5914 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5915 return;
5916 if (tmode == fmode)
5917 return;
5919 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5920 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5921 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5923 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5924 return;
5926 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5927 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5928 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5929 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5932 /* Pick proper libcall for fract_optab. We need to chose if we do
5933 interclass or intraclass. */
5935 void
5936 gen_fract_conv_libfunc (convert_optab tab,
5937 const char *opname,
5938 enum machine_mode tmode,
5939 enum machine_mode fmode)
5941 if (tmode == fmode)
5942 return;
5943 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5944 return;
5946 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5947 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5948 else
5949 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5952 /* Pick proper libcall for fractuns_optab. */
5954 void
5955 gen_fractuns_conv_libfunc (convert_optab tab,
5956 const char *opname,
5957 enum machine_mode tmode,
5958 enum machine_mode fmode)
5960 if (tmode == fmode)
5961 return;
5962 /* One mode must be a fixed-point mode, and the other must be an integer
5963 mode. */
5964 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
5965 || (ALL_FIXED_POINT_MODE_P (fmode)
5966 && GET_MODE_CLASS (tmode) == MODE_INT)))
5967 return;
5969 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5972 /* Pick proper libcall for satfract_optab. We need to chose if we do
5973 interclass or intraclass. */
5975 void
5976 gen_satfract_conv_libfunc (convert_optab tab,
5977 const char *opname,
5978 enum machine_mode tmode,
5979 enum machine_mode fmode)
5981 if (tmode == fmode)
5982 return;
5983 /* TMODE must be a fixed-point mode. */
5984 if (!ALL_FIXED_POINT_MODE_P (tmode))
5985 return;
5987 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5988 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5989 else
5990 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5993 /* Pick proper libcall for satfractuns_optab. */
5995 void
5996 gen_satfractuns_conv_libfunc (convert_optab tab,
5997 const char *opname,
5998 enum machine_mode tmode,
5999 enum machine_mode fmode)
6001 if (tmode == fmode)
6002 return;
6003 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
6004 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
6005 return;
6007 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6010 /* A table of previously-created libfuncs, hashed by name. */
6011 static GTY ((param_is (union tree_node))) htab_t libfunc_decls;
6013 /* Hashtable callbacks for libfunc_decls. */
6015 static hashval_t
6016 libfunc_decl_hash (const void *entry)
6018 return IDENTIFIER_HASH_VALUE (DECL_NAME ((const_tree) entry));
6021 static int
6022 libfunc_decl_eq (const void *entry1, const void *entry2)
6024 return DECL_NAME ((const_tree) entry1) == (const_tree) entry2;
6027 /* Build a decl for a libfunc named NAME. */
6029 tree
6030 build_libfunc_function (const char *name)
6032 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6033 get_identifier (name),
6034 build_function_type (integer_type_node, NULL_TREE));
6035 /* ??? We don't have any type information except for this is
6036 a function. Pretend this is "int foo()". */
6037 DECL_ARTIFICIAL (decl) = 1;
6038 DECL_EXTERNAL (decl) = 1;
6039 TREE_PUBLIC (decl) = 1;
6040 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6042 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6043 are the flags assigned by targetm.encode_section_info. */
6044 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6046 return decl;
6050 init_one_libfunc (const char *name)
6052 tree id, decl;
6053 void **slot;
6054 hashval_t hash;
6056 if (libfunc_decls == NULL)
6057 libfunc_decls = htab_create_ggc (37, libfunc_decl_hash,
6058 libfunc_decl_eq, NULL);
6060 /* See if we have already created a libfunc decl for this function. */
6061 id = get_identifier (name);
6062 hash = IDENTIFIER_HASH_VALUE (id);
6063 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, INSERT);
6064 decl = (tree) *slot;
6065 if (decl == NULL)
6067 /* Create a new decl, so that it can be passed to
6068 targetm.encode_section_info. */
6069 decl = build_libfunc_function (name);
6070 *slot = decl;
6072 return XEXP (DECL_RTL (decl), 0);
6075 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6078 set_user_assembler_libfunc (const char *name, const char *asmspec)
6080 tree id, decl;
6081 void **slot;
6082 hashval_t hash;
6084 id = get_identifier (name);
6085 hash = IDENTIFIER_HASH_VALUE (id);
6086 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, NO_INSERT);
6087 gcc_assert (slot);
6088 decl = (tree) *slot;
6089 set_user_assembler_name (decl, asmspec);
6090 return XEXP (DECL_RTL (decl), 0);
6093 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6094 MODE to NAME, which should be either 0 or a string constant. */
6095 void
6096 set_optab_libfunc (optab op, enum machine_mode mode, const char *name)
6098 rtx val;
6099 struct libfunc_entry e;
6100 struct libfunc_entry **slot;
6102 e.op = op;
6103 e.mode1 = mode;
6104 e.mode2 = VOIDmode;
6106 if (name)
6107 val = init_one_libfunc (name);
6108 else
6109 val = 0;
6110 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6111 if (*slot == NULL)
6112 *slot = ggc_alloc_libfunc_entry ();
6113 (*slot)->op = op;
6114 (*slot)->mode1 = mode;
6115 (*slot)->mode2 = VOIDmode;
6116 (*slot)->libfunc = val;
6119 /* Call this to reset the function entry for one conversion optab
6120 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6121 either 0 or a string constant. */
6122 void
6123 set_conv_libfunc (convert_optab optab, enum machine_mode tmode,
6124 enum machine_mode fmode, const char *name)
6126 rtx val;
6127 struct libfunc_entry e;
6128 struct libfunc_entry **slot;
6130 e.op = optab;
6131 e.mode1 = tmode;
6132 e.mode2 = fmode;
6134 if (name)
6135 val = init_one_libfunc (name);
6136 else
6137 val = 0;
6138 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6139 if (*slot == NULL)
6140 *slot = ggc_alloc_libfunc_entry ();
6141 (*slot)->op = optab;
6142 (*slot)->mode1 = tmode;
6143 (*slot)->mode2 = fmode;
6144 (*slot)->libfunc = val;
6147 /* Call this to initialize the contents of the optabs
6148 appropriately for the current target machine. */
6150 void
6151 init_optabs (void)
6153 if (libfunc_hash)
6154 htab_empty (libfunc_hash);
6155 else
6156 libfunc_hash = htab_create_ggc (10, hash_libfunc, eq_libfunc, NULL);
6158 /* Fill in the optabs with the insns we support. */
6159 init_all_optabs (this_fn_optabs);
6161 /* The ffs function operates on `int'. Fall back on it if we do not
6162 have a libgcc2 function for that width. */
6163 if (INT_TYPE_SIZE < BITS_PER_WORD)
6164 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6165 "ffs");
6167 /* Explicitly initialize the bswap libfuncs since we need them to be
6168 valid for things other than word_mode. */
6169 if (targetm.libfunc_gnu_prefix)
6171 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6172 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6174 else
6176 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6177 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6180 /* Use cabs for double complex abs, since systems generally have cabs.
6181 Don't define any libcall for float complex, so that cabs will be used. */
6182 if (complex_double_type_node)
6183 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node),
6184 "cabs");
6186 abort_libfunc = init_one_libfunc ("abort");
6187 memcpy_libfunc = init_one_libfunc ("memcpy");
6188 memmove_libfunc = init_one_libfunc ("memmove");
6189 memcmp_libfunc = init_one_libfunc ("memcmp");
6190 memset_libfunc = init_one_libfunc ("memset");
6191 setbits_libfunc = init_one_libfunc ("__setbits");
6193 #ifndef DONT_USE_BUILTIN_SETJMP
6194 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6195 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6196 #else
6197 setjmp_libfunc = init_one_libfunc ("setjmp");
6198 longjmp_libfunc = init_one_libfunc ("longjmp");
6199 #endif
6200 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6201 unwind_sjlj_unregister_libfunc
6202 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6204 /* For function entry/exit instrumentation. */
6205 profile_function_entry_libfunc
6206 = init_one_libfunc ("__cyg_profile_func_enter");
6207 profile_function_exit_libfunc
6208 = init_one_libfunc ("__cyg_profile_func_exit");
6210 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6212 /* Allow the target to add more libcalls or rename some, etc. */
6213 targetm.init_libfuncs ();
6216 /* Use the current target and options to initialize
6217 TREE_OPTIMIZATION_OPTABS (OPTNODE). */
6219 void
6220 init_tree_optimization_optabs (tree optnode)
6222 /* Quick exit if we have already computed optabs for this target. */
6223 if (TREE_OPTIMIZATION_BASE_OPTABS (optnode) == this_target_optabs)
6224 return;
6226 /* Forget any previous information and set up for the current target. */
6227 TREE_OPTIMIZATION_BASE_OPTABS (optnode) = this_target_optabs;
6228 struct target_optabs *tmp_optabs = (struct target_optabs *)
6229 TREE_OPTIMIZATION_OPTABS (optnode);
6230 if (tmp_optabs)
6231 memset (tmp_optabs, 0, sizeof (struct target_optabs));
6232 else
6233 tmp_optabs = (struct target_optabs *)
6234 ggc_alloc_atomic (sizeof (struct target_optabs));
6236 /* Generate a new set of optabs into tmp_optabs. */
6237 init_all_optabs (tmp_optabs);
6239 /* If the optabs changed, record it. */
6240 if (memcmp (tmp_optabs, this_target_optabs, sizeof (struct target_optabs)))
6241 TREE_OPTIMIZATION_OPTABS (optnode) = (unsigned char *) tmp_optabs;
6242 else
6244 TREE_OPTIMIZATION_OPTABS (optnode) = NULL;
6245 ggc_free (tmp_optabs);
6249 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6250 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6252 static void
6253 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6255 enum machine_mode mode;
6256 char buf[64];
6257 size_t len = strlen (base);
6258 int i;
6260 gcc_assert (max <= 8);
6261 gcc_assert (len + 3 < sizeof (buf));
6263 memcpy (buf, base, len);
6264 buf[len] = '_';
6265 buf[len + 1] = '0';
6266 buf[len + 2] = '\0';
6268 mode = QImode;
6269 for (i = 1; i <= max; i *= 2)
6271 buf[len + 1] = '0' + i;
6272 set_optab_libfunc (tab, mode, buf);
6273 mode = GET_MODE_2XWIDER_MODE (mode);
6277 void
6278 init_sync_libfuncs (int max)
6280 if (!flag_sync_libcalls)
6281 return;
6283 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6284 "__sync_val_compare_and_swap", max);
6285 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6286 "__sync_lock_test_and_set", max);
6288 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6289 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6290 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6291 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6292 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6293 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6295 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6296 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6297 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6298 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6299 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6300 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6303 /* Print information about the current contents of the optabs on
6304 STDERR. */
6306 DEBUG_FUNCTION void
6307 debug_optab_libfuncs (void)
6309 int i, j, k;
6311 /* Dump the arithmetic optabs. */
6312 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
6313 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6315 rtx l = optab_libfunc ((optab) i, (enum machine_mode) j);
6316 if (l)
6318 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6319 fprintf (stderr, "%s\t%s:\t%s\n",
6320 GET_RTX_NAME (optab_to_code ((optab) i)),
6321 GET_MODE_NAME (j),
6322 XSTR (l, 0));
6326 /* Dump the conversion optabs. */
6327 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
6328 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6329 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6331 rtx l = convert_optab_libfunc ((optab) i, (enum machine_mode) j,
6332 (enum machine_mode) k);
6333 if (l)
6335 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6336 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6337 GET_RTX_NAME (optab_to_code ((optab) i)),
6338 GET_MODE_NAME (j),
6339 GET_MODE_NAME (k),
6340 XSTR (l, 0));
6346 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6347 CODE. Return 0 on failure. */
6350 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6352 enum machine_mode mode = GET_MODE (op1);
6353 enum insn_code icode;
6354 rtx insn;
6355 rtx trap_rtx;
6357 if (mode == VOIDmode)
6358 return 0;
6360 icode = optab_handler (ctrap_optab, mode);
6361 if (icode == CODE_FOR_nothing)
6362 return 0;
6364 /* Some targets only accept a zero trap code. */
6365 if (!insn_operand_matches (icode, 3, tcode))
6366 return 0;
6368 do_pending_stack_adjust ();
6369 start_sequence ();
6370 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6371 &trap_rtx, &mode);
6372 if (!trap_rtx)
6373 insn = NULL_RTX;
6374 else
6375 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6376 tcode);
6378 /* If that failed, then give up. */
6379 if (insn == 0)
6381 end_sequence ();
6382 return 0;
6385 emit_insn (insn);
6386 insn = get_insns ();
6387 end_sequence ();
6388 return insn;
6391 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6392 or unsigned operation code. */
6394 static enum rtx_code
6395 get_rtx_code (enum tree_code tcode, bool unsignedp)
6397 enum rtx_code code;
6398 switch (tcode)
6400 case EQ_EXPR:
6401 code = EQ;
6402 break;
6403 case NE_EXPR:
6404 code = NE;
6405 break;
6406 case LT_EXPR:
6407 code = unsignedp ? LTU : LT;
6408 break;
6409 case LE_EXPR:
6410 code = unsignedp ? LEU : LE;
6411 break;
6412 case GT_EXPR:
6413 code = unsignedp ? GTU : GT;
6414 break;
6415 case GE_EXPR:
6416 code = unsignedp ? GEU : GE;
6417 break;
6419 case UNORDERED_EXPR:
6420 code = UNORDERED;
6421 break;
6422 case ORDERED_EXPR:
6423 code = ORDERED;
6424 break;
6425 case UNLT_EXPR:
6426 code = UNLT;
6427 break;
6428 case UNLE_EXPR:
6429 code = UNLE;
6430 break;
6431 case UNGT_EXPR:
6432 code = UNGT;
6433 break;
6434 case UNGE_EXPR:
6435 code = UNGE;
6436 break;
6437 case UNEQ_EXPR:
6438 code = UNEQ;
6439 break;
6440 case LTGT_EXPR:
6441 code = LTGT;
6442 break;
6444 default:
6445 gcc_unreachable ();
6447 return code;
6450 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6451 unsigned operators. Do not generate compare instruction. */
6453 static rtx
6454 vector_compare_rtx (enum tree_code tcode, tree t_op0, tree t_op1,
6455 bool unsignedp, enum insn_code icode)
6457 struct expand_operand ops[2];
6458 rtx rtx_op0, rtx_op1;
6459 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
6461 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
6463 /* Expand operands. */
6464 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6465 EXPAND_STACK_PARM);
6466 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6467 EXPAND_STACK_PARM);
6469 create_input_operand (&ops[0], rtx_op0, GET_MODE (rtx_op0));
6470 create_input_operand (&ops[1], rtx_op1, GET_MODE (rtx_op1));
6471 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6472 gcc_unreachable ();
6473 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6476 /* Return true if VEC_PERM_EXPR can be expanded using SIMD extensions
6477 of the CPU. SEL may be NULL, which stands for an unknown constant. */
6479 bool
6480 can_vec_perm_p (enum machine_mode mode, bool variable,
6481 const unsigned char *sel)
6483 enum machine_mode qimode;
6485 /* If the target doesn't implement a vector mode for the vector type,
6486 then no operations are supported. */
6487 if (!VECTOR_MODE_P (mode))
6488 return false;
6490 if (!variable)
6492 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6493 && (sel == NULL
6494 || targetm.vectorize.vec_perm_const_ok == NULL
6495 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6496 return true;
6499 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6500 return true;
6502 /* We allow fallback to a QI vector mode, and adjust the mask. */
6503 if (GET_MODE_INNER (mode) == QImode)
6504 return false;
6505 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6506 if (!VECTOR_MODE_P (qimode))
6507 return false;
6509 /* ??? For completeness, we ought to check the QImode version of
6510 vec_perm_const_optab. But all users of this implicit lowering
6511 feature implement the variable vec_perm_optab. */
6512 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6513 return false;
6515 /* In order to support the lowering of variable permutations,
6516 we need to support shifts and adds. */
6517 if (variable)
6519 if (GET_MODE_UNIT_SIZE (mode) > 2
6520 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6521 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6522 return false;
6523 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6524 return false;
6527 return true;
6530 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
6532 static rtx
6533 expand_vec_perm_1 (enum insn_code icode, rtx target,
6534 rtx v0, rtx v1, rtx sel)
6536 enum machine_mode tmode = GET_MODE (target);
6537 enum machine_mode smode = GET_MODE (sel);
6538 struct expand_operand ops[4];
6540 create_output_operand (&ops[0], target, tmode);
6541 create_input_operand (&ops[3], sel, smode);
6543 /* Make an effort to preserve v0 == v1. The target expander is able to
6544 rely on this to determine if we're permuting a single input operand. */
6545 if (rtx_equal_p (v0, v1))
6547 if (!insn_operand_matches (icode, 1, v0))
6548 v0 = force_reg (tmode, v0);
6549 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6550 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6552 create_fixed_operand (&ops[1], v0);
6553 create_fixed_operand (&ops[2], v0);
6555 else
6557 create_input_operand (&ops[1], v0, tmode);
6558 create_input_operand (&ops[2], v1, tmode);
6561 if (maybe_expand_insn (icode, 4, ops))
6562 return ops[0].value;
6563 return NULL_RTX;
6566 /* Generate instructions for vec_perm optab given its mode
6567 and three operands. */
6570 expand_vec_perm (enum machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6572 enum insn_code icode;
6573 enum machine_mode qimode;
6574 unsigned int i, w, e, u;
6575 rtx tmp, sel_qi = NULL;
6576 rtvec vec;
6578 if (!target || GET_MODE (target) != mode)
6579 target = gen_reg_rtx (mode);
6581 w = GET_MODE_SIZE (mode);
6582 e = GET_MODE_NUNITS (mode);
6583 u = GET_MODE_UNIT_SIZE (mode);
6585 /* Set QIMODE to a different vector mode with byte elements.
6586 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6587 qimode = VOIDmode;
6588 if (GET_MODE_INNER (mode) != QImode)
6590 qimode = mode_for_vector (QImode, w);
6591 if (!VECTOR_MODE_P (qimode))
6592 qimode = VOIDmode;
6595 /* If the input is a constant, expand it specially. */
6596 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6597 if (GET_CODE (sel) == CONST_VECTOR)
6599 icode = direct_optab_handler (vec_perm_const_optab, mode);
6600 if (icode != CODE_FOR_nothing)
6602 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6603 if (tmp)
6604 return tmp;
6607 /* Fall back to a constant byte-based permutation. */
6608 if (qimode != VOIDmode)
6610 vec = rtvec_alloc (w);
6611 for (i = 0; i < e; ++i)
6613 unsigned int j, this_e;
6615 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6616 this_e &= 2 * e - 1;
6617 this_e *= u;
6619 for (j = 0; j < u; ++j)
6620 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
6622 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
6624 icode = direct_optab_handler (vec_perm_const_optab, qimode);
6625 if (icode != CODE_FOR_nothing)
6627 tmp = expand_vec_perm_1 (icode, gen_lowpart (qimode, target),
6628 gen_lowpart (qimode, v0),
6629 gen_lowpart (qimode, v1), sel_qi);
6630 if (tmp)
6631 return gen_lowpart (mode, tmp);
6636 /* Otherwise expand as a fully variable permuation. */
6637 icode = direct_optab_handler (vec_perm_optab, mode);
6638 if (icode != CODE_FOR_nothing)
6640 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6641 if (tmp)
6642 return tmp;
6645 /* As a special case to aid several targets, lower the element-based
6646 permutation to a byte-based permutation and try again. */
6647 if (qimode == VOIDmode)
6648 return NULL_RTX;
6649 icode = direct_optab_handler (vec_perm_optab, qimode);
6650 if (icode == CODE_FOR_nothing)
6651 return NULL_RTX;
6653 if (sel_qi == NULL)
6655 /* Multiply each element by its byte size. */
6656 enum machine_mode selmode = GET_MODE (sel);
6657 if (u == 2)
6658 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6659 sel, 0, OPTAB_DIRECT);
6660 else
6661 sel = expand_simple_binop (selmode, ASHIFT, sel,
6662 GEN_INT (exact_log2 (u)),
6663 sel, 0, OPTAB_DIRECT);
6664 gcc_assert (sel != NULL);
6666 /* Broadcast the low byte each element into each of its bytes. */
6667 vec = rtvec_alloc (w);
6668 for (i = 0; i < w; ++i)
6670 int this_e = i / u * u;
6671 if (BYTES_BIG_ENDIAN)
6672 this_e += u - 1;
6673 RTVEC_ELT (vec, i) = GEN_INT (this_e);
6675 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6676 sel = gen_lowpart (qimode, sel);
6677 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
6678 gcc_assert (sel != NULL);
6680 /* Add the byte offset to each byte element. */
6681 /* Note that the definition of the indicies here is memory ordering,
6682 so there should be no difference between big and little endian. */
6683 vec = rtvec_alloc (w);
6684 for (i = 0; i < w; ++i)
6685 RTVEC_ELT (vec, i) = GEN_INT (i % u);
6686 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6687 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6688 sel, 0, OPTAB_DIRECT);
6689 gcc_assert (sel_qi != NULL);
6692 tmp = expand_vec_perm_1 (icode, gen_lowpart (qimode, target),
6693 gen_lowpart (qimode, v0),
6694 gen_lowpart (qimode, v1), sel_qi);
6695 if (tmp)
6696 tmp = gen_lowpart (mode, tmp);
6697 return tmp;
6700 /* Return insn code for a conditional operator with a comparison in
6701 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
6703 static inline enum insn_code
6704 get_vcond_icode (enum machine_mode vmode, enum machine_mode cmode, bool uns)
6706 enum insn_code icode = CODE_FOR_nothing;
6707 if (uns)
6708 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
6709 else
6710 icode = convert_optab_handler (vcond_optab, vmode, cmode);
6711 return icode;
6714 /* Return TRUE iff, appropriate vector insns are available
6715 for vector cond expr with vector type VALUE_TYPE and a comparison
6716 with operand vector types in CMP_OP_TYPE. */
6718 bool
6719 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
6721 enum machine_mode value_mode = TYPE_MODE (value_type);
6722 enum machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
6723 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
6724 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
6725 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
6726 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
6727 return false;
6728 return true;
6731 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6732 three operands. */
6735 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6736 rtx target)
6738 struct expand_operand ops[6];
6739 enum insn_code icode;
6740 rtx comparison, rtx_op1, rtx_op2;
6741 enum machine_mode mode = TYPE_MODE (vec_cond_type);
6742 enum machine_mode cmp_op_mode;
6743 bool unsignedp;
6744 tree op0a, op0b;
6745 enum tree_code tcode;
6747 if (COMPARISON_CLASS_P (op0))
6749 op0a = TREE_OPERAND (op0, 0);
6750 op0b = TREE_OPERAND (op0, 1);
6751 tcode = TREE_CODE (op0);
6753 else
6755 /* Fake op0 < 0. */
6756 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (op0)));
6757 op0a = op0;
6758 op0b = build_zero_cst (TREE_TYPE (op0));
6759 tcode = LT_EXPR;
6761 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6762 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
6765 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
6766 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
6768 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
6769 if (icode == CODE_FOR_nothing)
6770 return 0;
6772 comparison = vector_compare_rtx (tcode, op0a, op0b, unsignedp, icode);
6773 rtx_op1 = expand_normal (op1);
6774 rtx_op2 = expand_normal (op2);
6776 create_output_operand (&ops[0], target, mode);
6777 create_input_operand (&ops[1], rtx_op1, mode);
6778 create_input_operand (&ops[2], rtx_op2, mode);
6779 create_fixed_operand (&ops[3], comparison);
6780 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6781 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6782 expand_insn (icode, 6, ops);
6783 return ops[0].value;
6786 /* Return non-zero if a highpart multiply is supported of can be synthisized.
6787 For the benefit of expand_mult_highpart, the return value is 1 for direct,
6788 2 for even/odd widening, and 3 for hi/lo widening. */
6791 can_mult_highpart_p (enum machine_mode mode, bool uns_p)
6793 optab op;
6794 unsigned char *sel;
6795 unsigned i, nunits;
6797 op = uns_p ? umul_highpart_optab : smul_highpart_optab;
6798 if (optab_handler (op, mode) != CODE_FOR_nothing)
6799 return 1;
6801 /* If the mode is an integral vector, synth from widening operations. */
6802 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
6803 return 0;
6805 nunits = GET_MODE_NUNITS (mode);
6806 sel = XALLOCAVEC (unsigned char, nunits);
6808 op = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6809 if (optab_handler (op, mode) != CODE_FOR_nothing)
6811 op = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6812 if (optab_handler (op, mode) != CODE_FOR_nothing)
6814 for (i = 0; i < nunits; ++i)
6815 sel[i] = !BYTES_BIG_ENDIAN + (i & ~1) + ((i & 1) ? nunits : 0);
6816 if (can_vec_perm_p (mode, false, sel))
6817 return 2;
6821 op = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6822 if (optab_handler (op, mode) != CODE_FOR_nothing)
6824 op = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6825 if (optab_handler (op, mode) != CODE_FOR_nothing)
6827 for (i = 0; i < nunits; ++i)
6828 sel[i] = 2 * i + (BYTES_BIG_ENDIAN ? 0 : 1);
6829 if (can_vec_perm_p (mode, false, sel))
6830 return 3;
6834 return 0;
6837 /* Expand a highpart multiply. */
6840 expand_mult_highpart (enum machine_mode mode, rtx op0, rtx op1,
6841 rtx target, bool uns_p)
6843 struct expand_operand eops[3];
6844 enum insn_code icode;
6845 int method, i, nunits;
6846 enum machine_mode wmode;
6847 rtx m1, m2, perm;
6848 optab tab1, tab2;
6849 rtvec v;
6851 method = can_mult_highpart_p (mode, uns_p);
6852 switch (method)
6854 case 0:
6855 return NULL_RTX;
6856 case 1:
6857 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6858 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6859 OPTAB_LIB_WIDEN);
6860 case 2:
6861 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6862 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6863 break;
6864 case 3:
6865 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6866 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6867 if (BYTES_BIG_ENDIAN)
6869 optab t = tab1;
6870 tab1 = tab2;
6871 tab2 = t;
6873 break;
6874 default:
6875 gcc_unreachable ();
6878 icode = optab_handler (tab1, mode);
6879 nunits = GET_MODE_NUNITS (mode);
6880 wmode = insn_data[icode].operand[0].mode;
6881 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
6882 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
6884 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6885 create_input_operand (&eops[1], op0, mode);
6886 create_input_operand (&eops[2], op1, mode);
6887 expand_insn (icode, 3, eops);
6888 m1 = gen_lowpart (mode, eops[0].value);
6890 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6891 create_input_operand (&eops[1], op0, mode);
6892 create_input_operand (&eops[2], op1, mode);
6893 expand_insn (optab_handler (tab2, mode), 3, eops);
6894 m2 = gen_lowpart (mode, eops[0].value);
6896 v = rtvec_alloc (nunits);
6897 if (method == 2)
6899 for (i = 0; i < nunits; ++i)
6900 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
6901 + ((i & 1) ? nunits : 0));
6903 else
6905 for (i = 0; i < nunits; ++i)
6906 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
6908 perm = gen_rtx_CONST_VECTOR (mode, v);
6910 return expand_vec_perm (mode, m1, m2, perm, target);
6913 /* Return true if there is a compare_and_swap pattern. */
6915 bool
6916 can_compare_and_swap_p (enum machine_mode mode, bool allow_libcall)
6918 enum insn_code icode;
6920 /* Check for __atomic_compare_and_swap. */
6921 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
6922 if (icode != CODE_FOR_nothing)
6923 return true;
6925 /* Check for __sync_compare_and_swap. */
6926 icode = optab_handler (sync_compare_and_swap_optab, mode);
6927 if (icode != CODE_FOR_nothing)
6928 return true;
6929 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
6930 return true;
6932 /* No inline compare and swap. */
6933 return false;
6936 /* Return true if an atomic exchange can be performed. */
6938 bool
6939 can_atomic_exchange_p (enum machine_mode mode, bool allow_libcall)
6941 enum insn_code icode;
6943 /* Check for __atomic_exchange. */
6944 icode = direct_optab_handler (atomic_exchange_optab, mode);
6945 if (icode != CODE_FOR_nothing)
6946 return true;
6948 /* Don't check __sync_test_and_set, as on some platforms that
6949 has reduced functionality. Targets that really do support
6950 a proper exchange should simply be updated to the __atomics. */
6952 return can_compare_and_swap_p (mode, allow_libcall);
6956 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
6957 pattern. */
6959 static void
6960 find_cc_set (rtx x, const_rtx pat, void *data)
6962 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
6963 && GET_CODE (pat) == SET)
6965 rtx *p_cc_reg = (rtx *) data;
6966 gcc_assert (!*p_cc_reg);
6967 *p_cc_reg = x;
6971 /* This is a helper function for the other atomic operations. This function
6972 emits a loop that contains SEQ that iterates until a compare-and-swap
6973 operation at the end succeeds. MEM is the memory to be modified. SEQ is
6974 a set of instructions that takes a value from OLD_REG as an input and
6975 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
6976 set to the current contents of MEM. After SEQ, a compare-and-swap will
6977 attempt to update MEM with NEW_REG. The function returns true when the
6978 loop was generated successfully. */
6980 static bool
6981 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
6983 enum machine_mode mode = GET_MODE (mem);
6984 rtx label, cmp_reg, success, oldval;
6986 /* The loop we want to generate looks like
6988 cmp_reg = mem;
6989 label:
6990 old_reg = cmp_reg;
6991 seq;
6992 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
6993 if (success)
6994 goto label;
6996 Note that we only do the plain load from memory once. Subsequent
6997 iterations use the value loaded by the compare-and-swap pattern. */
6999 label = gen_label_rtx ();
7000 cmp_reg = gen_reg_rtx (mode);
7002 emit_move_insn (cmp_reg, mem);
7003 emit_label (label);
7004 emit_move_insn (old_reg, cmp_reg);
7005 if (seq)
7006 emit_insn (seq);
7008 success = NULL_RTX;
7009 oldval = cmp_reg;
7010 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
7011 new_reg, false, MEMMODEL_SEQ_CST,
7012 MEMMODEL_RELAXED))
7013 return false;
7015 if (oldval != cmp_reg)
7016 emit_move_insn (cmp_reg, oldval);
7018 /* Mark this jump predicted not taken. */
7019 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
7020 GET_MODE (success), 1, label, 0);
7021 return true;
7025 /* This function tries to emit an atomic_exchange intruction. VAL is written
7026 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
7027 using TARGET if possible. */
7029 static rtx
7030 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7032 enum machine_mode mode = GET_MODE (mem);
7033 enum insn_code icode;
7035 /* If the target supports the exchange directly, great. */
7036 icode = direct_optab_handler (atomic_exchange_optab, mode);
7037 if (icode != CODE_FOR_nothing)
7039 struct expand_operand ops[4];
7041 create_output_operand (&ops[0], target, mode);
7042 create_fixed_operand (&ops[1], mem);
7043 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7044 create_convert_operand_to (&ops[2], val, mode, true);
7045 create_integer_operand (&ops[3], model);
7046 if (maybe_expand_insn (icode, 4, ops))
7047 return ops[0].value;
7050 return NULL_RTX;
7053 /* This function tries to implement an atomic exchange operation using
7054 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7055 The previous contents of *MEM are returned, using TARGET if possible.
7056 Since this instructionn is an acquire barrier only, stronger memory
7057 models may require additional barriers to be emitted. */
7059 static rtx
7060 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7061 enum memmodel model)
7063 enum machine_mode mode = GET_MODE (mem);
7064 enum insn_code icode;
7065 rtx last_insn = get_last_insn ();
7067 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7069 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7070 exists, and the memory model is stronger than acquire, add a release
7071 barrier before the instruction. */
7073 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST
7074 || (model & MEMMODEL_MASK) == MEMMODEL_RELEASE
7075 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
7076 expand_mem_thread_fence (model);
7078 if (icode != CODE_FOR_nothing)
7080 struct expand_operand ops[3];
7081 create_output_operand (&ops[0], target, mode);
7082 create_fixed_operand (&ops[1], mem);
7083 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7084 create_convert_operand_to (&ops[2], val, mode, true);
7085 if (maybe_expand_insn (icode, 3, ops))
7086 return ops[0].value;
7089 /* If an external test-and-set libcall is provided, use that instead of
7090 any external compare-and-swap that we might get from the compare-and-
7091 swap-loop expansion later. */
7092 if (!can_compare_and_swap_p (mode, false))
7094 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7095 if (libfunc != NULL)
7097 rtx addr;
7099 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7100 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7101 mode, 2, addr, ptr_mode,
7102 val, mode);
7106 /* If the test_and_set can't be emitted, eliminate any barrier that might
7107 have been emitted. */
7108 delete_insns_since (last_insn);
7109 return NULL_RTX;
7112 /* This function tries to implement an atomic exchange operation using a
7113 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7114 *MEM are returned, using TARGET if possible. No memory model is required
7115 since a compare_and_swap loop is seq-cst. */
7117 static rtx
7118 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7120 enum machine_mode mode = GET_MODE (mem);
7122 if (can_compare_and_swap_p (mode, true))
7124 if (!target || !register_operand (target, mode))
7125 target = gen_reg_rtx (mode);
7126 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7127 val = convert_modes (mode, GET_MODE (val), val, 1);
7128 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7129 return target;
7132 return NULL_RTX;
7135 /* This function tries to implement an atomic test-and-set operation
7136 using the atomic_test_and_set instruction pattern. A boolean value
7137 is returned from the operation, using TARGET if possible. */
7139 #ifndef HAVE_atomic_test_and_set
7140 #define HAVE_atomic_test_and_set 0
7141 #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
7142 #endif
7144 static rtx
7145 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7147 enum machine_mode pat_bool_mode;
7148 struct expand_operand ops[3];
7150 if (!HAVE_atomic_test_and_set)
7151 return NULL_RTX;
7153 /* While we always get QImode from __atomic_test_and_set, we get
7154 other memory modes from __sync_lock_test_and_set. Note that we
7155 use no endian adjustment here. This matches the 4.6 behavior
7156 in the Sparc backend. */
7157 gcc_checking_assert
7158 (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
7159 if (GET_MODE (mem) != QImode)
7160 mem = adjust_address_nv (mem, QImode, 0);
7162 pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
7163 create_output_operand (&ops[0], target, pat_bool_mode);
7164 create_fixed_operand (&ops[1], mem);
7165 create_integer_operand (&ops[2], model);
7167 if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
7168 return ops[0].value;
7169 return NULL_RTX;
7172 /* This function expands the legacy _sync_lock test_and_set operation which is
7173 generally an atomic exchange. Some limited targets only allow the
7174 constant 1 to be stored. This is an ACQUIRE operation.
7176 TARGET is an optional place to stick the return value.
7177 MEM is where VAL is stored. */
7180 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7182 rtx ret;
7184 /* Try an atomic_exchange first. */
7185 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_ACQUIRE);
7186 if (ret)
7187 return ret;
7189 ret = maybe_emit_sync_lock_test_and_set (target, mem, val, MEMMODEL_ACQUIRE);
7190 if (ret)
7191 return ret;
7193 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7194 if (ret)
7195 return ret;
7197 /* If there are no other options, try atomic_test_and_set if the value
7198 being stored is 1. */
7199 if (val == const1_rtx)
7200 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_ACQUIRE);
7202 return ret;
7205 /* This function expands the atomic test_and_set operation:
7206 atomically store a boolean TRUE into MEM and return the previous value.
7208 MEMMODEL is the memory model variant to use.
7209 TARGET is an optional place to stick the return value. */
7212 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7214 enum machine_mode mode = GET_MODE (mem);
7215 rtx ret, trueval, subtarget;
7217 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7218 if (ret)
7219 return ret;
7221 /* Be binary compatible with non-default settings of trueval, and different
7222 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7223 another only has atomic-exchange. */
7224 if (targetm.atomic_test_and_set_trueval == 1)
7226 trueval = const1_rtx;
7227 subtarget = target ? target : gen_reg_rtx (mode);
7229 else
7231 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7232 subtarget = gen_reg_rtx (mode);
7235 /* Try the atomic-exchange optab... */
7236 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7238 /* ... then an atomic-compare-and-swap loop ... */
7239 if (!ret)
7240 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7242 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7243 if (!ret)
7244 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7246 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7247 things with the value 1. Thus we try again without trueval. */
7248 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7249 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7251 /* Failing all else, assume a single threaded environment and simply
7252 perform the operation. */
7253 if (!ret)
7255 emit_move_insn (subtarget, mem);
7256 emit_move_insn (mem, trueval);
7257 ret = subtarget;
7260 /* Recall that have to return a boolean value; rectify if trueval
7261 is not exactly one. */
7262 if (targetm.atomic_test_and_set_trueval != 1)
7263 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7265 return ret;
7268 /* This function expands the atomic exchange operation:
7269 atomically store VAL in MEM and return the previous value in MEM.
7271 MEMMODEL is the memory model variant to use.
7272 TARGET is an optional place to stick the return value. */
7275 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7277 rtx ret;
7279 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7281 /* Next try a compare-and-swap loop for the exchange. */
7282 if (!ret)
7283 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7285 return ret;
7288 /* This function expands the atomic compare exchange operation:
7290 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7291 *PTARGET_OVAL is an optional place to store the old value from memory.
7292 Both target parameters may be NULL to indicate that we do not care about
7293 that return value. Both target parameters are updated on success to
7294 the actual location of the corresponding result.
7296 MEMMODEL is the memory model variant to use.
7298 The return value of the function is true for success. */
7300 bool
7301 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7302 rtx mem, rtx expected, rtx desired,
7303 bool is_weak, enum memmodel succ_model,
7304 enum memmodel fail_model)
7306 enum machine_mode mode = GET_MODE (mem);
7307 struct expand_operand ops[8];
7308 enum insn_code icode;
7309 rtx target_oval, target_bool = NULL_RTX;
7310 rtx libfunc;
7312 /* Load expected into a register for the compare and swap. */
7313 if (MEM_P (expected))
7314 expected = copy_to_reg (expected);
7316 /* Make sure we always have some place to put the return oldval.
7317 Further, make sure that place is distinct from the input expected,
7318 just in case we need that path down below. */
7319 if (ptarget_oval == NULL
7320 || (target_oval = *ptarget_oval) == NULL
7321 || reg_overlap_mentioned_p (expected, target_oval))
7322 target_oval = gen_reg_rtx (mode);
7324 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7325 if (icode != CODE_FOR_nothing)
7327 enum machine_mode bool_mode = insn_data[icode].operand[0].mode;
7329 /* Make sure we always have a place for the bool operand. */
7330 if (ptarget_bool == NULL
7331 || (target_bool = *ptarget_bool) == NULL
7332 || GET_MODE (target_bool) != bool_mode)
7333 target_bool = gen_reg_rtx (bool_mode);
7335 /* Emit the compare_and_swap. */
7336 create_output_operand (&ops[0], target_bool, bool_mode);
7337 create_output_operand (&ops[1], target_oval, mode);
7338 create_fixed_operand (&ops[2], mem);
7339 create_convert_operand_to (&ops[3], expected, mode, true);
7340 create_convert_operand_to (&ops[4], desired, mode, true);
7341 create_integer_operand (&ops[5], is_weak);
7342 create_integer_operand (&ops[6], succ_model);
7343 create_integer_operand (&ops[7], fail_model);
7344 expand_insn (icode, 8, ops);
7346 /* Return success/failure. */
7347 target_bool = ops[0].value;
7348 target_oval = ops[1].value;
7349 goto success;
7352 /* Otherwise fall back to the original __sync_val_compare_and_swap
7353 which is always seq-cst. */
7354 icode = optab_handler (sync_compare_and_swap_optab, mode);
7355 if (icode != CODE_FOR_nothing)
7357 rtx cc_reg;
7359 create_output_operand (&ops[0], target_oval, mode);
7360 create_fixed_operand (&ops[1], mem);
7361 create_convert_operand_to (&ops[2], expected, mode, true);
7362 create_convert_operand_to (&ops[3], desired, mode, true);
7363 if (!maybe_expand_insn (icode, 4, ops))
7364 return false;
7366 target_oval = ops[0].value;
7368 /* If the caller isn't interested in the boolean return value,
7369 skip the computation of it. */
7370 if (ptarget_bool == NULL)
7371 goto success;
7373 /* Otherwise, work out if the compare-and-swap succeeded. */
7374 cc_reg = NULL_RTX;
7375 if (have_insn_for (COMPARE, CCmode))
7376 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7377 if (cc_reg)
7379 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7380 const0_rtx, VOIDmode, 0, 1);
7381 goto success;
7383 goto success_bool_from_val;
7386 /* Also check for library support for __sync_val_compare_and_swap. */
7387 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7388 if (libfunc != NULL)
7390 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7391 target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7392 mode, 3, addr, ptr_mode,
7393 expected, mode, desired, mode);
7395 /* Compute the boolean return value only if requested. */
7396 if (ptarget_bool)
7397 goto success_bool_from_val;
7398 else
7399 goto success;
7402 /* Failure. */
7403 return false;
7405 success_bool_from_val:
7406 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7407 expected, VOIDmode, 1, 1);
7408 success:
7409 /* Make sure that the oval output winds up where the caller asked. */
7410 if (ptarget_oval)
7411 *ptarget_oval = target_oval;
7412 if (ptarget_bool)
7413 *ptarget_bool = target_bool;
7414 return true;
7417 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7419 static void
7420 expand_asm_memory_barrier (void)
7422 rtx asm_op, clob;
7424 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7425 rtvec_alloc (0), rtvec_alloc (0),
7426 rtvec_alloc (0), UNKNOWN_LOCATION);
7427 MEM_VOLATILE_P (asm_op) = 1;
7429 clob = gen_rtx_SCRATCH (VOIDmode);
7430 clob = gen_rtx_MEM (BLKmode, clob);
7431 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7433 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7436 /* This routine will either emit the mem_thread_fence pattern or issue a
7437 sync_synchronize to generate a fence for memory model MEMMODEL. */
7439 #ifndef HAVE_mem_thread_fence
7440 # define HAVE_mem_thread_fence 0
7441 # define gen_mem_thread_fence(x) (gcc_unreachable (), NULL_RTX)
7442 #endif
7443 #ifndef HAVE_memory_barrier
7444 # define HAVE_memory_barrier 0
7445 # define gen_memory_barrier() (gcc_unreachable (), NULL_RTX)
7446 #endif
7448 void
7449 expand_mem_thread_fence (enum memmodel model)
7451 if (HAVE_mem_thread_fence)
7452 emit_insn (gen_mem_thread_fence (GEN_INT (model)));
7453 else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
7455 if (HAVE_memory_barrier)
7456 emit_insn (gen_memory_barrier ());
7457 else if (synchronize_libfunc != NULL_RTX)
7458 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7459 else
7460 expand_asm_memory_barrier ();
7464 /* This routine will either emit the mem_signal_fence pattern or issue a
7465 sync_synchronize to generate a fence for memory model MEMMODEL. */
7467 #ifndef HAVE_mem_signal_fence
7468 # define HAVE_mem_signal_fence 0
7469 # define gen_mem_signal_fence(x) (gcc_unreachable (), NULL_RTX)
7470 #endif
7472 void
7473 expand_mem_signal_fence (enum memmodel model)
7475 if (HAVE_mem_signal_fence)
7476 emit_insn (gen_mem_signal_fence (GEN_INT (model)));
7477 else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
7479 /* By default targets are coherent between a thread and the signal
7480 handler running on the same thread. Thus this really becomes a
7481 compiler barrier, in that stores must not be sunk past
7482 (or raised above) a given point. */
7483 expand_asm_memory_barrier ();
7487 /* This function expands the atomic load operation:
7488 return the atomically loaded value in MEM.
7490 MEMMODEL is the memory model variant to use.
7491 TARGET is an option place to stick the return value. */
7494 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7496 enum machine_mode mode = GET_MODE (mem);
7497 enum insn_code icode;
7499 /* If the target supports the load directly, great. */
7500 icode = direct_optab_handler (atomic_load_optab, mode);
7501 if (icode != CODE_FOR_nothing)
7503 struct expand_operand ops[3];
7505 create_output_operand (&ops[0], target, mode);
7506 create_fixed_operand (&ops[1], mem);
7507 create_integer_operand (&ops[2], model);
7508 if (maybe_expand_insn (icode, 3, ops))
7509 return ops[0].value;
7512 /* If the size of the object is greater than word size on this target,
7513 then we assume that a load will not be atomic. */
7514 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7516 /* Issue val = compare_and_swap (mem, 0, 0).
7517 This may cause the occasional harmless store of 0 when the value is
7518 already 0, but it seems to be OK according to the standards guys. */
7519 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7520 const0_rtx, false, model, model))
7521 return target;
7522 else
7523 /* Otherwise there is no atomic load, leave the library call. */
7524 return NULL_RTX;
7527 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7528 if (!target || target == const0_rtx)
7529 target = gen_reg_rtx (mode);
7531 /* For SEQ_CST, emit a barrier before the load. */
7532 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7533 expand_mem_thread_fence (model);
7535 emit_move_insn (target, mem);
7537 /* Emit the appropriate barrier after the load. */
7538 expand_mem_thread_fence (model);
7540 return target;
7543 /* This function expands the atomic store operation:
7544 Atomically store VAL in MEM.
7545 MEMMODEL is the memory model variant to use.
7546 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7547 function returns const0_rtx if a pattern was emitted. */
7550 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7552 enum machine_mode mode = GET_MODE (mem);
7553 enum insn_code icode;
7554 struct expand_operand ops[3];
7556 /* If the target supports the store directly, great. */
7557 icode = direct_optab_handler (atomic_store_optab, mode);
7558 if (icode != CODE_FOR_nothing)
7560 create_fixed_operand (&ops[0], mem);
7561 create_input_operand (&ops[1], val, mode);
7562 create_integer_operand (&ops[2], model);
7563 if (maybe_expand_insn (icode, 3, ops))
7564 return const0_rtx;
7567 /* If using __sync_lock_release is a viable alternative, try it. */
7568 if (use_release)
7570 icode = direct_optab_handler (sync_lock_release_optab, mode);
7571 if (icode != CODE_FOR_nothing)
7573 create_fixed_operand (&ops[0], mem);
7574 create_input_operand (&ops[1], const0_rtx, mode);
7575 if (maybe_expand_insn (icode, 2, ops))
7577 /* lock_release is only a release barrier. */
7578 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7579 expand_mem_thread_fence (model);
7580 return const0_rtx;
7585 /* If the size of the object is greater than word size on this target,
7586 a default store will not be atomic, Try a mem_exchange and throw away
7587 the result. If that doesn't work, don't do anything. */
7588 if (GET_MODE_PRECISION(mode) > BITS_PER_WORD)
7590 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7591 if (!target)
7592 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7593 if (target)
7594 return const0_rtx;
7595 else
7596 return NULL_RTX;
7599 /* Otherwise assume stores are atomic, and emit the proper barriers. */
7600 expand_mem_thread_fence (model);
7602 emit_move_insn (mem, val);
7604 /* For SEQ_CST, also emit a barrier after the store. */
7605 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7606 expand_mem_thread_fence (model);
7608 return const0_rtx;
7612 /* Structure containing the pointers and values required to process the
7613 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7615 struct atomic_op_functions
7617 direct_optab mem_fetch_before;
7618 direct_optab mem_fetch_after;
7619 direct_optab mem_no_result;
7620 optab fetch_before;
7621 optab fetch_after;
7622 direct_optab no_result;
7623 enum rtx_code reverse_code;
7627 /* Fill in structure pointed to by OP with the various optab entries for an
7628 operation of type CODE. */
7630 static void
7631 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7633 gcc_assert (op!= NULL);
7635 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7636 in the source code during compilation, and the optab entries are not
7637 computable until runtime. Fill in the values at runtime. */
7638 switch (code)
7640 case PLUS:
7641 op->mem_fetch_before = atomic_fetch_add_optab;
7642 op->mem_fetch_after = atomic_add_fetch_optab;
7643 op->mem_no_result = atomic_add_optab;
7644 op->fetch_before = sync_old_add_optab;
7645 op->fetch_after = sync_new_add_optab;
7646 op->no_result = sync_add_optab;
7647 op->reverse_code = MINUS;
7648 break;
7649 case MINUS:
7650 op->mem_fetch_before = atomic_fetch_sub_optab;
7651 op->mem_fetch_after = atomic_sub_fetch_optab;
7652 op->mem_no_result = atomic_sub_optab;
7653 op->fetch_before = sync_old_sub_optab;
7654 op->fetch_after = sync_new_sub_optab;
7655 op->no_result = sync_sub_optab;
7656 op->reverse_code = PLUS;
7657 break;
7658 case XOR:
7659 op->mem_fetch_before = atomic_fetch_xor_optab;
7660 op->mem_fetch_after = atomic_xor_fetch_optab;
7661 op->mem_no_result = atomic_xor_optab;
7662 op->fetch_before = sync_old_xor_optab;
7663 op->fetch_after = sync_new_xor_optab;
7664 op->no_result = sync_xor_optab;
7665 op->reverse_code = XOR;
7666 break;
7667 case AND:
7668 op->mem_fetch_before = atomic_fetch_and_optab;
7669 op->mem_fetch_after = atomic_and_fetch_optab;
7670 op->mem_no_result = atomic_and_optab;
7671 op->fetch_before = sync_old_and_optab;
7672 op->fetch_after = sync_new_and_optab;
7673 op->no_result = sync_and_optab;
7674 op->reverse_code = UNKNOWN;
7675 break;
7676 case IOR:
7677 op->mem_fetch_before = atomic_fetch_or_optab;
7678 op->mem_fetch_after = atomic_or_fetch_optab;
7679 op->mem_no_result = atomic_or_optab;
7680 op->fetch_before = sync_old_ior_optab;
7681 op->fetch_after = sync_new_ior_optab;
7682 op->no_result = sync_ior_optab;
7683 op->reverse_code = UNKNOWN;
7684 break;
7685 case NOT:
7686 op->mem_fetch_before = atomic_fetch_nand_optab;
7687 op->mem_fetch_after = atomic_nand_fetch_optab;
7688 op->mem_no_result = atomic_nand_optab;
7689 op->fetch_before = sync_old_nand_optab;
7690 op->fetch_after = sync_new_nand_optab;
7691 op->no_result = sync_nand_optab;
7692 op->reverse_code = UNKNOWN;
7693 break;
7694 default:
7695 gcc_unreachable ();
7699 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7700 using memory order MODEL. If AFTER is true the operation needs to return
7701 the value of *MEM after the operation, otherwise the previous value.
7702 TARGET is an optional place to place the result. The result is unused if
7703 it is const0_rtx.
7704 Return the result if there is a better sequence, otherwise NULL_RTX. */
7706 static rtx
7707 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7708 enum memmodel model, bool after)
7710 /* If the value is prefetched, or not used, it may be possible to replace
7711 the sequence with a native exchange operation. */
7712 if (!after || target == const0_rtx)
7714 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7715 if (code == AND && val == const0_rtx)
7717 if (target == const0_rtx)
7718 target = gen_reg_rtx (GET_MODE (mem));
7719 return maybe_emit_atomic_exchange (target, mem, val, model);
7722 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7723 if (code == IOR && val == constm1_rtx)
7725 if (target == const0_rtx)
7726 target = gen_reg_rtx (GET_MODE (mem));
7727 return maybe_emit_atomic_exchange (target, mem, val, model);
7731 return NULL_RTX;
7734 /* Try to emit an instruction for a specific operation varaition.
7735 OPTAB contains the OP functions.
7736 TARGET is an optional place to return the result. const0_rtx means unused.
7737 MEM is the memory location to operate on.
7738 VAL is the value to use in the operation.
7739 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7740 MODEL is the memory model, if used.
7741 AFTER is true if the returned result is the value after the operation. */
7743 static rtx
7744 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7745 rtx val, bool use_memmodel, enum memmodel model, bool after)
7747 enum machine_mode mode = GET_MODE (mem);
7748 struct expand_operand ops[4];
7749 enum insn_code icode;
7750 int op_counter = 0;
7751 int num_ops;
7753 /* Check to see if there is a result returned. */
7754 if (target == const0_rtx)
7756 if (use_memmodel)
7758 icode = direct_optab_handler (optab->mem_no_result, mode);
7759 create_integer_operand (&ops[2], model);
7760 num_ops = 3;
7762 else
7764 icode = direct_optab_handler (optab->no_result, mode);
7765 num_ops = 2;
7768 /* Otherwise, we need to generate a result. */
7769 else
7771 if (use_memmodel)
7773 icode = direct_optab_handler (after ? optab->mem_fetch_after
7774 : optab->mem_fetch_before, mode);
7775 create_integer_operand (&ops[3], model);
7776 num_ops = 4;
7778 else
7780 icode = optab_handler (after ? optab->fetch_after
7781 : optab->fetch_before, mode);
7782 num_ops = 3;
7784 create_output_operand (&ops[op_counter++], target, mode);
7786 if (icode == CODE_FOR_nothing)
7787 return NULL_RTX;
7789 create_fixed_operand (&ops[op_counter++], mem);
7790 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7791 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7793 if (maybe_expand_insn (icode, num_ops, ops))
7794 return (target == const0_rtx ? const0_rtx : ops[0].value);
7796 return NULL_RTX;
7800 /* This function expands an atomic fetch_OP or OP_fetch operation:
7801 TARGET is an option place to stick the return value. const0_rtx indicates
7802 the result is unused.
7803 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7804 CODE is the operation being performed (OP)
7805 MEMMODEL is the memory model variant to use.
7806 AFTER is true to return the result of the operation (OP_fetch).
7807 AFTER is false to return the value before the operation (fetch_OP).
7809 This function will *only* generate instructions if there is a direct
7810 optab. No compare and swap loops or libcalls will be generated. */
7812 static rtx
7813 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7814 enum rtx_code code, enum memmodel model,
7815 bool after)
7817 enum machine_mode mode = GET_MODE (mem);
7818 struct atomic_op_functions optab;
7819 rtx result;
7820 bool unused_result = (target == const0_rtx);
7822 get_atomic_op_for_code (&optab, code);
7824 /* Check to see if there are any better instructions. */
7825 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7826 if (result)
7827 return result;
7829 /* Check for the case where the result isn't used and try those patterns. */
7830 if (unused_result)
7832 /* Try the memory model variant first. */
7833 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7834 if (result)
7835 return result;
7837 /* Next try the old style withuot a memory model. */
7838 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7839 if (result)
7840 return result;
7842 /* There is no no-result pattern, so try patterns with a result. */
7843 target = NULL_RTX;
7846 /* Try the __atomic version. */
7847 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
7848 if (result)
7849 return result;
7851 /* Try the older __sync version. */
7852 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
7853 if (result)
7854 return result;
7856 /* If the fetch value can be calculated from the other variation of fetch,
7857 try that operation. */
7858 if (after || unused_result || optab.reverse_code != UNKNOWN)
7860 /* Try the __atomic version, then the older __sync version. */
7861 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
7862 if (!result)
7863 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
7865 if (result)
7867 /* If the result isn't used, no need to do compensation code. */
7868 if (unused_result)
7869 return result;
7871 /* Issue compensation code. Fetch_after == fetch_before OP val.
7872 Fetch_before == after REVERSE_OP val. */
7873 if (!after)
7874 code = optab.reverse_code;
7875 if (code == NOT)
7877 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
7878 true, OPTAB_LIB_WIDEN);
7879 result = expand_simple_unop (mode, NOT, result, target, true);
7881 else
7882 result = expand_simple_binop (mode, code, result, val, target,
7883 true, OPTAB_LIB_WIDEN);
7884 return result;
7888 /* No direct opcode can be generated. */
7889 return NULL_RTX;
7894 /* This function expands an atomic fetch_OP or OP_fetch operation:
7895 TARGET is an option place to stick the return value. const0_rtx indicates
7896 the result is unused.
7897 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7898 CODE is the operation being performed (OP)
7899 MEMMODEL is the memory model variant to use.
7900 AFTER is true to return the result of the operation (OP_fetch).
7901 AFTER is false to return the value before the operation (fetch_OP). */
7903 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7904 enum memmodel model, bool after)
7906 enum machine_mode mode = GET_MODE (mem);
7907 rtx result;
7908 bool unused_result = (target == const0_rtx);
7910 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
7911 after);
7913 if (result)
7914 return result;
7916 /* Add/sub can be implemented by doing the reverse operation with -(val). */
7917 if (code == PLUS || code == MINUS)
7919 rtx tmp;
7920 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
7922 start_sequence ();
7923 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
7924 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
7925 model, after);
7926 if (result)
7928 /* PLUS worked so emit the insns and return. */
7929 tmp = get_insns ();
7930 end_sequence ();
7931 emit_insn (tmp);
7932 return result;
7935 /* PLUS did not work, so throw away the negation code and continue. */
7936 end_sequence ();
7939 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
7940 if (!can_compare_and_swap_p (mode, false))
7942 rtx libfunc;
7943 bool fixup = false;
7944 enum rtx_code orig_code = code;
7945 struct atomic_op_functions optab;
7947 get_atomic_op_for_code (&optab, code);
7948 libfunc = optab_libfunc (after ? optab.fetch_after
7949 : optab.fetch_before, mode);
7950 if (libfunc == NULL
7951 && (after || unused_result || optab.reverse_code != UNKNOWN))
7953 fixup = true;
7954 if (!after)
7955 code = optab.reverse_code;
7956 libfunc = optab_libfunc (after ? optab.fetch_before
7957 : optab.fetch_after, mode);
7959 if (libfunc != NULL)
7961 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7962 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
7963 2, addr, ptr_mode, val, mode);
7965 if (!unused_result && fixup)
7966 result = expand_simple_binop (mode, code, result, val, target,
7967 true, OPTAB_LIB_WIDEN);
7968 return result;
7971 /* We need the original code for any further attempts. */
7972 code = orig_code;
7975 /* If nothing else has succeeded, default to a compare and swap loop. */
7976 if (can_compare_and_swap_p (mode, true))
7978 rtx insn;
7979 rtx t0 = gen_reg_rtx (mode), t1;
7981 start_sequence ();
7983 /* If the result is used, get a register for it. */
7984 if (!unused_result)
7986 if (!target || !register_operand (target, mode))
7987 target = gen_reg_rtx (mode);
7988 /* If fetch_before, copy the value now. */
7989 if (!after)
7990 emit_move_insn (target, t0);
7992 else
7993 target = const0_rtx;
7995 t1 = t0;
7996 if (code == NOT)
7998 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
7999 true, OPTAB_LIB_WIDEN);
8000 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
8002 else
8003 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
8004 OPTAB_LIB_WIDEN);
8006 /* For after, copy the value now. */
8007 if (!unused_result && after)
8008 emit_move_insn (target, t1);
8009 insn = get_insns ();
8010 end_sequence ();
8012 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
8013 return target;
8016 return NULL_RTX;
8019 /* Return true if OPERAND is suitable for operand number OPNO of
8020 instruction ICODE. */
8022 bool
8023 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
8025 return (!insn_data[(int) icode].operand[opno].predicate
8026 || (insn_data[(int) icode].operand[opno].predicate
8027 (operand, insn_data[(int) icode].operand[opno].mode)));
8030 /* TARGET is a target of a multiword operation that we are going to
8031 implement as a series of word-mode operations. Return true if
8032 TARGET is suitable for this purpose. */
8034 bool
8035 valid_multiword_target_p (rtx target)
8037 enum machine_mode mode;
8038 int i;
8040 mode = GET_MODE (target);
8041 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
8042 if (!validate_subreg (word_mode, mode, target, i))
8043 return false;
8044 return true;
8047 /* Like maybe_legitimize_operand, but do not change the code of the
8048 current rtx value. */
8050 static bool
8051 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8052 struct expand_operand *op)
8054 /* See if the operand matches in its current form. */
8055 if (insn_operand_matches (icode, opno, op->value))
8056 return true;
8058 /* If the operand is a memory whose address has no side effects,
8059 try forcing the address into a non-virtual pseudo register.
8060 The check for side effects is important because copy_to_mode_reg
8061 cannot handle things like auto-modified addresses. */
8062 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8064 rtx addr, mem;
8066 mem = op->value;
8067 addr = XEXP (mem, 0);
8068 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8069 && !side_effects_p (addr))
8071 rtx last;
8072 enum machine_mode mode;
8074 last = get_last_insn ();
8075 mode = get_address_mode (mem);
8076 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8077 if (insn_operand_matches (icode, opno, mem))
8079 op->value = mem;
8080 return true;
8082 delete_insns_since (last);
8086 return false;
8089 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8090 on success, storing the new operand value back in OP. */
8092 static bool
8093 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8094 struct expand_operand *op)
8096 enum machine_mode mode, imode;
8097 bool old_volatile_ok, result;
8099 mode = op->mode;
8100 switch (op->type)
8102 case EXPAND_FIXED:
8103 old_volatile_ok = volatile_ok;
8104 volatile_ok = true;
8105 result = maybe_legitimize_operand_same_code (icode, opno, op);
8106 volatile_ok = old_volatile_ok;
8107 return result;
8109 case EXPAND_OUTPUT:
8110 gcc_assert (mode != VOIDmode);
8111 if (op->value
8112 && op->value != const0_rtx
8113 && GET_MODE (op->value) == mode
8114 && maybe_legitimize_operand_same_code (icode, opno, op))
8115 return true;
8117 op->value = gen_reg_rtx (mode);
8118 break;
8120 case EXPAND_INPUT:
8121 input:
8122 gcc_assert (mode != VOIDmode);
8123 gcc_assert (GET_MODE (op->value) == VOIDmode
8124 || GET_MODE (op->value) == mode);
8125 if (maybe_legitimize_operand_same_code (icode, opno, op))
8126 return true;
8128 op->value = copy_to_mode_reg (mode, op->value);
8129 break;
8131 case EXPAND_CONVERT_TO:
8132 gcc_assert (mode != VOIDmode);
8133 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8134 goto input;
8136 case EXPAND_CONVERT_FROM:
8137 if (GET_MODE (op->value) != VOIDmode)
8138 mode = GET_MODE (op->value);
8139 else
8140 /* The caller must tell us what mode this value has. */
8141 gcc_assert (mode != VOIDmode);
8143 imode = insn_data[(int) icode].operand[opno].mode;
8144 if (imode != VOIDmode && imode != mode)
8146 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8147 mode = imode;
8149 goto input;
8151 case EXPAND_ADDRESS:
8152 gcc_assert (mode != VOIDmode);
8153 op->value = convert_memory_address (mode, op->value);
8154 goto input;
8156 case EXPAND_INTEGER:
8157 mode = insn_data[(int) icode].operand[opno].mode;
8158 if (mode != VOIDmode && const_int_operand (op->value, mode))
8159 goto input;
8160 break;
8162 return insn_operand_matches (icode, opno, op->value);
8165 /* Make OP describe an input operand that should have the same value
8166 as VALUE, after any mode conversion that the target might request.
8167 TYPE is the type of VALUE. */
8169 void
8170 create_convert_operand_from_type (struct expand_operand *op,
8171 rtx value, tree type)
8173 create_convert_operand_from (op, value, TYPE_MODE (type),
8174 TYPE_UNSIGNED (type));
8177 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8178 of instruction ICODE. Return true on success, leaving the new operand
8179 values in the OPS themselves. Emit no code on failure. */
8181 bool
8182 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8183 unsigned int nops, struct expand_operand *ops)
8185 rtx last;
8186 unsigned int i;
8188 last = get_last_insn ();
8189 for (i = 0; i < nops; i++)
8190 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8192 delete_insns_since (last);
8193 return false;
8195 return true;
8198 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8199 as its operands. Return the instruction pattern on success,
8200 and emit any necessary set-up code. Return null and emit no
8201 code on failure. */
8204 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8205 struct expand_operand *ops)
8207 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8208 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8209 return NULL_RTX;
8211 switch (nops)
8213 case 1:
8214 return GEN_FCN (icode) (ops[0].value);
8215 case 2:
8216 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8217 case 3:
8218 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8219 case 4:
8220 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8221 ops[3].value);
8222 case 5:
8223 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8224 ops[3].value, ops[4].value);
8225 case 6:
8226 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8227 ops[3].value, ops[4].value, ops[5].value);
8228 case 7:
8229 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8230 ops[3].value, ops[4].value, ops[5].value,
8231 ops[6].value);
8232 case 8:
8233 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8234 ops[3].value, ops[4].value, ops[5].value,
8235 ops[6].value, ops[7].value);
8237 gcc_unreachable ();
8240 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8241 as its operands. Return true on success and emit no code on failure. */
8243 bool
8244 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8245 struct expand_operand *ops)
8247 rtx pat = maybe_gen_insn (icode, nops, ops);
8248 if (pat)
8250 emit_insn (pat);
8251 return true;
8253 return false;
8256 /* Like maybe_expand_insn, but for jumps. */
8258 bool
8259 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8260 struct expand_operand *ops)
8262 rtx pat = maybe_gen_insn (icode, nops, ops);
8263 if (pat)
8265 emit_jump_insn (pat);
8266 return true;
8268 return false;
8271 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8272 as its operands. */
8274 void
8275 expand_insn (enum insn_code icode, unsigned int nops,
8276 struct expand_operand *ops)
8278 if (!maybe_expand_insn (icode, nops, ops))
8279 gcc_unreachable ();
8282 /* Like expand_insn, but for jumps. */
8284 void
8285 expand_jump_insn (enum insn_code icode, unsigned int nops,
8286 struct expand_operand *ops)
8288 if (!maybe_expand_jump_insn (icode, nops, ops))
8289 gcc_unreachable ();
8292 /* Reduce conditional compilation elsewhere. */
8293 #ifndef HAVE_insv
8294 #define HAVE_insv 0
8295 #define CODE_FOR_insv CODE_FOR_nothing
8296 #endif
8297 #ifndef HAVE_extv
8298 #define HAVE_extv 0
8299 #define CODE_FOR_extv CODE_FOR_nothing
8300 #endif
8301 #ifndef HAVE_extzv
8302 #define HAVE_extzv 0
8303 #define CODE_FOR_extzv CODE_FOR_nothing
8304 #endif
8306 /* Enumerates the possible types of structure operand to an
8307 extraction_insn. */
8308 enum extraction_type { ET_unaligned_mem, ET_reg };
8310 /* Check whether insv, extv or extzv pattern ICODE can be used for an
8311 insertion or extraction of type TYPE on a structure of mode MODE.
8312 Return true if so and fill in *INSN accordingly. STRUCT_OP is the
8313 operand number of the structure (the first sign_extract or zero_extract
8314 operand) and FIELD_OP is the operand number of the field (the other
8315 side of the set from the sign_extract or zero_extract). */
8317 static bool
8318 get_traditional_extraction_insn (extraction_insn *insn,
8319 enum extraction_type type,
8320 enum machine_mode mode,
8321 enum insn_code icode,
8322 int struct_op, int field_op)
8324 const struct insn_data_d *data = &insn_data[icode];
8326 enum machine_mode struct_mode = data->operand[struct_op].mode;
8327 if (struct_mode == VOIDmode)
8328 struct_mode = word_mode;
8329 if (mode != struct_mode)
8330 return false;
8332 enum machine_mode field_mode = data->operand[field_op].mode;
8333 if (field_mode == VOIDmode)
8334 field_mode = word_mode;
8336 enum machine_mode pos_mode = data->operand[struct_op + 2].mode;
8337 if (pos_mode == VOIDmode)
8338 pos_mode = word_mode;
8340 insn->icode = icode;
8341 insn->field_mode = field_mode;
8342 insn->struct_mode = (type == ET_unaligned_mem ? byte_mode : struct_mode);
8343 insn->pos_mode = pos_mode;
8344 return true;
8347 /* Return true if an optab exists to perform an insertion or extraction
8348 of type TYPE in mode MODE. Describe the instruction in *INSN if so.
8350 REG_OPTAB is the optab to use for register structures and
8351 MISALIGN_OPTAB is the optab to use for misaligned memory structures.
8352 POS_OP is the operand number of the bit position. */
8354 static bool
8355 get_optab_extraction_insn (struct extraction_insn *insn,
8356 enum extraction_type type,
8357 enum machine_mode mode, direct_optab reg_optab,
8358 direct_optab misalign_optab, int pos_op)
8360 direct_optab optab = (type == ET_unaligned_mem ? misalign_optab : reg_optab);
8361 enum insn_code icode = direct_optab_handler (optab, mode);
8362 if (icode == CODE_FOR_nothing)
8363 return false;
8365 const struct insn_data_d *data = &insn_data[icode];
8367 insn->icode = icode;
8368 insn->field_mode = mode;
8369 insn->struct_mode = (type == ET_unaligned_mem ? BLKmode : mode);
8370 insn->pos_mode = data->operand[pos_op].mode;
8371 if (insn->pos_mode == VOIDmode)
8372 insn->pos_mode = word_mode;
8373 return true;
8376 /* Return true if an instruction exists to perform an insertion or
8377 extraction (PATTERN says which) of type TYPE in mode MODE.
8378 Describe the instruction in *INSN if so. */
8380 static bool
8381 get_extraction_insn (extraction_insn *insn,
8382 enum extraction_pattern pattern,
8383 enum extraction_type type,
8384 enum machine_mode mode)
8386 switch (pattern)
8388 case EP_insv:
8389 if (HAVE_insv
8390 && get_traditional_extraction_insn (insn, type, mode,
8391 CODE_FOR_insv, 0, 3))
8392 return true;
8393 return get_optab_extraction_insn (insn, type, mode, insv_optab,
8394 insvmisalign_optab, 2);
8396 case EP_extv:
8397 if (HAVE_extv
8398 && get_traditional_extraction_insn (insn, type, mode,
8399 CODE_FOR_extv, 1, 0))
8400 return true;
8401 return get_optab_extraction_insn (insn, type, mode, extv_optab,
8402 extvmisalign_optab, 3);
8404 case EP_extzv:
8405 if (HAVE_extzv
8406 && get_traditional_extraction_insn (insn, type, mode,
8407 CODE_FOR_extzv, 1, 0))
8408 return true;
8409 return get_optab_extraction_insn (insn, type, mode, extzv_optab,
8410 extzvmisalign_optab, 3);
8412 default:
8413 gcc_unreachable ();
8417 /* Return true if an instruction exists to access a field of mode
8418 FIELDMODE in a structure that has STRUCT_BITS significant bits.
8419 Describe the "best" such instruction in *INSN if so. PATTERN and
8420 TYPE describe the type of insertion or extraction we want to perform.
8422 For an insertion, the number of significant structure bits includes
8423 all bits of the target. For an extraction, it need only include the
8424 most significant bit of the field. Larger widths are acceptable
8425 in both cases. */
8427 static bool
8428 get_best_extraction_insn (extraction_insn *insn,
8429 enum extraction_pattern pattern,
8430 enum extraction_type type,
8431 unsigned HOST_WIDE_INT struct_bits,
8432 enum machine_mode field_mode)
8434 enum machine_mode mode = smallest_mode_for_size (struct_bits, MODE_INT);
8435 while (mode != VOIDmode)
8437 if (get_extraction_insn (insn, pattern, type, mode))
8439 while (mode != VOIDmode
8440 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (field_mode)
8441 && !TRULY_NOOP_TRUNCATION_MODES_P (insn->field_mode,
8442 field_mode))
8444 get_extraction_insn (insn, pattern, type, mode);
8445 mode = GET_MODE_WIDER_MODE (mode);
8447 return true;
8449 mode = GET_MODE_WIDER_MODE (mode);
8451 return false;
8454 /* Return true if an instruction exists to access a field of mode
8455 FIELDMODE in a register structure that has STRUCT_BITS significant bits.
8456 Describe the "best" such instruction in *INSN if so. PATTERN describes
8457 the type of insertion or extraction we want to perform.
8459 For an insertion, the number of significant structure bits includes
8460 all bits of the target. For an extraction, it need only include the
8461 most significant bit of the field. Larger widths are acceptable
8462 in both cases. */
8464 bool
8465 get_best_reg_extraction_insn (extraction_insn *insn,
8466 enum extraction_pattern pattern,
8467 unsigned HOST_WIDE_INT struct_bits,
8468 enum machine_mode field_mode)
8470 return get_best_extraction_insn (insn, pattern, ET_reg, struct_bits,
8471 field_mode);
8474 /* Return true if an instruction exists to access a field of BITSIZE
8475 bits starting BITNUM bits into a memory structure. Describe the
8476 "best" such instruction in *INSN if so. PATTERN describes the type
8477 of insertion or extraction we want to perform and FIELDMODE is the
8478 natural mode of the extracted field.
8480 The instructions considered here only access bytes that overlap
8481 the bitfield; they do not touch any surrounding bytes. */
8483 bool
8484 get_best_mem_extraction_insn (extraction_insn *insn,
8485 enum extraction_pattern pattern,
8486 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitnum,
8487 enum machine_mode field_mode)
8489 unsigned HOST_WIDE_INT struct_bits = (bitnum % BITS_PER_UNIT
8490 + bitsize
8491 + BITS_PER_UNIT - 1);
8492 struct_bits -= struct_bits % BITS_PER_UNIT;
8493 return get_best_extraction_insn (insn, pattern, ET_unaligned_mem,
8494 struct_bits, field_mode);
8497 #include "gt-optabs.h"