Add -fno-sync-libcalls.
[official-gcc.git] / gcc / optabs.c
blob7ef513acae6ae3b6059b7171a3227c1c3c096d3d
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;
4156 /* Handle a libcall just for the mode we are using. */
4157 libfunc = optab_libfunc (cmp_optab, mode);
4158 gcc_assert (libfunc);
4160 /* If we want unsigned, and this mode has a distinct unsigned
4161 comparison routine, use that. */
4162 if (unsignedp)
4164 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4165 if (ulibfunc)
4166 libfunc = ulibfunc;
4169 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4170 targetm.libgcc_cmp_return_mode (),
4171 2, x, mode, y, mode);
4173 /* There are two kinds of comparison routines. Biased routines
4174 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4175 of gcc expect that the comparison operation is equivalent
4176 to the modified comparison. For signed comparisons compare the
4177 result against 1 in the biased case, and zero in the unbiased
4178 case. For unsigned comparisons always compare against 1 after
4179 biasing the unbiased result by adding 1. This gives us a way to
4180 represent LTU.
4181 The comparisons in the fixed-point helper library are always
4182 biased. */
4183 x = result;
4184 y = const1_rtx;
4186 if (!TARGET_LIB_INT_CMP_BIASED && !ALL_FIXED_POINT_MODE_P (mode))
4188 if (unsignedp)
4189 x = plus_constant (result, 1);
4190 else
4191 y = const0_rtx;
4194 *pmode = word_mode;
4195 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4196 ptest, pmode);
4198 else
4199 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4201 return;
4203 fail:
4204 *ptest = NULL_RTX;
4207 /* Before emitting an insn with code ICODE, make sure that X, which is going
4208 to be used for operand OPNUM of the insn, is converted from mode MODE to
4209 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4210 that it is accepted by the operand predicate. Return the new value. */
4213 prepare_operand (enum insn_code icode, rtx x, int opnum, enum machine_mode mode,
4214 enum machine_mode wider_mode, int unsignedp)
4216 if (mode != wider_mode)
4217 x = convert_modes (wider_mode, mode, x, unsignedp);
4219 if (!insn_operand_matches (icode, opnum, x))
4221 if (reload_completed)
4222 return NULL_RTX;
4223 x = copy_to_mode_reg (insn_data[(int) icode].operand[opnum].mode, x);
4226 return x;
4229 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4230 we can do the branch. */
4232 static void
4233 emit_cmp_and_jump_insn_1 (rtx test, enum machine_mode mode, rtx label)
4235 enum machine_mode optab_mode;
4236 enum mode_class mclass;
4237 enum insn_code icode;
4239 mclass = GET_MODE_CLASS (mode);
4240 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4241 icode = optab_handler (cbranch_optab, optab_mode);
4243 gcc_assert (icode != CODE_FOR_nothing);
4244 gcc_assert (insn_operand_matches (icode, 0, test));
4245 emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0), XEXP (test, 1), label));
4248 /* Generate code to compare X with Y so that the condition codes are
4249 set and to jump to LABEL if the condition is true. If X is a
4250 constant and Y is not a constant, then the comparison is swapped to
4251 ensure that the comparison RTL has the canonical form.
4253 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4254 need to be widened. UNSIGNEDP is also used to select the proper
4255 branch condition code.
4257 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4259 MODE is the mode of the inputs (in case they are const_int).
4261 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4262 It will be potentially converted into an unsigned variant based on
4263 UNSIGNEDP to select a proper jump instruction. */
4265 void
4266 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4267 enum machine_mode mode, int unsignedp, rtx label)
4269 rtx op0 = x, op1 = y;
4270 rtx test;
4272 /* Swap operands and condition to ensure canonical RTL. */
4273 if (swap_commutative_operands_p (x, y)
4274 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4276 op0 = y, op1 = x;
4277 comparison = swap_condition (comparison);
4280 /* If OP0 is still a constant, then both X and Y must be constants
4281 or the opposite comparison is not supported. Force X into a register
4282 to create canonical RTL. */
4283 if (CONSTANT_P (op0))
4284 op0 = force_reg (mode, op0);
4286 if (unsignedp)
4287 comparison = unsigned_condition (comparison);
4289 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4290 &test, &mode);
4291 emit_cmp_and_jump_insn_1 (test, mode, label);
4295 /* Emit a library call comparison between floating point X and Y.
4296 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4298 static void
4299 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4300 rtx *ptest, enum machine_mode *pmode)
4302 enum rtx_code swapped = swap_condition (comparison);
4303 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4304 enum machine_mode orig_mode = GET_MODE (x);
4305 enum machine_mode mode, cmp_mode;
4306 rtx true_rtx, false_rtx;
4307 rtx value, target, insns, equiv;
4308 rtx libfunc = 0;
4309 bool reversed_p = false;
4310 cmp_mode = targetm.libgcc_cmp_return_mode ();
4312 for (mode = orig_mode;
4313 mode != VOIDmode;
4314 mode = GET_MODE_WIDER_MODE (mode))
4316 if (code_to_optab[comparison]
4317 && (libfunc = optab_libfunc (code_to_optab[comparison], mode)))
4318 break;
4320 if (code_to_optab[swapped]
4321 && (libfunc = optab_libfunc (code_to_optab[swapped], mode)))
4323 rtx tmp;
4324 tmp = x; x = y; y = tmp;
4325 comparison = swapped;
4326 break;
4329 if (code_to_optab[reversed]
4330 && (libfunc = optab_libfunc (code_to_optab[reversed], mode)))
4332 comparison = reversed;
4333 reversed_p = true;
4334 break;
4338 gcc_assert (mode != VOIDmode);
4340 if (mode != orig_mode)
4342 x = convert_to_mode (mode, x, 0);
4343 y = convert_to_mode (mode, y, 0);
4346 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4347 the RTL. The allows the RTL optimizers to delete the libcall if the
4348 condition can be determined at compile-time. */
4349 if (comparison == UNORDERED
4350 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4352 true_rtx = const_true_rtx;
4353 false_rtx = const0_rtx;
4355 else
4357 switch (comparison)
4359 case EQ:
4360 true_rtx = const0_rtx;
4361 false_rtx = const_true_rtx;
4362 break;
4364 case NE:
4365 true_rtx = const_true_rtx;
4366 false_rtx = const0_rtx;
4367 break;
4369 case GT:
4370 true_rtx = const1_rtx;
4371 false_rtx = const0_rtx;
4372 break;
4374 case GE:
4375 true_rtx = const0_rtx;
4376 false_rtx = constm1_rtx;
4377 break;
4379 case LT:
4380 true_rtx = constm1_rtx;
4381 false_rtx = const0_rtx;
4382 break;
4384 case LE:
4385 true_rtx = const0_rtx;
4386 false_rtx = const1_rtx;
4387 break;
4389 default:
4390 gcc_unreachable ();
4394 if (comparison == UNORDERED)
4396 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4397 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4398 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4399 temp, const_true_rtx, equiv);
4401 else
4403 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4404 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4405 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4406 equiv, true_rtx, false_rtx);
4409 start_sequence ();
4410 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4411 cmp_mode, 2, x, mode, y, mode);
4412 insns = get_insns ();
4413 end_sequence ();
4415 target = gen_reg_rtx (cmp_mode);
4416 emit_libcall_block (insns, target, value, equiv);
4418 if (comparison == UNORDERED
4419 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison)
4420 || reversed_p)
4421 *ptest = gen_rtx_fmt_ee (reversed_p ? EQ : NE, VOIDmode, target, false_rtx);
4422 else
4423 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4425 *pmode = cmp_mode;
4428 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4430 void
4431 emit_indirect_jump (rtx loc)
4433 struct expand_operand ops[1];
4435 create_address_operand (&ops[0], loc);
4436 expand_jump_insn (CODE_FOR_indirect_jump, 1, ops);
4437 emit_barrier ();
4440 #ifdef HAVE_conditional_move
4442 /* Emit a conditional move instruction if the machine supports one for that
4443 condition and machine mode.
4445 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4446 the mode to use should they be constants. If it is VOIDmode, they cannot
4447 both be constants.
4449 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4450 should be stored there. MODE is the mode to use should they be constants.
4451 If it is VOIDmode, they cannot both be constants.
4453 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4454 is not supported. */
4457 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4458 enum machine_mode cmode, rtx op2, rtx op3,
4459 enum machine_mode mode, int unsignedp)
4461 rtx tem, comparison, last;
4462 enum insn_code icode;
4463 enum rtx_code reversed;
4465 /* If one operand is constant, make it the second one. Only do this
4466 if the other operand is not constant as well. */
4468 if (swap_commutative_operands_p (op0, op1))
4470 tem = op0;
4471 op0 = op1;
4472 op1 = tem;
4473 code = swap_condition (code);
4476 /* get_condition will prefer to generate LT and GT even if the old
4477 comparison was against zero, so undo that canonicalization here since
4478 comparisons against zero are cheaper. */
4479 if (code == LT && op1 == const1_rtx)
4480 code = LE, op1 = const0_rtx;
4481 else if (code == GT && op1 == constm1_rtx)
4482 code = GE, op1 = const0_rtx;
4484 if (cmode == VOIDmode)
4485 cmode = GET_MODE (op0);
4487 if (swap_commutative_operands_p (op2, op3)
4488 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4489 != UNKNOWN))
4491 tem = op2;
4492 op2 = op3;
4493 op3 = tem;
4494 code = reversed;
4497 if (mode == VOIDmode)
4498 mode = GET_MODE (op2);
4500 icode = direct_optab_handler (movcc_optab, mode);
4502 if (icode == CODE_FOR_nothing)
4503 return 0;
4505 if (!target)
4506 target = gen_reg_rtx (mode);
4508 code = unsignedp ? unsigned_condition (code) : code;
4509 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4511 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4512 return NULL and let the caller figure out how best to deal with this
4513 situation. */
4514 if (!COMPARISON_P (comparison))
4515 return NULL_RTX;
4517 do_pending_stack_adjust ();
4518 last = get_last_insn ();
4519 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4520 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4521 &comparison, &cmode);
4522 if (comparison)
4524 struct expand_operand ops[4];
4526 create_output_operand (&ops[0], target, mode);
4527 create_fixed_operand (&ops[1], comparison);
4528 create_input_operand (&ops[2], op2, mode);
4529 create_input_operand (&ops[3], op3, mode);
4530 if (maybe_expand_insn (icode, 4, ops))
4532 if (ops[0].value != target)
4533 convert_move (target, ops[0].value, false);
4534 return target;
4537 delete_insns_since (last);
4538 return NULL_RTX;
4541 /* Return nonzero if a conditional move of mode MODE is supported.
4543 This function is for combine so it can tell whether an insn that looks
4544 like a conditional move is actually supported by the hardware. If we
4545 guess wrong we lose a bit on optimization, but that's it. */
4546 /* ??? sparc64 supports conditionally moving integers values based on fp
4547 comparisons, and vice versa. How do we handle them? */
4550 can_conditionally_move_p (enum machine_mode mode)
4552 if (direct_optab_handler (movcc_optab, mode) != CODE_FOR_nothing)
4553 return 1;
4555 return 0;
4558 #endif /* HAVE_conditional_move */
4560 /* Emit a conditional addition instruction if the machine supports one for that
4561 condition and machine mode.
4563 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4564 the mode to use should they be constants. If it is VOIDmode, they cannot
4565 both be constants.
4567 OP2 should be stored in TARGET if the comparison is true, otherwise OP2+OP3
4568 should be stored there. MODE is the mode to use should they be constants.
4569 If it is VOIDmode, they cannot both be constants.
4571 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4572 is not supported. */
4575 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4576 enum machine_mode cmode, rtx op2, rtx op3,
4577 enum machine_mode mode, int unsignedp)
4579 rtx tem, comparison, last;
4580 enum insn_code icode;
4581 enum rtx_code reversed;
4583 /* If one operand is constant, make it the second one. Only do this
4584 if the other operand is not constant as well. */
4586 if (swap_commutative_operands_p (op0, op1))
4588 tem = op0;
4589 op0 = op1;
4590 op1 = tem;
4591 code = swap_condition (code);
4594 /* get_condition will prefer to generate LT and GT even if the old
4595 comparison was against zero, so undo that canonicalization here since
4596 comparisons against zero are cheaper. */
4597 if (code == LT && op1 == const1_rtx)
4598 code = LE, op1 = const0_rtx;
4599 else if (code == GT && op1 == constm1_rtx)
4600 code = GE, op1 = const0_rtx;
4602 if (cmode == VOIDmode)
4603 cmode = GET_MODE (op0);
4605 if (swap_commutative_operands_p (op2, op3)
4606 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4607 != UNKNOWN))
4609 tem = op2;
4610 op2 = op3;
4611 op3 = tem;
4612 code = reversed;
4615 if (mode == VOIDmode)
4616 mode = GET_MODE (op2);
4618 icode = optab_handler (addcc_optab, mode);
4620 if (icode == CODE_FOR_nothing)
4621 return 0;
4623 if (!target)
4624 target = gen_reg_rtx (mode);
4626 code = unsignedp ? unsigned_condition (code) : code;
4627 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4629 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4630 return NULL and let the caller figure out how best to deal with this
4631 situation. */
4632 if (!COMPARISON_P (comparison))
4633 return NULL_RTX;
4635 do_pending_stack_adjust ();
4636 last = get_last_insn ();
4637 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4638 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4639 &comparison, &cmode);
4640 if (comparison)
4642 struct expand_operand ops[4];
4644 create_output_operand (&ops[0], target, mode);
4645 create_fixed_operand (&ops[1], comparison);
4646 create_input_operand (&ops[2], op2, mode);
4647 create_input_operand (&ops[3], op3, mode);
4648 if (maybe_expand_insn (icode, 4, ops))
4650 if (ops[0].value != target)
4651 convert_move (target, ops[0].value, false);
4652 return target;
4655 delete_insns_since (last);
4656 return NULL_RTX;
4659 /* These functions attempt to generate an insn body, rather than
4660 emitting the insn, but if the gen function already emits them, we
4661 make no attempt to turn them back into naked patterns. */
4663 /* Generate and return an insn body to add Y to X. */
4666 gen_add2_insn (rtx x, rtx y)
4668 enum insn_code icode = optab_handler (add_optab, GET_MODE (x));
4670 gcc_assert (insn_operand_matches (icode, 0, x));
4671 gcc_assert (insn_operand_matches (icode, 1, x));
4672 gcc_assert (insn_operand_matches (icode, 2, y));
4674 return GEN_FCN (icode) (x, x, y);
4677 /* Generate and return an insn body to add r1 and c,
4678 storing the result in r0. */
4681 gen_add3_insn (rtx r0, rtx r1, rtx c)
4683 enum insn_code icode = optab_handler (add_optab, GET_MODE (r0));
4685 if (icode == CODE_FOR_nothing
4686 || !insn_operand_matches (icode, 0, r0)
4687 || !insn_operand_matches (icode, 1, r1)
4688 || !insn_operand_matches (icode, 2, c))
4689 return NULL_RTX;
4691 return GEN_FCN (icode) (r0, r1, c);
4695 have_add2_insn (rtx x, rtx y)
4697 enum insn_code icode;
4699 gcc_assert (GET_MODE (x) != VOIDmode);
4701 icode = optab_handler (add_optab, GET_MODE (x));
4703 if (icode == CODE_FOR_nothing)
4704 return 0;
4706 if (!insn_operand_matches (icode, 0, x)
4707 || !insn_operand_matches (icode, 1, x)
4708 || !insn_operand_matches (icode, 2, y))
4709 return 0;
4711 return 1;
4714 /* Generate and return an insn body to subtract Y from X. */
4717 gen_sub2_insn (rtx x, rtx y)
4719 enum insn_code icode = optab_handler (sub_optab, GET_MODE (x));
4721 gcc_assert (insn_operand_matches (icode, 0, x));
4722 gcc_assert (insn_operand_matches (icode, 1, x));
4723 gcc_assert (insn_operand_matches (icode, 2, y));
4725 return GEN_FCN (icode) (x, x, y);
4728 /* Generate and return an insn body to subtract r1 and c,
4729 storing the result in r0. */
4732 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4734 enum insn_code icode = optab_handler (sub_optab, GET_MODE (r0));
4736 if (icode == CODE_FOR_nothing
4737 || !insn_operand_matches (icode, 0, r0)
4738 || !insn_operand_matches (icode, 1, r1)
4739 || !insn_operand_matches (icode, 2, c))
4740 return NULL_RTX;
4742 return GEN_FCN (icode) (r0, r1, c);
4746 have_sub2_insn (rtx x, rtx y)
4748 enum insn_code icode;
4750 gcc_assert (GET_MODE (x) != VOIDmode);
4752 icode = optab_handler (sub_optab, GET_MODE (x));
4754 if (icode == CODE_FOR_nothing)
4755 return 0;
4757 if (!insn_operand_matches (icode, 0, x)
4758 || !insn_operand_matches (icode, 1, x)
4759 || !insn_operand_matches (icode, 2, y))
4760 return 0;
4762 return 1;
4765 /* Generate the body of an instruction to copy Y into X.
4766 It may be a list of insns, if one insn isn't enough. */
4769 gen_move_insn (rtx x, rtx y)
4771 rtx seq;
4773 start_sequence ();
4774 emit_move_insn_1 (x, y);
4775 seq = get_insns ();
4776 end_sequence ();
4777 return seq;
4780 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4781 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4782 no such operation exists, CODE_FOR_nothing will be returned. */
4784 enum insn_code
4785 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
4786 int unsignedp)
4788 convert_optab tab;
4789 #ifdef HAVE_ptr_extend
4790 if (unsignedp < 0)
4791 return CODE_FOR_ptr_extend;
4792 #endif
4794 tab = unsignedp ? zext_optab : sext_optab;
4795 return convert_optab_handler (tab, to_mode, from_mode);
4798 /* Generate the body of an insn to extend Y (with mode MFROM)
4799 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4802 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4803 enum machine_mode mfrom, int unsignedp)
4805 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4806 return GEN_FCN (icode) (x, y);
4809 /* can_fix_p and can_float_p say whether the target machine
4810 can directly convert a given fixed point type to
4811 a given floating point type, or vice versa.
4812 The returned value is the CODE_FOR_... value to use,
4813 or CODE_FOR_nothing if these modes cannot be directly converted.
4815 *TRUNCP_PTR is set to 1 if it is necessary to output
4816 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4818 static enum insn_code
4819 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4820 int unsignedp, int *truncp_ptr)
4822 convert_optab tab;
4823 enum insn_code icode;
4825 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4826 icode = convert_optab_handler (tab, fixmode, fltmode);
4827 if (icode != CODE_FOR_nothing)
4829 *truncp_ptr = 0;
4830 return icode;
4833 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4834 for this to work. We need to rework the fix* and ftrunc* patterns
4835 and documentation. */
4836 tab = unsignedp ? ufix_optab : sfix_optab;
4837 icode = convert_optab_handler (tab, fixmode, fltmode);
4838 if (icode != CODE_FOR_nothing
4839 && optab_handler (ftrunc_optab, fltmode) != CODE_FOR_nothing)
4841 *truncp_ptr = 1;
4842 return icode;
4845 *truncp_ptr = 0;
4846 return CODE_FOR_nothing;
4849 enum insn_code
4850 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4851 int unsignedp)
4853 convert_optab tab;
4855 tab = unsignedp ? ufloat_optab : sfloat_optab;
4856 return convert_optab_handler (tab, fltmode, fixmode);
4859 /* Function supportable_convert_operation
4861 Check whether an operation represented by the code CODE is a
4862 convert operation that is supported by the target platform in
4863 vector form (i.e., when operating on arguments of type VECTYPE_IN
4864 producing a result of type VECTYPE_OUT).
4866 Convert operations we currently support directly are FIX_TRUNC and FLOAT.
4867 This function checks if these operations are supported
4868 by the target platform either directly (via vector tree-codes), or via
4869 target builtins.
4871 Output:
4872 - CODE1 is code of vector operation to be used when
4873 vectorizing the operation, if available.
4874 - DECL is decl of target builtin functions to be used
4875 when vectorizing the operation, if available. In this case,
4876 CODE1 is CALL_EXPR. */
4878 bool
4879 supportable_convert_operation (enum tree_code code,
4880 tree vectype_out, tree vectype_in,
4881 tree *decl, enum tree_code *code1)
4883 enum machine_mode m1,m2;
4884 int truncp;
4886 m1 = TYPE_MODE (vectype_out);
4887 m2 = TYPE_MODE (vectype_in);
4889 /* First check if we can done conversion directly. */
4890 if ((code == FIX_TRUNC_EXPR
4891 && can_fix_p (m1,m2,TYPE_UNSIGNED (vectype_out), &truncp)
4892 != CODE_FOR_nothing)
4893 || (code == FLOAT_EXPR
4894 && can_float_p (m1,m2,TYPE_UNSIGNED (vectype_in))
4895 != CODE_FOR_nothing))
4897 *code1 = code;
4898 return true;
4901 /* Now check for builtin. */
4902 if (targetm.vectorize.builtin_conversion
4903 && targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in))
4905 *code1 = CALL_EXPR;
4906 *decl = targetm.vectorize.builtin_conversion (code, vectype_out, vectype_in);
4907 return true;
4909 return false;
4913 /* Generate code to convert FROM to floating point
4914 and store in TO. FROM must be fixed point and not VOIDmode.
4915 UNSIGNEDP nonzero means regard FROM as unsigned.
4916 Normally this is done by correcting the final value
4917 if it is negative. */
4919 void
4920 expand_float (rtx to, rtx from, int unsignedp)
4922 enum insn_code icode;
4923 rtx target = to;
4924 enum machine_mode fmode, imode;
4925 bool can_do_signed = false;
4927 /* Crash now, because we won't be able to decide which mode to use. */
4928 gcc_assert (GET_MODE (from) != VOIDmode);
4930 /* Look for an insn to do the conversion. Do it in the specified
4931 modes if possible; otherwise convert either input, output or both to
4932 wider mode. If the integer mode is wider than the mode of FROM,
4933 we can do the conversion signed even if the input is unsigned. */
4935 for (fmode = GET_MODE (to); fmode != VOIDmode;
4936 fmode = GET_MODE_WIDER_MODE (fmode))
4937 for (imode = GET_MODE (from); imode != VOIDmode;
4938 imode = GET_MODE_WIDER_MODE (imode))
4940 int doing_unsigned = unsignedp;
4942 if (fmode != GET_MODE (to)
4943 && significand_size (fmode) < GET_MODE_PRECISION (GET_MODE (from)))
4944 continue;
4946 icode = can_float_p (fmode, imode, unsignedp);
4947 if (icode == CODE_FOR_nothing && unsignedp)
4949 enum insn_code scode = can_float_p (fmode, imode, 0);
4950 if (scode != CODE_FOR_nothing)
4951 can_do_signed = true;
4952 if (imode != GET_MODE (from))
4953 icode = scode, doing_unsigned = 0;
4956 if (icode != CODE_FOR_nothing)
4958 if (imode != GET_MODE (from))
4959 from = convert_to_mode (imode, from, unsignedp);
4961 if (fmode != GET_MODE (to))
4962 target = gen_reg_rtx (fmode);
4964 emit_unop_insn (icode, target, from,
4965 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4967 if (target != to)
4968 convert_move (to, target, 0);
4969 return;
4973 /* Unsigned integer, and no way to convert directly. Convert as signed,
4974 then unconditionally adjust the result. */
4975 if (unsignedp && can_do_signed)
4977 rtx label = gen_label_rtx ();
4978 rtx temp;
4979 REAL_VALUE_TYPE offset;
4981 /* Look for a usable floating mode FMODE wider than the source and at
4982 least as wide as the target. Using FMODE will avoid rounding woes
4983 with unsigned values greater than the signed maximum value. */
4985 for (fmode = GET_MODE (to); fmode != VOIDmode;
4986 fmode = GET_MODE_WIDER_MODE (fmode))
4987 if (GET_MODE_PRECISION (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
4988 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
4989 break;
4991 if (fmode == VOIDmode)
4993 /* There is no such mode. Pretend the target is wide enough. */
4994 fmode = GET_MODE (to);
4996 /* Avoid double-rounding when TO is narrower than FROM. */
4997 if ((significand_size (fmode) + 1)
4998 < GET_MODE_PRECISION (GET_MODE (from)))
5000 rtx temp1;
5001 rtx neglabel = gen_label_rtx ();
5003 /* Don't use TARGET if it isn't a register, is a hard register,
5004 or is the wrong mode. */
5005 if (!REG_P (target)
5006 || REGNO (target) < FIRST_PSEUDO_REGISTER
5007 || GET_MODE (target) != fmode)
5008 target = gen_reg_rtx (fmode);
5010 imode = GET_MODE (from);
5011 do_pending_stack_adjust ();
5013 /* Test whether the sign bit is set. */
5014 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5015 0, neglabel);
5017 /* The sign bit is not set. Convert as signed. */
5018 expand_float (target, from, 0);
5019 emit_jump_insn (gen_jump (label));
5020 emit_barrier ();
5022 /* The sign bit is set.
5023 Convert to a usable (positive signed) value by shifting right
5024 one bit, while remembering if a nonzero bit was shifted
5025 out; i.e., compute (from & 1) | (from >> 1). */
5027 emit_label (neglabel);
5028 temp = expand_binop (imode, and_optab, from, const1_rtx,
5029 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5030 temp1 = expand_shift (RSHIFT_EXPR, imode, from, 1, NULL_RTX, 1);
5031 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5032 OPTAB_LIB_WIDEN);
5033 expand_float (target, temp, 0);
5035 /* Multiply by 2 to undo the shift above. */
5036 temp = expand_binop (fmode, add_optab, target, target,
5037 target, 0, OPTAB_LIB_WIDEN);
5038 if (temp != target)
5039 emit_move_insn (target, temp);
5041 do_pending_stack_adjust ();
5042 emit_label (label);
5043 goto done;
5047 /* If we are about to do some arithmetic to correct for an
5048 unsigned operand, do it in a pseudo-register. */
5050 if (GET_MODE (to) != fmode
5051 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5052 target = gen_reg_rtx (fmode);
5054 /* Convert as signed integer to floating. */
5055 expand_float (target, from, 0);
5057 /* If FROM is negative (and therefore TO is negative),
5058 correct its value by 2**bitwidth. */
5060 do_pending_stack_adjust ();
5061 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5062 0, label);
5065 real_2expN (&offset, GET_MODE_PRECISION (GET_MODE (from)), fmode);
5066 temp = expand_binop (fmode, add_optab, target,
5067 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5068 target, 0, OPTAB_LIB_WIDEN);
5069 if (temp != target)
5070 emit_move_insn (target, temp);
5072 do_pending_stack_adjust ();
5073 emit_label (label);
5074 goto done;
5077 /* No hardware instruction available; call a library routine. */
5079 rtx libfunc;
5080 rtx insns;
5081 rtx value;
5082 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5084 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
5085 from = convert_to_mode (SImode, from, unsignedp);
5087 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5088 gcc_assert (libfunc);
5090 start_sequence ();
5092 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5093 GET_MODE (to), 1, from,
5094 GET_MODE (from));
5095 insns = get_insns ();
5096 end_sequence ();
5098 emit_libcall_block (insns, target, value,
5099 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5100 GET_MODE (to), from));
5103 done:
5105 /* Copy result to requested destination
5106 if we have been computing in a temp location. */
5108 if (target != to)
5110 if (GET_MODE (target) == GET_MODE (to))
5111 emit_move_insn (to, target);
5112 else
5113 convert_move (to, target, 0);
5117 /* Generate code to convert FROM to fixed point and store in TO. FROM
5118 must be floating point. */
5120 void
5121 expand_fix (rtx to, rtx from, int unsignedp)
5123 enum insn_code icode;
5124 rtx target = to;
5125 enum machine_mode fmode, imode;
5126 int must_trunc = 0;
5128 /* We first try to find a pair of modes, one real and one integer, at
5129 least as wide as FROM and TO, respectively, in which we can open-code
5130 this conversion. If the integer mode is wider than the mode of TO,
5131 we can do the conversion either signed or unsigned. */
5133 for (fmode = GET_MODE (from); fmode != VOIDmode;
5134 fmode = GET_MODE_WIDER_MODE (fmode))
5135 for (imode = GET_MODE (to); imode != VOIDmode;
5136 imode = GET_MODE_WIDER_MODE (imode))
5138 int doing_unsigned = unsignedp;
5140 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5141 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5142 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5144 if (icode != CODE_FOR_nothing)
5146 rtx last = get_last_insn ();
5147 if (fmode != GET_MODE (from))
5148 from = convert_to_mode (fmode, from, 0);
5150 if (must_trunc)
5152 rtx temp = gen_reg_rtx (GET_MODE (from));
5153 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5154 temp, 0);
5157 if (imode != GET_MODE (to))
5158 target = gen_reg_rtx (imode);
5160 if (maybe_emit_unop_insn (icode, target, from,
5161 doing_unsigned ? UNSIGNED_FIX : FIX))
5163 if (target != to)
5164 convert_move (to, target, unsignedp);
5165 return;
5167 delete_insns_since (last);
5171 /* For an unsigned conversion, there is one more way to do it.
5172 If we have a signed conversion, we generate code that compares
5173 the real value to the largest representable positive number. If if
5174 is smaller, the conversion is done normally. Otherwise, subtract
5175 one plus the highest signed number, convert, and add it back.
5177 We only need to check all real modes, since we know we didn't find
5178 anything with a wider integer mode.
5180 This code used to extend FP value into mode wider than the destination.
5181 This is needed for decimal float modes which cannot accurately
5182 represent one plus the highest signed number of the same size, but
5183 not for binary modes. Consider, for instance conversion from SFmode
5184 into DImode.
5186 The hot path through the code is dealing with inputs smaller than 2^63
5187 and doing just the conversion, so there is no bits to lose.
5189 In the other path we know the value is positive in the range 2^63..2^64-1
5190 inclusive. (as for other input overflow happens and result is undefined)
5191 So we know that the most important bit set in mantissa corresponds to
5192 2^63. The subtraction of 2^63 should not generate any rounding as it
5193 simply clears out that bit. The rest is trivial. */
5195 if (unsignedp && GET_MODE_PRECISION (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5196 for (fmode = GET_MODE (from); fmode != VOIDmode;
5197 fmode = GET_MODE_WIDER_MODE (fmode))
5198 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5199 && (!DECIMAL_FLOAT_MODE_P (fmode)
5200 || GET_MODE_BITSIZE (fmode) > GET_MODE_PRECISION (GET_MODE (to))))
5202 int bitsize;
5203 REAL_VALUE_TYPE offset;
5204 rtx limit, lab1, lab2, insn;
5206 bitsize = GET_MODE_PRECISION (GET_MODE (to));
5207 real_2expN (&offset, bitsize - 1, fmode);
5208 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5209 lab1 = gen_label_rtx ();
5210 lab2 = gen_label_rtx ();
5212 if (fmode != GET_MODE (from))
5213 from = convert_to_mode (fmode, from, 0);
5215 /* See if we need to do the subtraction. */
5216 do_pending_stack_adjust ();
5217 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5218 0, lab1);
5220 /* If not, do the signed "fix" and branch around fixup code. */
5221 expand_fix (to, from, 0);
5222 emit_jump_insn (gen_jump (lab2));
5223 emit_barrier ();
5225 /* Otherwise, subtract 2**(N-1), convert to signed number,
5226 then add 2**(N-1). Do the addition using XOR since this
5227 will often generate better code. */
5228 emit_label (lab1);
5229 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5230 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5231 expand_fix (to, target, 0);
5232 target = expand_binop (GET_MODE (to), xor_optab, to,
5233 gen_int_mode
5234 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5235 GET_MODE (to)),
5236 to, 1, OPTAB_LIB_WIDEN);
5238 if (target != to)
5239 emit_move_insn (to, target);
5241 emit_label (lab2);
5243 if (optab_handler (mov_optab, GET_MODE (to)) != CODE_FOR_nothing)
5245 /* Make a place for a REG_NOTE and add it. */
5246 insn = emit_move_insn (to, to);
5247 set_dst_reg_note (insn, REG_EQUAL,
5248 gen_rtx_fmt_e (UNSIGNED_FIX, GET_MODE (to),
5249 copy_rtx (from)),
5250 to);
5253 return;
5256 /* We can't do it with an insn, so use a library call. But first ensure
5257 that the mode of TO is at least as wide as SImode, since those are the
5258 only library calls we know about. */
5260 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
5262 target = gen_reg_rtx (SImode);
5264 expand_fix (target, from, unsignedp);
5266 else
5268 rtx insns;
5269 rtx value;
5270 rtx libfunc;
5272 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5273 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5274 gcc_assert (libfunc);
5276 start_sequence ();
5278 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5279 GET_MODE (to), 1, from,
5280 GET_MODE (from));
5281 insns = get_insns ();
5282 end_sequence ();
5284 emit_libcall_block (insns, target, value,
5285 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5286 GET_MODE (to), from));
5289 if (target != to)
5291 if (GET_MODE (to) == GET_MODE (target))
5292 emit_move_insn (to, target);
5293 else
5294 convert_move (to, target, 0);
5298 /* Generate code to convert FROM or TO a fixed-point.
5299 If UINTP is true, either TO or FROM is an unsigned integer.
5300 If SATP is true, we need to saturate the result. */
5302 void
5303 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5305 enum machine_mode to_mode = GET_MODE (to);
5306 enum machine_mode from_mode = GET_MODE (from);
5307 convert_optab tab;
5308 enum rtx_code this_code;
5309 enum insn_code code;
5310 rtx insns, value;
5311 rtx libfunc;
5313 if (to_mode == from_mode)
5315 emit_move_insn (to, from);
5316 return;
5319 if (uintp)
5321 tab = satp ? satfractuns_optab : fractuns_optab;
5322 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5324 else
5326 tab = satp ? satfract_optab : fract_optab;
5327 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5329 code = convert_optab_handler (tab, to_mode, from_mode);
5330 if (code != CODE_FOR_nothing)
5332 emit_unop_insn (code, to, from, this_code);
5333 return;
5336 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5337 gcc_assert (libfunc);
5339 start_sequence ();
5340 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5341 1, from, from_mode);
5342 insns = get_insns ();
5343 end_sequence ();
5345 emit_libcall_block (insns, to, value,
5346 gen_rtx_fmt_e (tab->code, to_mode, from));
5349 /* Generate code to convert FROM to fixed point and store in TO. FROM
5350 must be floating point, TO must be signed. Use the conversion optab
5351 TAB to do the conversion. */
5353 bool
5354 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5356 enum insn_code icode;
5357 rtx target = to;
5358 enum machine_mode fmode, imode;
5360 /* We first try to find a pair of modes, one real and one integer, at
5361 least as wide as FROM and TO, respectively, in which we can open-code
5362 this conversion. If the integer mode is wider than the mode of TO,
5363 we can do the conversion either signed or unsigned. */
5365 for (fmode = GET_MODE (from); fmode != VOIDmode;
5366 fmode = GET_MODE_WIDER_MODE (fmode))
5367 for (imode = GET_MODE (to); imode != VOIDmode;
5368 imode = GET_MODE_WIDER_MODE (imode))
5370 icode = convert_optab_handler (tab, imode, fmode);
5371 if (icode != CODE_FOR_nothing)
5373 rtx last = get_last_insn ();
5374 if (fmode != GET_MODE (from))
5375 from = convert_to_mode (fmode, from, 0);
5377 if (imode != GET_MODE (to))
5378 target = gen_reg_rtx (imode);
5380 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5382 delete_insns_since (last);
5383 continue;
5385 if (target != to)
5386 convert_move (to, target, 0);
5387 return true;
5391 return false;
5394 /* Report whether we have an instruction to perform the operation
5395 specified by CODE on operands of mode MODE. */
5397 have_insn_for (enum rtx_code code, enum machine_mode mode)
5399 return (code_to_optab[(int) code] != 0
5400 && (optab_handler (code_to_optab[(int) code], mode)
5401 != CODE_FOR_nothing));
5404 /* Set all insn_code fields to CODE_FOR_nothing. */
5406 static void
5407 init_insn_codes (void)
5409 memset (optab_table, 0, sizeof (optab_table));
5410 memset (convert_optab_table, 0, sizeof (convert_optab_table));
5411 memset (direct_optab_table, 0, sizeof (direct_optab_table));
5414 /* Initialize OP's code to CODE, and write it into the code_to_optab table. */
5415 static inline void
5416 init_optab (optab op, enum rtx_code code)
5418 op->code = code;
5419 code_to_optab[(int) code] = op;
5422 /* Same, but fill in its code as CODE, and do _not_ write it into
5423 the code_to_optab table. */
5424 static inline void
5425 init_optabv (optab op, enum rtx_code code)
5427 op->code = code;
5430 /* Conversion optabs never go in the code_to_optab table. */
5431 static void
5432 init_convert_optab (convert_optab op, enum rtx_code code)
5434 op->code = code;
5437 /* Initialize the libfunc fields of an entire group of entries in some
5438 optab. Each entry is set equal to a string consisting of a leading
5439 pair of underscores followed by a generic operation name followed by
5440 a mode name (downshifted to lowercase) followed by a single character
5441 representing the number of operands for the given operation (which is
5442 usually one of the characters '2', '3', or '4').
5444 OPTABLE is the table in which libfunc fields are to be initialized.
5445 OPNAME is the generic (string) name of the operation.
5446 SUFFIX is the character which specifies the number of operands for
5447 the given generic operation.
5448 MODE is the mode to generate for.
5451 static void
5452 gen_libfunc (optab optable, const char *opname, int suffix, enum machine_mode mode)
5454 unsigned opname_len = strlen (opname);
5455 const char *mname = GET_MODE_NAME (mode);
5456 unsigned mname_len = strlen (mname);
5457 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5458 int len = prefix_len + opname_len + mname_len + 1 + 1;
5459 char *libfunc_name = XALLOCAVEC (char, len);
5460 char *p;
5461 const char *q;
5463 p = libfunc_name;
5464 *p++ = '_';
5465 *p++ = '_';
5466 if (targetm.libfunc_gnu_prefix)
5468 *p++ = 'g';
5469 *p++ = 'n';
5470 *p++ = 'u';
5471 *p++ = '_';
5473 for (q = opname; *q; )
5474 *p++ = *q++;
5475 for (q = mname; *q; q++)
5476 *p++ = TOLOWER (*q);
5477 *p++ = suffix;
5478 *p = '\0';
5480 set_optab_libfunc (optable, mode,
5481 ggc_alloc_string (libfunc_name, p - libfunc_name));
5484 /* Like gen_libfunc, but verify that integer operation is involved. */
5486 static void
5487 gen_int_libfunc (optab optable, const char *opname, char suffix,
5488 enum machine_mode mode)
5490 int maxsize = 2 * BITS_PER_WORD;
5492 if (GET_MODE_CLASS (mode) != MODE_INT)
5493 return;
5494 if (maxsize < LONG_LONG_TYPE_SIZE)
5495 maxsize = LONG_LONG_TYPE_SIZE;
5496 if (GET_MODE_CLASS (mode) != MODE_INT
5497 || mode < word_mode || GET_MODE_BITSIZE (mode) > maxsize)
5498 return;
5499 gen_libfunc (optable, opname, suffix, mode);
5502 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5504 static void
5505 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5506 enum machine_mode mode)
5508 char *dec_opname;
5510 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5511 gen_libfunc (optable, opname, suffix, mode);
5512 if (DECIMAL_FLOAT_MODE_P (mode))
5514 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5515 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5516 depending on the low level floating format used. */
5517 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5518 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5519 gen_libfunc (optable, dec_opname, suffix, mode);
5523 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5525 static void
5526 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5527 enum machine_mode mode)
5529 if (!ALL_FIXED_POINT_MODE_P (mode))
5530 return;
5531 gen_libfunc (optable, opname, suffix, mode);
5534 /* Like gen_libfunc, but verify that signed fixed-point operation is
5535 involved. */
5537 static void
5538 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5539 enum machine_mode mode)
5541 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5542 return;
5543 gen_libfunc (optable, opname, suffix, mode);
5546 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5547 involved. */
5549 static void
5550 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5551 enum machine_mode mode)
5553 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5554 return;
5555 gen_libfunc (optable, opname, suffix, mode);
5558 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5560 static void
5561 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5562 enum machine_mode mode)
5564 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5565 gen_fp_libfunc (optable, name, suffix, mode);
5566 if (INTEGRAL_MODE_P (mode))
5567 gen_int_libfunc (optable, name, suffix, mode);
5570 /* Like gen_libfunc, but verify that FP or INT operation is involved
5571 and add 'v' suffix for integer operation. */
5573 static void
5574 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5575 enum machine_mode mode)
5577 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5578 gen_fp_libfunc (optable, name, suffix, mode);
5579 if (GET_MODE_CLASS (mode) == MODE_INT)
5581 int len = strlen (name);
5582 char *v_name = XALLOCAVEC (char, len + 2);
5583 strcpy (v_name, name);
5584 v_name[len] = 'v';
5585 v_name[len + 1] = 0;
5586 gen_int_libfunc (optable, v_name, suffix, mode);
5590 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5591 involved. */
5593 static void
5594 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5595 enum machine_mode mode)
5597 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5598 gen_fp_libfunc (optable, name, suffix, mode);
5599 if (INTEGRAL_MODE_P (mode))
5600 gen_int_libfunc (optable, name, suffix, mode);
5601 if (ALL_FIXED_POINT_MODE_P (mode))
5602 gen_fixed_libfunc (optable, name, suffix, mode);
5605 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5606 involved. */
5608 static void
5609 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5610 enum machine_mode mode)
5612 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5613 gen_fp_libfunc (optable, name, suffix, mode);
5614 if (INTEGRAL_MODE_P (mode))
5615 gen_int_libfunc (optable, name, suffix, mode);
5616 if (SIGNED_FIXED_POINT_MODE_P (mode))
5617 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5620 /* Like gen_libfunc, but verify that INT or FIXED operation is
5621 involved. */
5623 static void
5624 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5625 enum machine_mode mode)
5627 if (INTEGRAL_MODE_P (mode))
5628 gen_int_libfunc (optable, name, suffix, mode);
5629 if (ALL_FIXED_POINT_MODE_P (mode))
5630 gen_fixed_libfunc (optable, name, suffix, mode);
5633 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5634 involved. */
5636 static void
5637 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5638 enum machine_mode mode)
5640 if (INTEGRAL_MODE_P (mode))
5641 gen_int_libfunc (optable, name, suffix, mode);
5642 if (SIGNED_FIXED_POINT_MODE_P (mode))
5643 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5646 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5647 involved. */
5649 static void
5650 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5651 enum machine_mode mode)
5653 if (INTEGRAL_MODE_P (mode))
5654 gen_int_libfunc (optable, name, suffix, mode);
5655 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5656 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5659 /* Initialize the libfunc fields of an entire group of entries of an
5660 inter-mode-class conversion optab. The string formation rules are
5661 similar to the ones for init_libfuncs, above, but instead of having
5662 a mode name and an operand count these functions have two mode names
5663 and no operand count. */
5665 static void
5666 gen_interclass_conv_libfunc (convert_optab tab,
5667 const char *opname,
5668 enum machine_mode tmode,
5669 enum machine_mode fmode)
5671 size_t opname_len = strlen (opname);
5672 size_t mname_len = 0;
5674 const char *fname, *tname;
5675 const char *q;
5676 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5677 char *libfunc_name, *suffix;
5678 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5679 char *p;
5681 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5682 depends on which underlying decimal floating point format is used. */
5683 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5685 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5687 nondec_name = XALLOCAVEC (char, prefix_len + opname_len + mname_len + 1 + 1);
5688 nondec_name[0] = '_';
5689 nondec_name[1] = '_';
5690 if (targetm.libfunc_gnu_prefix)
5692 nondec_name[2] = 'g';
5693 nondec_name[3] = 'n';
5694 nondec_name[4] = 'u';
5695 nondec_name[5] = '_';
5698 memcpy (&nondec_name[prefix_len], opname, opname_len);
5699 nondec_suffix = nondec_name + opname_len + prefix_len;
5701 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5702 dec_name[0] = '_';
5703 dec_name[1] = '_';
5704 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5705 memcpy (&dec_name[2+dec_len], opname, opname_len);
5706 dec_suffix = dec_name + dec_len + opname_len + 2;
5708 fname = GET_MODE_NAME (fmode);
5709 tname = GET_MODE_NAME (tmode);
5711 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5713 libfunc_name = dec_name;
5714 suffix = dec_suffix;
5716 else
5718 libfunc_name = nondec_name;
5719 suffix = nondec_suffix;
5722 p = suffix;
5723 for (q = fname; *q; p++, q++)
5724 *p = TOLOWER (*q);
5725 for (q = tname; *q; p++, q++)
5726 *p = TOLOWER (*q);
5728 *p = '\0';
5730 set_conv_libfunc (tab, tmode, fmode,
5731 ggc_alloc_string (libfunc_name, p - libfunc_name));
5734 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5735 int->fp conversion. */
5737 static void
5738 gen_int_to_fp_conv_libfunc (convert_optab tab,
5739 const char *opname,
5740 enum machine_mode tmode,
5741 enum machine_mode fmode)
5743 if (GET_MODE_CLASS (fmode) != MODE_INT)
5744 return;
5745 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5746 return;
5747 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5750 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5751 naming scheme. */
5753 static void
5754 gen_ufloat_conv_libfunc (convert_optab tab,
5755 const char *opname ATTRIBUTE_UNUSED,
5756 enum machine_mode tmode,
5757 enum machine_mode fmode)
5759 if (DECIMAL_FLOAT_MODE_P (tmode))
5760 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5761 else
5762 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5765 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5766 fp->int conversion. */
5768 static void
5769 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5770 const char *opname,
5771 enum machine_mode tmode,
5772 enum machine_mode fmode)
5774 if (GET_MODE_CLASS (fmode) != MODE_INT)
5775 return;
5776 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5777 return;
5778 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5781 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5782 fp->int conversion with no decimal floating point involved. */
5784 static void
5785 gen_fp_to_int_conv_libfunc (convert_optab tab,
5786 const char *opname,
5787 enum machine_mode tmode,
5788 enum machine_mode fmode)
5790 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5791 return;
5792 if (GET_MODE_CLASS (tmode) != MODE_INT)
5793 return;
5794 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5797 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5798 The string formation rules are
5799 similar to the ones for init_libfunc, above. */
5801 static void
5802 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5803 enum machine_mode tmode, enum machine_mode fmode)
5805 size_t opname_len = strlen (opname);
5806 size_t mname_len = 0;
5808 const char *fname, *tname;
5809 const char *q;
5810 int prefix_len = targetm.libfunc_gnu_prefix ? 6 : 2;
5811 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5812 char *libfunc_name, *suffix;
5813 char *p;
5815 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5816 depends on which underlying decimal floating point format is used. */
5817 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5819 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5821 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5822 nondec_name[0] = '_';
5823 nondec_name[1] = '_';
5824 if (targetm.libfunc_gnu_prefix)
5826 nondec_name[2] = 'g';
5827 nondec_name[3] = 'n';
5828 nondec_name[4] = 'u';
5829 nondec_name[5] = '_';
5831 memcpy (&nondec_name[prefix_len], opname, opname_len);
5832 nondec_suffix = nondec_name + opname_len + prefix_len;
5834 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5835 dec_name[0] = '_';
5836 dec_name[1] = '_';
5837 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5838 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5839 dec_suffix = dec_name + dec_len + opname_len + 2;
5841 fname = GET_MODE_NAME (fmode);
5842 tname = GET_MODE_NAME (tmode);
5844 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5846 libfunc_name = dec_name;
5847 suffix = dec_suffix;
5849 else
5851 libfunc_name = nondec_name;
5852 suffix = nondec_suffix;
5855 p = suffix;
5856 for (q = fname; *q; p++, q++)
5857 *p = TOLOWER (*q);
5858 for (q = tname; *q; p++, q++)
5859 *p = TOLOWER (*q);
5861 *p++ = '2';
5862 *p = '\0';
5864 set_conv_libfunc (tab, tmode, fmode,
5865 ggc_alloc_string (libfunc_name, p - libfunc_name));
5868 /* Pick proper libcall for trunc_optab. We need to chose if we do
5869 truncation or extension and interclass or intraclass. */
5871 static void
5872 gen_trunc_conv_libfunc (convert_optab tab,
5873 const char *opname,
5874 enum machine_mode tmode,
5875 enum machine_mode fmode)
5877 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5878 return;
5879 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5880 return;
5881 if (tmode == fmode)
5882 return;
5884 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5885 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5886 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5888 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5889 return;
5891 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5892 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5893 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5894 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5897 /* Pick proper libcall for extend_optab. We need to chose if we do
5898 truncation or extension and interclass or intraclass. */
5900 static void
5901 gen_extend_conv_libfunc (convert_optab tab,
5902 const char *opname ATTRIBUTE_UNUSED,
5903 enum machine_mode tmode,
5904 enum machine_mode fmode)
5906 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5907 return;
5908 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5909 return;
5910 if (tmode == fmode)
5911 return;
5913 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5914 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5915 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5917 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5918 return;
5920 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5921 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5922 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5923 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5926 /* Pick proper libcall for fract_optab. We need to chose if we do
5927 interclass or intraclass. */
5929 static void
5930 gen_fract_conv_libfunc (convert_optab tab,
5931 const char *opname,
5932 enum machine_mode tmode,
5933 enum machine_mode fmode)
5935 if (tmode == fmode)
5936 return;
5937 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5938 return;
5940 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5941 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5942 else
5943 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5946 /* Pick proper libcall for fractuns_optab. */
5948 static void
5949 gen_fractuns_conv_libfunc (convert_optab tab,
5950 const char *opname,
5951 enum machine_mode tmode,
5952 enum machine_mode fmode)
5954 if (tmode == fmode)
5955 return;
5956 /* One mode must be a fixed-point mode, and the other must be an integer
5957 mode. */
5958 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
5959 || (ALL_FIXED_POINT_MODE_P (fmode)
5960 && GET_MODE_CLASS (tmode) == MODE_INT)))
5961 return;
5963 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5966 /* Pick proper libcall for satfract_optab. We need to chose if we do
5967 interclass or intraclass. */
5969 static void
5970 gen_satfract_conv_libfunc (convert_optab tab,
5971 const char *opname,
5972 enum machine_mode tmode,
5973 enum machine_mode fmode)
5975 if (tmode == fmode)
5976 return;
5977 /* TMODE must be a fixed-point mode. */
5978 if (!ALL_FIXED_POINT_MODE_P (tmode))
5979 return;
5981 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5982 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5983 else
5984 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5987 /* Pick proper libcall for satfractuns_optab. */
5989 static void
5990 gen_satfractuns_conv_libfunc (convert_optab tab,
5991 const char *opname,
5992 enum machine_mode tmode,
5993 enum machine_mode fmode)
5995 if (tmode == fmode)
5996 return;
5997 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
5998 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
5999 return;
6001 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6004 /* A table of previously-created libfuncs, hashed by name. */
6005 static GTY ((param_is (union tree_node))) htab_t libfunc_decls;
6007 /* Hashtable callbacks for libfunc_decls. */
6009 static hashval_t
6010 libfunc_decl_hash (const void *entry)
6012 return IDENTIFIER_HASH_VALUE (DECL_NAME ((const_tree) entry));
6015 static int
6016 libfunc_decl_eq (const void *entry1, const void *entry2)
6018 return DECL_NAME ((const_tree) entry1) == (const_tree) entry2;
6021 /* Build a decl for a libfunc named NAME. */
6023 tree
6024 build_libfunc_function (const char *name)
6026 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
6027 get_identifier (name),
6028 build_function_type (integer_type_node, NULL_TREE));
6029 /* ??? We don't have any type information except for this is
6030 a function. Pretend this is "int foo()". */
6031 DECL_ARTIFICIAL (decl) = 1;
6032 DECL_EXTERNAL (decl) = 1;
6033 TREE_PUBLIC (decl) = 1;
6034 gcc_assert (DECL_ASSEMBLER_NAME (decl));
6036 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6037 are the flags assigned by targetm.encode_section_info. */
6038 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6040 return decl;
6044 init_one_libfunc (const char *name)
6046 tree id, decl;
6047 void **slot;
6048 hashval_t hash;
6050 if (libfunc_decls == NULL)
6051 libfunc_decls = htab_create_ggc (37, libfunc_decl_hash,
6052 libfunc_decl_eq, NULL);
6054 /* See if we have already created a libfunc decl for this function. */
6055 id = get_identifier (name);
6056 hash = IDENTIFIER_HASH_VALUE (id);
6057 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, INSERT);
6058 decl = (tree) *slot;
6059 if (decl == NULL)
6061 /* Create a new decl, so that it can be passed to
6062 targetm.encode_section_info. */
6063 decl = build_libfunc_function (name);
6064 *slot = decl;
6066 return XEXP (DECL_RTL (decl), 0);
6069 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6072 set_user_assembler_libfunc (const char *name, const char *asmspec)
6074 tree id, decl;
6075 void **slot;
6076 hashval_t hash;
6078 id = get_identifier (name);
6079 hash = IDENTIFIER_HASH_VALUE (id);
6080 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, NO_INSERT);
6081 gcc_assert (slot);
6082 decl = (tree) *slot;
6083 set_user_assembler_name (decl, asmspec);
6084 return XEXP (DECL_RTL (decl), 0);
6087 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6088 MODE to NAME, which should be either 0 or a string constant. */
6089 void
6090 set_optab_libfunc (optab optable, enum machine_mode mode, const char *name)
6092 rtx val;
6093 struct libfunc_entry e;
6094 struct libfunc_entry **slot;
6095 e.optab = (size_t) (optable - &optab_table[0]);
6096 e.mode1 = mode;
6097 e.mode2 = VOIDmode;
6099 if (name)
6100 val = init_one_libfunc (name);
6101 else
6102 val = 0;
6103 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6104 if (*slot == NULL)
6105 *slot = ggc_alloc_libfunc_entry ();
6106 (*slot)->optab = (size_t) (optable - &optab_table[0]);
6107 (*slot)->mode1 = mode;
6108 (*slot)->mode2 = VOIDmode;
6109 (*slot)->libfunc = val;
6112 /* Call this to reset the function entry for one conversion optab
6113 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6114 either 0 or a string constant. */
6115 void
6116 set_conv_libfunc (convert_optab optable, enum machine_mode tmode,
6117 enum machine_mode fmode, const char *name)
6119 rtx val;
6120 struct libfunc_entry e;
6121 struct libfunc_entry **slot;
6122 e.optab = (size_t) (optable - &convert_optab_table[0]);
6123 e.mode1 = tmode;
6124 e.mode2 = fmode;
6126 if (name)
6127 val = init_one_libfunc (name);
6128 else
6129 val = 0;
6130 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6131 if (*slot == NULL)
6132 *slot = ggc_alloc_libfunc_entry ();
6133 (*slot)->optab = (size_t) (optable - &convert_optab_table[0]);
6134 (*slot)->mode1 = tmode;
6135 (*slot)->mode2 = fmode;
6136 (*slot)->libfunc = val;
6139 /* Call this to initialize the contents of the optabs
6140 appropriately for the current target machine. */
6142 void
6143 init_optabs (void)
6145 if (libfunc_hash)
6147 htab_empty (libfunc_hash);
6148 /* We statically initialize the insn_codes with the equivalent of
6149 CODE_FOR_nothing. Repeat the process if reinitialising. */
6150 init_insn_codes ();
6152 else
6153 libfunc_hash = htab_create_ggc (10, hash_libfunc, eq_libfunc, NULL);
6155 init_optab (add_optab, PLUS);
6156 init_optabv (addv_optab, PLUS);
6157 init_optab (sub_optab, MINUS);
6158 init_optabv (subv_optab, MINUS);
6159 init_optab (ssadd_optab, SS_PLUS);
6160 init_optab (usadd_optab, US_PLUS);
6161 init_optab (sssub_optab, SS_MINUS);
6162 init_optab (ussub_optab, US_MINUS);
6163 init_optab (smul_optab, MULT);
6164 init_optab (ssmul_optab, SS_MULT);
6165 init_optab (usmul_optab, US_MULT);
6166 init_optabv (smulv_optab, MULT);
6167 init_optab (smul_highpart_optab, UNKNOWN);
6168 init_optab (umul_highpart_optab, UNKNOWN);
6169 init_optab (smul_widen_optab, UNKNOWN);
6170 init_optab (umul_widen_optab, UNKNOWN);
6171 init_optab (usmul_widen_optab, UNKNOWN);
6172 init_optab (smadd_widen_optab, UNKNOWN);
6173 init_optab (umadd_widen_optab, UNKNOWN);
6174 init_optab (ssmadd_widen_optab, UNKNOWN);
6175 init_optab (usmadd_widen_optab, UNKNOWN);
6176 init_optab (smsub_widen_optab, UNKNOWN);
6177 init_optab (umsub_widen_optab, UNKNOWN);
6178 init_optab (ssmsub_widen_optab, UNKNOWN);
6179 init_optab (usmsub_widen_optab, UNKNOWN);
6180 init_optab (sdiv_optab, DIV);
6181 init_optab (ssdiv_optab, SS_DIV);
6182 init_optab (usdiv_optab, US_DIV);
6183 init_optabv (sdivv_optab, DIV);
6184 init_optab (sdivmod_optab, UNKNOWN);
6185 init_optab (udiv_optab, UDIV);
6186 init_optab (udivmod_optab, UNKNOWN);
6187 init_optab (smod_optab, MOD);
6188 init_optab (umod_optab, UMOD);
6189 init_optab (fmod_optab, UNKNOWN);
6190 init_optab (remainder_optab, UNKNOWN);
6191 init_optab (ftrunc_optab, UNKNOWN);
6192 init_optab (and_optab, AND);
6193 init_optab (ior_optab, IOR);
6194 init_optab (xor_optab, XOR);
6195 init_optab (ashl_optab, ASHIFT);
6196 init_optab (ssashl_optab, SS_ASHIFT);
6197 init_optab (usashl_optab, US_ASHIFT);
6198 init_optab (ashr_optab, ASHIFTRT);
6199 init_optab (lshr_optab, LSHIFTRT);
6200 init_optabv (vashl_optab, ASHIFT);
6201 init_optabv (vashr_optab, ASHIFTRT);
6202 init_optabv (vlshr_optab, LSHIFTRT);
6203 init_optab (rotl_optab, ROTATE);
6204 init_optab (rotr_optab, ROTATERT);
6205 init_optab (smin_optab, SMIN);
6206 init_optab (smax_optab, SMAX);
6207 init_optab (umin_optab, UMIN);
6208 init_optab (umax_optab, UMAX);
6209 init_optab (pow_optab, UNKNOWN);
6210 init_optab (atan2_optab, UNKNOWN);
6211 init_optab (fma_optab, FMA);
6212 init_optab (fms_optab, UNKNOWN);
6213 init_optab (fnma_optab, UNKNOWN);
6214 init_optab (fnms_optab, UNKNOWN);
6216 /* These three have codes assigned exclusively for the sake of
6217 have_insn_for. */
6218 init_optab (mov_optab, SET);
6219 init_optab (movstrict_optab, STRICT_LOW_PART);
6220 init_optab (cbranch_optab, COMPARE);
6222 init_optab (cmov_optab, UNKNOWN);
6223 init_optab (cstore_optab, UNKNOWN);
6224 init_optab (ctrap_optab, UNKNOWN);
6226 init_optab (storent_optab, UNKNOWN);
6228 init_optab (cmp_optab, UNKNOWN);
6229 init_optab (ucmp_optab, UNKNOWN);
6231 init_optab (eq_optab, EQ);
6232 init_optab (ne_optab, NE);
6233 init_optab (gt_optab, GT);
6234 init_optab (ge_optab, GE);
6235 init_optab (lt_optab, LT);
6236 init_optab (le_optab, LE);
6237 init_optab (unord_optab, UNORDERED);
6239 init_optab (neg_optab, NEG);
6240 init_optab (ssneg_optab, SS_NEG);
6241 init_optab (usneg_optab, US_NEG);
6242 init_optabv (negv_optab, NEG);
6243 init_optab (abs_optab, ABS);
6244 init_optabv (absv_optab, ABS);
6245 init_optab (addcc_optab, UNKNOWN);
6246 init_optab (one_cmpl_optab, NOT);
6247 init_optab (bswap_optab, BSWAP);
6248 init_optab (ffs_optab, FFS);
6249 init_optab (clz_optab, CLZ);
6250 init_optab (ctz_optab, CTZ);
6251 init_optab (clrsb_optab, CLRSB);
6252 init_optab (popcount_optab, POPCOUNT);
6253 init_optab (parity_optab, PARITY);
6254 init_optab (sqrt_optab, SQRT);
6255 init_optab (floor_optab, UNKNOWN);
6256 init_optab (ceil_optab, UNKNOWN);
6257 init_optab (round_optab, UNKNOWN);
6258 init_optab (btrunc_optab, UNKNOWN);
6259 init_optab (nearbyint_optab, UNKNOWN);
6260 init_optab (rint_optab, UNKNOWN);
6261 init_optab (sincos_optab, UNKNOWN);
6262 init_optab (sin_optab, UNKNOWN);
6263 init_optab (asin_optab, UNKNOWN);
6264 init_optab (cos_optab, UNKNOWN);
6265 init_optab (acos_optab, UNKNOWN);
6266 init_optab (exp_optab, UNKNOWN);
6267 init_optab (exp10_optab, UNKNOWN);
6268 init_optab (exp2_optab, UNKNOWN);
6269 init_optab (expm1_optab, UNKNOWN);
6270 init_optab (ldexp_optab, UNKNOWN);
6271 init_optab (scalb_optab, UNKNOWN);
6272 init_optab (significand_optab, UNKNOWN);
6273 init_optab (logb_optab, UNKNOWN);
6274 init_optab (ilogb_optab, UNKNOWN);
6275 init_optab (log_optab, UNKNOWN);
6276 init_optab (log10_optab, UNKNOWN);
6277 init_optab (log2_optab, UNKNOWN);
6278 init_optab (log1p_optab, UNKNOWN);
6279 init_optab (tan_optab, UNKNOWN);
6280 init_optab (atan_optab, UNKNOWN);
6281 init_optab (copysign_optab, UNKNOWN);
6282 init_optab (signbit_optab, UNKNOWN);
6284 init_optab (isinf_optab, UNKNOWN);
6286 init_optab (strlen_optab, UNKNOWN);
6287 init_optab (push_optab, UNKNOWN);
6289 init_optab (reduc_smax_optab, UNKNOWN);
6290 init_optab (reduc_umax_optab, UNKNOWN);
6291 init_optab (reduc_smin_optab, UNKNOWN);
6292 init_optab (reduc_umin_optab, UNKNOWN);
6293 init_optab (reduc_splus_optab, UNKNOWN);
6294 init_optab (reduc_uplus_optab, UNKNOWN);
6296 init_optab (ssum_widen_optab, UNKNOWN);
6297 init_optab (usum_widen_optab, UNKNOWN);
6298 init_optab (sdot_prod_optab, UNKNOWN);
6299 init_optab (udot_prod_optab, UNKNOWN);
6301 init_optab (vec_extract_optab, UNKNOWN);
6302 init_optab (vec_set_optab, UNKNOWN);
6303 init_optab (vec_init_optab, UNKNOWN);
6304 init_optab (vec_shl_optab, UNKNOWN);
6305 init_optab (vec_shr_optab, UNKNOWN);
6306 init_optab (vec_realign_load_optab, UNKNOWN);
6307 init_optab (movmisalign_optab, UNKNOWN);
6308 init_optab (vec_widen_umult_hi_optab, UNKNOWN);
6309 init_optab (vec_widen_umult_lo_optab, UNKNOWN);
6310 init_optab (vec_widen_smult_hi_optab, UNKNOWN);
6311 init_optab (vec_widen_smult_lo_optab, UNKNOWN);
6312 init_optab (vec_widen_ushiftl_hi_optab, UNKNOWN);
6313 init_optab (vec_widen_ushiftl_lo_optab, UNKNOWN);
6314 init_optab (vec_widen_sshiftl_hi_optab, UNKNOWN);
6315 init_optab (vec_widen_sshiftl_lo_optab, UNKNOWN);
6316 init_optab (vec_unpacks_hi_optab, UNKNOWN);
6317 init_optab (vec_unpacks_lo_optab, UNKNOWN);
6318 init_optab (vec_unpacku_hi_optab, UNKNOWN);
6319 init_optab (vec_unpacku_lo_optab, UNKNOWN);
6320 init_optab (vec_unpacks_float_hi_optab, UNKNOWN);
6321 init_optab (vec_unpacks_float_lo_optab, UNKNOWN);
6322 init_optab (vec_unpacku_float_hi_optab, UNKNOWN);
6323 init_optab (vec_unpacku_float_lo_optab, UNKNOWN);
6324 init_optab (vec_pack_trunc_optab, UNKNOWN);
6325 init_optab (vec_pack_usat_optab, UNKNOWN);
6326 init_optab (vec_pack_ssat_optab, UNKNOWN);
6327 init_optab (vec_pack_ufix_trunc_optab, UNKNOWN);
6328 init_optab (vec_pack_sfix_trunc_optab, UNKNOWN);
6330 init_optab (powi_optab, UNKNOWN);
6332 /* Conversions. */
6333 init_convert_optab (sext_optab, SIGN_EXTEND);
6334 init_convert_optab (zext_optab, ZERO_EXTEND);
6335 init_convert_optab (trunc_optab, TRUNCATE);
6336 init_convert_optab (sfix_optab, FIX);
6337 init_convert_optab (ufix_optab, UNSIGNED_FIX);
6338 init_convert_optab (sfixtrunc_optab, UNKNOWN);
6339 init_convert_optab (ufixtrunc_optab, UNKNOWN);
6340 init_convert_optab (sfloat_optab, FLOAT);
6341 init_convert_optab (ufloat_optab, UNSIGNED_FLOAT);
6342 init_convert_optab (lrint_optab, UNKNOWN);
6343 init_convert_optab (lround_optab, UNKNOWN);
6344 init_convert_optab (lfloor_optab, UNKNOWN);
6345 init_convert_optab (lceil_optab, UNKNOWN);
6347 init_convert_optab (fract_optab, FRACT_CONVERT);
6348 init_convert_optab (fractuns_optab, UNSIGNED_FRACT_CONVERT);
6349 init_convert_optab (satfract_optab, SAT_FRACT);
6350 init_convert_optab (satfractuns_optab, UNSIGNED_SAT_FRACT);
6352 /* Fill in the optabs with the insns we support. */
6353 init_all_optabs ();
6355 /* Initialize the optabs with the names of the library functions. */
6356 add_optab->libcall_basename = "add";
6357 add_optab->libcall_suffix = '3';
6358 add_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6359 addv_optab->libcall_basename = "add";
6360 addv_optab->libcall_suffix = '3';
6361 addv_optab->libcall_gen = gen_intv_fp_libfunc;
6362 ssadd_optab->libcall_basename = "ssadd";
6363 ssadd_optab->libcall_suffix = '3';
6364 ssadd_optab->libcall_gen = gen_signed_fixed_libfunc;
6365 usadd_optab->libcall_basename = "usadd";
6366 usadd_optab->libcall_suffix = '3';
6367 usadd_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6368 sub_optab->libcall_basename = "sub";
6369 sub_optab->libcall_suffix = '3';
6370 sub_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6371 subv_optab->libcall_basename = "sub";
6372 subv_optab->libcall_suffix = '3';
6373 subv_optab->libcall_gen = gen_intv_fp_libfunc;
6374 sssub_optab->libcall_basename = "sssub";
6375 sssub_optab->libcall_suffix = '3';
6376 sssub_optab->libcall_gen = gen_signed_fixed_libfunc;
6377 ussub_optab->libcall_basename = "ussub";
6378 ussub_optab->libcall_suffix = '3';
6379 ussub_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6380 smul_optab->libcall_basename = "mul";
6381 smul_optab->libcall_suffix = '3';
6382 smul_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6383 smulv_optab->libcall_basename = "mul";
6384 smulv_optab->libcall_suffix = '3';
6385 smulv_optab->libcall_gen = gen_intv_fp_libfunc;
6386 ssmul_optab->libcall_basename = "ssmul";
6387 ssmul_optab->libcall_suffix = '3';
6388 ssmul_optab->libcall_gen = gen_signed_fixed_libfunc;
6389 usmul_optab->libcall_basename = "usmul";
6390 usmul_optab->libcall_suffix = '3';
6391 usmul_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6392 sdiv_optab->libcall_basename = "div";
6393 sdiv_optab->libcall_suffix = '3';
6394 sdiv_optab->libcall_gen = gen_int_fp_signed_fixed_libfunc;
6395 sdivv_optab->libcall_basename = "divv";
6396 sdivv_optab->libcall_suffix = '3';
6397 sdivv_optab->libcall_gen = gen_int_libfunc;
6398 ssdiv_optab->libcall_basename = "ssdiv";
6399 ssdiv_optab->libcall_suffix = '3';
6400 ssdiv_optab->libcall_gen = gen_signed_fixed_libfunc;
6401 udiv_optab->libcall_basename = "udiv";
6402 udiv_optab->libcall_suffix = '3';
6403 udiv_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6404 usdiv_optab->libcall_basename = "usdiv";
6405 usdiv_optab->libcall_suffix = '3';
6406 usdiv_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6407 sdivmod_optab->libcall_basename = "divmod";
6408 sdivmod_optab->libcall_suffix = '4';
6409 sdivmod_optab->libcall_gen = gen_int_libfunc;
6410 udivmod_optab->libcall_basename = "udivmod";
6411 udivmod_optab->libcall_suffix = '4';
6412 udivmod_optab->libcall_gen = gen_int_libfunc;
6413 smod_optab->libcall_basename = "mod";
6414 smod_optab->libcall_suffix = '3';
6415 smod_optab->libcall_gen = gen_int_libfunc;
6416 umod_optab->libcall_basename = "umod";
6417 umod_optab->libcall_suffix = '3';
6418 umod_optab->libcall_gen = gen_int_libfunc;
6419 ftrunc_optab->libcall_basename = "ftrunc";
6420 ftrunc_optab->libcall_suffix = '2';
6421 ftrunc_optab->libcall_gen = gen_fp_libfunc;
6422 and_optab->libcall_basename = "and";
6423 and_optab->libcall_suffix = '3';
6424 and_optab->libcall_gen = gen_int_libfunc;
6425 ior_optab->libcall_basename = "ior";
6426 ior_optab->libcall_suffix = '3';
6427 ior_optab->libcall_gen = gen_int_libfunc;
6428 xor_optab->libcall_basename = "xor";
6429 xor_optab->libcall_suffix = '3';
6430 xor_optab->libcall_gen = gen_int_libfunc;
6431 ashl_optab->libcall_basename = "ashl";
6432 ashl_optab->libcall_suffix = '3';
6433 ashl_optab->libcall_gen = gen_int_fixed_libfunc;
6434 ssashl_optab->libcall_basename = "ssashl";
6435 ssashl_optab->libcall_suffix = '3';
6436 ssashl_optab->libcall_gen = gen_signed_fixed_libfunc;
6437 usashl_optab->libcall_basename = "usashl";
6438 usashl_optab->libcall_suffix = '3';
6439 usashl_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6440 ashr_optab->libcall_basename = "ashr";
6441 ashr_optab->libcall_suffix = '3';
6442 ashr_optab->libcall_gen = gen_int_signed_fixed_libfunc;
6443 lshr_optab->libcall_basename = "lshr";
6444 lshr_optab->libcall_suffix = '3';
6445 lshr_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6446 smin_optab->libcall_basename = "min";
6447 smin_optab->libcall_suffix = '3';
6448 smin_optab->libcall_gen = gen_int_fp_libfunc;
6449 smax_optab->libcall_basename = "max";
6450 smax_optab->libcall_suffix = '3';
6451 smax_optab->libcall_gen = gen_int_fp_libfunc;
6452 umin_optab->libcall_basename = "umin";
6453 umin_optab->libcall_suffix = '3';
6454 umin_optab->libcall_gen = gen_int_libfunc;
6455 umax_optab->libcall_basename = "umax";
6456 umax_optab->libcall_suffix = '3';
6457 umax_optab->libcall_gen = gen_int_libfunc;
6458 neg_optab->libcall_basename = "neg";
6459 neg_optab->libcall_suffix = '2';
6460 neg_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6461 ssneg_optab->libcall_basename = "ssneg";
6462 ssneg_optab->libcall_suffix = '2';
6463 ssneg_optab->libcall_gen = gen_signed_fixed_libfunc;
6464 usneg_optab->libcall_basename = "usneg";
6465 usneg_optab->libcall_suffix = '2';
6466 usneg_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6467 negv_optab->libcall_basename = "neg";
6468 negv_optab->libcall_suffix = '2';
6469 negv_optab->libcall_gen = gen_intv_fp_libfunc;
6470 one_cmpl_optab->libcall_basename = "one_cmpl";
6471 one_cmpl_optab->libcall_suffix = '2';
6472 one_cmpl_optab->libcall_gen = gen_int_libfunc;
6473 ffs_optab->libcall_basename = "ffs";
6474 ffs_optab->libcall_suffix = '2';
6475 ffs_optab->libcall_gen = gen_int_libfunc;
6476 clz_optab->libcall_basename = "clz";
6477 clz_optab->libcall_suffix = '2';
6478 clz_optab->libcall_gen = gen_int_libfunc;
6479 ctz_optab->libcall_basename = "ctz";
6480 ctz_optab->libcall_suffix = '2';
6481 ctz_optab->libcall_gen = gen_int_libfunc;
6482 clrsb_optab->libcall_basename = "clrsb";
6483 clrsb_optab->libcall_suffix = '2';
6484 clrsb_optab->libcall_gen = gen_int_libfunc;
6485 popcount_optab->libcall_basename = "popcount";
6486 popcount_optab->libcall_suffix = '2';
6487 popcount_optab->libcall_gen = gen_int_libfunc;
6488 parity_optab->libcall_basename = "parity";
6489 parity_optab->libcall_suffix = '2';
6490 parity_optab->libcall_gen = gen_int_libfunc;
6492 /* Comparison libcalls for integers MUST come in pairs,
6493 signed/unsigned. */
6494 cmp_optab->libcall_basename = "cmp";
6495 cmp_optab->libcall_suffix = '2';
6496 cmp_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6497 ucmp_optab->libcall_basename = "ucmp";
6498 ucmp_optab->libcall_suffix = '2';
6499 ucmp_optab->libcall_gen = gen_int_libfunc;
6501 /* EQ etc are floating point only. */
6502 eq_optab->libcall_basename = "eq";
6503 eq_optab->libcall_suffix = '2';
6504 eq_optab->libcall_gen = gen_fp_libfunc;
6505 ne_optab->libcall_basename = "ne";
6506 ne_optab->libcall_suffix = '2';
6507 ne_optab->libcall_gen = gen_fp_libfunc;
6508 gt_optab->libcall_basename = "gt";
6509 gt_optab->libcall_suffix = '2';
6510 gt_optab->libcall_gen = gen_fp_libfunc;
6511 ge_optab->libcall_basename = "ge";
6512 ge_optab->libcall_suffix = '2';
6513 ge_optab->libcall_gen = gen_fp_libfunc;
6514 lt_optab->libcall_basename = "lt";
6515 lt_optab->libcall_suffix = '2';
6516 lt_optab->libcall_gen = gen_fp_libfunc;
6517 le_optab->libcall_basename = "le";
6518 le_optab->libcall_suffix = '2';
6519 le_optab->libcall_gen = gen_fp_libfunc;
6520 unord_optab->libcall_basename = "unord";
6521 unord_optab->libcall_suffix = '2';
6522 unord_optab->libcall_gen = gen_fp_libfunc;
6524 powi_optab->libcall_basename = "powi";
6525 powi_optab->libcall_suffix = '2';
6526 powi_optab->libcall_gen = gen_fp_libfunc;
6528 /* Conversions. */
6529 sfloat_optab->libcall_basename = "float";
6530 sfloat_optab->libcall_gen = gen_int_to_fp_conv_libfunc;
6531 ufloat_optab->libcall_gen = gen_ufloat_conv_libfunc;
6532 sfix_optab->libcall_basename = "fix";
6533 sfix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6534 ufix_optab->libcall_basename = "fixuns";
6535 ufix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6536 lrint_optab->libcall_basename = "lrint";
6537 lrint_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6538 lround_optab->libcall_basename = "lround";
6539 lround_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6540 lfloor_optab->libcall_basename = "lfloor";
6541 lfloor_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6542 lceil_optab->libcall_basename = "lceil";
6543 lceil_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6545 /* trunc_optab is also used for FLOAT_EXTEND. */
6546 sext_optab->libcall_basename = "extend";
6547 sext_optab->libcall_gen = gen_extend_conv_libfunc;
6548 trunc_optab->libcall_basename = "trunc";
6549 trunc_optab->libcall_gen = gen_trunc_conv_libfunc;
6551 /* Conversions for fixed-point modes and other modes. */
6552 fract_optab->libcall_basename = "fract";
6553 fract_optab->libcall_gen = gen_fract_conv_libfunc;
6554 satfract_optab->libcall_basename = "satfract";
6555 satfract_optab->libcall_gen = gen_satfract_conv_libfunc;
6556 fractuns_optab->libcall_basename = "fractuns";
6557 fractuns_optab->libcall_gen = gen_fractuns_conv_libfunc;
6558 satfractuns_optab->libcall_basename = "satfractuns";
6559 satfractuns_optab->libcall_gen = gen_satfractuns_conv_libfunc;
6561 /* The ffs function operates on `int'. Fall back on it if we do not
6562 have a libgcc2 function for that width. */
6563 if (INT_TYPE_SIZE < BITS_PER_WORD)
6564 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6565 "ffs");
6567 /* Explicitly initialize the bswap libfuncs since we need them to be
6568 valid for things other than word_mode. */
6569 if (targetm.libfunc_gnu_prefix)
6571 set_optab_libfunc (bswap_optab, SImode, "__gnu_bswapsi2");
6572 set_optab_libfunc (bswap_optab, DImode, "__gnu_bswapdi2");
6574 else
6576 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6577 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6580 /* Use cabs for double complex abs, since systems generally have cabs.
6581 Don't define any libcall for float complex, so that cabs will be used. */
6582 if (complex_double_type_node)
6583 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node), "cabs");
6585 abort_libfunc = init_one_libfunc ("abort");
6586 memcpy_libfunc = init_one_libfunc ("memcpy");
6587 memmove_libfunc = init_one_libfunc ("memmove");
6588 memcmp_libfunc = init_one_libfunc ("memcmp");
6589 memset_libfunc = init_one_libfunc ("memset");
6590 setbits_libfunc = init_one_libfunc ("__setbits");
6592 #ifndef DONT_USE_BUILTIN_SETJMP
6593 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6594 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6595 #else
6596 setjmp_libfunc = init_one_libfunc ("setjmp");
6597 longjmp_libfunc = init_one_libfunc ("longjmp");
6598 #endif
6599 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6600 unwind_sjlj_unregister_libfunc
6601 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6603 /* For function entry/exit instrumentation. */
6604 profile_function_entry_libfunc
6605 = init_one_libfunc ("__cyg_profile_func_enter");
6606 profile_function_exit_libfunc
6607 = init_one_libfunc ("__cyg_profile_func_exit");
6609 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6611 /* Allow the target to add more libcalls or rename some, etc. */
6612 targetm.init_libfuncs ();
6615 /* A helper function for init_sync_libfuncs. Using the basename BASE,
6616 install libfuncs into TAB for BASE_N for 1 <= N <= MAX. */
6618 static void
6619 init_sync_libfuncs_1 (optab tab, const char *base, int max)
6621 enum machine_mode mode;
6622 char buf[64];
6623 size_t len = strlen (base);
6624 int i;
6626 gcc_assert (max <= 8);
6627 gcc_assert (len + 3 < sizeof (buf));
6629 memcpy (buf, base, len);
6630 buf[len] = '_';
6631 buf[len + 1] = '0';
6632 buf[len + 2] = '\0';
6634 mode = QImode;
6635 for (i = 1; i <= max; i *= 2)
6637 buf[len + 1] = '0' + i;
6638 set_optab_libfunc (tab, mode, buf);
6639 mode = GET_MODE_2XWIDER_MODE (mode);
6643 void
6644 init_sync_libfuncs (int max)
6646 if (!flag_sync_libcalls)
6647 return;
6649 init_sync_libfuncs_1 (sync_compare_and_swap_optab,
6650 "__sync_val_compare_and_swap", max);
6651 init_sync_libfuncs_1 (sync_lock_test_and_set_optab,
6652 "__sync_lock_test_and_set", max);
6654 init_sync_libfuncs_1 (sync_old_add_optab, "__sync_fetch_and_add", max);
6655 init_sync_libfuncs_1 (sync_old_sub_optab, "__sync_fetch_and_sub", max);
6656 init_sync_libfuncs_1 (sync_old_ior_optab, "__sync_fetch_and_or", max);
6657 init_sync_libfuncs_1 (sync_old_and_optab, "__sync_fetch_and_and", max);
6658 init_sync_libfuncs_1 (sync_old_xor_optab, "__sync_fetch_and_xor", max);
6659 init_sync_libfuncs_1 (sync_old_nand_optab, "__sync_fetch_and_nand", max);
6661 init_sync_libfuncs_1 (sync_new_add_optab, "__sync_add_and_fetch", max);
6662 init_sync_libfuncs_1 (sync_new_sub_optab, "__sync_sub_and_fetch", max);
6663 init_sync_libfuncs_1 (sync_new_ior_optab, "__sync_or_and_fetch", max);
6664 init_sync_libfuncs_1 (sync_new_and_optab, "__sync_and_and_fetch", max);
6665 init_sync_libfuncs_1 (sync_new_xor_optab, "__sync_xor_and_fetch", max);
6666 init_sync_libfuncs_1 (sync_new_nand_optab, "__sync_nand_and_fetch", max);
6669 /* Print information about the current contents of the optabs on
6670 STDERR. */
6672 DEBUG_FUNCTION void
6673 debug_optab_libfuncs (void)
6675 int i;
6676 int j;
6677 int k;
6679 /* Dump the arithmetic optabs. */
6680 for (i = 0; i != (int) OTI_MAX; i++)
6681 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6683 optab o;
6684 rtx l;
6686 o = &optab_table[i];
6687 l = optab_libfunc (o, (enum machine_mode) j);
6688 if (l)
6690 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6691 fprintf (stderr, "%s\t%s:\t%s\n",
6692 GET_RTX_NAME (o->code),
6693 GET_MODE_NAME (j),
6694 XSTR (l, 0));
6698 /* Dump the conversion optabs. */
6699 for (i = 0; i < (int) COI_MAX; ++i)
6700 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6701 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6703 convert_optab o;
6704 rtx l;
6706 o = &convert_optab_table[i];
6707 l = convert_optab_libfunc (o, (enum machine_mode) j,
6708 (enum machine_mode) k);
6709 if (l)
6711 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6712 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6713 GET_RTX_NAME (o->code),
6714 GET_MODE_NAME (j),
6715 GET_MODE_NAME (k),
6716 XSTR (l, 0));
6722 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6723 CODE. Return 0 on failure. */
6726 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6728 enum machine_mode mode = GET_MODE (op1);
6729 enum insn_code icode;
6730 rtx insn;
6731 rtx trap_rtx;
6733 if (mode == VOIDmode)
6734 return 0;
6736 icode = optab_handler (ctrap_optab, mode);
6737 if (icode == CODE_FOR_nothing)
6738 return 0;
6740 /* Some targets only accept a zero trap code. */
6741 if (!insn_operand_matches (icode, 3, tcode))
6742 return 0;
6744 do_pending_stack_adjust ();
6745 start_sequence ();
6746 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6747 &trap_rtx, &mode);
6748 if (!trap_rtx)
6749 insn = NULL_RTX;
6750 else
6751 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6752 tcode);
6754 /* If that failed, then give up. */
6755 if (insn == 0)
6757 end_sequence ();
6758 return 0;
6761 emit_insn (insn);
6762 insn = get_insns ();
6763 end_sequence ();
6764 return insn;
6767 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6768 or unsigned operation code. */
6770 static enum rtx_code
6771 get_rtx_code (enum tree_code tcode, bool unsignedp)
6773 enum rtx_code code;
6774 switch (tcode)
6776 case EQ_EXPR:
6777 code = EQ;
6778 break;
6779 case NE_EXPR:
6780 code = NE;
6781 break;
6782 case LT_EXPR:
6783 code = unsignedp ? LTU : LT;
6784 break;
6785 case LE_EXPR:
6786 code = unsignedp ? LEU : LE;
6787 break;
6788 case GT_EXPR:
6789 code = unsignedp ? GTU : GT;
6790 break;
6791 case GE_EXPR:
6792 code = unsignedp ? GEU : GE;
6793 break;
6795 case UNORDERED_EXPR:
6796 code = UNORDERED;
6797 break;
6798 case ORDERED_EXPR:
6799 code = ORDERED;
6800 break;
6801 case UNLT_EXPR:
6802 code = UNLT;
6803 break;
6804 case UNLE_EXPR:
6805 code = UNLE;
6806 break;
6807 case UNGT_EXPR:
6808 code = UNGT;
6809 break;
6810 case UNGE_EXPR:
6811 code = UNGE;
6812 break;
6813 case UNEQ_EXPR:
6814 code = UNEQ;
6815 break;
6816 case LTGT_EXPR:
6817 code = LTGT;
6818 break;
6820 default:
6821 gcc_unreachable ();
6823 return code;
6826 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6827 unsigned operators. Do not generate compare instruction. */
6829 static rtx
6830 vector_compare_rtx (tree cond, bool unsignedp, enum insn_code icode)
6832 struct expand_operand ops[2];
6833 enum rtx_code rcode;
6834 tree t_op0, t_op1;
6835 rtx rtx_op0, rtx_op1;
6837 /* This is unlikely. While generating VEC_COND_EXPR, auto vectorizer
6838 ensures that condition is a relational operation. */
6839 gcc_assert (COMPARISON_CLASS_P (cond));
6841 rcode = get_rtx_code (TREE_CODE (cond), unsignedp);
6842 t_op0 = TREE_OPERAND (cond, 0);
6843 t_op1 = TREE_OPERAND (cond, 1);
6845 /* Expand operands. */
6846 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6847 EXPAND_STACK_PARM);
6848 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6849 EXPAND_STACK_PARM);
6851 create_input_operand (&ops[0], rtx_op0, GET_MODE (rtx_op0));
6852 create_input_operand (&ops[1], rtx_op1, GET_MODE (rtx_op1));
6853 if (!maybe_legitimize_operands (icode, 4, 2, ops))
6854 gcc_unreachable ();
6855 return gen_rtx_fmt_ee (rcode, VOIDmode, ops[0].value, ops[1].value);
6858 /* Return true if VEC_PERM_EXPR can be expanded using SIMD extensions
6859 of the CPU. SEL may be NULL, which stands for an unknown constant. */
6861 bool
6862 can_vec_perm_p (enum machine_mode mode, bool variable,
6863 const unsigned char *sel)
6865 enum machine_mode qimode;
6867 /* If the target doesn't implement a vector mode for the vector type,
6868 then no operations are supported. */
6869 if (!VECTOR_MODE_P (mode))
6870 return false;
6872 if (!variable)
6874 if (direct_optab_handler (vec_perm_const_optab, mode) != CODE_FOR_nothing
6875 && (sel == NULL
6876 || targetm.vectorize.vec_perm_const_ok == NULL
6877 || targetm.vectorize.vec_perm_const_ok (mode, sel)))
6878 return true;
6881 if (direct_optab_handler (vec_perm_optab, mode) != CODE_FOR_nothing)
6882 return true;
6884 /* We allow fallback to a QI vector mode, and adjust the mask. */
6885 if (GET_MODE_INNER (mode) == QImode)
6886 return false;
6887 qimode = mode_for_vector (QImode, GET_MODE_SIZE (mode));
6888 if (!VECTOR_MODE_P (qimode))
6889 return false;
6891 /* ??? For completeness, we ought to check the QImode version of
6892 vec_perm_const_optab. But all users of this implicit lowering
6893 feature implement the variable vec_perm_optab. */
6894 if (direct_optab_handler (vec_perm_optab, qimode) == CODE_FOR_nothing)
6895 return false;
6897 /* In order to support the lowering of variable permutations,
6898 we need to support shifts and adds. */
6899 if (variable)
6901 if (GET_MODE_UNIT_SIZE (mode) > 2
6902 && optab_handler (ashl_optab, mode) == CODE_FOR_nothing
6903 && optab_handler (vashl_optab, mode) == CODE_FOR_nothing)
6904 return false;
6905 if (optab_handler (add_optab, qimode) == CODE_FOR_nothing)
6906 return false;
6909 return true;
6912 /* A subroutine of expand_vec_perm for expanding one vec_perm insn. */
6914 static rtx
6915 expand_vec_perm_1 (enum insn_code icode, rtx target,
6916 rtx v0, rtx v1, rtx sel)
6918 enum machine_mode tmode = GET_MODE (target);
6919 enum machine_mode smode = GET_MODE (sel);
6920 struct expand_operand ops[4];
6922 create_output_operand (&ops[0], target, tmode);
6923 create_input_operand (&ops[3], sel, smode);
6925 /* Make an effort to preserve v0 == v1. The target expander is able to
6926 rely on this to determine if we're permuting a single input operand. */
6927 if (rtx_equal_p (v0, v1))
6929 if (!insn_operand_matches (icode, 1, v0))
6930 v0 = force_reg (tmode, v0);
6931 gcc_checking_assert (insn_operand_matches (icode, 1, v0));
6932 gcc_checking_assert (insn_operand_matches (icode, 2, v0));
6934 create_fixed_operand (&ops[1], v0);
6935 create_fixed_operand (&ops[2], v0);
6937 else
6939 create_input_operand (&ops[1], v0, tmode);
6940 create_input_operand (&ops[2], v1, tmode);
6943 if (maybe_expand_insn (icode, 4, ops))
6944 return ops[0].value;
6945 return NULL_RTX;
6948 /* Generate instructions for vec_perm optab given its mode
6949 and three operands. */
6952 expand_vec_perm (enum machine_mode mode, rtx v0, rtx v1, rtx sel, rtx target)
6954 enum insn_code icode;
6955 enum machine_mode qimode;
6956 unsigned int i, w, e, u;
6957 rtx tmp, sel_qi = NULL;
6958 rtvec vec;
6960 if (!target || GET_MODE (target) != mode)
6961 target = gen_reg_rtx (mode);
6963 w = GET_MODE_SIZE (mode);
6964 e = GET_MODE_NUNITS (mode);
6965 u = GET_MODE_UNIT_SIZE (mode);
6967 /* Set QIMODE to a different vector mode with byte elements.
6968 If no such mode, or if MODE already has byte elements, use VOIDmode. */
6969 qimode = VOIDmode;
6970 if (GET_MODE_INNER (mode) != QImode)
6972 qimode = mode_for_vector (QImode, w);
6973 if (!VECTOR_MODE_P (qimode))
6974 qimode = VOIDmode;
6977 /* If the input is a constant, expand it specially. */
6978 gcc_assert (GET_MODE_CLASS (GET_MODE (sel)) == MODE_VECTOR_INT);
6979 if (GET_CODE (sel) == CONST_VECTOR)
6981 icode = direct_optab_handler (vec_perm_const_optab, mode);
6982 if (icode != CODE_FOR_nothing)
6984 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
6985 if (tmp)
6986 return tmp;
6989 /* Fall back to a constant byte-based permutation. */
6990 if (qimode != VOIDmode)
6992 vec = rtvec_alloc (w);
6993 for (i = 0; i < e; ++i)
6995 unsigned int j, this_e;
6997 this_e = INTVAL (CONST_VECTOR_ELT (sel, i));
6998 this_e &= 2 * e - 1;
6999 this_e *= u;
7001 for (j = 0; j < u; ++j)
7002 RTVEC_ELT (vec, i * u + j) = GEN_INT (this_e + j);
7004 sel_qi = gen_rtx_CONST_VECTOR (qimode, vec);
7006 icode = direct_optab_handler (vec_perm_const_optab, qimode);
7007 if (icode != CODE_FOR_nothing)
7009 tmp = expand_vec_perm_1 (icode, gen_lowpart (qimode, target),
7010 gen_lowpart (qimode, v0),
7011 gen_lowpart (qimode, v1), sel_qi);
7012 if (tmp)
7013 return gen_lowpart (mode, tmp);
7018 /* Otherwise expand as a fully variable permuation. */
7019 icode = direct_optab_handler (vec_perm_optab, mode);
7020 if (icode != CODE_FOR_nothing)
7022 tmp = expand_vec_perm_1 (icode, target, v0, v1, sel);
7023 if (tmp)
7024 return tmp;
7027 /* As a special case to aid several targets, lower the element-based
7028 permutation to a byte-based permutation and try again. */
7029 if (qimode == VOIDmode)
7030 return NULL_RTX;
7031 icode = direct_optab_handler (vec_perm_optab, qimode);
7032 if (icode == CODE_FOR_nothing)
7033 return NULL_RTX;
7035 if (sel_qi == NULL)
7037 /* Multiply each element by its byte size. */
7038 enum machine_mode selmode = GET_MODE (sel);
7039 if (u == 2)
7040 sel = expand_simple_binop (selmode, PLUS, sel, sel,
7041 sel, 0, OPTAB_DIRECT);
7042 else
7043 sel = expand_simple_binop (selmode, ASHIFT, sel,
7044 GEN_INT (exact_log2 (u)),
7045 sel, 0, OPTAB_DIRECT);
7046 gcc_assert (sel != NULL);
7048 /* Broadcast the low byte each element into each of its bytes. */
7049 vec = rtvec_alloc (w);
7050 for (i = 0; i < w; ++i)
7052 int this_e = i / u * u;
7053 if (BYTES_BIG_ENDIAN)
7054 this_e += u - 1;
7055 RTVEC_ELT (vec, i) = GEN_INT (this_e);
7057 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
7058 sel = gen_lowpart (qimode, sel);
7059 sel = expand_vec_perm (qimode, sel, sel, tmp, NULL);
7060 gcc_assert (sel != NULL);
7062 /* Add the byte offset to each byte element. */
7063 /* Note that the definition of the indicies here is memory ordering,
7064 so there should be no difference between big and little endian. */
7065 vec = rtvec_alloc (w);
7066 for (i = 0; i < w; ++i)
7067 RTVEC_ELT (vec, i) = GEN_INT (i % u);
7068 tmp = gen_rtx_CONST_VECTOR (qimode, vec);
7069 sel_qi = expand_simple_binop (qimode, PLUS, sel, tmp,
7070 sel, 0, OPTAB_DIRECT);
7071 gcc_assert (sel_qi != NULL);
7074 tmp = expand_vec_perm_1 (icode, gen_lowpart (qimode, target),
7075 gen_lowpart (qimode, v0),
7076 gen_lowpart (qimode, v1), sel_qi);
7077 if (tmp)
7078 tmp = gen_lowpart (mode, tmp);
7079 return tmp;
7082 /* Return insn code for a conditional operator with a comparison in
7083 mode CMODE, unsigned if UNS is true, resulting in a value of mode VMODE. */
7085 static inline enum insn_code
7086 get_vcond_icode (enum machine_mode vmode, enum machine_mode cmode, bool uns)
7088 enum insn_code icode = CODE_FOR_nothing;
7089 if (uns)
7090 icode = convert_optab_handler (vcondu_optab, vmode, cmode);
7091 else
7092 icode = convert_optab_handler (vcond_optab, vmode, cmode);
7093 return icode;
7096 /* Return TRUE iff, appropriate vector insns are available
7097 for vector cond expr with vector type VALUE_TYPE and a comparison
7098 with operand vector types in CMP_OP_TYPE. */
7100 bool
7101 expand_vec_cond_expr_p (tree value_type, tree cmp_op_type)
7103 enum machine_mode value_mode = TYPE_MODE (value_type);
7104 enum machine_mode cmp_op_mode = TYPE_MODE (cmp_op_type);
7105 if (GET_MODE_SIZE (value_mode) != GET_MODE_SIZE (cmp_op_mode)
7106 || GET_MODE_NUNITS (value_mode) != GET_MODE_NUNITS (cmp_op_mode)
7107 || get_vcond_icode (TYPE_MODE (value_type), TYPE_MODE (cmp_op_type),
7108 TYPE_UNSIGNED (cmp_op_type)) == CODE_FOR_nothing)
7109 return false;
7110 return true;
7113 /* Generate insns for a VEC_COND_EXPR, given its TYPE and its
7114 three operands. */
7117 expand_vec_cond_expr (tree vec_cond_type, tree op0, tree op1, tree op2,
7118 rtx target)
7120 struct expand_operand ops[6];
7121 enum insn_code icode;
7122 rtx comparison, rtx_op1, rtx_op2;
7123 enum machine_mode mode = TYPE_MODE (vec_cond_type);
7124 enum machine_mode cmp_op_mode;
7125 bool unsignedp;
7127 gcc_assert (COMPARISON_CLASS_P (op0));
7129 unsignedp = TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (op0, 0)));
7130 cmp_op_mode = TYPE_MODE (TREE_TYPE (TREE_OPERAND (op0, 0)));
7132 gcc_assert (GET_MODE_SIZE (mode) == GET_MODE_SIZE (cmp_op_mode)
7133 && GET_MODE_NUNITS (mode) == GET_MODE_NUNITS (cmp_op_mode));
7135 icode = get_vcond_icode (mode, cmp_op_mode, unsignedp);
7136 if (icode == CODE_FOR_nothing)
7137 return 0;
7139 comparison = vector_compare_rtx (op0, unsignedp, icode);
7140 rtx_op1 = expand_normal (op1);
7141 rtx_op2 = expand_normal (op2);
7143 create_output_operand (&ops[0], target, mode);
7144 create_input_operand (&ops[1], rtx_op1, mode);
7145 create_input_operand (&ops[2], rtx_op2, mode);
7146 create_fixed_operand (&ops[3], comparison);
7147 create_fixed_operand (&ops[4], XEXP (comparison, 0));
7148 create_fixed_operand (&ops[5], XEXP (comparison, 1));
7149 expand_insn (icode, 6, ops);
7150 return ops[0].value;
7154 /* Return true if there is a compare_and_swap pattern. */
7156 bool
7157 can_compare_and_swap_p (enum machine_mode mode, bool allow_libcall)
7159 enum insn_code icode;
7161 /* Check for __atomic_compare_and_swap. */
7162 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7163 if (icode != CODE_FOR_nothing)
7164 return true;
7166 /* Check for __sync_compare_and_swap. */
7167 icode = optab_handler (sync_compare_and_swap_optab, mode);
7168 if (icode != CODE_FOR_nothing)
7169 return true;
7170 if (allow_libcall && optab_libfunc (sync_compare_and_swap_optab, mode))
7171 return true;
7173 /* No inline compare and swap. */
7174 return false;
7177 /* Return true if an atomic exchange can be performed. */
7179 bool
7180 can_atomic_exchange_p (enum machine_mode mode, bool allow_libcall)
7182 enum insn_code icode;
7184 /* Check for __atomic_exchange. */
7185 icode = direct_optab_handler (atomic_exchange_optab, mode);
7186 if (icode != CODE_FOR_nothing)
7187 return true;
7189 /* Don't check __sync_test_and_set, as on some platforms that
7190 has reduced functionality. Targets that really do support
7191 a proper exchange should simply be updated to the __atomics. */
7193 return can_compare_and_swap_p (mode, allow_libcall);
7197 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
7198 pattern. */
7200 static void
7201 find_cc_set (rtx x, const_rtx pat, void *data)
7203 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
7204 && GET_CODE (pat) == SET)
7206 rtx *p_cc_reg = (rtx *) data;
7207 gcc_assert (!*p_cc_reg);
7208 *p_cc_reg = x;
7212 /* This is a helper function for the other atomic operations. This function
7213 emits a loop that contains SEQ that iterates until a compare-and-swap
7214 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7215 a set of instructions that takes a value from OLD_REG as an input and
7216 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7217 set to the current contents of MEM. After SEQ, a compare-and-swap will
7218 attempt to update MEM with NEW_REG. The function returns true when the
7219 loop was generated successfully. */
7221 static bool
7222 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7224 enum machine_mode mode = GET_MODE (mem);
7225 rtx label, cmp_reg, success, oldval;
7227 /* The loop we want to generate looks like
7229 cmp_reg = mem;
7230 label:
7231 old_reg = cmp_reg;
7232 seq;
7233 (success, cmp_reg) = compare-and-swap(mem, old_reg, new_reg)
7234 if (success)
7235 goto label;
7237 Note that we only do the plain load from memory once. Subsequent
7238 iterations use the value loaded by the compare-and-swap pattern. */
7240 label = gen_label_rtx ();
7241 cmp_reg = gen_reg_rtx (mode);
7243 emit_move_insn (cmp_reg, mem);
7244 emit_label (label);
7245 emit_move_insn (old_reg, cmp_reg);
7246 if (seq)
7247 emit_insn (seq);
7249 success = NULL_RTX;
7250 oldval = cmp_reg;
7251 if (!expand_atomic_compare_and_swap (&success, &oldval, mem, old_reg,
7252 new_reg, false, MEMMODEL_SEQ_CST,
7253 MEMMODEL_RELAXED))
7254 return false;
7256 if (oldval != cmp_reg)
7257 emit_move_insn (cmp_reg, oldval);
7259 /* ??? Mark this jump predicted not taken? */
7260 emit_cmp_and_jump_insns (success, const0_rtx, EQ, const0_rtx,
7261 GET_MODE (success), 1, label);
7262 return true;
7266 /* This function tries to emit an atomic_exchange intruction. VAL is written
7267 to *MEM using memory model MODEL. The previous contents of *MEM are returned,
7268 using TARGET if possible. */
7270 static rtx
7271 maybe_emit_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7273 enum machine_mode mode = GET_MODE (mem);
7274 enum insn_code icode;
7276 /* If the target supports the exchange directly, great. */
7277 icode = direct_optab_handler (atomic_exchange_optab, mode);
7278 if (icode != CODE_FOR_nothing)
7280 struct expand_operand ops[4];
7282 create_output_operand (&ops[0], target, mode);
7283 create_fixed_operand (&ops[1], mem);
7284 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7285 create_convert_operand_to (&ops[2], val, mode, true);
7286 create_integer_operand (&ops[3], model);
7287 if (maybe_expand_insn (icode, 4, ops))
7288 return ops[0].value;
7291 return NULL_RTX;
7294 /* This function tries to implement an atomic exchange operation using
7295 __sync_lock_test_and_set. VAL is written to *MEM using memory model MODEL.
7296 The previous contents of *MEM are returned, using TARGET if possible.
7297 Since this instructionn is an acquire barrier only, stronger memory
7298 models may require additional barriers to be emitted. */
7300 static rtx
7301 maybe_emit_sync_lock_test_and_set (rtx target, rtx mem, rtx val,
7302 enum memmodel model)
7304 enum machine_mode mode = GET_MODE (mem);
7305 enum insn_code icode;
7306 rtx last_insn = get_last_insn ();
7308 icode = optab_handler (sync_lock_test_and_set_optab, mode);
7310 /* Legacy sync_lock_test_and_set is an acquire barrier. If the pattern
7311 exists, and the memory model is stronger than acquire, add a release
7312 barrier before the instruction. */
7314 if (model == MEMMODEL_SEQ_CST
7315 || model == MEMMODEL_RELEASE
7316 || model == MEMMODEL_ACQ_REL)
7317 expand_mem_thread_fence (model);
7319 if (icode != CODE_FOR_nothing)
7321 struct expand_operand ops[3];
7322 create_output_operand (&ops[0], target, mode);
7323 create_fixed_operand (&ops[1], mem);
7324 /* VAL may have been promoted to a wider mode. Shrink it if so. */
7325 create_convert_operand_to (&ops[2], val, mode, true);
7326 if (maybe_expand_insn (icode, 3, ops))
7327 return ops[0].value;
7330 /* If an external test-and-set libcall is provided, use that instead of
7331 any external compare-and-swap that we might get from the compare-and-
7332 swap-loop expansion later. */
7333 if (!can_compare_and_swap_p (mode, false))
7335 rtx libfunc = optab_libfunc (sync_lock_test_and_set_optab, mode);
7336 if (libfunc != NULL)
7338 rtx addr;
7340 addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7341 return emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7342 mode, 2, addr, ptr_mode,
7343 val, mode);
7347 /* If the test_and_set can't be emitted, eliminate any barrier that might
7348 have been emitted. */
7349 delete_insns_since (last_insn);
7350 return NULL_RTX;
7353 /* This function tries to implement an atomic exchange operation using a
7354 compare_and_swap loop. VAL is written to *MEM. The previous contents of
7355 *MEM are returned, using TARGET if possible. No memory model is required
7356 since a compare_and_swap loop is seq-cst. */
7358 static rtx
7359 maybe_emit_compare_and_swap_exchange_loop (rtx target, rtx mem, rtx val)
7361 enum machine_mode mode = GET_MODE (mem);
7363 if (can_compare_and_swap_p (mode, true))
7365 if (!target || !register_operand (target, mode))
7366 target = gen_reg_rtx (mode);
7367 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7368 val = convert_modes (mode, GET_MODE (val), val, 1);
7369 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7370 return target;
7373 return NULL_RTX;
7376 /* This function tries to implement an atomic test-and-set operation
7377 using the atomic_test_and_set instruction pattern. A boolean value
7378 is returned from the operation, using TARGET if possible. */
7380 #ifndef HAVE_atomic_test_and_set
7381 #define HAVE_atomic_test_and_set 0
7382 #define CODE_FOR_atomic_test_and_set CODE_FOR_nothing
7383 #endif
7385 static rtx
7386 maybe_emit_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7388 enum machine_mode pat_bool_mode;
7389 struct expand_operand ops[3];
7391 if (!HAVE_atomic_test_and_set)
7392 return NULL_RTX;
7394 /* While we always get QImode from __atomic_test_and_set, we get
7395 other memory modes from __sync_lock_test_and_set. Note that we
7396 use no endian adjustment here. This matches the 4.6 behavior
7397 in the Sparc backend. */
7398 gcc_checking_assert
7399 (insn_data[CODE_FOR_atomic_test_and_set].operand[1].mode == QImode);
7400 if (GET_MODE (mem) != QImode)
7401 mem = adjust_address_nv (mem, QImode, 0);
7403 pat_bool_mode = insn_data[CODE_FOR_atomic_test_and_set].operand[0].mode;
7404 create_output_operand (&ops[0], target, pat_bool_mode);
7405 create_fixed_operand (&ops[1], mem);
7406 create_integer_operand (&ops[2], model);
7408 if (maybe_expand_insn (CODE_FOR_atomic_test_and_set, 3, ops))
7409 return ops[0].value;
7410 return NULL_RTX;
7413 /* This function expands the legacy _sync_lock test_and_set operation which is
7414 generally an atomic exchange. Some limited targets only allow the
7415 constant 1 to be stored. This is an ACQUIRE operation.
7417 TARGET is an optional place to stick the return value.
7418 MEM is where VAL is stored. */
7421 expand_sync_lock_test_and_set (rtx target, rtx mem, rtx val)
7423 rtx ret;
7425 /* Try an atomic_exchange first. */
7426 ret = maybe_emit_atomic_exchange (target, mem, val, MEMMODEL_ACQUIRE);
7427 if (ret)
7428 return ret;
7430 ret = maybe_emit_sync_lock_test_and_set (target, mem, val, MEMMODEL_ACQUIRE);
7431 if (ret)
7432 return ret;
7434 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7435 if (ret)
7436 return ret;
7438 /* If there are no other options, try atomic_test_and_set if the value
7439 being stored is 1. */
7440 if (val == const1_rtx)
7441 ret = maybe_emit_atomic_test_and_set (target, mem, MEMMODEL_ACQUIRE);
7443 return ret;
7446 /* This function expands the atomic test_and_set operation:
7447 atomically store a boolean TRUE into MEM and return the previous value.
7449 MEMMODEL is the memory model variant to use.
7450 TARGET is an optional place to stick the return value. */
7453 expand_atomic_test_and_set (rtx target, rtx mem, enum memmodel model)
7455 enum machine_mode mode = GET_MODE (mem);
7456 rtx ret, trueval, subtarget;
7458 ret = maybe_emit_atomic_test_and_set (target, mem, model);
7459 if (ret)
7460 return ret;
7462 /* Be binary compatible with non-default settings of trueval, and different
7463 cpu revisions. E.g. one revision may have atomic-test-and-set, but
7464 another only has atomic-exchange. */
7465 if (targetm.atomic_test_and_set_trueval == 1)
7467 trueval = const1_rtx;
7468 subtarget = target ? target : gen_reg_rtx (mode);
7470 else
7472 trueval = gen_int_mode (targetm.atomic_test_and_set_trueval, mode);
7473 subtarget = gen_reg_rtx (mode);
7476 /* Try the atomic-exchange optab... */
7477 ret = maybe_emit_atomic_exchange (subtarget, mem, trueval, model);
7479 /* ... then an atomic-compare-and-swap loop ... */
7480 if (!ret)
7481 ret = maybe_emit_compare_and_swap_exchange_loop (subtarget, mem, trueval);
7483 /* ... before trying the vaguely defined legacy lock_test_and_set. */
7484 if (!ret)
7485 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, trueval, model);
7487 /* Recall that the legacy lock_test_and_set optab was allowed to do magic
7488 things with the value 1. Thus we try again without trueval. */
7489 if (!ret && targetm.atomic_test_and_set_trueval != 1)
7490 ret = maybe_emit_sync_lock_test_and_set (subtarget, mem, const1_rtx, model);
7492 /* Failing all else, assume a single threaded environment and simply
7493 perform the operation. */
7494 if (!ret)
7496 emit_move_insn (subtarget, mem);
7497 emit_move_insn (mem, trueval);
7498 ret = subtarget;
7501 /* Recall that have to return a boolean value; rectify if trueval
7502 is not exactly one. */
7503 if (targetm.atomic_test_and_set_trueval != 1)
7504 ret = emit_store_flag_force (target, NE, ret, const0_rtx, mode, 0, 1);
7506 return ret;
7509 /* This function expands the atomic exchange operation:
7510 atomically store VAL in MEM and return the previous value in MEM.
7512 MEMMODEL is the memory model variant to use.
7513 TARGET is an optional place to stick the return value. */
7516 expand_atomic_exchange (rtx target, rtx mem, rtx val, enum memmodel model)
7518 rtx ret;
7520 ret = maybe_emit_atomic_exchange (target, mem, val, model);
7522 /* Next try a compare-and-swap loop for the exchange. */
7523 if (!ret)
7524 ret = maybe_emit_compare_and_swap_exchange_loop (target, mem, val);
7526 return ret;
7529 /* This function expands the atomic compare exchange operation:
7531 *PTARGET_BOOL is an optional place to store the boolean success/failure.
7532 *PTARGET_OVAL is an optional place to store the old value from memory.
7533 Both target parameters may be NULL to indicate that we do not care about
7534 that return value. Both target parameters are updated on success to
7535 the actual location of the corresponding result.
7537 MEMMODEL is the memory model variant to use.
7539 The return value of the function is true for success. */
7541 bool
7542 expand_atomic_compare_and_swap (rtx *ptarget_bool, rtx *ptarget_oval,
7543 rtx mem, rtx expected, rtx desired,
7544 bool is_weak, enum memmodel succ_model,
7545 enum memmodel fail_model)
7547 enum machine_mode mode = GET_MODE (mem);
7548 struct expand_operand ops[8];
7549 enum insn_code icode;
7550 rtx target_oval, target_bool = NULL_RTX;
7551 rtx libfunc;
7553 /* Load expected into a register for the compare and swap. */
7554 if (MEM_P (expected))
7555 expected = copy_to_reg (expected);
7557 /* Make sure we always have some place to put the return oldval.
7558 Further, make sure that place is distinct from the input expected,
7559 just in case we need that path down below. */
7560 if (ptarget_oval == NULL
7561 || (target_oval = *ptarget_oval) == NULL
7562 || reg_overlap_mentioned_p (expected, target_oval))
7563 target_oval = gen_reg_rtx (mode);
7565 icode = direct_optab_handler (atomic_compare_and_swap_optab, mode);
7566 if (icode != CODE_FOR_nothing)
7568 enum machine_mode bool_mode = insn_data[icode].operand[0].mode;
7570 /* Make sure we always have a place for the bool operand. */
7571 if (ptarget_bool == NULL
7572 || (target_bool = *ptarget_bool) == NULL
7573 || GET_MODE (target_bool) != bool_mode)
7574 target_bool = gen_reg_rtx (bool_mode);
7576 /* Emit the compare_and_swap. */
7577 create_output_operand (&ops[0], target_bool, bool_mode);
7578 create_output_operand (&ops[1], target_oval, mode);
7579 create_fixed_operand (&ops[2], mem);
7580 create_convert_operand_to (&ops[3], expected, mode, true);
7581 create_convert_operand_to (&ops[4], desired, mode, true);
7582 create_integer_operand (&ops[5], is_weak);
7583 create_integer_operand (&ops[6], succ_model);
7584 create_integer_operand (&ops[7], fail_model);
7585 expand_insn (icode, 8, ops);
7587 /* Return success/failure. */
7588 target_bool = ops[0].value;
7589 target_oval = ops[1].value;
7590 goto success;
7593 /* Otherwise fall back to the original __sync_val_compare_and_swap
7594 which is always seq-cst. */
7595 icode = optab_handler (sync_compare_and_swap_optab, mode);
7596 if (icode != CODE_FOR_nothing)
7598 rtx cc_reg;
7600 create_output_operand (&ops[0], target_oval, mode);
7601 create_fixed_operand (&ops[1], mem);
7602 create_convert_operand_to (&ops[2], expected, mode, true);
7603 create_convert_operand_to (&ops[3], desired, mode, true);
7604 if (!maybe_expand_insn (icode, 4, ops))
7605 return false;
7607 target_oval = ops[0].value;
7609 /* If the caller isn't interested in the boolean return value,
7610 skip the computation of it. */
7611 if (ptarget_bool == NULL)
7612 goto success;
7614 /* Otherwise, work out if the compare-and-swap succeeded. */
7615 cc_reg = NULL_RTX;
7616 if (have_insn_for (COMPARE, CCmode))
7617 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7618 if (cc_reg)
7620 target_bool = emit_store_flag_force (target_bool, EQ, cc_reg,
7621 const0_rtx, VOIDmode, 0, 1);
7622 goto success;
7624 goto success_bool_from_val;
7627 /* Also check for library support for __sync_val_compare_and_swap. */
7628 libfunc = optab_libfunc (sync_compare_and_swap_optab, mode);
7629 if (libfunc != NULL)
7631 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
7632 target_oval = emit_library_call_value (libfunc, NULL_RTX, LCT_NORMAL,
7633 mode, 3, addr, ptr_mode,
7634 expected, mode, desired, mode);
7636 /* Compute the boolean return value only if requested. */
7637 if (ptarget_bool)
7638 goto success_bool_from_val;
7639 else
7640 goto success;
7643 /* Failure. */
7644 return false;
7646 success_bool_from_val:
7647 target_bool = emit_store_flag_force (target_bool, EQ, target_oval,
7648 expected, VOIDmode, 1, 1);
7649 success:
7650 /* Make sure that the oval output winds up where the caller asked. */
7651 if (ptarget_oval)
7652 *ptarget_oval = target_oval;
7653 if (ptarget_bool)
7654 *ptarget_bool = target_bool;
7655 return true;
7658 /* Generate asm volatile("" : : : "memory") as the memory barrier. */
7660 static void
7661 expand_asm_memory_barrier (void)
7663 rtx asm_op, clob;
7665 asm_op = gen_rtx_ASM_OPERANDS (VOIDmode, empty_string, empty_string, 0,
7666 rtvec_alloc (0), rtvec_alloc (0),
7667 rtvec_alloc (0), UNKNOWN_LOCATION);
7668 MEM_VOLATILE_P (asm_op) = 1;
7670 clob = gen_rtx_SCRATCH (VOIDmode);
7671 clob = gen_rtx_MEM (BLKmode, clob);
7672 clob = gen_rtx_CLOBBER (VOIDmode, clob);
7674 emit_insn (gen_rtx_PARALLEL (VOIDmode, gen_rtvec (2, asm_op, clob)));
7677 /* This routine will either emit the mem_thread_fence pattern or issue a
7678 sync_synchronize to generate a fence for memory model MEMMODEL. */
7680 #ifndef HAVE_mem_thread_fence
7681 # define HAVE_mem_thread_fence 0
7682 # define gen_mem_thread_fence(x) (gcc_unreachable (), NULL_RTX)
7683 #endif
7684 #ifndef HAVE_memory_barrier
7685 # define HAVE_memory_barrier 0
7686 # define gen_memory_barrier() (gcc_unreachable (), NULL_RTX)
7687 #endif
7689 void
7690 expand_mem_thread_fence (enum memmodel model)
7692 if (HAVE_mem_thread_fence)
7693 emit_insn (gen_mem_thread_fence (GEN_INT (model)));
7694 else if (model != MEMMODEL_RELAXED)
7696 if (HAVE_memory_barrier)
7697 emit_insn (gen_memory_barrier ());
7698 else if (synchronize_libfunc != NULL_RTX)
7699 emit_library_call (synchronize_libfunc, LCT_NORMAL, VOIDmode, 0);
7700 else
7701 expand_asm_memory_barrier ();
7705 /* This routine will either emit the mem_signal_fence pattern or issue a
7706 sync_synchronize to generate a fence for memory model MEMMODEL. */
7708 #ifndef HAVE_mem_signal_fence
7709 # define HAVE_mem_signal_fence 0
7710 # define gen_mem_signal_fence(x) (gcc_unreachable (), NULL_RTX)
7711 #endif
7713 void
7714 expand_mem_signal_fence (enum memmodel model)
7716 if (HAVE_mem_signal_fence)
7717 emit_insn (gen_mem_signal_fence (GEN_INT (model)));
7718 else if (model != MEMMODEL_RELAXED)
7720 /* By default targets are coherent between a thread and the signal
7721 handler running on the same thread. Thus this really becomes a
7722 compiler barrier, in that stores must not be sunk past
7723 (or raised above) a given point. */
7724 expand_asm_memory_barrier ();
7728 /* This function expands the atomic load operation:
7729 return the atomically loaded value in MEM.
7731 MEMMODEL is the memory model variant to use.
7732 TARGET is an option place to stick the return value. */
7735 expand_atomic_load (rtx target, rtx mem, enum memmodel model)
7737 enum machine_mode mode = GET_MODE (mem);
7738 enum insn_code icode;
7740 /* If the target supports the load directly, great. */
7741 icode = direct_optab_handler (atomic_load_optab, mode);
7742 if (icode != CODE_FOR_nothing)
7744 struct expand_operand ops[3];
7746 create_output_operand (&ops[0], target, mode);
7747 create_fixed_operand (&ops[1], mem);
7748 create_integer_operand (&ops[2], model);
7749 if (maybe_expand_insn (icode, 3, ops))
7750 return ops[0].value;
7753 /* If the size of the object is greater than word size on this target,
7754 then we assume that a load will not be atomic. */
7755 if (GET_MODE_PRECISION (mode) > BITS_PER_WORD)
7757 /* Issue val = compare_and_swap (mem, 0, 0).
7758 This may cause the occasional harmless store of 0 when the value is
7759 already 0, but it seems to be OK according to the standards guys. */
7760 if (expand_atomic_compare_and_swap (NULL, &target, mem, const0_rtx,
7761 const0_rtx, false, model, model))
7762 return target;
7763 else
7764 /* Otherwise there is no atomic load, leave the library call. */
7765 return NULL_RTX;
7768 /* Otherwise assume loads are atomic, and emit the proper barriers. */
7769 if (!target || target == const0_rtx)
7770 target = gen_reg_rtx (mode);
7772 /* Emit the appropriate barrier before the load. */
7773 expand_mem_thread_fence (model);
7775 emit_move_insn (target, mem);
7777 /* For SEQ_CST, also emit a barrier after the load. */
7778 if (model == MEMMODEL_SEQ_CST)
7779 expand_mem_thread_fence (model);
7781 return target;
7784 /* This function expands the atomic store operation:
7785 Atomically store VAL in MEM.
7786 MEMMODEL is the memory model variant to use.
7787 USE_RELEASE is true if __sync_lock_release can be used as a fall back.
7788 function returns const0_rtx if a pattern was emitted. */
7791 expand_atomic_store (rtx mem, rtx val, enum memmodel model, bool use_release)
7793 enum machine_mode mode = GET_MODE (mem);
7794 enum insn_code icode;
7795 struct expand_operand ops[3];
7797 /* If the target supports the store directly, great. */
7798 icode = direct_optab_handler (atomic_store_optab, mode);
7799 if (icode != CODE_FOR_nothing)
7801 create_fixed_operand (&ops[0], mem);
7802 create_input_operand (&ops[1], val, mode);
7803 create_integer_operand (&ops[2], model);
7804 if (maybe_expand_insn (icode, 3, ops))
7805 return const0_rtx;
7808 /* If using __sync_lock_release is a viable alternative, try it. */
7809 if (use_release)
7811 icode = direct_optab_handler (sync_lock_release_optab, mode);
7812 if (icode != CODE_FOR_nothing)
7814 create_fixed_operand (&ops[0], mem);
7815 create_input_operand (&ops[1], const0_rtx, mode);
7816 if (maybe_expand_insn (icode, 2, ops))
7818 /* lock_release is only a release barrier. */
7819 if (model == MEMMODEL_SEQ_CST)
7820 expand_mem_thread_fence (model);
7821 return const0_rtx;
7826 /* If the size of the object is greater than word size on this target,
7827 a default store will not be atomic, Try a mem_exchange and throw away
7828 the result. If that doesn't work, don't do anything. */
7829 if (GET_MODE_PRECISION(mode) > BITS_PER_WORD)
7831 rtx target = maybe_emit_atomic_exchange (NULL_RTX, mem, val, model);
7832 if (!target)
7833 target = maybe_emit_compare_and_swap_exchange_loop (NULL_RTX, mem, val);
7834 if (target)
7835 return const0_rtx;
7836 else
7837 return NULL_RTX;
7840 /* If there is no mem_store, default to a move with barriers */
7841 if (model == MEMMODEL_SEQ_CST || model == MEMMODEL_RELEASE)
7842 expand_mem_thread_fence (model);
7844 emit_move_insn (mem, val);
7846 /* For SEQ_CST, also emit a barrier after the load. */
7847 if (model == MEMMODEL_SEQ_CST)
7848 expand_mem_thread_fence (model);
7850 return const0_rtx;
7854 /* Structure containing the pointers and values required to process the
7855 various forms of the atomic_fetch_op and atomic_op_fetch builtins. */
7857 struct atomic_op_functions
7859 direct_optab mem_fetch_before;
7860 direct_optab mem_fetch_after;
7861 direct_optab mem_no_result;
7862 optab fetch_before;
7863 optab fetch_after;
7864 direct_optab no_result;
7865 enum rtx_code reverse_code;
7869 /* Fill in structure pointed to by OP with the various optab entries for an
7870 operation of type CODE. */
7872 static void
7873 get_atomic_op_for_code (struct atomic_op_functions *op, enum rtx_code code)
7875 gcc_assert (op!= NULL);
7877 /* If SWITCHABLE_TARGET is defined, then subtargets can be switched
7878 in the source code during compilation, and the optab entries are not
7879 computable until runtime. Fill in the values at runtime. */
7880 switch (code)
7882 case PLUS:
7883 op->mem_fetch_before = atomic_fetch_add_optab;
7884 op->mem_fetch_after = atomic_add_fetch_optab;
7885 op->mem_no_result = atomic_add_optab;
7886 op->fetch_before = sync_old_add_optab;
7887 op->fetch_after = sync_new_add_optab;
7888 op->no_result = sync_add_optab;
7889 op->reverse_code = MINUS;
7890 break;
7891 case MINUS:
7892 op->mem_fetch_before = atomic_fetch_sub_optab;
7893 op->mem_fetch_after = atomic_sub_fetch_optab;
7894 op->mem_no_result = atomic_sub_optab;
7895 op->fetch_before = sync_old_sub_optab;
7896 op->fetch_after = sync_new_sub_optab;
7897 op->no_result = sync_sub_optab;
7898 op->reverse_code = PLUS;
7899 break;
7900 case XOR:
7901 op->mem_fetch_before = atomic_fetch_xor_optab;
7902 op->mem_fetch_after = atomic_xor_fetch_optab;
7903 op->mem_no_result = atomic_xor_optab;
7904 op->fetch_before = sync_old_xor_optab;
7905 op->fetch_after = sync_new_xor_optab;
7906 op->no_result = sync_xor_optab;
7907 op->reverse_code = XOR;
7908 break;
7909 case AND:
7910 op->mem_fetch_before = atomic_fetch_and_optab;
7911 op->mem_fetch_after = atomic_and_fetch_optab;
7912 op->mem_no_result = atomic_and_optab;
7913 op->fetch_before = sync_old_and_optab;
7914 op->fetch_after = sync_new_and_optab;
7915 op->no_result = sync_and_optab;
7916 op->reverse_code = UNKNOWN;
7917 break;
7918 case IOR:
7919 op->mem_fetch_before = atomic_fetch_or_optab;
7920 op->mem_fetch_after = atomic_or_fetch_optab;
7921 op->mem_no_result = atomic_or_optab;
7922 op->fetch_before = sync_old_ior_optab;
7923 op->fetch_after = sync_new_ior_optab;
7924 op->no_result = sync_ior_optab;
7925 op->reverse_code = UNKNOWN;
7926 break;
7927 case NOT:
7928 op->mem_fetch_before = atomic_fetch_nand_optab;
7929 op->mem_fetch_after = atomic_nand_fetch_optab;
7930 op->mem_no_result = atomic_nand_optab;
7931 op->fetch_before = sync_old_nand_optab;
7932 op->fetch_after = sync_new_nand_optab;
7933 op->no_result = sync_nand_optab;
7934 op->reverse_code = UNKNOWN;
7935 break;
7936 default:
7937 gcc_unreachable ();
7941 /* See if there is a more optimal way to implement the operation "*MEM CODE VAL"
7942 using memory order MODEL. If AFTER is true the operation needs to return
7943 the value of *MEM after the operation, otherwise the previous value.
7944 TARGET is an optional place to place the result. The result is unused if
7945 it is const0_rtx.
7946 Return the result if there is a better sequence, otherwise NULL_RTX. */
7948 static rtx
7949 maybe_optimize_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
7950 enum memmodel model, bool after)
7952 /* If the value is prefetched, or not used, it may be possible to replace
7953 the sequence with a native exchange operation. */
7954 if (!after || target == const0_rtx)
7956 /* fetch_and (&x, 0, m) can be replaced with exchange (&x, 0, m). */
7957 if (code == AND && val == const0_rtx)
7959 if (target == const0_rtx)
7960 target = gen_reg_rtx (GET_MODE (mem));
7961 return maybe_emit_atomic_exchange (target, mem, val, model);
7964 /* fetch_or (&x, -1, m) can be replaced with exchange (&x, -1, m). */
7965 if (code == IOR && val == constm1_rtx)
7967 if (target == const0_rtx)
7968 target = gen_reg_rtx (GET_MODE (mem));
7969 return maybe_emit_atomic_exchange (target, mem, val, model);
7973 return NULL_RTX;
7976 /* Try to emit an instruction for a specific operation varaition.
7977 OPTAB contains the OP functions.
7978 TARGET is an optional place to return the result. const0_rtx means unused.
7979 MEM is the memory location to operate on.
7980 VAL is the value to use in the operation.
7981 USE_MEMMODEL is TRUE if the variation with a memory model should be tried.
7982 MODEL is the memory model, if used.
7983 AFTER is true if the returned result is the value after the operation. */
7985 static rtx
7986 maybe_emit_op (const struct atomic_op_functions *optab, rtx target, rtx mem,
7987 rtx val, bool use_memmodel, enum memmodel model, bool after)
7989 enum machine_mode mode = GET_MODE (mem);
7990 struct expand_operand ops[4];
7991 enum insn_code icode;
7992 int op_counter = 0;
7993 int num_ops;
7995 /* Check to see if there is a result returned. */
7996 if (target == const0_rtx)
7998 if (use_memmodel)
8000 icode = direct_optab_handler (optab->mem_no_result, mode);
8001 create_integer_operand (&ops[2], model);
8002 num_ops = 3;
8004 else
8006 icode = direct_optab_handler (optab->no_result, mode);
8007 num_ops = 2;
8010 /* Otherwise, we need to generate a result. */
8011 else
8013 if (use_memmodel)
8015 icode = direct_optab_handler (after ? optab->mem_fetch_after
8016 : optab->mem_fetch_before, mode);
8017 create_integer_operand (&ops[3], model);
8018 num_ops = 4;
8020 else
8022 icode = optab_handler (after ? optab->fetch_after
8023 : optab->fetch_before, mode);
8024 num_ops = 3;
8026 create_output_operand (&ops[op_counter++], target, mode);
8028 if (icode == CODE_FOR_nothing)
8029 return NULL_RTX;
8031 create_fixed_operand (&ops[op_counter++], mem);
8032 /* VAL may have been promoted to a wider mode. Shrink it if so. */
8033 create_convert_operand_to (&ops[op_counter++], val, mode, true);
8035 if (maybe_expand_insn (icode, num_ops, ops))
8036 return (target == const0_rtx ? const0_rtx : ops[0].value);
8038 return NULL_RTX;
8042 /* This function expands an atomic fetch_OP or OP_fetch operation:
8043 TARGET is an option place to stick the return value. const0_rtx indicates
8044 the result is unused.
8045 atomically fetch MEM, perform the operation with VAL and return it to MEM.
8046 CODE is the operation being performed (OP)
8047 MEMMODEL is the memory model variant to use.
8048 AFTER is true to return the result of the operation (OP_fetch).
8049 AFTER is false to return the value before the operation (fetch_OP). */
8051 expand_atomic_fetch_op (rtx target, rtx mem, rtx val, enum rtx_code code,
8052 enum memmodel model, bool after)
8054 enum machine_mode mode = GET_MODE (mem);
8055 struct atomic_op_functions optab;
8056 rtx result;
8057 bool unused_result = (target == const0_rtx);
8059 get_atomic_op_for_code (&optab, code);
8061 /* Check to see if there are any better instructions. */
8062 result = maybe_optimize_fetch_op (target, mem, val, code, model, after);
8063 if (result)
8064 return result;
8066 /* Check for the case where the result isn't used and try those patterns. */
8067 if (unused_result)
8069 /* Try the memory model variant first. */
8070 result = maybe_emit_op (&optab, target, mem, val, true, model, true);
8071 if (result)
8072 return result;
8074 /* Next try the old style withuot a memory model. */
8075 result = maybe_emit_op (&optab, target, mem, val, false, model, true);
8076 if (result)
8077 return result;
8079 /* There is no no-result pattern, so try patterns with a result. */
8080 target = NULL_RTX;
8083 /* Try the __atomic version. */
8084 result = maybe_emit_op (&optab, target, mem, val, true, model, after);
8085 if (result)
8086 return result;
8088 /* Try the older __sync version. */
8089 result = maybe_emit_op (&optab, target, mem, val, false, model, after);
8090 if (result)
8091 return result;
8093 /* If the fetch value can be calculated from the other variation of fetch,
8094 try that operation. */
8095 if (after || unused_result || optab.reverse_code != UNKNOWN)
8097 /* Try the __atomic version, then the older __sync version. */
8098 result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
8099 if (!result)
8100 result = maybe_emit_op (&optab, target, mem, val, false, model, !after);
8102 if (result)
8104 /* If the result isn't used, no need to do compensation code. */
8105 if (unused_result)
8106 return result;
8108 /* Issue compensation code. Fetch_after == fetch_before OP val.
8109 Fetch_before == after REVERSE_OP val. */
8110 if (!after)
8111 code = optab.reverse_code;
8112 if (code == NOT)
8114 result = expand_simple_binop (mode, AND, result, val, NULL_RTX,
8115 true, OPTAB_LIB_WIDEN);
8116 result = expand_simple_unop (mode, NOT, result, target, true);
8118 else
8119 result = expand_simple_binop (mode, code, result, val, target,
8120 true, OPTAB_LIB_WIDEN);
8121 return result;
8125 /* Try the __sync libcalls only if we can't do compare-and-swap inline. */
8126 if (!can_compare_and_swap_p (mode, false))
8128 rtx libfunc;
8129 bool fixup = false;
8131 libfunc = optab_libfunc (after ? optab.fetch_after
8132 : optab.fetch_before, mode);
8133 if (libfunc == NULL
8134 && (after || unused_result || optab.reverse_code != UNKNOWN))
8136 fixup = true;
8137 if (!after)
8138 code = optab.reverse_code;
8139 libfunc = optab_libfunc (after ? optab.fetch_before
8140 : optab.fetch_after, mode);
8142 if (libfunc != NULL)
8144 rtx addr = convert_memory_address (ptr_mode, XEXP (mem, 0));
8145 result = emit_library_call_value (libfunc, NULL, LCT_NORMAL, mode,
8146 2, addr, ptr_mode, val, mode);
8148 if (!unused_result && fixup)
8149 result = expand_simple_binop (mode, code, result, val, target,
8150 true, OPTAB_LIB_WIDEN);
8151 return result;
8155 /* If nothing else has succeeded, default to a compare and swap loop. */
8156 if (can_compare_and_swap_p (mode, true))
8158 rtx insn;
8159 rtx t0 = gen_reg_rtx (mode), t1;
8161 start_sequence ();
8163 /* If the result is used, get a register for it. */
8164 if (!unused_result)
8166 if (!target || !register_operand (target, mode))
8167 target = gen_reg_rtx (mode);
8168 /* If fetch_before, copy the value now. */
8169 if (!after)
8170 emit_move_insn (target, t0);
8172 else
8173 target = const0_rtx;
8175 t1 = t0;
8176 if (code == NOT)
8178 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
8179 true, OPTAB_LIB_WIDEN);
8180 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
8182 else
8183 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX, true,
8184 OPTAB_LIB_WIDEN);
8186 /* For after, copy the value now. */
8187 if (!unused_result && after)
8188 emit_move_insn (target, t1);
8189 insn = get_insns ();
8190 end_sequence ();
8192 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
8193 return target;
8196 return NULL_RTX;
8199 /* Return true if OPERAND is suitable for operand number OPNO of
8200 instruction ICODE. */
8202 bool
8203 insn_operand_matches (enum insn_code icode, unsigned int opno, rtx operand)
8205 return (!insn_data[(int) icode].operand[opno].predicate
8206 || (insn_data[(int) icode].operand[opno].predicate
8207 (operand, insn_data[(int) icode].operand[opno].mode)));
8210 /* TARGET is a target of a multiword operation that we are going to
8211 implement as a series of word-mode operations. Return true if
8212 TARGET is suitable for this purpose. */
8214 bool
8215 valid_multiword_target_p (rtx target)
8217 enum machine_mode mode;
8218 int i;
8220 mode = GET_MODE (target);
8221 for (i = 0; i < GET_MODE_SIZE (mode); i += UNITS_PER_WORD)
8222 if (!validate_subreg (word_mode, mode, target, i))
8223 return false;
8224 return true;
8227 /* Like maybe_legitimize_operand, but do not change the code of the
8228 current rtx value. */
8230 static bool
8231 maybe_legitimize_operand_same_code (enum insn_code icode, unsigned int opno,
8232 struct expand_operand *op)
8234 /* See if the operand matches in its current form. */
8235 if (insn_operand_matches (icode, opno, op->value))
8236 return true;
8238 /* If the operand is a memory whose address has no side effects,
8239 try forcing the address into a non-virtual pseudo register.
8240 The check for side effects is important because copy_to_mode_reg
8241 cannot handle things like auto-modified addresses. */
8242 if (insn_data[(int) icode].operand[opno].allows_mem && MEM_P (op->value))
8244 rtx addr, mem;
8246 mem = op->value;
8247 addr = XEXP (mem, 0);
8248 if (!(REG_P (addr) && REGNO (addr) > LAST_VIRTUAL_REGISTER)
8249 && !side_effects_p (addr))
8251 rtx last;
8252 enum machine_mode mode;
8254 last = get_last_insn ();
8255 mode = targetm.addr_space.address_mode (MEM_ADDR_SPACE (mem));
8256 mem = replace_equiv_address (mem, copy_to_mode_reg (mode, addr));
8257 if (insn_operand_matches (icode, opno, mem))
8259 op->value = mem;
8260 return true;
8262 delete_insns_since (last);
8266 return false;
8269 /* Try to make OP match operand OPNO of instruction ICODE. Return true
8270 on success, storing the new operand value back in OP. */
8272 static bool
8273 maybe_legitimize_operand (enum insn_code icode, unsigned int opno,
8274 struct expand_operand *op)
8276 enum machine_mode mode, imode;
8277 bool old_volatile_ok, result;
8279 mode = op->mode;
8280 switch (op->type)
8282 case EXPAND_FIXED:
8283 old_volatile_ok = volatile_ok;
8284 volatile_ok = true;
8285 result = maybe_legitimize_operand_same_code (icode, opno, op);
8286 volatile_ok = old_volatile_ok;
8287 return result;
8289 case EXPAND_OUTPUT:
8290 gcc_assert (mode != VOIDmode);
8291 if (op->value
8292 && op->value != const0_rtx
8293 && GET_MODE (op->value) == mode
8294 && maybe_legitimize_operand_same_code (icode, opno, op))
8295 return true;
8297 op->value = gen_reg_rtx (mode);
8298 break;
8300 case EXPAND_INPUT:
8301 input:
8302 gcc_assert (mode != VOIDmode);
8303 gcc_assert (GET_MODE (op->value) == VOIDmode
8304 || GET_MODE (op->value) == mode);
8305 if (maybe_legitimize_operand_same_code (icode, opno, op))
8306 return true;
8308 op->value = copy_to_mode_reg (mode, op->value);
8309 break;
8311 case EXPAND_CONVERT_TO:
8312 gcc_assert (mode != VOIDmode);
8313 op->value = convert_to_mode (mode, op->value, op->unsigned_p);
8314 goto input;
8316 case EXPAND_CONVERT_FROM:
8317 if (GET_MODE (op->value) != VOIDmode)
8318 mode = GET_MODE (op->value);
8319 else
8320 /* The caller must tell us what mode this value has. */
8321 gcc_assert (mode != VOIDmode);
8323 imode = insn_data[(int) icode].operand[opno].mode;
8324 if (imode != VOIDmode && imode != mode)
8326 op->value = convert_modes (imode, mode, op->value, op->unsigned_p);
8327 mode = imode;
8329 goto input;
8331 case EXPAND_ADDRESS:
8332 gcc_assert (mode != VOIDmode);
8333 op->value = convert_memory_address (mode, op->value);
8334 goto input;
8336 case EXPAND_INTEGER:
8337 mode = insn_data[(int) icode].operand[opno].mode;
8338 if (mode != VOIDmode && const_int_operand (op->value, mode))
8339 goto input;
8340 break;
8342 return insn_operand_matches (icode, opno, op->value);
8345 /* Make OP describe an input operand that should have the same value
8346 as VALUE, after any mode conversion that the target might request.
8347 TYPE is the type of VALUE. */
8349 void
8350 create_convert_operand_from_type (struct expand_operand *op,
8351 rtx value, tree type)
8353 create_convert_operand_from (op, value, TYPE_MODE (type),
8354 TYPE_UNSIGNED (type));
8357 /* Try to make operands [OPS, OPS + NOPS) match operands [OPNO, OPNO + NOPS)
8358 of instruction ICODE. Return true on success, leaving the new operand
8359 values in the OPS themselves. Emit no code on failure. */
8361 bool
8362 maybe_legitimize_operands (enum insn_code icode, unsigned int opno,
8363 unsigned int nops, struct expand_operand *ops)
8365 rtx last;
8366 unsigned int i;
8368 last = get_last_insn ();
8369 for (i = 0; i < nops; i++)
8370 if (!maybe_legitimize_operand (icode, opno + i, &ops[i]))
8372 delete_insns_since (last);
8373 return false;
8375 return true;
8378 /* Try to generate instruction ICODE, using operands [OPS, OPS + NOPS)
8379 as its operands. Return the instruction pattern on success,
8380 and emit any necessary set-up code. Return null and emit no
8381 code on failure. */
8384 maybe_gen_insn (enum insn_code icode, unsigned int nops,
8385 struct expand_operand *ops)
8387 gcc_assert (nops == (unsigned int) insn_data[(int) icode].n_generator_args);
8388 if (!maybe_legitimize_operands (icode, 0, nops, ops))
8389 return NULL_RTX;
8391 switch (nops)
8393 case 1:
8394 return GEN_FCN (icode) (ops[0].value);
8395 case 2:
8396 return GEN_FCN (icode) (ops[0].value, ops[1].value);
8397 case 3:
8398 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value);
8399 case 4:
8400 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8401 ops[3].value);
8402 case 5:
8403 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8404 ops[3].value, ops[4].value);
8405 case 6:
8406 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8407 ops[3].value, ops[4].value, ops[5].value);
8408 case 7:
8409 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8410 ops[3].value, ops[4].value, ops[5].value,
8411 ops[6].value);
8412 case 8:
8413 return GEN_FCN (icode) (ops[0].value, ops[1].value, ops[2].value,
8414 ops[3].value, ops[4].value, ops[5].value,
8415 ops[6].value, ops[7].value);
8417 gcc_unreachable ();
8420 /* Try to emit instruction ICODE, using operands [OPS, OPS + NOPS)
8421 as its operands. Return true on success and emit no code on failure. */
8423 bool
8424 maybe_expand_insn (enum insn_code icode, unsigned int nops,
8425 struct expand_operand *ops)
8427 rtx pat = maybe_gen_insn (icode, nops, ops);
8428 if (pat)
8430 emit_insn (pat);
8431 return true;
8433 return false;
8436 /* Like maybe_expand_insn, but for jumps. */
8438 bool
8439 maybe_expand_jump_insn (enum insn_code icode, unsigned int nops,
8440 struct expand_operand *ops)
8442 rtx pat = maybe_gen_insn (icode, nops, ops);
8443 if (pat)
8445 emit_jump_insn (pat);
8446 return true;
8448 return false;
8451 /* Emit instruction ICODE, using operands [OPS, OPS + NOPS)
8452 as its operands. */
8454 void
8455 expand_insn (enum insn_code icode, unsigned int nops,
8456 struct expand_operand *ops)
8458 if (!maybe_expand_insn (icode, nops, ops))
8459 gcc_unreachable ();
8462 /* Like expand_insn, but for jumps. */
8464 void
8465 expand_jump_insn (enum insn_code icode, unsigned int nops,
8466 struct expand_operand *ops)
8468 if (!maybe_expand_jump_insn (icode, nops, ops))
8469 gcc_unreachable ();
8472 #include "gt-optabs.h"