2009-07-17 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / optabs.c
blob9c73e645488eb0e82ee1cfe35ae6135e23d7541a
1 /* Expand the basic unary and binary arithmetic operations, for GNU compiler.
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 && HAVE_DESIGNATED_INITIALIZERS
58 __extension__ struct optab_d 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_d 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 && HAVE_DESIGNATED_INITIALIZERS
70 __extension__ struct convert_optab_d 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_d 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 #ifdef HAVE_conditional_move
83 /* Indexed by the machine mode, gives the insn code to make a conditional
84 move insn. This is not indexed by the rtx-code like bcc_gen_fctn and
85 setcc_gen_code to cut down on the number of named patterns. Consider a day
86 when a lot more rtx codes are conditional (eg: for the ARM). */
88 enum insn_code movcc_gen_code[NUM_MACHINE_MODES];
89 #endif
91 /* Indexed by the machine mode, gives the insn code for vector conditional
92 operation. */
94 enum insn_code vcond_gen_code[NUM_MACHINE_MODES];
95 enum insn_code vcondu_gen_code[NUM_MACHINE_MODES];
97 static void prepare_float_lib_cmp (rtx, rtx, enum rtx_code, rtx *,
98 enum machine_mode *);
99 static rtx expand_unop_direct (enum machine_mode, optab, rtx, rtx, int);
101 /* Debug facility for use in GDB. */
102 void debug_optab_libfuncs (void);
104 /* Prefixes for the current version of decimal floating point (BID vs. DPD) */
105 #if ENABLE_DECIMAL_BID_FORMAT
106 #define DECIMAL_PREFIX "bid_"
107 #else
108 #define DECIMAL_PREFIX "dpd_"
109 #endif
112 /* Info about libfunc. We use same hashtable for normal optabs and conversion
113 optab. In the first case mode2 is unused. */
114 struct GTY(()) libfunc_entry {
115 size_t optab;
116 enum machine_mode mode1, mode2;
117 rtx libfunc;
120 /* Hash table used to convert declarations into nodes. */
121 static GTY((param_is (struct libfunc_entry))) htab_t libfunc_hash;
123 /* Used for attribute_hash. */
125 static hashval_t
126 hash_libfunc (const void *p)
128 const struct libfunc_entry *const e = (const struct libfunc_entry *) p;
130 return (((int) e->mode1 + (int) e->mode2 * NUM_MACHINE_MODES)
131 ^ e->optab);
134 /* Used for optab_hash. */
136 static int
137 eq_libfunc (const void *p, const void *q)
139 const struct libfunc_entry *const e1 = (const struct libfunc_entry *) p;
140 const struct libfunc_entry *const e2 = (const struct libfunc_entry *) q;
142 return (e1->optab == e2->optab
143 && e1->mode1 == e2->mode1
144 && e1->mode2 == e2->mode2);
147 /* Return libfunc corresponding operation defined by OPTAB converting
148 from MODE2 to MODE1. Trigger lazy initialization if needed, return NULL
149 if no libfunc is available. */
151 convert_optab_libfunc (convert_optab optab, enum machine_mode mode1,
152 enum machine_mode mode2)
154 struct libfunc_entry e;
155 struct libfunc_entry **slot;
157 e.optab = (size_t) (optab - &convert_optab_table[0]);
158 e.mode1 = mode1;
159 e.mode2 = mode2;
160 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
161 if (!slot)
163 if (optab->libcall_gen)
165 optab->libcall_gen (optab, optab->libcall_basename, mode1, mode2);
166 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
167 if (slot)
168 return (*slot)->libfunc;
169 else
170 return NULL;
172 return NULL;
174 return (*slot)->libfunc;
177 /* Return libfunc corresponding operation defined by OPTAB in MODE.
178 Trigger lazy initialization if needed, return NULL if no libfunc is
179 available. */
181 optab_libfunc (optab optab, enum machine_mode mode)
183 struct libfunc_entry e;
184 struct libfunc_entry **slot;
186 e.optab = (size_t) (optab - &optab_table[0]);
187 e.mode1 = mode;
188 e.mode2 = VOIDmode;
189 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, NO_INSERT);
190 if (!slot)
192 if (optab->libcall_gen)
194 optab->libcall_gen (optab, optab->libcall_basename,
195 optab->libcall_suffix, mode);
196 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash,
197 &e, NO_INSERT);
198 if (slot)
199 return (*slot)->libfunc;
200 else
201 return NULL;
203 return NULL;
205 return (*slot)->libfunc;
209 /* Add a REG_EQUAL note to the last insn in INSNS. TARGET is being set to
210 the result of operation CODE applied to OP0 (and OP1 if it is a binary
211 operation).
213 If the last insn does not set TARGET, don't do anything, but return 1.
215 If a previous insn sets TARGET and TARGET is one of OP0 or OP1,
216 don't add the REG_EQUAL note but return 0. Our caller can then try
217 again, ensuring that TARGET is not one of the operands. */
219 static int
220 add_equal_note (rtx insns, rtx target, enum rtx_code code, rtx op0, rtx op1)
222 rtx last_insn, insn, set;
223 rtx note;
225 gcc_assert (insns && INSN_P (insns) && NEXT_INSN (insns));
227 if (GET_RTX_CLASS (code) != RTX_COMM_ARITH
228 && GET_RTX_CLASS (code) != RTX_BIN_ARITH
229 && GET_RTX_CLASS (code) != RTX_COMM_COMPARE
230 && GET_RTX_CLASS (code) != RTX_COMPARE
231 && GET_RTX_CLASS (code) != RTX_UNARY)
232 return 1;
234 if (GET_CODE (target) == ZERO_EXTRACT)
235 return 1;
237 for (last_insn = insns;
238 NEXT_INSN (last_insn) != NULL_RTX;
239 last_insn = NEXT_INSN (last_insn))
242 set = single_set (last_insn);
243 if (set == NULL_RTX)
244 return 1;
246 if (! rtx_equal_p (SET_DEST (set), target)
247 /* For a STRICT_LOW_PART, the REG_NOTE applies to what is inside it. */
248 && (GET_CODE (SET_DEST (set)) != STRICT_LOW_PART
249 || ! rtx_equal_p (XEXP (SET_DEST (set), 0), target)))
250 return 1;
252 /* If TARGET is in OP0 or OP1, check if anything in SEQ sets TARGET
253 besides the last insn. */
254 if (reg_overlap_mentioned_p (target, op0)
255 || (op1 && reg_overlap_mentioned_p (target, op1)))
257 insn = PREV_INSN (last_insn);
258 while (insn != NULL_RTX)
260 if (reg_set_p (target, insn))
261 return 0;
263 insn = PREV_INSN (insn);
267 if (GET_RTX_CLASS (code) == RTX_UNARY)
268 note = gen_rtx_fmt_e (code, GET_MODE (target), copy_rtx (op0));
269 else
270 note = gen_rtx_fmt_ee (code, GET_MODE (target), copy_rtx (op0), copy_rtx (op1));
272 set_unique_reg_note (last_insn, REG_EQUAL, note);
274 return 1;
277 /* Widen OP to MODE and return the rtx for the widened operand. UNSIGNEDP
278 says whether OP is signed or unsigned. NO_EXTEND is nonzero if we need
279 not actually do a sign-extend or zero-extend, but can leave the
280 higher-order bits of the result rtx undefined, for example, in the case
281 of logical operations, but not right shifts. */
283 static rtx
284 widen_operand (rtx op, enum machine_mode mode, enum machine_mode oldmode,
285 int unsignedp, int no_extend)
287 rtx result;
289 /* If we don't have to extend and this is a constant, return it. */
290 if (no_extend && GET_MODE (op) == VOIDmode)
291 return op;
293 /* If we must extend do so. If OP is a SUBREG for a promoted object, also
294 extend since it will be more efficient to do so unless the signedness of
295 a promoted object differs from our extension. */
296 if (! no_extend
297 || (GET_CODE (op) == SUBREG && SUBREG_PROMOTED_VAR_P (op)
298 && SUBREG_PROMOTED_UNSIGNED_P (op) == unsignedp))
299 return convert_modes (mode, oldmode, op, unsignedp);
301 /* If MODE is no wider than a single word, we return a paradoxical
302 SUBREG. */
303 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
304 return gen_rtx_SUBREG (mode, force_reg (GET_MODE (op), op), 0);
306 /* Otherwise, get an object of MODE, clobber it, and set the low-order
307 part to OP. */
309 result = gen_reg_rtx (mode);
310 emit_clobber (result);
311 emit_move_insn (gen_lowpart (GET_MODE (op), result), op);
312 return result;
315 /* Return the optab used for computing the operation given by the tree code,
316 CODE and the tree EXP. This function is not always usable (for example, it
317 cannot give complete results for multiplication or division) but probably
318 ought to be relied on more widely throughout the expander. */
319 optab
320 optab_for_tree_code (enum tree_code code, const_tree type,
321 enum optab_subtype subtype)
323 bool trapv;
324 switch (code)
326 case BIT_AND_EXPR:
327 return and_optab;
329 case BIT_IOR_EXPR:
330 return ior_optab;
332 case BIT_NOT_EXPR:
333 return one_cmpl_optab;
335 case BIT_XOR_EXPR:
336 return xor_optab;
338 case TRUNC_MOD_EXPR:
339 case CEIL_MOD_EXPR:
340 case FLOOR_MOD_EXPR:
341 case ROUND_MOD_EXPR:
342 return TYPE_UNSIGNED (type) ? umod_optab : smod_optab;
344 case RDIV_EXPR:
345 case TRUNC_DIV_EXPR:
346 case CEIL_DIV_EXPR:
347 case FLOOR_DIV_EXPR:
348 case ROUND_DIV_EXPR:
349 case EXACT_DIV_EXPR:
350 if (TYPE_SATURATING(type))
351 return TYPE_UNSIGNED(type) ? usdiv_optab : ssdiv_optab;
352 return TYPE_UNSIGNED (type) ? udiv_optab : sdiv_optab;
354 case LSHIFT_EXPR:
355 if (VECTOR_MODE_P (TYPE_MODE (type)))
357 if (subtype == optab_vector)
358 return TYPE_SATURATING (type) ? NULL : vashl_optab;
360 gcc_assert (subtype == optab_scalar);
362 if (TYPE_SATURATING(type))
363 return TYPE_UNSIGNED(type) ? usashl_optab : ssashl_optab;
364 return ashl_optab;
366 case RSHIFT_EXPR:
367 if (VECTOR_MODE_P (TYPE_MODE (type)))
369 if (subtype == optab_vector)
370 return TYPE_UNSIGNED (type) ? vlshr_optab : vashr_optab;
372 gcc_assert (subtype == optab_scalar);
374 return TYPE_UNSIGNED (type) ? lshr_optab : ashr_optab;
376 case LROTATE_EXPR:
377 if (VECTOR_MODE_P (TYPE_MODE (type)))
379 if (subtype == optab_vector)
380 return vrotl_optab;
382 gcc_assert (subtype == optab_scalar);
384 return rotl_optab;
386 case RROTATE_EXPR:
387 if (VECTOR_MODE_P (TYPE_MODE (type)))
389 if (subtype == optab_vector)
390 return vrotr_optab;
392 gcc_assert (subtype == optab_scalar);
394 return rotr_optab;
396 case MAX_EXPR:
397 return TYPE_UNSIGNED (type) ? umax_optab : smax_optab;
399 case MIN_EXPR:
400 return TYPE_UNSIGNED (type) ? umin_optab : smin_optab;
402 case REALIGN_LOAD_EXPR:
403 return vec_realign_load_optab;
405 case WIDEN_SUM_EXPR:
406 return TYPE_UNSIGNED (type) ? usum_widen_optab : ssum_widen_optab;
408 case DOT_PROD_EXPR:
409 return TYPE_UNSIGNED (type) ? udot_prod_optab : sdot_prod_optab;
411 case REDUC_MAX_EXPR:
412 return TYPE_UNSIGNED (type) ? reduc_umax_optab : reduc_smax_optab;
414 case REDUC_MIN_EXPR:
415 return TYPE_UNSIGNED (type) ? reduc_umin_optab : reduc_smin_optab;
417 case REDUC_PLUS_EXPR:
418 return TYPE_UNSIGNED (type) ? reduc_uplus_optab : reduc_splus_optab;
420 case VEC_LSHIFT_EXPR:
421 return vec_shl_optab;
423 case VEC_RSHIFT_EXPR:
424 return vec_shr_optab;
426 case VEC_WIDEN_MULT_HI_EXPR:
427 return TYPE_UNSIGNED (type) ?
428 vec_widen_umult_hi_optab : vec_widen_smult_hi_optab;
430 case VEC_WIDEN_MULT_LO_EXPR:
431 return TYPE_UNSIGNED (type) ?
432 vec_widen_umult_lo_optab : vec_widen_smult_lo_optab;
434 case VEC_UNPACK_HI_EXPR:
435 return TYPE_UNSIGNED (type) ?
436 vec_unpacku_hi_optab : vec_unpacks_hi_optab;
438 case VEC_UNPACK_LO_EXPR:
439 return TYPE_UNSIGNED (type) ?
440 vec_unpacku_lo_optab : vec_unpacks_lo_optab;
442 case VEC_UNPACK_FLOAT_HI_EXPR:
443 /* The signedness is determined from input operand. */
444 return TYPE_UNSIGNED (type) ?
445 vec_unpacku_float_hi_optab : vec_unpacks_float_hi_optab;
447 case VEC_UNPACK_FLOAT_LO_EXPR:
448 /* The signedness is determined from input operand. */
449 return TYPE_UNSIGNED (type) ?
450 vec_unpacku_float_lo_optab : vec_unpacks_float_lo_optab;
452 case VEC_PACK_TRUNC_EXPR:
453 return vec_pack_trunc_optab;
455 case VEC_PACK_SAT_EXPR:
456 return TYPE_UNSIGNED (type) ? vec_pack_usat_optab : vec_pack_ssat_optab;
458 case VEC_PACK_FIX_TRUNC_EXPR:
459 /* The signedness is determined from output operand. */
460 return TYPE_UNSIGNED (type) ?
461 vec_pack_ufix_trunc_optab : vec_pack_sfix_trunc_optab;
463 default:
464 break;
467 trapv = INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_TRAPS (type);
468 switch (code)
470 case POINTER_PLUS_EXPR:
471 case POINTER_PLUSNV_EXPR:
472 case PLUS_EXPR:
473 case PLUSNV_EXPR:
474 if (TYPE_SATURATING(type))
475 return TYPE_UNSIGNED(type) ? usadd_optab : ssadd_optab;
476 return trapv ? addv_optab : add_optab;
478 case MINUS_EXPR:
479 case MINUSNV_EXPR:
480 if (TYPE_SATURATING(type))
481 return TYPE_UNSIGNED(type) ? ussub_optab : sssub_optab;
482 return trapv ? subv_optab : sub_optab;
484 case MULT_EXPR:
485 case MULTNV_EXPR:
486 if (TYPE_SATURATING(type))
487 return TYPE_UNSIGNED(type) ? usmul_optab : ssmul_optab;
488 return trapv ? smulv_optab : smul_optab;
490 case NEGATE_EXPR:
491 case NEGATENV_EXPR:
492 if (TYPE_SATURATING(type))
493 return TYPE_UNSIGNED(type) ? usneg_optab : ssneg_optab;
494 return trapv ? negv_optab : neg_optab;
496 case ABS_EXPR:
497 return trapv ? absv_optab : abs_optab;
499 case VEC_EXTRACT_EVEN_EXPR:
500 return vec_extract_even_optab;
502 case VEC_EXTRACT_ODD_EXPR:
503 return vec_extract_odd_optab;
505 case VEC_INTERLEAVE_HIGH_EXPR:
506 return vec_interleave_high_optab;
508 case VEC_INTERLEAVE_LOW_EXPR:
509 return vec_interleave_low_optab;
511 default:
512 return NULL;
517 /* Expand vector widening operations.
519 There are two different classes of operations handled here:
520 1) Operations whose result is wider than all the arguments to the operation.
521 Examples: VEC_UNPACK_HI/LO_EXPR, VEC_WIDEN_MULT_HI/LO_EXPR
522 In this case OP0 and optionally OP1 would be initialized,
523 but WIDE_OP wouldn't (not relevant for this case).
524 2) Operations whose result is of the same size as the last argument to the
525 operation, but wider than all the other arguments to the operation.
526 Examples: WIDEN_SUM_EXPR, VEC_DOT_PROD_EXPR.
527 In the case WIDE_OP, OP0 and optionally OP1 would be initialized.
529 E.g, when called to expand the following operations, this is how
530 the arguments will be initialized:
531 nops OP0 OP1 WIDE_OP
532 widening-sum 2 oprnd0 - oprnd1
533 widening-dot-product 3 oprnd0 oprnd1 oprnd2
534 widening-mult 2 oprnd0 oprnd1 -
535 type-promotion (vec-unpack) 1 oprnd0 - - */
538 expand_widen_pattern_expr (tree exp, rtx op0, rtx op1, rtx wide_op, rtx target,
539 int unsignedp)
541 tree oprnd0, oprnd1, oprnd2;
542 enum machine_mode wmode = VOIDmode, tmode0, tmode1 = VOIDmode;
543 optab widen_pattern_optab;
544 int icode;
545 enum machine_mode xmode0, xmode1 = VOIDmode, wxmode = VOIDmode;
546 rtx temp;
547 rtx pat;
548 rtx xop0, xop1, wxop;
549 int nops = TREE_OPERAND_LENGTH (exp);
551 oprnd0 = TREE_OPERAND (exp, 0);
552 tmode0 = TYPE_MODE (TREE_TYPE (oprnd0));
553 widen_pattern_optab =
554 optab_for_tree_code (TREE_CODE (exp), TREE_TYPE (oprnd0), optab_default);
555 icode = (int) optab_handler (widen_pattern_optab, tmode0)->insn_code;
556 gcc_assert (icode != CODE_FOR_nothing);
557 xmode0 = insn_data[icode].operand[1].mode;
559 if (nops >= 2)
561 oprnd1 = TREE_OPERAND (exp, 1);
562 tmode1 = TYPE_MODE (TREE_TYPE (oprnd1));
563 xmode1 = insn_data[icode].operand[2].mode;
566 /* The last operand is of a wider mode than the rest of the operands. */
567 if (nops == 2)
569 wmode = tmode1;
570 wxmode = xmode1;
572 else if (nops == 3)
574 gcc_assert (tmode1 == tmode0);
575 gcc_assert (op1);
576 oprnd2 = TREE_OPERAND (exp, 2);
577 wmode = TYPE_MODE (TREE_TYPE (oprnd2));
578 wxmode = insn_data[icode].operand[3].mode;
581 if (!wide_op)
582 wmode = wxmode = insn_data[icode].operand[0].mode;
584 if (!target
585 || ! (*insn_data[icode].operand[0].predicate) (target, wmode))
586 temp = gen_reg_rtx (wmode);
587 else
588 temp = target;
590 xop0 = op0;
591 xop1 = op1;
592 wxop = wide_op;
594 /* In case the insn wants input operands in modes different from
595 those of the actual operands, convert the operands. It would
596 seem that we don't need to convert CONST_INTs, but we do, so
597 that they're properly zero-extended, sign-extended or truncated
598 for their mode. */
600 if (GET_MODE (op0) != xmode0 && xmode0 != VOIDmode)
601 xop0 = convert_modes (xmode0,
602 GET_MODE (op0) != VOIDmode
603 ? GET_MODE (op0)
604 : tmode0,
605 xop0, unsignedp);
607 if (op1)
608 if (GET_MODE (op1) != xmode1 && xmode1 != VOIDmode)
609 xop1 = convert_modes (xmode1,
610 GET_MODE (op1) != VOIDmode
611 ? GET_MODE (op1)
612 : tmode1,
613 xop1, unsignedp);
615 if (wide_op)
616 if (GET_MODE (wide_op) != wxmode && wxmode != VOIDmode)
617 wxop = convert_modes (wxmode,
618 GET_MODE (wide_op) != VOIDmode
619 ? GET_MODE (wide_op)
620 : wmode,
621 wxop, unsignedp);
623 /* Now, if insn's predicates don't allow our operands, put them into
624 pseudo regs. */
626 if (! (*insn_data[icode].operand[1].predicate) (xop0, xmode0)
627 && xmode0 != VOIDmode)
628 xop0 = copy_to_mode_reg (xmode0, xop0);
630 if (op1)
632 if (! (*insn_data[icode].operand[2].predicate) (xop1, xmode1)
633 && xmode1 != VOIDmode)
634 xop1 = copy_to_mode_reg (xmode1, xop1);
636 if (wide_op)
638 if (! (*insn_data[icode].operand[3].predicate) (wxop, wxmode)
639 && wxmode != VOIDmode)
640 wxop = copy_to_mode_reg (wxmode, wxop);
642 pat = GEN_FCN (icode) (temp, xop0, xop1, wxop);
644 else
645 pat = GEN_FCN (icode) (temp, xop0, xop1);
647 else
649 if (wide_op)
651 if (! (*insn_data[icode].operand[2].predicate) (wxop, wxmode)
652 && wxmode != VOIDmode)
653 wxop = copy_to_mode_reg (wxmode, wxop);
655 pat = GEN_FCN (icode) (temp, xop0, wxop);
657 else
658 pat = GEN_FCN (icode) (temp, xop0);
661 emit_insn (pat);
662 return temp;
665 /* Generate code to perform an operation specified by TERNARY_OPTAB
666 on operands OP0, OP1 and OP2, with result having machine-mode MODE.
668 UNSIGNEDP is for the case where we have to widen the operands
669 to perform the operation. It says to use zero-extension.
671 If TARGET is nonzero, the value
672 is generated there, if it is convenient to do so.
673 In all cases an rtx is returned for the locus of the value;
674 this may or may not be TARGET. */
677 expand_ternary_op (enum machine_mode mode, optab ternary_optab, rtx op0,
678 rtx op1, rtx op2, rtx target, int unsignedp)
680 int icode = (int) optab_handler (ternary_optab, mode)->insn_code;
681 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
682 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
683 enum machine_mode mode2 = insn_data[icode].operand[3].mode;
684 rtx temp;
685 rtx pat;
686 rtx xop0 = op0, xop1 = op1, xop2 = op2;
688 gcc_assert (optab_handler (ternary_optab, mode)->insn_code
689 != CODE_FOR_nothing);
691 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
692 temp = gen_reg_rtx (mode);
693 else
694 temp = target;
696 /* In case the insn wants input operands in modes different from
697 those of the actual operands, convert the operands. It would
698 seem that we don't need to convert CONST_INTs, but we do, so
699 that they're properly zero-extended, sign-extended or truncated
700 for their mode. */
702 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
703 xop0 = convert_modes (mode0,
704 GET_MODE (op0) != VOIDmode
705 ? GET_MODE (op0)
706 : mode,
707 xop0, unsignedp);
709 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
710 xop1 = convert_modes (mode1,
711 GET_MODE (op1) != VOIDmode
712 ? GET_MODE (op1)
713 : mode,
714 xop1, unsignedp);
716 if (GET_MODE (op2) != mode2 && mode2 != VOIDmode)
717 xop2 = convert_modes (mode2,
718 GET_MODE (op2) != VOIDmode
719 ? GET_MODE (op2)
720 : mode,
721 xop2, unsignedp);
723 /* Now, if insn's predicates don't allow our operands, put them into
724 pseudo regs. */
726 if (!insn_data[icode].operand[1].predicate (xop0, mode0)
727 && mode0 != VOIDmode)
728 xop0 = copy_to_mode_reg (mode0, xop0);
730 if (!insn_data[icode].operand[2].predicate (xop1, mode1)
731 && mode1 != VOIDmode)
732 xop1 = copy_to_mode_reg (mode1, xop1);
734 if (!insn_data[icode].operand[3].predicate (xop2, mode2)
735 && mode2 != VOIDmode)
736 xop2 = copy_to_mode_reg (mode2, xop2);
738 pat = GEN_FCN (icode) (temp, xop0, xop1, xop2);
740 emit_insn (pat);
741 return temp;
745 /* Like expand_binop, but return a constant rtx if the result can be
746 calculated at compile time. The arguments and return value are
747 otherwise the same as for expand_binop. */
749 static rtx
750 simplify_expand_binop (enum machine_mode mode, optab binoptab,
751 rtx op0, rtx op1, rtx target, int unsignedp,
752 enum optab_methods methods)
754 if (CONSTANT_P (op0) && CONSTANT_P (op1))
756 rtx x = simplify_binary_operation (binoptab->code, mode, op0, op1);
758 if (x)
759 return x;
762 return expand_binop (mode, binoptab, op0, op1, target, unsignedp, methods);
765 /* Like simplify_expand_binop, but always put the result in TARGET.
766 Return true if the expansion succeeded. */
768 bool
769 force_expand_binop (enum machine_mode mode, optab binoptab,
770 rtx op0, rtx op1, rtx target, int unsignedp,
771 enum optab_methods methods)
773 rtx x = simplify_expand_binop (mode, binoptab, op0, op1,
774 target, unsignedp, methods);
775 if (x == 0)
776 return false;
777 if (x != target)
778 emit_move_insn (target, x);
779 return true;
782 /* Generate insns for VEC_LSHIFT_EXPR, VEC_RSHIFT_EXPR. */
785 expand_vec_shift_expr (tree vec_shift_expr, rtx target)
787 enum insn_code icode;
788 rtx rtx_op1, rtx_op2;
789 enum machine_mode mode1;
790 enum machine_mode mode2;
791 enum machine_mode mode = TYPE_MODE (TREE_TYPE (vec_shift_expr));
792 tree vec_oprnd = TREE_OPERAND (vec_shift_expr, 0);
793 tree shift_oprnd = TREE_OPERAND (vec_shift_expr, 1);
794 optab shift_optab;
795 rtx pat;
797 switch (TREE_CODE (vec_shift_expr))
799 case VEC_RSHIFT_EXPR:
800 shift_optab = vec_shr_optab;
801 break;
802 case VEC_LSHIFT_EXPR:
803 shift_optab = vec_shl_optab;
804 break;
805 default:
806 gcc_unreachable ();
809 icode = optab_handler (shift_optab, mode)->insn_code;
810 gcc_assert (icode != CODE_FOR_nothing);
812 mode1 = insn_data[icode].operand[1].mode;
813 mode2 = insn_data[icode].operand[2].mode;
815 rtx_op1 = expand_normal (vec_oprnd);
816 if (!(*insn_data[icode].operand[1].predicate) (rtx_op1, mode1)
817 && mode1 != VOIDmode)
818 rtx_op1 = force_reg (mode1, rtx_op1);
820 rtx_op2 = expand_normal (shift_oprnd);
821 if (!(*insn_data[icode].operand[2].predicate) (rtx_op2, mode2)
822 && mode2 != VOIDmode)
823 rtx_op2 = force_reg (mode2, rtx_op2);
825 if (!target
826 || ! (*insn_data[icode].operand[0].predicate) (target, mode))
827 target = gen_reg_rtx (mode);
829 /* Emit instruction */
830 pat = GEN_FCN (icode) (target, rtx_op1, rtx_op2);
831 gcc_assert (pat);
832 emit_insn (pat);
834 return target;
837 /* This subroutine of expand_doubleword_shift handles the cases in which
838 the effective shift value is >= BITS_PER_WORD. The arguments and return
839 value are the same as for the parent routine, except that SUPERWORD_OP1
840 is the shift count to use when shifting OUTOF_INPUT into INTO_TARGET.
841 INTO_TARGET may be null if the caller has decided to calculate it. */
843 static bool
844 expand_superword_shift (optab binoptab, rtx outof_input, rtx superword_op1,
845 rtx outof_target, rtx into_target,
846 int unsignedp, enum optab_methods methods)
848 if (into_target != 0)
849 if (!force_expand_binop (word_mode, binoptab, outof_input, superword_op1,
850 into_target, unsignedp, methods))
851 return false;
853 if (outof_target != 0)
855 /* For a signed right shift, we must fill OUTOF_TARGET with copies
856 of the sign bit, otherwise we must fill it with zeros. */
857 if (binoptab != ashr_optab)
858 emit_move_insn (outof_target, CONST0_RTX (word_mode));
859 else
860 if (!force_expand_binop (word_mode, binoptab,
861 outof_input, GEN_INT (BITS_PER_WORD - 1),
862 outof_target, unsignedp, methods))
863 return false;
865 return true;
868 /* This subroutine of expand_doubleword_shift handles the cases in which
869 the effective shift value is < BITS_PER_WORD. The arguments and return
870 value are the same as for the parent routine. */
872 static bool
873 expand_subword_shift (enum machine_mode op1_mode, optab binoptab,
874 rtx outof_input, rtx into_input, rtx op1,
875 rtx outof_target, rtx into_target,
876 int unsignedp, enum optab_methods methods,
877 unsigned HOST_WIDE_INT shift_mask)
879 optab reverse_unsigned_shift, unsigned_shift;
880 rtx tmp, carries;
882 reverse_unsigned_shift = (binoptab == ashl_optab ? lshr_optab : ashl_optab);
883 unsigned_shift = (binoptab == ashl_optab ? ashl_optab : lshr_optab);
885 /* The low OP1 bits of INTO_TARGET come from the high bits of OUTOF_INPUT.
886 We therefore need to shift OUTOF_INPUT by (BITS_PER_WORD - OP1) bits in
887 the opposite direction to BINOPTAB. */
888 if (CONSTANT_P (op1) || shift_mask >= BITS_PER_WORD)
890 carries = outof_input;
891 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
892 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
893 0, true, methods);
895 else
897 /* We must avoid shifting by BITS_PER_WORD bits since that is either
898 the same as a zero shift (if shift_mask == BITS_PER_WORD - 1) or
899 has unknown behavior. Do a single shift first, then shift by the
900 remainder. It's OK to use ~OP1 as the remainder if shift counts
901 are truncated to the mode size. */
902 carries = expand_binop (word_mode, reverse_unsigned_shift,
903 outof_input, const1_rtx, 0, unsignedp, methods);
904 if (shift_mask == BITS_PER_WORD - 1)
906 tmp = immed_double_const (-1, -1, op1_mode);
907 tmp = simplify_expand_binop (op1_mode, xor_optab, op1, tmp,
908 0, true, methods);
910 else
912 tmp = immed_double_const (BITS_PER_WORD - 1, 0, op1_mode);
913 tmp = simplify_expand_binop (op1_mode, sub_optab, tmp, op1,
914 0, true, methods);
917 if (tmp == 0 || carries == 0)
918 return false;
919 carries = expand_binop (word_mode, reverse_unsigned_shift,
920 carries, tmp, 0, unsignedp, methods);
921 if (carries == 0)
922 return false;
924 /* Shift INTO_INPUT logically by OP1. This is the last use of INTO_INPUT
925 so the result can go directly into INTO_TARGET if convenient. */
926 tmp = expand_binop (word_mode, unsigned_shift, into_input, op1,
927 into_target, unsignedp, methods);
928 if (tmp == 0)
929 return false;
931 /* Now OR in the bits carried over from OUTOF_INPUT. */
932 if (!force_expand_binop (word_mode, ior_optab, tmp, carries,
933 into_target, unsignedp, methods))
934 return false;
936 /* Use a standard word_mode shift for the out-of half. */
937 if (outof_target != 0)
938 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
939 outof_target, unsignedp, methods))
940 return false;
942 return true;
946 #ifdef HAVE_conditional_move
947 /* Try implementing expand_doubleword_shift using conditional moves.
948 The shift is by < BITS_PER_WORD if (CMP_CODE CMP1 CMP2) is true,
949 otherwise it is by >= BITS_PER_WORD. SUBWORD_OP1 and SUPERWORD_OP1
950 are the shift counts to use in the former and latter case. All other
951 arguments are the same as the parent routine. */
953 static bool
954 expand_doubleword_shift_condmove (enum machine_mode op1_mode, optab binoptab,
955 enum rtx_code cmp_code, rtx cmp1, rtx cmp2,
956 rtx outof_input, rtx into_input,
957 rtx subword_op1, rtx superword_op1,
958 rtx outof_target, rtx into_target,
959 int unsignedp, enum optab_methods methods,
960 unsigned HOST_WIDE_INT shift_mask)
962 rtx outof_superword, into_superword;
964 /* Put the superword version of the output into OUTOF_SUPERWORD and
965 INTO_SUPERWORD. */
966 outof_superword = outof_target != 0 ? gen_reg_rtx (word_mode) : 0;
967 if (outof_target != 0 && subword_op1 == superword_op1)
969 /* The value INTO_TARGET >> SUBWORD_OP1, which we later store in
970 OUTOF_TARGET, is the same as the value of INTO_SUPERWORD. */
971 into_superword = outof_target;
972 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
973 outof_superword, 0, unsignedp, methods))
974 return false;
976 else
978 into_superword = gen_reg_rtx (word_mode);
979 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
980 outof_superword, into_superword,
981 unsignedp, methods))
982 return false;
985 /* Put the subword version directly in OUTOF_TARGET and INTO_TARGET. */
986 if (!expand_subword_shift (op1_mode, binoptab,
987 outof_input, into_input, subword_op1,
988 outof_target, into_target,
989 unsignedp, methods, shift_mask))
990 return false;
992 /* Select between them. Do the INTO half first because INTO_SUPERWORD
993 might be the current value of OUTOF_TARGET. */
994 if (!emit_conditional_move (into_target, cmp_code, cmp1, cmp2, op1_mode,
995 into_target, into_superword, word_mode, false))
996 return false;
998 if (outof_target != 0)
999 if (!emit_conditional_move (outof_target, cmp_code, cmp1, cmp2, op1_mode,
1000 outof_target, outof_superword,
1001 word_mode, false))
1002 return false;
1004 return true;
1006 #endif
1008 /* Expand a doubleword shift (ashl, ashr or lshr) using word-mode shifts.
1009 OUTOF_INPUT and INTO_INPUT are the two word-sized halves of the first
1010 input operand; the shift moves bits in the direction OUTOF_INPUT->
1011 INTO_TARGET. OUTOF_TARGET and INTO_TARGET are the equivalent words
1012 of the target. OP1 is the shift count and OP1_MODE is its mode.
1013 If OP1 is constant, it will have been truncated as appropriate
1014 and is known to be nonzero.
1016 If SHIFT_MASK is zero, the result of word shifts is undefined when the
1017 shift count is outside the range [0, BITS_PER_WORD). This routine must
1018 avoid generating such shifts for OP1s in the range [0, BITS_PER_WORD * 2).
1020 If SHIFT_MASK is nonzero, all word-mode shift counts are effectively
1021 masked by it and shifts in the range [BITS_PER_WORD, SHIFT_MASK) will
1022 fill with zeros or sign bits as appropriate.
1024 If SHIFT_MASK is BITS_PER_WORD - 1, this routine will synthesize
1025 a doubleword shift whose equivalent mask is BITS_PER_WORD * 2 - 1.
1026 Doing this preserves semantics required by SHIFT_COUNT_TRUNCATED.
1027 In all other cases, shifts by values outside [0, BITS_PER_UNIT * 2)
1028 are undefined.
1030 BINOPTAB, UNSIGNEDP and METHODS are as for expand_binop. This function
1031 may not use INTO_INPUT after modifying INTO_TARGET, and similarly for
1032 OUTOF_INPUT and OUTOF_TARGET. OUTOF_TARGET can be null if the parent
1033 function wants to calculate it itself.
1035 Return true if the shift could be successfully synthesized. */
1037 static bool
1038 expand_doubleword_shift (enum machine_mode op1_mode, optab binoptab,
1039 rtx outof_input, rtx into_input, rtx op1,
1040 rtx outof_target, rtx into_target,
1041 int unsignedp, enum optab_methods methods,
1042 unsigned HOST_WIDE_INT shift_mask)
1044 rtx superword_op1, tmp, cmp1, cmp2;
1045 rtx subword_label, done_label;
1046 enum rtx_code cmp_code;
1048 /* See if word-mode shifts by BITS_PER_WORD...BITS_PER_WORD * 2 - 1 will
1049 fill the result with sign or zero bits as appropriate. If so, the value
1050 of OUTOF_TARGET will always be (SHIFT OUTOF_INPUT OP1). Recursively call
1051 this routine to calculate INTO_TARGET (which depends on both OUTOF_INPUT
1052 and INTO_INPUT), then emit code to set up OUTOF_TARGET.
1054 This isn't worthwhile for constant shifts since the optimizers will
1055 cope better with in-range shift counts. */
1056 if (shift_mask >= BITS_PER_WORD
1057 && outof_target != 0
1058 && !CONSTANT_P (op1))
1060 if (!expand_doubleword_shift (op1_mode, binoptab,
1061 outof_input, into_input, op1,
1062 0, into_target,
1063 unsignedp, methods, shift_mask))
1064 return false;
1065 if (!force_expand_binop (word_mode, binoptab, outof_input, op1,
1066 outof_target, unsignedp, methods))
1067 return false;
1068 return true;
1071 /* Set CMP_CODE, CMP1 and CMP2 so that the rtx (CMP_CODE CMP1 CMP2)
1072 is true when the effective shift value is less than BITS_PER_WORD.
1073 Set SUPERWORD_OP1 to the shift count that should be used to shift
1074 OUTOF_INPUT into INTO_TARGET when the condition is false. */
1075 tmp = immed_double_const (BITS_PER_WORD, 0, op1_mode);
1076 if (!CONSTANT_P (op1) && shift_mask == BITS_PER_WORD - 1)
1078 /* Set CMP1 to OP1 & BITS_PER_WORD. The result is zero iff OP1
1079 is a subword shift count. */
1080 cmp1 = simplify_expand_binop (op1_mode, and_optab, op1, tmp,
1081 0, true, methods);
1082 cmp2 = CONST0_RTX (op1_mode);
1083 cmp_code = EQ;
1084 superword_op1 = op1;
1086 else
1088 /* Set CMP1 to OP1 - BITS_PER_WORD. */
1089 cmp1 = simplify_expand_binop (op1_mode, sub_optab, op1, tmp,
1090 0, true, methods);
1091 cmp2 = CONST0_RTX (op1_mode);
1092 cmp_code = LT;
1093 superword_op1 = cmp1;
1095 if (cmp1 == 0)
1096 return false;
1098 /* If we can compute the condition at compile time, pick the
1099 appropriate subroutine. */
1100 tmp = simplify_relational_operation (cmp_code, SImode, op1_mode, cmp1, cmp2);
1101 if (tmp != 0 && CONST_INT_P (tmp))
1103 if (tmp == const0_rtx)
1104 return expand_superword_shift (binoptab, outof_input, superword_op1,
1105 outof_target, into_target,
1106 unsignedp, methods);
1107 else
1108 return expand_subword_shift (op1_mode, binoptab,
1109 outof_input, into_input, op1,
1110 outof_target, into_target,
1111 unsignedp, methods, shift_mask);
1114 #ifdef HAVE_conditional_move
1115 /* Try using conditional moves to generate straight-line code. */
1117 rtx start = get_last_insn ();
1118 if (expand_doubleword_shift_condmove (op1_mode, binoptab,
1119 cmp_code, cmp1, cmp2,
1120 outof_input, into_input,
1121 op1, superword_op1,
1122 outof_target, into_target,
1123 unsignedp, methods, shift_mask))
1124 return true;
1125 delete_insns_since (start);
1127 #endif
1129 /* As a last resort, use branches to select the correct alternative. */
1130 subword_label = gen_label_rtx ();
1131 done_label = gen_label_rtx ();
1133 NO_DEFER_POP;
1134 do_compare_rtx_and_jump (cmp1, cmp2, cmp_code, false, op1_mode,
1135 0, 0, subword_label);
1136 OK_DEFER_POP;
1138 if (!expand_superword_shift (binoptab, outof_input, superword_op1,
1139 outof_target, into_target,
1140 unsignedp, methods))
1141 return false;
1143 emit_jump_insn (gen_jump (done_label));
1144 emit_barrier ();
1145 emit_label (subword_label);
1147 if (!expand_subword_shift (op1_mode, binoptab,
1148 outof_input, into_input, op1,
1149 outof_target, into_target,
1150 unsignedp, methods, shift_mask))
1151 return false;
1153 emit_label (done_label);
1154 return true;
1157 /* Subroutine of expand_binop. Perform a double word multiplication of
1158 operands OP0 and OP1 both of mode MODE, which is exactly twice as wide
1159 as the target's word_mode. This function return NULL_RTX if anything
1160 goes wrong, in which case it may have already emitted instructions
1161 which need to be deleted.
1163 If we want to multiply two two-word values and have normal and widening
1164 multiplies of single-word values, we can do this with three smaller
1165 multiplications.
1167 The multiplication proceeds as follows:
1168 _______________________
1169 [__op0_high_|__op0_low__]
1170 _______________________
1171 * [__op1_high_|__op1_low__]
1172 _______________________________________________
1173 _______________________
1174 (1) [__op0_low__*__op1_low__]
1175 _______________________
1176 (2a) [__op0_low__*__op1_high_]
1177 _______________________
1178 (2b) [__op0_high_*__op1_low__]
1179 _______________________
1180 (3) [__op0_high_*__op1_high_]
1183 This gives a 4-word result. Since we are only interested in the
1184 lower 2 words, partial result (3) and the upper words of (2a) and
1185 (2b) don't need to be calculated. Hence (2a) and (2b) can be
1186 calculated using non-widening multiplication.
1188 (1), however, needs to be calculated with an unsigned widening
1189 multiplication. If this operation is not directly supported we
1190 try using a signed widening multiplication and adjust the result.
1191 This adjustment works as follows:
1193 If both operands are positive then no adjustment is needed.
1195 If the operands have different signs, for example op0_low < 0 and
1196 op1_low >= 0, the instruction treats the most significant bit of
1197 op0_low as a sign bit instead of a bit with significance
1198 2**(BITS_PER_WORD-1), i.e. the instruction multiplies op1_low
1199 with 2**BITS_PER_WORD - op0_low, and two's complements the
1200 result. Conclusion: We need to add op1_low * 2**BITS_PER_WORD to
1201 the result.
1203 Similarly, if both operands are negative, we need to add
1204 (op0_low + op1_low) * 2**BITS_PER_WORD.
1206 We use a trick to adjust quickly. We logically shift op0_low right
1207 (op1_low) BITS_PER_WORD-1 steps to get 0 or 1, and add this to
1208 op0_high (op1_high) before it is used to calculate 2b (2a). If no
1209 logical shift exists, we do an arithmetic right shift and subtract
1210 the 0 or -1. */
1212 static rtx
1213 expand_doubleword_mult (enum machine_mode mode, rtx op0, rtx op1, rtx target,
1214 bool umulp, enum optab_methods methods)
1216 int low = (WORDS_BIG_ENDIAN ? 1 : 0);
1217 int high = (WORDS_BIG_ENDIAN ? 0 : 1);
1218 rtx wordm1 = umulp ? NULL_RTX : GEN_INT (BITS_PER_WORD - 1);
1219 rtx product, adjust, product_high, temp;
1221 rtx op0_high = operand_subword_force (op0, high, mode);
1222 rtx op0_low = operand_subword_force (op0, low, mode);
1223 rtx op1_high = operand_subword_force (op1, high, mode);
1224 rtx op1_low = operand_subword_force (op1, low, mode);
1226 /* If we're using an unsigned multiply to directly compute the product
1227 of the low-order words of the operands and perform any required
1228 adjustments of the operands, we begin by trying two more multiplications
1229 and then computing the appropriate sum.
1231 We have checked above that the required addition is provided.
1232 Full-word addition will normally always succeed, especially if
1233 it is provided at all, so we don't worry about its failure. The
1234 multiplication may well fail, however, so we do handle that. */
1236 if (!umulp)
1238 /* ??? This could be done with emit_store_flag where available. */
1239 temp = expand_binop (word_mode, lshr_optab, op0_low, wordm1,
1240 NULL_RTX, 1, methods);
1241 if (temp)
1242 op0_high = expand_binop (word_mode, add_optab, op0_high, temp,
1243 NULL_RTX, 0, OPTAB_DIRECT);
1244 else
1246 temp = expand_binop (word_mode, ashr_optab, op0_low, wordm1,
1247 NULL_RTX, 0, methods);
1248 if (!temp)
1249 return NULL_RTX;
1250 op0_high = expand_binop (word_mode, sub_optab, op0_high, temp,
1251 NULL_RTX, 0, OPTAB_DIRECT);
1254 if (!op0_high)
1255 return NULL_RTX;
1258 adjust = expand_binop (word_mode, smul_optab, op0_high, op1_low,
1259 NULL_RTX, 0, OPTAB_DIRECT);
1260 if (!adjust)
1261 return NULL_RTX;
1263 /* OP0_HIGH should now be dead. */
1265 if (!umulp)
1267 /* ??? This could be done with emit_store_flag where available. */
1268 temp = expand_binop (word_mode, lshr_optab, op1_low, wordm1,
1269 NULL_RTX, 1, methods);
1270 if (temp)
1271 op1_high = expand_binop (word_mode, add_optab, op1_high, temp,
1272 NULL_RTX, 0, OPTAB_DIRECT);
1273 else
1275 temp = expand_binop (word_mode, ashr_optab, op1_low, wordm1,
1276 NULL_RTX, 0, methods);
1277 if (!temp)
1278 return NULL_RTX;
1279 op1_high = expand_binop (word_mode, sub_optab, op1_high, temp,
1280 NULL_RTX, 0, OPTAB_DIRECT);
1283 if (!op1_high)
1284 return NULL_RTX;
1287 temp = expand_binop (word_mode, smul_optab, op1_high, op0_low,
1288 NULL_RTX, 0, OPTAB_DIRECT);
1289 if (!temp)
1290 return NULL_RTX;
1292 /* OP1_HIGH should now be dead. */
1294 adjust = expand_binop (word_mode, add_optab, adjust, temp,
1295 adjust, 0, OPTAB_DIRECT);
1297 if (target && !REG_P (target))
1298 target = NULL_RTX;
1300 if (umulp)
1301 product = expand_binop (mode, umul_widen_optab, op0_low, op1_low,
1302 target, 1, OPTAB_DIRECT);
1303 else
1304 product = expand_binop (mode, smul_widen_optab, op0_low, op1_low,
1305 target, 1, OPTAB_DIRECT);
1307 if (!product)
1308 return NULL_RTX;
1310 product_high = operand_subword (product, high, 1, mode);
1311 adjust = expand_binop (word_mode, add_optab, product_high, adjust,
1312 REG_P (product_high) ? product_high : adjust,
1313 0, OPTAB_DIRECT);
1314 emit_move_insn (product_high, adjust);
1315 return product;
1318 /* Wrapper around expand_binop which takes an rtx code to specify
1319 the operation to perform, not an optab pointer. All other
1320 arguments are the same. */
1322 expand_simple_binop (enum machine_mode mode, enum rtx_code code, rtx op0,
1323 rtx op1, rtx target, int unsignedp,
1324 enum optab_methods methods)
1326 optab binop = code_to_optab[(int) code];
1327 gcc_assert (binop);
1329 return expand_binop (mode, binop, op0, op1, target, unsignedp, methods);
1332 /* Return whether OP0 and OP1 should be swapped when expanding a commutative
1333 binop. Order them according to commutative_operand_precedence and, if
1334 possible, try to put TARGET or a pseudo first. */
1335 static bool
1336 swap_commutative_operands_with_target (rtx target, rtx op0, rtx op1)
1338 int op0_prec = commutative_operand_precedence (op0);
1339 int op1_prec = commutative_operand_precedence (op1);
1341 if (op0_prec < op1_prec)
1342 return true;
1344 if (op0_prec > op1_prec)
1345 return false;
1347 /* With equal precedence, both orders are ok, but it is better if the
1348 first operand is TARGET, or if both TARGET and OP0 are pseudos. */
1349 if (target == 0 || REG_P (target))
1350 return (REG_P (op1) && !REG_P (op0)) || target == op1;
1351 else
1352 return rtx_equal_p (op1, target);
1355 /* Return true if BINOPTAB implements a shift operation. */
1357 static bool
1358 shift_optab_p (optab binoptab)
1360 switch (binoptab->code)
1362 case ASHIFT:
1363 case SS_ASHIFT:
1364 case US_ASHIFT:
1365 case ASHIFTRT:
1366 case LSHIFTRT:
1367 case ROTATE:
1368 case ROTATERT:
1369 return true;
1371 default:
1372 return false;
1376 /* Return true if BINOPTAB implements a commutative binary operation. */
1378 static bool
1379 commutative_optab_p (optab binoptab)
1381 return (GET_RTX_CLASS (binoptab->code) == RTX_COMM_ARITH
1382 || binoptab == smul_widen_optab
1383 || binoptab == umul_widen_optab
1384 || binoptab == smul_highpart_optab
1385 || binoptab == umul_highpart_optab);
1388 /* X is to be used in mode MODE as an operand to BINOPTAB. If we're
1389 optimizing, and if the operand is a constant that costs more than
1390 1 instruction, force the constant into a register and return that
1391 register. Return X otherwise. UNSIGNEDP says whether X is unsigned. */
1393 static rtx
1394 avoid_expensive_constant (enum machine_mode mode, optab binoptab,
1395 rtx x, bool unsignedp)
1397 if (mode != VOIDmode
1398 && optimize
1399 && CONSTANT_P (x)
1400 && rtx_cost (x, binoptab->code, optimize_insn_for_speed_p ())
1401 > COSTS_N_INSNS (1))
1403 if (CONST_INT_P (x))
1405 HOST_WIDE_INT intval = trunc_int_for_mode (INTVAL (x), mode);
1406 if (intval != INTVAL (x))
1407 x = GEN_INT (intval);
1409 else
1410 x = convert_modes (mode, VOIDmode, x, unsignedp);
1411 x = force_reg (mode, x);
1413 return x;
1416 /* Helper function for expand_binop: handle the case where there
1417 is an insn that directly implements the indicated operation.
1418 Returns null if this is not possible. */
1419 static rtx
1420 expand_binop_directly (enum machine_mode mode, optab binoptab,
1421 rtx op0, rtx op1,
1422 rtx target, int unsignedp, enum optab_methods methods,
1423 rtx last)
1425 int icode = (int) optab_handler (binoptab, mode)->insn_code;
1426 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
1427 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
1428 enum machine_mode tmp_mode;
1429 bool commutative_p;
1430 rtx pat;
1431 rtx xop0 = op0, xop1 = op1;
1432 rtx temp;
1433 rtx swap;
1435 if (target)
1436 temp = target;
1437 else
1438 temp = gen_reg_rtx (mode);
1440 /* If it is a commutative operator and the modes would match
1441 if we would swap the operands, we can save the conversions. */
1442 commutative_p = commutative_optab_p (binoptab);
1443 if (commutative_p
1444 && GET_MODE (xop0) != mode0 && GET_MODE (xop1) != mode1
1445 && GET_MODE (xop0) == mode1 && GET_MODE (xop1) == mode1)
1447 swap = xop0;
1448 xop0 = xop1;
1449 xop1 = swap;
1452 /* If we are optimizing, force expensive constants into a register. */
1453 xop0 = avoid_expensive_constant (mode0, binoptab, xop0, unsignedp);
1454 if (!shift_optab_p (binoptab))
1455 xop1 = avoid_expensive_constant (mode1, binoptab, xop1, unsignedp);
1457 /* In case the insn wants input operands in modes different from
1458 those of the actual operands, convert the operands. It would
1459 seem that we don't need to convert CONST_INTs, but we do, so
1460 that they're properly zero-extended, sign-extended or truncated
1461 for their mode. */
1463 if (GET_MODE (xop0) != mode0 && mode0 != VOIDmode)
1464 xop0 = convert_modes (mode0,
1465 GET_MODE (xop0) != VOIDmode
1466 ? GET_MODE (xop0)
1467 : mode,
1468 xop0, unsignedp);
1470 if (GET_MODE (xop1) != mode1 && mode1 != VOIDmode)
1471 xop1 = convert_modes (mode1,
1472 GET_MODE (xop1) != VOIDmode
1473 ? GET_MODE (xop1)
1474 : mode,
1475 xop1, unsignedp);
1477 /* If operation is commutative,
1478 try to make the first operand a register.
1479 Even better, try to make it the same as the target.
1480 Also try to make the last operand a constant. */
1481 if (commutative_p
1482 && swap_commutative_operands_with_target (target, xop0, xop1))
1484 swap = xop1;
1485 xop1 = xop0;
1486 xop0 = swap;
1489 /* Now, if insn's predicates don't allow our operands, put them into
1490 pseudo regs. */
1492 if (!insn_data[icode].operand[1].predicate (xop0, mode0)
1493 && mode0 != VOIDmode)
1494 xop0 = copy_to_mode_reg (mode0, xop0);
1496 if (!insn_data[icode].operand[2].predicate (xop1, mode1)
1497 && mode1 != VOIDmode)
1498 xop1 = copy_to_mode_reg (mode1, xop1);
1500 if (binoptab == vec_pack_trunc_optab
1501 || binoptab == vec_pack_usat_optab
1502 || binoptab == vec_pack_ssat_optab
1503 || binoptab == vec_pack_ufix_trunc_optab
1504 || binoptab == vec_pack_sfix_trunc_optab)
1506 /* The mode of the result is different then the mode of the
1507 arguments. */
1508 tmp_mode = insn_data[icode].operand[0].mode;
1509 if (GET_MODE_NUNITS (tmp_mode) != 2 * GET_MODE_NUNITS (mode))
1510 return 0;
1512 else
1513 tmp_mode = mode;
1515 if (!insn_data[icode].operand[0].predicate (temp, tmp_mode))
1516 temp = gen_reg_rtx (tmp_mode);
1518 pat = GEN_FCN (icode) (temp, xop0, xop1);
1519 if (pat)
1521 /* If PAT is composed of more than one insn, try to add an appropriate
1522 REG_EQUAL note to it. If we can't because TEMP conflicts with an
1523 operand, call expand_binop again, this time without a target. */
1524 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
1525 && ! add_equal_note (pat, temp, binoptab->code, xop0, xop1))
1527 delete_insns_since (last);
1528 return expand_binop (mode, binoptab, op0, op1, NULL_RTX,
1529 unsignedp, methods);
1532 emit_insn (pat);
1533 return temp;
1536 delete_insns_since (last);
1537 return NULL_RTX;
1540 /* Generate code to perform an operation specified by BINOPTAB
1541 on operands OP0 and OP1, with result having machine-mode MODE.
1543 UNSIGNEDP is for the case where we have to widen the operands
1544 to perform the operation. It says to use zero-extension.
1546 If TARGET is nonzero, the value
1547 is generated there, if it is convenient to do so.
1548 In all cases an rtx is returned for the locus of the value;
1549 this may or may not be TARGET. */
1552 expand_binop (enum machine_mode mode, optab binoptab, rtx op0, rtx op1,
1553 rtx target, int unsignedp, enum optab_methods methods)
1555 enum optab_methods next_methods
1556 = (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN
1557 ? OPTAB_WIDEN : methods);
1558 enum mode_class mclass;
1559 enum machine_mode wider_mode;
1560 rtx libfunc;
1561 rtx temp;
1562 rtx entry_last = get_last_insn ();
1563 rtx last;
1565 mclass = GET_MODE_CLASS (mode);
1567 /* If subtracting an integer constant, convert this into an addition of
1568 the negated constant. */
1570 if (binoptab == sub_optab && CONST_INT_P (op1))
1572 op1 = negate_rtx (mode, op1);
1573 binoptab = add_optab;
1576 /* Record where to delete back to if we backtrack. */
1577 last = get_last_insn ();
1579 /* If we can do it with a three-operand insn, do so. */
1581 if (methods != OPTAB_MUST_WIDEN
1582 && optab_handler (binoptab, mode)->insn_code != CODE_FOR_nothing)
1584 temp = expand_binop_directly (mode, binoptab, op0, op1, target,
1585 unsignedp, methods, last);
1586 if (temp)
1587 return temp;
1590 /* If we were trying to rotate, and that didn't work, try rotating
1591 the other direction before falling back to shifts and bitwise-or. */
1592 if (((binoptab == rotl_optab
1593 && optab_handler (rotr_optab, mode)->insn_code != CODE_FOR_nothing)
1594 || (binoptab == rotr_optab
1595 && optab_handler (rotl_optab, mode)->insn_code != CODE_FOR_nothing))
1596 && mclass == MODE_INT)
1598 optab otheroptab = (binoptab == rotl_optab ? rotr_optab : rotl_optab);
1599 rtx newop1;
1600 unsigned int bits = GET_MODE_BITSIZE (mode);
1602 if (CONST_INT_P (op1))
1603 newop1 = GEN_INT (bits - INTVAL (op1));
1604 else if (targetm.shift_truncation_mask (mode) == bits - 1)
1605 newop1 = negate_rtx (mode, op1);
1606 else
1607 newop1 = expand_binop (mode, sub_optab,
1608 GEN_INT (bits), op1,
1609 NULL_RTX, unsignedp, OPTAB_DIRECT);
1611 temp = expand_binop_directly (mode, otheroptab, op0, newop1,
1612 target, unsignedp, methods, last);
1613 if (temp)
1614 return temp;
1617 /* If this is a multiply, see if we can do a widening operation that
1618 takes operands of this mode and makes a wider mode. */
1620 if (binoptab == smul_optab
1621 && GET_MODE_WIDER_MODE (mode) != VOIDmode
1622 && ((optab_handler ((unsignedp ? umul_widen_optab : smul_widen_optab),
1623 GET_MODE_WIDER_MODE (mode))->insn_code)
1624 != CODE_FOR_nothing))
1626 temp = expand_binop (GET_MODE_WIDER_MODE (mode),
1627 unsignedp ? umul_widen_optab : smul_widen_optab,
1628 op0, op1, NULL_RTX, unsignedp, OPTAB_DIRECT);
1630 if (temp != 0)
1632 if (GET_MODE_CLASS (mode) == MODE_INT
1633 && TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1634 GET_MODE_BITSIZE (GET_MODE (temp))))
1635 return gen_lowpart (mode, temp);
1636 else
1637 return convert_to_mode (mode, temp, unsignedp);
1641 /* Look for a wider mode of the same class for which we think we
1642 can open-code the operation. Check for a widening multiply at the
1643 wider mode as well. */
1645 if (CLASS_HAS_WIDER_MODES_P (mclass)
1646 && methods != OPTAB_DIRECT && methods != OPTAB_LIB)
1647 for (wider_mode = GET_MODE_WIDER_MODE (mode);
1648 wider_mode != VOIDmode;
1649 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
1651 if (optab_handler (binoptab, wider_mode)->insn_code != CODE_FOR_nothing
1652 || (binoptab == smul_optab
1653 && GET_MODE_WIDER_MODE (wider_mode) != VOIDmode
1654 && ((optab_handler ((unsignedp ? umul_widen_optab
1655 : smul_widen_optab),
1656 GET_MODE_WIDER_MODE (wider_mode))->insn_code)
1657 != CODE_FOR_nothing)))
1659 rtx xop0 = op0, xop1 = op1;
1660 int no_extend = 0;
1662 /* For certain integer operations, we need not actually extend
1663 the narrow operands, as long as we will truncate
1664 the results to the same narrowness. */
1666 if ((binoptab == ior_optab || binoptab == and_optab
1667 || binoptab == xor_optab
1668 || binoptab == add_optab || binoptab == sub_optab
1669 || binoptab == smul_optab || binoptab == ashl_optab)
1670 && mclass == MODE_INT)
1672 no_extend = 1;
1673 xop0 = avoid_expensive_constant (mode, binoptab,
1674 xop0, unsignedp);
1675 if (binoptab != ashl_optab)
1676 xop1 = avoid_expensive_constant (mode, binoptab,
1677 xop1, unsignedp);
1680 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp, no_extend);
1682 /* The second operand of a shift must always be extended. */
1683 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
1684 no_extend && binoptab != ashl_optab);
1686 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
1687 unsignedp, OPTAB_DIRECT);
1688 if (temp)
1690 if (mclass != MODE_INT
1691 || !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
1692 GET_MODE_BITSIZE (wider_mode)))
1694 if (target == 0)
1695 target = gen_reg_rtx (mode);
1696 convert_move (target, temp, 0);
1697 return target;
1699 else
1700 return gen_lowpart (mode, temp);
1702 else
1703 delete_insns_since (last);
1707 /* If operation is commutative,
1708 try to make the first operand a register.
1709 Even better, try to make it the same as the target.
1710 Also try to make the last operand a constant. */
1711 if (commutative_optab_p (binoptab)
1712 && swap_commutative_operands_with_target (target, op0, op1))
1714 temp = op1;
1715 op1 = op0;
1716 op0 = temp;
1719 /* These can be done a word at a time. */
1720 if ((binoptab == and_optab || binoptab == ior_optab || binoptab == xor_optab)
1721 && mclass == MODE_INT
1722 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
1723 && optab_handler (binoptab, word_mode)->insn_code != CODE_FOR_nothing)
1725 int i;
1726 rtx insns;
1727 rtx equiv_value;
1729 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1730 won't be accurate, so use a new target. */
1731 if (target == 0 || target == op0 || target == op1)
1732 target = gen_reg_rtx (mode);
1734 start_sequence ();
1736 /* Do the actual arithmetic. */
1737 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
1739 rtx target_piece = operand_subword (target, i, 1, mode);
1740 rtx x = expand_binop (word_mode, binoptab,
1741 operand_subword_force (op0, i, mode),
1742 operand_subword_force (op1, i, mode),
1743 target_piece, unsignedp, next_methods);
1745 if (x == 0)
1746 break;
1748 if (target_piece != x)
1749 emit_move_insn (target_piece, x);
1752 insns = get_insns ();
1753 end_sequence ();
1755 if (i == GET_MODE_BITSIZE (mode) / BITS_PER_WORD)
1757 if (binoptab->code != UNKNOWN)
1758 equiv_value
1759 = gen_rtx_fmt_ee (binoptab->code, mode,
1760 copy_rtx (op0), copy_rtx (op1));
1761 else
1762 equiv_value = 0;
1764 emit_insn (insns);
1765 return target;
1769 /* Synthesize double word shifts from single word shifts. */
1770 if ((binoptab == lshr_optab || binoptab == ashl_optab
1771 || binoptab == ashr_optab)
1772 && mclass == MODE_INT
1773 && (CONST_INT_P (op1) || optimize_insn_for_speed_p ())
1774 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1775 && optab_handler (binoptab, word_mode)->insn_code != CODE_FOR_nothing
1776 && optab_handler (ashl_optab, word_mode)->insn_code != CODE_FOR_nothing
1777 && optab_handler (lshr_optab, word_mode)->insn_code != CODE_FOR_nothing)
1779 unsigned HOST_WIDE_INT shift_mask, double_shift_mask;
1780 enum machine_mode op1_mode;
1782 double_shift_mask = targetm.shift_truncation_mask (mode);
1783 shift_mask = targetm.shift_truncation_mask (word_mode);
1784 op1_mode = GET_MODE (op1) != VOIDmode ? GET_MODE (op1) : word_mode;
1786 /* Apply the truncation to constant shifts. */
1787 if (double_shift_mask > 0 && CONST_INT_P (op1))
1788 op1 = GEN_INT (INTVAL (op1) & double_shift_mask);
1790 if (op1 == CONST0_RTX (op1_mode))
1791 return op0;
1793 /* Make sure that this is a combination that expand_doubleword_shift
1794 can handle. See the comments there for details. */
1795 if (double_shift_mask == 0
1796 || (shift_mask == BITS_PER_WORD - 1
1797 && double_shift_mask == BITS_PER_WORD * 2 - 1))
1799 rtx insns;
1800 rtx into_target, outof_target;
1801 rtx into_input, outof_input;
1802 int left_shift, outof_word;
1804 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1805 won't be accurate, so use a new target. */
1806 if (target == 0 || target == op0 || target == op1)
1807 target = gen_reg_rtx (mode);
1809 start_sequence ();
1811 /* OUTOF_* is the word we are shifting bits away from, and
1812 INTO_* is the word that we are shifting bits towards, thus
1813 they differ depending on the direction of the shift and
1814 WORDS_BIG_ENDIAN. */
1816 left_shift = binoptab == ashl_optab;
1817 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1819 outof_target = operand_subword (target, outof_word, 1, mode);
1820 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1822 outof_input = operand_subword_force (op0, outof_word, mode);
1823 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1825 if (expand_doubleword_shift (op1_mode, binoptab,
1826 outof_input, into_input, op1,
1827 outof_target, into_target,
1828 unsignedp, next_methods, shift_mask))
1830 insns = get_insns ();
1831 end_sequence ();
1833 emit_insn (insns);
1834 return target;
1836 end_sequence ();
1840 /* Synthesize double word rotates from single word shifts. */
1841 if ((binoptab == rotl_optab || binoptab == rotr_optab)
1842 && mclass == MODE_INT
1843 && CONST_INT_P (op1)
1844 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1845 && optab_handler (ashl_optab, word_mode)->insn_code != CODE_FOR_nothing
1846 && optab_handler (lshr_optab, word_mode)->insn_code != CODE_FOR_nothing)
1848 rtx insns;
1849 rtx into_target, outof_target;
1850 rtx into_input, outof_input;
1851 rtx inter;
1852 int shift_count, left_shift, outof_word;
1854 /* If TARGET is the same as one of the operands, the REG_EQUAL note
1855 won't be accurate, so use a new target. Do this also if target is not
1856 a REG, first because having a register instead may open optimization
1857 opportunities, and second because if target and op0 happen to be MEMs
1858 designating the same location, we would risk clobbering it too early
1859 in the code sequence we generate below. */
1860 if (target == 0 || target == op0 || target == op1 || ! REG_P (target))
1861 target = gen_reg_rtx (mode);
1863 start_sequence ();
1865 shift_count = INTVAL (op1);
1867 /* OUTOF_* is the word we are shifting bits away from, and
1868 INTO_* is the word that we are shifting bits towards, thus
1869 they differ depending on the direction of the shift and
1870 WORDS_BIG_ENDIAN. */
1872 left_shift = (binoptab == rotl_optab);
1873 outof_word = left_shift ^ ! WORDS_BIG_ENDIAN;
1875 outof_target = operand_subword (target, outof_word, 1, mode);
1876 into_target = operand_subword (target, 1 - outof_word, 1, mode);
1878 outof_input = operand_subword_force (op0, outof_word, mode);
1879 into_input = operand_subword_force (op0, 1 - outof_word, mode);
1881 if (shift_count == BITS_PER_WORD)
1883 /* This is just a word swap. */
1884 emit_move_insn (outof_target, into_input);
1885 emit_move_insn (into_target, outof_input);
1886 inter = const0_rtx;
1888 else
1890 rtx into_temp1, into_temp2, outof_temp1, outof_temp2;
1891 rtx first_shift_count, second_shift_count;
1892 optab reverse_unsigned_shift, unsigned_shift;
1894 reverse_unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1895 ? lshr_optab : ashl_optab);
1897 unsigned_shift = (left_shift ^ (shift_count < BITS_PER_WORD)
1898 ? ashl_optab : lshr_optab);
1900 if (shift_count > BITS_PER_WORD)
1902 first_shift_count = GEN_INT (shift_count - BITS_PER_WORD);
1903 second_shift_count = GEN_INT (2 * BITS_PER_WORD - shift_count);
1905 else
1907 first_shift_count = GEN_INT (BITS_PER_WORD - shift_count);
1908 second_shift_count = GEN_INT (shift_count);
1911 into_temp1 = expand_binop (word_mode, unsigned_shift,
1912 outof_input, first_shift_count,
1913 NULL_RTX, unsignedp, next_methods);
1914 into_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1915 into_input, second_shift_count,
1916 NULL_RTX, unsignedp, next_methods);
1918 if (into_temp1 != 0 && into_temp2 != 0)
1919 inter = expand_binop (word_mode, ior_optab, into_temp1, into_temp2,
1920 into_target, unsignedp, next_methods);
1921 else
1922 inter = 0;
1924 if (inter != 0 && inter != into_target)
1925 emit_move_insn (into_target, inter);
1927 outof_temp1 = expand_binop (word_mode, unsigned_shift,
1928 into_input, first_shift_count,
1929 NULL_RTX, unsignedp, next_methods);
1930 outof_temp2 = expand_binop (word_mode, reverse_unsigned_shift,
1931 outof_input, second_shift_count,
1932 NULL_RTX, unsignedp, next_methods);
1934 if (inter != 0 && outof_temp1 != 0 && outof_temp2 != 0)
1935 inter = expand_binop (word_mode, ior_optab,
1936 outof_temp1, outof_temp2,
1937 outof_target, unsignedp, next_methods);
1939 if (inter != 0 && inter != outof_target)
1940 emit_move_insn (outof_target, inter);
1943 insns = get_insns ();
1944 end_sequence ();
1946 if (inter != 0)
1948 emit_insn (insns);
1949 return target;
1953 /* These can be done a word at a time by propagating carries. */
1954 if ((binoptab == add_optab || binoptab == sub_optab)
1955 && mclass == MODE_INT
1956 && GET_MODE_SIZE (mode) >= 2 * UNITS_PER_WORD
1957 && optab_handler (binoptab, word_mode)->insn_code != CODE_FOR_nothing)
1959 unsigned int i;
1960 optab otheroptab = binoptab == add_optab ? sub_optab : add_optab;
1961 const unsigned int nwords = GET_MODE_BITSIZE (mode) / BITS_PER_WORD;
1962 rtx carry_in = NULL_RTX, carry_out = NULL_RTX;
1963 rtx xop0, xop1, xtarget;
1965 /* We can handle either a 1 or -1 value for the carry. If STORE_FLAG
1966 value is one of those, use it. Otherwise, use 1 since it is the
1967 one easiest to get. */
1968 #if STORE_FLAG_VALUE == 1 || STORE_FLAG_VALUE == -1
1969 int normalizep = STORE_FLAG_VALUE;
1970 #else
1971 int normalizep = 1;
1972 #endif
1974 /* Prepare the operands. */
1975 xop0 = force_reg (mode, op0);
1976 xop1 = force_reg (mode, op1);
1978 xtarget = gen_reg_rtx (mode);
1980 if (target == 0 || !REG_P (target))
1981 target = xtarget;
1983 /* Indicate for flow that the entire target reg is being set. */
1984 if (REG_P (target))
1985 emit_clobber (xtarget);
1987 /* Do the actual arithmetic. */
1988 for (i = 0; i < nwords; i++)
1990 int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
1991 rtx target_piece = operand_subword (xtarget, index, 1, mode);
1992 rtx op0_piece = operand_subword_force (xop0, index, mode);
1993 rtx op1_piece = operand_subword_force (xop1, index, mode);
1994 rtx x;
1996 /* Main add/subtract of the input operands. */
1997 x = expand_binop (word_mode, binoptab,
1998 op0_piece, op1_piece,
1999 target_piece, unsignedp, next_methods);
2000 if (x == 0)
2001 break;
2003 if (i + 1 < nwords)
2005 /* Store carry from main add/subtract. */
2006 carry_out = gen_reg_rtx (word_mode);
2007 carry_out = emit_store_flag_force (carry_out,
2008 (binoptab == add_optab
2009 ? LT : GT),
2010 x, op0_piece,
2011 word_mode, 1, normalizep);
2014 if (i > 0)
2016 rtx newx;
2018 /* Add/subtract previous carry to main result. */
2019 newx = expand_binop (word_mode,
2020 normalizep == 1 ? binoptab : otheroptab,
2021 x, carry_in,
2022 NULL_RTX, 1, next_methods);
2024 if (i + 1 < nwords)
2026 /* Get out carry from adding/subtracting carry in. */
2027 rtx carry_tmp = gen_reg_rtx (word_mode);
2028 carry_tmp = emit_store_flag_force (carry_tmp,
2029 (binoptab == add_optab
2030 ? LT : GT),
2031 newx, x,
2032 word_mode, 1, normalizep);
2034 /* Logical-ior the two poss. carry together. */
2035 carry_out = expand_binop (word_mode, ior_optab,
2036 carry_out, carry_tmp,
2037 carry_out, 0, next_methods);
2038 if (carry_out == 0)
2039 break;
2041 emit_move_insn (target_piece, newx);
2043 else
2045 if (x != target_piece)
2046 emit_move_insn (target_piece, x);
2049 carry_in = carry_out;
2052 if (i == GET_MODE_BITSIZE (mode) / (unsigned) BITS_PER_WORD)
2054 if (optab_handler (mov_optab, mode)->insn_code != CODE_FOR_nothing
2055 || ! rtx_equal_p (target, xtarget))
2057 rtx temp = emit_move_insn (target, xtarget);
2059 set_unique_reg_note (temp,
2060 REG_EQUAL,
2061 gen_rtx_fmt_ee (binoptab->code, mode,
2062 copy_rtx (xop0),
2063 copy_rtx (xop1)));
2065 else
2066 target = xtarget;
2068 return target;
2071 else
2072 delete_insns_since (last);
2075 /* Attempt to synthesize double word multiplies using a sequence of word
2076 mode multiplications. We first attempt to generate a sequence using a
2077 more efficient unsigned widening multiply, and if that fails we then
2078 try using a signed widening multiply. */
2080 if (binoptab == smul_optab
2081 && mclass == MODE_INT
2082 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
2083 && optab_handler (smul_optab, word_mode)->insn_code != CODE_FOR_nothing
2084 && optab_handler (add_optab, word_mode)->insn_code != CODE_FOR_nothing)
2086 rtx product = NULL_RTX;
2088 if (optab_handler (umul_widen_optab, mode)->insn_code
2089 != CODE_FOR_nothing)
2091 product = expand_doubleword_mult (mode, op0, op1, target,
2092 true, methods);
2093 if (!product)
2094 delete_insns_since (last);
2097 if (product == NULL_RTX
2098 && optab_handler (smul_widen_optab, mode)->insn_code
2099 != CODE_FOR_nothing)
2101 product = expand_doubleword_mult (mode, op0, op1, target,
2102 false, methods);
2103 if (!product)
2104 delete_insns_since (last);
2107 if (product != NULL_RTX)
2109 if (optab_handler (mov_optab, mode)->insn_code != CODE_FOR_nothing)
2111 temp = emit_move_insn (target ? target : product, product);
2112 set_unique_reg_note (temp,
2113 REG_EQUAL,
2114 gen_rtx_fmt_ee (MULT, mode,
2115 copy_rtx (op0),
2116 copy_rtx (op1)));
2118 return product;
2122 /* It can't be open-coded in this mode.
2123 Use a library call if one is available and caller says that's ok. */
2125 libfunc = optab_libfunc (binoptab, mode);
2126 if (libfunc
2127 && (methods == OPTAB_LIB || methods == OPTAB_LIB_WIDEN))
2129 rtx insns;
2130 rtx op1x = op1;
2131 enum machine_mode op1_mode = mode;
2132 rtx value;
2134 start_sequence ();
2136 if (shift_optab_p (binoptab))
2138 op1_mode = targetm.libgcc_shift_count_mode ();
2139 /* Specify unsigned here,
2140 since negative shift counts are meaningless. */
2141 op1x = convert_to_mode (op1_mode, op1, 1);
2144 if (GET_MODE (op0) != VOIDmode
2145 && GET_MODE (op0) != mode)
2146 op0 = convert_to_mode (mode, op0, unsignedp);
2148 /* Pass 1 for NO_QUEUE so we don't lose any increments
2149 if the libcall is cse'd or moved. */
2150 value = emit_library_call_value (libfunc,
2151 NULL_RTX, LCT_CONST, mode, 2,
2152 op0, mode, op1x, op1_mode);
2154 insns = get_insns ();
2155 end_sequence ();
2157 target = gen_reg_rtx (mode);
2158 emit_libcall_block (insns, target, value,
2159 gen_rtx_fmt_ee (binoptab->code, mode, op0, op1));
2161 return target;
2164 delete_insns_since (last);
2166 /* It can't be done in this mode. Can we do it in a wider mode? */
2168 if (! (methods == OPTAB_WIDEN || methods == OPTAB_LIB_WIDEN
2169 || methods == OPTAB_MUST_WIDEN))
2171 /* Caller says, don't even try. */
2172 delete_insns_since (entry_last);
2173 return 0;
2176 /* Compute the value of METHODS to pass to recursive calls.
2177 Don't allow widening to be tried recursively. */
2179 methods = (methods == OPTAB_LIB_WIDEN ? OPTAB_LIB : OPTAB_DIRECT);
2181 /* Look for a wider mode of the same class for which it appears we can do
2182 the operation. */
2184 if (CLASS_HAS_WIDER_MODES_P (mclass))
2186 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2187 wider_mode != VOIDmode;
2188 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2190 if ((optab_handler (binoptab, wider_mode)->insn_code
2191 != CODE_FOR_nothing)
2192 || (methods == OPTAB_LIB
2193 && optab_libfunc (binoptab, wider_mode)))
2195 rtx xop0 = op0, xop1 = op1;
2196 int no_extend = 0;
2198 /* For certain integer operations, we need not actually extend
2199 the narrow operands, as long as we will truncate
2200 the results to the same narrowness. */
2202 if ((binoptab == ior_optab || binoptab == and_optab
2203 || binoptab == xor_optab
2204 || binoptab == add_optab || binoptab == sub_optab
2205 || binoptab == smul_optab || binoptab == ashl_optab)
2206 && mclass == MODE_INT)
2207 no_extend = 1;
2209 xop0 = widen_operand (xop0, wider_mode, mode,
2210 unsignedp, no_extend);
2212 /* The second operand of a shift must always be extended. */
2213 xop1 = widen_operand (xop1, wider_mode, mode, unsignedp,
2214 no_extend && binoptab != ashl_optab);
2216 temp = expand_binop (wider_mode, binoptab, xop0, xop1, NULL_RTX,
2217 unsignedp, methods);
2218 if (temp)
2220 if (mclass != MODE_INT
2221 || !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
2222 GET_MODE_BITSIZE (wider_mode)))
2224 if (target == 0)
2225 target = gen_reg_rtx (mode);
2226 convert_move (target, temp, 0);
2227 return target;
2229 else
2230 return gen_lowpart (mode, temp);
2232 else
2233 delete_insns_since (last);
2238 delete_insns_since (entry_last);
2239 return 0;
2242 /* Expand a binary operator which has both signed and unsigned forms.
2243 UOPTAB is the optab for unsigned operations, and SOPTAB is for
2244 signed operations.
2246 If we widen unsigned operands, we may use a signed wider operation instead
2247 of an unsigned wider operation, since the result would be the same. */
2250 sign_expand_binop (enum machine_mode mode, optab uoptab, optab soptab,
2251 rtx op0, rtx op1, rtx target, int unsignedp,
2252 enum optab_methods methods)
2254 rtx temp;
2255 optab direct_optab = unsignedp ? uoptab : soptab;
2256 struct optab_d wide_soptab;
2258 /* Do it without widening, if possible. */
2259 temp = expand_binop (mode, direct_optab, op0, op1, target,
2260 unsignedp, OPTAB_DIRECT);
2261 if (temp || methods == OPTAB_DIRECT)
2262 return temp;
2264 /* Try widening to a signed int. Make a fake signed optab that
2265 hides any signed insn for direct use. */
2266 wide_soptab = *soptab;
2267 optab_handler (&wide_soptab, mode)->insn_code = CODE_FOR_nothing;
2268 /* We don't want to generate new hash table entries from this fake
2269 optab. */
2270 wide_soptab.libcall_gen = NULL;
2272 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2273 unsignedp, OPTAB_WIDEN);
2275 /* For unsigned operands, try widening to an unsigned int. */
2276 if (temp == 0 && unsignedp)
2277 temp = expand_binop (mode, uoptab, op0, op1, target,
2278 unsignedp, OPTAB_WIDEN);
2279 if (temp || methods == OPTAB_WIDEN)
2280 return temp;
2282 /* Use the right width libcall if that exists. */
2283 temp = expand_binop (mode, direct_optab, op0, op1, target, unsignedp, OPTAB_LIB);
2284 if (temp || methods == OPTAB_LIB)
2285 return temp;
2287 /* Must widen and use a libcall, use either signed or unsigned. */
2288 temp = expand_binop (mode, &wide_soptab, op0, op1, target,
2289 unsignedp, methods);
2290 if (temp != 0)
2291 return temp;
2292 if (unsignedp)
2293 return expand_binop (mode, uoptab, op0, op1, target,
2294 unsignedp, methods);
2295 return 0;
2298 /* Generate code to perform an operation specified by UNOPPTAB
2299 on operand OP0, with two results to TARG0 and TARG1.
2300 We assume that the order of the operands for the instruction
2301 is TARG0, TARG1, OP0.
2303 Either TARG0 or TARG1 may be zero, but what that means is that
2304 the result is not actually wanted. We will generate it into
2305 a dummy pseudo-reg and discard it. They may not both be zero.
2307 Returns 1 if this operation can be performed; 0 if not. */
2310 expand_twoval_unop (optab unoptab, rtx op0, rtx targ0, rtx targ1,
2311 int unsignedp)
2313 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2314 enum mode_class mclass;
2315 enum machine_mode wider_mode;
2316 rtx entry_last = get_last_insn ();
2317 rtx last;
2319 mclass = GET_MODE_CLASS (mode);
2321 if (!targ0)
2322 targ0 = gen_reg_rtx (mode);
2323 if (!targ1)
2324 targ1 = gen_reg_rtx (mode);
2326 /* Record where to go back to if we fail. */
2327 last = get_last_insn ();
2329 if (optab_handler (unoptab, mode)->insn_code != CODE_FOR_nothing)
2331 int icode = (int) optab_handler (unoptab, mode)->insn_code;
2332 enum machine_mode mode0 = insn_data[icode].operand[2].mode;
2333 rtx pat;
2334 rtx xop0 = op0;
2336 if (GET_MODE (xop0) != VOIDmode
2337 && GET_MODE (xop0) != mode0)
2338 xop0 = convert_to_mode (mode0, xop0, unsignedp);
2340 /* Now, if insn doesn't accept these operands, put them into pseudos. */
2341 if (!insn_data[icode].operand[2].predicate (xop0, mode0))
2342 xop0 = copy_to_mode_reg (mode0, xop0);
2344 /* We could handle this, but we should always be called with a pseudo
2345 for our targets and all insns should take them as outputs. */
2346 gcc_assert (insn_data[icode].operand[0].predicate (targ0, mode));
2347 gcc_assert (insn_data[icode].operand[1].predicate (targ1, mode));
2349 pat = GEN_FCN (icode) (targ0, targ1, xop0);
2350 if (pat)
2352 emit_insn (pat);
2353 return 1;
2355 else
2356 delete_insns_since (last);
2359 /* It can't be done in this mode. Can we do it in a wider mode? */
2361 if (CLASS_HAS_WIDER_MODES_P (mclass))
2363 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2364 wider_mode != VOIDmode;
2365 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2367 if (optab_handler (unoptab, wider_mode)->insn_code
2368 != CODE_FOR_nothing)
2370 rtx t0 = gen_reg_rtx (wider_mode);
2371 rtx t1 = gen_reg_rtx (wider_mode);
2372 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2374 if (expand_twoval_unop (unoptab, cop0, t0, t1, unsignedp))
2376 convert_move (targ0, t0, unsignedp);
2377 convert_move (targ1, t1, unsignedp);
2378 return 1;
2380 else
2381 delete_insns_since (last);
2386 delete_insns_since (entry_last);
2387 return 0;
2390 /* Generate code to perform an operation specified by BINOPTAB
2391 on operands OP0 and OP1, with two results to TARG1 and TARG2.
2392 We assume that the order of the operands for the instruction
2393 is TARG0, OP0, OP1, TARG1, which would fit a pattern like
2394 [(set TARG0 (operate OP0 OP1)) (set TARG1 (operate ...))].
2396 Either TARG0 or TARG1 may be zero, but what that means is that
2397 the result is not actually wanted. We will generate it into
2398 a dummy pseudo-reg and discard it. They may not both be zero.
2400 Returns 1 if this operation can be performed; 0 if not. */
2403 expand_twoval_binop (optab binoptab, rtx op0, rtx op1, rtx targ0, rtx targ1,
2404 int unsignedp)
2406 enum machine_mode mode = GET_MODE (targ0 ? targ0 : targ1);
2407 enum mode_class mclass;
2408 enum machine_mode wider_mode;
2409 rtx entry_last = get_last_insn ();
2410 rtx last;
2412 mclass = GET_MODE_CLASS (mode);
2414 if (!targ0)
2415 targ0 = gen_reg_rtx (mode);
2416 if (!targ1)
2417 targ1 = gen_reg_rtx (mode);
2419 /* Record where to go back to if we fail. */
2420 last = get_last_insn ();
2422 if (optab_handler (binoptab, mode)->insn_code != CODE_FOR_nothing)
2424 int icode = (int) optab_handler (binoptab, mode)->insn_code;
2425 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
2426 enum machine_mode mode1 = insn_data[icode].operand[2].mode;
2427 rtx pat;
2428 rtx xop0 = op0, xop1 = op1;
2430 /* If we are optimizing, force expensive constants into a register. */
2431 xop0 = avoid_expensive_constant (mode0, binoptab, xop0, unsignedp);
2432 xop1 = avoid_expensive_constant (mode1, binoptab, xop1, unsignedp);
2434 /* In case the insn wants input operands in modes different from
2435 those of the actual operands, convert the operands. It would
2436 seem that we don't need to convert CONST_INTs, but we do, so
2437 that they're properly zero-extended, sign-extended or truncated
2438 for their mode. */
2440 if (GET_MODE (op0) != mode0 && mode0 != VOIDmode)
2441 xop0 = convert_modes (mode0,
2442 GET_MODE (op0) != VOIDmode
2443 ? GET_MODE (op0)
2444 : mode,
2445 xop0, unsignedp);
2447 if (GET_MODE (op1) != mode1 && mode1 != VOIDmode)
2448 xop1 = convert_modes (mode1,
2449 GET_MODE (op1) != VOIDmode
2450 ? GET_MODE (op1)
2451 : mode,
2452 xop1, unsignedp);
2454 /* Now, if insn doesn't accept these operands, put them into pseudos. */
2455 if (!insn_data[icode].operand[1].predicate (xop0, mode0))
2456 xop0 = copy_to_mode_reg (mode0, xop0);
2458 if (!insn_data[icode].operand[2].predicate (xop1, mode1))
2459 xop1 = copy_to_mode_reg (mode1, xop1);
2461 /* We could handle this, but we should always be called with a pseudo
2462 for our targets and all insns should take them as outputs. */
2463 gcc_assert (insn_data[icode].operand[0].predicate (targ0, mode));
2464 gcc_assert (insn_data[icode].operand[3].predicate (targ1, mode));
2466 pat = GEN_FCN (icode) (targ0, xop0, xop1, targ1);
2467 if (pat)
2469 emit_insn (pat);
2470 return 1;
2472 else
2473 delete_insns_since (last);
2476 /* It can't be done in this mode. Can we do it in a wider mode? */
2478 if (CLASS_HAS_WIDER_MODES_P (mclass))
2480 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2481 wider_mode != VOIDmode;
2482 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2484 if (optab_handler (binoptab, wider_mode)->insn_code
2485 != CODE_FOR_nothing)
2487 rtx t0 = gen_reg_rtx (wider_mode);
2488 rtx t1 = gen_reg_rtx (wider_mode);
2489 rtx cop0 = convert_modes (wider_mode, mode, op0, unsignedp);
2490 rtx cop1 = convert_modes (wider_mode, mode, op1, unsignedp);
2492 if (expand_twoval_binop (binoptab, cop0, cop1,
2493 t0, t1, unsignedp))
2495 convert_move (targ0, t0, unsignedp);
2496 convert_move (targ1, t1, unsignedp);
2497 return 1;
2499 else
2500 delete_insns_since (last);
2505 delete_insns_since (entry_last);
2506 return 0;
2509 /* Expand the two-valued library call indicated by BINOPTAB, but
2510 preserve only one of the values. If TARG0 is non-NULL, the first
2511 value is placed into TARG0; otherwise the second value is placed
2512 into TARG1. Exactly one of TARG0 and TARG1 must be non-NULL. The
2513 value stored into TARG0 or TARG1 is equivalent to (CODE OP0 OP1).
2514 This routine assumes that the value returned by the library call is
2515 as if the return value was of an integral mode twice as wide as the
2516 mode of OP0. Returns 1 if the call was successful. */
2518 bool
2519 expand_twoval_binop_libfunc (optab binoptab, rtx op0, rtx op1,
2520 rtx targ0, rtx targ1, enum rtx_code code)
2522 enum machine_mode mode;
2523 enum machine_mode libval_mode;
2524 rtx libval;
2525 rtx insns;
2526 rtx libfunc;
2528 /* Exactly one of TARG0 or TARG1 should be non-NULL. */
2529 gcc_assert (!targ0 != !targ1);
2531 mode = GET_MODE (op0);
2532 libfunc = optab_libfunc (binoptab, mode);
2533 if (!libfunc)
2534 return false;
2536 /* The value returned by the library function will have twice as
2537 many bits as the nominal MODE. */
2538 libval_mode = smallest_mode_for_size (2 * GET_MODE_BITSIZE (mode),
2539 MODE_INT);
2540 start_sequence ();
2541 libval = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
2542 libval_mode, 2,
2543 op0, mode,
2544 op1, mode);
2545 /* Get the part of VAL containing the value that we want. */
2546 libval = simplify_gen_subreg (mode, libval, libval_mode,
2547 targ0 ? 0 : GET_MODE_SIZE (mode));
2548 insns = get_insns ();
2549 end_sequence ();
2550 /* Move the into the desired location. */
2551 emit_libcall_block (insns, targ0 ? targ0 : targ1, libval,
2552 gen_rtx_fmt_ee (code, mode, op0, op1));
2554 return true;
2558 /* Wrapper around expand_unop which takes an rtx code to specify
2559 the operation to perform, not an optab pointer. All other
2560 arguments are the same. */
2562 expand_simple_unop (enum machine_mode mode, enum rtx_code code, rtx op0,
2563 rtx target, int unsignedp)
2565 optab unop = code_to_optab[(int) code];
2566 gcc_assert (unop);
2568 return expand_unop (mode, unop, op0, target, unsignedp);
2571 /* Try calculating
2572 (clz:narrow x)
2574 (clz:wide (zero_extend:wide x)) - ((width wide) - (width narrow)). */
2575 static rtx
2576 widen_clz (enum machine_mode mode, rtx op0, rtx target)
2578 enum mode_class mclass = GET_MODE_CLASS (mode);
2579 if (CLASS_HAS_WIDER_MODES_P (mclass))
2581 enum machine_mode wider_mode;
2582 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2583 wider_mode != VOIDmode;
2584 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2586 if (optab_handler (clz_optab, wider_mode)->insn_code
2587 != CODE_FOR_nothing)
2589 rtx xop0, temp, last;
2591 last = get_last_insn ();
2593 if (target == 0)
2594 target = gen_reg_rtx (mode);
2595 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2596 temp = expand_unop (wider_mode, clz_optab, xop0, NULL_RTX, true);
2597 if (temp != 0)
2598 temp = expand_binop (wider_mode, sub_optab, temp,
2599 GEN_INT (GET_MODE_BITSIZE (wider_mode)
2600 - GET_MODE_BITSIZE (mode)),
2601 target, true, OPTAB_DIRECT);
2602 if (temp == 0)
2603 delete_insns_since (last);
2605 return temp;
2609 return 0;
2612 /* Try calculating clz of a double-word quantity as two clz's of word-sized
2613 quantities, choosing which based on whether the high word is nonzero. */
2614 static rtx
2615 expand_doubleword_clz (enum machine_mode mode, rtx op0, rtx target)
2617 rtx xop0 = force_reg (mode, op0);
2618 rtx subhi = gen_highpart (word_mode, xop0);
2619 rtx sublo = gen_lowpart (word_mode, xop0);
2620 rtx hi0_label = gen_label_rtx ();
2621 rtx after_label = gen_label_rtx ();
2622 rtx seq, temp, result;
2624 /* If we were not given a target, use a word_mode register, not a
2625 'mode' register. The result will fit, and nobody is expecting
2626 anything bigger (the return type of __builtin_clz* is int). */
2627 if (!target)
2628 target = gen_reg_rtx (word_mode);
2630 /* In any case, write to a word_mode scratch in both branches of the
2631 conditional, so we can ensure there is a single move insn setting
2632 'target' to tag a REG_EQUAL note on. */
2633 result = gen_reg_rtx (word_mode);
2635 start_sequence ();
2637 /* If the high word is not equal to zero,
2638 then clz of the full value is clz of the high word. */
2639 emit_cmp_and_jump_insns (subhi, CONST0_RTX (word_mode), EQ, 0,
2640 word_mode, true, hi0_label);
2642 temp = expand_unop_direct (word_mode, clz_optab, subhi, result, true);
2643 if (!temp)
2644 goto fail;
2646 if (temp != result)
2647 convert_move (result, temp, true);
2649 emit_jump_insn (gen_jump (after_label));
2650 emit_barrier ();
2652 /* Else clz of the full value is clz of the low word plus the number
2653 of bits in the high word. */
2654 emit_label (hi0_label);
2656 temp = expand_unop_direct (word_mode, clz_optab, sublo, 0, true);
2657 if (!temp)
2658 goto fail;
2659 temp = expand_binop (word_mode, add_optab, temp,
2660 GEN_INT (GET_MODE_BITSIZE (word_mode)),
2661 result, true, OPTAB_DIRECT);
2662 if (!temp)
2663 goto fail;
2664 if (temp != result)
2665 convert_move (result, temp, true);
2667 emit_label (after_label);
2668 convert_move (target, result, true);
2670 seq = get_insns ();
2671 end_sequence ();
2673 add_equal_note (seq, target, CLZ, xop0, 0);
2674 emit_insn (seq);
2675 return target;
2677 fail:
2678 end_sequence ();
2679 return 0;
2682 /* Try calculating
2683 (bswap:narrow x)
2685 (lshiftrt:wide (bswap:wide x) ((width wide) - (width narrow))). */
2686 static rtx
2687 widen_bswap (enum machine_mode mode, rtx op0, rtx target)
2689 enum mode_class mclass = GET_MODE_CLASS (mode);
2690 enum machine_mode wider_mode;
2691 rtx x, last;
2693 if (!CLASS_HAS_WIDER_MODES_P (mclass))
2694 return NULL_RTX;
2696 for (wider_mode = GET_MODE_WIDER_MODE (mode);
2697 wider_mode != VOIDmode;
2698 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2699 if (optab_handler (bswap_optab, wider_mode)->insn_code != CODE_FOR_nothing)
2700 goto found;
2701 return NULL_RTX;
2703 found:
2704 last = get_last_insn ();
2706 x = widen_operand (op0, wider_mode, mode, true, true);
2707 x = expand_unop (wider_mode, bswap_optab, x, NULL_RTX, true);
2709 if (x != 0)
2710 x = expand_shift (RSHIFT_EXPR, wider_mode, x,
2711 size_int (GET_MODE_BITSIZE (wider_mode)
2712 - GET_MODE_BITSIZE (mode)),
2713 NULL_RTX, true);
2715 if (x != 0)
2717 if (target == 0)
2718 target = gen_reg_rtx (mode);
2719 emit_move_insn (target, gen_lowpart (mode, x));
2721 else
2722 delete_insns_since (last);
2724 return target;
2727 /* Try calculating bswap as two bswaps of two word-sized operands. */
2729 static rtx
2730 expand_doubleword_bswap (enum machine_mode mode, rtx op, rtx target)
2732 rtx t0, t1;
2734 t1 = expand_unop (word_mode, bswap_optab,
2735 operand_subword_force (op, 0, mode), NULL_RTX, true);
2736 t0 = expand_unop (word_mode, bswap_optab,
2737 operand_subword_force (op, 1, mode), NULL_RTX, true);
2739 if (target == 0)
2740 target = gen_reg_rtx (mode);
2741 if (REG_P (target))
2742 emit_clobber (target);
2743 emit_move_insn (operand_subword (target, 0, 1, mode), t0);
2744 emit_move_insn (operand_subword (target, 1, 1, mode), t1);
2746 return target;
2749 /* Try calculating (parity x) as (and (popcount x) 1), where
2750 popcount can also be done in a wider mode. */
2751 static rtx
2752 expand_parity (enum machine_mode mode, rtx op0, rtx target)
2754 enum mode_class mclass = GET_MODE_CLASS (mode);
2755 if (CLASS_HAS_WIDER_MODES_P (mclass))
2757 enum machine_mode wider_mode;
2758 for (wider_mode = mode; wider_mode != VOIDmode;
2759 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
2761 if (optab_handler (popcount_optab, wider_mode)->insn_code
2762 != CODE_FOR_nothing)
2764 rtx xop0, temp, last;
2766 last = get_last_insn ();
2768 if (target == 0)
2769 target = gen_reg_rtx (mode);
2770 xop0 = widen_operand (op0, wider_mode, mode, true, false);
2771 temp = expand_unop (wider_mode, popcount_optab, xop0, NULL_RTX,
2772 true);
2773 if (temp != 0)
2774 temp = expand_binop (wider_mode, and_optab, temp, const1_rtx,
2775 target, true, OPTAB_DIRECT);
2776 if (temp == 0)
2777 delete_insns_since (last);
2779 return temp;
2783 return 0;
2786 /* Try calculating ctz(x) as K - clz(x & -x) ,
2787 where K is GET_MODE_BITSIZE(mode) - 1.
2789 Both __builtin_ctz and __builtin_clz are undefined at zero, so we
2790 don't have to worry about what the hardware does in that case. (If
2791 the clz instruction produces the usual value at 0, which is K, the
2792 result of this code sequence will be -1; expand_ffs, below, relies
2793 on this. It might be nice to have it be K instead, for consistency
2794 with the (very few) processors that provide a ctz with a defined
2795 value, but that would take one more instruction, and it would be
2796 less convenient for expand_ffs anyway. */
2798 static rtx
2799 expand_ctz (enum machine_mode mode, rtx op0, rtx target)
2801 rtx seq, temp;
2803 if (optab_handler (clz_optab, mode)->insn_code == CODE_FOR_nothing)
2804 return 0;
2806 start_sequence ();
2808 temp = expand_unop_direct (mode, neg_optab, op0, NULL_RTX, true);
2809 if (temp)
2810 temp = expand_binop (mode, and_optab, op0, temp, NULL_RTX,
2811 true, OPTAB_DIRECT);
2812 if (temp)
2813 temp = expand_unop_direct (mode, clz_optab, temp, NULL_RTX, true);
2814 if (temp)
2815 temp = expand_binop (mode, sub_optab, GEN_INT (GET_MODE_BITSIZE (mode) - 1),
2816 temp, target,
2817 true, OPTAB_DIRECT);
2818 if (temp == 0)
2820 end_sequence ();
2821 return 0;
2824 seq = get_insns ();
2825 end_sequence ();
2827 add_equal_note (seq, temp, CTZ, op0, 0);
2828 emit_insn (seq);
2829 return temp;
2833 /* Try calculating ffs(x) using ctz(x) if we have that instruction, or
2834 else with the sequence used by expand_clz.
2836 The ffs builtin promises to return zero for a zero value and ctz/clz
2837 may have an undefined value in that case. If they do not give us a
2838 convenient value, we have to generate a test and branch. */
2839 static rtx
2840 expand_ffs (enum machine_mode mode, rtx op0, rtx target)
2842 HOST_WIDE_INT val = 0;
2843 bool defined_at_zero = false;
2844 rtx temp, seq;
2846 if (optab_handler (ctz_optab, mode)->insn_code != CODE_FOR_nothing)
2848 start_sequence ();
2850 temp = expand_unop_direct (mode, ctz_optab, op0, 0, true);
2851 if (!temp)
2852 goto fail;
2854 defined_at_zero = (CTZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2);
2856 else if (optab_handler (clz_optab, mode)->insn_code != CODE_FOR_nothing)
2858 start_sequence ();
2859 temp = expand_ctz (mode, op0, 0);
2860 if (!temp)
2861 goto fail;
2863 if (CLZ_DEFINED_VALUE_AT_ZERO (mode, val) == 2)
2865 defined_at_zero = true;
2866 val = (GET_MODE_BITSIZE (mode) - 1) - val;
2869 else
2870 return 0;
2872 if (defined_at_zero && val == -1)
2873 /* No correction needed at zero. */;
2874 else
2876 /* We don't try to do anything clever with the situation found
2877 on some processors (eg Alpha) where ctz(0:mode) ==
2878 bitsize(mode). If someone can think of a way to send N to -1
2879 and leave alone all values in the range 0..N-1 (where N is a
2880 power of two), cheaper than this test-and-branch, please add it.
2882 The test-and-branch is done after the operation itself, in case
2883 the operation sets condition codes that can be recycled for this.
2884 (This is true on i386, for instance.) */
2886 rtx nonzero_label = gen_label_rtx ();
2887 emit_cmp_and_jump_insns (op0, CONST0_RTX (mode), NE, 0,
2888 mode, true, nonzero_label);
2890 convert_move (temp, GEN_INT (-1), false);
2891 emit_label (nonzero_label);
2894 /* temp now has a value in the range -1..bitsize-1. ffs is supposed
2895 to produce a value in the range 0..bitsize. */
2896 temp = expand_binop (mode, add_optab, temp, GEN_INT (1),
2897 target, false, OPTAB_DIRECT);
2898 if (!temp)
2899 goto fail;
2901 seq = get_insns ();
2902 end_sequence ();
2904 add_equal_note (seq, temp, FFS, op0, 0);
2905 emit_insn (seq);
2906 return temp;
2908 fail:
2909 end_sequence ();
2910 return 0;
2913 /* Extract the OMODE lowpart from VAL, which has IMODE. Under certain
2914 conditions, VAL may already be a SUBREG against which we cannot generate
2915 a further SUBREG. In this case, we expect forcing the value into a
2916 register will work around the situation. */
2918 static rtx
2919 lowpart_subreg_maybe_copy (enum machine_mode omode, rtx val,
2920 enum machine_mode imode)
2922 rtx ret;
2923 ret = lowpart_subreg (omode, val, imode);
2924 if (ret == NULL)
2926 val = force_reg (imode, val);
2927 ret = lowpart_subreg (omode, val, imode);
2928 gcc_assert (ret != NULL);
2930 return ret;
2933 /* Expand a floating point absolute value or negation operation via a
2934 logical operation on the sign bit. */
2936 static rtx
2937 expand_absneg_bit (enum rtx_code code, enum machine_mode mode,
2938 rtx op0, rtx target)
2940 const struct real_format *fmt;
2941 int bitpos, word, nwords, i;
2942 enum machine_mode imode;
2943 HOST_WIDE_INT hi, lo;
2944 rtx temp, insns;
2946 /* The format has to have a simple sign bit. */
2947 fmt = REAL_MODE_FORMAT (mode);
2948 if (fmt == NULL)
2949 return NULL_RTX;
2951 bitpos = fmt->signbit_rw;
2952 if (bitpos < 0)
2953 return NULL_RTX;
2955 /* Don't create negative zeros if the format doesn't support them. */
2956 if (code == NEG && !fmt->has_signed_zero)
2957 return NULL_RTX;
2959 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
2961 imode = int_mode_for_mode (mode);
2962 if (imode == BLKmode)
2963 return NULL_RTX;
2964 word = 0;
2965 nwords = 1;
2967 else
2969 imode = word_mode;
2971 if (FLOAT_WORDS_BIG_ENDIAN)
2972 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
2973 else
2974 word = bitpos / BITS_PER_WORD;
2975 bitpos = bitpos % BITS_PER_WORD;
2976 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
2979 if (bitpos < HOST_BITS_PER_WIDE_INT)
2981 hi = 0;
2982 lo = (HOST_WIDE_INT) 1 << bitpos;
2984 else
2986 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
2987 lo = 0;
2989 if (code == ABS)
2990 lo = ~lo, hi = ~hi;
2992 if (target == 0 || target == op0)
2993 target = gen_reg_rtx (mode);
2995 if (nwords > 1)
2997 start_sequence ();
2999 for (i = 0; i < nwords; ++i)
3001 rtx targ_piece = operand_subword (target, i, 1, mode);
3002 rtx op0_piece = operand_subword_force (op0, i, mode);
3004 if (i == word)
3006 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
3007 op0_piece,
3008 immed_double_const (lo, hi, imode),
3009 targ_piece, 1, OPTAB_LIB_WIDEN);
3010 if (temp != targ_piece)
3011 emit_move_insn (targ_piece, temp);
3013 else
3014 emit_move_insn (targ_piece, op0_piece);
3017 insns = get_insns ();
3018 end_sequence ();
3020 emit_insn (insns);
3022 else
3024 temp = expand_binop (imode, code == ABS ? and_optab : xor_optab,
3025 gen_lowpart (imode, op0),
3026 immed_double_const (lo, hi, imode),
3027 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3028 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3030 set_unique_reg_note (get_last_insn (), REG_EQUAL,
3031 gen_rtx_fmt_e (code, mode, copy_rtx (op0)));
3034 return target;
3037 /* As expand_unop, but will fail rather than attempt the operation in a
3038 different mode or with a libcall. */
3039 static rtx
3040 expand_unop_direct (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
3041 int unsignedp)
3043 if (optab_handler (unoptab, mode)->insn_code != CODE_FOR_nothing)
3045 int icode = (int) optab_handler (unoptab, mode)->insn_code;
3046 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3047 rtx xop0 = op0;
3048 rtx last = get_last_insn ();
3049 rtx pat, temp;
3051 if (target)
3052 temp = target;
3053 else
3054 temp = gen_reg_rtx (mode);
3056 if (GET_MODE (xop0) != VOIDmode
3057 && GET_MODE (xop0) != mode0)
3058 xop0 = convert_to_mode (mode0, xop0, unsignedp);
3060 /* Now, if insn doesn't accept our operand, put it into a pseudo. */
3062 if (!insn_data[icode].operand[1].predicate (xop0, mode0))
3063 xop0 = copy_to_mode_reg (mode0, xop0);
3065 if (!insn_data[icode].operand[0].predicate (temp, mode))
3066 temp = gen_reg_rtx (mode);
3068 pat = GEN_FCN (icode) (temp, xop0);
3069 if (pat)
3071 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX
3072 && ! add_equal_note (pat, temp, unoptab->code, xop0, NULL_RTX))
3074 delete_insns_since (last);
3075 return expand_unop (mode, unoptab, op0, NULL_RTX, unsignedp);
3078 emit_insn (pat);
3080 return temp;
3082 else
3083 delete_insns_since (last);
3085 return 0;
3088 /* Generate code to perform an operation specified by UNOPTAB
3089 on operand OP0, with result having machine-mode MODE.
3091 UNSIGNEDP is for the case where we have to widen the operands
3092 to perform the operation. It says to use zero-extension.
3094 If TARGET is nonzero, the value
3095 is generated there, if it is convenient to do so.
3096 In all cases an rtx is returned for the locus of the value;
3097 this may or may not be TARGET. */
3100 expand_unop (enum machine_mode mode, optab unoptab, rtx op0, rtx target,
3101 int unsignedp)
3103 enum mode_class mclass = GET_MODE_CLASS (mode);
3104 enum machine_mode wider_mode;
3105 rtx temp;
3106 rtx libfunc;
3108 temp = expand_unop_direct (mode, unoptab, op0, target, unsignedp);
3109 if (temp)
3110 return temp;
3112 /* It can't be done in this mode. Can we open-code it in a wider mode? */
3114 /* Widening (or narrowing) clz needs special treatment. */
3115 if (unoptab == clz_optab)
3117 temp = widen_clz (mode, op0, target);
3118 if (temp)
3119 return temp;
3121 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3122 && optab_handler (unoptab, word_mode)->insn_code != CODE_FOR_nothing)
3124 temp = expand_doubleword_clz (mode, op0, target);
3125 if (temp)
3126 return temp;
3129 goto try_libcall;
3132 /* Widening (or narrowing) bswap needs special treatment. */
3133 if (unoptab == bswap_optab)
3135 temp = widen_bswap (mode, op0, target);
3136 if (temp)
3137 return temp;
3139 if (GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
3140 && optab_handler (unoptab, word_mode)->insn_code != CODE_FOR_nothing)
3142 temp = expand_doubleword_bswap (mode, op0, target);
3143 if (temp)
3144 return temp;
3147 goto try_libcall;
3150 if (CLASS_HAS_WIDER_MODES_P (mclass))
3151 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3152 wider_mode != VOIDmode;
3153 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3155 if (optab_handler (unoptab, wider_mode)->insn_code != CODE_FOR_nothing)
3157 rtx xop0 = op0;
3158 rtx last = get_last_insn ();
3160 /* For certain operations, we need not actually extend
3161 the narrow operand, as long as we will truncate the
3162 results to the same narrowness. */
3164 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3165 (unoptab == neg_optab
3166 || unoptab == one_cmpl_optab)
3167 && mclass == MODE_INT);
3169 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3170 unsignedp);
3172 if (temp)
3174 if (mclass != MODE_INT
3175 || !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (mode),
3176 GET_MODE_BITSIZE (wider_mode)))
3178 if (target == 0)
3179 target = gen_reg_rtx (mode);
3180 convert_move (target, temp, 0);
3181 return target;
3183 else
3184 return gen_lowpart (mode, temp);
3186 else
3187 delete_insns_since (last);
3191 /* These can be done a word at a time. */
3192 if (unoptab == one_cmpl_optab
3193 && mclass == MODE_INT
3194 && GET_MODE_SIZE (mode) > UNITS_PER_WORD
3195 && optab_handler (unoptab, word_mode)->insn_code != CODE_FOR_nothing)
3197 int i;
3198 rtx insns;
3200 if (target == 0 || target == op0)
3201 target = gen_reg_rtx (mode);
3203 start_sequence ();
3205 /* Do the actual arithmetic. */
3206 for (i = 0; i < GET_MODE_BITSIZE (mode) / BITS_PER_WORD; i++)
3208 rtx target_piece = operand_subword (target, i, 1, mode);
3209 rtx x = expand_unop (word_mode, unoptab,
3210 operand_subword_force (op0, i, mode),
3211 target_piece, unsignedp);
3213 if (target_piece != x)
3214 emit_move_insn (target_piece, x);
3217 insns = get_insns ();
3218 end_sequence ();
3220 emit_insn (insns);
3221 return target;
3224 if (unoptab->code == NEG)
3226 /* Try negating floating point values by flipping the sign bit. */
3227 if (SCALAR_FLOAT_MODE_P (mode))
3229 temp = expand_absneg_bit (NEG, mode, op0, target);
3230 if (temp)
3231 return temp;
3234 /* If there is no negation pattern, and we have no negative zero,
3235 try subtracting from zero. */
3236 if (!HONOR_SIGNED_ZEROS (mode))
3238 temp = expand_binop (mode, (unoptab == negv_optab
3239 ? subv_optab : sub_optab),
3240 CONST0_RTX (mode), op0, target,
3241 unsignedp, OPTAB_DIRECT);
3242 if (temp)
3243 return temp;
3247 /* Try calculating parity (x) as popcount (x) % 2. */
3248 if (unoptab == parity_optab)
3250 temp = expand_parity (mode, op0, target);
3251 if (temp)
3252 return temp;
3255 /* Try implementing ffs (x) in terms of clz (x). */
3256 if (unoptab == ffs_optab)
3258 temp = expand_ffs (mode, op0, target);
3259 if (temp)
3260 return temp;
3263 /* Try implementing ctz (x) in terms of clz (x). */
3264 if (unoptab == ctz_optab)
3266 temp = expand_ctz (mode, op0, target);
3267 if (temp)
3268 return temp;
3271 try_libcall:
3272 /* Now try a library call in this mode. */
3273 libfunc = optab_libfunc (unoptab, mode);
3274 if (libfunc)
3276 rtx insns;
3277 rtx value;
3278 rtx eq_value;
3279 enum machine_mode outmode = mode;
3281 /* All of these functions return small values. Thus we choose to
3282 have them return something that isn't a double-word. */
3283 if (unoptab == ffs_optab || unoptab == clz_optab || unoptab == ctz_optab
3284 || unoptab == popcount_optab || unoptab == parity_optab)
3285 outmode
3286 = GET_MODE (hard_libcall_value (TYPE_MODE (integer_type_node)));
3288 start_sequence ();
3290 /* Pass 1 for NO_QUEUE so we don't lose any increments
3291 if the libcall is cse'd or moved. */
3292 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, outmode,
3293 1, op0, mode);
3294 insns = get_insns ();
3295 end_sequence ();
3297 target = gen_reg_rtx (outmode);
3298 eq_value = gen_rtx_fmt_e (unoptab->code, mode, op0);
3299 if (GET_MODE_SIZE (outmode) < GET_MODE_SIZE (mode))
3300 eq_value = simplify_gen_unary (TRUNCATE, outmode, eq_value, mode);
3301 else if (GET_MODE_SIZE (outmode) > GET_MODE_SIZE (mode))
3302 eq_value = simplify_gen_unary (ZERO_EXTEND, outmode, eq_value, mode);
3303 emit_libcall_block (insns, target, value, eq_value);
3305 return target;
3308 /* It can't be done in this mode. Can we do it in a wider mode? */
3310 if (CLASS_HAS_WIDER_MODES_P (mclass))
3312 for (wider_mode = GET_MODE_WIDER_MODE (mode);
3313 wider_mode != VOIDmode;
3314 wider_mode = GET_MODE_WIDER_MODE (wider_mode))
3316 if ((optab_handler (unoptab, wider_mode)->insn_code
3317 != CODE_FOR_nothing)
3318 || optab_libfunc (unoptab, wider_mode))
3320 rtx xop0 = op0;
3321 rtx last = get_last_insn ();
3323 /* For certain operations, we need not actually extend
3324 the narrow operand, as long as we will truncate the
3325 results to the same narrowness. */
3327 xop0 = widen_operand (xop0, wider_mode, mode, unsignedp,
3328 (unoptab == neg_optab
3329 || unoptab == one_cmpl_optab)
3330 && mclass == MODE_INT);
3332 temp = expand_unop (wider_mode, unoptab, xop0, NULL_RTX,
3333 unsignedp);
3335 /* If we are generating clz using wider mode, adjust the
3336 result. */
3337 if (unoptab == clz_optab && temp != 0)
3338 temp = expand_binop (wider_mode, sub_optab, temp,
3339 GEN_INT (GET_MODE_BITSIZE (wider_mode)
3340 - GET_MODE_BITSIZE (mode)),
3341 target, true, OPTAB_DIRECT);
3343 if (temp)
3345 if (mclass != MODE_INT)
3347 if (target == 0)
3348 target = gen_reg_rtx (mode);
3349 convert_move (target, temp, 0);
3350 return target;
3352 else
3353 return gen_lowpart (mode, temp);
3355 else
3356 delete_insns_since (last);
3361 /* One final attempt at implementing negation via subtraction,
3362 this time allowing widening of the operand. */
3363 if (unoptab->code == NEG && !HONOR_SIGNED_ZEROS (mode))
3365 rtx temp;
3366 temp = expand_binop (mode,
3367 unoptab == negv_optab ? subv_optab : sub_optab,
3368 CONST0_RTX (mode), op0,
3369 target, unsignedp, OPTAB_LIB_WIDEN);
3370 if (temp)
3371 return temp;
3374 return 0;
3377 /* Emit code to compute the absolute value of OP0, with result to
3378 TARGET if convenient. (TARGET may be 0.) The return value says
3379 where the result actually is to be found.
3381 MODE is the mode of the operand; the mode of the result is
3382 different but can be deduced from MODE.
3387 expand_abs_nojump (enum machine_mode mode, rtx op0, rtx target,
3388 int result_unsignedp)
3390 rtx temp;
3392 if (! flag_trapv)
3393 result_unsignedp = 1;
3395 /* First try to do it with a special abs instruction. */
3396 temp = expand_unop (mode, result_unsignedp ? abs_optab : absv_optab,
3397 op0, target, 0);
3398 if (temp != 0)
3399 return temp;
3401 /* For floating point modes, try clearing the sign bit. */
3402 if (SCALAR_FLOAT_MODE_P (mode))
3404 temp = expand_absneg_bit (ABS, mode, op0, target);
3405 if (temp)
3406 return temp;
3409 /* If we have a MAX insn, we can do this as MAX (x, -x). */
3410 if (optab_handler (smax_optab, mode)->insn_code != CODE_FOR_nothing
3411 && !HONOR_SIGNED_ZEROS (mode))
3413 rtx last = get_last_insn ();
3415 temp = expand_unop (mode, neg_optab, op0, NULL_RTX, 0);
3416 if (temp != 0)
3417 temp = expand_binop (mode, smax_optab, op0, temp, target, 0,
3418 OPTAB_WIDEN);
3420 if (temp != 0)
3421 return temp;
3423 delete_insns_since (last);
3426 /* If this machine has expensive jumps, we can do integer absolute
3427 value of X as (((signed) x >> (W-1)) ^ x) - ((signed) x >> (W-1)),
3428 where W is the width of MODE. */
3430 if (GET_MODE_CLASS (mode) == MODE_INT
3431 && BRANCH_COST (optimize_insn_for_speed_p (),
3432 false) >= 2)
3434 rtx extended = expand_shift (RSHIFT_EXPR, mode, op0,
3435 size_int (GET_MODE_BITSIZE (mode) - 1),
3436 NULL_RTX, 0);
3438 temp = expand_binop (mode, xor_optab, extended, op0, target, 0,
3439 OPTAB_LIB_WIDEN);
3440 if (temp != 0)
3441 temp = expand_binop (mode, result_unsignedp ? sub_optab : subv_optab,
3442 temp, extended, target, 0, OPTAB_LIB_WIDEN);
3444 if (temp != 0)
3445 return temp;
3448 return NULL_RTX;
3452 expand_abs (enum machine_mode mode, rtx op0, rtx target,
3453 int result_unsignedp, int safe)
3455 rtx temp, op1;
3457 if (! flag_trapv)
3458 result_unsignedp = 1;
3460 temp = expand_abs_nojump (mode, op0, target, result_unsignedp);
3461 if (temp != 0)
3462 return temp;
3464 /* If that does not win, use conditional jump and negate. */
3466 /* It is safe to use the target if it is the same
3467 as the source if this is also a pseudo register */
3468 if (op0 == target && REG_P (op0)
3469 && REGNO (op0) >= FIRST_PSEUDO_REGISTER)
3470 safe = 1;
3472 op1 = gen_label_rtx ();
3473 if (target == 0 || ! safe
3474 || GET_MODE (target) != mode
3475 || (MEM_P (target) && MEM_VOLATILE_P (target))
3476 || (REG_P (target)
3477 && REGNO (target) < FIRST_PSEUDO_REGISTER))
3478 target = gen_reg_rtx (mode);
3480 emit_move_insn (target, op0);
3481 NO_DEFER_POP;
3483 do_compare_rtx_and_jump (target, CONST0_RTX (mode), GE, 0, mode,
3484 NULL_RTX, NULL_RTX, op1);
3486 op0 = expand_unop (mode, result_unsignedp ? neg_optab : negv_optab,
3487 target, target, 0);
3488 if (op0 != target)
3489 emit_move_insn (target, op0);
3490 emit_label (op1);
3491 OK_DEFER_POP;
3492 return target;
3495 /* A subroutine of expand_copysign, perform the copysign operation using the
3496 abs and neg primitives advertised to exist on the target. The assumption
3497 is that we have a split register file, and leaving op0 in fp registers,
3498 and not playing with subregs so much, will help the register allocator. */
3500 static rtx
3501 expand_copysign_absneg (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3502 int bitpos, bool op0_is_abs)
3504 enum machine_mode imode;
3505 int icode;
3506 rtx sign, label;
3508 if (target == op1)
3509 target = NULL_RTX;
3511 /* Check if the back end provides an insn that handles signbit for the
3512 argument's mode. */
3513 icode = (int) signbit_optab->handlers [(int) mode].insn_code;
3514 if (icode != CODE_FOR_nothing)
3516 imode = insn_data[icode].operand[0].mode;
3517 sign = gen_reg_rtx (imode);
3518 emit_unop_insn (icode, sign, op1, UNKNOWN);
3520 else
3522 HOST_WIDE_INT hi, lo;
3524 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3526 imode = int_mode_for_mode (mode);
3527 if (imode == BLKmode)
3528 return NULL_RTX;
3529 op1 = gen_lowpart (imode, op1);
3531 else
3533 int word;
3535 imode = word_mode;
3536 if (FLOAT_WORDS_BIG_ENDIAN)
3537 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3538 else
3539 word = bitpos / BITS_PER_WORD;
3540 bitpos = bitpos % BITS_PER_WORD;
3541 op1 = operand_subword_force (op1, word, mode);
3544 if (bitpos < HOST_BITS_PER_WIDE_INT)
3546 hi = 0;
3547 lo = (HOST_WIDE_INT) 1 << bitpos;
3549 else
3551 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
3552 lo = 0;
3555 sign = gen_reg_rtx (imode);
3556 sign = expand_binop (imode, and_optab, op1,
3557 immed_double_const (lo, hi, imode),
3558 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3561 if (!op0_is_abs)
3563 op0 = expand_unop (mode, abs_optab, op0, target, 0);
3564 if (op0 == NULL)
3565 return NULL_RTX;
3566 target = op0;
3568 else
3570 if (target == NULL_RTX)
3571 target = copy_to_reg (op0);
3572 else
3573 emit_move_insn (target, op0);
3576 label = gen_label_rtx ();
3577 emit_cmp_and_jump_insns (sign, const0_rtx, EQ, NULL_RTX, imode, 1, label);
3579 if (GET_CODE (op0) == CONST_DOUBLE)
3580 op0 = simplify_unary_operation (NEG, mode, op0, mode);
3581 else
3582 op0 = expand_unop (mode, neg_optab, op0, target, 0);
3583 if (op0 != target)
3584 emit_move_insn (target, op0);
3586 emit_label (label);
3588 return target;
3592 /* A subroutine of expand_copysign, perform the entire copysign operation
3593 with integer bitmasks. BITPOS is the position of the sign bit; OP0_IS_ABS
3594 is true if op0 is known to have its sign bit clear. */
3596 static rtx
3597 expand_copysign_bit (enum machine_mode mode, rtx op0, rtx op1, rtx target,
3598 int bitpos, bool op0_is_abs)
3600 enum machine_mode imode;
3601 HOST_WIDE_INT hi, lo;
3602 int word, nwords, i;
3603 rtx temp, insns;
3605 if (GET_MODE_SIZE (mode) <= UNITS_PER_WORD)
3607 imode = int_mode_for_mode (mode);
3608 if (imode == BLKmode)
3609 return NULL_RTX;
3610 word = 0;
3611 nwords = 1;
3613 else
3615 imode = word_mode;
3617 if (FLOAT_WORDS_BIG_ENDIAN)
3618 word = (GET_MODE_BITSIZE (mode) - bitpos) / BITS_PER_WORD;
3619 else
3620 word = bitpos / BITS_PER_WORD;
3621 bitpos = bitpos % BITS_PER_WORD;
3622 nwords = (GET_MODE_BITSIZE (mode) + BITS_PER_WORD - 1) / BITS_PER_WORD;
3625 if (bitpos < HOST_BITS_PER_WIDE_INT)
3627 hi = 0;
3628 lo = (HOST_WIDE_INT) 1 << bitpos;
3630 else
3632 hi = (HOST_WIDE_INT) 1 << (bitpos - HOST_BITS_PER_WIDE_INT);
3633 lo = 0;
3636 if (target == 0 || target == op0 || target == op1)
3637 target = gen_reg_rtx (mode);
3639 if (nwords > 1)
3641 start_sequence ();
3643 for (i = 0; i < nwords; ++i)
3645 rtx targ_piece = operand_subword (target, i, 1, mode);
3646 rtx op0_piece = operand_subword_force (op0, i, mode);
3648 if (i == word)
3650 if (!op0_is_abs)
3651 op0_piece = expand_binop (imode, and_optab, op0_piece,
3652 immed_double_const (~lo, ~hi, imode),
3653 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3655 op1 = expand_binop (imode, and_optab,
3656 operand_subword_force (op1, i, mode),
3657 immed_double_const (lo, hi, imode),
3658 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3660 temp = expand_binop (imode, ior_optab, op0_piece, op1,
3661 targ_piece, 1, OPTAB_LIB_WIDEN);
3662 if (temp != targ_piece)
3663 emit_move_insn (targ_piece, temp);
3665 else
3666 emit_move_insn (targ_piece, op0_piece);
3669 insns = get_insns ();
3670 end_sequence ();
3672 emit_insn (insns);
3674 else
3676 op1 = expand_binop (imode, and_optab, gen_lowpart (imode, op1),
3677 immed_double_const (lo, hi, imode),
3678 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3680 op0 = gen_lowpart (imode, op0);
3681 if (!op0_is_abs)
3682 op0 = expand_binop (imode, and_optab, op0,
3683 immed_double_const (~lo, ~hi, imode),
3684 NULL_RTX, 1, OPTAB_LIB_WIDEN);
3686 temp = expand_binop (imode, ior_optab, op0, op1,
3687 gen_lowpart (imode, target), 1, OPTAB_LIB_WIDEN);
3688 target = lowpart_subreg_maybe_copy (mode, temp, imode);
3691 return target;
3694 /* Expand the C99 copysign operation. OP0 and OP1 must be the same
3695 scalar floating point mode. Return NULL if we do not know how to
3696 expand the operation inline. */
3699 expand_copysign (rtx op0, rtx op1, rtx target)
3701 enum machine_mode mode = GET_MODE (op0);
3702 const struct real_format *fmt;
3703 bool op0_is_abs;
3704 rtx temp;
3706 gcc_assert (SCALAR_FLOAT_MODE_P (mode));
3707 gcc_assert (GET_MODE (op1) == mode);
3709 /* First try to do it with a special instruction. */
3710 temp = expand_binop (mode, copysign_optab, op0, op1,
3711 target, 0, OPTAB_DIRECT);
3712 if (temp)
3713 return temp;
3715 fmt = REAL_MODE_FORMAT (mode);
3716 if (fmt == NULL || !fmt->has_signed_zero)
3717 return NULL_RTX;
3719 op0_is_abs = false;
3720 if (GET_CODE (op0) == CONST_DOUBLE)
3722 if (real_isneg (CONST_DOUBLE_REAL_VALUE (op0)))
3723 op0 = simplify_unary_operation (ABS, mode, op0, mode);
3724 op0_is_abs = true;
3727 if (fmt->signbit_ro >= 0
3728 && (GET_CODE (op0) == CONST_DOUBLE
3729 || (optab_handler (neg_optab, mode)->insn_code != CODE_FOR_nothing
3730 && optab_handler (abs_optab, mode)->insn_code != CODE_FOR_nothing)))
3732 temp = expand_copysign_absneg (mode, op0, op1, target,
3733 fmt->signbit_ro, op0_is_abs);
3734 if (temp)
3735 return temp;
3738 if (fmt->signbit_rw < 0)
3739 return NULL_RTX;
3740 return expand_copysign_bit (mode, op0, op1, target,
3741 fmt->signbit_rw, op0_is_abs);
3744 /* Generate an instruction whose insn-code is INSN_CODE,
3745 with two operands: an output TARGET and an input OP0.
3746 TARGET *must* be nonzero, and the output is always stored there.
3747 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3748 the value that is stored into TARGET.
3750 Return false if expansion failed. */
3752 bool
3753 maybe_emit_unop_insn (int icode, rtx target, rtx op0, enum rtx_code code)
3755 rtx temp;
3756 enum machine_mode mode0 = insn_data[icode].operand[1].mode;
3757 rtx pat;
3758 rtx last = get_last_insn ();
3760 temp = target;
3762 /* Now, if insn does not accept our operands, put them into pseudos. */
3764 if (!insn_data[icode].operand[1].predicate (op0, mode0))
3765 op0 = copy_to_mode_reg (mode0, op0);
3767 if (!insn_data[icode].operand[0].predicate (temp, GET_MODE (temp)))
3768 temp = gen_reg_rtx (GET_MODE (temp));
3770 pat = GEN_FCN (icode) (temp, op0);
3771 if (!pat)
3773 delete_insns_since (last);
3774 return false;
3777 if (INSN_P (pat) && NEXT_INSN (pat) != NULL_RTX && code != UNKNOWN)
3778 add_equal_note (pat, temp, code, op0, NULL_RTX);
3780 emit_insn (pat);
3782 if (temp != target)
3783 emit_move_insn (target, temp);
3784 return true;
3786 /* Generate an instruction whose insn-code is INSN_CODE,
3787 with two operands: an output TARGET and an input OP0.
3788 TARGET *must* be nonzero, and the output is always stored there.
3789 CODE is an rtx code such that (CODE OP0) is an rtx that describes
3790 the value that is stored into TARGET. */
3792 void
3793 emit_unop_insn (int icode, rtx target, rtx op0, enum rtx_code code)
3795 bool ok = maybe_emit_unop_insn (icode, target, op0, code);
3796 gcc_assert (ok);
3799 struct no_conflict_data
3801 rtx target, first, insn;
3802 bool must_stay;
3805 /* Called via note_stores by emit_libcall_block. Set P->must_stay if
3806 the currently examined clobber / store has to stay in the list of
3807 insns that constitute the actual libcall block. */
3808 static void
3809 no_conflict_move_test (rtx dest, const_rtx set, void *p0)
3811 struct no_conflict_data *p= (struct no_conflict_data *) p0;
3813 /* If this inns directly contributes to setting the target, it must stay. */
3814 if (reg_overlap_mentioned_p (p->target, dest))
3815 p->must_stay = true;
3816 /* If we haven't committed to keeping any other insns in the list yet,
3817 there is nothing more to check. */
3818 else if (p->insn == p->first)
3819 return;
3820 /* If this insn sets / clobbers a register that feeds one of the insns
3821 already in the list, this insn has to stay too. */
3822 else if (reg_overlap_mentioned_p (dest, PATTERN (p->first))
3823 || (CALL_P (p->first) && (find_reg_fusage (p->first, USE, dest)))
3824 || reg_used_between_p (dest, p->first, p->insn)
3825 /* Likewise if this insn depends on a register set by a previous
3826 insn in the list, or if it sets a result (presumably a hard
3827 register) that is set or clobbered by a previous insn.
3828 N.B. the modified_*_p (SET_DEST...) tests applied to a MEM
3829 SET_DEST perform the former check on the address, and the latter
3830 check on the MEM. */
3831 || (GET_CODE (set) == SET
3832 && (modified_in_p (SET_SRC (set), p->first)
3833 || modified_in_p (SET_DEST (set), p->first)
3834 || modified_between_p (SET_SRC (set), p->first, p->insn)
3835 || modified_between_p (SET_DEST (set), p->first, p->insn))))
3836 p->must_stay = true;
3840 /* Emit code to make a call to a constant function or a library call.
3842 INSNS is a list containing all insns emitted in the call.
3843 These insns leave the result in RESULT. Our block is to copy RESULT
3844 to TARGET, which is logically equivalent to EQUIV.
3846 We first emit any insns that set a pseudo on the assumption that these are
3847 loading constants into registers; doing so allows them to be safely cse'ed
3848 between blocks. Then we emit all the other insns in the block, followed by
3849 an insn to move RESULT to TARGET. This last insn will have a REQ_EQUAL
3850 note with an operand of EQUIV. */
3852 void
3853 emit_libcall_block (rtx insns, rtx target, rtx result, rtx equiv)
3855 rtx final_dest = target;
3856 rtx prev, next, last, insn;
3858 /* If this is a reg with REG_USERVAR_P set, then it could possibly turn
3859 into a MEM later. Protect the libcall block from this change. */
3860 if (! REG_P (target) || REG_USERVAR_P (target))
3861 target = gen_reg_rtx (GET_MODE (target));
3863 /* If we're using non-call exceptions, a libcall corresponding to an
3864 operation that may trap may also trap. */
3865 if (flag_non_call_exceptions && may_trap_p (equiv))
3867 for (insn = insns; insn; insn = NEXT_INSN (insn))
3868 if (CALL_P (insn))
3870 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3872 if (note != 0 && INTVAL (XEXP (note, 0)) <= 0)
3873 remove_note (insn, note);
3876 else
3877 /* look for any CALL_INSNs in this sequence, and attach a REG_EH_REGION
3878 reg note to indicate that this call cannot throw or execute a nonlocal
3879 goto (unless there is already a REG_EH_REGION note, in which case
3880 we update it). */
3881 for (insn = insns; insn; insn = NEXT_INSN (insn))
3882 if (CALL_P (insn))
3884 rtx note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
3886 if (note != 0)
3887 XEXP (note, 0) = constm1_rtx;
3888 else
3889 add_reg_note (insn, REG_EH_REGION, constm1_rtx);
3892 /* First emit all insns that set pseudos. Remove them from the list as
3893 we go. Avoid insns that set pseudos which were referenced in previous
3894 insns. These can be generated by move_by_pieces, for example,
3895 to update an address. Similarly, avoid insns that reference things
3896 set in previous insns. */
3898 for (insn = insns; insn; insn = next)
3900 rtx set = single_set (insn);
3902 next = NEXT_INSN (insn);
3904 if (set != 0 && REG_P (SET_DEST (set))
3905 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3907 struct no_conflict_data data;
3909 data.target = const0_rtx;
3910 data.first = insns;
3911 data.insn = insn;
3912 data.must_stay = 0;
3913 note_stores (PATTERN (insn), no_conflict_move_test, &data);
3914 if (! data.must_stay)
3916 if (PREV_INSN (insn))
3917 NEXT_INSN (PREV_INSN (insn)) = next;
3918 else
3919 insns = next;
3921 if (next)
3922 PREV_INSN (next) = PREV_INSN (insn);
3924 add_insn (insn);
3928 /* Some ports use a loop to copy large arguments onto the stack.
3929 Don't move anything outside such a loop. */
3930 if (LABEL_P (insn))
3931 break;
3934 prev = get_last_insn ();
3936 /* Write the remaining insns followed by the final copy. */
3938 for (insn = insns; insn; insn = next)
3940 next = NEXT_INSN (insn);
3942 add_insn (insn);
3945 last = emit_move_insn (target, result);
3946 if (optab_handler (mov_optab, GET_MODE (target))->insn_code
3947 != CODE_FOR_nothing)
3948 set_unique_reg_note (last, REG_EQUAL, copy_rtx (equiv));
3950 if (final_dest != target)
3951 emit_move_insn (final_dest, target);
3954 /* Nonzero if we can perform a comparison of mode MODE straightforwardly.
3955 PURPOSE describes how this comparison will be used. CODE is the rtx
3956 comparison code we will be using.
3958 ??? Actually, CODE is slightly weaker than that. A target is still
3959 required to implement all of the normal bcc operations, but not
3960 required to implement all (or any) of the unordered bcc operations. */
3963 can_compare_p (enum rtx_code code, enum machine_mode mode,
3964 enum can_compare_purpose purpose)
3966 rtx test;
3967 test = gen_rtx_fmt_ee (code, mode, const0_rtx, const0_rtx);
3970 int icode;
3972 if (purpose == ccp_jump
3973 && (icode = optab_handler (cbranch_optab, mode)->insn_code) != CODE_FOR_nothing
3974 && insn_data[icode].operand[0].predicate (test, mode))
3975 return 1;
3976 if (purpose == ccp_store_flag
3977 && (icode = optab_handler (cstore_optab, mode)->insn_code) != CODE_FOR_nothing
3978 && insn_data[icode].operand[1].predicate (test, mode))
3979 return 1;
3980 if (purpose == ccp_cmov
3981 && optab_handler (cmov_optab, mode)->insn_code != CODE_FOR_nothing)
3982 return 1;
3984 mode = GET_MODE_WIDER_MODE (mode);
3985 PUT_MODE (test, mode);
3987 while (mode != VOIDmode);
3989 return 0;
3992 /* This function is called when we are going to emit a compare instruction that
3993 compares the values found in *PX and *PY, using the rtl operator COMPARISON.
3995 *PMODE is the mode of the inputs (in case they are const_int).
3996 *PUNSIGNEDP nonzero says that the operands are unsigned;
3997 this matters if they need to be widened (as given by METHODS).
3999 If they have mode BLKmode, then SIZE specifies the size of both operands.
4001 This function performs all the setup necessary so that the caller only has
4002 to emit a single comparison insn. This setup can involve doing a BLKmode
4003 comparison or emitting a library call to perform the comparison if no insn
4004 is available to handle it.
4005 The values which are passed in through pointers can be modified; the caller
4006 should perform the comparison on the modified values. Constant
4007 comparisons must have already been folded. */
4009 static void
4010 prepare_cmp_insn (rtx x, rtx y, enum rtx_code comparison, rtx size,
4011 int unsignedp, enum optab_methods methods,
4012 rtx *ptest, enum machine_mode *pmode)
4014 enum machine_mode mode = *pmode;
4015 rtx libfunc, test;
4016 enum machine_mode cmp_mode;
4017 enum mode_class mclass;
4019 /* The other methods are not needed. */
4020 gcc_assert (methods == OPTAB_DIRECT || methods == OPTAB_WIDEN
4021 || methods == OPTAB_LIB_WIDEN);
4023 /* If we are optimizing, force expensive constants into a register. */
4024 if (CONSTANT_P (x) && optimize
4025 && (rtx_cost (x, COMPARE, optimize_insn_for_speed_p ())
4026 > COSTS_N_INSNS (1)))
4027 x = force_reg (mode, x);
4029 if (CONSTANT_P (y) && optimize
4030 && (rtx_cost (y, COMPARE, optimize_insn_for_speed_p ())
4031 > COSTS_N_INSNS (1)))
4032 y = force_reg (mode, y);
4034 #ifdef HAVE_cc0
4035 /* Make sure if we have a canonical comparison. The RTL
4036 documentation states that canonical comparisons are required only
4037 for targets which have cc0. */
4038 gcc_assert (!CONSTANT_P (x) || CONSTANT_P (y));
4039 #endif
4041 /* Don't let both operands fail to indicate the mode. */
4042 if (GET_MODE (x) == VOIDmode && GET_MODE (y) == VOIDmode)
4043 x = force_reg (mode, x);
4044 if (mode == VOIDmode)
4045 mode = GET_MODE (x) != VOIDmode ? GET_MODE (x) : GET_MODE (y);
4047 /* Handle all BLKmode compares. */
4049 if (mode == BLKmode)
4051 enum machine_mode result_mode;
4052 enum insn_code cmp_code;
4053 tree length_type;
4054 rtx libfunc;
4055 rtx result;
4056 rtx opalign
4057 = GEN_INT (MIN (MEM_ALIGN (x), MEM_ALIGN (y)) / BITS_PER_UNIT);
4059 gcc_assert (size);
4061 /* Try to use a memory block compare insn - either cmpstr
4062 or cmpmem will do. */
4063 for (cmp_mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
4064 cmp_mode != VOIDmode;
4065 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode))
4067 cmp_code = cmpmem_optab[cmp_mode];
4068 if (cmp_code == CODE_FOR_nothing)
4069 cmp_code = cmpstr_optab[cmp_mode];
4070 if (cmp_code == CODE_FOR_nothing)
4071 cmp_code = cmpstrn_optab[cmp_mode];
4072 if (cmp_code == CODE_FOR_nothing)
4073 continue;
4075 /* Must make sure the size fits the insn's mode. */
4076 if ((CONST_INT_P (size)
4077 && INTVAL (size) >= (1 << GET_MODE_BITSIZE (cmp_mode)))
4078 || (GET_MODE_BITSIZE (GET_MODE (size))
4079 > GET_MODE_BITSIZE (cmp_mode)))
4080 continue;
4082 result_mode = insn_data[cmp_code].operand[0].mode;
4083 result = gen_reg_rtx (result_mode);
4084 size = convert_to_mode (cmp_mode, size, 1);
4085 emit_insn (GEN_FCN (cmp_code) (result, x, y, size, opalign));
4087 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4088 *pmode = result_mode;
4089 return;
4092 if (methods != OPTAB_LIB && methods != OPTAB_LIB_WIDEN)
4093 goto fail;
4095 /* Otherwise call a library function, memcmp. */
4096 libfunc = memcmp_libfunc;
4097 length_type = sizetype;
4098 result_mode = TYPE_MODE (integer_type_node);
4099 cmp_mode = TYPE_MODE (length_type);
4100 size = convert_to_mode (TYPE_MODE (length_type), size,
4101 TYPE_UNSIGNED (length_type));
4103 result = emit_library_call_value (libfunc, 0, LCT_PURE,
4104 result_mode, 3,
4105 XEXP (x, 0), Pmode,
4106 XEXP (y, 0), Pmode,
4107 size, cmp_mode);
4109 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, result, const0_rtx);
4110 *pmode = result_mode;
4111 return;
4114 /* Don't allow operands to the compare to trap, as that can put the
4115 compare and branch in different basic blocks. */
4116 if (flag_non_call_exceptions)
4118 if (may_trap_p (x))
4119 x = force_reg (mode, x);
4120 if (may_trap_p (y))
4121 y = force_reg (mode, y);
4124 if (GET_MODE_CLASS (mode) == MODE_CC)
4126 gcc_assert (can_compare_p (comparison, CCmode, ccp_jump));
4127 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4128 return;
4131 mclass = GET_MODE_CLASS (mode);
4132 test = gen_rtx_fmt_ee (comparison, VOIDmode, x, y);
4133 cmp_mode = mode;
4136 enum insn_code icode;
4137 icode = optab_handler (cbranch_optab, cmp_mode)->insn_code;
4138 if (icode != CODE_FOR_nothing
4139 && insn_data[icode].operand[0].predicate (test, VOIDmode))
4141 rtx last = get_last_insn ();
4142 rtx op0 = prepare_operand (icode, x, 1, mode, cmp_mode, unsignedp);
4143 rtx op1 = prepare_operand (icode, y, 2, mode, cmp_mode, unsignedp);
4144 if (op0 && op1
4145 && insn_data[icode].operand[1].predicate
4146 (op0, insn_data[icode].operand[1].mode)
4147 && insn_data[icode].operand[2].predicate
4148 (op1, insn_data[icode].operand[2].mode))
4150 XEXP (test, 0) = op0;
4151 XEXP (test, 1) = op1;
4152 *ptest = test;
4153 *pmode = cmp_mode;
4154 return;
4156 delete_insns_since (last);
4159 if (methods == OPTAB_DIRECT || !CLASS_HAS_WIDER_MODES_P (mclass))
4160 break;
4161 cmp_mode = GET_MODE_WIDER_MODE (cmp_mode);
4163 while (cmp_mode != VOIDmode);
4165 if (methods != OPTAB_LIB_WIDEN)
4166 goto fail;
4168 if (!SCALAR_FLOAT_MODE_P (mode))
4170 rtx result;
4172 /* Handle a libcall just for the mode we are using. */
4173 libfunc = optab_libfunc (cmp_optab, mode);
4174 gcc_assert (libfunc);
4176 /* If we want unsigned, and this mode has a distinct unsigned
4177 comparison routine, use that. */
4178 if (unsignedp)
4180 rtx ulibfunc = optab_libfunc (ucmp_optab, mode);
4181 if (ulibfunc)
4182 libfunc = ulibfunc;
4185 result = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4186 targetm.libgcc_cmp_return_mode (),
4187 2, x, mode, y, mode);
4189 /* There are two kinds of comparison routines. Biased routines
4190 return 0/1/2, and unbiased routines return -1/0/1. Other parts
4191 of gcc expect that the comparison operation is equivalent
4192 to the modified comparison. For signed comparisons compare the
4193 result against 1 in the biased case, and zero in the unbiased
4194 case. For unsigned comparisons always compare against 1 after
4195 biasing the unbiased result by adding 1. This gives us a way to
4196 represent LTU. */
4197 x = result;
4198 y = const1_rtx;
4200 if (!TARGET_LIB_INT_CMP_BIASED)
4202 if (unsignedp)
4203 x = plus_constant (result, 1);
4204 else
4205 y = const0_rtx;
4208 *pmode = word_mode;
4209 prepare_cmp_insn (x, y, comparison, NULL_RTX, unsignedp, methods,
4210 ptest, pmode);
4212 else
4213 prepare_float_lib_cmp (x, y, comparison, ptest, pmode);
4215 return;
4217 fail:
4218 *ptest = NULL_RTX;
4221 /* Before emitting an insn with code ICODE, make sure that X, which is going
4222 to be used for operand OPNUM of the insn, is converted from mode MODE to
4223 WIDER_MODE (UNSIGNEDP determines whether it is an unsigned conversion), and
4224 that it is accepted by the operand predicate. Return the new value. */
4227 prepare_operand (int icode, rtx x, int opnum, enum machine_mode mode,
4228 enum machine_mode wider_mode, int unsignedp)
4230 if (mode != wider_mode)
4231 x = convert_modes (wider_mode, mode, x, unsignedp);
4233 if (!insn_data[icode].operand[opnum].predicate
4234 (x, insn_data[icode].operand[opnum].mode))
4236 if (reload_completed)
4237 return NULL_RTX;
4238 x = copy_to_mode_reg (insn_data[icode].operand[opnum].mode, x);
4241 return x;
4244 /* Subroutine of emit_cmp_and_jump_insns; this function is called when we know
4245 we can do the branch. */
4247 static void
4248 emit_cmp_and_jump_insn_1 (rtx test, enum machine_mode mode, rtx label)
4250 enum machine_mode optab_mode;
4251 enum mode_class mclass;
4252 enum insn_code icode;
4254 mclass = GET_MODE_CLASS (mode);
4255 optab_mode = (mclass == MODE_CC) ? CCmode : mode;
4256 icode = optab_handler (cbranch_optab, optab_mode)->insn_code;
4258 gcc_assert (icode != CODE_FOR_nothing);
4259 gcc_assert (insn_data[icode].operand[0].predicate (test, VOIDmode));
4260 emit_jump_insn (GEN_FCN (icode) (test, XEXP (test, 0), XEXP (test, 1), label));
4263 /* Generate code to compare X with Y so that the condition codes are
4264 set and to jump to LABEL if the condition is true. If X is a
4265 constant and Y is not a constant, then the comparison is swapped to
4266 ensure that the comparison RTL has the canonical form.
4268 UNSIGNEDP nonzero says that X and Y are unsigned; this matters if they
4269 need to be widened. UNSIGNEDP is also used to select the proper
4270 branch condition code.
4272 If X and Y have mode BLKmode, then SIZE specifies the size of both X and Y.
4274 MODE is the mode of the inputs (in case they are const_int).
4276 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.).
4277 It will be potentially converted into an unsigned variant based on
4278 UNSIGNEDP to select a proper jump instruction. */
4280 void
4281 emit_cmp_and_jump_insns (rtx x, rtx y, enum rtx_code comparison, rtx size,
4282 enum machine_mode mode, int unsignedp, rtx label)
4284 rtx op0 = x, op1 = y;
4285 rtx test;
4287 /* Swap operands and condition to ensure canonical RTL. */
4288 if (swap_commutative_operands_p (x, y)
4289 && can_compare_p (swap_condition (comparison), mode, ccp_jump))
4291 op0 = y, op1 = x;
4292 comparison = swap_condition (comparison);
4295 /* If OP0 is still a constant, then both X and Y must be constants
4296 or the opposite comparison is not supported. Force X into a register
4297 to create canonical RTL. */
4298 if (CONSTANT_P (op0))
4299 op0 = force_reg (mode, op0);
4301 if (unsignedp)
4302 comparison = unsigned_condition (comparison);
4304 prepare_cmp_insn (op0, op1, comparison, size, unsignedp, OPTAB_LIB_WIDEN,
4305 &test, &mode);
4306 emit_cmp_and_jump_insn_1 (test, mode, label);
4310 /* Emit a library call comparison between floating point X and Y.
4311 COMPARISON is the rtl operator to compare with (EQ, NE, GT, etc.). */
4313 static void
4314 prepare_float_lib_cmp (rtx x, rtx y, enum rtx_code comparison,
4315 rtx *ptest, enum machine_mode *pmode)
4317 enum rtx_code swapped = swap_condition (comparison);
4318 enum rtx_code reversed = reverse_condition_maybe_unordered (comparison);
4319 enum machine_mode orig_mode = GET_MODE (x);
4320 enum machine_mode mode, cmp_mode;
4321 rtx value, target, insns, equiv;
4322 rtx libfunc = 0;
4323 bool reversed_p = false;
4324 cmp_mode = targetm.libgcc_cmp_return_mode ();
4326 for (mode = orig_mode;
4327 mode != VOIDmode;
4328 mode = GET_MODE_WIDER_MODE (mode))
4330 if (code_to_optab[comparison]
4331 && (libfunc = optab_libfunc (code_to_optab[comparison], mode)))
4332 break;
4334 if (code_to_optab[swapped]
4335 && (libfunc = optab_libfunc (code_to_optab[swapped], mode)))
4337 rtx tmp;
4338 tmp = x; x = y; y = tmp;
4339 comparison = swapped;
4340 break;
4343 if (code_to_optab[reversed]
4344 && (libfunc = optab_libfunc (code_to_optab[reversed], mode))
4345 && FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, reversed))
4347 comparison = reversed;
4348 reversed_p = true;
4349 break;
4353 gcc_assert (mode != VOIDmode);
4355 if (mode != orig_mode)
4357 x = convert_to_mode (mode, x, 0);
4358 y = convert_to_mode (mode, y, 0);
4361 /* Attach a REG_EQUAL note describing the semantics of the libcall to
4362 the RTL. The allows the RTL optimizers to delete the libcall if the
4363 condition can be determined at compile-time. */
4364 if (comparison == UNORDERED)
4366 rtx temp = simplify_gen_relational (NE, cmp_mode, mode, x, x);
4367 equiv = simplify_gen_relational (NE, cmp_mode, mode, y, y);
4368 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4369 temp, const_true_rtx, equiv);
4371 else
4373 equiv = simplify_gen_relational (comparison, cmp_mode, mode, x, y);
4374 if (! FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4376 rtx true_rtx, false_rtx;
4378 switch (comparison)
4380 case EQ:
4381 true_rtx = const0_rtx;
4382 false_rtx = const_true_rtx;
4383 break;
4385 case NE:
4386 true_rtx = const_true_rtx;
4387 false_rtx = const0_rtx;
4388 break;
4390 case GT:
4391 true_rtx = const1_rtx;
4392 false_rtx = const0_rtx;
4393 break;
4395 case GE:
4396 true_rtx = const0_rtx;
4397 false_rtx = constm1_rtx;
4398 break;
4400 case LT:
4401 true_rtx = constm1_rtx;
4402 false_rtx = const0_rtx;
4403 break;
4405 case LE:
4406 true_rtx = const0_rtx;
4407 false_rtx = const1_rtx;
4408 break;
4410 default:
4411 gcc_unreachable ();
4413 equiv = simplify_gen_ternary (IF_THEN_ELSE, cmp_mode, cmp_mode,
4414 equiv, true_rtx, false_rtx);
4418 start_sequence ();
4419 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
4420 cmp_mode, 2, x, mode, y, mode);
4421 insns = get_insns ();
4422 end_sequence ();
4424 target = gen_reg_rtx (cmp_mode);
4425 emit_libcall_block (insns, target, value, equiv);
4427 if (comparison == UNORDERED
4428 || FLOAT_LIB_COMPARE_RETURNS_BOOL (mode, comparison))
4429 comparison = reversed_p ? EQ : NE;
4431 *ptest = gen_rtx_fmt_ee (comparison, VOIDmode, target, const0_rtx);
4432 *pmode = cmp_mode;
4435 /* Generate code to indirectly jump to a location given in the rtx LOC. */
4437 void
4438 emit_indirect_jump (rtx loc)
4440 if (!insn_data[(int) CODE_FOR_indirect_jump].operand[0].predicate
4441 (loc, Pmode))
4442 loc = copy_to_mode_reg (Pmode, loc);
4444 emit_jump_insn (gen_indirect_jump (loc));
4445 emit_barrier ();
4448 #ifdef HAVE_conditional_move
4450 /* Emit a conditional move instruction if the machine supports one for that
4451 condition and machine mode.
4453 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4454 the mode to use should they be constants. If it is VOIDmode, they cannot
4455 both be constants.
4457 OP2 should be stored in TARGET if the comparison is true, otherwise OP3
4458 should be stored there. MODE is the mode to use should they be constants.
4459 If it is VOIDmode, they cannot both be constants.
4461 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4462 is not supported. */
4465 emit_conditional_move (rtx target, enum rtx_code code, rtx op0, rtx op1,
4466 enum machine_mode cmode, rtx op2, rtx op3,
4467 enum machine_mode mode, int unsignedp)
4469 rtx tem, subtarget, comparison, insn;
4470 enum insn_code icode;
4471 enum rtx_code reversed;
4473 /* If one operand is constant, make it the second one. Only do this
4474 if the other operand is not constant as well. */
4476 if (swap_commutative_operands_p (op0, op1))
4478 tem = op0;
4479 op0 = op1;
4480 op1 = tem;
4481 code = swap_condition (code);
4484 /* get_condition will prefer to generate LT and GT even if the old
4485 comparison was against zero, so undo that canonicalization here since
4486 comparisons against zero are cheaper. */
4487 if (code == LT && op1 == const1_rtx)
4488 code = LE, op1 = const0_rtx;
4489 else if (code == GT && op1 == constm1_rtx)
4490 code = GE, op1 = const0_rtx;
4492 if (cmode == VOIDmode)
4493 cmode = GET_MODE (op0);
4495 if (swap_commutative_operands_p (op2, op3)
4496 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4497 != UNKNOWN))
4499 tem = op2;
4500 op2 = op3;
4501 op3 = tem;
4502 code = reversed;
4505 if (mode == VOIDmode)
4506 mode = GET_MODE (op2);
4508 icode = movcc_gen_code[mode];
4510 if (icode == CODE_FOR_nothing)
4511 return 0;
4513 if (!target)
4514 target = gen_reg_rtx (mode);
4516 subtarget = target;
4518 /* If the insn doesn't accept these operands, put them in pseudos. */
4520 if (!insn_data[icode].operand[0].predicate
4521 (subtarget, insn_data[icode].operand[0].mode))
4522 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
4524 if (!insn_data[icode].operand[2].predicate
4525 (op2, insn_data[icode].operand[2].mode))
4526 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
4528 if (!insn_data[icode].operand[3].predicate
4529 (op3, insn_data[icode].operand[3].mode))
4530 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
4532 /* Everything should now be in the suitable form. */
4534 code = unsignedp ? unsigned_condition (code) : code;
4535 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4537 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4538 return NULL and let the caller figure out how best to deal with this
4539 situation. */
4540 if (!COMPARISON_P (comparison))
4541 return NULL_RTX;
4543 do_pending_stack_adjust ();
4544 start_sequence ();
4545 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4546 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4547 &comparison, &cmode);
4548 if (!comparison)
4549 insn = NULL_RTX;
4550 else
4551 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
4553 /* If that failed, then give up. */
4554 if (insn == 0)
4556 end_sequence ();
4557 return 0;
4560 emit_insn (insn);
4561 insn = get_insns ();
4562 end_sequence ();
4563 emit_insn (insn);
4564 if (subtarget != target)
4565 convert_move (target, subtarget, 0);
4567 return target;
4570 /* Return nonzero if a conditional move of mode MODE is supported.
4572 This function is for combine so it can tell whether an insn that looks
4573 like a conditional move is actually supported by the hardware. If we
4574 guess wrong we lose a bit on optimization, but that's it. */
4575 /* ??? sparc64 supports conditionally moving integers values based on fp
4576 comparisons, and vice versa. How do we handle them? */
4579 can_conditionally_move_p (enum machine_mode mode)
4581 if (movcc_gen_code[mode] != CODE_FOR_nothing)
4582 return 1;
4584 return 0;
4587 #endif /* HAVE_conditional_move */
4589 /* Emit a conditional addition instruction if the machine supports one for that
4590 condition and machine mode.
4592 OP0 and OP1 are the operands that should be compared using CODE. CMODE is
4593 the mode to use should they be constants. If it is VOIDmode, they cannot
4594 both be constants.
4596 OP2 should be stored in TARGET if the comparison is true, otherwise OP2+OP3
4597 should be stored there. MODE is the mode to use should they be constants.
4598 If it is VOIDmode, they cannot both be constants.
4600 The result is either TARGET (perhaps modified) or NULL_RTX if the operation
4601 is not supported. */
4604 emit_conditional_add (rtx target, enum rtx_code code, rtx op0, rtx op1,
4605 enum machine_mode cmode, rtx op2, rtx op3,
4606 enum machine_mode mode, int unsignedp)
4608 rtx tem, subtarget, comparison, insn;
4609 enum insn_code icode;
4610 enum rtx_code reversed;
4612 /* If one operand is constant, make it the second one. Only do this
4613 if the other operand is not constant as well. */
4615 if (swap_commutative_operands_p (op0, op1))
4617 tem = op0;
4618 op0 = op1;
4619 op1 = tem;
4620 code = swap_condition (code);
4623 /* get_condition will prefer to generate LT and GT even if the old
4624 comparison was against zero, so undo that canonicalization here since
4625 comparisons against zero are cheaper. */
4626 if (code == LT && op1 == const1_rtx)
4627 code = LE, op1 = const0_rtx;
4628 else if (code == GT && op1 == constm1_rtx)
4629 code = GE, op1 = const0_rtx;
4631 if (cmode == VOIDmode)
4632 cmode = GET_MODE (op0);
4634 if (swap_commutative_operands_p (op2, op3)
4635 && ((reversed = reversed_comparison_code_parts (code, op0, op1, NULL))
4636 != UNKNOWN))
4638 tem = op2;
4639 op2 = op3;
4640 op3 = tem;
4641 code = reversed;
4644 if (mode == VOIDmode)
4645 mode = GET_MODE (op2);
4647 icode = optab_handler (addcc_optab, mode)->insn_code;
4649 if (icode == CODE_FOR_nothing)
4650 return 0;
4652 if (!target)
4653 target = gen_reg_rtx (mode);
4655 /* If the insn doesn't accept these operands, put them in pseudos. */
4657 if (!insn_data[icode].operand[0].predicate
4658 (target, insn_data[icode].operand[0].mode))
4659 subtarget = gen_reg_rtx (insn_data[icode].operand[0].mode);
4660 else
4661 subtarget = target;
4663 if (!insn_data[icode].operand[2].predicate
4664 (op2, insn_data[icode].operand[2].mode))
4665 op2 = copy_to_mode_reg (insn_data[icode].operand[2].mode, op2);
4667 if (!insn_data[icode].operand[3].predicate
4668 (op3, insn_data[icode].operand[3].mode))
4669 op3 = copy_to_mode_reg (insn_data[icode].operand[3].mode, op3);
4671 /* Everything should now be in the suitable form. */
4673 code = unsignedp ? unsigned_condition (code) : code;
4674 comparison = simplify_gen_relational (code, VOIDmode, cmode, op0, op1);
4676 /* We can get const0_rtx or const_true_rtx in some circumstances. Just
4677 return NULL and let the caller figure out how best to deal with this
4678 situation. */
4679 if (!COMPARISON_P (comparison))
4680 return NULL_RTX;
4682 do_pending_stack_adjust ();
4683 start_sequence ();
4684 prepare_cmp_insn (XEXP (comparison, 0), XEXP (comparison, 1),
4685 GET_CODE (comparison), NULL_RTX, unsignedp, OPTAB_WIDEN,
4686 &comparison, &cmode);
4687 if (!comparison)
4688 insn = NULL_RTX;
4689 else
4690 insn = GEN_FCN (icode) (subtarget, comparison, op2, op3);
4692 /* If that failed, then give up. */
4693 if (insn == 0)
4695 end_sequence ();
4696 return 0;
4699 emit_insn (insn);
4700 insn = get_insns ();
4701 end_sequence ();
4702 emit_insn (insn);
4703 if (subtarget != target)
4704 convert_move (target, subtarget, 0);
4706 return target;
4709 /* These functions attempt to generate an insn body, rather than
4710 emitting the insn, but if the gen function already emits them, we
4711 make no attempt to turn them back into naked patterns. */
4713 /* Generate and return an insn body to add Y to X. */
4716 gen_add2_insn (rtx x, rtx y)
4718 int icode = (int) optab_handler (add_optab, GET_MODE (x))->insn_code;
4720 gcc_assert (insn_data[icode].operand[0].predicate
4721 (x, insn_data[icode].operand[0].mode));
4722 gcc_assert (insn_data[icode].operand[1].predicate
4723 (x, insn_data[icode].operand[1].mode));
4724 gcc_assert (insn_data[icode].operand[2].predicate
4725 (y, insn_data[icode].operand[2].mode));
4727 return GEN_FCN (icode) (x, x, y);
4730 /* Generate and return an insn body to add r1 and c,
4731 storing the result in r0. */
4734 gen_add3_insn (rtx r0, rtx r1, rtx c)
4736 int icode = (int) optab_handler (add_optab, GET_MODE (r0))->insn_code;
4738 if (icode == CODE_FOR_nothing
4739 || !(insn_data[icode].operand[0].predicate
4740 (r0, insn_data[icode].operand[0].mode))
4741 || !(insn_data[icode].operand[1].predicate
4742 (r1, insn_data[icode].operand[1].mode))
4743 || !(insn_data[icode].operand[2].predicate
4744 (c, insn_data[icode].operand[2].mode)))
4745 return NULL_RTX;
4747 return GEN_FCN (icode) (r0, r1, c);
4751 have_add2_insn (rtx x, rtx y)
4753 int icode;
4755 gcc_assert (GET_MODE (x) != VOIDmode);
4757 icode = (int) optab_handler (add_optab, GET_MODE (x))->insn_code;
4759 if (icode == CODE_FOR_nothing)
4760 return 0;
4762 if (!(insn_data[icode].operand[0].predicate
4763 (x, insn_data[icode].operand[0].mode))
4764 || !(insn_data[icode].operand[1].predicate
4765 (x, insn_data[icode].operand[1].mode))
4766 || !(insn_data[icode].operand[2].predicate
4767 (y, insn_data[icode].operand[2].mode)))
4768 return 0;
4770 return 1;
4773 /* Generate and return an insn body to subtract Y from X. */
4776 gen_sub2_insn (rtx x, rtx y)
4778 int icode = (int) optab_handler (sub_optab, GET_MODE (x))->insn_code;
4780 gcc_assert (insn_data[icode].operand[0].predicate
4781 (x, insn_data[icode].operand[0].mode));
4782 gcc_assert (insn_data[icode].operand[1].predicate
4783 (x, insn_data[icode].operand[1].mode));
4784 gcc_assert (insn_data[icode].operand[2].predicate
4785 (y, insn_data[icode].operand[2].mode));
4787 return GEN_FCN (icode) (x, x, y);
4790 /* Generate and return an insn body to subtract r1 and c,
4791 storing the result in r0. */
4794 gen_sub3_insn (rtx r0, rtx r1, rtx c)
4796 int icode = (int) optab_handler (sub_optab, GET_MODE (r0))->insn_code;
4798 if (icode == CODE_FOR_nothing
4799 || !(insn_data[icode].operand[0].predicate
4800 (r0, insn_data[icode].operand[0].mode))
4801 || !(insn_data[icode].operand[1].predicate
4802 (r1, insn_data[icode].operand[1].mode))
4803 || !(insn_data[icode].operand[2].predicate
4804 (c, insn_data[icode].operand[2].mode)))
4805 return NULL_RTX;
4807 return GEN_FCN (icode) (r0, r1, c);
4811 have_sub2_insn (rtx x, rtx y)
4813 int icode;
4815 gcc_assert (GET_MODE (x) != VOIDmode);
4817 icode = (int) optab_handler (sub_optab, GET_MODE (x))->insn_code;
4819 if (icode == CODE_FOR_nothing)
4820 return 0;
4822 if (!(insn_data[icode].operand[0].predicate
4823 (x, insn_data[icode].operand[0].mode))
4824 || !(insn_data[icode].operand[1].predicate
4825 (x, insn_data[icode].operand[1].mode))
4826 || !(insn_data[icode].operand[2].predicate
4827 (y, insn_data[icode].operand[2].mode)))
4828 return 0;
4830 return 1;
4833 /* Generate the body of an instruction to copy Y into X.
4834 It may be a list of insns, if one insn isn't enough. */
4837 gen_move_insn (rtx x, rtx y)
4839 rtx seq;
4841 start_sequence ();
4842 emit_move_insn_1 (x, y);
4843 seq = get_insns ();
4844 end_sequence ();
4845 return seq;
4848 /* Return the insn code used to extend FROM_MODE to TO_MODE.
4849 UNSIGNEDP specifies zero-extension instead of sign-extension. If
4850 no such operation exists, CODE_FOR_nothing will be returned. */
4852 enum insn_code
4853 can_extend_p (enum machine_mode to_mode, enum machine_mode from_mode,
4854 int unsignedp)
4856 convert_optab tab;
4857 #ifdef HAVE_ptr_extend
4858 if (unsignedp < 0)
4859 return CODE_FOR_ptr_extend;
4860 #endif
4862 tab = unsignedp ? zext_optab : sext_optab;
4863 return convert_optab_handler (tab, to_mode, from_mode)->insn_code;
4866 /* Generate the body of an insn to extend Y (with mode MFROM)
4867 into X (with mode MTO). Do zero-extension if UNSIGNEDP is nonzero. */
4870 gen_extend_insn (rtx x, rtx y, enum machine_mode mto,
4871 enum machine_mode mfrom, int unsignedp)
4873 enum insn_code icode = can_extend_p (mto, mfrom, unsignedp);
4874 return GEN_FCN (icode) (x, y);
4877 /* can_fix_p and can_float_p say whether the target machine
4878 can directly convert a given fixed point type to
4879 a given floating point type, or vice versa.
4880 The returned value is the CODE_FOR_... value to use,
4881 or CODE_FOR_nothing if these modes cannot be directly converted.
4883 *TRUNCP_PTR is set to 1 if it is necessary to output
4884 an explicit FTRUNC insn before the fix insn; otherwise 0. */
4886 static enum insn_code
4887 can_fix_p (enum machine_mode fixmode, enum machine_mode fltmode,
4888 int unsignedp, int *truncp_ptr)
4890 convert_optab tab;
4891 enum insn_code icode;
4893 tab = unsignedp ? ufixtrunc_optab : sfixtrunc_optab;
4894 icode = convert_optab_handler (tab, fixmode, fltmode)->insn_code;
4895 if (icode != CODE_FOR_nothing)
4897 *truncp_ptr = 0;
4898 return icode;
4901 /* FIXME: This requires a port to define both FIX and FTRUNC pattern
4902 for this to work. We need to rework the fix* and ftrunc* patterns
4903 and documentation. */
4904 tab = unsignedp ? ufix_optab : sfix_optab;
4905 icode = convert_optab_handler (tab, fixmode, fltmode)->insn_code;
4906 if (icode != CODE_FOR_nothing
4907 && optab_handler (ftrunc_optab, fltmode)->insn_code != CODE_FOR_nothing)
4909 *truncp_ptr = 1;
4910 return icode;
4913 *truncp_ptr = 0;
4914 return CODE_FOR_nothing;
4917 static enum insn_code
4918 can_float_p (enum machine_mode fltmode, enum machine_mode fixmode,
4919 int unsignedp)
4921 convert_optab tab;
4923 tab = unsignedp ? ufloat_optab : sfloat_optab;
4924 return convert_optab_handler (tab, fltmode, fixmode)->insn_code;
4927 /* Generate code to convert FROM to floating point
4928 and store in TO. FROM must be fixed point and not VOIDmode.
4929 UNSIGNEDP nonzero means regard FROM as unsigned.
4930 Normally this is done by correcting the final value
4931 if it is negative. */
4933 void
4934 expand_float (rtx to, rtx from, int unsignedp)
4936 enum insn_code icode;
4937 rtx target = to;
4938 enum machine_mode fmode, imode;
4939 bool can_do_signed = false;
4941 /* Crash now, because we won't be able to decide which mode to use. */
4942 gcc_assert (GET_MODE (from) != VOIDmode);
4944 /* Look for an insn to do the conversion. Do it in the specified
4945 modes if possible; otherwise convert either input, output or both to
4946 wider mode. If the integer mode is wider than the mode of FROM,
4947 we can do the conversion signed even if the input is unsigned. */
4949 for (fmode = GET_MODE (to); fmode != VOIDmode;
4950 fmode = GET_MODE_WIDER_MODE (fmode))
4951 for (imode = GET_MODE (from); imode != VOIDmode;
4952 imode = GET_MODE_WIDER_MODE (imode))
4954 int doing_unsigned = unsignedp;
4956 if (fmode != GET_MODE (to)
4957 && significand_size (fmode) < GET_MODE_BITSIZE (GET_MODE (from)))
4958 continue;
4960 icode = can_float_p (fmode, imode, unsignedp);
4961 if (icode == CODE_FOR_nothing && unsignedp)
4963 enum insn_code scode = can_float_p (fmode, imode, 0);
4964 if (scode != CODE_FOR_nothing)
4965 can_do_signed = true;
4966 if (imode != GET_MODE (from))
4967 icode = scode, doing_unsigned = 0;
4970 if (icode != CODE_FOR_nothing)
4972 if (imode != GET_MODE (from))
4973 from = convert_to_mode (imode, from, unsignedp);
4975 if (fmode != GET_MODE (to))
4976 target = gen_reg_rtx (fmode);
4978 emit_unop_insn (icode, target, from,
4979 doing_unsigned ? UNSIGNED_FLOAT : FLOAT);
4981 if (target != to)
4982 convert_move (to, target, 0);
4983 return;
4987 /* Unsigned integer, and no way to convert directly. Convert as signed,
4988 then unconditionally adjust the result. */
4989 if (unsignedp && can_do_signed)
4991 rtx label = gen_label_rtx ();
4992 rtx temp;
4993 REAL_VALUE_TYPE offset;
4995 /* Look for a usable floating mode FMODE wider than the source and at
4996 least as wide as the target. Using FMODE will avoid rounding woes
4997 with unsigned values greater than the signed maximum value. */
4999 for (fmode = GET_MODE (to); fmode != VOIDmode;
5000 fmode = GET_MODE_WIDER_MODE (fmode))
5001 if (GET_MODE_BITSIZE (GET_MODE (from)) < GET_MODE_BITSIZE (fmode)
5002 && can_float_p (fmode, GET_MODE (from), 0) != CODE_FOR_nothing)
5003 break;
5005 if (fmode == VOIDmode)
5007 /* There is no such mode. Pretend the target is wide enough. */
5008 fmode = GET_MODE (to);
5010 /* Avoid double-rounding when TO is narrower than FROM. */
5011 if ((significand_size (fmode) + 1)
5012 < GET_MODE_BITSIZE (GET_MODE (from)))
5014 rtx temp1;
5015 rtx neglabel = gen_label_rtx ();
5017 /* Don't use TARGET if it isn't a register, is a hard register,
5018 or is the wrong mode. */
5019 if (!REG_P (target)
5020 || REGNO (target) < FIRST_PSEUDO_REGISTER
5021 || GET_MODE (target) != fmode)
5022 target = gen_reg_rtx (fmode);
5024 imode = GET_MODE (from);
5025 do_pending_stack_adjust ();
5027 /* Test whether the sign bit is set. */
5028 emit_cmp_and_jump_insns (from, const0_rtx, LT, NULL_RTX, imode,
5029 0, neglabel);
5031 /* The sign bit is not set. Convert as signed. */
5032 expand_float (target, from, 0);
5033 emit_jump_insn (gen_jump (label));
5034 emit_barrier ();
5036 /* The sign bit is set.
5037 Convert to a usable (positive signed) value by shifting right
5038 one bit, while remembering if a nonzero bit was shifted
5039 out; i.e., compute (from & 1) | (from >> 1). */
5041 emit_label (neglabel);
5042 temp = expand_binop (imode, and_optab, from, const1_rtx,
5043 NULL_RTX, 1, OPTAB_LIB_WIDEN);
5044 temp1 = expand_shift (RSHIFT_EXPR, imode, from, integer_one_node,
5045 NULL_RTX, 1);
5046 temp = expand_binop (imode, ior_optab, temp, temp1, temp, 1,
5047 OPTAB_LIB_WIDEN);
5048 expand_float (target, temp, 0);
5050 /* Multiply by 2 to undo the shift above. */
5051 temp = expand_binop (fmode, add_optab, target, target,
5052 target, 0, OPTAB_LIB_WIDEN);
5053 if (temp != target)
5054 emit_move_insn (target, temp);
5056 do_pending_stack_adjust ();
5057 emit_label (label);
5058 goto done;
5062 /* If we are about to do some arithmetic to correct for an
5063 unsigned operand, do it in a pseudo-register. */
5065 if (GET_MODE (to) != fmode
5066 || !REG_P (to) || REGNO (to) < FIRST_PSEUDO_REGISTER)
5067 target = gen_reg_rtx (fmode);
5069 /* Convert as signed integer to floating. */
5070 expand_float (target, from, 0);
5072 /* If FROM is negative (and therefore TO is negative),
5073 correct its value by 2**bitwidth. */
5075 do_pending_stack_adjust ();
5076 emit_cmp_and_jump_insns (from, const0_rtx, GE, NULL_RTX, GET_MODE (from),
5077 0, label);
5080 real_2expN (&offset, GET_MODE_BITSIZE (GET_MODE (from)), fmode);
5081 temp = expand_binop (fmode, add_optab, target,
5082 CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode),
5083 target, 0, OPTAB_LIB_WIDEN);
5084 if (temp != target)
5085 emit_move_insn (target, temp);
5087 do_pending_stack_adjust ();
5088 emit_label (label);
5089 goto done;
5092 /* No hardware instruction available; call a library routine. */
5094 rtx libfunc;
5095 rtx insns;
5096 rtx value;
5097 convert_optab tab = unsignedp ? ufloat_optab : sfloat_optab;
5099 if (GET_MODE_SIZE (GET_MODE (from)) < GET_MODE_SIZE (SImode))
5100 from = convert_to_mode (SImode, from, unsignedp);
5102 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5103 gcc_assert (libfunc);
5105 start_sequence ();
5107 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5108 GET_MODE (to), 1, from,
5109 GET_MODE (from));
5110 insns = get_insns ();
5111 end_sequence ();
5113 emit_libcall_block (insns, target, value,
5114 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FLOAT : FLOAT,
5115 GET_MODE (to), from));
5118 done:
5120 /* Copy result to requested destination
5121 if we have been computing in a temp location. */
5123 if (target != to)
5125 if (GET_MODE (target) == GET_MODE (to))
5126 emit_move_insn (to, target);
5127 else
5128 convert_move (to, target, 0);
5132 /* Generate code to convert FROM to fixed point and store in TO. FROM
5133 must be floating point. */
5135 void
5136 expand_fix (rtx to, rtx from, int unsignedp)
5138 enum insn_code icode;
5139 rtx target = to;
5140 enum machine_mode fmode, imode;
5141 int must_trunc = 0;
5143 /* We first try to find a pair of modes, one real and one integer, at
5144 least as wide as FROM and TO, respectively, in which we can open-code
5145 this conversion. If the integer mode is wider than the mode of TO,
5146 we can do the conversion either signed or unsigned. */
5148 for (fmode = GET_MODE (from); fmode != VOIDmode;
5149 fmode = GET_MODE_WIDER_MODE (fmode))
5150 for (imode = GET_MODE (to); imode != VOIDmode;
5151 imode = GET_MODE_WIDER_MODE (imode))
5153 int doing_unsigned = unsignedp;
5155 icode = can_fix_p (imode, fmode, unsignedp, &must_trunc);
5156 if (icode == CODE_FOR_nothing && imode != GET_MODE (to) && unsignedp)
5157 icode = can_fix_p (imode, fmode, 0, &must_trunc), doing_unsigned = 0;
5159 if (icode != CODE_FOR_nothing)
5161 rtx last = get_last_insn ();
5162 if (fmode != GET_MODE (from))
5163 from = convert_to_mode (fmode, from, 0);
5165 if (must_trunc)
5167 rtx temp = gen_reg_rtx (GET_MODE (from));
5168 from = expand_unop (GET_MODE (from), ftrunc_optab, from,
5169 temp, 0);
5172 if (imode != GET_MODE (to))
5173 target = gen_reg_rtx (imode);
5175 if (maybe_emit_unop_insn (icode, target, from,
5176 doing_unsigned ? UNSIGNED_FIX : FIX))
5178 if (target != to)
5179 convert_move (to, target, unsignedp);
5180 return;
5182 delete_insns_since (last);
5186 /* For an unsigned conversion, there is one more way to do it.
5187 If we have a signed conversion, we generate code that compares
5188 the real value to the largest representable positive number. If if
5189 is smaller, the conversion is done normally. Otherwise, subtract
5190 one plus the highest signed number, convert, and add it back.
5192 We only need to check all real modes, since we know we didn't find
5193 anything with a wider integer mode.
5195 This code used to extend FP value into mode wider than the destination.
5196 This is needed for decimal float modes which cannot accurately
5197 represent one plus the highest signed number of the same size, but
5198 not for binary modes. Consider, for instance conversion from SFmode
5199 into DImode.
5201 The hot path through the code is dealing with inputs smaller than 2^63
5202 and doing just the conversion, so there is no bits to lose.
5204 In the other path we know the value is positive in the range 2^63..2^64-1
5205 inclusive. (as for other input overflow happens and result is undefined)
5206 So we know that the most important bit set in mantissa corresponds to
5207 2^63. The subtraction of 2^63 should not generate any rounding as it
5208 simply clears out that bit. The rest is trivial. */
5210 if (unsignedp && GET_MODE_BITSIZE (GET_MODE (to)) <= HOST_BITS_PER_WIDE_INT)
5211 for (fmode = GET_MODE (from); fmode != VOIDmode;
5212 fmode = GET_MODE_WIDER_MODE (fmode))
5213 if (CODE_FOR_nothing != can_fix_p (GET_MODE (to), fmode, 0, &must_trunc)
5214 && (!DECIMAL_FLOAT_MODE_P (fmode)
5215 || GET_MODE_BITSIZE (fmode) > GET_MODE_BITSIZE (GET_MODE (to))))
5217 int bitsize;
5218 REAL_VALUE_TYPE offset;
5219 rtx limit, lab1, lab2, insn;
5221 bitsize = GET_MODE_BITSIZE (GET_MODE (to));
5222 real_2expN (&offset, bitsize - 1, fmode);
5223 limit = CONST_DOUBLE_FROM_REAL_VALUE (offset, fmode);
5224 lab1 = gen_label_rtx ();
5225 lab2 = gen_label_rtx ();
5227 if (fmode != GET_MODE (from))
5228 from = convert_to_mode (fmode, from, 0);
5230 /* See if we need to do the subtraction. */
5231 do_pending_stack_adjust ();
5232 emit_cmp_and_jump_insns (from, limit, GE, NULL_RTX, GET_MODE (from),
5233 0, lab1);
5235 /* If not, do the signed "fix" and branch around fixup code. */
5236 expand_fix (to, from, 0);
5237 emit_jump_insn (gen_jump (lab2));
5238 emit_barrier ();
5240 /* Otherwise, subtract 2**(N-1), convert to signed number,
5241 then add 2**(N-1). Do the addition using XOR since this
5242 will often generate better code. */
5243 emit_label (lab1);
5244 target = expand_binop (GET_MODE (from), sub_optab, from, limit,
5245 NULL_RTX, 0, OPTAB_LIB_WIDEN);
5246 expand_fix (to, target, 0);
5247 target = expand_binop (GET_MODE (to), xor_optab, to,
5248 gen_int_mode
5249 ((HOST_WIDE_INT) 1 << (bitsize - 1),
5250 GET_MODE (to)),
5251 to, 1, OPTAB_LIB_WIDEN);
5253 if (target != to)
5254 emit_move_insn (to, target);
5256 emit_label (lab2);
5258 if (optab_handler (mov_optab, GET_MODE (to))->insn_code
5259 != CODE_FOR_nothing)
5261 /* Make a place for a REG_NOTE and add it. */
5262 insn = emit_move_insn (to, to);
5263 set_unique_reg_note (insn,
5264 REG_EQUAL,
5265 gen_rtx_fmt_e (UNSIGNED_FIX,
5266 GET_MODE (to),
5267 copy_rtx (from)));
5270 return;
5273 /* We can't do it with an insn, so use a library call. But first ensure
5274 that the mode of TO is at least as wide as SImode, since those are the
5275 only library calls we know about. */
5277 if (GET_MODE_SIZE (GET_MODE (to)) < GET_MODE_SIZE (SImode))
5279 target = gen_reg_rtx (SImode);
5281 expand_fix (target, from, unsignedp);
5283 else
5285 rtx insns;
5286 rtx value;
5287 rtx libfunc;
5289 convert_optab tab = unsignedp ? ufix_optab : sfix_optab;
5290 libfunc = convert_optab_libfunc (tab, GET_MODE (to), GET_MODE (from));
5291 gcc_assert (libfunc);
5293 start_sequence ();
5295 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST,
5296 GET_MODE (to), 1, from,
5297 GET_MODE (from));
5298 insns = get_insns ();
5299 end_sequence ();
5301 emit_libcall_block (insns, target, value,
5302 gen_rtx_fmt_e (unsignedp ? UNSIGNED_FIX : FIX,
5303 GET_MODE (to), from));
5306 if (target != to)
5308 if (GET_MODE (to) == GET_MODE (target))
5309 emit_move_insn (to, target);
5310 else
5311 convert_move (to, target, 0);
5315 /* Generate code to convert FROM or TO a fixed-point.
5316 If UINTP is true, either TO or FROM is an unsigned integer.
5317 If SATP is true, we need to saturate the result. */
5319 void
5320 expand_fixed_convert (rtx to, rtx from, int uintp, int satp)
5322 enum machine_mode to_mode = GET_MODE (to);
5323 enum machine_mode from_mode = GET_MODE (from);
5324 convert_optab tab;
5325 enum rtx_code this_code;
5326 enum insn_code code;
5327 rtx insns, value;
5328 rtx libfunc;
5330 if (to_mode == from_mode)
5332 emit_move_insn (to, from);
5333 return;
5336 if (uintp)
5338 tab = satp ? satfractuns_optab : fractuns_optab;
5339 this_code = satp ? UNSIGNED_SAT_FRACT : UNSIGNED_FRACT_CONVERT;
5341 else
5343 tab = satp ? satfract_optab : fract_optab;
5344 this_code = satp ? SAT_FRACT : FRACT_CONVERT;
5346 code = tab->handlers[to_mode][from_mode].insn_code;
5347 if (code != CODE_FOR_nothing)
5349 emit_unop_insn (code, to, from, this_code);
5350 return;
5353 libfunc = convert_optab_libfunc (tab, to_mode, from_mode);
5354 gcc_assert (libfunc);
5356 start_sequence ();
5357 value = emit_library_call_value (libfunc, NULL_RTX, LCT_CONST, to_mode,
5358 1, from, from_mode);
5359 insns = get_insns ();
5360 end_sequence ();
5362 emit_libcall_block (insns, to, value,
5363 gen_rtx_fmt_e (tab->code, to_mode, from));
5366 /* Generate code to convert FROM to fixed point and store in TO. FROM
5367 must be floating point, TO must be signed. Use the conversion optab
5368 TAB to do the conversion. */
5370 bool
5371 expand_sfix_optab (rtx to, rtx from, convert_optab tab)
5373 enum insn_code icode;
5374 rtx target = to;
5375 enum machine_mode fmode, imode;
5377 /* We first try to find a pair of modes, one real and one integer, at
5378 least as wide as FROM and TO, respectively, in which we can open-code
5379 this conversion. If the integer mode is wider than the mode of TO,
5380 we can do the conversion either signed or unsigned. */
5382 for (fmode = GET_MODE (from); fmode != VOIDmode;
5383 fmode = GET_MODE_WIDER_MODE (fmode))
5384 for (imode = GET_MODE (to); imode != VOIDmode;
5385 imode = GET_MODE_WIDER_MODE (imode))
5387 icode = convert_optab_handler (tab, imode, fmode)->insn_code;
5388 if (icode != CODE_FOR_nothing)
5390 rtx last = get_last_insn ();
5391 if (fmode != GET_MODE (from))
5392 from = convert_to_mode (fmode, from, 0);
5394 if (imode != GET_MODE (to))
5395 target = gen_reg_rtx (imode);
5397 if (!maybe_emit_unop_insn (icode, target, from, UNKNOWN))
5399 delete_insns_since (last);
5400 continue;
5402 if (target != to)
5403 convert_move (to, target, 0);
5404 return true;
5408 return false;
5411 /* Report whether we have an instruction to perform the operation
5412 specified by CODE on operands of mode MODE. */
5414 have_insn_for (enum rtx_code code, enum machine_mode mode)
5416 return (code_to_optab[(int) code] != 0
5417 && (optab_handler (code_to_optab[(int) code], mode)->insn_code
5418 != CODE_FOR_nothing));
5421 /* Set all insn_code fields to CODE_FOR_nothing. */
5423 static void
5424 init_insn_codes (void)
5426 unsigned int i;
5428 for (i = 0; i < (unsigned int) OTI_MAX; i++)
5430 unsigned int j;
5431 optab op;
5433 op = &optab_table[i];
5434 for (j = 0; j < NUM_MACHINE_MODES; j++)
5435 optab_handler (op, j)->insn_code = CODE_FOR_nothing;
5437 for (i = 0; i < (unsigned int) COI_MAX; i++)
5439 unsigned int j, k;
5440 convert_optab op;
5442 op = &convert_optab_table[i];
5443 for (j = 0; j < NUM_MACHINE_MODES; j++)
5444 for (k = 0; k < NUM_MACHINE_MODES; k++)
5445 convert_optab_handler (op, j, k)->insn_code = CODE_FOR_nothing;
5449 /* Initialize OP's code to CODE, and write it into the code_to_optab table. */
5450 static inline void
5451 init_optab (optab op, enum rtx_code code)
5453 op->code = code;
5454 code_to_optab[(int) code] = op;
5457 /* Same, but fill in its code as CODE, and do _not_ write it into
5458 the code_to_optab table. */
5459 static inline void
5460 init_optabv (optab op, enum rtx_code code)
5462 op->code = code;
5465 /* Conversion optabs never go in the code_to_optab table. */
5466 static void
5467 init_convert_optab (convert_optab op, enum rtx_code code)
5469 op->code = code;
5472 /* Initialize the libfunc fields of an entire group of entries in some
5473 optab. Each entry is set equal to a string consisting of a leading
5474 pair of underscores followed by a generic operation name followed by
5475 a mode name (downshifted to lowercase) followed by a single character
5476 representing the number of operands for the given operation (which is
5477 usually one of the characters '2', '3', or '4').
5479 OPTABLE is the table in which libfunc fields are to be initialized.
5480 OPNAME is the generic (string) name of the operation.
5481 SUFFIX is the character which specifies the number of operands for
5482 the given generic operation.
5483 MODE is the mode to generate for.
5486 static void
5487 gen_libfunc (optab optable, const char *opname, int suffix, enum machine_mode mode)
5489 unsigned opname_len = strlen (opname);
5490 const char *mname = GET_MODE_NAME (mode);
5491 unsigned mname_len = strlen (mname);
5492 char *libfunc_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5493 char *p;
5494 const char *q;
5496 p = libfunc_name;
5497 *p++ = '_';
5498 *p++ = '_';
5499 for (q = opname; *q; )
5500 *p++ = *q++;
5501 for (q = mname; *q; q++)
5502 *p++ = TOLOWER (*q);
5503 *p++ = suffix;
5504 *p = '\0';
5506 set_optab_libfunc (optable, mode,
5507 ggc_alloc_string (libfunc_name, p - libfunc_name));
5510 /* Like gen_libfunc, but verify that integer operation is involved. */
5512 static void
5513 gen_int_libfunc (optab optable, const char *opname, char suffix,
5514 enum machine_mode mode)
5516 int maxsize = 2 * BITS_PER_WORD;
5518 if (GET_MODE_CLASS (mode) != MODE_INT)
5519 return;
5520 if (maxsize < LONG_LONG_TYPE_SIZE)
5521 maxsize = LONG_LONG_TYPE_SIZE;
5522 if (GET_MODE_CLASS (mode) != MODE_INT
5523 || mode < word_mode || GET_MODE_BITSIZE (mode) > maxsize)
5524 return;
5525 gen_libfunc (optable, opname, suffix, mode);
5528 /* Like gen_libfunc, but verify that FP and set decimal prefix if needed. */
5530 static void
5531 gen_fp_libfunc (optab optable, const char *opname, char suffix,
5532 enum machine_mode mode)
5534 char *dec_opname;
5536 if (GET_MODE_CLASS (mode) == MODE_FLOAT)
5537 gen_libfunc (optable, opname, suffix, mode);
5538 if (DECIMAL_FLOAT_MODE_P (mode))
5540 dec_opname = XALLOCAVEC (char, sizeof (DECIMAL_PREFIX) + strlen (opname));
5541 /* For BID support, change the name to have either a bid_ or dpd_ prefix
5542 depending on the low level floating format used. */
5543 memcpy (dec_opname, DECIMAL_PREFIX, sizeof (DECIMAL_PREFIX) - 1);
5544 strcpy (dec_opname + sizeof (DECIMAL_PREFIX) - 1, opname);
5545 gen_libfunc (optable, dec_opname, suffix, mode);
5549 /* Like gen_libfunc, but verify that fixed-point operation is involved. */
5551 static void
5552 gen_fixed_libfunc (optab optable, const char *opname, char suffix,
5553 enum machine_mode mode)
5555 if (!ALL_FIXED_POINT_MODE_P (mode))
5556 return;
5557 gen_libfunc (optable, opname, suffix, mode);
5560 /* Like gen_libfunc, but verify that signed fixed-point operation is
5561 involved. */
5563 static void
5564 gen_signed_fixed_libfunc (optab optable, const char *opname, char suffix,
5565 enum machine_mode mode)
5567 if (!SIGNED_FIXED_POINT_MODE_P (mode))
5568 return;
5569 gen_libfunc (optable, opname, suffix, mode);
5572 /* Like gen_libfunc, but verify that unsigned fixed-point operation is
5573 involved. */
5575 static void
5576 gen_unsigned_fixed_libfunc (optab optable, const char *opname, char suffix,
5577 enum machine_mode mode)
5579 if (!UNSIGNED_FIXED_POINT_MODE_P (mode))
5580 return;
5581 gen_libfunc (optable, opname, suffix, mode);
5584 /* Like gen_libfunc, but verify that FP or INT operation is involved. */
5586 static void
5587 gen_int_fp_libfunc (optab optable, const char *name, char suffix,
5588 enum machine_mode mode)
5590 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5591 gen_fp_libfunc (optable, name, suffix, mode);
5592 if (INTEGRAL_MODE_P (mode))
5593 gen_int_libfunc (optable, name, suffix, mode);
5596 /* Like gen_libfunc, but verify that FP or INT operation is involved
5597 and add 'v' suffix for integer operation. */
5599 static void
5600 gen_intv_fp_libfunc (optab optable, const char *name, char suffix,
5601 enum machine_mode mode)
5603 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5604 gen_fp_libfunc (optable, name, suffix, mode);
5605 if (GET_MODE_CLASS (mode) == MODE_INT)
5607 int len = strlen (name);
5608 char *v_name = XALLOCAVEC (char, len + 2);
5609 strcpy (v_name, name);
5610 v_name[len] = 'v';
5611 v_name[len + 1] = 0;
5612 gen_int_libfunc (optable, v_name, suffix, mode);
5616 /* Like gen_libfunc, but verify that FP or INT or FIXED operation is
5617 involved. */
5619 static void
5620 gen_int_fp_fixed_libfunc (optab optable, const char *name, char suffix,
5621 enum machine_mode mode)
5623 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5624 gen_fp_libfunc (optable, name, suffix, mode);
5625 if (INTEGRAL_MODE_P (mode))
5626 gen_int_libfunc (optable, name, suffix, mode);
5627 if (ALL_FIXED_POINT_MODE_P (mode))
5628 gen_fixed_libfunc (optable, name, suffix, mode);
5631 /* Like gen_libfunc, but verify that FP or INT or signed FIXED operation is
5632 involved. */
5634 static void
5635 gen_int_fp_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5636 enum machine_mode mode)
5638 if (DECIMAL_FLOAT_MODE_P (mode) || GET_MODE_CLASS (mode) == MODE_FLOAT)
5639 gen_fp_libfunc (optable, name, suffix, mode);
5640 if (INTEGRAL_MODE_P (mode))
5641 gen_int_libfunc (optable, name, suffix, mode);
5642 if (SIGNED_FIXED_POINT_MODE_P (mode))
5643 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5646 /* Like gen_libfunc, but verify that INT or FIXED operation is
5647 involved. */
5649 static void
5650 gen_int_fixed_libfunc (optab optable, const char *name, char suffix,
5651 enum machine_mode mode)
5653 if (INTEGRAL_MODE_P (mode))
5654 gen_int_libfunc (optable, name, suffix, mode);
5655 if (ALL_FIXED_POINT_MODE_P (mode))
5656 gen_fixed_libfunc (optable, name, suffix, mode);
5659 /* Like gen_libfunc, but verify that INT or signed FIXED operation is
5660 involved. */
5662 static void
5663 gen_int_signed_fixed_libfunc (optab optable, const char *name, char suffix,
5664 enum machine_mode mode)
5666 if (INTEGRAL_MODE_P (mode))
5667 gen_int_libfunc (optable, name, suffix, mode);
5668 if (SIGNED_FIXED_POINT_MODE_P (mode))
5669 gen_signed_fixed_libfunc (optable, name, suffix, mode);
5672 /* Like gen_libfunc, but verify that INT or unsigned FIXED operation is
5673 involved. */
5675 static void
5676 gen_int_unsigned_fixed_libfunc (optab optable, const char *name, char suffix,
5677 enum machine_mode mode)
5679 if (INTEGRAL_MODE_P (mode))
5680 gen_int_libfunc (optable, name, suffix, mode);
5681 if (UNSIGNED_FIXED_POINT_MODE_P (mode))
5682 gen_unsigned_fixed_libfunc (optable, name, suffix, mode);
5685 /* Initialize the libfunc fields of an entire group of entries of an
5686 inter-mode-class conversion optab. The string formation rules are
5687 similar to the ones for init_libfuncs, above, but instead of having
5688 a mode name and an operand count these functions have two mode names
5689 and no operand count. */
5691 static void
5692 gen_interclass_conv_libfunc (convert_optab tab,
5693 const char *opname,
5694 enum machine_mode tmode,
5695 enum machine_mode fmode)
5697 size_t opname_len = strlen (opname);
5698 size_t mname_len = 0;
5700 const char *fname, *tname;
5701 const char *q;
5702 char *libfunc_name, *suffix;
5703 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5704 char *p;
5706 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5707 depends on which underlying decimal floating point format is used. */
5708 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5710 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5712 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5713 nondec_name[0] = '_';
5714 nondec_name[1] = '_';
5715 memcpy (&nondec_name[2], opname, opname_len);
5716 nondec_suffix = nondec_name + opname_len + 2;
5718 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5719 dec_name[0] = '_';
5720 dec_name[1] = '_';
5721 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5722 memcpy (&dec_name[2+dec_len], opname, opname_len);
5723 dec_suffix = dec_name + dec_len + opname_len + 2;
5725 fname = GET_MODE_NAME (fmode);
5726 tname = GET_MODE_NAME (tmode);
5728 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5730 libfunc_name = dec_name;
5731 suffix = dec_suffix;
5733 else
5735 libfunc_name = nondec_name;
5736 suffix = nondec_suffix;
5739 p = suffix;
5740 for (q = fname; *q; p++, q++)
5741 *p = TOLOWER (*q);
5742 for (q = tname; *q; p++, q++)
5743 *p = TOLOWER (*q);
5745 *p = '\0';
5747 set_conv_libfunc (tab, tmode, fmode,
5748 ggc_alloc_string (libfunc_name, p - libfunc_name));
5751 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5752 int->fp conversion. */
5754 static void
5755 gen_int_to_fp_conv_libfunc (convert_optab tab,
5756 const char *opname,
5757 enum machine_mode tmode,
5758 enum machine_mode fmode)
5760 if (GET_MODE_CLASS (fmode) != MODE_INT)
5761 return;
5762 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5763 return;
5764 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5767 /* ufloat_optab is special by using floatun for FP and floatuns decimal fp
5768 naming scheme. */
5770 static void
5771 gen_ufloat_conv_libfunc (convert_optab tab,
5772 const char *opname ATTRIBUTE_UNUSED,
5773 enum machine_mode tmode,
5774 enum machine_mode fmode)
5776 if (DECIMAL_FLOAT_MODE_P (tmode))
5777 gen_int_to_fp_conv_libfunc (tab, "floatuns", tmode, fmode);
5778 else
5779 gen_int_to_fp_conv_libfunc (tab, "floatun", tmode, fmode);
5782 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5783 fp->int conversion. */
5785 static void
5786 gen_int_to_fp_nondecimal_conv_libfunc (convert_optab tab,
5787 const char *opname,
5788 enum machine_mode tmode,
5789 enum machine_mode fmode)
5791 if (GET_MODE_CLASS (fmode) != MODE_INT)
5792 return;
5793 if (GET_MODE_CLASS (tmode) != MODE_FLOAT)
5794 return;
5795 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5798 /* Same as gen_interclass_conv_libfunc but verify that we are producing
5799 fp->int conversion with no decimal floating point involved. */
5801 static void
5802 gen_fp_to_int_conv_libfunc (convert_optab tab,
5803 const char *opname,
5804 enum machine_mode tmode,
5805 enum machine_mode fmode)
5807 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5808 return;
5809 if (GET_MODE_CLASS (tmode) != MODE_INT)
5810 return;
5811 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5814 /* Initialize the libfunc fields of an of an intra-mode-class conversion optab.
5815 The string formation rules are
5816 similar to the ones for init_libfunc, above. */
5818 static void
5819 gen_intraclass_conv_libfunc (convert_optab tab, const char *opname,
5820 enum machine_mode tmode, enum machine_mode fmode)
5822 size_t opname_len = strlen (opname);
5823 size_t mname_len = 0;
5825 const char *fname, *tname;
5826 const char *q;
5827 char *nondec_name, *dec_name, *nondec_suffix, *dec_suffix;
5828 char *libfunc_name, *suffix;
5829 char *p;
5831 /* If this is a decimal conversion, add the current BID vs. DPD prefix that
5832 depends on which underlying decimal floating point format is used. */
5833 const size_t dec_len = sizeof (DECIMAL_PREFIX) - 1;
5835 mname_len = strlen (GET_MODE_NAME (tmode)) + strlen (GET_MODE_NAME (fmode));
5837 nondec_name = XALLOCAVEC (char, 2 + opname_len + mname_len + 1 + 1);
5838 nondec_name[0] = '_';
5839 nondec_name[1] = '_';
5840 memcpy (&nondec_name[2], opname, opname_len);
5841 nondec_suffix = nondec_name + opname_len + 2;
5843 dec_name = XALLOCAVEC (char, 2 + dec_len + opname_len + mname_len + 1 + 1);
5844 dec_name[0] = '_';
5845 dec_name[1] = '_';
5846 memcpy (&dec_name[2], DECIMAL_PREFIX, dec_len);
5847 memcpy (&dec_name[2 + dec_len], opname, opname_len);
5848 dec_suffix = dec_name + dec_len + opname_len + 2;
5850 fname = GET_MODE_NAME (fmode);
5851 tname = GET_MODE_NAME (tmode);
5853 if (DECIMAL_FLOAT_MODE_P(fmode) || DECIMAL_FLOAT_MODE_P(tmode))
5855 libfunc_name = dec_name;
5856 suffix = dec_suffix;
5858 else
5860 libfunc_name = nondec_name;
5861 suffix = nondec_suffix;
5864 p = suffix;
5865 for (q = fname; *q; p++, q++)
5866 *p = TOLOWER (*q);
5867 for (q = tname; *q; p++, q++)
5868 *p = TOLOWER (*q);
5870 *p++ = '2';
5871 *p = '\0';
5873 set_conv_libfunc (tab, tmode, fmode,
5874 ggc_alloc_string (libfunc_name, p - libfunc_name));
5877 /* Pick proper libcall for trunc_optab. We need to chose if we do
5878 truncation or extension and interclass or intraclass. */
5880 static void
5881 gen_trunc_conv_libfunc (convert_optab tab,
5882 const char *opname,
5883 enum machine_mode tmode,
5884 enum machine_mode fmode)
5886 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5887 return;
5888 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5889 return;
5890 if (tmode == fmode)
5891 return;
5893 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5894 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5895 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5897 if (GET_MODE_PRECISION (fmode) <= GET_MODE_PRECISION (tmode))
5898 return;
5900 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5901 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5902 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5903 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5906 /* Pick proper libcall for extend_optab. We need to chose if we do
5907 truncation or extension and interclass or intraclass. */
5909 static void
5910 gen_extend_conv_libfunc (convert_optab tab,
5911 const char *opname ATTRIBUTE_UNUSED,
5912 enum machine_mode tmode,
5913 enum machine_mode fmode)
5915 if (GET_MODE_CLASS (tmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (tmode))
5916 return;
5917 if (GET_MODE_CLASS (fmode) != MODE_FLOAT && !DECIMAL_FLOAT_MODE_P (fmode))
5918 return;
5919 if (tmode == fmode)
5920 return;
5922 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (fmode))
5923 || (GET_MODE_CLASS (fmode) == MODE_FLOAT && DECIMAL_FLOAT_MODE_P (tmode)))
5924 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5926 if (GET_MODE_PRECISION (fmode) > GET_MODE_PRECISION (tmode))
5927 return;
5929 if ((GET_MODE_CLASS (tmode) == MODE_FLOAT
5930 && GET_MODE_CLASS (fmode) == MODE_FLOAT)
5931 || (DECIMAL_FLOAT_MODE_P (fmode) && DECIMAL_FLOAT_MODE_P (tmode)))
5932 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5935 /* Pick proper libcall for fract_optab. We need to chose if we do
5936 interclass or intraclass. */
5938 static void
5939 gen_fract_conv_libfunc (convert_optab tab,
5940 const char *opname,
5941 enum machine_mode tmode,
5942 enum machine_mode fmode)
5944 if (tmode == fmode)
5945 return;
5946 if (!(ALL_FIXED_POINT_MODE_P (tmode) || ALL_FIXED_POINT_MODE_P (fmode)))
5947 return;
5949 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5950 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5951 else
5952 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5955 /* Pick proper libcall for fractuns_optab. */
5957 static void
5958 gen_fractuns_conv_libfunc (convert_optab tab,
5959 const char *opname,
5960 enum machine_mode tmode,
5961 enum machine_mode fmode)
5963 if (tmode == fmode)
5964 return;
5965 /* One mode must be a fixed-point mode, and the other must be an integer
5966 mode. */
5967 if (!((ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT)
5968 || (ALL_FIXED_POINT_MODE_P (fmode)
5969 && GET_MODE_CLASS (tmode) == MODE_INT)))
5970 return;
5972 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5975 /* Pick proper libcall for satfract_optab. We need to chose if we do
5976 interclass or intraclass. */
5978 static void
5979 gen_satfract_conv_libfunc (convert_optab tab,
5980 const char *opname,
5981 enum machine_mode tmode,
5982 enum machine_mode fmode)
5984 if (tmode == fmode)
5985 return;
5986 /* TMODE must be a fixed-point mode. */
5987 if (!ALL_FIXED_POINT_MODE_P (tmode))
5988 return;
5990 if (GET_MODE_CLASS (tmode) == GET_MODE_CLASS (fmode))
5991 gen_intraclass_conv_libfunc (tab, opname, tmode, fmode);
5992 else
5993 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
5996 /* Pick proper libcall for satfractuns_optab. */
5998 static void
5999 gen_satfractuns_conv_libfunc (convert_optab tab,
6000 const char *opname,
6001 enum machine_mode tmode,
6002 enum machine_mode fmode)
6004 if (tmode == fmode)
6005 return;
6006 /* TMODE must be a fixed-point mode, and FMODE must be an integer mode. */
6007 if (!(ALL_FIXED_POINT_MODE_P (tmode) && GET_MODE_CLASS (fmode) == MODE_INT))
6008 return;
6010 gen_interclass_conv_libfunc (tab, opname, tmode, fmode);
6013 /* A table of previously-created libfuncs, hashed by name. */
6014 static GTY ((param_is (union tree_node))) htab_t libfunc_decls;
6016 /* Hashtable callbacks for libfunc_decls. */
6018 static hashval_t
6019 libfunc_decl_hash (const void *entry)
6021 return htab_hash_string (IDENTIFIER_POINTER (DECL_NAME ((const_tree) entry)));
6024 static int
6025 libfunc_decl_eq (const void *entry1, const void *entry2)
6027 return DECL_NAME ((const_tree) entry1) == (const_tree) entry2;
6031 init_one_libfunc (const char *name)
6033 tree id, decl;
6034 void **slot;
6035 hashval_t hash;
6037 if (libfunc_decls == NULL)
6038 libfunc_decls = htab_create_ggc (37, libfunc_decl_hash,
6039 libfunc_decl_eq, NULL);
6041 /* See if we have already created a libfunc decl for this function. */
6042 id = get_identifier (name);
6043 hash = htab_hash_string (name);
6044 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, INSERT);
6045 decl = (tree) *slot;
6046 if (decl == NULL)
6048 /* Create a new decl, so that it can be passed to
6049 targetm.encode_section_info. */
6050 /* ??? We don't have any type information except for this is
6051 a function. Pretend this is "int foo()". */
6052 decl = build_decl (UNKNOWN_LOCATION,
6053 FUNCTION_DECL, get_identifier (name),
6054 build_function_type (integer_type_node, NULL_TREE));
6055 DECL_ARTIFICIAL (decl) = 1;
6056 DECL_EXTERNAL (decl) = 1;
6057 TREE_PUBLIC (decl) = 1;
6059 /* Zap the nonsensical SYMBOL_REF_DECL for this. What we're left with
6060 are the flags assigned by targetm.encode_section_info. */
6061 SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
6063 *slot = decl;
6065 return XEXP (DECL_RTL (decl), 0);
6068 /* Adjust the assembler name of libfunc NAME to ASMSPEC. */
6071 set_user_assembler_libfunc (const char *name, const char *asmspec)
6073 tree id, decl;
6074 void **slot;
6075 hashval_t hash;
6077 id = get_identifier (name);
6078 hash = htab_hash_string (name);
6079 slot = htab_find_slot_with_hash (libfunc_decls, id, hash, NO_INSERT);
6080 gcc_assert (slot);
6081 decl = (tree) *slot;
6082 set_user_assembler_name (decl, asmspec);
6083 return XEXP (DECL_RTL (decl), 0);
6086 /* Call this to reset the function entry for one optab (OPTABLE) in mode
6087 MODE to NAME, which should be either 0 or a string constant. */
6088 void
6089 set_optab_libfunc (optab optable, enum machine_mode mode, const char *name)
6091 rtx val;
6092 struct libfunc_entry e;
6093 struct libfunc_entry **slot;
6094 e.optab = (size_t) (optable - &optab_table[0]);
6095 e.mode1 = mode;
6096 e.mode2 = VOIDmode;
6098 if (name)
6099 val = init_one_libfunc (name);
6100 else
6101 val = 0;
6102 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6103 if (*slot == NULL)
6104 *slot = GGC_NEW (struct libfunc_entry);
6105 (*slot)->optab = (size_t) (optable - &optab_table[0]);
6106 (*slot)->mode1 = mode;
6107 (*slot)->mode2 = VOIDmode;
6108 (*slot)->libfunc = val;
6111 /* Call this to reset the function entry for one conversion optab
6112 (OPTABLE) from mode FMODE to mode TMODE to NAME, which should be
6113 either 0 or a string constant. */
6114 void
6115 set_conv_libfunc (convert_optab optable, enum machine_mode tmode,
6116 enum machine_mode fmode, const char *name)
6118 rtx val;
6119 struct libfunc_entry e;
6120 struct libfunc_entry **slot;
6121 e.optab = (size_t) (optable - &convert_optab_table[0]);
6122 e.mode1 = tmode;
6123 e.mode2 = fmode;
6125 if (name)
6126 val = init_one_libfunc (name);
6127 else
6128 val = 0;
6129 slot = (struct libfunc_entry **) htab_find_slot (libfunc_hash, &e, INSERT);
6130 if (*slot == NULL)
6131 *slot = GGC_NEW (struct libfunc_entry);
6132 (*slot)->optab = (size_t) (optable - &convert_optab_table[0]);
6133 (*slot)->mode1 = tmode;
6134 (*slot)->mode2 = fmode;
6135 (*slot)->libfunc = val;
6138 /* Call this to initialize the contents of the optabs
6139 appropriately for the current target machine. */
6141 void
6142 init_optabs (void)
6144 unsigned int i;
6145 enum machine_mode int_mode;
6146 static bool reinit;
6148 libfunc_hash = htab_create_ggc (10, hash_libfunc, eq_libfunc, NULL);
6149 /* Start by initializing all tables to contain CODE_FOR_nothing. */
6151 #ifdef HAVE_conditional_move
6152 for (i = 0; i < NUM_MACHINE_MODES; i++)
6153 movcc_gen_code[i] = CODE_FOR_nothing;
6154 #endif
6156 for (i = 0; i < NUM_MACHINE_MODES; i++)
6158 vcond_gen_code[i] = CODE_FOR_nothing;
6159 vcondu_gen_code[i] = CODE_FOR_nothing;
6162 #if GCC_VERSION >= 4000 && HAVE_DESIGNATED_INITIALIZERS
6163 /* We statically initialize the insn_codes with CODE_FOR_nothing. */
6164 if (reinit)
6165 init_insn_codes ();
6166 #else
6167 init_insn_codes ();
6168 #endif
6170 init_optab (add_optab, PLUS);
6171 init_optabv (addv_optab, PLUS);
6172 init_optab (sub_optab, MINUS);
6173 init_optabv (subv_optab, MINUS);
6174 init_optab (ssadd_optab, SS_PLUS);
6175 init_optab (usadd_optab, US_PLUS);
6176 init_optab (sssub_optab, SS_MINUS);
6177 init_optab (ussub_optab, US_MINUS);
6178 init_optab (smul_optab, MULT);
6179 init_optab (ssmul_optab, SS_MULT);
6180 init_optab (usmul_optab, US_MULT);
6181 init_optabv (smulv_optab, MULT);
6182 init_optab (smul_highpart_optab, UNKNOWN);
6183 init_optab (umul_highpart_optab, UNKNOWN);
6184 init_optab (smul_widen_optab, UNKNOWN);
6185 init_optab (umul_widen_optab, UNKNOWN);
6186 init_optab (usmul_widen_optab, UNKNOWN);
6187 init_optab (smadd_widen_optab, UNKNOWN);
6188 init_optab (umadd_widen_optab, UNKNOWN);
6189 init_optab (ssmadd_widen_optab, UNKNOWN);
6190 init_optab (usmadd_widen_optab, UNKNOWN);
6191 init_optab (smsub_widen_optab, UNKNOWN);
6192 init_optab (umsub_widen_optab, UNKNOWN);
6193 init_optab (ssmsub_widen_optab, UNKNOWN);
6194 init_optab (usmsub_widen_optab, UNKNOWN);
6195 init_optab (sdiv_optab, DIV);
6196 init_optab (ssdiv_optab, SS_DIV);
6197 init_optab (usdiv_optab, US_DIV);
6198 init_optabv (sdivv_optab, DIV);
6199 init_optab (sdivmod_optab, UNKNOWN);
6200 init_optab (udiv_optab, UDIV);
6201 init_optab (udivmod_optab, UNKNOWN);
6202 init_optab (smod_optab, MOD);
6203 init_optab (umod_optab, UMOD);
6204 init_optab (fmod_optab, UNKNOWN);
6205 init_optab (remainder_optab, UNKNOWN);
6206 init_optab (ftrunc_optab, UNKNOWN);
6207 init_optab (and_optab, AND);
6208 init_optab (ior_optab, IOR);
6209 init_optab (xor_optab, XOR);
6210 init_optab (ashl_optab, ASHIFT);
6211 init_optab (ssashl_optab, SS_ASHIFT);
6212 init_optab (usashl_optab, US_ASHIFT);
6213 init_optab (ashr_optab, ASHIFTRT);
6214 init_optab (lshr_optab, LSHIFTRT);
6215 init_optab (rotl_optab, ROTATE);
6216 init_optab (rotr_optab, ROTATERT);
6217 init_optab (smin_optab, SMIN);
6218 init_optab (smax_optab, SMAX);
6219 init_optab (umin_optab, UMIN);
6220 init_optab (umax_optab, UMAX);
6221 init_optab (pow_optab, UNKNOWN);
6222 init_optab (atan2_optab, UNKNOWN);
6224 /* These three have codes assigned exclusively for the sake of
6225 have_insn_for. */
6226 init_optab (mov_optab, SET);
6227 init_optab (movstrict_optab, STRICT_LOW_PART);
6228 init_optab (cbranch_optab, COMPARE);
6230 init_optab (cmov_optab, UNKNOWN);
6231 init_optab (cstore_optab, UNKNOWN);
6232 init_optab (ctrap_optab, UNKNOWN);
6234 init_optab (storent_optab, UNKNOWN);
6236 init_optab (cmp_optab, UNKNOWN);
6237 init_optab (ucmp_optab, UNKNOWN);
6239 init_optab (eq_optab, EQ);
6240 init_optab (ne_optab, NE);
6241 init_optab (gt_optab, GT);
6242 init_optab (ge_optab, GE);
6243 init_optab (lt_optab, LT);
6244 init_optab (le_optab, LE);
6245 init_optab (unord_optab, UNORDERED);
6247 init_optab (neg_optab, NEG);
6248 init_optab (ssneg_optab, SS_NEG);
6249 init_optab (usneg_optab, US_NEG);
6250 init_optabv (negv_optab, NEG);
6251 init_optab (abs_optab, ABS);
6252 init_optabv (absv_optab, ABS);
6253 init_optab (addcc_optab, UNKNOWN);
6254 init_optab (one_cmpl_optab, NOT);
6255 init_optab (bswap_optab, BSWAP);
6256 init_optab (ffs_optab, FFS);
6257 init_optab (clz_optab, CLZ);
6258 init_optab (ctz_optab, CTZ);
6259 init_optab (popcount_optab, POPCOUNT);
6260 init_optab (parity_optab, PARITY);
6261 init_optab (sqrt_optab, SQRT);
6262 init_optab (floor_optab, UNKNOWN);
6263 init_optab (ceil_optab, UNKNOWN);
6264 init_optab (round_optab, UNKNOWN);
6265 init_optab (btrunc_optab, UNKNOWN);
6266 init_optab (nearbyint_optab, UNKNOWN);
6267 init_optab (rint_optab, UNKNOWN);
6268 init_optab (sincos_optab, UNKNOWN);
6269 init_optab (sin_optab, UNKNOWN);
6270 init_optab (asin_optab, UNKNOWN);
6271 init_optab (cos_optab, UNKNOWN);
6272 init_optab (acos_optab, UNKNOWN);
6273 init_optab (exp_optab, UNKNOWN);
6274 init_optab (exp10_optab, UNKNOWN);
6275 init_optab (exp2_optab, UNKNOWN);
6276 init_optab (expm1_optab, UNKNOWN);
6277 init_optab (ldexp_optab, UNKNOWN);
6278 init_optab (scalb_optab, UNKNOWN);
6279 init_optab (significand_optab, UNKNOWN);
6280 init_optab (logb_optab, UNKNOWN);
6281 init_optab (ilogb_optab, UNKNOWN);
6282 init_optab (log_optab, UNKNOWN);
6283 init_optab (log10_optab, UNKNOWN);
6284 init_optab (log2_optab, UNKNOWN);
6285 init_optab (log1p_optab, UNKNOWN);
6286 init_optab (tan_optab, UNKNOWN);
6287 init_optab (atan_optab, UNKNOWN);
6288 init_optab (copysign_optab, UNKNOWN);
6289 init_optab (signbit_optab, UNKNOWN);
6291 init_optab (isinf_optab, UNKNOWN);
6293 init_optab (strlen_optab, UNKNOWN);
6294 init_optab (push_optab, UNKNOWN);
6296 init_optab (reduc_smax_optab, UNKNOWN);
6297 init_optab (reduc_umax_optab, UNKNOWN);
6298 init_optab (reduc_smin_optab, UNKNOWN);
6299 init_optab (reduc_umin_optab, UNKNOWN);
6300 init_optab (reduc_splus_optab, UNKNOWN);
6301 init_optab (reduc_uplus_optab, UNKNOWN);
6303 init_optab (ssum_widen_optab, UNKNOWN);
6304 init_optab (usum_widen_optab, UNKNOWN);
6305 init_optab (sdot_prod_optab, UNKNOWN);
6306 init_optab (udot_prod_optab, UNKNOWN);
6308 init_optab (vec_extract_optab, UNKNOWN);
6309 init_optab (vec_extract_even_optab, UNKNOWN);
6310 init_optab (vec_extract_odd_optab, UNKNOWN);
6311 init_optab (vec_interleave_high_optab, UNKNOWN);
6312 init_optab (vec_interleave_low_optab, UNKNOWN);
6313 init_optab (vec_set_optab, UNKNOWN);
6314 init_optab (vec_init_optab, UNKNOWN);
6315 init_optab (vec_shl_optab, UNKNOWN);
6316 init_optab (vec_shr_optab, UNKNOWN);
6317 init_optab (vec_realign_load_optab, UNKNOWN);
6318 init_optab (movmisalign_optab, UNKNOWN);
6319 init_optab (vec_widen_umult_hi_optab, UNKNOWN);
6320 init_optab (vec_widen_umult_lo_optab, UNKNOWN);
6321 init_optab (vec_widen_smult_hi_optab, UNKNOWN);
6322 init_optab (vec_widen_smult_lo_optab, UNKNOWN);
6323 init_optab (vec_unpacks_hi_optab, UNKNOWN);
6324 init_optab (vec_unpacks_lo_optab, UNKNOWN);
6325 init_optab (vec_unpacku_hi_optab, UNKNOWN);
6326 init_optab (vec_unpacku_lo_optab, UNKNOWN);
6327 init_optab (vec_unpacks_float_hi_optab, UNKNOWN);
6328 init_optab (vec_unpacks_float_lo_optab, UNKNOWN);
6329 init_optab (vec_unpacku_float_hi_optab, UNKNOWN);
6330 init_optab (vec_unpacku_float_lo_optab, UNKNOWN);
6331 init_optab (vec_pack_trunc_optab, UNKNOWN);
6332 init_optab (vec_pack_usat_optab, UNKNOWN);
6333 init_optab (vec_pack_ssat_optab, UNKNOWN);
6334 init_optab (vec_pack_ufix_trunc_optab, UNKNOWN);
6335 init_optab (vec_pack_sfix_trunc_optab, UNKNOWN);
6337 init_optab (powi_optab, UNKNOWN);
6339 /* Conversions. */
6340 init_convert_optab (sext_optab, SIGN_EXTEND);
6341 init_convert_optab (zext_optab, ZERO_EXTEND);
6342 init_convert_optab (trunc_optab, TRUNCATE);
6343 init_convert_optab (sfix_optab, FIX);
6344 init_convert_optab (ufix_optab, UNSIGNED_FIX);
6345 init_convert_optab (sfixtrunc_optab, UNKNOWN);
6346 init_convert_optab (ufixtrunc_optab, UNKNOWN);
6347 init_convert_optab (sfloat_optab, FLOAT);
6348 init_convert_optab (ufloat_optab, UNSIGNED_FLOAT);
6349 init_convert_optab (lrint_optab, UNKNOWN);
6350 init_convert_optab (lround_optab, UNKNOWN);
6351 init_convert_optab (lfloor_optab, UNKNOWN);
6352 init_convert_optab (lceil_optab, UNKNOWN);
6354 init_convert_optab (fract_optab, FRACT_CONVERT);
6355 init_convert_optab (fractuns_optab, UNSIGNED_FRACT_CONVERT);
6356 init_convert_optab (satfract_optab, SAT_FRACT);
6357 init_convert_optab (satfractuns_optab, UNSIGNED_SAT_FRACT);
6359 for (i = 0; i < NUM_MACHINE_MODES; i++)
6361 movmem_optab[i] = CODE_FOR_nothing;
6362 cmpstr_optab[i] = CODE_FOR_nothing;
6363 cmpstrn_optab[i] = CODE_FOR_nothing;
6364 cmpmem_optab[i] = CODE_FOR_nothing;
6365 setmem_optab[i] = CODE_FOR_nothing;
6367 sync_add_optab[i] = CODE_FOR_nothing;
6368 sync_sub_optab[i] = CODE_FOR_nothing;
6369 sync_ior_optab[i] = CODE_FOR_nothing;
6370 sync_and_optab[i] = CODE_FOR_nothing;
6371 sync_xor_optab[i] = CODE_FOR_nothing;
6372 sync_nand_optab[i] = CODE_FOR_nothing;
6373 sync_old_add_optab[i] = CODE_FOR_nothing;
6374 sync_old_sub_optab[i] = CODE_FOR_nothing;
6375 sync_old_ior_optab[i] = CODE_FOR_nothing;
6376 sync_old_and_optab[i] = CODE_FOR_nothing;
6377 sync_old_xor_optab[i] = CODE_FOR_nothing;
6378 sync_old_nand_optab[i] = CODE_FOR_nothing;
6379 sync_new_add_optab[i] = CODE_FOR_nothing;
6380 sync_new_sub_optab[i] = CODE_FOR_nothing;
6381 sync_new_ior_optab[i] = CODE_FOR_nothing;
6382 sync_new_and_optab[i] = CODE_FOR_nothing;
6383 sync_new_xor_optab[i] = CODE_FOR_nothing;
6384 sync_new_nand_optab[i] = CODE_FOR_nothing;
6385 sync_compare_and_swap[i] = CODE_FOR_nothing;
6386 sync_lock_test_and_set[i] = CODE_FOR_nothing;
6387 sync_lock_release[i] = CODE_FOR_nothing;
6389 reload_in_optab[i] = reload_out_optab[i] = CODE_FOR_nothing;
6392 /* Fill in the optabs with the insns we support. */
6393 init_all_optabs ();
6395 /* Initialize the optabs with the names of the library functions. */
6396 add_optab->libcall_basename = "add";
6397 add_optab->libcall_suffix = '3';
6398 add_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6399 addv_optab->libcall_basename = "add";
6400 addv_optab->libcall_suffix = '3';
6401 addv_optab->libcall_gen = gen_intv_fp_libfunc;
6402 ssadd_optab->libcall_basename = "ssadd";
6403 ssadd_optab->libcall_suffix = '3';
6404 ssadd_optab->libcall_gen = gen_signed_fixed_libfunc;
6405 usadd_optab->libcall_basename = "usadd";
6406 usadd_optab->libcall_suffix = '3';
6407 usadd_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6408 sub_optab->libcall_basename = "sub";
6409 sub_optab->libcall_suffix = '3';
6410 sub_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6411 subv_optab->libcall_basename = "sub";
6412 subv_optab->libcall_suffix = '3';
6413 subv_optab->libcall_gen = gen_intv_fp_libfunc;
6414 sssub_optab->libcall_basename = "sssub";
6415 sssub_optab->libcall_suffix = '3';
6416 sssub_optab->libcall_gen = gen_signed_fixed_libfunc;
6417 ussub_optab->libcall_basename = "ussub";
6418 ussub_optab->libcall_suffix = '3';
6419 ussub_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6420 smul_optab->libcall_basename = "mul";
6421 smul_optab->libcall_suffix = '3';
6422 smul_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6423 smulv_optab->libcall_basename = "mul";
6424 smulv_optab->libcall_suffix = '3';
6425 smulv_optab->libcall_gen = gen_intv_fp_libfunc;
6426 ssmul_optab->libcall_basename = "ssmul";
6427 ssmul_optab->libcall_suffix = '3';
6428 ssmul_optab->libcall_gen = gen_signed_fixed_libfunc;
6429 usmul_optab->libcall_basename = "usmul";
6430 usmul_optab->libcall_suffix = '3';
6431 usmul_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6432 sdiv_optab->libcall_basename = "div";
6433 sdiv_optab->libcall_suffix = '3';
6434 sdiv_optab->libcall_gen = gen_int_fp_signed_fixed_libfunc;
6435 sdivv_optab->libcall_basename = "divv";
6436 sdivv_optab->libcall_suffix = '3';
6437 sdivv_optab->libcall_gen = gen_int_libfunc;
6438 ssdiv_optab->libcall_basename = "ssdiv";
6439 ssdiv_optab->libcall_suffix = '3';
6440 ssdiv_optab->libcall_gen = gen_signed_fixed_libfunc;
6441 udiv_optab->libcall_basename = "udiv";
6442 udiv_optab->libcall_suffix = '3';
6443 udiv_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6444 usdiv_optab->libcall_basename = "usdiv";
6445 usdiv_optab->libcall_suffix = '3';
6446 usdiv_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6447 sdivmod_optab->libcall_basename = "divmod";
6448 sdivmod_optab->libcall_suffix = '4';
6449 sdivmod_optab->libcall_gen = gen_int_libfunc;
6450 udivmod_optab->libcall_basename = "udivmod";
6451 udivmod_optab->libcall_suffix = '4';
6452 udivmod_optab->libcall_gen = gen_int_libfunc;
6453 smod_optab->libcall_basename = "mod";
6454 smod_optab->libcall_suffix = '3';
6455 smod_optab->libcall_gen = gen_int_libfunc;
6456 umod_optab->libcall_basename = "umod";
6457 umod_optab->libcall_suffix = '3';
6458 umod_optab->libcall_gen = gen_int_libfunc;
6459 ftrunc_optab->libcall_basename = "ftrunc";
6460 ftrunc_optab->libcall_suffix = '2';
6461 ftrunc_optab->libcall_gen = gen_fp_libfunc;
6462 and_optab->libcall_basename = "and";
6463 and_optab->libcall_suffix = '3';
6464 and_optab->libcall_gen = gen_int_libfunc;
6465 ior_optab->libcall_basename = "ior";
6466 ior_optab->libcall_suffix = '3';
6467 ior_optab->libcall_gen = gen_int_libfunc;
6468 xor_optab->libcall_basename = "xor";
6469 xor_optab->libcall_suffix = '3';
6470 xor_optab->libcall_gen = gen_int_libfunc;
6471 ashl_optab->libcall_basename = "ashl";
6472 ashl_optab->libcall_suffix = '3';
6473 ashl_optab->libcall_gen = gen_int_fixed_libfunc;
6474 ssashl_optab->libcall_basename = "ssashl";
6475 ssashl_optab->libcall_suffix = '3';
6476 ssashl_optab->libcall_gen = gen_signed_fixed_libfunc;
6477 usashl_optab->libcall_basename = "usashl";
6478 usashl_optab->libcall_suffix = '3';
6479 usashl_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6480 ashr_optab->libcall_basename = "ashr";
6481 ashr_optab->libcall_suffix = '3';
6482 ashr_optab->libcall_gen = gen_int_signed_fixed_libfunc;
6483 lshr_optab->libcall_basename = "lshr";
6484 lshr_optab->libcall_suffix = '3';
6485 lshr_optab->libcall_gen = gen_int_unsigned_fixed_libfunc;
6486 smin_optab->libcall_basename = "min";
6487 smin_optab->libcall_suffix = '3';
6488 smin_optab->libcall_gen = gen_int_fp_libfunc;
6489 smax_optab->libcall_basename = "max";
6490 smax_optab->libcall_suffix = '3';
6491 smax_optab->libcall_gen = gen_int_fp_libfunc;
6492 umin_optab->libcall_basename = "umin";
6493 umin_optab->libcall_suffix = '3';
6494 umin_optab->libcall_gen = gen_int_libfunc;
6495 umax_optab->libcall_basename = "umax";
6496 umax_optab->libcall_suffix = '3';
6497 umax_optab->libcall_gen = gen_int_libfunc;
6498 neg_optab->libcall_basename = "neg";
6499 neg_optab->libcall_suffix = '2';
6500 neg_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6501 ssneg_optab->libcall_basename = "ssneg";
6502 ssneg_optab->libcall_suffix = '2';
6503 ssneg_optab->libcall_gen = gen_signed_fixed_libfunc;
6504 usneg_optab->libcall_basename = "usneg";
6505 usneg_optab->libcall_suffix = '2';
6506 usneg_optab->libcall_gen = gen_unsigned_fixed_libfunc;
6507 negv_optab->libcall_basename = "neg";
6508 negv_optab->libcall_suffix = '2';
6509 negv_optab->libcall_gen = gen_intv_fp_libfunc;
6510 one_cmpl_optab->libcall_basename = "one_cmpl";
6511 one_cmpl_optab->libcall_suffix = '2';
6512 one_cmpl_optab->libcall_gen = gen_int_libfunc;
6513 ffs_optab->libcall_basename = "ffs";
6514 ffs_optab->libcall_suffix = '2';
6515 ffs_optab->libcall_gen = gen_int_libfunc;
6516 clz_optab->libcall_basename = "clz";
6517 clz_optab->libcall_suffix = '2';
6518 clz_optab->libcall_gen = gen_int_libfunc;
6519 ctz_optab->libcall_basename = "ctz";
6520 ctz_optab->libcall_suffix = '2';
6521 ctz_optab->libcall_gen = gen_int_libfunc;
6522 popcount_optab->libcall_basename = "popcount";
6523 popcount_optab->libcall_suffix = '2';
6524 popcount_optab->libcall_gen = gen_int_libfunc;
6525 parity_optab->libcall_basename = "parity";
6526 parity_optab->libcall_suffix = '2';
6527 parity_optab->libcall_gen = gen_int_libfunc;
6529 /* Comparison libcalls for integers MUST come in pairs,
6530 signed/unsigned. */
6531 cmp_optab->libcall_basename = "cmp";
6532 cmp_optab->libcall_suffix = '2';
6533 cmp_optab->libcall_gen = gen_int_fp_fixed_libfunc;
6534 ucmp_optab->libcall_basename = "ucmp";
6535 ucmp_optab->libcall_suffix = '2';
6536 ucmp_optab->libcall_gen = gen_int_libfunc;
6538 /* EQ etc are floating point only. */
6539 eq_optab->libcall_basename = "eq";
6540 eq_optab->libcall_suffix = '2';
6541 eq_optab->libcall_gen = gen_fp_libfunc;
6542 ne_optab->libcall_basename = "ne";
6543 ne_optab->libcall_suffix = '2';
6544 ne_optab->libcall_gen = gen_fp_libfunc;
6545 gt_optab->libcall_basename = "gt";
6546 gt_optab->libcall_suffix = '2';
6547 gt_optab->libcall_gen = gen_fp_libfunc;
6548 ge_optab->libcall_basename = "ge";
6549 ge_optab->libcall_suffix = '2';
6550 ge_optab->libcall_gen = gen_fp_libfunc;
6551 lt_optab->libcall_basename = "lt";
6552 lt_optab->libcall_suffix = '2';
6553 lt_optab->libcall_gen = gen_fp_libfunc;
6554 le_optab->libcall_basename = "le";
6555 le_optab->libcall_suffix = '2';
6556 le_optab->libcall_gen = gen_fp_libfunc;
6557 unord_optab->libcall_basename = "unord";
6558 unord_optab->libcall_suffix = '2';
6559 unord_optab->libcall_gen = gen_fp_libfunc;
6561 powi_optab->libcall_basename = "powi";
6562 powi_optab->libcall_suffix = '2';
6563 powi_optab->libcall_gen = gen_fp_libfunc;
6565 /* Conversions. */
6566 sfloat_optab->libcall_basename = "float";
6567 sfloat_optab->libcall_gen = gen_int_to_fp_conv_libfunc;
6568 ufloat_optab->libcall_gen = gen_ufloat_conv_libfunc;
6569 sfix_optab->libcall_basename = "fix";
6570 sfix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6571 ufix_optab->libcall_basename = "fixuns";
6572 ufix_optab->libcall_gen = gen_fp_to_int_conv_libfunc;
6573 lrint_optab->libcall_basename = "lrint";
6574 lrint_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6575 lround_optab->libcall_basename = "lround";
6576 lround_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6577 lfloor_optab->libcall_basename = "lfloor";
6578 lfloor_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6579 lceil_optab->libcall_basename = "lceil";
6580 lceil_optab->libcall_gen = gen_int_to_fp_nondecimal_conv_libfunc;
6582 /* trunc_optab is also used for FLOAT_EXTEND. */
6583 sext_optab->libcall_basename = "extend";
6584 sext_optab->libcall_gen = gen_extend_conv_libfunc;
6585 trunc_optab->libcall_basename = "trunc";
6586 trunc_optab->libcall_gen = gen_trunc_conv_libfunc;
6588 /* Conversions for fixed-point modes and other modes. */
6589 fract_optab->libcall_basename = "fract";
6590 fract_optab->libcall_gen = gen_fract_conv_libfunc;
6591 satfract_optab->libcall_basename = "satfract";
6592 satfract_optab->libcall_gen = gen_satfract_conv_libfunc;
6593 fractuns_optab->libcall_basename = "fractuns";
6594 fractuns_optab->libcall_gen = gen_fractuns_conv_libfunc;
6595 satfractuns_optab->libcall_basename = "satfractuns";
6596 satfractuns_optab->libcall_gen = gen_satfractuns_conv_libfunc;
6598 /* The ffs function operates on `int'. Fall back on it if we do not
6599 have a libgcc2 function for that width. */
6600 if (INT_TYPE_SIZE < BITS_PER_WORD)
6602 int_mode = mode_for_size (INT_TYPE_SIZE, MODE_INT, 0);
6603 set_optab_libfunc (ffs_optab, mode_for_size (INT_TYPE_SIZE, MODE_INT, 0),
6604 "ffs");
6607 /* Explicitly initialize the bswap libfuncs since we need them to be
6608 valid for things other than word_mode. */
6609 set_optab_libfunc (bswap_optab, SImode, "__bswapsi2");
6610 set_optab_libfunc (bswap_optab, DImode, "__bswapdi2");
6612 /* Use cabs for double complex abs, since systems generally have cabs.
6613 Don't define any libcall for float complex, so that cabs will be used. */
6614 if (complex_double_type_node)
6615 set_optab_libfunc (abs_optab, TYPE_MODE (complex_double_type_node), "cabs");
6617 abort_libfunc = init_one_libfunc ("abort");
6618 memcpy_libfunc = init_one_libfunc ("memcpy");
6619 memmove_libfunc = init_one_libfunc ("memmove");
6620 memcmp_libfunc = init_one_libfunc ("memcmp");
6621 memset_libfunc = init_one_libfunc ("memset");
6622 setbits_libfunc = init_one_libfunc ("__setbits");
6624 #ifndef DONT_USE_BUILTIN_SETJMP
6625 setjmp_libfunc = init_one_libfunc ("__builtin_setjmp");
6626 longjmp_libfunc = init_one_libfunc ("__builtin_longjmp");
6627 #else
6628 setjmp_libfunc = init_one_libfunc ("setjmp");
6629 longjmp_libfunc = init_one_libfunc ("longjmp");
6630 #endif
6631 unwind_sjlj_register_libfunc = init_one_libfunc ("_Unwind_SjLj_Register");
6632 unwind_sjlj_unregister_libfunc
6633 = init_one_libfunc ("_Unwind_SjLj_Unregister");
6635 /* For function entry/exit instrumentation. */
6636 profile_function_entry_libfunc
6637 = init_one_libfunc ("__cyg_profile_func_enter");
6638 profile_function_exit_libfunc
6639 = init_one_libfunc ("__cyg_profile_func_exit");
6641 gcov_flush_libfunc = init_one_libfunc ("__gcov_flush");
6643 /* Allow the target to add more libcalls or rename some, etc. */
6644 targetm.init_libfuncs ();
6646 reinit = true;
6649 /* Print information about the current contents of the optabs on
6650 STDERR. */
6652 void
6653 debug_optab_libfuncs (void)
6655 int i;
6656 int j;
6657 int k;
6659 /* Dump the arithmetic optabs. */
6660 for (i = 0; i != (int) OTI_MAX; i++)
6661 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6663 optab o;
6664 rtx l;
6666 o = &optab_table[i];
6667 l = optab_libfunc (o, (enum machine_mode) j);
6668 if (l)
6670 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6671 fprintf (stderr, "%s\t%s:\t%s\n",
6672 GET_RTX_NAME (o->code),
6673 GET_MODE_NAME (j),
6674 XSTR (l, 0));
6678 /* Dump the conversion optabs. */
6679 for (i = 0; i < (int) COI_MAX; ++i)
6680 for (j = 0; j < NUM_MACHINE_MODES; ++j)
6681 for (k = 0; k < NUM_MACHINE_MODES; ++k)
6683 convert_optab o;
6684 rtx l;
6686 o = &convert_optab_table[i];
6687 l = convert_optab_libfunc (o, (enum machine_mode) j,
6688 (enum machine_mode) k);
6689 if (l)
6691 gcc_assert (GET_CODE (l) == SYMBOL_REF);
6692 fprintf (stderr, "%s\t%s\t%s:\t%s\n",
6693 GET_RTX_NAME (o->code),
6694 GET_MODE_NAME (j),
6695 GET_MODE_NAME (k),
6696 XSTR (l, 0));
6702 /* Generate insns to trap with code TCODE if OP1 and OP2 satisfy condition
6703 CODE. Return 0 on failure. */
6706 gen_cond_trap (enum rtx_code code, rtx op1, rtx op2, rtx tcode)
6708 enum machine_mode mode = GET_MODE (op1);
6709 enum insn_code icode;
6710 rtx insn;
6711 rtx trap_rtx;
6713 if (mode == VOIDmode)
6714 return 0;
6716 icode = optab_handler (ctrap_optab, mode)->insn_code;
6717 if (icode == CODE_FOR_nothing)
6718 return 0;
6720 /* Some targets only accept a zero trap code. */
6721 if (insn_data[icode].operand[3].predicate
6722 && !insn_data[icode].operand[3].predicate (tcode, VOIDmode))
6723 return 0;
6725 do_pending_stack_adjust ();
6726 start_sequence ();
6727 prepare_cmp_insn (op1, op2, code, NULL_RTX, false, OPTAB_DIRECT,
6728 &trap_rtx, &mode);
6729 if (!trap_rtx)
6730 insn = NULL_RTX;
6731 else
6732 insn = GEN_FCN (icode) (trap_rtx, XEXP (trap_rtx, 0), XEXP (trap_rtx, 1),
6733 tcode);
6735 /* If that failed, then give up. */
6736 if (insn == 0)
6738 end_sequence ();
6739 return 0;
6742 emit_insn (insn);
6743 insn = get_insns ();
6744 end_sequence ();
6745 return insn;
6748 /* Return rtx code for TCODE. Use UNSIGNEDP to select signed
6749 or unsigned operation code. */
6751 static enum rtx_code
6752 get_rtx_code (enum tree_code tcode, bool unsignedp)
6754 enum rtx_code code;
6755 switch (tcode)
6757 case EQ_EXPR:
6758 code = EQ;
6759 break;
6760 case NE_EXPR:
6761 code = NE;
6762 break;
6763 case LT_EXPR:
6764 code = unsignedp ? LTU : LT;
6765 break;
6766 case LE_EXPR:
6767 code = unsignedp ? LEU : LE;
6768 break;
6769 case GT_EXPR:
6770 code = unsignedp ? GTU : GT;
6771 break;
6772 case GE_EXPR:
6773 code = unsignedp ? GEU : GE;
6774 break;
6776 case UNORDERED_EXPR:
6777 code = UNORDERED;
6778 break;
6779 case ORDERED_EXPR:
6780 code = ORDERED;
6781 break;
6782 case UNLT_EXPR:
6783 code = UNLT;
6784 break;
6785 case UNLE_EXPR:
6786 code = UNLE;
6787 break;
6788 case UNGT_EXPR:
6789 code = UNGT;
6790 break;
6791 case UNGE_EXPR:
6792 code = UNGE;
6793 break;
6794 case UNEQ_EXPR:
6795 code = UNEQ;
6796 break;
6797 case LTGT_EXPR:
6798 code = LTGT;
6799 break;
6801 default:
6802 gcc_unreachable ();
6804 return code;
6807 /* Return comparison rtx for COND. Use UNSIGNEDP to select signed or
6808 unsigned operators. Do not generate compare instruction. */
6810 static rtx
6811 vector_compare_rtx (tree cond, bool unsignedp, enum insn_code icode)
6813 enum rtx_code rcode;
6814 tree t_op0, t_op1;
6815 rtx rtx_op0, rtx_op1;
6817 /* This is unlikely. While generating VEC_COND_EXPR, auto vectorizer
6818 ensures that condition is a relational operation. */
6819 gcc_assert (COMPARISON_CLASS_P (cond));
6821 rcode = get_rtx_code (TREE_CODE (cond), unsignedp);
6822 t_op0 = TREE_OPERAND (cond, 0);
6823 t_op1 = TREE_OPERAND (cond, 1);
6825 /* Expand operands. */
6826 rtx_op0 = expand_expr (t_op0, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op0)),
6827 EXPAND_STACK_PARM);
6828 rtx_op1 = expand_expr (t_op1, NULL_RTX, TYPE_MODE (TREE_TYPE (t_op1)),
6829 EXPAND_STACK_PARM);
6831 if (!insn_data[icode].operand[4].predicate (rtx_op0, GET_MODE (rtx_op0))
6832 && GET_MODE (rtx_op0) != VOIDmode)
6833 rtx_op0 = force_reg (GET_MODE (rtx_op0), rtx_op0);
6835 if (!insn_data[icode].operand[5].predicate (rtx_op1, GET_MODE (rtx_op1))
6836 && GET_MODE (rtx_op1) != VOIDmode)
6837 rtx_op1 = force_reg (GET_MODE (rtx_op1), rtx_op1);
6839 return gen_rtx_fmt_ee (rcode, VOIDmode, rtx_op0, rtx_op1);
6842 /* Return insn code for VEC_COND_EXPR EXPR. */
6844 static inline enum insn_code
6845 get_vcond_icode (tree expr, enum machine_mode mode)
6847 enum insn_code icode = CODE_FOR_nothing;
6849 if (TYPE_UNSIGNED (TREE_TYPE (expr)))
6850 icode = vcondu_gen_code[mode];
6851 else
6852 icode = vcond_gen_code[mode];
6853 return icode;
6856 /* Return TRUE iff, appropriate vector insns are available
6857 for vector cond expr expr in VMODE mode. */
6859 bool
6860 expand_vec_cond_expr_p (tree expr, enum machine_mode vmode)
6862 if (get_vcond_icode (expr, vmode) == CODE_FOR_nothing)
6863 return false;
6864 return true;
6867 /* Generate insns for VEC_COND_EXPR. */
6870 expand_vec_cond_expr (tree vec_cond_expr, rtx target)
6872 enum insn_code icode;
6873 rtx comparison, rtx_op1, rtx_op2, cc_op0, cc_op1;
6874 enum machine_mode mode = TYPE_MODE (TREE_TYPE (vec_cond_expr));
6875 bool unsignedp = TYPE_UNSIGNED (TREE_TYPE (vec_cond_expr));
6877 icode = get_vcond_icode (vec_cond_expr, mode);
6878 if (icode == CODE_FOR_nothing)
6879 return 0;
6881 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
6882 target = gen_reg_rtx (mode);
6884 /* Get comparison rtx. First expand both cond expr operands. */
6885 comparison = vector_compare_rtx (TREE_OPERAND (vec_cond_expr, 0),
6886 unsignedp, icode);
6887 cc_op0 = XEXP (comparison, 0);
6888 cc_op1 = XEXP (comparison, 1);
6889 /* Expand both operands and force them in reg, if required. */
6890 rtx_op1 = expand_normal (TREE_OPERAND (vec_cond_expr, 1));
6891 if (!insn_data[icode].operand[1].predicate (rtx_op1, mode)
6892 && mode != VOIDmode)
6893 rtx_op1 = force_reg (mode, rtx_op1);
6895 rtx_op2 = expand_normal (TREE_OPERAND (vec_cond_expr, 2));
6896 if (!insn_data[icode].operand[2].predicate (rtx_op2, mode)
6897 && mode != VOIDmode)
6898 rtx_op2 = force_reg (mode, rtx_op2);
6900 /* Emit instruction! */
6901 emit_insn (GEN_FCN (icode) (target, rtx_op1, rtx_op2,
6902 comparison, cc_op0, cc_op1));
6904 return target;
6908 /* This is an internal subroutine of the other compare_and_swap expanders.
6909 MEM, OLD_VAL and NEW_VAL are as you'd expect for a compare-and-swap
6910 operation. TARGET is an optional place to store the value result of
6911 the operation. ICODE is the particular instruction to expand. Return
6912 the result of the operation. */
6914 static rtx
6915 expand_val_compare_and_swap_1 (rtx mem, rtx old_val, rtx new_val,
6916 rtx target, enum insn_code icode)
6918 enum machine_mode mode = GET_MODE (mem);
6919 rtx insn;
6921 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
6922 target = gen_reg_rtx (mode);
6924 if (GET_MODE (old_val) != VOIDmode && GET_MODE (old_val) != mode)
6925 old_val = convert_modes (mode, GET_MODE (old_val), old_val, 1);
6926 if (!insn_data[icode].operand[2].predicate (old_val, mode))
6927 old_val = force_reg (mode, old_val);
6929 if (GET_MODE (new_val) != VOIDmode && GET_MODE (new_val) != mode)
6930 new_val = convert_modes (mode, GET_MODE (new_val), new_val, 1);
6931 if (!insn_data[icode].operand[3].predicate (new_val, mode))
6932 new_val = force_reg (mode, new_val);
6934 insn = GEN_FCN (icode) (target, mem, old_val, new_val);
6935 if (insn == NULL_RTX)
6936 return NULL_RTX;
6937 emit_insn (insn);
6939 return target;
6942 /* Expand a compare-and-swap operation and return its value. */
6945 expand_val_compare_and_swap (rtx mem, rtx old_val, rtx new_val, rtx target)
6947 enum machine_mode mode = GET_MODE (mem);
6948 enum insn_code icode = sync_compare_and_swap[mode];
6950 if (icode == CODE_FOR_nothing)
6951 return NULL_RTX;
6953 return expand_val_compare_and_swap_1 (mem, old_val, new_val, target, icode);
6956 /* Helper function to find the MODE_CC set in a sync_compare_and_swap
6957 pattern. */
6959 static void
6960 find_cc_set (rtx x, const_rtx pat, void *data)
6962 if (REG_P (x) && GET_MODE_CLASS (GET_MODE (x)) == MODE_CC
6963 && GET_CODE (pat) == SET)
6965 rtx *p_cc_reg = (rtx *) data;
6966 gcc_assert (!*p_cc_reg);
6967 *p_cc_reg = x;
6971 /* Expand a compare-and-swap operation and store true into the result if
6972 the operation was successful and false otherwise. Return the result.
6973 Unlike other routines, TARGET is not optional. */
6976 expand_bool_compare_and_swap (rtx mem, rtx old_val, rtx new_val, rtx target)
6978 enum machine_mode mode = GET_MODE (mem);
6979 enum insn_code icode;
6980 rtx subtarget, seq, cc_reg;
6982 /* If the target supports a compare-and-swap pattern that simultaneously
6983 sets some flag for success, then use it. Otherwise use the regular
6984 compare-and-swap and follow that immediately with a compare insn. */
6985 icode = sync_compare_and_swap[mode];
6986 if (icode == CODE_FOR_nothing)
6987 return NULL_RTX;
6991 start_sequence ();
6992 subtarget = expand_val_compare_and_swap_1 (mem, old_val, new_val,
6993 NULL_RTX, icode);
6994 cc_reg = NULL_RTX;
6995 if (subtarget == NULL_RTX)
6997 end_sequence ();
6998 return NULL_RTX;
7001 if (have_insn_for (COMPARE, CCmode))
7002 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7003 seq = get_insns ();
7004 end_sequence ();
7006 /* We might be comparing against an old value. Try again. :-( */
7007 if (!cc_reg && MEM_P (old_val))
7009 seq = NULL_RTX;
7010 old_val = force_reg (mode, old_val);
7013 while (!seq);
7015 emit_insn (seq);
7016 if (cc_reg)
7017 return emit_store_flag_force (target, EQ, cc_reg, const0_rtx, VOIDmode, 0, 1);
7018 else
7019 return emit_store_flag_force (target, EQ, subtarget, old_val, VOIDmode, 1, 1);
7022 /* This is a helper function for the other atomic operations. This function
7023 emits a loop that contains SEQ that iterates until a compare-and-swap
7024 operation at the end succeeds. MEM is the memory to be modified. SEQ is
7025 a set of instructions that takes a value from OLD_REG as an input and
7026 produces a value in NEW_REG as an output. Before SEQ, OLD_REG will be
7027 set to the current contents of MEM. After SEQ, a compare-and-swap will
7028 attempt to update MEM with NEW_REG. The function returns true when the
7029 loop was generated successfully. */
7031 static bool
7032 expand_compare_and_swap_loop (rtx mem, rtx old_reg, rtx new_reg, rtx seq)
7034 enum machine_mode mode = GET_MODE (mem);
7035 enum insn_code icode;
7036 rtx label, cmp_reg, subtarget, cc_reg;
7038 /* The loop we want to generate looks like
7040 cmp_reg = mem;
7041 label:
7042 old_reg = cmp_reg;
7043 seq;
7044 cmp_reg = compare-and-swap(mem, old_reg, new_reg)
7045 if (cmp_reg != old_reg)
7046 goto label;
7048 Note that we only do the plain load from memory once. Subsequent
7049 iterations use the value loaded by the compare-and-swap pattern. */
7051 label = gen_label_rtx ();
7052 cmp_reg = gen_reg_rtx (mode);
7054 emit_move_insn (cmp_reg, mem);
7055 emit_label (label);
7056 emit_move_insn (old_reg, cmp_reg);
7057 if (seq)
7058 emit_insn (seq);
7060 /* If the target supports a compare-and-swap pattern that simultaneously
7061 sets some flag for success, then use it. Otherwise use the regular
7062 compare-and-swap and follow that immediately with a compare insn. */
7063 icode = sync_compare_and_swap[mode];
7064 if (icode == CODE_FOR_nothing)
7065 return false;
7067 subtarget = expand_val_compare_and_swap_1 (mem, old_reg, new_reg,
7068 cmp_reg, icode);
7069 if (subtarget == NULL_RTX)
7070 return false;
7072 cc_reg = NULL_RTX;
7073 if (have_insn_for (COMPARE, CCmode))
7074 note_stores (PATTERN (get_last_insn ()), find_cc_set, &cc_reg);
7075 if (cc_reg)
7077 cmp_reg = cc_reg;
7078 old_reg = const0_rtx;
7080 else
7082 if (subtarget != cmp_reg)
7083 emit_move_insn (cmp_reg, subtarget);
7086 /* ??? Mark this jump predicted not taken? */
7087 emit_cmp_and_jump_insns (cmp_reg, old_reg, NE, const0_rtx, GET_MODE (cmp_reg), 1,
7088 label);
7089 return true;
7092 /* This function generates the atomic operation MEM CODE= VAL. In this
7093 case, we do not care about any resulting value. Returns NULL if we
7094 cannot generate the operation. */
7097 expand_sync_operation (rtx mem, rtx val, enum rtx_code code)
7099 enum machine_mode mode = GET_MODE (mem);
7100 enum insn_code icode;
7101 rtx insn;
7103 /* Look to see if the target supports the operation directly. */
7104 switch (code)
7106 case PLUS:
7107 icode = sync_add_optab[mode];
7108 break;
7109 case IOR:
7110 icode = sync_ior_optab[mode];
7111 break;
7112 case XOR:
7113 icode = sync_xor_optab[mode];
7114 break;
7115 case AND:
7116 icode = sync_and_optab[mode];
7117 break;
7118 case NOT:
7119 icode = sync_nand_optab[mode];
7120 break;
7122 case MINUS:
7123 icode = sync_sub_optab[mode];
7124 if (icode == CODE_FOR_nothing || CONST_INT_P (val))
7126 icode = sync_add_optab[mode];
7127 if (icode != CODE_FOR_nothing)
7129 val = expand_simple_unop (mode, NEG, val, NULL_RTX, 1);
7130 code = PLUS;
7133 break;
7135 default:
7136 gcc_unreachable ();
7139 /* Generate the direct operation, if present. */
7140 if (icode != CODE_FOR_nothing)
7142 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7143 val = convert_modes (mode, GET_MODE (val), val, 1);
7144 if (!insn_data[icode].operand[1].predicate (val, mode))
7145 val = force_reg (mode, val);
7147 insn = GEN_FCN (icode) (mem, val);
7148 if (insn)
7150 emit_insn (insn);
7151 return const0_rtx;
7155 /* Failing that, generate a compare-and-swap loop in which we perform the
7156 operation with normal arithmetic instructions. */
7157 if (sync_compare_and_swap[mode] != CODE_FOR_nothing)
7159 rtx t0 = gen_reg_rtx (mode), t1;
7161 start_sequence ();
7163 t1 = t0;
7164 if (code == NOT)
7166 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
7167 true, OPTAB_LIB_WIDEN);
7168 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
7170 else
7171 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX,
7172 true, OPTAB_LIB_WIDEN);
7173 insn = get_insns ();
7174 end_sequence ();
7176 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
7177 return const0_rtx;
7180 return NULL_RTX;
7183 /* This function generates the atomic operation MEM CODE= VAL. In this
7184 case, we do care about the resulting value: if AFTER is true then
7185 return the value MEM holds after the operation, if AFTER is false
7186 then return the value MEM holds before the operation. TARGET is an
7187 optional place for the result value to be stored. */
7190 expand_sync_fetch_operation (rtx mem, rtx val, enum rtx_code code,
7191 bool after, rtx target)
7193 enum machine_mode mode = GET_MODE (mem);
7194 enum insn_code old_code, new_code, icode;
7195 bool compensate;
7196 rtx insn;
7198 /* Look to see if the target supports the operation directly. */
7199 switch (code)
7201 case PLUS:
7202 old_code = sync_old_add_optab[mode];
7203 new_code = sync_new_add_optab[mode];
7204 break;
7205 case IOR:
7206 old_code = sync_old_ior_optab[mode];
7207 new_code = sync_new_ior_optab[mode];
7208 break;
7209 case XOR:
7210 old_code = sync_old_xor_optab[mode];
7211 new_code = sync_new_xor_optab[mode];
7212 break;
7213 case AND:
7214 old_code = sync_old_and_optab[mode];
7215 new_code = sync_new_and_optab[mode];
7216 break;
7217 case NOT:
7218 old_code = sync_old_nand_optab[mode];
7219 new_code = sync_new_nand_optab[mode];
7220 break;
7222 case MINUS:
7223 old_code = sync_old_sub_optab[mode];
7224 new_code = sync_new_sub_optab[mode];
7225 if ((old_code == CODE_FOR_nothing && new_code == CODE_FOR_nothing)
7226 || CONST_INT_P (val))
7228 old_code = sync_old_add_optab[mode];
7229 new_code = sync_new_add_optab[mode];
7230 if (old_code != CODE_FOR_nothing || new_code != CODE_FOR_nothing)
7232 val = expand_simple_unop (mode, NEG, val, NULL_RTX, 1);
7233 code = PLUS;
7236 break;
7238 default:
7239 gcc_unreachable ();
7242 /* If the target does supports the proper new/old operation, great. But
7243 if we only support the opposite old/new operation, check to see if we
7244 can compensate. In the case in which the old value is supported, then
7245 we can always perform the operation again with normal arithmetic. In
7246 the case in which the new value is supported, then we can only handle
7247 this in the case the operation is reversible. */
7248 compensate = false;
7249 if (after)
7251 icode = new_code;
7252 if (icode == CODE_FOR_nothing)
7254 icode = old_code;
7255 if (icode != CODE_FOR_nothing)
7256 compensate = true;
7259 else
7261 icode = old_code;
7262 if (icode == CODE_FOR_nothing
7263 && (code == PLUS || code == MINUS || code == XOR))
7265 icode = new_code;
7266 if (icode != CODE_FOR_nothing)
7267 compensate = true;
7271 /* If we found something supported, great. */
7272 if (icode != CODE_FOR_nothing)
7274 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
7275 target = gen_reg_rtx (mode);
7277 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7278 val = convert_modes (mode, GET_MODE (val), val, 1);
7279 if (!insn_data[icode].operand[2].predicate (val, mode))
7280 val = force_reg (mode, val);
7282 insn = GEN_FCN (icode) (target, mem, val);
7283 if (insn)
7285 emit_insn (insn);
7287 /* If we need to compensate for using an operation with the
7288 wrong return value, do so now. */
7289 if (compensate)
7291 if (!after)
7293 if (code == PLUS)
7294 code = MINUS;
7295 else if (code == MINUS)
7296 code = PLUS;
7299 if (code == NOT)
7301 target = expand_simple_binop (mode, AND, target, val,
7302 NULL_RTX, true,
7303 OPTAB_LIB_WIDEN);
7304 target = expand_simple_unop (mode, code, target,
7305 NULL_RTX, true);
7307 else
7308 target = expand_simple_binop (mode, code, target, val,
7309 NULL_RTX, true,
7310 OPTAB_LIB_WIDEN);
7313 return target;
7317 /* Failing that, generate a compare-and-swap loop in which we perform the
7318 operation with normal arithmetic instructions. */
7319 if (sync_compare_and_swap[mode] != CODE_FOR_nothing)
7321 rtx t0 = gen_reg_rtx (mode), t1;
7323 if (!target || !register_operand (target, mode))
7324 target = gen_reg_rtx (mode);
7326 start_sequence ();
7328 if (!after)
7329 emit_move_insn (target, t0);
7330 t1 = t0;
7331 if (code == NOT)
7333 t1 = expand_simple_binop (mode, AND, t1, val, NULL_RTX,
7334 true, OPTAB_LIB_WIDEN);
7335 t1 = expand_simple_unop (mode, code, t1, NULL_RTX, true);
7337 else
7338 t1 = expand_simple_binop (mode, code, t1, val, NULL_RTX,
7339 true, OPTAB_LIB_WIDEN);
7340 if (after)
7341 emit_move_insn (target, t1);
7343 insn = get_insns ();
7344 end_sequence ();
7346 if (t1 != NULL && expand_compare_and_swap_loop (mem, t0, t1, insn))
7347 return target;
7350 return NULL_RTX;
7353 /* This function expands a test-and-set operation. Ideally we atomically
7354 store VAL in MEM and return the previous value in MEM. Some targets
7355 may not support this operation and only support VAL with the constant 1;
7356 in this case while the return value will be 0/1, but the exact value
7357 stored in MEM is target defined. TARGET is an option place to stick
7358 the return value. */
7361 expand_sync_lock_test_and_set (rtx mem, rtx val, rtx target)
7363 enum machine_mode mode = GET_MODE (mem);
7364 enum insn_code icode;
7365 rtx insn;
7367 /* If the target supports the test-and-set directly, great. */
7368 icode = sync_lock_test_and_set[mode];
7369 if (icode != CODE_FOR_nothing)
7371 if (!target || !insn_data[icode].operand[0].predicate (target, mode))
7372 target = gen_reg_rtx (mode);
7374 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7375 val = convert_modes (mode, GET_MODE (val), val, 1);
7376 if (!insn_data[icode].operand[2].predicate (val, mode))
7377 val = force_reg (mode, val);
7379 insn = GEN_FCN (icode) (target, mem, val);
7380 if (insn)
7382 emit_insn (insn);
7383 return target;
7387 /* Otherwise, use a compare-and-swap loop for the exchange. */
7388 if (sync_compare_and_swap[mode] != CODE_FOR_nothing)
7390 if (!target || !register_operand (target, mode))
7391 target = gen_reg_rtx (mode);
7392 if (GET_MODE (val) != VOIDmode && GET_MODE (val) != mode)
7393 val = convert_modes (mode, GET_MODE (val), val, 1);
7394 if (expand_compare_and_swap_loop (mem, target, val, NULL_RTX))
7395 return target;
7398 return NULL_RTX;
7401 #include "gt-optabs.h"