gcc/
[official-gcc.git] / gcc / optabs.c
blob9a549ff066776501ec49c825b7c6d5c63c524006
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 2011, 2012 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "diagnostic-core.h"
29 /* Include insn-config.h before expr.h so that HAVE_conditional_move
30 is properly defined. */
31 #include "insn-config.h"
32 #include "rtl.h"
33 #include "tree.h"
34 #include "tm_p.h"
35 #include "flags.h"
36 #include "function.h"
37 #include "except.h"
38 #include "expr.h"
39 #include "optabs.h"
40 #include "libfuncs.h"
41 #include "recog.h"
42 #include "reload.h"
43 #include "ggc.h"
44 #include "basic-block.h"
45 #include "target.h"
47 struct target_optabs default_target_optabs;
48 struct target_libfuncs default_target_libfuncs;
49 #if SWITCHABLE_TARGET
50 struct target_optabs *this_target_optabs = &default_target_optabs;
51 struct target_libfuncs *this_target_libfuncs = &default_target_libfuncs;
52 #endif
54 #define libfunc_hash \
55 (this_target_libfuncs->x_libfunc_hash)
57 /* Contains the optab used for each rtx code. */
58 optab code_to_optab[NUM_RTX_CODE + 1];
60 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
61 enum machine_mode *);
62 static rtx expand_unop_direct (enum machine_mode, optab, rtx, rtx, int);
63 static void emit_libcall_block_1 (rtx, rtx, rtx, rtx, bool);
65 /* Debug facility for use in GDB. */
66 void debug_optab_libfuncs (void);
68 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
69 #if ENABLE_DECIMAL_BID_FORMAT
70 #define DECIMAL_PREFIX "bid_"
71 #else
72 #define DECIMAL_PREFIX "dpd_"
73 #endif
75 /* Used for libfunc_hash. */
77 static hashval_t
78 hash_libfunc (const void *p)
80 const struct libfunc_entry *const e = (const struct libfunc_entry *) p;
82 return (((int) e->mode1 + (int) e->mode2 * NUM_MACHINE_MODES)
83 ^ e->optab);
86 /* Used for libfunc_hash. */
88 static int
89 eq_libfunc (const void *p, const void *q)
91 const struct libfunc_entry *const e1 = (const struct libfunc_entry *) p;
92 const struct libfunc_entry *const e2 = (const struct libfunc_entry *) q;
94 return (e1->optab == e2->optab
95 && e1->mode1 == e2->mode1
96 && e1->mode2 == e2->mode2);
99 /* Return libfunc corresponding operation defined by OPTAB converting
100 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
101 if no libfunc is available. */
103 convert_optab_libfunc (convert_optab optab, enum machine_mode mode1,
104 enum machine_mode mode2)
106 struct libfunc_entry e;
107 struct libfunc_entry **slot;
109 e.optab = (size_t) (optab - &convert_optab_table[0]);
110 e.mode1 = mode1;
111 e.mode2 = mode2;
112 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
113 if (!slot)
115 if (optab->libcall_gen)
117 optab->libcall_gen (optab, optab->libcall_basename, mode1, mode2);
118 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
119 if (slot)
120 return (*slot)->libfunc;
121 else
122 return NULL;
124 return NULL;
126 return (*slot)->libfunc;
129 /* Return libfunc corresponding operation defined by OPTAB in MODE.
130 Trigger lazy initialization if needed, return NULL if no libfunc is
131 available. */
133 optab_libfunc (optab optab, enum machine_mode mode)
135 struct libfunc_entry e;
136 struct libfunc_entry **slot;
138 e.optab = (size_t) (optab - &optab_table[0]);
139 e.mode1 = mode;
140 e.mode2 = VOIDmode;
141 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
142 if (!slot)
144 if (optab->libcall_gen)
146 optab->libcall_gen (optab, optab->libcall_basename,
147 optab->libcall_suffix, mode);
148 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash,
149 &e, NO_INSERT);
150 if (slot)
151 return (*slot)->libfunc;
152 else
153 return NULL;
155 return NULL;
157 return (*slot)->libfunc;
161 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
162 the result of operation CODE applied to OP0 (and OP1 if it is a binary
163 operation).
165 If the last insn does not set TARGET, don't do anything, but return 1.
167 If a previous insn sets TARGET and TARGET is one of OP0 or OP1,
168 don't add the REG_EQUAL note but return 0. Our caller can then try
169 again, ensuring that TARGET is not one of the operands. */
171 static int
172 add_equal_note (rtx insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
174 rtx last_insn, insn, set;
175 rtx note;
177 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
179 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
180 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
181 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
182 && GET_RTX_CLASS (code) != RTX_COMPARE
183 && GET_RTX_CLASS (code) != RTX_UNARY)
184 return 1;
186 if (GET_CODE (target) == ZERO_EXTRACT)
187 return 1;
189 for (last_insn = insns;
190 NEXT_INSN (last_insn) != NULL_RTX;
191 last_insn = NEXT_INSN (last_insn))
194 set = single_set (last_insn);
195 if (set == NULL_RTX)
196 return 1;
198 if (! rtx_equal_p (SET_DEST (set), target)
199 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
200 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
201 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
202 return 1;
204 /* If TARGET is in OP0 or OP1, check if anything in SEQ sets TARGET
205 besides the last insn. */
206 if (reg_overlap_mentioned_p (target, op0)
207 || (op1 && reg_overlap_mentioned_p (target, op1)))
209 insn = PREV_INSN (last_insn);
210 while (insn != NULL_RTX)
212 if (reg_set_p (target, insn))
213 return 0;
215 insn = PREV_INSN (insn);
219 if (GET_RTX_CLASS (code) == RTX_UNARY)
220 switch (code)
222 case FFS:
223 case CLZ:
224 case CTZ:
225 case CLRSB:
226 case POPCOUNT:
227 case PARITY:
228 case BSWAP:
229 if (GET_MODE (op0) != VOIDmode && GET_MODE (target) != GET_MODE (op0))
231 note = gen_rtx_fmt_e (code, GET_MODE (op0), copy_rtx (op0));
232 if (GET_MODE_SIZE (GET_MODE (op0))
233 > GET_MODE_SIZE (GET_MODE (target)))
234 note = simplify_gen_unary (TRUNCATE, GET_MODE (target),
235 note, GET_MODE (op0));
236 else
237 note = simplify_gen_unary (ZERO_EXTEND, GET_MODE (target),
238 note, GET_MODE (op0));
239 break;
241 /* FALLTHRU */
242 default:
243 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
244 break;
246 else
247 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
249 set_unique_reg_note (last_insn, REG_EQUAL, note);
251 return 1;
254 /* Given two input operands, OP0 and OP1, determine what the correct from_mode
255 for a widening operation would be. In most cases this would be OP0, but if
256 that's a constant it'll be VOIDmode, which isn't useful. */
258 static enum machine_mode
259 widened_mode (enum machine_mode to_mode, rtx op0, rtx op1)
261 enum machine_mode m0 = GET_MODE (op0);
262 enum machine_mode m1 = GET_MODE (op1);
263 enum machine_mode result;
265 if (m0 == VOIDmode && m1 == VOIDmode)
266 return to_mode;
267 else if (m0 == VOIDmode || GET_MODE_SIZE (m0) < GET_MODE_SIZE (m1))
268 result = m1;
269 else
270 result = m0;
272 if (GET_MODE_SIZE (result) > GET_MODE_SIZE (to_mode))
273 return to_mode;
275 return result;
278 /* Find a widening optab even if it doesn't widen as much as we want.
279 E.g. if from_mode is HImode, and to_mode is DImode, and there is no
280 direct HI->SI insn, then return SI->DI, if that exists.
281 If PERMIT_NON_WIDENING is non-zero then this can be used with
282 non-widening optabs also. */
284 enum insn_code
285 find_widening_optab_handler_and_mode (optab op, enum machine_mode to_mode,
286 enum machine_mode from_mode,
287 int permit_non_widening,
288 enum machine_mode *found_mode)
290 for (; (permit_non_widening || from_mode != to_mode)
291 && GET_MODE_SIZE (from_mode) <= GET_MODE_SIZE (to_mode)
292 && from_mode != VOIDmode;
293 from_mode = GET_MODE_WIDER_MODE (from_mode))
295 enum insn_code handler = widening_optab_handler (op, to_mode,
296 from_mode);
298 if (handler != CODE_FOR_nothing)
300 if (found_mode)
301 *found_mode = from_mode;
302 return handler;
306 return CODE_FOR_nothing;
309 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
310 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
311 not actually do a sign-extend or zero-extend, but can leave the
312 higher-order bits of the result rtx undefined, for example, in the case
313 of logical operations, but not right shifts. */
315 static rtx
316 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
317 int unsignedp, int no_extend)
319 rtx result;
321 /* If we don't have to extend and this is a constant, return it. */
322 if (no_extend && GET_MODE (op) == VOIDmode)
323 return op;
325 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
326 extend since it will be more efficient to do so unless the signedness of
327 a promoted object differs from our extension. */
328 if (! no_extend
329 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
330 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
331 return convert_modes (mode, oldmode, op, unsignedp);
333 /* If MODE is no wider than a single word, we return a paradoxical
334 SUBREG. */
335 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
336 return gen_rtx_SUBREG (mode, force_reg (GET_MODE (op), op), 0);
338 /* Otherwise, get an object of MODE, clobber it, and set the low-order
339 part to OP. */
341 result = gen_reg_rtx (mode);
342 emit_clobber (result);
343 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
344 return result;
347 /* Return the optab used for computing the operation given by the tree code,
348 CODE and the tree EXP. This function is not always usable (for example, it
349 cannot give complete results for multiplication or division) but probably
350 ought to be relied on more widely throughout the expander. */
351 optab
352 optab_for_tree_code (enum tree_code code, const_tree type,
353 enum optab_subtype subtype)
355 bool trapv;
356 switch (code)
358 case BIT_AND_EXPR:
359 return and_optab;
361 case BIT_IOR_EXPR:
362 return ior_optab;
364 case BIT_NOT_EXPR:
365 return one_cmpl_optab;
367 case BIT_XOR_EXPR:
368 return xor_optab;
370 case TRUNC_MOD_EXPR:
371 case CEIL_MOD_EXPR:
372 case FLOOR_MOD_EXPR:
373 case ROUND_MOD_EXPR:
374 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
376 case RDIV_EXPR:
377 case TRUNC_DIV_EXPR:
378 case CEIL_DIV_EXPR:
379 case FLOOR_DIV_EXPR:
380 case ROUND_DIV_EXPR:
381 case EXACT_DIV_EXPR:
382 if (TYPE_SATURATING(type))
383 return TYPE_UNSIGNED(type) ? usdiv_optab : ssdiv_optab;
384 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
386 case LSHIFT_EXPR:
387 if (TREE_CODE (type) == VECTOR_TYPE)
389 if (subtype == optab_vector)
390 return TYPE_SATURATING (type) ? NULL : vashl_optab;
392 gcc_assert (subtype == optab_scalar);
394 if (TYPE_SATURATING(type))
395 return TYPE_UNSIGNED(type) ? usashl_optab : ssashl_optab;
396 return ashl_optab;
398 case RSHIFT_EXPR:
399 if (TREE_CODE (type) == VECTOR_TYPE)
401 if (subtype == optab_vector)
402 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
404 gcc_assert (subtype == optab_scalar);
406 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
408 case LROTATE_EXPR:
409 if (TREE_CODE (type) == VECTOR_TYPE)
411 if (subtype == optab_vector)
412 return vrotl_optab;
414 gcc_assert (subtype == optab_scalar);
416 return rotl_optab;
418 case RROTATE_EXPR:
419 if (TREE_CODE (type) == VECTOR_TYPE)
421 if (subtype == optab_vector)
422 return vrotr_optab;
424 gcc_assert (subtype == optab_scalar);
426 return rotr_optab;
428 case MAX_EXPR:
429 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
431 case MIN_EXPR:
432 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
434 case REALIGN_LOAD_EXPR:
435 return vec_realign_load_optab;
437 case WIDEN_SUM_EXPR:
438 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
440 case DOT_PROD_EXPR:
441 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
443 case WIDEN_MULT_PLUS_EXPR:
444 return (TYPE_UNSIGNED (type)
445 ? (TYPE_SATURATING (type)
446 ? usmadd_widen_optab : umadd_widen_optab)
447 : (TYPE_SATURATING (type)
448 ? ssmadd_widen_optab : smadd_widen_optab));
450 case WIDEN_MULT_MINUS_EXPR:
451 return (TYPE_UNSIGNED (type)
452 ? (TYPE_SATURATING (type)
453 ? usmsub_widen_optab : umsub_widen_optab)
454 : (TYPE_SATURATING (type)
455 ? ssmsub_widen_optab : smsub_widen_optab));
457 case FMA_EXPR:
458 return fma_optab;
460 case REDUC_MAX_EXPR:
461 return TYPE_UNSIGNED (type) ? reduc_umax_optab : reduc_smax_optab;
463 case REDUC_MIN_EXPR:
464 return TYPE_UNSIGNED (type) ? reduc_umin_optab : reduc_smin_optab;
466 case REDUC_PLUS_EXPR:
467 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
469 case VEC_LSHIFT_EXPR:
470 return vec_shl_optab;
472 case VEC_RSHIFT_EXPR:
473 return vec_shr_optab;
475 case VEC_WIDEN_MULT_HI_EXPR:
476 return TYPE_UNSIGNED (type) ?
477 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
479 case VEC_WIDEN_MULT_LO_EXPR:
480 return TYPE_UNSIGNED (type) ?
481 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
483 case VEC_WIDEN_LSHIFT_HI_EXPR:
484 return TYPE_UNSIGNED (type) ?
485 vec_widen_ushiftl_hi_optab : vec_widen_sshiftl_hi_optab;
487 case VEC_WIDEN_LSHIFT_LO_EXPR:
488 return TYPE_UNSIGNED (type) ?
489 vec_widen_ushiftl_lo_optab : vec_widen_sshiftl_lo_optab;
491 case VEC_UNPACK_HI_EXPR:
492 return TYPE_UNSIGNED (type) ?
493 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
495 case VEC_UNPACK_LO_EXPR:
496 return TYPE_UNSIGNED (type) ?
497 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
499 case VEC_UNPACK_FLOAT_HI_EXPR:
500 /* The signedness is determined from input operand. */
501 return TYPE_UNSIGNED (type) ?
502 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
504 case VEC_UNPACK_FLOAT_LO_EXPR:
505 /* The signedness is determined from input operand. */
506 return TYPE_UNSIGNED (type) ?
507 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
509 case VEC_PACK_TRUNC_EXPR:
510 return vec_pack_trunc_optab;
512 case VEC_PACK_SAT_EXPR:
513 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
515 case VEC_PACK_FIX_TRUNC_EXPR:
516 /* The signedness is determined from output operand. */
517 return TYPE_UNSIGNED (type) ?
518 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
520 default:
521 break;
524 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
525 switch (code)
527 case POINTER_PLUS_EXPR:
528 case PLUS_EXPR:
529 if (TYPE_SATURATING(type))
530 return TYPE_UNSIGNED(type) ? usadd_optab : ssadd_optab;
531 return trapv ? addv_optab : add_optab;
533 case MINUS_EXPR:
534 if (TYPE_SATURATING(type))
535 return TYPE_UNSIGNED(type) ? ussub_optab : sssub_optab;
536 return trapv ? subv_optab : sub_optab;
538 case MULT_EXPR:
539 if (TYPE_SATURATING(type))
540 return TYPE_UNSIGNED(type) ? usmul_optab : ssmul_optab;
541 return trapv ? smulv_optab : smul_optab;
543 case NEGATE_EXPR:
544 if (TYPE_SATURATING(type))
545 return TYPE_UNSIGNED(type) ? usneg_optab : ssneg_optab;
546 return trapv ? negv_optab : neg_optab;
548 case ABS_EXPR:
549 return trapv ? absv_optab : abs_optab;
551 default:
552 return NULL;
557 /* Expand vector widening operations.
559 There are two different classes of operations handled here:
560 1) Operations whose result is wider than all the arguments to the operation.
561 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
562 In this case OP0 and optionally OP1 would be initialized,
563 but WIDE_OP wouldn't (not relevant for this case).
564 2) Operations whose result is of the same size as the last argument to the
565 operation, but wider than all the other arguments to the operation.
566 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
567 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
569 E.g, when called to expand the following operations, this is how
570 the arguments will be initialized:
571 nops OP0 OP1 WIDE_OP
572 widening-sum 2 oprnd0 - oprnd1
573 widening-dot-product 3 oprnd0 oprnd1 oprnd2
574 widening-mult 2 oprnd0 oprnd1 -
575 type-promotion (vec-unpack) 1 oprnd0 - - */
578 expand_widen_pattern_expr (sepops ops, rtx op0, rtx op1, rtx wide_op,
579 rtx target, int unsignedp)
581 struct expand_operand eops[4];
582 tree oprnd0, oprnd1, oprnd2;
583 enum machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
584 optab widen_pattern_optab;
585 enum insn_code icode;
586 int nops = TREE_CODE_LENGTH (ops->code);
587 int op;
589 oprnd0 = ops->op0;
590 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
591 widen_pattern_optab =
592 optab_for_tree_code (ops->code, TREE_TYPE (oprnd0), optab_default);
593 if (ops->code == WIDEN_MULT_PLUS_EXPR
594 || ops->code == WIDEN_MULT_MINUS_EXPR)
595 icode = find_widening_optab_handler (widen_pattern_optab,
596 TYPE_MODE (TREE_TYPE (ops->op2)),
597 tmode0, 0);
598 else
599 icode = optab_handler (widen_pattern_optab, tmode0);
600 gcc_assert (icode != CODE_FOR_nothing);
602 if (nops >= 2)
604 oprnd1 = ops->op1;
605 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
608 /* The last operand is of a wider mode than the rest of the operands. */
609 if (nops == 2)
610 wmode = tmode1;
611 else if (nops == 3)
613 gcc_assert (tmode1 == tmode0);
614 gcc_assert (op1);
615 oprnd2 = ops->op2;
616 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
619 op = 0;
620 create_output_operand (&eops[op++], target, TYPE_MODE (ops->type));
621 create_convert_operand_from (&eops[op++], op0, tmode0, unsignedp);
622 if (op1)
623 create_convert_operand_from (&eops[op++], op1, tmode1, unsignedp);
624 if (wide_op)
625 create_convert_operand_from (&eops[op++], wide_op, wmode, unsignedp);
626 expand_insn (icode, op, eops);
627 return eops[0].value;
630 /* Generate code to perform an operation specified by TERNARY_OPTAB
631 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
633 UNSIGNEDP is for the case where we have to widen the operands
634 to perform the operation. It says to use zero-extension.
636 If TARGET is nonzero, the value
637 is generated there, if it is convenient to do so.
638 In all cases an rtx is returned for the locus of the value;
639 this may or may not be TARGET. */
642 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
643 rtx op1, rtx op2, rtx target, int unsignedp)
645 struct expand_operand ops[4];
646 enum insn_code icode = optab_handler (ternary_optab, mode);
648 gcc_assert (optab_handler (ternary_optab, mode) != CODE_FOR_nothing);
650 create_output_operand (&ops[0], target, mode);
651 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
652 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
653 create_convert_operand_from (&ops[3], op2, mode, unsignedp);
654 expand_insn (icode, 4, ops);
655 return ops[0].value;
659 /* Like expand_binop, but return a constant rtx if the result can be
660 calculated at compile time. The arguments and return value are
661 otherwise the same as for expand_binop. */
664 simplify_expand_binop (enum machine_mode mode, optab binoptab,
665 rtx op0, rtx op1, rtx target, int unsignedp,
666 enum optab_methods methods)
668 if (CONSTANT_P (op0) && CONSTANT_P (op1))
670 rtx x = simplify_binary_operation (binoptab->code, mode, op0, op1);
672 if (x)
673 return x;
676 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
679 /* Like simplify_expand_binop, but always put the result in TARGET.
680 Return true if the expansion succeeded. */
682 bool
683 force_expand_binop (enum machine_mode mode, optab binoptab,
684 rtx op0, rtx op1, rtx target, int unsignedp,
685 enum optab_methods methods)
687 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
688 target, unsignedp, methods);
689 if (x == 0)
690 return false;
691 if (x != target)
692 emit_move_insn (target, x);
693 return true;
696 /* Generate insns for VEC_LSHIFT_EXPR, VEC_RSHIFT_EXPR. */
699 expand_vec_shift_expr (sepops ops, rtx target)
701 struct expand_operand eops[3];
702 enum insn_code icode;
703 rtx rtx_op1, rtx_op2;
704 enum machine_mode mode = TYPE_MODE (ops->type);
705 tree vec_oprnd = ops->op0;
706 tree shift_oprnd = ops->op1;
707 optab shift_optab;
709 switch (ops->code)
711 case VEC_RSHIFT_EXPR:
712 shift_optab = vec_shr_optab;
713 break;
714 case VEC_LSHIFT_EXPR:
715 shift_optab = vec_shl_optab;
716 break;
717 default:
718 gcc_unreachable ();
721 icode = optab_handler (shift_optab, mode);
722 gcc_assert (icode != CODE_FOR_nothing);
724 rtx_op1 = expand_normal (vec_oprnd);
725 rtx_op2 = expand_normal (shift_oprnd);
727 create_output_operand (&eops[0], target, mode);
728 create_input_operand (&eops[1], rtx_op1, GET_MODE (rtx_op1));
729 create_convert_operand_from_type (&eops[2], rtx_op2, TREE_TYPE (shift_oprnd));
730 expand_insn (icode, 3, eops);
732 return eops[0].value;
735 /* Create a new vector value in VMODE with all elements set to OP. The
736 mode of OP must be the element mode of VMODE. If OP is a constant,
737 then the return value will be a constant. */
739 static rtx
740 expand_vector_broadcast (enum machine_mode vmode, rtx op)
742 enum insn_code icode;
743 rtvec vec;
744 rtx ret;
745 int i, n;
747 gcc_checking_assert (VECTOR_MODE_P (vmode));
749 n = GET_MODE_NUNITS (vmode);
750 vec = rtvec_alloc (n);
751 for (i = 0; i < n; ++i)
752 RTVEC_ELT (vec, i) = op;
754 if (CONSTANT_P (op))
755 return gen_rtx_CONST_VECTOR (vmode, vec);
757 /* ??? If the target doesn't have a vec_init, then we have no easy way
758 of performing this operation. Most of this sort of generic support
759 is hidden away in the vector lowering support in gimple. */
760 icode = optab_handler (vec_init_optab, vmode);
761 if (icode == CODE_FOR_nothing)
762 return NULL;
764 ret = gen_reg_rtx (vmode);
765 emit_insn (GEN_FCN (icode) (ret, gen_rtx_PARALLEL (vmode, vec)));
767 return ret;
770 /* This subroutine of expand_doubleword_shift handles the cases in which
771 the effective shift value is >= BITS_PER_WORD. The arguments and return
772 value are the same as for the parent routine, except that SUPERWORD_OP1
773 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
774 INTO_TARGET may be null if the caller has decided to calculate it. */
776 static bool
777 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
778 rtx outof_target, rtx into_target,
779 int unsignedp, enum optab_methods methods)
781 if (into_target != 0)
782 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
783 into_target, unsignedp, methods))
784 return false;
786 if (outof_target != 0)
788 /* For a signed right shift, we must fill OUTOF_TARGET with copies
789 of the sign bit, otherwise we must fill it with zeros. */
790 if (binoptab != ashr_optab)
791 emit_move_insn (outof_target, CONST0_RTX (word_mode));
792 else
793 if (!force_expand_binop (word_mode, binoptab,
794 outof_input, GEN_INT (BITS_PER_WORD - 1),
795 outof_target, unsignedp, methods))
796 return false;
798 return true;
801 /* This subroutine of expand_doubleword_shift handles the cases in which
802 the effective shift value is < BITS_PER_WORD. The arguments and return
803 value are the same as for the parent routine. */
805 static bool
806 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
807 rtx outof_input, rtx into_input, rtx op1,
808 rtx outof_target, rtx into_target,
809 int unsignedp, enum optab_methods methods,
810 unsigned HOST_WIDE_INT shift_mask)
812 optab reverse_unsigned_shift, unsigned_shift;
813 rtx tmp, carries;
815 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
816 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
818 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
819 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
820 the opposite direction to BINOPTAB. */
821 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
823 carries = outof_input;
824 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
825 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
826 0, true, methods);
828 else
830 /* We must avoid shifting by BITS_PER_WORD bits since that is either
831 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
832 has unknown behavior. Do a single shift first, then shift by the
833 remainder. It's OK to use ~OP1 as the remainder if shift counts
834 are truncated to the mode size. */
835 carries = expand_binop (word_mode, reverse_unsigned_shift,
836 outof_input, const1_rtx, 0, unsignedp, methods);
837 if (shift_mask == BITS_PER_WORD - 1)
839 tmp = immed_double_const (-1, -1, op1_mode);
840 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
841 0, true, methods);
843 else
845 tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
846 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
847 0, true, methods);
850 if (tmp == 0 || carries == 0)
851 return false;
852 carries = expand_binop (word_mode, reverse_unsigned_shift,
853 carries, tmp, 0, unsignedp, methods);
854 if (carries == 0)
855 return false;
857 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
858 so the result can go directly into INTO_TARGET if convenient. */
859 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
860 into_target, unsignedp, methods);
861 if (tmp == 0)
862 return false;
864 /* Now OR in the bits carried over from OUTOF_INPUT. */
865 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
866 into_target, unsignedp, methods))
867 return false;
869 /* Use a standard word_mode shift for the out-of half. */
870 if (outof_target != 0)
871 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
872 outof_target, unsignedp, methods))
873 return false;
875 return true;
879 #ifdef HAVE_conditional_move
880 /* Try implementing expand_doubleword_shift using conditional moves.
881 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
882 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
883 are the shift counts to use in the former and latter case. All other
884 arguments are the same as the parent routine. */
886 static bool
887 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
888 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
889 rtx outof_input, rtx into_input,
890 rtx subword_op1, rtx superword_op1,
891 rtx outof_target, rtx into_target,
892 int unsignedp, enum optab_methods methods,
893 unsigned HOST_WIDE_INT shift_mask)
895 rtx outof_superword, into_superword;
897 /* Put the superword version of the output into OUTOF_SUPERWORD and
898 INTO_SUPERWORD. */
899 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
900 if (outof_target != 0 && subword_op1 == superword_op1)
902 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
903 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
904 into_superword = outof_target;
905 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
906 outof_superword, 0, unsignedp, methods))
907 return false;
909 else
911 into_superword = gen_reg_rtx (word_mode);
912 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
913 outof_superword, into_superword,
914 unsignedp, methods))
915 return false;
918 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
919 if (!expand_subword_shift (op1_mode, binoptab,
920 outof_input, into_input, subword_op1,
921 outof_target, into_target,
922 unsignedp, methods, shift_mask))
923 return false;
925 /* Select between them. Do the INTO half first because INTO_SUPERWORD
926 might be the current value of OUTOF_TARGET. */
927 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
928 into_target, into_superword, word_mode, false))
929 return false;
931 if (outof_target != 0)
932 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
933 outof_target, outof_superword,
934 word_mode, false))
935 return false;
937 return true;
939 #endif
941 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
942 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
943 input operand; the shift moves bits in the direction OUTOF_INPUT->
944 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
945 of the target. OP1 is the shift count and OP1_MODE is its mode.
946 If OP1 is constant, it will have been truncated as appropriate
947 and is known to be nonzero.
949 If SHIFT_MASK is zero, the result of word shifts is undefined when the
950 shift count is outside the range [0, BITS_PER_WORD). This routine must
951 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
953 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
954 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
955 fill with zeros or sign bits as appropriate.
957 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
958 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
959 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
960 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
961 are undefined.
963 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
964 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
965 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
966 function wants to calculate it itself.
968 Return true if the shift could be successfully synthesized. */
970 static bool
971 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
972 rtx outof_input, rtx into_input, rtx op1,
973 rtx outof_target, rtx into_target,
974 int unsignedp, enum optab_methods methods,
975 unsigned HOST_WIDE_INT shift_mask)
977 rtx superword_op1, tmp, cmp1, cmp2;
978 rtx subword_label, done_label;
979 enum rtx_code cmp_code;
981 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
982 fill the result with sign or zero bits as appropriate. If so, the value
983 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
984 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
985 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
987 This isn't worthwhile for constant shifts since the optimizers will
988 cope better with in-range shift counts. */
989 if (shift_mask >= BITS_PER_WORD
990 && outof_target != 0
991 && !CONSTANT_P (op1))
993 if (!expand_doubleword_shift (op1_mode, binoptab,
994 outof_input, into_input, op1,
995 0, into_target,
996 unsignedp, methods, shift_mask))
997 return false;
998 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
999 outof_target, unsignedp, methods))
1000 return false;
1001 return true;
1004 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1005 is true when the effective shift value is less than BITS_PER_WORD.
1006 Set SUPERWORD_OP1 to the shift count that should be used to shift
1007 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1008 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
1009 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1011 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1012 is a subword shift count. */
1013 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1014 0, true, methods);
1015 cmp2 = CONST0_RTX (op1_mode);
1016 cmp_code = EQ;
1017 superword_op1 = op1;
1019 else
1021 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1022 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1023 0, true, methods);
1024 cmp2 = CONST0_RTX (op1_mode);
1025 cmp_code = LT;
1026 superword_op1 = cmp1;
1028 if (cmp1 == 0)
1029 return false;
1031 /* If we can compute the condition at compile time, pick the
1032 appropriate subroutine. */
1033 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1034 if (tmp != 0 && CONST_INT_P (tmp))
1036 if (tmp == const0_rtx)
1037 return expand_superword_shift (binoptab, outof_input, superword_op1,
1038 outof_target, into_target,
1039 unsignedp, methods);
1040 else
1041 return expand_subword_shift (op1_mode, binoptab,
1042 outof_input, into_input, op1,
1043 outof_target, into_target,
1044 unsignedp, methods, shift_mask);
1047 #ifdef HAVE_conditional_move
1048 /* Try using conditional moves to generate straight-line code. */
1050 rtx start = get_last_insn ();
1051 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1052 cmp_code, cmp1, cmp2,
1053 outof_input, into_input,
1054 op1, superword_op1,
1055 outof_target, into_target,
1056 unsignedp, methods, shift_mask))
1057 return true;
1058 delete_insns_since (start);
1060 #endif
1062 /* As a last resort, use branches to select the correct alternative. */
1063 subword_label = gen_label_rtx ();
1064 done_label = gen_label_rtx ();
1066 NO_DEFER_POP;
1067 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1068 0, 0, subword_label, -1);
1069 OK_DEFER_POP;
1071 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1072 outof_target, into_target,
1073 unsignedp, methods))
1074 return false;
1076 emit_jump_insn (gen_jump (done_label));
1077 emit_barrier ();
1078 emit_label (subword_label);
1080 if (!expand_subword_shift (op1_mode, binoptab,
1081 outof_input, into_input, op1,
1082 outof_target, into_target,
1083 unsignedp, methods, shift_mask))
1084 return false;
1086 emit_label (done_label);
1087 return true;
1090 /* Subroutine of expand_binop. Perform a double word multiplication of
1091 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1092 as the target's word_mode. This function return NULL_RTX if anything
1093 goes wrong, in which case it may have already emitted instructions
1094 which need to be deleted.
1096 If we want to multiply two two-word values and have normal and widening
1097 multiplies of single-word values, we can do this with three smaller
1098 multiplications.
1100 The multiplication proceeds as follows:
1101 _______________________
1102 [__op0_high_|__op0_low__]
1103 _______________________
1104 * [__op1_high_|__op1_low__]
1105 _______________________________________________
1106 _______________________
1107 (1) [__op0_low__*__op1_low__]
1108 _______________________
1109 (2a) [__op0_low__*__op1_high_]
1110 _______________________
1111 (2b) [__op0_high_*__op1_low__]
1112 _______________________
1113 (3) [__op0_high_*__op1_high_]
1116 This gives a 4-word result. Since we are only interested in the
1117 lower 2 words, partial result (3) and the upper words of (2a) and
1118 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1119 calculated using non-widening multiplication.
1121 (1), however, needs to be calculated with an unsigned widening
1122 multiplication. If this operation is not directly supported we
1123 try using a signed widening multiplication and adjust the result.
1124 This adjustment works as follows:
1126 If both operands are positive then no adjustment is needed.
1128 If the operands have different signs, for example op0_low < 0 and
1129 op1_low >= 0, the instruction treats the most significant bit of
1130 op0_low as a sign bit instead of a bit with significance
1131 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1132 with 2**BITS_PER_WORD - op0_low, and two's complements the
1133 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1134 the result.
1136 Similarly, if both operands are negative, we need to add
1137 (op0_low + op1_low) * 2**BITS_PER_WORD.
1139 We use a trick to adjust quickly. We logically shift op0_low right
1140 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1141 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1142 logical shift exists, we do an arithmetic right shift and subtract
1143 the 0 or -1. */
1145 static rtx
1146 expand_doubleword_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
1147 bool umulp, enum optab_methods methods)
1149 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1150 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1151 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1152 rtx product, adjust, product_high, temp;
1154 rtx op0_high = operand_subword_force (op0, high, mode);
1155 rtx op0_low = operand_subword_force (op0, low, mode);
1156 rtx op1_high = operand_subword_force (op1, high, mode);
1157 rtx op1_low = operand_subword_force (op1, low, mode);
1159 /* If we're using an unsigned multiply to directly compute the product
1160 of the low-order words of the operands and perform any required
1161 adjustments of the operands, we begin by trying two more multiplications
1162 and then computing the appropriate sum.
1164 We have checked above that the required addition is provided.
1165 Full-word addition will normally always succeed, especially if
1166 it is provided at all, so we don't worry about its failure. The
1167 multiplication may well fail, however, so we do handle that. */
1169 if (!umulp)
1171 /* ??? This could be done with emit_store_flag where available. */
1172 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1173 NULL_RTX, 1, methods);
1174 if (temp)
1175 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1176 NULL_RTX, 0, OPTAB_DIRECT);
1177 else
1179 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1180 NULL_RTX, 0, methods);
1181 if (!temp)
1182 return NULL_RTX;
1183 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1184 NULL_RTX, 0, OPTAB_DIRECT);
1187 if (!op0_high)
1188 return NULL_RTX;
1191 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1192 NULL_RTX, 0, OPTAB_DIRECT);
1193 if (!adjust)
1194 return NULL_RTX;
1196 /* OP0_HIGH should now be dead. */
1198 if (!umulp)
1200 /* ??? This could be done with emit_store_flag where available. */
1201 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1202 NULL_RTX, 1, methods);
1203 if (temp)
1204 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1205 NULL_RTX, 0, OPTAB_DIRECT);
1206 else
1208 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1209 NULL_RTX, 0, methods);
1210 if (!temp)
1211 return NULL_RTX;
1212 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1213 NULL_RTX, 0, OPTAB_DIRECT);
1216 if (!op1_high)
1217 return NULL_RTX;
1220 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1221 NULL_RTX, 0, OPTAB_DIRECT);
1222 if (!temp)
1223 return NULL_RTX;
1225 /* OP1_HIGH should now be dead. */
1227 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1228 NULL_RTX, 0, OPTAB_DIRECT);
1230 if (target && !REG_P (target))
1231 target = NULL_RTX;
1233 if (umulp)
1234 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1235 target, 1, OPTAB_DIRECT);
1236 else
1237 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1238 target, 1, OPTAB_DIRECT);
1240 if (!product)
1241 return NULL_RTX;
1243 product_high = operand_subword (product, high, 1, mode);
1244 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1245 NULL_RTX, 0, OPTAB_DIRECT);
1246 emit_move_insn (product_high, adjust);
1247 return product;
1250 /* Wrapper around expand_binop which takes an rtx code to specify
1251 the operation to perform, not an optab pointer. All other
1252 arguments are the same. */
1254 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
1255 rtx op1, rtx target, int unsignedp,
1256 enum optab_methods methods)
1258 optab binop = code_to_optab[(int) code];
1259 gcc_assert (binop);
1261 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1264 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1265 binop. Order them according to commutative_operand_precedence and, if
1266 possible, try to put TARGET or a pseudo first. */
1267 static bool
1268 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1270 int op0_prec = commutative_operand_precedence (op0);
1271 int op1_prec = commutative_operand_precedence (op1);
1273 if (op0_prec < op1_prec)
1274 return true;
1276 if (op0_prec > op1_prec)
1277 return false;
1279 /* With equal precedence, both orders are ok, but it is better if the
1280 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1281 if (target == 0 || REG_P (target))
1282 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1283 else
1284 return rtx_equal_p (op1, target);
1287 /* Return true if BINOPTAB implements a shift operation. */
1289 static bool
1290 shift_optab_p (optab binoptab)
1292 switch (binoptab->code)
1294 case ASHIFT:
1295 case SS_ASHIFT:
1296 case US_ASHIFT:
1297 case ASHIFTRT:
1298 case LSHIFTRT:
1299 case ROTATE:
1300 case ROTATERT:
1301 return true;
1303 default:
1304 return false;
1308 /* Return true if BINOPTAB implements a commutative binary operation. */
1310 static bool
1311 commutative_optab_p (optab binoptab)
1313 return (GET_RTX_CLASS (binoptab->code) == RTX_COMM_ARITH
1314 || binoptab == smul_widen_optab
1315 || binoptab == umul_widen_optab
1316 || binoptab == smul_highpart_optab
1317 || binoptab == umul_highpart_optab);
1320 /* X is to be used in mode MODE as operand OPN to BINOPTAB. If we're
1321 optimizing, and if the operand is a constant that costs more than
1322 1 instruction, force the constant into a register and return that
1323 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1325 static rtx
1326 avoid_expensive_constant (enum machine_mode mode, optab binoptab,
1327 int opn, rtx x, bool unsignedp)
1329 bool speed = optimize_insn_for_speed_p ();
1331 if (mode != VOIDmode
1332 && optimize
1333 && CONSTANT_P (x)
1334 && rtx_cost (x, binoptab->code, opn, speed) > set_src_cost (x, speed))
1336 if (CONST_INT_P (x))
1338 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1339 if (intval != INTVAL (x))
1340 x = GEN_INT (intval);
1342 else
1343 x = convert_modes (mode, VOIDmode, x, unsignedp);
1344 x = force_reg (mode, x);
1346 return x;
1349 /* Helper function for expand_binop: handle the case where there
1350 is an insn that directly implements the indicated operation.
1351 Returns null if this is not possible. */
1352 static rtx
1353 expand_binop_directly (enum machine_mode mode, optab binoptab,
1354 rtx op0, rtx op1,
1355 rtx target, int unsignedp, enum optab_methods methods,
1356 rtx last)
1358 enum machine_mode from_mode = widened_mode (mode, op0, op1);
1359 enum insn_code icode = find_widening_optab_handler (binoptab, mode,
1360 from_mode, 1);
1361 enum machine_mode xmode0 = insn_data[(int) icode].operand[1].mode;
1362 enum machine_mode xmode1 = insn_data[(int) icode].operand[2].mode;
1363 enum machine_mode mode0, mode1, tmp_mode;
1364 struct expand_operand ops[3];
1365 bool commutative_p;
1366 rtx pat;
1367 rtx xop0 = op0, xop1 = op1;
1368 rtx swap;
1370 /* If it is a commutative operator and the modes would match
1371 if we would swap the operands, we can save the conversions. */
1372 commutative_p = commutative_optab_p (binoptab);
1373 if (commutative_p
1374 && GET_MODE (xop0) != xmode0 && GET_MODE (xop1) != xmode1
1375 && GET_MODE (xop0) == xmode1 && GET_MODE (xop1) == xmode1)
1377 swap = xop0;
1378 xop0 = xop1;
1379 xop1 = swap;
1382 /* If we are optimizing, force expensive constants into a register. */
1383 xop0 = avoid_expensive_constant (xmode0, binoptab, 0, xop0, unsignedp);
1384 if (!shift_optab_p (binoptab))
1385 xop1 = avoid_expensive_constant (xmode1, binoptab, 1, xop1, unsignedp);
1387 /* In case the insn wants input operands in modes different from
1388 those of the actual operands, convert the operands. It would
1389 seem that we don't need to convert CONST_INTs, but we do, so
1390 that they're properly zero-extended, sign-extended or truncated
1391 for their mode. */
1393 mode0 = GET_MODE (xop0) != VOIDmode ? GET_MODE (xop0) : mode;
1394 if (xmode0 != VOIDmode && xmode0 != mode0)
1396 xop0 = convert_modes (xmode0, mode0, xop0, unsignedp);
1397 mode0 = xmode0;
1400 mode1 = GET_MODE (xop1) != VOIDmode ? GET_MODE (xop1) : mode;
1401 if (xmode1 != VOIDmode && xmode1 != mode1)
1403 xop1 = convert_modes (xmode1, mode1, xop1, unsignedp);
1404 mode1 = xmode1;
1407 /* If operation is commutative,
1408 try to make the first operand a register.
1409 Even better, try to make it the same as the target.
1410 Also try to make the last operand a constant. */
1411 if (commutative_p
1412 && swap_commutative_operands_with_target (target, xop0, xop1))
1414 swap = xop1;
1415 xop1 = xop0;
1416 xop0 = swap;
1419 /* Now, if insn's predicates don't allow our operands, put them into
1420 pseudo regs. */
1422 if (binoptab == vec_pack_trunc_optab
1423 || binoptab == vec_pack_usat_optab
1424 || binoptab == vec_pack_ssat_optab
1425 || binoptab == vec_pack_ufix_trunc_optab
1426 || binoptab == vec_pack_sfix_trunc_optab)
1428 /* The mode of the result is different then the mode of the
1429 arguments. */
1430 tmp_mode = insn_data[(int) icode].operand[0].mode;
1431 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1433 delete_insns_since (last);
1434 return NULL_RTX;
1437 else
1438 tmp_mode = mode;
1440 create_output_operand (&ops[0], target, tmp_mode);
1441 create_input_operand (&ops[1], xop0, mode0);
1442 create_input_operand (&ops[2], xop1, mode1);
1443 pat = maybe_gen_insn (icode, 3, ops);
1444 if (pat)
1446 /* If PAT is composed of more than one insn, try to add an appropriate
1447 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1448 operand, call expand_binop again, this time without a target. */
1449 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1450 && ! add_equal_note (pat, ops[0].value, binoptab->code,
1451 ops[1].value, ops[2].value))
1453 delete_insns_since (last);
1454 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1455 unsignedp, methods);
1458 emit_insn (pat);
1459 return ops[0].value;
1461 delete_insns_since (last);
1462 return NULL_RTX;
1465 /* Generate code to perform an operation specified by BINOPTAB
1466 on operands OP0 and OP1, with result having machine-mode MODE.
1468 UNSIGNEDP is for the case where we have to widen the operands
1469 to perform the operation. It says to use zero-extension.
1471 If TARGET is nonzero, the value
1472 is generated there, if it is convenient to do so.
1473 In all cases an rtx is returned for the locus of the value;
1474 this may or may not be TARGET. */
1477 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
1478 rtx target, int unsignedp, enum optab_methods methods)
1480 enum optab_methods next_methods
1481 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1482 ? OPTAB_WIDEN : methods);
1483 enum mode_class mclass;
1484 enum machine_mode wider_mode;
1485 rtx libfunc;
1486 rtx temp;
1487 rtx entry_last = get_last_insn ();
1488 rtx last;
1490 mclass = GET_MODE_CLASS (mode);
1492 /* If subtracting an integer constant, convert this into an addition of
1493 the negated constant. */
1495 if (binoptab == sub_optab && CONST_INT_P (op1))
1497 op1 = negate_rtx (mode, op1);
1498 binoptab = add_optab;
1501 /* Record where to delete back to if we backtrack. */
1502 last = get_last_insn ();
1504 /* If we can do it with a three-operand insn, do so. */
1506 if (methods != OPTAB_MUST_WIDEN
1507 && find_widening_optab_handler (binoptab, mode,
1508 widened_mode (mode, op0, op1), 1)
1509 != CODE_FOR_nothing)
1511 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1512 unsignedp, methods, last);
1513 if (temp)
1514 return temp;
1517 /* If we were trying to rotate, and that didn't work, try rotating
1518 the other direction before falling back to shifts and bitwise-or. */
1519 if (((binoptab == rotl_optab
1520 && optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
1521 || (binoptab == rotr_optab
1522 && optab_handler (rotl_optab, mode) != CODE_FOR_nothing))
1523 && mclass == MODE_INT)
1525 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1526 rtx newop1;
1527 unsigned int bits = GET_MODE_PRECISION (mode);
1529 if (CONST_INT_P (op1))
1530 newop1 = GEN_INT (bits - INTVAL (op1));
1531 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1532 newop1 = negate_rtx (GET_MODE (op1), op1);
1533 else
1534 newop1 = expand_binop (GET_MODE (op1), sub_optab,
1535 GEN_INT (bits), op1,
1536 NULL_RTX, unsignedp, OPTAB_DIRECT);
1538 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1539 target, unsignedp, methods, last);
1540 if (temp)
1541 return temp;
1544 /* If this is a multiply, see if we can do a widening operation that
1545 takes operands of this mode and makes a wider mode. */
1547 if (binoptab == smul_optab
1548 && GET_MODE_2XWIDER_MODE (mode) != VOIDmode
1549 && (widening_optab_handler ((unsignedp ? umul_widen_optab
1550 : smul_widen_optab),
1551 GET_MODE_2XWIDER_MODE (mode), mode)
1552 != CODE_FOR_nothing))
1554 temp = expand_binop (GET_MODE_2XWIDER_MODE (mode),
1555 unsignedp ? umul_widen_optab : smul_widen_optab,
1556 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1558 if (temp != 0)
1560 if (GET_MODE_CLASS (mode) == MODE_INT
1561 && TRULY_NOOP_TRUNCATION_MODES_P (mode, GET_MODE (temp)))
1562 return gen_lowpart (mode, temp);
1563 else
1564 return convert_to_mode (mode, temp, unsignedp);
1568 /* If this is a vector shift by a scalar, see if we can do a vector
1569 shift by a vector. If so, broadcast the scalar into a vector. */
1570 if (mclass == MODE_VECTOR_INT)
1572 optab otheroptab = NULL;
1574 if (binoptab == ashl_optab)
1575 otheroptab = vashl_optab;
1576 else if (binoptab == ashr_optab)
1577 otheroptab = vashr_optab;
1578 else if (binoptab == lshr_optab)
1579 otheroptab = vlshr_optab;
1580 else if (binoptab == rotl_optab)
1581 otheroptab = vrotl_optab;
1582 else if (binoptab == rotr_optab)
1583 otheroptab = vrotr_optab;
1585 if (otheroptab && optab_handler (otheroptab, mode) != CODE_FOR_nothing)
1587 rtx vop1 = expand_vector_broadcast (mode, op1);
1588 if (vop1)
1590 temp = expand_binop_directly (mode, otheroptab, op0, vop1,
1591 target, unsignedp, methods, last);
1592 if (temp)
1593 return temp;
1598 /* Look for a wider mode of the same class for which we think we
1599 can open-code the operation. Check for a widening multiply at the
1600 wider mode as well. */
1602 if (CLASS_HAS_WIDER_MODES_P (mclass)
1603 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1604 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1605 wider_mode != VOIDmode;
1606 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1608 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing
1609 || (binoptab == smul_optab
1610 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1611 && (find_widening_optab_handler ((unsignedp
1612 ? umul_widen_optab
1613 : smul_widen_optab),
1614 GET_MODE_WIDER_MODE (wider_mode),
1615 mode, 0)
1616 != CODE_FOR_nothing)))
1618 rtx xop0 = op0, xop1 = op1;
1619 int no_extend = 0;
1621 /* For certain integer operations, we need not actually extend
1622 the narrow operands, as long as we will truncate
1623 the results to the same narrowness. */
1625 if ((binoptab == ior_optab || binoptab == and_optab
1626 || binoptab == xor_optab
1627 || binoptab == add_optab || binoptab == sub_optab
1628 || binoptab == smul_optab || binoptab == ashl_optab)
1629 && mclass == MODE_INT)
1631 no_extend = 1;
1632 xop0 = avoid_expensive_constant (mode, binoptab, 0,
1633 xop0, unsignedp);
1634 if (binoptab != ashl_optab)
1635 xop1 = avoid_expensive_constant (mode, binoptab, 1,
1636 xop1, unsignedp);
1639 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1641 /* The second operand of a shift must always be extended. */
1642 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1643 no_extend && binoptab != ashl_optab);
1645 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1646 unsignedp, OPTAB_DIRECT);
1647 if (temp)
1649 if (mclass != MODE_INT
1650 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
1652 if (target == 0)
1653 target = gen_reg_rtx (mode);
1654 convert_move (target, temp, 0);
1655 return target;
1657 else
1658 return gen_lowpart (mode, temp);
1660 else
1661 delete_insns_since (last);
1665 /* If operation is commutative,
1666 try to make the first operand a register.
1667 Even better, try to make it the same as the target.
1668 Also try to make the last operand a constant. */
1669 if (commutative_optab_p (binoptab)
1670 && swap_commutative_operands_with_target (target, op0, op1))
1672 temp = op1;
1673 op1 = op0;
1674 op0 = temp;
1677 /* These can be done a word at a time. */
1678 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1679 && mclass == MODE_INT
1680 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1681 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1683 int i;
1684 rtx insns;
1686 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1687 won't be accurate, so use a new target. */
1688 if (target == 0
1689 || target == op0
1690 || target == op1
1691 || !valid_multiword_target_p (target))
1692 target = gen_reg_rtx (mode);
1694 start_sequence ();
1696 /* Do the actual arithmetic. */
1697 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1699 rtx target_piece = operand_subword (target, i, 1, mode);
1700 rtx x = expand_binop (word_mode, binoptab,
1701 operand_subword_force (op0, i, mode),
1702 operand_subword_force (op1, i, mode),
1703 target_piece, unsignedp, next_methods);
1705 if (x == 0)
1706 break;
1708 if (target_piece != x)
1709 emit_move_insn (target_piece, x);
1712 insns = get_insns ();
1713 end_sequence ();
1715 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1717 emit_insn (insns);
1718 return target;
1722 /* Synthesize double word shifts from single word shifts. */
1723 if ((binoptab == lshr_optab || binoptab == ashl_optab
1724 || binoptab == ashr_optab)
1725 && mclass == MODE_INT
1726 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1727 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1728 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode)
1729 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing
1730 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1731 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1733 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1734 enum machine_mode op1_mode;
1736 double_shift_mask = targetm.shift_truncation_mask (mode);
1737 shift_mask = targetm.shift_truncation_mask (word_mode);
1738 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1740 /* Apply the truncation to constant shifts. */
1741 if (double_shift_mask > 0 && CONST_INT_P (op1))
1742 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1744 if (op1 == CONST0_RTX (op1_mode))
1745 return op0;
1747 /* Make sure that this is a combination that expand_doubleword_shift
1748 can handle. See the comments there for details. */
1749 if (double_shift_mask == 0
1750 || (shift_mask == BITS_PER_WORD - 1
1751 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1753 rtx insns;
1754 rtx into_target, outof_target;
1755 rtx into_input, outof_input;
1756 int left_shift, outof_word;
1758 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1759 won't be accurate, so use a new target. */
1760 if (target == 0
1761 || target == op0
1762 || target == op1
1763 || !valid_multiword_target_p (target))
1764 target = gen_reg_rtx (mode);
1766 start_sequence ();
1768 /* OUTOF_* is the word we are shifting bits away from, and
1769 INTO_* is the word that we are shifting bits towards, thus
1770 they differ depending on the direction of the shift and
1771 WORDS_BIG_ENDIAN. */
1773 left_shift = binoptab == ashl_optab;
1774 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1776 outof_target = operand_subword (target, outof_word, 1, mode);
1777 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1779 outof_input = operand_subword_force (op0, outof_word, mode);
1780 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1782 if (expand_doubleword_shift (op1_mode, binoptab,
1783 outof_input, into_input, op1,
1784 outof_target, into_target,
1785 unsignedp, next_methods, shift_mask))
1787 insns = get_insns ();
1788 end_sequence ();
1790 emit_insn (insns);
1791 return target;
1793 end_sequence ();
1797 /* Synthesize double word rotates from single word shifts. */
1798 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1799 && mclass == MODE_INT
1800 && CONST_INT_P (op1)
1801 && GET_MODE_PRECISION (mode) == 2 * BITS_PER_WORD
1802 && optab_handler (ashl_optab, word_mode) != CODE_FOR_nothing
1803 && optab_handler (lshr_optab, word_mode) != CODE_FOR_nothing)
1805 rtx insns;
1806 rtx into_target, outof_target;
1807 rtx into_input, outof_input;
1808 rtx inter;
1809 int shift_count, left_shift, outof_word;
1811 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1812 won't be accurate, so use a new target. Do this also if target is not
1813 a REG, first because having a register instead may open optimization
1814 opportunities, and second because if target and op0 happen to be MEMs
1815 designating the same location, we would risk clobbering it too early
1816 in the code sequence we generate below. */
1817 if (target == 0
1818 || target == op0
1819 || target == op1
1820 || !REG_P (target)
1821 || !valid_multiword_target_p (target))
1822 target = gen_reg_rtx (mode);
1824 start_sequence ();
1826 shift_count = INTVAL (op1);
1828 /* OUTOF_* is the word we are shifting bits away from, and
1829 INTO_* is the word that we are shifting bits towards, thus
1830 they differ depending on the direction of the shift and
1831 WORDS_BIG_ENDIAN. */
1833 left_shift = (binoptab == rotl_optab);
1834 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1836 outof_target = operand_subword (target, outof_word, 1, mode);
1837 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1839 outof_input = operand_subword_force (op0, outof_word, mode);
1840 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1842 if (shift_count == BITS_PER_WORD)
1844 /* This is just a word swap. */
1845 emit_move_insn (outof_target, into_input);
1846 emit_move_insn (into_target, outof_input);
1847 inter = const0_rtx;
1849 else
1851 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1852 rtx first_shift_count, second_shift_count;
1853 optab reverse_unsigned_shift, unsigned_shift;
1855 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1856 ? lshr_optab : ashl_optab);
1858 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1859 ? ashl_optab : lshr_optab);
1861 if (shift_count > BITS_PER_WORD)
1863 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1864 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1866 else
1868 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1869 second_shift_count = GEN_INT (shift_count);
1872 into_temp1 = expand_binop (word_mode, unsigned_shift,
1873 outof_input, first_shift_count,
1874 NULL_RTX, unsignedp, next_methods);
1875 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1876 into_input, second_shift_count,
1877 NULL_RTX, unsignedp, next_methods);
1879 if (into_temp1 != 0 && into_temp2 != 0)
1880 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1881 into_target, unsignedp, next_methods);
1882 else
1883 inter = 0;
1885 if (inter != 0 && inter != into_target)
1886 emit_move_insn (into_target, inter);
1888 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1889 into_input, first_shift_count,
1890 NULL_RTX, unsignedp, next_methods);
1891 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1892 outof_input, second_shift_count,
1893 NULL_RTX, unsignedp, next_methods);
1895 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1896 inter = expand_binop (word_mode, ior_optab,
1897 outof_temp1, outof_temp2,
1898 outof_target, unsignedp, next_methods);
1900 if (inter != 0 && inter != outof_target)
1901 emit_move_insn (outof_target, inter);
1904 insns = get_insns ();
1905 end_sequence ();
1907 if (inter != 0)
1909 emit_insn (insns);
1910 return target;
1914 /* These can be done a word at a time by propagating carries. */
1915 if ((binoptab == add_optab || binoptab == sub_optab)
1916 && mclass == MODE_INT
1917 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1918 && optab_handler (binoptab, word_mode) != CODE_FOR_nothing)
1920 unsigned int i;
1921 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1922 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1923 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1924 rtx xop0, xop1, xtarget;
1926 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1927 value is one of those, use it. Otherwise, use 1 since it is the
1928 one easiest to get. */
1929 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1930 int normalizep = STORE_FLAG_VALUE;
1931 #else
1932 int normalizep = 1;
1933 #endif
1935 /* Prepare the operands. */
1936 xop0 = force_reg (mode, op0);
1937 xop1 = force_reg (mode, op1);
1939 xtarget = gen_reg_rtx (mode);
1941 if (target == 0 || !REG_P (target) || !valid_multiword_target_p (target))
1942 target = xtarget;
1944 /* Indicate for flow that the entire target reg is being set. */
1945 if (REG_P (target))
1946 emit_clobber (xtarget);
1948 /* Do the actual arithmetic. */
1949 for (i = 0; i < nwords; i++)
1951 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1952 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1953 rtx op0_piece = operand_subword_force (xop0, index, mode);
1954 rtx op1_piece = operand_subword_force (xop1, index, mode);
1955 rtx x;
1957 /* Main add/subtract of the input operands. */
1958 x = expand_binop (word_mode, binoptab,
1959 op0_piece, op1_piece,
1960 target_piece, unsignedp, next_methods);
1961 if (x == 0)
1962 break;
1964 if (i + 1 < nwords)
1966 /* Store carry from main add/subtract. */
1967 carry_out = gen_reg_rtx (word_mode);
1968 carry_out = emit_store_flag_force (carry_out,
1969 (binoptab == add_optab
1970 ? LT : GT),
1971 x, op0_piece,
1972 word_mode, 1, normalizep);
1975 if (i > 0)
1977 rtx newx;
1979 /* Add/subtract previous carry to main result. */
1980 newx = expand_binop (word_mode,
1981 normalizep == 1 ? binoptab : otheroptab,
1982 x, carry_in,
1983 NULL_RTX, 1, next_methods);
1985 if (i + 1 < nwords)
1987 /* Get out carry from adding/subtracting carry in. */
1988 rtx carry_tmp = gen_reg_rtx (word_mode);
1989 carry_tmp = emit_store_flag_force (carry_tmp,
1990 (binoptab == add_optab
1991 ? LT : GT),
1992 newx, x,
1993 word_mode, 1, normalizep);
1995 /* Logical-ior the two poss. carry together. */
1996 carry_out = expand_binop (word_mode, ior_optab,
1997 carry_out, carry_tmp,
1998 carry_out, 0, next_methods);
1999 if (carry_out == 0)
2000 break;
2002 emit_move_insn (target_piece, newx);
2004 else
2006 if (x != target_piece)
2007 emit_move_insn (target_piece, x);
2010 carry_in = carry_out;
2013 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2015 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing
2016 || ! rtx_equal_p (target, xtarget))
2018 rtx temp = emit_move_insn (target, xtarget);
2020 set_dst_reg_note (temp, REG_EQUAL,
2021 gen_rtx_fmt_ee (binoptab->code, mode,
2022 copy_rtx (xop0),
2023 copy_rtx (xop1)),
2024 target);
2026 else
2027 target = xtarget;
2029 return target;
2032 else
2033 delete_insns_since (last);
2036 /* Attempt to synthesize double word multiplies using a sequence of word
2037 mode multiplications. We first attempt to generate a sequence using a
2038 more efficient unsigned widening multiply, and if that fails we then
2039 try using a signed widening multiply. */
2041 if (binoptab == smul_optab
2042 && mclass == MODE_INT
2043 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2044 && optab_handler (smul_optab, word_mode) != CODE_FOR_nothing
2045 && optab_handler (add_optab, word_mode) != CODE_FOR_nothing)
2047 rtx product = NULL_RTX;
2048 if (widening_optab_handler (umul_widen_optab, mode, word_mode)
2049 != CODE_FOR_nothing)
2051 product = expand_doubleword_mult (mode, op0, op1, target,
2052 true, methods);
2053 if (!product)
2054 delete_insns_since (last);
2057 if (product == NULL_RTX
2058 && widening_optab_handler (smul_widen_optab, mode, word_mode)
2059 != CODE_FOR_nothing)
2061 product = expand_doubleword_mult (mode, op0, op1, target,
2062 false, methods);
2063 if (!product)
2064 delete_insns_since (last);
2067 if (product != NULL_RTX)
2069 if (optab_handler (mov_optab, mode) != CODE_FOR_nothing)
2071 temp = emit_move_insn (target ? target : product, product);
2072 set_dst_reg_note (temp,
2073 REG_EQUAL,
2074 gen_rtx_fmt_ee (MULT, mode,
2075 copy_rtx (op0),
2076 copy_rtx (op1)),
2077 target ? target : product);
2079 return product;
2083 /* It can't be open-coded in this mode.
2084 Use a library call if one is available and caller says that's ok. */
2086 libfunc = optab_libfunc (binoptab, mode);
2087 if (libfunc
2088 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2090 rtx insns;
2091 rtx op1x = op1;
2092 enum machine_mode op1_mode = mode;
2093 rtx value;
2095 start_sequence ();
2097 if (shift_optab_p (binoptab))
2099 op1_mode = targetm.libgcc_shift_count_mode ();
2100 /* Specify unsigned here,
2101 since negative shift counts are meaningless. */
2102 op1x = convert_to_mode (op1_mode, op1, 1);
2105 if (GET_MODE (op0) != VOIDmode
2106 && GET_MODE (op0) != mode)
2107 op0 = convert_to_mode (mode, op0, unsignedp);
2109 /* Pass 1 for NO_QUEUE so we don't lose any increments
2110 if the libcall is cse'd or moved. */
2111 value = emit_library_call_value (libfunc,
2112 NULL_RTX, LCT_CONST, mode, 2,
2113 op0, mode, op1x, op1_mode);
2115 insns = get_insns ();
2116 end_sequence ();
2118 target = gen_reg_rtx (mode);
2119 emit_libcall_block_1 (insns, target, value,
2120 gen_rtx_fmt_ee (binoptab->code, mode, op0, op1),
2121 trapv_binoptab_p (binoptab));
2123 return target;
2126 delete_insns_since (last);
2128 /* It can't be done in this mode. Can we do it in a wider mode? */
2130 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2131 || methods == OPTAB_MUST_WIDEN))
2133 /* Caller says, don't even try. */
2134 delete_insns_since (entry_last);
2135 return 0;
2138 /* Compute the value of METHODS to pass to recursive calls.
2139 Don't allow widening to be tried recursively. */
2141 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2143 /* Look for a wider mode of the same class for which it appears we can do
2144 the operation. */
2146 if (CLASS_HAS_WIDER_MODES_P (mclass))
2148 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2149 wider_mode != VOIDmode;
2150 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2152 if (find_widening_optab_handler (binoptab, wider_mode, mode, 1)
2153 != CODE_FOR_nothing
2154 || (methods == OPTAB_LIB
2155 && optab_libfunc (binoptab, wider_mode)))
2157 rtx xop0 = op0, xop1 = op1;
2158 int no_extend = 0;
2160 /* For certain integer operations, we need not actually extend
2161 the narrow operands, as long as we will truncate
2162 the results to the same narrowness. */
2164 if ((binoptab == ior_optab || binoptab == and_optab
2165 || binoptab == xor_optab
2166 || binoptab == add_optab || binoptab == sub_optab
2167 || binoptab == smul_optab || binoptab == ashl_optab)
2168 && mclass == MODE_INT)
2169 no_extend = 1;
2171 xop0 = widen_operand (xop0, wider_mode, mode,
2172 unsignedp, no_extend);
2174 /* The second operand of a shift must always be extended. */
2175 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2176 no_extend && binoptab != ashl_optab);
2178 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2179 unsignedp, methods);
2180 if (temp)
2182 if (mclass != MODE_INT
2183 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
2185 if (target == 0)
2186 target = gen_reg_rtx (mode);
2187 convert_move (target, temp, 0);
2188 return target;
2190 else
2191 return gen_lowpart (mode, temp);
2193 else
2194 delete_insns_since (last);
2199 delete_insns_since (entry_last);
2200 return 0;
2203 /* Expand a binary operator which has both signed and unsigned forms.
2204 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2205 signed operations.
2207 If we widen unsigned operands, we may use a signed wider operation instead
2208 of an unsigned wider operation, since the result would be the same. */
2211 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
2212 rtx op0, rtx op1, rtx target, int unsignedp,
2213 enum optab_methods methods)
2215 rtx temp;
2216 optab direct_optab = unsignedp ? uoptab : soptab;
2217 struct optab_d wide_soptab;
2219 /* Do it without widening, if possible. */
2220 temp = expand_binop (mode, direct_optab, op0, op1, target,
2221 unsignedp, OPTAB_DIRECT);
2222 if (temp || methods == OPTAB_DIRECT)
2223 return temp;
2225 /* Try widening to a signed int. Make a fake signed optab that
2226 hides any signed insn for direct use. */
2227 wide_soptab = *soptab;
2228 set_optab_handler (&wide_soptab, mode, CODE_FOR_nothing);
2229 /* We don't want to generate new hash table entries from this fake
2230 optab. */
2231 wide_soptab.libcall_gen = NULL;
2233 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2234 unsignedp, OPTAB_WIDEN);
2236 /* For unsigned operands, try widening to an unsigned int. */
2237 if (temp == 0 && unsignedp)
2238 temp = expand_binop (mode, uoptab, op0, op1, target,
2239 unsignedp, OPTAB_WIDEN);
2240 if (temp || methods == OPTAB_WIDEN)
2241 return temp;
2243 /* Use the right width libcall if that exists. */
2244 temp = expand_binop (mode, direct_optab, op0, op1, target, unsignedp, OPTAB_LIB);
2245 if (temp || methods == OPTAB_LIB)
2246 return temp;
2248 /* Must widen and use a libcall, use either signed or unsigned. */
2249 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2250 unsignedp, methods);
2251 if (temp != 0)
2252 return temp;
2253 if (unsignedp)
2254 return expand_binop (mode, uoptab, op0, op1, target,
2255 unsignedp, methods);
2256 return 0;
2259 /* Generate code to perform an operation specified by UNOPPTAB
2260 on operand OP0, with two results to TARG0 and TARG1.
2261 We assume that the order of the operands for the instruction
2262 is TARG0, TARG1, OP0.
2264 Either TARG0 or TARG1 may be zero, but what that means is that
2265 the result is not actually wanted. We will generate it into
2266 a dummy pseudo-reg and discard it. They may not both be zero.
2268 Returns 1 if this operation can be performed; 0 if not. */
2271 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2272 int unsignedp)
2274 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2275 enum mode_class mclass;
2276 enum machine_mode wider_mode;
2277 rtx entry_last = get_last_insn ();
2278 rtx last;
2280 mclass = GET_MODE_CLASS (mode);
2282 if (!targ0)
2283 targ0 = gen_reg_rtx (mode);
2284 if (!targ1)
2285 targ1 = gen_reg_rtx (mode);
2287 /* Record where to go back to if we fail. */
2288 last = get_last_insn ();
2290 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2292 struct expand_operand ops[3];
2293 enum insn_code icode = optab_handler (unoptab, mode);
2295 create_fixed_operand (&ops[0], targ0);
2296 create_fixed_operand (&ops[1], targ1);
2297 create_convert_operand_from (&ops[2], op0, mode, unsignedp);
2298 if (maybe_expand_insn (icode, 3, ops))
2299 return 1;
2302 /* It can't be done in this mode. Can we do it in a wider mode? */
2304 if (CLASS_HAS_WIDER_MODES_P (mclass))
2306 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2307 wider_mode != VOIDmode;
2308 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2310 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2312 rtx t0 = gen_reg_rtx (wider_mode);
2313 rtx t1 = gen_reg_rtx (wider_mode);
2314 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2316 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2318 convert_move (targ0, t0, unsignedp);
2319 convert_move (targ1, t1, unsignedp);
2320 return 1;
2322 else
2323 delete_insns_since (last);
2328 delete_insns_since (entry_last);
2329 return 0;
2332 /* Generate code to perform an operation specified by BINOPTAB
2333 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2334 We assume that the order of the operands for the instruction
2335 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2336 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2338 Either TARG0 or TARG1 may be zero, but what that means is that
2339 the result is not actually wanted. We will generate it into
2340 a dummy pseudo-reg and discard it. They may not both be zero.
2342 Returns 1 if this operation can be performed; 0 if not. */
2345 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2346 int unsignedp)
2348 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2349 enum mode_class mclass;
2350 enum machine_mode wider_mode;
2351 rtx entry_last = get_last_insn ();
2352 rtx last;
2354 mclass = GET_MODE_CLASS (mode);
2356 if (!targ0)
2357 targ0 = gen_reg_rtx (mode);
2358 if (!targ1)
2359 targ1 = gen_reg_rtx (mode);
2361 /* Record where to go back to if we fail. */
2362 last = get_last_insn ();
2364 if (optab_handler (binoptab, mode) != CODE_FOR_nothing)
2366 struct expand_operand ops[4];
2367 enum insn_code icode = optab_handler (binoptab, mode);
2368 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2369 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
2370 rtx xop0 = op0, xop1 = op1;
2372 /* If we are optimizing, force expensive constants into a register. */
2373 xop0 = avoid_expensive_constant (mode0, binoptab, 0, xop0, unsignedp);
2374 xop1 = avoid_expensive_constant (mode1, binoptab, 1, xop1, unsignedp);
2376 create_fixed_operand (&ops[0], targ0);
2377 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2378 create_convert_operand_from (&ops[2], op1, mode, unsignedp);
2379 create_fixed_operand (&ops[3], targ1);
2380 if (maybe_expand_insn (icode, 4, ops))
2381 return 1;
2382 delete_insns_since (last);
2385 /* It can't be done in this mode. Can we do it in a wider mode? */
2387 if (CLASS_HAS_WIDER_MODES_P (mclass))
2389 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2390 wider_mode != VOIDmode;
2391 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2393 if (optab_handler (binoptab, wider_mode) != CODE_FOR_nothing)
2395 rtx t0 = gen_reg_rtx (wider_mode);
2396 rtx t1 = gen_reg_rtx (wider_mode);
2397 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2398 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2400 if (expand_twoval_binop (binoptab, cop0, cop1,
2401 t0, t1, unsignedp))
2403 convert_move (targ0, t0, unsignedp);
2404 convert_move (targ1, t1, unsignedp);
2405 return 1;
2407 else
2408 delete_insns_since (last);
2413 delete_insns_since (entry_last);
2414 return 0;
2417 /* Expand the two-valued library call indicated by BINOPTAB, but
2418 preserve only one of the values. If TARG0 is non-NULL, the first
2419 value is placed into TARG0; otherwise the second value is placed
2420 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2421 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2422 This routine assumes that the value returned by the library call is
2423 as if the return value was of an integral mode twice as wide as the
2424 mode of OP0. Returns 1 if the call was successful. */
2426 bool
2427 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2428 rtx targ0, rtx targ1, enum rtx_code code)
2430 enum machine_mode mode;
2431 enum machine_mode libval_mode;
2432 rtx libval;
2433 rtx insns;
2434 rtx libfunc;
2436 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2437 gcc_assert (!targ0 != !targ1);
2439 mode = GET_MODE (op0);
2440 libfunc = optab_libfunc (binoptab, mode);
2441 if (!libfunc)
2442 return false;
2444 /* The value returned by the library function will have twice as
2445 many bits as the nominal MODE. */
2446 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2447 MODE_INT);
2448 start_sequence ();
2449 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2450 libval_mode, 2,
2451 op0, mode,
2452 op1, mode);
2453 /* Get the part of VAL containing the value that we want. */
2454 libval = simplify_gen_subreg (mode, libval, libval_mode,
2455 targ0 ? 0 : GET_MODE_SIZE (mode));
2456 insns = get_insns ();
2457 end_sequence ();
2458 /* Move the into the desired location. */
2459 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2460 gen_rtx_fmt_ee (code, mode, op0, op1));
2462 return true;
2466 /* Wrapper around expand_unop which takes an rtx code to specify
2467 the operation to perform, not an optab pointer. All other
2468 arguments are the same. */
2470 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2471 rtx target, int unsignedp)
2473 optab unop = code_to_optab[(int) code];
2474 gcc_assert (unop);
2476 return expand_unop (mode, unop, op0, target, unsignedp);
2479 /* Try calculating
2480 (clz:narrow x)
2482 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)).
2484 A similar operation can be used for clrsb. UNOPTAB says which operation
2485 we are trying to expand. */
2486 static rtx
2487 widen_leading (enum machine_mode mode, rtx op0, rtx target, optab unoptab)
2489 enum mode_class mclass = GET_MODE_CLASS (mode);
2490 if (CLASS_HAS_WIDER_MODES_P (mclass))
2492 enum machine_mode wider_mode;
2493 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2494 wider_mode != VOIDmode;
2495 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2497 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
2499 rtx xop0, temp, last;
2501 last = get_last_insn ();
2503 if (target == 0)
2504 target = gen_reg_rtx (mode);
2505 xop0 = widen_operand (op0, wider_mode, mode,
2506 unoptab != clrsb_optab, false);
2507 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
2508 unoptab != clrsb_optab);
2509 if (temp != 0)
2510 temp = expand_binop (wider_mode, sub_optab, temp,
2511 GEN_INT (GET_MODE_PRECISION (wider_mode)
2512 - GET_MODE_PRECISION (mode)),
2513 target, true, OPTAB_DIRECT);
2514 if (temp == 0)
2515 delete_insns_since (last);
2517 return temp;
2521 return 0;
2524 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2525 quantities, choosing which based on whether the high word is nonzero. */
2526 static rtx
2527 expand_doubleword_clz (enum machine_mode mode, rtx op0, rtx target)
2529 rtx xop0 = force_reg (mode, op0);
2530 rtx subhi = gen_highpart (word_mode, xop0);
2531 rtx sublo = gen_lowpart (word_mode, xop0);
2532 rtx hi0_label = gen_label_rtx ();
2533 rtx after_label = gen_label_rtx ();
2534 rtx seq, temp, result;
2536 /* If we were not given a target, use a word_mode register, not a
2537 'mode' register. The result will fit, and nobody is expecting
2538 anything bigger (the return type of __builtin_clz* is int). */
2539 if (!target)
2540 target = gen_reg_rtx (word_mode);
2542 /* In any case, write to a word_mode scratch in both branches of the
2543 conditional, so we can ensure there is a single move insn setting
2544 'target' to tag a REG_EQUAL note on. */
2545 result = gen_reg_rtx (word_mode);
2547 start_sequence ();
2549 /* If the high word is not equal to zero,
2550 then clz of the full value is clz of the high word. */
2551 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2552 word_mode, true, hi0_label);
2554 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2555 if (!temp)
2556 goto fail;
2558 if (temp != result)
2559 convert_move (result, temp, true);
2561 emit_jump_insn (gen_jump (after_label));
2562 emit_barrier ();
2564 /* Else clz of the full value is clz of the low word plus the number
2565 of bits in the high word. */
2566 emit_label (hi0_label);
2568 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2569 if (!temp)
2570 goto fail;
2571 temp = expand_binop (word_mode, add_optab, temp,
2572 GEN_INT (GET_MODE_BITSIZE (word_mode)),
2573 result, true, OPTAB_DIRECT);
2574 if (!temp)
2575 goto fail;
2576 if (temp != result)
2577 convert_move (result, temp, true);
2579 emit_label (after_label);
2580 convert_move (target, result, true);
2582 seq = get_insns ();
2583 end_sequence ();
2585 add_equal_note (seq, target, CLZ, xop0, 0);
2586 emit_insn (seq);
2587 return target;
2589 fail:
2590 end_sequence ();
2591 return 0;
2594 /* Try calculating
2595 (bswap:narrow x)
2597 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2598 static rtx
2599 widen_bswap (enum machine_mode mode, rtx op0, rtx target)
2601 enum mode_class mclass = GET_MODE_CLASS (mode);
2602 enum machine_mode wider_mode;
2603 rtx x, last;
2605 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2606 return NULL_RTX;
2608 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2609 wider_mode != VOIDmode;
2610 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2611 if (optab_handler (bswap_optab, wider_mode) != CODE_FOR_nothing)
2612 goto found;
2613 return NULL_RTX;
2615 found:
2616 last = get_last_insn ();
2618 x = widen_operand (op0, wider_mode, mode, true, true);
2619 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2621 gcc_assert (GET_MODE_PRECISION (wider_mode) == GET_MODE_BITSIZE (wider_mode)
2622 && GET_MODE_PRECISION (mode) == GET_MODE_BITSIZE (mode));
2623 if (x != 0)
2624 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2625 GET_MODE_BITSIZE (wider_mode)
2626 - GET_MODE_BITSIZE (mode),
2627 NULL_RTX, true);
2629 if (x != 0)
2631 if (target == 0)
2632 target = gen_reg_rtx (mode);
2633 emit_move_insn (target, gen_lowpart (mode, x));
2635 else
2636 delete_insns_since (last);
2638 return target;
2641 /* Try calculating bswap as two bswaps of two word-sized operands. */
2643 static rtx
2644 expand_doubleword_bswap (enum machine_mode mode, rtx op, rtx target)
2646 rtx t0, t1;
2648 t1 = expand_unop (word_mode, bswap_optab,
2649 operand_subword_force (op, 0, mode), NULL_RTX, true);
2650 t0 = expand_unop (word_mode, bswap_optab,
2651 operand_subword_force (op, 1, mode), NULL_RTX, true);
2653 if (target == 0 || !valid_multiword_target_p (target))
2654 target = gen_reg_rtx (mode);
2655 if (REG_P (target))
2656 emit_clobber (target);
2657 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2658 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2660 return target;
2663 /* Try calculating (parity x) as (and (popcount x) 1), where
2664 popcount can also be done in a wider mode. */
2665 static rtx
2666 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2668 enum mode_class mclass = GET_MODE_CLASS (mode);
2669 if (CLASS_HAS_WIDER_MODES_P (mclass))
2671 enum machine_mode wider_mode;
2672 for (wider_mode = mode; wider_mode != VOIDmode;
2673 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2675 if (optab_handler (popcount_optab, wider_mode) != CODE_FOR_nothing)
2677 rtx xop0, temp, last;
2679 last = get_last_insn ();
2681 if (target == 0)
2682 target = gen_reg_rtx (mode);
2683 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2684 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2685 true);
2686 if (temp != 0)
2687 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2688 target, true, OPTAB_DIRECT);
2689 if (temp == 0)
2690 delete_insns_since (last);
2692 return temp;
2696 return 0;
2699 /* Try calculating ctz(x) as K - clz(x & -x) ,
2700 where K is GET_MODE_PRECISION(mode) - 1.
2702 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2703 don't have to worry about what the hardware does in that case. (If
2704 the clz instruction produces the usual value at 0, which is K, the
2705 result of this code sequence will be -1; expand_ffs, below, relies
2706 on this. It might be nice to have it be K instead, for consistency
2707 with the (very few) processors that provide a ctz with a defined
2708 value, but that would take one more instruction, and it would be
2709 less convenient for expand_ffs anyway. */
2711 static rtx
2712 expand_ctz (enum machine_mode mode, rtx op0, rtx target)
2714 rtx seq, temp;
2716 if (optab_handler (clz_optab, mode) == CODE_FOR_nothing)
2717 return 0;
2719 start_sequence ();
2721 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2722 if (temp)
2723 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2724 true, OPTAB_DIRECT);
2725 if (temp)
2726 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2727 if (temp)
2728 temp = expand_binop (mode, sub_optab, GEN_INT (GET_MODE_PRECISION (mode) - 1),
2729 temp, target,
2730 true, OPTAB_DIRECT);
2731 if (temp == 0)
2733 end_sequence ();
2734 return 0;
2737 seq = get_insns ();
2738 end_sequence ();
2740 add_equal_note (seq, temp, CTZ, op0, 0);
2741 emit_insn (seq);
2742 return temp;
2746 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2747 else with the sequence used by expand_clz.
2749 The ffs builtin promises to return zero for a zero value and ctz/clz
2750 may have an undefined value in that case. If they do not give us a
2751 convenient value, we have to generate a test and branch. */
2752 static rtx
2753 expand_ffs (enum machine_mode mode, rtx op0, rtx target)
2755 HOST_WIDE_INT val = 0;
2756 bool defined_at_zero = false;
2757 rtx temp, seq;
2759 if (optab_handler (ctz_optab, mode) != CODE_FOR_nothing)
2761 start_sequence ();
2763 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2764 if (!temp)
2765 goto fail;
2767 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2769 else if (optab_handler (clz_optab, mode) != CODE_FOR_nothing)
2771 start_sequence ();
2772 temp = expand_ctz (mode, op0, 0);
2773 if (!temp)
2774 goto fail;
2776 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2778 defined_at_zero = true;
2779 val = (GET_MODE_PRECISION (mode) - 1) - val;
2782 else
2783 return 0;
2785 if (defined_at_zero && val == -1)
2786 /* No correction needed at zero. */;
2787 else
2789 /* We don't try to do anything clever with the situation found
2790 on some processors (eg Alpha) where ctz(0:mode) ==
2791 bitsize(mode). If someone can think of a way to send N to -1
2792 and leave alone all values in the range 0..N-1 (where N is a
2793 power of two), cheaper than this test-and-branch, please add it.
2795 The test-and-branch is done after the operation itself, in case
2796 the operation sets condition codes that can be recycled for this.
2797 (This is true on i386, for instance.) */
2799 rtx nonzero_label = gen_label_rtx ();
2800 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2801 mode, true, nonzero_label);
2803 convert_move (temp, GEN_INT (-1), false);
2804 emit_label (nonzero_label);
2807 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2808 to produce a value in the range 0..bitsize. */
2809 temp = expand_binop (mode, add_optab, temp, GEN_INT (1),
2810 target, false, OPTAB_DIRECT);
2811 if (!temp)
2812 goto fail;
2814 seq = get_insns ();
2815 end_sequence ();
2817 add_equal_note (seq, temp, FFS, op0, 0);
2818 emit_insn (seq);
2819 return temp;
2821 fail:
2822 end_sequence ();
2823 return 0;
2826 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2827 conditions, VAL may already be a SUBREG against which we cannot generate
2828 a further SUBREG. In this case, we expect forcing the value into a
2829 register will work around the situation. */
2831 static rtx
2832 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2833 enum machine_mode imode)
2835 rtx ret;
2836 ret = lowpart_subreg (omode, val, imode);
2837 if (ret == NULL)
2839 val = force_reg (imode, val);
2840 ret = lowpart_subreg (omode, val, imode);
2841 gcc_assert (ret != NULL);
2843 return ret;
2846 /* Expand a floating point absolute value or negation operation via a
2847 logical operation on the sign bit. */
2849 static rtx
2850 expand_absneg_bit (enum rtx_code code, enum machine_mode mode,
2851 rtx op0, rtx target)
2853 const struct real_format *fmt;
2854 int bitpos, word, nwords, i;
2855 enum machine_mode imode;
2856 double_int mask;
2857 rtx temp, insns;
2859 /* The format has to have a simple sign bit. */
2860 fmt = REAL_MODE_FORMAT (mode);
2861 if (fmt == NULL)
2862 return NULL_RTX;
2864 bitpos = fmt->signbit_rw;
2865 if (bitpos < 0)
2866 return NULL_RTX;
2868 /* Don't create negative zeros if the format doesn't support them. */
2869 if (code == NEG && !fmt->has_signed_zero)
2870 return NULL_RTX;
2872 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2874 imode = int_mode_for_mode (mode);
2875 if (imode == BLKmode)
2876 return NULL_RTX;
2877 word = 0;
2878 nwords = 1;
2880 else
2882 imode = word_mode;
2884 if (FLOAT_WORDS_BIG_ENDIAN)
2885 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2886 else
2887 word = bitpos / BITS_PER_WORD;
2888 bitpos = bitpos % BITS_PER_WORD;
2889 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2892 mask = double_int_setbit (double_int_zero, bitpos);
2893 if (code == ABS)
2894 mask = double_int_not (mask);
2896 if (target == 0
2897 || target == op0
2898 || (nwords > 1 && !valid_multiword_target_p (target)))
2899 target = gen_reg_rtx (mode);
2901 if (nwords > 1)
2903 start_sequence ();
2905 for (i = 0; i < nwords; ++i)
2907 rtx targ_piece = operand_subword (target, i, 1, mode);
2908 rtx op0_piece = operand_subword_force (op0, i, mode);
2910 if (i == word)
2912 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2913 op0_piece,
2914 immed_double_int_const (mask, imode),
2915 targ_piece, 1, OPTAB_LIB_WIDEN);
2916 if (temp != targ_piece)
2917 emit_move_insn (targ_piece, temp);
2919 else
2920 emit_move_insn (targ_piece, op0_piece);
2923 insns = get_insns ();
2924 end_sequence ();
2926 emit_insn (insns);
2928 else
2930 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
2931 gen_lowpart (imode, op0),
2932 immed_double_int_const (mask, imode),
2933 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
2934 target = lowpart_subreg_maybe_copy (mode, temp, imode);
2936 set_dst_reg_note (get_last_insn (), REG_EQUAL,
2937 gen_rtx_fmt_e (code, mode, copy_rtx (op0)),
2938 target);
2941 return target;
2944 /* As expand_unop, but will fail rather than attempt the operation in a
2945 different mode or with a libcall. */
2946 static rtx
2947 expand_unop_direct (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2948 int unsignedp)
2950 if (optab_handler (unoptab, mode) != CODE_FOR_nothing)
2952 struct expand_operand ops[2];
2953 enum insn_code icode = optab_handler (unoptab, mode);
2954 rtx last = get_last_insn ();
2955 rtx pat;
2957 create_output_operand (&ops[0], target, mode);
2958 create_convert_operand_from (&ops[1], op0, mode, unsignedp);
2959 pat = maybe_gen_insn (icode, 2, ops);
2960 if (pat)
2962 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
2963 && ! add_equal_note (pat, ops[0].value, unoptab->code,
2964 ops[1].value, NULL_RTX))
2966 delete_insns_since (last);
2967 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
2970 emit_insn (pat);
2972 return ops[0].value;
2975 return 0;
2978 /* Generate code to perform an operation specified by UNOPTAB
2979 on operand OP0, with result having machine-mode MODE.
2981 UNSIGNEDP is for the case where we have to widen the operands
2982 to perform the operation. It says to use zero-extension.
2984 If TARGET is nonzero, the value
2985 is generated there, if it is convenient to do so.
2986 In all cases an rtx is returned for the locus of the value;
2987 this may or may not be TARGET. */
2990 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
2991 int unsignedp)
2993 enum mode_class mclass = GET_MODE_CLASS (mode);
2994 enum machine_mode wider_mode;
2995 rtx temp;
2996 rtx libfunc;
2998 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
2999 if (temp)
3000 return temp;
3002 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3004 /* Widening (or narrowing) clz needs special treatment. */
3005 if (unoptab == clz_optab)
3007 temp = widen_leading (mode, op0, target, unoptab);
3008 if (temp)
3009 return temp;
3011 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3012 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3014 temp = expand_doubleword_clz (mode, op0, target);
3015 if (temp)
3016 return temp;
3019 goto try_libcall;
3022 if (unoptab == clrsb_optab)
3024 temp = widen_leading (mode, op0, target, unoptab);
3025 if (temp)
3026 return temp;
3027 goto try_libcall;
3030 /* Widening (or narrowing) bswap needs special treatment. */
3031 if (unoptab == bswap_optab)
3033 /* HImode is special because in this mode BSWAP is equivalent to ROTATE
3034 or ROTATERT. First try these directly; if this fails, then try the
3035 obvious pair of shifts with allowed widening, as this will probably
3036 be always more efficient than the other fallback methods. */
3037 if (mode == HImode)
3039 rtx last, temp1, temp2;
3041 if (optab_handler (rotl_optab, mode) != CODE_FOR_nothing)
3043 temp = expand_binop (mode, rotl_optab, op0, GEN_INT (8), target,
3044 unsignedp, OPTAB_DIRECT);
3045 if (temp)
3046 return temp;
3049 if (optab_handler (rotr_optab, mode) != CODE_FOR_nothing)
3051 temp = expand_binop (mode, rotr_optab, op0, GEN_INT (8), target,
3052 unsignedp, OPTAB_DIRECT);
3053 if (temp)
3054 return temp;
3057 last = get_last_insn ();
3059 temp1 = expand_binop (mode, ashl_optab, op0, GEN_INT (8), NULL_RTX,
3060 unsignedp, OPTAB_WIDEN);
3061 temp2 = expand_binop (mode, lshr_optab, op0, GEN_INT (8), NULL_RTX,
3062 unsignedp, OPTAB_WIDEN);
3063 if (temp1 && temp2)
3065 temp = expand_binop (mode, ior_optab, temp1, temp2, target,
3066 unsignedp, OPTAB_WIDEN);
3067 if (temp)
3068 return temp;
3071 delete_insns_since (last);
3074 temp = widen_bswap (mode, op0, target);
3075 if (temp)
3076 return temp;
3078 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3079 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3081 temp = expand_doubleword_bswap (mode, op0, target);
3082 if (temp)
3083 return temp;
3086 goto try_libcall;
3089 if (CLASS_HAS_WIDER_MODES_P (mclass))
3090 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3091 wider_mode != VOIDmode;
3092 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3094 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing)
3096 rtx xop0 = op0;
3097 rtx last = get_last_insn ();
3099 /* For certain operations, we need not actually extend
3100 the narrow operand, as long as we will truncate the
3101 results to the same narrowness. */
3103 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3104 (unoptab == neg_optab
3105 || unoptab == one_cmpl_optab)
3106 && mclass == MODE_INT);
3108 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3109 unsignedp);
3111 if (temp)
3113 if (mclass != MODE_INT
3114 || !TRULY_NOOP_TRUNCATION_MODES_P (mode, wider_mode))
3116 if (target == 0)
3117 target = gen_reg_rtx (mode);
3118 convert_move (target, temp, 0);
3119 return target;
3121 else
3122 return gen_lowpart (mode, temp);
3124 else
3125 delete_insns_since (last);
3129 /* These can be done a word at a time. */
3130 if (unoptab == one_cmpl_optab
3131 && mclass == MODE_INT
3132 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3133 && optab_handler (unoptab, word_mode) != CODE_FOR_nothing)
3135 int i;
3136 rtx insns;
3138 if (target == 0 || target == op0 || !valid_multiword_target_p (target))
3139 target = gen_reg_rtx (mode);
3141 start_sequence ();
3143 /* Do the actual arithmetic. */
3144 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3146 rtx target_piece = operand_subword (target, i, 1, mode);
3147 rtx x = expand_unop (word_mode, unoptab,
3148 operand_subword_force (op0, i, mode),
3149 target_piece, unsignedp);
3151 if (target_piece != x)
3152 emit_move_insn (target_piece, x);
3155 insns = get_insns ();
3156 end_sequence ();
3158 emit_insn (insns);
3159 return target;
3162 if (unoptab->code == NEG)
3164 /* Try negating floating point values by flipping the sign bit. */
3165 if (SCALAR_FLOAT_MODE_P (mode))
3167 temp = expand_absneg_bit (NEG, mode, op0, target);
3168 if (temp)
3169 return temp;
3172 /* If there is no negation pattern, and we have no negative zero,
3173 try subtracting from zero. */
3174 if (!HONOR_SIGNED_ZEROS (mode))
3176 temp = expand_binop (mode, (unoptab == negv_optab
3177 ? subv_optab : sub_optab),
3178 CONST0_RTX (mode), op0, target,
3179 unsignedp, OPTAB_DIRECT);
3180 if (temp)
3181 return temp;
3185 /* Try calculating parity (x) as popcount (x) % 2. */
3186 if (unoptab == parity_optab)
3188 temp = expand_parity (mode, op0, target);
3189 if (temp)
3190 return temp;
3193 /* Try implementing ffs (x) in terms of clz (x). */
3194 if (unoptab == ffs_optab)
3196 temp = expand_ffs (mode, op0, target);
3197 if (temp)
3198 return temp;
3201 /* Try implementing ctz (x) in terms of clz (x). */
3202 if (unoptab == ctz_optab)
3204 temp = expand_ctz (mode, op0, target);
3205 if (temp)
3206 return temp;
3209 try_libcall:
3210 /* Now try a library call in this mode. */
3211 libfunc = optab_libfunc (unoptab, mode);
3212 if (libfunc)
3214 rtx insns;
3215 rtx value;
3216 rtx eq_value;
3217 enum machine_mode outmode = mode;
3219 /* All of these functions return small values. Thus we choose to
3220 have them return something that isn't a double-word. */
3221 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3222 || unoptab == clrsb_optab || unoptab == popcount_optab
3223 || unoptab == parity_optab)
3224 outmode
3225 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node),
3226 optab_libfunc (unoptab, mode)));
3228 start_sequence ();
3230 /* Pass 1 for NO_QUEUE so we don't lose any increments
3231 if the libcall is cse'd or moved. */
3232 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3233 1, op0, mode);
3234 insns = get_insns ();
3235 end_sequence ();
3237 target = gen_reg_rtx (outmode);
3238 eq_value = gen_rtx_fmt_e (unoptab->code, mode, op0);
3239 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3240 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3241 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3242 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3243 emit_libcall_block_1 (insns, target, value, eq_value,
3244 trapv_unoptab_p (unoptab));
3246 return target;
3249 /* It can't be done in this mode. Can we do it in a wider mode? */
3251 if (CLASS_HAS_WIDER_MODES_P (mclass))
3253 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3254 wider_mode != VOIDmode;
3255 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3257 if (optab_handler (unoptab, wider_mode) != CODE_FOR_nothing
3258 || optab_libfunc (unoptab, wider_mode))
3260 rtx xop0 = op0;
3261 rtx last = get_last_insn ();
3263 /* For certain operations, we need not actually extend
3264 the narrow operand, as long as we will truncate the
3265 results to the same narrowness. */
3266 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3267 (unoptab == neg_optab
3268 || unoptab == one_cmpl_optab
3269 || unoptab == bswap_optab)
3270 && mclass == MODE_INT);
3272 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3273 unsignedp);
3275 /* If we are generating clz using wider mode, adjust the
3276 result. Similarly for clrsb. */
3277 if ((unoptab == clz_optab || unoptab == clrsb_optab)
3278 && temp != 0)
3279 temp = expand_binop (wider_mode, sub_optab, temp,
3280 GEN_INT (GET_MODE_PRECISION (wider_mode)
3281 - GET_MODE_PRECISION (mode)),
3282 target, true, OPTAB_DIRECT);
3284 /* Likewise for bswap. */
3285 if (unoptab == bswap_optab && temp != 0)
3287 gcc_assert (GET_MODE_PRECISION (wider_mode)
3288 == GET_MODE_BITSIZE (wider_mode)
3289 && GET_MODE_PRECISION (mode)
3290 == GET_MODE_BITSIZE (mode));
3292 temp = expand_shift (RSHIFT_EXPR, wider_mode, temp,
3293 GET_MODE_BITSIZE (wider_mode)
3294 - GET_MODE_BITSIZE (mode),
3295 NULL_RTX, true);
3298 if (temp)
3300 if (mclass != MODE_INT)
3302 if (target == 0)
3303 target = gen_reg_rtx (mode);
3304 convert_move (target, temp, 0);
3305 return target;
3307 else
3308 return gen_lowpart (mode, temp);
3310 else
3311 delete_insns_since (last);
3316 /* One final attempt at implementing negation via subtraction,
3317 this time allowing widening of the operand. */
3318 if (unoptab->code == NEG && !HONOR_SIGNED_ZEROS (mode))
3320 rtx temp;
3321 temp = expand_binop (mode,
3322 unoptab == negv_optab ? subv_optab : sub_optab,
3323 CONST0_RTX (mode), op0,
3324 target, unsignedp, OPTAB_LIB_WIDEN);
3325 if (temp)
3326 return temp;
3329 return 0;
3332 /* Emit code to compute the absolute value of OP0, with result to
3333 TARGET if convenient. (TARGET may be 0.) The return value says
3334 where the result actually is to be found.
3336 MODE is the mode of the operand; the mode of the result is
3337 different but can be deduced from MODE.
3342 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
3343 int result_unsignedp)
3345 rtx temp;
3347 if (! flag_trapv)
3348 result_unsignedp = 1;
3350 /* First try to do it with a special abs instruction. */
3351 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3352 op0, target, 0);
3353 if (temp != 0)
3354 return temp;
3356 /* For floating point modes, try clearing the sign bit. */
3357 if (SCALAR_FLOAT_MODE_P (mode))
3359 temp = expand_absneg_bit (ABS, mode, op0, target);
3360 if (temp)
3361 return temp;
3364 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3365 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing
3366 && !HONOR_SIGNED_ZEROS (mode))
3368 rtx last = get_last_insn ();
3370 temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
3371 if (temp != 0)
3372 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3373 OPTAB_WIDEN);
3375 if (temp != 0)
3376 return temp;
3378 delete_insns_since (last);
3381 /* If this machine has expensive jumps, we can do integer absolute
3382 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3383 where W is the width of MODE. */
3385 if (GET_MODE_CLASS (mode) == MODE_INT
3386 && BRANCH_COST (optimize_insn_for_speed_p (),
3387 false) >= 2)
3389 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3390 GET_MODE_PRECISION (mode) - 1,
3391 NULL_RTX, 0);
3393 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3394 OPTAB_LIB_WIDEN);
3395 if (temp != 0)
3396 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3397 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3399 if (temp != 0)
3400 return temp;
3403 return NULL_RTX;
3407 expand_abs (enum machine_mode mode, rtx op0, rtx target,
3408 int result_unsignedp, int safe)
3410 rtx temp, op1;
3412 if (! flag_trapv)
3413 result_unsignedp = 1;
3415 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3416 if (temp != 0)
3417 return temp;
3419 /* If that does not win, use conditional jump and negate. */
3421 /* It is safe to use the target if it is the same
3422 as the source if this is also a pseudo register */
3423 if (op0 == target && REG_P (op0)
3424 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3425 safe = 1;
3427 op1 = gen_label_rtx ();
3428 if (target == 0 || ! safe
3429 || GET_MODE (target) != mode
3430 || (MEM_P (target) && MEM_VOLATILE_P (target))
3431 || (REG_P (target)
3432 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3433 target = gen_reg_rtx (mode);
3435 emit_move_insn (target, op0);
3436 NO_DEFER_POP;
3438 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3439 NULL_RTX, NULL_RTX, op1, -1);
3441 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3442 target, target, 0);
3443 if (op0 != target)
3444 emit_move_insn (target, op0);
3445 emit_label (op1);
3446 OK_DEFER_POP;
3447 return target;
3450 /* Emit code to compute the one's complement absolute value of OP0
3451 (if (OP0 < 0) OP0 = ~OP0), with result to TARGET if convenient.
3452 (TARGET may be NULL_RTX.) The return value says where the result
3453 actually is to be found.
3455 MODE is the mode of the operand; the mode of the result is
3456 different but can be deduced from MODE. */
3459 expand_one_cmpl_abs_nojump (enum machine_mode mode, rtx op0, rtx target)
3461 rtx temp;
3463 /* Not applicable for floating point modes. */
3464 if (FLOAT_MODE_P (mode))
3465 return NULL_RTX;
3467 /* If we have a MAX insn, we can do this as MAX (x, ~x). */
3468 if (optab_handler (smax_optab, mode) != CODE_FOR_nothing)
3470 rtx last = get_last_insn ();
3472 temp = expand_unop (mode, one_cmpl_optab, op0, NULL_RTX, 0);
3473 if (temp != 0)
3474 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3475 OPTAB_WIDEN);
3477 if (temp != 0)
3478 return temp;
3480 delete_insns_since (last);
3483 /* If this machine has expensive jumps, we can do one's complement
3484 absolute value of X as (((signed) x >> (W-1)) ^ x). */
3486 if (GET_MODE_CLASS (mode) == MODE_INT
3487 && BRANCH_COST (optimize_insn_for_speed_p (),
3488 false) >= 2)
3490 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3491 GET_MODE_PRECISION (mode) - 1,
3492 NULL_RTX, 0);
3494 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3495 OPTAB_LIB_WIDEN);
3497 if (temp != 0)
3498 return temp;
3501 return NULL_RTX;
3504 /* A subroutine of expand_copysign, perform the copysign operation using the
3505 abs and neg primitives advertised to exist on the target. The assumption
3506 is that we have a split register file, and leaving op0 in fp registers,
3507 and not playing with subregs so much, will help the register allocator. */
3509 static rtx
3510 expand_copysign_absneg (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3511 int bitpos, bool op0_is_abs)
3513 enum machine_mode imode;
3514 enum insn_code icode;
3515 rtx sign, label;
3517 if (target == op1)
3518 target = NULL_RTX;
3520 /* Check if the back end provides an insn that handles signbit for the
3521 argument's mode. */
3522 icode = optab_handler (signbit_optab, mode);
3523 if (icode != CODE_FOR_nothing)
3525 imode = insn_data[(int) icode].operand[0].mode;
3526 sign = gen_reg_rtx (imode);
3527 emit_unop_insn (icode, sign, op1, UNKNOWN);
3529 else
3531 double_int mask;
3533 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3535 imode = int_mode_for_mode (mode);
3536 if (imode == BLKmode)
3537 return NULL_RTX;
3538 op1 = gen_lowpart (imode, op1);
3540 else
3542 int word;
3544 imode = word_mode;
3545 if (FLOAT_WORDS_BIG_ENDIAN)
3546 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3547 else
3548 word = bitpos / BITS_PER_WORD;
3549 bitpos = bitpos % BITS_PER_WORD;
3550 op1 = operand_subword_force (op1, word, mode);
3553 mask = double_int_setbit (double_int_zero, bitpos);
3555 sign = expand_binop (imode, and_optab, op1,
3556 immed_double_int_const (mask, imode),
3557 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3560 if (!op0_is_abs)
3562 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3563 if (op0 == NULL)
3564 return NULL_RTX;
3565 target = op0;
3567 else
3569 if (target == NULL_RTX)
3570 target = copy_to_reg (op0);
3571 else
3572 emit_move_insn (target, op0);
3575 label = gen_label_rtx ();
3576 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3578 if (GET_CODE (op0) == CONST_DOUBLE)
3579 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3580 else
3581 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3582 if (op0 != target)
3583 emit_move_insn (target, op0);
3585 emit_label (label);
3587 return target;
3591 /* A subroutine of expand_copysign, perform the entire copysign operation
3592 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3593 is true if op0 is known to have its sign bit clear. */
3595 static rtx
3596 expand_copysign_bit (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3597 int bitpos, bool op0_is_abs)
3599 enum machine_mode imode;
3600 double_int mask;
3601 int word, nwords, i;
3602 rtx temp, insns;
3604 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3606 imode = int_mode_for_mode (mode);
3607 if (imode == BLKmode)
3608 return NULL_RTX;
3609 word = 0;
3610 nwords = 1;
3612 else
3614 imode = word_mode;
3616 if (FLOAT_WORDS_BIG_ENDIAN)
3617 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3618 else
3619 word = bitpos / BITS_PER_WORD;
3620 bitpos = bitpos % BITS_PER_WORD;
3621 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3624 mask = double_int_setbit (double_int_zero, bitpos);
3626 if (target == 0
3627 || target == op0
3628 || target == op1
3629 || (nwords > 1 && !valid_multiword_target_p (target)))
3630 target = gen_reg_rtx (mode);
3632 if (nwords > 1)
3634 start_sequence ();
3636 for (i = 0; i < nwords; ++i)
3638 rtx targ_piece = operand_subword (target, i, 1, mode);
3639 rtx op0_piece = operand_subword_force (op0, i, mode);
3641 if (i == word)
3643 if (!op0_is_abs)
3644 op0_piece
3645 = expand_binop (imode, and_optab, op0_piece,
3646 immed_double_int_const (double_int_not (mask),
3647 imode),
3648 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3650 op1 = expand_binop (imode, and_optab,
3651 operand_subword_force (op1, i, mode),
3652 immed_double_int_const (mask, imode),
3653 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3655 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3656 targ_piece, 1, OPTAB_LIB_WIDEN);
3657 if (temp != targ_piece)
3658 emit_move_insn (targ_piece, temp);
3660 else
3661 emit_move_insn (targ_piece, op0_piece);
3664 insns = get_insns ();
3665 end_sequence ();
3667 emit_insn (insns);
3669 else
3671 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3672 immed_double_int_const (mask, imode),
3673 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3675 op0 = gen_lowpart (imode, op0);
3676 if (!op0_is_abs)
3677 op0 = expand_binop (imode, and_optab, op0,
3678 immed_double_int_const (double_int_not (mask),
3679 imode),
3680 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3682 temp = expand_binop (imode, ior_optab, op0, op1,
3683 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3684 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3687 return target;
3690 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3691 scalar floating point mode. Return NULL if we do not know how to
3692 expand the operation inline. */
3695 expand_copysign (rtx op0, rtx op1, rtx target)
3697 enum machine_mode mode = GET_MODE (op0);
3698 const struct real_format *fmt;
3699 bool op0_is_abs;
3700 rtx temp;
3702 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3703 gcc_assert (GET_MODE (op1) == mode);
3705 /* First try to do it with a special instruction. */
3706 temp = expand_binop (mode, copysign_optab, op0, op1,
3707 target, 0, OPTAB_DIRECT);
3708 if (temp)
3709 return temp;
3711 fmt = REAL_MODE_FORMAT (mode);
3712 if (fmt == NULL || !fmt->has_signed_zero)
3713 return NULL_RTX;
3715 op0_is_abs = false;
3716 if (GET_CODE (op0) == CONST_DOUBLE)
3718 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3719 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3720 op0_is_abs = true;
3723 if (fmt->signbit_ro >= 0
3724 && (GET_CODE (op0) == CONST_DOUBLE
3725 || (optab_handler (neg_optab, mode) != CODE_FOR_nothing
3726 && optab_handler (abs_optab, mode) != CODE_FOR_nothing)))
3728 temp = expand_copysign_absneg (mode, op0, op1, target,
3729 fmt->signbit_ro, op0_is_abs);
3730 if (temp)
3731 return temp;
3734 if (fmt->signbit_rw < 0)
3735 return NULL_RTX;
3736 return expand_copysign_bit (mode, op0, op1, target,
3737 fmt->signbit_rw, op0_is_abs);
3740 /* Generate an instruction whose insn-code is INSN_CODE,
3741 with two operands: an output TARGET and an input OP0.
3742 TARGET *must* be nonzero, and the output is always stored there.
3743 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3744 the value that is stored into TARGET.
3746 Return false if expansion failed. */
3748 bool
3749 maybe_emit_unop_insn (enum insn_code icode, rtx target, rtx op0,
3750 enum rtx_code code)
3752 struct expand_operand ops[2];
3753 rtx pat;
3755 create_output_operand (&ops[0], target, GET_MODE (target));
3756 create_input_operand (&ops[1], op0, GET_MODE (op0));
3757 pat = maybe_gen_insn (icode, 2, ops);
3758 if (!pat)
3759 return false;
3761 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
3762 add_equal_note (pat, ops[0].value, code, ops[1].value, NULL_RTX);
3764 emit_insn (pat);
3766 if (ops[0].value != target)
3767 emit_move_insn (target, ops[0].value);
3768 return true;
3770 /* Generate an instruction whose insn-code is INSN_CODE,
3771 with two operands: an output TARGET and an input OP0.
3772 TARGET *must* be nonzero, and the output is always stored there.
3773 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3774 the value that is stored into TARGET. */
3776 void
3777 emit_unop_insn (enum insn_code icode, rtx target, rtx op0, enum rtx_code code)
3779 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3780 gcc_assert (ok);
3783 struct no_conflict_data
3785 rtx target, first, insn;
3786 bool must_stay;
3789 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3790 the currently examined clobber / store has to stay in the list of
3791 insns that constitute the actual libcall block. */
3792 static void
3793 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3795 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3797 /* If this inns directly contributes to setting the target, it must stay. */
3798 if (reg_overlap_mentioned_p (p->target, dest))
3799 p->must_stay = true;
3800 /* If we haven't committed to keeping any other insns in the list yet,
3801 there is nothing more to check. */
3802 else if (p->insn == p->first)
3803 return;
3804 /* If this insn sets / clobbers a register that feeds one of the insns
3805 already in the list, this insn has to stay too. */
3806 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3807 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3808 || reg_used_between_p (dest, p->first, p->insn)
3809 /* Likewise if this insn depends on a register set by a previous
3810 insn in the list, or if it sets a result (presumably a hard
3811 register) that is set or clobbered by a previous insn.
3812 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3813 SET_DEST perform the former check on the address, and the latter
3814 check on the MEM. */
3815 || (GET_CODE (set) == SET
3816 && (modified_in_p (SET_SRC (set), p->first)
3817 || modified_in_p (SET_DEST (set), p->first)
3818 || modified_between_p (SET_SRC (set), p->first, p->insn)
3819 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3820 p->must_stay = true;
3824 /* Emit code to make a call to a constant function or a library call.
3826 INSNS is a list containing all insns emitted in the call.
3827 These insns leave the result in RESULT. Our block is to copy RESULT
3828 to TARGET, which is logically equivalent to EQUIV.
3830 We first emit any insns that set a pseudo on the assumption that these are
3831 loading constants into registers; doing so allows them to be safely cse'ed
3832 between blocks. Then we emit all the other insns in the block, followed by
3833 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3834 note with an operand of EQUIV. */
3836 static void
3837 emit_libcall_block_1 (rtx insns, rtx target, rtx result, rtx equiv,
3838 bool equiv_may_trap)
3840 rtx final_dest = target;
3841 rtx next, last, insn;
3843 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3844 into a MEM later. Protect the libcall block from this change. */
3845 if (! REG_P (target) || REG_USERVAR_P (target))
3846 target = gen_reg_rtx (GET_MODE (target));
3848 /* If we're using non-call exceptions, a libcall corresponding to an
3849 operation that may trap may also trap. */
3850 /* ??? See the comment in front of make_reg_eh_region_note. */
3851 if (cfun->can_throw_non_call_exceptions
3852 && (equiv_may_trap || may_trap_p (equiv)))
3854 for (insn = insns; insn; insn = NEXT_INSN (insn))
3855 if (CALL_P (insn))
3857 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3858 if (note)
3860 int lp_nr = INTVAL (XEXP (note, 0));
3861 if (lp_nr == 0 || lp_nr == INT_MIN)
3862 remove_note (insn, note);
3866 else
3868 /* Look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3869 reg note to indicate that this call cannot throw or execute a nonlocal
3870 goto (unless there is already a REG_EH_REGION note, in which case
3871 we update it). */
3872 for (insn = insns; insn; insn = NEXT_INSN (insn))
3873 if (CALL_P (insn))
3874 make_reg_eh_region_note_nothrow_nononlocal (insn);
3877 /* First emit all insns that set pseudos. Remove them from the list as
3878 we go. Avoid insns that set pseudos which were referenced in previous
3879 insns. These can be generated by move_by_pieces, for example,
3880 to update an address. Similarly, avoid insns that reference things
3881 set in previous insns. */
3883 for (insn = insns; insn; insn = next)
3885 rtx set = single_set (insn);
3887 next = NEXT_INSN (insn);
3889 if (set != 0 && REG_P (SET_DEST (set))
3890 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3892 struct no_conflict_data data;
3894 data.target = const0_rtx;
3895 data.first = insns;
3896 data.insn = insn;
3897 data.must_stay = 0;
3898 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3899 if (! data.must_stay)
3901 if (PREV_INSN (insn))
3902 NEXT_INSN (PREV_INSN (insn)) = next;
3903 else
3904 insns = next;
3906 if (next)
3907 PREV_INSN (next) = PREV_INSN (insn);
3909 add_insn (insn);
3913 /* Some ports use a loop to copy large arguments onto the stack.
3914 Don't move anything outside such a loop. */
3915 if (LABEL_P (insn))
3916 break;
3919 /* Write the remaining insns followed by the final copy. */
3920 for (insn = insns; insn; insn = next)
3922 next = NEXT_INSN (insn);
3924 add_insn (insn);
3927 last = emit_move_insn (target, result);
3928 set_dst_reg_note (last, REG_EQUAL, copy_rtx (equiv), target);
3930 if (final_dest != target)
3931 emit_move_insn (final_dest, target);
3934 void
3935 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3937 emit_libcall_block_1 (insns, target, result, equiv, false);
3940 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3941 PURPOSE describes how this comparison will be used. CODE is the rtx
3942 comparison code we will be using.
3944 ??? Actually, CODE is slightly weaker than that. A target is still
3945 required to implement all of the normal bcc operations, but not
3946 required to implement all (or any) of the unordered bcc operations. */
3949 can_compare_p (enum rtx_code code, enum machine_mode mode,
3950 enum can_compare_purpose purpose)
3952 rtx test;
3953 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3956 enum insn_code icode;
3958 if (purpose == ccp_jump
3959 && (icode = optab_handler (cbranch_optab, mode)) != CODE_FOR_nothing
3960 && insn_operand_matches (icode, 0, test))
3961 return 1;
3962 if (purpose == ccp_store_flag
3963 && (icode = optab_handler (cstore_optab, mode)) != CODE_FOR_nothing
3964 && insn_operand_matches (icode, 1, test))
3965 return 1;
3966 if (purpose == ccp_cmov
3967 && optab_handler (cmov_optab, mode) != CODE_FOR_nothing)
3968 return 1;
3970 mode = GET_MODE_WIDER_MODE (mode);
3971 PUT_MODE (test, mode);
3973 while (mode != VOIDmode);
3975 return 0;
3978 /* This function is called when we are going to emit a compare instruction that
3979 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
3981 *PMODE is the mode of the inputs (in case they are const_int).
3982 *PUNSIGNEDP nonzero says that the operands are unsigned;
3983 this matters if they need to be widened (as given by METHODS).
3985 If they have mode BLKmode, then SIZE specifies the size of both operands.
3987 This function performs all the setup necessary so that the caller only has
3988 to emit a single comparison insn. This setup can involve doing a BLKmode
3989 comparison or emitting a library call to perform the comparison if no insn
3990 is available to handle it.
3991 The values which are passed in through pointers can be modified; the caller
3992 should perform the comparison on the modified values. Constant
3993 comparisons must have already been folded. */
3995 static void
3996 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
3997 int unsignedp, enum optab_methods methods,
3998 rtx *ptest, enum machine_mode *pmode)
4000 enum machine_mode mode = *pmode;
4001 rtx libfunc, test;
4002 enum machine_mode cmp_mode;
4003 enum mode_class mclass;
4005 /* The other methods are not needed. */
4006 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4007 || methods == OPTAB_LIB_WIDEN);
4009 /* If we are optimizing, force expensive constants into a register. */
4010 if (CONSTANT_P (x) && optimize
4011 && (rtx_cost (x, COMPARE, 0, optimize_insn_for_speed_p ())
4012 > COSTS_N_INSNS (1)))
4013 x = force_reg (mode, x);
4015 if (CONSTANT_P (y) && optimize
4016 && (rtx_cost (y, COMPARE, 1, optimize_insn_for_speed_p ())
4017 > COSTS_N_INSNS (1)))
4018 y = force_reg (mode, y);
4020 #ifdef HAVE_cc0
4021 /* Make sure if we have a canonical comparison. The RTL
4022 documentation states that canonical comparisons are required only
4023 for targets which have cc0. */
4024 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4025 #endif
4027 /* Don't let both operands fail to indicate the mode. */
4028 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4029 x = force_reg (mode, x);
4030 if (mode == VOIDmode)
4031 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4033 /* Handle all BLKmode compares. */
4035 if (mode == BLKmode)
4037 enum machine_mode result_mode;
4038 enum insn_code cmp_code;
4039 tree length_type;
4040 rtx libfunc;
4041 rtx result;
4042 rtx opalign
4043 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4045 gcc_assert (size);
4047 /* Try to use a memory block compare insn - either cmpstr
4048 or cmpmem will do. */
4049 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4050 cmp_mode != VOIDmode;
4051 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4053 cmp_code = direct_optab_handler (cmpmem_optab, cmp_mode);
4054 if (cmp_code == CODE_FOR_nothing)
4055 cmp_code = direct_optab_handler (cmpstr_optab, cmp_mode);
4056 if (cmp_code == CODE_FOR_nothing)
4057 cmp_code = direct_optab_handler (cmpstrn_optab, cmp_mode);
4058 if (cmp_code == CODE_FOR_nothing)
4059 continue;
4061 /* Must make sure the size fits the insn's mode. */
4062 if ((CONST_INT_P (size)
4063 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4064 || (GET_MODE_BITSIZE (GET_MODE (size))
4065 > GET_MODE_BITSIZE (cmp_mode)))
4066 continue;
4068 result_mode = insn_data[cmp_code].operand[0].mode;
4069 result = gen_reg_rtx (result_mode);
4070 size = convert_to_mode (cmp_mode, size, 1);
4071 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4073 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4074 *pmode = result_mode;
4075 return;
4078 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4079 goto fail;
4081 /* Otherwise call a library function, memcmp. */
4082 libfunc = memcmp_libfunc;
4083 length_type = sizetype;
4084 result_mode = TYPE_MODE (integer_type_node);
4085 cmp_mode = TYPE_MODE (length_type);
4086 size = convert_to_mode (TYPE_MODE (length_type), size,
4087 TYPE_UNSIGNED (length_type));
4089 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4090 result_mode, 3,
4091 XEXP (x, 0), Pmode,
4092 XEXP (y, 0), Pmode,
4093 size, cmp_mode);
4095 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4096 *pmode = result_mode;
4097 return;
4100 /* Don't allow operands to the compare to trap, as that can put the
4101 compare and branch in different basic blocks. */
4102 if (cfun->can_throw_non_call_exceptions)
4104 if (may_trap_p (x))
4105 x = force_reg (mode, x);
4106 if (may_trap_p (y))
4107 y = force_reg (mode, y);
4110 if (GET_MODE_CLASS (mode) == MODE_CC)
4112 gcc_assert (can_compare_p (comparison, CCmode, ccp_jump));
4113 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4114 return;
4117 mclass = GET_MODE_CLASS (mode);
4118 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4119 cmp_mode = mode;
4122 enum insn_code icode;
4123 icode = optab_handler (cbranch_optab, cmp_mode);
4124 if (icode != CODE_FOR_nothing
4125 && insn_operand_matches (icode, 0, test))
4127 rtx last = get_last_insn ();
4128 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4129 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4130 if (op0 && op1
4131 && insn_operand_matches (icode, 1, op0)
4132 && insn_operand_matches (icode, 2, op1))
4134 XEXP (test, 0) = op0;
4135 XEXP (test, 1) = op1;
4136 *ptest = test;
4137 *pmode = cmp_mode;
4138 return;
4140 delete_insns_since (last);
4143 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4144 break;
4145 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4147 while (cmp_mode != VOIDmode);
4149 if (methods != OPTAB_LIB_WIDEN)
4150 goto fail;
4152 if (!SCALAR_FLOAT_MODE_P (mode))
4154 rtx result;
4155 enum machine_mode ret_mode;
4157 /* Handle a libcall just for the mode we are using. */
4158 libfunc = optab_libfunc (cmp_optab, mode);
4159 gcc_assert (libfunc);
4161 /* If we want unsigned, and this mode has a distinct unsigned
4162 comparison routine, use that. */
4163 if (unsignedp)
4165 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4166 if (ulibfunc)
4167 libfunc = ulibfunc;
4170 ret_mode = targetm.libgcc_cmp_return_mode ();
4171 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4172 ret_mode, 2, x, mode, y, mode);
4174 /* There are two kinds of comparison routines. Biased routines
4175 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4176 of gcc expect that the comparison operation is equivalent
4177 to the modified comparison. For signed comparisons compare the
4178 result against 1 in the biased case, and zero in the unbiased
4179 case. For unsigned comparisons always compare against 1 after
4180 biasing the unbiased result by adding 1. This gives us a way to
4181 represent LTU.
4182 The comparisons in the fixed-point helper library are always
4183 biased. */
4184 x = result;
4185 y = const1_rtx;
4187 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4189 if (unsignedp)
4190 x = plus_constant (ret_mode, result, 1);
4191 else
4192 y = const0_rtx;
4195 *pmode = word_mode;
4196 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4197 ptest, pmode);
4199 else
4200 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4202 return;
4204 fail:
4205 *ptest = NULL_RTX;
4208 /* Before emitting an insn with code ICODE, make sure that X, which is going
4209 to be used for operand OPNUM of the insn, is converted from mode MODE to
4210 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4211 that it is accepted by the operand predicate. Return the new value. */
4214 prepare_operand (enum insn_code icode, rtx x, int opnum, enum machine_mode mode,
4215 enum machine_mode wider_mode, int unsignedp)
4217 if (mode != wider_mode)
4218 x = convert_modes (wider_mode, mode, x, unsignedp);
4220 if (!insn_operand_matches (icode, opnum, x))
4222 if (reload_completed)
4223 return NULL_RTX;
4224 x = copy_to_mode_reg (insn_data[(int) icode].operand[opnum].mode, x);
4227 return x;
4230 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4231 we can do the branch. */
4233 static void
4234 emit_cmp_and_jump_insn_1 (rtx test, enum machine_mode mode, rtx label)
4236 enum machine_mode optab_mode;
4237 enum mode_class mclass;
4238 enum insn_code icode;
4240 mclass = GET_MODE_CLASS (mode);
4241 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4242 icode = optab_handler (cbranch_optab, optab_mode);
4244 gcc_assert (icode != CODE_FOR_nothing);
4245 gcc_assert (insn_operand_matches (icode, 0, test));
4246 emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0), XEXP (test, 1), label));
4249 /* Generate code to compare X with Y so that the condition codes are
4250 set and to jump to LABEL if the condition is true. If X is a
4251 constant and Y is not a constant, then the comparison is swapped to
4252 ensure that the comparison RTL has the canonical form.
4254 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4255 need to be widened. UNSIGNEDP is also used to select the proper
4256 branch condition code.
4258 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4260 MODE is the mode of the inputs (in case they are const_int).
4262 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4263 It will be potentially converted into an unsigned variant based on
4264 UNSIGNEDP to select a proper jump instruction. */
4266 void
4267 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4268 enum machine_mode mode, int unsignedp, rtx label)
4270 rtx op0 = x, op1 = y;
4271 rtx test;
4273 /* Swap operands and condition to ensure canonical RTL. */
4274 if (swap_commutative_operands_p (x, y)
4275 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4277 op0 = y, op1 = x;
4278 comparison = swap_condition (comparison);
4281 /* If OP0 is still a constant, then both X and Y must be constants
4282 or the opposite comparison is not supported. Force X into a register
4283 to create canonical RTL. */
4284 if (CONSTANT_P (op0))
4285 op0 = force_reg (mode, op0);
4287 if (unsignedp)
4288 comparison = unsigned_condition (comparison);
4290 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4291 &test, &mode);
4292 emit_cmp_and_jump_insn_1 (test, mode, label);
4296 /* Emit a library call comparison between floating point X and Y.
4297 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4299 static void
4300 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4301 rtx *ptest, enum machine_mode *pmode)
4303 enum rtx_code swapped = swap_condition (comparison);
4304 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4305 enum machine_mode orig_mode = GET_MODE (x);
4306 enum machine_mode mode, cmp_mode;
4307 rtx true_rtx, false_rtx;
4308 rtx value, target, insns, equiv;
4309 rtx libfunc = 0;
4310 bool reversed_p = false;
4311 cmp_mode = targetm.libgcc_cmp_return_mode ();
4313 for (mode = orig_mode;
4314 mode != VOIDmode;
4315 mode = GET_MODE_WIDER_MODE (mode))
4317 if (code_to_optab[comparison]
4318 && (libfunc = optab_libfunc (code_to_optab[comparison], mode)))
4319 break;
4321 if (code_to_optab[swapped]
4322 && (libfunc = optab_libfunc (code_to_optab[swapped], mode)))
4324 rtx tmp;
4325 tmp = x; x = y; y = tmp;
4326 comparison = swapped;
4327 break;
4330 if (code_to_optab[reversed]
4331 && (libfunc = optab_libfunc (code_to_optab[reversed], mode)))
4333 comparison = reversed;
4334 reversed_p = true;
4335 break;
4339 gcc_assert (mode != VOIDmode);
4341 if (mode != orig_mode)
4343 x = convert_to_mode (mode, x, 0);
4344 y = convert_to_mode (mode, y, 0);
4347 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4348 the RTL. The allows the RTL optimizers to delete the libcall if the
4349 condition can be determined at compile-time. */
4350 if (comparison == UNORDERED
4351 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4353 true_rtx = const_true_rtx;
4354 false_rtx = const0_rtx;
4356 else
4358 switch (comparison)
4360 case EQ:
4361 true_rtx = const0_rtx;
4362 false_rtx = const_true_rtx;
4363 break;
4365 case NE:
4366 true_rtx = const_true_rtx;
4367 false_rtx = const0_rtx;
4368 break;
4370 case GT:
4371 true_rtx = const1_rtx;
4372 false_rtx = const0_rtx;
4373 break;
4375 case GE:
4376 true_rtx = const0_rtx;
4377 false_rtx = constm1_rtx;
4378 break;
4380 case LT:
4381 true_rtx = constm1_rtx;
4382 false_rtx = const0_rtx;
4383 break;
4385 case LE:
4386 true_rtx = const0_rtx;
4387 false_rtx = const1_rtx;
4388 break;
4390 default:
4391 gcc_unreachable ();
4395 if (comparison == UNORDERED)
4397 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4398 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4399 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4400 temp, const_true_rtx, equiv);
4402 else
4404 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4405 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4406 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4407 equiv, true_rtx, false_rtx);
4410 start_sequence ();
4411 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4412 cmp_mode, 2, x, mode, y, mode);
4413 insns = get_insns ();
4414 end_sequence ();
4416 target = gen_reg_rtx (cmp_mode);
4417 emit_libcall_block (insns, target, value, equiv);
4419 if (comparison == UNORDERED
4420 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4421 || reversed_p)
4422 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4423 else
4424 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4426 *pmode = cmp_mode;
4429 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4431 void
4432 emit_indirect_jump (rtx loc)
4434 struct expand_operand ops[1];
4436 create_address_operand (&ops[0], loc);
4437 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4438 emit_barrier ();
4441 #ifdef HAVE_conditional_move
4443 /* Emit a conditional move instruction if the machine supports one for that
4444 condition and machine mode.
4446 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4447 the mode to use should they be constants. If it is VOIDmode, they cannot
4448 both be constants.
4450 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4451 should be stored there. MODE is the mode to use should they be constants.
4452 If it is VOIDmode, they cannot both be constants.
4454 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4455 is not supported. */
4458 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4459 enum machine_mode cmode, rtx op2, rtx op3,
4460 enum machine_mode mode, int unsignedp)
4462 rtx tem, comparison, last;
4463 enum insn_code icode;
4464 enum rtx_code reversed;
4466 /* If one operand is constant, make it the second one. Only do this
4467 if the other operand is not constant as well. */
4469 if (swap_commutative_operands_p (op0, op1))
4471 tem = op0;
4472 op0 = op1;
4473 op1 = tem;
4474 code = swap_condition (code);
4477 /* get_condition will prefer to generate LT and GT even if the old
4478 comparison was against zero, so undo that canonicalization here since
4479 comparisons against zero are cheaper. */
4480 if (code == LT && op1 == const1_rtx)
4481 code = LE, op1 = const0_rtx;
4482 else if (code == GT && op1 == constm1_rtx)
4483 code = GE, op1 = const0_rtx;
4485 if (cmode == VOIDmode)
4486 cmode = GET_MODE (op0);
4488 if (swap_commutative_operands_p (op2, op3)
4489 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4490 != UNKNOWN))
4492 tem = op2;
4493 op2 = op3;
4494 op3 = tem;
4495 code = reversed;
4498 if (mode == VOIDmode)
4499 mode = GET_MODE (op2);
4501 icode = direct_optab_handler (movcc_optab, mode);
4503 if (icode == CODE_FOR_nothing)
4504 return 0;
4506 if (!target)
4507 target = gen_reg_rtx (mode);
4509 code = unsignedp ? unsigned_condition (code) : code;
4510 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4512 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4513 return NULL and let the caller figure out how best to deal with this
4514 situation. */
4515 if (!COMPARISON_P (comparison))
4516 return NULL_RTX;
4518 do_pending_stack_adjust ();
4519 last = get_last_insn ();
4520 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4521 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4522 &comparison, &cmode);
4523 if (comparison)
4525 struct expand_operand ops[4];
4527 create_output_operand (&ops[0], target, mode);
4528 create_fixed_operand (&ops[1], comparison);
4529 create_input_operand (&ops[2], op2, mode);
4530 create_input_operand (&ops[3], op3, mode);
4531 if (maybe_expand_insn (icode, 4, ops))
4533 if (ops[0].value != target)
4534 convert_move (target, ops[0].value, false);
4535 return target;
4538 delete_insns_since (last);
4539 return NULL_RTX;
4542 /* Return nonzero if a conditional move of mode MODE is supported.
4544 This function is for combine so it can tell whether an insn that looks
4545 like a conditional move is actually supported by the hardware. If we
4546 guess wrong we lose a bit on optimization, but that's it. */
4547 /* ??? sparc64 supports conditionally moving integers values based on fp
4548 comparisons, and vice versa. How do we handle them? */
4551 can_conditionally_move_p (enum machine_mode mode)
4553 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4554 return 1;
4556 return 0;
4559 #endif /* HAVE_conditional_move */
4561 /* Emit a conditional addition instruction if the machine supports one for that
4562 condition and machine mode.
4564 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4565 the mode to use should they be constants. If it is VOIDmode, they cannot
4566 both be constants.
4568 OP2 should be stored in TARGET if the comparison is true, otherwise OP2+OP3
4569 should be stored there. MODE is the mode to use should they be constants.
4570 If it is VOIDmode, they cannot both be constants.
4572 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4573 is not supported. */
4576 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4577 enum machine_mode cmode, rtx op2, rtx op3,
4578 enum machine_mode mode, int unsignedp)
4580 rtx tem, comparison, last;
4581 enum insn_code icode;
4582 enum rtx_code reversed;
4584 /* If one operand is constant, make it the second one. Only do this
4585 if the other operand is not constant as well. */
4587 if (swap_commutative_operands_p (op0, op1))
4589 tem = op0;
4590 op0 = op1;
4591 op1 = tem;
4592 code = swap_condition (code);
4595 /* get_condition will prefer to generate LT and GT even if the old
4596 comparison was against zero, so undo that canonicalization here since
4597 comparisons against zero are cheaper. */
4598 if (code == LT && op1 == const1_rtx)
4599 code = LE, op1 = const0_rtx;
4600 else if (code == GT && op1 == constm1_rtx)
4601 code = GE, op1 = const0_rtx;
4603 if (cmode == VOIDmode)
4604 cmode = GET_MODE (op0);
4606 if (swap_commutative_operands_p (op2, op3)
4607 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4608 != UNKNOWN))
4610 tem = op2;
4611 op2 = op3;
4612 op3 = tem;
4613 code = reversed;
4616 if (mode == VOIDmode)
4617 mode = GET_MODE (op2);
4619 icode = optab_handler (addcc_optab, mode);
4621 if (icode == CODE_FOR_nothing)
4622 return 0;
4624 if (!target)
4625 target = gen_reg_rtx (mode);
4627 code = unsignedp ? unsigned_condition (code) : code;
4628 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4630 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4631 return NULL and let the caller figure out how best to deal with this
4632 situation. */
4633 if (!COMPARISON_P (comparison))
4634 return NULL_RTX;
4636 do_pending_stack_adjust ();
4637 last = get_last_insn ();
4638 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4639 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4640 &comparison, &cmode);
4641 if (comparison)
4643 struct expand_operand ops[4];
4645 create_output_operand (&ops[0], target, mode);
4646 create_fixed_operand (&ops[1], comparison);
4647 create_input_operand (&ops[2], op2, mode);
4648 create_input_operand (&ops[3], op3, mode);
4649 if (maybe_expand_insn (icode, 4, ops))
4651 if (ops[0].value != target)
4652 convert_move (target, ops[0].value, false);
4653 return target;
4656 delete_insns_since (last);
4657 return NULL_RTX;
4660 /* These functions attempt to generate an insn body, rather than
4661 emitting the insn, but if the gen function already emits them, we
4662 make no attempt to turn them back into naked patterns. */
4664 /* Generate and return an insn body to add Y to X. */
4667 gen_add2_insn (rtx x, rtx y)
4669 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4671 gcc_assert (insn_operand_matches (icode, 0, x));
4672 gcc_assert (insn_operand_matches (icode, 1, x));
4673 gcc_assert (insn_operand_matches (icode, 2, y));
4675 return GEN_FCN (icode) (x, x, y);
4678 /* Generate and return an insn body to add r1 and c,
4679 storing the result in r0. */
4682 gen_add3_insn (rtx r0, rtx r1, rtx c)
4684 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4686 if (icode == CODE_FOR_nothing
4687 || !insn_operand_matches (icode, 0, r0)
4688 || !insn_operand_matches (icode, 1, r1)
4689 || !insn_operand_matches (icode, 2, c))
4690 return NULL_RTX;
4692 return GEN_FCN (icode) (r0, r1, c);
4696 have_add2_insn (rtx x, rtx y)
4698 enum insn_code icode;
4700 gcc_assert (GET_MODE (x) != VOIDmode);
4702 icode = optab_handler (add_optab, GET_MODE (x));
4704 if (icode == CODE_FOR_nothing)
4705 return 0;
4707 if (!insn_operand_matches (icode, 0, x)
4708 || !insn_operand_matches (icode, 1, x)
4709 || !insn_operand_matches (icode, 2, y))
4710 return 0;
4712 return 1;
4715 /* Generate and return an insn body to subtract Y from X. */
4718 gen_sub2_insn (rtx x, rtx y)
4720 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4722 gcc_assert (insn_operand_matches (icode, 0, x));
4723 gcc_assert (insn_operand_matches (icode, 1, x));
4724 gcc_assert (insn_operand_matches (icode, 2, y));
4726 return GEN_FCN (icode) (x, x, y);
4729 /* Generate and return an insn body to subtract r1 and c,
4730 storing the result in r0. */
4733 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4735 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4737 if (icode == CODE_FOR_nothing
4738 || !insn_operand_matches (icode, 0, r0)
4739 || !insn_operand_matches (icode, 1, r1)
4740 || !insn_operand_matches (icode, 2, c))
4741 return NULL_RTX;
4743 return GEN_FCN (icode) (r0, r1, c);
4747 have_sub2_insn (rtx x, rtx y)
4749 enum insn_code icode;
4751 gcc_assert (GET_MODE (x) != VOIDmode);
4753 icode = optab_handler (sub_optab, GET_MODE (x));
4755 if (icode == CODE_FOR_nothing)
4756 return 0;
4758 if (!insn_operand_matches (icode, 0, x)
4759 || !insn_operand_matches (icode, 1, x)
4760 || !insn_operand_matches (icode, 2, y))
4761 return 0;
4763 return 1;
4766 /* Generate the body of an instruction to copy Y into X.
4767 It may be a list of insns, if one insn isn't enough. */
4770 gen_move_insn (rtx x, rtx y)
4772 rtx seq;
4774 start_sequence ();
4775 emit_move_insn_1 (x, y);
4776 seq = get_insns ();
4777 end_sequence ();
4778 return seq;
4781 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4782 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4783 no such operation exists, CODE_FOR_nothing will be returned. */
4785 enum insn_code
4786 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
4787 int unsignedp)
4789 convert_optab tab;
4790 #ifdef HAVE_ptr_extend
4791 if (unsignedp < 0)
4792 return CODE_FOR_ptr_extend;
4793 #endif
4795 tab = unsignedp ? zext_optab : sext_optab;
4796 return convert_optab_handler (tab, to_mode, from_mode);
4799 /* Generate the body of an insn to extend Y (with mode MFROM)
4800 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4803 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4804 enum machine_mode mfrom, int unsignedp)
4806 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4807 return GEN_FCN (icode) (x, y);
4810 /* can_fix_p and can_float_p say whether the target machine
4811 can directly convert a given fixed point type to
4812 a given floating point type, or vice versa.
4813 The returned value is the CODE_FOR_... value to use,
4814 or CODE_FOR_nothing if these modes cannot be directly converted.
4816 *TRUNCP_PTR is set to 1 if it is necessary to output
4817 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4819 static enum insn_code
4820 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4821 int unsignedp, int *truncp_ptr)
4823 convert_optab tab;
4824 enum insn_code icode;
4826 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4827 icode = convert_optab_handler (tab, fixmode, fltmode);
4828 if (icode != CODE_FOR_nothing)
4830 *truncp_ptr = 0;
4831 return icode;
4834 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4835 for this to work. We need to rework the fix* and ftrunc* patterns
4836 and documentation. */
4837 tab = unsignedp ? ufix_optab : sfix_optab;
4838 icode = convert_optab_handler (tab, fixmode, fltmode);
4839 if (icode != CODE_FOR_nothing
4840 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4842 *truncp_ptr = 1;
4843 return icode;
4846 *truncp_ptr = 0;
4847 return CODE_FOR_nothing;
4850 enum insn_code
4851 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4852 int unsignedp)
4854 convert_optab tab;
4856 tab = unsignedp ? ufloat_optab : sfloat_optab;
4857 return convert_optab_handler (tab, fltmode, fixmode);
4860 /* Function supportable_convert_operation
4862 Check whether an operation represented by the code CODE is a
4863 convert operation that is supported by the target platform in
4864 vector form (i.e., when operating on arguments of type VECTYPE_IN
4865 producing a result of type VECTYPE_OUT).
4867 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4868 This function checks if these operations are supported
4869 by the target platform either directly (via vector tree-codes), or via
4870 target builtins.
4872 Output:
4873 - CODE1 is code of vector operation to be used when
4874 vectorizing the operation, if available.
4875 - DECL is decl of target builtin functions to be used
4876 when vectorizing the operation, if available. In this case,
4877 CODE1 is CALL_EXPR. */
4879 bool
4880 supportable_convert_operation (enum tree_code code,
4881 tree vectype_out, tree vectype_in,
4882 tree *decl, enum tree_code *code1)
4884 enum machine_mode m1,m2;
4885 int truncp;
4887 m1 = TYPE_MODE (vectype_out);
4888 m2 = TYPE_MODE (vectype_in);
4890 /* First check if we can done conversion directly. */
4891 if ((code == FIX_TRUNC_EXPR
4892 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
4893 != CODE_FOR_nothing)
4894 || (code == FLOAT_EXPR
4895 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
4896 != CODE_FOR_nothing))
4898 *code1 = code;
4899 return true;
4902 /* Now check for builtin. */
4903 if (targetm.vectorize.builtin_conversion
4904 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
4906 *code1 = CALL_EXPR;
4907 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
4908 return true;
4910 return false;
4914 /* Generate code to convert FROM to floating point
4915 and store in TO. FROM must be fixed point and not VOIDmode.
4916 UNSIGNEDP nonzero means regard FROM as unsigned.
4917 Normally this is done by correcting the final value
4918 if it is negative. */
4920 void
4921 expand_float (rtx to, rtx from, int unsignedp)
4923 enum insn_code icode;
4924 rtx target = to;
4925 enum machine_mode fmode, imode;
4926 bool can_do_signed = false;
4928 /* Crash now, because we won't be able to decide which mode to use. */
4929 gcc_assert (GET_MODE (from) != VOIDmode);
4931 /* Look for an insn to do the conversion. Do it in the specified
4932 modes if possible; otherwise convert either input, output or both to
4933 wider mode. If the integer mode is wider than the mode of FROM,
4934 we can do the conversion signed even if the input is unsigned. */
4936 for (fmode = GET_MODE (to); fmode != VOIDmode;
4937 fmode = GET_MODE_WIDER_MODE (fmode))
4938 for (imode = GET_MODE (from); imode != VOIDmode;
4939 imode = GET_MODE_WIDER_MODE (imode))
4941 int doing_unsigned = unsignedp;
4943 if (fmode != GET_MODE (to)
4944 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
4945 continue;
4947 icode = can_float_p (fmode, imode, unsignedp);
4948 if (icode == CODE_FOR_nothing && unsignedp)
4950 enum insn_code scode = can_float_p (fmode, imode, 0);
4951 if (scode != CODE_FOR_nothing)
4952 can_do_signed = true;
4953 if (imode != GET_MODE (from))
4954 icode = scode, doing_unsigned = 0;
4957 if (icode != CODE_FOR_nothing)
4959 if (imode != GET_MODE (from))
4960 from = convert_to_mode (imode, from, unsignedp);
4962 if (fmode != GET_MODE (to))
4963 target = gen_reg_rtx (fmode);
4965 emit_unop_insn (icode, target, from,
4966 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4968 if (target != to)
4969 convert_move (to, target, 0);
4970 return;
4974 /* Unsigned integer, and no way to convert directly. Convert as signed,
4975 then unconditionally adjust the result. */
4976 if (unsignedp && can_do_signed)
4978 rtx label = gen_label_rtx ();
4979 rtx temp;
4980 REAL_VALUE_TYPE offset;
4982 /* Look for a usable floating mode FMODE wider than the source and at
4983 least as wide as the target. Using FMODE will avoid rounding woes
4984 with unsigned values greater than the signed maximum value. */
4986 for (fmode = GET_MODE (to); fmode != VOIDmode;
4987 fmode = GET_MODE_WIDER_MODE (fmode))
4988 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
4989 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
4990 break;
4992 if (fmode == VOIDmode)
4994 /* There is no such mode. Pretend the target is wide enough. */
4995 fmode = GET_MODE (to);
4997 /* Avoid double-rounding when TO is narrower than FROM. */
4998 if ((significand_size (fmode) + 1)
4999 < GET_MODE_PRECISION (GET_MODE (from)))
5001 rtx temp1;
5002 rtx neglabel = gen_label_rtx ();
5004 /* Don't use TARGET if it isn't a register, is a hard register,
5005 or is the wrong mode. */
5006 if (!REG_P (target)
5007 || REGNO (target) < FIRST_PSEUDO_REGISTER
5008 || GET_MODE (target) != fmode)
5009 target = gen_reg_rtx (fmode);
5011 imode = GET_MODE (from);
5012 do_pending_stack_adjust ();
5014 /* Test whether the sign bit is set. */
5015 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5016 0, neglabel);
5018 /* The sign bit is not set. Convert as signed. */
5019 expand_float (target, from, 0);
5020 emit_jump_insn (gen_jump (label));
5021 emit_barrier ();
5023 /* The sign bit is set.
5024 Convert to a usable (positive signed) value by shifting right
5025 one bit, while remembering if a nonzero bit was shifted
5026 out; i.e., compute (from & 1) | (from >> 1). */
5028 emit_label (neglabel);
5029 temp = expand_binop (imode, and_optab, from, const1_rtx,
5030 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5031 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5032 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5033 OPTAB_LIB_WIDEN);
5034 expand_float (target, temp, 0);
5036 /* Multiply by 2 to undo the shift above. */
5037 temp = expand_binop (fmode, add_optab, target, target,
5038 target, 0, OPTAB_LIB_WIDEN);
5039 if (temp != target)
5040 emit_move_insn (target, temp);
5042 do_pending_stack_adjust ();
5043 emit_label (label);
5044 goto done;
5048 /* If we are about to do some arithmetic to correct for an
5049 unsigned operand, do it in a pseudo-register. */
5051 if (GET_MODE (to) != fmode
5052 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5053 target = gen_reg_rtx (fmode);
5055 /* Convert as signed integer to floating. */
5056 expand_float (target, from, 0);
5058 /* If FROM is negative (and therefore TO is negative),
5059 correct its value by 2**bitwidth. */
5061 do_pending_stack_adjust ();
5062 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5063 0, label);
5066 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5067 temp = expand_binop (fmode, add_optab, target,
5068 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5069 target, 0, OPTAB_LIB_WIDEN);
5070 if (temp != target)
5071 emit_move_insn (target, temp);
5073 do_pending_stack_adjust ();
5074 emit_label (label);
5075 goto done;
5078 /* No hardware instruction available; call a library routine. */
5080 rtx libfunc;
5081 rtx insns;
5082 rtx value;
5083 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5085 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
5086 from = convert_to_mode (SImode, from, unsignedp);
5088 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5089 gcc_assert (libfunc);
5091 start_sequence ();
5093 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5094 GET_MODE (to), 1, from,
5095 GET_MODE (from));
5096 insns = get_insns ();
5097 end_sequence ();
5099 emit_libcall_block (insns, target, value,
5100 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5101 GET_MODE (to), from));
5104 done:
5106 /* Copy result to requested destination
5107 if we have been computing in a temp location. */
5109 if (target != to)
5111 if (GET_MODE (target) == GET_MODE (to))
5112 emit_move_insn (to, target);
5113 else
5114 convert_move (to, target, 0);
5118 /* Generate code to convert FROM to fixed point and store in TO. FROM
5119 must be floating point. */
5121 void
5122 expand_fix (rtx to, rtx from, int unsignedp)
5124 enum insn_code icode;
5125 rtx target = to;
5126 enum machine_mode fmode, imode;
5127 int must_trunc = 0;
5129 /* We first try to find a pair of modes, one real and one integer, at
5130 least as wide as FROM and TO, respectively, in which we can open-code
5131 this conversion. If the integer mode is wider than the mode of TO,
5132 we can do the conversion either signed or unsigned. */
5134 for (fmode = GET_MODE (from); fmode != VOIDmode;
5135 fmode = GET_MODE_WIDER_MODE (fmode))
5136 for (imode = GET_MODE (to); imode != VOIDmode;
5137 imode = GET_MODE_WIDER_MODE (imode))
5139 int doing_unsigned = unsignedp;
5141 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5142 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5143 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5145 if (icode != CODE_FOR_nothing)
5147 rtx last = get_last_insn ();
5148 if (fmode != GET_MODE (from))
5149 from = convert_to_mode (fmode, from, 0);
5151 if (must_trunc)
5153 rtx temp = gen_reg_rtx (GET_MODE (from));
5154 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5155 temp, 0);
5158 if (imode != GET_MODE (to))
5159 target = gen_reg_rtx (imode);
5161 if (maybe_emit_unop_insn (icode, target, from,
5162 doing_unsigned ? UNSIGNED_FIX : FIX))
5164 if (target != to)
5165 convert_move (to, target, unsignedp);
5166 return;
5168 delete_insns_since (last);
5172 /* For an unsigned conversion, there is one more way to do it.
5173 If we have a signed conversion, we generate code that compares
5174 the real value to the largest representable positive number. If if
5175 is smaller, the conversion is done normally. Otherwise, subtract
5176 one plus the highest signed number, convert, and add it back.
5178 We only need to check all real modes, since we know we didn't find
5179 anything with a wider integer mode.
5181 This code used to extend FP value into mode wider than the destination.
5182 This is needed for decimal float modes which cannot accurately
5183 represent one plus the highest signed number of the same size, but
5184 not for binary modes. Consider, for instance conversion from SFmode
5185 into DImode.
5187 The hot path through the code is dealing with inputs smaller than 2^63
5188 and doing just the conversion, so there is no bits to lose.
5190 In the other path we know the value is positive in the range 2^63..2^64-1
5191 inclusive. (as for other input overflow happens and result is undefined)
5192 So we know that the most important bit set in mantissa corresponds to
5193 2^63. The subtraction of 2^63 should not generate any rounding as it
5194 simply clears out that bit. The rest is trivial. */
5196 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5197 for (fmode = GET_MODE (from); fmode != VOIDmode;
5198 fmode = GET_MODE_WIDER_MODE (fmode))
5199 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5200 && (!DECIMAL_FLOAT_MODE_P (fmode)
5201 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5203 int bitsize;
5204 REAL_VALUE_TYPE offset;
5205 rtx limit, lab1, lab2, insn;
5207 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5208 real_2expN (&offset, bitsize - 1, fmode);
5209 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5210 lab1 = gen_label_rtx ();
5211 lab2 = gen_label_rtx ();
5213 if (fmode != GET_MODE (from))
5214 from = convert_to_mode (fmode, from, 0);
5216 /* See if we need to do the subtraction. */
5217 do_pending_stack_adjust ();
5218 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5219 0, lab1);
5221 /* If not, do the signed "fix" and branch around fixup code. */
5222 expand_fix (to, from, 0);
5223 emit_jump_insn (gen_jump (lab2));
5224 emit_barrier ();
5226 /* Otherwise, subtract 2**(N-1), convert to signed number,
5227 then add 2**(N-1). Do the addition using XOR since this
5228 will often generate better code. */
5229 emit_label (lab1);
5230 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5231 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5232 expand_fix (to, target, 0);
5233 target = expand_binop (GET_MODE (to), xor_optab, to,
5234 gen_int_mode
5235 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5236 GET_MODE (to)),
5237 to, 1, OPTAB_LIB_WIDEN);
5239 if (target != to)
5240 emit_move_insn (to, target);
5242 emit_label (lab2);
5244 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5246 /* Make a place for a REG_NOTE and add it. */
5247 insn = emit_move_insn (to, to);
5248 set_dst_reg_note (insn, REG_EQUAL,
5249 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5250 copy_rtx (from)),
5251 to);
5254 return;
5257 /* We can't do it with an insn, so use a library call. But first ensure
5258 that the mode of TO is at least as wide as SImode, since those are the
5259 only library calls we know about. */
5261 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
5263 target = gen_reg_rtx (SImode);
5265 expand_fix (target, from, unsignedp);
5267 else
5269 rtx insns;
5270 rtx value;
5271 rtx libfunc;
5273 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5274 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5275 gcc_assert (libfunc);
5277 start_sequence ();
5279 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5280 GET_MODE (to), 1, from,
5281 GET_MODE (from));
5282 insns = get_insns ();
5283 end_sequence ();
5285 emit_libcall_block (insns, target, value,
5286 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5287 GET_MODE (to), from));
5290 if (target != to)
5292 if (GET_MODE (to) == GET_MODE (target))
5293 emit_move_insn (to, target);
5294 else
5295 convert_move (to, target, 0);
5299 /* Generate code to convert FROM or TO a fixed-point.
5300 If UINTP is true, either TO or FROM is an unsigned integer.
5301 If SATP is true, we need to saturate the result. */
5303 void
5304 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5306 enum machine_mode to_mode = GET_MODE (to);
5307 enum machine_mode from_mode = GET_MODE (from);
5308 convert_optab tab;
5309 enum rtx_code this_code;
5310 enum insn_code code;
5311 rtx insns, value;
5312 rtx libfunc;
5314 if (to_mode == from_mode)
5316 emit_move_insn (to, from);
5317 return;
5320 if (uintp)
5322 tab = satp ? satfractuns_optab : fractuns_optab;
5323 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5325 else
5327 tab = satp ? satfract_optab : fract_optab;
5328 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5330 code = convert_optab_handler (tab, to_mode, from_mode);
5331 if (code != CODE_FOR_nothing)
5333 emit_unop_insn (code, to, from, this_code);
5334 return;
5337 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5338 gcc_assert (libfunc);
5340 start_sequence ();
5341 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5342 1, from, from_mode);
5343 insns = get_insns ();
5344 end_sequence ();
5346 emit_libcall_block (insns, to, value,
5347 gen_rtx_fmt_e (tab->code, to_mode, from));
5350 /* Generate code to convert FROM to fixed point and store in TO. FROM
5351 must be floating point, TO must be signed. Use the conversion optab
5352 TAB to do the conversion. */
5354 bool
5355 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5357 enum insn_code icode;
5358 rtx target = to;
5359 enum machine_mode fmode, imode;
5361 /* We first try to find a pair of modes, one real and one integer, at
5362 least as wide as FROM and TO, respectively, in which we can open-code
5363 this conversion. If the integer mode is wider than the mode of TO,
5364 we can do the conversion either signed or unsigned. */
5366 for (fmode = GET_MODE (from); fmode != VOIDmode;
5367 fmode = GET_MODE_WIDER_MODE (fmode))
5368 for (imode = GET_MODE (to); imode != VOIDmode;
5369 imode = GET_MODE_WIDER_MODE (imode))
5371 icode = convert_optab_handler (tab, imode, fmode);
5372 if (icode != CODE_FOR_nothing)
5374 rtx last = get_last_insn ();
5375 if (fmode != GET_MODE (from))
5376 from = convert_to_mode (fmode, from, 0);
5378 if (imode != GET_MODE (to))
5379 target = gen_reg_rtx (imode);
5381 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5383 delete_insns_since (last);
5384 continue;
5386 if (target != to)
5387 convert_move (to, target, 0);
5388 return true;
5392 return false;
5395 /* Report whether we have an instruction to perform the operation
5396 specified by CODE on operands of mode MODE. */
5398 have_insn_for (enum rtx_code code, enum machine_mode mode)
5400 return (code_to_optab[(int) code] != 0
5401 && (optab_handler (code_to_optab[(int) code], mode)
5402 != CODE_FOR_nothing));
5405 /* Set all insn_code fields to CODE_FOR_nothing. */
5407 static void
5408 init_insn_codes (void)
5410 memset (optab_table, 0, sizeof (optab_table));
5411 memset (convert_optab_table, 0, sizeof (convert_optab_table));
5412 memset (direct_optab_table, 0, sizeof (direct_optab_table));
5415 /* Initialize OP's code to CODE, and write it into the code_to_optab table. */
5416 static inline void
5417 init_optab (optab op, enum rtx_code code)
5419 op->code = code;
5420 code_to_optab[(int) code] = op;
5423 /* Same, but fill in its code as CODE, and do _not_ write it into
5424 the code_to_optab table. */
5425 static inline void
5426 init_optabv (optab op, enum rtx_code code)
5428 op->code = code;
5431 /* Conversion optabs never go in the code_to_optab table. */
5432 static void
5433 init_convert_optab (convert_optab op, enum rtx_code code)
5435 op->code = code;
5438 /* Initialize the libfunc fields of an entire group of entries in some
5439 optab. Each entry is set equal to a string consisting of a leading
5440 pair of underscores followed by a generic operation name followed by
5441 a mode name (downshifted to lowercase) followed by a single character
5442 representing the number of operands for the given operation (which is
5443 usually one of the characters '2', '3', or '4').
5445 OPTABLE is the table in which libfunc fields are to be initialized.
5446 OPNAME is the generic (string) name of the operation.
5447 SUFFIX is the character which specifies the number of operands for
5448 the given generic operation.
5449 MODE is the mode to generate for.
5452 static void
5453 gen_libfunc (optab optable, const char *opname, int suffix, enum machine_mode mode)
5455 unsigned opname_len = strlen (opname);
5456 const char *mname = GET_MODE_NAME (mode);
5457 unsigned mname_len = strlen (mname);
5458 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5459 int len = prefix_len + opname_len + mname_len + 1 + 1;
5460 char *libfunc_name = XALLOCAVEC (char, len);
5461 char *p;
5462 const char *q;
5464 p = libfunc_name;
5465 *p++ = '_';
5466 *p++ = '_';
5467 if (targetm.libfunc_gnu_prefix)
5469 *p++ = 'g';
5470 *p++ = 'n';
5471 *p++ = 'u';
5472 *p++ = '_';
5474 for (q = opname; *q; )
5475 *p++ = *q++;
5476 for (q = mname; *q; q++)
5477 *p++ = TOLOWER (*q);
5478 *p++ = suffix;
5479 *p = '\0';
5481 set_optab_libfunc (optable, mode,
5482 ggc_alloc_string (libfunc_name, p - libfunc_name));
5485 /* Like gen_libfunc, but verify that integer operation is involved. */
5487 static void
5488 gen_int_libfunc (optab optable, const char *opname, char suffix,
5489 enum machine_mode mode)
5491 int maxsize = 2 * BITS_PER_WORD;
5493 if (GET_MODE_CLASS (mode) != MODE_INT)
5494 return;
5495 if (maxsize < LONG_LONG_TYPE_SIZE)
5496 maxsize = LONG_LONG_TYPE_SIZE;
5497 if (GET_MODE_CLASS (mode) != MODE_INT
5498 || mode < word_mode || GET_MODE_BITSIZE (mode) > maxsize)
5499 return;
5500 gen_libfunc (optable, opname, suffix, mode);
5503 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5505 static void
5506 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5507 enum machine_mode mode)
5509 char *dec_opname;
5511 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5512 gen_libfunc (optable, opname, suffix, mode);
5513 if (DECIMAL_FLOAT_MODE_P (mode))
5515 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5516 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5517 depending on the low level floating format used. */
5518 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5519 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5520 gen_libfunc (optable, dec_opname, suffix, mode);
5524 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5526 static void
5527 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5528 enum machine_mode mode)
5530 if (!ALL_FIXED_POINT_MODE_P (mode))
5531 return;
5532 gen_libfunc (optable, opname, suffix, mode);
5535 /* Like gen_libfunc, but verify that signed fixed-point operation is
5536 involved. */
5538 static void
5539 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5540 enum machine_mode mode)
5542 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5543 return;
5544 gen_libfunc (optable, opname, suffix, mode);
5547 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5548 involved. */
5550 static void
5551 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5552 enum machine_mode mode)
5554 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5555 return;
5556 gen_libfunc (optable, opname, suffix, mode);
5559 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5561 static void
5562 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5563 enum machine_mode mode)
5565 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5566 gen_fp_libfunc (optable, name, suffix, mode);
5567 if (INTEGRAL_MODE_P (mode))
5568 gen_int_libfunc (optable, name, suffix, mode);
5571 /* Like gen_libfunc, but verify that FP or INT operation is involved
5572 and add 'v' suffix for integer operation. */
5574 static void
5575 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5576 enum machine_mode mode)
5578 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5579 gen_fp_libfunc (optable, name, suffix, mode);
5580 if (GET_MODE_CLASS (mode) == MODE_INT)
5582 int len = strlen (name);
5583 char *v_name = XALLOCAVEC (char, len + 2);
5584 strcpy (v_name, name);
5585 v_name[len] = 'v';
5586 v_name[len + 1] = 0;
5587 gen_int_libfunc (optable, v_name, suffix, mode);
5591 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5592 involved. */
5594 static void
5595 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5596 enum machine_mode mode)
5598 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5599 gen_fp_libfunc (optable, name, suffix, mode);
5600 if (INTEGRAL_MODE_P (mode))
5601 gen_int_libfunc (optable, name, suffix, mode);
5602 if (ALL_FIXED_POINT_MODE_P (mode))
5603 gen_fixed_libfunc (optable, name, suffix, mode);
5606 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5607 involved. */
5609 static void
5610 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5611 enum machine_mode mode)
5613 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5614 gen_fp_libfunc (optable, name, suffix, mode);
5615 if (INTEGRAL_MODE_P (mode))
5616 gen_int_libfunc (optable, name, suffix, mode);
5617 if (SIGNED_FIXED_POINT_MODE_P (mode))
5618 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5621 /* Like gen_libfunc, but verify that INT or FIXED operation is
5622 involved. */
5624 static void
5625 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5626 enum machine_mode mode)
5628 if (INTEGRAL_MODE_P (mode))
5629 gen_int_libfunc (optable, name, suffix, mode);
5630 if (ALL_FIXED_POINT_MODE_P (mode))
5631 gen_fixed_libfunc (optable, name, suffix, mode);
5634 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5635 involved. */
5637 static void
5638 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5639 enum machine_mode mode)
5641 if (INTEGRAL_MODE_P (mode))
5642 gen_int_libfunc (optable, name, suffix, mode);
5643 if (SIGNED_FIXED_POINT_MODE_P (mode))
5644 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5647 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5648 involved. */
5650 static void
5651 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5652 enum machine_mode mode)
5654 if (INTEGRAL_MODE_P (mode))
5655 gen_int_libfunc (optable, name, suffix, mode);
5656 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5657 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5660 /* Initialize the libfunc fields of an entire group of entries of an
5661 inter-mode-class conversion optab. The string formation rules are
5662 similar to the ones for init_libfuncs, above, but instead of having
5663 a mode name and an operand count these functions have two mode names
5664 and no operand count. */
5666 static void
5667 gen_interclass_conv_libfunc (convert_optab tab,
5668 const char *opname,
5669 enum machine_mode tmode,
5670 enum machine_mode fmode)
5672 size_t opname_len = strlen (opname);
5673 size_t mname_len = 0;
5675 const char *fname, *tname;
5676 const char *q;
5677 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5678 char *libfunc_name, *suffix;
5679 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5680 char *p;
5682 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5683 depends on which underlying decimal floating point format is used. */
5684 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5686 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5688 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5689 nondec_name[0] = '_';
5690 nondec_name[1] = '_';
5691 if (targetm.libfunc_gnu_prefix)
5693 nondec_name[2] = 'g';
5694 nondec_name[3] = 'n';
5695 nondec_name[4] = 'u';
5696 nondec_name[5] = '_';
5699 memcpy (&nondec_name[prefix_len], opname, opname_len);
5700 nondec_suffix = nondec_name + opname_len + prefix_len;
5702 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5703 dec_name[0] = '_';
5704 dec_name[1] = '_';
5705 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5706 memcpy (&dec_name[2+dec_len], opname, opname_len);
5707 dec_suffix = dec_name + dec_len + opname_len + 2;
5709 fname = GET_MODE_NAME (fmode);
5710 tname = GET_MODE_NAME (tmode);
5712 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5714 libfunc_name = dec_name;
5715 suffix = dec_suffix;
5717 else
5719 libfunc_name = nondec_name;
5720 suffix = nondec_suffix;
5723 p = suffix;
5724 for (q = fname; *q; p++, q++)
5725 *p = TOLOWER (*q);
5726 for (q = tname; *q; p++, q++)
5727 *p = TOLOWER (*q);
5729 *p = '\0';
5731 set_conv_libfunc (tab, tmode, fmode,
5732 ggc_alloc_string (libfunc_name, p - libfunc_name));
5735 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5736 int->fp conversion. */
5738 static void
5739 gen_int_to_fp_conv_libfunc (convert_optab tab,
5740 const char *opname,
5741 enum machine_mode tmode,
5742 enum machine_mode fmode)
5744 if (GET_MODE_CLASS (fmode) != MODE_INT)
5745 return;
5746 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5747 return;
5748 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5751 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5752 naming scheme. */
5754 static void
5755 gen_ufloat_conv_libfunc (convert_optab tab,
5756 const char *opname ATTRIBUTE_UNUSED,
5757 enum machine_mode tmode,
5758 enum machine_mode fmode)
5760 if (DECIMAL_FLOAT_MODE_P (tmode))
5761 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5762 else
5763 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5766 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5767 fp->int conversion. */
5769 static void
5770 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5771 const char *opname,
5772 enum machine_mode tmode,
5773 enum machine_mode fmode)
5775 if (GET_MODE_CLASS (fmode) != MODE_INT)
5776 return;
5777 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5778 return;
5779 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5782 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5783 fp->int conversion with no decimal floating point involved. */
5785 static void
5786 gen_fp_to_int_conv_libfunc (convert_optab tab,
5787 const char *opname,
5788 enum machine_mode tmode,
5789 enum machine_mode fmode)
5791 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5792 return;
5793 if (GET_MODE_CLASS (tmode) != MODE_INT)
5794 return;
5795 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5798 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5799 The string formation rules are
5800 similar to the ones for init_libfunc, above. */
5802 static void
5803 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5804 enum machine_mode tmode, enum machine_mode fmode)
5806 size_t opname_len = strlen (opname);
5807 size_t mname_len = 0;
5809 const char *fname, *tname;
5810 const char *q;
5811 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5812 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5813 char *libfunc_name, *suffix;
5814 char *p;
5816 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5817 depends on which underlying decimal floating point format is used. */
5818 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5820 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5822 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5823 nondec_name[0] = '_';
5824 nondec_name[1] = '_';
5825 if (targetm.libfunc_gnu_prefix)
5827 nondec_name[2] = 'g';
5828 nondec_name[3] = 'n';
5829 nondec_name[4] = 'u';
5830 nondec_name[5] = '_';
5832 memcpy (&nondec_name[prefix_len], opname, opname_len);
5833 nondec_suffix = nondec_name + opname_len + prefix_len;
5835 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5836 dec_name[0] = '_';
5837 dec_name[1] = '_';
5838 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5839 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5840 dec_suffix = dec_name + dec_len + opname_len + 2;
5842 fname = GET_MODE_NAME (fmode);
5843 tname = GET_MODE_NAME (tmode);
5845 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5847 libfunc_name = dec_name;
5848 suffix = dec_suffix;
5850 else
5852 libfunc_name = nondec_name;
5853 suffix = nondec_suffix;
5856 p = suffix;
5857 for (q = fname; *q; p++, q++)
5858 *p = TOLOWER (*q);
5859 for (q = tname; *q; p++, q++)
5860 *p = TOLOWER (*q);
5862 *p++ = '2';
5863 *p = '\0';
5865 set_conv_libfunc (tab, tmode, fmode,
5866 ggc_alloc_string (libfunc_name, p - libfunc_name));
5869 /* Pick proper libcall for trunc_optab. We need to chose if we do
5870 truncation or extension and interclass or intraclass. */
5872 static void
5873 gen_trunc_conv_libfunc (convert_optab tab,
5874 const char *opname,
5875 enum machine_mode tmode,
5876 enum machine_mode fmode)
5878 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5879 return;
5880 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5881 return;
5882 if (tmode == fmode)
5883 return;
5885 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5886 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5887 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5889 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5890 return;
5892 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5893 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5894 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5895 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5898 /* Pick proper libcall for extend_optab. We need to chose if we do
5899 truncation or extension and interclass or intraclass. */
5901 static void
5902 gen_extend_conv_libfunc (convert_optab tab,
5903 const char *opname ATTRIBUTE_UNUSED,
5904 enum machine_mode tmode,
5905 enum machine_mode fmode)
5907 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5908 return;
5909 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5910 return;
5911 if (tmode == fmode)
5912 return;
5914 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5915 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5916 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5918 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5919 return;
5921 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5922 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5923 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5924 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5927 /* Pick proper libcall for fract_optab. We need to chose if we do
5928 interclass or intraclass. */
5930 static void
5931 gen_fract_conv_libfunc (convert_optab tab,
5932 const char *opname,
5933 enum machine_mode tmode,
5934 enum machine_mode fmode)
5936 if (tmode == fmode)
5937 return;
5938 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5939 return;
5941 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5942 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5943 else
5944 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5947 /* Pick proper libcall for fractuns_optab. */
5949 static void
5950 gen_fractuns_conv_libfunc (convert_optab tab,
5951 const char *opname,
5952 enum machine_mode tmode,
5953 enum machine_mode fmode)
5955 if (tmode == fmode)
5956 return;
5957 /* One mode must be a fixed-point mode, and the other must be an integer
5958 mode. */
5959 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
5960 || (ALL_FIXED_POINT_MODE_P (fmode)
5961 && GET_MODE_CLASS (tmode) == MODE_INT)))
5962 return;
5964 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5967 /* Pick proper libcall for satfract_optab. We need to chose if we do
5968 interclass or intraclass. */
5970 static void
5971 gen_satfract_conv_libfunc (convert_optab tab,
5972 const char *opname,
5973 enum machine_mode tmode,
5974 enum machine_mode fmode)
5976 if (tmode == fmode)
5977 return;
5978 /* TMODE must be a fixed-point mode. */
5979 if (!ALL_FIXED_POINT_MODE_P (tmode))
5980 return;
5982 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5983 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5984 else
5985 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5988 /* Pick proper libcall for satfractuns_optab. */
5990 static void
5991 gen_satfractuns_conv_libfunc (convert_optab tab,
5992 const char *opname,
5993 enum machine_mode tmode,
5994 enum machine_mode fmode)
5996 if (tmode == fmode)
5997 return;
5998 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
5999 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
6000 return;
6002 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6005 /* A table of previously-created libfuncs, hashed by name. */
6006 static GTY ((param_is (union tree_node))) htab_t libfunc_decls;
6008 /* Hashtable callbacks for libfunc_decls. */
6010 static hashval_t
6011 libfunc_decl_hash (const void *entry)
6013 return IDENTIFIER_HASH_VALUE (DECL_NAME ((const_tree) entry));
6016 static int
6017 libfunc_decl_eq (const void *entry1, const void *entry2)
6019 return DECL_NAME ((const_tree) entry1) == (const_tree) entry2;
6022 /* Build a decl for a libfunc named NAME. */
6024 tree
6025 build_libfunc_function (const char *name)
6027 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6028 get_identifier (name),
6029 build_function_type (integer_type_node, NULL_TREE));
6030 /* ??? We don't have any type information except for this is
6031 a function. Pretend this is "int foo()". */
6032 DECL_ARTIFICIAL (decl) = 1;
6033 DECL_EXTERNAL (decl) = 1;
6034 TREE_PUBLIC (decl) = 1;
6035 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6037 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6038 are the flags assigned by targetm.encode_section_info. */
6039 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6041 return decl;
6045 init_one_libfunc (const char *name)
6047 tree id, decl;
6048 void **slot;
6049 hashval_t hash;
6051 if (libfunc_decls == NULL)
6052 libfunc_decls = htab_create_ggc (37, libfunc_decl_hash,
6053 libfunc_decl_eq, NULL);
6055 /* See if we have already created a libfunc decl for this function. */
6056 id = get_identifier (name);
6057 hash = IDENTIFIER_HASH_VALUE (id);
6058 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, INSERT);
6059 decl = (tree) *slot;
6060 if (decl == NULL)
6062 /* Create a new decl, so that it can be passed to
6063 targetm.encode_section_info. */
6064 decl = build_libfunc_function (name);
6065 *slot = decl;
6067 return XEXP (DECL_RTL (decl), 0);
6070 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6073 set_user_assembler_libfunc (const char *name, const char *asmspec)
6075 tree id, decl;
6076 void **slot;
6077 hashval_t hash;
6079 id = get_identifier (name);
6080 hash = IDENTIFIER_HASH_VALUE (id);
6081 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, NO_INSERT);
6082 gcc_assert (slot);
6083 decl = (tree) *slot;
6084 set_user_assembler_name (decl, asmspec);
6085 return XEXP (DECL_RTL (decl), 0);
6088 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6089 MODE to NAME, which should be either 0 or a string constant. */
6090 void
6091 set_optab_libfunc (optab optable, enum machine_mode mode, const char *name)
6093 rtx val;
6094 struct libfunc_entry e;
6095 struct libfunc_entry **slot;
6096 e.optab = (size_t) (optable - &optab_table[0]);
6097 e.mode1 = mode;
6098 e.mode2 = VOIDmode;
6100 if (name)
6101 val = init_one_libfunc (name);
6102 else
6103 val = 0;
6104 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6105 if (*slot == NULL)
6106 *slot = ggc_alloc_libfunc_entry ();
6107 (*slot)->optab = (size_t) (optable - &optab_table[0]);
6108 (*slot)->mode1 = mode;
6109 (*slot)->mode2 = VOIDmode;
6110 (*slot)->libfunc = val;
6113 /* Call this to reset the function entry for one conversion optab
6114 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6115 either 0 or a string constant. */
6116 void
6117 set_conv_libfunc (convert_optab optable, enum machine_mode tmode,
6118 enum machine_mode fmode, const char *name)
6120 rtx val;
6121 struct libfunc_entry e;
6122 struct libfunc_entry **slot;
6123 e.optab = (size_t) (optable - &convert_optab_table[0]);
6124 e.mode1 = tmode;
6125 e.mode2 = fmode;
6127 if (name)
6128 val = init_one_libfunc (name);
6129 else
6130 val = 0;
6131 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6132 if (*slot == NULL)
6133 *slot = ggc_alloc_libfunc_entry ();
6134 (*slot)->optab = (size_t) (optable - &convert_optab_table[0]);
6135 (*slot)->mode1 = tmode;
6136 (*slot)->mode2 = fmode;
6137 (*slot)->libfunc = val;
6140 /* Call this to initialize the contents of the optabs
6141 appropriately for the current target machine. */
6143 void
6144 init_optabs (void)
6146 if (libfunc_hash)
6148 htab_empty (libfunc_hash);
6149 /* We statically initialize the insn_codes with the equivalent of
6150 CODE_FOR_nothing. Repeat the process if reinitialising. */
6151 init_insn_codes ();
6153 else
6154 libfunc_hash = htab_create_ggc (10, hash_libfunc, eq_libfunc, NULL);
6156 init_optab (add_optab, PLUS);
6157 init_optabv (addv_optab, PLUS);
6158 init_optab (sub_optab, MINUS);
6159 init_optabv (subv_optab, MINUS);
6160 init_optab (ssadd_optab, SS_PLUS);
6161 init_optab (usadd_optab, US_PLUS);
6162 init_optab (sssub_optab, SS_MINUS);
6163 init_optab (ussub_optab, US_MINUS);
6164 init_optab (smul_optab, MULT);
6165 init_optab (ssmul_optab, SS_MULT);
6166 init_optab (usmul_optab, US_MULT);
6167 init_optabv (smulv_optab, MULT);
6168 init_optab (smul_highpart_optab, UNKNOWN);
6169 init_optab (umul_highpart_optab, UNKNOWN);
6170 init_optab (smul_widen_optab, UNKNOWN);
6171 init_optab (umul_widen_optab, UNKNOWN);
6172 init_optab (usmul_widen_optab, UNKNOWN);
6173 init_optab (smadd_widen_optab, UNKNOWN);
6174 init_optab (umadd_widen_optab, UNKNOWN);
6175 init_optab (ssmadd_widen_optab, UNKNOWN);
6176 init_optab (usmadd_widen_optab, UNKNOWN);
6177 init_optab (smsub_widen_optab, UNKNOWN);
6178 init_optab (umsub_widen_optab, UNKNOWN);
6179 init_optab (ssmsub_widen_optab, UNKNOWN);
6180 init_optab (usmsub_widen_optab, UNKNOWN);
6181 init_optab (sdiv_optab, DIV);
6182 init_optab (ssdiv_optab, SS_DIV);
6183 init_optab (usdiv_optab, US_DIV);
6184 init_optabv (sdivv_optab, DIV);
6185 init_optab (sdivmod_optab, UNKNOWN);
6186 init_optab (udiv_optab, UDIV);
6187 init_optab (udivmod_optab, UNKNOWN);
6188 init_optab (smod_optab, MOD);
6189 init_optab (umod_optab, UMOD);
6190 init_optab (fmod_optab, UNKNOWN);
6191 init_optab (remainder_optab, UNKNOWN);
6192 init_optab (ftrunc_optab, UNKNOWN);
6193 init_optab (and_optab, AND);
6194 init_optab (ior_optab, IOR);
6195 init_optab (xor_optab, XOR);
6196 init_optab (ashl_optab, ASHIFT);
6197 init_optab (ssashl_optab, SS_ASHIFT);
6198 init_optab (usashl_optab, US_ASHIFT);
6199 init_optab (ashr_optab, ASHIFTRT);
6200 init_optab (lshr_optab, LSHIFTRT);
6201 init_optabv (vashl_optab, ASHIFT);
6202 init_optabv (vashr_optab, ASHIFTRT);
6203 init_optabv (vlshr_optab, LSHIFTRT);
6204 init_optab (rotl_optab, ROTATE);
6205 init_optab (rotr_optab, ROTATERT);
6206 init_optab (smin_optab, SMIN);
6207 init_optab (smax_optab, SMAX);
6208 init_optab (umin_optab, UMIN);
6209 init_optab (umax_optab, UMAX);
6210 init_optab (pow_optab, UNKNOWN);
6211 init_optab (atan2_optab, UNKNOWN);
6212 init_optab (fma_optab, FMA);
6213 init_optab (fms_optab, UNKNOWN);
6214 init_optab (fnma_optab, UNKNOWN);
6215 init_optab (fnms_optab, UNKNOWN);
6217 /* These three have codes assigned exclusively for the sake of
6218 have_insn_for. */
6219 init_optab (mov_optab, SET);
6220 init_optab (movstrict_optab, STRICT_LOW_PART);
6221 init_optab (cbranch_optab, COMPARE);
6223 init_optab (cmov_optab, UNKNOWN);
6224 init_optab (cstore_optab, UNKNOWN);
6225 init_optab (ctrap_optab, UNKNOWN);
6227 init_optab (storent_optab, UNKNOWN);
6229 init_optab (cmp_optab, UNKNOWN);
6230 init_optab (ucmp_optab, UNKNOWN);
6232 init_optab (eq_optab, EQ);
6233 init_optab (ne_optab, NE);
6234 init_optab (gt_optab, GT);
6235 init_optab (ge_optab, GE);
6236 init_optab (lt_optab, LT);
6237 init_optab (le_optab, LE);
6238 init_optab (unord_optab, UNORDERED);
6240 init_optab (neg_optab, NEG);
6241 init_optab (ssneg_optab, SS_NEG);
6242 init_optab (usneg_optab, US_NEG);
6243 init_optabv (negv_optab, NEG);
6244 init_optab (abs_optab, ABS);
6245 init_optabv (absv_optab, ABS);
6246 init_optab (addcc_optab, UNKNOWN);
6247 init_optab (one_cmpl_optab, NOT);
6248 init_optab (bswap_optab, BSWAP);
6249 init_optab (ffs_optab, FFS);
6250 init_optab (clz_optab, CLZ);
6251 init_optab (ctz_optab, CTZ);
6252 init_optab (clrsb_optab, CLRSB);
6253 init_optab (popcount_optab, POPCOUNT);
6254 init_optab (parity_optab, PARITY);
6255 init_optab (sqrt_optab, SQRT);
6256 init_optab (floor_optab, UNKNOWN);
6257 init_optab (ceil_optab, UNKNOWN);
6258 init_optab (round_optab, UNKNOWN);
6259 init_optab (btrunc_optab, UNKNOWN);
6260 init_optab (nearbyint_optab, UNKNOWN);
6261 init_optab (rint_optab, UNKNOWN);
6262 init_optab (sincos_optab, UNKNOWN);
6263 init_optab (sin_optab, UNKNOWN);
6264 init_optab (asin_optab, UNKNOWN);
6265 init_optab (cos_optab, UNKNOWN);
6266 init_optab (acos_optab, UNKNOWN);
6267 init_optab (exp_optab, UNKNOWN);
6268 init_optab (exp10_optab, UNKNOWN);
6269 init_optab (exp2_optab, UNKNOWN);
6270 init_optab (expm1_optab, UNKNOWN);
6271 init_optab (ldexp_optab, UNKNOWN);
6272 init_optab (scalb_optab, UNKNOWN);
6273 init_optab (significand_optab, UNKNOWN);
6274 init_optab (logb_optab, UNKNOWN);
6275 init_optab (ilogb_optab, UNKNOWN);
6276 init_optab (log_optab, UNKNOWN);
6277 init_optab (log10_optab, UNKNOWN);
6278 init_optab (log2_optab, UNKNOWN);
6279 init_optab (log1p_optab, UNKNOWN);
6280 init_optab (tan_optab, UNKNOWN);
6281 init_optab (atan_optab, UNKNOWN);
6282 init_optab (copysign_optab, UNKNOWN);
6283 init_optab (signbit_optab, UNKNOWN);
6285 init_optab (isinf_optab, UNKNOWN);
6287 init_optab (strlen_optab, UNKNOWN);
6288 init_optab (push_optab, UNKNOWN);
6290 init_optab (reduc_smax_optab, UNKNOWN);
6291 init_optab (reduc_umax_optab, UNKNOWN);
6292 init_optab (reduc_smin_optab, UNKNOWN);
6293 init_optab (reduc_umin_optab, UNKNOWN);
6294 init_optab (reduc_splus_optab, UNKNOWN);
6295 init_optab (reduc_uplus_optab, UNKNOWN);
6297 init_optab (ssum_widen_optab, UNKNOWN);
6298 init_optab (usum_widen_optab, UNKNOWN);
6299 init_optab (sdot_prod_optab, UNKNOWN);
6300 init_optab (udot_prod_optab, UNKNOWN);
6302 init_optab (vec_extract_optab, UNKNOWN);
6303 init_optab (vec_set_optab, UNKNOWN);
6304 init_optab (vec_init_optab, UNKNOWN);
6305 init_optab (vec_shl_optab, UNKNOWN);
6306 init_optab (vec_shr_optab, UNKNOWN);
6307 init_optab (vec_realign_load_optab, UNKNOWN);
6308 init_optab (movmisalign_optab, UNKNOWN);
6309 init_optab (vec_widen_umult_hi_optab, UNKNOWN);
6310 init_optab (vec_widen_umult_lo_optab, UNKNOWN);
6311 init_optab (vec_widen_smult_hi_optab, UNKNOWN);
6312 init_optab (vec_widen_smult_lo_optab, UNKNOWN);
6313 init_optab (vec_widen_ushiftl_hi_optab, UNKNOWN);
6314 init_optab (vec_widen_ushiftl_lo_optab, UNKNOWN);
6315 init_optab (vec_widen_sshiftl_hi_optab, UNKNOWN);
6316 init_optab (vec_widen_sshiftl_lo_optab, UNKNOWN);
6317 init_optab (vec_unpacks_hi_optab, UNKNOWN);
6318 init_optab (vec_unpacks_lo_optab, UNKNOWN);
6319 init_optab (vec_unpacku_hi_optab, UNKNOWN);
6320 init_optab (vec_unpacku_lo_optab, UNKNOWN);
6321 init_optab (vec_unpacks_float_hi_optab, UNKNOWN);
6322 init_optab (vec_unpacks_float_lo_optab, UNKNOWN);
6323 init_optab (vec_unpacku_float_hi_optab, UNKNOWN);
6324 init_optab (vec_unpacku_float_lo_optab, UNKNOWN);
6325 init_optab (vec_pack_trunc_optab, UNKNOWN);
6326 init_optab (vec_pack_usat_optab, UNKNOWN);
6327 init_optab (vec_pack_ssat_optab, UNKNOWN);
6328 init_optab (vec_pack_ufix_trunc_optab, UNKNOWN);
6329 init_optab (vec_pack_sfix_trunc_optab, UNKNOWN);
6331 init_optab (powi_optab, UNKNOWN);
6333 /* Conversions. */
6334 init_convert_optab (sext_optab, SIGN_EXTEND);
6335 init_convert_optab (zext_optab, ZERO_EXTEND);
6336 init_convert_optab (trunc_optab, TRUNCATE);
6337 init_convert_optab (sfix_optab, FIX);
6338 init_convert_optab (ufix_optab, UNSIGNED_FIX);
6339 init_convert_optab (sfixtrunc_optab, UNKNOWN);
6340 init_convert_optab (ufixtrunc_optab, UNKNOWN);
6341 init_convert_optab (sfloat_optab, FLOAT);
6342 init_convert_optab (ufloat_optab, UNSIGNED_FLOAT);
6343 init_convert_optab (lrint_optab, UNKNOWN);
6344 init_convert_optab (lround_optab, UNKNOWN);
6345 init_convert_optab (lfloor_optab, UNKNOWN);
6346 init_convert_optab (lceil_optab, UNKNOWN);
6348 init_convert_optab (fract_optab, FRACT_CONVERT);
6349 init_convert_optab (fractuns_optab, UNSIGNED_FRACT_CONVERT);
6350 init_convert_optab (satfract_optab, SAT_FRACT);
6351 init_convert_optab (satfractuns_optab, UNSIGNED_SAT_FRACT);
6353 /* Fill in the optabs with the insns we support. */
6354 init_all_optabs ();
6356 /* Initialize the optabs with the names of the library functions. */
6357 add_optab->libcall_basename = "add";
6358 add_optab->libcall_suffix = '3';
6359 add_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6360 addv_optab->libcall_basename = "add";
6361 addv_optab->libcall_suffix = '3';
6362 addv_optab->libcall_gen = gen_intv_fp_libfunc;
6363 ssadd_optab->libcall_basename = "ssadd";
6364 ssadd_optab->libcall_suffix = '3';
6365 ssadd_optab->libcall_gen = gen_signed_fixed_libfunc;
6366 usadd_optab->libcall_basename = "usadd";
6367 usadd_optab->libcall_suffix = '3';
6368 usadd_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6369 sub_optab->libcall_basename = "sub";
6370 sub_optab->libcall_suffix = '3';
6371 sub_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6372 subv_optab->libcall_basename = "sub";
6373 subv_optab->libcall_suffix = '3';
6374 subv_optab->libcall_gen = gen_intv_fp_libfunc;
6375 sssub_optab->libcall_basename = "sssub";
6376 sssub_optab->libcall_suffix = '3';
6377 sssub_optab->libcall_gen = gen_signed_fixed_libfunc;
6378 ussub_optab->libcall_basename = "ussub";
6379 ussub_optab->libcall_suffix = '3';
6380 ussub_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6381 smul_optab->libcall_basename = "mul";
6382 smul_optab->libcall_suffix = '3';
6383 smul_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6384 smulv_optab->libcall_basename = "mul";
6385 smulv_optab->libcall_suffix = '3';
6386 smulv_optab->libcall_gen = gen_intv_fp_libfunc;
6387 ssmul_optab->libcall_basename = "ssmul";
6388 ssmul_optab->libcall_suffix = '3';
6389 ssmul_optab->libcall_gen = gen_signed_fixed_libfunc;
6390 usmul_optab->libcall_basename = "usmul";
6391 usmul_optab->libcall_suffix = '3';
6392 usmul_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6393 sdiv_optab->libcall_basename = "div";
6394 sdiv_optab->libcall_suffix = '3';
6395 sdiv_optab->libcall_gen = gen_int_fp_signed_fixed_libfunc;
6396 sdivv_optab->libcall_basename = "divv";
6397 sdivv_optab->libcall_suffix = '3';
6398 sdivv_optab->libcall_gen = gen_int_libfunc;
6399 ssdiv_optab->libcall_basename = "ssdiv";
6400 ssdiv_optab->libcall_suffix = '3';
6401 ssdiv_optab->libcall_gen = gen_signed_fixed_libfunc;
6402 udiv_optab->libcall_basename = "udiv";
6403 udiv_optab->libcall_suffix = '3';
6404 udiv_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6405 usdiv_optab->libcall_basename = "usdiv";
6406 usdiv_optab->libcall_suffix = '3';
6407 usdiv_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6408 sdivmod_optab->libcall_basename = "divmod";
6409 sdivmod_optab->libcall_suffix = '4';
6410 sdivmod_optab->libcall_gen = gen_int_libfunc;
6411 udivmod_optab->libcall_basename = "udivmod";
6412 udivmod_optab->libcall_suffix = '4';
6413 udivmod_optab->libcall_gen = gen_int_libfunc;
6414 smod_optab->libcall_basename = "mod";
6415 smod_optab->libcall_suffix = '3';
6416 smod_optab->libcall_gen = gen_int_libfunc;
6417 umod_optab->libcall_basename = "umod";
6418 umod_optab->libcall_suffix = '3';
6419 umod_optab->libcall_gen = gen_int_libfunc;
6420 ftrunc_optab->libcall_basename = "ftrunc";
6421 ftrunc_optab->libcall_suffix = '2';
6422 ftrunc_optab->libcall_gen = gen_fp_libfunc;
6423 and_optab->libcall_basename = "and";
6424 and_optab->libcall_suffix = '3';
6425 and_optab->libcall_gen = gen_int_libfunc;
6426 ior_optab->libcall_basename = "ior";
6427 ior_optab->libcall_suffix = '3';
6428 ior_optab->libcall_gen = gen_int_libfunc;
6429 xor_optab->libcall_basename = "xor";
6430 xor_optab->libcall_suffix = '3';
6431 xor_optab->libcall_gen = gen_int_libfunc;
6432 ashl_optab->libcall_basename = "ashl";
6433 ashl_optab->libcall_suffix = '3';
6434 ashl_optab->libcall_gen = gen_int_fixed_libfunc;
6435 ssashl_optab->libcall_basename = "ssashl";
6436 ssashl_optab->libcall_suffix = '3';
6437 ssashl_optab->libcall_gen = gen_signed_fixed_libfunc;
6438 usashl_optab->libcall_basename = "usashl";
6439 usashl_optab->libcall_suffix = '3';
6440 usashl_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6441 ashr_optab->libcall_basename = "ashr";
6442 ashr_optab->libcall_suffix = '3';
6443 ashr_optab->libcall_gen = gen_int_signed_fixed_libfunc;
6444 lshr_optab->libcall_basename = "lshr";
6445 lshr_optab->libcall_suffix = '3';
6446 lshr_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6447 smin_optab->libcall_basename = "min";
6448 smin_optab->libcall_suffix = '3';
6449 smin_optab->libcall_gen = gen_int_fp_libfunc;
6450 smax_optab->libcall_basename = "max";
6451 smax_optab->libcall_suffix = '3';
6452 smax_optab->libcall_gen = gen_int_fp_libfunc;
6453 umin_optab->libcall_basename = "umin";
6454 umin_optab->libcall_suffix = '3';
6455 umin_optab->libcall_gen = gen_int_libfunc;
6456 umax_optab->libcall_basename = "umax";
6457 umax_optab->libcall_suffix = '3';
6458 umax_optab->libcall_gen = gen_int_libfunc;
6459 neg_optab->libcall_basename = "neg";
6460 neg_optab->libcall_suffix = '2';
6461 neg_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6462 ssneg_optab->libcall_basename = "ssneg";
6463 ssneg_optab->libcall_suffix = '2';
6464 ssneg_optab->libcall_gen = gen_signed_fixed_libfunc;
6465 usneg_optab->libcall_basename = "usneg";
6466 usneg_optab->libcall_suffix = '2';
6467 usneg_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6468 negv_optab->libcall_basename = "neg";
6469 negv_optab->libcall_suffix = '2';
6470 negv_optab->libcall_gen = gen_intv_fp_libfunc;
6471 one_cmpl_optab->libcall_basename = "one_cmpl";
6472 one_cmpl_optab->libcall_suffix = '2';
6473 one_cmpl_optab->libcall_gen = gen_int_libfunc;
6474 ffs_optab->libcall_basename = "ffs";
6475 ffs_optab->libcall_suffix = '2';
6476 ffs_optab->libcall_gen = gen_int_libfunc;
6477 clz_optab->libcall_basename = "clz";
6478 clz_optab->libcall_suffix = '2';
6479 clz_optab->libcall_gen = gen_int_libfunc;
6480 ctz_optab->libcall_basename = "ctz";
6481 ctz_optab->libcall_suffix = '2';
6482 ctz_optab->libcall_gen = gen_int_libfunc;
6483 clrsb_optab->libcall_basename = "clrsb";
6484 clrsb_optab->libcall_suffix = '2';
6485 clrsb_optab->libcall_gen = gen_int_libfunc;
6486 popcount_optab->libcall_basename = "popcount";
6487 popcount_optab->libcall_suffix = '2';
6488 popcount_optab->libcall_gen = gen_int_libfunc;
6489 parity_optab->libcall_basename = "parity";
6490 parity_optab->libcall_suffix = '2';
6491 parity_optab->libcall_gen = gen_int_libfunc;
6493 /* Comparison libcalls for integers MUST come in pairs,
6494 signed/unsigned. */
6495 cmp_optab->libcall_basename = "cmp";
6496 cmp_optab->libcall_suffix = '2';
6497 cmp_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6498 ucmp_optab->libcall_basename = "ucmp";
6499 ucmp_optab->libcall_suffix = '2';
6500 ucmp_optab->libcall_gen = gen_int_libfunc;
6502 /* EQ etc are floating point only. */
6503 eq_optab->libcall_basename = "eq";
6504 eq_optab->libcall_suffix = '2';
6505 eq_optab->libcall_gen = gen_fp_libfunc;
6506 ne_optab->libcall_basename = "ne";
6507 ne_optab->libcall_suffix = '2';
6508 ne_optab->libcall_gen = gen_fp_libfunc;
6509 gt_optab->libcall_basename = "gt";
6510 gt_optab->libcall_suffix = '2';
6511 gt_optab->libcall_gen = gen_fp_libfunc;
6512 ge_optab->libcall_basename = "ge";
6513 ge_optab->libcall_suffix = '2';
6514 ge_optab->libcall_gen = gen_fp_libfunc;
6515 lt_optab->libcall_basename = "lt";
6516 lt_optab->libcall_suffix = '2';
6517 lt_optab->libcall_gen = gen_fp_libfunc;
6518 le_optab->libcall_basename = "le";
6519 le_optab->libcall_suffix = '2';
6520 le_optab->libcall_gen = gen_fp_libfunc;
6521 unord_optab->libcall_basename = "unord";
6522 unord_optab->libcall_suffix = '2';
6523 unord_optab->libcall_gen = gen_fp_libfunc;
6525 powi_optab->libcall_basename = "powi";
6526 powi_optab->libcall_suffix = '2';
6527 powi_optab->libcall_gen = gen_fp_libfunc;
6529 /* Conversions. */
6530 sfloat_optab->libcall_basename = "float";
6531 sfloat_optab->libcall_gen = gen_int_to_fp_conv_libfunc;
6532 ufloat_optab->libcall_gen = gen_ufloat_conv_libfunc;
6533 sfix_optab->libcall_basename = "fix";
6534 sfix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6535 ufix_optab->libcall_basename = "fixuns";
6536 ufix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6537 lrint_optab->libcall_basename = "lrint";
6538 lrint_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6539 lround_optab->libcall_basename = "lround";
6540 lround_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6541 lfloor_optab->libcall_basename = "lfloor";
6542 lfloor_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6543 lceil_optab->libcall_basename = "lceil";
6544 lceil_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6546 /* trunc_optab is also used for FLOAT_EXTEND. */
6547 sext_optab->libcall_basename = "extend";
6548 sext_optab->libcall_gen = gen_extend_conv_libfunc;
6549 trunc_optab->libcall_basename = "trunc";
6550 trunc_optab->libcall_gen = gen_trunc_conv_libfunc;
6552 /* Conversions for fixed-point modes and other modes. */
6553 fract_optab->libcall_basename = "fract";
6554 fract_optab->libcall_gen = gen_fract_conv_libfunc;
6555 satfract_optab->libcall_basename = "satfract";
6556 satfract_optab->libcall_gen = gen_satfract_conv_libfunc;
6557 fractuns_optab->libcall_basename = "fractuns";
6558 fractuns_optab->libcall_gen = gen_fractuns_conv_libfunc;
6559 satfractuns_optab->libcall_basename = "satfractuns";
6560 satfractuns_optab->libcall_gen = gen_satfractuns_conv_libfunc;
6562 /* The ffs function operates on `int'. Fall back on it if we do not
6563 have a libgcc2 function for that width. */
6564 if (INT_TYPE_SIZE < BITS_PER_WORD)
6565 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6566 "ffs");
6568 /* Explicitly initialize the bswap libfuncs since we need them to be
6569 valid for things other than word_mode. */
6570 if (targetm.libfunc_gnu_prefix)
6572 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6573 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6575 else
6577 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6578 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6581 /* Use cabs for double complex abs, since systems generally have cabs.
6582 Don't define any libcall for float complex, so that cabs will be used. */
6583 if (complex_double_type_node)
6584 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node), "cabs");
6586 abort_libfunc = init_one_libfunc ("abort");
6587 memcpy_libfunc = init_one_libfunc ("memcpy");
6588 memmove_libfunc = init_one_libfunc ("memmove");
6589 memcmp_libfunc = init_one_libfunc ("memcmp");
6590 memset_libfunc = init_one_libfunc ("memset");
6591 setbits_libfunc = init_one_libfunc ("__setbits");
6593 #ifndef DONT_USE_BUILTIN_SETJMP
6594 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6595 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6596 #else
6597 setjmp_libfunc = init_one_libfunc ("setjmp");
6598 longjmp_libfunc = init_one_libfunc ("longjmp");
6599 #endif
6600 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6601 unwind_sjlj_unregister_libfunc
6602 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6604 /* For function entry/exit instrumentation. */
6605 profile_function_entry_libfunc
6606 = init_one_libfunc ("__cyg_profile_func_enter");
6607 profile_function_exit_libfunc
6608 = init_one_libfunc ("__cyg_profile_func_exit");
6610 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6612 /* Allow the target to add more libcalls or rename some, etc. */
6613 targetm.init_libfuncs ();
6616 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6617 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6619 static void
6620 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6622 enum machine_mode mode;
6623 char buf[64];
6624 size_t len = strlen (base);
6625 int i;
6627 gcc_assert (max <= 8);
6628 gcc_assert (len + 3 < sizeof (buf));
6630 memcpy (buf, base, len);
6631 buf[len] = '_';
6632 buf[len + 1] = '0';
6633 buf[len + 2] = '\0';
6635 mode = QImode;
6636 for (i = 1; i <= max; i *= 2)
6638 buf[len + 1] = '0' + i;
6639 set_optab_libfunc (tab, mode, buf);
6640 mode = GET_MODE_2XWIDER_MODE (mode);
6644 void
6645 init_sync_libfuncs (int max)
6647 if (!flag_sync_libcalls)
6648 return;
6650 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6651 "__sync_val_compare_and_swap", max);
6652 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6653 "__sync_lock_test_and_set", max);
6655 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6656 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6657 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6658 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6659 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6660 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6662 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6663 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6664 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6665 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6666 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6667 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6670 /* Print information about the current contents of the optabs on
6671 STDERR. */
6673 DEBUG_FUNCTION void
6674 debug_optab_libfuncs (void)
6676 int i;
6677 int j;
6678 int k;
6680 /* Dump the arithmetic optabs. */
6681 for (i = 0; i != (int) OTI_MAX; i++)
6682 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6684 optab o;
6685 rtx l;
6687 o = &optab_table[i];
6688 l = optab_libfunc (o, (enum machine_mode) j);
6689 if (l)
6691 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6692 fprintf (stderr, "%s\t%s:\t%s\n",
6693 GET_RTX_NAME (o->code),
6694 GET_MODE_NAME (j),
6695 XSTR (l, 0));
6699 /* Dump the conversion optabs. */
6700 for (i = 0; i < (int) COI_MAX; ++i)
6701 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6702 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6704 convert_optab o;
6705 rtx l;
6707 o = &convert_optab_table[i];
6708 l = convert_optab_libfunc (o, (enum machine_mode) j,
6709 (enum machine_mode) k);
6710 if (l)
6712 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6713 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6714 GET_RTX_NAME (o->code),
6715 GET_MODE_NAME (j),
6716 GET_MODE_NAME (k),
6717 XSTR (l, 0));
6723 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6724 CODE. Return 0 on failure. */
6727 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6729 enum machine_mode mode = GET_MODE (op1);
6730 enum insn_code icode;
6731 rtx insn;
6732 rtx trap_rtx;
6734 if (mode == VOIDmode)
6735 return 0;
6737 icode = optab_handler (ctrap_optab, mode);
6738 if (icode == CODE_FOR_nothing)
6739 return 0;
6741 /* Some targets only accept a zero trap code. */
6742 if (!insn_operand_matches (icode, 3, tcode))
6743 return 0;
6745 do_pending_stack_adjust ();
6746 start_sequence ();
6747 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6748 &trap_rtx, &mode);
6749 if (!trap_rtx)
6750 insn = NULL_RTX;
6751 else
6752 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6753 tcode);
6755 /* If that failed, then give up. */
6756 if (insn == 0)
6758 end_sequence ();
6759 return 0;
6762 emit_insn (insn);
6763 insn = get_insns ();
6764 end_sequence ();
6765 return insn;
6768 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6769 or unsigned operation code. */
6771 static enum rtx_code
6772 get_rtx_code (enum tree_code tcode, bool unsignedp)
6774 enum rtx_code code;
6775 switch (tcode)
6777 case EQ_EXPR:
6778 code = EQ;
6779 break;
6780 case NE_EXPR:
6781 code = NE;
6782 break;
6783 case LT_EXPR:
6784 code = unsignedp ? LTU : LT;
6785 break;
6786 case LE_EXPR:
6787 code = unsignedp ? LEU : LE;
6788 break;
6789 case GT_EXPR:
6790 code = unsignedp ? GTU : GT;
6791 break;
6792 case GE_EXPR:
6793 code = unsignedp ? GEU : GE;
6794 break;
6796 case UNORDERED_EXPR:
6797 code = UNORDERED;
6798 break;
6799 case ORDERED_EXPR:
6800 code = ORDERED;
6801 break;
6802 case UNLT_EXPR:
6803 code = UNLT;
6804 break;
6805 case UNLE_EXPR:
6806 code = UNLE;
6807 break;
6808 case UNGT_EXPR:
6809 code = UNGT;
6810 break;
6811 case UNGE_EXPR:
6812 code = UNGE;
6813 break;
6814 case UNEQ_EXPR:
6815 code = UNEQ;
6816 break;
6817 case LTGT_EXPR:
6818 code = LTGT;
6819 break;
6821 default:
6822 gcc_unreachable ();
6824 return code;
6827 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6828 unsigned operators. Do not generate compare instruction. */
6830 static rtx
6831 vector_compare_rtx (tree cond, bool unsignedp, enum insn_code icode)
6833 struct expand_operand ops[2];
6834 enum rtx_code rcode;
6835 tree t_op0, t_op1;
6836 rtx rtx_op0, rtx_op1;
6838 /* This is unlikely. While generating VEC_COND_EXPR, auto vectorizer
6839 ensures that condition is a relational operation. */
6840 gcc_assert (COMPARISON_CLASS_P (cond));
6842 rcode = get_rtx_code (TREE_CODE (cond), unsignedp);
6843 t_op0 = TREE_OPERAND (cond, 0);
6844 t_op1 = TREE_OPERAND (cond, 1);
6846 /* Expand operands. */
6847 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6848 EXPAND_STACK_PARM);
6849 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6850 EXPAND_STACK_PARM);
6852 create_input_operand (&ops[0], rtx_op0, GET_MODE (rtx_op0));
6853 create_input_operand (&ops[1], rtx_op1, GET_MODE (rtx_op1));
6854 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6855 gcc_unreachable ();
6856 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6859 /* Return true if VEC_PERM_EXPR can be expanded using SIMD extensions
6860 of the CPU. SEL may be NULL, which stands for an unknown constant. */
6862 bool
6863 can_vec_perm_p (enum machine_mode mode, bool variable,
6864 const unsigned char *sel)
6866 enum machine_mode qimode;
6868 /* If the target doesn't implement a vector mode for the vector type,
6869 then no operations are supported. */
6870 if (!VECTOR_MODE_P (mode))
6871 return false;
6873 if (!variable)
6875 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6876 && (sel == NULL
6877 || targetm.vectorize.vec_perm_const_ok == NULL
6878 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6879 return true;
6882 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6883 return true;
6885 /* We allow fallback to a QI vector mode, and adjust the mask. */
6886 if (GET_MODE_INNER (mode) == QImode)
6887 return false;
6888 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6889 if (!VECTOR_MODE_P (qimode))
6890 return false;
6892 /* ??? For completeness, we ought to check the QImode version of
6893 vec_perm_const_optab. But all users of this implicit lowering
6894 feature implement the variable vec_perm_optab. */
6895 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6896 return false;
6898 /* In order to support the lowering of variable permutations,
6899 we need to support shifts and adds. */
6900 if (variable)
6902 if (GET_MODE_UNIT_SIZE (mode) > 2
6903 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6904 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6905 return false;
6906 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6907 return false;
6910 return true;
6913 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
6915 static rtx
6916 expand_vec_perm_1 (enum insn_code icode, rtx target,
6917 rtx v0, rtx v1, rtx sel)
6919 enum machine_mode tmode = GET_MODE (target);
6920 enum machine_mode smode = GET_MODE (sel);
6921 struct expand_operand ops[4];
6923 create_output_operand (&ops[0], target, tmode);
6924 create_input_operand (&ops[3], sel, smode);
6926 /* Make an effort to preserve v0 == v1. The target expander is able to
6927 rely on this to determine if we're permuting a single input operand. */
6928 if (rtx_equal_p (v0, v1))
6930 if (!insn_operand_matches (icode, 1, v0))
6931 v0 = force_reg (tmode, v0);
6932 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6933 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6935 create_fixed_operand (&ops[1], v0);
6936 create_fixed_operand (&ops[2], v0);
6938 else
6940 create_input_operand (&ops[1], v0, tmode);
6941 create_input_operand (&ops[2], v1, tmode);
6944 if (maybe_expand_insn (icode, 4, ops))
6945 return ops[0].value;
6946 return NULL_RTX;
6949 /* Generate instructions for vec_perm optab given its mode
6950 and three operands. */
6953 expand_vec_perm (enum machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6955 enum insn_code icode;
6956 enum machine_mode qimode;
6957 unsigned int i, w, e, u;
6958 rtx tmp, sel_qi = NULL;
6959 rtvec vec;
6961 if (!target || GET_MODE (target) != mode)
6962 target = gen_reg_rtx (mode);
6964 w = GET_MODE_SIZE (mode);
6965 e = GET_MODE_NUNITS (mode);
6966 u = GET_MODE_UNIT_SIZE (mode);
6968 /* Set QIMODE to a different vector mode with byte elements.
6969 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6970 qimode = VOIDmode;
6971 if (GET_MODE_INNER (mode) != QImode)
6973 qimode = mode_for_vector (QImode, w);
6974 if (!VECTOR_MODE_P (qimode))
6975 qimode = VOIDmode;
6978 /* If the input is a constant, expand it specially. */
6979 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6980 if (GET_CODE (sel) == CONST_VECTOR)
6982 icode = direct_optab_handler (vec_perm_const_optab, mode);
6983 if (icode != CODE_FOR_nothing)
6985 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6986 if (tmp)
6987 return tmp;
6990 /* Fall back to a constant byte-based permutation. */
6991 if (qimode != VOIDmode)
6993 vec = rtvec_alloc (w);
6994 for (i = 0; i < e; ++i)
6996 unsigned int j, this_e;
6998 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6999 this_e &= 2 * e - 1;
7000 this_e *= u;
7002 for (j = 0; j < u; ++j)
7003 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
7005 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
7007 icode = direct_optab_handler (vec_perm_const_optab, qimode);
7008 if (icode != CODE_FOR_nothing)
7010 tmp = expand_vec_perm_1 (icode, gen_lowpart (qimode, target),
7011 gen_lowpart (qimode, v0),
7012 gen_lowpart (qimode, v1), sel_qi);
7013 if (tmp)
7014 return gen_lowpart (mode, tmp);
7019 /* Otherwise expand as a fully variable permuation. */
7020 icode = direct_optab_handler (vec_perm_optab, mode);
7021 if (icode != CODE_FOR_nothing)
7023 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
7024 if (tmp)
7025 return tmp;
7028 /* As a special case to aid several targets, lower the element-based
7029 permutation to a byte-based permutation and try again. */
7030 if (qimode == VOIDmode)
7031 return NULL_RTX;
7032 icode = direct_optab_handler (vec_perm_optab, qimode);
7033 if (icode == CODE_FOR_nothing)
7034 return NULL_RTX;
7036 if (sel_qi == NULL)
7038 /* Multiply each element by its byte size. */
7039 enum machine_mode selmode = GET_MODE (sel);
7040 if (u == 2)
7041 sel = expand_simple_binop (selmode, PLUS, sel, sel,
7042 sel, 0, OPTAB_DIRECT);
7043 else
7044 sel = expand_simple_binop (selmode, ASHIFT, sel,
7045 GEN_INT (exact_log2 (u)),
7046 sel, 0, OPTAB_DIRECT);
7047 gcc_assert (sel != NULL);
7049 /* Broadcast the low byte each element into each of its bytes. */
7050 vec = rtvec_alloc (w);
7051 for (i = 0; i < w; ++i)
7053 int this_e = i / u * u;
7054 if (BYTES_BIG_ENDIAN)
7055 this_e += u - 1;
7056 RTVEC_ELT (vec, i) = GEN_INT (this_e);
7058 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
7059 sel = gen_lowpart (qimode, sel);
7060 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
7061 gcc_assert (sel != NULL);
7063 /* Add the byte offset to each byte element. */
7064 /* Note that the definition of the indicies here is memory ordering,
7065 so there should be no difference between big and little endian. */
7066 vec = rtvec_alloc (w);
7067 for (i = 0; i < w; ++i)
7068 RTVEC_ELT (vec, i) = GEN_INT (i % u);
7069 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
7070 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
7071 sel, 0, OPTAB_DIRECT);
7072 gcc_assert (sel_qi != NULL);
7075 tmp = expand_vec_perm_1 (icode, gen_lowpart (qimode, target),
7076 gen_lowpart (qimode, v0),
7077 gen_lowpart (qimode, v1), sel_qi);
7078 if (tmp)
7079 tmp = gen_lowpart (mode, tmp);
7080 return tmp;
7083 /* Return insn code for a conditional operator with a comparison in
7084 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
7086 static inline enum insn_code
7087 get_vcond_icode (enum machine_mode vmode, enum machine_mode cmode, bool uns)
7089 enum insn_code icode = CODE_FOR_nothing;
7090 if (uns)
7091 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
7092 else
7093 icode = convert_optab_handler (vcond_optab, vmode, cmode);
7094 return icode;
7097 /* Return TRUE iff, appropriate vector insns are available
7098 for vector cond expr with vector type VALUE_TYPE and a comparison
7099 with operand vector types in CMP_OP_TYPE. */
7101 bool
7102 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
7104 enum machine_mode value_mode = TYPE_MODE (value_type);
7105 enum machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
7106 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
7107 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
7108 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
7109 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
7110 return false;
7111 return true;
7114 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
7115 three operands. */
7118 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
7119 rtx target)
7121 struct expand_operand ops[6];
7122 enum insn_code icode;
7123 rtx comparison, rtx_op1, rtx_op2;
7124 enum machine_mode mode = TYPE_MODE (vec_cond_type);
7125 enum machine_mode cmp_op_mode;
7126 bool unsignedp;
7128 gcc_assert (COMPARISON_CLASS_P (op0));
7130 unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)));
7131 cmp_op_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (op0, 0)));
7133 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
7134 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
7136 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
7137 if (icode == CODE_FOR_nothing)
7138 return 0;
7140 comparison = vector_compare_rtx (op0, unsignedp, icode);
7141 rtx_op1 = expand_normal (op1);
7142 rtx_op2 = expand_normal (op2);
7144 create_output_operand (&ops[0], target, mode);
7145 create_input_operand (&ops[1], rtx_op1, mode);
7146 create_input_operand (&ops[2], rtx_op2, mode);
7147 create_fixed_operand (&ops[3], comparison);
7148 create_fixed_operand (&ops[4], XEXP (comparison, 0));
7149 create_fixed_operand (&ops[5], XEXP (comparison, 1));
7150 expand_insn (icode, 6, ops);
7151 return ops[0].value;
7155 /* Return true if there is a compare_and_swap pattern. */
7157 bool
7158 can_compare_and_swap_p (enum machine_mode mode, bool allow_libcall)
7160 enum insn_code icode;
7162 /* Check for __atomic_compare_and_swap. */
7163 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7164 if (icode != CODE_FOR_nothing)
7165 return true;
7167 /* Check for __sync_compare_and_swap. */
7168 icode = optab_handler (sync_compare_and_swap_optab, mode);
7169 if (icode != CODE_FOR_nothing)
7170 return true;
7171 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
7172 return true;
7174 /* No inline compare and swap. */
7175 return false;
7178 /* Return true if an atomic exchange can be performed. */
7180 bool
7181 can_atomic_exchange_p (enum machine_mode mode, bool allow_libcall)
7183 enum insn_code icode;
7185 /* Check for __atomic_exchange. */
7186 icode = direct_optab_handler (atomic_exchange_optab, mode);
7187 if (icode != CODE_FOR_nothing)
7188 return true;
7190 /* Don't check __sync_test_and_set, as on some platforms that
7191 has reduced functionality. Targets that really do support
7192 a proper exchange should simply be updated to the __atomics. */
7194 return can_compare_and_swap_p (mode, allow_libcall);
7198 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
7199 pattern. */
7201 static void
7202 find_cc_set (rtx x, const_rtx pat, void *data)
7204 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
7205 && GET_CODE (pat) == SET)
7207 rtx *p_cc_reg = (rtx *) data;
7208 gcc_assert (!*p_cc_reg);
7209 *p_cc_reg = x;
7213 /* This is a helper function for the other atomic operations. This function
7214 emits a loop that contains SEQ that iterates until a compare-and-swap
7215 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7216 a set of instructions that takes a value from OLD_REG as an input and
7217 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7218 set to the current contents of MEM. After SEQ, a compare-and-swap will
7219 attempt to update MEM with NEW_REG. The function returns true when the
7220 loop was generated successfully. */
7222 static bool
7223 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7225 enum machine_mode mode = GET_MODE (mem);
7226 rtx label, cmp_reg, success, oldval;
7228 /* The loop we want to generate looks like
7230 cmp_reg = mem;
7231 label:
7232 old_reg = cmp_reg;
7233 seq;
7234 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
7235 if (success)
7236 goto label;
7238 Note that we only do the plain load from memory once. Subsequent
7239 iterations use the value loaded by the compare-and-swap pattern. */
7241 label = gen_label_rtx ();
7242 cmp_reg = gen_reg_rtx (mode);
7244 emit_move_insn (cmp_reg, mem);
7245 emit_label (label);
7246 emit_move_insn (old_reg, cmp_reg);
7247 if (seq)
7248 emit_insn (seq);
7250 success = NULL_RTX;
7251 oldval = cmp_reg;
7252 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
7253 new_reg, false, MEMMODEL_SEQ_CST,
7254 MEMMODEL_RELAXED))
7255 return false;
7257 if (oldval != cmp_reg)
7258 emit_move_insn (cmp_reg, oldval);
7260 /* ??? Mark this jump predicted not taken? */
7261 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
7262 GET_MODE (success), 1, label);
7263 return true;
7267 /* This function tries to emit an atomic_exchange intruction. VAL is written
7268 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
7269 using TARGET if possible. */
7271 static rtx
7272 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7274 enum machine_mode mode = GET_MODE (mem);
7275 enum insn_code icode;
7277 /* If the target supports the exchange directly, great. */
7278 icode = direct_optab_handler (atomic_exchange_optab, mode);
7279 if (icode != CODE_FOR_nothing)
7281 struct expand_operand ops[4];
7283 create_output_operand (&ops[0], target, mode);
7284 create_fixed_operand (&ops[1], mem);
7285 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7286 create_convert_operand_to (&ops[2], val, mode, true);
7287 create_integer_operand (&ops[3], model);
7288 if (maybe_expand_insn (icode, 4, ops))
7289 return ops[0].value;
7292 return NULL_RTX;
7295 /* This function tries to implement an atomic exchange operation using
7296 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7297 The previous contents of *MEM are returned, using TARGET if possible.
7298 Since this instructionn is an acquire barrier only, stronger memory
7299 models may require additional barriers to be emitted. */
7301 static rtx
7302 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7303 enum memmodel model)
7305 enum machine_mode mode = GET_MODE (mem);
7306 enum insn_code icode;
7307 rtx last_insn = get_last_insn ();
7309 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7311 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7312 exists, and the memory model is stronger than acquire, add a release
7313 barrier before the instruction. */
7315 if (model == MEMMODEL_SEQ_CST
7316 || model == MEMMODEL_RELEASE
7317 || model == MEMMODEL_ACQ_REL)
7318 expand_mem_thread_fence (model);
7320 if (icode != CODE_FOR_nothing)
7322 struct expand_operand ops[3];
7323 create_output_operand (&ops[0], target, mode);
7324 create_fixed_operand (&ops[1], mem);
7325 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7326 create_convert_operand_to (&ops[2], val, mode, true);
7327 if (maybe_expand_insn (icode, 3, ops))
7328 return ops[0].value;
7331 /* If an external test-and-set libcall is provided, use that instead of
7332 any external compare-and-swap that we might get from the compare-and-
7333 swap-loop expansion later. */
7334 if (!can_compare_and_swap_p (mode, false))
7336 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7337 if (libfunc != NULL)
7339 rtx addr;
7341 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7342 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7343 mode, 2, addr, ptr_mode,
7344 val, mode);
7348 /* If the test_and_set can't be emitted, eliminate any barrier that might
7349 have been emitted. */
7350 delete_insns_since (last_insn);
7351 return NULL_RTX;
7354 /* This function tries to implement an atomic exchange operation using a
7355 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7356 *MEM are returned, using TARGET if possible. No memory model is required
7357 since a compare_and_swap loop is seq-cst. */
7359 static rtx
7360 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7362 enum machine_mode mode = GET_MODE (mem);
7364 if (can_compare_and_swap_p (mode, true))
7366 if (!target || !register_operand (target, mode))
7367 target = gen_reg_rtx (mode);
7368 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7369 val = convert_modes (mode, GET_MODE (val), val, 1);
7370 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7371 return target;
7374 return NULL_RTX;
7377 /* This function tries to implement an atomic test-and-set operation
7378 using the atomic_test_and_set instruction pattern. A boolean value
7379 is returned from the operation, using TARGET if possible. */
7381 #ifndef HAVE_atomic_test_and_set
7382 #define HAVE_atomic_test_and_set 0
7383 #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
7384 #endif
7386 static rtx
7387 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7389 enum machine_mode pat_bool_mode;
7390 struct expand_operand ops[3];
7392 if (!HAVE_atomic_test_and_set)
7393 return NULL_RTX;
7395 /* While we always get QImode from __atomic_test_and_set, we get
7396 other memory modes from __sync_lock_test_and_set. Note that we
7397 use no endian adjustment here. This matches the 4.6 behavior
7398 in the Sparc backend. */
7399 gcc_checking_assert
7400 (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
7401 if (GET_MODE (mem) != QImode)
7402 mem = adjust_address_nv (mem, QImode, 0);
7404 pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
7405 create_output_operand (&ops[0], target, pat_bool_mode);
7406 create_fixed_operand (&ops[1], mem);
7407 create_integer_operand (&ops[2], model);
7409 if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
7410 return ops[0].value;
7411 return NULL_RTX;
7414 /* This function expands the legacy _sync_lock test_and_set operation which is
7415 generally an atomic exchange. Some limited targets only allow the
7416 constant 1 to be stored. This is an ACQUIRE operation.
7418 TARGET is an optional place to stick the return value.
7419 MEM is where VAL is stored. */
7422 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7424 rtx ret;
7426 /* Try an atomic_exchange first. */
7427 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_ACQUIRE);
7428 if (ret)
7429 return ret;
7431 ret = maybe_emit_sync_lock_test_and_set (target, mem, val, MEMMODEL_ACQUIRE);
7432 if (ret)
7433 return ret;
7435 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7436 if (ret)
7437 return ret;
7439 /* If there are no other options, try atomic_test_and_set if the value
7440 being stored is 1. */
7441 if (val == const1_rtx)
7442 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_ACQUIRE);
7444 return ret;
7447 /* This function expands the atomic test_and_set operation:
7448 atomically store a boolean TRUE into MEM and return the previous value.
7450 MEMMODEL is the memory model variant to use.
7451 TARGET is an optional place to stick the return value. */
7454 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7456 enum machine_mode mode = GET_MODE (mem);
7457 rtx ret, trueval, subtarget;
7459 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7460 if (ret)
7461 return ret;
7463 /* Be binary compatible with non-default settings of trueval, and different
7464 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7465 another only has atomic-exchange. */
7466 if (targetm.atomic_test_and_set_trueval == 1)
7468 trueval = const1_rtx;
7469 subtarget = target ? target : gen_reg_rtx (mode);
7471 else
7473 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7474 subtarget = gen_reg_rtx (mode);
7477 /* Try the atomic-exchange optab... */
7478 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7480 /* ... then an atomic-compare-and-swap loop ... */
7481 if (!ret)
7482 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7484 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7485 if (!ret)
7486 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7488 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7489 things with the value 1. Thus we try again without trueval. */
7490 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7491 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7493 /* Failing all else, assume a single threaded environment and simply
7494 perform the operation. */
7495 if (!ret)
7497 emit_move_insn (subtarget, mem);
7498 emit_move_insn (mem, trueval);
7499 ret = subtarget;
7502 /* Recall that have to return a boolean value; rectify if trueval
7503 is not exactly one. */
7504 if (targetm.atomic_test_and_set_trueval != 1)
7505 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7507 return ret;
7510 /* This function expands the atomic exchange operation:
7511 atomically store VAL in MEM and return the previous value in MEM.
7513 MEMMODEL is the memory model variant to use.
7514 TARGET is an optional place to stick the return value. */
7517 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7519 rtx ret;
7521 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7523 /* Next try a compare-and-swap loop for the exchange. */
7524 if (!ret)
7525 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7527 return ret;
7530 /* This function expands the atomic compare exchange operation:
7532 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7533 *PTARGET_OVAL is an optional place to store the old value from memory.
7534 Both target parameters may be NULL to indicate that we do not care about
7535 that return value. Both target parameters are updated on success to
7536 the actual location of the corresponding result.
7538 MEMMODEL is the memory model variant to use.
7540 The return value of the function is true for success. */
7542 bool
7543 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7544 rtx mem, rtx expected, rtx desired,
7545 bool is_weak, enum memmodel succ_model,
7546 enum memmodel fail_model)
7548 enum machine_mode mode = GET_MODE (mem);
7549 struct expand_operand ops[8];
7550 enum insn_code icode;
7551 rtx target_oval, target_bool = NULL_RTX;
7552 rtx libfunc;
7554 /* Load expected into a register for the compare and swap. */
7555 if (MEM_P (expected))
7556 expected = copy_to_reg (expected);
7558 /* Make sure we always have some place to put the return oldval.
7559 Further, make sure that place is distinct from the input expected,
7560 just in case we need that path down below. */
7561 if (ptarget_oval == NULL
7562 || (target_oval = *ptarget_oval) == NULL
7563 || reg_overlap_mentioned_p (expected, target_oval))
7564 target_oval = gen_reg_rtx (mode);
7566 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7567 if (icode != CODE_FOR_nothing)
7569 enum machine_mode bool_mode = insn_data[icode].operand[0].mode;
7571 /* Make sure we always have a place for the bool operand. */
7572 if (ptarget_bool == NULL
7573 || (target_bool = *ptarget_bool) == NULL
7574 || GET_MODE (target_bool) != bool_mode)
7575 target_bool = gen_reg_rtx (bool_mode);
7577 /* Emit the compare_and_swap. */
7578 create_output_operand (&ops[0], target_bool, bool_mode);
7579 create_output_operand (&ops[1], target_oval, mode);
7580 create_fixed_operand (&ops[2], mem);
7581 create_convert_operand_to (&ops[3], expected, mode, true);
7582 create_convert_operand_to (&ops[4], desired, mode, true);
7583 create_integer_operand (&ops[5], is_weak);
7584 create_integer_operand (&ops[6], succ_model);
7585 create_integer_operand (&ops[7], fail_model);
7586 expand_insn (icode, 8, ops);
7588 /* Return success/failure. */
7589 target_bool = ops[0].value;
7590 target_oval = ops[1].value;
7591 goto success;
7594 /* Otherwise fall back to the original __sync_val_compare_and_swap
7595 which is always seq-cst. */
7596 icode = optab_handler (sync_compare_and_swap_optab, mode);
7597 if (icode != CODE_FOR_nothing)
7599 rtx cc_reg;
7601 create_output_operand (&ops[0], target_oval, mode);
7602 create_fixed_operand (&ops[1], mem);
7603 create_convert_operand_to (&ops[2], expected, mode, true);
7604 create_convert_operand_to (&ops[3], desired, mode, true);
7605 if (!maybe_expand_insn (icode, 4, ops))
7606 return false;
7608 target_oval = ops[0].value;
7610 /* If the caller isn't interested in the boolean return value,
7611 skip the computation of it. */
7612 if (ptarget_bool == NULL)
7613 goto success;
7615 /* Otherwise, work out if the compare-and-swap succeeded. */
7616 cc_reg = NULL_RTX;
7617 if (have_insn_for (COMPARE, CCmode))
7618 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7619 if (cc_reg)
7621 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7622 const0_rtx, VOIDmode, 0, 1);
7623 goto success;
7625 goto success_bool_from_val;
7628 /* Also check for library support for __sync_val_compare_and_swap. */
7629 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7630 if (libfunc != NULL)
7632 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7633 target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7634 mode, 3, addr, ptr_mode,
7635 expected, mode, desired, mode);
7637 /* Compute the boolean return value only if requested. */
7638 if (ptarget_bool)
7639 goto success_bool_from_val;
7640 else
7641 goto success;
7644 /* Failure. */
7645 return false;
7647 success_bool_from_val:
7648 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7649 expected, VOIDmode, 1, 1);
7650 success:
7651 /* Make sure that the oval output winds up where the caller asked. */
7652 if (ptarget_oval)
7653 *ptarget_oval = target_oval;
7654 if (ptarget_bool)
7655 *ptarget_bool = target_bool;
7656 return true;
7659 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7661 static void
7662 expand_asm_memory_barrier (void)
7664 rtx asm_op, clob;
7666 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7667 rtvec_alloc (0), rtvec_alloc (0),
7668 rtvec_alloc (0), UNKNOWN_LOCATION);
7669 MEM_VOLATILE_P (asm_op) = 1;
7671 clob = gen_rtx_SCRATCH (VOIDmode);
7672 clob = gen_rtx_MEM (BLKmode, clob);
7673 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7675 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7678 /* This routine will either emit the mem_thread_fence pattern or issue a
7679 sync_synchronize to generate a fence for memory model MEMMODEL. */
7681 #ifndef HAVE_mem_thread_fence
7682 # define HAVE_mem_thread_fence 0
7683 # define gen_mem_thread_fence(x) (gcc_unreachable (), NULL_RTX)
7684 #endif
7685 #ifndef HAVE_memory_barrier
7686 # define HAVE_memory_barrier 0
7687 # define gen_memory_barrier() (gcc_unreachable (), NULL_RTX)
7688 #endif
7690 void
7691 expand_mem_thread_fence (enum memmodel model)
7693 if (HAVE_mem_thread_fence)
7694 emit_insn (gen_mem_thread_fence (GEN_INT (model)));
7695 else if (model != MEMMODEL_RELAXED)
7697 if (HAVE_memory_barrier)
7698 emit_insn (gen_memory_barrier ());
7699 else if (synchronize_libfunc != NULL_RTX)
7700 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7701 else
7702 expand_asm_memory_barrier ();
7706 /* This routine will either emit the mem_signal_fence pattern or issue a
7707 sync_synchronize to generate a fence for memory model MEMMODEL. */
7709 #ifndef HAVE_mem_signal_fence
7710 # define HAVE_mem_signal_fence 0
7711 # define gen_mem_signal_fence(x) (gcc_unreachable (), NULL_RTX)
7712 #endif
7714 void
7715 expand_mem_signal_fence (enum memmodel model)
7717 if (HAVE_mem_signal_fence)
7718 emit_insn (gen_mem_signal_fence (GEN_INT (model)));
7719 else if (model != MEMMODEL_RELAXED)
7721 /* By default targets are coherent between a thread and the signal
7722 handler running on the same thread. Thus this really becomes a
7723 compiler barrier, in that stores must not be sunk past
7724 (or raised above) a given point. */
7725 expand_asm_memory_barrier ();
7729 /* This function expands the atomic load operation:
7730 return the atomically loaded value in MEM.
7732 MEMMODEL is the memory model variant to use.
7733 TARGET is an option place to stick the return value. */
7736 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7738 enum machine_mode mode = GET_MODE (mem);
7739 enum insn_code icode;
7741 /* If the target supports the load directly, great. */
7742 icode = direct_optab_handler (atomic_load_optab, mode);
7743 if (icode != CODE_FOR_nothing)
7745 struct expand_operand ops[3];
7747 create_output_operand (&ops[0], target, mode);
7748 create_fixed_operand (&ops[1], mem);
7749 create_integer_operand (&ops[2], model);
7750 if (maybe_expand_insn (icode, 3, ops))
7751 return ops[0].value;
7754 /* If the size of the object is greater than word size on this target,
7755 then we assume that a load will not be atomic. */
7756 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7758 /* Issue val = compare_and_swap (mem, 0, 0).
7759 This may cause the occasional harmless store of 0 when the value is
7760 already 0, but it seems to be OK according to the standards guys. */
7761 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7762 const0_rtx, false, model, model))
7763 return target;
7764 else
7765 /* Otherwise there is no atomic load, leave the library call. */
7766 return NULL_RTX;
7769 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7770 if (!target || target == const0_rtx)
7771 target = gen_reg_rtx (mode);
7773 /* Emit the appropriate barrier before the load. */
7774 expand_mem_thread_fence (model);
7776 emit_move_insn (target, mem);
7778 /* For SEQ_CST, also emit a barrier after the load. */
7779 if (model == MEMMODEL_SEQ_CST)
7780 expand_mem_thread_fence (model);
7782 return target;
7785 /* This function expands the atomic store operation:
7786 Atomically store VAL in MEM.
7787 MEMMODEL is the memory model variant to use.
7788 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7789 function returns const0_rtx if a pattern was emitted. */
7792 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7794 enum machine_mode mode = GET_MODE (mem);
7795 enum insn_code icode;
7796 struct expand_operand ops[3];
7798 /* If the target supports the store directly, great. */
7799 icode = direct_optab_handler (atomic_store_optab, mode);
7800 if (icode != CODE_FOR_nothing)
7802 create_fixed_operand (&ops[0], mem);
7803 create_input_operand (&ops[1], val, mode);
7804 create_integer_operand (&ops[2], model);
7805 if (maybe_expand_insn (icode, 3, ops))
7806 return const0_rtx;
7809 /* If using __sync_lock_release is a viable alternative, try it. */
7810 if (use_release)
7812 icode = direct_optab_handler (sync_lock_release_optab, mode);
7813 if (icode != CODE_FOR_nothing)
7815 create_fixed_operand (&ops[0], mem);
7816 create_input_operand (&ops[1], const0_rtx, mode);
7817 if (maybe_expand_insn (icode, 2, ops))
7819 /* lock_release is only a release barrier. */
7820 if (model == MEMMODEL_SEQ_CST)
7821 expand_mem_thread_fence (model);
7822 return const0_rtx;
7827 /* If the size of the object is greater than word size on this target,
7828 a default store will not be atomic, Try a mem_exchange and throw away
7829 the result. If that doesn't work, don't do anything. */
7830 if (GET_MODE_PRECISION(mode) > BITS_PER_WORD)
7832 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7833 if (!target)
7834 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7835 if (target)
7836 return const0_rtx;
7837 else
7838 return NULL_RTX;
7841 /* If there is no mem_store, default to a move with barriers */
7842 if (model == MEMMODEL_SEQ_CST || model == MEMMODEL_RELEASE)
7843 expand_mem_thread_fence (model);
7845 emit_move_insn (mem, val);
7847 /* For SEQ_CST, also emit a barrier after the load. */
7848 if (model == MEMMODEL_SEQ_CST)
7849 expand_mem_thread_fence (model);
7851 return const0_rtx;
7855 /* Structure containing the pointers and values required to process the
7856 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7858 struct atomic_op_functions
7860 direct_optab mem_fetch_before;
7861 direct_optab mem_fetch_after;
7862 direct_optab mem_no_result;
7863 optab fetch_before;
7864 optab fetch_after;
7865 direct_optab no_result;
7866 enum rtx_code reverse_code;
7870 /* Fill in structure pointed to by OP with the various optab entries for an
7871 operation of type CODE. */
7873 static void
7874 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7876 gcc_assert (op!= NULL);
7878 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7879 in the source code during compilation, and the optab entries are not
7880 computable until runtime. Fill in the values at runtime. */
7881 switch (code)
7883 case PLUS:
7884 op->mem_fetch_before = atomic_fetch_add_optab;
7885 op->mem_fetch_after = atomic_add_fetch_optab;
7886 op->mem_no_result = atomic_add_optab;
7887 op->fetch_before = sync_old_add_optab;
7888 op->fetch_after = sync_new_add_optab;
7889 op->no_result = sync_add_optab;
7890 op->reverse_code = MINUS;
7891 break;
7892 case MINUS:
7893 op->mem_fetch_before = atomic_fetch_sub_optab;
7894 op->mem_fetch_after = atomic_sub_fetch_optab;
7895 op->mem_no_result = atomic_sub_optab;
7896 op->fetch_before = sync_old_sub_optab;
7897 op->fetch_after = sync_new_sub_optab;
7898 op->no_result = sync_sub_optab;
7899 op->reverse_code = PLUS;
7900 break;
7901 case XOR:
7902 op->mem_fetch_before = atomic_fetch_xor_optab;
7903 op->mem_fetch_after = atomic_xor_fetch_optab;
7904 op->mem_no_result = atomic_xor_optab;
7905 op->fetch_before = sync_old_xor_optab;
7906 op->fetch_after = sync_new_xor_optab;
7907 op->no_result = sync_xor_optab;
7908 op->reverse_code = XOR;
7909 break;
7910 case AND:
7911 op->mem_fetch_before = atomic_fetch_and_optab;
7912 op->mem_fetch_after = atomic_and_fetch_optab;
7913 op->mem_no_result = atomic_and_optab;
7914 op->fetch_before = sync_old_and_optab;
7915 op->fetch_after = sync_new_and_optab;
7916 op->no_result = sync_and_optab;
7917 op->reverse_code = UNKNOWN;
7918 break;
7919 case IOR:
7920 op->mem_fetch_before = atomic_fetch_or_optab;
7921 op->mem_fetch_after = atomic_or_fetch_optab;
7922 op->mem_no_result = atomic_or_optab;
7923 op->fetch_before = sync_old_ior_optab;
7924 op->fetch_after = sync_new_ior_optab;
7925 op->no_result = sync_ior_optab;
7926 op->reverse_code = UNKNOWN;
7927 break;
7928 case NOT:
7929 op->mem_fetch_before = atomic_fetch_nand_optab;
7930 op->mem_fetch_after = atomic_nand_fetch_optab;
7931 op->mem_no_result = atomic_nand_optab;
7932 op->fetch_before = sync_old_nand_optab;
7933 op->fetch_after = sync_new_nand_optab;
7934 op->no_result = sync_nand_optab;
7935 op->reverse_code = UNKNOWN;
7936 break;
7937 default:
7938 gcc_unreachable ();
7942 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7943 using memory order MODEL. If AFTER is true the operation needs to return
7944 the value of *MEM after the operation, otherwise the previous value.
7945 TARGET is an optional place to place the result. The result is unused if
7946 it is const0_rtx.
7947 Return the result if there is a better sequence, otherwise NULL_RTX. */
7949 static rtx
7950 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7951 enum memmodel model, bool after)
7953 /* If the value is prefetched, or not used, it may be possible to replace
7954 the sequence with a native exchange operation. */
7955 if (!after || target == const0_rtx)
7957 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7958 if (code == AND && val == const0_rtx)
7960 if (target == const0_rtx)
7961 target = gen_reg_rtx (GET_MODE (mem));
7962 return maybe_emit_atomic_exchange (target, mem, val, model);
7965 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7966 if (code == IOR && val == constm1_rtx)
7968 if (target == const0_rtx)
7969 target = gen_reg_rtx (GET_MODE (mem));
7970 return maybe_emit_atomic_exchange (target, mem, val, model);
7974 return NULL_RTX;
7977 /* Try to emit an instruction for a specific operation varaition.
7978 OPTAB contains the OP functions.
7979 TARGET is an optional place to return the result. const0_rtx means unused.
7980 MEM is the memory location to operate on.
7981 VAL is the value to use in the operation.
7982 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7983 MODEL is the memory model, if used.
7984 AFTER is true if the returned result is the value after the operation. */
7986 static rtx
7987 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7988 rtx val, bool use_memmodel, enum memmodel model, bool after)
7990 enum machine_mode mode = GET_MODE (mem);
7991 struct expand_operand ops[4];
7992 enum insn_code icode;
7993 int op_counter = 0;
7994 int num_ops;
7996 /* Check to see if there is a result returned. */
7997 if (target == const0_rtx)
7999 if (use_memmodel)
8001 icode = direct_optab_handler (optab->mem_no_result, mode);
8002 create_integer_operand (&ops[2], model);
8003 num_ops = 3;
8005 else
8007 icode = direct_optab_handler (optab->no_result, mode);
8008 num_ops = 2;
8011 /* Otherwise, we need to generate a result. */
8012 else
8014 if (use_memmodel)
8016 icode = direct_optab_handler (after ? optab->mem_fetch_after
8017 : optab->mem_fetch_before, mode);
8018 create_integer_operand (&ops[3], model);
8019 num_ops = 4;
8021 else
8023 icode = optab_handler (after ? optab->fetch_after
8024 : optab->fetch_before, mode);
8025 num_ops = 3;
8027 create_output_operand (&ops[op_counter++], target, mode);
8029 if (icode == CODE_FOR_nothing)
8030 return NULL_RTX;
8032 create_fixed_operand (&ops[op_counter++], mem);
8033 /* VAL may have been promoted to a wider mode. Shrink it if so. */
8034 create_convert_operand_to (&ops[op_counter++], val, mode, true);
8036 if (maybe_expand_insn (icode, num_ops, ops))
8037 return (target == const0_rtx ? const0_rtx : ops[0].value);
8039 return NULL_RTX;
8043 /* This function expands an atomic fetch_OP or OP_fetch operation:
8044 TARGET is an option place to stick the return value. const0_rtx indicates
8045 the result is unused.
8046 atomically fetch MEM, perform the operation with VAL and return it to MEM.
8047 CODE is the operation being performed (OP)
8048 MEMMODEL is the memory model variant to use.
8049 AFTER is true to return the result of the operation (OP_fetch).
8050 AFTER is false to return the value before the operation (fetch_OP). */
8052 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
8053 enum memmodel model, bool after)
8055 enum machine_mode mode = GET_MODE (mem);
8056 struct atomic_op_functions optab;
8057 rtx result;
8058 bool unused_result = (target == const0_rtx);
8060 get_atomic_op_for_code (&optab, code);
8062 /* Check to see if there are any better instructions. */
8063 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
8064 if (result)
8065 return result;
8067 /* Check for the case where the result isn't used and try those patterns. */
8068 if (unused_result)
8070 /* Try the memory model variant first. */
8071 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
8072 if (result)
8073 return result;
8075 /* Next try the old style withuot a memory model. */
8076 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
8077 if (result)
8078 return result;
8080 /* There is no no-result pattern, so try patterns with a result. */
8081 target = NULL_RTX;
8084 /* Try the __atomic version. */
8085 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
8086 if (result)
8087 return result;
8089 /* Try the older __sync version. */
8090 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
8091 if (result)
8092 return result;
8094 /* If the fetch value can be calculated from the other variation of fetch,
8095 try that operation. */
8096 if (after || unused_result || optab.reverse_code != UNKNOWN)
8098 /* Try the __atomic version, then the older __sync version. */
8099 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
8100 if (!result)
8101 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
8103 if (result)
8105 /* If the result isn't used, no need to do compensation code. */
8106 if (unused_result)
8107 return result;
8109 /* Issue compensation code. Fetch_after == fetch_before OP val.
8110 Fetch_before == after REVERSE_OP val. */
8111 if (!after)
8112 code = optab.reverse_code;
8113 if (code == NOT)
8115 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
8116 true, OPTAB_LIB_WIDEN);
8117 result = expand_simple_unop (mode, NOT, result, target, true);
8119 else
8120 result = expand_simple_binop (mode, code, result, val, target,
8121 true, OPTAB_LIB_WIDEN);
8122 return result;
8126 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
8127 if (!can_compare_and_swap_p (mode, false))
8129 rtx libfunc;
8130 bool fixup = false;
8132 libfunc = optab_libfunc (after ? optab.fetch_after
8133 : optab.fetch_before, mode);
8134 if (libfunc == NULL
8135 && (after || unused_result || optab.reverse_code != UNKNOWN))
8137 fixup = true;
8138 if (!after)
8139 code = optab.reverse_code;
8140 libfunc = optab_libfunc (after ? optab.fetch_before
8141 : optab.fetch_after, mode);
8143 if (libfunc != NULL)
8145 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
8146 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
8147 2, addr, ptr_mode, val, mode);
8149 if (!unused_result && fixup)
8150 result = expand_simple_binop (mode, code, result, val, target,
8151 true, OPTAB_LIB_WIDEN);
8152 return result;
8156 /* If nothing else has succeeded, default to a compare and swap loop. */
8157 if (can_compare_and_swap_p (mode, true))
8159 rtx insn;
8160 rtx t0 = gen_reg_rtx (mode), t1;
8162 start_sequence ();
8164 /* If the result is used, get a register for it. */
8165 if (!unused_result)
8167 if (!target || !register_operand (target, mode))
8168 target = gen_reg_rtx (mode);
8169 /* If fetch_before, copy the value now. */
8170 if (!after)
8171 emit_move_insn (target, t0);
8173 else
8174 target = const0_rtx;
8176 t1 = t0;
8177 if (code == NOT)
8179 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
8180 true, OPTAB_LIB_WIDEN);
8181 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
8183 else
8184 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
8185 OPTAB_LIB_WIDEN);
8187 /* For after, copy the value now. */
8188 if (!unused_result && after)
8189 emit_move_insn (target, t1);
8190 insn = get_insns ();
8191 end_sequence ();
8193 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
8194 return target;
8197 return NULL_RTX;
8200 /* Return true if OPERAND is suitable for operand number OPNO of
8201 instruction ICODE. */
8203 bool
8204 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
8206 return (!insn_data[(int) icode].operand[opno].predicate
8207 || (insn_data[(int) icode].operand[opno].predicate
8208 (operand, insn_data[(int) icode].operand[opno].mode)));
8211 /* TARGET is a target of a multiword operation that we are going to
8212 implement as a series of word-mode operations. Return true if
8213 TARGET is suitable for this purpose. */
8215 bool
8216 valid_multiword_target_p (rtx target)
8218 enum machine_mode mode;
8219 int i;
8221 mode = GET_MODE (target);
8222 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
8223 if (!validate_subreg (word_mode, mode, target, i))
8224 return false;
8225 return true;
8228 /* Like maybe_legitimize_operand, but do not change the code of the
8229 current rtx value. */
8231 static bool
8232 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8233 struct expand_operand *op)
8235 /* See if the operand matches in its current form. */
8236 if (insn_operand_matches (icode, opno, op->value))
8237 return true;
8239 /* If the operand is a memory whose address has no side effects,
8240 try forcing the address into a non-virtual pseudo register.
8241 The check for side effects is important because copy_to_mode_reg
8242 cannot handle things like auto-modified addresses. */
8243 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8245 rtx addr, mem;
8247 mem = op->value;
8248 addr = XEXP (mem, 0);
8249 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8250 && !side_effects_p (addr))
8252 rtx last;
8253 enum machine_mode mode;
8255 last = get_last_insn ();
8256 mode = get_address_mode (mem);
8257 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8258 if (insn_operand_matches (icode, opno, mem))
8260 op->value = mem;
8261 return true;
8263 delete_insns_since (last);
8267 return false;
8270 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8271 on success, storing the new operand value back in OP. */
8273 static bool
8274 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8275 struct expand_operand *op)
8277 enum machine_mode mode, imode;
8278 bool old_volatile_ok, result;
8280 mode = op->mode;
8281 switch (op->type)
8283 case EXPAND_FIXED:
8284 old_volatile_ok = volatile_ok;
8285 volatile_ok = true;
8286 result = maybe_legitimize_operand_same_code (icode, opno, op);
8287 volatile_ok = old_volatile_ok;
8288 return result;
8290 case EXPAND_OUTPUT:
8291 gcc_assert (mode != VOIDmode);
8292 if (op->value
8293 && op->value != const0_rtx
8294 && GET_MODE (op->value) == mode
8295 && maybe_legitimize_operand_same_code (icode, opno, op))
8296 return true;
8298 op->value = gen_reg_rtx (mode);
8299 break;
8301 case EXPAND_INPUT:
8302 input:
8303 gcc_assert (mode != VOIDmode);
8304 gcc_assert (GET_MODE (op->value) == VOIDmode
8305 || GET_MODE (op->value) == mode);
8306 if (maybe_legitimize_operand_same_code (icode, opno, op))
8307 return true;
8309 op->value = copy_to_mode_reg (mode, op->value);
8310 break;
8312 case EXPAND_CONVERT_TO:
8313 gcc_assert (mode != VOIDmode);
8314 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8315 goto input;
8317 case EXPAND_CONVERT_FROM:
8318 if (GET_MODE (op->value) != VOIDmode)
8319 mode = GET_MODE (op->value);
8320 else
8321 /* The caller must tell us what mode this value has. */
8322 gcc_assert (mode != VOIDmode);
8324 imode = insn_data[(int) icode].operand[opno].mode;
8325 if (imode != VOIDmode && imode != mode)
8327 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8328 mode = imode;
8330 goto input;
8332 case EXPAND_ADDRESS:
8333 gcc_assert (mode != VOIDmode);
8334 op->value = convert_memory_address (mode, op->value);
8335 goto input;
8337 case EXPAND_INTEGER:
8338 mode = insn_data[(int) icode].operand[opno].mode;
8339 if (mode != VOIDmode && const_int_operand (op->value, mode))
8340 goto input;
8341 break;
8343 return insn_operand_matches (icode, opno, op->value);
8346 /* Make OP describe an input operand that should have the same value
8347 as VALUE, after any mode conversion that the target might request.
8348 TYPE is the type of VALUE. */
8350 void
8351 create_convert_operand_from_type (struct expand_operand *op,
8352 rtx value, tree type)
8354 create_convert_operand_from (op, value, TYPE_MODE (type),
8355 TYPE_UNSIGNED (type));
8358 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8359 of instruction ICODE. Return true on success, leaving the new operand
8360 values in the OPS themselves. Emit no code on failure. */
8362 bool
8363 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8364 unsigned int nops, struct expand_operand *ops)
8366 rtx last;
8367 unsigned int i;
8369 last = get_last_insn ();
8370 for (i = 0; i < nops; i++)
8371 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8373 delete_insns_since (last);
8374 return false;
8376 return true;
8379 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8380 as its operands. Return the instruction pattern on success,
8381 and emit any necessary set-up code. Return null and emit no
8382 code on failure. */
8385 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8386 struct expand_operand *ops)
8388 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8389 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8390 return NULL_RTX;
8392 switch (nops)
8394 case 1:
8395 return GEN_FCN (icode) (ops[0].value);
8396 case 2:
8397 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8398 case 3:
8399 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8400 case 4:
8401 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8402 ops[3].value);
8403 case 5:
8404 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8405 ops[3].value, ops[4].value);
8406 case 6:
8407 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8408 ops[3].value, ops[4].value, ops[5].value);
8409 case 7:
8410 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8411 ops[3].value, ops[4].value, ops[5].value,
8412 ops[6].value);
8413 case 8:
8414 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8415 ops[3].value, ops[4].value, ops[5].value,
8416 ops[6].value, ops[7].value);
8418 gcc_unreachable ();
8421 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8422 as its operands. Return true on success and emit no code on failure. */
8424 bool
8425 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8426 struct expand_operand *ops)
8428 rtx pat = maybe_gen_insn (icode, nops, ops);
8429 if (pat)
8431 emit_insn (pat);
8432 return true;
8434 return false;
8437 /* Like maybe_expand_insn, but for jumps. */
8439 bool
8440 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8441 struct expand_operand *ops)
8443 rtx pat = maybe_gen_insn (icode, nops, ops);
8444 if (pat)
8446 emit_jump_insn (pat);
8447 return true;
8449 return false;
8452 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8453 as its operands. */
8455 void
8456 expand_insn (enum insn_code icode, unsigned int nops,
8457 struct expand_operand *ops)
8459 if (!maybe_expand_insn (icode, nops, ops))
8460 gcc_unreachable ();
8463 /* Like expand_insn, but for jumps. */
8465 void
8466 expand_jump_insn (enum insn_code icode, unsigned int nops,
8467 struct expand_operand *ops)
8469 if (!maybe_expand_jump_insn (icode, nops, ops))
8470 gcc_unreachable ();
8473 #include "gt-optabs.h"