gcc/cp/
[official-gcc.git] / gcc / optabs.c
blobee5bec11a41633d0569e478ecc3ec6ae2cf5debf
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
4 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 "toplev.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 "real.h"
45 #include "basic-block.h"
46 #include "target.h"
48 /* Each optab contains info on how this target machine
49 can perform a particular operation
50 for all sizes and kinds of operands.
52 The operation to be performed is often specified
53 by passing one of these optabs as an argument.
55 See expr.h for documentation of these optabs. */
57 #if GCC_VERSION >= 4000
58 __extension__ struct optab optab_table[OTI_MAX]
59 = { [0 ... OTI_MAX - 1].handlers[0 ... NUM_MACHINE_MODES - 1].insn_code
60 = CODE_FOR_nothing };
61 #else
62 /* init_insn_codes will do runtime initialization otherwise. */
63 struct optab optab_table[OTI_MAX];
64 #endif
66 rtx libfunc_table[LTI_MAX];
68 /* Tables of patterns for converting one mode to another. */
69 #if GCC_VERSION >= 4000
70 __extension__ struct convert_optab convert_optab_table[COI_MAX]
71 = { [0 ... COI_MAX - 1].handlers[0 ... NUM_MACHINE_MODES - 1]
72 [0 ... NUM_MACHINE_MODES - 1].insn_code
73 = CODE_FOR_nothing };
74 #else
75 /* init_convert_optab will do runtime initialization otherwise. */
76 struct convert_optab convert_optab_table[COI_MAX];
77 #endif
79 /* Contains the optab used for each rtx code. */
80 optab code_to_optab[NUM_RTX_CODE + 1];
82 /* Indexed by the rtx-code for a conditional (eg. EQ, LT,...)
83 gives the gen_function to make a branch to test that condition. */
85 rtxfun bcc_gen_fctn[NUM_RTX_CODE];
87 /* Indexed by the rtx-code for a conditional (eg. EQ, LT,...)
88 gives the insn code to make a store-condition insn
89 to test that condition. */
91 enum insn_code setcc_gen_code[NUM_RTX_CODE];
93 #ifdef HAVE_conditional_move
94 /* Indexed by the machine mode, gives the insn code to make a conditional
95 move insn. This is not indexed by the rtx-code like bcc_gen_fctn and
96 setcc_gen_code to cut down on the number of named patterns. Consider a day
97 when a lot more rtx codes are conditional (eg: for the ARM). */
99 enum insn_code movcc_gen_code[NUM_MACHINE_MODES];
100 #endif
102 /* Indexed by the machine mode, gives the insn code for vector conditional
103 operation. */
105 enum insn_code vcond_gen_code[NUM_MACHINE_MODES];
106 enum insn_code vcondu_gen_code[NUM_MACHINE_MODES];
108 /* The insn generating function can not take an rtx_code argument.
109 TRAP_RTX is used as an rtx argument. Its code is replaced with
110 the code to be used in the trap insn and all other fields are ignored. */
111 static GTY(()) rtx trap_rtx;
113 static void prepare_float_lib_cmp (rtx *, rtx *, enum rtx_code *,
114 enum machine_mode *, int *);
115 static rtx expand_unop_direct (enum machine_mode, optab, rtx, rtx, int);
117 /* Debug facility for use in GDB. */
118 void debug_optab_libfuncs (void);
120 #ifndef HAVE_conditional_trap
121 #define HAVE_conditional_trap 0
122 #define gen_conditional_trap(a,b) (gcc_unreachable (), NULL_RTX)
123 #endif
125 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
126 #if ENABLE_DECIMAL_BID_FORMAT
127 #define DECIMAL_PREFIX "bid_"
128 #else
129 #define DECIMAL_PREFIX "dpd_"
130 #endif
133 /* Info about libfunc. We use same hashtable for normal optabs and conversion
134 optab. In the first case mode2 is unused. */
135 struct libfunc_entry GTY(())
137 size_t optab;
138 enum machine_mode mode1, mode2;
139 rtx libfunc;
142 /* Hash table used to convert declarations into nodes. */
143 static GTY((param_is (struct libfunc_entry))) htab_t libfunc_hash;
145 /* Used for attribute_hash. */
147 static hashval_t
148 hash_libfunc (const void *p)
150 const struct libfunc_entry *const e = (const struct libfunc_entry *) p;
152 return (((int) e->mode1 + (int) e->mode2 * NUM_MACHINE_MODES)
153 ^ e->optab);
156 /* Used for optab_hash. */
158 static int
159 eq_libfunc (const void *p, const void *q)
161 const struct libfunc_entry *const e1 = (const struct libfunc_entry *) p;
162 const struct libfunc_entry *const e2 = (const struct libfunc_entry *) q;
164 return (e1->optab == e2->optab
165 && e1->mode1 == e2->mode1
166 && e1->mode2 == e2->mode2);
169 /* Return libfunc corresponding operation defined by OPTAB converting
170 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
171 if no libfunc is available. */
173 convert_optab_libfunc (convert_optab optab, enum machine_mode mode1,
174 enum machine_mode mode2)
176 struct libfunc_entry e;
177 struct libfunc_entry **slot;
179 e.optab = (size_t) (optab - &convert_optab_table[0]);
180 e.mode1 = mode1;
181 e.mode2 = mode2;
182 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
183 if (!slot)
185 if (optab->libcall_gen)
187 optab->libcall_gen (optab, optab->libcall_basename, mode1, mode2);
188 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
189 if (slot)
190 return (*slot)->libfunc;
191 else
192 return NULL;
194 return NULL;
196 return (*slot)->libfunc;
199 /* Return libfunc corresponding operation defined by OPTAB in MODE.
200 Trigger lazy initialization if needed, return NULL if no libfunc is
201 available. */
203 optab_libfunc (optab optab, enum machine_mode mode)
205 struct libfunc_entry e;
206 struct libfunc_entry **slot;
208 e.optab = (size_t) (optab - &optab_table[0]);
209 e.mode1 = mode;
210 e.mode2 = VOIDmode;
211 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
212 if (!slot)
214 if (optab->libcall_gen)
216 optab->libcall_gen (optab, optab->libcall_basename,
217 optab->libcall_suffix, mode);
218 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash,
219 &e, NO_INSERT);
220 if (slot)
221 return (*slot)->libfunc;
222 else
223 return NULL;
225 return NULL;
227 return (*slot)->libfunc;
231 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
232 the result of operation CODE applied to OP0 (and OP1 if it is a binary
233 operation).
235 If the last insn does not set TARGET, don't do anything, but return 1.
237 If a previous insn sets TARGET and TARGET is one of OP0 or OP1,
238 don't add the REG_EQUAL note but return 0. Our caller can then try
239 again, ensuring that TARGET is not one of the operands. */
241 static int
242 add_equal_note (rtx insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
244 rtx last_insn, insn, set;
245 rtx note;
247 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
249 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
250 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
251 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
252 && GET_RTX_CLASS (code) != RTX_COMPARE
253 && GET_RTX_CLASS (code) != RTX_UNARY)
254 return 1;
256 if (GET_CODE (target) == ZERO_EXTRACT)
257 return 1;
259 for (last_insn = insns;
260 NEXT_INSN (last_insn) != NULL_RTX;
261 last_insn = NEXT_INSN (last_insn))
264 set = single_set (last_insn);
265 if (set == NULL_RTX)
266 return 1;
268 if (! rtx_equal_p (SET_DEST (set), target)
269 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
270 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
271 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
272 return 1;
274 /* If TARGET is in OP0 or OP1, check if anything in SEQ sets TARGET
275 besides the last insn. */
276 if (reg_overlap_mentioned_p (target, op0)
277 || (op1 && reg_overlap_mentioned_p (target, op1)))
279 insn = PREV_INSN (last_insn);
280 while (insn != NULL_RTX)
282 if (reg_set_p (target, insn))
283 return 0;
285 insn = PREV_INSN (insn);
289 if (GET_RTX_CLASS (code) == RTX_UNARY)
290 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
291 else
292 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
294 set_unique_reg_note (last_insn, REG_EQUAL, note);
296 return 1;
299 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
300 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
301 not actually do a sign-extend or zero-extend, but can leave the
302 higher-order bits of the result rtx undefined, for example, in the case
303 of logical operations, but not right shifts. */
305 static rtx
306 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
307 int unsignedp, int no_extend)
309 rtx result;
311 /* If we don't have to extend and this is a constant, return it. */
312 if (no_extend && GET_MODE (op) == VOIDmode)
313 return op;
315 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
316 extend since it will be more efficient to do so unless the signedness of
317 a promoted object differs from our extension. */
318 if (! no_extend
319 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
320 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
321 return convert_modes (mode, oldmode, op, unsignedp);
323 /* If MODE is no wider than a single word, we return a paradoxical
324 SUBREG. */
325 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
326 return gen_rtx_SUBREG (mode, force_reg (GET_MODE (op), op), 0);
328 /* Otherwise, get an object of MODE, clobber it, and set the low-order
329 part to OP. */
331 result = gen_reg_rtx (mode);
332 emit_clobber (result);
333 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
334 return result;
337 /* Return the optab used for computing the operation given by the tree code,
338 CODE and the tree EXP. This function is not always usable (for example, it
339 cannot give complete results for multiplication or division) but probably
340 ought to be relied on more widely throughout the expander. */
341 optab
342 optab_for_tree_code (enum tree_code code, const_tree type,
343 enum optab_subtype subtype)
345 bool trapv;
346 switch (code)
348 case BIT_AND_EXPR:
349 return and_optab;
351 case BIT_IOR_EXPR:
352 return ior_optab;
354 case BIT_NOT_EXPR:
355 return one_cmpl_optab;
357 case BIT_XOR_EXPR:
358 return xor_optab;
360 case TRUNC_MOD_EXPR:
361 case CEIL_MOD_EXPR:
362 case FLOOR_MOD_EXPR:
363 case ROUND_MOD_EXPR:
364 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
366 case RDIV_EXPR:
367 case TRUNC_DIV_EXPR:
368 case CEIL_DIV_EXPR:
369 case FLOOR_DIV_EXPR:
370 case ROUND_DIV_EXPR:
371 case EXACT_DIV_EXPR:
372 if (TYPE_SATURATING(type))
373 return TYPE_UNSIGNED(type) ? usdiv_optab : ssdiv_optab;
374 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
376 case LSHIFT_EXPR:
377 if (VECTOR_MODE_P (TYPE_MODE (type)))
379 if (subtype == optab_vector)
380 return TYPE_SATURATING (type) ? NULL : vashl_optab;
382 gcc_assert (subtype == optab_scalar);
384 if (TYPE_SATURATING(type))
385 return TYPE_UNSIGNED(type) ? usashl_optab : ssashl_optab;
386 return ashl_optab;
388 case RSHIFT_EXPR:
389 if (VECTOR_MODE_P (TYPE_MODE (type)))
391 if (subtype == optab_vector)
392 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
394 gcc_assert (subtype == optab_scalar);
396 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
398 case LROTATE_EXPR:
399 if (VECTOR_MODE_P (TYPE_MODE (type)))
401 if (subtype == optab_vector)
402 return vrotl_optab;
404 gcc_assert (subtype == optab_scalar);
406 return rotl_optab;
408 case RROTATE_EXPR:
409 if (VECTOR_MODE_P (TYPE_MODE (type)))
411 if (subtype == optab_vector)
412 return vrotr_optab;
414 gcc_assert (subtype == optab_scalar);
416 return rotr_optab;
418 case MAX_EXPR:
419 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
421 case MIN_EXPR:
422 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
424 case REALIGN_LOAD_EXPR:
425 return vec_realign_load_optab;
427 case WIDEN_SUM_EXPR:
428 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
430 case DOT_PROD_EXPR:
431 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
433 case REDUC_MAX_EXPR:
434 return TYPE_UNSIGNED (type) ? reduc_umax_optab : reduc_smax_optab;
436 case REDUC_MIN_EXPR:
437 return TYPE_UNSIGNED (type) ? reduc_umin_optab : reduc_smin_optab;
439 case REDUC_PLUS_EXPR:
440 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
442 case VEC_LSHIFT_EXPR:
443 return vec_shl_optab;
445 case VEC_RSHIFT_EXPR:
446 return vec_shr_optab;
448 case VEC_WIDEN_MULT_HI_EXPR:
449 return TYPE_UNSIGNED (type) ?
450 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
452 case VEC_WIDEN_MULT_LO_EXPR:
453 return TYPE_UNSIGNED (type) ?
454 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
456 case VEC_UNPACK_HI_EXPR:
457 return TYPE_UNSIGNED (type) ?
458 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
460 case VEC_UNPACK_LO_EXPR:
461 return TYPE_UNSIGNED (type) ?
462 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
464 case VEC_UNPACK_FLOAT_HI_EXPR:
465 /* The signedness is determined from input operand. */
466 return TYPE_UNSIGNED (type) ?
467 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
469 case VEC_UNPACK_FLOAT_LO_EXPR:
470 /* The signedness is determined from input operand. */
471 return TYPE_UNSIGNED (type) ?
472 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
474 case VEC_PACK_TRUNC_EXPR:
475 return vec_pack_trunc_optab;
477 case VEC_PACK_SAT_EXPR:
478 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
480 case VEC_PACK_FIX_TRUNC_EXPR:
481 /* The signedness is determined from output operand. */
482 return TYPE_UNSIGNED (type) ?
483 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
485 default:
486 break;
489 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
490 switch (code)
492 case POINTER_PLUS_EXPR:
493 case PLUS_EXPR:
494 if (TYPE_SATURATING(type))
495 return TYPE_UNSIGNED(type) ? usadd_optab : ssadd_optab;
496 return trapv ? addv_optab : add_optab;
498 case MINUS_EXPR:
499 if (TYPE_SATURATING(type))
500 return TYPE_UNSIGNED(type) ? ussub_optab : sssub_optab;
501 return trapv ? subv_optab : sub_optab;
503 case MULT_EXPR:
504 if (TYPE_SATURATING(type))
505 return TYPE_UNSIGNED(type) ? usmul_optab : ssmul_optab;
506 return trapv ? smulv_optab : smul_optab;
508 case NEGATE_EXPR:
509 if (TYPE_SATURATING(type))
510 return TYPE_UNSIGNED(type) ? usneg_optab : ssneg_optab;
511 return trapv ? negv_optab : neg_optab;
513 case ABS_EXPR:
514 return trapv ? absv_optab : abs_optab;
516 case VEC_EXTRACT_EVEN_EXPR:
517 return vec_extract_even_optab;
519 case VEC_EXTRACT_ODD_EXPR:
520 return vec_extract_odd_optab;
522 case VEC_INTERLEAVE_HIGH_EXPR:
523 return vec_interleave_high_optab;
525 case VEC_INTERLEAVE_LOW_EXPR:
526 return vec_interleave_low_optab;
528 default:
529 return NULL;
534 /* Expand vector widening operations.
536 There are two different classes of operations handled here:
537 1) Operations whose result is wider than all the arguments to the operation.
538 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
539 In this case OP0 and optionally OP1 would be initialized,
540 but WIDE_OP wouldn't (not relevant for this case).
541 2) Operations whose result is of the same size as the last argument to the
542 operation, but wider than all the other arguments to the operation.
543 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
544 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
546 E.g, when called to expand the following operations, this is how
547 the arguments will be initialized:
548 nops OP0 OP1 WIDE_OP
549 widening-sum 2 oprnd0 - oprnd1
550 widening-dot-product 3 oprnd0 oprnd1 oprnd2
551 widening-mult 2 oprnd0 oprnd1 -
552 type-promotion (vec-unpack) 1 oprnd0 - - */
555 expand_widen_pattern_expr (tree exp, rtx op0, rtx op1, rtx wide_op, rtx target,
556 int unsignedp)
558 tree oprnd0, oprnd1, oprnd2;
559 enum machine_mode wmode = 0, tmode0, tmode1 = 0;
560 optab widen_pattern_optab;
561 int icode;
562 enum machine_mode xmode0, xmode1 = 0, wxmode = 0;
563 rtx temp;
564 rtx pat;
565 rtx xop0, xop1, wxop;
566 int nops = TREE_OPERAND_LENGTH (exp);
568 oprnd0 = TREE_OPERAND (exp, 0);
569 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
570 widen_pattern_optab =
571 optab_for_tree_code (TREE_CODE (exp), TREE_TYPE (oprnd0), optab_default);
572 icode = (int) optab_handler (widen_pattern_optab, tmode0)->insn_code;
573 gcc_assert (icode != CODE_FOR_nothing);
574 xmode0 = insn_data[icode].operand[1].mode;
576 if (nops >= 2)
578 oprnd1 = TREE_OPERAND (exp, 1);
579 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
580 xmode1 = insn_data[icode].operand[2].mode;
583 /* The last operand is of a wider mode than the rest of the operands. */
584 if (nops == 2)
586 wmode = tmode1;
587 wxmode = xmode1;
589 else if (nops == 3)
591 gcc_assert (tmode1 == tmode0);
592 gcc_assert (op1);
593 oprnd2 = TREE_OPERAND (exp, 2);
594 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
595 wxmode = insn_data[icode].operand[3].mode;
598 if (!wide_op)
599 wmode = wxmode = insn_data[icode].operand[0].mode;
601 if (!target
602 || ! (*insn_data[icode].operand[0].predicate) (target, wmode))
603 temp = gen_reg_rtx (wmode);
604 else
605 temp = target;
607 xop0 = op0;
608 xop1 = op1;
609 wxop = wide_op;
611 /* In case the insn wants input operands in modes different from
612 those of the actual operands, convert the operands. It would
613 seem that we don't need to convert CONST_INTs, but we do, so
614 that they're properly zero-extended, sign-extended or truncated
615 for their mode. */
617 if (GET_MODE (op0) != xmode0 && xmode0 != VOIDmode)
618 xop0 = convert_modes (xmode0,
619 GET_MODE (op0) != VOIDmode
620 ? GET_MODE (op0)
621 : tmode0,
622 xop0, unsignedp);
624 if (op1)
625 if (GET_MODE (op1) != xmode1 && xmode1 != VOIDmode)
626 xop1 = convert_modes (xmode1,
627 GET_MODE (op1) != VOIDmode
628 ? GET_MODE (op1)
629 : tmode1,
630 xop1, unsignedp);
632 if (wide_op)
633 if (GET_MODE (wide_op) != wxmode && wxmode != VOIDmode)
634 wxop = convert_modes (wxmode,
635 GET_MODE (wide_op) != VOIDmode
636 ? GET_MODE (wide_op)
637 : wmode,
638 wxop, unsignedp);
640 /* Now, if insn's predicates don't allow our operands, put them into
641 pseudo regs. */
643 if (! (*insn_data[icode].operand[1].predicate) (xop0, xmode0)
644 && xmode0 != VOIDmode)
645 xop0 = copy_to_mode_reg (xmode0, xop0);
647 if (op1)
649 if (! (*insn_data[icode].operand[2].predicate) (xop1, xmode1)
650 && xmode1 != VOIDmode)
651 xop1 = copy_to_mode_reg (xmode1, xop1);
653 if (wide_op)
655 if (! (*insn_data[icode].operand[3].predicate) (wxop, wxmode)
656 && wxmode != VOIDmode)
657 wxop = copy_to_mode_reg (wxmode, wxop);
659 pat = GEN_FCN (icode) (temp, xop0, xop1, wxop);
661 else
662 pat = GEN_FCN (icode) (temp, xop0, xop1);
664 else
666 if (wide_op)
668 if (! (*insn_data[icode].operand[2].predicate) (wxop, wxmode)
669 && wxmode != VOIDmode)
670 wxop = copy_to_mode_reg (wxmode, wxop);
672 pat = GEN_FCN (icode) (temp, xop0, wxop);
674 else
675 pat = GEN_FCN (icode) (temp, xop0);
678 emit_insn (pat);
679 return temp;
682 /* Generate code to perform an operation specified by TERNARY_OPTAB
683 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
685 UNSIGNEDP is for the case where we have to widen the operands
686 to perform the operation. It says to use zero-extension.
688 If TARGET is nonzero, the value
689 is generated there, if it is convenient to do so.
690 In all cases an rtx is returned for the locus of the value;
691 this may or may not be TARGET. */
694 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
695 rtx op1, rtx op2, rtx target, int unsignedp)
697 int icode = (int) optab_handler (ternary_optab, mode)->insn_code;
698 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
699 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
700 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
701 rtx temp;
702 rtx pat;
703 rtx xop0 = op0, xop1 = op1, xop2 = op2;
705 gcc_assert (optab_handler (ternary_optab, mode)->insn_code
706 != CODE_FOR_nothing);
708 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
709 temp = gen_reg_rtx (mode);
710 else
711 temp = target;
713 /* In case the insn wants input operands in modes different from
714 those of the actual operands, convert the operands. It would
715 seem that we don't need to convert CONST_INTs, but we do, so
716 that they're properly zero-extended, sign-extended or truncated
717 for their mode. */
719 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
720 xop0 = convert_modes (mode0,
721 GET_MODE (op0) != VOIDmode
722 ? GET_MODE (op0)
723 : mode,
724 xop0, unsignedp);
726 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
727 xop1 = convert_modes (mode1,
728 GET_MODE (op1) != VOIDmode
729 ? GET_MODE (op1)
730 : mode,
731 xop1, unsignedp);
733 if (GET_MODE (op2) != mode2 && mode2 != VOIDmode)
734 xop2 = convert_modes (mode2,
735 GET_MODE (op2) != VOIDmode
736 ? GET_MODE (op2)
737 : mode,
738 xop2, unsignedp);
740 /* Now, if insn's predicates don't allow our operands, put them into
741 pseudo regs. */
743 if (!insn_data[icode].operand[1].predicate (xop0, mode0)
744 && mode0 != VOIDmode)
745 xop0 = copy_to_mode_reg (mode0, xop0);
747 if (!insn_data[icode].operand[2].predicate (xop1, mode1)
748 && mode1 != VOIDmode)
749 xop1 = copy_to_mode_reg (mode1, xop1);
751 if (!insn_data[icode].operand[3].predicate (xop2, mode2)
752 && mode2 != VOIDmode)
753 xop2 = copy_to_mode_reg (mode2, xop2);
755 pat = GEN_FCN (icode) (temp, xop0, xop1, xop2);
757 emit_insn (pat);
758 return temp;
762 /* Like expand_binop, but return a constant rtx if the result can be
763 calculated at compile time. The arguments and return value are
764 otherwise the same as for expand_binop. */
766 static rtx
767 simplify_expand_binop (enum machine_mode mode, optab binoptab,
768 rtx op0, rtx op1, rtx target, int unsignedp,
769 enum optab_methods methods)
771 if (CONSTANT_P (op0) && CONSTANT_P (op1))
773 rtx x = simplify_binary_operation (binoptab->code, mode, op0, op1);
775 if (x)
776 return x;
779 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
782 /* Like simplify_expand_binop, but always put the result in TARGET.
783 Return true if the expansion succeeded. */
785 bool
786 force_expand_binop (enum machine_mode mode, optab binoptab,
787 rtx op0, rtx op1, rtx target, int unsignedp,
788 enum optab_methods methods)
790 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
791 target, unsignedp, methods);
792 if (x == 0)
793 return false;
794 if (x != target)
795 emit_move_insn (target, x);
796 return true;
799 /* Generate insns for VEC_LSHIFT_EXPR, VEC_RSHIFT_EXPR. */
802 expand_vec_shift_expr (tree vec_shift_expr, rtx target)
804 enum insn_code icode;
805 rtx rtx_op1, rtx_op2;
806 enum machine_mode mode1;
807 enum machine_mode mode2;
808 enum machine_mode mode = TYPE_MODE (TREE_TYPE (vec_shift_expr));
809 tree vec_oprnd = TREE_OPERAND (vec_shift_expr, 0);
810 tree shift_oprnd = TREE_OPERAND (vec_shift_expr, 1);
811 optab shift_optab;
812 rtx pat;
814 switch (TREE_CODE (vec_shift_expr))
816 case VEC_RSHIFT_EXPR:
817 shift_optab = vec_shr_optab;
818 break;
819 case VEC_LSHIFT_EXPR:
820 shift_optab = vec_shl_optab;
821 break;
822 default:
823 gcc_unreachable ();
826 icode = (int) optab_handler (shift_optab, mode)->insn_code;
827 gcc_assert (icode != CODE_FOR_nothing);
829 mode1 = insn_data[icode].operand[1].mode;
830 mode2 = insn_data[icode].operand[2].mode;
832 rtx_op1 = expand_normal (vec_oprnd);
833 if (!(*insn_data[icode].operand[1].predicate) (rtx_op1, mode1)
834 && mode1 != VOIDmode)
835 rtx_op1 = force_reg (mode1, rtx_op1);
837 rtx_op2 = expand_normal (shift_oprnd);
838 if (!(*insn_data[icode].operand[2].predicate) (rtx_op2, mode2)
839 && mode2 != VOIDmode)
840 rtx_op2 = force_reg (mode2, rtx_op2);
842 if (!target
843 || ! (*insn_data[icode].operand[0].predicate) (target, mode))
844 target = gen_reg_rtx (mode);
846 /* Emit instruction */
847 pat = GEN_FCN (icode) (target, rtx_op1, rtx_op2);
848 gcc_assert (pat);
849 emit_insn (pat);
851 return target;
854 /* This subroutine of expand_doubleword_shift handles the cases in which
855 the effective shift value is >= BITS_PER_WORD. The arguments and return
856 value are the same as for the parent routine, except that SUPERWORD_OP1
857 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
858 INTO_TARGET may be null if the caller has decided to calculate it. */
860 static bool
861 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
862 rtx outof_target, rtx into_target,
863 int unsignedp, enum optab_methods methods)
865 if (into_target != 0)
866 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
867 into_target, unsignedp, methods))
868 return false;
870 if (outof_target != 0)
872 /* For a signed right shift, we must fill OUTOF_TARGET with copies
873 of the sign bit, otherwise we must fill it with zeros. */
874 if (binoptab != ashr_optab)
875 emit_move_insn (outof_target, CONST0_RTX (word_mode));
876 else
877 if (!force_expand_binop (word_mode, binoptab,
878 outof_input, GEN_INT (BITS_PER_WORD - 1),
879 outof_target, unsignedp, methods))
880 return false;
882 return true;
885 /* This subroutine of expand_doubleword_shift handles the cases in which
886 the effective shift value is < BITS_PER_WORD. The arguments and return
887 value are the same as for the parent routine. */
889 static bool
890 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
891 rtx outof_input, rtx into_input, rtx op1,
892 rtx outof_target, rtx into_target,
893 int unsignedp, enum optab_methods methods,
894 unsigned HOST_WIDE_INT shift_mask)
896 optab reverse_unsigned_shift, unsigned_shift;
897 rtx tmp, carries;
899 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
900 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
902 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
903 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
904 the opposite direction to BINOPTAB. */
905 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
907 carries = outof_input;
908 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
909 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
910 0, true, methods);
912 else
914 /* We must avoid shifting by BITS_PER_WORD bits since that is either
915 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
916 has unknown behavior. Do a single shift first, then shift by the
917 remainder. It's OK to use ~OP1 as the remainder if shift counts
918 are truncated to the mode size. */
919 carries = expand_binop (word_mode, reverse_unsigned_shift,
920 outof_input, const1_rtx, 0, unsignedp, methods);
921 if (shift_mask == BITS_PER_WORD - 1)
923 tmp = immed_double_const (-1, -1, op1_mode);
924 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
925 0, true, methods);
927 else
929 tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
930 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
931 0, true, methods);
934 if (tmp == 0 || carries == 0)
935 return false;
936 carries = expand_binop (word_mode, reverse_unsigned_shift,
937 carries, tmp, 0, unsignedp, methods);
938 if (carries == 0)
939 return false;
941 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
942 so the result can go directly into INTO_TARGET if convenient. */
943 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
944 into_target, unsignedp, methods);
945 if (tmp == 0)
946 return false;
948 /* Now OR in the bits carried over from OUTOF_INPUT. */
949 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
950 into_target, unsignedp, methods))
951 return false;
953 /* Use a standard word_mode shift for the out-of half. */
954 if (outof_target != 0)
955 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
956 outof_target, unsignedp, methods))
957 return false;
959 return true;
963 #ifdef HAVE_conditional_move
964 /* Try implementing expand_doubleword_shift using conditional moves.
965 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
966 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
967 are the shift counts to use in the former and latter case. All other
968 arguments are the same as the parent routine. */
970 static bool
971 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
972 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
973 rtx outof_input, rtx into_input,
974 rtx subword_op1, rtx superword_op1,
975 rtx outof_target, rtx into_target,
976 int unsignedp, enum optab_methods methods,
977 unsigned HOST_WIDE_INT shift_mask)
979 rtx outof_superword, into_superword;
981 /* Put the superword version of the output into OUTOF_SUPERWORD and
982 INTO_SUPERWORD. */
983 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
984 if (outof_target != 0 && subword_op1 == superword_op1)
986 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
987 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
988 into_superword = outof_target;
989 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
990 outof_superword, 0, unsignedp, methods))
991 return false;
993 else
995 into_superword = gen_reg_rtx (word_mode);
996 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
997 outof_superword, into_superword,
998 unsignedp, methods))
999 return false;
1002 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
1003 if (!expand_subword_shift (op1_mode, binoptab,
1004 outof_input, into_input, subword_op1,
1005 outof_target, into_target,
1006 unsignedp, methods, shift_mask))
1007 return false;
1009 /* Select between them. Do the INTO half first because INTO_SUPERWORD
1010 might be the current value of OUTOF_TARGET. */
1011 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
1012 into_target, into_superword, word_mode, false))
1013 return false;
1015 if (outof_target != 0)
1016 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
1017 outof_target, outof_superword,
1018 word_mode, false))
1019 return false;
1021 return true;
1023 #endif
1025 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
1026 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
1027 input operand; the shift moves bits in the direction OUTOF_INPUT->
1028 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
1029 of the target. OP1 is the shift count and OP1_MODE is its mode.
1030 If OP1 is constant, it will have been truncated as appropriate
1031 and is known to be nonzero.
1033 If SHIFT_MASK is zero, the result of word shifts is undefined when the
1034 shift count is outside the range [0, BITS_PER_WORD). This routine must
1035 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
1037 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
1038 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
1039 fill with zeros or sign bits as appropriate.
1041 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
1042 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
1043 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
1044 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
1045 are undefined.
1047 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
1048 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
1049 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
1050 function wants to calculate it itself.
1052 Return true if the shift could be successfully synthesized. */
1054 static bool
1055 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
1056 rtx outof_input, rtx into_input, rtx op1,
1057 rtx outof_target, rtx into_target,
1058 int unsignedp, enum optab_methods methods,
1059 unsigned HOST_WIDE_INT shift_mask)
1061 rtx superword_op1, tmp, cmp1, cmp2;
1062 rtx subword_label, done_label;
1063 enum rtx_code cmp_code;
1065 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1066 fill the result with sign or zero bits as appropriate. If so, the value
1067 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1068 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1069 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1071 This isn't worthwhile for constant shifts since the optimizers will
1072 cope better with in-range shift counts. */
1073 if (shift_mask >= BITS_PER_WORD
1074 && outof_target != 0
1075 && !CONSTANT_P (op1))
1077 if (!expand_doubleword_shift (op1_mode, binoptab,
1078 outof_input, into_input, op1,
1079 0, into_target,
1080 unsignedp, methods, shift_mask))
1081 return false;
1082 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1083 outof_target, unsignedp, methods))
1084 return false;
1085 return true;
1088 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1089 is true when the effective shift value is less than BITS_PER_WORD.
1090 Set SUPERWORD_OP1 to the shift count that should be used to shift
1091 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1092 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
1093 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1095 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1096 is a subword shift count. */
1097 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1098 0, true, methods);
1099 cmp2 = CONST0_RTX (op1_mode);
1100 cmp_code = EQ;
1101 superword_op1 = op1;
1103 else
1105 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1106 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1107 0, true, methods);
1108 cmp2 = CONST0_RTX (op1_mode);
1109 cmp_code = LT;
1110 superword_op1 = cmp1;
1112 if (cmp1 == 0)
1113 return false;
1115 /* If we can compute the condition at compile time, pick the
1116 appropriate subroutine. */
1117 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1118 if (tmp != 0 && GET_CODE (tmp) == CONST_INT)
1120 if (tmp == const0_rtx)
1121 return expand_superword_shift (binoptab, outof_input, superword_op1,
1122 outof_target, into_target,
1123 unsignedp, methods);
1124 else
1125 return expand_subword_shift (op1_mode, binoptab,
1126 outof_input, into_input, op1,
1127 outof_target, into_target,
1128 unsignedp, methods, shift_mask);
1131 #ifdef HAVE_conditional_move
1132 /* Try using conditional moves to generate straight-line code. */
1134 rtx start = get_last_insn ();
1135 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1136 cmp_code, cmp1, cmp2,
1137 outof_input, into_input,
1138 op1, superword_op1,
1139 outof_target, into_target,
1140 unsignedp, methods, shift_mask))
1141 return true;
1142 delete_insns_since (start);
1144 #endif
1146 /* As a last resort, use branches to select the correct alternative. */
1147 subword_label = gen_label_rtx ();
1148 done_label = gen_label_rtx ();
1150 NO_DEFER_POP;
1151 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1152 0, 0, subword_label);
1153 OK_DEFER_POP;
1155 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1156 outof_target, into_target,
1157 unsignedp, methods))
1158 return false;
1160 emit_jump_insn (gen_jump (done_label));
1161 emit_barrier ();
1162 emit_label (subword_label);
1164 if (!expand_subword_shift (op1_mode, binoptab,
1165 outof_input, into_input, op1,
1166 outof_target, into_target,
1167 unsignedp, methods, shift_mask))
1168 return false;
1170 emit_label (done_label);
1171 return true;
1174 /* Subroutine of expand_binop. Perform a double word multiplication of
1175 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1176 as the target's word_mode. This function return NULL_RTX if anything
1177 goes wrong, in which case it may have already emitted instructions
1178 which need to be deleted.
1180 If we want to multiply two two-word values and have normal and widening
1181 multiplies of single-word values, we can do this with three smaller
1182 multiplications.
1184 The multiplication proceeds as follows:
1185 _______________________
1186 [__op0_high_|__op0_low__]
1187 _______________________
1188 * [__op1_high_|__op1_low__]
1189 _______________________________________________
1190 _______________________
1191 (1) [__op0_low__*__op1_low__]
1192 _______________________
1193 (2a) [__op0_low__*__op1_high_]
1194 _______________________
1195 (2b) [__op0_high_*__op1_low__]
1196 _______________________
1197 (3) [__op0_high_*__op1_high_]
1200 This gives a 4-word result. Since we are only interested in the
1201 lower 2 words, partial result (3) and the upper words of (2a) and
1202 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1203 calculated using non-widening multiplication.
1205 (1), however, needs to be calculated with an unsigned widening
1206 multiplication. If this operation is not directly supported we
1207 try using a signed widening multiplication and adjust the result.
1208 This adjustment works as follows:
1210 If both operands are positive then no adjustment is needed.
1212 If the operands have different signs, for example op0_low < 0 and
1213 op1_low >= 0, the instruction treats the most significant bit of
1214 op0_low as a sign bit instead of a bit with significance
1215 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1216 with 2**BITS_PER_WORD - op0_low, and two's complements the
1217 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1218 the result.
1220 Similarly, if both operands are negative, we need to add
1221 (op0_low + op1_low) * 2**BITS_PER_WORD.
1223 We use a trick to adjust quickly. We logically shift op0_low right
1224 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1225 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1226 logical shift exists, we do an arithmetic right shift and subtract
1227 the 0 or -1. */
1229 static rtx
1230 expand_doubleword_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
1231 bool umulp, enum optab_methods methods)
1233 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1234 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1235 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1236 rtx product, adjust, product_high, temp;
1238 rtx op0_high = operand_subword_force (op0, high, mode);
1239 rtx op0_low = operand_subword_force (op0, low, mode);
1240 rtx op1_high = operand_subword_force (op1, high, mode);
1241 rtx op1_low = operand_subword_force (op1, low, mode);
1243 /* If we're using an unsigned multiply to directly compute the product
1244 of the low-order words of the operands and perform any required
1245 adjustments of the operands, we begin by trying two more multiplications
1246 and then computing the appropriate sum.
1248 We have checked above that the required addition is provided.
1249 Full-word addition will normally always succeed, especially if
1250 it is provided at all, so we don't worry about its failure. The
1251 multiplication may well fail, however, so we do handle that. */
1253 if (!umulp)
1255 /* ??? This could be done with emit_store_flag where available. */
1256 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1257 NULL_RTX, 1, methods);
1258 if (temp)
1259 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1260 NULL_RTX, 0, OPTAB_DIRECT);
1261 else
1263 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1264 NULL_RTX, 0, methods);
1265 if (!temp)
1266 return NULL_RTX;
1267 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1268 NULL_RTX, 0, OPTAB_DIRECT);
1271 if (!op0_high)
1272 return NULL_RTX;
1275 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1276 NULL_RTX, 0, OPTAB_DIRECT);
1277 if (!adjust)
1278 return NULL_RTX;
1280 /* OP0_HIGH should now be dead. */
1282 if (!umulp)
1284 /* ??? This could be done with emit_store_flag where available. */
1285 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1286 NULL_RTX, 1, methods);
1287 if (temp)
1288 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1289 NULL_RTX, 0, OPTAB_DIRECT);
1290 else
1292 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1293 NULL_RTX, 0, methods);
1294 if (!temp)
1295 return NULL_RTX;
1296 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1297 NULL_RTX, 0, OPTAB_DIRECT);
1300 if (!op1_high)
1301 return NULL_RTX;
1304 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1305 NULL_RTX, 0, OPTAB_DIRECT);
1306 if (!temp)
1307 return NULL_RTX;
1309 /* OP1_HIGH should now be dead. */
1311 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1312 adjust, 0, OPTAB_DIRECT);
1314 if (target && !REG_P (target))
1315 target = NULL_RTX;
1317 if (umulp)
1318 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1319 target, 1, OPTAB_DIRECT);
1320 else
1321 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1322 target, 1, OPTAB_DIRECT);
1324 if (!product)
1325 return NULL_RTX;
1327 product_high = operand_subword (product, high, 1, mode);
1328 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1329 REG_P (product_high) ? product_high : adjust,
1330 0, OPTAB_DIRECT);
1331 emit_move_insn (product_high, adjust);
1332 return product;
1335 /* Wrapper around expand_binop which takes an rtx code to specify
1336 the operation to perform, not an optab pointer. All other
1337 arguments are the same. */
1339 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
1340 rtx op1, rtx target, int unsignedp,
1341 enum optab_methods methods)
1343 optab binop = code_to_optab[(int) code];
1344 gcc_assert (binop);
1346 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1349 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1350 binop. Order them according to commutative_operand_precedence and, if
1351 possible, try to put TARGET or a pseudo first. */
1352 static bool
1353 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1355 int op0_prec = commutative_operand_precedence (op0);
1356 int op1_prec = commutative_operand_precedence (op1);
1358 if (op0_prec < op1_prec)
1359 return true;
1361 if (op0_prec > op1_prec)
1362 return false;
1364 /* With equal precedence, both orders are ok, but it is better if the
1365 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1366 if (target == 0 || REG_P (target))
1367 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1368 else
1369 return rtx_equal_p (op1, target);
1372 /* Return true if BINOPTAB implements a shift operation. */
1374 static bool
1375 shift_optab_p (optab binoptab)
1377 switch (binoptab->code)
1379 case ASHIFT:
1380 case SS_ASHIFT:
1381 case US_ASHIFT:
1382 case ASHIFTRT:
1383 case LSHIFTRT:
1384 case ROTATE:
1385 case ROTATERT:
1386 return true;
1388 default:
1389 return false;
1393 /* Return true if BINOPTAB implements a commutative binary operation. */
1395 static bool
1396 commutative_optab_p (optab binoptab)
1398 return (GET_RTX_CLASS (binoptab->code) == RTX_COMM_ARITH
1399 || binoptab == smul_widen_optab
1400 || binoptab == umul_widen_optab
1401 || binoptab == smul_highpart_optab
1402 || binoptab == umul_highpart_optab);
1405 /* X is to be used in mode MODE as an operand to BINOPTAB. If we're
1406 optimizing, and if the operand is a constant that costs more than
1407 1 instruction, force the constant into a register and return that
1408 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1410 static rtx
1411 avoid_expensive_constant (enum machine_mode mode, optab binoptab,
1412 rtx x, bool unsignedp)
1414 if (mode != VOIDmode
1415 && optimize
1416 && CONSTANT_P (x)
1417 && rtx_cost (x, binoptab->code) > COSTS_N_INSNS (1))
1419 if (GET_CODE (x) == CONST_INT)
1421 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1422 if (intval != INTVAL (x))
1423 x = GEN_INT (intval);
1425 else
1426 x = convert_modes (mode, VOIDmode, x, unsignedp);
1427 x = force_reg (mode, x);
1429 return x;
1432 /* Helper function for expand_binop: handle the case where there
1433 is an insn that directly implements the indicated operation.
1434 Returns null if this is not possible. */
1435 static rtx
1436 expand_binop_directly (enum machine_mode mode, optab binoptab,
1437 rtx op0, rtx op1,
1438 rtx target, int unsignedp, enum optab_methods methods,
1439 rtx last)
1441 int icode = (int) optab_handler (binoptab, mode)->insn_code;
1442 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
1443 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
1444 enum machine_mode tmp_mode;
1445 bool commutative_p;
1446 rtx pat;
1447 rtx xop0 = op0, xop1 = op1;
1448 rtx temp;
1449 rtx swap;
1451 if (target)
1452 temp = target;
1453 else
1454 temp = gen_reg_rtx (mode);
1456 /* If it is a commutative operator and the modes would match
1457 if we would swap the operands, we can save the conversions. */
1458 commutative_p = commutative_optab_p (binoptab);
1459 if (commutative_p
1460 && GET_MODE (xop0) != mode0 && GET_MODE (xop1) != mode1
1461 && GET_MODE (xop0) == mode1 && GET_MODE (xop1) == mode1)
1463 swap = xop0;
1464 xop0 = xop1;
1465 xop1 = swap;
1468 /* If we are optimizing, force expensive constants into a register. */
1469 xop0 = avoid_expensive_constant (mode0, binoptab, xop0, unsignedp);
1470 if (!shift_optab_p (binoptab))
1471 xop1 = avoid_expensive_constant (mode1, binoptab, xop1, unsignedp);
1473 /* In case the insn wants input operands in modes different from
1474 those of the actual operands, convert the operands. It would
1475 seem that we don't need to convert CONST_INTs, but we do, so
1476 that they're properly zero-extended, sign-extended or truncated
1477 for their mode. */
1479 if (GET_MODE (xop0) != mode0 && mode0 != VOIDmode)
1480 xop0 = convert_modes (mode0,
1481 GET_MODE (xop0) != VOIDmode
1482 ? GET_MODE (xop0)
1483 : mode,
1484 xop0, unsignedp);
1486 if (GET_MODE (xop1) != mode1 && mode1 != VOIDmode)
1487 xop1 = convert_modes (mode1,
1488 GET_MODE (xop1) != VOIDmode
1489 ? GET_MODE (xop1)
1490 : mode,
1491 xop1, unsignedp);
1493 /* If operation is commutative,
1494 try to make the first operand a register.
1495 Even better, try to make it the same as the target.
1496 Also try to make the last operand a constant. */
1497 if (commutative_p
1498 && swap_commutative_operands_with_target (target, xop0, xop1))
1500 swap = xop1;
1501 xop1 = xop0;
1502 xop0 = swap;
1505 /* Now, if insn's predicates don't allow our operands, put them into
1506 pseudo regs. */
1508 if (!insn_data[icode].operand[1].predicate (xop0, mode0)
1509 && mode0 != VOIDmode)
1510 xop0 = copy_to_mode_reg (mode0, xop0);
1512 if (!insn_data[icode].operand[2].predicate (xop1, mode1)
1513 && mode1 != VOIDmode)
1514 xop1 = copy_to_mode_reg (mode1, xop1);
1516 if (binoptab == vec_pack_trunc_optab
1517 || binoptab == vec_pack_usat_optab
1518 || binoptab == vec_pack_ssat_optab
1519 || binoptab == vec_pack_ufix_trunc_optab
1520 || binoptab == vec_pack_sfix_trunc_optab)
1522 /* The mode of the result is different then the mode of the
1523 arguments. */
1524 tmp_mode = insn_data[icode].operand[0].mode;
1525 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1526 return 0;
1528 else
1529 tmp_mode = mode;
1531 if (!insn_data[icode].operand[0].predicate (temp, tmp_mode))
1532 temp = gen_reg_rtx (tmp_mode);
1534 pat = GEN_FCN (icode) (temp, xop0, xop1);
1535 if (pat)
1537 /* If PAT is composed of more than one insn, try to add an appropriate
1538 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1539 operand, call expand_binop again, this time without a target. */
1540 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1541 && ! add_equal_note (pat, temp, binoptab->code, xop0, xop1))
1543 delete_insns_since (last);
1544 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1545 unsignedp, methods);
1548 emit_insn (pat);
1549 return temp;
1552 delete_insns_since (last);
1553 return NULL_RTX;
1556 /* Generate code to perform an operation specified by BINOPTAB
1557 on operands OP0 and OP1, with result having machine-mode MODE.
1559 UNSIGNEDP is for the case where we have to widen the operands
1560 to perform the operation. It says to use zero-extension.
1562 If TARGET is nonzero, the value
1563 is generated there, if it is convenient to do so.
1564 In all cases an rtx is returned for the locus of the value;
1565 this may or may not be TARGET. */
1568 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
1569 rtx target, int unsignedp, enum optab_methods methods)
1571 enum optab_methods next_methods
1572 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1573 ? OPTAB_WIDEN : methods);
1574 enum mode_class mclass;
1575 enum machine_mode wider_mode;
1576 rtx libfunc;
1577 rtx temp;
1578 rtx entry_last = get_last_insn ();
1579 rtx last;
1581 mclass = GET_MODE_CLASS (mode);
1583 /* If subtracting an integer constant, convert this into an addition of
1584 the negated constant. */
1586 if (binoptab == sub_optab && GET_CODE (op1) == CONST_INT)
1588 op1 = negate_rtx (mode, op1);
1589 binoptab = add_optab;
1592 /* Record where to delete back to if we backtrack. */
1593 last = get_last_insn ();
1595 /* If we can do it with a three-operand insn, do so. */
1597 if (methods != OPTAB_MUST_WIDEN
1598 && optab_handler (binoptab, mode)->insn_code != CODE_FOR_nothing)
1600 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1601 unsignedp, methods, last);
1602 if (temp)
1603 return temp;
1606 /* If we were trying to rotate, and that didn't work, try rotating
1607 the other direction before falling back to shifts and bitwise-or. */
1608 if (((binoptab == rotl_optab
1609 && optab_handler (rotr_optab, mode)->insn_code != CODE_FOR_nothing)
1610 || (binoptab == rotr_optab
1611 && optab_handler (rotl_optab, mode)->insn_code != CODE_FOR_nothing))
1612 && mclass == MODE_INT)
1614 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1615 rtx newop1;
1616 unsigned int bits = GET_MODE_BITSIZE (mode);
1618 if (GET_CODE (op1) == CONST_INT)
1619 newop1 = GEN_INT (bits - INTVAL (op1));
1620 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1621 newop1 = negate_rtx (mode, op1);
1622 else
1623 newop1 = expand_binop (mode, sub_optab,
1624 GEN_INT (bits), op1,
1625 NULL_RTX, unsignedp, OPTAB_DIRECT);
1627 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1628 target, unsignedp, methods, last);
1629 if (temp)
1630 return temp;
1633 /* If this is a multiply, see if we can do a widening operation that
1634 takes operands of this mode and makes a wider mode. */
1636 if (binoptab == smul_optab
1637 && GET_MODE_WIDER_MODE (mode) != VOIDmode
1638 && ((optab_handler ((unsignedp ? umul_widen_optab : smul_widen_optab),
1639 GET_MODE_WIDER_MODE (mode))->insn_code)
1640 != CODE_FOR_nothing))
1642 temp = expand_binop (GET_MODE_WIDER_MODE (mode),
1643 unsignedp ? umul_widen_optab : smul_widen_optab,
1644 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1646 if (temp != 0)
1648 if (GET_MODE_CLASS (mode) == MODE_INT
1649 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1650 GET_MODE_BITSIZE (GET_MODE (temp))))
1651 return gen_lowpart (mode, temp);
1652 else
1653 return convert_to_mode (mode, temp, unsignedp);
1657 /* Look for a wider mode of the same class for which we think we
1658 can open-code the operation. Check for a widening multiply at the
1659 wider mode as well. */
1661 if (CLASS_HAS_WIDER_MODES_P (mclass)
1662 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1663 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1664 wider_mode != VOIDmode;
1665 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1667 if (optab_handler (binoptab, wider_mode)->insn_code != CODE_FOR_nothing
1668 || (binoptab == smul_optab
1669 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1670 && ((optab_handler ((unsignedp ? umul_widen_optab
1671 : smul_widen_optab),
1672 GET_MODE_WIDER_MODE (wider_mode))->insn_code)
1673 != CODE_FOR_nothing)))
1675 rtx xop0 = op0, xop1 = op1;
1676 int no_extend = 0;
1678 /* For certain integer operations, we need not actually extend
1679 the narrow operands, as long as we will truncate
1680 the results to the same narrowness. */
1682 if ((binoptab == ior_optab || binoptab == and_optab
1683 || binoptab == xor_optab
1684 || binoptab == add_optab || binoptab == sub_optab
1685 || binoptab == smul_optab || binoptab == ashl_optab)
1686 && mclass == MODE_INT)
1688 no_extend = 1;
1689 xop0 = avoid_expensive_constant (mode, binoptab,
1690 xop0, unsignedp);
1691 if (binoptab != ashl_optab)
1692 xop1 = avoid_expensive_constant (mode, binoptab,
1693 xop1, unsignedp);
1696 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1698 /* The second operand of a shift must always be extended. */
1699 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1700 no_extend && binoptab != ashl_optab);
1702 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1703 unsignedp, OPTAB_DIRECT);
1704 if (temp)
1706 if (mclass != MODE_INT
1707 || !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1708 GET_MODE_BITSIZE (wider_mode)))
1710 if (target == 0)
1711 target = gen_reg_rtx (mode);
1712 convert_move (target, temp, 0);
1713 return target;
1715 else
1716 return gen_lowpart (mode, temp);
1718 else
1719 delete_insns_since (last);
1723 /* If operation is commutative,
1724 try to make the first operand a register.
1725 Even better, try to make it the same as the target.
1726 Also try to make the last operand a constant. */
1727 if (commutative_optab_p (binoptab)
1728 && swap_commutative_operands_with_target (target, op0, op1))
1730 temp = op1;
1731 op1 = op0;
1732 op0 = temp;
1735 /* These can be done a word at a time. */
1736 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1737 && mclass == MODE_INT
1738 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1739 && optab_handler (binoptab, word_mode)->insn_code != CODE_FOR_nothing)
1741 int i;
1742 rtx insns;
1743 rtx equiv_value;
1745 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1746 won't be accurate, so use a new target. */
1747 if (target == 0 || target == op0 || target == op1)
1748 target = gen_reg_rtx (mode);
1750 start_sequence ();
1752 /* Do the actual arithmetic. */
1753 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1755 rtx target_piece = operand_subword (target, i, 1, mode);
1756 rtx x = expand_binop (word_mode, binoptab,
1757 operand_subword_force (op0, i, mode),
1758 operand_subword_force (op1, i, mode),
1759 target_piece, unsignedp, next_methods);
1761 if (x == 0)
1762 break;
1764 if (target_piece != x)
1765 emit_move_insn (target_piece, x);
1768 insns = get_insns ();
1769 end_sequence ();
1771 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1773 if (binoptab->code != UNKNOWN)
1774 equiv_value
1775 = gen_rtx_fmt_ee (binoptab->code, mode,
1776 copy_rtx (op0), copy_rtx (op1));
1777 else
1778 equiv_value = 0;
1780 emit_insn (insns);
1781 return target;
1785 /* Synthesize double word shifts from single word shifts. */
1786 if ((binoptab == lshr_optab || binoptab == ashl_optab
1787 || binoptab == ashr_optab)
1788 && mclass == MODE_INT
1789 && (GET_CODE (op1) == CONST_INT || !optimize_size)
1790 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1791 && optab_handler (binoptab, word_mode)->insn_code != CODE_FOR_nothing
1792 && optab_handler (ashl_optab, word_mode)->insn_code != CODE_FOR_nothing
1793 && optab_handler (lshr_optab, word_mode)->insn_code != CODE_FOR_nothing)
1795 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1796 enum machine_mode op1_mode;
1798 double_shift_mask = targetm.shift_truncation_mask (mode);
1799 shift_mask = targetm.shift_truncation_mask (word_mode);
1800 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1802 /* Apply the truncation to constant shifts. */
1803 if (double_shift_mask > 0 && GET_CODE (op1) == CONST_INT)
1804 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1806 if (op1 == CONST0_RTX (op1_mode))
1807 return op0;
1809 /* Make sure that this is a combination that expand_doubleword_shift
1810 can handle. See the comments there for details. */
1811 if (double_shift_mask == 0
1812 || (shift_mask == BITS_PER_WORD - 1
1813 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1815 rtx insns;
1816 rtx into_target, outof_target;
1817 rtx into_input, outof_input;
1818 int left_shift, outof_word;
1820 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1821 won't be accurate, so use a new target. */
1822 if (target == 0 || target == op0 || target == op1)
1823 target = gen_reg_rtx (mode);
1825 start_sequence ();
1827 /* OUTOF_* is the word we are shifting bits away from, and
1828 INTO_* is the word that we are shifting bits towards, thus
1829 they differ depending on the direction of the shift and
1830 WORDS_BIG_ENDIAN. */
1832 left_shift = binoptab == ashl_optab;
1833 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1835 outof_target = operand_subword (target, outof_word, 1, mode);
1836 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1838 outof_input = operand_subword_force (op0, outof_word, mode);
1839 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1841 if (expand_doubleword_shift (op1_mode, binoptab,
1842 outof_input, into_input, op1,
1843 outof_target, into_target,
1844 unsignedp, next_methods, shift_mask))
1846 insns = get_insns ();
1847 end_sequence ();
1849 emit_insn (insns);
1850 return target;
1852 end_sequence ();
1856 /* Synthesize double word rotates from single word shifts. */
1857 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1858 && mclass == MODE_INT
1859 && GET_CODE (op1) == CONST_INT
1860 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1861 && optab_handler (ashl_optab, word_mode)->insn_code != CODE_FOR_nothing
1862 && optab_handler (lshr_optab, word_mode)->insn_code != CODE_FOR_nothing)
1864 rtx insns;
1865 rtx into_target, outof_target;
1866 rtx into_input, outof_input;
1867 rtx inter;
1868 int shift_count, left_shift, outof_word;
1870 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1871 won't be accurate, so use a new target. Do this also if target is not
1872 a REG, first because having a register instead may open optimization
1873 opportunities, and second because if target and op0 happen to be MEMs
1874 designating the same location, we would risk clobbering it too early
1875 in the code sequence we generate below. */
1876 if (target == 0 || target == op0 || target == op1 || ! REG_P (target))
1877 target = gen_reg_rtx (mode);
1879 start_sequence ();
1881 shift_count = INTVAL (op1);
1883 /* OUTOF_* is the word we are shifting bits away from, and
1884 INTO_* is the word that we are shifting bits towards, thus
1885 they differ depending on the direction of the shift and
1886 WORDS_BIG_ENDIAN. */
1888 left_shift = (binoptab == rotl_optab);
1889 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1891 outof_target = operand_subword (target, outof_word, 1, mode);
1892 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1894 outof_input = operand_subword_force (op0, outof_word, mode);
1895 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1897 if (shift_count == BITS_PER_WORD)
1899 /* This is just a word swap. */
1900 emit_move_insn (outof_target, into_input);
1901 emit_move_insn (into_target, outof_input);
1902 inter = const0_rtx;
1904 else
1906 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1907 rtx first_shift_count, second_shift_count;
1908 optab reverse_unsigned_shift, unsigned_shift;
1910 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1911 ? lshr_optab : ashl_optab);
1913 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1914 ? ashl_optab : lshr_optab);
1916 if (shift_count > BITS_PER_WORD)
1918 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1919 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1921 else
1923 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1924 second_shift_count = GEN_INT (shift_count);
1927 into_temp1 = expand_binop (word_mode, unsigned_shift,
1928 outof_input, first_shift_count,
1929 NULL_RTX, unsignedp, next_methods);
1930 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1931 into_input, second_shift_count,
1932 NULL_RTX, unsignedp, next_methods);
1934 if (into_temp1 != 0 && into_temp2 != 0)
1935 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1936 into_target, unsignedp, next_methods);
1937 else
1938 inter = 0;
1940 if (inter != 0 && inter != into_target)
1941 emit_move_insn (into_target, inter);
1943 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1944 into_input, first_shift_count,
1945 NULL_RTX, unsignedp, next_methods);
1946 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1947 outof_input, second_shift_count,
1948 NULL_RTX, unsignedp, next_methods);
1950 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1951 inter = expand_binop (word_mode, ior_optab,
1952 outof_temp1, outof_temp2,
1953 outof_target, unsignedp, next_methods);
1955 if (inter != 0 && inter != outof_target)
1956 emit_move_insn (outof_target, inter);
1959 insns = get_insns ();
1960 end_sequence ();
1962 if (inter != 0)
1964 emit_insn (insns);
1965 return target;
1969 /* These can be done a word at a time by propagating carries. */
1970 if ((binoptab == add_optab || binoptab == sub_optab)
1971 && mclass == MODE_INT
1972 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1973 && optab_handler (binoptab, word_mode)->insn_code != CODE_FOR_nothing)
1975 unsigned int i;
1976 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1977 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1978 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1979 rtx xop0, xop1, xtarget;
1981 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1982 value is one of those, use it. Otherwise, use 1 since it is the
1983 one easiest to get. */
1984 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1985 int normalizep = STORE_FLAG_VALUE;
1986 #else
1987 int normalizep = 1;
1988 #endif
1990 /* Prepare the operands. */
1991 xop0 = force_reg (mode, op0);
1992 xop1 = force_reg (mode, op1);
1994 xtarget = gen_reg_rtx (mode);
1996 if (target == 0 || !REG_P (target))
1997 target = xtarget;
1999 /* Indicate for flow that the entire target reg is being set. */
2000 if (REG_P (target))
2001 emit_clobber (xtarget);
2003 /* Do the actual arithmetic. */
2004 for (i = 0; i < nwords; i++)
2006 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
2007 rtx target_piece = operand_subword (xtarget, index, 1, mode);
2008 rtx op0_piece = operand_subword_force (xop0, index, mode);
2009 rtx op1_piece = operand_subword_force (xop1, index, mode);
2010 rtx x;
2012 /* Main add/subtract of the input operands. */
2013 x = expand_binop (word_mode, binoptab,
2014 op0_piece, op1_piece,
2015 target_piece, unsignedp, next_methods);
2016 if (x == 0)
2017 break;
2019 if (i + 1 < nwords)
2021 /* Store carry from main add/subtract. */
2022 carry_out = gen_reg_rtx (word_mode);
2023 carry_out = emit_store_flag_force (carry_out,
2024 (binoptab == add_optab
2025 ? LT : GT),
2026 x, op0_piece,
2027 word_mode, 1, normalizep);
2030 if (i > 0)
2032 rtx newx;
2034 /* Add/subtract previous carry to main result. */
2035 newx = expand_binop (word_mode,
2036 normalizep == 1 ? binoptab : otheroptab,
2037 x, carry_in,
2038 NULL_RTX, 1, next_methods);
2040 if (i + 1 < nwords)
2042 /* Get out carry from adding/subtracting carry in. */
2043 rtx carry_tmp = gen_reg_rtx (word_mode);
2044 carry_tmp = emit_store_flag_force (carry_tmp,
2045 (binoptab == add_optab
2046 ? LT : GT),
2047 newx, x,
2048 word_mode, 1, normalizep);
2050 /* Logical-ior the two poss. carry together. */
2051 carry_out = expand_binop (word_mode, ior_optab,
2052 carry_out, carry_tmp,
2053 carry_out, 0, next_methods);
2054 if (carry_out == 0)
2055 break;
2057 emit_move_insn (target_piece, newx);
2059 else
2061 if (x != target_piece)
2062 emit_move_insn (target_piece, x);
2065 carry_in = carry_out;
2068 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2070 if (optab_handler (mov_optab, mode)->insn_code != CODE_FOR_nothing
2071 || ! rtx_equal_p (target, xtarget))
2073 rtx temp = emit_move_insn (target, xtarget);
2075 set_unique_reg_note (temp,
2076 REG_EQUAL,
2077 gen_rtx_fmt_ee (binoptab->code, mode,
2078 copy_rtx (xop0),
2079 copy_rtx (xop1)));
2081 else
2082 target = xtarget;
2084 return target;
2087 else
2088 delete_insns_since (last);
2091 /* Attempt to synthesize double word multiplies using a sequence of word
2092 mode multiplications. We first attempt to generate a sequence using a
2093 more efficient unsigned widening multiply, and if that fails we then
2094 try using a signed widening multiply. */
2096 if (binoptab == smul_optab
2097 && mclass == MODE_INT
2098 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2099 && optab_handler (smul_optab, word_mode)->insn_code != CODE_FOR_nothing
2100 && optab_handler (add_optab, word_mode)->insn_code != CODE_FOR_nothing)
2102 rtx product = NULL_RTX;
2104 if (optab_handler (umul_widen_optab, mode)->insn_code
2105 != CODE_FOR_nothing)
2107 product = expand_doubleword_mult (mode, op0, op1, target,
2108 true, methods);
2109 if (!product)
2110 delete_insns_since (last);
2113 if (product == NULL_RTX
2114 && optab_handler (smul_widen_optab, mode)->insn_code
2115 != CODE_FOR_nothing)
2117 product = expand_doubleword_mult (mode, op0, op1, target,
2118 false, methods);
2119 if (!product)
2120 delete_insns_since (last);
2123 if (product != NULL_RTX)
2125 if (optab_handler (mov_optab, mode)->insn_code != CODE_FOR_nothing)
2127 temp = emit_move_insn (target ? target : product, product);
2128 set_unique_reg_note (temp,
2129 REG_EQUAL,
2130 gen_rtx_fmt_ee (MULT, mode,
2131 copy_rtx (op0),
2132 copy_rtx (op1)));
2134 return product;
2138 /* It can't be open-coded in this mode.
2139 Use a library call if one is available and caller says that's ok. */
2141 libfunc = optab_libfunc (binoptab, mode);
2142 if (libfunc
2143 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2145 rtx insns;
2146 rtx op1x = op1;
2147 enum machine_mode op1_mode = mode;
2148 rtx value;
2150 start_sequence ();
2152 if (shift_optab_p (binoptab))
2154 op1_mode = targetm.libgcc_shift_count_mode ();
2155 /* Specify unsigned here,
2156 since negative shift counts are meaningless. */
2157 op1x = convert_to_mode (op1_mode, op1, 1);
2160 if (GET_MODE (op0) != VOIDmode
2161 && GET_MODE (op0) != mode)
2162 op0 = convert_to_mode (mode, op0, unsignedp);
2164 /* Pass 1 for NO_QUEUE so we don't lose any increments
2165 if the libcall is cse'd or moved. */
2166 value = emit_library_call_value (libfunc,
2167 NULL_RTX, LCT_CONST, mode, 2,
2168 op0, mode, op1x, op1_mode);
2170 insns = get_insns ();
2171 end_sequence ();
2173 target = gen_reg_rtx (mode);
2174 emit_libcall_block (insns, target, value,
2175 gen_rtx_fmt_ee (binoptab->code, mode, op0, op1));
2177 return target;
2180 delete_insns_since (last);
2182 /* It can't be done in this mode. Can we do it in a wider mode? */
2184 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2185 || methods == OPTAB_MUST_WIDEN))
2187 /* Caller says, don't even try. */
2188 delete_insns_since (entry_last);
2189 return 0;
2192 /* Compute the value of METHODS to pass to recursive calls.
2193 Don't allow widening to be tried recursively. */
2195 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2197 /* Look for a wider mode of the same class for which it appears we can do
2198 the operation. */
2200 if (CLASS_HAS_WIDER_MODES_P (mclass))
2202 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2203 wider_mode != VOIDmode;
2204 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2206 if ((optab_handler (binoptab, wider_mode)->insn_code
2207 != CODE_FOR_nothing)
2208 || (methods == OPTAB_LIB
2209 && optab_libfunc (binoptab, wider_mode)))
2211 rtx xop0 = op0, xop1 = op1;
2212 int no_extend = 0;
2214 /* For certain integer operations, we need not actually extend
2215 the narrow operands, as long as we will truncate
2216 the results to the same narrowness. */
2218 if ((binoptab == ior_optab || binoptab == and_optab
2219 || binoptab == xor_optab
2220 || binoptab == add_optab || binoptab == sub_optab
2221 || binoptab == smul_optab || binoptab == ashl_optab)
2222 && mclass == MODE_INT)
2223 no_extend = 1;
2225 xop0 = widen_operand (xop0, wider_mode, mode,
2226 unsignedp, no_extend);
2228 /* The second operand of a shift must always be extended. */
2229 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2230 no_extend && binoptab != ashl_optab);
2232 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2233 unsignedp, methods);
2234 if (temp)
2236 if (mclass != MODE_INT
2237 || !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
2238 GET_MODE_BITSIZE (wider_mode)))
2240 if (target == 0)
2241 target = gen_reg_rtx (mode);
2242 convert_move (target, temp, 0);
2243 return target;
2245 else
2246 return gen_lowpart (mode, temp);
2248 else
2249 delete_insns_since (last);
2254 delete_insns_since (entry_last);
2255 return 0;
2258 /* Expand a binary operator which has both signed and unsigned forms.
2259 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2260 signed operations.
2262 If we widen unsigned operands, we may use a signed wider operation instead
2263 of an unsigned wider operation, since the result would be the same. */
2266 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
2267 rtx op0, rtx op1, rtx target, int unsignedp,
2268 enum optab_methods methods)
2270 rtx temp;
2271 optab direct_optab = unsignedp ? uoptab : soptab;
2272 struct optab wide_soptab;
2274 /* Do it without widening, if possible. */
2275 temp = expand_binop (mode, direct_optab, op0, op1, target,
2276 unsignedp, OPTAB_DIRECT);
2277 if (temp || methods == OPTAB_DIRECT)
2278 return temp;
2280 /* Try widening to a signed int. Make a fake signed optab that
2281 hides any signed insn for direct use. */
2282 wide_soptab = *soptab;
2283 optab_handler (&wide_soptab, mode)->insn_code = CODE_FOR_nothing;
2284 /* We don't want to generate new hash table entries from this fake
2285 optab. */
2286 wide_soptab.libcall_gen = NULL;
2288 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2289 unsignedp, OPTAB_WIDEN);
2291 /* For unsigned operands, try widening to an unsigned int. */
2292 if (temp == 0 && unsignedp)
2293 temp = expand_binop (mode, uoptab, op0, op1, target,
2294 unsignedp, OPTAB_WIDEN);
2295 if (temp || methods == OPTAB_WIDEN)
2296 return temp;
2298 /* Use the right width lib call if that exists. */
2299 temp = expand_binop (mode, direct_optab, op0, op1, target, unsignedp, OPTAB_LIB);
2300 if (temp || methods == OPTAB_LIB)
2301 return temp;
2303 /* Must widen and use a lib call, use either signed or unsigned. */
2304 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2305 unsignedp, methods);
2306 if (temp != 0)
2307 return temp;
2308 if (unsignedp)
2309 return expand_binop (mode, uoptab, op0, op1, target,
2310 unsignedp, methods);
2311 return 0;
2314 /* Generate code to perform an operation specified by UNOPPTAB
2315 on operand OP0, with two results to TARG0 and TARG1.
2316 We assume that the order of the operands for the instruction
2317 is TARG0, TARG1, OP0.
2319 Either TARG0 or TARG1 may be zero, but what that means is that
2320 the result is not actually wanted. We will generate it into
2321 a dummy pseudo-reg and discard it. They may not both be zero.
2323 Returns 1 if this operation can be performed; 0 if not. */
2326 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2327 int unsignedp)
2329 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2330 enum mode_class mclass;
2331 enum machine_mode wider_mode;
2332 rtx entry_last = get_last_insn ();
2333 rtx last;
2335 mclass = GET_MODE_CLASS (mode);
2337 if (!targ0)
2338 targ0 = gen_reg_rtx (mode);
2339 if (!targ1)
2340 targ1 = gen_reg_rtx (mode);
2342 /* Record where to go back to if we fail. */
2343 last = get_last_insn ();
2345 if (optab_handler (unoptab, mode)->insn_code != CODE_FOR_nothing)
2347 int icode = (int) optab_handler (unoptab, mode)->insn_code;
2348 enum machine_mode mode0 = insn_data[icode].operand[2].mode;
2349 rtx pat;
2350 rtx xop0 = op0;
2352 if (GET_MODE (xop0) != VOIDmode
2353 && GET_MODE (xop0) != mode0)
2354 xop0 = convert_to_mode (mode0, xop0, unsignedp);
2356 /* Now, if insn doesn't accept these operands, put them into pseudos. */
2357 if (!insn_data[icode].operand[2].predicate (xop0, mode0))
2358 xop0 = copy_to_mode_reg (mode0, xop0);
2360 /* We could handle this, but we should always be called with a pseudo
2361 for our targets and all insns should take them as outputs. */
2362 gcc_assert (insn_data[icode].operand[0].predicate (targ0, mode));
2363 gcc_assert (insn_data[icode].operand[1].predicate (targ1, mode));
2365 pat = GEN_FCN (icode) (targ0, targ1, xop0);
2366 if (pat)
2368 emit_insn (pat);
2369 return 1;
2371 else
2372 delete_insns_since (last);
2375 /* It can't be done in this mode. Can we do it in a wider mode? */
2377 if (CLASS_HAS_WIDER_MODES_P (mclass))
2379 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2380 wider_mode != VOIDmode;
2381 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2383 if (optab_handler (unoptab, wider_mode)->insn_code
2384 != CODE_FOR_nothing)
2386 rtx t0 = gen_reg_rtx (wider_mode);
2387 rtx t1 = gen_reg_rtx (wider_mode);
2388 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2390 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2392 convert_move (targ0, t0, unsignedp);
2393 convert_move (targ1, t1, unsignedp);
2394 return 1;
2396 else
2397 delete_insns_since (last);
2402 delete_insns_since (entry_last);
2403 return 0;
2406 /* Generate code to perform an operation specified by BINOPTAB
2407 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2408 We assume that the order of the operands for the instruction
2409 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2410 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2412 Either TARG0 or TARG1 may be zero, but what that means is that
2413 the result is not actually wanted. We will generate it into
2414 a dummy pseudo-reg and discard it. They may not both be zero.
2416 Returns 1 if this operation can be performed; 0 if not. */
2419 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2420 int unsignedp)
2422 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2423 enum mode_class mclass;
2424 enum machine_mode wider_mode;
2425 rtx entry_last = get_last_insn ();
2426 rtx last;
2428 mclass = GET_MODE_CLASS (mode);
2430 if (!targ0)
2431 targ0 = gen_reg_rtx (mode);
2432 if (!targ1)
2433 targ1 = gen_reg_rtx (mode);
2435 /* Record where to go back to if we fail. */
2436 last = get_last_insn ();
2438 if (optab_handler (binoptab, mode)->insn_code != CODE_FOR_nothing)
2440 int icode = (int) optab_handler (binoptab, mode)->insn_code;
2441 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2442 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
2443 rtx pat;
2444 rtx xop0 = op0, xop1 = op1;
2446 /* If we are optimizing, force expensive constants into a register. */
2447 xop0 = avoid_expensive_constant (mode0, binoptab, xop0, unsignedp);
2448 xop1 = avoid_expensive_constant (mode1, binoptab, xop1, unsignedp);
2450 /* In case the insn wants input operands in modes different from
2451 those of the actual operands, convert the operands. It would
2452 seem that we don't need to convert CONST_INTs, but we do, so
2453 that they're properly zero-extended, sign-extended or truncated
2454 for their mode. */
2456 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
2457 xop0 = convert_modes (mode0,
2458 GET_MODE (op0) != VOIDmode
2459 ? GET_MODE (op0)
2460 : mode,
2461 xop0, unsignedp);
2463 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
2464 xop1 = convert_modes (mode1,
2465 GET_MODE (op1) != VOIDmode
2466 ? GET_MODE (op1)
2467 : mode,
2468 xop1, unsignedp);
2470 /* Now, if insn doesn't accept these operands, put them into pseudos. */
2471 if (!insn_data[icode].operand[1].predicate (xop0, mode0))
2472 xop0 = copy_to_mode_reg (mode0, xop0);
2474 if (!insn_data[icode].operand[2].predicate (xop1, mode1))
2475 xop1 = copy_to_mode_reg (mode1, xop1);
2477 /* We could handle this, but we should always be called with a pseudo
2478 for our targets and all insns should take them as outputs. */
2479 gcc_assert (insn_data[icode].operand[0].predicate (targ0, mode));
2480 gcc_assert (insn_data[icode].operand[3].predicate (targ1, mode));
2482 pat = GEN_FCN (icode) (targ0, xop0, xop1, targ1);
2483 if (pat)
2485 emit_insn (pat);
2486 return 1;
2488 else
2489 delete_insns_since (last);
2492 /* It can't be done in this mode. Can we do it in a wider mode? */
2494 if (CLASS_HAS_WIDER_MODES_P (mclass))
2496 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2497 wider_mode != VOIDmode;
2498 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2500 if (optab_handler (binoptab, wider_mode)->insn_code
2501 != CODE_FOR_nothing)
2503 rtx t0 = gen_reg_rtx (wider_mode);
2504 rtx t1 = gen_reg_rtx (wider_mode);
2505 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2506 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2508 if (expand_twoval_binop (binoptab, cop0, cop1,
2509 t0, t1, unsignedp))
2511 convert_move (targ0, t0, unsignedp);
2512 convert_move (targ1, t1, unsignedp);
2513 return 1;
2515 else
2516 delete_insns_since (last);
2521 delete_insns_since (entry_last);
2522 return 0;
2525 /* Expand the two-valued library call indicated by BINOPTAB, but
2526 preserve only one of the values. If TARG0 is non-NULL, the first
2527 value is placed into TARG0; otherwise the second value is placed
2528 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2529 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2530 This routine assumes that the value returned by the library call is
2531 as if the return value was of an integral mode twice as wide as the
2532 mode of OP0. Returns 1 if the call was successful. */
2534 bool
2535 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2536 rtx targ0, rtx targ1, enum rtx_code code)
2538 enum machine_mode mode;
2539 enum machine_mode libval_mode;
2540 rtx libval;
2541 rtx insns;
2542 rtx libfunc;
2544 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2545 gcc_assert (!targ0 != !targ1);
2547 mode = GET_MODE (op0);
2548 libfunc = optab_libfunc (binoptab, mode);
2549 if (!libfunc)
2550 return false;
2552 /* The value returned by the library function will have twice as
2553 many bits as the nominal MODE. */
2554 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2555 MODE_INT);
2556 start_sequence ();
2557 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2558 libval_mode, 2,
2559 op0, mode,
2560 op1, mode);
2561 /* Get the part of VAL containing the value that we want. */
2562 libval = simplify_gen_subreg (mode, libval, libval_mode,
2563 targ0 ? 0 : GET_MODE_SIZE (mode));
2564 insns = get_insns ();
2565 end_sequence ();
2566 /* Move the into the desired location. */
2567 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2568 gen_rtx_fmt_ee (code, mode, op0, op1));
2570 return true;
2574 /* Wrapper around expand_unop which takes an rtx code to specify
2575 the operation to perform, not an optab pointer. All other
2576 arguments are the same. */
2578 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2579 rtx target, int unsignedp)
2581 optab unop = code_to_optab[(int) code];
2582 gcc_assert (unop);
2584 return expand_unop (mode, unop, op0, target, unsignedp);
2587 /* Try calculating
2588 (clz:narrow x)
2590 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)). */
2591 static rtx
2592 widen_clz (enum machine_mode mode, rtx op0, rtx target)
2594 enum mode_class mclass = GET_MODE_CLASS (mode);
2595 if (CLASS_HAS_WIDER_MODES_P (mclass))
2597 enum machine_mode wider_mode;
2598 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2599 wider_mode != VOIDmode;
2600 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2602 if (optab_handler (clz_optab, wider_mode)->insn_code
2603 != CODE_FOR_nothing)
2605 rtx xop0, temp, last;
2607 last = get_last_insn ();
2609 if (target == 0)
2610 target = gen_reg_rtx (mode);
2611 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2612 temp = expand_unop (wider_mode, clz_optab, xop0, NULL_RTX, true);
2613 if (temp != 0)
2614 temp = expand_binop (wider_mode, sub_optab, temp,
2615 GEN_INT (GET_MODE_BITSIZE (wider_mode)
2616 - GET_MODE_BITSIZE (mode)),
2617 target, true, OPTAB_DIRECT);
2618 if (temp == 0)
2619 delete_insns_since (last);
2621 return temp;
2625 return 0;
2628 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2629 quantities, choosing which based on whether the high word is nonzero. */
2630 static rtx
2631 expand_doubleword_clz (enum machine_mode mode, rtx op0, rtx target)
2633 rtx xop0 = force_reg (mode, op0);
2634 rtx subhi = gen_highpart (word_mode, xop0);
2635 rtx sublo = gen_lowpart (word_mode, xop0);
2636 rtx hi0_label = gen_label_rtx ();
2637 rtx after_label = gen_label_rtx ();
2638 rtx seq, temp, result;
2640 /* If we were not given a target, use a word_mode register, not a
2641 'mode' register. The result will fit, and nobody is expecting
2642 anything bigger (the return type of __builtin_clz* is int). */
2643 if (!target)
2644 target = gen_reg_rtx (word_mode);
2646 /* In any case, write to a word_mode scratch in both branches of the
2647 conditional, so we can ensure there is a single move insn setting
2648 'target' to tag a REG_EQUAL note on. */
2649 result = gen_reg_rtx (word_mode);
2651 start_sequence ();
2653 /* If the high word is not equal to zero,
2654 then clz of the full value is clz of the high word. */
2655 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2656 word_mode, true, hi0_label);
2658 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2659 if (!temp)
2660 goto fail;
2662 if (temp != result)
2663 convert_move (result, temp, true);
2665 emit_jump_insn (gen_jump (after_label));
2666 emit_barrier ();
2668 /* Else clz of the full value is clz of the low word plus the number
2669 of bits in the high word. */
2670 emit_label (hi0_label);
2672 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2673 if (!temp)
2674 goto fail;
2675 temp = expand_binop (word_mode, add_optab, temp,
2676 GEN_INT (GET_MODE_BITSIZE (word_mode)),
2677 result, true, OPTAB_DIRECT);
2678 if (!temp)
2679 goto fail;
2680 if (temp != result)
2681 convert_move (result, temp, true);
2683 emit_label (after_label);
2684 convert_move (target, result, true);
2686 seq = get_insns ();
2687 end_sequence ();
2689 add_equal_note (seq, target, CLZ, xop0, 0);
2690 emit_insn (seq);
2691 return target;
2693 fail:
2694 end_sequence ();
2695 return 0;
2698 /* Try calculating
2699 (bswap:narrow x)
2701 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2702 static rtx
2703 widen_bswap (enum machine_mode mode, rtx op0, rtx target)
2705 enum mode_class mclass = GET_MODE_CLASS (mode);
2706 enum machine_mode wider_mode;
2707 rtx x, last;
2709 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2710 return NULL_RTX;
2712 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2713 wider_mode != VOIDmode;
2714 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2715 if (optab_handler (bswap_optab, wider_mode)->insn_code != CODE_FOR_nothing)
2716 goto found;
2717 return NULL_RTX;
2719 found:
2720 last = get_last_insn ();
2722 x = widen_operand (op0, wider_mode, mode, true, true);
2723 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2725 if (x != 0)
2726 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2727 size_int (GET_MODE_BITSIZE (wider_mode)
2728 - GET_MODE_BITSIZE (mode)),
2729 NULL_RTX, true);
2731 if (x != 0)
2733 if (target == 0)
2734 target = gen_reg_rtx (mode);
2735 emit_move_insn (target, gen_lowpart (mode, x));
2737 else
2738 delete_insns_since (last);
2740 return target;
2743 /* Try calculating bswap as two bswaps of two word-sized operands. */
2745 static rtx
2746 expand_doubleword_bswap (enum machine_mode mode, rtx op, rtx target)
2748 rtx t0, t1;
2750 t1 = expand_unop (word_mode, bswap_optab,
2751 operand_subword_force (op, 0, mode), NULL_RTX, true);
2752 t0 = expand_unop (word_mode, bswap_optab,
2753 operand_subword_force (op, 1, mode), NULL_RTX, true);
2755 if (target == 0)
2756 target = gen_reg_rtx (mode);
2757 if (REG_P (target))
2758 emit_clobber (target);
2759 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2760 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2762 return target;
2765 /* Try calculating (parity x) as (and (popcount x) 1), where
2766 popcount can also be done in a wider mode. */
2767 static rtx
2768 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2770 enum mode_class mclass = GET_MODE_CLASS (mode);
2771 if (CLASS_HAS_WIDER_MODES_P (mclass))
2773 enum machine_mode wider_mode;
2774 for (wider_mode = mode; wider_mode != VOIDmode;
2775 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2777 if (optab_handler (popcount_optab, wider_mode)->insn_code
2778 != CODE_FOR_nothing)
2780 rtx xop0, temp, last;
2782 last = get_last_insn ();
2784 if (target == 0)
2785 target = gen_reg_rtx (mode);
2786 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2787 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2788 true);
2789 if (temp != 0)
2790 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2791 target, true, OPTAB_DIRECT);
2792 if (temp == 0)
2793 delete_insns_since (last);
2795 return temp;
2799 return 0;
2802 /* Try calculating ctz(x) as K - clz(x & -x) ,
2803 where K is GET_MODE_BITSIZE(mode) - 1.
2805 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2806 don't have to worry about what the hardware does in that case. (If
2807 the clz instruction produces the usual value at 0, which is K, the
2808 result of this code sequence will be -1; expand_ffs, below, relies
2809 on this. It might be nice to have it be K instead, for consistency
2810 with the (very few) processors that provide a ctz with a defined
2811 value, but that would take one more instruction, and it would be
2812 less convenient for expand_ffs anyway. */
2814 static rtx
2815 expand_ctz (enum machine_mode mode, rtx op0, rtx target)
2817 rtx seq, temp;
2819 if (optab_handler (clz_optab, mode)->insn_code == CODE_FOR_nothing)
2820 return 0;
2822 start_sequence ();
2824 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2825 if (temp)
2826 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2827 true, OPTAB_DIRECT);
2828 if (temp)
2829 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2830 if (temp)
2831 temp = expand_binop (mode, sub_optab, GEN_INT (GET_MODE_BITSIZE (mode) - 1),
2832 temp, target,
2833 true, OPTAB_DIRECT);
2834 if (temp == 0)
2836 end_sequence ();
2837 return 0;
2840 seq = get_insns ();
2841 end_sequence ();
2843 add_equal_note (seq, temp, CTZ, op0, 0);
2844 emit_insn (seq);
2845 return temp;
2849 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2850 else with the sequence used by expand_clz.
2852 The ffs builtin promises to return zero for a zero value and ctz/clz
2853 may have an undefined value in that case. If they do not give us a
2854 convenient value, we have to generate a test and branch. */
2855 static rtx
2856 expand_ffs (enum machine_mode mode, rtx op0, rtx target)
2858 HOST_WIDE_INT val = 0;
2859 bool defined_at_zero = false;
2860 rtx temp, seq;
2862 if (optab_handler (ctz_optab, mode)->insn_code != CODE_FOR_nothing)
2864 start_sequence ();
2866 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2867 if (!temp)
2868 goto fail;
2870 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2872 else if (optab_handler (clz_optab, mode)->insn_code != CODE_FOR_nothing)
2874 start_sequence ();
2875 temp = expand_ctz (mode, op0, 0);
2876 if (!temp)
2877 goto fail;
2879 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2881 defined_at_zero = true;
2882 val = (GET_MODE_BITSIZE (mode) - 1) - val;
2885 else
2886 return 0;
2888 if (defined_at_zero && val == -1)
2889 /* No correction needed at zero. */;
2890 else
2892 /* We don't try to do anything clever with the situation found
2893 on some processors (eg Alpha) where ctz(0:mode) ==
2894 bitsize(mode). If someone can think of a way to send N to -1
2895 and leave alone all values in the range 0..N-1 (where N is a
2896 power of two), cheaper than this test-and-branch, please add it.
2898 The test-and-branch is done after the operation itself, in case
2899 the operation sets condition codes that can be recycled for this.
2900 (This is true on i386, for instance.) */
2902 rtx nonzero_label = gen_label_rtx ();
2903 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2904 mode, true, nonzero_label);
2906 convert_move (temp, GEN_INT (-1), false);
2907 emit_label (nonzero_label);
2910 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2911 to produce a value in the range 0..bitsize. */
2912 temp = expand_binop (mode, add_optab, temp, GEN_INT (1),
2913 target, false, OPTAB_DIRECT);
2914 if (!temp)
2915 goto fail;
2917 seq = get_insns ();
2918 end_sequence ();
2920 add_equal_note (seq, temp, FFS, op0, 0);
2921 emit_insn (seq);
2922 return temp;
2924 fail:
2925 end_sequence ();
2926 return 0;
2929 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2930 conditions, VAL may already be a SUBREG against which we cannot generate
2931 a further SUBREG. In this case, we expect forcing the value into a
2932 register will work around the situation. */
2934 static rtx
2935 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2936 enum machine_mode imode)
2938 rtx ret;
2939 ret = lowpart_subreg (omode, val, imode);
2940 if (ret == NULL)
2942 val = force_reg (imode, val);
2943 ret = lowpart_subreg (omode, val, imode);
2944 gcc_assert (ret != NULL);
2946 return ret;
2949 /* Expand a floating point absolute value or negation operation via a
2950 logical operation on the sign bit. */
2952 static rtx
2953 expand_absneg_bit (enum rtx_code code, enum machine_mode mode,
2954 rtx op0, rtx target)
2956 const struct real_format *fmt;
2957 int bitpos, word, nwords, i;
2958 enum machine_mode imode;
2959 HOST_WIDE_INT hi, lo;
2960 rtx temp, insns;
2962 /* The format has to have a simple sign bit. */
2963 fmt = REAL_MODE_FORMAT (mode);
2964 if (fmt == NULL)
2965 return NULL_RTX;
2967 bitpos = fmt->signbit_rw;
2968 if (bitpos < 0)
2969 return NULL_RTX;
2971 /* Don't create negative zeros if the format doesn't support them. */
2972 if (code == NEG && !fmt->has_signed_zero)
2973 return NULL_RTX;
2975 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2977 imode = int_mode_for_mode (mode);
2978 if (imode == BLKmode)
2979 return NULL_RTX;
2980 word = 0;
2981 nwords = 1;
2983 else
2985 imode = word_mode;
2987 if (FLOAT_WORDS_BIG_ENDIAN)
2988 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2989 else
2990 word = bitpos / BITS_PER_WORD;
2991 bitpos = bitpos % BITS_PER_WORD;
2992 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2995 if (bitpos < HOST_BITS_PER_WIDE_INT)
2997 hi = 0;
2998 lo = (HOST_WIDE_INT) 1 << bitpos;
3000 else
3002 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
3003 lo = 0;
3005 if (code == ABS)
3006 lo = ~lo, hi = ~hi;
3008 if (target == 0 || target == op0)
3009 target = gen_reg_rtx (mode);
3011 if (nwords > 1)
3013 start_sequence ();
3015 for (i = 0; i < nwords; ++i)
3017 rtx targ_piece = operand_subword (target, i, 1, mode);
3018 rtx op0_piece = operand_subword_force (op0, i, mode);
3020 if (i == word)
3022 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
3023 op0_piece,
3024 immed_double_const (lo, hi, imode),
3025 targ_piece, 1, OPTAB_LIB_WIDEN);
3026 if (temp != targ_piece)
3027 emit_move_insn (targ_piece, temp);
3029 else
3030 emit_move_insn (targ_piece, op0_piece);
3033 insns = get_insns ();
3034 end_sequence ();
3036 emit_insn (insns);
3038 else
3040 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
3041 gen_lowpart (imode, op0),
3042 immed_double_const (lo, hi, imode),
3043 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3044 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3046 set_unique_reg_note (get_last_insn (), REG_EQUAL,
3047 gen_rtx_fmt_e (code, mode, copy_rtx (op0)));
3050 return target;
3053 /* As expand_unop, but will fail rather than attempt the operation in a
3054 different mode or with a libcall. */
3055 static rtx
3056 expand_unop_direct (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
3057 int unsignedp)
3059 if (optab_handler (unoptab, mode)->insn_code != CODE_FOR_nothing)
3061 int icode = (int) optab_handler (unoptab, mode)->insn_code;
3062 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3063 rtx xop0 = op0;
3064 rtx last = get_last_insn ();
3065 rtx pat, temp;
3067 if (target)
3068 temp = target;
3069 else
3070 temp = gen_reg_rtx (mode);
3072 if (GET_MODE (xop0) != VOIDmode
3073 && GET_MODE (xop0) != mode0)
3074 xop0 = convert_to_mode (mode0, xop0, unsignedp);
3076 /* Now, if insn doesn't accept our operand, put it into a pseudo. */
3078 if (!insn_data[icode].operand[1].predicate (xop0, mode0))
3079 xop0 = copy_to_mode_reg (mode0, xop0);
3081 if (!insn_data[icode].operand[0].predicate (temp, mode))
3082 temp = gen_reg_rtx (mode);
3084 pat = GEN_FCN (icode) (temp, xop0);
3085 if (pat)
3087 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3088 && ! add_equal_note (pat, temp, unoptab->code, xop0, NULL_RTX))
3090 delete_insns_since (last);
3091 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3094 emit_insn (pat);
3096 return temp;
3098 else
3099 delete_insns_since (last);
3101 return 0;
3104 /* Generate code to perform an operation specified by UNOPTAB
3105 on operand OP0, with result having machine-mode MODE.
3107 UNSIGNEDP is for the case where we have to widen the operands
3108 to perform the operation. It says to use zero-extension.
3110 If TARGET is nonzero, the value
3111 is generated there, if it is convenient to do so.
3112 In all cases an rtx is returned for the locus of the value;
3113 this may or may not be TARGET. */
3116 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
3117 int unsignedp)
3119 enum mode_class mclass = GET_MODE_CLASS (mode);
3120 enum machine_mode wider_mode;
3121 rtx temp;
3122 rtx libfunc;
3124 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3125 if (temp)
3126 return temp;
3128 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3130 /* Widening (or narrowing) clz needs special treatment. */
3131 if (unoptab == clz_optab)
3133 temp = widen_clz (mode, op0, target);
3134 if (temp)
3135 return temp;
3137 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3138 && optab_handler (unoptab, word_mode)->insn_code != CODE_FOR_nothing)
3140 temp = expand_doubleword_clz (mode, op0, target);
3141 if (temp)
3142 return temp;
3145 goto try_libcall;
3148 /* Widening (or narrowing) bswap needs special treatment. */
3149 if (unoptab == bswap_optab)
3151 temp = widen_bswap (mode, op0, target);
3152 if (temp)
3153 return temp;
3155 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3156 && optab_handler (unoptab, word_mode)->insn_code != CODE_FOR_nothing)
3158 temp = expand_doubleword_bswap (mode, op0, target);
3159 if (temp)
3160 return temp;
3163 goto try_libcall;
3166 if (CLASS_HAS_WIDER_MODES_P (mclass))
3167 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3168 wider_mode != VOIDmode;
3169 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3171 if (optab_handler (unoptab, wider_mode)->insn_code != CODE_FOR_nothing)
3173 rtx xop0 = op0;
3174 rtx last = get_last_insn ();
3176 /* For certain operations, we need not actually extend
3177 the narrow operand, as long as we will truncate the
3178 results to the same narrowness. */
3180 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3181 (unoptab == neg_optab
3182 || unoptab == one_cmpl_optab)
3183 && mclass == MODE_INT);
3185 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3186 unsignedp);
3188 if (temp)
3190 if (mclass != MODE_INT
3191 || !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3192 GET_MODE_BITSIZE (wider_mode)))
3194 if (target == 0)
3195 target = gen_reg_rtx (mode);
3196 convert_move (target, temp, 0);
3197 return target;
3199 else
3200 return gen_lowpart (mode, temp);
3202 else
3203 delete_insns_since (last);
3207 /* These can be done a word at a time. */
3208 if (unoptab == one_cmpl_optab
3209 && mclass == MODE_INT
3210 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3211 && optab_handler (unoptab, word_mode)->insn_code != CODE_FOR_nothing)
3213 int i;
3214 rtx insns;
3216 if (target == 0 || target == op0)
3217 target = gen_reg_rtx (mode);
3219 start_sequence ();
3221 /* Do the actual arithmetic. */
3222 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3224 rtx target_piece = operand_subword (target, i, 1, mode);
3225 rtx x = expand_unop (word_mode, unoptab,
3226 operand_subword_force (op0, i, mode),
3227 target_piece, unsignedp);
3229 if (target_piece != x)
3230 emit_move_insn (target_piece, x);
3233 insns = get_insns ();
3234 end_sequence ();
3236 emit_insn (insns);
3237 return target;
3240 if (unoptab->code == NEG)
3242 /* Try negating floating point values by flipping the sign bit. */
3243 if (SCALAR_FLOAT_MODE_P (mode))
3245 temp = expand_absneg_bit (NEG, mode, op0, target);
3246 if (temp)
3247 return temp;
3250 /* If there is no negation pattern, and we have no negative zero,
3251 try subtracting from zero. */
3252 if (!HONOR_SIGNED_ZEROS (mode))
3254 temp = expand_binop (mode, (unoptab == negv_optab
3255 ? subv_optab : sub_optab),
3256 CONST0_RTX (mode), op0, target,
3257 unsignedp, OPTAB_DIRECT);
3258 if (temp)
3259 return temp;
3263 /* Try calculating parity (x) as popcount (x) % 2. */
3264 if (unoptab == parity_optab)
3266 temp = expand_parity (mode, op0, target);
3267 if (temp)
3268 return temp;
3271 /* Try implementing ffs (x) in terms of clz (x). */
3272 if (unoptab == ffs_optab)
3274 temp = expand_ffs (mode, op0, target);
3275 if (temp)
3276 return temp;
3279 /* Try implementing ctz (x) in terms of clz (x). */
3280 if (unoptab == ctz_optab)
3282 temp = expand_ctz (mode, op0, target);
3283 if (temp)
3284 return temp;
3287 try_libcall:
3288 /* Now try a library call in this mode. */
3289 libfunc = optab_libfunc (unoptab, mode);
3290 if (libfunc)
3292 rtx insns;
3293 rtx value;
3294 rtx eq_value;
3295 enum machine_mode outmode = mode;
3297 /* All of these functions return small values. Thus we choose to
3298 have them return something that isn't a double-word. */
3299 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3300 || unoptab == popcount_optab || unoptab == parity_optab)
3301 outmode
3302 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node)));
3304 start_sequence ();
3306 /* Pass 1 for NO_QUEUE so we don't lose any increments
3307 if the libcall is cse'd or moved. */
3308 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3309 1, op0, mode);
3310 insns = get_insns ();
3311 end_sequence ();
3313 target = gen_reg_rtx (outmode);
3314 eq_value = gen_rtx_fmt_e (unoptab->code, mode, op0);
3315 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3316 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3317 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3318 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3319 emit_libcall_block (insns, target, value, eq_value);
3321 return target;
3324 /* It can't be done in this mode. Can we do it in a wider mode? */
3326 if (CLASS_HAS_WIDER_MODES_P (mclass))
3328 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3329 wider_mode != VOIDmode;
3330 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3332 if ((optab_handler (unoptab, wider_mode)->insn_code
3333 != CODE_FOR_nothing)
3334 || optab_libfunc (unoptab, wider_mode))
3336 rtx xop0 = op0;
3337 rtx last = get_last_insn ();
3339 /* For certain operations, we need not actually extend
3340 the narrow operand, as long as we will truncate the
3341 results to the same narrowness. */
3343 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3344 (unoptab == neg_optab
3345 || unoptab == one_cmpl_optab)
3346 && mclass == MODE_INT);
3348 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3349 unsignedp);
3351 /* If we are generating clz using wider mode, adjust the
3352 result. */
3353 if (unoptab == clz_optab && temp != 0)
3354 temp = expand_binop (wider_mode, sub_optab, temp,
3355 GEN_INT (GET_MODE_BITSIZE (wider_mode)
3356 - GET_MODE_BITSIZE (mode)),
3357 target, true, OPTAB_DIRECT);
3359 if (temp)
3361 if (mclass != MODE_INT)
3363 if (target == 0)
3364 target = gen_reg_rtx (mode);
3365 convert_move (target, temp, 0);
3366 return target;
3368 else
3369 return gen_lowpart (mode, temp);
3371 else
3372 delete_insns_since (last);
3377 /* One final attempt at implementing negation via subtraction,
3378 this time allowing widening of the operand. */
3379 if (unoptab->code == NEG && !HONOR_SIGNED_ZEROS (mode))
3381 rtx temp;
3382 temp = expand_binop (mode,
3383 unoptab == negv_optab ? subv_optab : sub_optab,
3384 CONST0_RTX (mode), op0,
3385 target, unsignedp, OPTAB_LIB_WIDEN);
3386 if (temp)
3387 return temp;
3390 return 0;
3393 /* Emit code to compute the absolute value of OP0, with result to
3394 TARGET if convenient. (TARGET may be 0.) The return value says
3395 where the result actually is to be found.
3397 MODE is the mode of the operand; the mode of the result is
3398 different but can be deduced from MODE.
3403 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
3404 int result_unsignedp)
3406 rtx temp;
3408 if (! flag_trapv)
3409 result_unsignedp = 1;
3411 /* First try to do it with a special abs instruction. */
3412 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3413 op0, target, 0);
3414 if (temp != 0)
3415 return temp;
3417 /* For floating point modes, try clearing the sign bit. */
3418 if (SCALAR_FLOAT_MODE_P (mode))
3420 temp = expand_absneg_bit (ABS, mode, op0, target);
3421 if (temp)
3422 return temp;
3425 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3426 if (optab_handler (smax_optab, mode)->insn_code != CODE_FOR_nothing
3427 && !HONOR_SIGNED_ZEROS (mode))
3429 rtx last = get_last_insn ();
3431 temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
3432 if (temp != 0)
3433 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3434 OPTAB_WIDEN);
3436 if (temp != 0)
3437 return temp;
3439 delete_insns_since (last);
3442 /* If this machine has expensive jumps, we can do integer absolute
3443 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3444 where W is the width of MODE. */
3446 if (GET_MODE_CLASS (mode) == MODE_INT && BRANCH_COST >= 2)
3448 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3449 size_int (GET_MODE_BITSIZE (mode) - 1),
3450 NULL_RTX, 0);
3452 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3453 OPTAB_LIB_WIDEN);
3454 if (temp != 0)
3455 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3456 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3458 if (temp != 0)
3459 return temp;
3462 return NULL_RTX;
3466 expand_abs (enum machine_mode mode, rtx op0, rtx target,
3467 int result_unsignedp, int safe)
3469 rtx temp, op1;
3471 if (! flag_trapv)
3472 result_unsignedp = 1;
3474 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3475 if (temp != 0)
3476 return temp;
3478 /* If that does not win, use conditional jump and negate. */
3480 /* It is safe to use the target if it is the same
3481 as the source if this is also a pseudo register */
3482 if (op0 == target && REG_P (op0)
3483 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3484 safe = 1;
3486 op1 = gen_label_rtx ();
3487 if (target == 0 || ! safe
3488 || GET_MODE (target) != mode
3489 || (MEM_P (target) && MEM_VOLATILE_P (target))
3490 || (REG_P (target)
3491 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3492 target = gen_reg_rtx (mode);
3494 emit_move_insn (target, op0);
3495 NO_DEFER_POP;
3497 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3498 NULL_RTX, NULL_RTX, op1);
3500 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3501 target, target, 0);
3502 if (op0 != target)
3503 emit_move_insn (target, op0);
3504 emit_label (op1);
3505 OK_DEFER_POP;
3506 return target;
3509 /* A subroutine of expand_copysign, perform the copysign operation using the
3510 abs and neg primitives advertised to exist on the target. The assumption
3511 is that we have a split register file, and leaving op0 in fp registers,
3512 and not playing with subregs so much, will help the register allocator. */
3514 static rtx
3515 expand_copysign_absneg (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3516 int bitpos, bool op0_is_abs)
3518 enum machine_mode imode;
3519 int icode;
3520 rtx sign, label;
3522 if (target == op1)
3523 target = NULL_RTX;
3525 /* Check if the back end provides an insn that handles signbit for the
3526 argument's mode. */
3527 icode = (int) signbit_optab->handlers [(int) mode].insn_code;
3528 if (icode != CODE_FOR_nothing)
3530 imode = insn_data[icode].operand[0].mode;
3531 sign = gen_reg_rtx (imode);
3532 emit_unop_insn (icode, sign, op1, UNKNOWN);
3534 else
3536 HOST_WIDE_INT hi, lo;
3538 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3540 imode = int_mode_for_mode (mode);
3541 if (imode == BLKmode)
3542 return NULL_RTX;
3543 op1 = gen_lowpart (imode, op1);
3545 else
3547 int word;
3549 imode = word_mode;
3550 if (FLOAT_WORDS_BIG_ENDIAN)
3551 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3552 else
3553 word = bitpos / BITS_PER_WORD;
3554 bitpos = bitpos % BITS_PER_WORD;
3555 op1 = operand_subword_force (op1, word, mode);
3558 if (bitpos < HOST_BITS_PER_WIDE_INT)
3560 hi = 0;
3561 lo = (HOST_WIDE_INT) 1 << bitpos;
3563 else
3565 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
3566 lo = 0;
3569 sign = gen_reg_rtx (imode);
3570 sign = expand_binop (imode, and_optab, op1,
3571 immed_double_const (lo, hi, imode),
3572 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3575 if (!op0_is_abs)
3577 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3578 if (op0 == NULL)
3579 return NULL_RTX;
3580 target = op0;
3582 else
3584 if (target == NULL_RTX)
3585 target = copy_to_reg (op0);
3586 else
3587 emit_move_insn (target, op0);
3590 label = gen_label_rtx ();
3591 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3593 if (GET_CODE (op0) == CONST_DOUBLE)
3594 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3595 else
3596 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3597 if (op0 != target)
3598 emit_move_insn (target, op0);
3600 emit_label (label);
3602 return target;
3606 /* A subroutine of expand_copysign, perform the entire copysign operation
3607 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3608 is true if op0 is known to have its sign bit clear. */
3610 static rtx
3611 expand_copysign_bit (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3612 int bitpos, bool op0_is_abs)
3614 enum machine_mode imode;
3615 HOST_WIDE_INT hi, lo;
3616 int word, nwords, i;
3617 rtx temp, insns;
3619 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3621 imode = int_mode_for_mode (mode);
3622 if (imode == BLKmode)
3623 return NULL_RTX;
3624 word = 0;
3625 nwords = 1;
3627 else
3629 imode = word_mode;
3631 if (FLOAT_WORDS_BIG_ENDIAN)
3632 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3633 else
3634 word = bitpos / BITS_PER_WORD;
3635 bitpos = bitpos % BITS_PER_WORD;
3636 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3639 if (bitpos < HOST_BITS_PER_WIDE_INT)
3641 hi = 0;
3642 lo = (HOST_WIDE_INT) 1 << bitpos;
3644 else
3646 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
3647 lo = 0;
3650 if (target == 0 || target == op0 || target == op1)
3651 target = gen_reg_rtx (mode);
3653 if (nwords > 1)
3655 start_sequence ();
3657 for (i = 0; i < nwords; ++i)
3659 rtx targ_piece = operand_subword (target, i, 1, mode);
3660 rtx op0_piece = operand_subword_force (op0, i, mode);
3662 if (i == word)
3664 if (!op0_is_abs)
3665 op0_piece = expand_binop (imode, and_optab, op0_piece,
3666 immed_double_const (~lo, ~hi, imode),
3667 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3669 op1 = expand_binop (imode, and_optab,
3670 operand_subword_force (op1, i, mode),
3671 immed_double_const (lo, hi, imode),
3672 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3674 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3675 targ_piece, 1, OPTAB_LIB_WIDEN);
3676 if (temp != targ_piece)
3677 emit_move_insn (targ_piece, temp);
3679 else
3680 emit_move_insn (targ_piece, op0_piece);
3683 insns = get_insns ();
3684 end_sequence ();
3686 emit_insn (insns);
3688 else
3690 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3691 immed_double_const (lo, hi, imode),
3692 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3694 op0 = gen_lowpart (imode, op0);
3695 if (!op0_is_abs)
3696 op0 = expand_binop (imode, and_optab, op0,
3697 immed_double_const (~lo, ~hi, imode),
3698 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3700 temp = expand_binop (imode, ior_optab, op0, op1,
3701 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3702 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3705 return target;
3708 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3709 scalar floating point mode. Return NULL if we do not know how to
3710 expand the operation inline. */
3713 expand_copysign (rtx op0, rtx op1, rtx target)
3715 enum machine_mode mode = GET_MODE (op0);
3716 const struct real_format *fmt;
3717 bool op0_is_abs;
3718 rtx temp;
3720 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3721 gcc_assert (GET_MODE (op1) == mode);
3723 /* First try to do it with a special instruction. */
3724 temp = expand_binop (mode, copysign_optab, op0, op1,
3725 target, 0, OPTAB_DIRECT);
3726 if (temp)
3727 return temp;
3729 fmt = REAL_MODE_FORMAT (mode);
3730 if (fmt == NULL || !fmt->has_signed_zero)
3731 return NULL_RTX;
3733 op0_is_abs = false;
3734 if (GET_CODE (op0) == CONST_DOUBLE)
3736 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3737 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3738 op0_is_abs = true;
3741 if (fmt->signbit_ro >= 0
3742 && (GET_CODE (op0) == CONST_DOUBLE
3743 || (optab_handler (neg_optab, mode)->insn_code != CODE_FOR_nothing
3744 && optab_handler (abs_optab, mode)->insn_code != CODE_FOR_nothing)))
3746 temp = expand_copysign_absneg (mode, op0, op1, target,
3747 fmt->signbit_ro, op0_is_abs);
3748 if (temp)
3749 return temp;
3752 if (fmt->signbit_rw < 0)
3753 return NULL_RTX;
3754 return expand_copysign_bit (mode, op0, op1, target,
3755 fmt->signbit_rw, op0_is_abs);
3758 /* Generate an instruction whose insn-code is INSN_CODE,
3759 with two operands: an output TARGET and an input OP0.
3760 TARGET *must* be nonzero, and the output is always stored there.
3761 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3762 the value that is stored into TARGET. */
3764 void
3765 emit_unop_insn (int icode, rtx target, rtx op0, enum rtx_code code)
3767 rtx temp;
3768 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3769 rtx pat;
3771 temp = target;
3773 /* Now, if insn does not accept our operands, put them into pseudos. */
3775 if (!insn_data[icode].operand[1].predicate (op0, mode0))
3776 op0 = copy_to_mode_reg (mode0, op0);
3778 if (!insn_data[icode].operand[0].predicate (temp, GET_MODE (temp)))
3779 temp = gen_reg_rtx (GET_MODE (temp));
3781 pat = GEN_FCN (icode) (temp, op0);
3783 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
3784 add_equal_note (pat, temp, code, op0, NULL_RTX);
3786 emit_insn (pat);
3788 if (temp != target)
3789 emit_move_insn (target, temp);
3792 struct no_conflict_data
3794 rtx target, first, insn;
3795 bool must_stay;
3798 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3799 the currently examined clobber / store has to stay in the list of
3800 insns that constitute the actual libcall block. */
3801 static void
3802 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3804 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3806 /* If this inns directly contributes to setting the target, it must stay. */
3807 if (reg_overlap_mentioned_p (p->target, dest))
3808 p->must_stay = true;
3809 /* If we haven't committed to keeping any other insns in the list yet,
3810 there is nothing more to check. */
3811 else if (p->insn == p->first)
3812 return;
3813 /* If this insn sets / clobbers a register that feeds one of the insns
3814 already in the list, this insn has to stay too. */
3815 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3816 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3817 || reg_used_between_p (dest, p->first, p->insn)
3818 /* Likewise if this insn depends on a register set by a previous
3819 insn in the list, or if it sets a result (presumably a hard
3820 register) that is set or clobbered by a previous insn.
3821 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3822 SET_DEST perform the former check on the address, and the latter
3823 check on the MEM. */
3824 || (GET_CODE (set) == SET
3825 && (modified_in_p (SET_SRC (set), p->first)
3826 || modified_in_p (SET_DEST (set), p->first)
3827 || modified_between_p (SET_SRC (set), p->first, p->insn)
3828 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3829 p->must_stay = true;
3833 /* Emit code to make a call to a constant function or a library call.
3835 INSNS is a list containing all insns emitted in the call.
3836 These insns leave the result in RESULT. Our block is to copy RESULT
3837 to TARGET, which is logically equivalent to EQUIV.
3839 We first emit any insns that set a pseudo on the assumption that these are
3840 loading constants into registers; doing so allows them to be safely cse'ed
3841 between blocks. Then we emit all the other insns in the block, followed by
3842 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3843 note with an operand of EQUIV. */
3845 void
3846 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3848 rtx final_dest = target;
3849 rtx prev, next, last, insn;
3851 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3852 into a MEM later. Protect the libcall block from this change. */
3853 if (! REG_P (target) || REG_USERVAR_P (target))
3854 target = gen_reg_rtx (GET_MODE (target));
3856 /* If we're using non-call exceptions, a libcall corresponding to an
3857 operation that may trap may also trap. */
3858 if (flag_non_call_exceptions && may_trap_p (equiv))
3860 for (insn = insns; insn; insn = NEXT_INSN (insn))
3861 if (CALL_P (insn))
3863 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3865 if (note != 0 && INTVAL (XEXP (note, 0)) <= 0)
3866 remove_note (insn, note);
3869 else
3870 /* look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3871 reg note to indicate that this call cannot throw or execute a nonlocal
3872 goto (unless there is already a REG_EH_REGION note, in which case
3873 we update it). */
3874 for (insn = insns; insn; insn = NEXT_INSN (insn))
3875 if (CALL_P (insn))
3877 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3879 if (note != 0)
3880 XEXP (note, 0) = constm1_rtx;
3881 else
3882 add_reg_note (insn, REG_EH_REGION, constm1_rtx);
3885 /* First emit all insns that set pseudos. Remove them from the list as
3886 we go. Avoid insns that set pseudos which were referenced in previous
3887 insns. These can be generated by move_by_pieces, for example,
3888 to update an address. Similarly, avoid insns that reference things
3889 set in previous insns. */
3891 for (insn = insns; insn; insn = next)
3893 rtx set = single_set (insn);
3895 next = NEXT_INSN (insn);
3897 if (set != 0 && REG_P (SET_DEST (set))
3898 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3900 struct no_conflict_data data;
3902 data.target = const0_rtx;
3903 data.first = insns;
3904 data.insn = insn;
3905 data.must_stay = 0;
3906 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3907 if (! data.must_stay)
3909 if (PREV_INSN (insn))
3910 NEXT_INSN (PREV_INSN (insn)) = next;
3911 else
3912 insns = next;
3914 if (next)
3915 PREV_INSN (next) = PREV_INSN (insn);
3917 add_insn (insn);
3921 /* Some ports use a loop to copy large arguments onto the stack.
3922 Don't move anything outside such a loop. */
3923 if (LABEL_P (insn))
3924 break;
3927 prev = get_last_insn ();
3929 /* Write the remaining insns followed by the final copy. */
3931 for (insn = insns; insn; insn = next)
3933 next = NEXT_INSN (insn);
3935 add_insn (insn);
3938 last = emit_move_insn (target, result);
3939 if (optab_handler (mov_optab, GET_MODE (target))->insn_code
3940 != CODE_FOR_nothing)
3941 set_unique_reg_note (last, REG_EQUAL, copy_rtx (equiv));
3943 if (final_dest != target)
3944 emit_move_insn (final_dest, target);
3947 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3948 PURPOSE describes how this comparison will be used. CODE is the rtx
3949 comparison code we will be using.
3951 ??? Actually, CODE is slightly weaker than that. A target is still
3952 required to implement all of the normal bcc operations, but not
3953 required to implement all (or any) of the unordered bcc operations. */
3956 can_compare_p (enum rtx_code code, enum machine_mode mode,
3957 enum can_compare_purpose purpose)
3961 if (optab_handler (cmp_optab, mode)->insn_code != CODE_FOR_nothing)
3963 if (purpose == ccp_jump)
3964 return bcc_gen_fctn[(int) code] != NULL;
3965 else if (purpose == ccp_store_flag)
3966 return setcc_gen_code[(int) code] != CODE_FOR_nothing;
3967 else
3968 /* There's only one cmov entry point, and it's allowed to fail. */
3969 return 1;
3971 if (purpose == ccp_jump
3972 && optab_handler (cbranch_optab, mode)->insn_code != CODE_FOR_nothing)
3973 return 1;
3974 if (purpose == ccp_cmov
3975 && optab_handler (cmov_optab, mode)->insn_code != CODE_FOR_nothing)
3976 return 1;
3977 if (purpose == ccp_store_flag
3978 && optab_handler (cstore_optab, mode)->insn_code != CODE_FOR_nothing)
3979 return 1;
3980 mode = GET_MODE_WIDER_MODE (mode);
3982 while (mode != VOIDmode);
3984 return 0;
3987 /* This function is called when we are going to emit a compare instruction that
3988 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
3990 *PMODE is the mode of the inputs (in case they are const_int).
3991 *PUNSIGNEDP nonzero says that the operands are unsigned;
3992 this matters if they need to be widened.
3994 If they have mode BLKmode, then SIZE specifies the size of both operands.
3996 This function performs all the setup necessary so that the caller only has
3997 to emit a single comparison insn. This setup can involve doing a BLKmode
3998 comparison or emitting a library call to perform the comparison if no insn
3999 is available to handle it.
4000 The values which are passed in through pointers can be modified; the caller
4001 should perform the comparison on the modified values. Constant
4002 comparisons must have already been folded. */
4004 static void
4005 prepare_cmp_insn (rtx *px, rtx *py, enum rtx_code *pcomparison, rtx size,
4006 enum machine_mode *pmode, int *punsignedp,
4007 enum can_compare_purpose purpose)
4009 enum machine_mode mode = *pmode;
4010 rtx x = *px, y = *py;
4011 int unsignedp = *punsignedp;
4012 rtx libfunc;
4014 /* If we are inside an appropriately-short loop and we are optimizing,
4015 force expensive constants into a register. */
4016 if (CONSTANT_P (x) && optimize
4017 && rtx_cost (x, COMPARE) > COSTS_N_INSNS (1))
4018 x = force_reg (mode, x);
4020 if (CONSTANT_P (y) && optimize
4021 && rtx_cost (y, COMPARE) > COSTS_N_INSNS (1))
4022 y = force_reg (mode, y);
4024 #ifdef HAVE_cc0
4025 /* Make sure if we have a canonical comparison. The RTL
4026 documentation states that canonical comparisons are required only
4027 for targets which have cc0. */
4028 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4029 #endif
4031 /* Don't let both operands fail to indicate the mode. */
4032 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4033 x = force_reg (mode, x);
4035 /* Handle all BLKmode compares. */
4037 if (mode == BLKmode)
4039 enum machine_mode cmp_mode, result_mode;
4040 enum insn_code cmp_code;
4041 tree length_type;
4042 rtx libfunc;
4043 rtx result;
4044 rtx opalign
4045 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4047 gcc_assert (size);
4049 /* Try to use a memory block compare insn - either cmpstr
4050 or cmpmem will do. */
4051 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4052 cmp_mode != VOIDmode;
4053 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4055 cmp_code = cmpmem_optab[cmp_mode];
4056 if (cmp_code == CODE_FOR_nothing)
4057 cmp_code = cmpstr_optab[cmp_mode];
4058 if (cmp_code == CODE_FOR_nothing)
4059 cmp_code = cmpstrn_optab[cmp_mode];
4060 if (cmp_code == CODE_FOR_nothing)
4061 continue;
4063 /* Must make sure the size fits the insn's mode. */
4064 if ((GET_CODE (size) == CONST_INT
4065 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4066 || (GET_MODE_BITSIZE (GET_MODE (size))
4067 > GET_MODE_BITSIZE (cmp_mode)))
4068 continue;
4070 result_mode = insn_data[cmp_code].operand[0].mode;
4071 result = gen_reg_rtx (result_mode);
4072 size = convert_to_mode (cmp_mode, size, 1);
4073 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4075 *px = result;
4076 *py = const0_rtx;
4077 *pmode = result_mode;
4078 return;
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);
4094 *px = result;
4095 *py = 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 (flag_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 *px = x;
4111 *py = y;
4112 if (can_compare_p (*pcomparison, mode, purpose))
4113 return;
4115 /* Handle a lib call just for the mode we are using. */
4117 libfunc = optab_libfunc (cmp_optab, mode);
4118 if (libfunc && !SCALAR_FLOAT_MODE_P (mode))
4120 rtx result;
4122 /* If we want unsigned, and this mode has a distinct unsigned
4123 comparison routine, use that. */
4124 if (unsignedp)
4126 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4127 if (ulibfunc)
4128 libfunc = ulibfunc;
4131 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4132 targetm.libgcc_cmp_return_mode (),
4133 2, x, mode, y, mode);
4135 /* There are two kinds of comparison routines. Biased routines
4136 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4137 of gcc expect that the comparison operation is equivalent
4138 to the modified comparison. For signed comparisons compare the
4139 result against 1 in the biased case, and zero in the unbiased
4140 case. For unsigned comparisons always compare against 1 after
4141 biasing the unbiased result by adding 1. This gives us a way to
4142 represent LTU. */
4143 *px = result;
4144 *pmode = word_mode;
4145 *py = const1_rtx;
4147 if (!TARGET_LIB_INT_CMP_BIASED)
4149 if (*punsignedp)
4150 *px = plus_constant (result, 1);
4151 else
4152 *py = const0_rtx;
4154 return;
4157 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
4158 prepare_float_lib_cmp (px, py, pcomparison, pmode, punsignedp);
4161 /* Before emitting an insn with code ICODE, make sure that X, which is going
4162 to be used for operand OPNUM of the insn, is converted from mode MODE to
4163 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4164 that it is accepted by the operand predicate. Return the new value. */
4166 static rtx
4167 prepare_operand (int icode, rtx x, int opnum, enum machine_mode mode,
4168 enum machine_mode wider_mode, int unsignedp)
4170 if (mode != wider_mode)
4171 x = convert_modes (wider_mode, mode, x, unsignedp);
4173 if (!insn_data[icode].operand[opnum].predicate
4174 (x, insn_data[icode].operand[opnum].mode))
4176 if (reload_completed)
4177 return NULL_RTX;
4178 x = copy_to_mode_reg (insn_data[icode].operand[opnum].mode, x);
4181 return x;
4184 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4185 we can do the comparison.
4186 The arguments are the same as for emit_cmp_and_jump_insns; but LABEL may
4187 be NULL_RTX which indicates that only a comparison is to be generated. */
4189 static void
4190 emit_cmp_and_jump_insn_1 (rtx x, rtx y, enum machine_mode mode,
4191 enum rtx_code comparison, int unsignedp, rtx label)
4193 rtx test = gen_rtx_fmt_ee (comparison, mode, x, y);
4194 enum mode_class mclass = GET_MODE_CLASS (mode);
4195 enum machine_mode wider_mode = mode;
4197 /* Try combined insns first. */
4200 enum insn_code icode;
4201 PUT_MODE (test, wider_mode);
4203 if (label)
4205 icode = optab_handler (cbranch_optab, wider_mode)->insn_code;
4207 if (icode != CODE_FOR_nothing
4208 && insn_data[icode].operand[0].predicate (test, wider_mode))
4210 x = prepare_operand (icode, x, 1, mode, wider_mode, unsignedp);
4211 y = prepare_operand (icode, y, 2, mode, wider_mode, unsignedp);
4212 emit_jump_insn (GEN_FCN (icode) (test, x, y, label));
4213 return;
4217 /* Handle some compares against zero. */
4218 icode = (int) optab_handler (tst_optab, wider_mode)->insn_code;
4219 if (y == CONST0_RTX (mode) && icode != CODE_FOR_nothing)
4221 x = prepare_operand (icode, x, 0, mode, wider_mode, unsignedp);
4222 emit_insn (GEN_FCN (icode) (x));
4223 if (label)
4224 emit_jump_insn (bcc_gen_fctn[(int) comparison] (label));
4225 return;
4228 /* Handle compares for which there is a directly suitable insn. */
4230 icode = (int) optab_handler (cmp_optab, wider_mode)->insn_code;
4231 if (icode != CODE_FOR_nothing)
4233 x = prepare_operand (icode, x, 0, mode, wider_mode, unsignedp);
4234 y = prepare_operand (icode, y, 1, mode, wider_mode, unsignedp);
4235 emit_insn (GEN_FCN (icode) (x, y));
4236 if (label)
4237 emit_jump_insn (bcc_gen_fctn[(int) comparison] (label));
4238 return;
4241 if (!CLASS_HAS_WIDER_MODES_P (mclass))
4242 break;
4244 wider_mode = GET_MODE_WIDER_MODE (wider_mode);
4246 while (wider_mode != VOIDmode);
4248 gcc_unreachable ();
4251 /* Generate code to compare X with Y so that the condition codes are
4252 set and to jump to LABEL if the condition is true. If X is a
4253 constant and Y is not a constant, then the comparison is swapped to
4254 ensure that the comparison RTL has the canonical form.
4256 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4257 need to be widened by emit_cmp_insn. UNSIGNEDP is also used to select
4258 the proper branch condition code.
4260 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4262 MODE is the mode of the inputs (in case they are const_int).
4264 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). It will
4265 be passed unchanged to emit_cmp_insn, then potentially converted into an
4266 unsigned variant based on UNSIGNEDP to select a proper jump instruction. */
4268 void
4269 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4270 enum machine_mode mode, int unsignedp, rtx label)
4272 rtx op0 = x, op1 = y;
4274 /* Swap operands and condition to ensure canonical RTL. */
4275 if (swap_commutative_operands_p (x, y))
4277 /* If we're not emitting a branch, callers are required to pass
4278 operands in an order conforming to canonical RTL. We relax this
4279 for commutative comparisons so callers using EQ don't need to do
4280 swapping by hand. */
4281 gcc_assert (label || (comparison == swap_condition (comparison)));
4283 op0 = y, op1 = x;
4284 comparison = swap_condition (comparison);
4287 #ifdef HAVE_cc0
4288 /* If OP0 is still a constant, then both X and Y must be constants.
4289 Force X into a register to create canonical RTL. */
4290 if (CONSTANT_P (op0))
4291 op0 = force_reg (mode, op0);
4292 #endif
4294 if (unsignedp)
4295 comparison = unsigned_condition (comparison);
4297 prepare_cmp_insn (&op0, &op1, &comparison, size, &mode, &unsignedp,
4298 ccp_jump);
4299 emit_cmp_and_jump_insn_1 (op0, op1, mode, comparison, unsignedp, label);
4302 /* Like emit_cmp_and_jump_insns, but generate only the comparison. */
4304 void
4305 emit_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4306 enum machine_mode mode, int unsignedp)
4308 emit_cmp_and_jump_insns (x, y, comparison, size, mode, unsignedp, 0);
4311 /* Emit a library call comparison between floating point X and Y.
4312 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4314 static void
4315 prepare_float_lib_cmp (rtx *px, rtx *py, enum rtx_code *pcomparison,
4316 enum machine_mode *pmode, int *punsignedp)
4318 enum rtx_code comparison = *pcomparison;
4319 enum rtx_code swapped = swap_condition (comparison);
4320 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4321 rtx x = *px;
4322 rtx y = *py;
4323 enum machine_mode orig_mode = GET_MODE (x);
4324 enum machine_mode mode, cmp_mode;
4325 rtx value, target, insns, equiv;
4326 rtx libfunc = 0;
4327 bool reversed_p = false;
4328 cmp_mode = targetm.libgcc_cmp_return_mode ();
4330 for (mode = orig_mode;
4331 mode != VOIDmode;
4332 mode = GET_MODE_WIDER_MODE (mode))
4334 if ((libfunc = optab_libfunc (code_to_optab[comparison], mode)))
4335 break;
4337 if ((libfunc = optab_libfunc (code_to_optab[swapped] , mode)))
4339 rtx tmp;
4340 tmp = x; x = y; y = tmp;
4341 comparison = swapped;
4342 break;
4345 if ((libfunc = optab_libfunc (code_to_optab[reversed], mode))
4346 && FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, reversed))
4348 comparison = reversed;
4349 reversed_p = true;
4350 break;
4354 gcc_assert (mode != VOIDmode);
4356 if (mode != orig_mode)
4358 x = convert_to_mode (mode, x, 0);
4359 y = convert_to_mode (mode, y, 0);
4362 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4363 the RTL. The allows the RTL optimizers to delete the libcall if the
4364 condition can be determined at compile-time. */
4365 if (comparison == UNORDERED)
4367 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4368 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4369 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4370 temp, const_true_rtx, equiv);
4372 else
4374 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4375 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4377 rtx true_rtx, false_rtx;
4379 switch (comparison)
4381 case EQ:
4382 true_rtx = const0_rtx;
4383 false_rtx = const_true_rtx;
4384 break;
4386 case NE:
4387 true_rtx = const_true_rtx;
4388 false_rtx = const0_rtx;
4389 break;
4391 case GT:
4392 true_rtx = const1_rtx;
4393 false_rtx = const0_rtx;
4394 break;
4396 case GE:
4397 true_rtx = const0_rtx;
4398 false_rtx = constm1_rtx;
4399 break;
4401 case LT:
4402 true_rtx = constm1_rtx;
4403 false_rtx = const0_rtx;
4404 break;
4406 case LE:
4407 true_rtx = const0_rtx;
4408 false_rtx = const1_rtx;
4409 break;
4411 default:
4412 gcc_unreachable ();
4414 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4415 equiv, true_rtx, false_rtx);
4419 start_sequence ();
4420 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4421 cmp_mode, 2, x, mode, y, mode);
4422 insns = get_insns ();
4423 end_sequence ();
4425 target = gen_reg_rtx (cmp_mode);
4426 emit_libcall_block (insns, target, value, equiv);
4428 if (comparison == UNORDERED
4429 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4430 comparison = reversed_p ? EQ : NE;
4432 *px = target;
4433 *py = const0_rtx;
4434 *pmode = cmp_mode;
4435 *pcomparison = comparison;
4436 *punsignedp = 0;
4439 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4441 void
4442 emit_indirect_jump (rtx loc)
4444 if (!insn_data[(int) CODE_FOR_indirect_jump].operand[0].predicate
4445 (loc, Pmode))
4446 loc = copy_to_mode_reg (Pmode, loc);
4448 emit_jump_insn (gen_indirect_jump (loc));
4449 emit_barrier ();
4452 #ifdef HAVE_conditional_move
4454 /* Emit a conditional move instruction if the machine supports one for that
4455 condition and machine mode.
4457 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4458 the mode to use should they be constants. If it is VOIDmode, they cannot
4459 both be constants.
4461 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4462 should be stored there. MODE is the mode to use should they be constants.
4463 If it is VOIDmode, they cannot both be constants.
4465 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4466 is not supported. */
4469 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4470 enum machine_mode cmode, rtx op2, rtx op3,
4471 enum machine_mode mode, int unsignedp)
4473 rtx tem, subtarget, comparison, insn;
4474 enum insn_code icode;
4475 enum rtx_code reversed;
4477 /* If one operand is constant, make it the second one. Only do this
4478 if the other operand is not constant as well. */
4480 if (swap_commutative_operands_p (op0, op1))
4482 tem = op0;
4483 op0 = op1;
4484 op1 = tem;
4485 code = swap_condition (code);
4488 /* get_condition will prefer to generate LT and GT even if the old
4489 comparison was against zero, so undo that canonicalization here since
4490 comparisons against zero are cheaper. */
4491 if (code == LT && op1 == const1_rtx)
4492 code = LE, op1 = const0_rtx;
4493 else if (code == GT && op1 == constm1_rtx)
4494 code = GE, op1 = const0_rtx;
4496 if (cmode == VOIDmode)
4497 cmode = GET_MODE (op0);
4499 if (swap_commutative_operands_p (op2, op3)
4500 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4501 != UNKNOWN))
4503 tem = op2;
4504 op2 = op3;
4505 op3 = tem;
4506 code = reversed;
4509 if (mode == VOIDmode)
4510 mode = GET_MODE (op2);
4512 icode = movcc_gen_code[mode];
4514 if (icode == CODE_FOR_nothing)
4515 return 0;
4517 if (!target)
4518 target = gen_reg_rtx (mode);
4520 subtarget = target;
4522 /* If the insn doesn't accept these operands, put them in pseudos. */
4524 if (!insn_data[icode].operand[0].predicate
4525 (subtarget, insn_data[icode].operand[0].mode))
4526 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
4528 if (!insn_data[icode].operand[2].predicate
4529 (op2, insn_data[icode].operand[2].mode))
4530 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
4532 if (!insn_data[icode].operand[3].predicate
4533 (op3, insn_data[icode].operand[3].mode))
4534 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
4536 /* Everything should now be in the suitable form, so emit the compare insn
4537 and then the conditional move. */
4539 comparison
4540 = compare_from_rtx (op0, op1, code, unsignedp, cmode, NULL_RTX);
4542 /* ??? Watch for const0_rtx (nop) and const_true_rtx (unconditional)? */
4543 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4544 return NULL and let the caller figure out how best to deal with this
4545 situation. */
4546 if (GET_CODE (comparison) != code)
4547 return NULL_RTX;
4549 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
4551 /* If that failed, then give up. */
4552 if (insn == 0)
4553 return 0;
4555 emit_insn (insn);
4557 if (subtarget != target)
4558 convert_move (target, subtarget, 0);
4560 return target;
4563 /* Return nonzero if a conditional move of mode MODE is supported.
4565 This function is for combine so it can tell whether an insn that looks
4566 like a conditional move is actually supported by the hardware. If we
4567 guess wrong we lose a bit on optimization, but that's it. */
4568 /* ??? sparc64 supports conditionally moving integers values based on fp
4569 comparisons, and vice versa. How do we handle them? */
4572 can_conditionally_move_p (enum machine_mode mode)
4574 if (movcc_gen_code[mode] != CODE_FOR_nothing)
4575 return 1;
4577 return 0;
4580 #endif /* HAVE_conditional_move */
4582 /* Emit a conditional addition instruction if the machine supports one for that
4583 condition and machine mode.
4585 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4586 the mode to use should they be constants. If it is VOIDmode, they cannot
4587 both be constants.
4589 OP2 should be stored in TARGET if the comparison is true, otherwise OP2+OP3
4590 should be stored there. MODE is the mode to use should they be constants.
4591 If it is VOIDmode, they cannot both be constants.
4593 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4594 is not supported. */
4597 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4598 enum machine_mode cmode, rtx op2, rtx op3,
4599 enum machine_mode mode, int unsignedp)
4601 rtx tem, subtarget, comparison, insn;
4602 enum insn_code icode;
4603 enum rtx_code reversed;
4605 /* If one operand is constant, make it the second one. Only do this
4606 if the other operand is not constant as well. */
4608 if (swap_commutative_operands_p (op0, op1))
4610 tem = op0;
4611 op0 = op1;
4612 op1 = tem;
4613 code = swap_condition (code);
4616 /* get_condition will prefer to generate LT and GT even if the old
4617 comparison was against zero, so undo that canonicalization here since
4618 comparisons against zero are cheaper. */
4619 if (code == LT && op1 == const1_rtx)
4620 code = LE, op1 = const0_rtx;
4621 else if (code == GT && op1 == constm1_rtx)
4622 code = GE, op1 = const0_rtx;
4624 if (cmode == VOIDmode)
4625 cmode = GET_MODE (op0);
4627 if (swap_commutative_operands_p (op2, op3)
4628 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4629 != UNKNOWN))
4631 tem = op2;
4632 op2 = op3;
4633 op3 = tem;
4634 code = reversed;
4637 if (mode == VOIDmode)
4638 mode = GET_MODE (op2);
4640 icode = optab_handler (addcc_optab, mode)->insn_code;
4642 if (icode == CODE_FOR_nothing)
4643 return 0;
4645 if (!target)
4646 target = gen_reg_rtx (mode);
4648 /* If the insn doesn't accept these operands, put them in pseudos. */
4650 if (!insn_data[icode].operand[0].predicate
4651 (target, insn_data[icode].operand[0].mode))
4652 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
4653 else
4654 subtarget = target;
4656 if (!insn_data[icode].operand[2].predicate
4657 (op2, insn_data[icode].operand[2].mode))
4658 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
4660 if (!insn_data[icode].operand[3].predicate
4661 (op3, insn_data[icode].operand[3].mode))
4662 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
4664 /* Everything should now be in the suitable form, so emit the compare insn
4665 and then the conditional move. */
4667 comparison
4668 = compare_from_rtx (op0, op1, code, unsignedp, cmode, NULL_RTX);
4670 /* ??? Watch for const0_rtx (nop) and const_true_rtx (unconditional)? */
4671 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4672 return NULL and let the caller figure out how best to deal with this
4673 situation. */
4674 if (GET_CODE (comparison) != code)
4675 return NULL_RTX;
4677 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
4679 /* If that failed, then give up. */
4680 if (insn == 0)
4681 return 0;
4683 emit_insn (insn);
4685 if (subtarget != target)
4686 convert_move (target, subtarget, 0);
4688 return target;
4691 /* These functions attempt to generate an insn body, rather than
4692 emitting the insn, but if the gen function already emits them, we
4693 make no attempt to turn them back into naked patterns. */
4695 /* Generate and return an insn body to add Y to X. */
4698 gen_add2_insn (rtx x, rtx y)
4700 int icode = (int) optab_handler (add_optab, GET_MODE (x))->insn_code;
4702 gcc_assert (insn_data[icode].operand[0].predicate
4703 (x, insn_data[icode].operand[0].mode));
4704 gcc_assert (insn_data[icode].operand[1].predicate
4705 (x, insn_data[icode].operand[1].mode));
4706 gcc_assert (insn_data[icode].operand[2].predicate
4707 (y, insn_data[icode].operand[2].mode));
4709 return GEN_FCN (icode) (x, x, y);
4712 /* Generate and return an insn body to add r1 and c,
4713 storing the result in r0. */
4716 gen_add3_insn (rtx r0, rtx r1, rtx c)
4718 int icode = (int) optab_handler (add_optab, GET_MODE (r0))->insn_code;
4720 if (icode == CODE_FOR_nothing
4721 || !(insn_data[icode].operand[0].predicate
4722 (r0, insn_data[icode].operand[0].mode))
4723 || !(insn_data[icode].operand[1].predicate
4724 (r1, insn_data[icode].operand[1].mode))
4725 || !(insn_data[icode].operand[2].predicate
4726 (c, insn_data[icode].operand[2].mode)))
4727 return NULL_RTX;
4729 return GEN_FCN (icode) (r0, r1, c);
4733 have_add2_insn (rtx x, rtx y)
4735 int icode;
4737 gcc_assert (GET_MODE (x) != VOIDmode);
4739 icode = (int) optab_handler (add_optab, GET_MODE (x))->insn_code;
4741 if (icode == CODE_FOR_nothing)
4742 return 0;
4744 if (!(insn_data[icode].operand[0].predicate
4745 (x, insn_data[icode].operand[0].mode))
4746 || !(insn_data[icode].operand[1].predicate
4747 (x, insn_data[icode].operand[1].mode))
4748 || !(insn_data[icode].operand[2].predicate
4749 (y, insn_data[icode].operand[2].mode)))
4750 return 0;
4752 return 1;
4755 /* Generate and return an insn body to subtract Y from X. */
4758 gen_sub2_insn (rtx x, rtx y)
4760 int icode = (int) optab_handler (sub_optab, GET_MODE (x))->insn_code;
4762 gcc_assert (insn_data[icode].operand[0].predicate
4763 (x, insn_data[icode].operand[0].mode));
4764 gcc_assert (insn_data[icode].operand[1].predicate
4765 (x, insn_data[icode].operand[1].mode));
4766 gcc_assert (insn_data[icode].operand[2].predicate
4767 (y, insn_data[icode].operand[2].mode));
4769 return GEN_FCN (icode) (x, x, y);
4772 /* Generate and return an insn body to subtract r1 and c,
4773 storing the result in r0. */
4776 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4778 int icode = (int) optab_handler (sub_optab, GET_MODE (r0))->insn_code;
4780 if (icode == CODE_FOR_nothing
4781 || !(insn_data[icode].operand[0].predicate
4782 (r0, insn_data[icode].operand[0].mode))
4783 || !(insn_data[icode].operand[1].predicate
4784 (r1, insn_data[icode].operand[1].mode))
4785 || !(insn_data[icode].operand[2].predicate
4786 (c, insn_data[icode].operand[2].mode)))
4787 return NULL_RTX;
4789 return GEN_FCN (icode) (r0, r1, c);
4793 have_sub2_insn (rtx x, rtx y)
4795 int icode;
4797 gcc_assert (GET_MODE (x) != VOIDmode);
4799 icode = (int) optab_handler (sub_optab, GET_MODE (x))->insn_code;
4801 if (icode == CODE_FOR_nothing)
4802 return 0;
4804 if (!(insn_data[icode].operand[0].predicate
4805 (x, insn_data[icode].operand[0].mode))
4806 || !(insn_data[icode].operand[1].predicate
4807 (x, insn_data[icode].operand[1].mode))
4808 || !(insn_data[icode].operand[2].predicate
4809 (y, insn_data[icode].operand[2].mode)))
4810 return 0;
4812 return 1;
4815 /* Generate the body of an instruction to copy Y into X.
4816 It may be a list of insns, if one insn isn't enough. */
4819 gen_move_insn (rtx x, rtx y)
4821 rtx seq;
4823 start_sequence ();
4824 emit_move_insn_1 (x, y);
4825 seq = get_insns ();
4826 end_sequence ();
4827 return seq;
4830 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4831 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4832 no such operation exists, CODE_FOR_nothing will be returned. */
4834 enum insn_code
4835 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
4836 int unsignedp)
4838 convert_optab tab;
4839 #ifdef HAVE_ptr_extend
4840 if (unsignedp < 0)
4841 return CODE_FOR_ptr_extend;
4842 #endif
4844 tab = unsignedp ? zext_optab : sext_optab;
4845 return convert_optab_handler (tab, to_mode, from_mode)->insn_code;
4848 /* Generate the body of an insn to extend Y (with mode MFROM)
4849 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4852 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4853 enum machine_mode mfrom, int unsignedp)
4855 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4856 return GEN_FCN (icode) (x, y);
4859 /* can_fix_p and can_float_p say whether the target machine
4860 can directly convert a given fixed point type to
4861 a given floating point type, or vice versa.
4862 The returned value is the CODE_FOR_... value to use,
4863 or CODE_FOR_nothing if these modes cannot be directly converted.
4865 *TRUNCP_PTR is set to 1 if it is necessary to output
4866 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4868 static enum insn_code
4869 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4870 int unsignedp, int *truncp_ptr)
4872 convert_optab tab;
4873 enum insn_code icode;
4875 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4876 icode = convert_optab_handler (tab, fixmode, fltmode)->insn_code;
4877 if (icode != CODE_FOR_nothing)
4879 *truncp_ptr = 0;
4880 return icode;
4883 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4884 for this to work. We need to rework the fix* and ftrunc* patterns
4885 and documentation. */
4886 tab = unsignedp ? ufix_optab : sfix_optab;
4887 icode = convert_optab_handler (tab, fixmode, fltmode)->insn_code;
4888 if (icode != CODE_FOR_nothing
4889 && optab_handler (ftrunc_optab, fltmode)->insn_code != CODE_FOR_nothing)
4891 *truncp_ptr = 1;
4892 return icode;
4895 *truncp_ptr = 0;
4896 return CODE_FOR_nothing;
4899 static enum insn_code
4900 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4901 int unsignedp)
4903 convert_optab tab;
4905 tab = unsignedp ? ufloat_optab : sfloat_optab;
4906 return convert_optab_handler (tab, fltmode, fixmode)->insn_code;
4909 /* Generate code to convert FROM to floating point
4910 and store in TO. FROM must be fixed point and not VOIDmode.
4911 UNSIGNEDP nonzero means regard FROM as unsigned.
4912 Normally this is done by correcting the final value
4913 if it is negative. */
4915 void
4916 expand_float (rtx to, rtx from, int unsignedp)
4918 enum insn_code icode;
4919 rtx target = to;
4920 enum machine_mode fmode, imode;
4921 bool can_do_signed = false;
4923 /* Crash now, because we won't be able to decide which mode to use. */
4924 gcc_assert (GET_MODE (from) != VOIDmode);
4926 /* Look for an insn to do the conversion. Do it in the specified
4927 modes if possible; otherwise convert either input, output or both to
4928 wider mode. If the integer mode is wider than the mode of FROM,
4929 we can do the conversion signed even if the input is unsigned. */
4931 for (fmode = GET_MODE (to); fmode != VOIDmode;
4932 fmode = GET_MODE_WIDER_MODE (fmode))
4933 for (imode = GET_MODE (from); imode != VOIDmode;
4934 imode = GET_MODE_WIDER_MODE (imode))
4936 int doing_unsigned = unsignedp;
4938 if (fmode != GET_MODE (to)
4939 && significand_size (fmode) < GET_MODE_BITSIZE (GET_MODE (from)))
4940 continue;
4942 icode = can_float_p (fmode, imode, unsignedp);
4943 if (icode == CODE_FOR_nothing && unsignedp)
4945 enum insn_code scode = can_float_p (fmode, imode, 0);
4946 if (scode != CODE_FOR_nothing)
4947 can_do_signed = true;
4948 if (imode != GET_MODE (from))
4949 icode = scode, doing_unsigned = 0;
4952 if (icode != CODE_FOR_nothing)
4954 if (imode != GET_MODE (from))
4955 from = convert_to_mode (imode, from, unsignedp);
4957 if (fmode != GET_MODE (to))
4958 target = gen_reg_rtx (fmode);
4960 emit_unop_insn (icode, target, from,
4961 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4963 if (target != to)
4964 convert_move (to, target, 0);
4965 return;
4969 /* Unsigned integer, and no way to convert directly. Convert as signed,
4970 then unconditionally adjust the result. */
4971 if (unsignedp && can_do_signed)
4973 rtx label = gen_label_rtx ();
4974 rtx temp;
4975 REAL_VALUE_TYPE offset;
4977 /* Look for a usable floating mode FMODE wider than the source and at
4978 least as wide as the target. Using FMODE will avoid rounding woes
4979 with unsigned values greater than the signed maximum value. */
4981 for (fmode = GET_MODE (to); fmode != VOIDmode;
4982 fmode = GET_MODE_WIDER_MODE (fmode))
4983 if (GET_MODE_BITSIZE (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
4984 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
4985 break;
4987 if (fmode == VOIDmode)
4989 /* There is no such mode. Pretend the target is wide enough. */
4990 fmode = GET_MODE (to);
4992 /* Avoid double-rounding when TO is narrower than FROM. */
4993 if ((significand_size (fmode) + 1)
4994 < GET_MODE_BITSIZE (GET_MODE (from)))
4996 rtx temp1;
4997 rtx neglabel = gen_label_rtx ();
4999 /* Don't use TARGET if it isn't a register, is a hard register,
5000 or is the wrong mode. */
5001 if (!REG_P (target)
5002 || REGNO (target) < FIRST_PSEUDO_REGISTER
5003 || GET_MODE (target) != fmode)
5004 target = gen_reg_rtx (fmode);
5006 imode = GET_MODE (from);
5007 do_pending_stack_adjust ();
5009 /* Test whether the sign bit is set. */
5010 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5011 0, neglabel);
5013 /* The sign bit is not set. Convert as signed. */
5014 expand_float (target, from, 0);
5015 emit_jump_insn (gen_jump (label));
5016 emit_barrier ();
5018 /* The sign bit is set.
5019 Convert to a usable (positive signed) value by shifting right
5020 one bit, while remembering if a nonzero bit was shifted
5021 out; i.e., compute (from & 1) | (from >> 1). */
5023 emit_label (neglabel);
5024 temp = expand_binop (imode, and_optab, from, const1_rtx,
5025 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5026 temp1 = expand_shift (RSHIFT_EXPR, imode, from, integer_one_node,
5027 NULL_RTX, 1);
5028 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5029 OPTAB_LIB_WIDEN);
5030 expand_float (target, temp, 0);
5032 /* Multiply by 2 to undo the shift above. */
5033 temp = expand_binop (fmode, add_optab, target, target,
5034 target, 0, OPTAB_LIB_WIDEN);
5035 if (temp != target)
5036 emit_move_insn (target, temp);
5038 do_pending_stack_adjust ();
5039 emit_label (label);
5040 goto done;
5044 /* If we are about to do some arithmetic to correct for an
5045 unsigned operand, do it in a pseudo-register. */
5047 if (GET_MODE (to) != fmode
5048 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5049 target = gen_reg_rtx (fmode);
5051 /* Convert as signed integer to floating. */
5052 expand_float (target, from, 0);
5054 /* If FROM is negative (and therefore TO is negative),
5055 correct its value by 2**bitwidth. */
5057 do_pending_stack_adjust ();
5058 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5059 0, label);
5062 real_2expN (&offset, GET_MODE_BITSIZE (GET_MODE (from)), fmode);
5063 temp = expand_binop (fmode, add_optab, target,
5064 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5065 target, 0, OPTAB_LIB_WIDEN);
5066 if (temp != target)
5067 emit_move_insn (target, temp);
5069 do_pending_stack_adjust ();
5070 emit_label (label);
5071 goto done;
5074 /* No hardware instruction available; call a library routine. */
5076 rtx libfunc;
5077 rtx insns;
5078 rtx value;
5079 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5081 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
5082 from = convert_to_mode (SImode, from, unsignedp);
5084 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5085 gcc_assert (libfunc);
5087 start_sequence ();
5089 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5090 GET_MODE (to), 1, from,
5091 GET_MODE (from));
5092 insns = get_insns ();
5093 end_sequence ();
5095 emit_libcall_block (insns, target, value,
5096 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5097 GET_MODE (to), from));
5100 done:
5102 /* Copy result to requested destination
5103 if we have been computing in a temp location. */
5105 if (target != to)
5107 if (GET_MODE (target) == GET_MODE (to))
5108 emit_move_insn (to, target);
5109 else
5110 convert_move (to, target, 0);
5114 /* Generate code to convert FROM to fixed point and store in TO. FROM
5115 must be floating point. */
5117 void
5118 expand_fix (rtx to, rtx from, int unsignedp)
5120 enum insn_code icode;
5121 rtx target = to;
5122 enum machine_mode fmode, imode;
5123 int must_trunc = 0;
5125 /* We first try to find a pair of modes, one real and one integer, at
5126 least as wide as FROM and TO, respectively, in which we can open-code
5127 this conversion. If the integer mode is wider than the mode of TO,
5128 we can do the conversion either signed or unsigned. */
5130 for (fmode = GET_MODE (from); fmode != VOIDmode;
5131 fmode = GET_MODE_WIDER_MODE (fmode))
5132 for (imode = GET_MODE (to); imode != VOIDmode;
5133 imode = GET_MODE_WIDER_MODE (imode))
5135 int doing_unsigned = unsignedp;
5137 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5138 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5139 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5141 if (icode != CODE_FOR_nothing)
5143 if (fmode != GET_MODE (from))
5144 from = convert_to_mode (fmode, from, 0);
5146 if (must_trunc)
5148 rtx temp = gen_reg_rtx (GET_MODE (from));
5149 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5150 temp, 0);
5153 if (imode != GET_MODE (to))
5154 target = gen_reg_rtx (imode);
5156 emit_unop_insn (icode, target, from,
5157 doing_unsigned ? UNSIGNED_FIX : FIX);
5158 if (target != to)
5159 convert_move (to, target, unsignedp);
5160 return;
5164 /* For an unsigned conversion, there is one more way to do it.
5165 If we have a signed conversion, we generate code that compares
5166 the real value to the largest representable positive number. If if
5167 is smaller, the conversion is done normally. Otherwise, subtract
5168 one plus the highest signed number, convert, and add it back.
5170 We only need to check all real modes, since we know we didn't find
5171 anything with a wider integer mode.
5173 This code used to extend FP value into mode wider than the destination.
5174 This is needed for decimal float modes which cannot accurately
5175 represent one plus the highest signed number of the same size, but
5176 not for binary modes. Consider, for instance conversion from SFmode
5177 into DImode.
5179 The hot path through the code is dealing with inputs smaller than 2^63
5180 and doing just the conversion, so there is no bits to lose.
5182 In the other path we know the value is positive in the range 2^63..2^64-1
5183 inclusive. (as for other input overflow happens and result is undefined)
5184 So we know that the most important bit set in mantissa corresponds to
5185 2^63. The subtraction of 2^63 should not generate any rounding as it
5186 simply clears out that bit. The rest is trivial. */
5188 if (unsignedp && GET_MODE_BITSIZE (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5189 for (fmode = GET_MODE (from); fmode != VOIDmode;
5190 fmode = GET_MODE_WIDER_MODE (fmode))
5191 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5192 && (!DECIMAL_FLOAT_MODE_P (fmode)
5193 || GET_MODE_BITSIZE (fmode) > GET_MODE_BITSIZE (GET_MODE (to))))
5195 int bitsize;
5196 REAL_VALUE_TYPE offset;
5197 rtx limit, lab1, lab2, insn;
5199 bitsize = GET_MODE_BITSIZE (GET_MODE (to));
5200 real_2expN (&offset, bitsize - 1, fmode);
5201 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5202 lab1 = gen_label_rtx ();
5203 lab2 = gen_label_rtx ();
5205 if (fmode != GET_MODE (from))
5206 from = convert_to_mode (fmode, from, 0);
5208 /* See if we need to do the subtraction. */
5209 do_pending_stack_adjust ();
5210 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5211 0, lab1);
5213 /* If not, do the signed "fix" and branch around fixup code. */
5214 expand_fix (to, from, 0);
5215 emit_jump_insn (gen_jump (lab2));
5216 emit_barrier ();
5218 /* Otherwise, subtract 2**(N-1), convert to signed number,
5219 then add 2**(N-1). Do the addition using XOR since this
5220 will often generate better code. */
5221 emit_label (lab1);
5222 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5223 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5224 expand_fix (to, target, 0);
5225 target = expand_binop (GET_MODE (to), xor_optab, to,
5226 gen_int_mode
5227 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5228 GET_MODE (to)),
5229 to, 1, OPTAB_LIB_WIDEN);
5231 if (target != to)
5232 emit_move_insn (to, target);
5234 emit_label (lab2);
5236 if (optab_handler (mov_optab, GET_MODE (to))->insn_code
5237 != CODE_FOR_nothing)
5239 /* Make a place for a REG_NOTE and add it. */
5240 insn = emit_move_insn (to, to);
5241 set_unique_reg_note (insn,
5242 REG_EQUAL,
5243 gen_rtx_fmt_e (UNSIGNED_FIX,
5244 GET_MODE (to),
5245 copy_rtx (from)));
5248 return;
5251 /* We can't do it with an insn, so use a library call. But first ensure
5252 that the mode of TO is at least as wide as SImode, since those are the
5253 only library calls we know about. */
5255 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
5257 target = gen_reg_rtx (SImode);
5259 expand_fix (target, from, unsignedp);
5261 else
5263 rtx insns;
5264 rtx value;
5265 rtx libfunc;
5267 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5268 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5269 gcc_assert (libfunc);
5271 start_sequence ();
5273 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5274 GET_MODE (to), 1, from,
5275 GET_MODE (from));
5276 insns = get_insns ();
5277 end_sequence ();
5279 emit_libcall_block (insns, target, value,
5280 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5281 GET_MODE (to), from));
5284 if (target != to)
5286 if (GET_MODE (to) == GET_MODE (target))
5287 emit_move_insn (to, target);
5288 else
5289 convert_move (to, target, 0);
5293 /* Generate code to convert FROM or TO a fixed-point.
5294 If UINTP is true, either TO or FROM is an unsigned integer.
5295 If SATP is true, we need to saturate the result. */
5297 void
5298 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5300 enum machine_mode to_mode = GET_MODE (to);
5301 enum machine_mode from_mode = GET_MODE (from);
5302 convert_optab tab;
5303 enum rtx_code this_code;
5304 enum insn_code code;
5305 rtx insns, value;
5306 rtx libfunc;
5308 if (to_mode == from_mode)
5310 emit_move_insn (to, from);
5311 return;
5314 if (uintp)
5316 tab = satp ? satfractuns_optab : fractuns_optab;
5317 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5319 else
5321 tab = satp ? satfract_optab : fract_optab;
5322 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5324 code = tab->handlers[to_mode][from_mode].insn_code;
5325 if (code != CODE_FOR_nothing)
5327 emit_unop_insn (code, to, from, this_code);
5328 return;
5331 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5332 gcc_assert (libfunc);
5334 start_sequence ();
5335 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5336 1, from, from_mode);
5337 insns = get_insns ();
5338 end_sequence ();
5340 emit_libcall_block (insns, to, value,
5341 gen_rtx_fmt_e (tab->code, to_mode, from));
5344 /* Generate code to convert FROM to fixed point and store in TO. FROM
5345 must be floating point, TO must be signed. Use the conversion optab
5346 TAB to do the conversion. */
5348 bool
5349 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5351 enum insn_code icode;
5352 rtx target = to;
5353 enum machine_mode fmode, imode;
5355 /* We first try to find a pair of modes, one real and one integer, at
5356 least as wide as FROM and TO, respectively, in which we can open-code
5357 this conversion. If the integer mode is wider than the mode of TO,
5358 we can do the conversion either signed or unsigned. */
5360 for (fmode = GET_MODE (from); fmode != VOIDmode;
5361 fmode = GET_MODE_WIDER_MODE (fmode))
5362 for (imode = GET_MODE (to); imode != VOIDmode;
5363 imode = GET_MODE_WIDER_MODE (imode))
5365 icode = convert_optab_handler (tab, imode, fmode)->insn_code;
5366 if (icode != CODE_FOR_nothing)
5368 if (fmode != GET_MODE (from))
5369 from = convert_to_mode (fmode, from, 0);
5371 if (imode != GET_MODE (to))
5372 target = gen_reg_rtx (imode);
5374 emit_unop_insn (icode, target, from, UNKNOWN);
5375 if (target != to)
5376 convert_move (to, target, 0);
5377 return true;
5381 return false;
5384 /* Report whether we have an instruction to perform the operation
5385 specified by CODE on operands of mode MODE. */
5387 have_insn_for (enum rtx_code code, enum machine_mode mode)
5389 return (code_to_optab[(int) code] != 0
5390 && (optab_handler (code_to_optab[(int) code], mode)->insn_code
5391 != CODE_FOR_nothing));
5394 /* Set all insn_code fields to CODE_FOR_nothing. */
5396 static void
5397 init_insn_codes (void)
5399 unsigned int i;
5401 for (i = 0; i < (unsigned int) OTI_MAX; i++)
5403 unsigned int j;
5404 optab op;
5406 op = &optab_table[i];
5407 for (j = 0; j < NUM_MACHINE_MODES; j++)
5408 optab_handler (op, j)->insn_code = CODE_FOR_nothing;
5410 for (i = 0; i < (unsigned int) COI_MAX; i++)
5412 unsigned int j, k;
5413 convert_optab op;
5415 op = &convert_optab_table[i];
5416 for (j = 0; j < NUM_MACHINE_MODES; j++)
5417 for (k = 0; k < NUM_MACHINE_MODES; k++)
5418 convert_optab_handler (op, j, k)->insn_code = CODE_FOR_nothing;
5422 /* Initialize OP's code to CODE, and write it into the code_to_optab table. */
5423 static inline void
5424 init_optab (optab op, enum rtx_code code)
5426 op->code = code;
5427 code_to_optab[(int) code] = op;
5430 /* Same, but fill in its code as CODE, and do _not_ write it into
5431 the code_to_optab table. */
5432 static inline void
5433 init_optabv (optab op, enum rtx_code code)
5435 op->code = code;
5438 /* Conversion optabs never go in the code_to_optab table. */
5439 static void
5440 init_convert_optab (convert_optab op, enum rtx_code code)
5442 op->code = code;
5445 /* Initialize the libfunc fields of an entire group of entries in some
5446 optab. Each entry is set equal to a string consisting of a leading
5447 pair of underscores followed by a generic operation name followed by
5448 a mode name (downshifted to lowercase) followed by a single character
5449 representing the number of operands for the given operation (which is
5450 usually one of the characters '2', '3', or '4').
5452 OPTABLE is the table in which libfunc fields are to be initialized.
5453 OPNAME is the generic (string) name of the operation.
5454 SUFFIX is the character which specifies the number of operands for
5455 the given generic operation.
5456 MODE is the mode to generate for.
5459 static void
5460 gen_libfunc (optab optable, const char *opname, int suffix, enum machine_mode mode)
5462 unsigned opname_len = strlen (opname);
5463 const char *mname = GET_MODE_NAME (mode);
5464 unsigned mname_len = strlen (mname);
5465 char *libfunc_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5466 char *p;
5467 const char *q;
5469 p = libfunc_name;
5470 *p++ = '_';
5471 *p++ = '_';
5472 for (q = opname; *q; )
5473 *p++ = *q++;
5474 for (q = mname; *q; q++)
5475 *p++ = TOLOWER (*q);
5476 *p++ = suffix;
5477 *p = '\0';
5479 set_optab_libfunc (optable, mode,
5480 ggc_alloc_string (libfunc_name, p - libfunc_name));
5483 /* Like gen_libfunc, but verify that integer operation is involved. */
5485 static void
5486 gen_int_libfunc (optab optable, const char *opname, char suffix,
5487 enum machine_mode mode)
5489 int maxsize = 2 * BITS_PER_WORD;
5491 if (GET_MODE_CLASS (mode) != MODE_INT)
5492 return;
5493 if (maxsize < LONG_LONG_TYPE_SIZE)
5494 maxsize = LONG_LONG_TYPE_SIZE;
5495 if (GET_MODE_CLASS (mode) != MODE_INT
5496 || mode < word_mode || GET_MODE_BITSIZE (mode) > maxsize)
5497 return;
5498 gen_libfunc (optable, opname, suffix, mode);
5501 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5503 static void
5504 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5505 enum machine_mode mode)
5507 char *dec_opname;
5509 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5510 gen_libfunc (optable, opname, suffix, mode);
5511 if (DECIMAL_FLOAT_MODE_P (mode))
5513 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5514 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5515 depending on the low level floating format used. */
5516 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5517 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5518 gen_libfunc (optable, dec_opname, suffix, mode);
5522 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5524 static void
5525 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5526 enum machine_mode mode)
5528 if (!ALL_FIXED_POINT_MODE_P (mode))
5529 return;
5530 gen_libfunc (optable, opname, suffix, mode);
5533 /* Like gen_libfunc, but verify that signed fixed-point operation is
5534 involved. */
5536 static void
5537 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5538 enum machine_mode mode)
5540 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5541 return;
5542 gen_libfunc (optable, opname, suffix, mode);
5545 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5546 involved. */
5548 static void
5549 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5550 enum machine_mode mode)
5552 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5553 return;
5554 gen_libfunc (optable, opname, suffix, mode);
5557 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5559 static void
5560 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5561 enum machine_mode mode)
5563 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5564 gen_fp_libfunc (optable, name, suffix, mode);
5565 if (INTEGRAL_MODE_P (mode))
5566 gen_int_libfunc (optable, name, suffix, mode);
5569 /* Like gen_libfunc, but verify that FP or INT operation is involved
5570 and add 'v' suffix for integer operation. */
5572 static void
5573 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5574 enum machine_mode mode)
5576 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5577 gen_fp_libfunc (optable, name, suffix, mode);
5578 if (GET_MODE_CLASS (mode) == MODE_INT)
5580 int len = strlen (name);
5581 char *v_name = XALLOCAVEC (char, len + 2);
5582 strcpy (v_name, name);
5583 v_name[len] = 'v';
5584 v_name[len + 1] = 0;
5585 gen_int_libfunc (optable, v_name, suffix, mode);
5589 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5590 involved. */
5592 static void
5593 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5594 enum machine_mode mode)
5596 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5597 gen_fp_libfunc (optable, name, suffix, mode);
5598 if (INTEGRAL_MODE_P (mode))
5599 gen_int_libfunc (optable, name, suffix, mode);
5600 if (ALL_FIXED_POINT_MODE_P (mode))
5601 gen_fixed_libfunc (optable, name, suffix, mode);
5604 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5605 involved. */
5607 static void
5608 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5609 enum machine_mode mode)
5611 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5612 gen_fp_libfunc (optable, name, suffix, mode);
5613 if (INTEGRAL_MODE_P (mode))
5614 gen_int_libfunc (optable, name, suffix, mode);
5615 if (SIGNED_FIXED_POINT_MODE_P (mode))
5616 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5619 /* Like gen_libfunc, but verify that INT or FIXED operation is
5620 involved. */
5622 static void
5623 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5624 enum machine_mode mode)
5626 if (INTEGRAL_MODE_P (mode))
5627 gen_int_libfunc (optable, name, suffix, mode);
5628 if (ALL_FIXED_POINT_MODE_P (mode))
5629 gen_fixed_libfunc (optable, name, suffix, mode);
5632 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5633 involved. */
5635 static void
5636 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5637 enum machine_mode mode)
5639 if (INTEGRAL_MODE_P (mode))
5640 gen_int_libfunc (optable, name, suffix, mode);
5641 if (SIGNED_FIXED_POINT_MODE_P (mode))
5642 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5645 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5646 involved. */
5648 static void
5649 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5650 enum machine_mode mode)
5652 if (INTEGRAL_MODE_P (mode))
5653 gen_int_libfunc (optable, name, suffix, mode);
5654 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5655 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5658 /* Initialize the libfunc fields of an entire group of entries of an
5659 inter-mode-class conversion optab. The string formation rules are
5660 similar to the ones for init_libfuncs, above, but instead of having
5661 a mode name and an operand count these functions have two mode names
5662 and no operand count. */
5664 static void
5665 gen_interclass_conv_libfunc (convert_optab tab,
5666 const char *opname,
5667 enum machine_mode tmode,
5668 enum machine_mode fmode)
5670 size_t opname_len = strlen (opname);
5671 size_t mname_len = 0;
5673 const char *fname, *tname;
5674 const char *q;
5675 char *libfunc_name, *suffix;
5676 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5677 char *p;
5679 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5680 depends on which underlying decimal floating point format is used. */
5681 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5683 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5685 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5686 nondec_name[0] = '_';
5687 nondec_name[1] = '_';
5688 memcpy (&nondec_name[2], opname, opname_len);
5689 nondec_suffix = nondec_name + opname_len + 2;
5691 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5692 dec_name[0] = '_';
5693 dec_name[1] = '_';
5694 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5695 memcpy (&dec_name[2+dec_len], opname, opname_len);
5696 dec_suffix = dec_name + dec_len + opname_len + 2;
5698 fname = GET_MODE_NAME (fmode);
5699 tname = GET_MODE_NAME (tmode);
5701 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5703 libfunc_name = dec_name;
5704 suffix = dec_suffix;
5706 else
5708 libfunc_name = nondec_name;
5709 suffix = nondec_suffix;
5712 p = suffix;
5713 for (q = fname; *q; p++, q++)
5714 *p = TOLOWER (*q);
5715 for (q = tname; *q; p++, q++)
5716 *p = TOLOWER (*q);
5718 *p = '\0';
5720 set_conv_libfunc (tab, tmode, fmode,
5721 ggc_alloc_string (libfunc_name, p - libfunc_name));
5724 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5725 int->fp conversion. */
5727 static void
5728 gen_int_to_fp_conv_libfunc (convert_optab tab,
5729 const char *opname,
5730 enum machine_mode tmode,
5731 enum machine_mode fmode)
5733 if (GET_MODE_CLASS (fmode) != MODE_INT)
5734 return;
5735 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5736 return;
5737 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5740 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5741 naming scheme. */
5743 static void
5744 gen_ufloat_conv_libfunc (convert_optab tab,
5745 const char *opname ATTRIBUTE_UNUSED,
5746 enum machine_mode tmode,
5747 enum machine_mode fmode)
5749 if (DECIMAL_FLOAT_MODE_P (tmode))
5750 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5751 else
5752 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5755 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5756 fp->int conversion. */
5758 static void
5759 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5760 const char *opname,
5761 enum machine_mode tmode,
5762 enum machine_mode fmode)
5764 if (GET_MODE_CLASS (fmode) != MODE_INT)
5765 return;
5766 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5767 return;
5768 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5771 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5772 fp->int conversion with no decimal floating point involved. */
5774 static void
5775 gen_fp_to_int_conv_libfunc (convert_optab tab,
5776 const char *opname,
5777 enum machine_mode tmode,
5778 enum machine_mode fmode)
5780 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5781 return;
5782 if (GET_MODE_CLASS (tmode) != MODE_INT)
5783 return;
5784 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5787 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5788 The string formation rules are
5789 similar to the ones for init_libfunc, above. */
5791 static void
5792 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5793 enum machine_mode tmode, enum machine_mode fmode)
5795 size_t opname_len = strlen (opname);
5796 size_t mname_len = 0;
5798 const char *fname, *tname;
5799 const char *q;
5800 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5801 char *libfunc_name, *suffix;
5802 char *p;
5804 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5805 depends on which underlying decimal floating point format is used. */
5806 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5808 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5810 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5811 nondec_name[0] = '_';
5812 nondec_name[1] = '_';
5813 memcpy (&nondec_name[2], opname, opname_len);
5814 nondec_suffix = nondec_name + opname_len + 2;
5816 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5817 dec_name[0] = '_';
5818 dec_name[1] = '_';
5819 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5820 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5821 dec_suffix = dec_name + dec_len + opname_len + 2;
5823 fname = GET_MODE_NAME (fmode);
5824 tname = GET_MODE_NAME (tmode);
5826 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5828 libfunc_name = dec_name;
5829 suffix = dec_suffix;
5831 else
5833 libfunc_name = nondec_name;
5834 suffix = nondec_suffix;
5837 p = suffix;
5838 for (q = fname; *q; p++, q++)
5839 *p = TOLOWER (*q);
5840 for (q = tname; *q; p++, q++)
5841 *p = TOLOWER (*q);
5843 *p++ = '2';
5844 *p = '\0';
5846 set_conv_libfunc (tab, tmode, fmode,
5847 ggc_alloc_string (libfunc_name, p - libfunc_name));
5850 /* Pick proper libcall for trunc_optab. We need to chose if we do
5851 truncation or extension and interclass or intraclass. */
5853 static void
5854 gen_trunc_conv_libfunc (convert_optab tab,
5855 const char *opname,
5856 enum machine_mode tmode,
5857 enum machine_mode fmode)
5859 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5860 return;
5861 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5862 return;
5863 if (tmode == fmode)
5864 return;
5866 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5867 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5868 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5870 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5871 return;
5873 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5874 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5875 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5876 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5879 /* Pick proper libcall for extend_optab. We need to chose if we do
5880 truncation or extension and interclass or intraclass. */
5882 static void
5883 gen_extend_conv_libfunc (convert_optab tab,
5884 const char *opname ATTRIBUTE_UNUSED,
5885 enum machine_mode tmode,
5886 enum machine_mode fmode)
5888 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5889 return;
5890 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5891 return;
5892 if (tmode == fmode)
5893 return;
5895 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5896 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5897 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5899 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5900 return;
5902 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5903 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5904 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5905 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5908 /* Pick proper libcall for fract_optab. We need to chose if we do
5909 interclass or intraclass. */
5911 static void
5912 gen_fract_conv_libfunc (convert_optab tab,
5913 const char *opname,
5914 enum machine_mode tmode,
5915 enum machine_mode fmode)
5917 if (tmode == fmode)
5918 return;
5919 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5920 return;
5922 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5923 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5924 else
5925 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5928 /* Pick proper libcall for fractuns_optab. */
5930 static void
5931 gen_fractuns_conv_libfunc (convert_optab tab,
5932 const char *opname,
5933 enum machine_mode tmode,
5934 enum machine_mode fmode)
5936 if (tmode == fmode)
5937 return;
5938 /* One mode must be a fixed-point mode, and the other must be an integer
5939 mode. */
5940 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
5941 || (ALL_FIXED_POINT_MODE_P (fmode)
5942 && GET_MODE_CLASS (tmode) == MODE_INT)))
5943 return;
5945 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5948 /* Pick proper libcall for satfract_optab. We need to chose if we do
5949 interclass or intraclass. */
5951 static void
5952 gen_satfract_conv_libfunc (convert_optab tab,
5953 const char *opname,
5954 enum machine_mode tmode,
5955 enum machine_mode fmode)
5957 if (tmode == fmode)
5958 return;
5959 /* TMODE must be a fixed-point mode. */
5960 if (!ALL_FIXED_POINT_MODE_P (tmode))
5961 return;
5963 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5964 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5965 else
5966 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5969 /* Pick proper libcall for satfractuns_optab. */
5971 static void
5972 gen_satfractuns_conv_libfunc (convert_optab tab,
5973 const char *opname,
5974 enum machine_mode tmode,
5975 enum machine_mode fmode)
5977 if (tmode == fmode)
5978 return;
5979 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
5980 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
5981 return;
5983 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5986 /* A table of previously-created libfuncs, hashed by name. */
5987 static GTY ((param_is (union tree_node))) htab_t libfunc_decls;
5989 /* Hashtable callbacks for libfunc_decls. */
5991 static hashval_t
5992 libfunc_decl_hash (const void *entry)
5994 return htab_hash_string (IDENTIFIER_POINTER (DECL_NAME ((const_tree) entry)));
5997 static int
5998 libfunc_decl_eq (const void *entry1, const void *entry2)
6000 return DECL_NAME ((const_tree) entry1) == (const_tree) entry2;
6004 init_one_libfunc (const char *name)
6006 tree id, decl;
6007 void **slot;
6008 hashval_t hash;
6010 if (libfunc_decls == NULL)
6011 libfunc_decls = htab_create_ggc (37, libfunc_decl_hash,
6012 libfunc_decl_eq, NULL);
6014 /* See if we have already created a libfunc decl for this function. */
6015 id = get_identifier (name);
6016 hash = htab_hash_string (name);
6017 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, INSERT);
6018 decl = (tree) *slot;
6019 if (decl == NULL)
6021 /* Create a new decl, so that it can be passed to
6022 targetm.encode_section_info. */
6023 /* ??? We don't have any type information except for this is
6024 a function. Pretend this is "int foo()". */
6025 decl = build_decl (FUNCTION_DECL, get_identifier (name),
6026 build_function_type (integer_type_node, NULL_TREE));
6027 DECL_ARTIFICIAL (decl) = 1;
6028 DECL_EXTERNAL (decl) = 1;
6029 TREE_PUBLIC (decl) = 1;
6031 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6032 are the flags assigned by targetm.encode_section_info. */
6033 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6035 *slot = decl;
6037 return XEXP (DECL_RTL (decl), 0);
6040 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6041 MODE to NAME, which should be either 0 or a string constant. */
6042 void
6043 set_optab_libfunc (optab optable, enum machine_mode mode, const char *name)
6045 rtx val;
6046 struct libfunc_entry e;
6047 struct libfunc_entry **slot;
6048 e.optab = (size_t) (optable - &optab_table[0]);
6049 e.mode1 = mode;
6050 e.mode2 = VOIDmode;
6052 if (name)
6053 val = init_one_libfunc (name);
6054 else
6055 val = 0;
6056 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6057 if (*slot == NULL)
6058 *slot = GGC_NEW (struct libfunc_entry);
6059 (*slot)->optab = (size_t) (optable - &optab_table[0]);
6060 (*slot)->mode1 = mode;
6061 (*slot)->mode2 = VOIDmode;
6062 (*slot)->libfunc = val;
6065 /* Call this to reset the function entry for one conversion optab
6066 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6067 either 0 or a string constant. */
6068 void
6069 set_conv_libfunc (convert_optab optable, enum machine_mode tmode,
6070 enum machine_mode fmode, const char *name)
6072 rtx val;
6073 struct libfunc_entry e;
6074 struct libfunc_entry **slot;
6075 e.optab = (size_t) (optable - &convert_optab_table[0]);
6076 e.mode1 = tmode;
6077 e.mode2 = fmode;
6079 if (name)
6080 val = init_one_libfunc (name);
6081 else
6082 val = 0;
6083 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6084 if (*slot == NULL)
6085 *slot = GGC_NEW (struct libfunc_entry);
6086 (*slot)->optab = (size_t) (optable - &convert_optab_table[0]);
6087 (*slot)->mode1 = tmode;
6088 (*slot)->mode2 = fmode;
6089 (*slot)->libfunc = val;
6092 /* Call this to initialize the contents of the optabs
6093 appropriately for the current target machine. */
6095 void
6096 init_optabs (void)
6098 unsigned int i;
6099 enum machine_mode int_mode;
6100 static bool reinit;
6102 libfunc_hash = htab_create_ggc (10, hash_libfunc, eq_libfunc, NULL);
6103 /* Start by initializing all tables to contain CODE_FOR_nothing. */
6105 for (i = 0; i < NUM_RTX_CODE; i++)
6106 setcc_gen_code[i] = CODE_FOR_nothing;
6108 #ifdef HAVE_conditional_move
6109 for (i = 0; i < NUM_MACHINE_MODES; i++)
6110 movcc_gen_code[i] = CODE_FOR_nothing;
6111 #endif
6113 for (i = 0; i < NUM_MACHINE_MODES; i++)
6115 vcond_gen_code[i] = CODE_FOR_nothing;
6116 vcondu_gen_code[i] = CODE_FOR_nothing;
6119 #if GCC_VERSION >= 4000
6120 /* We statically initialize the insn_codes with CODE_FOR_nothing. */
6121 if (reinit)
6122 init_insn_codes ();
6123 #else
6124 init_insn_codes ();
6125 #endif
6127 init_optab (add_optab, PLUS);
6128 init_optabv (addv_optab, PLUS);
6129 init_optab (sub_optab, MINUS);
6130 init_optabv (subv_optab, MINUS);
6131 init_optab (ssadd_optab, SS_PLUS);
6132 init_optab (usadd_optab, US_PLUS);
6133 init_optab (sssub_optab, SS_MINUS);
6134 init_optab (ussub_optab, US_MINUS);
6135 init_optab (smul_optab, MULT);
6136 init_optab (ssmul_optab, SS_MULT);
6137 init_optab (usmul_optab, US_MULT);
6138 init_optabv (smulv_optab, MULT);
6139 init_optab (smul_highpart_optab, UNKNOWN);
6140 init_optab (umul_highpart_optab, UNKNOWN);
6141 init_optab (smul_widen_optab, UNKNOWN);
6142 init_optab (umul_widen_optab, UNKNOWN);
6143 init_optab (usmul_widen_optab, UNKNOWN);
6144 init_optab (smadd_widen_optab, UNKNOWN);
6145 init_optab (umadd_widen_optab, UNKNOWN);
6146 init_optab (ssmadd_widen_optab, UNKNOWN);
6147 init_optab (usmadd_widen_optab, UNKNOWN);
6148 init_optab (smsub_widen_optab, UNKNOWN);
6149 init_optab (umsub_widen_optab, UNKNOWN);
6150 init_optab (ssmsub_widen_optab, UNKNOWN);
6151 init_optab (usmsub_widen_optab, UNKNOWN);
6152 init_optab (sdiv_optab, DIV);
6153 init_optab (ssdiv_optab, SS_DIV);
6154 init_optab (usdiv_optab, US_DIV);
6155 init_optabv (sdivv_optab, DIV);
6156 init_optab (sdivmod_optab, UNKNOWN);
6157 init_optab (udiv_optab, UDIV);
6158 init_optab (udivmod_optab, UNKNOWN);
6159 init_optab (smod_optab, MOD);
6160 init_optab (umod_optab, UMOD);
6161 init_optab (fmod_optab, UNKNOWN);
6162 init_optab (remainder_optab, UNKNOWN);
6163 init_optab (ftrunc_optab, UNKNOWN);
6164 init_optab (and_optab, AND);
6165 init_optab (ior_optab, IOR);
6166 init_optab (xor_optab, XOR);
6167 init_optab (ashl_optab, ASHIFT);
6168 init_optab (ssashl_optab, SS_ASHIFT);
6169 init_optab (usashl_optab, US_ASHIFT);
6170 init_optab (ashr_optab, ASHIFTRT);
6171 init_optab (lshr_optab, LSHIFTRT);
6172 init_optab (rotl_optab, ROTATE);
6173 init_optab (rotr_optab, ROTATERT);
6174 init_optab (smin_optab, SMIN);
6175 init_optab (smax_optab, SMAX);
6176 init_optab (umin_optab, UMIN);
6177 init_optab (umax_optab, UMAX);
6178 init_optab (pow_optab, UNKNOWN);
6179 init_optab (atan2_optab, UNKNOWN);
6181 /* These three have codes assigned exclusively for the sake of
6182 have_insn_for. */
6183 init_optab (mov_optab, SET);
6184 init_optab (movstrict_optab, STRICT_LOW_PART);
6185 init_optab (cmp_optab, COMPARE);
6187 init_optab (storent_optab, UNKNOWN);
6189 init_optab (ucmp_optab, UNKNOWN);
6190 init_optab (tst_optab, UNKNOWN);
6192 init_optab (eq_optab, EQ);
6193 init_optab (ne_optab, NE);
6194 init_optab (gt_optab, GT);
6195 init_optab (ge_optab, GE);
6196 init_optab (lt_optab, LT);
6197 init_optab (le_optab, LE);
6198 init_optab (unord_optab, UNORDERED);
6200 init_optab (neg_optab, NEG);
6201 init_optab (ssneg_optab, SS_NEG);
6202 init_optab (usneg_optab, US_NEG);
6203 init_optabv (negv_optab, NEG);
6204 init_optab (abs_optab, ABS);
6205 init_optabv (absv_optab, ABS);
6206 init_optab (addcc_optab, UNKNOWN);
6207 init_optab (one_cmpl_optab, NOT);
6208 init_optab (bswap_optab, BSWAP);
6209 init_optab (ffs_optab, FFS);
6210 init_optab (clz_optab, CLZ);
6211 init_optab (ctz_optab, CTZ);
6212 init_optab (popcount_optab, POPCOUNT);
6213 init_optab (parity_optab, PARITY);
6214 init_optab (sqrt_optab, SQRT);
6215 init_optab (floor_optab, UNKNOWN);
6216 init_optab (ceil_optab, UNKNOWN);
6217 init_optab (round_optab, UNKNOWN);
6218 init_optab (btrunc_optab, UNKNOWN);
6219 init_optab (nearbyint_optab, UNKNOWN);
6220 init_optab (rint_optab, UNKNOWN);
6221 init_optab (sincos_optab, UNKNOWN);
6222 init_optab (sin_optab, UNKNOWN);
6223 init_optab (asin_optab, UNKNOWN);
6224 init_optab (cos_optab, UNKNOWN);
6225 init_optab (acos_optab, UNKNOWN);
6226 init_optab (exp_optab, UNKNOWN);
6227 init_optab (exp10_optab, UNKNOWN);
6228 init_optab (exp2_optab, UNKNOWN);
6229 init_optab (expm1_optab, UNKNOWN);
6230 init_optab (ldexp_optab, UNKNOWN);
6231 init_optab (scalb_optab, UNKNOWN);
6232 init_optab (logb_optab, UNKNOWN);
6233 init_optab (ilogb_optab, UNKNOWN);
6234 init_optab (log_optab, UNKNOWN);
6235 init_optab (log10_optab, UNKNOWN);
6236 init_optab (log2_optab, UNKNOWN);
6237 init_optab (log1p_optab, UNKNOWN);
6238 init_optab (tan_optab, UNKNOWN);
6239 init_optab (atan_optab, UNKNOWN);
6240 init_optab (copysign_optab, UNKNOWN);
6241 init_optab (signbit_optab, UNKNOWN);
6243 init_optab (isinf_optab, UNKNOWN);
6245 init_optab (strlen_optab, UNKNOWN);
6246 init_optab (cbranch_optab, UNKNOWN);
6247 init_optab (cmov_optab, UNKNOWN);
6248 init_optab (cstore_optab, UNKNOWN);
6249 init_optab (push_optab, UNKNOWN);
6251 init_optab (reduc_smax_optab, UNKNOWN);
6252 init_optab (reduc_umax_optab, UNKNOWN);
6253 init_optab (reduc_smin_optab, UNKNOWN);
6254 init_optab (reduc_umin_optab, UNKNOWN);
6255 init_optab (reduc_splus_optab, UNKNOWN);
6256 init_optab (reduc_uplus_optab, UNKNOWN);
6258 init_optab (ssum_widen_optab, UNKNOWN);
6259 init_optab (usum_widen_optab, UNKNOWN);
6260 init_optab (sdot_prod_optab, UNKNOWN);
6261 init_optab (udot_prod_optab, UNKNOWN);
6263 init_optab (vec_extract_optab, UNKNOWN);
6264 init_optab (vec_extract_even_optab, UNKNOWN);
6265 init_optab (vec_extract_odd_optab, UNKNOWN);
6266 init_optab (vec_interleave_high_optab, UNKNOWN);
6267 init_optab (vec_interleave_low_optab, UNKNOWN);
6268 init_optab (vec_set_optab, UNKNOWN);
6269 init_optab (vec_init_optab, UNKNOWN);
6270 init_optab (vec_shl_optab, UNKNOWN);
6271 init_optab (vec_shr_optab, UNKNOWN);
6272 init_optab (vec_realign_load_optab, UNKNOWN);
6273 init_optab (movmisalign_optab, UNKNOWN);
6274 init_optab (vec_widen_umult_hi_optab, UNKNOWN);
6275 init_optab (vec_widen_umult_lo_optab, UNKNOWN);
6276 init_optab (vec_widen_smult_hi_optab, UNKNOWN);
6277 init_optab (vec_widen_smult_lo_optab, UNKNOWN);
6278 init_optab (vec_unpacks_hi_optab, UNKNOWN);
6279 init_optab (vec_unpacks_lo_optab, UNKNOWN);
6280 init_optab (vec_unpacku_hi_optab, UNKNOWN);
6281 init_optab (vec_unpacku_lo_optab, UNKNOWN);
6282 init_optab (vec_unpacks_float_hi_optab, UNKNOWN);
6283 init_optab (vec_unpacks_float_lo_optab, UNKNOWN);
6284 init_optab (vec_unpacku_float_hi_optab, UNKNOWN);
6285 init_optab (vec_unpacku_float_lo_optab, UNKNOWN);
6286 init_optab (vec_pack_trunc_optab, UNKNOWN);
6287 init_optab (vec_pack_usat_optab, UNKNOWN);
6288 init_optab (vec_pack_ssat_optab, UNKNOWN);
6289 init_optab (vec_pack_ufix_trunc_optab, UNKNOWN);
6290 init_optab (vec_pack_sfix_trunc_optab, UNKNOWN);
6292 init_optab (powi_optab, UNKNOWN);
6294 /* Conversions. */
6295 init_convert_optab (sext_optab, SIGN_EXTEND);
6296 init_convert_optab (zext_optab, ZERO_EXTEND);
6297 init_convert_optab (trunc_optab, TRUNCATE);
6298 init_convert_optab (sfix_optab, FIX);
6299 init_convert_optab (ufix_optab, UNSIGNED_FIX);
6300 init_convert_optab (sfixtrunc_optab, UNKNOWN);
6301 init_convert_optab (ufixtrunc_optab, UNKNOWN);
6302 init_convert_optab (sfloat_optab, FLOAT);
6303 init_convert_optab (ufloat_optab, UNSIGNED_FLOAT);
6304 init_convert_optab (lrint_optab, UNKNOWN);
6305 init_convert_optab (lround_optab, UNKNOWN);
6306 init_convert_optab (lfloor_optab, UNKNOWN);
6307 init_convert_optab (lceil_optab, UNKNOWN);
6309 init_convert_optab (fract_optab, FRACT_CONVERT);
6310 init_convert_optab (fractuns_optab, UNSIGNED_FRACT_CONVERT);
6311 init_convert_optab (satfract_optab, SAT_FRACT);
6312 init_convert_optab (satfractuns_optab, UNSIGNED_SAT_FRACT);
6314 for (i = 0; i < NUM_MACHINE_MODES; i++)
6316 movmem_optab[i] = CODE_FOR_nothing;
6317 cmpstr_optab[i] = CODE_FOR_nothing;
6318 cmpstrn_optab[i] = CODE_FOR_nothing;
6319 cmpmem_optab[i] = CODE_FOR_nothing;
6320 setmem_optab[i] = CODE_FOR_nothing;
6322 sync_add_optab[i] = CODE_FOR_nothing;
6323 sync_sub_optab[i] = CODE_FOR_nothing;
6324 sync_ior_optab[i] = CODE_FOR_nothing;
6325 sync_and_optab[i] = CODE_FOR_nothing;
6326 sync_xor_optab[i] = CODE_FOR_nothing;
6327 sync_nand_optab[i] = CODE_FOR_nothing;
6328 sync_old_add_optab[i] = CODE_FOR_nothing;
6329 sync_old_sub_optab[i] = CODE_FOR_nothing;
6330 sync_old_ior_optab[i] = CODE_FOR_nothing;
6331 sync_old_and_optab[i] = CODE_FOR_nothing;
6332 sync_old_xor_optab[i] = CODE_FOR_nothing;
6333 sync_old_nand_optab[i] = CODE_FOR_nothing;
6334 sync_new_add_optab[i] = CODE_FOR_nothing;
6335 sync_new_sub_optab[i] = CODE_FOR_nothing;
6336 sync_new_ior_optab[i] = CODE_FOR_nothing;
6337 sync_new_and_optab[i] = CODE_FOR_nothing;
6338 sync_new_xor_optab[i] = CODE_FOR_nothing;
6339 sync_new_nand_optab[i] = CODE_FOR_nothing;
6340 sync_compare_and_swap[i] = CODE_FOR_nothing;
6341 sync_compare_and_swap_cc[i] = CODE_FOR_nothing;
6342 sync_lock_test_and_set[i] = CODE_FOR_nothing;
6343 sync_lock_release[i] = CODE_FOR_nothing;
6345 reload_in_optab[i] = reload_out_optab[i] = CODE_FOR_nothing;
6348 /* Fill in the optabs with the insns we support. */
6349 init_all_optabs ();
6351 /* Initialize the optabs with the names of the library functions. */
6352 add_optab->libcall_basename = "add";
6353 add_optab->libcall_suffix = '3';
6354 add_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6355 addv_optab->libcall_basename = "add";
6356 addv_optab->libcall_suffix = '3';
6357 addv_optab->libcall_gen = gen_intv_fp_libfunc;
6358 ssadd_optab->libcall_basename = "ssadd";
6359 ssadd_optab->libcall_suffix = '3';
6360 ssadd_optab->libcall_gen = gen_signed_fixed_libfunc;
6361 usadd_optab->libcall_basename = "usadd";
6362 usadd_optab->libcall_suffix = '3';
6363 usadd_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6364 sub_optab->libcall_basename = "sub";
6365 sub_optab->libcall_suffix = '3';
6366 sub_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6367 subv_optab->libcall_basename = "sub";
6368 subv_optab->libcall_suffix = '3';
6369 subv_optab->libcall_gen = gen_intv_fp_libfunc;
6370 sssub_optab->libcall_basename = "sssub";
6371 sssub_optab->libcall_suffix = '3';
6372 sssub_optab->libcall_gen = gen_signed_fixed_libfunc;
6373 ussub_optab->libcall_basename = "ussub";
6374 ussub_optab->libcall_suffix = '3';
6375 ussub_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6376 smul_optab->libcall_basename = "mul";
6377 smul_optab->libcall_suffix = '3';
6378 smul_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6379 smulv_optab->libcall_basename = "mul";
6380 smulv_optab->libcall_suffix = '3';
6381 smulv_optab->libcall_gen = gen_intv_fp_libfunc;
6382 ssmul_optab->libcall_basename = "ssmul";
6383 ssmul_optab->libcall_suffix = '3';
6384 ssmul_optab->libcall_gen = gen_signed_fixed_libfunc;
6385 usmul_optab->libcall_basename = "usmul";
6386 usmul_optab->libcall_suffix = '3';
6387 usmul_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6388 sdiv_optab->libcall_basename = "div";
6389 sdiv_optab->libcall_suffix = '3';
6390 sdiv_optab->libcall_gen = gen_int_fp_signed_fixed_libfunc;
6391 sdivv_optab->libcall_basename = "divv";
6392 sdivv_optab->libcall_suffix = '3';
6393 sdivv_optab->libcall_gen = gen_int_libfunc;
6394 ssdiv_optab->libcall_basename = "ssdiv";
6395 ssdiv_optab->libcall_suffix = '3';
6396 ssdiv_optab->libcall_gen = gen_signed_fixed_libfunc;
6397 udiv_optab->libcall_basename = "udiv";
6398 udiv_optab->libcall_suffix = '3';
6399 udiv_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6400 usdiv_optab->libcall_basename = "usdiv";
6401 usdiv_optab->libcall_suffix = '3';
6402 usdiv_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6403 sdivmod_optab->libcall_basename = "divmod";
6404 sdivmod_optab->libcall_suffix = '4';
6405 sdivmod_optab->libcall_gen = gen_int_libfunc;
6406 udivmod_optab->libcall_basename = "udivmod";
6407 udivmod_optab->libcall_suffix = '4';
6408 udivmod_optab->libcall_gen = gen_int_libfunc;
6409 smod_optab->libcall_basename = "mod";
6410 smod_optab->libcall_suffix = '3';
6411 smod_optab->libcall_gen = gen_int_libfunc;
6412 umod_optab->libcall_basename = "umod";
6413 umod_optab->libcall_suffix = '3';
6414 umod_optab->libcall_gen = gen_int_libfunc;
6415 ftrunc_optab->libcall_basename = "ftrunc";
6416 ftrunc_optab->libcall_suffix = '2';
6417 ftrunc_optab->libcall_gen = gen_fp_libfunc;
6418 and_optab->libcall_basename = "and";
6419 and_optab->libcall_suffix = '3';
6420 and_optab->libcall_gen = gen_int_libfunc;
6421 ior_optab->libcall_basename = "ior";
6422 ior_optab->libcall_suffix = '3';
6423 ior_optab->libcall_gen = gen_int_libfunc;
6424 xor_optab->libcall_basename = "xor";
6425 xor_optab->libcall_suffix = '3';
6426 xor_optab->libcall_gen = gen_int_libfunc;
6427 ashl_optab->libcall_basename = "ashl";
6428 ashl_optab->libcall_suffix = '3';
6429 ashl_optab->libcall_gen = gen_int_fixed_libfunc;
6430 ssashl_optab->libcall_basename = "ssashl";
6431 ssashl_optab->libcall_suffix = '3';
6432 ssashl_optab->libcall_gen = gen_signed_fixed_libfunc;
6433 usashl_optab->libcall_basename = "usashl";
6434 usashl_optab->libcall_suffix = '3';
6435 usashl_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6436 ashr_optab->libcall_basename = "ashr";
6437 ashr_optab->libcall_suffix = '3';
6438 ashr_optab->libcall_gen = gen_int_signed_fixed_libfunc;
6439 lshr_optab->libcall_basename = "lshr";
6440 lshr_optab->libcall_suffix = '3';
6441 lshr_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6442 smin_optab->libcall_basename = "min";
6443 smin_optab->libcall_suffix = '3';
6444 smin_optab->libcall_gen = gen_int_fp_libfunc;
6445 smax_optab->libcall_basename = "max";
6446 smax_optab->libcall_suffix = '3';
6447 smax_optab->libcall_gen = gen_int_fp_libfunc;
6448 umin_optab->libcall_basename = "umin";
6449 umin_optab->libcall_suffix = '3';
6450 umin_optab->libcall_gen = gen_int_libfunc;
6451 umax_optab->libcall_basename = "umax";
6452 umax_optab->libcall_suffix = '3';
6453 umax_optab->libcall_gen = gen_int_libfunc;
6454 neg_optab->libcall_basename = "neg";
6455 neg_optab->libcall_suffix = '2';
6456 neg_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6457 ssneg_optab->libcall_basename = "ssneg";
6458 ssneg_optab->libcall_suffix = '2';
6459 ssneg_optab->libcall_gen = gen_signed_fixed_libfunc;
6460 usneg_optab->libcall_basename = "usneg";
6461 usneg_optab->libcall_suffix = '2';
6462 usneg_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6463 negv_optab->libcall_basename = "neg";
6464 negv_optab->libcall_suffix = '2';
6465 negv_optab->libcall_gen = gen_intv_fp_libfunc;
6466 one_cmpl_optab->libcall_basename = "one_cmpl";
6467 one_cmpl_optab->libcall_suffix = '2';
6468 one_cmpl_optab->libcall_gen = gen_int_libfunc;
6469 ffs_optab->libcall_basename = "ffs";
6470 ffs_optab->libcall_suffix = '2';
6471 ffs_optab->libcall_gen = gen_int_libfunc;
6472 clz_optab->libcall_basename = "clz";
6473 clz_optab->libcall_suffix = '2';
6474 clz_optab->libcall_gen = gen_int_libfunc;
6475 ctz_optab->libcall_basename = "ctz";
6476 ctz_optab->libcall_suffix = '2';
6477 ctz_optab->libcall_gen = gen_int_libfunc;
6478 popcount_optab->libcall_basename = "popcount";
6479 popcount_optab->libcall_suffix = '2';
6480 popcount_optab->libcall_gen = gen_int_libfunc;
6481 parity_optab->libcall_basename = "parity";
6482 parity_optab->libcall_suffix = '2';
6483 parity_optab->libcall_gen = gen_int_libfunc;
6485 /* Comparison libcalls for integers MUST come in pairs,
6486 signed/unsigned. */
6487 cmp_optab->libcall_basename = "cmp";
6488 cmp_optab->libcall_suffix = '2';
6489 cmp_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6490 ucmp_optab->libcall_basename = "ucmp";
6491 ucmp_optab->libcall_suffix = '2';
6492 ucmp_optab->libcall_gen = gen_int_libfunc;
6494 /* EQ etc are floating point only. */
6495 eq_optab->libcall_basename = "eq";
6496 eq_optab->libcall_suffix = '2';
6497 eq_optab->libcall_gen = gen_fp_libfunc;
6498 ne_optab->libcall_basename = "ne";
6499 ne_optab->libcall_suffix = '2';
6500 ne_optab->libcall_gen = gen_fp_libfunc;
6501 gt_optab->libcall_basename = "gt";
6502 gt_optab->libcall_suffix = '2';
6503 gt_optab->libcall_gen = gen_fp_libfunc;
6504 ge_optab->libcall_basename = "ge";
6505 ge_optab->libcall_suffix = '2';
6506 ge_optab->libcall_gen = gen_fp_libfunc;
6507 lt_optab->libcall_basename = "lt";
6508 lt_optab->libcall_suffix = '2';
6509 lt_optab->libcall_gen = gen_fp_libfunc;
6510 le_optab->libcall_basename = "le";
6511 le_optab->libcall_suffix = '2';
6512 le_optab->libcall_gen = gen_fp_libfunc;
6513 unord_optab->libcall_basename = "unord";
6514 unord_optab->libcall_suffix = '2';
6515 unord_optab->libcall_gen = gen_fp_libfunc;
6517 powi_optab->libcall_basename = "powi";
6518 powi_optab->libcall_suffix = '2';
6519 powi_optab->libcall_gen = gen_fp_libfunc;
6521 /* Conversions. */
6522 sfloat_optab->libcall_basename = "float";
6523 sfloat_optab->libcall_gen = gen_int_to_fp_conv_libfunc;
6524 ufloat_optab->libcall_gen = gen_ufloat_conv_libfunc;
6525 sfix_optab->libcall_basename = "fix";
6526 sfix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6527 ufix_optab->libcall_basename = "fixuns";
6528 ufix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6529 lrint_optab->libcall_basename = "lrint";
6530 lrint_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6531 lround_optab->libcall_basename = "lround";
6532 lround_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6533 lfloor_optab->libcall_basename = "lfloor";
6534 lfloor_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6535 lceil_optab->libcall_basename = "lceil";
6536 lceil_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6538 /* trunc_optab is also used for FLOAT_EXTEND. */
6539 sext_optab->libcall_basename = "extend";
6540 sext_optab->libcall_gen = gen_extend_conv_libfunc;
6541 trunc_optab->libcall_basename = "trunc";
6542 trunc_optab->libcall_gen = gen_trunc_conv_libfunc;
6544 /* Conversions for fixed-point modes and other modes. */
6545 fract_optab->libcall_basename = "fract";
6546 fract_optab->libcall_gen = gen_fract_conv_libfunc;
6547 satfract_optab->libcall_basename = "satfract";
6548 satfract_optab->libcall_gen = gen_satfract_conv_libfunc;
6549 fractuns_optab->libcall_basename = "fractuns";
6550 fractuns_optab->libcall_gen = gen_fractuns_conv_libfunc;
6551 satfractuns_optab->libcall_basename = "satfractuns";
6552 satfractuns_optab->libcall_gen = gen_satfractuns_conv_libfunc;
6554 /* The ffs function operates on `int'. Fall back on it if we do not
6555 have a libgcc2 function for that width. */
6556 if (INT_TYPE_SIZE < BITS_PER_WORD)
6558 int_mode = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
6559 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6560 "ffs");
6563 /* Explicitly initialize the bswap libfuncs since we need them to be
6564 valid for things other than word_mode. */
6565 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6566 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6568 /* Use cabs for double complex abs, since systems generally have cabs.
6569 Don't define any libcall for float complex, so that cabs will be used. */
6570 if (complex_double_type_node)
6571 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node), "cabs");
6573 abort_libfunc = init_one_libfunc ("abort");
6574 memcpy_libfunc = init_one_libfunc ("memcpy");
6575 memmove_libfunc = init_one_libfunc ("memmove");
6576 memcmp_libfunc = init_one_libfunc ("memcmp");
6577 memset_libfunc = init_one_libfunc ("memset");
6578 setbits_libfunc = init_one_libfunc ("__setbits");
6580 #ifndef DONT_USE_BUILTIN_SETJMP
6581 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6582 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6583 #else
6584 setjmp_libfunc = init_one_libfunc ("setjmp");
6585 longjmp_libfunc = init_one_libfunc ("longjmp");
6586 #endif
6587 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6588 unwind_sjlj_unregister_libfunc
6589 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6591 /* For function entry/exit instrumentation. */
6592 profile_function_entry_libfunc
6593 = init_one_libfunc ("__cyg_profile_func_enter");
6594 profile_function_exit_libfunc
6595 = init_one_libfunc ("__cyg_profile_func_exit");
6597 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6599 if (HAVE_conditional_trap)
6600 trap_rtx = gen_rtx_fmt_ee (EQ, VOIDmode, NULL_RTX, NULL_RTX);
6602 /* Allow the target to add more libcalls or rename some, etc. */
6603 targetm.init_libfuncs ();
6605 reinit = true;
6608 /* Print information about the current contents of the optabs on
6609 STDERR. */
6611 void
6612 debug_optab_libfuncs (void)
6614 int i;
6615 int j;
6616 int k;
6618 /* Dump the arithmetic optabs. */
6619 for (i = 0; i != (int) OTI_MAX; i++)
6620 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6622 optab o;
6623 rtx l;
6625 o = &optab_table[i];
6626 l = optab_libfunc (o, j);
6627 if (l)
6629 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6630 fprintf (stderr, "%s\t%s:\t%s\n",
6631 GET_RTX_NAME (o->code),
6632 GET_MODE_NAME (j),
6633 XSTR (l, 0));
6637 /* Dump the conversion optabs. */
6638 for (i = 0; i < (int) COI_MAX; ++i)
6639 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6640 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6642 convert_optab o;
6643 rtx l;
6645 o = &convert_optab_table[i];
6646 l = convert_optab_libfunc (o, j, k);
6647 if (l)
6649 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6650 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6651 GET_RTX_NAME (o->code),
6652 GET_MODE_NAME (j),
6653 GET_MODE_NAME (k),
6654 XSTR (l, 0));
6660 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6661 CODE. Return 0 on failure. */
6664 gen_cond_trap (enum rtx_code code ATTRIBUTE_UNUSED, rtx op1,
6665 rtx op2 ATTRIBUTE_UNUSED, rtx tcode ATTRIBUTE_UNUSED)
6667 enum machine_mode mode = GET_MODE (op1);
6668 enum insn_code icode;
6669 rtx insn;
6671 if (!HAVE_conditional_trap)
6672 return 0;
6674 if (mode == VOIDmode)
6675 return 0;
6677 icode = optab_handler (cmp_optab, mode)->insn_code;
6678 if (icode == CODE_FOR_nothing)
6679 return 0;
6681 start_sequence ();
6682 op1 = prepare_operand (icode, op1, 0, mode, mode, 0);
6683 op2 = prepare_operand (icode, op2, 1, mode, mode, 0);
6684 if (!op1 || !op2)
6686 end_sequence ();
6687 return 0;
6689 emit_insn (GEN_FCN (icode) (op1, op2));
6691 PUT_CODE (trap_rtx, code);
6692 gcc_assert (HAVE_conditional_trap);
6693 insn = gen_conditional_trap (trap_rtx, tcode);
6694 if (insn)
6696 emit_insn (insn);
6697 insn = get_insns ();
6699 end_sequence ();
6701 return insn;
6704 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6705 or unsigned operation code. */
6707 static enum rtx_code
6708 get_rtx_code (enum tree_code tcode, bool unsignedp)
6710 enum rtx_code code;
6711 switch (tcode)
6713 case EQ_EXPR:
6714 code = EQ;
6715 break;
6716 case NE_EXPR:
6717 code = NE;
6718 break;
6719 case LT_EXPR:
6720 code = unsignedp ? LTU : LT;
6721 break;
6722 case LE_EXPR:
6723 code = unsignedp ? LEU : LE;
6724 break;
6725 case GT_EXPR:
6726 code = unsignedp ? GTU : GT;
6727 break;
6728 case GE_EXPR:
6729 code = unsignedp ? GEU : GE;
6730 break;
6732 case UNORDERED_EXPR:
6733 code = UNORDERED;
6734 break;
6735 case ORDERED_EXPR:
6736 code = ORDERED;
6737 break;
6738 case UNLT_EXPR:
6739 code = UNLT;
6740 break;
6741 case UNLE_EXPR:
6742 code = UNLE;
6743 break;
6744 case UNGT_EXPR:
6745 code = UNGT;
6746 break;
6747 case UNGE_EXPR:
6748 code = UNGE;
6749 break;
6750 case UNEQ_EXPR:
6751 code = UNEQ;
6752 break;
6753 case LTGT_EXPR:
6754 code = LTGT;
6755 break;
6757 default:
6758 gcc_unreachable ();
6760 return code;
6763 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6764 unsigned operators. Do not generate compare instruction. */
6766 static rtx
6767 vector_compare_rtx (tree cond, bool unsignedp, enum insn_code icode)
6769 enum rtx_code rcode;
6770 tree t_op0, t_op1;
6771 rtx rtx_op0, rtx_op1;
6773 /* This is unlikely. While generating VEC_COND_EXPR, auto vectorizer
6774 ensures that condition is a relational operation. */
6775 gcc_assert (COMPARISON_CLASS_P (cond));
6777 rcode = get_rtx_code (TREE_CODE (cond), unsignedp);
6778 t_op0 = TREE_OPERAND (cond, 0);
6779 t_op1 = TREE_OPERAND (cond, 1);
6781 /* Expand operands. */
6782 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6783 EXPAND_STACK_PARM);
6784 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6785 EXPAND_STACK_PARM);
6787 if (!insn_data[icode].operand[4].predicate (rtx_op0, GET_MODE (rtx_op0))
6788 && GET_MODE (rtx_op0) != VOIDmode)
6789 rtx_op0 = force_reg (GET_MODE (rtx_op0), rtx_op0);
6791 if (!insn_data[icode].operand[5].predicate (rtx_op1, GET_MODE (rtx_op1))
6792 && GET_MODE (rtx_op1) != VOIDmode)
6793 rtx_op1 = force_reg (GET_MODE (rtx_op1), rtx_op1);
6795 return gen_rtx_fmt_ee (rcode, VOIDmode, rtx_op0, rtx_op1);
6798 /* Return insn code for VEC_COND_EXPR EXPR. */
6800 static inline enum insn_code
6801 get_vcond_icode (tree expr, enum machine_mode mode)
6803 enum insn_code icode = CODE_FOR_nothing;
6805 if (TYPE_UNSIGNED (TREE_TYPE (expr)))
6806 icode = vcondu_gen_code[mode];
6807 else
6808 icode = vcond_gen_code[mode];
6809 return icode;
6812 /* Return TRUE iff, appropriate vector insns are available
6813 for vector cond expr expr in VMODE mode. */
6815 bool
6816 expand_vec_cond_expr_p (tree expr, enum machine_mode vmode)
6818 if (get_vcond_icode (expr, vmode) == CODE_FOR_nothing)
6819 return false;
6820 return true;
6823 /* Generate insns for VEC_COND_EXPR. */
6826 expand_vec_cond_expr (tree vec_cond_expr, rtx target)
6828 enum insn_code icode;
6829 rtx comparison, rtx_op1, rtx_op2, cc_op0, cc_op1;
6830 enum machine_mode mode = TYPE_MODE (TREE_TYPE (vec_cond_expr));
6831 bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (vec_cond_expr));
6833 icode = get_vcond_icode (vec_cond_expr, mode);
6834 if (icode == CODE_FOR_nothing)
6835 return 0;
6837 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
6838 target = gen_reg_rtx (mode);
6840 /* Get comparison rtx. First expand both cond expr operands. */
6841 comparison = vector_compare_rtx (TREE_OPERAND (vec_cond_expr, 0),
6842 unsignedp, icode);
6843 cc_op0 = XEXP (comparison, 0);
6844 cc_op1 = XEXP (comparison, 1);
6845 /* Expand both operands and force them in reg, if required. */
6846 rtx_op1 = expand_normal (TREE_OPERAND (vec_cond_expr, 1));
6847 if (!insn_data[icode].operand[1].predicate (rtx_op1, mode)
6848 && mode != VOIDmode)
6849 rtx_op1 = force_reg (mode, rtx_op1);
6851 rtx_op2 = expand_normal (TREE_OPERAND (vec_cond_expr, 2));
6852 if (!insn_data[icode].operand[2].predicate (rtx_op2, mode)
6853 && mode != VOIDmode)
6854 rtx_op2 = force_reg (mode, rtx_op2);
6856 /* Emit instruction! */
6857 emit_insn (GEN_FCN (icode) (target, rtx_op1, rtx_op2,
6858 comparison, cc_op0, cc_op1));
6860 return target;
6864 /* This is an internal subroutine of the other compare_and_swap expanders.
6865 MEM, OLD_VAL and NEW_VAL are as you'd expect for a compare-and-swap
6866 operation. TARGET is an optional place to store the value result of
6867 the operation. ICODE is the particular instruction to expand. Return
6868 the result of the operation. */
6870 static rtx
6871 expand_val_compare_and_swap_1 (rtx mem, rtx old_val, rtx new_val,
6872 rtx target, enum insn_code icode)
6874 enum machine_mode mode = GET_MODE (mem);
6875 rtx insn;
6877 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
6878 target = gen_reg_rtx (mode);
6880 if (GET_MODE (old_val) != VOIDmode && GET_MODE (old_val) != mode)
6881 old_val = convert_modes (mode, GET_MODE (old_val), old_val, 1);
6882 if (!insn_data[icode].operand[2].predicate (old_val, mode))
6883 old_val = force_reg (mode, old_val);
6885 if (GET_MODE (new_val) != VOIDmode && GET_MODE (new_val) != mode)
6886 new_val = convert_modes (mode, GET_MODE (new_val), new_val, 1);
6887 if (!insn_data[icode].operand[3].predicate (new_val, mode))
6888 new_val = force_reg (mode, new_val);
6890 insn = GEN_FCN (icode) (target, mem, old_val, new_val);
6891 if (insn == NULL_RTX)
6892 return NULL_RTX;
6893 emit_insn (insn);
6895 return target;
6898 /* Expand a compare-and-swap operation and return its value. */
6901 expand_val_compare_and_swap (rtx mem, rtx old_val, rtx new_val, rtx target)
6903 enum machine_mode mode = GET_MODE (mem);
6904 enum insn_code icode = sync_compare_and_swap[mode];
6906 if (icode == CODE_FOR_nothing)
6907 return NULL_RTX;
6909 return expand_val_compare_and_swap_1 (mem, old_val, new_val, target, icode);
6912 /* Expand a compare-and-swap operation and store true into the result if
6913 the operation was successful and false otherwise. Return the result.
6914 Unlike other routines, TARGET is not optional. */
6917 expand_bool_compare_and_swap (rtx mem, rtx old_val, rtx new_val, rtx target)
6919 enum machine_mode mode = GET_MODE (mem);
6920 enum insn_code icode;
6921 rtx subtarget, label0, label1;
6923 /* If the target supports a compare-and-swap pattern that simultaneously
6924 sets some flag for success, then use it. Otherwise use the regular
6925 compare-and-swap and follow that immediately with a compare insn. */
6926 icode = sync_compare_and_swap_cc[mode];
6927 switch (icode)
6929 default:
6930 subtarget = expand_val_compare_and_swap_1 (mem, old_val, new_val,
6931 NULL_RTX, icode);
6932 if (subtarget != NULL_RTX)
6933 break;
6935 /* FALLTHRU */
6936 case CODE_FOR_nothing:
6937 icode = sync_compare_and_swap[mode];
6938 if (icode == CODE_FOR_nothing)
6939 return NULL_RTX;
6941 /* Ensure that if old_val == mem, that we're not comparing
6942 against an old value. */
6943 if (MEM_P (old_val))
6944 old_val = force_reg (mode, old_val);
6946 subtarget = expand_val_compare_and_swap_1 (mem, old_val, new_val,
6947 NULL_RTX, icode);
6948 if (subtarget == NULL_RTX)
6949 return NULL_RTX;
6951 emit_cmp_insn (subtarget, old_val, EQ, const0_rtx, mode, true);
6954 /* If the target has a sane STORE_FLAG_VALUE, then go ahead and use a
6955 setcc instruction from the beginning. We don't work too hard here,
6956 but it's nice to not be stupid about initial code gen either. */
6957 if (STORE_FLAG_VALUE == 1)
6959 icode = setcc_gen_code[EQ];
6960 if (icode != CODE_FOR_nothing)
6962 enum machine_mode cmode = insn_data[icode].operand[0].mode;
6963 rtx insn;
6965 subtarget = target;
6966 if (!insn_data[icode].operand[0].predicate (target, cmode))
6967 subtarget = gen_reg_rtx (cmode);
6969 insn = GEN_FCN (icode) (subtarget);
6970 if (insn)
6972 emit_insn (insn);
6973 if (GET_MODE (target) != GET_MODE (subtarget))
6975 convert_move (target, subtarget, 1);
6976 subtarget = target;
6978 return subtarget;
6983 /* Without an appropriate setcc instruction, use a set of branches to
6984 get 1 and 0 stored into target. Presumably if the target has a
6985 STORE_FLAG_VALUE that isn't 1, then this will get cleaned up by ifcvt. */
6987 label0 = gen_label_rtx ();
6988 label1 = gen_label_rtx ();
6990 emit_jump_insn (bcc_gen_fctn[EQ] (label0));
6991 emit_move_insn (target, const0_rtx);
6992 emit_jump_insn (gen_jump (label1));
6993 emit_barrier ();
6994 emit_label (label0);
6995 emit_move_insn (target, const1_rtx);
6996 emit_label (label1);
6998 return target;
7001 /* This is a helper function for the other atomic operations. This function
7002 emits a loop that contains SEQ that iterates until a compare-and-swap
7003 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7004 a set of instructions that takes a value from OLD_REG as an input and
7005 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7006 set to the current contents of MEM. After SEQ, a compare-and-swap will
7007 attempt to update MEM with NEW_REG. The function returns true when the
7008 loop was generated successfully. */
7010 static bool
7011 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7013 enum machine_mode mode = GET_MODE (mem);
7014 enum insn_code icode;
7015 rtx label, cmp_reg, subtarget;
7017 /* The loop we want to generate looks like
7019 cmp_reg = mem;
7020 label:
7021 old_reg = cmp_reg;
7022 seq;
7023 cmp_reg = compare-and-swap(mem, old_reg, new_reg)
7024 if (cmp_reg != old_reg)
7025 goto label;
7027 Note that we only do the plain load from memory once. Subsequent
7028 iterations use the value loaded by the compare-and-swap pattern. */
7030 label = gen_label_rtx ();
7031 cmp_reg = gen_reg_rtx (mode);
7033 emit_move_insn (cmp_reg, mem);
7034 emit_label (label);
7035 emit_move_insn (old_reg, cmp_reg);
7036 if (seq)
7037 emit_insn (seq);
7039 /* If the target supports a compare-and-swap pattern that simultaneously
7040 sets some flag for success, then use it. Otherwise use the regular
7041 compare-and-swap and follow that immediately with a compare insn. */
7042 icode = sync_compare_and_swap_cc[mode];
7043 switch (icode)
7045 default:
7046 subtarget = expand_val_compare_and_swap_1 (mem, old_reg, new_reg,
7047 cmp_reg, icode);
7048 if (subtarget != NULL_RTX)
7050 gcc_assert (subtarget == cmp_reg);
7051 break;
7054 /* FALLTHRU */
7055 case CODE_FOR_nothing:
7056 icode = sync_compare_and_swap[mode];
7057 if (icode == CODE_FOR_nothing)
7058 return false;
7060 subtarget = expand_val_compare_and_swap_1 (mem, old_reg, new_reg,
7061 cmp_reg, icode);
7062 if (subtarget == NULL_RTX)
7063 return false;
7064 if (subtarget != cmp_reg)
7065 emit_move_insn (cmp_reg, subtarget);
7067 emit_cmp_insn (cmp_reg, old_reg, EQ, const0_rtx, mode, true);
7070 /* ??? Mark this jump predicted not taken? */
7071 emit_jump_insn (bcc_gen_fctn[NE] (label));
7073 return true;
7076 /* This function generates the atomic operation MEM CODE= VAL. In this
7077 case, we do not care about any resulting value. Returns NULL if we
7078 cannot generate the operation. */
7081 expand_sync_operation (rtx mem, rtx val, enum rtx_code code)
7083 enum machine_mode mode = GET_MODE (mem);
7084 enum insn_code icode;
7085 rtx insn;
7087 /* Look to see if the target supports the operation directly. */
7088 switch (code)
7090 case PLUS:
7091 icode = sync_add_optab[mode];
7092 break;
7093 case IOR:
7094 icode = sync_ior_optab[mode];
7095 break;
7096 case XOR:
7097 icode = sync_xor_optab[mode];
7098 break;
7099 case AND:
7100 icode = sync_and_optab[mode];
7101 break;
7102 case NOT:
7103 icode = sync_nand_optab[mode];
7104 break;
7106 case MINUS:
7107 icode = sync_sub_optab[mode];
7108 if (icode == CODE_FOR_nothing || CONST_INT_P (val))
7110 icode = sync_add_optab[mode];
7111 if (icode != CODE_FOR_nothing)
7113 val = expand_simple_unop (mode, NEG, val, NULL_RTX, 1);
7114 code = PLUS;
7117 break;
7119 default:
7120 gcc_unreachable ();
7123 /* Generate the direct operation, if present. */
7124 if (icode != CODE_FOR_nothing)
7126 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7127 val = convert_modes (mode, GET_MODE (val), val, 1);
7128 if (!insn_data[icode].operand[1].predicate (val, mode))
7129 val = force_reg (mode, val);
7131 insn = GEN_FCN (icode) (mem, val);
7132 if (insn)
7134 emit_insn (insn);
7135 return const0_rtx;
7139 /* Failing that, generate a compare-and-swap loop in which we perform the
7140 operation with normal arithmetic instructions. */
7141 if (sync_compare_and_swap[mode] != CODE_FOR_nothing)
7143 rtx t0 = gen_reg_rtx (mode), t1;
7145 start_sequence ();
7147 t1 = t0;
7148 if (code == NOT)
7150 t1 = expand_simple_unop (mode, NOT, t1, NULL_RTX, true);
7151 code = AND;
7153 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX,
7154 true, OPTAB_LIB_WIDEN);
7156 insn = get_insns ();
7157 end_sequence ();
7159 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
7160 return const0_rtx;
7163 return NULL_RTX;
7166 /* This function generates the atomic operation MEM CODE= VAL. In this
7167 case, we do care about the resulting value: if AFTER is true then
7168 return the value MEM holds after the operation, if AFTER is false
7169 then return the value MEM holds before the operation. TARGET is an
7170 optional place for the result value to be stored. */
7173 expand_sync_fetch_operation (rtx mem, rtx val, enum rtx_code code,
7174 bool after, rtx target)
7176 enum machine_mode mode = GET_MODE (mem);
7177 enum insn_code old_code, new_code, icode;
7178 bool compensate;
7179 rtx insn;
7181 /* Look to see if the target supports the operation directly. */
7182 switch (code)
7184 case PLUS:
7185 old_code = sync_old_add_optab[mode];
7186 new_code = sync_new_add_optab[mode];
7187 break;
7188 case IOR:
7189 old_code = sync_old_ior_optab[mode];
7190 new_code = sync_new_ior_optab[mode];
7191 break;
7192 case XOR:
7193 old_code = sync_old_xor_optab[mode];
7194 new_code = sync_new_xor_optab[mode];
7195 break;
7196 case AND:
7197 old_code = sync_old_and_optab[mode];
7198 new_code = sync_new_and_optab[mode];
7199 break;
7200 case NOT:
7201 old_code = sync_old_nand_optab[mode];
7202 new_code = sync_new_nand_optab[mode];
7203 break;
7205 case MINUS:
7206 old_code = sync_old_sub_optab[mode];
7207 new_code = sync_new_sub_optab[mode];
7208 if ((old_code == CODE_FOR_nothing && new_code == CODE_FOR_nothing)
7209 || CONST_INT_P (val))
7211 old_code = sync_old_add_optab[mode];
7212 new_code = sync_new_add_optab[mode];
7213 if (old_code != CODE_FOR_nothing || new_code != CODE_FOR_nothing)
7215 val = expand_simple_unop (mode, NEG, val, NULL_RTX, 1);
7216 code = PLUS;
7219 break;
7221 default:
7222 gcc_unreachable ();
7225 /* If the target does supports the proper new/old operation, great. But
7226 if we only support the opposite old/new operation, check to see if we
7227 can compensate. In the case in which the old value is supported, then
7228 we can always perform the operation again with normal arithmetic. In
7229 the case in which the new value is supported, then we can only handle
7230 this in the case the operation is reversible. */
7231 compensate = false;
7232 if (after)
7234 icode = new_code;
7235 if (icode == CODE_FOR_nothing)
7237 icode = old_code;
7238 if (icode != CODE_FOR_nothing)
7239 compensate = true;
7242 else
7244 icode = old_code;
7245 if (icode == CODE_FOR_nothing
7246 && (code == PLUS || code == MINUS || code == XOR))
7248 icode = new_code;
7249 if (icode != CODE_FOR_nothing)
7250 compensate = true;
7254 /* If we found something supported, great. */
7255 if (icode != CODE_FOR_nothing)
7257 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
7258 target = gen_reg_rtx (mode);
7260 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7261 val = convert_modes (mode, GET_MODE (val), val, 1);
7262 if (!insn_data[icode].operand[2].predicate (val, mode))
7263 val = force_reg (mode, val);
7265 insn = GEN_FCN (icode) (target, mem, val);
7266 if (insn)
7268 emit_insn (insn);
7270 /* If we need to compensate for using an operation with the
7271 wrong return value, do so now. */
7272 if (compensate)
7274 if (!after)
7276 if (code == PLUS)
7277 code = MINUS;
7278 else if (code == MINUS)
7279 code = PLUS;
7282 if (code == NOT)
7283 target = expand_simple_unop (mode, NOT, target, NULL_RTX, true);
7284 target = expand_simple_binop (mode, code, target, val, NULL_RTX,
7285 true, OPTAB_LIB_WIDEN);
7288 return target;
7292 /* Failing that, generate a compare-and-swap loop in which we perform the
7293 operation with normal arithmetic instructions. */
7294 if (sync_compare_and_swap[mode] != CODE_FOR_nothing)
7296 rtx t0 = gen_reg_rtx (mode), t1;
7298 if (!target || !register_operand (target, mode))
7299 target = gen_reg_rtx (mode);
7301 start_sequence ();
7303 if (!after)
7304 emit_move_insn (target, t0);
7305 t1 = t0;
7306 if (code == NOT)
7308 t1 = expand_simple_unop (mode, NOT, t1, NULL_RTX, true);
7309 code = AND;
7311 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX,
7312 true, OPTAB_LIB_WIDEN);
7313 if (after)
7314 emit_move_insn (target, t1);
7316 insn = get_insns ();
7317 end_sequence ();
7319 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
7320 return target;
7323 return NULL_RTX;
7326 /* This function expands a test-and-set operation. Ideally we atomically
7327 store VAL in MEM and return the previous value in MEM. Some targets
7328 may not support this operation and only support VAL with the constant 1;
7329 in this case while the return value will be 0/1, but the exact value
7330 stored in MEM is target defined. TARGET is an option place to stick
7331 the return value. */
7334 expand_sync_lock_test_and_set (rtx mem, rtx val, rtx target)
7336 enum machine_mode mode = GET_MODE (mem);
7337 enum insn_code icode;
7338 rtx insn;
7340 /* If the target supports the test-and-set directly, great. */
7341 icode = sync_lock_test_and_set[mode];
7342 if (icode != CODE_FOR_nothing)
7344 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
7345 target = gen_reg_rtx (mode);
7347 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7348 val = convert_modes (mode, GET_MODE (val), val, 1);
7349 if (!insn_data[icode].operand[2].predicate (val, mode))
7350 val = force_reg (mode, val);
7352 insn = GEN_FCN (icode) (target, mem, val);
7353 if (insn)
7355 emit_insn (insn);
7356 return target;
7360 /* Otherwise, use a compare-and-swap loop for the exchange. */
7361 if (sync_compare_and_swap[mode] != CODE_FOR_nothing)
7363 if (!target || !register_operand (target, mode))
7364 target = gen_reg_rtx (mode);
7365 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7366 val = convert_modes (mode, GET_MODE (val), val, 1);
7367 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7368 return target;
7371 return NULL_RTX;
7374 #include "gt-optabs.h"