Merge aosp-toolchain/gcc/gcc-4_9 changes.
[official-gcc.git] / gcc-4_9-mobile / gcc / optabs.c
blob39e8cd4f64a32984ec0bb61ea0c0aeaaa2ad224c
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "diagnostic-core.h"
27 /* Include insn-config.h before expr.h so that HAVE_conditional_move
28 is properly defined. */
29 #include "insn-config.h"
30 #include "rtl.h"
31 #include "tree.h"
32 #include "stor-layout.h"
33 #include "stringpool.h"
34 #include "varasm.h"
35 #include "tm_p.h"
36 #include "flags.h"
37 #include "function.h"
38 #include "except.h"
39 #include "expr.h"
40 #include "optabs.h"
41 #include "libfuncs.h"
42 #include "recog.h"
43 #include "reload.h"
44 #include "ggc.h"
45 #include "basic-block.h"
46 #include "target.h"
48 struct target_optabs default_target_optabs;
49 struct target_libfuncs default_target_libfuncs;
50 struct target_optabs *this_fn_optabs = &default_target_optabs;
51 #if SWITCHABLE_TARGET
52 struct target_optabs *this_target_optabs = &default_target_optabs;
53 struct target_libfuncs *this_target_libfuncs = &default_target_libfuncs;
54 #endif
56 #define libfunc_hash \
57 (this_target_libfuncs->x_libfunc_hash)
59 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
60 enum machine_mode *);
61 static rtx expand_unop_direct (enum machine_mode, optab, rtx, rtx, int);
62 static void emit_libcall_block_1 (rtx, rtx, rtx, rtx, bool);
64 /* Debug facility for use in GDB. */
65 void debug_optab_libfuncs (void);
67 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
68 #if ENABLE_DECIMAL_BID_FORMAT
69 #define DECIMAL_PREFIX "bid_"
70 #else
71 #define DECIMAL_PREFIX "dpd_"
72 #endif
74 /* Used for libfunc_hash. */
76 static hashval_t
77 hash_libfunc (const void *p)
79 const struct libfunc_entry *const e = (const struct libfunc_entry *) p;
80 return ((e->mode1 + e->mode2 * NUM_MACHINE_MODES) ^ e->op);
83 /* Used for libfunc_hash. */
85 static int
86 eq_libfunc (const void *p, const void *q)
88 const struct libfunc_entry *const e1 = (const struct libfunc_entry *) p;
89 const struct libfunc_entry *const e2 = (const struct libfunc_entry *) q;
90 return e1->op == e2->op && e1->mode1 == e2->mode1 && e1->mode2 == e2->mode2;
93 /* Return libfunc corresponding operation defined by OPTAB converting
94 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
95 if no libfunc is available. */
96 rtx
97 convert_optab_libfunc (convert_optab optab, enum machine_mode mode1,
98 enum machine_mode mode2)
100 struct libfunc_entry e;
101 struct libfunc_entry **slot;
103 /* ??? This ought to be an assert, but not all of the places
104 that we expand optabs know about the optabs that got moved
105 to being direct. */
106 if (!(optab >= FIRST_CONV_OPTAB && optab <= LAST_CONVLIB_OPTAB))
107 return NULL_RTX;
109 e.op = optab;
110 e.mode1 = mode1;
111 e.mode2 = mode2;
112 slot = (struct libfunc_entry **)
113 htab_find_slot (libfunc_hash, &e, NO_INSERT);
114 if (!slot)
116 const struct convert_optab_libcall_d *d
117 = &convlib_def[optab - FIRST_CONV_OPTAB];
119 if (d->libcall_gen == NULL)
120 return NULL;
122 d->libcall_gen (optab, d->libcall_basename, mode1, mode2);
123 slot = (struct libfunc_entry **)
124 htab_find_slot (libfunc_hash, &e, NO_INSERT);
125 if (!slot)
126 return NULL;
128 return (*slot)->libfunc;
131 /* Return libfunc corresponding operation defined by OPTAB in MODE.
132 Trigger lazy initialization if needed, return NULL if no libfunc is
133 available. */
135 optab_libfunc (optab optab, enum machine_mode mode)
137 struct libfunc_entry e;
138 struct libfunc_entry **slot;
140 /* ??? This ought to be an assert, but not all of the places
141 that we expand optabs know about the optabs that got moved
142 to being direct. */
143 if (!(optab >= FIRST_NORM_OPTAB && optab <= LAST_NORMLIB_OPTAB))
144 return NULL_RTX;
146 e.op = optab;
147 e.mode1 = mode;
148 e.mode2 = VOIDmode;
149 slot = (struct libfunc_entry **)
150 htab_find_slot (libfunc_hash, &e, NO_INSERT);
151 if (!slot)
153 const struct optab_libcall_d *d
154 = &normlib_def[optab - FIRST_NORM_OPTAB];
156 if (d->libcall_gen == NULL)
157 return NULL;
159 d->libcall_gen (optab, d->libcall_basename, d->libcall_suffix, mode);
160 slot = (struct libfunc_entry **)
161 htab_find_slot (libfunc_hash, &e, NO_INSERT);
162 if (!slot)
163 return NULL;
165 return (*slot)->libfunc;
169 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
170 the result of operation CODE applied to OP0 (and OP1 if it is a binary
171 operation).
173 If the last insn does not set TARGET, don't do anything, but return 1.
175 If the last insn or a previous insn sets TARGET and TARGET is one of OP0
176 or OP1, don't add the REG_EQUAL note but return 0. Our caller can then
177 try again, ensuring that TARGET is not one of the operands. */
179 static int
180 add_equal_note (rtx insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
182 rtx last_insn, set;
183 rtx note;
185 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
187 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
188 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
189 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
190 && GET_RTX_CLASS (code) != RTX_COMPARE
191 && GET_RTX_CLASS (code) != RTX_UNARY)
192 return 1;
194 if (GET_CODE (target) == ZERO_EXTRACT)
195 return 1;
197 for (last_insn = insns;
198 NEXT_INSN (last_insn) != NULL_RTX;
199 last_insn = NEXT_INSN (last_insn))
202 /* If TARGET is in OP0 or OP1, punt. We'd end up with a note referencing
203 a value changing in the insn, so the note would be invalid for CSE. */
204 if (reg_overlap_mentioned_p (target, op0)
205 || (op1 && reg_overlap_mentioned_p (target, op1)))
207 if (MEM_P (target)
208 && (rtx_equal_p (target, op0)
209 || (op1 && rtx_equal_p (target, op1))))
211 /* For MEM target, with MEM = MEM op X, prefer no REG_EQUAL note
212 over expanding it as temp = MEM op X, MEM = temp. If the target
213 supports MEM = MEM op X instructions, it is sometimes too hard
214 to reconstruct that form later, especially if X is also a memory,
215 and due to multiple occurrences of addresses the address might
216 be forced into register unnecessarily.
217 Note that not emitting the REG_EQUIV note might inhibit
218 CSE in some cases. */
219 set = single_set (last_insn);
220 if (set
221 && GET_CODE (SET_SRC (set)) == code
222 && MEM_P (SET_DEST (set))
223 && (rtx_equal_p (SET_DEST (set), XEXP (SET_SRC (set), 0))
224 || (op1 && rtx_equal_p (SET_DEST (set),
225 XEXP (SET_SRC (set), 1)))))
226 return 1;
228 return 0;
231 set = single_set (last_insn);
232 if (set == NULL_RTX)
233 return 1;
235 if (! rtx_equal_p (SET_DEST (set), target)
236 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
237 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
238 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
239 return 1;
241 if (GET_RTX_CLASS (code) == RTX_UNARY)
242 switch (code)
244 case FFS:
245 case CLZ:
246 case CTZ:
247 case CLRSB:
248 case POPCOUNT:
249 case PARITY:
250 case BSWAP:
251 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
253 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
254 if (GET_MODE_SIZE (GET_MODE (op0))
255 > GET_MODE_SIZE (GET_MODE (target)))
256 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
257 note, GET_MODE (op0));
258 else
259 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
260 note, GET_MODE (op0));
261 break;
263 /* FALLTHRU */
264 default:
265 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
266 break;
268 else
269 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
271 set_unique_reg_note (last_insn, REG_EQUAL, note);
273 return 1;
276 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
277 for a widening operation would be. In most cases this would be OP0, but if
278 that's a constant it'll be VOIDmode, which isn't useful. */
280 static enum machine_mode
281 widened_mode (enum machine_mode to_mode, rtx op0, rtx op1)
283 enum machine_mode m0 = GET_MODE (op0);
284 enum machine_mode m1 = GET_MODE (op1);
285 enum machine_mode result;
287 if (m0 == VOIDmode && m1 == VOIDmode)
288 return to_mode;
289 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
290 result = m1;
291 else
292 result = m0;
294 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
295 return to_mode;
297 return result;
300 /* Find a widening optab even if it doesn't widen as much as we want.
301 E.g. if from_mode is HImode, and to_mode is DImode, and there is no
302 direct HI->SI insn, then return SI->DI, if that exists.
303 If PERMIT_NON_WIDENING is non-zero then this can be used with
304 non-widening optabs also. */
306 enum insn_code
307 find_widening_optab_handler_and_mode (optab op, enum machine_mode to_mode,
308 enum machine_mode from_mode,
309 int permit_non_widening,
310 enum machine_mode *found_mode)
312 for (; (permit_non_widening || from_mode != to_mode)
313 && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
314 && from_mode != VOIDmode;
315 from_mode = GET_MODE_WIDER_MODE (from_mode))
317 enum insn_code handler = widening_optab_handler (op, to_mode,
318 from_mode);
320 if (handler != CODE_FOR_nothing)
322 if (found_mode)
323 *found_mode = from_mode;
324 return handler;
328 return CODE_FOR_nothing;
331 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
332 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
333 not actually do a sign-extend or zero-extend, but can leave the
334 higher-order bits of the result rtx undefined, for example, in the case
335 of logical operations, but not right shifts. */
337 static rtx
338 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
339 int unsignedp, int no_extend)
341 rtx result;
343 /* If we don't have to extend and this is a constant, return it. */
344 if (no_extend && GET_MODE (op) == VOIDmode)
345 return op;
347 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
348 extend since it will be more efficient to do so unless the signedness of
349 a promoted object differs from our extension. */
350 if (! no_extend
351 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
352 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
353 return convert_modes (mode, oldmode, op, unsignedp);
355 /* If MODE is no wider than a single word, we return a lowpart or paradoxical
356 SUBREG. */
357 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
358 return gen_lowpart (mode, force_reg (GET_MODE (op), op));
360 /* Otherwise, get an object of MODE, clobber it, and set the low-order
361 part to OP. */
363 result = gen_reg_rtx (mode);
364 emit_clobber (result);
365 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
366 return result;
369 /* Return the optab used for computing the operation given by the tree code,
370 CODE and the tree EXP. This function is not always usable (for example, it
371 cannot give complete results for multiplication or division) but probably
372 ought to be relied on more widely throughout the expander. */
373 optab
374 optab_for_tree_code (enum tree_code code, const_tree type,
375 enum optab_subtype subtype)
377 bool trapv;
378 switch (code)
380 case BIT_AND_EXPR:
381 return and_optab;
383 case BIT_IOR_EXPR:
384 return ior_optab;
386 case BIT_NOT_EXPR:
387 return one_cmpl_optab;
389 case BIT_XOR_EXPR:
390 return xor_optab;
392 case MULT_HIGHPART_EXPR:
393 return TYPE_UNSIGNED (type) ? umul_highpart_optab : smul_highpart_optab;
395 case TRUNC_MOD_EXPR:
396 case CEIL_MOD_EXPR:
397 case FLOOR_MOD_EXPR:
398 case ROUND_MOD_EXPR:
399 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
401 case RDIV_EXPR:
402 case TRUNC_DIV_EXPR:
403 case CEIL_DIV_EXPR:
404 case FLOOR_DIV_EXPR:
405 case ROUND_DIV_EXPR:
406 case EXACT_DIV_EXPR:
407 if (TYPE_SATURATING (type))
408 return TYPE_UNSIGNED (type) ? usdiv_optab : ssdiv_optab;
409 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
411 case LSHIFT_EXPR:
412 if (TREE_CODE (type) == VECTOR_TYPE)
414 if (subtype == optab_vector)
415 return TYPE_SATURATING (type) ? unknown_optab : vashl_optab;
417 gcc_assert (subtype == optab_scalar);
419 if (TYPE_SATURATING (type))
420 return TYPE_UNSIGNED (type) ? usashl_optab : ssashl_optab;
421 return ashl_optab;
423 case RSHIFT_EXPR:
424 if (TREE_CODE (type) == VECTOR_TYPE)
426 if (subtype == optab_vector)
427 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
429 gcc_assert (subtype == optab_scalar);
431 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
433 case LROTATE_EXPR:
434 if (TREE_CODE (type) == VECTOR_TYPE)
436 if (subtype == optab_vector)
437 return vrotl_optab;
439 gcc_assert (subtype == optab_scalar);
441 return rotl_optab;
443 case RROTATE_EXPR:
444 if (TREE_CODE (type) == VECTOR_TYPE)
446 if (subtype == optab_vector)
447 return vrotr_optab;
449 gcc_assert (subtype == optab_scalar);
451 return rotr_optab;
453 case MAX_EXPR:
454 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
456 case MIN_EXPR:
457 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
459 case REALIGN_LOAD_EXPR:
460 return vec_realign_load_optab;
462 case WIDEN_SUM_EXPR:
463 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
465 case DOT_PROD_EXPR:
466 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
468 case SAD_EXPR:
469 return TYPE_UNSIGNED (type) ? usad_optab : ssad_optab;
471 case WIDEN_MULT_PLUS_EXPR:
472 return (TYPE_UNSIGNED (type)
473 ? (TYPE_SATURATING (type)
474 ? usmadd_widen_optab : umadd_widen_optab)
475 : (TYPE_SATURATING (type)
476 ? ssmadd_widen_optab : smadd_widen_optab));
478 case WIDEN_MULT_MINUS_EXPR:
479 return (TYPE_UNSIGNED (type)
480 ? (TYPE_SATURATING (type)
481 ? usmsub_widen_optab : umsub_widen_optab)
482 : (TYPE_SATURATING (type)
483 ? ssmsub_widen_optab : smsub_widen_optab));
485 case FMA_EXPR:
486 return fma_optab;
488 case REDUC_MAX_EXPR:
489 return TYPE_UNSIGNED (type) ? reduc_umax_optab : reduc_smax_optab;
491 case REDUC_MIN_EXPR:
492 return TYPE_UNSIGNED (type) ? reduc_umin_optab : reduc_smin_optab;
494 case REDUC_PLUS_EXPR:
495 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
497 case VEC_LSHIFT_EXPR:
498 return vec_shl_optab;
500 case VEC_RSHIFT_EXPR:
501 return vec_shr_optab;
503 case VEC_WIDEN_MULT_HI_EXPR:
504 return TYPE_UNSIGNED (type) ?
505 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
507 case VEC_WIDEN_MULT_LO_EXPR:
508 return TYPE_UNSIGNED (type) ?
509 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
511 case VEC_WIDEN_MULT_EVEN_EXPR:
512 return TYPE_UNSIGNED (type) ?
513 vec_widen_umult_even_optab : vec_widen_smult_even_optab;
515 case VEC_WIDEN_MULT_ODD_EXPR:
516 return TYPE_UNSIGNED (type) ?
517 vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
519 case VEC_WIDEN_LSHIFT_HI_EXPR:
520 return TYPE_UNSIGNED (type) ?
521 vec_widen_ushiftl_hi_optab : vec_widen_sshiftl_hi_optab;
523 case VEC_WIDEN_LSHIFT_LO_EXPR:
524 return TYPE_UNSIGNED (type) ?
525 vec_widen_ushiftl_lo_optab : vec_widen_sshiftl_lo_optab;
527 case VEC_UNPACK_HI_EXPR:
528 return TYPE_UNSIGNED (type) ?
529 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
531 case VEC_UNPACK_LO_EXPR:
532 return TYPE_UNSIGNED (type) ?
533 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
535 case VEC_UNPACK_FLOAT_HI_EXPR:
536 /* The signedness is determined from input operand. */
537 return TYPE_UNSIGNED (type) ?
538 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
540 case VEC_UNPACK_FLOAT_LO_EXPR:
541 /* The signedness is determined from input operand. */
542 return TYPE_UNSIGNED (type) ?
543 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
545 case VEC_PACK_TRUNC_EXPR:
546 return vec_pack_trunc_optab;
548 case VEC_PACK_SAT_EXPR:
549 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
551 case VEC_PACK_FIX_TRUNC_EXPR:
552 /* The signedness is determined from output operand. */
553 return TYPE_UNSIGNED (type) ?
554 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
556 default:
557 break;
560 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
561 switch (code)
563 case POINTER_PLUS_EXPR:
564 case PLUS_EXPR:
565 if (TYPE_SATURATING (type))
566 return TYPE_UNSIGNED (type) ? usadd_optab : ssadd_optab;
567 return trapv ? addv_optab : add_optab;
569 case MINUS_EXPR:
570 if (TYPE_SATURATING (type))
571 return TYPE_UNSIGNED (type) ? ussub_optab : sssub_optab;
572 return trapv ? subv_optab : sub_optab;
574 case MULT_EXPR:
575 if (TYPE_SATURATING (type))
576 return TYPE_UNSIGNED (type) ? usmul_optab : ssmul_optab;
577 return trapv ? smulv_optab : smul_optab;
579 case NEGATE_EXPR:
580 if (TYPE_SATURATING (type))
581 return TYPE_UNSIGNED (type) ? usneg_optab : ssneg_optab;
582 return trapv ? negv_optab : neg_optab;
584 case ABS_EXPR:
585 return trapv ? absv_optab : abs_optab;
587 default:
588 return unknown_optab;
593 /* Expand vector widening operations.
595 There are two different classes of operations handled here:
596 1) Operations whose result is wider than all the arguments to the operation.
597 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
598 In this case OP0 and optionally OP1 would be initialized,
599 but WIDE_OP wouldn't (not relevant for this case).
600 2) Operations whose result is of the same size as the last argument to the
601 operation, but wider than all the other arguments to the operation.
602 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
603 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
605 E.g, when called to expand the following operations, this is how
606 the arguments will be initialized:
607 nops OP0 OP1 WIDE_OP
608 widening-sum 2 oprnd0 - oprnd1
609 widening-dot-product 3 oprnd0 oprnd1 oprnd2
610 widening-mult 2 oprnd0 oprnd1 -
611 type-promotion (vec-unpack) 1 oprnd0 - - */
614 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
615 rtx target, int unsignedp)
617 struct expand_operand eops[4];
618 tree oprnd0, oprnd1, oprnd2;
619 enum machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
620 optab widen_pattern_optab;
621 enum insn_code icode;
622 int nops = TREE_CODE_LENGTH (ops->code);
623 int op;
625 oprnd0 = ops->op0;
626 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
627 widen_pattern_optab =
628 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
629 if (ops->code == WIDEN_MULT_PLUS_EXPR
630 || ops->code == WIDEN_MULT_MINUS_EXPR)
631 icode = find_widening_optab_handler (widen_pattern_optab,
632 TYPE_MODE (TREE_TYPE (ops->op2)),
633 tmode0, 0);
634 else
635 icode = optab_handler (widen_pattern_optab, tmode0);
636 gcc_assert (icode != CODE_FOR_nothing);
638 if (nops >= 2)
640 oprnd1 = ops->op1;
641 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
644 /* The last operand is of a wider mode than the rest of the operands. */
645 if (nops == 2)
646 wmode = tmode1;
647 else if (nops == 3)
649 gcc_assert (tmode1 == tmode0);
650 gcc_assert (op1);
651 oprnd2 = ops->op2;
652 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
655 op = 0;
656 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
657 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
658 if (op1)
659 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
660 if (wide_op)
661 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
662 expand_insn (icode, op, eops);
663 return eops[0].value;
666 /* Generate code to perform an operation specified by TERNARY_OPTAB
667 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
669 UNSIGNEDP is for the case where we have to widen the operands
670 to perform the operation. It says to use zero-extension.
672 If TARGET is nonzero, the value
673 is generated there, if it is convenient to do so.
674 In all cases an rtx is returned for the locus of the value;
675 this may or may not be TARGET. */
678 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
679 rtx op1, rtx op2, rtx target, int unsignedp)
681 struct expand_operand ops[4];
682 enum insn_code icode = optab_handler (ternary_optab, mode);
684 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
686 create_output_operand (&ops[0], target, mode);
687 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
688 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
689 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
690 expand_insn (icode, 4, ops);
691 return ops[0].value;
695 /* Like expand_binop, but return a constant rtx if the result can be
696 calculated at compile time. The arguments and return value are
697 otherwise the same as for expand_binop. */
700 simplify_expand_binop (enum machine_mode mode, optab binoptab,
701 rtx op0, rtx op1, rtx target, int unsignedp,
702 enum optab_methods methods)
704 if (CONSTANT_P (op0) && CONSTANT_P (op1))
706 rtx x = simplify_binary_operation (optab_to_code (binoptab),
707 mode, op0, op1);
708 if (x)
709 return x;
712 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
715 /* Like simplify_expand_binop, but always put the result in TARGET.
716 Return true if the expansion succeeded. */
718 bool
719 force_expand_binop (enum machine_mode mode, optab binoptab,
720 rtx op0, rtx op1, rtx target, int unsignedp,
721 enum optab_methods methods)
723 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
724 target, unsignedp, methods);
725 if (x == 0)
726 return false;
727 if (x != target)
728 emit_move_insn (target, x);
729 return true;
732 /* Generate insns for VEC_LSHIFT_EXPR, VEC_RSHIFT_EXPR. */
735 expand_vec_shift_expr (sepops ops, rtx target)
737 struct expand_operand eops[3];
738 enum insn_code icode;
739 rtx rtx_op1, rtx_op2;
740 enum machine_mode mode = TYPE_MODE (ops->type);
741 tree vec_oprnd = ops->op0;
742 tree shift_oprnd = ops->op1;
743 optab shift_optab;
745 switch (ops->code)
747 case VEC_RSHIFT_EXPR:
748 shift_optab = vec_shr_optab;
749 break;
750 case VEC_LSHIFT_EXPR:
751 shift_optab = vec_shl_optab;
752 break;
753 default:
754 gcc_unreachable ();
757 icode = optab_handler (shift_optab, mode);
758 gcc_assert (icode != CODE_FOR_nothing);
760 rtx_op1 = expand_normal (vec_oprnd);
761 rtx_op2 = expand_normal (shift_oprnd);
763 create_output_operand (&eops[0], target, mode);
764 create_input_operand (&eops[1], rtx_op1, GET_MODE (rtx_op1));
765 create_convert_operand_from_type (&eops[2], rtx_op2, TREE_TYPE (shift_oprnd));
766 expand_insn (icode, 3, eops);
768 return eops[0].value;
771 /* Create a new vector value in VMODE with all elements set to OP. The
772 mode of OP must be the element mode of VMODE. If OP is a constant,
773 then the return value will be a constant. */
775 static rtx
776 expand_vector_broadcast (enum machine_mode vmode, rtx op)
778 enum insn_code icode;
779 rtvec vec;
780 rtx ret;
781 int i, n;
783 gcc_checking_assert (VECTOR_MODE_P (vmode));
785 n = GET_MODE_NUNITS (vmode);
786 vec = rtvec_alloc (n);
787 for (i = 0; i < n; ++i)
788 RTVEC_ELT (vec, i) = op;
790 if (CONSTANT_P (op))
791 return gen_rtx_CONST_VECTOR (vmode, vec);
793 /* ??? If the target doesn't have a vec_init, then we have no easy way
794 of performing this operation. Most of this sort of generic support
795 is hidden away in the vector lowering support in gimple. */
796 icode = optab_handler (vec_init_optab, vmode);
797 if (icode == CODE_FOR_nothing)
798 return NULL;
800 ret = gen_reg_rtx (vmode);
801 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
803 return ret;
806 /* This subroutine of expand_doubleword_shift handles the cases in which
807 the effective shift value is >= BITS_PER_WORD. The arguments and return
808 value are the same as for the parent routine, except that SUPERWORD_OP1
809 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
810 INTO_TARGET may be null if the caller has decided to calculate it. */
812 static bool
813 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
814 rtx outof_target, rtx into_target,
815 int unsignedp, enum optab_methods methods)
817 if (into_target != 0)
818 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
819 into_target, unsignedp, methods))
820 return false;
822 if (outof_target != 0)
824 /* For a signed right shift, we must fill OUTOF_TARGET with copies
825 of the sign bit, otherwise we must fill it with zeros. */
826 if (binoptab != ashr_optab)
827 emit_move_insn (outof_target, CONST0_RTX (word_mode));
828 else
829 if (!force_expand_binop (word_mode, binoptab,
830 outof_input, GEN_INT (BITS_PER_WORD - 1),
831 outof_target, unsignedp, methods))
832 return false;
834 return true;
837 /* This subroutine of expand_doubleword_shift handles the cases in which
838 the effective shift value is < BITS_PER_WORD. The arguments and return
839 value are the same as for the parent routine. */
841 static bool
842 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
843 rtx outof_input, rtx into_input, rtx op1,
844 rtx outof_target, rtx into_target,
845 int unsignedp, enum optab_methods methods,
846 unsigned HOST_WIDE_INT shift_mask)
848 optab reverse_unsigned_shift, unsigned_shift;
849 rtx tmp, carries;
851 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
852 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
854 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
855 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
856 the opposite direction to BINOPTAB. */
857 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
859 carries = outof_input;
860 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
861 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
862 0, true, methods);
864 else
866 /* We must avoid shifting by BITS_PER_WORD bits since that is either
867 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
868 has unknown behavior. Do a single shift first, then shift by the
869 remainder. It's OK to use ~OP1 as the remainder if shift counts
870 are truncated to the mode size. */
871 carries = expand_binop (word_mode, reverse_unsigned_shift,
872 outof_input, const1_rtx, 0, unsignedp, methods);
873 if (shift_mask == BITS_PER_WORD - 1)
875 tmp = immed_double_const (-1, -1, op1_mode);
876 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
877 0, true, methods);
879 else
881 tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
882 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
883 0, true, methods);
886 if (tmp == 0 || carries == 0)
887 return false;
888 carries = expand_binop (word_mode, reverse_unsigned_shift,
889 carries, tmp, 0, unsignedp, methods);
890 if (carries == 0)
891 return false;
893 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
894 so the result can go directly into INTO_TARGET if convenient. */
895 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
896 into_target, unsignedp, methods);
897 if (tmp == 0)
898 return false;
900 /* Now OR in the bits carried over from OUTOF_INPUT. */
901 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
902 into_target, unsignedp, methods))
903 return false;
905 /* Use a standard word_mode shift for the out-of half. */
906 if (outof_target != 0)
907 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
908 outof_target, unsignedp, methods))
909 return false;
911 return true;
915 #ifdef HAVE_conditional_move
916 /* Try implementing expand_doubleword_shift using conditional moves.
917 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
918 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
919 are the shift counts to use in the former and latter case. All other
920 arguments are the same as the parent routine. */
922 static bool
923 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
924 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
925 rtx outof_input, rtx into_input,
926 rtx subword_op1, rtx superword_op1,
927 rtx outof_target, rtx into_target,
928 int unsignedp, enum optab_methods methods,
929 unsigned HOST_WIDE_INT shift_mask)
931 rtx outof_superword, into_superword;
933 /* Put the superword version of the output into OUTOF_SUPERWORD and
934 INTO_SUPERWORD. */
935 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
936 if (outof_target != 0 && subword_op1 == superword_op1)
938 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
939 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
940 into_superword = outof_target;
941 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
942 outof_superword, 0, unsignedp, methods))
943 return false;
945 else
947 into_superword = gen_reg_rtx (word_mode);
948 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
949 outof_superword, into_superword,
950 unsignedp, methods))
951 return false;
954 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
955 if (!expand_subword_shift (op1_mode, binoptab,
956 outof_input, into_input, subword_op1,
957 outof_target, into_target,
958 unsignedp, methods, shift_mask))
959 return false;
961 /* Select between them. Do the INTO half first because INTO_SUPERWORD
962 might be the current value of OUTOF_TARGET. */
963 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
964 into_target, into_superword, word_mode, false))
965 return false;
967 if (outof_target != 0)
968 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
969 outof_target, outof_superword,
970 word_mode, false))
971 return false;
973 return true;
975 #endif
977 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
978 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
979 input operand; the shift moves bits in the direction OUTOF_INPUT->
980 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
981 of the target. OP1 is the shift count and OP1_MODE is its mode.
982 If OP1 is constant, it will have been truncated as appropriate
983 and is known to be nonzero.
985 If SHIFT_MASK is zero, the result of word shifts is undefined when the
986 shift count is outside the range [0, BITS_PER_WORD). This routine must
987 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
989 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
990 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
991 fill with zeros or sign bits as appropriate.
993 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
994 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
995 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
996 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
997 are undefined.
999 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
1000 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
1001 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
1002 function wants to calculate it itself.
1004 Return true if the shift could be successfully synthesized. */
1006 static bool
1007 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
1008 rtx outof_input, rtx into_input, rtx op1,
1009 rtx outof_target, rtx into_target,
1010 int unsignedp, enum optab_methods methods,
1011 unsigned HOST_WIDE_INT shift_mask)
1013 rtx superword_op1, tmp, cmp1, cmp2;
1014 rtx subword_label, done_label;
1015 enum rtx_code cmp_code;
1017 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1018 fill the result with sign or zero bits as appropriate. If so, the value
1019 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1020 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1021 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1023 This isn't worthwhile for constant shifts since the optimizers will
1024 cope better with in-range shift counts. */
1025 if (shift_mask >= BITS_PER_WORD
1026 && outof_target != 0
1027 && !CONSTANT_P (op1))
1029 if (!expand_doubleword_shift (op1_mode, binoptab,
1030 outof_input, into_input, op1,
1031 0, into_target,
1032 unsignedp, methods, shift_mask))
1033 return false;
1034 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1035 outof_target, unsignedp, methods))
1036 return false;
1037 return true;
1040 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1041 is true when the effective shift value is less than BITS_PER_WORD.
1042 Set SUPERWORD_OP1 to the shift count that should be used to shift
1043 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1044 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
1045 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1047 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1048 is a subword shift count. */
1049 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1050 0, true, methods);
1051 cmp2 = CONST0_RTX (op1_mode);
1052 cmp_code = EQ;
1053 superword_op1 = op1;
1055 else
1057 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1058 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1059 0, true, methods);
1060 cmp2 = CONST0_RTX (op1_mode);
1061 cmp_code = LT;
1062 superword_op1 = cmp1;
1064 if (cmp1 == 0)
1065 return false;
1067 /* If we can compute the condition at compile time, pick the
1068 appropriate subroutine. */
1069 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1070 if (tmp != 0 && CONST_INT_P (tmp))
1072 if (tmp == const0_rtx)
1073 return expand_superword_shift (binoptab, outof_input, superword_op1,
1074 outof_target, into_target,
1075 unsignedp, methods);
1076 else
1077 return expand_subword_shift (op1_mode, binoptab,
1078 outof_input, into_input, op1,
1079 outof_target, into_target,
1080 unsignedp, methods, shift_mask);
1083 #ifdef HAVE_conditional_move
1084 /* Try using conditional moves to generate straight-line code. */
1086 rtx start = get_last_insn ();
1087 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1088 cmp_code, cmp1, cmp2,
1089 outof_input, into_input,
1090 op1, superword_op1,
1091 outof_target, into_target,
1092 unsignedp, methods, shift_mask))
1093 return true;
1094 delete_insns_since (start);
1096 #endif
1098 /* As a last resort, use branches to select the correct alternative. */
1099 subword_label = gen_label_rtx ();
1100 done_label = gen_label_rtx ();
1102 NO_DEFER_POP;
1103 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1104 0, 0, subword_label, -1);
1105 OK_DEFER_POP;
1107 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1108 outof_target, into_target,
1109 unsignedp, methods))
1110 return false;
1112 emit_jump_insn (gen_jump (done_label));
1113 emit_barrier ();
1114 emit_label (subword_label);
1116 if (!expand_subword_shift (op1_mode, binoptab,
1117 outof_input, into_input, op1,
1118 outof_target, into_target,
1119 unsignedp, methods, shift_mask))
1120 return false;
1122 emit_label (done_label);
1123 return true;
1126 /* Subroutine of expand_binop. Perform a double word multiplication of
1127 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1128 as the target's word_mode. This function return NULL_RTX if anything
1129 goes wrong, in which case it may have already emitted instructions
1130 which need to be deleted.
1132 If we want to multiply two two-word values and have normal and widening
1133 multiplies of single-word values, we can do this with three smaller
1134 multiplications.
1136 The multiplication proceeds as follows:
1137 _______________________
1138 [__op0_high_|__op0_low__]
1139 _______________________
1140 * [__op1_high_|__op1_low__]
1141 _______________________________________________
1142 _______________________
1143 (1) [__op0_low__*__op1_low__]
1144 _______________________
1145 (2a) [__op0_low__*__op1_high_]
1146 _______________________
1147 (2b) [__op0_high_*__op1_low__]
1148 _______________________
1149 (3) [__op0_high_*__op1_high_]
1152 This gives a 4-word result. Since we are only interested in the
1153 lower 2 words, partial result (3) and the upper words of (2a) and
1154 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1155 calculated using non-widening multiplication.
1157 (1), however, needs to be calculated with an unsigned widening
1158 multiplication. If this operation is not directly supported we
1159 try using a signed widening multiplication and adjust the result.
1160 This adjustment works as follows:
1162 If both operands are positive then no adjustment is needed.
1164 If the operands have different signs, for example op0_low < 0 and
1165 op1_low >= 0, the instruction treats the most significant bit of
1166 op0_low as a sign bit instead of a bit with significance
1167 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1168 with 2**BITS_PER_WORD - op0_low, and two's complements the
1169 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1170 the result.
1172 Similarly, if both operands are negative, we need to add
1173 (op0_low + op1_low) * 2**BITS_PER_WORD.
1175 We use a trick to adjust quickly. We logically shift op0_low right
1176 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1177 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1178 logical shift exists, we do an arithmetic right shift and subtract
1179 the 0 or -1. */
1181 static rtx
1182 expand_doubleword_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
1183 bool umulp, enum optab_methods methods)
1185 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1186 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1187 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1188 rtx product, adjust, product_high, temp;
1190 rtx op0_high = operand_subword_force (op0, high, mode);
1191 rtx op0_low = operand_subword_force (op0, low, mode);
1192 rtx op1_high = operand_subword_force (op1, high, mode);
1193 rtx op1_low = operand_subword_force (op1, low, mode);
1195 /* If we're using an unsigned multiply to directly compute the product
1196 of the low-order words of the operands and perform any required
1197 adjustments of the operands, we begin by trying two more multiplications
1198 and then computing the appropriate sum.
1200 We have checked above that the required addition is provided.
1201 Full-word addition will normally always succeed, especially if
1202 it is provided at all, so we don't worry about its failure. The
1203 multiplication may well fail, however, so we do handle that. */
1205 if (!umulp)
1207 /* ??? This could be done with emit_store_flag where available. */
1208 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1209 NULL_RTX, 1, methods);
1210 if (temp)
1211 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1212 NULL_RTX, 0, OPTAB_DIRECT);
1213 else
1215 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1216 NULL_RTX, 0, methods);
1217 if (!temp)
1218 return NULL_RTX;
1219 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1220 NULL_RTX, 0, OPTAB_DIRECT);
1223 if (!op0_high)
1224 return NULL_RTX;
1227 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1228 NULL_RTX, 0, OPTAB_DIRECT);
1229 if (!adjust)
1230 return NULL_RTX;
1232 /* OP0_HIGH should now be dead. */
1234 if (!umulp)
1236 /* ??? This could be done with emit_store_flag where available. */
1237 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1238 NULL_RTX, 1, methods);
1239 if (temp)
1240 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1241 NULL_RTX, 0, OPTAB_DIRECT);
1242 else
1244 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1245 NULL_RTX, 0, methods);
1246 if (!temp)
1247 return NULL_RTX;
1248 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1249 NULL_RTX, 0, OPTAB_DIRECT);
1252 if (!op1_high)
1253 return NULL_RTX;
1256 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1257 NULL_RTX, 0, OPTAB_DIRECT);
1258 if (!temp)
1259 return NULL_RTX;
1261 /* OP1_HIGH should now be dead. */
1263 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1264 NULL_RTX, 0, OPTAB_DIRECT);
1266 if (target && !REG_P (target))
1267 target = NULL_RTX;
1269 if (umulp)
1270 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1271 target, 1, OPTAB_DIRECT);
1272 else
1273 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1274 target, 1, OPTAB_DIRECT);
1276 if (!product)
1277 return NULL_RTX;
1279 product_high = operand_subword (product, high, 1, mode);
1280 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1281 NULL_RTX, 0, OPTAB_DIRECT);
1282 emit_move_insn (product_high, adjust);
1283 return product;
1286 /* Wrapper around expand_binop which takes an rtx code to specify
1287 the operation to perform, not an optab pointer. All other
1288 arguments are the same. */
1290 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
1291 rtx op1, rtx target, int unsignedp,
1292 enum optab_methods methods)
1294 optab binop = code_to_optab (code);
1295 gcc_assert (binop);
1297 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1300 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1301 binop. Order them according to commutative_operand_precedence and, if
1302 possible, try to put TARGET or a pseudo first. */
1303 static bool
1304 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1306 int op0_prec = commutative_operand_precedence (op0);
1307 int op1_prec = commutative_operand_precedence (op1);
1309 if (op0_prec < op1_prec)
1310 return true;
1312 if (op0_prec > op1_prec)
1313 return false;
1315 /* With equal precedence, both orders are ok, but it is better if the
1316 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1317 if (target == 0 || REG_P (target))
1318 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1319 else
1320 return rtx_equal_p (op1, target);
1323 /* Return true if BINOPTAB implements a shift operation. */
1325 static bool
1326 shift_optab_p (optab binoptab)
1328 switch (optab_to_code (binoptab))
1330 case ASHIFT:
1331 case SS_ASHIFT:
1332 case US_ASHIFT:
1333 case ASHIFTRT:
1334 case LSHIFTRT:
1335 case ROTATE:
1336 case ROTATERT:
1337 return true;
1339 default:
1340 return false;
1344 /* Return true if BINOPTAB implements a commutative binary operation. */
1346 static bool
1347 commutative_optab_p (optab binoptab)
1349 return (GET_RTX_CLASS (optab_to_code (binoptab)) == RTX_COMM_ARITH
1350 || binoptab == smul_widen_optab
1351 || binoptab == umul_widen_optab
1352 || binoptab == smul_highpart_optab
1353 || binoptab == umul_highpart_optab);
1356 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1357 optimizing, and if the operand is a constant that costs more than
1358 1 instruction, force the constant into a register and return that
1359 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1361 static rtx
1362 avoid_expensive_constant (enum machine_mode mode, optab binoptab,
1363 int opn, rtx x, bool unsignedp)
1365 bool speed = optimize_insn_for_speed_p ();
1367 if (mode != VOIDmode
1368 && optimize
1369 && CONSTANT_P (x)
1370 && (rtx_cost (x, optab_to_code (binoptab), opn, speed)
1371 > set_src_cost (x, speed)))
1373 if (CONST_INT_P (x))
1375 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1376 if (intval != INTVAL (x))
1377 x = GEN_INT (intval);
1379 else
1380 x = convert_modes (mode, VOIDmode, x, unsignedp);
1381 x = force_reg (mode, x);
1383 return x;
1386 /* Helper function for expand_binop: handle the case where there
1387 is an insn that directly implements the indicated operation.
1388 Returns null if this is not possible. */
1389 static rtx
1390 expand_binop_directly (enum machine_mode mode, optab binoptab,
1391 rtx op0, rtx op1,
1392 rtx target, int unsignedp, enum optab_methods methods,
1393 rtx last)
1395 enum machine_mode from_mode = widened_mode (mode, op0, op1);
1396 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1397 from_mode, 1);
1398 enum machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1399 enum machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1400 enum machine_mode mode0, mode1, tmp_mode;
1401 struct expand_operand ops[3];
1402 bool commutative_p;
1403 rtx pat;
1404 rtx xop0 = op0, xop1 = op1;
1405 rtx swap;
1407 /* If it is a commutative operator and the modes would match
1408 if we would swap the operands, we can save the conversions. */
1409 commutative_p = commutative_optab_p (binoptab);
1410 if (commutative_p
1411 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1412 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1414 swap = xop0;
1415 xop0 = xop1;
1416 xop1 = swap;
1419 /* If we are optimizing, force expensive constants into a register. */
1420 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1421 if (!shift_optab_p (binoptab))
1422 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1424 /* In case the insn wants input operands in modes different from
1425 those of the actual operands, convert the operands. It would
1426 seem that we don't need to convert CONST_INTs, but we do, so
1427 that they're properly zero-extended, sign-extended or truncated
1428 for their mode. */
1430 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1431 if (xmode0 != VOIDmode && xmode0 != mode0)
1433 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1434 mode0 = xmode0;
1437 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1438 if (xmode1 != VOIDmode && xmode1 != mode1)
1440 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1441 mode1 = xmode1;
1444 /* If operation is commutative,
1445 try to make the first operand a register.
1446 Even better, try to make it the same as the target.
1447 Also try to make the last operand a constant. */
1448 if (commutative_p
1449 && swap_commutative_operands_with_target (target, xop0, xop1))
1451 swap = xop1;
1452 xop1 = xop0;
1453 xop0 = swap;
1456 /* Now, if insn's predicates don't allow our operands, put them into
1457 pseudo regs. */
1459 if (binoptab == vec_pack_trunc_optab
1460 || binoptab == vec_pack_usat_optab
1461 || binoptab == vec_pack_ssat_optab
1462 || binoptab == vec_pack_ufix_trunc_optab
1463 || binoptab == vec_pack_sfix_trunc_optab)
1465 /* The mode of the result is different then the mode of the
1466 arguments. */
1467 tmp_mode = insn_data[(int) icode].operand[0].mode;
1468 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1470 delete_insns_since (last);
1471 return NULL_RTX;
1474 else
1475 tmp_mode = mode;
1477 create_output_operand (&ops[0], target, tmp_mode);
1478 create_input_operand (&ops[1], xop0, mode0);
1479 create_input_operand (&ops[2], xop1, mode1);
1480 pat = maybe_gen_insn (icode, 3, ops);
1481 if (pat)
1483 /* If PAT is composed of more than one insn, try to add an appropriate
1484 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1485 operand, call expand_binop again, this time without a target. */
1486 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1487 && ! add_equal_note (pat, ops[0].value, optab_to_code (binoptab),
1488 ops[1].value, ops[2].value))
1490 delete_insns_since (last);
1491 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1492 unsignedp, methods);
1495 emit_insn (pat);
1496 return ops[0].value;
1498 delete_insns_since (last);
1499 return NULL_RTX;
1502 /* Generate code to perform an operation specified by BINOPTAB
1503 on operands OP0 and OP1, with result having machine-mode MODE.
1505 UNSIGNEDP is for the case where we have to widen the operands
1506 to perform the operation. It says to use zero-extension.
1508 If TARGET is nonzero, the value
1509 is generated there, if it is convenient to do so.
1510 In all cases an rtx is returned for the locus of the value;
1511 this may or may not be TARGET. */
1514 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
1515 rtx target, int unsignedp, enum optab_methods methods)
1517 enum optab_methods next_methods
1518 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1519 ? OPTAB_WIDEN : methods);
1520 enum mode_class mclass;
1521 enum machine_mode wider_mode;
1522 rtx libfunc;
1523 rtx temp;
1524 rtx entry_last = get_last_insn ();
1525 rtx last;
1527 mclass = GET_MODE_CLASS (mode);
1529 /* If subtracting an integer constant, convert this into an addition of
1530 the negated constant. */
1532 if (binoptab == sub_optab && CONST_INT_P (op1))
1534 op1 = negate_rtx (mode, op1);
1535 binoptab = add_optab;
1538 /* Record where to delete back to if we backtrack. */
1539 last = get_last_insn ();
1541 /* If we can do it with a three-operand insn, do so. */
1543 if (methods != OPTAB_MUST_WIDEN
1544 && find_widening_optab_handler (binoptab, mode,
1545 widened_mode (mode, op0, op1), 1)
1546 != CODE_FOR_nothing)
1548 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1549 unsignedp, methods, last);
1550 if (temp)
1551 return temp;
1554 /* If we were trying to rotate, and that didn't work, try rotating
1555 the other direction before falling back to shifts and bitwise-or. */
1556 if (((binoptab == rotl_optab
1557 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1558 || (binoptab == rotr_optab
1559 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1560 && mclass == MODE_INT)
1562 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1563 rtx newop1;
1564 unsigned int bits = GET_MODE_PRECISION (mode);
1566 if (CONST_INT_P (op1))
1567 newop1 = GEN_INT (bits - INTVAL (op1));
1568 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1569 newop1 = negate_rtx (GET_MODE (op1), op1);
1570 else
1571 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1572 gen_int_mode (bits, GET_MODE (op1)), op1,
1573 NULL_RTX, unsignedp, OPTAB_DIRECT);
1575 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1576 target, unsignedp, methods, last);
1577 if (temp)
1578 return temp;
1581 /* If this is a multiply, see if we can do a widening operation that
1582 takes operands of this mode and makes a wider mode. */
1584 if (binoptab == smul_optab
1585 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1586 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1587 : smul_widen_optab),
1588 GET_MODE_2XWIDER_MODE (mode), mode)
1589 != CODE_FOR_nothing))
1591 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1592 unsignedp ? umul_widen_optab : smul_widen_optab,
1593 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1595 if (temp != 0)
1597 if (GET_MODE_CLASS (mode) == MODE_INT
1598 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1599 return gen_lowpart (mode, temp);
1600 else
1601 return convert_to_mode (mode, temp, unsignedp);
1605 /* If this is a vector shift by a scalar, see if we can do a vector
1606 shift by a vector. If so, broadcast the scalar into a vector. */
1607 if (mclass == MODE_VECTOR_INT)
1609 optab otheroptab = unknown_optab;
1611 if (binoptab == ashl_optab)
1612 otheroptab = vashl_optab;
1613 else if (binoptab == ashr_optab)
1614 otheroptab = vashr_optab;
1615 else if (binoptab == lshr_optab)
1616 otheroptab = vlshr_optab;
1617 else if (binoptab == rotl_optab)
1618 otheroptab = vrotl_optab;
1619 else if (binoptab == rotr_optab)
1620 otheroptab = vrotr_optab;
1622 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1624 rtx vop1 = expand_vector_broadcast (mode, op1);
1625 if (vop1)
1627 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1628 target, unsignedp, methods, last);
1629 if (temp)
1630 return temp;
1635 /* Look for a wider mode of the same class for which we think we
1636 can open-code the operation. Check for a widening multiply at the
1637 wider mode as well. */
1639 if (CLASS_HAS_WIDER_MODES_P (mclass)
1640 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1641 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1642 wider_mode != VOIDmode;
1643 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1645 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1646 || (binoptab == smul_optab
1647 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1648 && (find_widening_optab_handler ((unsignedp
1649 ? umul_widen_optab
1650 : smul_widen_optab),
1651 GET_MODE_WIDER_MODE (wider_mode),
1652 mode, 0)
1653 != CODE_FOR_nothing)))
1655 rtx xop0 = op0, xop1 = op1;
1656 int no_extend = 0;
1658 /* For certain integer operations, we need not actually extend
1659 the narrow operands, as long as we will truncate
1660 the results to the same narrowness. */
1662 if ((binoptab == ior_optab || binoptab == and_optab
1663 || binoptab == xor_optab
1664 || binoptab == add_optab || binoptab == sub_optab
1665 || binoptab == smul_optab || binoptab == ashl_optab)
1666 && mclass == MODE_INT)
1668 no_extend = 1;
1669 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1670 xop0, unsignedp);
1671 if (binoptab != ashl_optab)
1672 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1673 xop1, unsignedp);
1676 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1678 /* The second operand of a shift must always be extended. */
1679 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1680 no_extend && binoptab != ashl_optab);
1682 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1683 unsignedp, OPTAB_DIRECT);
1684 if (temp)
1686 if (mclass != MODE_INT
1687 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1689 if (target == 0)
1690 target = gen_reg_rtx (mode);
1691 convert_move (target, temp, 0);
1692 return target;
1694 else
1695 return gen_lowpart (mode, temp);
1697 else
1698 delete_insns_since (last);
1702 /* If operation is commutative,
1703 try to make the first operand a register.
1704 Even better, try to make it the same as the target.
1705 Also try to make the last operand a constant. */
1706 if (commutative_optab_p (binoptab)
1707 && swap_commutative_operands_with_target (target, op0, op1))
1709 temp = op1;
1710 op1 = op0;
1711 op0 = temp;
1714 /* These can be done a word at a time. */
1715 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1716 && mclass == MODE_INT
1717 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1718 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1720 int i;
1721 rtx insns;
1723 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1724 won't be accurate, so use a new target. */
1725 if (target == 0
1726 || target == op0
1727 || target == op1
1728 || !valid_multiword_target_p (target))
1729 target = gen_reg_rtx (mode);
1731 start_sequence ();
1733 /* Do the actual arithmetic. */
1734 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1736 rtx target_piece = operand_subword (target, i, 1, mode);
1737 rtx x = expand_binop (word_mode, binoptab,
1738 operand_subword_force (op0, i, mode),
1739 operand_subword_force (op1, i, mode),
1740 target_piece, unsignedp, next_methods);
1742 if (x == 0)
1743 break;
1745 if (target_piece != x)
1746 emit_move_insn (target_piece, x);
1749 insns = get_insns ();
1750 end_sequence ();
1752 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1754 emit_insn (insns);
1755 return target;
1759 /* Synthesize double word shifts from single word shifts. */
1760 if ((binoptab == lshr_optab || binoptab == ashl_optab
1761 || binoptab == ashr_optab)
1762 && mclass == MODE_INT
1763 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1764 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1765 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1766 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1767 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1768 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1770 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1771 enum machine_mode op1_mode;
1773 double_shift_mask = targetm.shift_truncation_mask (mode);
1774 shift_mask = targetm.shift_truncation_mask (word_mode);
1775 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1777 /* Apply the truncation to constant shifts. */
1778 if (double_shift_mask > 0 && CONST_INT_P (op1))
1779 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1781 if (op1 == CONST0_RTX (op1_mode))
1782 return op0;
1784 /* Make sure that this is a combination that expand_doubleword_shift
1785 can handle. See the comments there for details. */
1786 if (double_shift_mask == 0
1787 || (shift_mask == BITS_PER_WORD - 1
1788 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1790 rtx insns;
1791 rtx into_target, outof_target;
1792 rtx into_input, outof_input;
1793 int left_shift, outof_word;
1795 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1796 won't be accurate, so use a new target. */
1797 if (target == 0
1798 || target == op0
1799 || target == op1
1800 || !valid_multiword_target_p (target))
1801 target = gen_reg_rtx (mode);
1803 start_sequence ();
1805 /* OUTOF_* is the word we are shifting bits away from, and
1806 INTO_* is the word that we are shifting bits towards, thus
1807 they differ depending on the direction of the shift and
1808 WORDS_BIG_ENDIAN. */
1810 left_shift = binoptab == ashl_optab;
1811 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1813 outof_target = operand_subword (target, outof_word, 1, mode);
1814 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1816 outof_input = operand_subword_force (op0, outof_word, mode);
1817 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1819 if (expand_doubleword_shift (op1_mode, binoptab,
1820 outof_input, into_input, op1,
1821 outof_target, into_target,
1822 unsignedp, next_methods, shift_mask))
1824 insns = get_insns ();
1825 end_sequence ();
1827 emit_insn (insns);
1828 return target;
1830 end_sequence ();
1834 /* Synthesize double word rotates from single word shifts. */
1835 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1836 && mclass == MODE_INT
1837 && CONST_INT_P (op1)
1838 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1839 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1840 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1842 rtx insns;
1843 rtx into_target, outof_target;
1844 rtx into_input, outof_input;
1845 rtx inter;
1846 int shift_count, left_shift, outof_word;
1848 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1849 won't be accurate, so use a new target. Do this also if target is not
1850 a REG, first because having a register instead may open optimization
1851 opportunities, and second because if target and op0 happen to be MEMs
1852 designating the same location, we would risk clobbering it too early
1853 in the code sequence we generate below. */
1854 if (target == 0
1855 || target == op0
1856 || target == op1
1857 || !REG_P (target)
1858 || !valid_multiword_target_p (target))
1859 target = gen_reg_rtx (mode);
1861 start_sequence ();
1863 shift_count = INTVAL (op1);
1865 /* OUTOF_* is the word we are shifting bits away from, and
1866 INTO_* is the word that we are shifting bits towards, thus
1867 they differ depending on the direction of the shift and
1868 WORDS_BIG_ENDIAN. */
1870 left_shift = (binoptab == rotl_optab);
1871 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1873 outof_target = operand_subword (target, outof_word, 1, mode);
1874 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1876 outof_input = operand_subword_force (op0, outof_word, mode);
1877 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1879 if (shift_count == BITS_PER_WORD)
1881 /* This is just a word swap. */
1882 emit_move_insn (outof_target, into_input);
1883 emit_move_insn (into_target, outof_input);
1884 inter = const0_rtx;
1886 else
1888 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1889 rtx first_shift_count, second_shift_count;
1890 optab reverse_unsigned_shift, unsigned_shift;
1892 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1893 ? lshr_optab : ashl_optab);
1895 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1896 ? ashl_optab : lshr_optab);
1898 if (shift_count > BITS_PER_WORD)
1900 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1901 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1903 else
1905 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1906 second_shift_count = GEN_INT (shift_count);
1909 into_temp1 = expand_binop (word_mode, unsigned_shift,
1910 outof_input, first_shift_count,
1911 NULL_RTX, unsignedp, next_methods);
1912 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1913 into_input, second_shift_count,
1914 NULL_RTX, unsignedp, next_methods);
1916 if (into_temp1 != 0 && into_temp2 != 0)
1917 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1918 into_target, unsignedp, next_methods);
1919 else
1920 inter = 0;
1922 if (inter != 0 && inter != into_target)
1923 emit_move_insn (into_target, inter);
1925 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1926 into_input, first_shift_count,
1927 NULL_RTX, unsignedp, next_methods);
1928 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1929 outof_input, second_shift_count,
1930 NULL_RTX, unsignedp, next_methods);
1932 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1933 inter = expand_binop (word_mode, ior_optab,
1934 outof_temp1, outof_temp2,
1935 outof_target, unsignedp, next_methods);
1937 if (inter != 0 && inter != outof_target)
1938 emit_move_insn (outof_target, inter);
1941 insns = get_insns ();
1942 end_sequence ();
1944 if (inter != 0)
1946 emit_insn (insns);
1947 return target;
1951 /* These can be done a word at a time by propagating carries. */
1952 if ((binoptab == add_optab || binoptab == sub_optab)
1953 && mclass == MODE_INT
1954 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1955 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1957 unsigned int i;
1958 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1959 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1960 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1961 rtx xop0, xop1, xtarget;
1963 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1964 value is one of those, use it. Otherwise, use 1 since it is the
1965 one easiest to get. */
1966 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1967 int normalizep = STORE_FLAG_VALUE;
1968 #else
1969 int normalizep = 1;
1970 #endif
1972 /* Prepare the operands. */
1973 xop0 = force_reg (mode, op0);
1974 xop1 = force_reg (mode, op1);
1976 xtarget = gen_reg_rtx (mode);
1978 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1979 target = xtarget;
1981 /* Indicate for flow that the entire target reg is being set. */
1982 if (REG_P (target))
1983 emit_clobber (xtarget);
1985 /* Do the actual arithmetic. */
1986 for (i = 0; i < nwords; i++)
1988 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1989 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1990 rtx op0_piece = operand_subword_force (xop0, index, mode);
1991 rtx op1_piece = operand_subword_force (xop1, index, mode);
1992 rtx x;
1994 /* Main add/subtract of the input operands. */
1995 x = expand_binop (word_mode, binoptab,
1996 op0_piece, op1_piece,
1997 target_piece, unsignedp, next_methods);
1998 if (x == 0)
1999 break;
2001 if (i + 1 < nwords)
2003 /* Store carry from main add/subtract. */
2004 carry_out = gen_reg_rtx (word_mode);
2005 carry_out = emit_store_flag_force (carry_out,
2006 (binoptab == add_optab
2007 ? LT : GT),
2008 x, op0_piece,
2009 word_mode, 1, normalizep);
2012 if (i > 0)
2014 rtx newx;
2016 /* Add/subtract previous carry to main result. */
2017 newx = expand_binop (word_mode,
2018 normalizep == 1 ? binoptab : otheroptab,
2019 x, carry_in,
2020 NULL_RTX, 1, next_methods);
2022 if (i + 1 < nwords)
2024 /* Get out carry from adding/subtracting carry in. */
2025 rtx carry_tmp = gen_reg_rtx (word_mode);
2026 carry_tmp = emit_store_flag_force (carry_tmp,
2027 (binoptab == add_optab
2028 ? LT : GT),
2029 newx, x,
2030 word_mode, 1, normalizep);
2032 /* Logical-ior the two poss. carry together. */
2033 carry_out = expand_binop (word_mode, ior_optab,
2034 carry_out, carry_tmp,
2035 carry_out, 0, next_methods);
2036 if (carry_out == 0)
2037 break;
2039 emit_move_insn (target_piece, newx);
2041 else
2043 if (x != target_piece)
2044 emit_move_insn (target_piece, x);
2047 carry_in = carry_out;
2050 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2052 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2053 || ! rtx_equal_p (target, xtarget))
2055 rtx temp = emit_move_insn (target, xtarget);
2057 set_dst_reg_note (temp, REG_EQUAL,
2058 gen_rtx_fmt_ee (optab_to_code (binoptab),
2059 mode, copy_rtx (xop0),
2060 copy_rtx (xop1)),
2061 target);
2063 else
2064 target = xtarget;
2066 return target;
2069 else
2070 delete_insns_since (last);
2073 /* Attempt to synthesize double word multiplies using a sequence of word
2074 mode multiplications. We first attempt to generate a sequence using a
2075 more efficient unsigned widening multiply, and if that fails we then
2076 try using a signed widening multiply. */
2078 if (binoptab == smul_optab
2079 && mclass == MODE_INT
2080 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2081 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2082 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2084 rtx product = NULL_RTX;
2085 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
2086 != CODE_FOR_nothing)
2088 product = expand_doubleword_mult (mode, op0, op1, target,
2089 true, methods);
2090 if (!product)
2091 delete_insns_since (last);
2094 if (product == NULL_RTX
2095 && widening_optab_handler (smul_widen_optab, mode, word_mode)
2096 != CODE_FOR_nothing)
2098 product = expand_doubleword_mult (mode, op0, op1, target,
2099 false, methods);
2100 if (!product)
2101 delete_insns_since (last);
2104 if (product != NULL_RTX)
2106 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2108 temp = emit_move_insn (target ? target : product, product);
2109 set_dst_reg_note (temp,
2110 REG_EQUAL,
2111 gen_rtx_fmt_ee (MULT, mode,
2112 copy_rtx (op0),
2113 copy_rtx (op1)),
2114 target ? target : product);
2116 return product;
2120 /* It can't be open-coded in this mode.
2121 Use a library call if one is available and caller says that's ok. */
2123 libfunc = optab_libfunc (binoptab, mode);
2124 if (libfunc
2125 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2127 rtx insns;
2128 rtx op1x = op1;
2129 enum machine_mode op1_mode = mode;
2130 rtx value;
2132 start_sequence ();
2134 if (shift_optab_p (binoptab))
2136 op1_mode = targetm.libgcc_shift_count_mode ();
2137 /* Specify unsigned here,
2138 since negative shift counts are meaningless. */
2139 op1x = convert_to_mode (op1_mode, op1, 1);
2142 if (GET_MODE (op0) != VOIDmode
2143 && GET_MODE (op0) != mode)
2144 op0 = convert_to_mode (mode, op0, unsignedp);
2146 /* Pass 1 for NO_QUEUE so we don't lose any increments
2147 if the libcall is cse'd or moved. */
2148 value = emit_library_call_value (libfunc,
2149 NULL_RTX, LCT_CONST, mode, 2,
2150 op0, mode, op1x, op1_mode);
2152 insns = get_insns ();
2153 end_sequence ();
2155 target = gen_reg_rtx (mode);
2156 emit_libcall_block_1 (insns, target, value,
2157 gen_rtx_fmt_ee (optab_to_code (binoptab),
2158 mode, op0, op1),
2159 trapv_binoptab_p (binoptab));
2161 return target;
2164 delete_insns_since (last);
2166 /* It can't be done in this mode. Can we do it in a wider mode? */
2168 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2169 || methods == OPTAB_MUST_WIDEN))
2171 /* Caller says, don't even try. */
2172 delete_insns_since (entry_last);
2173 return 0;
2176 /* Compute the value of METHODS to pass to recursive calls.
2177 Don't allow widening to be tried recursively. */
2179 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2181 /* Look for a wider mode of the same class for which it appears we can do
2182 the operation. */
2184 if (CLASS_HAS_WIDER_MODES_P (mclass))
2186 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2187 wider_mode != VOIDmode;
2188 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2190 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2191 != CODE_FOR_nothing
2192 || (methods == OPTAB_LIB
2193 && optab_libfunc (binoptab, wider_mode)))
2195 rtx xop0 = op0, xop1 = op1;
2196 int no_extend = 0;
2198 /* For certain integer operations, we need not actually extend
2199 the narrow operands, as long as we will truncate
2200 the results to the same narrowness. */
2202 if ((binoptab == ior_optab || binoptab == and_optab
2203 || binoptab == xor_optab
2204 || binoptab == add_optab || binoptab == sub_optab
2205 || binoptab == smul_optab || binoptab == ashl_optab)
2206 && mclass == MODE_INT)
2207 no_extend = 1;
2209 xop0 = widen_operand (xop0, wider_mode, mode,
2210 unsignedp, no_extend);
2212 /* The second operand of a shift must always be extended. */
2213 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2214 no_extend && binoptab != ashl_optab);
2216 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2217 unsignedp, methods);
2218 if (temp)
2220 if (mclass != MODE_INT
2221 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2223 if (target == 0)
2224 target = gen_reg_rtx (mode);
2225 convert_move (target, temp, 0);
2226 return target;
2228 else
2229 return gen_lowpart (mode, temp);
2231 else
2232 delete_insns_since (last);
2237 delete_insns_since (entry_last);
2238 return 0;
2241 /* Expand a binary operator which has both signed and unsigned forms.
2242 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2243 signed operations.
2245 If we widen unsigned operands, we may use a signed wider operation instead
2246 of an unsigned wider operation, since the result would be the same. */
2249 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
2250 rtx op0, rtx op1, rtx target, int unsignedp,
2251 enum optab_methods methods)
2253 rtx temp;
2254 optab direct_optab = unsignedp ? uoptab : soptab;
2255 bool save_enable;
2257 /* Do it without widening, if possible. */
2258 temp = expand_binop (mode, direct_optab, op0, op1, target,
2259 unsignedp, OPTAB_DIRECT);
2260 if (temp || methods == OPTAB_DIRECT)
2261 return temp;
2263 /* Try widening to a signed int. Disable any direct use of any
2264 signed insn in the current mode. */
2265 save_enable = swap_optab_enable (soptab, mode, false);
2267 temp = expand_binop (mode, soptab, op0, op1, target,
2268 unsignedp, OPTAB_WIDEN);
2270 /* For unsigned operands, try widening to an unsigned int. */
2271 if (!temp && unsignedp)
2272 temp = expand_binop (mode, uoptab, op0, op1, target,
2273 unsignedp, OPTAB_WIDEN);
2274 if (temp || methods == OPTAB_WIDEN)
2275 goto egress;
2277 /* Use the right width libcall if that exists. */
2278 temp = expand_binop (mode, direct_optab, op0, op1, target,
2279 unsignedp, OPTAB_LIB);
2280 if (temp || methods == OPTAB_LIB)
2281 goto egress;
2283 /* Must widen and use a libcall, use either signed or unsigned. */
2284 temp = expand_binop (mode, soptab, op0, op1, target,
2285 unsignedp, methods);
2286 if (!temp && unsignedp)
2287 temp = expand_binop (mode, uoptab, op0, op1, target,
2288 unsignedp, methods);
2290 egress:
2291 /* Undo the fiddling above. */
2292 if (save_enable)
2293 swap_optab_enable (soptab, mode, true);
2294 return temp;
2297 /* Generate code to perform an operation specified by UNOPPTAB
2298 on operand OP0, with two results to TARG0 and TARG1.
2299 We assume that the order of the operands for the instruction
2300 is TARG0, TARG1, OP0.
2302 Either TARG0 or TARG1 may be zero, but what that means is that
2303 the result is not actually wanted. We will generate it into
2304 a dummy pseudo-reg and discard it. They may not both be zero.
2306 Returns 1 if this operation can be performed; 0 if not. */
2309 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2310 int unsignedp)
2312 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2313 enum mode_class mclass;
2314 enum machine_mode wider_mode;
2315 rtx entry_last = get_last_insn ();
2316 rtx last;
2318 mclass = GET_MODE_CLASS (mode);
2320 if (!targ0)
2321 targ0 = gen_reg_rtx (mode);
2322 if (!targ1)
2323 targ1 = gen_reg_rtx (mode);
2325 /* Record where to go back to if we fail. */
2326 last = get_last_insn ();
2328 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2330 struct expand_operand ops[3];
2331 enum insn_code icode = optab_handler (unoptab, mode);
2333 create_fixed_operand (&ops[0], targ0);
2334 create_fixed_operand (&ops[1], targ1);
2335 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2336 if (maybe_expand_insn (icode, 3, ops))
2337 return 1;
2340 /* It can't be done in this mode. Can we do it in a wider mode? */
2342 if (CLASS_HAS_WIDER_MODES_P (mclass))
2344 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2345 wider_mode != VOIDmode;
2346 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2348 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2350 rtx t0 = gen_reg_rtx (wider_mode);
2351 rtx t1 = gen_reg_rtx (wider_mode);
2352 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2354 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2356 convert_move (targ0, t0, unsignedp);
2357 convert_move (targ1, t1, unsignedp);
2358 return 1;
2360 else
2361 delete_insns_since (last);
2366 delete_insns_since (entry_last);
2367 return 0;
2370 /* Generate code to perform an operation specified by BINOPTAB
2371 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2372 We assume that the order of the operands for the instruction
2373 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2374 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2376 Either TARG0 or TARG1 may be zero, but what that means is that
2377 the result is not actually wanted. We will generate it into
2378 a dummy pseudo-reg and discard it. They may not both be zero.
2380 Returns 1 if this operation can be performed; 0 if not. */
2383 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2384 int unsignedp)
2386 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2387 enum mode_class mclass;
2388 enum machine_mode wider_mode;
2389 rtx entry_last = get_last_insn ();
2390 rtx last;
2392 mclass = GET_MODE_CLASS (mode);
2394 if (!targ0)
2395 targ0 = gen_reg_rtx (mode);
2396 if (!targ1)
2397 targ1 = gen_reg_rtx (mode);
2399 /* Record where to go back to if we fail. */
2400 last = get_last_insn ();
2402 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2404 struct expand_operand ops[4];
2405 enum insn_code icode = optab_handler (binoptab, mode);
2406 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2407 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
2408 rtx xop0 = op0, xop1 = op1;
2410 /* If we are optimizing, force expensive constants into a register. */
2411 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2412 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2414 create_fixed_operand (&ops[0], targ0);
2415 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2416 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2417 create_fixed_operand (&ops[3], targ1);
2418 if (maybe_expand_insn (icode, 4, ops))
2419 return 1;
2420 delete_insns_since (last);
2423 /* It can't be done in this mode. Can we do it in a wider mode? */
2425 if (CLASS_HAS_WIDER_MODES_P (mclass))
2427 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2428 wider_mode != VOIDmode;
2429 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2431 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2433 rtx t0 = gen_reg_rtx (wider_mode);
2434 rtx t1 = gen_reg_rtx (wider_mode);
2435 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2436 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2438 if (expand_twoval_binop (binoptab, cop0, cop1,
2439 t0, t1, unsignedp))
2441 convert_move (targ0, t0, unsignedp);
2442 convert_move (targ1, t1, unsignedp);
2443 return 1;
2445 else
2446 delete_insns_since (last);
2451 delete_insns_since (entry_last);
2452 return 0;
2455 /* Expand the two-valued library call indicated by BINOPTAB, but
2456 preserve only one of the values. If TARG0 is non-NULL, the first
2457 value is placed into TARG0; otherwise the second value is placed
2458 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2459 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2460 This routine assumes that the value returned by the library call is
2461 as if the return value was of an integral mode twice as wide as the
2462 mode of OP0. Returns 1 if the call was successful. */
2464 bool
2465 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2466 rtx targ0, rtx targ1, enum rtx_code code)
2468 enum machine_mode mode;
2469 enum machine_mode libval_mode;
2470 rtx libval;
2471 rtx insns;
2472 rtx libfunc;
2474 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2475 gcc_assert (!targ0 != !targ1);
2477 mode = GET_MODE (op0);
2478 libfunc = optab_libfunc (binoptab, mode);
2479 if (!libfunc)
2480 return false;
2482 /* The value returned by the library function will have twice as
2483 many bits as the nominal MODE. */
2484 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2485 MODE_INT);
2486 start_sequence ();
2487 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2488 libval_mode, 2,
2489 op0, mode,
2490 op1, mode);
2491 /* Get the part of VAL containing the value that we want. */
2492 libval = simplify_gen_subreg (mode, libval, libval_mode,
2493 targ0 ? 0 : GET_MODE_SIZE (mode));
2494 insns = get_insns ();
2495 end_sequence ();
2496 /* Move the into the desired location. */
2497 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2498 gen_rtx_fmt_ee (code, mode, op0, op1));
2500 return true;
2504 /* Wrapper around expand_unop which takes an rtx code to specify
2505 the operation to perform, not an optab pointer. All other
2506 arguments are the same. */
2508 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2509 rtx target, int unsignedp)
2511 optab unop = code_to_optab (code);
2512 gcc_assert (unop);
2514 return expand_unop (mode, unop, op0, target, unsignedp);
2517 /* Try calculating
2518 (clz:narrow x)
2520 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2522 A similar operation can be used for clrsb. UNOPTAB says which operation
2523 we are trying to expand. */
2524 static rtx
2525 widen_leading (enum machine_mode mode, rtx op0, rtx target, optab unoptab)
2527 enum mode_class mclass = GET_MODE_CLASS (mode);
2528 if (CLASS_HAS_WIDER_MODES_P (mclass))
2530 enum machine_mode wider_mode;
2531 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2532 wider_mode != VOIDmode;
2533 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2535 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2537 rtx xop0, temp, last;
2539 last = get_last_insn ();
2541 if (target == 0)
2542 target = gen_reg_rtx (mode);
2543 xop0 = widen_operand (op0, wider_mode, mode,
2544 unoptab != clrsb_optab, false);
2545 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2546 unoptab != clrsb_optab);
2547 if (temp != 0)
2548 temp = expand_binop
2549 (wider_mode, sub_optab, temp,
2550 gen_int_mode (GET_MODE_PRECISION (wider_mode)
2551 - GET_MODE_PRECISION (mode),
2552 wider_mode),
2553 target, true, OPTAB_DIRECT);
2554 if (temp == 0)
2555 delete_insns_since (last);
2557 return temp;
2561 return 0;
2564 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2565 quantities, choosing which based on whether the high word is nonzero. */
2566 static rtx
2567 expand_doubleword_clz (enum machine_mode mode, rtx op0, rtx target)
2569 rtx xop0 = force_reg (mode, op0);
2570 rtx subhi = gen_highpart (word_mode, xop0);
2571 rtx sublo = gen_lowpart (word_mode, xop0);
2572 rtx hi0_label = gen_label_rtx ();
2573 rtx after_label = gen_label_rtx ();
2574 rtx seq, temp, result;
2576 /* If we were not given a target, use a word_mode register, not a
2577 'mode' register. The result will fit, and nobody is expecting
2578 anything bigger (the return type of __builtin_clz* is int). */
2579 if (!target)
2580 target = gen_reg_rtx (word_mode);
2582 /* In any case, write to a word_mode scratch in both branches of the
2583 conditional, so we can ensure there is a single move insn setting
2584 'target' to tag a REG_EQUAL note on. */
2585 result = gen_reg_rtx (word_mode);
2587 start_sequence ();
2589 /* If the high word is not equal to zero,
2590 then clz of the full value is clz of the high word. */
2591 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2592 word_mode, true, hi0_label);
2594 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2595 if (!temp)
2596 goto fail;
2598 if (temp != result)
2599 convert_move (result, temp, true);
2601 emit_jump_insn (gen_jump (after_label));
2602 emit_barrier ();
2604 /* Else clz of the full value is clz of the low word plus the number
2605 of bits in the high word. */
2606 emit_label (hi0_label);
2608 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2609 if (!temp)
2610 goto fail;
2611 temp = expand_binop (word_mode, add_optab, temp,
2612 gen_int_mode (GET_MODE_BITSIZE (word_mode), word_mode),
2613 result, true, OPTAB_DIRECT);
2614 if (!temp)
2615 goto fail;
2616 if (temp != result)
2617 convert_move (result, temp, true);
2619 emit_label (after_label);
2620 convert_move (target, result, true);
2622 seq = get_insns ();
2623 end_sequence ();
2625 add_equal_note (seq, target, CLZ, xop0, 0);
2626 emit_insn (seq);
2627 return target;
2629 fail:
2630 end_sequence ();
2631 return 0;
2634 /* Try calculating
2635 (bswap:narrow x)
2637 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2638 static rtx
2639 widen_bswap (enum machine_mode mode, rtx op0, rtx target)
2641 enum mode_class mclass = GET_MODE_CLASS (mode);
2642 enum machine_mode wider_mode;
2643 rtx x, last;
2645 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2646 return NULL_RTX;
2648 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2649 wider_mode != VOIDmode;
2650 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2651 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2652 goto found;
2653 return NULL_RTX;
2655 found:
2656 last = get_last_insn ();
2658 x = widen_operand (op0, wider_mode, mode, true, true);
2659 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2661 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2662 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2663 if (x != 0)
2664 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2665 GET_MODE_BITSIZE (wider_mode)
2666 - GET_MODE_BITSIZE (mode),
2667 NULL_RTX, true);
2669 if (x != 0)
2671 if (target == 0)
2672 target = gen_reg_rtx (mode);
2673 emit_move_insn (target, gen_lowpart (mode, x));
2675 else
2676 delete_insns_since (last);
2678 return target;
2681 /* Try calculating bswap as two bswaps of two word-sized operands. */
2683 static rtx
2684 expand_doubleword_bswap (enum machine_mode mode, rtx op, rtx target)
2686 rtx t0, t1;
2688 t1 = expand_unop (word_mode, bswap_optab,
2689 operand_subword_force (op, 0, mode), NULL_RTX, true);
2690 t0 = expand_unop (word_mode, bswap_optab,
2691 operand_subword_force (op, 1, mode), NULL_RTX, true);
2693 if (target == 0 || !valid_multiword_target_p (target))
2694 target = gen_reg_rtx (mode);
2695 if (REG_P (target))
2696 emit_clobber (target);
2697 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2698 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2700 return target;
2703 /* Try calculating (parity x) as (and (popcount x) 1), where
2704 popcount can also be done in a wider mode. */
2705 static rtx
2706 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2708 enum mode_class mclass = GET_MODE_CLASS (mode);
2709 if (CLASS_HAS_WIDER_MODES_P (mclass))
2711 enum machine_mode wider_mode;
2712 for (wider_mode = mode; wider_mode != VOIDmode;
2713 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2715 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2717 rtx xop0, temp, last;
2719 last = get_last_insn ();
2721 if (target == 0)
2722 target = gen_reg_rtx (mode);
2723 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2724 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2725 true);
2726 if (temp != 0)
2727 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2728 target, true, OPTAB_DIRECT);
2729 if (temp == 0)
2730 delete_insns_since (last);
2732 return temp;
2736 return 0;
2739 /* Try calculating ctz(x) as K - clz(x & -x) ,
2740 where K is GET_MODE_PRECISION(mode) - 1.
2742 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2743 don't have to worry about what the hardware does in that case. (If
2744 the clz instruction produces the usual value at 0, which is K, the
2745 result of this code sequence will be -1; expand_ffs, below, relies
2746 on this. It might be nice to have it be K instead, for consistency
2747 with the (very few) processors that provide a ctz with a defined
2748 value, but that would take one more instruction, and it would be
2749 less convenient for expand_ffs anyway. */
2751 static rtx
2752 expand_ctz (enum machine_mode mode, rtx op0, rtx target)
2754 rtx seq, temp;
2756 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2757 return 0;
2759 start_sequence ();
2761 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2762 if (temp)
2763 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2764 true, OPTAB_DIRECT);
2765 if (temp)
2766 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2767 if (temp)
2768 temp = expand_binop (mode, sub_optab,
2769 gen_int_mode (GET_MODE_PRECISION (mode) - 1, mode),
2770 temp, target,
2771 true, OPTAB_DIRECT);
2772 if (temp == 0)
2774 end_sequence ();
2775 return 0;
2778 seq = get_insns ();
2779 end_sequence ();
2781 add_equal_note (seq, temp, CTZ, op0, 0);
2782 emit_insn (seq);
2783 return temp;
2787 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2788 else with the sequence used by expand_clz.
2790 The ffs builtin promises to return zero for a zero value and ctz/clz
2791 may have an undefined value in that case. If they do not give us a
2792 convenient value, we have to generate a test and branch. */
2793 static rtx
2794 expand_ffs (enum machine_mode mode, rtx op0, rtx target)
2796 HOST_WIDE_INT val = 0;
2797 bool defined_at_zero = false;
2798 rtx temp, seq;
2800 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2802 start_sequence ();
2804 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2805 if (!temp)
2806 goto fail;
2808 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2810 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2812 start_sequence ();
2813 temp = expand_ctz (mode, op0, 0);
2814 if (!temp)
2815 goto fail;
2817 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2819 defined_at_zero = true;
2820 val = (GET_MODE_PRECISION (mode) - 1) - val;
2823 else
2824 return 0;
2826 if (defined_at_zero && val == -1)
2827 /* No correction needed at zero. */;
2828 else
2830 /* We don't try to do anything clever with the situation found
2831 on some processors (eg Alpha) where ctz(0:mode) ==
2832 bitsize(mode). If someone can think of a way to send N to -1
2833 and leave alone all values in the range 0..N-1 (where N is a
2834 power of two), cheaper than this test-and-branch, please add it.
2836 The test-and-branch is done after the operation itself, in case
2837 the operation sets condition codes that can be recycled for this.
2838 (This is true on i386, for instance.) */
2840 rtx nonzero_label = gen_label_rtx ();
2841 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2842 mode, true, nonzero_label);
2844 convert_move (temp, GEN_INT (-1), false);
2845 emit_label (nonzero_label);
2848 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2849 to produce a value in the range 0..bitsize. */
2850 temp = expand_binop (mode, add_optab, temp, gen_int_mode (1, mode),
2851 target, false, OPTAB_DIRECT);
2852 if (!temp)
2853 goto fail;
2855 seq = get_insns ();
2856 end_sequence ();
2858 add_equal_note (seq, temp, FFS, op0, 0);
2859 emit_insn (seq);
2860 return temp;
2862 fail:
2863 end_sequence ();
2864 return 0;
2867 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2868 conditions, VAL may already be a SUBREG against which we cannot generate
2869 a further SUBREG. In this case, we expect forcing the value into a
2870 register will work around the situation. */
2872 static rtx
2873 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2874 enum machine_mode imode)
2876 rtx ret;
2877 ret = lowpart_subreg (omode, val, imode);
2878 if (ret == NULL)
2880 val = force_reg (imode, val);
2881 ret = lowpart_subreg (omode, val, imode);
2882 gcc_assert (ret != NULL);
2884 return ret;
2887 /* Expand a floating point absolute value or negation operation via a
2888 logical operation on the sign bit. */
2890 static rtx
2891 expand_absneg_bit (enum rtx_code code, enum machine_mode mode,
2892 rtx op0, rtx target)
2894 const struct real_format *fmt;
2895 int bitpos, word, nwords, i;
2896 enum machine_mode imode;
2897 double_int mask;
2898 rtx temp, insns;
2900 /* The format has to have a simple sign bit. */
2901 fmt = REAL_MODE_FORMAT (mode);
2902 if (fmt == NULL)
2903 return NULL_RTX;
2905 bitpos = fmt->signbit_rw;
2906 if (bitpos < 0)
2907 return NULL_RTX;
2909 /* Don't create negative zeros if the format doesn't support them. */
2910 if (code == NEG && !fmt->has_signed_zero)
2911 return NULL_RTX;
2913 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2915 imode = int_mode_for_mode (mode);
2916 if (imode == BLKmode)
2917 return NULL_RTX;
2918 word = 0;
2919 nwords = 1;
2921 else
2923 imode = word_mode;
2925 if (FLOAT_WORDS_BIG_ENDIAN)
2926 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2927 else
2928 word = bitpos / BITS_PER_WORD;
2929 bitpos = bitpos % BITS_PER_WORD;
2930 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2933 mask = double_int_zero.set_bit (bitpos);
2934 if (code == ABS)
2935 mask = ~mask;
2937 if (target == 0
2938 || target == op0
2939 || (nwords > 1 && !valid_multiword_target_p (target)))
2940 target = gen_reg_rtx (mode);
2942 if (nwords > 1)
2944 start_sequence ();
2946 for (i = 0; i < nwords; ++i)
2948 rtx targ_piece = operand_subword (target, i, 1, mode);
2949 rtx op0_piece = operand_subword_force (op0, i, mode);
2951 if (i == word)
2953 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2954 op0_piece,
2955 immed_double_int_const (mask, imode),
2956 targ_piece, 1, OPTAB_LIB_WIDEN);
2957 if (temp != targ_piece)
2958 emit_move_insn (targ_piece, temp);
2960 else
2961 emit_move_insn (targ_piece, op0_piece);
2964 insns = get_insns ();
2965 end_sequence ();
2967 emit_insn (insns);
2969 else
2971 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2972 gen_lowpart (imode, op0),
2973 immed_double_int_const (mask, imode),
2974 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2975 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2977 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2978 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2979 target);
2982 return target;
2985 /* As expand_unop, but will fail rather than attempt the operation in a
2986 different mode or with a libcall. */
2987 static rtx
2988 expand_unop_direct (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2989 int unsignedp)
2991 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2993 struct expand_operand ops[2];
2994 enum insn_code icode = optab_handler (unoptab, mode);
2995 rtx last = get_last_insn ();
2996 rtx pat;
2998 create_output_operand (&ops[0], target, mode);
2999 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
3000 pat = maybe_gen_insn (icode, 2, ops);
3001 if (pat)
3003 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3004 && ! add_equal_note (pat, ops[0].value, optab_to_code (unoptab),
3005 ops[1].value, NULL_RTX))
3007 delete_insns_since (last);
3008 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3011 emit_insn (pat);
3013 return ops[0].value;
3016 return 0;
3019 /* Generate code to perform an operation specified by UNOPTAB
3020 on operand OP0, with result having machine-mode MODE.
3022 UNSIGNEDP is for the case where we have to widen the operands
3023 to perform the operation. It says to use zero-extension.
3025 If TARGET is nonzero, the value
3026 is generated there, if it is convenient to do so.
3027 In all cases an rtx is returned for the locus of the value;
3028 this may or may not be TARGET. */
3031 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
3032 int unsignedp)
3034 enum mode_class mclass = GET_MODE_CLASS (mode);
3035 enum machine_mode wider_mode;
3036 rtx temp;
3037 rtx libfunc;
3039 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3040 if (temp)
3041 return temp;
3043 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3045 /* Widening (or narrowing) clz needs special treatment. */
3046 if (unoptab == clz_optab)
3048 temp = widen_leading (mode, op0, target, unoptab);
3049 if (temp)
3050 return temp;
3052 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3053 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3055 temp = expand_doubleword_clz (mode, op0, target);
3056 if (temp)
3057 return temp;
3060 goto try_libcall;
3063 if (unoptab == clrsb_optab)
3065 temp = widen_leading (mode, op0, target, unoptab);
3066 if (temp)
3067 return temp;
3068 goto try_libcall;
3071 /* Widening (or narrowing) bswap needs special treatment. */
3072 if (unoptab == bswap_optab)
3074 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3075 or ROTATERT. First try these directly; if this fails, then try the
3076 obvious pair of shifts with allowed widening, as this will probably
3077 be always more efficient than the other fallback methods. */
3078 if (mode == HImode)
3080 rtx last, temp1, temp2;
3082 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3084 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
3085 unsignedp, OPTAB_DIRECT);
3086 if (temp)
3087 return temp;
3090 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3092 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
3093 unsignedp, OPTAB_DIRECT);
3094 if (temp)
3095 return temp;
3098 last = get_last_insn ();
3100 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
3101 unsignedp, OPTAB_WIDEN);
3102 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
3103 unsignedp, OPTAB_WIDEN);
3104 if (temp1 && temp2)
3106 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3107 unsignedp, OPTAB_WIDEN);
3108 if (temp)
3109 return temp;
3112 delete_insns_since (last);
3115 temp = widen_bswap (mode, op0, target);
3116 if (temp)
3117 return temp;
3119 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3120 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3122 temp = expand_doubleword_bswap (mode, op0, target);
3123 if (temp)
3124 return temp;
3127 goto try_libcall;
3130 if (CLASS_HAS_WIDER_MODES_P (mclass))
3131 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3132 wider_mode != VOIDmode;
3133 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3135 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3137 rtx xop0 = op0;
3138 rtx last = get_last_insn ();
3140 /* For certain operations, we need not actually extend
3141 the narrow operand, as long as we will truncate the
3142 results to the same narrowness. */
3144 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3145 (unoptab == neg_optab
3146 || unoptab == one_cmpl_optab)
3147 && mclass == MODE_INT);
3149 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3150 unsignedp);
3152 if (temp)
3154 if (mclass != MODE_INT
3155 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3157 if (target == 0)
3158 target = gen_reg_rtx (mode);
3159 convert_move (target, temp, 0);
3160 return target;
3162 else
3163 return gen_lowpart (mode, temp);
3165 else
3166 delete_insns_since (last);
3170 /* These can be done a word at a time. */
3171 if (unoptab == one_cmpl_optab
3172 && mclass == MODE_INT
3173 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3174 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3176 int i;
3177 rtx insns;
3179 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3180 target = gen_reg_rtx (mode);
3182 start_sequence ();
3184 /* Do the actual arithmetic. */
3185 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3187 rtx target_piece = operand_subword (target, i, 1, mode);
3188 rtx x = expand_unop (word_mode, unoptab,
3189 operand_subword_force (op0, i, mode),
3190 target_piece, unsignedp);
3192 if (target_piece != x)
3193 emit_move_insn (target_piece, x);
3196 insns = get_insns ();
3197 end_sequence ();
3199 emit_insn (insns);
3200 return target;
3203 if (optab_to_code (unoptab) == NEG)
3205 /* Try negating floating point values by flipping the sign bit. */
3206 if (SCALAR_FLOAT_MODE_P (mode))
3208 temp = expand_absneg_bit (NEG, mode, op0, target);
3209 if (temp)
3210 return temp;
3213 /* If there is no negation pattern, and we have no negative zero,
3214 try subtracting from zero. */
3215 if (!HONOR_SIGNED_ZEROS (mode))
3217 temp = expand_binop (mode, (unoptab == negv_optab
3218 ? subv_optab : sub_optab),
3219 CONST0_RTX (mode), op0, target,
3220 unsignedp, OPTAB_DIRECT);
3221 if (temp)
3222 return temp;
3226 /* Try calculating parity (x) as popcount (x) % 2. */
3227 if (unoptab == parity_optab)
3229 temp = expand_parity (mode, op0, target);
3230 if (temp)
3231 return temp;
3234 /* Try implementing ffs (x) in terms of clz (x). */
3235 if (unoptab == ffs_optab)
3237 temp = expand_ffs (mode, op0, target);
3238 if (temp)
3239 return temp;
3242 /* Try implementing ctz (x) in terms of clz (x). */
3243 if (unoptab == ctz_optab)
3245 temp = expand_ctz (mode, op0, target);
3246 if (temp)
3247 return temp;
3250 try_libcall:
3251 /* Now try a library call in this mode. */
3252 libfunc = optab_libfunc (unoptab, mode);
3253 if (libfunc)
3255 rtx insns;
3256 rtx value;
3257 rtx eq_value;
3258 enum machine_mode outmode = mode;
3260 /* All of these functions return small values. Thus we choose to
3261 have them return something that isn't a double-word. */
3262 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3263 || unoptab == clrsb_optab || unoptab == popcount_optab
3264 || unoptab == parity_optab)
3265 outmode
3266 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3267 optab_libfunc (unoptab, mode)));
3269 start_sequence ();
3271 /* Pass 1 for NO_QUEUE so we don't lose any increments
3272 if the libcall is cse'd or moved. */
3273 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3274 1, op0, mode);
3275 insns = get_insns ();
3276 end_sequence ();
3278 target = gen_reg_rtx (outmode);
3279 eq_value = gen_rtx_fmt_e (optab_to_code (unoptab), mode, op0);
3280 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3281 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3282 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3283 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3284 emit_libcall_block_1 (insns, target, value, eq_value,
3285 trapv_unoptab_p (unoptab));
3287 return target;
3290 /* It can't be done in this mode. Can we do it in a wider mode? */
3292 if (CLASS_HAS_WIDER_MODES_P (mclass))
3294 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3295 wider_mode != VOIDmode;
3296 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3298 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3299 || optab_libfunc (unoptab, wider_mode))
3301 rtx xop0 = op0;
3302 rtx last = get_last_insn ();
3304 /* For certain operations, we need not actually extend
3305 the narrow operand, as long as we will truncate the
3306 results to the same narrowness. */
3307 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3308 (unoptab == neg_optab
3309 || unoptab == one_cmpl_optab
3310 || unoptab == bswap_optab)
3311 && mclass == MODE_INT);
3313 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3314 unsignedp);
3316 /* If we are generating clz using wider mode, adjust the
3317 result. Similarly for clrsb. */
3318 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3319 && temp != 0)
3320 temp = expand_binop
3321 (wider_mode, sub_optab, temp,
3322 gen_int_mode (GET_MODE_PRECISION (wider_mode)
3323 - GET_MODE_PRECISION (mode),
3324 wider_mode),
3325 target, true, OPTAB_DIRECT);
3327 /* Likewise for bswap. */
3328 if (unoptab == bswap_optab && temp != 0)
3330 gcc_assert (GET_MODE_PRECISION (wider_mode)
3331 == GET_MODE_BITSIZE (wider_mode)
3332 && GET_MODE_PRECISION (mode)
3333 == GET_MODE_BITSIZE (mode));
3335 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3336 GET_MODE_BITSIZE (wider_mode)
3337 - GET_MODE_BITSIZE (mode),
3338 NULL_RTX, true);
3341 if (temp)
3343 if (mclass != MODE_INT)
3345 if (target == 0)
3346 target = gen_reg_rtx (mode);
3347 convert_move (target, temp, 0);
3348 return target;
3350 else
3351 return gen_lowpart (mode, temp);
3353 else
3354 delete_insns_since (last);
3359 /* One final attempt at implementing negation via subtraction,
3360 this time allowing widening of the operand. */
3361 if (optab_to_code (unoptab) == NEG && !HONOR_SIGNED_ZEROS (mode))
3363 rtx temp;
3364 temp = expand_binop (mode,
3365 unoptab == negv_optab ? subv_optab : sub_optab,
3366 CONST0_RTX (mode), op0,
3367 target, unsignedp, OPTAB_LIB_WIDEN);
3368 if (temp)
3369 return temp;
3372 return 0;
3375 /* Emit code to compute the absolute value of OP0, with result to
3376 TARGET if convenient. (TARGET may be 0.) The return value says
3377 where the result actually is to be found.
3379 MODE is the mode of the operand; the mode of the result is
3380 different but can be deduced from MODE.
3385 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
3386 int result_unsignedp)
3388 rtx temp;
3390 if (GET_MODE_CLASS (mode) != MODE_INT
3391 || ! flag_trapv)
3392 result_unsignedp = 1;
3394 /* First try to do it with a special abs instruction. */
3395 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3396 op0, target, 0);
3397 if (temp != 0)
3398 return temp;
3400 /* For floating point modes, try clearing the sign bit. */
3401 if (SCALAR_FLOAT_MODE_P (mode))
3403 temp = expand_absneg_bit (ABS, mode, op0, target);
3404 if (temp)
3405 return temp;
3408 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3409 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3410 && !HONOR_SIGNED_ZEROS (mode))
3412 rtx last = get_last_insn ();
3414 temp = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3415 op0, NULL_RTX, 0);
3416 if (temp != 0)
3417 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3418 OPTAB_WIDEN);
3420 if (temp != 0)
3421 return temp;
3423 delete_insns_since (last);
3426 /* If this machine has expensive jumps, we can do integer absolute
3427 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3428 where W is the width of MODE. */
3430 if (GET_MODE_CLASS (mode) == MODE_INT
3431 && BRANCH_COST (optimize_insn_for_speed_p (),
3432 false) >= 2)
3434 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3435 GET_MODE_PRECISION (mode) - 1,
3436 NULL_RTX, 0);
3438 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3439 OPTAB_LIB_WIDEN);
3440 if (temp != 0)
3441 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3442 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3444 if (temp != 0)
3445 return temp;
3448 return NULL_RTX;
3452 expand_abs (enum machine_mode mode, rtx op0, rtx target,
3453 int result_unsignedp, int safe)
3455 rtx temp, op1;
3457 if (GET_MODE_CLASS (mode) != MODE_INT
3458 || ! flag_trapv)
3459 result_unsignedp = 1;
3461 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3462 if (temp != 0)
3463 return temp;
3465 /* If that does not win, use conditional jump and negate. */
3467 /* It is safe to use the target if it is the same
3468 as the source if this is also a pseudo register */
3469 if (op0 == target && REG_P (op0)
3470 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3471 safe = 1;
3473 op1 = gen_label_rtx ();
3474 if (target == 0 || ! safe
3475 || GET_MODE (target) != mode
3476 || (MEM_P (target) && MEM_VOLATILE_P (target))
3477 || (REG_P (target)
3478 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3479 target = gen_reg_rtx (mode);
3481 emit_move_insn (target, op0);
3482 NO_DEFER_POP;
3484 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3485 NULL_RTX, NULL_RTX, op1, -1);
3487 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3488 target, target, 0);
3489 if (op0 != target)
3490 emit_move_insn (target, op0);
3491 emit_label (op1);
3492 OK_DEFER_POP;
3493 return target;
3496 /* Emit code to compute the one's complement absolute value of OP0
3497 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3498 (TARGET may be NULL_RTX.) The return value says where the result
3499 actually is to be found.
3501 MODE is the mode of the operand; the mode of the result is
3502 different but can be deduced from MODE. */
3505 expand_one_cmpl_abs_nojump (enum machine_mode mode, rtx op0, rtx target)
3507 rtx temp;
3509 /* Not applicable for floating point modes. */
3510 if (FLOAT_MODE_P (mode))
3511 return NULL_RTX;
3513 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3514 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3516 rtx last = get_last_insn ();
3518 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3519 if (temp != 0)
3520 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3521 OPTAB_WIDEN);
3523 if (temp != 0)
3524 return temp;
3526 delete_insns_since (last);
3529 /* If this machine has expensive jumps, we can do one's complement
3530 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3532 if (GET_MODE_CLASS (mode) == MODE_INT
3533 && BRANCH_COST (optimize_insn_for_speed_p (),
3534 false) >= 2)
3536 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3537 GET_MODE_PRECISION (mode) - 1,
3538 NULL_RTX, 0);
3540 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3541 OPTAB_LIB_WIDEN);
3543 if (temp != 0)
3544 return temp;
3547 return NULL_RTX;
3550 /* A subroutine of expand_copysign, perform the copysign operation using the
3551 abs and neg primitives advertised to exist on the target. The assumption
3552 is that we have a split register file, and leaving op0 in fp registers,
3553 and not playing with subregs so much, will help the register allocator. */
3555 static rtx
3556 expand_copysign_absneg (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3557 int bitpos, bool op0_is_abs)
3559 enum machine_mode imode;
3560 enum insn_code icode;
3561 rtx sign, label;
3563 if (target == op1)
3564 target = NULL_RTX;
3566 /* Check if the back end provides an insn that handles signbit for the
3567 argument's mode. */
3568 icode = optab_handler (signbit_optab, mode);
3569 if (icode != CODE_FOR_nothing)
3571 imode = insn_data[(int) icode].operand[0].mode;
3572 sign = gen_reg_rtx (imode);
3573 emit_unop_insn (icode, sign, op1, UNKNOWN);
3575 else
3577 double_int mask;
3579 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3581 imode = int_mode_for_mode (mode);
3582 if (imode == BLKmode)
3583 return NULL_RTX;
3584 op1 = gen_lowpart (imode, op1);
3586 else
3588 int word;
3590 imode = word_mode;
3591 if (FLOAT_WORDS_BIG_ENDIAN)
3592 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3593 else
3594 word = bitpos / BITS_PER_WORD;
3595 bitpos = bitpos % BITS_PER_WORD;
3596 op1 = operand_subword_force (op1, word, mode);
3599 mask = double_int_zero.set_bit (bitpos);
3601 sign = expand_binop (imode, and_optab, op1,
3602 immed_double_int_const (mask, imode),
3603 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3606 if (!op0_is_abs)
3608 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3609 if (op0 == NULL)
3610 return NULL_RTX;
3611 target = op0;
3613 else
3615 if (target == NULL_RTX)
3616 target = copy_to_reg (op0);
3617 else
3618 emit_move_insn (target, op0);
3621 label = gen_label_rtx ();
3622 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3624 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3625 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3626 else
3627 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3628 if (op0 != target)
3629 emit_move_insn (target, op0);
3631 emit_label (label);
3633 return target;
3637 /* A subroutine of expand_copysign, perform the entire copysign operation
3638 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3639 is true if op0 is known to have its sign bit clear. */
3641 static rtx
3642 expand_copysign_bit (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3643 int bitpos, bool op0_is_abs)
3645 enum machine_mode imode;
3646 double_int mask;
3647 int word, nwords, i;
3648 rtx temp, insns;
3650 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3652 imode = int_mode_for_mode (mode);
3653 if (imode == BLKmode)
3654 return NULL_RTX;
3655 word = 0;
3656 nwords = 1;
3658 else
3660 imode = word_mode;
3662 if (FLOAT_WORDS_BIG_ENDIAN)
3663 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3664 else
3665 word = bitpos / BITS_PER_WORD;
3666 bitpos = bitpos % BITS_PER_WORD;
3667 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3670 mask = double_int_zero.set_bit (bitpos);
3672 if (target == 0
3673 || target == op0
3674 || target == op1
3675 || (nwords > 1 && !valid_multiword_target_p (target)))
3676 target = gen_reg_rtx (mode);
3678 if (nwords > 1)
3680 start_sequence ();
3682 for (i = 0; i < nwords; ++i)
3684 rtx targ_piece = operand_subword (target, i, 1, mode);
3685 rtx op0_piece = operand_subword_force (op0, i, mode);
3687 if (i == word)
3689 if (!op0_is_abs)
3690 op0_piece
3691 = expand_binop (imode, and_optab, op0_piece,
3692 immed_double_int_const (~mask, imode),
3693 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3695 op1 = expand_binop (imode, and_optab,
3696 operand_subword_force (op1, i, mode),
3697 immed_double_int_const (mask, imode),
3698 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3700 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3701 targ_piece, 1, OPTAB_LIB_WIDEN);
3702 if (temp != targ_piece)
3703 emit_move_insn (targ_piece, temp);
3705 else
3706 emit_move_insn (targ_piece, op0_piece);
3709 insns = get_insns ();
3710 end_sequence ();
3712 emit_insn (insns);
3714 else
3716 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3717 immed_double_int_const (mask, imode),
3718 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3720 op0 = gen_lowpart (imode, op0);
3721 if (!op0_is_abs)
3722 op0 = expand_binop (imode, and_optab, op0,
3723 immed_double_int_const (~mask, imode),
3724 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3726 temp = expand_binop (imode, ior_optab, op0, op1,
3727 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3728 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3731 return target;
3734 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3735 scalar floating point mode. Return NULL if we do not know how to
3736 expand the operation inline. */
3739 expand_copysign (rtx op0, rtx op1, rtx target)
3741 enum machine_mode mode = GET_MODE (op0);
3742 const struct real_format *fmt;
3743 bool op0_is_abs;
3744 rtx temp;
3746 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3747 gcc_assert (GET_MODE (op1) == mode);
3749 /* First try to do it with a special instruction. */
3750 temp = expand_binop (mode, copysign_optab, op0, op1,
3751 target, 0, OPTAB_DIRECT);
3752 if (temp)
3753 return temp;
3755 fmt = REAL_MODE_FORMAT (mode);
3756 if (fmt == NULL || !fmt->has_signed_zero)
3757 return NULL_RTX;
3759 op0_is_abs = false;
3760 if (CONST_DOUBLE_AS_FLOAT_P (op0))
3762 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3763 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3764 op0_is_abs = true;
3767 if (fmt->signbit_ro >= 0
3768 && (CONST_DOUBLE_AS_FLOAT_P (op0)
3769 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3770 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3772 temp = expand_copysign_absneg (mode, op0, op1, target,
3773 fmt->signbit_ro, op0_is_abs);
3774 if (temp)
3775 return temp;
3778 if (fmt->signbit_rw < 0)
3779 return NULL_RTX;
3780 return expand_copysign_bit (mode, op0, op1, target,
3781 fmt->signbit_rw, op0_is_abs);
3784 /* Generate an instruction whose insn-code is INSN_CODE,
3785 with two operands: an output TARGET and an input OP0.
3786 TARGET *must* be nonzero, and the output is always stored there.
3787 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3788 the value that is stored into TARGET.
3790 Return false if expansion failed. */
3792 bool
3793 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3794 enum rtx_code code)
3796 struct expand_operand ops[2];
3797 rtx pat;
3799 create_output_operand (&ops[0], target, GET_MODE (target));
3800 create_input_operand (&ops[1], op0, GET_MODE (op0));
3801 pat = maybe_gen_insn (icode, 2, ops);
3802 if (!pat)
3803 return false;
3805 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
3806 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3808 emit_insn (pat);
3810 if (ops[0].value != target)
3811 emit_move_insn (target, ops[0].value);
3812 return true;
3814 /* Generate an instruction whose insn-code is INSN_CODE,
3815 with two operands: an output TARGET and an input OP0.
3816 TARGET *must* be nonzero, and the output is always stored there.
3817 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3818 the value that is stored into TARGET. */
3820 void
3821 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3823 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3824 gcc_assert (ok);
3827 struct no_conflict_data
3829 rtx target, first, insn;
3830 bool must_stay;
3833 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3834 the currently examined clobber / store has to stay in the list of
3835 insns that constitute the actual libcall block. */
3836 static void
3837 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3839 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3841 /* If this inns directly contributes to setting the target, it must stay. */
3842 if (reg_overlap_mentioned_p (p->target, dest))
3843 p->must_stay = true;
3844 /* If we haven't committed to keeping any other insns in the list yet,
3845 there is nothing more to check. */
3846 else if (p->insn == p->first)
3847 return;
3848 /* If this insn sets / clobbers a register that feeds one of the insns
3849 already in the list, this insn has to stay too. */
3850 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3851 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3852 || reg_used_between_p (dest, p->first, p->insn)
3853 /* Likewise if this insn depends on a register set by a previous
3854 insn in the list, or if it sets a result (presumably a hard
3855 register) that is set or clobbered by a previous insn.
3856 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3857 SET_DEST perform the former check on the address, and the latter
3858 check on the MEM. */
3859 || (GET_CODE (set) == SET
3860 && (modified_in_p (SET_SRC (set), p->first)
3861 || modified_in_p (SET_DEST (set), p->first)
3862 || modified_between_p (SET_SRC (set), p->first, p->insn)
3863 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3864 p->must_stay = true;
3868 /* Emit code to make a call to a constant function or a library call.
3870 INSNS is a list containing all insns emitted in the call.
3871 These insns leave the result in RESULT. Our block is to copy RESULT
3872 to TARGET, which is logically equivalent to EQUIV.
3874 We first emit any insns that set a pseudo on the assumption that these are
3875 loading constants into registers; doing so allows them to be safely cse'ed
3876 between blocks. Then we emit all the other insns in the block, followed by
3877 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3878 note with an operand of EQUIV. */
3880 static void
3881 emit_libcall_block_1 (rtx insns, rtx target, rtx result, rtx equiv,
3882 bool equiv_may_trap)
3884 rtx final_dest = target;
3885 rtx next, last, insn;
3887 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3888 into a MEM later. Protect the libcall block from this change. */
3889 if (! REG_P (target) || REG_USERVAR_P (target))
3890 target = gen_reg_rtx (GET_MODE (target));
3892 /* If we're using non-call exceptions, a libcall corresponding to an
3893 operation that may trap may also trap. */
3894 /* ??? See the comment in front of make_reg_eh_region_note. */
3895 if (cfun->can_throw_non_call_exceptions
3896 && (equiv_may_trap || may_trap_p (equiv)))
3898 for (insn = insns; insn; insn = NEXT_INSN (insn))
3899 if (CALL_P (insn))
3901 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3902 if (note)
3904 int lp_nr = INTVAL (XEXP (note, 0));
3905 if (lp_nr == 0 || lp_nr == INT_MIN)
3906 remove_note (insn, note);
3910 else
3912 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3913 reg note to indicate that this call cannot throw or execute a nonlocal
3914 goto (unless there is already a REG_EH_REGION note, in which case
3915 we update it). */
3916 for (insn = insns; insn; insn = NEXT_INSN (insn))
3917 if (CALL_P (insn))
3918 make_reg_eh_region_note_nothrow_nononlocal (insn);
3921 /* First emit all insns that set pseudos. Remove them from the list as
3922 we go. Avoid insns that set pseudos which were referenced in previous
3923 insns. These can be generated by move_by_pieces, for example,
3924 to update an address. Similarly, avoid insns that reference things
3925 set in previous insns. */
3927 for (insn = insns; insn; insn = next)
3929 rtx set = single_set (insn);
3931 next = NEXT_INSN (insn);
3933 if (set != 0 && REG_P (SET_DEST (set))
3934 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3936 struct no_conflict_data data;
3938 data.target = const0_rtx;
3939 data.first = insns;
3940 data.insn = insn;
3941 data.must_stay = 0;
3942 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3943 if (! data.must_stay)
3945 if (PREV_INSN (insn))
3946 NEXT_INSN (PREV_INSN (insn)) = next;
3947 else
3948 insns = next;
3950 if (next)
3951 PREV_INSN (next) = PREV_INSN (insn);
3953 add_insn (insn);
3957 /* Some ports use a loop to copy large arguments onto the stack.
3958 Don't move anything outside such a loop. */
3959 if (LABEL_P (insn))
3960 break;
3963 /* Write the remaining insns followed by the final copy. */
3964 for (insn = insns; insn; insn = next)
3966 next = NEXT_INSN (insn);
3968 add_insn (insn);
3971 last = emit_move_insn (target, result);
3972 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3974 if (final_dest != target)
3975 emit_move_insn (final_dest, target);
3978 void
3979 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3981 emit_libcall_block_1 (insns, target, result, equiv, false);
3984 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3985 PURPOSE describes how this comparison will be used. CODE is the rtx
3986 comparison code we will be using.
3988 ??? Actually, CODE is slightly weaker than that. A target is still
3989 required to implement all of the normal bcc operations, but not
3990 required to implement all (or any) of the unordered bcc operations. */
3993 can_compare_p (enum rtx_code code, enum machine_mode mode,
3994 enum can_compare_purpose purpose)
3996 rtx test;
3997 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
4000 enum insn_code icode;
4002 if (purpose == ccp_jump
4003 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
4004 && insn_operand_matches (icode, 0, test))
4005 return 1;
4006 if (purpose == ccp_store_flag
4007 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
4008 && insn_operand_matches (icode, 1, test))
4009 return 1;
4010 if (purpose == ccp_cmov
4011 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
4012 return 1;
4014 mode = GET_MODE_WIDER_MODE (mode);
4015 PUT_MODE (test, mode);
4017 while (mode != VOIDmode);
4019 return 0;
4022 /* This function is called when we are going to emit a compare instruction that
4023 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
4025 *PMODE is the mode of the inputs (in case they are const_int).
4026 *PUNSIGNEDP nonzero says that the operands are unsigned;
4027 this matters if they need to be widened (as given by METHODS).
4029 If they have mode BLKmode, then SIZE specifies the size of both operands.
4031 This function performs all the setup necessary so that the caller only has
4032 to emit a single comparison insn. This setup can involve doing a BLKmode
4033 comparison or emitting a library call to perform the comparison if no insn
4034 is available to handle it.
4035 The values which are passed in through pointers can be modified; the caller
4036 should perform the comparison on the modified values. Constant
4037 comparisons must have already been folded. */
4039 static void
4040 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4041 int unsignedp, enum optab_methods methods,
4042 rtx *ptest, enum machine_mode *pmode)
4044 enum machine_mode mode = *pmode;
4045 rtx libfunc, test;
4046 enum machine_mode cmp_mode;
4047 enum mode_class mclass;
4049 /* The other methods are not needed. */
4050 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4051 || methods == OPTAB_LIB_WIDEN);
4053 /* If we are optimizing, force expensive constants into a register. */
4054 if (CONSTANT_P (x) && optimize
4055 && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
4056 > COSTS_N_INSNS (1)))
4057 x = force_reg (mode, x);
4059 if (CONSTANT_P (y) && optimize
4060 && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
4061 > COSTS_N_INSNS (1)))
4062 y = force_reg (mode, y);
4064 #ifdef HAVE_cc0
4065 /* Make sure if we have a canonical comparison. The RTL
4066 documentation states that canonical comparisons are required only
4067 for targets which have cc0. */
4068 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4069 #endif
4071 /* Don't let both operands fail to indicate the mode. */
4072 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4073 x = force_reg (mode, x);
4074 if (mode == VOIDmode)
4075 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4077 /* Handle all BLKmode compares. */
4079 if (mode == BLKmode)
4081 enum machine_mode result_mode;
4082 enum insn_code cmp_code;
4083 tree length_type;
4084 rtx libfunc;
4085 rtx result;
4086 rtx opalign
4087 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4089 gcc_assert (size);
4091 /* Try to use a memory block compare insn - either cmpstr
4092 or cmpmem will do. */
4093 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4094 cmp_mode != VOIDmode;
4095 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4097 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4098 if (cmp_code == CODE_FOR_nothing)
4099 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4100 if (cmp_code == CODE_FOR_nothing)
4101 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4102 if (cmp_code == CODE_FOR_nothing)
4103 continue;
4105 /* Must make sure the size fits the insn's mode. */
4106 if ((CONST_INT_P (size)
4107 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4108 || (GET_MODE_BITSIZE (GET_MODE (size))
4109 > GET_MODE_BITSIZE (cmp_mode)))
4110 continue;
4112 result_mode = insn_data[cmp_code].operand[0].mode;
4113 result = gen_reg_rtx (result_mode);
4114 size = convert_to_mode (cmp_mode, size, 1);
4115 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4117 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4118 *pmode = result_mode;
4119 return;
4122 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4123 goto fail;
4125 /* Otherwise call a library function, memcmp. */
4126 libfunc = memcmp_libfunc;
4127 length_type = sizetype;
4128 result_mode = TYPE_MODE (integer_type_node);
4129 cmp_mode = TYPE_MODE (length_type);
4130 size = convert_to_mode (TYPE_MODE (length_type), size,
4131 TYPE_UNSIGNED (length_type));
4133 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4134 result_mode, 3,
4135 XEXP (x, 0), Pmode,
4136 XEXP (y, 0), Pmode,
4137 size, cmp_mode);
4138 x = result;
4139 y = const0_rtx;
4140 mode = result_mode;
4141 methods = OPTAB_LIB_WIDEN;
4142 unsignedp = false;
4145 /* Don't allow operands to the compare to trap, as that can put the
4146 compare and branch in different basic blocks. */
4147 if (cfun->can_throw_non_call_exceptions)
4149 if (may_trap_p (x))
4150 x = force_reg (mode, x);
4151 if (may_trap_p (y))
4152 y = force_reg (mode, y);
4155 if (GET_MODE_CLASS (mode) == MODE_CC)
4157 gcc_assert (can_compare_p (comparison, CCmode, ccp_jump));
4158 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4159 return;
4162 mclass = GET_MODE_CLASS (mode);
4163 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4164 cmp_mode = mode;
4167 enum insn_code icode;
4168 icode = optab_handler (cbranch_optab, cmp_mode);
4169 if (icode != CODE_FOR_nothing
4170 && insn_operand_matches (icode, 0, test))
4172 rtx last = get_last_insn ();
4173 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4174 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4175 if (op0 && op1
4176 && insn_operand_matches (icode, 1, op0)
4177 && insn_operand_matches (icode, 2, op1))
4179 XEXP (test, 0) = op0;
4180 XEXP (test, 1) = op1;
4181 *ptest = test;
4182 *pmode = cmp_mode;
4183 return;
4185 delete_insns_since (last);
4188 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4189 break;
4190 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4192 while (cmp_mode != VOIDmode);
4194 if (methods != OPTAB_LIB_WIDEN)
4195 goto fail;
4197 if (!SCALAR_FLOAT_MODE_P (mode))
4199 rtx result;
4200 enum machine_mode ret_mode;
4202 /* Handle a libcall just for the mode we are using. */
4203 libfunc = optab_libfunc (cmp_optab, mode);
4204 gcc_assert (libfunc);
4206 /* If we want unsigned, and this mode has a distinct unsigned
4207 comparison routine, use that. */
4208 if (unsignedp)
4210 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4211 if (ulibfunc)
4212 libfunc = ulibfunc;
4215 ret_mode = targetm.libgcc_cmp_return_mode ();
4216 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4217 ret_mode, 2, x, mode, y, mode);
4219 /* There are two kinds of comparison routines. Biased routines
4220 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4221 of gcc expect that the comparison operation is equivalent
4222 to the modified comparison. For signed comparisons compare the
4223 result against 1 in the biased case, and zero in the unbiased
4224 case. For unsigned comparisons always compare against 1 after
4225 biasing the unbiased result by adding 1. This gives us a way to
4226 represent LTU.
4227 The comparisons in the fixed-point helper library are always
4228 biased. */
4229 x = result;
4230 y = const1_rtx;
4232 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4234 if (unsignedp)
4235 x = plus_constant (ret_mode, result, 1);
4236 else
4237 y = const0_rtx;
4240 *pmode = word_mode;
4241 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4242 ptest, pmode);
4244 else
4245 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4247 return;
4249 fail:
4250 *ptest = NULL_RTX;
4253 /* Before emitting an insn with code ICODE, make sure that X, which is going
4254 to be used for operand OPNUM of the insn, is converted from mode MODE to
4255 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4256 that it is accepted by the operand predicate. Return the new value. */
4259 prepare_operand (enum insn_code icode, rtx x, int opnum, enum machine_mode mode,
4260 enum machine_mode wider_mode, int unsignedp)
4262 if (mode != wider_mode)
4263 x = convert_modes (wider_mode, mode, x, unsignedp);
4265 if (!insn_operand_matches (icode, opnum, x))
4267 if (reload_completed)
4268 return NULL_RTX;
4269 x = copy_to_mode_reg (insn_data[(int) icode].operand[opnum].mode, x);
4272 return x;
4275 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4276 we can do the branch. */
4278 static void
4279 emit_cmp_and_jump_insn_1 (rtx test, enum machine_mode mode, rtx label, int prob)
4281 enum machine_mode optab_mode;
4282 enum mode_class mclass;
4283 enum insn_code icode;
4284 rtx insn;
4286 mclass = GET_MODE_CLASS (mode);
4287 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4288 icode = optab_handler (cbranch_optab, optab_mode);
4290 gcc_assert (icode != CODE_FOR_nothing);
4291 gcc_assert (insn_operand_matches (icode, 0, test));
4292 insn = emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0),
4293 XEXP (test, 1), label));
4294 if (prob != -1
4295 && profile_status_for_fn (cfun) != PROFILE_ABSENT
4296 && insn
4297 && JUMP_P (insn)
4298 && any_condjump_p (insn)
4299 && !find_reg_note (insn, REG_BR_PROB, 0))
4300 add_int_reg_note (insn, REG_BR_PROB, prob);
4303 /* Generate code to compare X with Y so that the condition codes are
4304 set and to jump to LABEL if the condition is true. If X is a
4305 constant and Y is not a constant, then the comparison is swapped to
4306 ensure that the comparison RTL has the canonical form.
4308 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4309 need to be widened. UNSIGNEDP is also used to select the proper
4310 branch condition code.
4312 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4314 MODE is the mode of the inputs (in case they are const_int).
4316 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4317 It will be potentially converted into an unsigned variant based on
4318 UNSIGNEDP to select a proper jump instruction.
4320 PROB is the probability of jumping to LABEL. */
4322 void
4323 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4324 enum machine_mode mode, int unsignedp, rtx label,
4325 int prob)
4327 rtx op0 = x, op1 = y;
4328 rtx test;
4330 /* Swap operands and condition to ensure canonical RTL. */
4331 if (swap_commutative_operands_p (x, y)
4332 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4334 op0 = y, op1 = x;
4335 comparison = swap_condition (comparison);
4338 /* If OP0 is still a constant, then both X and Y must be constants
4339 or the opposite comparison is not supported. Force X into a register
4340 to create canonical RTL. */
4341 if (CONSTANT_P (op0))
4342 op0 = force_reg (mode, op0);
4344 if (unsignedp)
4345 comparison = unsigned_condition (comparison);
4347 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4348 &test, &mode);
4349 emit_cmp_and_jump_insn_1 (test, mode, label, prob);
4353 /* Emit a library call comparison between floating point X and Y.
4354 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4356 static void
4357 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4358 rtx *ptest, enum machine_mode *pmode)
4360 enum rtx_code swapped = swap_condition (comparison);
4361 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4362 enum machine_mode orig_mode = GET_MODE (x);
4363 enum machine_mode mode, cmp_mode;
4364 rtx true_rtx, false_rtx;
4365 rtx value, target, insns, equiv;
4366 rtx libfunc = 0;
4367 bool reversed_p = false;
4368 cmp_mode = targetm.libgcc_cmp_return_mode ();
4370 for (mode = orig_mode;
4371 mode != VOIDmode;
4372 mode = GET_MODE_WIDER_MODE (mode))
4374 if (code_to_optab (comparison)
4375 && (libfunc = optab_libfunc (code_to_optab (comparison), mode)))
4376 break;
4378 if (code_to_optab (swapped)
4379 && (libfunc = optab_libfunc (code_to_optab (swapped), mode)))
4381 rtx tmp;
4382 tmp = x; x = y; y = tmp;
4383 comparison = swapped;
4384 break;
4387 if (code_to_optab (reversed)
4388 && (libfunc = optab_libfunc (code_to_optab (reversed), mode)))
4390 comparison = reversed;
4391 reversed_p = true;
4392 break;
4396 gcc_assert (mode != VOIDmode);
4398 if (mode != orig_mode)
4400 x = convert_to_mode (mode, x, 0);
4401 y = convert_to_mode (mode, y, 0);
4404 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4405 the RTL. The allows the RTL optimizers to delete the libcall if the
4406 condition can be determined at compile-time. */
4407 if (comparison == UNORDERED
4408 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4410 true_rtx = const_true_rtx;
4411 false_rtx = const0_rtx;
4413 else
4415 switch (comparison)
4417 case EQ:
4418 true_rtx = const0_rtx;
4419 false_rtx = const_true_rtx;
4420 break;
4422 case NE:
4423 true_rtx = const_true_rtx;
4424 false_rtx = const0_rtx;
4425 break;
4427 case GT:
4428 true_rtx = const1_rtx;
4429 false_rtx = const0_rtx;
4430 break;
4432 case GE:
4433 true_rtx = const0_rtx;
4434 false_rtx = constm1_rtx;
4435 break;
4437 case LT:
4438 true_rtx = constm1_rtx;
4439 false_rtx = const0_rtx;
4440 break;
4442 case LE:
4443 true_rtx = const0_rtx;
4444 false_rtx = const1_rtx;
4445 break;
4447 default:
4448 gcc_unreachable ();
4452 if (comparison == UNORDERED)
4454 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4455 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4456 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4457 temp, const_true_rtx, equiv);
4459 else
4461 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4462 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4463 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4464 equiv, true_rtx, false_rtx);
4467 start_sequence ();
4468 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4469 cmp_mode, 2, x, mode, y, mode);
4470 insns = get_insns ();
4471 end_sequence ();
4473 target = gen_reg_rtx (cmp_mode);
4474 emit_libcall_block (insns, target, value, equiv);
4476 if (comparison == UNORDERED
4477 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4478 || reversed_p)
4479 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4480 else
4481 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4483 *pmode = cmp_mode;
4486 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4488 void
4489 emit_indirect_jump (rtx loc)
4491 struct expand_operand ops[1];
4493 create_address_operand (&ops[0], loc);
4494 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4495 emit_barrier ();
4498 #ifdef HAVE_conditional_move
4500 /* Emit a conditional move instruction if the machine supports one for that
4501 condition and machine mode.
4503 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4504 the mode to use should they be constants. If it is VOIDmode, they cannot
4505 both be constants.
4507 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4508 should be stored there. MODE is the mode to use should they be constants.
4509 If it is VOIDmode, they cannot both be constants.
4511 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4512 is not supported. */
4515 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4516 enum machine_mode cmode, rtx op2, rtx op3,
4517 enum machine_mode mode, int unsignedp)
4519 rtx tem, comparison, last;
4520 enum insn_code icode;
4521 enum rtx_code reversed;
4523 /* If one operand is constant, make it the second one. Only do this
4524 if the other operand is not constant as well. */
4526 if (swap_commutative_operands_p (op0, op1))
4528 tem = op0;
4529 op0 = op1;
4530 op1 = tem;
4531 code = swap_condition (code);
4534 /* get_condition will prefer to generate LT and GT even if the old
4535 comparison was against zero, so undo that canonicalization here since
4536 comparisons against zero are cheaper. */
4537 if (code == LT && op1 == const1_rtx)
4538 code = LE, op1 = const0_rtx;
4539 else if (code == GT && op1 == constm1_rtx)
4540 code = GE, op1 = const0_rtx;
4542 if (cmode == VOIDmode)
4543 cmode = GET_MODE (op0);
4545 if (swap_commutative_operands_p (op2, op3)
4546 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4547 != UNKNOWN))
4549 tem = op2;
4550 op2 = op3;
4551 op3 = tem;
4552 code = reversed;
4555 if (mode == VOIDmode)
4556 mode = GET_MODE (op2);
4558 icode = direct_optab_handler (movcc_optab, mode);
4560 if (icode == CODE_FOR_nothing)
4561 return 0;
4563 if (!target)
4564 target = gen_reg_rtx (mode);
4566 code = unsignedp ? unsigned_condition (code) : code;
4567 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4569 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4570 return NULL and let the caller figure out how best to deal with this
4571 situation. */
4572 if (!COMPARISON_P (comparison))
4573 return NULL_RTX;
4575 saved_pending_stack_adjust save;
4576 save_pending_stack_adjust (&save);
4577 last = get_last_insn ();
4578 do_pending_stack_adjust ();
4579 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4580 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4581 &comparison, &cmode);
4582 if (comparison)
4584 struct expand_operand ops[4];
4586 create_output_operand (&ops[0], target, mode);
4587 create_fixed_operand (&ops[1], comparison);
4588 create_input_operand (&ops[2], op2, mode);
4589 create_input_operand (&ops[3], op3, mode);
4590 if (maybe_expand_insn (icode, 4, ops))
4592 if (ops[0].value != target)
4593 convert_move (target, ops[0].value, false);
4594 return target;
4597 delete_insns_since (last);
4598 restore_pending_stack_adjust (&save);
4599 return NULL_RTX;
4602 /* Return nonzero if a conditional move of mode MODE is supported.
4604 This function is for combine so it can tell whether an insn that looks
4605 like a conditional move is actually supported by the hardware. If we
4606 guess wrong we lose a bit on optimization, but that's it. */
4607 /* ??? sparc64 supports conditionally moving integers values based on fp
4608 comparisons, and vice versa. How do we handle them? */
4611 can_conditionally_move_p (enum machine_mode mode)
4613 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4614 return 1;
4616 return 0;
4619 #endif /* HAVE_conditional_move */
4621 /* Emit a conditional addition instruction if the machine supports one for that
4622 condition and machine mode.
4624 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4625 the mode to use should they be constants. If it is VOIDmode, they cannot
4626 both be constants.
4628 OP2 should be stored in TARGET if the comparison is false, otherwise OP2+OP3
4629 should be stored there. MODE is the mode to use should they be constants.
4630 If it is VOIDmode, they cannot both be constants.
4632 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4633 is not supported. */
4636 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4637 enum machine_mode cmode, rtx op2, rtx op3,
4638 enum machine_mode mode, int unsignedp)
4640 rtx tem, comparison, last;
4641 enum insn_code icode;
4643 /* If one operand is constant, make it the second one. Only do this
4644 if the other operand is not constant as well. */
4646 if (swap_commutative_operands_p (op0, op1))
4648 tem = op0;
4649 op0 = op1;
4650 op1 = tem;
4651 code = swap_condition (code);
4654 /* get_condition will prefer to generate LT and GT even if the old
4655 comparison was against zero, so undo that canonicalization here since
4656 comparisons against zero are cheaper. */
4657 if (code == LT && op1 == const1_rtx)
4658 code = LE, op1 = const0_rtx;
4659 else if (code == GT && op1 == constm1_rtx)
4660 code = GE, op1 = const0_rtx;
4662 if (cmode == VOIDmode)
4663 cmode = GET_MODE (op0);
4665 if (mode == VOIDmode)
4666 mode = GET_MODE (op2);
4668 icode = optab_handler (addcc_optab, mode);
4670 if (icode == CODE_FOR_nothing)
4671 return 0;
4673 if (!target)
4674 target = gen_reg_rtx (mode);
4676 code = unsignedp ? unsigned_condition (code) : code;
4677 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4679 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4680 return NULL and let the caller figure out how best to deal with this
4681 situation. */
4682 if (!COMPARISON_P (comparison))
4683 return NULL_RTX;
4685 do_pending_stack_adjust ();
4686 last = get_last_insn ();
4687 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4688 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4689 &comparison, &cmode);
4690 if (comparison)
4692 struct expand_operand ops[4];
4694 create_output_operand (&ops[0], target, mode);
4695 create_fixed_operand (&ops[1], comparison);
4696 create_input_operand (&ops[2], op2, mode);
4697 create_input_operand (&ops[3], op3, mode);
4698 if (maybe_expand_insn (icode, 4, ops))
4700 if (ops[0].value != target)
4701 convert_move (target, ops[0].value, false);
4702 return target;
4705 delete_insns_since (last);
4706 return NULL_RTX;
4709 /* These functions attempt to generate an insn body, rather than
4710 emitting the insn, but if the gen function already emits them, we
4711 make no attempt to turn them back into naked patterns. */
4713 /* Generate and return an insn body to add Y to X. */
4716 gen_add2_insn (rtx x, rtx y)
4718 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4720 gcc_assert (insn_operand_matches (icode, 0, x));
4721 gcc_assert (insn_operand_matches (icode, 1, x));
4722 gcc_assert (insn_operand_matches (icode, 2, y));
4724 return GEN_FCN (icode) (x, x, y);
4727 /* Generate and return an insn body to add r1 and c,
4728 storing the result in r0. */
4731 gen_add3_insn (rtx r0, rtx r1, rtx c)
4733 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4735 if (icode == CODE_FOR_nothing
4736 || !insn_operand_matches (icode, 0, r0)
4737 || !insn_operand_matches (icode, 1, r1)
4738 || !insn_operand_matches (icode, 2, c))
4739 return NULL_RTX;
4741 return GEN_FCN (icode) (r0, r1, c);
4745 have_add2_insn (rtx x, rtx y)
4747 enum insn_code icode;
4749 gcc_assert (GET_MODE (x) != VOIDmode);
4751 icode = optab_handler (add_optab, GET_MODE (x));
4753 if (icode == CODE_FOR_nothing)
4754 return 0;
4756 if (!insn_operand_matches (icode, 0, x)
4757 || !insn_operand_matches (icode, 1, x)
4758 || !insn_operand_matches (icode, 2, y))
4759 return 0;
4761 return 1;
4764 /* Generate and return an insn body to add Y to X. */
4767 gen_addptr3_insn (rtx x, rtx y, rtx z)
4769 enum insn_code icode = optab_handler (addptr3_optab, GET_MODE (x));
4771 gcc_assert (insn_operand_matches (icode, 0, x));
4772 gcc_assert (insn_operand_matches (icode, 1, y));
4773 gcc_assert (insn_operand_matches (icode, 2, z));
4775 return GEN_FCN (icode) (x, y, z);
4778 /* Return true if the target implements an addptr pattern and X, Y,
4779 and Z are valid for the pattern predicates. */
4782 have_addptr3_insn (rtx x, rtx y, rtx z)
4784 enum insn_code icode;
4786 gcc_assert (GET_MODE (x) != VOIDmode);
4788 icode = optab_handler (addptr3_optab, GET_MODE (x));
4790 if (icode == CODE_FOR_nothing)
4791 return 0;
4793 if (!insn_operand_matches (icode, 0, x)
4794 || !insn_operand_matches (icode, 1, y)
4795 || !insn_operand_matches (icode, 2, z))
4796 return 0;
4798 return 1;
4801 /* Generate and return an insn body to subtract Y from X. */
4804 gen_sub2_insn (rtx x, rtx y)
4806 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4808 gcc_assert (insn_operand_matches (icode, 0, x));
4809 gcc_assert (insn_operand_matches (icode, 1, x));
4810 gcc_assert (insn_operand_matches (icode, 2, y));
4812 return GEN_FCN (icode) (x, x, y);
4815 /* Generate and return an insn body to subtract r1 and c,
4816 storing the result in r0. */
4819 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4821 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4823 if (icode == CODE_FOR_nothing
4824 || !insn_operand_matches (icode, 0, r0)
4825 || !insn_operand_matches (icode, 1, r1)
4826 || !insn_operand_matches (icode, 2, c))
4827 return NULL_RTX;
4829 return GEN_FCN (icode) (r0, r1, c);
4833 have_sub2_insn (rtx x, rtx y)
4835 enum insn_code icode;
4837 gcc_assert (GET_MODE (x) != VOIDmode);
4839 icode = optab_handler (sub_optab, GET_MODE (x));
4841 if (icode == CODE_FOR_nothing)
4842 return 0;
4844 if (!insn_operand_matches (icode, 0, x)
4845 || !insn_operand_matches (icode, 1, x)
4846 || !insn_operand_matches (icode, 2, y))
4847 return 0;
4849 return 1;
4852 /* Generate the body of an instruction to copy Y into X.
4853 It may be a list of insns, if one insn isn't enough. */
4856 gen_move_insn (rtx x, rtx y)
4858 rtx seq;
4860 start_sequence ();
4861 emit_move_insn_1 (x, y);
4862 seq = get_insns ();
4863 end_sequence ();
4864 return seq;
4867 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4868 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4869 no such operation exists, CODE_FOR_nothing will be returned. */
4871 enum insn_code
4872 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
4873 int unsignedp)
4875 convert_optab tab;
4876 #ifdef HAVE_ptr_extend
4877 if (unsignedp < 0)
4878 return CODE_FOR_ptr_extend;
4879 #endif
4881 tab = unsignedp ? zext_optab : sext_optab;
4882 return convert_optab_handler (tab, to_mode, from_mode);
4885 /* Generate the body of an insn to extend Y (with mode MFROM)
4886 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4889 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4890 enum machine_mode mfrom, int unsignedp)
4892 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4893 return GEN_FCN (icode) (x, y);
4896 /* can_fix_p and can_float_p say whether the target machine
4897 can directly convert a given fixed point type to
4898 a given floating point type, or vice versa.
4899 The returned value is the CODE_FOR_... value to use,
4900 or CODE_FOR_nothing if these modes cannot be directly converted.
4902 *TRUNCP_PTR is set to 1 if it is necessary to output
4903 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4905 static enum insn_code
4906 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4907 int unsignedp, int *truncp_ptr)
4909 convert_optab tab;
4910 enum insn_code icode;
4912 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4913 icode = convert_optab_handler (tab, fixmode, fltmode);
4914 if (icode != CODE_FOR_nothing)
4916 *truncp_ptr = 0;
4917 return icode;
4920 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4921 for this to work. We need to rework the fix* and ftrunc* patterns
4922 and documentation. */
4923 tab = unsignedp ? ufix_optab : sfix_optab;
4924 icode = convert_optab_handler (tab, fixmode, fltmode);
4925 if (icode != CODE_FOR_nothing
4926 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4928 *truncp_ptr = 1;
4929 return icode;
4932 *truncp_ptr = 0;
4933 return CODE_FOR_nothing;
4936 enum insn_code
4937 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4938 int unsignedp)
4940 convert_optab tab;
4942 tab = unsignedp ? ufloat_optab : sfloat_optab;
4943 return convert_optab_handler (tab, fltmode, fixmode);
4946 /* Function supportable_convert_operation
4948 Check whether an operation represented by the code CODE is a
4949 convert operation that is supported by the target platform in
4950 vector form (i.e., when operating on arguments of type VECTYPE_IN
4951 producing a result of type VECTYPE_OUT).
4953 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4954 This function checks if these operations are supported
4955 by the target platform either directly (via vector tree-codes), or via
4956 target builtins.
4958 Output:
4959 - CODE1 is code of vector operation to be used when
4960 vectorizing the operation, if available.
4961 - DECL is decl of target builtin functions to be used
4962 when vectorizing the operation, if available. In this case,
4963 CODE1 is CALL_EXPR. */
4965 bool
4966 supportable_convert_operation (enum tree_code code,
4967 tree vectype_out, tree vectype_in,
4968 tree *decl, enum tree_code *code1)
4970 enum machine_mode m1,m2;
4971 int truncp;
4973 m1 = TYPE_MODE (vectype_out);
4974 m2 = TYPE_MODE (vectype_in);
4976 /* First check if we can done conversion directly. */
4977 if ((code == FIX_TRUNC_EXPR
4978 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
4979 != CODE_FOR_nothing)
4980 || (code == FLOAT_EXPR
4981 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
4982 != CODE_FOR_nothing))
4984 *code1 = code;
4985 return true;
4988 /* Now check for builtin. */
4989 if (targetm.vectorize.builtin_conversion
4990 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
4992 *code1 = CALL_EXPR;
4993 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
4994 return true;
4996 return false;
5000 /* Generate code to convert FROM to floating point
5001 and store in TO. FROM must be fixed point and not VOIDmode.
5002 UNSIGNEDP nonzero means regard FROM as unsigned.
5003 Normally this is done by correcting the final value
5004 if it is negative. */
5006 void
5007 expand_float (rtx to, rtx from, int unsignedp)
5009 enum insn_code icode;
5010 rtx target = to;
5011 enum machine_mode fmode, imode;
5012 bool can_do_signed = false;
5014 /* Crash now, because we won't be able to decide which mode to use. */
5015 gcc_assert (GET_MODE (from) != VOIDmode);
5017 /* Look for an insn to do the conversion. Do it in the specified
5018 modes if possible; otherwise convert either input, output or both to
5019 wider mode. If the integer mode is wider than the mode of FROM,
5020 we can do the conversion signed even if the input is unsigned. */
5022 for (fmode = GET_MODE (to); fmode != VOIDmode;
5023 fmode = GET_MODE_WIDER_MODE (fmode))
5024 for (imode = GET_MODE (from); imode != VOIDmode;
5025 imode = GET_MODE_WIDER_MODE (imode))
5027 int doing_unsigned = unsignedp;
5029 if (fmode != GET_MODE (to)
5030 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
5031 continue;
5033 icode = can_float_p (fmode, imode, unsignedp);
5034 if (icode == CODE_FOR_nothing && unsignedp)
5036 enum insn_code scode = can_float_p (fmode, imode, 0);
5037 if (scode != CODE_FOR_nothing)
5038 can_do_signed = true;
5039 if (imode != GET_MODE (from))
5040 icode = scode, doing_unsigned = 0;
5043 if (icode != CODE_FOR_nothing)
5045 if (imode != GET_MODE (from))
5046 from = convert_to_mode (imode, from, unsignedp);
5048 if (fmode != GET_MODE (to))
5049 target = gen_reg_rtx (fmode);
5051 emit_unop_insn (icode, target, from,
5052 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
5054 if (target != to)
5055 convert_move (to, target, 0);
5056 return;
5060 /* Unsigned integer, and no way to convert directly. Convert as signed,
5061 then unconditionally adjust the result. */
5062 if (unsignedp && can_do_signed)
5064 rtx label = gen_label_rtx ();
5065 rtx temp;
5066 REAL_VALUE_TYPE offset;
5068 /* Look for a usable floating mode FMODE wider than the source and at
5069 least as wide as the target. Using FMODE will avoid rounding woes
5070 with unsigned values greater than the signed maximum value. */
5072 for (fmode = GET_MODE (to); fmode != VOIDmode;
5073 fmode = GET_MODE_WIDER_MODE (fmode))
5074 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
5075 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
5076 break;
5078 if (fmode == VOIDmode)
5080 /* There is no such mode. Pretend the target is wide enough. */
5081 fmode = GET_MODE (to);
5083 /* Avoid double-rounding when TO is narrower than FROM. */
5084 if ((significand_size (fmode) + 1)
5085 < GET_MODE_PRECISION (GET_MODE (from)))
5087 rtx temp1;
5088 rtx neglabel = gen_label_rtx ();
5090 /* Don't use TARGET if it isn't a register, is a hard register,
5091 or is the wrong mode. */
5092 if (!REG_P (target)
5093 || REGNO (target) < FIRST_PSEUDO_REGISTER
5094 || GET_MODE (target) != fmode)
5095 target = gen_reg_rtx (fmode);
5097 imode = GET_MODE (from);
5098 do_pending_stack_adjust ();
5100 /* Test whether the sign bit is set. */
5101 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5102 0, neglabel);
5104 /* The sign bit is not set. Convert as signed. */
5105 expand_float (target, from, 0);
5106 emit_jump_insn (gen_jump (label));
5107 emit_barrier ();
5109 /* The sign bit is set.
5110 Convert to a usable (positive signed) value by shifting right
5111 one bit, while remembering if a nonzero bit was shifted
5112 out; i.e., compute (from & 1) | (from >> 1). */
5114 emit_label (neglabel);
5115 temp = expand_binop (imode, and_optab, from, const1_rtx,
5116 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5117 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5118 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5119 OPTAB_LIB_WIDEN);
5120 expand_float (target, temp, 0);
5122 /* Multiply by 2 to undo the shift above. */
5123 temp = expand_binop (fmode, add_optab, target, target,
5124 target, 0, OPTAB_LIB_WIDEN);
5125 if (temp != target)
5126 emit_move_insn (target, temp);
5128 do_pending_stack_adjust ();
5129 emit_label (label);
5130 goto done;
5134 /* If we are about to do some arithmetic to correct for an
5135 unsigned operand, do it in a pseudo-register. */
5137 if (GET_MODE (to) != fmode
5138 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5139 target = gen_reg_rtx (fmode);
5141 /* Convert as signed integer to floating. */
5142 expand_float (target, from, 0);
5144 /* If FROM is negative (and therefore TO is negative),
5145 correct its value by 2**bitwidth. */
5147 do_pending_stack_adjust ();
5148 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5149 0, label);
5152 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5153 temp = expand_binop (fmode, add_optab, target,
5154 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5155 target, 0, OPTAB_LIB_WIDEN);
5156 if (temp != target)
5157 emit_move_insn (target, temp);
5159 do_pending_stack_adjust ();
5160 emit_label (label);
5161 goto done;
5164 /* No hardware instruction available; call a library routine. */
5166 rtx libfunc;
5167 rtx insns;
5168 rtx value;
5169 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5171 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
5172 from = convert_to_mode (SImode, from, unsignedp);
5174 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5175 gcc_assert (libfunc);
5177 start_sequence ();
5179 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5180 GET_MODE (to), 1, from,
5181 GET_MODE (from));
5182 insns = get_insns ();
5183 end_sequence ();
5185 emit_libcall_block (insns, target, value,
5186 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5187 GET_MODE (to), from));
5190 done:
5192 /* Copy result to requested destination
5193 if we have been computing in a temp location. */
5195 if (target != to)
5197 if (GET_MODE (target) == GET_MODE (to))
5198 emit_move_insn (to, target);
5199 else
5200 convert_move (to, target, 0);
5204 /* Generate code to convert FROM to fixed point and store in TO. FROM
5205 must be floating point. */
5207 void
5208 expand_fix (rtx to, rtx from, int unsignedp)
5210 enum insn_code icode;
5211 rtx target = to;
5212 enum machine_mode fmode, imode;
5213 int must_trunc = 0;
5215 /* We first try to find a pair of modes, one real and one integer, at
5216 least as wide as FROM and TO, respectively, in which we can open-code
5217 this conversion. If the integer mode is wider than the mode of TO,
5218 we can do the conversion either signed or unsigned. */
5220 for (fmode = GET_MODE (from); fmode != VOIDmode;
5221 fmode = GET_MODE_WIDER_MODE (fmode))
5222 for (imode = GET_MODE (to); imode != VOIDmode;
5223 imode = GET_MODE_WIDER_MODE (imode))
5225 int doing_unsigned = unsignedp;
5227 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5228 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5229 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5231 if (icode != CODE_FOR_nothing)
5233 rtx last = get_last_insn ();
5234 if (fmode != GET_MODE (from))
5235 from = convert_to_mode (fmode, from, 0);
5237 if (must_trunc)
5239 rtx temp = gen_reg_rtx (GET_MODE (from));
5240 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5241 temp, 0);
5244 if (imode != GET_MODE (to))
5245 target = gen_reg_rtx (imode);
5247 if (maybe_emit_unop_insn (icode, target, from,
5248 doing_unsigned ? UNSIGNED_FIX : FIX))
5250 if (target != to)
5251 convert_move (to, target, unsignedp);
5252 return;
5254 delete_insns_since (last);
5258 /* For an unsigned conversion, there is one more way to do it.
5259 If we have a signed conversion, we generate code that compares
5260 the real value to the largest representable positive number. If if
5261 is smaller, the conversion is done normally. Otherwise, subtract
5262 one plus the highest signed number, convert, and add it back.
5264 We only need to check all real modes, since we know we didn't find
5265 anything with a wider integer mode.
5267 This code used to extend FP value into mode wider than the destination.
5268 This is needed for decimal float modes which cannot accurately
5269 represent one plus the highest signed number of the same size, but
5270 not for binary modes. Consider, for instance conversion from SFmode
5271 into DImode.
5273 The hot path through the code is dealing with inputs smaller than 2^63
5274 and doing just the conversion, so there is no bits to lose.
5276 In the other path we know the value is positive in the range 2^63..2^64-1
5277 inclusive. (as for other input overflow happens and result is undefined)
5278 So we know that the most important bit set in mantissa corresponds to
5279 2^63. The subtraction of 2^63 should not generate any rounding as it
5280 simply clears out that bit. The rest is trivial. */
5282 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5283 for (fmode = GET_MODE (from); fmode != VOIDmode;
5284 fmode = GET_MODE_WIDER_MODE (fmode))
5285 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5286 && (!DECIMAL_FLOAT_MODE_P (fmode)
5287 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5289 int bitsize;
5290 REAL_VALUE_TYPE offset;
5291 rtx limit, lab1, lab2, insn;
5293 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5294 real_2expN (&offset, bitsize - 1, fmode);
5295 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5296 lab1 = gen_label_rtx ();
5297 lab2 = gen_label_rtx ();
5299 if (fmode != GET_MODE (from))
5300 from = convert_to_mode (fmode, from, 0);
5302 /* See if we need to do the subtraction. */
5303 do_pending_stack_adjust ();
5304 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5305 0, lab1);
5307 /* If not, do the signed "fix" and branch around fixup code. */
5308 expand_fix (to, from, 0);
5309 emit_jump_insn (gen_jump (lab2));
5310 emit_barrier ();
5312 /* Otherwise, subtract 2**(N-1), convert to signed number,
5313 then add 2**(N-1). Do the addition using XOR since this
5314 will often generate better code. */
5315 emit_label (lab1);
5316 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5317 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5318 expand_fix (to, target, 0);
5319 target = expand_binop (GET_MODE (to), xor_optab, to,
5320 gen_int_mode
5321 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5322 GET_MODE (to)),
5323 to, 1, OPTAB_LIB_WIDEN);
5325 if (target != to)
5326 emit_move_insn (to, target);
5328 emit_label (lab2);
5330 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5332 /* Make a place for a REG_NOTE and add it. */
5333 insn = emit_move_insn (to, to);
5334 set_dst_reg_note (insn, REG_EQUAL,
5335 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5336 copy_rtx (from)),
5337 to);
5340 return;
5343 /* We can't do it with an insn, so use a library call. But first ensure
5344 that the mode of TO is at least as wide as SImode, since those are the
5345 only library calls we know about. */
5347 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
5349 target = gen_reg_rtx (SImode);
5351 expand_fix (target, from, unsignedp);
5353 else
5355 rtx insns;
5356 rtx value;
5357 rtx libfunc;
5359 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5360 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5361 gcc_assert (libfunc);
5363 start_sequence ();
5365 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5366 GET_MODE (to), 1, from,
5367 GET_MODE (from));
5368 insns = get_insns ();
5369 end_sequence ();
5371 emit_libcall_block (insns, target, value,
5372 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5373 GET_MODE (to), from));
5376 if (target != to)
5378 if (GET_MODE (to) == GET_MODE (target))
5379 emit_move_insn (to, target);
5380 else
5381 convert_move (to, target, 0);
5385 /* Generate code to convert FROM or TO a fixed-point.
5386 If UINTP is true, either TO or FROM is an unsigned integer.
5387 If SATP is true, we need to saturate the result. */
5389 void
5390 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5392 enum machine_mode to_mode = GET_MODE (to);
5393 enum machine_mode from_mode = GET_MODE (from);
5394 convert_optab tab;
5395 enum rtx_code this_code;
5396 enum insn_code code;
5397 rtx insns, value;
5398 rtx libfunc;
5400 if (to_mode == from_mode)
5402 emit_move_insn (to, from);
5403 return;
5406 if (uintp)
5408 tab = satp ? satfractuns_optab : fractuns_optab;
5409 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5411 else
5413 tab = satp ? satfract_optab : fract_optab;
5414 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5416 code = convert_optab_handler (tab, to_mode, from_mode);
5417 if (code != CODE_FOR_nothing)
5419 emit_unop_insn (code, to, from, this_code);
5420 return;
5423 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5424 gcc_assert (libfunc);
5426 start_sequence ();
5427 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5428 1, from, from_mode);
5429 insns = get_insns ();
5430 end_sequence ();
5432 emit_libcall_block (insns, to, value,
5433 gen_rtx_fmt_e (optab_to_code (tab), to_mode, from));
5436 /* Generate code to convert FROM to fixed point and store in TO. FROM
5437 must be floating point, TO must be signed. Use the conversion optab
5438 TAB to do the conversion. */
5440 bool
5441 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5443 enum insn_code icode;
5444 rtx target = to;
5445 enum machine_mode fmode, imode;
5447 /* We first try to find a pair of modes, one real and one integer, at
5448 least as wide as FROM and TO, respectively, in which we can open-code
5449 this conversion. If the integer mode is wider than the mode of TO,
5450 we can do the conversion either signed or unsigned. */
5452 for (fmode = GET_MODE (from); fmode != VOIDmode;
5453 fmode = GET_MODE_WIDER_MODE (fmode))
5454 for (imode = GET_MODE (to); imode != VOIDmode;
5455 imode = GET_MODE_WIDER_MODE (imode))
5457 icode = convert_optab_handler (tab, imode, fmode);
5458 if (icode != CODE_FOR_nothing)
5460 rtx last = get_last_insn ();
5461 if (fmode != GET_MODE (from))
5462 from = convert_to_mode (fmode, from, 0);
5464 if (imode != GET_MODE (to))
5465 target = gen_reg_rtx (imode);
5467 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5469 delete_insns_since (last);
5470 continue;
5472 if (target != to)
5473 convert_move (to, target, 0);
5474 return true;
5478 return false;
5481 /* Report whether we have an instruction to perform the operation
5482 specified by CODE on operands of mode MODE. */
5484 have_insn_for (enum rtx_code code, enum machine_mode mode)
5486 return (code_to_optab (code)
5487 && (optab_handler (code_to_optab (code), mode)
5488 != CODE_FOR_nothing));
5491 /* Initialize the libfunc fields of an entire group of entries in some
5492 optab. Each entry is set equal to a string consisting of a leading
5493 pair of underscores followed by a generic operation name followed by
5494 a mode name (downshifted to lowercase) followed by a single character
5495 representing the number of operands for the given operation (which is
5496 usually one of the characters '2', '3', or '4').
5498 OPTABLE is the table in which libfunc fields are to be initialized.
5499 OPNAME is the generic (string) name of the operation.
5500 SUFFIX is the character which specifies the number of operands for
5501 the given generic operation.
5502 MODE is the mode to generate for.
5505 static void
5506 gen_libfunc (optab optable, const char *opname, int suffix,
5507 enum machine_mode mode)
5509 unsigned opname_len = strlen (opname);
5510 const char *mname = GET_MODE_NAME (mode);
5511 unsigned mname_len = strlen (mname);
5512 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5513 int len = prefix_len + opname_len + mname_len + 1 + 1;
5514 char *libfunc_name = XALLOCAVEC (char, len);
5515 char *p;
5516 const char *q;
5518 p = libfunc_name;
5519 *p++ = '_';
5520 *p++ = '_';
5521 if (targetm.libfunc_gnu_prefix)
5523 *p++ = 'g';
5524 *p++ = 'n';
5525 *p++ = 'u';
5526 *p++ = '_';
5528 for (q = opname; *q; )
5529 *p++ = *q++;
5530 for (q = mname; *q; q++)
5531 *p++ = TOLOWER (*q);
5532 *p++ = suffix;
5533 *p = '\0';
5535 set_optab_libfunc (optable, mode,
5536 ggc_alloc_string (libfunc_name, p - libfunc_name));
5539 /* Like gen_libfunc, but verify that integer operation is involved. */
5541 void
5542 gen_int_libfunc (optab optable, const char *opname, char suffix,
5543 enum machine_mode mode)
5545 int maxsize = 2 * BITS_PER_WORD;
5547 if (GET_MODE_CLASS (mode) != MODE_INT)
5548 return;
5549 if (maxsize < LONG_LONG_TYPE_SIZE)
5550 maxsize = LONG_LONG_TYPE_SIZE;
5551 if (GET_MODE_CLASS (mode) != MODE_INT
5552 || GET_MODE_BITSIZE (mode) < BITS_PER_WORD
5553 || GET_MODE_BITSIZE (mode) > maxsize)
5554 return;
5555 gen_libfunc (optable, opname, suffix, mode);
5558 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5560 void
5561 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5562 enum machine_mode mode)
5564 char *dec_opname;
5566 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5567 gen_libfunc (optable, opname, suffix, mode);
5568 if (DECIMAL_FLOAT_MODE_P (mode))
5570 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5571 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5572 depending on the low level floating format used. */
5573 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5574 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5575 gen_libfunc (optable, dec_opname, suffix, mode);
5579 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5581 void
5582 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5583 enum machine_mode mode)
5585 if (!ALL_FIXED_POINT_MODE_P (mode))
5586 return;
5587 gen_libfunc (optable, opname, suffix, mode);
5590 /* Like gen_libfunc, but verify that signed fixed-point operation is
5591 involved. */
5593 void
5594 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5595 enum machine_mode mode)
5597 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5598 return;
5599 gen_libfunc (optable, opname, suffix, mode);
5602 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5603 involved. */
5605 void
5606 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5607 enum machine_mode mode)
5609 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5610 return;
5611 gen_libfunc (optable, opname, suffix, mode);
5614 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5616 void
5617 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5618 enum machine_mode mode)
5620 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5621 gen_fp_libfunc (optable, name, suffix, mode);
5622 if (INTEGRAL_MODE_P (mode))
5623 gen_int_libfunc (optable, name, suffix, mode);
5626 /* Like gen_libfunc, but verify that FP or INT operation is involved
5627 and add 'v' suffix for integer operation. */
5629 void
5630 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5631 enum machine_mode mode)
5633 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5634 gen_fp_libfunc (optable, name, suffix, mode);
5635 if (GET_MODE_CLASS (mode) == MODE_INT)
5637 int len = strlen (name);
5638 char *v_name = XALLOCAVEC (char, len + 2);
5639 strcpy (v_name, name);
5640 v_name[len] = 'v';
5641 v_name[len + 1] = 0;
5642 gen_int_libfunc (optable, v_name, suffix, mode);
5646 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5647 involved. */
5649 void
5650 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5651 enum machine_mode mode)
5653 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5654 gen_fp_libfunc (optable, name, suffix, mode);
5655 if (INTEGRAL_MODE_P (mode))
5656 gen_int_libfunc (optable, name, suffix, mode);
5657 if (ALL_FIXED_POINT_MODE_P (mode))
5658 gen_fixed_libfunc (optable, name, suffix, mode);
5661 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5662 involved. */
5664 void
5665 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5666 enum machine_mode mode)
5668 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5669 gen_fp_libfunc (optable, name, suffix, mode);
5670 if (INTEGRAL_MODE_P (mode))
5671 gen_int_libfunc (optable, name, suffix, mode);
5672 if (SIGNED_FIXED_POINT_MODE_P (mode))
5673 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5676 /* Like gen_libfunc, but verify that INT or FIXED operation is
5677 involved. */
5679 void
5680 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5681 enum machine_mode mode)
5683 if (INTEGRAL_MODE_P (mode))
5684 gen_int_libfunc (optable, name, suffix, mode);
5685 if (ALL_FIXED_POINT_MODE_P (mode))
5686 gen_fixed_libfunc (optable, name, suffix, mode);
5689 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5690 involved. */
5692 void
5693 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5694 enum machine_mode mode)
5696 if (INTEGRAL_MODE_P (mode))
5697 gen_int_libfunc (optable, name, suffix, mode);
5698 if (SIGNED_FIXED_POINT_MODE_P (mode))
5699 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5702 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5703 involved. */
5705 void
5706 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5707 enum machine_mode mode)
5709 if (INTEGRAL_MODE_P (mode))
5710 gen_int_libfunc (optable, name, suffix, mode);
5711 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5712 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5715 /* Initialize the libfunc fields of an entire group of entries of an
5716 inter-mode-class conversion optab. The string formation rules are
5717 similar to the ones for init_libfuncs, above, but instead of having
5718 a mode name and an operand count these functions have two mode names
5719 and no operand count. */
5721 void
5722 gen_interclass_conv_libfunc (convert_optab tab,
5723 const char *opname,
5724 enum machine_mode tmode,
5725 enum machine_mode fmode)
5727 size_t opname_len = strlen (opname);
5728 size_t mname_len = 0;
5730 const char *fname, *tname;
5731 const char *q;
5732 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5733 char *libfunc_name, *suffix;
5734 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5735 char *p;
5737 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5738 depends on which underlying decimal floating point format is used. */
5739 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5741 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5743 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5744 nondec_name[0] = '_';
5745 nondec_name[1] = '_';
5746 if (targetm.libfunc_gnu_prefix)
5748 nondec_name[2] = 'g';
5749 nondec_name[3] = 'n';
5750 nondec_name[4] = 'u';
5751 nondec_name[5] = '_';
5754 memcpy (&nondec_name[prefix_len], opname, opname_len);
5755 nondec_suffix = nondec_name + opname_len + prefix_len;
5757 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5758 dec_name[0] = '_';
5759 dec_name[1] = '_';
5760 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5761 memcpy (&dec_name[2+dec_len], opname, opname_len);
5762 dec_suffix = dec_name + dec_len + opname_len + 2;
5764 fname = GET_MODE_NAME (fmode);
5765 tname = GET_MODE_NAME (tmode);
5767 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5769 libfunc_name = dec_name;
5770 suffix = dec_suffix;
5772 else
5774 libfunc_name = nondec_name;
5775 suffix = nondec_suffix;
5778 p = suffix;
5779 for (q = fname; *q; p++, q++)
5780 *p = TOLOWER (*q);
5781 for (q = tname; *q; p++, q++)
5782 *p = TOLOWER (*q);
5784 *p = '\0';
5786 set_conv_libfunc (tab, tmode, fmode,
5787 ggc_alloc_string (libfunc_name, p - libfunc_name));
5790 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5791 int->fp conversion. */
5793 void
5794 gen_int_to_fp_conv_libfunc (convert_optab tab,
5795 const char *opname,
5796 enum machine_mode tmode,
5797 enum machine_mode fmode)
5799 if (GET_MODE_CLASS (fmode) != MODE_INT)
5800 return;
5801 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5802 return;
5803 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5806 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5807 naming scheme. */
5809 void
5810 gen_ufloat_conv_libfunc (convert_optab tab,
5811 const char *opname ATTRIBUTE_UNUSED,
5812 enum machine_mode tmode,
5813 enum machine_mode fmode)
5815 if (DECIMAL_FLOAT_MODE_P (tmode))
5816 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5817 else
5818 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5821 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5822 fp->int conversion. */
5824 void
5825 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5826 const char *opname,
5827 enum machine_mode tmode,
5828 enum machine_mode fmode)
5830 if (GET_MODE_CLASS (fmode) != MODE_INT)
5831 return;
5832 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5833 return;
5834 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5837 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5838 fp->int conversion with no decimal floating point involved. */
5840 void
5841 gen_fp_to_int_conv_libfunc (convert_optab tab,
5842 const char *opname,
5843 enum machine_mode tmode,
5844 enum machine_mode fmode)
5846 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5847 return;
5848 if (GET_MODE_CLASS (tmode) != MODE_INT)
5849 return;
5850 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5853 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5854 The string formation rules are
5855 similar to the ones for init_libfunc, above. */
5857 void
5858 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5859 enum machine_mode tmode, enum machine_mode fmode)
5861 size_t opname_len = strlen (opname);
5862 size_t mname_len = 0;
5864 const char *fname, *tname;
5865 const char *q;
5866 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5867 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5868 char *libfunc_name, *suffix;
5869 char *p;
5871 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5872 depends on which underlying decimal floating point format is used. */
5873 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5875 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5877 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5878 nondec_name[0] = '_';
5879 nondec_name[1] = '_';
5880 if (targetm.libfunc_gnu_prefix)
5882 nondec_name[2] = 'g';
5883 nondec_name[3] = 'n';
5884 nondec_name[4] = 'u';
5885 nondec_name[5] = '_';
5887 memcpy (&nondec_name[prefix_len], opname, opname_len);
5888 nondec_suffix = nondec_name + opname_len + prefix_len;
5890 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5891 dec_name[0] = '_';
5892 dec_name[1] = '_';
5893 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5894 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5895 dec_suffix = dec_name + dec_len + opname_len + 2;
5897 fname = GET_MODE_NAME (fmode);
5898 tname = GET_MODE_NAME (tmode);
5900 if (DECIMAL_FLOAT_MODE_P (fmode) || DECIMAL_FLOAT_MODE_P (tmode))
5902 libfunc_name = dec_name;
5903 suffix = dec_suffix;
5905 else
5907 libfunc_name = nondec_name;
5908 suffix = nondec_suffix;
5911 p = suffix;
5912 for (q = fname; *q; p++, q++)
5913 *p = TOLOWER (*q);
5914 for (q = tname; *q; p++, q++)
5915 *p = TOLOWER (*q);
5917 *p++ = '2';
5918 *p = '\0';
5920 set_conv_libfunc (tab, tmode, fmode,
5921 ggc_alloc_string (libfunc_name, p - libfunc_name));
5924 /* Pick proper libcall for trunc_optab. We need to chose if we do
5925 truncation or extension and interclass or intraclass. */
5927 void
5928 gen_trunc_conv_libfunc (convert_optab tab,
5929 const char *opname,
5930 enum machine_mode tmode,
5931 enum machine_mode fmode)
5933 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5934 return;
5935 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5936 return;
5937 if (tmode == fmode)
5938 return;
5940 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5941 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5942 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5944 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5945 return;
5947 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5948 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5949 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5950 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5953 /* Pick proper libcall for extend_optab. We need to chose if we do
5954 truncation or extension and interclass or intraclass. */
5956 void
5957 gen_extend_conv_libfunc (convert_optab tab,
5958 const char *opname ATTRIBUTE_UNUSED,
5959 enum machine_mode tmode,
5960 enum machine_mode fmode)
5962 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5963 return;
5964 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5965 return;
5966 if (tmode == fmode)
5967 return;
5969 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5970 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5971 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5973 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5974 return;
5976 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5977 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5978 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5979 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5982 /* Pick proper libcall for fract_optab. We need to chose if we do
5983 interclass or intraclass. */
5985 void
5986 gen_fract_conv_libfunc (convert_optab tab,
5987 const char *opname,
5988 enum machine_mode tmode,
5989 enum machine_mode fmode)
5991 if (tmode == fmode)
5992 return;
5993 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5994 return;
5996 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5997 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5998 else
5999 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6002 /* Pick proper libcall for fractuns_optab. */
6004 void
6005 gen_fractuns_conv_libfunc (convert_optab tab,
6006 const char *opname,
6007 enum machine_mode tmode,
6008 enum machine_mode fmode)
6010 if (tmode == fmode)
6011 return;
6012 /* One mode must be a fixed-point mode, and the other must be an integer
6013 mode. */
6014 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
6015 || (ALL_FIXED_POINT_MODE_P (fmode)
6016 && GET_MODE_CLASS (tmode) == MODE_INT)))
6017 return;
6019 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6022 /* Pick proper libcall for satfract_optab. We need to chose if we do
6023 interclass or intraclass. */
6025 void
6026 gen_satfract_conv_libfunc (convert_optab tab,
6027 const char *opname,
6028 enum machine_mode tmode,
6029 enum machine_mode fmode)
6031 if (tmode == fmode)
6032 return;
6033 /* TMODE must be a fixed-point mode. */
6034 if (!ALL_FIXED_POINT_MODE_P (tmode))
6035 return;
6037 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
6038 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
6039 else
6040 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6043 /* Pick proper libcall for satfractuns_optab. */
6045 void
6046 gen_satfractuns_conv_libfunc (convert_optab tab,
6047 const char *opname,
6048 enum machine_mode tmode,
6049 enum machine_mode fmode)
6051 if (tmode == fmode)
6052 return;
6053 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
6054 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
6055 return;
6057 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6060 /* A table of previously-created libfuncs, hashed by name. */
6061 static GTY ((param_is (union tree_node))) htab_t libfunc_decls;
6063 /* Hashtable callbacks for libfunc_decls. */
6065 static hashval_t
6066 libfunc_decl_hash (const void *entry)
6068 return IDENTIFIER_HASH_VALUE (DECL_NAME ((const_tree) entry));
6071 static int
6072 libfunc_decl_eq (const void *entry1, const void *entry2)
6074 return DECL_NAME ((const_tree) entry1) == (const_tree) entry2;
6077 /* Build a decl for a libfunc named NAME. */
6079 tree
6080 build_libfunc_function (const char *name)
6082 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6083 get_identifier (name),
6084 build_function_type (integer_type_node, NULL_TREE));
6085 /* ??? We don't have any type information except for this is
6086 a function. Pretend this is "int foo()". */
6087 DECL_ARTIFICIAL (decl) = 1;
6088 DECL_EXTERNAL (decl) = 1;
6089 TREE_PUBLIC (decl) = 1;
6090 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6092 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6093 are the flags assigned by targetm.encode_section_info. */
6094 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6096 return decl;
6100 init_one_libfunc (const char *name)
6102 tree id, decl;
6103 void **slot;
6104 hashval_t hash;
6106 if (libfunc_decls == NULL)
6107 libfunc_decls = htab_create_ggc (37, libfunc_decl_hash,
6108 libfunc_decl_eq, NULL);
6110 /* See if we have already created a libfunc decl for this function. */
6111 id = get_identifier (name);
6112 hash = IDENTIFIER_HASH_VALUE (id);
6113 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, INSERT);
6114 decl = (tree) *slot;
6115 if (decl == NULL)
6117 /* Create a new decl, so that it can be passed to
6118 targetm.encode_section_info. */
6119 decl = build_libfunc_function (name);
6120 *slot = decl;
6122 return XEXP (DECL_RTL (decl), 0);
6125 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6128 set_user_assembler_libfunc (const char *name, const char *asmspec)
6130 tree id, decl;
6131 void **slot;
6132 hashval_t hash;
6134 id = get_identifier (name);
6135 hash = IDENTIFIER_HASH_VALUE (id);
6136 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, NO_INSERT);
6137 gcc_assert (slot);
6138 decl = (tree) *slot;
6139 set_user_assembler_name (decl, asmspec);
6140 return XEXP (DECL_RTL (decl), 0);
6143 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6144 MODE to NAME, which should be either 0 or a string constant. */
6145 void
6146 set_optab_libfunc (optab op, enum machine_mode mode, const char *name)
6148 rtx val;
6149 struct libfunc_entry e;
6150 struct libfunc_entry **slot;
6152 e.op = op;
6153 e.mode1 = mode;
6154 e.mode2 = VOIDmode;
6156 if (name)
6157 val = init_one_libfunc (name);
6158 else
6159 val = 0;
6160 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6161 if (*slot == NULL)
6162 *slot = ggc_alloc_libfunc_entry ();
6163 (*slot)->op = op;
6164 (*slot)->mode1 = mode;
6165 (*slot)->mode2 = VOIDmode;
6166 (*slot)->libfunc = val;
6169 /* Call this to reset the function entry for one conversion optab
6170 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6171 either 0 or a string constant. */
6172 void
6173 set_conv_libfunc (convert_optab optab, enum machine_mode tmode,
6174 enum machine_mode fmode, const char *name)
6176 rtx val;
6177 struct libfunc_entry e;
6178 struct libfunc_entry **slot;
6180 e.op = optab;
6181 e.mode1 = tmode;
6182 e.mode2 = fmode;
6184 if (name)
6185 val = init_one_libfunc (name);
6186 else
6187 val = 0;
6188 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6189 if (*slot == NULL)
6190 *slot = ggc_alloc_libfunc_entry ();
6191 (*slot)->op = optab;
6192 (*slot)->mode1 = tmode;
6193 (*slot)->mode2 = fmode;
6194 (*slot)->libfunc = val;
6197 /* Call this to initialize the contents of the optabs
6198 appropriately for the current target machine. */
6200 void
6201 init_optabs (void)
6203 if (libfunc_hash)
6204 htab_empty (libfunc_hash);
6205 else
6206 libfunc_hash = htab_create_ggc (10, hash_libfunc, eq_libfunc, NULL);
6208 /* Fill in the optabs with the insns we support. */
6209 init_all_optabs (this_fn_optabs);
6211 /* The ffs function operates on `int'. Fall back on it if we do not
6212 have a libgcc2 function for that width. */
6213 if (INT_TYPE_SIZE < BITS_PER_WORD)
6214 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6215 "ffs");
6217 /* Explicitly initialize the bswap libfuncs since we need them to be
6218 valid for things other than word_mode. */
6219 if (targetm.libfunc_gnu_prefix)
6221 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6222 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6224 else
6226 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6227 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6230 /* Use cabs for double complex abs, since systems generally have cabs.
6231 Don't define any libcall for float complex, so that cabs will be used. */
6232 if (complex_double_type_node)
6233 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node),
6234 "cabs");
6236 abort_libfunc = init_one_libfunc ("abort");
6237 memcpy_libfunc = init_one_libfunc ("memcpy");
6238 memmove_libfunc = init_one_libfunc ("memmove");
6239 memcmp_libfunc = init_one_libfunc ("memcmp");
6240 memset_libfunc = init_one_libfunc ("memset");
6241 setbits_libfunc = init_one_libfunc ("__setbits");
6243 #ifndef DONT_USE_BUILTIN_SETJMP
6244 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6245 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6246 #else
6247 setjmp_libfunc = init_one_libfunc ("setjmp");
6248 longjmp_libfunc = init_one_libfunc ("longjmp");
6249 #endif
6250 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6251 unwind_sjlj_unregister_libfunc
6252 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6254 /* For function entry/exit instrumentation. */
6255 profile_function_entry_libfunc
6256 = init_one_libfunc ("__cyg_profile_func_enter");
6257 profile_function_exit_libfunc
6258 = init_one_libfunc ("__cyg_profile_func_exit");
6260 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6262 /* Allow the target to add more libcalls or rename some, etc. */
6263 targetm.init_libfuncs ();
6266 /* Use the current target and options to initialize
6267 TREE_OPTIMIZATION_OPTABS (OPTNODE). */
6269 void
6270 init_tree_optimization_optabs (tree optnode)
6272 /* Quick exit if we have already computed optabs for this target. */
6273 if (TREE_OPTIMIZATION_BASE_OPTABS (optnode) == this_target_optabs)
6274 return;
6276 /* Forget any previous information and set up for the current target. */
6277 TREE_OPTIMIZATION_BASE_OPTABS (optnode) = this_target_optabs;
6278 struct target_optabs *tmp_optabs = (struct target_optabs *)
6279 TREE_OPTIMIZATION_OPTABS (optnode);
6280 if (tmp_optabs)
6281 memset (tmp_optabs, 0, sizeof (struct target_optabs));
6282 else
6283 tmp_optabs = (struct target_optabs *)
6284 ggc_alloc_atomic (sizeof (struct target_optabs));
6286 /* Generate a new set of optabs into tmp_optabs. */
6287 init_all_optabs (tmp_optabs);
6289 /* If the optabs changed, record it. */
6290 if (memcmp (tmp_optabs, this_target_optabs, sizeof (struct target_optabs)))
6291 TREE_OPTIMIZATION_OPTABS (optnode) = tmp_optabs;
6292 else
6294 TREE_OPTIMIZATION_OPTABS (optnode) = NULL;
6295 ggc_free (tmp_optabs);
6299 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6300 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6302 static void
6303 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6305 enum machine_mode mode;
6306 char buf[64];
6307 size_t len = strlen (base);
6308 int i;
6310 gcc_assert (max <= 8);
6311 gcc_assert (len + 3 < sizeof (buf));
6313 memcpy (buf, base, len);
6314 buf[len] = '_';
6315 buf[len + 1] = '0';
6316 buf[len + 2] = '\0';
6318 mode = QImode;
6319 for (i = 1; i <= max; i *= 2)
6321 buf[len + 1] = '0' + i;
6322 set_optab_libfunc (tab, mode, buf);
6323 mode = GET_MODE_2XWIDER_MODE (mode);
6327 void
6328 init_sync_libfuncs (int max)
6330 if (!flag_sync_libcalls)
6331 return;
6333 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6334 "__sync_val_compare_and_swap", max);
6335 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6336 "__sync_lock_test_and_set", max);
6338 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6339 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6340 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6341 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6342 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6343 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6345 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6346 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6347 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6348 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6349 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6350 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6353 /* Print information about the current contents of the optabs on
6354 STDERR. */
6356 DEBUG_FUNCTION void
6357 debug_optab_libfuncs (void)
6359 int i, j, k;
6361 /* Dump the arithmetic optabs. */
6362 for (i = FIRST_NORM_OPTAB; i <= LAST_NORMLIB_OPTAB; ++i)
6363 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6365 rtx l = optab_libfunc ((optab) i, (enum machine_mode) j);
6366 if (l)
6368 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6369 fprintf (stderr, "%s\t%s:\t%s\n",
6370 GET_RTX_NAME (optab_to_code ((optab) i)),
6371 GET_MODE_NAME (j),
6372 XSTR (l, 0));
6376 /* Dump the conversion optabs. */
6377 for (i = FIRST_CONV_OPTAB; i <= LAST_CONVLIB_OPTAB; ++i)
6378 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6379 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6381 rtx l = convert_optab_libfunc ((optab) i, (enum machine_mode) j,
6382 (enum machine_mode) k);
6383 if (l)
6385 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6386 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6387 GET_RTX_NAME (optab_to_code ((optab) i)),
6388 GET_MODE_NAME (j),
6389 GET_MODE_NAME (k),
6390 XSTR (l, 0));
6396 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6397 CODE. Return 0 on failure. */
6400 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6402 enum machine_mode mode = GET_MODE (op1);
6403 enum insn_code icode;
6404 rtx insn;
6405 rtx trap_rtx;
6407 if (mode == VOIDmode)
6408 return 0;
6410 icode = optab_handler (ctrap_optab, mode);
6411 if (icode == CODE_FOR_nothing)
6412 return 0;
6414 /* Some targets only accept a zero trap code. */
6415 if (!insn_operand_matches (icode, 3, tcode))
6416 return 0;
6418 do_pending_stack_adjust ();
6419 start_sequence ();
6420 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6421 &trap_rtx, &mode);
6422 if (!trap_rtx)
6423 insn = NULL_RTX;
6424 else
6425 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6426 tcode);
6428 /* If that failed, then give up. */
6429 if (insn == 0)
6431 end_sequence ();
6432 return 0;
6435 emit_insn (insn);
6436 insn = get_insns ();
6437 end_sequence ();
6438 return insn;
6441 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6442 or unsigned operation code. */
6444 static enum rtx_code
6445 get_rtx_code (enum tree_code tcode, bool unsignedp)
6447 enum rtx_code code;
6448 switch (tcode)
6450 case EQ_EXPR:
6451 code = EQ;
6452 break;
6453 case NE_EXPR:
6454 code = NE;
6455 break;
6456 case LT_EXPR:
6457 code = unsignedp ? LTU : LT;
6458 break;
6459 case LE_EXPR:
6460 code = unsignedp ? LEU : LE;
6461 break;
6462 case GT_EXPR:
6463 code = unsignedp ? GTU : GT;
6464 break;
6465 case GE_EXPR:
6466 code = unsignedp ? GEU : GE;
6467 break;
6469 case UNORDERED_EXPR:
6470 code = UNORDERED;
6471 break;
6472 case ORDERED_EXPR:
6473 code = ORDERED;
6474 break;
6475 case UNLT_EXPR:
6476 code = UNLT;
6477 break;
6478 case UNLE_EXPR:
6479 code = UNLE;
6480 break;
6481 case UNGT_EXPR:
6482 code = UNGT;
6483 break;
6484 case UNGE_EXPR:
6485 code = UNGE;
6486 break;
6487 case UNEQ_EXPR:
6488 code = UNEQ;
6489 break;
6490 case LTGT_EXPR:
6491 code = LTGT;
6492 break;
6494 default:
6495 gcc_unreachable ();
6497 return code;
6500 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6501 unsigned operators. Do not generate compare instruction. */
6503 static rtx
6504 vector_compare_rtx (enum tree_code tcode, tree t_op0, tree t_op1,
6505 bool unsignedp, enum insn_code icode)
6507 struct expand_operand ops[2];
6508 rtx rtx_op0, rtx_op1;
6509 enum rtx_code rcode = get_rtx_code (tcode, unsignedp);
6511 gcc_assert (TREE_CODE_CLASS (tcode) == tcc_comparison);
6513 /* Expand operands. */
6514 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6515 EXPAND_STACK_PARM);
6516 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6517 EXPAND_STACK_PARM);
6519 create_input_operand (&ops[0], rtx_op0, GET_MODE (rtx_op0));
6520 create_input_operand (&ops[1], rtx_op1, GET_MODE (rtx_op1));
6521 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6522 gcc_unreachable ();
6523 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6526 /* Return true if VEC_PERM_EXPR can be expanded using SIMD extensions
6527 of the CPU. SEL may be NULL, which stands for an unknown constant. */
6529 bool
6530 can_vec_perm_p (enum machine_mode mode, bool variable,
6531 const unsigned char *sel)
6533 enum machine_mode qimode;
6535 /* If the target doesn't implement a vector mode for the vector type,
6536 then no operations are supported. */
6537 if (!VECTOR_MODE_P (mode))
6538 return false;
6540 if (!variable)
6542 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6543 && (sel == NULL
6544 || targetm.vectorize.vec_perm_const_ok == NULL
6545 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6546 return true;
6549 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6550 return true;
6552 /* We allow fallback to a QI vector mode, and adjust the mask. */
6553 if (GET_MODE_INNER (mode) == QImode)
6554 return false;
6555 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6556 if (!VECTOR_MODE_P (qimode))
6557 return false;
6559 /* ??? For completeness, we ought to check the QImode version of
6560 vec_perm_const_optab. But all users of this implicit lowering
6561 feature implement the variable vec_perm_optab. */
6562 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6563 return false;
6565 /* In order to support the lowering of variable permutations,
6566 we need to support shifts and adds. */
6567 if (variable)
6569 if (GET_MODE_UNIT_SIZE (mode) > 2
6570 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6571 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6572 return false;
6573 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6574 return false;
6577 return true;
6580 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
6582 static rtx
6583 expand_vec_perm_1 (enum insn_code icode, rtx target,
6584 rtx v0, rtx v1, rtx sel)
6586 enum machine_mode tmode = GET_MODE (target);
6587 enum machine_mode smode = GET_MODE (sel);
6588 struct expand_operand ops[4];
6590 create_output_operand (&ops[0], target, tmode);
6591 create_input_operand (&ops[3], sel, smode);
6593 /* Make an effort to preserve v0 == v1. The target expander is able to
6594 rely on this to determine if we're permuting a single input operand. */
6595 if (rtx_equal_p (v0, v1))
6597 if (!insn_operand_matches (icode, 1, v0))
6598 v0 = force_reg (tmode, v0);
6599 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6600 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6602 create_fixed_operand (&ops[1], v0);
6603 create_fixed_operand (&ops[2], v0);
6605 else
6607 create_input_operand (&ops[1], v0, tmode);
6608 create_input_operand (&ops[2], v1, tmode);
6611 if (maybe_expand_insn (icode, 4, ops))
6612 return ops[0].value;
6613 return NULL_RTX;
6616 /* Generate instructions for vec_perm optab given its mode
6617 and three operands. */
6620 expand_vec_perm (enum machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6622 enum insn_code icode;
6623 enum machine_mode qimode;
6624 unsigned int i, w, e, u;
6625 rtx tmp, sel_qi = NULL;
6626 rtvec vec;
6628 if (!target || GET_MODE (target) != mode)
6629 target = gen_reg_rtx (mode);
6631 w = GET_MODE_SIZE (mode);
6632 e = GET_MODE_NUNITS (mode);
6633 u = GET_MODE_UNIT_SIZE (mode);
6635 /* Set QIMODE to a different vector mode with byte elements.
6636 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6637 qimode = VOIDmode;
6638 if (GET_MODE_INNER (mode) != QImode)
6640 qimode = mode_for_vector (QImode, w);
6641 if (!VECTOR_MODE_P (qimode))
6642 qimode = VOIDmode;
6645 /* If the input is a constant, expand it specially. */
6646 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6647 if (GET_CODE (sel) == CONST_VECTOR)
6649 icode = direct_optab_handler (vec_perm_const_optab, mode);
6650 if (icode != CODE_FOR_nothing)
6652 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6653 if (tmp)
6654 return tmp;
6657 /* Fall back to a constant byte-based permutation. */
6658 if (qimode != VOIDmode)
6660 vec = rtvec_alloc (w);
6661 for (i = 0; i < e; ++i)
6663 unsigned int j, this_e;
6665 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6666 this_e &= 2 * e - 1;
6667 this_e *= u;
6669 for (j = 0; j < u; ++j)
6670 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
6672 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
6674 icode = direct_optab_handler (vec_perm_const_optab, qimode);
6675 if (icode != CODE_FOR_nothing)
6677 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6678 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6679 gen_lowpart (qimode, v1), sel_qi);
6680 if (tmp)
6681 return gen_lowpart (mode, tmp);
6686 /* Otherwise expand as a fully variable permuation. */
6687 icode = direct_optab_handler (vec_perm_optab, mode);
6688 if (icode != CODE_FOR_nothing)
6690 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6691 if (tmp)
6692 return tmp;
6695 /* As a special case to aid several targets, lower the element-based
6696 permutation to a byte-based permutation and try again. */
6697 if (qimode == VOIDmode)
6698 return NULL_RTX;
6699 icode = direct_optab_handler (vec_perm_optab, qimode);
6700 if (icode == CODE_FOR_nothing)
6701 return NULL_RTX;
6703 if (sel_qi == NULL)
6705 /* Multiply each element by its byte size. */
6706 enum machine_mode selmode = GET_MODE (sel);
6707 if (u == 2)
6708 sel = expand_simple_binop (selmode, PLUS, sel, sel,
6709 sel, 0, OPTAB_DIRECT);
6710 else
6711 sel = expand_simple_binop (selmode, ASHIFT, sel,
6712 GEN_INT (exact_log2 (u)),
6713 sel, 0, OPTAB_DIRECT);
6714 gcc_assert (sel != NULL);
6716 /* Broadcast the low byte each element into each of its bytes. */
6717 vec = rtvec_alloc (w);
6718 for (i = 0; i < w; ++i)
6720 int this_e = i / u * u;
6721 if (BYTES_BIG_ENDIAN)
6722 this_e += u - 1;
6723 RTVEC_ELT (vec, i) = GEN_INT (this_e);
6725 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6726 sel = gen_lowpart (qimode, sel);
6727 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
6728 gcc_assert (sel != NULL);
6730 /* Add the byte offset to each byte element. */
6731 /* Note that the definition of the indicies here is memory ordering,
6732 so there should be no difference between big and little endian. */
6733 vec = rtvec_alloc (w);
6734 for (i = 0; i < w; ++i)
6735 RTVEC_ELT (vec, i) = GEN_INT (i % u);
6736 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
6737 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
6738 sel, 0, OPTAB_DIRECT);
6739 gcc_assert (sel_qi != NULL);
6742 tmp = mode != qimode ? gen_reg_rtx (qimode) : target;
6743 tmp = expand_vec_perm_1 (icode, tmp, gen_lowpart (qimode, v0),
6744 gen_lowpart (qimode, v1), sel_qi);
6745 if (tmp)
6746 tmp = gen_lowpart (mode, tmp);
6747 return tmp;
6750 /* Return insn code for a conditional operator with a comparison in
6751 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
6753 static inline enum insn_code
6754 get_vcond_icode (enum machine_mode vmode, enum machine_mode cmode, bool uns)
6756 enum insn_code icode = CODE_FOR_nothing;
6757 if (uns)
6758 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
6759 else
6760 icode = convert_optab_handler (vcond_optab, vmode, cmode);
6761 return icode;
6764 /* Return TRUE iff, appropriate vector insns are available
6765 for vector cond expr with vector type VALUE_TYPE and a comparison
6766 with operand vector types in CMP_OP_TYPE. */
6768 bool
6769 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
6771 enum machine_mode value_mode = TYPE_MODE (value_type);
6772 enum machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
6773 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
6774 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
6775 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
6776 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
6777 return false;
6778 return true;
6781 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
6782 three operands. */
6785 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
6786 rtx target)
6788 struct expand_operand ops[6];
6789 enum insn_code icode;
6790 rtx comparison, rtx_op1, rtx_op2;
6791 enum machine_mode mode = TYPE_MODE (vec_cond_type);
6792 enum machine_mode cmp_op_mode;
6793 bool unsignedp;
6794 tree op0a, op0b;
6795 enum tree_code tcode;
6797 if (COMPARISON_CLASS_P (op0))
6799 op0a = TREE_OPERAND (op0, 0);
6800 op0b = TREE_OPERAND (op0, 1);
6801 tcode = TREE_CODE (op0);
6803 else
6805 /* Fake op0 < 0. */
6806 gcc_assert (!TYPE_UNSIGNED (TREE_TYPE (op0)));
6807 op0a = op0;
6808 op0b = build_zero_cst (TREE_TYPE (op0));
6809 tcode = LT_EXPR;
6811 unsignedp = TYPE_UNSIGNED (TREE_TYPE (op0a));
6812 cmp_op_mode = TYPE_MODE (TREE_TYPE (op0a));
6815 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
6816 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
6818 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
6819 if (icode == CODE_FOR_nothing)
6820 return 0;
6822 comparison = vector_compare_rtx (tcode, op0a, op0b, unsignedp, icode);
6823 rtx_op1 = expand_normal (op1);
6824 rtx_op2 = expand_normal (op2);
6826 create_output_operand (&ops[0], target, mode);
6827 create_input_operand (&ops[1], rtx_op1, mode);
6828 create_input_operand (&ops[2], rtx_op2, mode);
6829 create_fixed_operand (&ops[3], comparison);
6830 create_fixed_operand (&ops[4], XEXP (comparison, 0));
6831 create_fixed_operand (&ops[5], XEXP (comparison, 1));
6832 expand_insn (icode, 6, ops);
6833 return ops[0].value;
6836 /* Return non-zero if a highpart multiply is supported of can be synthisized.
6837 For the benefit of expand_mult_highpart, the return value is 1 for direct,
6838 2 for even/odd widening, and 3 for hi/lo widening. */
6841 can_mult_highpart_p (enum machine_mode mode, bool uns_p)
6843 optab op;
6844 unsigned char *sel;
6845 unsigned i, nunits;
6847 op = uns_p ? umul_highpart_optab : smul_highpart_optab;
6848 if (optab_handler (op, mode) != CODE_FOR_nothing)
6849 return 1;
6851 /* If the mode is an integral vector, synth from widening operations. */
6852 if (GET_MODE_CLASS (mode) != MODE_VECTOR_INT)
6853 return 0;
6855 nunits = GET_MODE_NUNITS (mode);
6856 sel = XALLOCAVEC (unsigned char, nunits);
6858 op = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6859 if (optab_handler (op, mode) != CODE_FOR_nothing)
6861 op = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6862 if (optab_handler (op, mode) != CODE_FOR_nothing)
6864 for (i = 0; i < nunits; ++i)
6865 sel[i] = !BYTES_BIG_ENDIAN + (i & ~1) + ((i & 1) ? nunits : 0);
6866 if (can_vec_perm_p (mode, false, sel))
6867 return 2;
6871 op = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6872 if (optab_handler (op, mode) != CODE_FOR_nothing)
6874 op = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6875 if (optab_handler (op, mode) != CODE_FOR_nothing)
6877 for (i = 0; i < nunits; ++i)
6878 sel[i] = 2 * i + (BYTES_BIG_ENDIAN ? 0 : 1);
6879 if (can_vec_perm_p (mode, false, sel))
6880 return 3;
6884 return 0;
6887 /* Expand a highpart multiply. */
6890 expand_mult_highpart (enum machine_mode mode, rtx op0, rtx op1,
6891 rtx target, bool uns_p)
6893 struct expand_operand eops[3];
6894 enum insn_code icode;
6895 int method, i, nunits;
6896 enum machine_mode wmode;
6897 rtx m1, m2, perm;
6898 optab tab1, tab2;
6899 rtvec v;
6901 method = can_mult_highpart_p (mode, uns_p);
6902 switch (method)
6904 case 0:
6905 return NULL_RTX;
6906 case 1:
6907 tab1 = uns_p ? umul_highpart_optab : smul_highpart_optab;
6908 return expand_binop (mode, tab1, op0, op1, target, uns_p,
6909 OPTAB_LIB_WIDEN);
6910 case 2:
6911 tab1 = uns_p ? vec_widen_umult_even_optab : vec_widen_smult_even_optab;
6912 tab2 = uns_p ? vec_widen_umult_odd_optab : vec_widen_smult_odd_optab;
6913 break;
6914 case 3:
6915 tab1 = uns_p ? vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
6916 tab2 = uns_p ? vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
6917 if (BYTES_BIG_ENDIAN)
6919 optab t = tab1;
6920 tab1 = tab2;
6921 tab2 = t;
6923 break;
6924 default:
6925 gcc_unreachable ();
6928 icode = optab_handler (tab1, mode);
6929 nunits = GET_MODE_NUNITS (mode);
6930 wmode = insn_data[icode].operand[0].mode;
6931 gcc_checking_assert (2 * GET_MODE_NUNITS (wmode) == nunits);
6932 gcc_checking_assert (GET_MODE_SIZE (wmode) == GET_MODE_SIZE (mode));
6934 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6935 create_input_operand (&eops[1], op0, mode);
6936 create_input_operand (&eops[2], op1, mode);
6937 expand_insn (icode, 3, eops);
6938 m1 = gen_lowpart (mode, eops[0].value);
6940 create_output_operand (&eops[0], gen_reg_rtx (wmode), wmode);
6941 create_input_operand (&eops[1], op0, mode);
6942 create_input_operand (&eops[2], op1, mode);
6943 expand_insn (optab_handler (tab2, mode), 3, eops);
6944 m2 = gen_lowpart (mode, eops[0].value);
6946 v = rtvec_alloc (nunits);
6947 if (method == 2)
6949 for (i = 0; i < nunits; ++i)
6950 RTVEC_ELT (v, i) = GEN_INT (!BYTES_BIG_ENDIAN + (i & ~1)
6951 + ((i & 1) ? nunits : 0));
6953 else
6955 for (i = 0; i < nunits; ++i)
6956 RTVEC_ELT (v, i) = GEN_INT (2 * i + (BYTES_BIG_ENDIAN ? 0 : 1));
6958 perm = gen_rtx_CONST_VECTOR (mode, v);
6960 return expand_vec_perm (mode, m1, m2, perm, target);
6963 /* Return true if target supports vector masked load/store for mode. */
6964 bool
6965 can_vec_mask_load_store_p (enum machine_mode mode, bool is_load)
6967 optab op = is_load ? maskload_optab : maskstore_optab;
6968 enum machine_mode vmode;
6969 unsigned int vector_sizes;
6971 /* If mode is vector mode, check it directly. */
6972 if (VECTOR_MODE_P (mode))
6973 return optab_handler (op, mode) != CODE_FOR_nothing;
6975 /* Otherwise, return true if there is some vector mode with
6976 the mask load/store supported. */
6978 /* See if there is any chance the mask load or store might be
6979 vectorized. If not, punt. */
6980 vmode = targetm.vectorize.preferred_simd_mode (mode);
6981 if (!VECTOR_MODE_P (vmode))
6982 return false;
6984 if (optab_handler (op, vmode) != CODE_FOR_nothing)
6985 return true;
6987 vector_sizes = targetm.vectorize.autovectorize_vector_sizes ();
6988 while (vector_sizes != 0)
6990 unsigned int cur = 1 << floor_log2 (vector_sizes);
6991 vector_sizes &= ~cur;
6992 if (cur <= GET_MODE_SIZE (mode))
6993 continue;
6994 vmode = mode_for_vector (mode, cur / GET_MODE_SIZE (mode));
6995 if (VECTOR_MODE_P (vmode)
6996 && optab_handler (op, vmode) != CODE_FOR_nothing)
6997 return true;
6999 return false;
7002 /* Return true if there is a compare_and_swap pattern. */
7004 bool
7005 can_compare_and_swap_p (enum machine_mode mode, bool allow_libcall)
7007 enum insn_code icode;
7009 /* Check for __atomic_compare_and_swap. */
7010 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7011 if (icode != CODE_FOR_nothing)
7012 return true;
7014 /* Check for __sync_compare_and_swap. */
7015 icode = optab_handler (sync_compare_and_swap_optab, mode);
7016 if (icode != CODE_FOR_nothing)
7017 return true;
7018 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
7019 return true;
7021 /* No inline compare and swap. */
7022 return false;
7025 /* Return true if an atomic exchange can be performed. */
7027 bool
7028 can_atomic_exchange_p (enum machine_mode mode, bool allow_libcall)
7030 enum insn_code icode;
7032 /* Check for __atomic_exchange. */
7033 icode = direct_optab_handler (atomic_exchange_optab, mode);
7034 if (icode != CODE_FOR_nothing)
7035 return true;
7037 /* Don't check __sync_test_and_set, as on some platforms that
7038 has reduced functionality. Targets that really do support
7039 a proper exchange should simply be updated to the __atomics. */
7041 return can_compare_and_swap_p (mode, allow_libcall);
7045 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
7046 pattern. */
7048 static void
7049 find_cc_set (rtx x, const_rtx pat, void *data)
7051 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
7052 && GET_CODE (pat) == SET)
7054 rtx *p_cc_reg = (rtx *) data;
7055 gcc_assert (!*p_cc_reg);
7056 *p_cc_reg = x;
7060 /* This is a helper function for the other atomic operations. This function
7061 emits a loop that contains SEQ that iterates until a compare-and-swap
7062 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7063 a set of instructions that takes a value from OLD_REG as an input and
7064 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7065 set to the current contents of MEM. After SEQ, a compare-and-swap will
7066 attempt to update MEM with NEW_REG. The function returns true when the
7067 loop was generated successfully. */
7069 static bool
7070 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7072 enum machine_mode mode = GET_MODE (mem);
7073 rtx label, cmp_reg, success, oldval;
7075 /* The loop we want to generate looks like
7077 cmp_reg = mem;
7078 label:
7079 old_reg = cmp_reg;
7080 seq;
7081 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
7082 if (success)
7083 goto label;
7085 Note that we only do the plain load from memory once. Subsequent
7086 iterations use the value loaded by the compare-and-swap pattern. */
7088 label = gen_label_rtx ();
7089 cmp_reg = gen_reg_rtx (mode);
7091 emit_move_insn (cmp_reg, mem);
7092 emit_label (label);
7093 emit_move_insn (old_reg, cmp_reg);
7094 if (seq)
7095 emit_insn (seq);
7097 success = NULL_RTX;
7098 oldval = cmp_reg;
7099 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
7100 new_reg, false, MEMMODEL_SEQ_CST,
7101 MEMMODEL_RELAXED))
7102 return false;
7104 if (oldval != cmp_reg)
7105 emit_move_insn (cmp_reg, oldval);
7107 /* Mark this jump predicted not taken. */
7108 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
7109 GET_MODE (success), 1, label, 0);
7110 return true;
7114 /* This function tries to emit an atomic_exchange intruction. VAL is written
7115 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
7116 using TARGET if possible. */
7118 static rtx
7119 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7121 enum machine_mode mode = GET_MODE (mem);
7122 enum insn_code icode;
7124 /* If the target supports the exchange directly, great. */
7125 icode = direct_optab_handler (atomic_exchange_optab, mode);
7126 if (icode != CODE_FOR_nothing)
7128 struct expand_operand ops[4];
7130 create_output_operand (&ops[0], target, mode);
7131 create_fixed_operand (&ops[1], mem);
7132 create_input_operand (&ops[2], val, mode);
7133 create_integer_operand (&ops[3], model);
7134 if (maybe_expand_insn (icode, 4, ops))
7135 return ops[0].value;
7138 return NULL_RTX;
7141 /* This function tries to implement an atomic exchange operation using
7142 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7143 The previous contents of *MEM are returned, using TARGET if possible.
7144 Since this instructionn is an acquire barrier only, stronger memory
7145 models may require additional barriers to be emitted. */
7147 static rtx
7148 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7149 enum memmodel model)
7151 enum machine_mode mode = GET_MODE (mem);
7152 enum insn_code icode;
7153 rtx last_insn = get_last_insn ();
7155 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7157 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7158 exists, and the memory model is stronger than acquire, add a release
7159 barrier before the instruction. */
7161 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST
7162 || (model & MEMMODEL_MASK) == MEMMODEL_RELEASE
7163 || (model & MEMMODEL_MASK) == MEMMODEL_ACQ_REL)
7164 expand_mem_thread_fence (model);
7166 if (icode != CODE_FOR_nothing)
7168 struct expand_operand ops[3];
7169 create_output_operand (&ops[0], target, mode);
7170 create_fixed_operand (&ops[1], mem);
7171 create_input_operand (&ops[2], val, mode);
7172 if (maybe_expand_insn (icode, 3, ops))
7173 return ops[0].value;
7176 /* If an external test-and-set libcall is provided, use that instead of
7177 any external compare-and-swap that we might get from the compare-and-
7178 swap-loop expansion later. */
7179 if (!can_compare_and_swap_p (mode, false))
7181 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7182 if (libfunc != NULL)
7184 rtx addr;
7186 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7187 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7188 mode, 2, addr, ptr_mode,
7189 val, mode);
7193 /* If the test_and_set can't be emitted, eliminate any barrier that might
7194 have been emitted. */
7195 delete_insns_since (last_insn);
7196 return NULL_RTX;
7199 /* This function tries to implement an atomic exchange operation using a
7200 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7201 *MEM are returned, using TARGET if possible. No memory model is required
7202 since a compare_and_swap loop is seq-cst. */
7204 static rtx
7205 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7207 enum machine_mode mode = GET_MODE (mem);
7209 if (can_compare_and_swap_p (mode, true))
7211 if (!target || !register_operand (target, mode))
7212 target = gen_reg_rtx (mode);
7213 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7214 return target;
7217 return NULL_RTX;
7220 /* This function tries to implement an atomic test-and-set operation
7221 using the atomic_test_and_set instruction pattern. A boolean value
7222 is returned from the operation, using TARGET if possible. */
7224 #ifndef HAVE_atomic_test_and_set
7225 #define HAVE_atomic_test_and_set 0
7226 #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
7227 #endif
7229 static rtx
7230 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7232 enum machine_mode pat_bool_mode;
7233 struct expand_operand ops[3];
7235 if (!HAVE_atomic_test_and_set)
7236 return NULL_RTX;
7238 /* While we always get QImode from __atomic_test_and_set, we get
7239 other memory modes from __sync_lock_test_and_set. Note that we
7240 use no endian adjustment here. This matches the 4.6 behavior
7241 in the Sparc backend. */
7242 gcc_checking_assert
7243 (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
7244 if (GET_MODE (mem) != QImode)
7245 mem = adjust_address_nv (mem, QImode, 0);
7247 pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
7248 create_output_operand (&ops[0], target, pat_bool_mode);
7249 create_fixed_operand (&ops[1], mem);
7250 create_integer_operand (&ops[2], model);
7252 if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
7253 return ops[0].value;
7254 return NULL_RTX;
7257 /* This function expands the legacy _sync_lock test_and_set operation which is
7258 generally an atomic exchange. Some limited targets only allow the
7259 constant 1 to be stored. This is an ACQUIRE operation.
7261 TARGET is an optional place to stick the return value.
7262 MEM is where VAL is stored. */
7265 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7267 rtx ret;
7269 /* Try an atomic_exchange first. */
7270 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_ACQUIRE);
7271 if (ret)
7272 return ret;
7274 ret = maybe_emit_sync_lock_test_and_set (target, mem, val, MEMMODEL_ACQUIRE);
7275 if (ret)
7276 return ret;
7278 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7279 if (ret)
7280 return ret;
7282 /* If there are no other options, try atomic_test_and_set if the value
7283 being stored is 1. */
7284 if (val == const1_rtx)
7285 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_ACQUIRE);
7287 return ret;
7290 /* This function expands the atomic test_and_set operation:
7291 atomically store a boolean TRUE into MEM and return the previous value.
7293 MEMMODEL is the memory model variant to use.
7294 TARGET is an optional place to stick the return value. */
7297 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7299 enum machine_mode mode = GET_MODE (mem);
7300 rtx ret, trueval, subtarget;
7302 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7303 if (ret)
7304 return ret;
7306 /* Be binary compatible with non-default settings of trueval, and different
7307 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7308 another only has atomic-exchange. */
7309 if (targetm.atomic_test_and_set_trueval == 1)
7311 trueval = const1_rtx;
7312 subtarget = target ? target : gen_reg_rtx (mode);
7314 else
7316 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7317 subtarget = gen_reg_rtx (mode);
7320 /* Try the atomic-exchange optab... */
7321 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7323 /* ... then an atomic-compare-and-swap loop ... */
7324 if (!ret)
7325 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7327 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7328 if (!ret)
7329 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7331 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7332 things with the value 1. Thus we try again without trueval. */
7333 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7334 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7336 /* Failing all else, assume a single threaded environment and simply
7337 perform the operation. */
7338 if (!ret)
7340 /* If the result is ignored skip the move to target. */
7341 if (subtarget != const0_rtx)
7342 emit_move_insn (subtarget, mem);
7344 emit_move_insn (mem, trueval);
7345 ret = subtarget;
7348 /* Recall that have to return a boolean value; rectify if trueval
7349 is not exactly one. */
7350 if (targetm.atomic_test_and_set_trueval != 1)
7351 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7353 return ret;
7356 /* This function expands the atomic exchange operation:
7357 atomically store VAL in MEM and return the previous value in MEM.
7359 MEMMODEL is the memory model variant to use.
7360 TARGET is an optional place to stick the return value. */
7363 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7365 rtx ret;
7367 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7369 /* Next try a compare-and-swap loop for the exchange. */
7370 if (!ret)
7371 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7373 return ret;
7376 /* This function expands the atomic compare exchange operation:
7378 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7379 *PTARGET_OVAL is an optional place to store the old value from memory.
7380 Both target parameters may be NULL to indicate that we do not care about
7381 that return value. Both target parameters are updated on success to
7382 the actual location of the corresponding result.
7384 MEMMODEL is the memory model variant to use.
7386 The return value of the function is true for success. */
7388 bool
7389 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7390 rtx mem, rtx expected, rtx desired,
7391 bool is_weak, enum memmodel succ_model,
7392 enum memmodel fail_model)
7394 enum machine_mode mode = GET_MODE (mem);
7395 struct expand_operand ops[8];
7396 enum insn_code icode;
7397 rtx target_oval, target_bool = NULL_RTX;
7398 rtx libfunc;
7400 /* Load expected into a register for the compare and swap. */
7401 if (MEM_P (expected))
7402 expected = copy_to_reg (expected);
7404 /* Make sure we always have some place to put the return oldval.
7405 Further, make sure that place is distinct from the input expected,
7406 just in case we need that path down below. */
7407 if (ptarget_oval == NULL
7408 || (target_oval = *ptarget_oval) == NULL
7409 || reg_overlap_mentioned_p (expected, target_oval))
7410 target_oval = gen_reg_rtx (mode);
7412 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7413 if (icode != CODE_FOR_nothing)
7415 enum machine_mode bool_mode = insn_data[icode].operand[0].mode;
7417 /* Make sure we always have a place for the bool operand. */
7418 if (ptarget_bool == NULL
7419 || (target_bool = *ptarget_bool) == NULL
7420 || GET_MODE (target_bool) != bool_mode)
7421 target_bool = gen_reg_rtx (bool_mode);
7423 /* Emit the compare_and_swap. */
7424 create_output_operand (&ops[0], target_bool, bool_mode);
7425 create_output_operand (&ops[1], target_oval, mode);
7426 create_fixed_operand (&ops[2], mem);
7427 create_input_operand (&ops[3], expected, mode);
7428 create_input_operand (&ops[4], desired, mode);
7429 create_integer_operand (&ops[5], is_weak);
7430 create_integer_operand (&ops[6], succ_model);
7431 create_integer_operand (&ops[7], fail_model);
7432 if (maybe_expand_insn (icode, 8, ops))
7434 /* Return success/failure. */
7435 target_bool = ops[0].value;
7436 target_oval = ops[1].value;
7437 goto success;
7441 /* Otherwise fall back to the original __sync_val_compare_and_swap
7442 which is always seq-cst. */
7443 icode = optab_handler (sync_compare_and_swap_optab, mode);
7444 if (icode != CODE_FOR_nothing)
7446 rtx cc_reg;
7448 create_output_operand (&ops[0], target_oval, mode);
7449 create_fixed_operand (&ops[1], mem);
7450 create_input_operand (&ops[2], expected, mode);
7451 create_input_operand (&ops[3], desired, mode);
7452 if (!maybe_expand_insn (icode, 4, ops))
7453 return false;
7455 target_oval = ops[0].value;
7457 /* If the caller isn't interested in the boolean return value,
7458 skip the computation of it. */
7459 if (ptarget_bool == NULL)
7460 goto success;
7462 /* Otherwise, work out if the compare-and-swap succeeded. */
7463 cc_reg = NULL_RTX;
7464 if (have_insn_for (COMPARE, CCmode))
7465 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7466 if (cc_reg)
7468 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7469 const0_rtx, VOIDmode, 0, 1);
7470 goto success;
7472 goto success_bool_from_val;
7475 /* Also check for library support for __sync_val_compare_and_swap. */
7476 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7477 if (libfunc != NULL)
7479 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7480 target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7481 mode, 3, addr, ptr_mode,
7482 expected, mode, desired, mode);
7484 /* Compute the boolean return value only if requested. */
7485 if (ptarget_bool)
7486 goto success_bool_from_val;
7487 else
7488 goto success;
7491 /* Failure. */
7492 return false;
7494 success_bool_from_val:
7495 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7496 expected, VOIDmode, 1, 1);
7497 success:
7498 /* Make sure that the oval output winds up where the caller asked. */
7499 if (ptarget_oval)
7500 *ptarget_oval = target_oval;
7501 if (ptarget_bool)
7502 *ptarget_bool = target_bool;
7503 return true;
7506 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7508 static void
7509 expand_asm_memory_barrier (void)
7511 rtx asm_op, clob;
7513 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7514 rtvec_alloc (0), rtvec_alloc (0),
7515 rtvec_alloc (0), UNKNOWN_LOCATION);
7516 MEM_VOLATILE_P (asm_op) = 1;
7518 clob = gen_rtx_SCRATCH (VOIDmode);
7519 clob = gen_rtx_MEM (BLKmode, clob);
7520 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7522 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7525 /* This routine will either emit the mem_thread_fence pattern or issue a
7526 sync_synchronize to generate a fence for memory model MEMMODEL. */
7528 #ifndef HAVE_mem_thread_fence
7529 # define HAVE_mem_thread_fence 0
7530 # define gen_mem_thread_fence(x) (gcc_unreachable (), NULL_RTX)
7531 #endif
7532 #ifndef HAVE_memory_barrier
7533 # define HAVE_memory_barrier 0
7534 # define gen_memory_barrier() (gcc_unreachable (), NULL_RTX)
7535 #endif
7537 void
7538 expand_mem_thread_fence (enum memmodel model)
7540 if (HAVE_mem_thread_fence)
7541 emit_insn (gen_mem_thread_fence (GEN_INT (model)));
7542 else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
7544 if (HAVE_memory_barrier)
7545 emit_insn (gen_memory_barrier ());
7546 else if (synchronize_libfunc != NULL_RTX)
7547 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7548 else
7549 expand_asm_memory_barrier ();
7553 /* This routine will either emit the mem_signal_fence pattern or issue a
7554 sync_synchronize to generate a fence for memory model MEMMODEL. */
7556 #ifndef HAVE_mem_signal_fence
7557 # define HAVE_mem_signal_fence 0
7558 # define gen_mem_signal_fence(x) (gcc_unreachable (), NULL_RTX)
7559 #endif
7561 void
7562 expand_mem_signal_fence (enum memmodel model)
7564 if (HAVE_mem_signal_fence)
7565 emit_insn (gen_mem_signal_fence (GEN_INT (model)));
7566 else if ((model & MEMMODEL_MASK) != MEMMODEL_RELAXED)
7568 /* By default targets are coherent between a thread and the signal
7569 handler running on the same thread. Thus this really becomes a
7570 compiler barrier, in that stores must not be sunk past
7571 (or raised above) a given point. */
7572 expand_asm_memory_barrier ();
7576 /* This function expands the atomic load operation:
7577 return the atomically loaded value in MEM.
7579 MEMMODEL is the memory model variant to use.
7580 TARGET is an option place to stick the return value. */
7583 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7585 enum machine_mode mode = GET_MODE (mem);
7586 enum insn_code icode;
7588 /* If the target supports the load directly, great. */
7589 icode = direct_optab_handler (atomic_load_optab, mode);
7590 if (icode != CODE_FOR_nothing)
7592 struct expand_operand ops[3];
7594 create_output_operand (&ops[0], target, mode);
7595 create_fixed_operand (&ops[1], mem);
7596 create_integer_operand (&ops[2], model);
7597 if (maybe_expand_insn (icode, 3, ops))
7598 return ops[0].value;
7601 /* If the size of the object is greater than word size on this target,
7602 then we assume that a load will not be atomic. */
7603 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7605 /* Issue val = compare_and_swap (mem, 0, 0).
7606 This may cause the occasional harmless store of 0 when the value is
7607 already 0, but it seems to be OK according to the standards guys. */
7608 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7609 const0_rtx, false, model, model))
7610 return target;
7611 else
7612 /* Otherwise there is no atomic load, leave the library call. */
7613 return NULL_RTX;
7616 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7617 if (!target || target == const0_rtx)
7618 target = gen_reg_rtx (mode);
7620 /* For SEQ_CST, emit a barrier before the load. */
7621 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7622 expand_mem_thread_fence (model);
7624 emit_move_insn (target, mem);
7626 /* Emit the appropriate barrier after the load. */
7627 expand_mem_thread_fence (model);
7629 return target;
7632 /* This function expands the atomic store operation:
7633 Atomically store VAL in MEM.
7634 MEMMODEL is the memory model variant to use.
7635 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7636 function returns const0_rtx if a pattern was emitted. */
7639 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7641 enum machine_mode mode = GET_MODE (mem);
7642 enum insn_code icode;
7643 struct expand_operand ops[3];
7645 /* If the target supports the store directly, great. */
7646 icode = direct_optab_handler (atomic_store_optab, mode);
7647 if (icode != CODE_FOR_nothing)
7649 create_fixed_operand (&ops[0], mem);
7650 create_input_operand (&ops[1], val, mode);
7651 create_integer_operand (&ops[2], model);
7652 if (maybe_expand_insn (icode, 3, ops))
7653 return const0_rtx;
7656 /* If using __sync_lock_release is a viable alternative, try it. */
7657 if (use_release)
7659 icode = direct_optab_handler (sync_lock_release_optab, mode);
7660 if (icode != CODE_FOR_nothing)
7662 create_fixed_operand (&ops[0], mem);
7663 create_input_operand (&ops[1], const0_rtx, mode);
7664 if (maybe_expand_insn (icode, 2, ops))
7666 /* lock_release is only a release barrier. */
7667 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7668 expand_mem_thread_fence (model);
7669 return const0_rtx;
7674 /* If the size of the object is greater than word size on this target,
7675 a default store will not be atomic, Try a mem_exchange and throw away
7676 the result. If that doesn't work, don't do anything. */
7677 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7679 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7680 if (!target)
7681 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7682 if (target)
7683 return const0_rtx;
7684 else
7685 return NULL_RTX;
7688 /* Otherwise assume stores are atomic, and emit the proper barriers. */
7689 expand_mem_thread_fence (model);
7691 emit_move_insn (mem, val);
7693 /* For SEQ_CST, also emit a barrier after the store. */
7694 if ((model & MEMMODEL_MASK) == MEMMODEL_SEQ_CST)
7695 expand_mem_thread_fence (model);
7697 return const0_rtx;
7701 /* Structure containing the pointers and values required to process the
7702 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7704 struct atomic_op_functions
7706 direct_optab mem_fetch_before;
7707 direct_optab mem_fetch_after;
7708 direct_optab mem_no_result;
7709 optab fetch_before;
7710 optab fetch_after;
7711 direct_optab no_result;
7712 enum rtx_code reverse_code;
7716 /* Fill in structure pointed to by OP with the various optab entries for an
7717 operation of type CODE. */
7719 static void
7720 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7722 gcc_assert (op!= NULL);
7724 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7725 in the source code during compilation, and the optab entries are not
7726 computable until runtime. Fill in the values at runtime. */
7727 switch (code)
7729 case PLUS:
7730 op->mem_fetch_before = atomic_fetch_add_optab;
7731 op->mem_fetch_after = atomic_add_fetch_optab;
7732 op->mem_no_result = atomic_add_optab;
7733 op->fetch_before = sync_old_add_optab;
7734 op->fetch_after = sync_new_add_optab;
7735 op->no_result = sync_add_optab;
7736 op->reverse_code = MINUS;
7737 break;
7738 case MINUS:
7739 op->mem_fetch_before = atomic_fetch_sub_optab;
7740 op->mem_fetch_after = atomic_sub_fetch_optab;
7741 op->mem_no_result = atomic_sub_optab;
7742 op->fetch_before = sync_old_sub_optab;
7743 op->fetch_after = sync_new_sub_optab;
7744 op->no_result = sync_sub_optab;
7745 op->reverse_code = PLUS;
7746 break;
7747 case XOR:
7748 op->mem_fetch_before = atomic_fetch_xor_optab;
7749 op->mem_fetch_after = atomic_xor_fetch_optab;
7750 op->mem_no_result = atomic_xor_optab;
7751 op->fetch_before = sync_old_xor_optab;
7752 op->fetch_after = sync_new_xor_optab;
7753 op->no_result = sync_xor_optab;
7754 op->reverse_code = XOR;
7755 break;
7756 case AND:
7757 op->mem_fetch_before = atomic_fetch_and_optab;
7758 op->mem_fetch_after = atomic_and_fetch_optab;
7759 op->mem_no_result = atomic_and_optab;
7760 op->fetch_before = sync_old_and_optab;
7761 op->fetch_after = sync_new_and_optab;
7762 op->no_result = sync_and_optab;
7763 op->reverse_code = UNKNOWN;
7764 break;
7765 case IOR:
7766 op->mem_fetch_before = atomic_fetch_or_optab;
7767 op->mem_fetch_after = atomic_or_fetch_optab;
7768 op->mem_no_result = atomic_or_optab;
7769 op->fetch_before = sync_old_ior_optab;
7770 op->fetch_after = sync_new_ior_optab;
7771 op->no_result = sync_ior_optab;
7772 op->reverse_code = UNKNOWN;
7773 break;
7774 case NOT:
7775 op->mem_fetch_before = atomic_fetch_nand_optab;
7776 op->mem_fetch_after = atomic_nand_fetch_optab;
7777 op->mem_no_result = atomic_nand_optab;
7778 op->fetch_before = sync_old_nand_optab;
7779 op->fetch_after = sync_new_nand_optab;
7780 op->no_result = sync_nand_optab;
7781 op->reverse_code = UNKNOWN;
7782 break;
7783 default:
7784 gcc_unreachable ();
7788 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7789 using memory order MODEL. If AFTER is true the operation needs to return
7790 the value of *MEM after the operation, otherwise the previous value.
7791 TARGET is an optional place to place the result. The result is unused if
7792 it is const0_rtx.
7793 Return the result if there is a better sequence, otherwise NULL_RTX. */
7795 static rtx
7796 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7797 enum memmodel model, bool after)
7799 /* If the value is prefetched, or not used, it may be possible to replace
7800 the sequence with a native exchange operation. */
7801 if (!after || target == const0_rtx)
7803 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7804 if (code == AND && val == const0_rtx)
7806 if (target == const0_rtx)
7807 target = gen_reg_rtx (GET_MODE (mem));
7808 return maybe_emit_atomic_exchange (target, mem, val, model);
7811 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7812 if (code == IOR && val == constm1_rtx)
7814 if (target == const0_rtx)
7815 target = gen_reg_rtx (GET_MODE (mem));
7816 return maybe_emit_atomic_exchange (target, mem, val, model);
7820 return NULL_RTX;
7823 /* Try to emit an instruction for a specific operation varaition.
7824 OPTAB contains the OP functions.
7825 TARGET is an optional place to return the result. const0_rtx means unused.
7826 MEM is the memory location to operate on.
7827 VAL is the value to use in the operation.
7828 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7829 MODEL is the memory model, if used.
7830 AFTER is true if the returned result is the value after the operation. */
7832 static rtx
7833 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7834 rtx val, bool use_memmodel, enum memmodel model, bool after)
7836 enum machine_mode mode = GET_MODE (mem);
7837 struct expand_operand ops[4];
7838 enum insn_code icode;
7839 int op_counter = 0;
7840 int num_ops;
7842 /* Check to see if there is a result returned. */
7843 if (target == const0_rtx)
7845 if (use_memmodel)
7847 icode = direct_optab_handler (optab->mem_no_result, mode);
7848 create_integer_operand (&ops[2], model);
7849 num_ops = 3;
7851 else
7853 icode = direct_optab_handler (optab->no_result, mode);
7854 num_ops = 2;
7857 /* Otherwise, we need to generate a result. */
7858 else
7860 if (use_memmodel)
7862 icode = direct_optab_handler (after ? optab->mem_fetch_after
7863 : optab->mem_fetch_before, mode);
7864 create_integer_operand (&ops[3], model);
7865 num_ops = 4;
7867 else
7869 icode = optab_handler (after ? optab->fetch_after
7870 : optab->fetch_before, mode);
7871 num_ops = 3;
7873 create_output_operand (&ops[op_counter++], target, mode);
7875 if (icode == CODE_FOR_nothing)
7876 return NULL_RTX;
7878 create_fixed_operand (&ops[op_counter++], mem);
7879 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7880 create_convert_operand_to (&ops[op_counter++], val, mode, true);
7882 if (maybe_expand_insn (icode, num_ops, ops))
7883 return (target == const0_rtx ? const0_rtx : ops[0].value);
7885 return NULL_RTX;
7889 /* This function expands an atomic fetch_OP or OP_fetch operation:
7890 TARGET is an option place to stick the return value. const0_rtx indicates
7891 the result is unused.
7892 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7893 CODE is the operation being performed (OP)
7894 MEMMODEL is the memory model variant to use.
7895 AFTER is true to return the result of the operation (OP_fetch).
7896 AFTER is false to return the value before the operation (fetch_OP).
7898 This function will *only* generate instructions if there is a direct
7899 optab. No compare and swap loops or libcalls will be generated. */
7901 static rtx
7902 expand_atomic_fetch_op_no_fallback (rtx target, rtx mem, rtx val,
7903 enum rtx_code code, enum memmodel model,
7904 bool after)
7906 enum machine_mode mode = GET_MODE (mem);
7907 struct atomic_op_functions optab;
7908 rtx result;
7909 bool unused_result = (target == const0_rtx);
7911 get_atomic_op_for_code (&optab, code);
7913 /* Check to see if there are any better instructions. */
7914 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
7915 if (result)
7916 return result;
7918 /* Check for the case where the result isn't used and try those patterns. */
7919 if (unused_result)
7921 /* Try the memory model variant first. */
7922 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
7923 if (result)
7924 return result;
7926 /* Next try the old style withuot a memory model. */
7927 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
7928 if (result)
7929 return result;
7931 /* There is no no-result pattern, so try patterns with a result. */
7932 target = NULL_RTX;
7935 /* Try the __atomic version. */
7936 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
7937 if (result)
7938 return result;
7940 /* Try the older __sync version. */
7941 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
7942 if (result)
7943 return result;
7945 /* If the fetch value can be calculated from the other variation of fetch,
7946 try that operation. */
7947 if (after || unused_result || optab.reverse_code != UNKNOWN)
7949 /* Try the __atomic version, then the older __sync version. */
7950 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
7951 if (!result)
7952 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
7954 if (result)
7956 /* If the result isn't used, no need to do compensation code. */
7957 if (unused_result)
7958 return result;
7960 /* Issue compensation code. Fetch_after == fetch_before OP val.
7961 Fetch_before == after REVERSE_OP val. */
7962 if (!after)
7963 code = optab.reverse_code;
7964 if (code == NOT)
7966 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
7967 true, OPTAB_LIB_WIDEN);
7968 result = expand_simple_unop (mode, NOT, result, target, true);
7970 else
7971 result = expand_simple_binop (mode, code, result, val, target,
7972 true, OPTAB_LIB_WIDEN);
7973 return result;
7977 /* No direct opcode can be generated. */
7978 return NULL_RTX;
7983 /* This function expands an atomic fetch_OP or OP_fetch operation:
7984 TARGET is an option place to stick the return value. const0_rtx indicates
7985 the result is unused.
7986 atomically fetch MEM, perform the operation with VAL and return it to MEM.
7987 CODE is the operation being performed (OP)
7988 MEMMODEL is the memory model variant to use.
7989 AFTER is true to return the result of the operation (OP_fetch).
7990 AFTER is false to return the value before the operation (fetch_OP). */
7992 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7993 enum memmodel model, bool after)
7995 enum machine_mode mode = GET_MODE (mem);
7996 rtx result;
7997 bool unused_result = (target == const0_rtx);
7999 result = expand_atomic_fetch_op_no_fallback (target, mem, val, code, model,
8000 after);
8002 if (result)
8003 return result;
8005 /* Add/sub can be implemented by doing the reverse operation with -(val). */
8006 if (code == PLUS || code == MINUS)
8008 rtx tmp;
8009 enum rtx_code reverse = (code == PLUS ? MINUS : PLUS);
8011 start_sequence ();
8012 tmp = expand_simple_unop (mode, NEG, val, NULL_RTX, true);
8013 result = expand_atomic_fetch_op_no_fallback (target, mem, tmp, reverse,
8014 model, after);
8015 if (result)
8017 /* PLUS worked so emit the insns and return. */
8018 tmp = get_insns ();
8019 end_sequence ();
8020 emit_insn (tmp);
8021 return result;
8024 /* PLUS did not work, so throw away the negation code and continue. */
8025 end_sequence ();
8028 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
8029 if (!can_compare_and_swap_p (mode, false))
8031 rtx libfunc;
8032 bool fixup = false;
8033 enum rtx_code orig_code = code;
8034 struct atomic_op_functions optab;
8036 get_atomic_op_for_code (&optab, code);
8037 libfunc = optab_libfunc (after ? optab.fetch_after
8038 : optab.fetch_before, mode);
8039 if (libfunc == NULL
8040 && (after || unused_result || optab.reverse_code != UNKNOWN))
8042 fixup = true;
8043 if (!after)
8044 code = optab.reverse_code;
8045 libfunc = optab_libfunc (after ? optab.fetch_before
8046 : optab.fetch_after, mode);
8048 if (libfunc != NULL)
8050 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
8051 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
8052 2, addr, ptr_mode, val, mode);
8054 if (!unused_result && fixup)
8055 result = expand_simple_binop (mode, code, result, val, target,
8056 true, OPTAB_LIB_WIDEN);
8057 return result;
8060 /* We need the original code for any further attempts. */
8061 code = orig_code;
8064 /* If nothing else has succeeded, default to a compare and swap loop. */
8065 if (can_compare_and_swap_p (mode, true))
8067 rtx insn;
8068 rtx t0 = gen_reg_rtx (mode), t1;
8070 start_sequence ();
8072 /* If the result is used, get a register for it. */
8073 if (!unused_result)
8075 if (!target || !register_operand (target, mode))
8076 target = gen_reg_rtx (mode);
8077 /* If fetch_before, copy the value now. */
8078 if (!after)
8079 emit_move_insn (target, t0);
8081 else
8082 target = const0_rtx;
8084 t1 = t0;
8085 if (code == NOT)
8087 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
8088 true, OPTAB_LIB_WIDEN);
8089 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
8091 else
8092 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
8093 OPTAB_LIB_WIDEN);
8095 /* For after, copy the value now. */
8096 if (!unused_result && after)
8097 emit_move_insn (target, t1);
8098 insn = get_insns ();
8099 end_sequence ();
8101 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
8102 return target;
8105 return NULL_RTX;
8108 /* Return true if OPERAND is suitable for operand number OPNO of
8109 instruction ICODE. */
8111 bool
8112 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
8114 return (!insn_data[(int) icode].operand[opno].predicate
8115 || (insn_data[(int) icode].operand[opno].predicate
8116 (operand, insn_data[(int) icode].operand[opno].mode)));
8119 /* TARGET is a target of a multiword operation that we are going to
8120 implement as a series of word-mode operations. Return true if
8121 TARGET is suitable for this purpose. */
8123 bool
8124 valid_multiword_target_p (rtx target)
8126 enum machine_mode mode;
8127 int i;
8129 mode = GET_MODE (target);
8130 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
8131 if (!validate_subreg (word_mode, mode, target, i))
8132 return false;
8133 return true;
8136 /* Like maybe_legitimize_operand, but do not change the code of the
8137 current rtx value. */
8139 static bool
8140 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8141 struct expand_operand *op)
8143 /* See if the operand matches in its current form. */
8144 if (insn_operand_matches (icode, opno, op->value))
8145 return true;
8147 /* If the operand is a memory whose address has no side effects,
8148 try forcing the address into a non-virtual pseudo register.
8149 The check for side effects is important because copy_to_mode_reg
8150 cannot handle things like auto-modified addresses. */
8151 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8153 rtx addr, mem;
8155 mem = op->value;
8156 addr = XEXP (mem, 0);
8157 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8158 && !side_effects_p (addr))
8160 rtx last;
8161 enum machine_mode mode;
8163 last = get_last_insn ();
8164 mode = get_address_mode (mem);
8165 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8166 if (insn_operand_matches (icode, opno, mem))
8168 op->value = mem;
8169 return true;
8171 delete_insns_since (last);
8175 return false;
8178 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8179 on success, storing the new operand value back in OP. */
8181 static bool
8182 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8183 struct expand_operand *op)
8185 enum machine_mode mode, imode;
8186 bool old_volatile_ok, result;
8188 mode = op->mode;
8189 switch (op->type)
8191 case EXPAND_FIXED:
8192 old_volatile_ok = volatile_ok;
8193 volatile_ok = true;
8194 result = maybe_legitimize_operand_same_code (icode, opno, op);
8195 volatile_ok = old_volatile_ok;
8196 return result;
8198 case EXPAND_OUTPUT:
8199 gcc_assert (mode != VOIDmode);
8200 if (op->value
8201 && op->value != const0_rtx
8202 && GET_MODE (op->value) == mode
8203 && maybe_legitimize_operand_same_code (icode, opno, op))
8204 return true;
8206 op->value = gen_reg_rtx (mode);
8207 break;
8209 case EXPAND_INPUT:
8210 input:
8211 gcc_assert (mode != VOIDmode);
8212 gcc_assert (GET_MODE (op->value) == VOIDmode
8213 || GET_MODE (op->value) == mode);
8214 if (maybe_legitimize_operand_same_code (icode, opno, op))
8215 return true;
8217 op->value = copy_to_mode_reg (mode, op->value);
8218 break;
8220 case EXPAND_CONVERT_TO:
8221 gcc_assert (mode != VOIDmode);
8222 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8223 goto input;
8225 case EXPAND_CONVERT_FROM:
8226 if (GET_MODE (op->value) != VOIDmode)
8227 mode = GET_MODE (op->value);
8228 else
8229 /* The caller must tell us what mode this value has. */
8230 gcc_assert (mode != VOIDmode);
8232 imode = insn_data[(int) icode].operand[opno].mode;
8233 if (imode != VOIDmode && imode != mode)
8235 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8236 mode = imode;
8238 goto input;
8240 case EXPAND_ADDRESS:
8241 gcc_assert (mode != VOIDmode);
8242 op->value = convert_memory_address (mode, op->value);
8243 goto input;
8245 case EXPAND_INTEGER:
8246 mode = insn_data[(int) icode].operand[opno].mode;
8247 if (mode != VOIDmode && const_int_operand (op->value, mode))
8248 goto input;
8249 break;
8251 return insn_operand_matches (icode, opno, op->value);
8254 /* Make OP describe an input operand that should have the same value
8255 as VALUE, after any mode conversion that the target might request.
8256 TYPE is the type of VALUE. */
8258 void
8259 create_convert_operand_from_type (struct expand_operand *op,
8260 rtx value, tree type)
8262 create_convert_operand_from (op, value, TYPE_MODE (type),
8263 TYPE_UNSIGNED (type));
8266 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8267 of instruction ICODE. Return true on success, leaving the new operand
8268 values in the OPS themselves. Emit no code on failure. */
8270 bool
8271 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8272 unsigned int nops, struct expand_operand *ops)
8274 rtx last;
8275 unsigned int i;
8277 last = get_last_insn ();
8278 for (i = 0; i < nops; i++)
8279 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8281 delete_insns_since (last);
8282 return false;
8284 return true;
8287 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8288 as its operands. Return the instruction pattern on success,
8289 and emit any necessary set-up code. Return null and emit no
8290 code on failure. */
8293 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8294 struct expand_operand *ops)
8296 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8297 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8298 return NULL_RTX;
8300 switch (nops)
8302 case 1:
8303 return GEN_FCN (icode) (ops[0].value);
8304 case 2:
8305 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8306 case 3:
8307 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8308 case 4:
8309 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8310 ops[3].value);
8311 case 5:
8312 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8313 ops[3].value, ops[4].value);
8314 case 6:
8315 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8316 ops[3].value, ops[4].value, ops[5].value);
8317 case 7:
8318 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8319 ops[3].value, ops[4].value, ops[5].value,
8320 ops[6].value);
8321 case 8:
8322 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8323 ops[3].value, ops[4].value, ops[5].value,
8324 ops[6].value, ops[7].value);
8325 case 9:
8326 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8327 ops[3].value, ops[4].value, ops[5].value,
8328 ops[6].value, ops[7].value, ops[8].value);
8330 gcc_unreachable ();
8333 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8334 as its operands. Return true on success and emit no code on failure. */
8336 bool
8337 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8338 struct expand_operand *ops)
8340 rtx pat = maybe_gen_insn (icode, nops, ops);
8341 if (pat)
8343 emit_insn (pat);
8344 return true;
8346 return false;
8349 /* Like maybe_expand_insn, but for jumps. */
8351 bool
8352 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8353 struct expand_operand *ops)
8355 rtx pat = maybe_gen_insn (icode, nops, ops);
8356 if (pat)
8358 emit_jump_insn (pat);
8359 return true;
8361 return false;
8364 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8365 as its operands. */
8367 void
8368 expand_insn (enum insn_code icode, unsigned int nops,
8369 struct expand_operand *ops)
8371 if (!maybe_expand_insn (icode, nops, ops))
8372 gcc_unreachable ();
8375 /* Like expand_insn, but for jumps. */
8377 void
8378 expand_jump_insn (enum insn_code icode, unsigned int nops,
8379 struct expand_operand *ops)
8381 if (!maybe_expand_jump_insn (icode, nops, ops))
8382 gcc_unreachable ();
8385 /* Reduce conditional compilation elsewhere. */
8386 #ifndef HAVE_insv
8387 #define HAVE_insv 0
8388 #define CODE_FOR_insv CODE_FOR_nothing
8389 #endif
8390 #ifndef HAVE_extv
8391 #define HAVE_extv 0
8392 #define CODE_FOR_extv CODE_FOR_nothing
8393 #endif
8394 #ifndef HAVE_extzv
8395 #define HAVE_extzv 0
8396 #define CODE_FOR_extzv CODE_FOR_nothing
8397 #endif
8399 /* Enumerates the possible types of structure operand to an
8400 extraction_insn. */
8401 enum extraction_type { ET_unaligned_mem, ET_reg };
8403 /* Check whether insv, extv or extzv pattern ICODE can be used for an
8404 insertion or extraction of type TYPE on a structure of mode MODE.
8405 Return true if so and fill in *INSN accordingly. STRUCT_OP is the
8406 operand number of the structure (the first sign_extract or zero_extract
8407 operand) and FIELD_OP is the operand number of the field (the other
8408 side of the set from the sign_extract or zero_extract). */
8410 static bool
8411 get_traditional_extraction_insn (extraction_insn *insn,
8412 enum extraction_type type,
8413 enum machine_mode mode,
8414 enum insn_code icode,
8415 int struct_op, int field_op)
8417 const struct insn_data_d *data = &insn_data[icode];
8419 enum machine_mode struct_mode = data->operand[struct_op].mode;
8420 if (struct_mode == VOIDmode)
8421 struct_mode = word_mode;
8422 if (mode != struct_mode)
8423 return false;
8425 enum machine_mode field_mode = data->operand[field_op].mode;
8426 if (field_mode == VOIDmode)
8427 field_mode = word_mode;
8429 enum machine_mode pos_mode = data->operand[struct_op + 2].mode;
8430 if (pos_mode == VOIDmode)
8431 pos_mode = word_mode;
8433 insn->icode = icode;
8434 insn->field_mode = field_mode;
8435 insn->struct_mode = (type == ET_unaligned_mem ? byte_mode : struct_mode);
8436 insn->pos_mode = pos_mode;
8437 return true;
8440 /* Return true if an optab exists to perform an insertion or extraction
8441 of type TYPE in mode MODE. Describe the instruction in *INSN if so.
8443 REG_OPTAB is the optab to use for register structures and
8444 MISALIGN_OPTAB is the optab to use for misaligned memory structures.
8445 POS_OP is the operand number of the bit position. */
8447 static bool
8448 get_optab_extraction_insn (struct extraction_insn *insn,
8449 enum extraction_type type,
8450 enum machine_mode mode, direct_optab reg_optab,
8451 direct_optab misalign_optab, int pos_op)
8453 direct_optab optab = (type == ET_unaligned_mem ? misalign_optab : reg_optab);
8454 enum insn_code icode = direct_optab_handler (optab, mode);
8455 if (icode == CODE_FOR_nothing)
8456 return false;
8458 const struct insn_data_d *data = &insn_data[icode];
8460 insn->icode = icode;
8461 insn->field_mode = mode;
8462 insn->struct_mode = (type == ET_unaligned_mem ? BLKmode : mode);
8463 insn->pos_mode = data->operand[pos_op].mode;
8464 if (insn->pos_mode == VOIDmode)
8465 insn->pos_mode = word_mode;
8466 return true;
8469 /* Return true if an instruction exists to perform an insertion or
8470 extraction (PATTERN says which) of type TYPE in mode MODE.
8471 Describe the instruction in *INSN if so. */
8473 static bool
8474 get_extraction_insn (extraction_insn *insn,
8475 enum extraction_pattern pattern,
8476 enum extraction_type type,
8477 enum machine_mode mode)
8479 switch (pattern)
8481 case EP_insv:
8482 if (HAVE_insv
8483 && get_traditional_extraction_insn (insn, type, mode,
8484 CODE_FOR_insv, 0, 3))
8485 return true;
8486 return get_optab_extraction_insn (insn, type, mode, insv_optab,
8487 insvmisalign_optab, 2);
8489 case EP_extv:
8490 if (HAVE_extv
8491 && get_traditional_extraction_insn (insn, type, mode,
8492 CODE_FOR_extv, 1, 0))
8493 return true;
8494 return get_optab_extraction_insn (insn, type, mode, extv_optab,
8495 extvmisalign_optab, 3);
8497 case EP_extzv:
8498 if (HAVE_extzv
8499 && get_traditional_extraction_insn (insn, type, mode,
8500 CODE_FOR_extzv, 1, 0))
8501 return true;
8502 return get_optab_extraction_insn (insn, type, mode, extzv_optab,
8503 extzvmisalign_optab, 3);
8505 default:
8506 gcc_unreachable ();
8510 /* Return true if an instruction exists to access a field of mode
8511 FIELDMODE in a structure that has STRUCT_BITS significant bits.
8512 Describe the "best" such instruction in *INSN if so. PATTERN and
8513 TYPE describe the type of insertion or extraction we want to perform.
8515 For an insertion, the number of significant structure bits includes
8516 all bits of the target. For an extraction, it need only include the
8517 most significant bit of the field. Larger widths are acceptable
8518 in both cases. */
8520 static bool
8521 get_best_extraction_insn (extraction_insn *insn,
8522 enum extraction_pattern pattern,
8523 enum extraction_type type,
8524 unsigned HOST_WIDE_INT struct_bits,
8525 enum machine_mode field_mode)
8527 enum machine_mode mode = smallest_mode_for_size (struct_bits, MODE_INT);
8528 while (mode != VOIDmode)
8530 if (get_extraction_insn (insn, pattern, type, mode))
8532 while (mode != VOIDmode
8533 && GET_MODE_SIZE (mode) <= GET_MODE_SIZE (field_mode)
8534 && !TRULY_NOOP_TRUNCATION_MODES_P (insn->field_mode,
8535 field_mode))
8537 get_extraction_insn (insn, pattern, type, mode);
8538 mode = GET_MODE_WIDER_MODE (mode);
8540 return true;
8542 mode = GET_MODE_WIDER_MODE (mode);
8544 return false;
8547 /* Return true if an instruction exists to access a field of mode
8548 FIELDMODE in a register structure that has STRUCT_BITS significant bits.
8549 Describe the "best" such instruction in *INSN if so. PATTERN describes
8550 the type of insertion or extraction we want to perform.
8552 For an insertion, the number of significant structure bits includes
8553 all bits of the target. For an extraction, it need only include the
8554 most significant bit of the field. Larger widths are acceptable
8555 in both cases. */
8557 bool
8558 get_best_reg_extraction_insn (extraction_insn *insn,
8559 enum extraction_pattern pattern,
8560 unsigned HOST_WIDE_INT struct_bits,
8561 enum machine_mode field_mode)
8563 return get_best_extraction_insn (insn, pattern, ET_reg, struct_bits,
8564 field_mode);
8567 /* Return true if an instruction exists to access a field of BITSIZE
8568 bits starting BITNUM bits into a memory structure. Describe the
8569 "best" such instruction in *INSN if so. PATTERN describes the type
8570 of insertion or extraction we want to perform and FIELDMODE is the
8571 natural mode of the extracted field.
8573 The instructions considered here only access bytes that overlap
8574 the bitfield; they do not touch any surrounding bytes. */
8576 bool
8577 get_best_mem_extraction_insn (extraction_insn *insn,
8578 enum extraction_pattern pattern,
8579 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitnum,
8580 enum machine_mode field_mode)
8582 unsigned HOST_WIDE_INT struct_bits = (bitnum % BITS_PER_UNIT
8583 + bitsize
8584 + BITS_PER_UNIT - 1);
8585 struct_bits -= struct_bits % BITS_PER_UNIT;
8586 return get_best_extraction_insn (insn, pattern, ET_unaligned_mem,
8587 struct_bits, field_mode);
8590 #include "gt-optabs.h"